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