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