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