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