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