ae585b0cdc4cdbecee55e827afb2048160b14578
[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 }
38
39 ##find ip of interface
40 ##params: interface name
41 function find_ip {
42   ip addr show $1 | grep -Eo '^\s+inet\s+[\.0-9]+' | awk '{print $2}'
43 }
44
45 ##finds subnet of ip and netmask
46 ##params: ip, 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))"
51 }
52
53 ##finds netmask of interface
54 ##params: 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}'
58 }
59
60 ##finds short netmask of interface
61 ##params: 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)"
65 }
66
67 ##increments next IP
68 ##params: ip
69 ##assumes a /24 subnet
70 function next_ip {
71   baseaddr="$(echo $1 | cut -d. -f1-3)"
72   lsv="$(echo $1 | cut -d. -f4)"
73   if [ "$lsv" -ge 254 ]; then
74     return 1
75   fi
76   ((lsv++))
77   echo $baseaddr.$lsv
78 }
79
80 ##removes the network interface config from Vagrantfile
81 ##params: interface
82 ##assumes you are in the directory of Vagrantfile
83 function remove_vagrant_network {
84   sed -i 's/^.*'"$1"'.*$//' Vagrantfile
85 }
86
87 ##check if IP is in use
88 ##params: ip
89 ##ping ip to get arp entry, then check arp
90 function is_ip_used {
91   ping -c 5 $1 > /dev/null 2>&1
92   arp -n | grep "$1 " | grep -iv incomplete > /dev/null 2>&1
93 }
94
95 ##find next usable IP
96 ##params: ip
97 function next_usable_ip {
98   new_ip=$(next_ip $1)
99   while [ "$new_ip" ]; do
100     if ! is_ip_used $new_ip; then
101       echo $new_ip
102       return 0
103     fi
104     new_ip=$(next_ip $new_ip)
105   done
106   return 1
107 }
108
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)"
115   incrval=$2
116   lsv=$((lsv+incrval))
117   if [ "$lsv" -ge 254 ]; then
118     return 1
119   fi
120   echo $baseaddr.$lsv
121 }
122
123 ##END FUNCTIONS
124
125 if [[ ( $1 == "--help") ||  $1 == "-h" ]]; then
126     display_usage
127     exit 0
128 fi
129
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"
132 sleep 2
133
134 while [ "`echo $1 | cut -c1`" = "-" ]
135 do
136     echo $1
137     case "$1" in
138         -base_config)
139                 base_config=$2
140                 shift 2
141             ;;
142         -no_parse)
143                 no_parse="TRUE"
144                 shift 1
145             ;;
146         *)
147                 display_usage
148                 exit 1
149             ;;
150 esac
151 done
152
153
154
155 ##disable selinux
156 /sbin/setenforce 0
157
158 ##install EPEL
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
162     exit 1
163   fi
164 else
165   printf '%s\n' 'deploy.sh: Skipping EPEL repo as it is already configured.'
166 fi
167
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
171   exit 1
172 fi
173
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
177 else
178   vboxurl=http://download.virtualbox.org/virtualbox/rpm/el/\$releasever/\$basearch
179 fi
180
181 cat > /etc/yum.repos.d/virtualbox.repo << EOM
182 [virtualbox]
183 name=Oracle Linux / RHEL / CentOS-\$releasever / \$basearch - VirtualBox
184 baseurl=$vboxurl
185 enabled=1
186 gpgcheck=1
187 gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
188 skip_if_unavailable = 1
189 keepcache = 0
190 EOM
191
192 ##install VirtualBox
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
196     exit 1
197   fi
198 fi
199
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
204     exit 1
205   fi
206 else
207   printf '%s\n' 'deploy.sh: Skipping kernel module for virtualbox.  Already Installed'
208 fi
209
210 ##install Ansible
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
214     exit 1
215   fi
216 fi
217
218 ##install Vagrant
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
222     exit 1
223   fi
224 else
225   printf '%s\n' 'deploy.sh: Skipping Vagrant install as it is already installed.'
226 fi
227
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
232     exit 1
233   fi
234 else
235   printf '%s\n' 'deploy.sh: Skipping Vagrant box add as centos-7.0 is already installed.'
236 fi
237
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
242   fi
243 else
244   printf '%s\n' 'deploy.sh: Skipping Vagrant plugin as centos7 workaround is already installed.'
245 fi
246
247 cd /tmp/
248
249 ##remove bgs vagrant incase it wasn't cleaned up
250 rm -rf /tmp/bgs_vagrant
251
252 ##clone 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
256   exit 1
257 fi
258
259 cd bgs_vagrant
260
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/://'`
265
266 if [ ! "$output" ]; then
267   printf '%s\n' 'deploy.sh: Unable to detect interfaces to bridge to' >&2
268   exit 1
269 fi
270
271 ##find number of interfaces with ip and substitute in VagrantFile
272 if_counter=0
273 for interface in ${output}; do
274
275   if [ "$if_counter" -ge 4 ]; then
276     break
277   fi
278   interface_ip=$(find_ip $interface)
279   if [ ! "$interface_ip" ]; then
280     continue
281   fi
282   new_ip=$(next_usable_ip $interface_ip)
283   if [ ! "$new_ip" ]; then
284     continue
285   fi
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)
292   fi
293   if [ "$if_counter" -eq 3 ]; then
294     storage_subnet_mask=$subnet_mask
295   fi
296   sed -i 's/^.*eth_replace'"$if_counter"'.*$/  config.vm.network "public_network", ip: '\""$new_ip"\"', bridge: '\'"$interface"\'', netmask: '\""$subnet_mask"\"'/' Vagrantfile
297   ((if_counter++))
298 done
299
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
316 else
317   deployment_type="multi_network"
318 fi
319
320 echo "${blue}Network detected: ${deployment_type}! ${reset}"
321
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
328            0)
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
332              ;;
333            1)
334              echo "${red}Default Gateway Detected on Private Interface!${reset}"
335              echo "${red}Private subnet should be private and not have Internet access!${reset}"
336              exit 1
337              ;;
338            2)
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]}
345              ;;
346            3)
347              echo "${red}Default Gateway Detected on Storage Interface!${reset}"
348              echo "${red}Storage subnet should be private and not have Internet access!${reset}"
349              exit 1
350              ;;
351            *)
352              echo "${red}Unable to determine which interface default gateway is on..Exiting!${reset}"
353              exit 1
354              ;;
355   esac
356 else
357   #assumes 24 bit mask
358   defaultgw=`echo ${interface_ip_arr[0]} | cut -d. -f1-3`
359   firstip=.1
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
364 fi
365
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}"
369     exit 1
370   fi
371 fi
372
373 if [ $no_parse ]; then
374 echo "${blue}Skipping parsing variables into settings file as no_parse flag is set${reset}"
375
376 else
377
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
383
384 sed -i 's/^.*default_gw:.*$/default_gw:'" $node_default_gw"'/' opnfv_ksgen_settings.yml
385
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}"
395   exit 0
396
397 elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_network" ]]; then
398
399   if [ "$deployment_type" == "three_network" ]; then
400     sed -i 's/^.*network_type:.*$/network_type: three_network/' opnfv_ksgen_settings.yml
401   fi
402
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]}
406   type=_private
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
411        exit 1
412     fi
413     sed -i 's/'"$node$type"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
414     controller_ip_array=$controller_ip_array$next_private_ip,
415   done
416
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
420
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)
425
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
431        exit 1
432     fi
433   done
434
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
445        exit 1
446     fi
447   done
448
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
455   else
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
459   fi
460
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
464 else
465   printf '%s\n' 'deploy.sh: Unknown network type: $deployment_type' >&2
466   exit 1
467 fi
468
469 echo "${blue}Parameters Complete.  Settings have been set for Foreman. ${reset}"
470
471 fi
472
473 echo "${blue}Starting Vagrant! ${reset}"
474
475 ##stand up vagrant
476 if ! vagrant up; then
477   printf '%s\n' 'deploy.sh: Unable to start vagrant' >&2
478   exit 1
479 fi
480
481