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