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