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