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