enable interface and subnet as per the labconfig.yaml
[joid.git] / ci / 03-maasdeploy.sh
1 #!/bin/bash
2 #placeholder for deployment script.
3 set -ex
4
5 virtinstall=0
6 labname=$1
7
8 if [ ! -e $HOME/.ssh/id_rsa ]; then
9     ssh-keygen -N '' -f $HOME/.ssh/id_rsa
10 fi
11
12 #install the packages needed
13 sudo apt-add-repository ppa:juju/devel -y
14 sudo apt-add-repository ppa:maas/stable -y
15 sudo apt-add-repository cloud-archive:newton -y
16 sudo apt-get update -y
17 #sudo apt-get dist-upgrade -y
18 sudo apt-get install openssh-server bzr git virtinst qemu-kvm libvirt-bin juju \
19              maas maas-region-controller python-pip python-psutil python-openstackclient \
20              python-congressclient gsutil charm-tools pastebinit python-jinja2 sshpass \
21              openssh-server vlan ipmitool jq expect -y
22
23 sudo pip install --upgrade pip
24
25 #first parameter should be custom and second should be either
26 # absolute location of file (including file name) or url of the
27 # file to download.
28
29
30 #
31 # Config preparation
32 #
33
34 # Get labconfig and generate deployconfig.yaml
35 case "$labname" in
36     intelpod[569]|orangepod[12]|cengnpod[12] )
37         array=(${labname//pod/ })
38         cp ../labconfig/${array[0]}/pod${array[1]}/labconfig.yaml .
39         python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
40         ;;
41     'attvirpod1' )
42         cp ../labconfig/att/virpod1/labconfig.yaml .
43         python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
44         ;;
45     'custom')
46         labfile=$2
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             labname="default"
59             cp ../labconfig/default/labconfig.yaml ./
60             cp ../labconfig/default/deployconfig.yaml ./
61         else
62             python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
63             labname=`grep "maas_name" deployconfig.yaml | cut -d ':' -f 2 | sed -e 's/ //'`
64         fi
65         ;;
66     * )
67         virtinstall=1
68         labname="default"
69         cp ../labconfig/default/labconfig.yaml ./
70         python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
71         ;;
72 esac
73
74 python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < labconfig.yaml > labconfig.json
75 python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < deployconfig.yaml > deployconfig.json
76
77 MAAS_IP=$(grep " ip_address" deployconfig.yaml | cut -d ':' -f 2 | sed -e 's/ //')
78 MAAS_NAME=`grep "maas_name" deployconfig.yaml | cut -d ':' -f 2 | sed -e 's/ //'`
79 API_SERVER="http://$MAAS_IP/MAAS/api/2.0"
80 API_SERVERMAAS="http://$MAAS_IP/MAAS/"
81 PROFILE=ubuntu
82 MY_UPSTREAM_DNS=`grep "upstream_dns" deployconfig.yaml | cut -d ':' -f 2 | sed -e 's/ //'`
83 SSH_KEY=`cat ~/.ssh/id_rsa.pub`
84 MAIN_ARCHIVE=`grep "main_archive" deployconfig.yaml | cut -d ':' -f 2-3 | sed -e 's/ //'`
85 URL=https://images.maas.io/ephemeral-v2/daily/
86 KEYRING_FILE=/usr/share/keyrings/ubuntu-cloudimage-keyring.gpg
87 SOURCE_ID=1
88 FABRIC_ID=1
89 PRIMARY_RACK_CONTROLLER="$MAAS_IP"
90 SUBNET_CIDR=`cat labconfig.json | jq '.opnfv.spaces[] | select(.type=="admin")'.cidr | cut -d \" -f 2 `
91 SUBNETDATA_CIDR=`cat labconfig.json | jq '.opnfv.spaces[] | select(.type=="data")'.cidr | cut -d \" -f 2 `
92 SUBNETPUB_CIDR=`cat labconfig.json | jq '.opnfv.spaces[] | select(.type=="public")'.cidr | cut -d \" -f 2 `
93 VLAN_TAG="untagged"
94
95 # In the case of a virtual deployment get deployconfig.yaml
96 if [ "$virtinstall" -eq 1 ]; then
97     ./cleanvm.sh || true
98 fi
99
100 #create backup directory
101 mkdir ~/joid_config/ || true
102
103 # Backup deployconfig.yaml in joid_config folder
104
105 if [ -e ./deployconfig.yaml ]; then
106     cp ./deployconfig.yaml ~/joid_config/
107     cp ./labconfig.yaml ~/joid_config/
108 fi
109
110 #
111 # Prepare local environment to avoid password asking
112 #
113
114 # make sure no password asked during the deployment.
115 sudoer_file=/etc/sudoers.d/90-joid-init
116 sudoer_entry="$USER ALL=(ALL) NOPASSWD:ALL"
117 if [ -e $sudoer_file ]; then
118     if ! sudo grep -q "$sudoer_entry" $sudoer_file; then
119         sudo sed -i -e "1i$sudoer_entry" $sudoer_file
120     fi
121 else
122     echo "$sudoer_entry" > 90-joid-init
123     sudo chown root:root 90-joid-init
124     sudo mv 90-joid-init /etc/sudoers.d/
125 fi
126
127 echo "... Deployment of maas Started ...."
128
129 #
130 # Virsh preparation
131 #
132
133 # define the pool and try to start even though its already exist.
134 # For fresh install this may or may not there.
135 sudo adduser $USER libvirtd
136 sudo virsh pool-define-as default --type dir --target /var/lib/libvirt/images/ || true
137 sudo virsh pool-start default || true
138 sudo virsh pool-autostart default || true
139
140 # In case of virtual install set network
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     rm -f default-net-org.xml
149 fi
150
151 #
152 # Cleanup, juju init and config backup
153 #
154
155 # To avoid problem between apiclient/maas_client and apiclient from google
156 # we remove the package google-api-python-client from yardstick installer
157 if [ $(pip list |grep google-api-python-client |wc -l) == 1 ]; then
158     sudo pip uninstall google-api-python-client
159 fi
160
161
162 if [ ! -e ~maas/.ssh/id_rsa.pub ]; then
163     if [ ! -e $HOME/id_rsa_maas.pub ]; then
164         [ -e $HOME/id_rsa_maas ] && rm -f $HOME/id_rsa_maas
165         sudo su - $USER -c "echo |ssh-keygen -t rsa -f $HOME/id_rsa_maas"
166     fi
167     sudo -u maas mkdir ~maas/.ssh/ || true
168     sudo cp $HOME/id_rsa_maas ~maas/.ssh/id_rsa
169     sudo cp $HOME/id_rsa_maas.pub ~maas/.ssh/id_rsa.pub
170     sudo chown maas:maas ~maas/.ssh/id_rsa
171     sudo chown maas:maas ~maas/.ssh/id_rsa.pub
172 fi
173
174 # Ensure virsh can connect without ssh auth
175 sudo cat ~maas/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys
176 sudo cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys
177
178 #
179 # MAAS deploy
180 #
181
182 installmaas(){
183     sudo apt-get install maas maas-region-controller -y
184 }
185
186 #
187 # MAAS config
188 # https://insights.ubuntu.com/2016/01/23/maas-setup-deploying-openstack-on-maas-1-9-with-juju/
189 # http://blog.naydenov.net/2016/01/nodes-networking-deploying-openstack-on-maas-1-9-with-juju/
190 #
191 configuremaas(){
192     #reconfigure maas with correct MAAS address.
193     #Below code is needed as MAAS have issue in commisoning without restart.
194     sudo ./maas-reconfigure-region.sh $MAAS_IP
195     sudo maas-rack config --region-url http://$MAAS_IP:5240/MAAS
196
197     sudo maas createadmin --username=ubuntu --email=ubuntu@ubuntu.com --password=ubuntu || true
198     API_KEY=`sudo maas-region apikey --username=ubuntu`
199     maas login $PROFILE $API_SERVERMAAS $API_KEY
200     maas $PROFILE maas set-config name='main_archive' value=$MAIN_ARCHIVE || true
201     maas $PROFILE maas set-config name=upstream_dns value=$MY_UPSTREAM_DNS || true
202     maas $PROFILE maas set-config name='maas_name' value=$MAAS_NAME || true
203     maas $PROFILE maas set-config name='ntp_server' value='ntp.ubuntu.com' || true
204     maas $PROFILE sshkeys create "key=$SSH_KEY" || true
205
206     maas $PROFILE tags create name='bootstrap' || true
207     maas $PROFILE tags create name='compute' || true
208     maas $PROFILE tags create name='control' || true
209     maas $PROFILE tags create name='storage' || true
210
211     #create the required spaces.
212     maas $PROFILE space update 0 name=default || true
213     maas $PROFILE spaces create name=unused || true
214     maas $PROFILE spaces create name=admin-api || true
215     maas $PROFILE spaces create name=internal-api || true
216     maas $PROFILE spaces create name=public-api || true
217     maas $PROFILE spaces create name=compute-data || true
218     maas $PROFILE spaces create name=compute-external || true
219     maas $PROFILE spaces create name=storage-data || true
220     maas $PROFILE spaces create name=storage-cluster || true
221
222     maas $PROFILE boot-source update $SOURCE_ID \
223          url=$URL keyring_filename=$KEYRING_FILE || true
224
225     maas $PROFILE boot-resources import || true
226     sleep 10
227
228     while [ "$(maas $PROFILE boot-resources is-importing)" == "true" ];
229     do
230         sleep 60
231     done
232 }
233
234 enablesubnetanddhcp(){
235     TEMP_CIDR=$1
236     enabledhcp=$2
237     space=$3
238
239     SUBNET_PREFIX=${TEMP_CIDR::-5}
240
241     IP_RES_RANGE_LOW="$SUBNET_PREFIX.1"
242     IP_RES_RANGE_HIGH="$SUBNET_PREFIX.39"
243
244     API_KEY=`sudo maas-region apikey --username=ubuntu`
245     maas login $PROFILE $API_SERVERMAAS $API_KEY
246
247     maas $PROFILE ipranges create type=reserved \
248          start_ip=$IP_RES_RANGE_LOW end_ip=$IP_RES_RANGE_HIGH \
249          comment='This is a reserved range' || true
250
251     IP_DYNAMIC_RANGE_LOW="$SUBNET_PREFIX.40"
252     IP_DYNAMIC_RANGE_HIGH="$SUBNET_PREFIX.150"
253
254     maas $PROFILE ipranges create type=dynamic \
255         start_ip=$IP_DYNAMIC_RANGE_LOW end_ip=$IP_DYNAMIC_RANGE_HIGH \
256         comment='This is a reserved dynamic range' || true
257
258     FABRIC_ID=$(maas $PROFILE subnet read $TEMP_CIDR | jq '.vlan.fabric_id')
259
260     PRIMARY_RACK_CONTROLLER=$(maas $PROFILE rack-controllers read | jq -r '.[0].system_id')
261
262     if [ "$space" == "admin" ]; then
263         MY_GATEWAY=`cat labconfig.json | jq '.opnfv.spaces[] | select(.type=="admin")'.gateway | cut -d \" -f 2 `
264         #MY_NAMESERVER=`cat deployconfig.json | jq '.opnfv.upstream_dns' | cut -d \" -f 2`
265         maas $PROFILE subnet update $TEMP_CIDR gateway_ip=$MY_GATEWAY || true
266         #maas $PROFILE subnet update $TEMP_CIDR dns_servers=$MY_NAMESERVER || true
267         #below command will enable the interface with internal-api space.
268         SPACEID=$(maas $PROFILE space read internal-api | jq '.id')
269         maas $PROFILE subnet update $TEMP_CIDR space=$SPACEID || true
270         if [ "$enabledhcp" == "true" ]; then
271             maas $PROFILE vlan update $FABRIC_ID $VLAN_TAG dhcp_on=True primary_rack=$PRIMARY_RACK_CONTROLLER || true
272         fi
273     elif [ "$space" == "data" ]; then
274         MY_GATEWAY=`cat labconfig.json | jq '.opnfv.spaces[] | select(.type=="data")'.gateway | cut -d \" -f 2 `
275         if [ $MY_GATEWAY ]; then
276             maas $PROFILE subnet update $TEMP_CIDR gateway_ip=$MY_GATEWAY || true
277         fi
278         #below command will enable the interface with data-api space for data network.
279         SPACEID=$(maas $PROFILE space read data-api | jq '.id')
280         maas $PROFILE subnet update $TEMP_CIDR space=$SPACEID || true
281         if [ "$enabledhcp" == "true" ]; then
282             maas $PROFILE vlan update $FABRIC_ID $VLAN_TAG dhcp_on=True primary_rack=$PRIMARY_RACK_CONTROLLER || true
283         fi
284     elif [ "$space" == "public" ]; then
285         MY_GATEWAY=`cat labconfig.json | jq '.opnfv.spaces[] | select(.type=="data")'.public | cut -d \" -f 2 `
286         if [ $MY_GATEWAY ]; then
287             maas $PROFILE subnet update $TEMP_CIDR gateway_ip=$MY_GATEWAY || true
288         fi
289         #below command will enable the interface with public-api space for data network.
290         SPACEID=$(maas $PROFILE space read public-api | jq '.id')
291         maas $PROFILE subnet update $TEMP_CIDR space=$SPACEID || true
292         if [ "$enabledhcp" == "true" ]; then
293             maas $PROFILE vlan update $FABRIC_ID $VLAN_TAG dhcp_on=True primary_rack=$PRIMARY_RACK_CONTROLLER || true
294         fi
295     fi
296 }
297
298 addnodes(){
299     API_KEY=`sudo maas-region apikey --username=ubuntu`
300     maas login $PROFILE $API_SERVERMAAS $API_KEY
301
302     # make sure there is no machine entry in maas
303     for m in $(maas $PROFILE machines read | jq -r '.[].system_id')
304     do
305         maas ubuntu machine delete $m
306     done
307
308     if [ "$virtinstall" -eq 1 ]; then
309         netw=" --network bridge=virbr0,model=virtio"
310     else
311         brid=`brctl show | grep 8000 | cut -d "8" -f 1 |  tr "\n" " " | tr "\t" " " | tr -s " "`
312
313         netw=""
314         for feature in $brid; do
315             if [ "$feature" == "" ]; then
316                 netw=$netw
317             elif [ "$feature" == "virbr0" ]; then
318                 netw=$netw
319             else
320                 netw=$netw" --network bridge="$feature",model=virtio"
321             fi
322         done
323     fi
324
325     sudo virt-install --connect qemu:///system --name bootstrap --ram 4098 --cpu host --vcpus 2 --video \
326                  cirrus --arch x86_64 --disk size=20,format=qcow2,bus=virtio,io=native,pool=default \
327                  $netw --boot network,hd,menu=off --noautoconsole \
328                  --vnc --print-xml | tee bootstrap
329
330     if [ "$virtinstall" -eq 1 ]; then
331         bootstrapmac=`grep  "mac address" bootstrap | head -1 | cut -d '"' -f 2`
332     else
333         bootstrapmac=""
334         bootstrapmacs=`grep  "mac address" bootstrap| cut -d '"' -f 2`
335         for mac in $bootstrapmacs; do
336             bootstrapmac=$bootstrapmac" mac_addresses="$mac
337         done
338     fi
339     sudo virsh -c qemu:///system define --file bootstrap
340     rm -f bootstrap
341
342     maas $PROFILE machines create autodetect_nodegroup='yes' name='bootstrap' \
343         tags='bootstrap' hostname='bootstrap' power_type='virsh' mac_addresses=$bootstrapmac \
344         power_parameters_power_address='qemu+ssh://'$USER'@'$MAAS_IP'/system' \
345         architecture='amd64/generic' power_parameters_power_id='bootstrap'
346
347     bootstrapid=$(maas $PROFILE machines read | jq -r 'select(.[].hostname == "bootstrap")[0].system_id')
348
349     maas $PROFILE tag update-nodes bootstrap add=$bootstrapid
350
351     if [ "$virtinstall" -eq 1 ]; then
352
353         sudo virt-install --connect qemu:///system --name node1-control --ram 8192 --cpu host --vcpus 4 \
354                      --disk size=120,format=qcow2,bus=virtio,io=native,pool=default \
355                      $netw $netw --boot network,hd,menu=off --noautoconsole --vnc --print-xml | tee node1-control
356
357         sudo virt-install --connect qemu:///system --name node2-compute --ram 8192 --cpu host --vcpus 4 \
358                     --disk size=120,format=qcow2,bus=virtio,io=native,pool=default \
359                     $netw $netw --boot network,hd,menu=off --noautoconsole --vnc --print-xml | tee node2-compute
360
361         sudo virt-install --connect qemu:///system --name node5-compute --ram 8192 --cpu host --vcpus 4 \
362                    --disk size=120,format=qcow2,bus=virtio,io=native,pool=default \
363                    $netw $netw --boot network,hd,menu=off --noautoconsole --vnc --print-xml | tee node5-compute
364
365
366         node1controlmac=`grep  "mac address" node1-control | head -1 | cut -d '"' -f 2`
367         node2computemac=`grep  "mac address" node2-compute | head -1 | cut -d '"' -f 2`
368         node5computemac=`grep  "mac address" node5-compute | head -1 | cut -d '"' -f 2`
369
370         sudo virsh -c qemu:///system define --file node1-control
371         sudo virsh -c qemu:///system define --file node2-compute
372         sudo virsh -c qemu:///system define --file node5-compute
373         rm -f node1-control node2-compute node5-compute
374
375
376         maas $PROFILE machines create autodetect_nodegroup='yes' name='node1-control' \
377             tags='control' hostname='node1-control' power_type='virsh' mac_addresses=$node1controlmac \
378             power_parameters_power_address='qemu+ssh://'$USER'@'$MAAS_IP'/system' \
379             architecture='amd64/generic' power_parameters_power_id='node1-control'
380         controlnodeid=$(maas $PROFILE machines read | jq -r 'select(.[].hostname == "node1-control")[0].system_id')
381         maas $PROFILE machines create autodetect_nodegroup='yes' name='node2-compute' \
382             tags='compute' hostname='node2-compute' power_type='virsh' mac_addresses=$node2computemac \
383             power_parameters_power_address='qemu+ssh://'$USER'@'$MAAS_IP'/system' \
384             architecture='amd64/generic' power_parameters_power_id='node2-compute'
385         compute2nodeid=$(maas $PROFILE machines read | jq -r 'select(.[].hostname == "node2-compute")[0].system_id')
386         maas $PROFILE machines create autodetect_nodegroup='yes' name='node5-compute' \
387             tags='compute' hostname='node5-compute' power_type='virsh' mac_addresses=$node5computemac \
388             power_parameters_power_address='qemu+ssh://'$USER'@'$MAAS_IP'/system' \
389             architecture='amd64/generic' power_parameters_power_id='node5-compute'
390         compute5nodeid=$(maas $PROFILE machines read | jq -r 'select(.[].hostname == "node5-compute")[0].system_id')
391
392         maas $PROFILE tag update-nodes control add=$controlnodeid || true
393         maas $PROFILE tag update-nodes compute add=$compute2nodeid || true
394         maas $PROFILE tag update-nodes compute add=$compute5nodeid || true
395     else
396        units=`cat deployconfig.json | jq .opnfv.units`
397
398        until [ $(($units)) -lt 1 ]; do
399            units=$(($units - 1));
400            NODE_NAME=`cat labconfig.json | jq ".lab.racks[].nodes[$units].name" | cut -d \" -f 2 `
401            MAC_ADDRESS=`cat labconfig.json | jq ".lab.racks[].nodes[$units].nics[] | select(.spaces[]==\"admin\").mac"[0] | cut -d \" -f 2 `
402            POWER_TYPE=`cat labconfig.json | jq ".lab.racks[].nodes[$units].power.type" | cut -d \" -f 2 `
403            POWER_IP=`cat labconfig.json |  jq ".lab.racks[].nodes[$units].power.address" | cut -d \" -f 2 `
404            POWER_USER=`cat labconfig.json |  jq ".lab.racks[].nodes[$units].power.user" | cut -d \" -f 2 `
405            POWER_PASS=`cat labconfig.json |  jq ".lab.racks[].nodes[$units].power.pass" | cut -d \" -f 2 `
406
407            maas $PROFILE machines create autodetect_nodegroup='yes' name=$NODE_NAME \
408                hostname=$NODE_NAME power_type=$POWER_TYPE power_parameters_power_address=$POWER_IP \
409                power_parameters_power_user=$POWER_USER power_parameters_power_pass=$POWER_PASS mac_addresses=$MAC_ADDRESS \
410                architecture='amd64/generic'
411        done
412     fi
413
414     # make sure nodes are added into MAAS and none of them is in commisoning state
415     while [ "$(maas $PROFILE nodes read | grep  Commissioning )" ];
416     do
417         sleep 60
418     done
419
420 }
421
422 #configure MAAS with the different options.
423 configuremaas
424
425 # functioncall with subnetid to add and second parameter is dhcp enable
426 # third parameter will define the space. It is required to have admin
427
428 if [ $SUBNET_CIDR ]; then
429     enablesubnetanddhcp $SUBNET_CIDR true admin
430 else
431     echo "atleast admin network should be defined"
432     echo "MAAS configuration can not continue"
433     exit 2
434 fi
435
436 if [ $SUBNETDATA_CIDR ]; then
437     enablesubnetanddhcp $SUBNETDATA_CIDR false data
438 fi
439 if [ $SUBNETPUB_CIDR ]; then
440     enablesubnetanddhcp $SUBNETPUB_CIDR false public
441 fi
442
443 #just make sure rack controller has been synced and import only
444 # just whether images have been imported or not.
445 sleep 120
446
447 #lets add the nodes now. Currently works only for virtual deploymnet.
448 addnodes
449
450 echo "... Deployment of maas finish ...."
451
452 #Added the Qtip public to run the Qtip test after install on bare metal nodes.
453 #maas $PROFILE sshkeys new key="`cat ./maas/sshkeys/QtipKey.pub`"
454 #maas $PROFILE sshkeys new key="`cat ./maas/sshkeys/DominoKey.pub`"
455
456 #
457 # Functions for MAAS network customization
458 #
459
460 #Below function will mark the interfaces in Auto mode to enbled by MAAS
461 enableautomode() {
462     API_KEY=`sudo maas-region apikey --username=ubuntu`
463     maas login $PROFILE $API_SERVERMAAS $API_KEY
464
465     for node in $(maas $PROFILE nodes read | jq -r '.[].system_id')
466     do
467         maas $PROFILE interface link-subnet $node $1  mode=$2 subnet=$3 || true
468     done
469 }
470
471 #Below function will mark the interfaces in Auto mode to enbled by MAAS
472 # using hostname of the node added into MAAS
473 enableautomodebyname() {
474     API_KEY=`sudo maas-region apikey --username=ubuntu`
475     maas login $PROFILE $API_SERVERMAAS $API_KEY
476
477     if [ ! -z "$4" ]; then
478         for i in `seq 1 7`;
479         do
480             nodes=$(maas $PROFILE nodes read | jq -r '.[].system_id')
481             if [ ! -z "$nodes" ]; then
482                 maas $PROFILE interface link-subnet $nodes $1  mode=$2 subnet=$3 || true
483             fi
484        done
485     fi
486 }
487
488 #Below function will create vlan and update interface with the new vlan
489 # will return the vlan id created
490 crvlanupdsubnet() {
491     API_KEY=`sudo maas-region apikey --username=ubuntu`
492     maas login $PROFILE $API_SERVERMAAS $API_KEY
493
494     # TODO: fix subnet creation and use 'jq'
495     newvlanid=`maas $PROFILE vlans create $2 name=$3 vid=$4 | grep resource | cut -d '/' -f 6 `
496     maas $PROFILE subnet update $5 vlan=$newvlanid
497     eval "$1"="'$newvlanid'"
498 }
499
500 #Below function will create interface with new vlan and bind to physical interface
501 crnodevlanint() {
502     API_KEY=`sudo maas-region apikey --username=ubuntu`
503     maas login $PROFILE $API_SERVERMAAS $API_KEY
504
505     for node in $(maas $PROFILE nodes read | jq -r '.[].system_id')
506     do
507         interface=$(maas $PROFILE interface read $node $2 | jq -r '.id')
508         maas $PROFILE interfaces create-vlan $node vlan=$1 parent=$interface
509      done
510  }
511
512 #function for JUJU envronment
513
514 addcredential() {
515     API_KEY=`sudo maas-region apikey --username=ubuntu`
516     controllername=`awk 'NR==1{print substr($1, 1, length($1)-1)}' deployconfig.yaml`
517     cloudname=`awk 'NR==1{print substr($1, 1, length($1)-1)}' deployconfig.yaml`
518
519     echo  "credentials:" > credential.yaml
520     echo  "  $controllername:" >> credential.yaml
521     echo  "    opnfv-credentials:" >> credential.yaml
522     echo  "      auth-type: oauth1" >> credential.yaml
523     echo  "      maas-oauth: $API_KEY" >> credential.yaml
524
525     juju add-credential $controllername -f credential.yaml --replace
526 }
527
528 addcloud() {
529     controllername=`awk 'NR==1{print substr($1, 1, length($1)-1)}' deployconfig.yaml`
530     cloudname=`awk 'NR==1{print substr($1, 1, length($1)-1)}' deployconfig.yaml`
531
532     echo "clouds:" > maas-cloud.yaml
533     echo "   $cloudname:" >> maas-cloud.yaml
534     echo "      type: maas" >> maas-cloud.yaml
535     echo "      auth-types: [oauth1]" >> maas-cloud.yaml
536     echo "      endpoint: $API_SERVERMAAS" >> maas-cloud.yaml
537
538     juju add-cloud $cloudname maas-cloud.yaml --replace
539 }
540
541
542 #
543 # VLAN customization
544 #
545
546 case "$labname" in
547     'intelpod9' )
548         maas refresh
549         crvlanupdsubnet vlan904 fabric-1 "MgmtNetwork" 904 2 || true
550         crvlanupdsubnet vlan905 fabric-2 "PublicNetwork" 905 3 || true
551         crnodevlanint $vlan905 eth1 || true
552         crnodevlanint $vlan905 eth3 || true
553         enableautomodebyname eth1.905 AUTO "10.9.15.0/24" || true
554         enableautomodebyname eth3.905 AUTO "10.9.15.0/24" || true
555         enableautomodebyname eth0 AUTO "10.9.12.0/24" || true
556         enableautomodebyname eth2 AUTO "10.9.12.0/24" || true
557         ;;
558 esac
559
560 #
561 # Enable MAAS nodes interfaces
562 #
563
564 #read interface needed in Auto mode and enable it. Will be rmeoved once auto enablement will be implemented in the maas-deployer.
565
566 if [ -e ./deployconfig.yaml ]; then
567   enableiflist=`grep "interface-enable" deployconfig.yaml | cut -d ' ' -f 4 `
568   datanet=`grep "dataNetwork" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //'`
569   stornet=`grep "storageNetwork" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //'`
570   pubnet=`grep "publicNetwork" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //'`
571
572   # split EXTERNAL_NETWORK=first ip;last ip; gateway;network
573
574   if [ "$datanet" != "''" ]; then
575       EXTNET=(${enableiflist//,/ })
576       i="0"
577       while [ ! -z "${EXTNET[i]}" ];
578       do
579           enableautomode ${EXTNET[i]} AUTO $datanet || true
580           i=$[$i+1]
581       done
582
583   fi
584   if [ "$stornet" != "''" ]; then
585       EXTNET=(${enableiflist//,/ })
586       i="0"
587       while [ ! -z "${EXTNET[i]}" ];
588       do
589           enableautomode ${EXTNET[i]} AUTO $stornet || true
590           i=$[$i+1]
591       done
592   fi
593   if [ "$pubnet" != "''" ]; then
594       EXTNET=(${enableiflist//,/ })
595       i="0"
596       while [ ! -z "${EXTNET[i]}" ];
597       do
598           enableautomode ${EXTNET[i]} AUTO $pubnet || true
599           i=$[$i+1]
600       done
601   fi
602 fi
603
604
605 # Add the cloud and controller credentials for MAAS for that lab.
606 jujuver=`juju --version`
607
608 if [[ "$jujuver" > "2" ]]; then
609     addcloud
610     addcredential
611 fi
612
613 #
614 # End of scripts
615 #
616 echo " .... MAAS deployment finished successfully ...."