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