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