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