3 #Deploy script to install provisioning server for Foreman/QuickStack
4 #author: Tim Rozet (trozet@redhat.com)
6 #Uses Vagrant and VirtualBox
7 #VagrantFile uses bootsrap.sh which Installs Khaleesi
8 #Khaleesi will install and configure Foreman/QuickStack
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):
16 #eth1- private network (+storage network in 3 NIC config)
18 #eth3- storage network
19 #script assumes /24 subnet mask
27 declare -A interface_arr
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"
40 ##find ip of interface
41 ##params: interface name
43 ip addr show $1 | grep -Eo '^\s+inet\s+[\.0-9]+' | awk '{print $2}'
46 ##finds subnet of ip and 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))"
54 ##finds netmask of interface
56 ##returns long format 255.255.x.x
57 function find_netmask {
58 ifconfig $1 | grep -Eo 'netmask\s+[\.0-9]+' | awk '{print $2}'
61 ##finds short netmask of interface
63 ##returns short format, ex: /21
64 function find_short_netmask {
65 echo "/$(ip addr show $1 | grep -Eo '^\s+inet\s+[\/\.0-9]+' | awk '{print $2}' | cut -d / -f2)"
70 ##assumes a /24 subnet
72 baseaddr="$(echo $1 | cut -d. -f1-3)"
73 lsv="$(echo $1 | cut -d. -f4)"
74 if [ "$lsv" -ge 254 ]; then
81 ##removes the network interface config from Vagrantfile
83 ##assumes you are in the directory of Vagrantfile
84 function remove_vagrant_network {
85 sed -i 's/^.*'"$1"'.*$//' Vagrantfile
88 ##check if IP is in use
90 ##ping ip to get arp entry, then check arp
92 ping -c 5 $1 > /dev/null 2>&1
93 arp -n | grep "$1 " | grep -iv incomplete > /dev/null 2>&1
98 function next_usable_ip {
100 while [ "$new_ip" ]; do
101 if ! is_ip_used $new_ip; then
105 new_ip=$(next_ip $new_ip)
110 ##increment ip by value
111 ##params: ip, amount to increment by
112 ##increment_ip $next_private_ip 10
113 function increment_ip {
114 baseaddr="$(echo $1 | cut -d. -f1-3)"
115 lsv="$(echo $1 | cut -d. -f4)"
118 if [ "$lsv" -ge 254 ]; then
124 ##translates yaml into variables
125 ##params: filename, prefix (ex. "config_")
126 ##usage: parse_yaml opnfv_ksgen_settings.yml "config_"
129 local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
130 sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
131 -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
133 indent = length($1)/2;
135 for (i in vname) {if (i > indent) {delete vname[i]}}
136 if (length($3) > 0) {
137 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
138 printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
145 if [[ ( $1 == "--help") || $1 == "-h" ]]; then
150 echo -e "\n\n${blue}This script is used to deploy Foreman/QuickStack Installer and Provision OPNFV Target System${reset}\n\n"
151 echo "Use -h to display help"
154 while [ "`echo $1 | cut -c1`" = "-" ]
181 if ! yum repolist | grep "epel/"; then
182 if ! rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm; then
183 printf '%s\n' 'deploy.sh: Unable to configure EPEL repo' >&2
187 printf '%s\n' 'deploy.sh: Skipping EPEL repo as it is already configured.'
190 ##install dependencies
191 if ! yum -y install binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-devel dkms; then
192 printf '%s\n' 'deploy.sh: Unable to install depdency packages' >&2
196 ##install VirtualBox repo
197 if cat /etc/*release | grep -i "Fedora release"; then
198 vboxurl=http://download.virtualbox.org/virtualbox/rpm/fedora/\$releasever/\$basearch
200 vboxurl=http://download.virtualbox.org/virtualbox/rpm/el/\$releasever/\$basearch
203 cat > /etc/yum.repos.d/virtualbox.repo << EOM
205 name=Oracle Linux / RHEL / CentOS-\$releasever / \$basearch - VirtualBox
209 gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
210 skip_if_unavailable = 1
215 if ! yum list installed | grep -i virtualbox; then
216 if ! yum -y install VirtualBox-4.3; then
217 printf '%s\n' 'deploy.sh: Unable to install virtualbox package' >&2
222 ##install kmod-VirtualBox
223 if ! lsmod | grep vboxdrv; then
224 if ! sudo /etc/init.d/vboxdrv setup; then
225 printf '%s\n' 'deploy.sh: Unable to install kernel module for virtualbox' >&2
229 printf '%s\n' 'deploy.sh: Skipping kernel module for virtualbox. Already Installed'
233 if ! yum list installed | grep -i ansible; then
234 if ! yum -y install ansible; then
235 printf '%s\n' 'deploy.sh: Unable to install Ansible package' >&2
241 if ! rpm -qa | grep vagrant; then
242 if ! rpm -Uvh https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.rpm; then
243 printf '%s\n' 'deploy.sh: Unable to install vagrant package' >&2
247 printf '%s\n' 'deploy.sh: Skipping Vagrant install as it is already installed.'
250 ##add centos 7 box to vagrant
251 if ! vagrant box list | grep chef/centos-7.0; then
252 if ! vagrant box add chef/centos-7.0 --provider virtualbox; then
253 printf '%s\n' 'deploy.sh: Unable to download centos7 box for Vagrant' >&2
257 printf '%s\n' 'deploy.sh: Skipping Vagrant box add as centos-7.0 is already installed.'
260 ##install workaround for centos7
261 if ! vagrant plugin list | grep vagrant-centos7_fix; then
262 if ! vagrant plugin install vagrant-centos7_fix; then
263 printf '%s\n' 'deploy.sh: Warning: unable to install vagrant centos7 workaround' >&2
266 printf '%s\n' 'deploy.sh: Skipping Vagrant plugin as centos7 workaround is already installed.'
271 ##remove bgs vagrant incase it wasn't cleaned up
272 rm -rf /tmp/bgs_vagrant
275 ##will change this to be opnfv repo when commit is done
276 if ! git clone https://github.com/trozet/bgs_vagrant.git; then
277 printf '%s\n' 'deploy.sh: Unable to clone vagrant repo' >&2
283 echo "${blue}Detecting network configuration...${reset}"
284 ##detect host 1 or 3 interface configuration
285 #output=`ip link show | grep -E "^[0-9]" | grep -Ev ": lo|tun|virbr|vboxnet" | awk '{print $2}' | sed 's/://'`
286 output=`ifconfig | grep -E "^[a-zA-Z0-9]+:"| grep -Ev "lo|tun|virbr|vboxnet" | awk '{print $1}' | sed 's/://'`
288 if [ ! "$output" ]; then
289 printf '%s\n' 'deploy.sh: Unable to detect interfaces to bridge to' >&2
293 ##find number of interfaces with ip and substitute in VagrantFile
295 for interface in ${output}; do
297 if [ "$if_counter" -ge 4 ]; then
300 interface_ip=$(find_ip $interface)
301 if [ ! "$interface_ip" ]; then
304 new_ip=$(next_usable_ip $interface_ip)
305 if [ ! "$new_ip" ]; then
308 interface_arr[$interface]=$if_counter
309 interface_ip_arr[$if_counter]=$new_ip
310 subnet_mask=$(find_netmask $interface)
311 if [ "$if_counter" -eq 1 ]; then
312 private_subnet_mask=$subnet_mask
313 private_short_subnet_mask=$(find_short_netmask $interface)
315 if [ "$if_counter" -eq 3 ]; then
316 storage_subnet_mask=$subnet_mask
318 sed -i 's/^.*eth_replace'"$if_counter"'.*$/ config.vm.network "public_network", ip: '\""$new_ip"\"', bridge: '\'"$interface"\'', netmask: '\""$subnet_mask"\"'/' Vagrantfile
322 ##now remove interface config in Vagrantfile for 1 node
323 ##if 1, 3, or 4 interfaces set deployment type
324 ##if 2 interfaces remove 2nd interface and set deployment type
325 if [ "$if_counter" == 1 ]; then
326 deployment_type="single_network"
327 remove_vagrant_network eth_replace1
328 remove_vagrant_network eth_replace2
329 remove_vagrant_network eth_replace3
330 elif [ "$if_counter" == 2 ]; then
331 deployment_type="single_network"
332 second_interface=`echo $output | awk '{print $2}'`
333 remove_vagrant_network $second_interface
334 remove_vagrant_network eth_replace2
335 elif [ "$if_counter" == 3 ]; then
336 deployment_type="three_network"
337 remove_vagrant_network eth_replace3
339 deployment_type="multi_network"
342 echo "${blue}Network detected: ${deployment_type}! ${reset}"
344 if route | grep default; then
345 echo "${blue}Default Gateway Detected ${reset}"
346 host_default_gw=$(ip route | grep default | awk '{print $3}')
347 echo "${blue}Default Gateway: $host_default_gw ${reset}"
348 default_gw_interface=$(ip route get $host_default_gw | awk '{print $3}')
349 case "${interface_arr[$default_gw_interface]}" in
351 echo "${blue}Default Gateway Detected on Admin Interface!${reset}"
352 sed -i 's/^.*default_gw =.*$/ default_gw = '\""$host_default_gw"\"'/' Vagrantfile
353 node_default_gw=$host_default_gw
356 echo "${red}Default Gateway Detected on Private Interface!${reset}"
357 echo "${red}Private subnet should be private and not have Internet access!${reset}"
361 echo "${blue}Default Gateway Detected on Public Interface!${reset}"
362 sed -i 's/^.*default_gw =.*$/ default_gw = '\""$host_default_gw"\"'/' Vagrantfile
363 echo "${blue}Will setup NAT from Admin -> Public Network on VM!${reset}"
364 sed -i 's/^.*nat_flag =.*$/ nat_flag = true/' Vagrantfile
365 echo "${blue}Setting node gateway to be VM Admin IP${reset}"
366 node_default_gw=${interface_ip_arr[0]}
369 echo "${red}Default Gateway Detected on Storage Interface!${reset}"
370 echo "${red}Storage subnet should be private and not have Internet access!${reset}"
374 echo "${red}Unable to determine which interface default gateway is on..Exiting!${reset}"
380 defaultgw=`echo ${interface_ip_arr[0]} | cut -d. -f1-3`
382 defaultgw=$defaultgw$firstip
383 echo "${blue}Unable to find default gateway. Assuming it is $defaultgw ${reset}"
384 sed -i 's/^.*default_gw =.*$/ default_gw = '\""$defaultgw"\"'/' Vagrantfile
385 node_default_gw=$defaultgw
388 if [ $base_config ]; then
389 if ! cp -f $base_config opnfv_ksgen_settings.yml; then
390 echo "{red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
395 if [ $no_parse ]; then
396 echo "${blue}Skipping parsing variables into settings file as no_parse flag is set${reset}"
400 echo "${blue}Gathering network parameters for Target System...this may take a few minutes${reset}"
401 ##Edit the ksgen settings appropriately
402 ##ksgen settings will be stored in /vagrant on the vagrant machine
403 ##if single node deployment all the variables will have the same ip
404 ##interface names will be enp0s3, enp0s8, enp0s9 in chef/centos7
406 sed -i 's/^.*default_gw:.*$/default_gw:'" $node_default_gw"'/' opnfv_ksgen_settings.yml
408 ##replace private interface parameter
409 ##private interface will be of hosts, so we need to know the provisioned host interface name
410 ##we add biosdevname=0, net.ifnames=0 to the kickstart to use regular interface naming convention on hosts
411 ##replace IP for parameters with next IP that will be given to controller
412 if [ "$deployment_type" == "single_network" ]; then
413 ##we also need to assign IP addresses to nodes
414 ##for single node, foreman is managing the single network, so we can't reserve them
415 ##not supporting single network anymore for now
416 echo "{blue}Single Network type is unsupported right now. Please check your interface configuration. Exiting. ${reset}"
419 elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_network" ]]; then
421 if [ "$deployment_type" == "three_network" ]; then
422 sed -i 's/^.*network_type:.*$/network_type: three_network/' opnfv_ksgen_settings.yml
425 ##get ip addresses for private network on controllers to make dhcp entries
426 ##required for controllers_ip_array global param
427 next_private_ip=${interface_ip_arr[1]}
429 for node in controller1 controller2 controller3; do
430 next_private_ip=$(next_usable_ip $next_private_ip)
431 if [ ! "$next_private_ip" ]; then
432 printf '%s\n' 'deploy.sh: Unable to find next ip for private network for control nodes' >&2
435 sed -i 's/'"$node$type"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
436 controller_ip_array=$controller_ip_array$next_private_ip,
439 ##replace global param for contollers_ip_array
440 controller_ip_array=${controller_ip_array%?}
441 sed -i 's/^.*controllers_ip_array:.*$/ controllers_ip_array: '"$controller_ip_array"'/' opnfv_ksgen_settings.yml
443 ##now replace all the VIP variables. admin//private can be the same IP
444 ##we have to use IP's here that won't be allocated to hosts at provisioning time
445 ##therefore we increment the ip by 10 to make sure we have a safe buffer
446 next_private_ip=$(increment_ip $next_private_ip 10)
448 grep -E '*private_vip|loadbalancer_vip|db_vip|amqp_vip|*admin_vip' opnfv_ksgen_settings.yml | while read -r line ; do
449 sed -i 's/^.*'"$line"'.*$/ '"$line $next_private_ip"'/' opnfv_ksgen_settings.yml
450 next_private_ip=$(next_usable_ip $next_private_ip)
451 if [ ! "$next_private_ip" ]; then
452 printf '%s\n' 'deploy.sh: Unable to find next ip for private network for vip replacement' >&2
457 ##replace foreman site
458 next_public_ip=${interface_ip_arr[2]}
459 sed -i 's/^.*foreman_url:.*$/ foreman_url:'" https:\/\/$next_public_ip"'\/api\/v2\//' opnfv_ksgen_settings.yml
460 ##replace public vips
461 next_public_ip=$(increment_ip $next_public_ip 10)
462 grep -E '*public_vip' opnfv_ksgen_settings.yml | while read -r line ; do
463 sed -i 's/^.*'"$line"'.*$/ '"$line $next_public_ip"'/' opnfv_ksgen_settings.yml
464 next_public_ip=$(next_usable_ip $next_public_ip)
465 if [ ! "$next_public_ip" ]; then
466 printf '%s\n' 'deploy.sh: Unable to find next ip for public network for vip replcement' >&2
471 ##replace private_network param
472 private_subnet=$(find_subnet $next_private_ip $private_subnet_mask)
473 sed -i 's/^.*private_network:.*$/ private_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
474 ##replace storage_network
475 if [ "$deployment_type" == "three_network" ]; then
476 sed -i 's/^.*storage_network:.*$/ storage_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
478 next_storage_ip=${interface_ip_arr[3]}
479 storage_subnet=$(find_subnet $next_storage_ip $storage_subnet_mask)
480 sed -i 's/^.*storage_network:.*$/ storage_network:'" $storage_subnet"'/' opnfv_ksgen_settings.yml
483 ##replace private_subnet param
484 private_subnet=$private_subnet'\'$private_short_subnet_mask
485 sed -i 's/^.*private_subnet:.*$/ private_subnet:'" $private_subnet"'/' opnfv_ksgen_settings.yml
487 printf '%s\n' 'deploy.sh: Unknown network type: $deployment_type' >&2
491 echo "${blue}Parameters Complete. Settings have been set for Foreman. ${reset}"
495 if [ $virtual ]; then
496 echo "${blue} Virtual flag detected, setting Khaleesi playbook to be opnfv-vm.yml ${reset}"
497 sed -i 's/opnfv.yml/opnfv-vm.yml/' bootstrap.sh
500 echo "${blue}Starting Vagrant! ${reset}"
503 if ! vagrant up; then
504 printf '%s\n' 'deploy.sh: Unable to start vagrant' >&2
507 echo "${blue}Foreman VM is up! ${reset}"
510 if [ $virtual ]; then
513 echo "${blue}Setting VMs up... ${reset}"
514 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'`
515 ##due to ODL Helium bug of OVS connecting to ODL too early, we need controllers to install first
516 ##this is fix kind of assumes more than I would like to, but for now it should be OK as we always have
517 ##3 static controllers
518 compute_nodes=`echo $nodes | tr " " "\n" | grep -v controller | tr "\n" " "`
519 controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
520 nodes=${controller_nodes}${compute_nodes}
522 for node in ${nodes}; do
525 ##remove VM nodes incase it wasn't cleaned up
529 ##will change this to be opnfv repo when commit is done
530 if ! git clone https://github.com/trozet/bgs_vagrant.git $node; then
531 printf '%s\n' 'deploy.sh: Unable to clone vagrant repo' >&2
537 if [ $base_config ]; then
538 if ! cp -f $base_config opnfv_ksgen_settings.yml; then
539 echo "{red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
544 ##parse yaml into variables
545 eval $(parse_yaml opnfv_ksgen_settings.yml "config_")
547 node_type=config_nodes_${node}_type
548 node_type=$(eval echo \$$node_type)
550 ##find number of interfaces with ip and substitute in VagrantFile
551 output=`ifconfig | grep -E "^[a-zA-Z0-9]+:"| grep -Ev "lo|tun|virbr|vboxnet" | awk '{print $1}' | sed 's/://'`
553 if [ ! "$output" ]; then
554 printf '%s\n' 'deploy.sh: Unable to detect interfaces to bridge to' >&2
560 for interface in ${output}; do
562 if [ "$if_counter" -ge 4 ]; then
565 interface_ip=$(find_ip $interface)
566 if [ ! "$interface_ip" ]; then
569 case "${if_counter}" in
571 mac_string=config_nodes_${node}_mac_address
572 mac_addr=$(eval echo \$$mac_string)
573 mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
574 if [ $mac_addr == "" ]; then
575 echo "${red} Unable to find mac_address for $node! ${reset}"
580 if [ "$node_type" == "controller" ]; then
581 mac_string=config_nodes_${node}_private_mac
582 mac_addr=$(eval echo \$$mac_string)
583 if [ $mac_addr == "" ]; then
584 echo "${red} Unable to find private_mac for $node! ${reset}"
588 ##generate random mac
589 mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
591 mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
594 mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
595 mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
598 sed -i 's/^.*eth_replace'"$if_counter"'.*$/ config.vm.network "public_network", bridge: '\'"$interface"\'', :mac => '\""$mac_addr"\"'/' Vagrantfile
602 ##now remove interface config in Vagrantfile for 1 node
603 ##if 1, 3, or 4 interfaces set deployment type
604 ##if 2 interfaces remove 2nd interface and set deployment type
605 if [ "$if_counter" == 1 ]; then
606 deployment_type="single_network"
607 remove_vagrant_network eth_replace1
608 remove_vagrant_network eth_replace2
609 remove_vagrant_network eth_replace3
610 elif [ "$if_counter" == 2 ]; then
611 deployment_type="single_network"
612 second_interface=`echo $output | awk '{print $2}'`
613 remove_vagrant_network $second_interface
614 remove_vagrant_network eth_replace2
615 elif [ "$if_counter" == 3 ]; then
616 deployment_type="three_network"
617 remove_vagrant_network eth_replace3
619 deployment_type="multi_network"
622 ##modify provisioning to do puppet install, config, and foreman check-in
623 ##substitute host_name and dns_server in the provisioning script
624 host_string=config_nodes_${node}_hostname
625 host_name=$(eval echo \$$host_string)
626 sed -i 's/^host_name=REPLACE/host_name='$host_name'/' vm_nodes_provision.sh
627 ##dns server should be the foreman server
628 sed -i 's/^dns_server=REPLACE/dns_server='${interface_ip_arr[0]}'/' vm_nodes_provision.sh
630 ## remove bootstrap and NAT provisioning
631 sed -i '/nat_setup.sh/d' Vagrantfile
632 sed -i 's/bootstrap.sh/vm_nodes_provision.sh/' Vagrantfile
634 ## modify default_gw to be node_default_gw
635 sed -i 's/^.*default_gw =.*$/ default_gw = '\""$node_default_gw"\"'/' Vagrantfile
637 ## modify VM memory to be 4gig
638 sed -i 's/^.*vb.memory =.*$/ vb.memory = 4096/' Vagrantfile
640 echo "${blue}Starting Vagrant Node $node! ${reset}"
643 if ! vagrant up; then
644 echo "${red} Unable to start $node ${reset}"
647 echo "${blue} $node VM is up! ${reset}"
652 echo "${blue} All VMs are UP! ${reset}"