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