Adds ability to specify NICs to bridge on the jumphost
[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 ##END VARS
34
35 ##FUNCTIONS
36 display_usage() {
37   echo -e "\n\n${blue}This script is used to deploy Foreman/QuickStack Installer and Provision OPNFV Target System${reset}\n\n"
38   echo -e "\n${green}Make sure you have the latest kernel installed before running this script! (yum update kernel +reboot)${reset}\n"
39   echo -e "\nUsage:\n$0 [arguments] \n"
40   echo -e "\n   -no_parse : No variable parsing into config. Flag. \n"
41   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"
42   echo -e "\n   -virtual : Node virtualization instead of baremetal. Flag. \n"
43   echo -e "\n   -enable_virtual_dhcp : Run dhcp server instead of using static IPs.  Use this with -virtual only. \n"
44   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"
45   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"
46   echo -e "\n   -floating_ip_count : number of IP address from the public range to be used for floating IP. Default is 20.\n"
47   echo -e "\n   -admin_nic : Baremetal NIC for the admin network.  Required if other "nic" arguments are used.  \
48 Not applicable with -virtual.  Example: -admin_nic em1"
49   echo -e "\n   -private_nic : Baremetal NIC for the private network.  Required if other "nic" arguments are used.  \
50 Not applicable with -virtual.  Example: -private_nic em2"
51   echo -e "\n   -public_nic : Baremetal NIC for the public network.  Required if other "nic" arguments are used.  \
52 Can also be used with -virtual.  Example: -public_nic em3"
53   echo -e "\n   -storage_nic : Baremetal NIC for the storage network.  Optional.  Not applicable with -virtual. \
54 Private NIC will be used for storage if not specified. Example: -storage_nic em4"
55 }
56
57 ##verify vm dir exists
58 ##params: none
59 function verify_vm_dir {
60   if [ -d "$vm_dir" ]; then
61     echo -e "\n\n${red}ERROR: VM Directory: $vm_dir already exists.  Environment not clean.  Please use clean.sh.  Exiting${reset}\n\n"
62     exit 1
63   else
64     mkdir -p $vm_dir
65   fi
66
67   chmod 700 $vm_dir
68
69   if [ ! -d $vm_dir ]; then
70     echo -e "\n\n${red}ERROR: Unable to create VM Directory: $vm_dir  Exiting${reset}\n\n"
71     exit -1
72   fi
73 }
74
75 ##find ip of interface
76 ##params: interface name
77 function find_ip {
78   ip addr show $1 | grep -Eo '^\s+inet\s+[\.0-9]+' | awk '{print $2}'
79 }
80
81 ##finds subnet of ip and netmask
82 ##params: ip, netmask
83 function find_subnet {
84   IFS=. read -r i1 i2 i3 i4 <<< "$1"
85   IFS=. read -r m1 m2 m3 m4 <<< "$2"
86   printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"
87 }
88
89 ##verify subnet has at least n IPs
90 ##params: subnet mask, n IPs
91 function verify_subnet_size {
92   IFS=. read -r i1 i2 i3 i4 <<< "$1"
93   num_ips_required=$2
94
95   ##this function assumes you would never need more than 254
96   ##we check here to make sure
97   if [ "$num_ips_required" -ge 254 ]; then
98     echo -e "\n\n${red}ERROR: allocating more than 254 IPs is unsupported...Exiting${reset}\n\n"
99     return 1
100   fi
101
102   ##we just return if 3rd octet is not 255
103   ##because we know the subnet is big enough
104   if [ "$i3" -ne 255 ]; then
105     return 0
106   elif [ $((254-$i4)) -ge "$num_ips_required" ]; then
107     return 0
108   else
109     echo -e "\n\n${red}ERROR: Subnet is too small${reset}\n\n"
110     return 1
111   fi
112 }
113
114 ##finds last usable ip (broadcast minus 1) of a subnet from an IP and netmask
115 ## Warning: This function only works for IPv4 at the moment.
116 ##params: ip, netmask
117 function find_last_ip_subnet {
118   IFS=. read -r i1 i2 i3 i4 <<< "$1"
119   IFS=. read -r m1 m2 m3 m4 <<< "$2"
120   IFS=. read -r s1 s2 s3 s4 <<< "$((i1 & m1)).$((i2 & m2)).$((i3 & m3)).$((i4 & m4))"
121   printf "%d.%d.%d.%d\n" "$((255 - $m1 + $s1))" "$((255 - $m2 + $s2))" "$((255 - $m3 + $s3))" "$((255 - $m4 + $s4 - 1))"
122 }
123
124 ##increments subnet by a value
125 ##params: ip, value
126 ##assumes low value
127 function increment_subnet {
128   IFS=. read -r i1 i2 i3 i4 <<< "$1"
129   printf "%d.%d.%d.%d\n" "$i1" "$i2" "$i3" "$((i4 | $2))"
130 }
131
132
133 ##finds netmask of interface
134 ##params: interface
135 ##returns long format 255.255.x.x
136 function find_netmask {
137   ifconfig $1 | grep -Eo 'netmask\s+[\.0-9]+' | awk '{print $2}'
138 }
139
140 ##finds short netmask of interface
141 ##params: interface
142 ##returns short format, ex: /21
143 function find_short_netmask {
144   echo "/$(ip addr show $1 | grep -Eo '^\s+inet\s+[\/\.0-9]+' | awk '{print $2}' | cut -d / -f2)"
145 }
146
147 ##increments next IP
148 ##params: ip
149 ##assumes a /24 subnet
150 function next_ip {
151   baseaddr="$(echo $1 | cut -d. -f1-3)"
152   lsv="$(echo $1 | cut -d. -f4)"
153   if [ "$lsv" -ge 254 ]; then
154     return 1
155   fi
156   ((lsv++))
157   echo $baseaddr.$lsv
158 }
159
160 ##subtracts a value from an IP address
161 ##params: last ip, ip_count
162 ##assumes ip_count is less than the last octect of the address
163 subtract_ip() {
164   IFS=. read -r i1 i2 i3 i4 <<< "$1"
165   ip_count=$2
166   if [ $i4 -lt $ip_count ]; then
167     echo -e "\n\n${red}ERROR: Can't subtract $ip_count from IP address $1  Exiting${reset}\n\n"
168     exit 1
169   fi
170   printf "%d.%d.%d.%d\n" "$i1" "$i2" "$i3" "$((i4 - $ip_count ))"
171 }
172
173 ##removes the network interface config from Vagrantfile
174 ##params: interface
175 ##assumes you are in the directory of Vagrantfile
176 function remove_vagrant_network {
177   sed -i 's/^.*'"$1"'.*$//' Vagrantfile
178 }
179
180 ##check if IP is in use
181 ##params: ip
182 ##ping ip to get arp entry, then check arp
183 function is_ip_used {
184   ping -c 5 $1 > /dev/null 2>&1
185   arp -n | grep "$1 " | grep -iv incomplete > /dev/null 2>&1
186 }
187
188 ##find next usable IP
189 ##params: ip
190 function next_usable_ip {
191   new_ip=$(next_ip $1)
192   while [ "$new_ip" ]; do
193     if ! is_ip_used $new_ip; then
194       echo $new_ip
195       return 0
196     fi
197     new_ip=$(next_ip $new_ip)
198   done
199   return 1
200 }
201
202 ##increment ip by value
203 ##params: ip, amount to increment by
204 ##increment_ip $next_private_ip 10
205 function increment_ip {
206   baseaddr="$(echo $1 | cut -d. -f1-3)"
207   lsv="$(echo $1 | cut -d. -f4)"
208   incrval=$2
209   lsv=$((lsv+incrval))
210   if [ "$lsv" -ge 254 ]; then
211     return 1
212   fi
213   echo $baseaddr.$lsv
214 }
215
216 ##translates yaml into variables
217 ##params: filename, prefix (ex. "config_")
218 ##usage: parse_yaml opnfv_ksgen_settings.yml "config_"
219 parse_yaml() {
220    local prefix=$2
221    local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
222    sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
223         -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p"  $1 |
224    awk -F$fs '{
225       indent = length($1)/2;
226       vname[indent] = $2;
227       for (i in vname) {if (i > indent) {delete vname[i]}}
228       if (length($3) > 0) {
229          vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
230          printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
231       }
232    }'
233 }
234
235 ##translates the command line paramaters into variables
236 ##params: $@ the entire command line is passed
237 ##usage: parse_cmd_line() "$@"
238 parse_cmdline() {
239   if [[ ( $1 == "--help") ||  $1 == "-h" ]]; then
240     display_usage
241     exit 0
242   fi
243
244   echo -e "\n\n${blue}This script is used to deploy Foreman/QuickStack Installer and Provision OPNFV Target System${reset}\n\n"
245   echo "Use -h to display help"
246   sleep 2
247
248   while [ "`echo $1 | cut -c1`" = "-" ]
249   do
250     echo $1
251     case "$1" in
252         -base_config)
253                 base_config=$2
254                 shift 2
255             ;;
256         -no_parse)
257                 no_parse="TRUE"
258                 shift 1
259             ;;
260         -virtual)
261                 virtual="TRUE"
262                 shift 1
263             ;;
264         -enable_virtual_dhcp)
265                 enable_virtual_dhcp="TRUE"
266                 shift 1
267             ;;
268         -static_ip_range)
269                 static_ip_range=$2
270                 shift 2
271             ;;
272         -ping_site)
273                 ping_site=$2
274                 shift 2
275             ;;
276         -floating_ip_count)
277                 floating_ip_count=$2
278                 shift 2
279             ;;
280         -admin_nic)
281                 admin_nic=$2
282                 shift 2
283                 nic_arg_flag=1
284             ;;
285         -private_nic)
286                 private_nic=$2
287                 shift 2
288                 nic_arg_flag=1
289             ;;
290         -public_nic)
291                 public_nic=$2
292                 shift 2
293                 nic_arg_flag=1
294             ;;
295         -storage_nic)
296                 storage_nic=$2
297                 shift 2
298                 nic_arg_flag=1
299             ;;
300         *)
301                 display_usage
302                 exit 1
303             ;;
304     esac
305   done
306
307   if [ ! -z "$enable_virtual_dhcp" ] && [ ! -z "$static_ip_range" ]; then
308     echo -e "\n\n${red}ERROR: Incorrect Usage.  Static IP range cannot be set when using DHCP!.  Exiting${reset}\n\n"
309     exit 1
310   fi
311
312   if [ -z "$virtual" ]; then
313     if [ ! -z "$enable_virtual_dhcp" ]; then
314       echo -e "\n\n${red}ERROR: Incorrect Usage.  enable_virtual_dhcp can only be set when using -virtual!.  Exiting${reset}\n\n"
315       exit 1
316     elif [ ! -z "$static_ip_range" ]; then
317       echo -e "\n\n${red}ERROR: Incorrect Usage.  static_ip_range can only be set when using -virtual!.  Exiting${reset}\n\n"
318       exit 1
319     fi
320   fi
321
322   if [ -z "$floating_ip_count" ]; then
323     floating_ip_count=20
324   fi
325
326   ##Validate nic args
327   if [ $nic_arg_flag -eq 1 ]; then
328     if [ -z "$virtual" ]; then
329       for nic_type in admin_nic private_nic public_nic; do
330         eval "nic_value=\$$nic_type"
331         if [ -z "$nic_value" ]; then
332           echo "${red}$nic_type is empty or not defined.  Required when other nic args are given!${reset}"
333           exit 1
334         fi
335         interface_ip=$(find_ip $nic_value)
336         if [ ! "$interface_ip" ]; then
337           echo "${red}$nic_value does not have an IP address! Exiting... ${reset}"
338           exit 1
339         fi
340       done
341     else
342       ##if virtual only public_nic should be specified
343       for nic_type in admin_nic private_nic storage_nic; do
344         eval "nic_value=\$$nic_type"
345         if [ ! -z "$nic_value" ]; then
346           echo "${red}$nic_type is not a valid argument using -virtual.  Please only specify public_nic!${reset}"
347           exit 1
348         fi
349       done
350
351       interface_ip=$(find_ip $public_nic)
352       if [ ! "$interface_ip" ]; then
353         echo "${red}Public NIC: $public_nic does not have an IP address! Exiting... ${reset}"
354         exit 1
355       fi
356     fi
357   fi
358 }
359
360 ##disable selinux
361 ##params: none
362 ##usage: disable_selinux()
363 disable_selinux() {
364   /sbin/setenforce 0
365 }
366
367 ##Install the EPEL repository and additional packages
368 ##params: none
369 ##usage: install_EPEL()
370 install_EPEL() {
371   # Install EPEL repo for access to many other yum repos
372   # Major version is pinned to force some consistency for Arno
373   yum install -y epel-release-7*
374
375   # Install other required packages
376   # Major versions are pinned to force some consistency for Arno
377   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
378     printf '%s\n' 'deploy.sh: Unable to install depdency packages' >&2
379     exit 1
380   fi
381 }
382
383 ##Download and install virtual box
384 ##params: none
385 ##usage: install_vbox()
386 install_vbox() {
387   ##install VirtualBox repo
388   if cat /etc/*release | grep -i "Fedora release"; then
389     vboxurl=http://download.virtualbox.org/virtualbox/rpm/fedora/\$releasever/\$basearch
390   else
391     vboxurl=http://download.virtualbox.org/virtualbox/rpm/el/\$releasever/\$basearch
392   fi
393
394   cat > /etc/yum.repos.d/virtualbox.repo << EOM
395 [virtualbox]
396 name=Oracle Linux / RHEL / CentOS-\$releasever / \$basearch - VirtualBox
397 baseurl=$vboxurl
398 enabled=1
399 gpgcheck=1
400 gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
401 skip_if_unavailable = 1
402 keepcache = 0
403 EOM
404
405   ##install VirtualBox
406   if ! yum list installed | grep -i virtualbox; then
407     if ! yum -y install VirtualBox-4.3; then
408       printf '%s\n' 'deploy.sh: Unable to install virtualbox package' >&2
409       exit 1
410     fi
411   fi
412
413   ##install kmod-VirtualBox
414   if ! lsmod | grep vboxdrv; then
415     sudo /etc/init.d/vboxdrv setup
416     if ! lsmod | grep vboxdrv; then
417       printf '%s\n' 'deploy.sh: Unable to install kernel module for virtualbox' >&2
418       exit 1
419     fi
420   else
421     printf '%s\n' 'deploy.sh: Skipping kernel module for virtualbox.  Already Installed'
422   fi
423 }
424
425 ##install Ansible using yum
426 ##params: none
427 ##usage: install_ansible()
428 install_ansible() {
429   if ! yum list installed | grep -i ansible; then
430     if ! yum -y install ansible-1*; then
431       printf '%s\n' 'deploy.sh: Unable to install Ansible package' >&2
432       exit 1
433     fi
434   fi
435 }
436
437 ##install Vagrant RPM directly with the bintray.com site
438 ##params: none
439 ##usage: install_vagrant()
440 install_vagrant() {
441   if ! rpm -qa | grep vagrant; then
442     if ! rpm -Uvh https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.rpm; then
443       printf '%s\n' 'deploy.sh: Unable to install vagrant package' >&2
444       exit 1
445     fi
446   else
447     printf '%s\n' 'deploy.sh: Skipping Vagrant install as it is already installed.'
448   fi
449
450   ##add centos 7 box to vagrant
451   if ! vagrant box list | grep chef/centos-7.0; then
452     if ! vagrant box add chef/centos-7.0 --provider virtualbox; then
453       printf '%s\n' 'deploy.sh: Unable to download centos7 box for Vagrant' >&2
454       exit 1
455     fi
456   else
457     printf '%s\n' 'deploy.sh: Skipping Vagrant box add as centos-7.0 is already installed.'
458   fi
459
460   ##install workaround for centos7
461   if ! vagrant plugin list | grep vagrant-centos7_fix; then
462     if ! vagrant plugin install vagrant-centos7_fix; then
463       printf '%s\n' 'deploy.sh: Warning: unable to install vagrant centos7 workaround' >&2
464     fi
465   else
466     printf '%s\n' 'deploy.sh: Skipping Vagrant plugin as centos7 workaround is already installed.'
467   fi
468 }
469
470
471 ##remove bgs vagrant incase it wasn't cleaned up
472 ##params: none
473 ##usage: clean_tmp()
474 clean_tmp() {
475   rm -rf $vm_dir/foreman_vm
476 }
477
478 ##clone genesis and move to node vm dir
479 ##params: none
480 ##usage: clone_bgs
481 clone_bgs() {
482   cd /tmp/
483   rm -rf /tmp/genesis/
484
485   ##clone artifacts and move into foreman_vm dir
486   if ! GIT_SSL_NO_VERIFY=true git clone https://gerrit.opnfv.org/gerrit/genesis.git; then
487     printf '%s\n' 'deploy.sh: Unable to clone genesis repo' >&2
488     exit 1
489   fi
490
491   mv -f /tmp/genesis/foreman/ci $vm_dir/foreman_vm
492   rm -rf /tmp/genesis/
493 }
494
495 ##validates the network settings and update VagrantFile with network settings
496 ##params: none
497 ##usage: configure_network()
498 configure_network() {
499   cd $vm_dir/foreman_vm
500
501   ##if nic_arg_flag is set, then we don't figure out
502   ##NICs dynamically
503   if [ $nic_arg_flag -eq 1 ]; then
504     echo "${blue}Static Network Interfaces Defined.  Updating Vagrantfile...${reset}"
505     if [ $virtual ]; then
506       nic_list="$public_nic"
507     elif [ -z "$storage_nic" ]; then
508       echo "${blue}storage_nic not defined, will combine storage into private VLAN ${reset}"
509       nic_list="$admin_nic $private_nic $public_nic"
510     else
511       nic_list="$admin_nic $private_nic $public_nic $storage_nic"
512     fi
513     nic_array=( $nic_list )
514     output=$nic_list
515   else
516     echo "${blue}Detecting network configuration...${reset}"
517     ##detect host 1 or 3 interface configuration
518     #output=`ip link show | grep -E "^[0-9]" | grep -Ev ": lo|tun|virbr|vboxnet" | awk '{print $2}' | sed 's/://'`
519     output=`ifconfig | grep -E "^[a-zA-Z0-9]+:"| grep -Ev "lo|tun|virbr|vboxnet" | awk '{print $1}' | sed 's/://'`
520   fi
521
522   if [ ! "$output" ]; then
523     printf '%s\n' 'deploy.sh: Unable to detect interfaces to bridge to' >&2
524     exit 1
525   fi
526
527   ##virtual we only find 1 interface
528   if [ $virtual ]; then
529     if [ ! -z "${nic_array[0]}" ]; then
530       echo "${blue}Public Interface specified: ${nic_array[0]}${reset}"
531       this_default_gw_interface=${nic_array[0]}
532     else
533       ##find interface with default gateway
534       this_default_gw=$(ip route | grep default | awk '{print $3}')
535       echo "${blue}Default Gateway: $this_default_gw ${reset}"
536       this_default_gw_interface=$(ip route get $this_default_gw | awk '{print $3}')
537     fi
538
539     ##find interface IP, make sure its valid
540     interface_ip=$(find_ip $this_default_gw_interface)
541     if [ ! "$interface_ip" ]; then
542       echo "${red}Interface ${this_default_gw_interface} does not have an IP: $interface_ip ! Exiting ${reset}"
543       exit 1
544     fi
545
546     ##set variable info
547     if [ ! -z "$static_ip_range" ]; then
548       new_ip=$(echo $static_ip_range | cut -d , -f1)
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   if [ $no_parse ]; then
736     echo "${blue}Skipping parsing variables into settings file as no_parse flag is set${reset}"
737
738   else
739
740     echo "${blue}Gathering network parameters for Target System...this may take a few minutes${reset}"
741     ##Edit the ksgen settings appropriately
742     ##ksgen settings will be stored in /vagrant on the vagrant machine
743     ##if single node deployment all the variables will have the same ip
744     ##interface names will be enp0s3, enp0s8, enp0s9 in chef/centos7
745
746     sed -i 's/^.*default_gw:.*$/default_gw:'" $node_default_gw"'/' opnfv_ksgen_settings.yml
747
748     ##replace private interface parameter
749     ##private interface will be of hosts, so we need to know the provisioned host interface name
750     ##we add biosdevname=0, net.ifnames=0 to the kickstart to use regular interface naming convention on hosts
751     ##replace IP for parameters with next IP that will be given to controller
752     if [ "$deployment_type" == "single_network" ]; then
753       ##we also need to assign IP addresses to nodes
754       ##for single node, foreman is managing the single network, so we can't reserve them
755       ##not supporting single network anymore for now
756       echo "{blue}Single Network type is unsupported right now.  Please check your interface configuration.  Exiting. ${reset}"
757       exit 0
758
759     elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_network" ]]; then
760
761       if [ "$deployment_type" == "three_network" ]; then
762         sed -i 's/^.*network_type:.*$/network_type: three_network/' opnfv_ksgen_settings.yml
763       fi
764
765       sed -i 's/^.*deployment_type:.*$/  deployment_type: '"$deployment_type"'/' opnfv_ksgen_settings.yml
766
767       ##get ip addresses for private network on controllers to make dhcp entries
768       ##required for controllers_ip_array global param
769       next_private_ip=${interface_ip_arr[1]}
770       type=_private
771       control_count=0
772       for node in controller1 controller2 controller3; do
773         next_private_ip=$(next_usable_ip $next_private_ip)
774         if [ ! "$next_private_ip" ]; then
775           printf '%s\n' 'deploy.sh: Unable to find next ip for private network for control nodes' >&2
776           exit 1
777         fi
778         sed -i 's/'"$node$type"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
779         controller_ip_array=$controller_ip_array$next_private_ip,
780         controllers_ip_arr[$control_count]=$next_private_ip
781         ((control_count++))
782       done
783
784       next_public_ip=${interface_ip_arr[2]}
785       foreman_ip=$next_public_ip
786
787       ##if no dhcp, find all the Admin IPs for nodes in advance
788       if [ $virtual ]; then
789         if [ -z "$enable_virtual_dhcp" ]; then
790           sed -i 's/^.*no_dhcp:.*$/no_dhcp: true/' opnfv_ksgen_settings.yml
791           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'`
792           compute_nodes=`echo $nodes | tr " " "\n" | grep -v controller | tr "\n" " "`
793           controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
794           nodes=${controller_nodes}${compute_nodes}
795           next_admin_ip=${interface_ip_arr[0]}
796           type=_admin
797           for node in ${nodes}; do
798             next_admin_ip=$(next_ip $next_admin_ip)
799             if [ ! "$next_admin_ip" ]; then
800               echo "${red} Unable to find an unused IP in admin_network for $node ! ${reset}"
801               exit 1
802             else
803               admin_ip_arr[$node]=$next_admin_ip
804               sed -i 's/'"$node$type"'/'"$next_admin_ip"'/g' opnfv_ksgen_settings.yml
805             fi
806           done
807
808           ##allocate node public IPs
809           for node in ${nodes}; do
810             next_public_ip=$(next_usable_ip $next_public_ip)
811             if [ ! "$next_public_ip" ]; then
812               echo "${red} Unable to find an unused IP in admin_network for $node ! ${reset}"
813               exit 1
814             else
815               public_ip_arr[$node]=$next_public_ip
816             fi
817           done
818         fi
819       fi
820       ##replace global param for controllers_ip_array
821       controller_ip_array=${controller_ip_array%?}
822       sed -i 's/^.*controllers_ip_array:.*$/  controllers_ip_array: '"$controller_ip_array"'/' opnfv_ksgen_settings.yml
823
824       ##now replace all the VIP variables.  admin//private can be the same IP
825       ##we have to use IP's here that won't be allocated to hosts at provisioning time
826       ##therefore we increment the ip by 10 to make sure we have a safe buffer
827       next_private_ip=$(increment_ip $next_private_ip 10)
828
829       private_output=$(grep -E '*private_vip|loadbalancer_vip|db_vip|amqp_vip|*admin_vip' opnfv_ksgen_settings.yml)
830       if [ ! -z "$private_output" ]; then
831         while read -r line; do
832           sed -i 's/^.*'"$line"'.*$/  '"$line $next_private_ip"'/' opnfv_ksgen_settings.yml
833           next_private_ip=$(next_usable_ip $next_private_ip)
834           if [ ! "$next_private_ip" ]; then
835             printf '%s\n' 'deploy.sh: Unable to find next ip for private network for vip replacement' >&2
836             exit 1
837           fi
838         done <<< "$private_output"
839       fi
840
841       ##replace odl_control_ip (non-HA only)
842       odl_control_ip=${controllers_ip_arr[0]}
843       sed -i 's/^.*odl_control_ip:.*$/  odl_control_ip: '"$odl_control_ip"'/' opnfv_ksgen_settings.yml
844
845       ##replace controller_ip (non-HA only)
846       sed -i 's/^.*controller_ip:.*$/  controller_ip: '"$odl_control_ip"'/' opnfv_ksgen_settings.yml
847
848       ##replace foreman site
849       sed -i 's/^.*foreman_url:.*$/  foreman_url:'" https:\/\/$foreman_ip"'\/api\/v2\//' opnfv_ksgen_settings.yml
850       ##replace public vips
851       ##no need to do this if no dhcp
852       if [[ -z "$enable_virtual_dhcp" && ! -z "$virtual" ]]; then
853         next_public_ip=$(next_usable_ip $next_public_ip)
854       else
855         next_public_ip=$(increment_ip $next_public_ip 10)
856       fi
857
858       public_output=$(grep -E '*public_vip' opnfv_ksgen_settings.yml)
859       if [ ! -z "$public_output" ]; then
860         while read -r line; do
861           if echo $line | grep horizon_public_vip; then
862             horizon_public_vip=$next_public_ip
863           fi
864           sed -i 's/^.*'"$line"'.*$/  '"$line $next_public_ip"'/' opnfv_ksgen_settings.yml
865           next_public_ip=$(next_usable_ip $next_public_ip)
866           if [ ! "$next_public_ip" ]; then
867             printf '%s\n' 'deploy.sh: Unable to find next ip for public network for vip replcement' >&2
868             exit 1
869           fi
870         done <<< "$public_output"
871       fi
872
873       ##replace public_network param
874       public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
875       sed -i 's/^.*public_network:.*$/  public_network:'" $public_subnet"'/' opnfv_ksgen_settings.yml
876       ##replace private_network param
877       private_subnet=$(find_subnet $next_private_ip $private_subnet_mask)
878       sed -i 's/^.*private_network:.*$/  private_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
879       ##replace storage_network
880       if [ "$deployment_type" == "three_network" ]; then
881         sed -i 's/^.*storage_network:.*$/  storage_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
882       else
883         next_storage_ip=${interface_ip_arr[3]}
884         storage_subnet=$(find_subnet $next_storage_ip $storage_subnet_mask)
885         sed -i 's/^.*storage_network:.*$/  storage_network:'" $storage_subnet"'/' opnfv_ksgen_settings.yml
886       fi
887
888       ##replace public_subnet param
889       public_subnet=$public_subnet'\'$public_short_subnet_mask
890       sed -i 's/^.*public_subnet:.*$/  public_subnet:'" $public_subnet"'/' opnfv_ksgen_settings.yml
891       ##replace private_subnet param
892       private_subnet=$private_subnet'\'$private_short_subnet_mask
893       sed -i 's/^.*private_subnet:.*$/  private_subnet:'" $private_subnet"'/' opnfv_ksgen_settings.yml
894
895       ##replace public_dns param to be foreman server
896       sed -i 's/^.*public_dns:.*$/  public_dns: '${interface_ip_arr[2]}'/' opnfv_ksgen_settings.yml
897
898       ##replace public_gateway
899       if [ -z "$public_gateway" ]; then
900         ##if unset then we assume its the first IP in the public subnet
901         public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
902         public_gateway=$(increment_subnet $public_subnet 1)
903       fi
904       sed -i 's/^.*public_gateway:.*$/  public_gateway:'" $public_gateway"'/' opnfv_ksgen_settings.yml
905
906       ##we have to define an allocation range of the public subnet to give
907       ##to neutron to use as floating IPs
908       ##if static ip range, then we take the difference of the end range and current ip
909       ## to be the allocation pool
910       ##if not static ip, we will use the last 20 IP from the subnet
911       ## note that this is not a really good idea because the subnet must be at least a /27 for this to work...
912       public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
913       if [ ! -z "$static_ip_range" ]; then
914         begin_octet=$(echo $next_public_ip | cut -d . -f4)
915         end_octet=$(echo $static_ip_range_end | cut -d . -f4)
916         ip_diff=$((end_octet-begin_octet))
917         if [ $ip_diff -le 0 ]; then
918           echo "${red}ip range left for floating range is less than or equal to 0! $ipdiff ${reset}"
919           exit 1
920         else
921           public_allocation_start=$(next_ip $next_public_ip)
922           public_allocation_end=$static_ip_range_end
923         fi
924       else
925         last_ip_subnet=$(find_last_ip_subnet $next_public_ip $public_subnet_mask)
926         public_allocation_start=$(subtract_ip $last_ip_subnet $floating_ip_count )
927         public_allocation_end=${last_ip_subnet}
928       fi
929       echo "${blue}Neutron Floating IP range: $public_allocation_start to $public_allocation_end ${reset}"
930
931       sed -i 's/^.*public_allocation_start:.*$/  public_allocation_start:'" $public_allocation_start"'/' opnfv_ksgen_settings.yml
932       sed -i 's/^.*public_allocation_end:.*$/  public_allocation_end:'" $public_allocation_end"'/' opnfv_ksgen_settings.yml
933
934     else
935       printf '%s\n' 'deploy.sh: Unknown network type: $deployment_type' >&2
936       exit 1
937     fi
938
939     echo "${blue}Parameters Complete.  Settings have been set for Foreman. ${reset}"
940
941   fi
942 }
943
944 ##Configure bootstrap.sh to use the virtual Khaleesi playbook
945 ##params: none
946 ##usage: configure_virtual()
947 configure_virtual() {
948   if [ $virtual ]; then
949     echo "${blue} Virtual flag detected, setting Khaleesi playbook to be opnfv-vm.yml ${reset}"
950     sed -i 's/opnfv.yml/opnfv-vm.yml/' bootstrap.sh
951   fi
952 }
953
954 ##Starts Foreman VM with Vagrant
955 ##params: none
956 ##usage: start_vagrant()
957 start_foreman() {
958   echo "${blue}Starting Vagrant! ${reset}"
959
960   ##stand up vagrant
961   if ! vagrant up; then
962     printf '%s\n' 'deploy.sh: Unable to complete Foreman VM install' >&2
963     exit 1
964   else
965     echo "${blue}Foreman VM is up! ${reset}"
966   fi
967 }
968
969 ##start the VM if this is a virtual installation
970 ##this function does nothing if baremetal servers are being used
971 ##params: none
972 ##usage: start_virtual_nodes()
973 start_virtual_nodes() {
974   if [ $virtual ]; then
975
976     ##Bring up VM nodes
977     echo "${blue}Setting VMs up... ${reset}"
978     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'`
979     ##due to ODL Helium bug of OVS connecting to ODL too early, we need controllers to install first
980     ##this is fix kind of assumes more than I would like to, but for now it should be OK as we always have
981     ##3 static controllers
982     compute_nodes=`echo $nodes | tr " " "\n" | grep -v controller | tr "\n" " "`
983     controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
984     nodes=${controller_nodes}${compute_nodes}
985     controller_count=0
986     compute_wait_completed=false
987
988     for node in ${nodes}; do
989       cd /tmp/
990
991       ##remove VM nodes incase it wasn't cleaned up
992       rm -rf $vm_dir/$node
993       rm -rf /tmp/genesis/
994
995       ##clone genesis and move into node folder
996       if ! GIT_SSL_NO_VERIFY=true git clone https://gerrit.opnfv.org/gerrit/genesis.git; then
997         printf '%s\n' 'deploy.sh: Unable to clone vagrant repo' >&2
998         exit 1
999       fi
1000
1001       mv -f /tmp/genesis/foreman/ci $vm_dir/$node
1002       rm -rf /tmp/genesis/
1003
1004       cd $vm_dir/$node
1005
1006       if [ $base_config ]; then
1007         if ! cp -f $base_config opnfv_ksgen_settings.yml; then
1008           echo "${red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
1009           exit 1
1010         fi
1011       fi
1012
1013       ##parse yaml into variables
1014       eval $(parse_yaml opnfv_ksgen_settings.yml "config_")
1015       ##find node type
1016       node_type=config_nodes_${node}_type
1017       node_type=$(eval echo \$$node_type)
1018
1019       ##trozet test make compute nodes wait 20 minutes
1020       if [ "$compute_wait_completed" = false ] && [ "$node_type" != "controller" ]; then
1021         echo "${blue}Waiting 20 minutes for Control nodes to install before continuing with Compute nodes..."
1022         compute_wait_completed=true
1023         sleep 1400
1024       fi
1025
1026       ## Add Admin interface
1027       mac_string=config_nodes_${node}_mac_address
1028       mac_addr=$(eval echo \$$mac_string)
1029       mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
1030       if [ $mac_addr == "" ]; then
1031         echo "${red} Unable to find mac_address for $node! ${reset}"
1032         exit 1
1033       fi
1034       this_admin_ip=${admin_ip_arr[$node]}
1035       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
1036
1037       ## Add private interface
1038       if [ "$node_type" == "controller" ]; then
1039           mac_string=config_nodes_${node}_private_mac
1040           mac_addr=$(eval echo \$$mac_string)
1041           if [ $mac_addr == "" ]; then
1042             echo "${red} Unable to find private_mac for $node! ${reset}"
1043             exit 1
1044           fi
1045       else
1046           ##generate random mac
1047           mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
1048       fi
1049       mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
1050       if [ "$node_type" == "controller" ]; then
1051         new_node_ip=${controllers_ip_arr[$controller_count]}
1052         if [ ! "$new_node_ip" ]; then
1053           echo "{red}ERROR: Empty node ip for controller $controller_count ${reset}"
1054           exit 1
1055         fi
1056         ((controller_count++))
1057       else
1058         next_private_ip=$(next_ip $next_private_ip)
1059         if [ ! "$next_private_ip" ]; then
1060           echo "{red}ERROR: Could not find private ip for $node ${reset}"
1061           exit 1
1062         fi
1063         new_node_ip=$next_private_ip
1064       fi
1065       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
1066       ##replace host_ip in vm_nodes_provision with private ip
1067       sed -i 's/^host_ip=REPLACE/host_ip='$new_node_ip'/' vm_nodes_provision.sh
1068       ##replace ping site
1069       if [ ! -z "$ping_site" ]; then
1070         sed -i 's/www.google.com/'$ping_site'/' vm_nodes_provision.sh
1071       fi
1072
1073       ##find public ip info and add public interface
1074       mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
1075       mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
1076       this_public_ip=${public_ip_arr[$node]}
1077
1078       if [ -z "$enable_virtual_dhcp" ]; then
1079         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
1080       else
1081         sed -i 's/^.*eth_replace2.*$/  config.vm.network "public_network", bridge: '\'"$public_interface"\'', :mac => '\""$mac_addr"\"'/' Vagrantfile
1082       fi
1083       remove_vagrant_network eth_replace3
1084
1085       ##modify provisioning to do puppet install, config, and foreman check-in
1086       ##substitute host_name and dns_server in the provisioning script
1087       host_string=config_nodes_${node}_hostname
1088       host_name=$(eval echo \$$host_string)
1089       sed -i 's/^host_name=REPLACE/host_name='$host_name'/' vm_nodes_provision.sh
1090       ##dns server should be the foreman server
1091       sed -i 's/^dns_server=REPLACE/dns_server='${interface_ip_arr[0]}'/' vm_nodes_provision.sh
1092       ## remove bootstrap and NAT provisioning
1093       sed -i '/nat_setup.sh/d' Vagrantfile
1094       sed -i 's/bootstrap.sh/vm_nodes_provision.sh/' Vagrantfile
1095       ## modify default_gw to be node_default_gw
1096       sed -i 's/^.*default_gw =.*$/  default_gw = '\""$node_default_gw"\"'/' Vagrantfile
1097       ## modify VM memory to be 4gig
1098       ##if node type is controller
1099       if [ "$node_type" == "controller" ]; then
1100         sed -i 's/^.*vb.memory =.*$/     vb.memory = 4096/' Vagrantfile
1101       fi
1102       echo "${blue}Starting Vagrant Node $node! ${reset}"
1103       ##stand up vagrant
1104       if ! vagrant up; then
1105         echo "${red} Unable to start $node ${reset}"
1106         exit 1
1107       else
1108         echo "${blue} $node VM is up! ${reset}"
1109       fi
1110     done
1111     echo "${blue} All VMs are UP! ${reset}"
1112     echo "${blue} Waiting for puppet to complete on the nodes... ${reset}"
1113     ##check puppet is complete
1114     ##ssh into foreman server, run check to verify puppet is complete
1115     pushd $vm_dir/foreman_vm
1116     if ! vagrant ssh -c "/opt/khaleesi/run.sh --no-logs --use /vagrant/opnfv_ksgen_settings.yml /opt/khaleesi/playbooks/validate_opnfv-vm.yml"; then
1117       echo "${red} Failed to validate puppet completion on nodes ${reset}"
1118       exit 1
1119     else
1120       echo "{$blue} Puppet complete on all nodes! ${reset}"
1121     fi
1122     popd
1123     ##add routes back to nodes
1124     for node in ${nodes}; do
1125       pushd $vm_dir/$node
1126       if ! vagrant ssh -c "route | grep default | grep $this_default_gw"; then
1127         echo "${blue} Adding public route back to $node! ${reset}"
1128         vagrant ssh -c "route add default gw $this_default_gw"
1129       fi
1130       popd
1131     done
1132     if [ ! -z "$horizon_public_vip" ]; then
1133       echo "${blue} Virtual deployment SUCCESS!! Foreman URL:  http://${foreman_ip}, Horizon URL: http://${horizon_public_vip} ${reset}"
1134     else
1135       echo "${blue} Virtual deployment SUCCESS!! Foreman URL:  http://${foreman_ip}, Horizon URL: http://${odl_control_ip} ${reset}"
1136     fi
1137   fi
1138 }
1139
1140 ##END FUNCTIONS
1141
1142 main() {
1143   parse_cmdline "$@"
1144   disable_selinux
1145   install_EPEL
1146   install_vbox
1147   install_ansible
1148   install_vagrant
1149   clean_tmp
1150   verify_vm_dir
1151   clone_bgs
1152   configure_network
1153   configure_virtual
1154   start_foreman
1155   start_virtual_nodes
1156 }
1157
1158 main "$@"