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