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"
39 ##find ip of interface
40 ##params: interface name
42 ip addr show $1 | grep -Eo '^\s+inet\s+[\.0-9]+' | awk '{print $2}'
45 ##finds subnet of ip and netmask
47 function find_subnet {
48 IFS=. read -r i1 i2 i3 i4 <<< "$1"
49 IFS=. read -r m1 m2 m3 m4 <<< "$2"
50 printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"
53 ##finds netmask of interface
55 ##returns long format 255.255.x.x
56 function find_netmask {
57 ifconfig $1 | grep -Eo 'netmask\s+[\.0-9]+' | awk '{print $2}'
60 ##finds short netmask of interface
62 ##returns short format, ex: /21
63 function find_short_netmask {
64 echo "/$(ip addr show $1 | grep -Eo '^\s+inet\s+[\/\.0-9]+' | awk '{print $2}' | cut -d / -f2)"
69 ##assumes a /24 subnet
71 baseaddr="$(echo $1 | cut -d. -f1-3)"
72 lsv="$(echo $1 | cut -d. -f4)"
73 if [ "$lsv" -ge 254 ]; then
80 ##removes the network interface config from Vagrantfile
82 ##assumes you are in the directory of Vagrantfile
83 function remove_vagrant_network {
84 sed -i 's/^.*'"$1"'.*$//' Vagrantfile
87 ##check if IP is in use
89 ##ping ip to get arp entry, then check arp
91 ping -c 5 $1 > /dev/null 2>&1
92 arp -n | grep "$1 " | grep -iv incomplete > /dev/null 2>&1
97 function next_usable_ip {
99 while [ "$new_ip" ]; do
100 if ! is_ip_used $new_ip; then
104 new_ip=$(next_ip $new_ip)
109 ##increment ip by value
110 ##params: ip, amount to increment by
111 ##increment_ip $next_private_ip 10
112 function increment_ip {
113 baseaddr="$(echo $1 | cut -d. -f1-3)"
114 lsv="$(echo $1 | cut -d. -f4)"
117 if [ "$lsv" -ge 254 ]; then
125 if [[ ( $1 == "--help") || $1 == "-h" ]]; then
130 echo -e "\n\n${blue}This script is used to deploy Foreman/QuickStack Installer and Provision OPNFV Target System${reset}\n\n"
131 echo "Use -h to display help"
134 while [ "`echo $1 | cut -c1`" = "-" ]
159 if ! yum repolist | grep "epel/"; then
160 if ! rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm; then
161 printf '%s\n' 'deploy.sh: Unable to configure EPEL repo' >&2
165 printf '%s\n' 'deploy.sh: Skipping EPEL repo as it is already configured.'
168 ##install dependencies
169 if ! yum -y install binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-devel dkms; then
170 printf '%s\n' 'deploy.sh: Unable to install depdency packages' >&2
174 ##install VirtualBox repo
175 if cat /etc/*release | grep -i "Fedora release"; then
176 vboxurl=http://download.virtualbox.org/virtualbox/rpm/fedora/\$releasever/\$basearch
178 vboxurl=http://download.virtualbox.org/virtualbox/rpm/el/\$releasever/\$basearch
181 cat > /etc/yum.repos.d/virtualbox.repo << EOM
183 name=Oracle Linux / RHEL / CentOS-\$releasever / \$basearch - VirtualBox
187 gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
188 skip_if_unavailable = 1
193 if ! yum list installed | grep -i virtualbox; then
194 if ! yum -y install VirtualBox-4.3; then
195 printf '%s\n' 'deploy.sh: Unable to install virtualbox package' >&2
200 ##install kmod-VirtualBox
201 if ! lsmod | grep vboxdrv; then
202 if ! sudo /etc/init.d/vboxdrv setup; then
203 printf '%s\n' 'deploy.sh: Unable to install kernel module for virtualbox' >&2
207 printf '%s\n' 'deploy.sh: Skipping kernel module for virtualbox. Already Installed'
211 if ! yum list installed | grep -i ansible; then
212 if ! yum -y install ansible; then
213 printf '%s\n' 'deploy.sh: Unable to install Ansible package' >&2
219 if ! rpm -qa | grep vagrant; then
220 if ! rpm -Uvh https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.rpm; then
221 printf '%s\n' 'deploy.sh: Unable to install vagrant package' >&2
225 printf '%s\n' 'deploy.sh: Skipping Vagrant install as it is already installed.'
228 ##add centos 7 box to vagrant
229 if ! vagrant box list | grep chef/centos-7.0; then
230 if ! vagrant box add chef/centos-7.0 --provider virtualbox; then
231 printf '%s\n' 'deploy.sh: Unable to download centos7 box for Vagrant' >&2
235 printf '%s\n' 'deploy.sh: Skipping Vagrant box add as centos-7.0 is already installed.'
238 ##install workaround for centos7
239 if ! vagrant plugin list | grep vagrant-centos7_fix; then
240 if ! vagrant plugin install vagrant-centos7_fix; then
241 printf '%s\n' 'deploy.sh: Warning: unable to install vagrant centos7 workaround' >&2
244 printf '%s\n' 'deploy.sh: Skipping Vagrant plugin as centos7 workaround is already installed.'
249 ##remove bgs vagrant incase it wasn't cleaned up
250 rm -rf /tmp/bgs_vagrant
253 ##will change this to be opnfv repo when commit is done
254 if ! git clone https://github.com/trozet/bgs_vagrant.git; then
255 printf '%s\n' 'deploy.sh: Unable to clone vagrant repo' >&2
261 echo "${blue}Detecting network configuration...${reset}"
262 ##detect host 1 or 3 interface configuration
263 #output=`ip link show | grep -E "^[0-9]" | grep -Ev ": lo|tun|virbr|vboxnet" | awk '{print $2}' | sed 's/://'`
264 output=`ifconfig | grep -E "^[a-zA-Z0-9]+:"| grep -Ev "lo|tun|virbr|vboxnet" | awk '{print $1}' | sed 's/://'`
266 if [ ! "$output" ]; then
267 printf '%s\n' 'deploy.sh: Unable to detect interfaces to bridge to' >&2
271 ##find number of interfaces with ip and substitute in VagrantFile
273 for interface in ${output}; do
275 if [ "$if_counter" -ge 4 ]; then
278 interface_ip=$(find_ip $interface)
279 if [ ! "$interface_ip" ]; then
282 new_ip=$(next_usable_ip $interface_ip)
283 if [ ! "$new_ip" ]; then
286 interface_arr[$interface]=$if_counter
287 interface_ip_arr[$if_counter]=$new_ip
288 subnet_mask=$(find_netmask $interface)
289 if [ "$if_counter" -eq 1 ]; then
290 private_subnet_mask=$subnet_mask
291 private_short_subnet_mask=$(find_short_netmask $interface)
293 if [ "$if_counter" -eq 3 ]; then
294 storage_subnet_mask=$subnet_mask
296 sed -i 's/^.*eth_replace'"$if_counter"'.*$/ config.vm.network "public_network", ip: '\""$new_ip"\"', bridge: '\'"$interface"\'', netmask: '\""$subnet_mask"\"'/' Vagrantfile
300 ##now remove interface config in Vagrantfile for 1 node
301 ##if 1, 3, or 4 interfaces set deployment type
302 ##if 2 interfaces remove 2nd interface and set deployment type
303 if [ "$if_counter" == 1 ]; then
304 deployment_type="single_network"
305 remove_vagrant_network eth_replace1
306 remove_vagrant_network eth_replace2
307 remove_vagrant_network eth_replace3
308 elif [ "$if_counter" == 2 ]; then
309 deployment_type="single_network"
310 second_interface=`echo $output | awk '{print $2}'`
311 remove_vagrant_network $second_interface
312 remove_vagrant_network eth_replace2
313 elif [ "$if_counter" == 3 ]; then
314 deployment_type="three_network"
315 remove_vagrant_network eth_replace3
317 deployment_type="multi_network"
320 echo "${blue}Network detected: ${deployment_type}! ${reset}"
322 if route | grep default; then
323 echo "${blue}Default Gateway Detected ${reset}"
324 host_default_gw=$(ip route | grep default | awk '{print $3}')
325 echo "${blue}Default Gateway: $host_default_gw ${reset}"
326 default_gw_interface=$(ip route get $host_default_gw | awk '{print $3}')
327 case "${interface_arr[$default_gw_interface]}" in
329 echo "${blue}Default Gateway Detected on Admin Interface!${reset}"
330 sed -i 's/^.*default_gw =.*$/ default_gw = '\""$host_default_gw"\"'/' Vagrantfile
331 node_default_gw=$host_default_gw
334 echo "${red}Default Gateway Detected on Private Interface!${reset}"
335 echo "${red}Private subnet should be private and not have Internet access!${reset}"
339 echo "${blue}Default Gateway Detected on Public Interface!${reset}"
340 sed -i 's/^.*default_gw =.*$/ default_gw = '\""$host_default_gw"\"'/' Vagrantfile
341 echo "${blue}Will setup NAT from Admin -> Public Network on VM!${reset}"
342 sed -i 's/^.*nat_flag =.*$/ nat_flag = true/' Vagrantfile
343 echo "${blue}Setting node gateway to be VM Admin IP${reset}"
344 node_default_gw=${interface_ip_arr[0]}
347 echo "${red}Default Gateway Detected on Storage Interface!${reset}"
348 echo "${red}Storage subnet should be private and not have Internet access!${reset}"
352 echo "${red}Unable to determine which interface default gateway is on..Exiting!${reset}"
358 defaultgw=`echo ${interface_ip_arr[0]} | cut -d. -f1-3`
360 defaultgw=$defaultgw$firstip
361 echo "${blue}Unable to find default gateway. Assuming it is $defaultgw ${reset}"
362 sed -i 's/^.*default_gw =.*$/ default_gw = '\""$defaultgw"\"'/' Vagrantfile
363 node_default_gw=$defaultgw
366 if [ $base_config ]; then
367 if ! cp -f $base_config opnfv_ksgen_settings.yml; then
368 echo "{red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
373 if [ $no_parse ]; then
374 echo "${blue}Skipping parsing variables into settings file as no_parse flag is set${reset}"
378 echo "${blue}Gathering network parameters for Target System...this may take a few minutes${reset}"
379 ##Edit the ksgen settings appropriately
380 ##ksgen settings will be stored in /vagrant on the vagrant machine
381 ##if single node deployment all the variables will have the same ip
382 ##interface names will be enp0s3, enp0s8, enp0s9 in chef/centos7
384 sed -i 's/^.*default_gw:.*$/default_gw:'" $node_default_gw"'/' opnfv_ksgen_settings.yml
386 ##replace private interface parameter
387 ##private interface will be of hosts, so we need to know the provisioned host interface name
388 ##we add biosdevname=0, net.ifnames=0 to the kickstart to use regular interface naming convention on hosts
389 ##replace IP for parameters with next IP that will be given to controller
390 if [ "$deployment_type" == "single_network" ]; then
391 ##we also need to assign IP addresses to nodes
392 ##for single node, foreman is managing the single network, so we can't reserve them
393 ##not supporting single network anymore for now
394 echo "{blue}Single Network type is unsupported right now. Please check your interface configuration. Exiting. ${reset}"
397 elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_network" ]]; then
399 if [ "$deployment_type" == "three_network" ]; then
400 sed -i 's/^.*network_type:.*$/network_type: three_network/' opnfv_ksgen_settings.yml
403 ##get ip addresses for private network on controllers to make dhcp entries
404 ##required for controllers_ip_array global param
405 next_private_ip=${interface_ip_arr[1]}
407 for node in controller1 controller2 controller3; do
408 next_private_ip=$(next_usable_ip $next_private_ip)
409 if [ ! "$next_private_ip" ]; then
410 printf '%s\n' 'deploy.sh: Unable to find next ip for private network for control nodes' >&2
413 sed -i 's/'"$node$type"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
414 controller_ip_array=$controller_ip_array$next_private_ip,
417 ##replace global param for contollers_ip_array
418 controller_ip_array=${controller_ip_array%?}
419 sed -i 's/^.*controllers_ip_array:.*$/ controllers_ip_array: '"$controller_ip_array"'/' opnfv_ksgen_settings.yml
421 ##now replace all the VIP variables. admin//private can be the same IP
422 ##we have to use IP's here that won't be allocated to hosts at provisioning time
423 ##therefore we increment the ip by 10 to make sure we have a safe buffer
424 next_private_ip=$(increment_ip $next_private_ip 10)
426 grep -E '*private_vip|loadbalancer_vip|db_vip|amqp_vip|*admin_vip' opnfv_ksgen_settings.yml | while read -r line ; do
427 sed -i 's/^.*'"$line"'.*$/ '"$line $next_private_ip"'/' opnfv_ksgen_settings.yml
428 next_private_ip=$(next_usable_ip $next_private_ip)
429 if [ ! "$next_private_ip" ]; then
430 printf '%s\n' 'deploy.sh: Unable to find next ip for private network for vip replacement' >&2
435 ##replace foreman site
436 next_public_ip=${interface_ip_arr[2]}
437 sed -i 's/^.*foreman_url:.*$/ foreman_url:'" https:\/\/$next_public_ip"'\/api\/v2\//' opnfv_ksgen_settings.yml
438 ##replace public vips
439 next_public_ip=$(increment_ip $next_public_ip 10)
440 grep -E '*public_vip' opnfv_ksgen_settings.yml | while read -r line ; do
441 sed -i 's/^.*'"$line"'.*$/ '"$line $next_public_ip"'/' opnfv_ksgen_settings.yml
442 next_public_ip=$(next_usable_ip $next_public_ip)
443 if [ ! "$next_public_ip" ]; then
444 printf '%s\n' 'deploy.sh: Unable to find next ip for public network for vip replcement' >&2
449 ##replace private_network param
450 private_subnet=$(find_subnet $next_private_ip $private_subnet_mask)
451 sed -i 's/^.*private_network:.*$/ private_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
452 ##replace storage_network
453 if [ "$deployment_type" == "three_network" ]; then
454 sed -i 's/^.*storage_network:.*$/ storage_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
456 next_storage_ip=${interface_ip_arr[3]}
457 storage_subnet=$(find_subnet $next_storage_ip $storage_subnet_mask)
458 sed -i 's/^.*storage_network:.*$/ storage_network:'" $storage_subnet"'/' opnfv_ksgen_settings.yml
461 ##replace private_subnet param
462 private_subnet=$private_subnet'\'$private_short_subnet_mask
463 sed -i 's/^.*private_subnet:.*$/ private_subnet:'" $private_subnet"'/' opnfv_ksgen_settings.yml
465 printf '%s\n' 'deploy.sh: Unknown network type: $deployment_type' >&2
469 echo "${blue}Parameters Complete. Settings have been set for Foreman. ${reset}"
473 echo "${blue}Starting Vagrant! ${reset}"
476 if ! vagrant up; then
477 printf '%s\n' 'deploy.sh: Unable to start vagrant' >&2