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