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