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