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