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