Merge "JOID-46 Add script to create the external network and update the gw_mac for...
[joid.git] / ci / 02-maasdeploy.sh
1 #!/bin/bash
2 #placeholder for deployment script.
3 set -ex
4
5 virtinstall=0
6
7 case "$1" in
8     'intelpod5' )
9         cp maas/intel/pod5/deployment.yaml ./deployment.yaml
10         ;;
11     'intelpod6' )
12         cp maas/intel/pod6/deployment.yaml ./deployment.yaml
13         ;;
14     'orangepod2' )
15         cp maas/orange/pod2/deployment.yaml ./deployment.yaml
16         ;;
17     'attvirpod1' )
18         cp maas/att/virpod1/deployment.yaml ./deployment.yaml
19         ;;
20     'juniperpod1' )
21         cp maas/juniper/pod1/deployment.yaml ./deployment.yaml
22         ;;
23     'cengnlynxpod1' )
24         cp maas/cengn_lynx/pod1/deployment.yaml ./deployment.yaml
25         ;;
26     * )
27         virtinstall=1
28         cp maas/default/deployment.yaml ./deployment.yaml
29         ;;
30 esac
31
32 #make sure no password asked during the deployment. 
33
34 echo "$USER ALL=(ALL) NOPASSWD:ALL" > 90-joid-init
35
36 if [ -e /etc/sudoers.d/90-joid-init ]; then
37     sudo cp /etc/sudoers.d/90-joid-init 91-joid-init
38     cat 90-joid-init >> 91-joid-init
39     sudo chown root:root 91-joid-init
40     sudo mv 91-joid-init /etc/sudoers.d/
41 else
42     sudo chown root:root 90-joid-init
43     sudo mv 90-joid-init /etc/sudoers.d/
44 fi
45
46 echo "... Deployment of maas Started ...."
47
48 if [ ! -e $HOME/.ssh/id_rsa ]; then
49     ssh-keygen -N '' -f $HOME/.ssh/id_rsa
50 fi
51
52 if [ ! -e /var/lib/libvirt/images ]; then
53
54     sudo apt-get install libvirt-bin -y
55     sudo adduser ubuntu libvirtd
56     sudo virsh pool-define-as default --type dir --target /var/lib/libvirt/images/
57     sudo virsh pool-start default
58     sudo virsh pool-autostart default
59
60 fi
61
62 sudo apt-add-repository ppa:maas-deployers/stable -y
63 sudo apt-add-repository ppa:juju/stable -y
64 sudo apt-add-repository ppa:maas/stable -y
65 sudo apt-get update -y
66 sudo apt-get install openssh-server git maas-deployer juju juju-deployer maas-cli python-pip -y
67 sudo pip install shyaml
68 juju init -f
69
70 cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys
71
72 if [ "$virtinstall" -eq 1 ]; then
73     sudo virsh net-dumpxml default > default-net-org.xml
74     sudo sed -i '/dhcp/d' default-net-org.xml
75     sudo sed -i '/range/d' default-net-org.xml
76     sudo virsh net-define default-net-org.xml
77     sudo virsh net-destroy default
78     sudo virsh net-start default
79 fi
80
81 #Below function will mark the interfaces in Auto mode to enbled by MAAS
82 enableautomode() {
83     listofnodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
84     for nodes in $listofnodes
85     do
86         maas maas interface link-subnet $nodes $1  mode=$2 subnet=$3
87     done
88 }
89
90 #Below function will mark the interfaces in Auto mode to enbled by MAAS
91 # using hostname of the node added into MAAS
92
93 enableautomodebyname() {
94     if [ ! -z "$4" ]; then
95         for i in `seq 1 7`;
96         do
97             nodes=`maas maas nodes list hostname=node$i-$4 | grep system_id | cut -d '"' -f 4`
98             if [ ! -z "$nodes" ]; then
99                 maas maas interface link-subnet $nodes $1  mode=$2 subnet=$3
100             fi
101        done
102     fi
103 }
104
105 #Below function will create vlan and update interface with the new vlan
106 # will return the vlan id created
107 crvlanupdsubnet() {
108     newvlanid=`maas maas vlans create $2 name=$3 vid=$4 | grep resource | cut -d '/' -f 6 `
109     maas maas subnet update $5 vlan=$newvlanid
110     eval "$1"="'$newvlanid'"
111 }
112
113 #Below function will create interface with new vlan and bind to physical interface
114 crnodevlanint() {
115     listofnodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
116
117     for nodes in $listofnodes
118     do
119         parentid=`maas maas interface read $nodes eth2 | grep interfaces | cut -d '/' -f 8`
120         maas maas interfaces create-vlan $nodes vlan=$1 parent=$parentid
121      done
122  }
123
124 sudo maas-deployer -c deployment.yaml -d --force
125
126 sudo chown $USER:$USER environments.yaml
127
128 echo "... Deployment of maas finish ...."
129
130 maas_ip=`grep " ip_address" deployment.yaml | cut -d " "  -f 10`
131 apikey=`grep maas-oauth: environments.yaml | cut -d "'" -f 2`
132 maas login maas http://${maas_ip}/MAAS/api/1.0 ${apikey}
133 maas maas boot-source update 1 url="http://maas.ubuntu.com/images/ephemeral-v2/daily/"
134 #maas maas boot-source-selections create 1 os="ubuntu" release="precise" arches="amd64" subarches="*" labels="*"
135 maas maas boot-resources import
136 maas maas sshkeys new key="`cat $HOME/.ssh/id_rsa.pub`"
137
138 #adding compute and control nodes VM to MAAS for deployment purpose.
139 if [ "$virtinstall" -eq 1 ]; then
140     # create two more VMs to do the deployment.
141     sudo virt-install --connect qemu:///system --name node1-control --ram 8192 --vcpus 4 --disk size=120,format=qcow2,bus=virtio,io=native,pool=default --network bridge=virbr0,model=virtio --network bridge=virbr0,model=virtio --boot network,hd,menu=off --noautoconsole --vnc --print-xml | tee node1-control
142
143     sudo virt-install --connect qemu:///system --name node2-compute --ram 8192 --vcpus 4 --disk size=120,format=qcow2,bus=virtio,io=native,pool=default --network bridge=virbr0,model=virtio --network bridge=virbr0,model=virtio --boot network,hd,menu=off --noautoconsole --vnc --print-xml | tee node2-compute
144
145     node1controlmac=`grep  "mac address" node1-control | head -1 | cut -d "'" -f 2`
146     node2computemac=`grep  "mac address" node2-compute | head -1 | cut -d "'" -f 2`
147
148     sudo virsh -c qemu:///system define --file node1-control
149     sudo virsh -c qemu:///system define --file node2-compute
150
151     maas maas tags new name='control'
152     maas maas tags new name='compute'
153
154     controlnodeid=`maas maas nodes new autodetect_nodegroup='yes' name='node1-control' tags='control' hostname='node1-control' power_type='virsh' mac_addresses=$node1controlmac power_parameters_power_address='qemu+ssh://'$USER'@192.168.122.1/system' architecture='amd64/generic' power_parameters_power_id='node1-control' | grep system_id | cut -d '"' -f 4 `
155
156     maas maas tag update-nodes control add=$controlnodeid
157
158     computenodeid=`maas maas nodes new autodetect_nodegroup='yes' name='node2-compute' tags='compute' hostname='node2-compute' power_type='virsh' mac_addresses=$node2computemac power_parameters_power_address='qemu+ssh://'$USER'@192.168.122.1/system' architecture='amd64/generic' power_parameters_power_id='node2-compute' | grep system_id | cut -d '"' -f 4 `
159
160     maas maas tag update-nodes compute add=$computenodeid
161
162 fi
163
164 # Enable vlan interfaces with maas
165 case "$1" in
166     'intelpod5' )
167         maas refresh
168         crvlanupdsubnet vlan721 1 "DataNetwork" 721 2 || true
169         crvlanupdsubnet vlan724 2 "PublicNetwork" 724 3 || true
170         crnodevlanint $vlan721 || true
171         crnodevlanint $vlan724 || true
172         enableautomodebyname eth2.721 AUTO "10.4.9.0/24" compute || true
173         enableautomodebyname eth2.721 AUTO "10.4.9.0/24" control || true
174         ;;
175     'intelpod6' )
176         maas refresh
177         enableautomodebyname eth1 AUTO "10.4.9.0/24" compute || true
178         enableautomodebyname eth1 AUTO "10.4.9.0/24" control || true
179         ;;
180     'orangepod2' )
181         maas refresh
182         enableautomodebyname eth1 DHCP "192.168.2.0/24" compute || true
183         enableautomodebyname eth5 AUTO "192.168.12.0/24" compute || true
184         enableautomodebyname eth1 DHCP "192.168.2.0/24" control || true
185         enableautomodebyname eth3 AUTO "192.168.12.0/24" control || true
186         ;;
187     'attvirpod1' )
188         ;;
189     'juniperpod1' )
190         ;;
191     'cengnlynxpod1' )
192         maas refresh
193         crvlanupdsubnet vlan1201 1 "DataNetwork" 1201 2 || true
194         crvlanupdsubnet vlan1202 2 "PublicNetwork" 1202 3 || true
195         crnodevlanint $vlan1201 || true
196         crnodevlanint $vlan1202 || true
197         enableautomode eth2.1201 AUTO "172.16.121.3/24" compute || true
198         enableautomode eth2.1201 AUTO "172.16.121.3/24" control || true
199         ;;
200 esac
201
202 echo " .... MAAS deployment finished successfully ...."
203
204 #echo "... Deployment of opnfv release Started ...."
205 #python deploy.py $maas_ip
206