Allows domain name to be configured
[genesis.git] / foreman / ci / deploy.sh
1 #!/usr/bin/env bash
2
3 #Deploy script to install provisioning server for Foreman/QuickStack
4 #author: Tim Rozet (trozet@redhat.com)
5 #
6 #Uses Vagrant and VirtualBox
7 #VagrantFile uses bootsrap.sh which Installs Khaleesi
8 #Khaleesi will install and configure Foreman/QuickStack
9 #
10 #Pre-requisties:
11 #Supports 3 or 4 network interface configuration
12 #Target system must be RPM based
13 #Ensure the host's kernel is up to date (yum update)
14 #Provisioned nodes expected to have following order of network connections (note: not all have to exist, but order is maintained):
15 #eth0- admin network
16 #eth1- private network (+storage network in 3 NIC config)
17 #eth2- public network
18 #eth3- storage network
19 #script assumes /24 subnet mask
20
21 ##VARS
22 reset=`tput sgr0`
23 blue=`tput setaf 4`
24 red=`tput setaf 1`
25 green=`tput setaf 2`
26
27 declare -A interface_arr
28 declare -A controllers_ip_arr
29 declare -A admin_ip_arr
30 declare -A public_ip_arr
31
32 vm_dir=/var/opt/opnfv
33 script=`realpath $0`
34 ##END VARS
35
36 ##FUNCTIONS
37 display_usage() {
38   echo -e "\n\n${blue}This script is used to deploy Foreman/QuickStack Installer and Provision OPNFV Target System${reset}\n\n"
39   echo -e "\n${green}Make sure you have the latest kernel installed before running this script! (yum update kernel +reboot)${reset}\n"
40   echo -e "\nUsage:\n$0 [arguments] \n"
41   echo -e "\n   -no_parse : No variable parsing into config. Flag. \n"
42   echo -e "\n   -base_config : Full path of settings file to parse. Optional.  Will provide a new base settings file rather than the default.  Example:  -base_config /opt/myinventory.yml \n"
43   echo -e "\n   -virtual : Node virtualization instead of baremetal. Flag. \n"
44   echo -e "\n   -enable_virtual_dhcp : Run dhcp server instead of using static IPs.  Use this with -virtual only. \n"
45   echo -e "\n   -static_ip_range : static IP range to define when using virtual and when dhcp is not being used (default), must at least a 20 IP block.  Format: '192.168.1.1,192.168.1.20' \n"
46   echo -e "\n   -ping_site : site to use to verify IP connectivity from the VM when -virtual is used.  Format: -ping_site www.blah.com \n"
47   echo -e "\n   -floating_ip_count : number of IP address from the public range to be used for floating IP. Default is 20.\n"
48   echo -e "\n   -admin_nic : Baremetal NIC for the admin network.  Required if other "nic" arguments are used.  \
49 Not applicable with -virtual.  Example: -admin_nic em1"
50   echo -e "\n   -private_nic : Baremetal NIC for the private network.  Required if other "nic" arguments are used.  \
51 Not applicable with -virtual.  Example: -private_nic em2"
52   echo -e "\n   -public_nic : Baremetal NIC for the public network.  Required if other "nic" arguments are used.  \
53 Can also be used with -virtual.  Example: -public_nic em3"
54   echo -e "\n   -storage_nic : Baremetal NIC for the storage network.  Optional.  Not applicable with -virtual. \
55 Private NIC will be used for storage if not specified. Example: -storage_nic em4"
56 }
57
58 ##verify vm dir exists
59 ##params: none
60 function verify_vm_dir {
61   if [ -d "$vm_dir" ]; then
62     echo -e "\n\n${red}ERROR: VM Directory: $vm_dir already exists.  Environment not clean.  Please use clean.sh.  Exiting${reset}\n\n"
63     exit 1
64   else
65     mkdir -p $vm_dir
66   fi
67
68   chmod 700 $vm_dir
69
70   if [ ! -d $vm_dir ]; then
71     echo -e "\n\n${red}ERROR: Unable to create VM Directory: $vm_dir  Exiting${reset}\n\n"
72     exit -1
73   fi
74 }
75
76 ##find ip of interface
77 ##params: interface name
78 function find_ip {
79   ip addr show $1 | grep -Eo '^\s+inet\s+[\.0-9]+' | awk '{print $2}'
80 }
81
82 ##finds subnet of ip and netmask
83 ##params: ip, netmask
84 function find_subnet {
85   IFS=. read -r i1 i2 i3 i4 <<< "$1"
86   IFS=. read -r m1 m2 m3 m4 <<< "$2"
87   printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"
88 }
89
90 ##verify subnet has at least n IPs
91 ##params: subnet mask, n IPs
92 function verify_subnet_size {
93   IFS=. read -r i1 i2 i3 i4 <<< "$1"
94   num_ips_required=$2
95
96   ##this function assumes you would never need more than 254
97   ##we check here to make sure
98   if [ "$num_ips_required" -ge 254 ]; then
99     echo -e "\n\n${red}ERROR: allocating more than 254 IPs is unsupported...Exiting${reset}\n\n"
100     return 1
101   fi
102
103   ##we just return if 3rd octet is not 255
104   ##because we know the subnet is big enough
105   if [ "$i3" -ne 255 ]; then
106     return 0
107   elif [ $((254-$i4)) -ge "$num_ips_required" ]; then
108     return 0
109   else
110     echo -e "\n\n${red}ERROR: Subnet is too small${reset}\n\n"
111     return 1
112   fi
113 }
114
115 ##finds last usable ip (broadcast minus 1) of a subnet from an IP and netmask
116 ## Warning: This function only works for IPv4 at the moment.
117 ##params: ip, netmask
118 function find_last_ip_subnet {
119   IFS=. read -r i1 i2 i3 i4 <<< "$1"
120   IFS=. read -r m1 m2 m3 m4 <<< "$2"
121   IFS=. read -r s1 s2 s3 s4 <<< "$((i1 & m1)).$((i2 & m2)).$((i3 & m3)).$((i4 & m4))"
122   printf "%d.%d.%d.%d\n" "$((255 - $m1 + $s1))" "$((255 - $m2 + $s2))" "$((255 - $m3 + $s3))" "$((255 - $m4 + $s4 - 1))"
123 }
124
125 ##increments subnet by a value
126 ##params: ip, value
127 ##assumes low value
128 function increment_subnet {
129   IFS=. read -r i1 i2 i3 i4 <<< "$1"
130   printf "%d.%d.%d.%d\n" "$i1" "$i2" "$i3" "$((i4 | $2))"
131 }
132
133
134 ##finds netmask of interface
135 ##params: interface
136 ##returns long format 255.255.x.x
137 function find_netmask {
138   ifconfig $1 | grep -Eo 'netmask\s+[\.0-9]+' | awk '{print $2}'
139 }
140
141 ##finds short netmask of interface
142 ##params: interface
143 ##returns short format, ex: /21
144 function find_short_netmask {
145   echo "/$(ip addr show $1 | grep -Eo '^\s+inet\s+[\/\.0-9]+' | awk '{print $2}' | cut -d / -f2)"
146 }
147
148 ##increments next IP
149 ##params: ip
150 ##assumes a /24 subnet
151 function next_ip {
152   baseaddr="$(echo $1 | cut -d. -f1-3)"
153   lsv="$(echo $1 | cut -d. -f4)"
154   if [ "$lsv" -ge 254 ]; then
155     return 1
156   fi
157   ((lsv++))
158   echo $baseaddr.$lsv
159 }
160
161 ##subtracts a value from an IP address
162 ##params: last ip, ip_count
163 ##assumes ip_count is less than the last octect of the address
164 subtract_ip() {
165   IFS=. read -r i1 i2 i3 i4 <<< "$1"
166   ip_count=$2
167   if [ $i4 -lt $ip_count ]; then
168     echo -e "\n\n${red}ERROR: Can't subtract $ip_count from IP address $1  Exiting${reset}\n\n"
169     exit 1
170   fi
171   printf "%d.%d.%d.%d\n" "$i1" "$i2" "$i3" "$((i4 - $ip_count ))"
172 }
173
174 ##removes the network interface config from Vagrantfile
175 ##params: interface
176 ##assumes you are in the directory of Vagrantfile
177 function remove_vagrant_network {
178   sed -i 's/^.*'"$1"'.*$//' Vagrantfile
179 }
180
181 ##check if IP is in use
182 ##params: ip
183 ##ping ip to get arp entry, then check arp
184 function is_ip_used {
185   ping -c 5 $1 > /dev/null 2>&1
186   arp -n | grep "$1 " | grep -iv incomplete > /dev/null 2>&1
187 }
188
189 ##find next usable IP
190 ##params: ip
191 function next_usable_ip {
192   new_ip=$(next_ip $1)
193   while [ "$new_ip" ]; do
194     if ! is_ip_used $new_ip; then
195       echo $new_ip
196       return 0
197     fi
198     new_ip=$(next_ip $new_ip)
199   done
200   return 1
201 }
202
203 ##increment ip by value
204 ##params: ip, amount to increment by
205 ##increment_ip $next_private_ip 10
206 function increment_ip {
207   baseaddr="$(echo $1 | cut -d. -f1-3)"
208   lsv="$(echo $1 | cut -d. -f4)"
209   incrval=$2
210   lsv=$((lsv+incrval))
211   if [ "$lsv" -ge 254 ]; then
212     return 1
213   fi
214   echo $baseaddr.$lsv
215 }
216
217 ##translates yaml into variables
218 ##params: filename, prefix (ex. "config_")
219 ##usage: parse_yaml opnfv_ksgen_settings.yml "config_"
220 parse_yaml() {
221    local prefix=$2
222    local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
223    sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
224         -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p"  $1 |
225    awk -F$fs '{
226       indent = length($1)/2;
227       vname[indent] = $2;
228       for (i in vname) {if (i > indent) {delete vname[i]}}
229       if (length($3) > 0) {
230          vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
231          printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
232       }
233    }'
234 }
235
236 ##translates the command line paramaters into variables
237 ##params: $@ the entire command line is passed
238 ##usage: parse_cmd_line() "$@"
239 parse_cmdline() {
240   if [[ ( $1 == "--help") ||  $1 == "-h" ]]; then
241     display_usage
242     exit 0
243   fi
244
245   echo -e "\n\n${blue}This script is used to deploy Foreman/QuickStack Installer and Provision OPNFV Target System${reset}\n\n"
246   echo "Use -h to display help"
247   sleep 2
248
249   while [ "`echo $1 | cut -c1`" = "-" ]
250   do
251     echo $1
252     case "$1" in
253         -base_config)
254                 base_config=$2
255                 shift 2
256             ;;
257         -no_parse)
258                 no_parse="TRUE"
259                 shift 1
260             ;;
261         -virtual)
262                 virtual="TRUE"
263                 shift 1
264             ;;
265         -enable_virtual_dhcp)
266                 enable_virtual_dhcp="TRUE"
267                 shift 1
268             ;;
269         -static_ip_range)
270                 static_ip_range=$2
271                 shift 2
272             ;;
273         -ping_site)
274                 ping_site=$2
275                 shift 2
276             ;;
277         -floating_ip_count)
278                 floating_ip_count=$2
279                 shift 2
280             ;;
281         -admin_nic)
282                 admin_nic=$2
283                 shift 2
284                 nic_arg_flag=1
285             ;;
286         -private_nic)
287                 private_nic=$2
288                 shift 2
289                 nic_arg_flag=1
290             ;;
291         -public_nic)
292                 public_nic=$2
293                 shift 2
294                 nic_arg_flag=1
295             ;;
296         -storage_nic)
297                 storage_nic=$2
298                 shift 2
299                 nic_arg_flag=1
300             ;;
301         *)
302                 display_usage
303                 exit 1
304             ;;
305     esac
306   done
307
308   if [ ! -z "$enable_virtual_dhcp" ] && [ ! -z "$static_ip_range" ]; then
309     echo -e "\n\n${red}ERROR: Incorrect Usage.  Static IP range cannot be set when using DHCP!.  Exiting${reset}\n\n"
310     exit 1
311   fi
312
313   if [ -z "$virtual" ]; then
314     if [ ! -z "$enable_virtual_dhcp" ]; then
315       echo -e "\n\n${red}ERROR: Incorrect Usage.  enable_virtual_dhcp can only be set when using -virtual!.  Exiting${reset}\n\n"
316       exit 1
317     elif [ ! -z "$static_ip_range" ]; then
318       echo -e "\n\n${red}ERROR: Incorrect Usage.  static_ip_range can only be set when using -virtual!.  Exiting${reset}\n\n"
319       exit 1
320     fi
321   fi
322
323   if [ -z "$floating_ip_count" ]; then
324     floating_ip_count=20
325   fi
326
327   ##Validate nic args
328   if [[ $nic_arg_flag -eq 1 ]]; then
329     if [ -z "$virtual" ]; then
330       for nic_type in admin_nic private_nic public_nic; do
331         eval "nic_value=\$$nic_type"
332         if [ -z "$nic_value" ]; then
333           echo "${red}$nic_type is empty or not defined.  Required when other nic args are given!${reset}"
334           exit 1
335         fi
336         interface_ip=$(find_ip $nic_value)
337         if [ ! "$interface_ip" ]; then
338           echo "${red}$nic_value does not have an IP address! Exiting... ${reset}"
339           exit 1
340         fi
341       done
342     else
343       ##if virtual only public_nic should be specified
344       for nic_type in admin_nic private_nic storage_nic; do
345         eval "nic_value=\$$nic_type"
346         if [ ! -z "$nic_value" ]; then
347           echo "${red}$nic_type is not a valid argument using -virtual.  Please only specify public_nic!${reset}"
348           exit 1
349         fi
350       done
351
352       interface_ip=$(find_ip $public_nic)
353       if [ ! "$interface_ip" ]; then
354         echo "${red}Public NIC: $public_nic does not have an IP address! Exiting... ${reset}"
355         exit 1
356       fi
357     fi
358   fi
359 }
360
361 ##disable selinux
362 ##params: none
363 ##usage: disable_selinux()
364 disable_selinux() {
365   /sbin/setenforce 0
366 }
367
368 ##Install the EPEL repository and additional packages
369 ##params: none
370 ##usage: install_EPEL()
371 install_EPEL() {
372   # Install EPEL repo for access to many other yum repos
373   # Major version is pinned to force some consistency for Arno
374   yum install -y epel-release-7*
375
376   # Install other required packages
377   # Major versions are pinned to force some consistency for Arno
378   if ! yum install -y binutils-2* gcc-4* make-3* patch-2* libgomp-4* glibc-headers-2* glibc-devel-2* kernel-headers-3* kernel-devel-3* dkms-2* psmisc-22*; then
379     printf '%s\n' 'deploy.sh: Unable to install depdency packages' >&2
380     exit 1
381   fi
382 }
383
384 ##Download and install virtual box
385 ##params: none
386 ##usage: install_vbox()
387 install_vbox() {
388   ##install VirtualBox repo
389   if cat /etc/*release | grep -i "Fedora release"; then
390     vboxurl=http://download.virtualbox.org/virtualbox/rpm/fedora/\$releasever/\$basearch
391   else
392     vboxurl=http://download.virtualbox.org/virtualbox/rpm/el/\$releasever/\$basearch
393   fi
394
395   cat > /etc/yum.repos.d/virtualbox.repo << EOM
396 [virtualbox]
397 name=Oracle Linux / RHEL / CentOS-\$releasever / \$basearch - VirtualBox
398 baseurl=$vboxurl
399 enabled=1
400 gpgcheck=1
401 gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
402 skip_if_unavailable = 1
403 keepcache = 0
404 EOM
405
406   ##install VirtualBox
407   if ! yum list installed | grep -i virtualbox; then
408     if ! yum -y install VirtualBox-4.3; then
409       printf '%s\n' 'deploy.sh: Unable to install virtualbox package' >&2
410       exit 1
411     fi
412   fi
413
414   ##install kmod-VirtualBox
415   if ! lsmod | grep vboxdrv; then
416     sudo /etc/init.d/vboxdrv setup
417     if ! lsmod | grep vboxdrv; then
418       printf '%s\n' 'deploy.sh: Unable to install kernel module for virtualbox' >&2
419       exit 1
420     fi
421   else
422     printf '%s\n' 'deploy.sh: Skipping kernel module for virtualbox.  Already Installed'
423   fi
424 }
425
426 ##install Ansible using yum
427 ##params: none
428 ##usage: install_ansible()
429 install_ansible() {
430   if ! yum list installed | grep -i ansible; then
431     if ! yum -y install ansible-1*; then
432       printf '%s\n' 'deploy.sh: Unable to install Ansible package' >&2
433       exit 1
434     fi
435   fi
436 }
437
438 ##install Vagrant RPM directly with the bintray.com site
439 ##params: none
440 ##usage: install_vagrant()
441 install_vagrant() {
442   if ! rpm -qa | grep vagrant; then
443     if ! rpm -Uvh https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.rpm; then
444       printf '%s\n' 'deploy.sh: Unable to install vagrant package' >&2
445       exit 1
446     fi
447   else
448     printf '%s\n' 'deploy.sh: Skipping Vagrant install as it is already installed.'
449   fi
450
451   ##add centos 7 box to vagrant
452   if ! vagrant box list | grep opnfv/centos-7.0; then
453     if ! vagrant box add opnfv/centos-7.0 --provider virtualbox; then
454       printf '%s\n' 'deploy.sh: Unable to download centos7 box for Vagrant' >&2
455       exit 1
456     fi
457   else
458     printf '%s\n' 'deploy.sh: Skipping Vagrant box add as centos-7.0 is already installed.'
459   fi
460
461   ##install workaround for centos7
462   if ! vagrant plugin list | grep vagrant-centos7_fix; then
463     if ! vagrant plugin install vagrant-centos7_fix; then
464       printf '%s\n' 'deploy.sh: Warning: unable to install vagrant centos7 workaround' >&2
465     fi
466   else
467     printf '%s\n' 'deploy.sh: Skipping Vagrant plugin as centos7 workaround is already installed.'
468   fi
469 }
470
471
472 ##remove bgs vagrant incase it wasn't cleaned up
473 ##params: none
474 ##usage: clean_tmp()
475 clean_tmp() {
476   rm -rf $vm_dir/foreman_vm
477 }
478
479 ##clone genesis and move to node vm dir
480 ##params: destination directory
481 ##usage: clone_bgs /tmp/myvm/
482 clone_bgs() {
483   script_dir="`dirname "$script"`"
484   cp -fr $script_dir/ $1
485   cp -fr $script_dir/../../common/puppet-opnfv $1
486 }
487
488 ##validates the network settings and update VagrantFile with network settings
489 ##params: none
490 ##usage: configure_network()
491 configure_network() {
492   cd $vm_dir/foreman_vm
493
494   ##if nic_arg_flag is set, then we don't figure out
495   ##NICs dynamically
496   if [[ $nic_arg_flag -eq 1 ]]; then
497     echo "${blue}Static Network Interfaces Defined.  Updating Vagrantfile...${reset}"
498     if [ $virtual ]; then
499       nic_list="$public_nic"
500     elif [ -z "$storage_nic" ]; then
501       echo "${blue}storage_nic not defined, will combine storage into private VLAN ${reset}"
502       nic_list="$admin_nic $private_nic $public_nic"
503     else
504       nic_list="$admin_nic $private_nic $public_nic $storage_nic"
505     fi
506     nic_array=( $nic_list )
507     output=$nic_list
508   else
509     echo "${blue}Detecting network configuration...${reset}"
510     ##detect host 1 or 3 interface configuration
511     #output=`ip link show | grep -E "^[0-9]" | grep -Ev ": lo|tun|virbr|vboxnet" | awk '{print $2}' | sed 's/://'`
512     output=`/bin/ls -l /sys/class/net | tail -n +2 | grep -v virtual | cut -d " " -f10`
513   fi
514
515   if [ ! "$output" ]; then
516     printf '%s\n' 'deploy.sh: Unable to detect interfaces to bridge to' >&2
517     exit 1
518   fi
519
520   ##virtual we only find 1 interface
521   if [ $virtual ]; then
522     if [ ! -z "${nic_array[0]}" ]; then
523       echo "${blue}Public Interface specified: ${nic_array[0]}${reset}"
524       this_default_gw_interface=${nic_array[0]}
525     else
526       ##find interface with default gateway
527       this_default_gw=$(ip route | grep default | awk '{print $3}')
528       echo "${blue}Default Gateway: $this_default_gw ${reset}"
529       this_default_gw_interface=$(ip route get $this_default_gw | awk '{print $3}')
530     fi
531
532     ##find interface IP, make sure its valid
533     interface_ip=$(find_ip $this_default_gw_interface)
534     if [ ! "$interface_ip" ]; then
535       echo "${red}Interface ${this_default_gw_interface} does not have an IP: $interface_ip ! Exiting ${reset}"
536       exit 1
537     fi
538
539     ##set variable info
540     if [ ! -z "$static_ip_range" ]; then
541       new_ip=$(echo $static_ip_range | cut -d , -f1)
542       subnet_mask=$(find_netmask $this_default_gw_interface)
543       host_subnet=$(find_subnet $interface_ip $subnet_mask)
544       ip_range_subnet=$(find_subnet $new_ip $subnet_mask)
545       if [ "$ip_range_subnet" != "$host_subnet" ]; then
546         echo "${red}static_ip_range: ${static_ip_range} is not in the same subnet as your default gateway interface: ${host_subnet}.  Please use a correct range!${reset}"
547         exit 1
548       fi
549     else
550       new_ip=$(next_usable_ip $interface_ip)
551       if [ ! "$new_ip" ]; then
552         echo "${red} Cannot find next IP on interface ${this_default_gw_interface} new_ip: $new_ip ! Exiting ${reset}"
553         exit 1
554       fi
555     fi
556     interface=$this_default_gw_interface
557     public_interface=$interface
558     interface_arr[$interface]=2
559     interface_ip_arr[2]=$new_ip
560     subnet_mask=$(find_netmask $interface)
561     public_subnet_mask=$subnet_mask
562     public_short_subnet_mask=$(find_short_netmask $interface)
563
564     if ! verify_subnet_size $public_subnet_mask 25; then
565       echo "${red} Not enough IPs in public subnet: $interface_ip_arr[2] ${public_subnet_mask}.  Need at least 25 IPs.  Please resize subnet! Exiting ${reset}"
566       exit 1
567     fi
568
569     ##set that interface to be public
570     sed -i 's/^.*eth_replace2.*$/  config.vm.network "public_network", ip: '\""$new_ip"\"', bridge: '\'"$interface"\'', netmask: '\""$subnet_mask"\"'/' Vagrantfile
571     if_counter=1
572   else
573     ##find number of interfaces with ip and substitute in VagrantFile
574     if_counter=0
575     for interface in ${output}; do
576
577       if [ "$if_counter" -ge 4 ]; then
578         break
579       fi
580       interface_ip=$(find_ip $interface)
581       if [ ! "$interface_ip" ]; then
582         continue
583       fi
584       new_ip=$(next_usable_ip $interface_ip)
585       if [ ! "$new_ip" ]; then
586         continue
587       fi
588       interface_arr[$interface]=$if_counter
589       interface_ip_arr[$if_counter]=$new_ip
590       subnet_mask=$(find_netmask $interface)
591       if [ "$if_counter" -eq 0 ]; then
592         admin_subnet_mask=$subnet_mask
593         if ! verify_subnet_size $admin_subnet_mask 5; then
594           echo "${red} Not enough IPs in admin subnet: ${interface_ip_arr[$if_counter]} ${admin_subnet_mask}.  Need at least 5 IPs.  Please resize subnet! Exiting ${reset}"
595           exit 1
596         fi
597
598       elif [ "$if_counter" -eq 1 ]; then
599         private_subnet_mask=$subnet_mask
600         private_short_subnet_mask=$(find_short_netmask $interface)
601
602         if ! verify_subnet_size $private_subnet_mask 15; then
603           echo "${red} Not enough IPs in private subnet: ${interface_ip_arr[$if_counter]} ${private_subnet_mask}.  Need at least 15 IPs.  Please resize subnet! Exiting ${reset}"
604           exit 1
605         fi
606       elif [ "$if_counter" -eq 2 ]; then
607         public_subnet_mask=$subnet_mask
608         public_short_subnet_mask=$(find_short_netmask $interface)
609
610         if ! verify_subnet_size $public_subnet_mask 25; then
611           echo "${red} Not enough IPs in public subnet: ${interface_ip_arr[$if_counter]} ${public_subnet_mask}.  Need at least 25 IPs.  Please resize subnet! Exiting ${reset}"
612           exit 1
613         fi
614       elif [ "$if_counter" -eq 3 ]; then
615         storage_subnet_mask=$subnet_mask
616
617         if ! verify_subnet_size $storage_subnet_mask 10; then
618           echo "${red} Not enough IPs in storage subnet: ${interface_ip_arr[$if_counter]} ${storage_subnet_mask}.  Need at least 10 IPs.  Please resize subnet! Exiting ${reset}"
619           exit 1
620         fi
621       else
622         echo "${red}ERROR: interface counter outside valid range of 0 to 3: $if_counter ! ${reset}"
623         exit 1
624       fi
625       sed -i 's/^.*eth_replace'"$if_counter"'.*$/  config.vm.network "public_network", ip: '\""$new_ip"\"', bridge: '\'"$interface"\'', netmask: '\""$subnet_mask"\"'/' Vagrantfile
626       ((if_counter++))
627     done
628   fi
629   ##now remove interface config in Vagrantfile for 1 node
630   ##if 1, 3, or 4 interfaces set deployment type
631   ##if 2 interfaces remove 2nd interface and set deployment type
632   if [[ "$if_counter" == 1 || "$if_counter" == 2 ]]; then
633     if [ $virtual ]; then
634       deployment_type="single_network"
635       echo "${blue}Single network detected for Virtual deployment...converting to three_network with internal networks! ${reset}"
636       private_internal_ip=155.1.2.2
637       admin_internal_ip=156.1.2.2
638       private_subnet_mask=255.255.255.0
639       private_short_subnet_mask=/24
640       interface_ip_arr[1]=$private_internal_ip
641       interface_ip_arr[0]=$admin_internal_ip
642       admin_subnet_mask=255.255.255.0
643       admin_short_subnet_mask=/24
644       sed -i 's/^.*eth_replace1.*$/  config.vm.network "private_network", virtualbox__intnet: "my_private_network", ip: '\""$private_internal_ip"\"', netmask: '\""$private_subnet_mask"\"'/' Vagrantfile
645       sed -i 's/^.*eth_replace0.*$/  config.vm.network "private_network", virtualbox__intnet: "my_admin_network", ip: '\""$admin_internal_ip"\"', netmask: '\""$private_subnet_mask"\"'/' Vagrantfile
646       remove_vagrant_network eth_replace3
647       deployment_type=three_network
648     else
649        echo "${blue}Single network or 2 network detected for baremetal deployment.  This is unsupported! Exiting. ${reset}"
650        exit 1
651     fi
652   elif [ "$if_counter" == 3 ]; then
653     deployment_type="three_network"
654     remove_vagrant_network eth_replace3
655   else
656     deployment_type="multi_network"
657   fi
658
659   echo "${blue}Network detected: ${deployment_type}! ${reset}"
660
661   if [ $virtual ]; then
662     if [ -z "$enable_virtual_dhcp" ]; then
663       sed -i 's/^.*disable_dhcp_flag =.*$/  disable_dhcp_flag = true/' Vagrantfile
664       if [ $static_ip_range ]; then
665         ##verify static range is at least 20 IPs
666         static_ip_range_begin=$(echo $static_ip_range | cut -d , -f1)
667         static_ip_range_end=$(echo $static_ip_range | cut -d , -f2)
668         ##verify range is at least 20 ips
669         ##assumes less than 255 range pool
670         begin_octet=$(echo $static_ip_range_begin | cut -d . -f4)
671         end_octet=$(echo $static_ip_range_end | cut -d . -f4)
672         ip_count=$((end_octet-begin_octet+1))
673         if [ "$ip_count" -lt 20 ]; then
674           echo "${red}Static range is less than 20 ips: ${ip_count}, exiting  ${reset}"
675           exit 1
676         else
677           echo "${blue}Static IP range is size $ip_count ${reset}"
678         fi
679       fi
680     fi
681   fi
682
683   if route | grep default; then
684     echo "${blue}Default Gateway Detected ${reset}"
685     host_default_gw=$(ip route | grep default | awk '{print $3}')
686     echo "${blue}Default Gateway: $host_default_gw ${reset}"
687     default_gw_interface=$(ip route get $host_default_gw | awk '{print $3}')
688     case "${interface_arr[$default_gw_interface]}" in
689       0)
690         echo "${blue}Default Gateway Detected on Admin Interface!${reset}"
691         sed -i 's/^.*default_gw =.*$/  default_gw = '\""$host_default_gw"\"'/' Vagrantfile
692         node_default_gw=$host_default_gw
693         ;;
694       1)
695         echo "${red}Default Gateway Detected on Private Interface!${reset}"
696         echo "${red}Private subnet should be private and not have Internet access!${reset}"
697         exit 1
698         ;;
699       2)
700         echo "${blue}Default Gateway Detected on Public Interface!${reset}"
701         sed -i 's/^.*default_gw =.*$/  default_gw = '\""$host_default_gw"\"'/' Vagrantfile
702         echo "${blue}Will setup NAT from Admin -> Public Network on VM!${reset}"
703         sed -i 's/^.*nat_flag =.*$/  nat_flag = true/' Vagrantfile
704         echo "${blue}Setting node gateway to be VM Admin IP${reset}"
705         node_default_gw=${interface_ip_arr[0]}
706         public_gateway=$default_gw
707         ;;
708       3)
709         echo "${red}Default Gateway Detected on Storage Interface!${reset}"
710         echo "${red}Storage subnet should be private and not have Internet access!${reset}"
711         exit 1
712         ;;
713       *)
714         echo "${red}Unable to determine which interface default gateway is on..Exiting!${reset}"
715         exit 1
716         ;;
717     esac
718   else
719     #assumes 24 bit mask
720     defaultgw=`echo ${interface_ip_arr[0]} | cut -d. -f1-3`
721     firstip=.1
722     defaultgw=$defaultgw$firstip
723     echo "${blue}Unable to find default gateway.  Assuming it is $defaultgw ${reset}"
724     sed -i 's/^.*default_gw =.*$/  default_gw = '\""$defaultgw"\"'/' Vagrantfile
725     node_default_gw=$defaultgw
726   fi
727
728   if [ $base_config ]; then
729     if ! cp -f $base_config opnfv_ksgen_settings.yml; then
730       echo "{red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
731       exit 1
732     fi
733   fi
734
735   nodes=`sed -nr '/nodes:/{:start /workaround/!{N;b start};//p}' opnfv_ksgen_settings.yml | sed -n '/^  [A-Za-z0-9]\+:$/p' | sed 's/\s*//g' | sed 's/://g'`
736   controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
737   echo "${blue}Controller nodes found in settings: ${controller_nodes}${reset}"
738   my_controller_array=( $controller_nodes )
739   num_control_nodes=${#my_controller_array[@]}
740   if [ "$num_control_nodes" -ne 3 ]; then
741     if cat opnfv_ksgen_settings.yml | grep ha_flag | grep true; then
742       echo "${red}Error: You must define exactly 3 control nodes when HA flag is true!${reset}"
743       exit 1
744     fi
745   else
746     echo "${blue}Number of Controller nodes detected: ${num_control_nodes}${reset}"
747   fi
748
749   if [ $no_parse ]; then
750     echo "${blue}Skipping parsing variables into settings file as no_parse flag is set${reset}"
751
752   else
753
754     echo "${blue}Gathering network parameters for Target System...this may take a few minutes${reset}"
755     ##Edit the ksgen settings appropriately
756     ##ksgen settings will be stored in /vagrant on the vagrant machine
757     ##if single node deployment all the variables will have the same ip
758     ##interface names will be enp0s3, enp0s8, enp0s9 in chef/centos7
759
760     sed -i 's/^.*default_gw:.*$/default_gw:'" $node_default_gw"'/' opnfv_ksgen_settings.yml
761
762     ##replace private interface parameter
763     ##private interface will be of hosts, so we need to know the provisioned host interface name
764     ##we add biosdevname=0, net.ifnames=0 to the kickstart to use regular interface naming convention on hosts
765     ##replace IP for parameters with next IP that will be given to controller
766     if [ "$deployment_type" == "single_network" ]; then
767       ##we also need to assign IP addresses to nodes
768       ##for single node, foreman is managing the single network, so we can't reserve them
769       ##not supporting single network anymore for now
770       echo "{blue}Single Network type is unsupported right now.  Please check your interface configuration.  Exiting. ${reset}"
771       exit 0
772
773     elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_network" ]]; then
774
775       if [ "$deployment_type" == "three_network" ]; then
776         sed -i 's/^.*network_type:.*$/network_type: three_network/' opnfv_ksgen_settings.yml
777       fi
778
779       sed -i 's/^.*deployment_type:.*$/  deployment_type: '"$deployment_type"'/' opnfv_ksgen_settings.yml
780
781       ##get ip addresses for private network on controllers to make dhcp entries
782       ##required for controllers_ip_array global param
783       next_private_ip=${interface_ip_arr[1]}
784       type=_private
785       control_count=0
786       for node in controller1 controller2 controller3; do
787         next_private_ip=$(next_usable_ip $next_private_ip)
788         if [ ! "$next_private_ip" ]; then
789           printf '%s\n' 'deploy.sh: Unable to find next ip for private network for control nodes' >&2
790           exit 1
791         fi
792         sed -i 's/'"$node$type"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
793         controller_ip_array=$controller_ip_array$next_private_ip,
794         controllers_ip_arr[$control_count]=$next_private_ip
795         ((control_count++))
796       done
797
798       next_public_ip=${interface_ip_arr[2]}
799       foreman_ip=$next_public_ip
800
801       ##if no dhcp, find all the Admin IPs for nodes in advance
802       if [ $virtual ]; then
803         if [ -z "$enable_virtual_dhcp" ]; then
804           sed -i 's/^.*no_dhcp:.*$/no_dhcp: true/' opnfv_ksgen_settings.yml
805           nodes=`sed -nr '/nodes:/{:start /workaround/!{N;b start};//p}' opnfv_ksgen_settings.yml | sed -n '/^  [A-Za-z0-9]\+:$/p' | sed 's/\s*//g' | sed 's/://g'`
806           compute_nodes=`echo $nodes | tr " " "\n" | grep -v controller | tr "\n" " "`
807           controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
808           nodes=${controller_nodes}${compute_nodes}
809           next_admin_ip=${interface_ip_arr[0]}
810           type=_admin
811           for node in ${nodes}; do
812             next_admin_ip=$(next_ip $next_admin_ip)
813             if [ ! "$next_admin_ip" ]; then
814               echo "${red} Unable to find an unused IP in admin_network for $node ! ${reset}"
815               exit 1
816             else
817               admin_ip_arr[$node]=$next_admin_ip
818               sed -i 's/'"$node$type"'/'"$next_admin_ip"'/g' opnfv_ksgen_settings.yml
819             fi
820           done
821
822           ##allocate node public IPs
823           for node in ${nodes}; do
824             next_public_ip=$(next_usable_ip $next_public_ip)
825             if [ ! "$next_public_ip" ]; then
826               echo "${red} Unable to find an unused IP in admin_network for $node ! ${reset}"
827               exit 1
828             else
829               public_ip_arr[$node]=$next_public_ip
830             fi
831           done
832         fi
833       fi
834       ##replace global param for controllers_ip_array
835       controller_ip_array=${controller_ip_array%?}
836       sed -i 's/^.*controllers_ip_array:.*$/  controllers_ip_array: '"$controller_ip_array"'/' opnfv_ksgen_settings.yml
837
838       ##now replace all the VIP variables.  admin//private can be the same IP
839       ##we have to use IP's here that won't be allocated to hosts at provisioning time
840       ##therefore we increment the ip by 10 to make sure we have a safe buffer
841       next_private_ip=$(increment_ip $next_private_ip 10)
842
843       private_output=$(grep -E '*private_vip|loadbalancer_vip|db_vip|amqp_vip|*admin_vip' opnfv_ksgen_settings.yml)
844       if [ ! -z "$private_output" ]; then
845         while read -r line; do
846           sed -i 's/^.*'"$line"'.*$/  '"$line $next_private_ip"'/' opnfv_ksgen_settings.yml
847           next_private_ip=$(next_usable_ip $next_private_ip)
848           if [ ! "$next_private_ip" ]; then
849             printf '%s\n' 'deploy.sh: Unable to find next ip for private network for vip replacement' >&2
850             exit 1
851           fi
852         done <<< "$private_output"
853       fi
854
855       ##replace odl_control_ip (non-HA only)
856       odl_control_ip=${controllers_ip_arr[0]}
857       sed -i 's/^.*odl_control_ip:.*$/  odl_control_ip: '"$odl_control_ip"'/' opnfv_ksgen_settings.yml
858
859       ##replace controller_ip (non-HA only)
860       sed -i 's/^.*controller_ip:.*$/  controller_ip: '"$odl_control_ip"'/' opnfv_ksgen_settings.yml
861
862       ##replace foreman site
863       sed -i 's/^.*foreman_url:.*$/  foreman_url:'" https:\/\/$foreman_ip"'\/api\/v2\//' opnfv_ksgen_settings.yml
864       ##replace public vips
865       ##no need to do this if no dhcp
866       if [[ -z "$enable_virtual_dhcp" && ! -z "$virtual" ]]; then
867         next_public_ip=$(next_usable_ip $next_public_ip)
868       else
869         next_public_ip=$(increment_ip $next_public_ip 10)
870       fi
871
872       public_output=$(grep -E '*public_vip' opnfv_ksgen_settings.yml)
873       if [ ! -z "$public_output" ]; then
874         while read -r line; do
875           if echo $line | grep horizon_public_vip; then
876             horizon_public_vip=$next_public_ip
877           fi
878           sed -i 's/^.*'"$line"'.*$/  '"$line $next_public_ip"'/' opnfv_ksgen_settings.yml
879           next_public_ip=$(next_usable_ip $next_public_ip)
880           if [ ! "$next_public_ip" ]; then
881             printf '%s\n' 'deploy.sh: Unable to find next ip for public network for vip replcement' >&2
882             exit 1
883           fi
884         done <<< "$public_output"
885       fi
886
887       ##replace public_network param
888       public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
889       sed -i 's/^.*public_network:.*$/  public_network:'" $public_subnet"'/' opnfv_ksgen_settings.yml
890       ##replace private_network param
891       private_subnet=$(find_subnet $next_private_ip $private_subnet_mask)
892       sed -i 's/^.*private_network:.*$/  private_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
893       ##replace storage_network
894       if [ "$deployment_type" == "three_network" ]; then
895         sed -i 's/^.*storage_network:.*$/  storage_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
896       else
897         next_storage_ip=${interface_ip_arr[3]}
898         storage_subnet=$(find_subnet $next_storage_ip $storage_subnet_mask)
899         sed -i 's/^.*storage_network:.*$/  storage_network:'" $storage_subnet"'/' opnfv_ksgen_settings.yml
900       fi
901
902       ##replace public_subnet param
903       public_subnet=$public_subnet'\'$public_short_subnet_mask
904       sed -i 's/^.*public_subnet:.*$/  public_subnet:'" $public_subnet"'/' opnfv_ksgen_settings.yml
905       ##replace private_subnet param
906       private_subnet=$private_subnet'\'$private_short_subnet_mask
907       sed -i 's/^.*private_subnet:.*$/  private_subnet:'" $private_subnet"'/' opnfv_ksgen_settings.yml
908
909       ##replace public_dns param to be foreman server
910       sed -i 's/^.*public_dns:.*$/  public_dns: '${interface_ip_arr[2]}'/' opnfv_ksgen_settings.yml
911
912       ##replace public_gateway
913       if [ -z "$public_gateway" ]; then
914         ##if unset then we assume its the first IP in the public subnet
915         public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
916         public_gateway=$(increment_subnet $public_subnet 1)
917       fi
918       sed -i 's/^.*public_gateway:.*$/  public_gateway:'" $public_gateway"'/' opnfv_ksgen_settings.yml
919
920       ##we have to define an allocation range of the public subnet to give
921       ##to neutron to use as floating IPs
922       ##if static ip range, then we take the difference of the end range and current ip
923       ## to be the allocation pool
924       ##if not static ip, we will use the last 20 IP from the subnet
925       ## note that this is not a really good idea because the subnet must be at least a /27 for this to work...
926       public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
927       if [ ! -z "$static_ip_range" ]; then
928         begin_octet=$(echo $next_public_ip | cut -d . -f4)
929         end_octet=$(echo $static_ip_range_end | cut -d . -f4)
930         ip_diff=$((end_octet-begin_octet))
931         if [ $ip_diff -le 0 ]; then
932           echo "${red}ip range left for floating range is less than or equal to 0! $ipdiff ${reset}"
933           exit 1
934         else
935           public_allocation_start=$(next_ip $next_public_ip)
936           public_allocation_end=$static_ip_range_end
937         fi
938       else
939         last_ip_subnet=$(find_last_ip_subnet $next_public_ip $public_subnet_mask)
940         public_allocation_start=$(subtract_ip $last_ip_subnet $floating_ip_count )
941         public_allocation_end=${last_ip_subnet}
942       fi
943       echo "${blue}Neutron Floating IP range: $public_allocation_start to $public_allocation_end ${reset}"
944
945       sed -i 's/^.*public_allocation_start:.*$/  public_allocation_start:'" $public_allocation_start"'/' opnfv_ksgen_settings.yml
946       sed -i 's/^.*public_allocation_end:.*$/  public_allocation_end:'" $public_allocation_end"'/' opnfv_ksgen_settings.yml
947
948     else
949       printf '%s\n' 'deploy.sh: Unknown network type: $deployment_type' >&2
950       exit 1
951     fi
952
953     echo "${blue}Parameters Complete.  Settings have been set for Foreman. ${reset}"
954
955   fi
956 }
957
958 ##Configure bootstrap.sh to use the virtual Khaleesi playbook
959 ##params: none
960 ##usage: configure_virtual()
961 configure_virtual() {
962   if [ $virtual ]; then
963     echo "${blue} Virtual flag detected, setting Khaleesi playbook to be opnfv-vm.yml ${reset}"
964     sed -i 's/opnfv.yml/opnfv-vm.yml/' bootstrap.sh
965   fi
966 }
967
968 ##Starts Foreman VM with Vagrant
969 ##params: none
970 ##usage: start_vagrant()
971 start_foreman() {
972   echo "${blue}Starting Vagrant! ${reset}"
973
974   ##stand up vagrant
975   if ! vagrant up; then
976     printf '%s\n' 'deploy.sh: Unable to complete Foreman VM install' >&2
977     exit 1
978   else
979     echo "${blue}Foreman VM is up! ${reset}"
980   fi
981 }
982
983 ##start the VM if this is a virtual installation
984 ##this function does nothing if baremetal servers are being used
985 ##params: none
986 ##usage: start_virtual_nodes()
987 start_virtual_nodes() {
988   if [ $virtual ]; then
989
990     ##Bring up VM nodes
991     echo "${blue}Setting VMs up... ${reset}"
992     nodes=`sed -nr '/nodes:/{:start /workaround/!{N;b start};//p}' opnfv_ksgen_settings.yml | sed -n '/^  [A-Za-z0-9]\+:$/p' | sed 's/\s*//g' | sed 's/://g'`
993     ##due to ODL Helium bug of OVS connecting to ODL too early, we need controllers to install first
994     ##this is fix kind of assumes more than I would like to, but for now it should be OK as we always have
995     ##3 static controllers
996     compute_nodes=`echo $nodes | tr " " "\n" | grep -v controller | tr "\n" " "`
997     controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
998     nodes=${controller_nodes}${compute_nodes}
999     controller_count=0
1000     compute_wait_completed=false
1001
1002     for node in ${nodes}; do
1003
1004       ##remove VM nodes incase it wasn't cleaned up
1005       rm -rf $vm_dir/$node
1006       rm -rf /tmp/genesis/
1007
1008       ##clone genesis and move into node folder
1009       clone_bgs $vm_dir/$node
1010
1011       cd $vm_dir/$node
1012
1013       if [ $base_config ]; then
1014         if ! cp -f $base_config opnfv_ksgen_settings.yml; then
1015           echo "${red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
1016           exit 1
1017         fi
1018       fi
1019
1020       ##parse yaml into variables
1021       eval $(parse_yaml opnfv_ksgen_settings.yml "config_")
1022       ##find node type
1023       node_type=config_nodes_${node}_type
1024       node_type=$(eval echo \$$node_type)
1025
1026       ##trozet test make compute nodes wait 20 minutes
1027       if [ "$compute_wait_completed" = false ] && [ "$node_type" != "controller" ]; then
1028         echo "${blue}Waiting 20 minutes for Control nodes to install before continuing with Compute nodes..."
1029         compute_wait_completed=true
1030         sleep 1400
1031       fi
1032
1033       ## Add Admin interface
1034       mac_string=config_nodes_${node}_mac_address
1035       mac_addr=$(eval echo \$$mac_string)
1036       mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
1037       if [ $mac_addr == "" ]; then
1038         echo "${red} Unable to find mac_address for $node! ${reset}"
1039         exit 1
1040       fi
1041       this_admin_ip=${admin_ip_arr[$node]}
1042       sed -i 's/^.*eth_replace0.*$/  config.vm.network "private_network", virtualbox__intnet: "my_admin_network", ip: '\""$this_admin_ip"\"', netmask: '\""$admin_subnet_mask"\"', :mac => '\""$mac_addr"\"'/' Vagrantfile
1043
1044       ## Add private interface
1045       if [ "$node_type" == "controller" ]; then
1046           mac_string=config_nodes_${node}_private_mac
1047           mac_addr=$(eval echo \$$mac_string)
1048           if [ $mac_addr == "" ]; then
1049             echo "${red} Unable to find private_mac for $node! ${reset}"
1050             exit 1
1051           fi
1052       else
1053           ##generate random mac
1054           mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
1055       fi
1056       mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
1057       if [ "$node_type" == "controller" ]; then
1058         new_node_ip=${controllers_ip_arr[$controller_count]}
1059         if [ ! "$new_node_ip" ]; then
1060           echo "{red}ERROR: Empty node ip for controller $controller_count ${reset}"
1061           exit 1
1062         fi
1063         ((controller_count++))
1064       else
1065         next_private_ip=$(next_ip $next_private_ip)
1066         if [ ! "$next_private_ip" ]; then
1067           echo "{red}ERROR: Could not find private ip for $node ${reset}"
1068           exit 1
1069         fi
1070         new_node_ip=$next_private_ip
1071       fi
1072       sed -i 's/^.*eth_replace1.*$/  config.vm.network "private_network", virtualbox__intnet: "my_private_network", :mac => '\""$mac_addr"\"', ip: '\""$new_node_ip"\"', netmask: '\""$private_subnet_mask"\"'/' Vagrantfile
1073       ##replace host_ip in vm_nodes_provision with private ip
1074       sed -i 's/^host_ip=REPLACE/host_ip='$new_node_ip'/' vm_nodes_provision.sh
1075       ##replace ping site
1076       if [ ! -z "$ping_site" ]; then
1077         sed -i 's/www.google.com/'$ping_site'/' vm_nodes_provision.sh
1078       fi
1079
1080       ##find public ip info and add public interface
1081       mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
1082       mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
1083       this_public_ip=${public_ip_arr[$node]}
1084
1085       if [ -z "$enable_virtual_dhcp" ]; then
1086         sed -i 's/^.*eth_replace2.*$/  config.vm.network "public_network", bridge: '\'"$public_interface"\'', :mac => '\""$mac_addr"\"', ip: '\""$this_public_ip"\"', netmask: '\""$public_subnet_mask"\"'/' Vagrantfile
1087       else
1088         sed -i 's/^.*eth_replace2.*$/  config.vm.network "public_network", bridge: '\'"$public_interface"\'', :mac => '\""$mac_addr"\"'/' Vagrantfile
1089       fi
1090       remove_vagrant_network eth_replace3
1091
1092       ##modify provisioning to do puppet install, config, and foreman check-in
1093       ##substitute host_name and dns_server in the provisioning script
1094       host_string=config_nodes_${node}_short_name
1095       short_host_name=$(eval echo \$$host_string)
1096       ##substitute domain_name
1097       domain_name=$config_domain_name
1098       sed -i 's/^domain_name=REPLACE/domain_name='$domain_name'/' vm_nodes_provision.sh
1099       host_name=${short_host_name}.${domain_name}
1100       sed -i 's/^host_name=REPLACE/host_name='$host_name'/' vm_nodes_provision.sh
1101       ##dns server should be the foreman server
1102       sed -i 's/^dns_server=REPLACE/dns_server='${interface_ip_arr[0]}'/' vm_nodes_provision.sh
1103       ## remove bootstrap and NAT provisioning
1104       sed -i '/nat_setup.sh/d' Vagrantfile
1105       sed -i 's/bootstrap.sh/vm_nodes_provision.sh/' Vagrantfile
1106       ## modify default_gw to be node_default_gw
1107       sed -i 's/^.*default_gw =.*$/  default_gw = '\""$node_default_gw"\"'/' Vagrantfile
1108       ## modify VM memory to be 4gig
1109       ##if node type is controller
1110       if [ "$node_type" == "controller" ]; then
1111         sed -i 's/^.*vb.memory =.*$/     vb.memory = 4096/' Vagrantfile
1112       fi
1113       echo "${blue}Starting Vagrant Node $node! ${reset}"
1114       ##stand up vagrant
1115       if ! vagrant up; then
1116         echo "${red} Unable to start $node ${reset}"
1117         exit 1
1118       else
1119         echo "${blue} $node VM is up! ${reset}"
1120       fi
1121     done
1122     echo "${blue} All VMs are UP! ${reset}"
1123     echo "${blue} Waiting for puppet to complete on the nodes... ${reset}"
1124     ##check puppet is complete
1125     ##ssh into foreman server, run check to verify puppet is complete
1126     pushd $vm_dir/foreman_vm
1127     if ! vagrant ssh -c "/opt/khaleesi/run.sh --no-logs --use /vagrant/opnfv_ksgen_settings.yml /opt/khaleesi/playbooks/validate_opnfv-vm.yml"; then
1128       echo "${red} Failed to validate puppet completion on nodes ${reset}"
1129       exit 1
1130     else
1131       echo "{$blue} Puppet complete on all nodes! ${reset}"
1132     fi
1133     popd
1134     ##add routes back to nodes
1135     for node in ${nodes}; do
1136       pushd $vm_dir/$node
1137       if ! vagrant ssh -c "route | grep default | grep $this_default_gw"; then
1138         echo "${blue} Adding public route back to $node! ${reset}"
1139         vagrant ssh -c "route add default gw $this_default_gw"
1140         vagrant ssh -c "route delete default gw 10.0.2.2"
1141       fi
1142       popd
1143     done
1144     if [ ! -z "$horizon_public_vip" ]; then
1145       echo "${blue} Virtual deployment SUCCESS!! Foreman URL:  http://${foreman_ip}, Horizon URL: http://${horizon_public_vip} ${reset}"
1146     else
1147       ##Find public IP of controller
1148       for node in ${nodes}; do
1149         node_type=config_nodes_${node}_type
1150         node_type=$(eval echo \$$node_type)
1151         if [ "$node_type" == "controller" ]; then
1152           pushd $vm_dir/$node
1153           horizon_ip=`vagrant ssh -c "ifconfig enp0s10" | grep -Eo "inet [0-9\.]+" | awk {'print $2'}`
1154           popd
1155           break
1156         fi
1157       done
1158       if [ -z "$horizon_ip" ]; then
1159         echo "${red}Warn: Unable to determine horizon IP, please login to your controller node to find it${reset}"
1160       fi
1161       echo "${blue} Virtual deployment SUCCESS!! Foreman URL:  http://${foreman_ip}, Horizon URL: http://${horizon_ip} ${reset}"
1162     fi
1163   fi
1164 }
1165
1166 ##check to make sure nodes are powered off
1167 ##this function does nothing if virtual
1168 ##params: none
1169 ##usage: check_baremetal_nodes()
1170 check_baremetal_nodes() {
1171   if [ $virtual ]; then
1172     echo "${blue}Skipping Baremetal node power status check as deployment is virtual ${reset}"
1173   else
1174     echo "${blue}Checking Baremetal nodes power state... ${reset}"
1175     if [ ! -z "$base_config" ]; then
1176       # Install ipmitool
1177       # Major version is pinned to force some consistency for Arno
1178       if ! yum list installed | grep -i ipmitool; then
1179         echo "${blue}Installing ipmitool...${reset}"
1180         if ! yum -y install ipmitool-1*; then
1181           echo "${red}Failed to install ipmitool!${reset}"
1182           exit 1
1183         fi
1184       fi
1185
1186         ###find all the bmc IPs and number of nodes
1187       node_counter=0
1188       output=`grep bmc_ip $base_config | grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+'`
1189       for line in ${output} ; do
1190         bmc_ip[$node_counter]=$line
1191         ((node_counter++))
1192       done
1193
1194       max_nodes=$((node_counter-1))
1195
1196       ###find bmc_users per node
1197       node_counter=0
1198       output=`grep bmc_user $base_config | sed 's/\s*bmc_user:\s*//'`
1199       for line in ${output} ; do
1200         bmc_user[$node_counter]=$line
1201         ((node_counter++))
1202       done
1203
1204       ###find bmc_pass per node
1205       node_counter=0
1206       output=`grep bmc_pass $base_config | sed 's/\s*bmc_pass:\s*//'`
1207       for line in ${output} ; do
1208         bmc_pass[$node_counter]=$line
1209         ((node_counter++))
1210       done
1211
1212       for mynode in `seq 0 $max_nodes`; do
1213         echo "${blue}Node: ${bmc_ip[$mynode]} ${bmc_user[$mynode]} ${bmc_pass[$mynode]} ${reset}"
1214         ipmi_output=`ipmitool -I lanplus -P ${bmc_pass[$mynode]} -U ${bmc_user[$mynode]} -H ${bmc_ip[$mynode]} chassis status \
1215                     | grep "System Power" | cut -d ':' -f2 | tr -d [:blank:]`
1216         if [ "$ipmi_output" == "on" ]; then
1217           echo "${red}Error: Node is powered on: ${bmc_ip[$mynode]} ${reset}"
1218           echo "${red}Please run clean.sh before running deploy! ${reset}"
1219           exit 1
1220         elif [ "$ipmi_output" == "off" ]; then
1221           echo "${blue}Node: ${bmc_ip[$mynode]} is powered off${reset}"
1222         else
1223           echo "${red}Warning: Unable to detect node power state: ${bmc_ip[$mynode]} ${reset}"
1224         fi
1225       done
1226     else
1227       echo "${red}base_config was not provided for a baremetal install! Exiting${reset}"
1228       exit 1
1229     fi
1230   fi
1231 }
1232
1233 ##END FUNCTIONS
1234
1235 main() {
1236   parse_cmdline "$@"
1237   disable_selinux
1238   check_baremetal_nodes
1239   install_EPEL
1240   install_vbox
1241   install_ansible
1242   install_vagrant
1243   clean_tmp
1244   verify_vm_dir
1245   clone_bgs $vm_dir/foreman_vm
1246   configure_network
1247   configure_virtual
1248   start_foreman
1249   start_virtual_nodes
1250 }
1251
1252 main "$@"