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
31 echo -e "\n\n${blue}This script is used to deploy Foreman/QuickStack Installer and Provision OPNFV Target System${reset}\n\n"
32 echo -e "\n${green}Make sure you have the latest kernel installed before running this script! (yum update kernel +reboot)${reset}\n"
33 echo -e "\nUsage:\n$0 [arguments] \n"
34 echo -e "\n -no_parse : No variable parsing into config. Flag. \n"
35 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"
38 ##find ip of interface
39 ##params: interface name
41 ip addr show $1 | grep -Eo '^\s+inet\s+[\.0-9]+' | awk '{print $2}'
44 ##finds subnet of ip and netmask
46 function find_subnet {
47 IFS=. read -r i1 i2 i3 i4 <<< "$1"
48 IFS=. read -r m1 m2 m3 m4 <<< "$2"
49 printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"
52 ##finds netmask of interface
54 ##returns long format 255.255.x.x
55 function find_netmask {
56 ifconfig $1 | grep -Eo 'netmask\s+[\.0-9]+' | awk '{print $2}'
59 ##finds short netmask of interface
61 ##returns short format, ex: /21
62 function find_short_netmask {
63 echo "/$(ip addr show $1 | grep -Eo '^\s+inet\s+[\/\.0-9]+' | awk '{print $2}' | cut -d / -f2)"
68 ##assumes a /24 subnet
70 baseaddr="$(echo $1 | cut -d. -f1-3)"
71 lsv="$(echo $1 | cut -d. -f4)"
72 if [ "$lsv" -ge 254 ]; then
79 ##removes the network interface config from Vagrantfile
81 ##assumes you are in the directory of Vagrantfile
82 function remove_vagrant_network {
83 sed -i 's/^.*'"$1"'.*$//' Vagrantfile
86 ##check if IP is in use
88 ##ping ip to get arp entry, then check arp
90 ping -c 5 $1 > /dev/null 2>&1
91 arp -n | grep "$1 " | grep -iv incomplete > /dev/null 2>&1
96 function next_usable_ip {
98 while [ "$new_ip" ]; do
99 if ! is_ip_used $new_ip; then
103 new_ip=$(next_ip $new_ip)
108 ##increment ip by value
109 ##params: ip, amount to increment by
110 ##increment_ip $next_private_ip 10
111 function increment_ip {
112 baseaddr="$(echo $1 | cut -d. -f1-3)"
113 lsv="$(echo $1 | cut -d. -f4)"
116 if [ "$lsv" -ge 254 ]; then
124 if [[ ( $1 == "--help") || $1 == "-h" ]]; then
129 echo -e "\n\n${blue}This script is used to deploy Foreman/QuickStack Installer and Provision OPNFV Target System${reset}\n\n"
130 echo "Use -h to display help"
133 while [ "`echo $1 | cut -c1`" = "-" ]
158 if ! yum repolist | grep "epel/"; then
159 if ! rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm; then
160 printf '%s\n' 'deploy.sh: Unable to configure EPEL repo' >&2
164 printf '%s\n' 'deploy.sh: Skipping EPEL repo as it is already configured.'
167 ##install dependencies
168 if ! yum -y install binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-devel dkms; then
169 printf '%s\n' 'deploy.sh: Unable to install depdency packages' >&2
173 ##install VirtualBox repo
174 if cat /etc/*release | grep -i "Fedora release"; then
175 vboxurl=http://download.virtualbox.org/virtualbox/rpm/fedora/\$releasever/\$basearch
177 vboxurl=http://download.virtualbox.org/virtualbox/rpm/el/\$releasever/\$basearch
180 cat > /etc/yum.repos.d/virtualbox.repo << EOM
182 name=Oracle Linux / RHEL / CentOS-\$releasever / \$basearch - VirtualBox
186 gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
187 skip_if_unavailable = 1
192 if ! yum list installed | grep -i virtualbox; then
193 if ! yum -y install VirtualBox-4.3; then
194 printf '%s\n' 'deploy.sh: Unable to install virtualbox package' >&2
199 ##install kmod-VirtualBox
200 if ! lsmod | grep vboxdrv; then
201 if ! sudo /etc/init.d/vboxdrv setup; then
202 printf '%s\n' 'deploy.sh: Unable to install kernel module for virtualbox' >&2
206 printf '%s\n' 'deploy.sh: Skipping kernel module for virtualbox. Already Installed'
210 if ! rpm -qa | grep vagrant; then
211 if ! rpm -Uvh https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.rpm; then
212 printf '%s\n' 'deploy.sh: Unable to install vagrant package' >&2
216 printf '%s\n' 'deploy.sh: Skipping Vagrant install as it is already installed.'
219 ##add centos 7 box to vagrant
220 if ! vagrant box list | grep chef/centos-7.0; then
221 if ! vagrant box add chef/centos-7.0 --provider virtualbox; then
222 printf '%s\n' 'deploy.sh: Unable to download centos7 box for Vagrant' >&2
226 printf '%s\n' 'deploy.sh: Skipping Vagrant box add as centos-7.0 is already installed.'
229 ##install workaround for centos7
230 if ! vagrant plugin list | grep vagrant-centos7_fix; then
231 if ! vagrant plugin install vagrant-centos7_fix; then
232 printf '%s\n' 'deploy.sh: Warning: unable to install vagrant centos7 workaround' >&2
235 printf '%s\n' 'deploy.sh: Skipping Vagrant plugin as centos7 workaround is already installed.'
240 ##remove bgs vagrant incase it wasn't cleaned up
241 rm -rf /tmp/bgs_vagrant
244 ##will change this to be opnfv repo when commit is done
245 if ! git clone https://github.com/trozet/bgs_vagrant.git; then
246 printf '%s\n' 'deploy.sh: Unable to clone vagrant repo' >&2
252 echo "${blue}Detecting network configuration...${reset}"
253 ##detect host 1 or 3 interface configuration
254 #output=`ip link show | grep -E "^[0-9]" | grep -Ev ": lo|tun|virbr|vboxnet" | awk '{print $2}' | sed 's/://'`
255 output=`ifconfig | grep -E "^[a-Z0-9]+:"| grep -Ev "lo|tun|virbr|vboxnet:" | awk '{print $1}' | sed 's/://'`
257 if [ ! "$output" ]; then
258 printf '%s\n' 'deploy.sh: Unable to detect interfaces to bridge to' >&2
262 ##find number of interfaces with ip and substitute in VagrantFile
264 for interface in ${output}; do
266 if [ "$if_counter" -ge 4 ]; then
269 interface_ip=$(find_ip $interface)
270 if [ ! "$interface_ip" ]; then
273 new_ip=$(next_usable_ip $interface_ip)
274 if [ ! "$new_ip" ]; then
277 interface_ip_arr[$if_counter]=$new_ip
278 subnet_mask=$(find_netmask $interface)
279 if [ "$if_counter" -eq 1 ]; then
280 private_subnet_mask=$subnet_mask
281 private_short_subnet_mask=$(find_short_netmask $interface)
283 if [ "$if_counter" -eq 3 ]; then
284 storage_subnet_mask=$subnet_mask
286 sed -i 's/^.*eth_replace'"$if_counter"'.*$/ config.vm.network "public_network", ip: '\""$new_ip"\"', bridge: '\'"$interface"\'', netmask: '\""$subnet_mask"\"'/' Vagrantfile
290 ##now remove interface config in Vagrantfile for 1 node
291 ##if 1, 3, or 4 interfaces set deployment type
292 ##if 2 interfaces remove 2nd interface and set deployment type
293 if [ "$if_counter" == 1 ]; then
294 deployment_type="single_network"
295 remove_vagrant_network eth_replace1
296 remove_vagrant_network eth_replace2
297 remove_vagrant_network eth_replace3
298 elif [ "$if_counter" == 2 ]; then
299 deployment_type="single_network"
300 second_interface=`echo $output | awk '{print $2}'`
301 remove_vagrant_network $second_interface
302 remove_vagrant_network eth_replace2
303 elif [ "$if_counter" == 3 ]; then
304 deployment_type="three_network"
305 remove_vagrant_network eth_replace3
307 deployment_type="multi_network"
310 echo "${blue}Network detected: ${deployment_type}! ${reset}"
312 if route | grep default; then
313 defaultgw=$(route | grep default | awk '{print $2}')
314 echo "${blue}Default gateway detected: $defaultgw ${reset}"
315 sed -i 's/^.*default_gw =.*$/ default_gw = '\""$defaultgw"\"'/' Vagrantfile
317 defaultgw=`echo ${interface_arr_ip[0]} | cut -d. -f1-3`
319 defaultgw=$defaultgw$firstip
320 echo "${blue}Unable to find default gateway. Assuming it is $defaultgw ${reset}"
321 sed -i 's/^.*default_gw =.*$/ default_gw = '\""$defaultgw"\"'/' Vagrantfile
324 if [ $base_config ]; then
325 if ! cp -f $base_config opnfv_ksgen_settings.yml; then
326 echo "{red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
331 if [ $no_parse ]; then
332 echo "${blue}Skipping parsing variables into settings file as no_parse flag is set${reset}"
336 echo "${blue}Gathering network parameters for Target System...this may take a few minutes${reset}"
337 ##Edit the ksgen settings appropriately
338 ##ksgen settings will be stored in /vagrant on the vagrant machine
339 ##if single node deployment all the variables will have the same ip
340 ##interface names will be enp0s3, enp0s8, enp0s9 in chef/centos7
342 sed -i 's/^.*default_gw:.*$/default_gw:'" $defaultgw"'/' opnfv_ksgen_settings.yml
344 ##replace private interface parameter
345 ##private interface will be of hosts, so we need to know the provisioned host interface name
346 ##we add biosdevname=0, net.ifnames=0 to the kickstart to use regular interface naming convention on hosts
347 ##replace IP for parameters with next IP that will be given to controller
348 if [ "$deployment_type" == "single_network" ]; then
349 ##we also need to assign IP addresses to nodes
350 ##for single node, foreman is managing the single network, so we can't reserve them
351 ##not supporting single network anymore for now
352 echo "{blue}Single Network type is unsupported right now. Please check your interface configuration. Exiting. ${reset}"
355 elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_network" ]]; then
357 if [ "$deployment_type" == "three_network" ]; then
358 sed -i 's/^.*network_type:.*$/network_type: three_network/' opnfv_ksgen_settings.yml
361 ##get ip addresses for private network on controllers to make dhcp entries
362 ##required for controllers_ip_array global param
363 next_private_ip=${interface_ip_arr[1]}
365 for node in controller1 controller2 controller3; do
366 next_private_ip=$(next_usable_ip $next_private_ip)
367 if [ ! "$next_private_ip" ]; then
368 printf '%s\n' 'deploy.sh: Unable to find next ip for private network for control nodes' >&2
371 sed -i 's/'"$node$type"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
372 controller_ip_array=$controller_ip_array$next_private_ip,
375 ##replace global param for contollers_ip_array
376 controller_ip_array=${controller_ip_array%?}
377 sed -i 's/^.*controllers_ip_array:.*$/ controllers_ip_array: '"$controller_ip_array"'/' opnfv_ksgen_settings.yml
379 ##now replace all the VIP variables. admin//private can be the same IP
380 ##we have to use IP's here that won't be allocated to hosts at provisioning time
381 ##therefore we increment the ip by 10 to make sure we have a safe buffer
382 next_private_ip=$(increment_ip $next_private_ip 10)
384 grep -E '*private_vip|loadbalancer_vip|db_vip|amqp_vip|*admin_vip' opnfv_ksgen_settings.yml | while read -r line ; do
385 sed -i 's/^.*'"$line"'.*$/ '"$line $next_private_ip"'/' opnfv_ksgen_settings.yml
386 next_private_ip=$(next_usable_ip $next_private_ip)
387 if [ ! "$next_private_ip" ]; then
388 printf '%s\n' 'deploy.sh: Unable to find next ip for private network for vip replacement' >&2
393 ##replace foreman site
394 next_public_ip=${interface_ip_arr[2]}
395 sed -i 's/^.*foreman_url:.*$/ foreman_url:'" https:\/\/$next_public_ip"'\/api\/v2\//' opnfv_ksgen_settings.yml
396 ##replace public vips
397 next_public_ip=$(increment_ip $next_public_ip 10)
398 grep -E '*public_vip' opnfv_ksgen_settings.yml | while read -r line ; do
399 sed -i 's/^.*'"$line"'.*$/ '"$line $next_public_ip"'/' opnfv_ksgen_settings.yml
400 next_public_ip=$(next_usable_ip $next_public_ip)
401 if [ ! "$next_public_ip" ]; then
402 printf '%s\n' 'deploy.sh: Unable to find next ip for public network for vip replcement' >&2
407 ##replace private_network param
408 private_subnet=$(find_subnet $next_private_ip $private_subnet_mask)
409 sed -i 's/^.*private_network:.*$/ private_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
410 ##replace storage_network
411 if [ "$deployment_type" == "three_network" ]; then
412 sed -i 's/^.*storage_network:.*$/ storage_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
414 next_storage_ip=${interface_ip_arr[3]}
415 storage_subnet=$(find_subnet $next_storage_ip $storage_subnet_mask)
416 sed -i 's/^.*storage_network:.*$/ storage_network:'" $storage_subnet"'/' opnfv_ksgen_settings.yml
419 ##replace private_subnet param
420 private_subnet=$private_subnet'\'$private_short_subnet_mask
421 sed -i 's/^.*private_subnet:.*$/ private_subnet:'" $private_subnet"'/' opnfv_ksgen_settings.yml
423 printf '%s\n' 'deploy.sh: Unknown network type: $deployment_type' >&2
427 echo "${blue}Parameters Complete. Settings have been set for Foreman. ${reset}"
431 echo "${blue}Starting Vagrant! ${reset}"
434 if ! vagrant up; then
435 printf '%s\n' 'deploy.sh: Unable to start vagrant' >&2