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