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