Merge "Generally refactor Foreman installation guide"
[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 ##END VARS
29
30 ##FUNCTIONS
31 display_usage() {
32   echo -e "\n\n${blue}This script is used to deploy Foreman/QuickStack Installer and Provision OPNFV Target System${reset}\n\n"
33   echo -e "\n${green}Make sure you have the latest kernel installed before running this script! (yum update kernel +reboot)${reset}\n"
34   echo -e "\nUsage:\n$0 [arguments] \n"
35   echo -e "\n   -no_parse : No variable parsing into config. Flag. \n"
36   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"
37   echo -e "\n   -virtual : Node virtualization instead of baremetal. Flag. \n"
38 }
39
40 ##find ip of interface
41 ##params: interface name
42 function find_ip {
43   ip addr show $1 | grep -Eo '^\s+inet\s+[\.0-9]+' | awk '{print $2}'
44 }
45
46 ##finds subnet of ip and netmask
47 ##params: ip, netmask
48 function find_subnet {
49   IFS=. read -r i1 i2 i3 i4 <<< "$1"
50   IFS=. read -r m1 m2 m3 m4 <<< "$2"
51   printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"
52 }
53
54 ##finds netmask of interface
55 ##params: interface
56 ##returns long format 255.255.x.x
57 function find_netmask {
58   ifconfig $1 | grep -Eo 'netmask\s+[\.0-9]+' | awk '{print $2}'
59 }
60
61 ##finds short netmask of interface
62 ##params: interface
63 ##returns short format, ex: /21
64 function find_short_netmask {
65   echo "/$(ip addr show $1 | grep -Eo '^\s+inet\s+[\/\.0-9]+' | awk '{print $2}' | cut -d / -f2)"
66 }
67
68 ##increments next IP
69 ##params: ip
70 ##assumes a /24 subnet
71 function next_ip {
72   baseaddr="$(echo $1 | cut -d. -f1-3)"
73   lsv="$(echo $1 | cut -d. -f4)"
74   if [ "$lsv" -ge 254 ]; then
75     return 1
76   fi
77   ((lsv++))
78   echo $baseaddr.$lsv
79 }
80
81 ##removes the network interface config from Vagrantfile
82 ##params: interface
83 ##assumes you are in the directory of Vagrantfile
84 function remove_vagrant_network {
85   sed -i 's/^.*'"$1"'.*$//' Vagrantfile
86 }
87
88 ##check if IP is in use
89 ##params: ip
90 ##ping ip to get arp entry, then check arp
91 function is_ip_used {
92   ping -c 5 $1 > /dev/null 2>&1
93   arp -n | grep "$1 " | grep -iv incomplete > /dev/null 2>&1
94 }
95
96 ##find next usable IP
97 ##params: ip
98 function next_usable_ip {
99   new_ip=$(next_ip $1)
100   while [ "$new_ip" ]; do
101     if ! is_ip_used $new_ip; then
102       echo $new_ip
103       return 0
104     fi
105     new_ip=$(next_ip $new_ip)
106   done
107   return 1
108 }
109
110 ##increment ip by value
111 ##params: ip, amount to increment by
112 ##increment_ip $next_private_ip 10
113 function increment_ip {
114   baseaddr="$(echo $1 | cut -d. -f1-3)"
115   lsv="$(echo $1 | cut -d. -f4)"
116   incrval=$2
117   lsv=$((lsv+incrval))
118   if [ "$lsv" -ge 254 ]; then
119     return 1
120   fi
121   echo $baseaddr.$lsv
122 }
123
124 ##translates yaml into variables
125 ##params: filename, prefix (ex. "config_")
126 ##usage: parse_yaml opnfv_ksgen_settings.yml "config_"
127 parse_yaml() {
128    local prefix=$2
129    local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
130    sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
131         -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p"  $1 |
132    awk -F$fs '{
133       indent = length($1)/2;
134       vname[indent] = $2;
135       for (i in vname) {if (i > indent) {delete vname[i]}}
136       if (length($3) > 0) {
137          vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
138          printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
139       }
140    }'
141 }
142
143 ##END FUNCTIONS
144
145 if [[ ( $1 == "--help") ||  $1 == "-h" ]]; then
146     display_usage
147     exit 0
148 fi
149
150 echo -e "\n\n${blue}This script is used to deploy Foreman/QuickStack Installer and Provision OPNFV Target System${reset}\n\n"
151 echo "Use -h to display help"
152 sleep 2
153
154 while [ "`echo $1 | cut -c1`" = "-" ]
155 do
156     echo $1
157     case "$1" in
158         -base_config)
159                 base_config=$2
160                 shift 2
161             ;;
162         -no_parse)
163                 no_parse="TRUE"
164                 shift 1
165             ;;
166         -virtual)
167                 virtual="TRUE"
168                 shift 1
169             ;;
170         *)
171                 display_usage
172                 exit 1
173             ;;
174 esac
175 done
176
177 ##disable selinux
178 /sbin/setenforce 0
179
180 ##install EPEL
181 if ! yum repolist | grep "epel/"; then
182   if ! rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm; then
183     printf '%s\n' 'deploy.sh: Unable to configure EPEL repo' >&2
184     exit 1
185   fi
186 else
187   printf '%s\n' 'deploy.sh: Skipping EPEL repo as it is already configured.'
188 fi
189
190 ##install dependencies
191 if ! yum -y install binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-devel dkms; then
192   printf '%s\n' 'deploy.sh: Unable to install depdency packages' >&2
193   exit 1
194 fi
195
196 ##install VirtualBox repo
197 if cat /etc/*release | grep -i "Fedora release"; then
198   vboxurl=http://download.virtualbox.org/virtualbox/rpm/fedora/\$releasever/\$basearch
199 else
200   vboxurl=http://download.virtualbox.org/virtualbox/rpm/el/\$releasever/\$basearch
201 fi
202
203 cat > /etc/yum.repos.d/virtualbox.repo << EOM
204 [virtualbox]
205 name=Oracle Linux / RHEL / CentOS-\$releasever / \$basearch - VirtualBox
206 baseurl=$vboxurl
207 enabled=1
208 gpgcheck=1
209 gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
210 skip_if_unavailable = 1
211 keepcache = 0
212 EOM
213
214 ##install VirtualBox
215 if ! yum list installed | grep -i virtualbox; then
216   if ! yum -y install VirtualBox-4.3; then
217     printf '%s\n' 'deploy.sh: Unable to install virtualbox package' >&2
218     exit 1
219   fi
220 fi
221
222 ##install kmod-VirtualBox
223 if ! lsmod | grep vboxdrv; then
224   if ! sudo /etc/init.d/vboxdrv setup; then
225     printf '%s\n' 'deploy.sh: Unable to install kernel module for virtualbox' >&2
226     exit 1
227   fi
228 else
229   printf '%s\n' 'deploy.sh: Skipping kernel module for virtualbox.  Already Installed'
230 fi
231
232 ##install Ansible
233 if ! yum list installed | grep -i ansible; then
234   if ! yum -y install ansible; then
235     printf '%s\n' 'deploy.sh: Unable to install Ansible package' >&2
236     exit 1
237   fi
238 fi
239
240 ##install Vagrant
241 if ! rpm -qa | grep vagrant; then
242   if ! rpm -Uvh https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.rpm; then
243     printf '%s\n' 'deploy.sh: Unable to install vagrant package' >&2
244     exit 1
245   fi
246 else
247   printf '%s\n' 'deploy.sh: Skipping Vagrant install as it is already installed.'
248 fi
249
250 ##add centos 7 box to vagrant
251 if ! vagrant box list | grep chef/centos-7.0; then
252   if ! vagrant box add chef/centos-7.0 --provider virtualbox; then
253     printf '%s\n' 'deploy.sh: Unable to download centos7 box for Vagrant' >&2
254     exit 1
255   fi
256 else
257   printf '%s\n' 'deploy.sh: Skipping Vagrant box add as centos-7.0 is already installed.'
258 fi
259
260 ##install workaround for centos7
261 if ! vagrant plugin list | grep vagrant-centos7_fix; then
262   if ! vagrant plugin install vagrant-centos7_fix; then
263     printf '%s\n' 'deploy.sh: Warning: unable to install vagrant centos7 workaround' >&2
264   fi
265 else
266   printf '%s\n' 'deploy.sh: Skipping Vagrant plugin as centos7 workaround is already installed.'
267 fi
268
269 cd /tmp/
270
271 ##remove bgs vagrant incase it wasn't cleaned up
272 rm -rf /tmp/bgs_vagrant
273
274 ##clone bgs vagrant
275 ##will change this to be opnfv repo when commit is done
276 if ! git clone https://github.com/trozet/bgs_vagrant.git; then
277   printf '%s\n' 'deploy.sh: Unable to clone vagrant repo' >&2
278   exit 1
279 fi
280
281 cd bgs_vagrant
282
283 echo "${blue}Detecting network configuration...${reset}"
284 ##detect host 1 or 3 interface configuration
285 #output=`ip link show | grep -E "^[0-9]" | grep -Ev ": lo|tun|virbr|vboxnet" | awk '{print $2}' | sed 's/://'`
286 output=`ifconfig | grep -E "^[a-zA-Z0-9]+:"| grep -Ev "lo|tun|virbr|vboxnet" | awk '{print $1}' | sed 's/://'`
287
288 if [ ! "$output" ]; then
289   printf '%s\n' 'deploy.sh: Unable to detect interfaces to bridge to' >&2
290   exit 1
291 fi
292
293 ##find number of interfaces with ip and substitute in VagrantFile
294 if_counter=0
295 for interface in ${output}; do
296
297   if [ "$if_counter" -ge 4 ]; then
298     break
299   fi
300   interface_ip=$(find_ip $interface)
301   if [ ! "$interface_ip" ]; then
302     continue
303   fi
304   new_ip=$(next_usable_ip $interface_ip)
305   if [ ! "$new_ip" ]; then
306     continue
307   fi
308   interface_arr[$interface]=$if_counter
309   interface_ip_arr[$if_counter]=$new_ip
310   subnet_mask=$(find_netmask $interface)
311   if [ "$if_counter" -eq 1 ]; then
312     private_subnet_mask=$subnet_mask
313     private_short_subnet_mask=$(find_short_netmask $interface)
314   fi
315   if [ "$if_counter" -eq 3 ]; then
316     storage_subnet_mask=$subnet_mask
317   fi
318   sed -i 's/^.*eth_replace'"$if_counter"'.*$/  config.vm.network "public_network", ip: '\""$new_ip"\"', bridge: '\'"$interface"\'', netmask: '\""$subnet_mask"\"'/' Vagrantfile
319   ((if_counter++))
320 done
321
322 ##now remove interface config in Vagrantfile for 1 node
323 ##if 1, 3, or 4 interfaces set deployment type
324 ##if 2 interfaces remove 2nd interface and set deployment type
325 if [ "$if_counter" == 1 ]; then
326   deployment_type="single_network"
327   remove_vagrant_network eth_replace1
328   remove_vagrant_network eth_replace2
329   remove_vagrant_network eth_replace3
330 elif [ "$if_counter" == 2 ]; then
331   deployment_type="single_network"
332   second_interface=`echo $output | awk '{print $2}'`
333   remove_vagrant_network $second_interface
334   remove_vagrant_network eth_replace2
335 elif [ "$if_counter" == 3 ]; then
336   deployment_type="three_network"
337   remove_vagrant_network eth_replace3
338 else
339   deployment_type="multi_network"
340 fi
341
342 echo "${blue}Network detected: ${deployment_type}! ${reset}"
343
344 if route | grep default; then
345   echo "${blue}Default Gateway Detected ${reset}"
346   host_default_gw=$(ip route | grep default | awk '{print $3}')
347   echo "${blue}Default Gateway: $host_default_gw ${reset}"
348   default_gw_interface=$(ip route get $host_default_gw | awk '{print $3}')
349   case "${interface_arr[$default_gw_interface]}" in
350            0)
351              echo "${blue}Default Gateway Detected on Admin Interface!${reset}"
352              sed -i 's/^.*default_gw =.*$/  default_gw = '\""$host_default_gw"\"'/' Vagrantfile
353              node_default_gw=$host_default_gw
354              ;;
355            1)
356              echo "${red}Default Gateway Detected on Private Interface!${reset}"
357              echo "${red}Private subnet should be private and not have Internet access!${reset}"
358              exit 1
359              ;;
360            2)
361              echo "${blue}Default Gateway Detected on Public Interface!${reset}"
362              sed -i 's/^.*default_gw =.*$/  default_gw = '\""$host_default_gw"\"'/' Vagrantfile
363              echo "${blue}Will setup NAT from Admin -> Public Network on VM!${reset}"
364              sed -i 's/^.*nat_flag =.*$/  nat_flag = true/' Vagrantfile
365              echo "${blue}Setting node gateway to be VM Admin IP${reset}"
366              node_default_gw=${interface_ip_arr[0]}
367              ;;
368            3)
369              echo "${red}Default Gateway Detected on Storage Interface!${reset}"
370              echo "${red}Storage subnet should be private and not have Internet access!${reset}"
371              exit 1
372              ;;
373            *)
374              echo "${red}Unable to determine which interface default gateway is on..Exiting!${reset}"
375              exit 1
376              ;;
377   esac
378 else
379   #assumes 24 bit mask
380   defaultgw=`echo ${interface_ip_arr[0]} | cut -d. -f1-3`
381   firstip=.1
382   defaultgw=$defaultgw$firstip
383   echo "${blue}Unable to find default gateway.  Assuming it is $defaultgw ${reset}"
384   sed -i 's/^.*default_gw =.*$/  default_gw = '\""$defaultgw"\"'/' Vagrantfile
385   node_default_gw=$defaultgw
386 fi
387
388 if [ $base_config ]; then
389   if ! cp -f $base_config opnfv_ksgen_settings.yml; then
390     echo "{red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
391     exit 1
392   fi
393 fi
394
395 if [ $no_parse ]; then
396 echo "${blue}Skipping parsing variables into settings file as no_parse flag is set${reset}"
397
398 else
399
400 echo "${blue}Gathering network parameters for Target System...this may take a few minutes${reset}"
401 ##Edit the ksgen settings appropriately
402 ##ksgen settings will be stored in /vagrant on the vagrant machine
403 ##if single node deployment all the variables will have the same ip
404 ##interface names will be enp0s3, enp0s8, enp0s9 in chef/centos7
405
406 sed -i 's/^.*default_gw:.*$/default_gw:'" $node_default_gw"'/' opnfv_ksgen_settings.yml
407
408 ##replace private interface parameter
409 ##private interface will be of hosts, so we need to know the provisioned host interface name
410 ##we add biosdevname=0, net.ifnames=0 to the kickstart to use regular interface naming convention on hosts
411 ##replace IP for parameters with next IP that will be given to controller
412 if [ "$deployment_type" == "single_network" ]; then
413   ##we also need to assign IP addresses to nodes
414   ##for single node, foreman is managing the single network, so we can't reserve them
415   ##not supporting single network anymore for now
416   echo "{blue}Single Network type is unsupported right now.  Please check your interface configuration.  Exiting. ${reset}"
417   exit 0
418
419 elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_network" ]]; then
420
421   if [ "$deployment_type" == "three_network" ]; then
422     sed -i 's/^.*network_type:.*$/network_type: three_network/' opnfv_ksgen_settings.yml
423   fi
424
425   ##get ip addresses for private network on controllers to make dhcp entries
426   ##required for controllers_ip_array global param
427   next_private_ip=${interface_ip_arr[1]}
428   type=_private
429   for node in controller1 controller2 controller3; do
430     next_private_ip=$(next_usable_ip $next_private_ip)
431     if [ ! "$next_private_ip" ]; then
432        printf '%s\n' 'deploy.sh: Unable to find next ip for private network for control nodes' >&2
433        exit 1
434     fi
435     sed -i 's/'"$node$type"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
436     controller_ip_array=$controller_ip_array$next_private_ip,
437   done
438
439   ##replace global param for contollers_ip_array
440   controller_ip_array=${controller_ip_array%?}
441   sed -i 's/^.*controllers_ip_array:.*$/  controllers_ip_array: '"$controller_ip_array"'/' opnfv_ksgen_settings.yml
442
443   ##now replace all the VIP variables.  admin//private can be the same IP
444   ##we have to use IP's here that won't be allocated to hosts at provisioning time
445   ##therefore we increment the ip by 10 to make sure we have a safe buffer
446   next_private_ip=$(increment_ip $next_private_ip 10)
447
448   grep -E '*private_vip|loadbalancer_vip|db_vip|amqp_vip|*admin_vip' opnfv_ksgen_settings.yml | while read -r line ; do
449     sed -i 's/^.*'"$line"'.*$/  '"$line $next_private_ip"'/' opnfv_ksgen_settings.yml
450     next_private_ip=$(next_usable_ip $next_private_ip)
451     if [ ! "$next_private_ip" ]; then
452        printf '%s\n' 'deploy.sh: Unable to find next ip for private network for vip replacement' >&2
453        exit 1
454     fi
455   done
456
457   ##replace foreman site
458   next_public_ip=${interface_ip_arr[2]}
459   sed -i 's/^.*foreman_url:.*$/  foreman_url:'" https:\/\/$next_public_ip"'\/api\/v2\//' opnfv_ksgen_settings.yml
460   ##replace public vips
461   next_public_ip=$(increment_ip $next_public_ip 10)
462   grep -E '*public_vip' opnfv_ksgen_settings.yml | while read -r line ; do
463     sed -i 's/^.*'"$line"'.*$/  '"$line $next_public_ip"'/' opnfv_ksgen_settings.yml
464     next_public_ip=$(next_usable_ip $next_public_ip)
465     if [ ! "$next_public_ip" ]; then
466        printf '%s\n' 'deploy.sh: Unable to find next ip for public network for vip replcement' >&2
467        exit 1
468     fi
469   done
470
471   ##replace private_network param
472   private_subnet=$(find_subnet $next_private_ip $private_subnet_mask)
473   sed -i 's/^.*private_network:.*$/  private_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
474   ##replace storage_network
475   if [ "$deployment_type" == "three_network" ]; then
476     sed -i 's/^.*storage_network:.*$/  storage_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
477   else
478     next_storage_ip=${interface_ip_arr[3]}
479     storage_subnet=$(find_subnet $next_storage_ip $storage_subnet_mask)
480     sed -i 's/^.*storage_network:.*$/  storage_network:'" $storage_subnet"'/' opnfv_ksgen_settings.yml
481   fi
482
483   ##replace private_subnet param
484   private_subnet=$private_subnet'\'$private_short_subnet_mask
485   sed -i 's/^.*private_subnet:.*$/  private_subnet:'" $private_subnet"'/' opnfv_ksgen_settings.yml
486 else
487   printf '%s\n' 'deploy.sh: Unknown network type: $deployment_type' >&2
488   exit 1
489 fi
490
491 echo "${blue}Parameters Complete.  Settings have been set for Foreman. ${reset}"
492
493 fi
494
495 if [ $virtual ]; then
496   echo "${blue} Virtual flag detected, setting Khaleesi playbook to be opnfv-vm.yml ${reset}"
497   sed -i 's/opnfv.yml/opnfv-vm.yml/' bootstrap.sh
498 fi
499
500 echo "${blue}Starting Vagrant! ${reset}"
501
502 ##stand up vagrant
503 if ! vagrant up; then
504   printf '%s\n' 'deploy.sh: Unable to start vagrant' >&2
505   exit 1
506 else
507   echo "${blue}Foreman VM is up! ${reset}"
508 fi
509
510 if [ $virtual ]; then
511
512 ##Bring up VM nodes
513 echo "${blue}Setting VMs up... ${reset}"
514 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'`
515 ##due to ODL Helium bug of OVS connecting to ODL too early, we need controllers to install first
516 ##this is fix kind of assumes more than I would like to, but for now it should be OK as we always have
517 ##3 static controllers
518 compute_nodes=`echo $nodes | tr " " "\n" | grep -v controller | tr "\n" " "`
519 controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
520 nodes=${controller_nodes}${compute_nodes}
521
522 for node in ${nodes}; do
523   cd /tmp
524
525   ##remove VM nodes incase it wasn't cleaned up
526   rm -rf /tmp/$node
527
528   ##clone bgs vagrant
529   ##will change this to be opnfv repo when commit is done
530   if ! git clone https://github.com/trozet/bgs_vagrant.git $node; then
531     printf '%s\n' 'deploy.sh: Unable to clone vagrant repo' >&2
532     exit 1
533   fi
534
535   cd $node
536
537   if [ $base_config ]; then
538     if ! cp -f $base_config opnfv_ksgen_settings.yml; then
539       echo "{red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
540       exit 1
541     fi
542   fi
543
544   ##parse yaml into variables
545   eval $(parse_yaml opnfv_ksgen_settings.yml "config_")
546   ##find node type
547   node_type=config_nodes_${node}_type
548   node_type=$(eval echo \$$node_type)
549
550   ##find number of interfaces with ip and substitute in VagrantFile
551   output=`ifconfig | grep -E "^[a-zA-Z0-9]+:"| grep -Ev "lo|tun|virbr|vboxnet" | awk '{print $1}' | sed 's/://'`
552
553   if [ ! "$output" ]; then
554     printf '%s\n' 'deploy.sh: Unable to detect interfaces to bridge to' >&2
555     exit 1
556   fi
557
558
559   if_counter=0
560   for interface in ${output}; do
561
562     if [ "$if_counter" -ge 4 ]; then
563       break
564     fi
565     interface_ip=$(find_ip $interface)
566     if [ ! "$interface_ip" ]; then
567       continue
568     fi
569     case "${if_counter}" in
570            0)
571              mac_string=config_nodes_${node}_mac_address
572              mac_addr=$(eval echo \$$mac_string)
573              mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
574              if [ $mac_addr == "" ]; then
575                  echo "${red} Unable to find mac_address for $node! ${reset}"
576                  exit 1
577              fi
578              ;;
579            1)
580              if [ "$node_type" == "controller" ]; then
581                mac_string=config_nodes_${node}_private_mac
582                mac_addr=$(eval echo \$$mac_string)
583                if [ $mac_addr == "" ]; then
584                  echo "${red} Unable to find private_mac for $node! ${reset}"
585                  exit 1
586                fi
587              else
588                ##generate random mac
589                mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
590              fi
591              mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
592              ;;
593            *)
594              mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
595              mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
596              ;;
597     esac
598     sed -i 's/^.*eth_replace'"$if_counter"'.*$/  config.vm.network "public_network", bridge: '\'"$interface"\'', :mac => '\""$mac_addr"\"'/' Vagrantfile
599     ((if_counter++))
600   done
601
602   ##now remove interface config in Vagrantfile for 1 node
603   ##if 1, 3, or 4 interfaces set deployment type
604   ##if 2 interfaces remove 2nd interface and set deployment type
605   if [ "$if_counter" == 1 ]; then
606     deployment_type="single_network"
607     remove_vagrant_network eth_replace1
608     remove_vagrant_network eth_replace2
609     remove_vagrant_network eth_replace3
610   elif [ "$if_counter" == 2 ]; then
611     deployment_type="single_network"
612     second_interface=`echo $output | awk '{print $2}'`
613     remove_vagrant_network $second_interface
614     remove_vagrant_network eth_replace2
615   elif [ "$if_counter" == 3 ]; then
616     deployment_type="three_network"
617     remove_vagrant_network eth_replace3
618   else
619     deployment_type="multi_network"
620   fi
621
622   ##modify provisioning to do puppet install, config, and foreman check-in
623   ##substitute host_name and dns_server in the provisioning script
624   host_string=config_nodes_${node}_hostname
625   host_name=$(eval echo \$$host_string)
626   sed -i 's/^host_name=REPLACE/host_name='$host_name'/' vm_nodes_provision.sh
627   ##dns server should be the foreman server
628   sed -i 's/^dns_server=REPLACE/dns_server='${interface_ip_arr[0]}'/' vm_nodes_provision.sh
629
630   ## remove bootstrap and NAT provisioning
631   sed -i '/nat_setup.sh/d' Vagrantfile
632   sed -i 's/bootstrap.sh/vm_nodes_provision.sh/' Vagrantfile
633
634   ## modify default_gw to be node_default_gw
635   sed -i 's/^.*default_gw =.*$/  default_gw = '\""$node_default_gw"\"'/' Vagrantfile
636
637   ## modify VM memory to be 4gig
638   sed -i 's/^.*vb.memory =.*$/     vb.memory = 4096/' Vagrantfile
639
640   echo "${blue}Starting Vagrant Node $node! ${reset}"
641
642   ##stand up vagrant
643   if ! vagrant up; then
644     echo "${red} Unable to start $node ${reset}"
645     exit 1
646   else
647     echo "${blue} $node VM is up! ${reset}"
648   fi
649
650 done
651
652  echo "${blue} All VMs are UP! ${reset}"
653
654 fi