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