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