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