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