Adds single NIC support for baremetal deployments
[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       public_short_subnet_mask=$(find_short_netmask $single_baremetal_nic)
626       if_counter=1
627     else
628       ##find number of interfaces with ip and substitute in VagrantFile
629       if_counter=0
630       for interface in ${output}; do
631
632         if [ "$if_counter" -ge 4 ]; then
633           break
634         fi
635         interface_ip=$(find_ip $interface)
636         if [ ! "$interface_ip" ]; then
637           continue
638         fi
639         new_ip=$(next_usable_ip $interface_ip)
640         if [ ! "$new_ip" ]; then
641           continue
642         fi
643         interface_arr[$interface]=$if_counter
644         interface_ip_arr[$if_counter]=$new_ip
645         subnet_mask=$(find_netmask $interface)
646         if [ "$if_counter" -eq 0 ]; then
647           admin_subnet_mask=$subnet_mask
648           if ! verify_subnet_size $admin_subnet_mask 5; then
649             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}"
650             exit 1
651           fi
652
653         elif [ "$if_counter" -eq 1 ]; then
654           private_subnet_mask=$subnet_mask
655           private_short_subnet_mask=$(find_short_netmask $interface)
656
657           if ! verify_subnet_size $private_subnet_mask 15; then
658             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}"
659             exit 1
660           fi
661         elif [ "$if_counter" -eq 2 ]; then
662           public_subnet_mask=$subnet_mask
663           public_short_subnet_mask=$(find_short_netmask $interface)
664
665           if ! verify_subnet_size $public_subnet_mask 25; then
666             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}"
667             exit 1
668           fi
669         elif [ "$if_counter" -eq 3 ]; then
670           storage_subnet_mask=$subnet_mask
671
672           if ! verify_subnet_size $storage_subnet_mask 10; then
673             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}"
674             exit 1
675           fi
676         else
677           echo "${red}ERROR: interface counter outside valid range of 0 to 3: $if_counter ! ${reset}"
678           exit 1
679         fi
680         sed -i 's/^.*eth_replace'"$if_counter"'.*$/  config.vm.network "public_network", ip: '\""$new_ip"\"', bridge: '\'"$interface"\'', netmask: '\""$subnet_mask"\"'/' Vagrantfile
681         ((if_counter++))
682       done
683     fi
684   fi
685
686   ##now remove interface config in Vagrantfile for 1 node
687   ##if 1, 3, or 4 interfaces set deployment type
688   ##if 2 interfaces remove 2nd interface and set deployment type
689   if [[ "$if_counter" == 1 || "$if_counter" == 2 ]]; then
690     if [ $virtual ]; then
691       deployment_type="single_network"
692       echo "${blue}Single network detected for Virtual deployment...converting to three_network with internal networks! ${reset}"
693       private_internal_ip=155.1.2.2
694       admin_internal_ip=156.1.2.2
695       private_subnet_mask=255.255.255.0
696       private_short_subnet_mask=/24
697       interface_ip_arr[1]=$private_internal_ip
698       interface_ip_arr[0]=$admin_internal_ip
699       admin_subnet_mask=255.255.255.0
700       admin_short_subnet_mask=/24
701       sed -i 's/^.*eth_replace1.*$/  config.vm.network "private_network", virtualbox__intnet: "my_private_network", ip: '\""$private_internal_ip"\"', netmask: '\""$private_subnet_mask"\"'/' Vagrantfile
702       sed -i 's/^.*eth_replace0.*$/  config.vm.network "private_network", virtualbox__intnet: "my_admin_network", ip: '\""$admin_internal_ip"\"', netmask: '\""$private_subnet_mask"\"'/' Vagrantfile
703       remove_vagrant_network eth_replace3
704       deployment_type=three_network
705     elif [[ "$if_counter" == 1 ]]; then
706        echo "${blue}Single network detected for Baremetal deployment! ${reset}"
707        remove_vagrant_network eth_replace1
708        remove_vagrant_network eth_replace2
709        remove_vagrant_network eth_replace3
710        deployment_type="single_network"
711     else
712        echo "${blue}Single network or 2 network detected for baremetal deployment.  This is unsupported! Exiting. ${reset}"
713        exit 1
714     fi
715   elif [ "$if_counter" == 3 ]; then
716     deployment_type="three_network"
717     remove_vagrant_network eth_replace3
718   else
719     deployment_type="multi_network"
720   fi
721
722   echo "${blue}Network detected: ${deployment_type}! ${reset}"
723
724   if [ $virtual ]; then
725     if [ -z "$enable_virtual_dhcp" ]; then
726       sed -i 's/^.*disable_dhcp_flag =.*$/  disable_dhcp_flag = true/' Vagrantfile
727       if [ $static_ip_range ]; then
728         ##verify static range is at least 20 IPs
729         static_ip_range_begin=$(echo $static_ip_range | cut -d , -f1)
730         static_ip_range_end=$(echo $static_ip_range | cut -d , -f2)
731         ##verify range is at least 20 ips
732         ##assumes less than 255 range pool
733         begin_octet=$(echo $static_ip_range_begin | cut -d . -f4)
734         end_octet=$(echo $static_ip_range_end | cut -d . -f4)
735         ip_count=$((end_octet-begin_octet+1))
736         if [ "$ip_count" -lt 20 ]; then
737           echo "${red}Static range is less than 20 ips: ${ip_count}, exiting  ${reset}"
738           exit 1
739         else
740           echo "${blue}Static IP range is size $ip_count ${reset}"
741         fi
742       fi
743     fi
744   fi
745
746   if route | grep default; then
747     echo "${blue}Default Gateway Detected ${reset}"
748     host_default_gw=$(ip route | grep default | awk '{print $3}')
749     echo "${blue}Default Gateway: $host_default_gw ${reset}"
750     default_gw_interface=$(ip route get $host_default_gw | awk '{print $3}')
751     case "${interface_arr[$default_gw_interface]}" in
752       0)
753         echo "${blue}Default Gateway Detected on Admin Interface!${reset}"
754         sed -i 's/^.*default_gw =.*$/  default_gw = '\""$host_default_gw"\"'/' Vagrantfile
755         node_default_gw=$host_default_gw
756         ;;
757       1)
758         echo "${red}Default Gateway Detected on Private Interface!${reset}"
759         echo "${red}Private subnet should be private and not have Internet access!${reset}"
760         exit 1
761         ;;
762       2)
763         echo "${blue}Default Gateway Detected on Public Interface!${reset}"
764         sed -i 's/^.*default_gw =.*$/  default_gw = '\""$host_default_gw"\"'/' Vagrantfile
765         echo "${blue}Will setup NAT from Admin -> Public Network on VM!${reset}"
766         sed -i 's/^.*nat_flag =.*$/  nat_flag = true/' Vagrantfile
767         echo "${blue}Setting node gateway to be VM Admin IP${reset}"
768         node_default_gw=${interface_ip_arr[0]}
769         public_gateway=$default_gw
770         ;;
771       3)
772         echo "${red}Default Gateway Detected on Storage Interface!${reset}"
773         echo "${red}Storage subnet should be private and not have Internet access!${reset}"
774         exit 1
775         ;;
776       *)
777         echo "${red}Unable to determine which interface default gateway is on..Exiting!${reset}"
778         exit 1
779         ;;
780     esac
781   else
782     #assumes 24 bit mask
783     defaultgw=`echo ${interface_ip_arr[0]} | cut -d. -f1-3`
784     firstip=.1
785     defaultgw=$defaultgw$firstip
786     echo "${blue}Unable to find default gateway.  Assuming it is $defaultgw ${reset}"
787     sed -i 's/^.*default_gw =.*$/  default_gw = '\""$defaultgw"\"'/' Vagrantfile
788     node_default_gw=$defaultgw
789   fi
790
791   if [ $base_config ]; then
792     if ! cp -f $base_config opnfv_ksgen_settings.yml; then
793       echo "{red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
794       exit 1
795     fi
796   fi
797
798   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'`
799   controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
800   echo "${blue}Controller nodes found in settings: ${controller_nodes}${reset}"
801   my_controller_array=( $controller_nodes )
802   num_control_nodes=${#my_controller_array[@]}
803   if [ "$num_control_nodes" -ne 3 ]; then
804     if cat opnfv_ksgen_settings.yml | grep ha_flag | grep true; then
805       echo "${red}Error: You must define exactly 3 control nodes when HA flag is true!${reset}"
806       exit 1
807     fi
808   else
809     echo "${blue}Number of Controller nodes detected: ${num_control_nodes}${reset}"
810   fi
811
812   if [ $no_parse ]; then
813     echo "${blue}Skipping parsing variables into settings file as no_parse flag is set${reset}"
814
815   else
816
817     echo "${blue}Gathering network parameters for Target System...this may take a few minutes${reset}"
818     ##Edit the ksgen settings appropriately
819     ##ksgen settings will be stored in /vagrant on the vagrant machine
820     ##if single node deployment all the variables will have the same ip
821     ##interface names will be enp0s3, enp0s8, enp0s9 in chef/centos7
822
823     sed -i 's/^.*default_gw:.*$/default_gw:'" $node_default_gw"'/' opnfv_ksgen_settings.yml
824
825     ##replace private interface parameter
826     ##private interface will be of hosts, so we need to know the provisioned host interface name
827     ##we add biosdevname=0, net.ifnames=0 to the kickstart to use regular interface naming convention on hosts
828     ##replace IP for parameters with next IP that will be given to controller
829
830     if [[ "$deployment_type" == "single_network" || "$deployment_type" == "multi_network" || "$deployment_type" == "three_network" ]]; then
831
832       if [ "$deployment_type" == "three_network" ]; then
833         sed -i 's/^.*network_type:.*$/network_type: three_network/' opnfv_ksgen_settings.yml
834       elif [ "$deployment_type" == "single_network" ]; then
835         sed -i 's/^.*network_type:.*$/network_type: single_network/' opnfv_ksgen_settings.yml
836         next_single_ip=${interface_ip_arr[0]}
837         foreman_ip=$next_single_ip
838         next_single_ip=$(next_usable_ip $next_single_ip)
839       fi
840
841       sed -i 's/^.*deployment_type:.*$/  deployment_type: '"$deployment_type"'/' opnfv_ksgen_settings.yml
842
843       ##get ip addresses for private network on controllers to make dhcp entries
844       ##required for controllers_ip_array global param
845       if [ "$deployment_type" == "single_network" ]; then
846         next_private_ip=$next_single_ip
847         sed -i 's/^.*no_dhcp:.*$/no_dhcp: true/' opnfv_ksgen_settings.yml
848         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'`
849         compute_nodes=`echo $nodes | tr " " "\n" | grep -v controller | tr "\n" " "`
850         controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
851         nodes=${controller_nodes}${compute_nodes}
852         next_admin_ip=${interface_ip_arr[0]}
853         type1=_admin
854         type2=_private
855         control_count=0
856         for node in ${controller_nodes}; do
857           next_private_ip=$(next_usable_ip $next_private_ip)
858           if [ ! "$next_private_ip" ]; then
859             echo "${red} Unable to find an unused IP for $node ! ${reset}"
860             exit 1
861           else
862             sed -i 's/'"$node$type1"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
863             sed -i 's/'"$node$type2"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
864             controller_ip_array=$controller_ip_array$next_private_ip,
865             controllers_ip_arr[$control_count]=$next_private_ip
866             ((control_count++))
867           fi
868         done
869
870         for node in ${compute_nodes}; do
871           next_private_ip=$(next_usable_ip $next_private_ip)
872           if [ ! "$next_private_ip" ]; then
873             echo "${red} Unable to find an unused IP for $node ! ${reset}"
874             exit 1
875           else
876             sed -i 's/'"$node$type1"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
877           fi
878         done
879
880       else
881         next_private_ip=${interface_ip_arr[1]}
882
883         type=_private
884         control_count=0
885         for node in controller1 controller2 controller3; do
886           next_private_ip=$(next_usable_ip $next_private_ip)
887           if [ ! "$next_private_ip" ]; then
888             printf '%s\n' 'deploy.sh: Unable to find next ip for private network for control nodes' >&2
889             exit 1
890           fi
891           sed -i 's/'"$node$type"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
892           controller_ip_array=$controller_ip_array$next_private_ip,
893           controllers_ip_arr[$control_count]=$next_private_ip
894           ((control_count++))
895         done
896       fi
897
898       if [[ "$deployment_type" != "single_network" ]]; then
899         next_public_ip=${interface_ip_arr[2]}
900         foreman_ip=$next_public_ip
901       fi
902
903       ##if no dhcp, find all the Admin IPs for nodes in advance
904       if [ $virtual ]; then
905         if [ -z "$enable_virtual_dhcp" ]; then
906           sed -i 's/^.*no_dhcp:.*$/no_dhcp: true/' opnfv_ksgen_settings.yml
907           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'`
908           compute_nodes=`echo $nodes | tr " " "\n" | grep -v controller | tr "\n" " "`
909           controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
910           nodes=${controller_nodes}${compute_nodes}
911           next_admin_ip=${interface_ip_arr[0]}
912           type=_admin
913           for node in ${nodes}; do
914             next_admin_ip=$(next_ip $next_admin_ip)
915             if [ ! "$next_admin_ip" ]; then
916               echo "${red} Unable to find an unused IP in admin_network for $node ! ${reset}"
917               exit 1
918             else
919               admin_ip_arr[$node]=$next_admin_ip
920               sed -i 's/'"$node$type"'/'"$next_admin_ip"'/g' opnfv_ksgen_settings.yml
921             fi
922           done
923
924           ##allocate node public IPs
925           for node in ${nodes}; do
926             next_public_ip=$(next_usable_ip $next_public_ip)
927             if [ ! "$next_public_ip" ]; then
928               echo "${red} Unable to find an unused IP in admin_network for $node ! ${reset}"
929               exit 1
930             else
931               public_ip_arr[$node]=$next_public_ip
932             fi
933           done
934         fi
935       fi
936       ##replace global param for controllers_ip_array
937       controller_ip_array=${controller_ip_array%?}
938       sed -i 's/^.*controllers_ip_array:.*$/  controllers_ip_array: '"$controller_ip_array"'/' opnfv_ksgen_settings.yml
939
940       ##now replace all the VIP variables.  admin//private can be the same IP
941       ##we have to use IP's here that won't be allocated to hosts at provisioning time
942       ##therefore we increment the ip by 10 to make sure we have a safe buffer
943       next_private_ip=$(increment_ip $next_private_ip 10)
944
945       private_output=$(grep -E '*private_vip|loadbalancer_vip|db_vip|amqp_vip|*admin_vip' opnfv_ksgen_settings.yml)
946       if [ ! -z "$private_output" ]; then
947         while read -r line; do
948           sed -i 's/^.*'"$line"'.*$/  '"$line $next_private_ip"'/' opnfv_ksgen_settings.yml
949           next_private_ip=$(next_usable_ip $next_private_ip)
950           if [ ! "$next_private_ip" ]; then
951             printf '%s\n' 'deploy.sh: Unable to find next ip for private network for vip replacement' >&2
952             exit 1
953           fi
954         done <<< "$private_output"
955       fi
956
957       ##replace odl_control_ip (non-HA only)
958       odl_control_ip=${controllers_ip_arr[0]}
959       sed -i 's/^.*odl_control_ip:.*$/  odl_control_ip: '"$odl_control_ip"'/' opnfv_ksgen_settings.yml
960
961       ##replace controller_ip (non-HA only)
962       sed -i 's/^.*controller_ip:.*$/  controller_ip: '"$odl_control_ip"'/' opnfv_ksgen_settings.yml
963
964       ##replace foreman site
965       sed -i 's/^.*foreman_url:.*$/  foreman_url:'" https:\/\/$foreman_ip"'\/api\/v2\//' opnfv_ksgen_settings.yml
966       ##replace public vips
967
968       ##if single_network deployment we continue next_public_ip from next_private_ip
969       if [[ "$deployment_type" == "single_network" ]]; then
970         next_public_ip=$(next_usable_ip $next_private_ip)
971       else
972         ##no need to do this if no dhcp
973         if [[ -z "$enable_virtual_dhcp" && ! -z "$virtual" ]]; then
974           next_public_ip=$(next_usable_ip $next_public_ip)
975         else
976           next_public_ip=$(increment_ip $next_public_ip 10)
977         fi
978       fi
979
980       public_output=$(grep -E '*public_vip' opnfv_ksgen_settings.yml)
981       if [ ! -z "$public_output" ]; then
982         while read -r line; do
983           if echo $line | grep horizon_public_vip; then
984             horizon_public_vip=$next_public_ip
985           fi
986           sed -i 's/^.*'"$line"'.*$/  '"$line $next_public_ip"'/' opnfv_ksgen_settings.yml
987           next_public_ip=$(next_usable_ip $next_public_ip)
988           if [ ! "$next_public_ip" ]; then
989             printf '%s\n' 'deploy.sh: Unable to find next ip for public network for vip replcement' >&2
990             exit 1
991           fi
992         done <<< "$public_output"
993       fi
994
995       ##replace public_network param
996       public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
997       sed -i 's/^.*public_network:.*$/  public_network:'" $public_subnet"'/' opnfv_ksgen_settings.yml
998       if [ "$deployment_type" == "single_network" ]; then
999         sed -i 's/^.*private_network:.*$/  private_network:'" $public_subnet"'/' opnfv_ksgen_settings.yml
1000       else
1001         ##replace private_network param
1002         private_subnet=$(find_subnet $next_private_ip $private_subnet_mask)
1003         sed -i 's/^.*private_network:.*$/  private_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
1004       fi
1005
1006       ##replace storage_network
1007       if [ "$deployment_type" == "single_network" ]; then
1008         sed -i 's/^.*storage_network:.*$/  storage_network:'" $public_subnet"'/' opnfv_ksgen_settings.yml
1009       elif [ "$deployment_type" == "three_network" ]; then
1010         sed -i 's/^.*storage_network:.*$/  storage_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
1011       else
1012         next_storage_ip=${interface_ip_arr[3]}
1013         storage_subnet=$(find_subnet $next_storage_ip $storage_subnet_mask)
1014         sed -i 's/^.*storage_network:.*$/  storage_network:'" $storage_subnet"'/' opnfv_ksgen_settings.yml
1015       fi
1016
1017       ##replace public_subnet param
1018       public_subnet=$public_subnet'\'$public_short_subnet_mask
1019       sed -i 's/^.*public_subnet:.*$/  public_subnet:'" $public_subnet"'/' opnfv_ksgen_settings.yml
1020       if [ "$deployment_type" == "single_network" ]; then
1021         sed -i 's/^.*private_subnet:.*$/  private_subnet:'" $public_subnet"'/' opnfv_ksgen_settings.yml
1022       else
1023         ##replace private_subnet param
1024         private_subnet=$private_subnet'\'$private_short_subnet_mask
1025         sed -i 's/^.*private_subnet:.*$/  private_subnet:'" $private_subnet"'/' opnfv_ksgen_settings.yml
1026       fi
1027
1028       ##replace public_dns param to be foreman server
1029       if [ "$deployment_type" == "single_network" ]; then
1030         sed -i 's/^.*public_dns:.*$/  public_dns: '${interface_ip_arr[0]}'/' opnfv_ksgen_settings.yml
1031       else
1032         sed -i 's/^.*public_dns:.*$/  public_dns: '${interface_ip_arr[2]}'/' opnfv_ksgen_settings.yml
1033       fi
1034
1035       ##replace public_gateway
1036       if [ -z "$public_gateway" ]; then
1037         if [ "$deployment_type" == "single_network" ]; then
1038           public_gateway=$node_default_gw
1039         else
1040           ##if unset then we assume its the first IP in the public subnet
1041           public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
1042           public_gateway=$(increment_subnet $public_subnet 1)
1043         fi
1044       fi
1045       sed -i 's/^.*public_gateway:.*$/  public_gateway:'" $public_gateway"'/' opnfv_ksgen_settings.yml
1046
1047       ##we have to define an allocation range of the public subnet to give
1048       ##to neutron to use as floating IPs
1049       ##if static ip range, then we take the difference of the end range and current ip
1050       ## to be the allocation pool
1051       ##if not static ip, we will use the last 20 IP from the subnet
1052       ## note that this is not a really good idea because the subnet must be at least a /27 for this to work...
1053       public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
1054       if [ ! -z "$static_ip_range" ]; then
1055         begin_octet=$(echo $next_public_ip | cut -d . -f4)
1056         end_octet=$(echo $static_ip_range_end | cut -d . -f4)
1057         ip_diff=$((end_octet-begin_octet))
1058         if [ $ip_diff -le 0 ]; then
1059           echo "${red}ip range left for floating range is less than or equal to 0! $ipdiff ${reset}"
1060           exit 1
1061         else
1062           public_allocation_start=$(next_ip $next_public_ip)
1063           public_allocation_end=$static_ip_range_end
1064         fi
1065       else
1066         last_ip_subnet=$(find_last_ip_subnet $next_public_ip $public_subnet_mask)
1067         public_allocation_start=$(subtract_ip $last_ip_subnet $floating_ip_count )
1068         public_allocation_end=${last_ip_subnet}
1069       fi
1070       echo "${blue}Neutron Floating IP range: $public_allocation_start to $public_allocation_end ${reset}"
1071
1072       sed -i 's/^.*public_allocation_start:.*$/  public_allocation_start:'" $public_allocation_start"'/' opnfv_ksgen_settings.yml
1073       sed -i 's/^.*public_allocation_end:.*$/  public_allocation_end:'" $public_allocation_end"'/' opnfv_ksgen_settings.yml
1074
1075     else
1076       printf '%s\n' 'deploy.sh: Unknown network type: $deployment_type' >&2
1077       exit 1
1078     fi
1079
1080     echo "${blue}Parameters Complete.  Settings have been set for Foreman. ${reset}"
1081
1082   fi
1083 }
1084
1085 ##Configure bootstrap.sh to use the virtual Khaleesi playbook
1086 ##params: none
1087 ##usage: configure_virtual()
1088 configure_virtual() {
1089   if [ $virtual ]; then
1090     echo "${blue} Virtual flag detected, setting Khaleesi playbook to be opnfv-vm.yml ${reset}"
1091     sed -i 's/opnfv.yml/opnfv-vm.yml/' bootstrap.sh
1092   fi
1093 }
1094
1095 ##Starts Foreman VM with Vagrant
1096 ##params: none
1097 ##usage: start_vagrant()
1098 start_foreman() {
1099   echo "${blue}Starting Vagrant! ${reset}"
1100
1101   ##stand up vagrant
1102   if ! vagrant up; then
1103     printf '%s\n' 'deploy.sh: Unable to complete Foreman VM install' >&2
1104     exit 1
1105   else
1106     echo "${blue}Foreman VM is up! ${reset}"
1107   fi
1108 }
1109
1110 ##start the VM if this is a virtual installation
1111 ##this function does nothing if baremetal servers are being used
1112 ##params: none
1113 ##usage: start_virtual_nodes()
1114 start_virtual_nodes() {
1115   if [ $virtual ]; then
1116
1117     ##Bring up VM nodes
1118     echo "${blue}Setting VMs up... ${reset}"
1119     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'`
1120     ##due to ODL Helium bug of OVS connecting to ODL too early, we need controllers to install first
1121     ##this is fix kind of assumes more than I would like to, but for now it should be OK as we always have
1122     ##3 static controllers
1123     compute_nodes=`echo $nodes | tr " " "\n" | grep -v controller | tr "\n" " "`
1124     controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
1125     nodes=${controller_nodes}${compute_nodes}
1126     controller_count=0
1127     compute_wait_completed=false
1128
1129     for node in ${nodes}; do
1130
1131       ##remove VM nodes incase it wasn't cleaned up
1132       rm -rf $vm_dir/$node
1133       rm -rf /tmp/genesis/
1134
1135       ##clone genesis and move into node folder
1136       clone_bgs $vm_dir/$node
1137
1138       cd $vm_dir/$node
1139
1140       if [ $base_config ]; then
1141         if ! cp -f $base_config opnfv_ksgen_settings.yml; then
1142           echo "${red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
1143           exit 1
1144         fi
1145       fi
1146
1147       ##parse yaml into variables
1148       eval $(parse_yaml opnfv_ksgen_settings.yml "config_")
1149       ##find node type
1150       node_type=config_nodes_${node}_type
1151       node_type=$(eval echo \$$node_type)
1152
1153       ##modify memory and cpu
1154       node_memory=$(eval echo \${config_nodes_${node}_memory})
1155       node_vcpus=$(eval echo \${config_nodes_${node}_cpus})
1156       node_storage=$(eval echo \${config_nodes_${node}_disk})
1157
1158       sed -i 's/^.*vb.memory =.*$/     vb.memory = '"$node_memory"'/' Vagrantfile
1159       sed -i 's/^.*vb.cpus =.*$/     vb.cpus = '"$node_vcpus"'/' Vagrantfile
1160
1161       if ! resize_vagrant_disk $node_storage; then
1162         echo "${red}Error while resizing vagrant box to size $node_storage for $node! ${reset}"
1163         exit 1
1164       fi
1165
1166       ##trozet test make compute nodes wait 20 minutes
1167       if [ "$compute_wait_completed" = false ] && [ "$node_type" != "controller" ]; then
1168         echo "${blue}Waiting 20 minutes for Control nodes to install before continuing with Compute nodes..."
1169         compute_wait_completed=true
1170         sleep 1400
1171       fi
1172
1173       ## Add Admin interface
1174       mac_string=config_nodes_${node}_mac_address
1175       mac_addr=$(eval echo \$$mac_string)
1176       mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
1177       if [ $mac_addr == "" ]; then
1178         echo "${red} Unable to find mac_address for $node! ${reset}"
1179         exit 1
1180       fi
1181       this_admin_ip=${admin_ip_arr[$node]}
1182       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
1183
1184       ## Add private interface
1185       if [ "$node_type" == "controller" ]; then
1186           mac_string=config_nodes_${node}_private_mac
1187           mac_addr=$(eval echo \$$mac_string)
1188           if [ $mac_addr == "" ]; then
1189             echo "${red} Unable to find private_mac for $node! ${reset}"
1190             exit 1
1191           fi
1192       else
1193           ##generate random mac
1194           mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
1195       fi
1196       mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
1197       if [ "$node_type" == "controller" ]; then
1198         new_node_ip=${controllers_ip_arr[$controller_count]}
1199         if [ ! "$new_node_ip" ]; then
1200           echo "{red}ERROR: Empty node ip for controller $controller_count ${reset}"
1201           exit 1
1202         fi
1203         ((controller_count++))
1204       else
1205         next_private_ip=$(next_ip $next_private_ip)
1206         if [ ! "$next_private_ip" ]; then
1207           echo "{red}ERROR: Could not find private ip for $node ${reset}"
1208           exit 1
1209         fi
1210         new_node_ip=$next_private_ip
1211       fi
1212       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
1213       ##replace host_ip in vm_nodes_provision with private ip
1214       sed -i 's/^host_ip=REPLACE/host_ip='$new_node_ip'/' vm_nodes_provision.sh
1215       ##replace ping site
1216       if [ ! -z "$ping_site" ]; then
1217         sed -i 's/www.google.com/'$ping_site'/' vm_nodes_provision.sh
1218       fi
1219
1220       ##find public ip info and add public interface
1221       mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
1222       mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
1223       this_public_ip=${public_ip_arr[$node]}
1224
1225       if [ -z "$enable_virtual_dhcp" ]; then
1226         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
1227       else
1228         sed -i 's/^.*eth_replace2.*$/  config.vm.network "public_network", bridge: '\'"$public_interface"\'', :mac => '\""$mac_addr"\"'/' Vagrantfile
1229       fi
1230       remove_vagrant_network eth_replace3
1231
1232       ##modify provisioning to do puppet install, config, and foreman check-in
1233       ##substitute host_name and dns_server in the provisioning script
1234       host_string=config_nodes_${node}_short_name
1235       short_host_name=$(eval echo \$$host_string)
1236       ##substitute domain_name
1237       domain_name=$config_domain_name
1238       sed -i 's/^domain_name=REPLACE/domain_name='$domain_name'/' vm_nodes_provision.sh
1239       host_name=${short_host_name}.${domain_name}
1240       sed -i 's/^host_name=REPLACE/host_name='$host_name'/' vm_nodes_provision.sh
1241       ##dns server should be the foreman server
1242       sed -i 's/^dns_server=REPLACE/dns_server='${interface_ip_arr[0]}'/' vm_nodes_provision.sh
1243       ## remove bootstrap and NAT provisioning
1244       sed -i '/nat_setup.sh/d' Vagrantfile
1245       sed -i 's/bootstrap.sh/vm_nodes_provision.sh/' Vagrantfile
1246       ## modify default_gw to be node_default_gw
1247       sed -i 's/^.*default_gw =.*$/  default_gw = '\""$node_default_gw"\"'/' Vagrantfile
1248       echo "${blue}Starting Vagrant Node $node! ${reset}"
1249       ##stand up vagrant
1250       if ! vagrant up; then
1251         echo "${red} Unable to start $node ${reset}"
1252         exit 1
1253       else
1254         echo "${blue} $node VM is up! ${reset}"
1255       fi
1256     done
1257     echo "${blue} All VMs are UP! ${reset}"
1258     echo "${blue} Waiting for puppet to complete on the nodes... ${reset}"
1259     ##check puppet is complete
1260     ##ssh into foreman server, run check to verify puppet is complete
1261     pushd $vm_dir/foreman_vm
1262     if ! vagrant ssh -c "/opt/khaleesi/run.sh --no-logs --use /vagrant/opnfv_ksgen_settings.yml /opt/khaleesi/playbooks/validate_opnfv-vm.yml"; then
1263       echo "${red} Failed to validate puppet completion on nodes ${reset}"
1264       exit 1
1265     else
1266       echo "{$blue} Puppet complete on all nodes! ${reset}"
1267     fi
1268     popd
1269     ##add routes back to nodes
1270     for node in ${nodes}; do
1271       pushd $vm_dir/$node
1272       if ! vagrant ssh -c "route | grep default | grep $this_default_gw"; then
1273         echo "${blue} Adding public route back to $node! ${reset}"
1274         vagrant ssh -c "route add default gw $this_default_gw"
1275         vagrant ssh -c "route delete default gw 10.0.2.2"
1276       fi
1277       popd
1278     done
1279     if [ ! -z "$horizon_public_vip" ]; then
1280       echo "${blue} Virtual deployment SUCCESS!! Foreman URL:  http://${foreman_ip}, Horizon URL: http://${horizon_public_vip} ${reset}"
1281     else
1282       ##Find public IP of controller
1283       for node in ${nodes}; do
1284         node_type=config_nodes_${node}_type
1285         node_type=$(eval echo \$$node_type)
1286         if [ "$node_type" == "controller" ]; then
1287           pushd $vm_dir/$node
1288           horizon_ip=`vagrant ssh -c "ifconfig enp0s10" | grep -Eo "inet [0-9\.]+" | awk {'print $2'}`
1289           popd
1290           break
1291         fi
1292       done
1293       if [ -z "$horizon_ip" ]; then
1294         echo "${red}Warn: Unable to determine horizon IP, please login to your controller node to find it${reset}"
1295       fi
1296       echo "${blue} Virtual deployment SUCCESS!! Foreman URL:  http://${foreman_ip}, Horizon URL: http://${horizon_ip} ${reset}"
1297     fi
1298   fi
1299 }
1300
1301 ##check to make sure nodes are powered off
1302 ##this function does nothing if virtual
1303 ##params: none
1304 ##usage: check_baremetal_nodes()
1305 check_baremetal_nodes() {
1306   if [ $virtual ]; then
1307     echo "${blue}Skipping Baremetal node power status check as deployment is virtual ${reset}"
1308   else
1309     echo "${blue}Checking Baremetal nodes power state... ${reset}"
1310     if [ ! -z "$base_config" ]; then
1311       # Install ipmitool
1312       # Major version is pinned to force some consistency for Arno
1313       if ! yum list installed | grep -i ipmitool; then
1314         echo "${blue}Installing ipmitool...${reset}"
1315         if ! yum -y install ipmitool-1*; then
1316           echo "${red}Failed to install ipmitool!${reset}"
1317           exit 1
1318         fi
1319       fi
1320
1321         ###find all the bmc IPs and number of nodes
1322       node_counter=0
1323       output=`grep bmc_ip $base_config | grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+'`
1324       for line in ${output} ; do
1325         bmc_ip[$node_counter]=$line
1326         ((node_counter++))
1327       done
1328
1329       max_nodes=$((node_counter-1))
1330
1331       ###find bmc_users per node
1332       node_counter=0
1333       output=`grep bmc_user $base_config | sed 's/\s*bmc_user:\s*//'`
1334       for line in ${output} ; do
1335         bmc_user[$node_counter]=$line
1336         ((node_counter++))
1337       done
1338
1339       ###find bmc_pass per node
1340       node_counter=0
1341       output=`grep bmc_pass $base_config | sed 's/\s*bmc_pass:\s*//'`
1342       for line in ${output} ; do
1343         bmc_pass[$node_counter]=$line
1344         ((node_counter++))
1345       done
1346
1347       for mynode in `seq 0 $max_nodes`; do
1348         echo "${blue}Node: ${bmc_ip[$mynode]} ${bmc_user[$mynode]} ${bmc_pass[$mynode]} ${reset}"
1349         ipmi_output=`ipmitool -I lanplus -P ${bmc_pass[$mynode]} -U ${bmc_user[$mynode]} -H ${bmc_ip[$mynode]} chassis status \
1350                     | grep "System Power" | cut -d ':' -f2 | tr -d [:blank:]`
1351         if [ "$ipmi_output" == "on" ]; then
1352           echo "${red}Error: Node is powered on: ${bmc_ip[$mynode]} ${reset}"
1353           echo "${red}Please run clean.sh before running deploy! ${reset}"
1354           exit 1
1355         elif [ "$ipmi_output" == "off" ]; then
1356           echo "${blue}Node: ${bmc_ip[$mynode]} is powered off${reset}"
1357         else
1358           echo "${red}Warning: Unable to detect node power state: ${bmc_ip[$mynode]} ${reset}"
1359         fi
1360       done
1361     else
1362       echo "${red}base_config was not provided for a baremetal install! Exiting${reset}"
1363       exit 1
1364     fi
1365   fi
1366 }
1367
1368 ##resizes vagrant disk (cannot shrink)
1369 ##params: size in GB
1370 ##usage: resize_vagrant_disk 100
1371 resize_vagrant_disk() {
1372   if [[ "$1" < 40 ]]; then
1373     echo "${blue}Warn: Requested disk size cannot be less than 40, using 40 as new size${reset}"
1374     new_size_gb=40
1375   else
1376     new_size_gb=$1
1377   fi
1378
1379   if ! vagrant box list | grep opnfv; then
1380     vagrant box remove -f opnfv/centos-7.0
1381     if ! vagrant box add opnfv/centos-7.0 --provider virtualbox; then
1382       echo "${red}Unable to reclone vagrant box! Exiting...${reset}"
1383       exit 1
1384     fi
1385   fi
1386
1387   pushd $vagrant_box_dir
1388
1389   # Close medium to make sure we can modify it
1390   vboxmanage closemedium disk $vagrant_box_vmdk
1391
1392   cur_size=$(vboxmanage showhdinfo $vagrant_box_vmdk | grep -i capacity | grep -Eo [0-9]+)
1393   cur_size_gb=$((cur_size / 1024))
1394
1395   if [ "$cur_size_gb" -eq "$new_size_gb" ]; then
1396     echo "${blue}Info: Disk size already ${cur_size_gb} ${reset}"
1397     popd
1398     return
1399   elif [[ "$new_size_gb" < "$cur_size_gb" ]] ; then
1400     echo "${blue}Info: Requested disk is less than ${cur_size_gb} ${reset}"
1401     echo "${blue}Re-adding vagrant box${reset}"
1402     if vagrant box list | grep opnfv; then
1403       popd
1404       vagrant box remove -f opnfv/centos-7.0
1405       if ! vagrant box add opnfv/centos-7.0 --provider virtualbox; then
1406         echo "${red}Unable to reclone vagrant box! Exiting...${reset}"
1407         exit 1
1408       fi
1409       pushd $vagrant_box_dir
1410     fi
1411   fi
1412
1413   new_size=$((new_size_gb * 1024))
1414   if ! vboxmanage clonehd $vagrant_box_vmdk tmp-disk.vdi --format vdi; then
1415     echo "${red}Error: Unable to clone ${vagrant_box_vmdk}${reset}"
1416     popd
1417     return 1
1418   fi
1419
1420   if ! vboxmanage modifyhd tmp-disk.vdi --resize $new_size; then
1421     echo "${red}Error: Unable modify tmp-disk.vdi to ${new_size}${reset}"
1422     popd
1423     return 1
1424   fi
1425
1426   if  ! vboxmanage clonehd tmp-disk.vdi resized-disk.vmdk --format vmdk; then
1427     echo "${red}Error: Unable clone tmp-disk.vdi to vmdk${reset}"
1428     popd
1429     return 1
1430   fi
1431
1432   vboxmanage closemedium disk tmp-disk.vdi --delete
1433   rm -f tmp-disk.vdi $vagrant_box_vmdk
1434   cp -f resized-disk.vmdk $vagrant_box_vmdk
1435   vboxmanage closemedium disk resized-disk.vmdk --delete
1436   popd
1437 }
1438
1439 ##END FUNCTIONS
1440
1441 main() {
1442   parse_cmdline "$@"
1443   disable_selinux
1444   check_baremetal_nodes
1445   install_EPEL
1446   install_vbox
1447   install_ansible
1448   install_vagrant
1449   clean_tmp
1450   verify_vm_dir
1451   clone_bgs $vm_dir/foreman_vm
1452   configure_network
1453   configure_virtual
1454   start_foreman
1455   start_virtual_nodes
1456 }
1457
1458 main "$@"