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