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