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