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