modified to typo error.
[joid.git] / ci / 03-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 juju-core -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 installmaas(){
152     sudo apt-get install maas -y
153
154
155 }
156
157 configuremaas(){
158     sudo maas createadmin --username=ubuntu --email=ubuntu@ubuntu.com
159 }
160
161 addnodes(){
162
163
164 }
165
166 sudo maas-deployer -c deployment.yaml -d --force
167
168 sudo chown $USER:$USER environments.yaml
169
170 echo "... Deployment of maas finish ...."
171
172 # Backup deployment.yaml and deployconfig.yaml in .juju folder
173
174 cp ./environments.yaml ~/.juju/
175 cp ./environments.yaml ~/joid_config/
176
177 if [ -e ./deployconfig.yaml ]; then
178     cp ./deployconfig.yaml ~/.juju/
179     cp ./labconfig.yaml ~/.juju/
180     cp ./deployconfig.yaml ~/joid_config/
181     cp ./labconfig.yaml ~/joid_config/
182 fi
183
184 if [ -e ./deployment.yaml ]; then
185     cp ./deployment.yaml ~/.juju/
186     cp ./deployment.yaml ~/joid_config/
187 fi
188
189 #
190 # MAAS Customization
191 #
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 #maas maas sshkeys new key="`cat ./maas/sshkeys/DominoKey.pub`"
201
202 #adding compute and control nodes VM to MAAS for virtual deployment purpose.
203 if [ "$virtinstall" -eq 1 ]; then
204     # create two more VMs to do the deployment.
205     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
206
207     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
208
209     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
210
211     node1controlmac=`grep  "mac address" node1-control | head -1 | cut -d "'" -f 2`
212     node2computemac=`grep  "mac address" node2-compute | head -1 | cut -d "'" -f 2`
213     node5computemac=`grep  "mac address" node5-compute | head -1 | cut -d "'" -f 2`
214
215     sudo virsh -c qemu:///system define --file node1-control
216     sudo virsh -c qemu:///system define --file node2-compute
217     sudo virsh -c qemu:///system define --file node5-compute
218
219     maas maas tags new name='control'
220     maas maas tags new name='compute'
221
222     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 `
223
224     maas maas tag update-nodes control add=$controlnodeid
225
226     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 `
227
228     maas maas tag update-nodes compute add=$computenodeid
229
230     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 `
231
232     maas maas tag update-nodes compute add=$computenodeid
233 fi
234
235 #
236 # Functions for MAAS network customization
237 #
238
239 #Below function will mark the interfaces in Auto mode to enbled by MAAS
240 enableautomode() {
241     listofnodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
242     for nodes in $listofnodes
243     do
244         maas maas interface link-subnet $nodes $1  mode=$2 subnet=$3
245     done
246 }
247
248 #Below function will mark the interfaces in Auto mode to enbled by MAAS
249 # using hostname of the node added into MAAS
250 enableautomodebyname() {
251     if [ ! -z "$4" ]; then
252         for i in `seq 1 7`;
253         do
254             nodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
255             if [ ! -z "$nodes" ]; then
256                 maas maas interface link-subnet $nodes $1  mode=$2 subnet=$3
257             fi
258        done
259     fi
260 }
261
262 #Below function will create vlan and update interface with the new vlan
263 # will return the vlan id created
264 crvlanupdsubnet() {
265     newvlanid=`maas maas vlans create $2 name=$3 vid=$4 | grep resource | cut -d '/' -f 6 `
266     maas maas subnet update $5 vlan=$newvlanid
267     eval "$1"="'$newvlanid'"
268 }
269
270 #Below function will create interface with new vlan and bind to physical interface
271 crnodevlanint() {
272     listofnodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
273
274     for nodes in $listofnodes
275     do
276         parentid=`maas maas interface read $nodes $2 | grep interfaces | cut -d '/' -f 8`
277         maas maas interfaces create-vlan $nodes vlan=$1 parent=$parentid
278      done
279  }
280
281 #function for JUJU envronment
282
283 addcredential() {
284     controllername=`awk 'NR==1{print $2}' environments.yaml`
285     cloudname=`awk 'NR==1{print $2}' environments.yaml`
286
287     echo  "credentials:" > credential.yaml
288     echo  "  $controllername:" >> credential.yaml
289     echo  "    opnfv-credentials:" >> credential.yaml
290     echo  "      auth-type: oauth1" >> credential.yaml
291     echo  "      maas-oauth: $apikey" >> credential.yaml
292
293     juju add-credential $controllername -f credential.yaml --replace
294 }
295
296 addcloud() {
297     controllername=`awk 'NR==1{print $2}' environments.yaml`
298     cloudname=`awk 'NR==1{print $2}' environments.yaml`
299
300     echo "clouds:" > maas-cloud.yaml
301     echo "   $cloudname:" >> maas-cloud.yaml
302     echo "      type: maas" >> maas-cloud.yaml
303     echo "      auth-types: [oauth1]" >> maas-cloud.yaml
304     echo "      endpoint: http://$maas_ip/MAAS" >> maas-cloud.yaml
305
306     juju add-cloud $cloudname maas-cloud.yaml --replace
307 }
308
309
310 #
311 # VLAN customization
312 #
313
314 case "$labname" in
315     'intelpod9' )
316         maas refresh
317         crvlanupdsubnet vlan904 fabric-1 "MgmtNetwork" 904 2 || true
318         crvlanupdsubnet vlan905 fabric-2 "PublicNetwork" 905 3 || true
319         crnodevlanint $vlan905 eth1 || true
320         crnodevlanint $vlan905 eth3 || true
321         enableautomodebyname eth1.905 AUTO "10.9.15.0/24" || true
322         enableautomodebyname eth3.905 AUTO "10.9.15.0/24" || true
323         enableautomodebyname eth0 AUTO "10.9.12.0/24" || true
324         enableautomodebyname eth2 AUTO "10.9.12.0/24" || true
325         ;;
326 esac
327
328 #
329 # Enable MAAS nodes interfaces
330 #
331
332 #read interface needed in Auto mode and enable it. Will be rmeoved once auto enablement will be implemented in the maas-deployer.
333 if [ -e ~/joid_config/deployconfig.yaml ]; then
334   cp ~/joid_config/deployconfig.yaml ./deployconfig.yaml
335 elif [ -e ~/.juju/deployconfig.yaml ]; then
336   cp ~/.juju/deployconfig.yaml ./deployconfig.yaml
337 fi
338
339   enableiflist=`grep "interface-enable" deployconfig.yaml | cut -d ' ' -f 4 `
340   datanet=`grep "dataNetwork" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //'`
341   stornet=`grep "storageNetwork" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //'`
342   pubnet=`grep "publicNetwork" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //'`
343
344   # split EXTERNAL_NETWORK=first ip;last ip; gateway;network
345
346   if [ "$datanet" != "''" ]; then
347       EXTNET=(${enableiflist//,/ })
348       i="0"
349       while [ ! -z "${EXTNET[i]}" ];
350       do
351           enableautomode ${EXTNET[i]} AUTO $datanet || true
352           i=$[$i+1]
353       done
354   fi
355   if [ "$stornet" != "''" ]; then
356       EXTNET=(${enableiflist//,/ })
357       i="0"
358       while [ ! -z "${EXTNET[i]}" ];
359       do
360           enableautomode ${EXTNET[i]} AUTO $stornet || true
361           i=$[$i+1]
362       done
363   fi
364   if [ "$pubnet" != "''" ]; then
365       EXTNET=(${enableiflist//,/ })
366       i="0"
367       while [ ! -z "${EXTNET[i]}" ];
368       do
369           enableautomode ${EXTNET[i]} AUTO $pubnet || true
370           i=$[$i+1]
371       done
372   fi
373 fi
374
375
376 # Add the cloud and controller credentials for MAAS for that lab.
377 jujuver=`juju --version`
378
379 if [ "$jujuver" > "2" ]; then
380     addcloud
381     addcredential
382 fi
383
384 #
385 # End of scripts
386 #
387 echo " .... MAAS deployment finished successfully ...."