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