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