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