Merge "Fix ISO name in Fuel Install Instruction"
[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 repo for access to many other yum repos
190 # Major version is pinned to force some consistency for Arno
191 yum install -y epel-release-7*
192
193 # Install other required packages
194 # Major versions are pinned to force some consistency for Arno
195 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
196   printf '%s\n' 'deploy.sh: Unable to install depdency packages' >&2
197   exit 1
198 fi
199
200 ##install VirtualBox repo
201 if cat /etc/*release | grep -i "Fedora release"; then
202   vboxurl=http://download.virtualbox.org/virtualbox/rpm/fedora/\$releasever/\$basearch
203 else
204   vboxurl=http://download.virtualbox.org/virtualbox/rpm/el/\$releasever/\$basearch
205 fi
206
207 cat > /etc/yum.repos.d/virtualbox.repo << EOM
208 [virtualbox]
209 name=Oracle Linux / RHEL / CentOS-\$releasever / \$basearch - VirtualBox
210 baseurl=$vboxurl
211 enabled=1
212 gpgcheck=1
213 gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
214 skip_if_unavailable = 1
215 keepcache = 0
216 EOM
217
218 ##install VirtualBox
219 if ! yum list installed | grep -i virtualbox; then
220   if ! yum -y install VirtualBox-4.3; then
221     printf '%s\n' 'deploy.sh: Unable to install virtualbox package' >&2
222     exit 1
223   fi
224 fi
225
226 ##install kmod-VirtualBox
227 if ! lsmod | grep vboxdrv; then
228   if ! sudo /etc/init.d/vboxdrv setup; then
229     printf '%s\n' 'deploy.sh: Unable to install kernel module for virtualbox' >&2
230     exit 1
231   fi
232 else
233   printf '%s\n' 'deploy.sh: Skipping kernel module for virtualbox.  Already Installed'
234 fi
235
236 ##install Ansible
237 if ! yum list installed | grep -i ansible; then
238   if ! yum -y install ansible-1*; then
239     printf '%s\n' 'deploy.sh: Unable to install Ansible package' >&2
240     exit 1
241   fi
242 fi
243
244 ##install Vagrant
245 if ! rpm -qa | grep vagrant; then
246   if ! rpm -Uvh https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.rpm; then
247     printf '%s\n' 'deploy.sh: Unable to install vagrant package' >&2
248     exit 1
249   fi
250 else
251   printf '%s\n' 'deploy.sh: Skipping Vagrant install as it is already installed.'
252 fi
253
254 ##add centos 7 box to vagrant
255 if ! vagrant box list | grep chef/centos-7.0; then
256   if ! vagrant box add chef/centos-7.0 --provider virtualbox; then
257     printf '%s\n' 'deploy.sh: Unable to download centos7 box for Vagrant' >&2
258     exit 1
259   fi
260 else
261   printf '%s\n' 'deploy.sh: Skipping Vagrant box add as centos-7.0 is already installed.'
262 fi
263
264 ##install workaround for centos7
265 if ! vagrant plugin list | grep vagrant-centos7_fix; then
266   if ! vagrant plugin install vagrant-centos7_fix; then
267     printf '%s\n' 'deploy.sh: Warning: unable to install vagrant centos7 workaround' >&2
268   fi
269 else
270   printf '%s\n' 'deploy.sh: Skipping Vagrant plugin as centos7 workaround is already installed.'
271 fi
272
273 cd /tmp/
274
275 ##remove bgs vagrant incase it wasn't cleaned up
276 rm -rf /tmp/bgs_vagrant
277
278 ##clone bgs vagrant
279 ##will change this to be opnfv repo when commit is done
280 if ! git clone -b v1.0 https://github.com/trozet/bgs_vagrant.git; then
281   printf '%s\n' 'deploy.sh: Unable to clone vagrant repo' >&2
282   exit 1
283 fi
284
285 cd bgs_vagrant
286
287 echo "${blue}Detecting network configuration...${reset}"
288 ##detect host 1 or 3 interface configuration
289 #output=`ip link show | grep -E "^[0-9]" | grep -Ev ": lo|tun|virbr|vboxnet" | awk '{print $2}' | sed 's/://'`
290 output=`ifconfig | grep -E "^[a-zA-Z0-9]+:"| grep -Ev "lo|tun|virbr|vboxnet" | awk '{print $1}' | sed 's/://'`
291
292 if [ ! "$output" ]; then
293   printf '%s\n' 'deploy.sh: Unable to detect interfaces to bridge to' >&2
294   exit 1
295 fi
296
297 ##find number of interfaces with ip and substitute in VagrantFile
298 if_counter=0
299 for interface in ${output}; do
300
301   if [ "$if_counter" -ge 4 ]; then
302     break
303   fi
304   interface_ip=$(find_ip $interface)
305   if [ ! "$interface_ip" ]; then
306     continue
307   fi
308   new_ip=$(next_usable_ip $interface_ip)
309   if [ ! "$new_ip" ]; then
310     continue
311   fi
312   interface_arr[$interface]=$if_counter
313   interface_ip_arr[$if_counter]=$new_ip
314   subnet_mask=$(find_netmask $interface)
315   if [ "$if_counter" -eq 1 ]; then
316     private_subnet_mask=$subnet_mask
317     private_short_subnet_mask=$(find_short_netmask $interface)
318   fi
319   if [ "$if_counter" -eq 2 ]; then
320     public_subnet_mask=$subnet_mask
321     public_short_subnet_mask=$(find_short_netmask $interface)
322   fi
323   if [ "$if_counter" -eq 3 ]; then
324     storage_subnet_mask=$subnet_mask
325   fi
326   sed -i 's/^.*eth_replace'"$if_counter"'.*$/  config.vm.network "public_network", ip: '\""$new_ip"\"', bridge: '\'"$interface"\'', netmask: '\""$subnet_mask"\"'/' Vagrantfile
327   ((if_counter++))
328 done
329
330 ##now remove interface config in Vagrantfile for 1 node
331 ##if 1, 3, or 4 interfaces set deployment type
332 ##if 2 interfaces remove 2nd interface and set deployment type
333 if [ "$if_counter" == 1 ]; then
334   deployment_type="single_network"
335   remove_vagrant_network eth_replace1
336   remove_vagrant_network eth_replace2
337   remove_vagrant_network eth_replace3
338 elif [ "$if_counter" == 2 ]; then
339   deployment_type="single_network"
340   second_interface=`echo $output | awk '{print $2}'`
341   remove_vagrant_network $second_interface
342   remove_vagrant_network eth_replace2
343 elif [ "$if_counter" == 3 ]; then
344   deployment_type="three_network"
345   remove_vagrant_network eth_replace3
346 else
347   deployment_type="multi_network"
348 fi
349
350 echo "${blue}Network detected: ${deployment_type}! ${reset}"
351
352 if route | grep default; then
353   echo "${blue}Default Gateway Detected ${reset}"
354   host_default_gw=$(ip route | grep default | awk '{print $3}')
355   echo "${blue}Default Gateway: $host_default_gw ${reset}"
356   default_gw_interface=$(ip route get $host_default_gw | awk '{print $3}')
357   case "${interface_arr[$default_gw_interface]}" in
358            0)
359              echo "${blue}Default Gateway Detected on Admin Interface!${reset}"
360              sed -i 's/^.*default_gw =.*$/  default_gw = '\""$host_default_gw"\"'/' Vagrantfile
361              node_default_gw=$host_default_gw
362              ;;
363            1)
364              echo "${red}Default Gateway Detected on Private Interface!${reset}"
365              echo "${red}Private subnet should be private and not have Internet access!${reset}"
366              exit 1
367              ;;
368            2)
369              echo "${blue}Default Gateway Detected on Public Interface!${reset}"
370              sed -i 's/^.*default_gw =.*$/  default_gw = '\""$host_default_gw"\"'/' Vagrantfile
371              echo "${blue}Will setup NAT from Admin -> Public Network on VM!${reset}"
372              sed -i 's/^.*nat_flag =.*$/  nat_flag = true/' Vagrantfile
373              echo "${blue}Setting node gateway to be VM Admin IP${reset}"
374              node_default_gw=${interface_ip_arr[0]}
375              public_gateway=$default_gw
376              ;;
377            3)
378              echo "${red}Default Gateway Detected on Storage Interface!${reset}"
379              echo "${red}Storage subnet should be private and not have Internet access!${reset}"
380              exit 1
381              ;;
382            *)
383              echo "${red}Unable to determine which interface default gateway is on..Exiting!${reset}"
384              exit 1
385              ;;
386   esac
387 else
388   #assumes 24 bit mask
389   defaultgw=`echo ${interface_ip_arr[0]} | cut -d. -f1-3`
390   firstip=.1
391   defaultgw=$defaultgw$firstip
392   echo "${blue}Unable to find default gateway.  Assuming it is $defaultgw ${reset}"
393   sed -i 's/^.*default_gw =.*$/  default_gw = '\""$defaultgw"\"'/' Vagrantfile
394   node_default_gw=$defaultgw
395 fi
396
397 if [ $base_config ]; then
398   if ! cp -f $base_config opnfv_ksgen_settings.yml; then
399     echo "{red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
400     exit 1
401   fi
402 fi
403
404 if [ $no_parse ]; then
405 echo "${blue}Skipping parsing variables into settings file as no_parse flag is set${reset}"
406
407 else
408
409 echo "${blue}Gathering network parameters for Target System...this may take a few minutes${reset}"
410 ##Edit the ksgen settings appropriately
411 ##ksgen settings will be stored in /vagrant on the vagrant machine
412 ##if single node deployment all the variables will have the same ip
413 ##interface names will be enp0s3, enp0s8, enp0s9 in chef/centos7
414
415 sed -i 's/^.*default_gw:.*$/default_gw:'" $node_default_gw"'/' opnfv_ksgen_settings.yml
416
417 ##replace private interface parameter
418 ##private interface will be of hosts, so we need to know the provisioned host interface name
419 ##we add biosdevname=0, net.ifnames=0 to the kickstart to use regular interface naming convention on hosts
420 ##replace IP for parameters with next IP that will be given to controller
421 if [ "$deployment_type" == "single_network" ]; then
422   ##we also need to assign IP addresses to nodes
423   ##for single node, foreman is managing the single network, so we can't reserve them
424   ##not supporting single network anymore for now
425   echo "{blue}Single Network type is unsupported right now.  Please check your interface configuration.  Exiting. ${reset}"
426   exit 0
427
428 elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_network" ]]; then
429
430   if [ "$deployment_type" == "three_network" ]; then
431     sed -i 's/^.*network_type:.*$/network_type: three_network/' opnfv_ksgen_settings.yml
432   fi
433
434   sed -i 's/^.*deployment_type:.*$/  deployment_type: '"$deployment_type"'/' opnfv_ksgen_settings.yml
435
436   ##get ip addresses for private network on controllers to make dhcp entries
437   ##required for controllers_ip_array global param
438   next_private_ip=${interface_ip_arr[1]}
439   type=_private
440   for node in controller1 controller2 controller3; do
441     next_private_ip=$(next_usable_ip $next_private_ip)
442     if [ ! "$next_private_ip" ]; then
443        printf '%s\n' 'deploy.sh: Unable to find next ip for private network for control nodes' >&2
444        exit 1
445     fi
446     sed -i 's/'"$node$type"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
447     controller_ip_array=$controller_ip_array$next_private_ip,
448   done
449
450   ##replace global param for contollers_ip_array
451   controller_ip_array=${controller_ip_array%?}
452   sed -i 's/^.*controllers_ip_array:.*$/  controllers_ip_array: '"$controller_ip_array"'/' opnfv_ksgen_settings.yml
453
454   ##now replace all the VIP variables.  admin//private can be the same IP
455   ##we have to use IP's here that won't be allocated to hosts at provisioning time
456   ##therefore we increment the ip by 10 to make sure we have a safe buffer
457   next_private_ip=$(increment_ip $next_private_ip 10)
458
459   grep -E '*private_vip|loadbalancer_vip|db_vip|amqp_vip|*admin_vip' opnfv_ksgen_settings.yml | while read -r line ; do
460     sed -i 's/^.*'"$line"'.*$/  '"$line $next_private_ip"'/' opnfv_ksgen_settings.yml
461     next_private_ip=$(next_usable_ip $next_private_ip)
462     if [ ! "$next_private_ip" ]; then
463        printf '%s\n' 'deploy.sh: Unable to find next ip for private network for vip replacement' >&2
464        exit 1
465     fi
466   done
467
468   ##replace foreman site
469   next_public_ip=${interface_ip_arr[2]}
470   sed -i 's/^.*foreman_url:.*$/  foreman_url:'" https:\/\/$next_public_ip"'\/api\/v2\//' opnfv_ksgen_settings.yml
471   ##replace public vips
472   next_public_ip=$(increment_ip $next_public_ip 10)
473   grep -E '*public_vip' opnfv_ksgen_settings.yml | while read -r line ; do
474     sed -i 's/^.*'"$line"'.*$/  '"$line $next_public_ip"'/' opnfv_ksgen_settings.yml
475     next_public_ip=$(next_usable_ip $next_public_ip)
476     if [ ! "$next_public_ip" ]; then
477        printf '%s\n' 'deploy.sh: Unable to find next ip for public network for vip replcement' >&2
478        exit 1
479     fi
480   done
481
482   ##replace public_network param
483   public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
484   sed -i 's/^.*public_network:.*$/  public_network:'" $public_subnet"'/' opnfv_ksgen_settings.yml
485   ##replace private_network param
486   private_subnet=$(find_subnet $next_private_ip $private_subnet_mask)
487   sed -i 's/^.*private_network:.*$/  private_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
488   ##replace storage_network
489   if [ "$deployment_type" == "three_network" ]; then
490     sed -i 's/^.*storage_network:.*$/  storage_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
491   else
492     next_storage_ip=${interface_ip_arr[3]}
493     storage_subnet=$(find_subnet $next_storage_ip $storage_subnet_mask)
494     sed -i 's/^.*storage_network:.*$/  storage_network:'" $storage_subnet"'/' opnfv_ksgen_settings.yml
495   fi
496
497   ##replace public_subnet param
498   public_subnet=$public_subnet'\'$public_short_subnet_mask
499   sed -i 's/^.*public_subnet:.*$/  public_subnet:'" $public_subnet"'/' opnfv_ksgen_settings.yml
500   ##replace private_subnet param
501   private_subnet=$private_subnet'\'$private_short_subnet_mask
502   sed -i 's/^.*private_subnet:.*$/  private_subnet:'" $private_subnet"'/' opnfv_ksgen_settings.yml
503
504   ##replace public_dns param to be foreman server
505   sed -i 's/^.*public_dns:.*$/  public_dns: '${interface_ip_arr[2]}'/' opnfv_ksgen_settings.yml
506
507   ##replace public_gateway
508   if [ -z "$public_gateway" ]; then
509     ##if unset then we assume its the first IP in the public subnet
510     public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
511     public_gateway=$(increment_subnet $public_subnet 1)
512   fi
513   sed -i 's/^.*public_gateway:.*$/  public_gateway:'" $public_gateway"'/' opnfv_ksgen_settings.yml
514
515   ##we have to define an allocation range of the public subnet to give
516   ##to neutron to use as floating IPs
517   ##we should control this subnet, so this range should work .150-200
518   ##but generally this is a bad idea and we are assuming at least a /24 subnet here
519   public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
520   public_allocation_start=$(increment_subnet $public_subnet 150)
521   public_allocation_end=$(increment_subnet $public_subnet 200)
522
523   sed -i 's/^.*public_allocation_start:.*$/  public_allocation_start:'" $public_allocation_start"'/' opnfv_ksgen_settings.yml
524   sed -i 's/^.*public_allocation_end:.*$/  public_allocation_end:'" $public_allocation_end"'/' opnfv_ksgen_settings.yml
525
526 else
527   printf '%s\n' 'deploy.sh: Unknown network type: $deployment_type' >&2
528   exit 1
529 fi
530
531 echo "${blue}Parameters Complete.  Settings have been set for Foreman. ${reset}"
532
533 fi
534
535 if [ $virtual ]; then
536   echo "${blue} Virtual flag detected, setting Khaleesi playbook to be opnfv-vm.yml ${reset}"
537   sed -i 's/opnfv.yml/opnfv-vm.yml/' bootstrap.sh
538 fi
539
540 echo "${blue}Starting Vagrant! ${reset}"
541
542 ##stand up vagrant
543 if ! vagrant up; then
544   printf '%s\n' 'deploy.sh: Unable to start vagrant' >&2
545   exit 1
546 else
547   echo "${blue}Foreman VM is up! ${reset}"
548 fi
549
550 if [ $virtual ]; then
551
552 ##Bring up VM nodes
553 echo "${blue}Setting VMs up... ${reset}"
554 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'`
555 ##due to ODL Helium bug of OVS connecting to ODL too early, we need controllers to install first
556 ##this is fix kind of assumes more than I would like to, but for now it should be OK as we always have
557 ##3 static controllers
558 compute_nodes=`echo $nodes | tr " " "\n" | grep -v controller | tr "\n" " "`
559 controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
560 nodes=${controller_nodes}${compute_nodes}
561
562 for node in ${nodes}; do
563   cd /tmp
564
565   ##remove VM nodes incase it wasn't cleaned up
566   rm -rf /tmp/$node
567
568   ##clone bgs vagrant
569   ##will change this to be opnfv repo when commit is done
570   if ! git clone -b v1.0 https://github.com/trozet/bgs_vagrant.git $node; then
571     printf '%s\n' 'deploy.sh: Unable to clone vagrant repo' >&2
572     exit 1
573   fi
574
575   cd $node
576
577   if [ $base_config ]; then
578     if ! cp -f $base_config opnfv_ksgen_settings.yml; then
579       echo "{red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
580       exit 1
581     fi
582   fi
583
584   ##parse yaml into variables
585   eval $(parse_yaml opnfv_ksgen_settings.yml "config_")
586   ##find node type
587   node_type=config_nodes_${node}_type
588   node_type=$(eval echo \$$node_type)
589
590   ##find number of interfaces with ip and substitute in VagrantFile
591   output=`ifconfig | grep -E "^[a-zA-Z0-9]+:"| grep -Ev "lo|tun|virbr|vboxnet" | awk '{print $1}' | sed 's/://'`
592
593   if [ ! "$output" ]; then
594     printf '%s\n' 'deploy.sh: Unable to detect interfaces to bridge to' >&2
595     exit 1
596   fi
597
598
599   if_counter=0
600   for interface in ${output}; do
601
602     if [ "$if_counter" -ge 4 ]; then
603       break
604     fi
605     interface_ip=$(find_ip $interface)
606     if [ ! "$interface_ip" ]; then
607       continue
608     fi
609     case "${if_counter}" in
610            0)
611              mac_string=config_nodes_${node}_mac_address
612              mac_addr=$(eval echo \$$mac_string)
613              mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
614              if [ $mac_addr == "" ]; then
615                  echo "${red} Unable to find mac_address for $node! ${reset}"
616                  exit 1
617              fi
618              ;;
619            1)
620              if [ "$node_type" == "controller" ]; then
621                mac_string=config_nodes_${node}_private_mac
622                mac_addr=$(eval echo \$$mac_string)
623                if [ $mac_addr == "" ]; then
624                  echo "${red} Unable to find private_mac for $node! ${reset}"
625                  exit 1
626                fi
627              else
628                ##generate random mac
629                mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
630              fi
631              mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
632              ;;
633            *)
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              mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
636              ;;
637     esac
638     sed -i 's/^.*eth_replace'"$if_counter"'.*$/  config.vm.network "public_network", bridge: '\'"$interface"\'', :mac => '\""$mac_addr"\"'/' Vagrantfile
639     ((if_counter++))
640   done
641
642   ##now remove interface config in Vagrantfile for 1 node
643   ##if 1, 3, or 4 interfaces set deployment type
644   ##if 2 interfaces remove 2nd interface and set deployment type
645   if [ "$if_counter" == 1 ]; then
646     deployment_type="single_network"
647     remove_vagrant_network eth_replace1
648     remove_vagrant_network eth_replace2
649     remove_vagrant_network eth_replace3
650   elif [ "$if_counter" == 2 ]; then
651     deployment_type="single_network"
652     second_interface=`echo $output | awk '{print $2}'`
653     remove_vagrant_network $second_interface
654     remove_vagrant_network eth_replace2
655   elif [ "$if_counter" == 3 ]; then
656     deployment_type="three_network"
657     remove_vagrant_network eth_replace3
658   else
659     deployment_type="multi_network"
660   fi
661
662   ##modify provisioning to do puppet install, config, and foreman check-in
663   ##substitute host_name and dns_server in the provisioning script
664   host_string=config_nodes_${node}_hostname
665   host_name=$(eval echo \$$host_string)
666   sed -i 's/^host_name=REPLACE/host_name='$host_name'/' vm_nodes_provision.sh
667   ##dns server should be the foreman server
668   sed -i 's/^dns_server=REPLACE/dns_server='${interface_ip_arr[0]}'/' vm_nodes_provision.sh
669
670   ## remove bootstrap and NAT provisioning
671   sed -i '/nat_setup.sh/d' Vagrantfile
672   sed -i 's/bootstrap.sh/vm_nodes_provision.sh/' Vagrantfile
673
674   ## modify default_gw to be node_default_gw
675   sed -i 's/^.*default_gw =.*$/  default_gw = '\""$node_default_gw"\"'/' Vagrantfile
676
677   ## modify VM memory to be 4gig
678   sed -i 's/^.*vb.memory =.*$/     vb.memory = 4096/' Vagrantfile
679
680   echo "${blue}Starting Vagrant Node $node! ${reset}"
681
682   ##stand up vagrant
683   if ! vagrant up; then
684     echo "${red} Unable to start $node ${reset}"
685     exit 1
686   else
687     echo "${blue} $node VM is up! ${reset}"
688   fi
689
690 done
691
692  echo "${blue} All VMs are UP! ${reset}"
693
694 fi