14937730a0653724401b03c542d0734f7d343ed1
[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 # Backup deployment.yaml and deployconfig.yaml in .juju folder
143 cp ./deployment.yaml ~/.juju/
144
145 if [ -e ./deployconfig.yaml ]; then
146     cp ./deployconfig.yaml ~/.juju/
147 fi
148
149 #
150 # MAAS deploy
151 #
152
153 sudo maas-deployer -c deployment.yaml -d --force
154
155 sudo chown $USER:$USER environments.yaml
156
157 echo "... Deployment of maas finish ...."
158
159 #
160 # MAAS Customization
161 #
162
163 maas_ip=`grep " ip_address" deployment.yaml | cut -d ':' -f 2 | sed -e 's/ //'`
164 apikey=`grep maas-oauth: environments.yaml | cut -d "'" -f 2`
165 maas login maas http://${maas_ip}/MAAS/api/1.0 ${apikey}
166 maas maas sshkeys new key="`cat $HOME/.ssh/id_rsa.pub`"
167
168 #Added the Qtip public to run the Qtip test after install on bare metal nodes.
169 maas maas sshkeys new key="`cat ./maas/sshkeys/QtipKey.pub`"
170 maas maas sshkeys new key="`cat ./maas/sshkeys/DominoKey.pub`"
171
172 #adding compute and control nodes VM to MAAS for virtual deployment purpose.
173 if [ "$virtinstall" -eq 1 ]; then
174     # create two more VMs to do the deployment.
175     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
176
177     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
178
179     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
180
181     node1controlmac=`grep  "mac address" node1-control | head -1 | cut -d "'" -f 2`
182     node2computemac=`grep  "mac address" node2-compute | head -1 | cut -d "'" -f 2`
183     node5computemac=`grep  "mac address" node5-compute | head -1 | cut -d "'" -f 2`
184
185     sudo virsh -c qemu:///system define --file node1-control
186     sudo virsh -c qemu:///system define --file node2-compute
187     sudo virsh -c qemu:///system define --file node5-compute
188
189     maas maas tags new name='control'
190     maas maas tags new name='compute'
191
192     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 `
193
194     maas maas tag update-nodes control add=$controlnodeid
195
196     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 `
197
198     maas maas tag update-nodes compute add=$computenodeid
199
200     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 `
201
202     maas maas tag update-nodes compute add=$computenodeid
203 fi
204
205 #
206 # Functions for MAAS network customization
207 #
208
209 #Below function will mark the interfaces in Auto mode to enbled by MAAS
210 enableautomode() {
211     listofnodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
212     for nodes in $listofnodes
213     do
214         maas maas interface link-subnet $nodes $1  mode=$2 subnet=$3
215     done
216 }
217
218 #Below function will mark the interfaces in Auto mode to enbled by MAAS
219 # using hostname of the node added into MAAS
220 enableautomodebyname() {
221     if [ ! -z "$4" ]; then
222         for i in `seq 1 7`;
223         do
224             nodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
225             if [ ! -z "$nodes" ]; then
226                 maas maas interface link-subnet $nodes $1  mode=$2 subnet=$3
227             fi
228        done
229     fi
230 }
231
232 #Below function will create vlan and update interface with the new vlan
233 # will return the vlan id created
234 crvlanupdsubnet() {
235     newvlanid=`maas maas vlans create $2 name=$3 vid=$4 | grep resource | cut -d '/' -f 6 `
236     maas maas subnet update $5 vlan=$newvlanid
237     eval "$1"="'$newvlanid'"
238 }
239
240 #Below function will create interface with new vlan and bind to physical interface
241 crnodevlanint() {
242     listofnodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
243
244     for nodes in $listofnodes
245     do
246         parentid=`maas maas interface read $nodes $2 | grep interfaces | cut -d '/' -f 8`
247         maas maas interfaces create-vlan $nodes vlan=$1 parent=$parentid
248      done
249  }
250
251 #
252 # VLAN customization
253 #
254
255 case "$labname" in
256     'intelpod9' )
257         maas refresh
258         crvlanupdsubnet vlan904 fabric-1 "MgmtNetwork" 904 2 || true
259         crvlanupdsubnet vlan905 fabric-2 "PublicNetwork" 905 3 || true
260         crnodevlanint $vlan905 eth1 || true
261         crnodevlanint $vlan905 eth3 || true
262         enableautomodebyname eth1.905 AUTO "10.9.15.0/24" || true
263         enableautomodebyname eth3.905 AUTO "10.9.15.0/24" || true
264         enableautomodebyname eth0 AUTO "10.9.12.0/24" || true
265         enableautomodebyname eth2 AUTO "10.9.12.0/24" || true
266         ;;
267 esac
268
269 #
270 # Enable MAAS nodes interfaces
271 #
272
273 #read interface needed in Auto mode and enable it. Will be rmeoved once auto enablement will be implemented in the maas-deployer.
274 if [ -e ~/.juju/deployconfig.yaml ]; then
275   cp ~/.juju/deployconfig.yaml ./deployconfig.yaml
276
277   enableiflist=`grep "interface-enable" deployconfig.yaml | cut -d ' ' -f 4 `
278   datanet=`grep "dataNetwork" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //'`
279   stornet=`grep "storageNetwork" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //'`
280   pubnet=`grep "publicNetwork" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //'`
281
282   # split EXTERNAL_NETWORK=first ip;last ip; gateway;network
283
284   if [ "$datanet" != "''" ]; then
285       EXTNET=(${enableiflist//,/ })
286       i="0"
287       while [ ! -z "${EXTNET[i]}" ];
288       do
289           enableautomode ${EXTNET[i]} AUTO $datanet || true
290           i=$[$i+1]
291       done
292   fi
293   if [ "$stornet" != "''" ]; then
294       EXTNET=(${enableiflist//,/ })
295       i="0"
296       while [ ! -z "${EXTNET[i]}" ];
297       do
298           enableautomode ${EXTNET[i]} AUTO $stornet || true
299           i=$[$i+1]
300       done
301   fi
302   if [ "$pubnet" != "''" ]; then
303       EXTNET=(${enableiflist//,/ })
304       i="0"
305       while [ ! -z "${EXTNET[i]}" ];
306       do
307           enableautomode ${EXTNET[i]} AUTO $pubnet || true
308           i=$[$i+1]
309       done
310   fi
311 fi
312
313 #
314 # End of scripts
315 #
316 echo " .... MAAS deployment finished successfully ...."