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