remove exit 1
[joid.git] / ci / 02-maasdeploy.sh
1 #!/bin/bash
2 #placeholder for deployment script.
3 set -ex
4
5 virtinstall=0
6
7 #install the packages needed
8 sudo apt-add-repository ppa:maas-deployers/stable -y
9 sudo apt-add-repository ppa:juju/stable -y
10 sudo apt-add-repository ppa:maas/stable -y
11 sudo apt-add-repository cloud-archive:liberty -y
12 sudo apt-get update -y
13 sudo apt-get dist-upgrade -y
14 sudo apt-get install openssh-server git maas-deployer juju juju-deployer maas-cli python-pip python-openstackclient gsutil -y
15
16 cp maas/deployment.yaml ./deployment.yaml
17
18 #first parameter should be custom and second should be either 
19 # absolute location of file (including file name) or url of the 
20 # file to download.
21
22 if [ "$1" == "custom" ]; then
23     if [ -e $2 ]; then
24         cp $2 ./labconfig.yaml || true
25         python deploy.py
26     else
27         wget $2 -t 3 -T 10 -O ./labconfig.yaml || true
28         count=`wc -l labconfig.yaml  | cut -d " " -f 1`
29
30         if [ $count -lt 10 ]; then
31             rm -rf labconfig.yaml
32         else
33             python deploy.py
34         fi
35     fi
36
37     if [ ! -e ./labconfig.yaml ]; then
38         virtinstall=1
39         cp ../labconfig/default/labconfig.yaml ./
40         python deploy.py
41     fi
42 fi
43
44 case "$1" in
45     'intelpod5' )
46         cp ../labconfig/intel/pod5/labconfig.yaml ./
47         #to be removed later once converted for all labs.
48         python deploy.py
49         ;;
50     'intelpod6' )
51         cp ../labconfig/intel/pod6/labconfig.yaml ./
52         #to be removed later once converted for all labs.
53         python deploy.py
54         ;;
55     'intelpod9' )
56         cp ../labconfig/intel/pod6/labconfig.yaml ./
57         #to be removed later once converted for all labs.
58         python deploy.py
59         ;;
60     'orangepod1' )
61         cp maas/orange/pod1/deployment.yaml ./deployment.yaml
62         ;;
63     'orangepod2' )
64         cp maas/orange/pod2/deployment.yaml ./deployment.yaml
65         ;;
66     'attvirpod1' )
67         cp maas/att/virpod1/deployment.yaml ./deployment.yaml
68         ;;
69     'juniperpod1' )
70         cp maas/juniper/pod1/deployment.yaml ./deployment.yaml
71         ;;
72     'cengnlynxpod1' )
73         cp maas/cengn_lynx/pod1/deployment.yaml ./deployment.yaml
74         ;;
75     'custom' )
76         cp maas/custom/deployment.yaml ./deployment.yaml
77         ;;
78     * )
79         virtinstall=1
80         ./cleanvm.sh
81         cp maas/default/deployment.yaml ./deployment.yaml
82         ;;
83 esac
84
85
86 #make sure no password asked during the deployment.
87
88 echo "$USER ALL=(ALL) NOPASSWD:ALL" > 90-joid-init
89
90 if [ -e /etc/sudoers.d/90-joid-init ]; then
91     sudo cp /etc/sudoers.d/90-joid-init 91-joid-init
92     sudo chown $USER:$USER 91-joid-init
93     sudo chmod 660 91-joid-init
94     sudo cat 90-joid-init >> 91-joid-init
95     sudo chown root:root 91-joid-init
96     sudo mv 91-joid-init /etc/sudoers.d/
97 else
98     sudo chown root:root 90-joid-init
99     sudo mv 90-joid-init /etc/sudoers.d/
100 fi
101
102 echo "... Deployment of maas Started ...."
103
104 if [ ! -e $HOME/.ssh/id_rsa ]; then
105     ssh-keygen -N '' -f $HOME/.ssh/id_rsa
106 fi
107
108 #define the pool and try to start even though its already exist.
109 # For fresh install this may or may not there.
110
111 sudo apt-get install libvirt-bin -y
112 sudo adduser $USER libvirtd
113 sudo virsh pool-define-as default --type dir --target /var/lib/libvirt/images/ || true
114 sudo virsh pool-start default || true
115 sudo virsh pool-autostart default || true
116
117 # To avoid problem between apiclient/maas_client and apiclient from google
118 # we remove the package google-api-python-client from yardstick installer
119 if [ $(pip list |grep google-api-python-client |wc -l) == 1 ]; then
120     sudo pip uninstall google-api-python-client
121 fi
122
123 sudo pip install shyaml
124 juju init -f
125
126 cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys
127
128 if [ "$virtinstall" -eq 1 ]; then
129     sudo virsh net-dumpxml default > default-net-org.xml
130     sudo sed -i '/dhcp/d' default-net-org.xml
131     sudo sed -i '/range/d' default-net-org.xml
132     sudo virsh net-define default-net-org.xml
133     sudo virsh net-destroy default
134     sudo virsh net-start default
135 fi
136
137 #Below function will mark the interfaces in Auto mode to enbled by MAAS
138 enableautomode() {
139     listofnodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
140     for nodes in $listofnodes
141     do
142         maas maas interface link-subnet $nodes $1  mode=$2 subnet=$3
143     done
144 }
145
146 #Below function will mark the interfaces in Auto mode to enbled by MAAS
147 # using hostname of the node added into MAAS
148
149 enableautomodebyname() {
150     if [ ! -z "$4" ]; then
151         for i in `seq 1 7`;
152         do
153             nodes=`maas maas nodes list hostname=node$i-$4 | grep system_id | cut -d '"' -f 4`
154             if [ ! -z "$nodes" ]; then
155                 maas maas interface link-subnet $nodes $1  mode=$2 subnet=$3
156             fi
157        done
158     fi
159 }
160
161 #Below function will create vlan and update interface with the new vlan
162 # will return the vlan id created
163 crvlanupdsubnet() {
164     newvlanid=`maas maas vlans create $2 name=$3 vid=$4 | grep resource | cut -d '/' -f 6 `
165     maas maas subnet update $5 vlan=$newvlanid
166     eval "$1"="'$newvlanid'"
167 }
168
169 #Below function will create interface with new vlan and bind to physical interface
170 crnodevlanint() {
171     listofnodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
172
173     for nodes in $listofnodes
174     do
175         parentid=`maas maas interface read $nodes $2 | grep interfaces | cut -d '/' -f 8`
176         maas maas interfaces create-vlan $nodes vlan=$1 parent=$parentid
177      done
178  }
179
180 #convert labconfig file to deployment.yaml to consume by MAAS.
181 #python deploy.py
182
183 #just make sure the ssh keys added into maas for the current user
184 sed --i "s@/home/ubuntu@$HOME@g" ./deployment.yaml
185 sed --i "s@qemu+ssh://ubuntu@qemu+ssh://$USER@g" ./deployment.yaml
186
187 sudo maas-deployer -c deployment.yaml -d --force
188
189 sudo chown $USER:$USER environments.yaml
190
191 echo "... Deployment of maas finish ...."
192
193 maas_ip=`grep " ip_address" deployment.yaml | cut -d ':' -f 2 | sed -e 's/ //'`
194 apikey=`grep maas-oauth: environments.yaml | cut -d "'" -f 2`
195 maas login maas http://${maas_ip}/MAAS/api/1.0 ${apikey}
196 maas maas sshkeys new key="`cat $HOME/.ssh/id_rsa.pub`"
197
198 #Added the Qtip public to run the Qtip test after install on bare metal nodes.
199 maas maas sshkeys new key="`cat ./maas/sshkeys/QtipKey.pub`"
200
201 #adding compute and control nodes VM to MAAS for deployment purpose.
202 if [ "$virtinstall" -eq 1 ]; then
203     # create two more VMs to do the deployment.
204     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
205
206     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
207
208     sudo virt-install --connect qemu:///system --name node5-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 node5-compute
209
210     node1controlmac=`grep  "mac address" node1-control | head -1 | cut -d "'" -f 2`
211     node2computemac=`grep  "mac address" node2-compute | head -1 | cut -d "'" -f 2`
212     node5computemac=`grep  "mac address" node5-compute | head -1 | cut -d "'" -f 2`
213
214     sudo virsh -c qemu:///system define --file node1-control
215     sudo virsh -c qemu:///system define --file node2-compute
216     sudo virsh -c qemu:///system define --file node5-compute
217
218     maas maas tags new name='control'
219     maas maas tags new name='compute'
220
221     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 `
222
223     maas maas tag update-nodes control add=$controlnodeid
224
225     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 `
226
227     maas maas tag update-nodes compute add=$computenodeid
228
229     computenodeid=`maas maas nodes new autodetect_nodegroup='yes' name='node5-compute' tags='compute' hostname='node5-compute' power_type='virsh' mac_addresses=$node5computemac power_parameters_power_address='qemu+ssh://'$USER'@192.168.122.1/system' architecture='amd64/generic' power_parameters_power_id='node5-compute' | grep system_id | cut -d '"' -f 4 `
230
231     maas maas tag update-nodes compute add=$computenodeid
232 fi
233
234 # Enable vlan interfaces with maas
235 case "$1" in
236     'intelpod5' )
237         maas refresh
238         enableautomodebyname eth4 AUTO "10.5.12.0/24" compute || true
239         enableautomodebyname eth4 AUTO "10.5.12.0/24" control || true
240         ;;
241     'intelpod6' )
242         maas refresh
243         enableautomodebyname eth4 AUTO "10.6.12.0/24" compute || true
244         enableautomodebyname eth4 AUTO "10.6.12.0/24" control || true
245         ;;
246     'intelpod9' )
247         maas refresh
248         crvlanupdsubnet vlan902 1 "DataNetwork" 902 2 || true
249         crvlanupdsubnet vlan905 2 "PublicNetwork" 905 3 || true
250         crnodevlanint $vlan902 eth0 || true
251         crnodevlanint $vlan905 eth1 || true
252         enableautomodebyname eth0.902 AUTO "10.9.12.0/24" compute || true
253         enableautomodebyname eth1.905 AUTO "10.9.15.0/24" compute || true
254         enableautomodebyname eth0.902 AUTO "10.9.12.0/24" control || true
255         enableautomodebyname eth1.905 AUTO "10.9.15.0/24" control || true
256         ;;
257     'orangepod1' )
258         maas refresh
259         enableautomodebyname eth2 DHCP "192.168.21.0/24" compute || true
260         enableautomodebyname eth3 AUTO "192.168.11.0/24" compute || true
261         enableautomodebyname eth2 DHCP "192.168.21.0/24" control || true
262         enableautomodebyname eth3 AUTO "192.168.11.0/24" control || true
263         ;;
264     'orangepod2' )
265         maas refresh
266         enableautomodebyname eth4 DHCP "192.168.22.0/24" compute || true
267         enableautomodebyname eth5 AUTO "192.168.12.0/24" compute || true
268         enableautomodebyname eth2 DHCP "192.168.22.0/24" control || true
269         enableautomodebyname eth3 AUTO "192.168.12.0/24" control || true
270         ;;
271     'attvirpod1' )
272         enableautomodebyname eth1 AUTO "192.168.10.0/24" control || true
273         ;;
274     'juniperpod1' )
275         ;;
276     'cengnlynxpod1' )
277         maas refresh
278         crvlanupdsubnet vlan1201 1 "DataNetwork" 1201 2 || true
279         crvlanupdsubnet vlan1202 2 "PublicNetwork" 1202 3 || true
280         crnodevlanint $vlan1201 eth1 || true
281         crnodevlanint $vlan1202 eth1 || true
282         enableautomode eth1.1201 AUTO "172.16.121.3/24" compute || true
283         enableautomode eth1.1201 AUTO "172.16.121.3/24" control || true
284         ;;
285 esac
286
287 echo " .... MAAS deployment finished successfully ...."
288
289 #echo "... Deployment of opnfv release Started ...."
290 #python deploy.py $maas_ip