more changes needed for juju 2.0
[joid.git] / ci / 00-maasdeploy.sh
1 #!/bin/bash
2 #placeholder for deployment script.
3 set -ex
4
5 virtinstall=0
6 labname=$1
7
8 #install the packages needed
9 sudo apt-add-repository ppa:opnfv-team/proposed -y
10 sudo apt-add-repository ppa:maas-deployers/stable -y
11 sudo apt-add-repository ppa:juju/stable -y
12 sudo apt-add-repository ppa:maas/stable -y
13 sudo apt-add-repository cloud-archive:mitaka -y
14 sudo apt-get update -y
15 sudo apt-get dist-upgrade -y
16 sudo apt-get install openssh-server bzr git maas-deployer juju juju-deployer \
17              maas-cli python-pip python-psutil python-openstackclient \
18              python-congressclient gsutil charm-tools pastebinit -y
19
20 #first parameter should be custom and second should be either
21 # absolute location of file (including file name) or url of the
22 # file to download.
23
24 labname=$1
25 labfile=$2
26
27 #
28 # Config preparation
29 #
30
31 # Get labconfig and generate deployment.yaml for MAAS and deployconfig.yaml
32 case "$labname" in
33     intelpod[569]|orangepod[12]|cengnpod[12] )
34         array=(${labname//pod/ })
35         cp ../labconfig/${array[0]}/pod${array[1]}/labconfig.yaml .
36         python genMAASConfig.py -l labconfig.yaml > deployment.yaml
37         python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
38         ;;
39     'attvirpod1' )
40         cp ../labconfig/att/virpod1/labconfig.yaml .
41         python genMAASConfig.py -l labconfig.yaml > deployment.yaml
42         python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
43         ;;
44     'juniperpod1' )
45         cp maas/juniper/pod1/deployment.yaml ./deployment.yaml
46         ;;
47     'custom')
48         if [ -e $labfile ]; then
49             cp $labfile ./labconfig.yaml || true
50         else
51             wget $labconfigfile -t 3 -T 10 -O ./labconfig.yaml || true
52             count=`wc -l labconfig.yaml  | cut -d " " -f 1`
53             if [ $count -lt 10 ]; then
54                 rm -rf labconfig.yaml
55             fi
56         fi
57         if [ ! -e ./labconfig.yaml ]; then
58             virtinstall=1
59         else
60             python genMAASConfig.py -l labconfig.yaml > deployment.yaml
61             python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
62             labname=`grep "maas_name" deployment.yaml | cut -d ':' -f 2 | sed -e 's/ //'`
63         fi
64         ;;
65     * )
66         virtinstall=1
67         ;;
68 esac
69
70 # In the case of a virtual deployment get deployment.yaml and deployconfig.yaml
71 if [ "$virtinstall" -eq 1 ]; then
72     labname="default"
73     ./cleanvm.sh || true
74     cp ../labconfig/default/deployment.yaml ./
75     cp ../labconfig/default/labconfig.yaml ./
76     cp ../labconfig/default/deployconfig.yaml ./
77 fi
78
79 #
80 # Prepare local environment to avoid password asking
81 #
82
83 # make sure no password asked during the deployment.
84 echo "$USER ALL=(ALL) NOPASSWD:ALL" > 90-joid-init
85
86 if [ -e /etc/sudoers.d/90-joid-init ]; then
87     sudo cp /etc/sudoers.d/90-joid-init 91-joid-init
88     sudo chown $USER:$USER 91-joid-init
89     sudo chmod 660 91-joid-init
90     sudo cat 90-joid-init >> 91-joid-init
91     sudo chown root:root 91-joid-init
92     sudo mv 91-joid-init /etc/sudoers.d/
93 else
94     sudo chown root:root 90-joid-init
95     sudo mv 90-joid-init /etc/sudoers.d/
96 fi
97
98 if [ ! -e $HOME/.ssh/id_rsa ]; then
99     ssh-keygen -N '' -f $HOME/.ssh/id_rsa
100 fi
101
102 echo "... Deployment of maas Started ...."
103
104 #
105 # Virsh preparation
106 #
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 sudo apt-get install libvirt-bin -y
111 sudo adduser $USER libvirtd
112 sudo virsh pool-define-as default --type dir --target /var/lib/libvirt/images/ || true
113 sudo virsh pool-start default || true
114 sudo virsh pool-autostart default || true
115
116 # In case of virtual install set network
117 if [ "$virtinstall" -eq 1 ]; then
118     sudo virsh net-dumpxml default > default-net-org.xml
119     sudo sed -i '/dhcp/d' default-net-org.xml
120     sudo sed -i '/range/d' default-net-org.xml
121     sudo virsh net-define default-net-org.xml
122     sudo virsh net-destroy default
123     sudo virsh net-start default
124 fi
125
126 # Ensure virsh can connect without ssh auth
127 cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys
128
129
130 #
131 # Cleanup, juju init and config backup
132 #
133
134 # To avoid problem between apiclient/maas_client and apiclient from google
135 # we remove the package google-api-python-client from yardstick installer
136 if [ $(pip list |grep google-api-python-client |wc -l) == 1 ]; then
137     sudo pip uninstall google-api-python-client
138 fi
139
140 #create backup directory
141 mkdir ~/joid_config/ || true
142 mkdir ~/.juju/ || true
143
144 # Init Juju
145 juju init -f || true
146
147 #
148 # MAAS deploy
149 #
150
151 sudo maas-deployer -c deployment.yaml -d --force
152
153 sudo chown $USER:$USER environments.yaml
154
155 echo "... Deployment of maas finish ...."
156
157 # Backup deployment.yaml and deployconfig.yaml in .juju folder
158
159 cp ./environments.yaml ~/.juju/
160 cp ./environments.yaml ~/joid_config/
161
162 if [ -e ./deployconfig.yaml ]; then
163     cp ./deployconfig.yaml ~/.juju/
164     cp ./labconfig.yaml ~/.juju/
165     cp ./deployconfig.yaml ~/joid_config/
166     cp ./labconfig.yaml ~/joid_config/
167 fi
168
169 if [ -e ./deployment.yaml ]; then
170     cp ./deployment.yaml ~/.juju/
171     cp ./deployment.yaml ~/joid_config/
172 fi
173
174 #
175 # MAAS Customization
176 #
177
178 maas_ip=`grep " ip_address" deployment.yaml | cut -d ':' -f 2 | sed -e 's/ //'`
179 apikey=`grep maas-oauth: environments.yaml | cut -d "'" -f 2`
180 maas login maas http://${maas_ip}/MAAS/api/1.0 ${apikey}
181 maas maas sshkeys new key="`cat $HOME/.ssh/id_rsa.pub`"
182
183 #Added the Qtip public to run the Qtip test after install on bare metal nodes.
184 #maas maas sshkeys new key="`cat ./maas/sshkeys/QtipKey.pub`"
185 #maas maas sshkeys new key="`cat ./maas/sshkeys/DominoKey.pub`"
186
187 #adding compute and control nodes VM to MAAS for virtual deployment purpose.
188 if [ "$virtinstall" -eq 1 ]; then
189     # create two more VMs to do the deployment.
190     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
191
192     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
193
194     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
195
196     node1controlmac=`grep  "mac address" node1-control | head -1 | cut -d "'" -f 2`
197     node2computemac=`grep  "mac address" node2-compute | head -1 | cut -d "'" -f 2`
198     node5computemac=`grep  "mac address" node5-compute | head -1 | cut -d "'" -f 2`
199
200     sudo virsh -c qemu:///system define --file node1-control
201     sudo virsh -c qemu:///system define --file node2-compute
202     sudo virsh -c qemu:///system define --file node5-compute
203
204     maas maas tags new name='control'
205     maas maas tags new name='compute'
206
207     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 `
208
209     maas maas tag update-nodes control add=$controlnodeid
210
211     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 `
212
213     maas maas tag update-nodes compute add=$computenodeid
214
215     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 `
216
217     maas maas tag update-nodes compute add=$computenodeid
218 fi
219
220 #
221 # Functions for MAAS network customization
222 #
223
224 #Below function will mark the interfaces in Auto mode to enbled by MAAS
225 enableautomode() {
226     listofnodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
227     for nodes in $listofnodes
228     do
229         maas maas interface link-subnet $nodes $1  mode=$2 subnet=$3
230     done
231 }
232
233 #Below function will mark the interfaces in Auto mode to enbled by MAAS
234 # using hostname of the node added into MAAS
235 enableautomodebyname() {
236     if [ ! -z "$4" ]; then
237         for i in `seq 1 7`;
238         do
239             nodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
240             if [ ! -z "$nodes" ]; then
241                 maas maas interface link-subnet $nodes $1  mode=$2 subnet=$3
242             fi
243        done
244     fi
245 }
246
247 #Below function will create vlan and update interface with the new vlan
248 # will return the vlan id created
249 crvlanupdsubnet() {
250     newvlanid=`maas maas vlans create $2 name=$3 vid=$4 | grep resource | cut -d '/' -f 6 `
251     maas maas subnet update $5 vlan=$newvlanid
252     eval "$1"="'$newvlanid'"
253 }
254
255 #Below function will create interface with new vlan and bind to physical interface
256 crnodevlanint() {
257     listofnodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
258
259     for nodes in $listofnodes
260     do
261         parentid=`maas maas interface read $nodes $2 | grep interfaces | cut -d '/' -f 8`
262         maas maas interfaces create-vlan $nodes vlan=$1 parent=$parentid
263      done
264  }
265
266 #function for JUJU envronment
267
268 addcredential() {
269     controllername=`awk 'NR==1{print $2}' environments.yaml`
270     cloudname=`awk 'NR==1{print $2}' environments.yaml`
271
272     echo  "credentials:" > credential.yaml
273     echo  "  $controllername:" >> credential.yaml
274     echo  "    opnfv-credentials:" >> credential.yaml
275     echo  "      auth-type: oauth1" >> credential.yaml
276     echo  "      maas-oauth: $apikey" >> credential.yaml
277
278     juju add-credential $controllername -f credential.yaml --replace
279 }
280
281 addcloud() {
282     controllername=`awk 'NR==1{print $2}' environments.yaml`
283     cloudname=`awk 'NR==1{print $2}' environments.yaml`
284
285     echo "clouds:" > maas-cloud.yaml
286     echo "   $cloudname:" >> maas-cloud.yaml
287     echo "      type: maas" >> maas-cloud.yaml
288     echo "      auth-types: [oauth1]" >> maas-cloud.yaml
289     echo "      endpoint: http://$maas_ip/MAAS" >> maas-cloud.yaml
290
291     juju add-cloud $cloudname maas-cloud.yaml --replace
292 }
293
294
295 #
296 # VLAN customization
297 #
298
299 case "$labname" in
300     'intelpod9' )
301         maas refresh
302         crvlanupdsubnet vlan904 fabric-1 "MgmtNetwork" 904 2 || true
303         crvlanupdsubnet vlan905 fabric-2 "PublicNetwork" 905 3 || true
304         crnodevlanint $vlan905 eth1 || true
305         crnodevlanint $vlan905 eth3 || true
306         enableautomodebyname eth1.905 AUTO "10.9.15.0/24" || true
307         enableautomodebyname eth3.905 AUTO "10.9.15.0/24" || true
308         enableautomodebyname eth0 AUTO "10.9.12.0/24" || true
309         enableautomodebyname eth2 AUTO "10.9.12.0/24" || true
310         ;;
311 esac
312
313 #
314 # Enable MAAS nodes interfaces
315 #
316
317 #read interface needed in Auto mode and enable it. Will be rmeoved once auto enablement will be implemented in the maas-deployer.
318 if [ -e ~/joid_config/deployconfig.yaml ]; then
319   cp ~/joid_config/deployconfig.yaml ./deployconfig.yaml
320 elif [ -e ~/.juju/deployconfig.yaml ]; then
321   cp ~/.juju/deployconfig.yaml ./deployconfig.yaml
322 fi
323
324   enableiflist=`grep "interface-enable" deployconfig.yaml | cut -d ' ' -f 4 `
325   datanet=`grep "dataNetwork" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //'`
326   stornet=`grep "storageNetwork" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //'`
327   pubnet=`grep "publicNetwork" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //'`
328
329   # split EXTERNAL_NETWORK=first ip;last ip; gateway;network
330
331   if [ "$datanet" != "''" ]; then
332       EXTNET=(${enableiflist//,/ })
333       i="0"
334       while [ ! -z "${EXTNET[i]}" ];
335       do
336           enableautomode ${EXTNET[i]} AUTO $datanet || true
337           i=$[$i+1]
338       done
339   fi
340   if [ "$stornet" != "''" ]; then
341       EXTNET=(${enableiflist//,/ })
342       i="0"
343       while [ ! -z "${EXTNET[i]}" ];
344       do
345           enableautomode ${EXTNET[i]} AUTO $stornet || true
346           i=$[$i+1]
347       done
348   fi
349   if [ "$pubnet" != "''" ]; then
350       EXTNET=(${enableiflist//,/ })
351       i="0"
352       while [ ! -z "${EXTNET[i]}" ];
353       do
354           enableautomode ${EXTNET[i]} AUTO $pubnet || true
355           i=$[$i+1]
356       done
357   fi
358 fi
359
360
361 # Add the cloud and controller credentials for MAAS for that lab.
362 jujuver=`juju --version`
363
364 if [ "$jujuver" > "2" ]; then
365     addcloud
366     addcredential
367 fi
368
369 #
370 # End of scripts
371 #
372 echo " .... MAAS deployment finished successfully ...."