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