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