49e1590db03984a64d08ec749a6762a98683be3f
[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 ##END VARS
28
29 ##FUNCTIONS
30 display_usage() {
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"
36 }
37
38 ##find ip of interface
39 ##params: interface name
40 function find_ip {
41   ip addr show $1 | grep -Eo '^\s+inet\s+[\.0-9]+' | awk '{print $2}'
42 }
43
44 ##finds subnet of ip and netmask
45 ##params: ip, 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))"
50 }
51
52 ##finds netmask of interface
53 ##params: 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}'
57 }
58
59 ##finds short netmask of interface
60 ##params: 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)"
64 }
65
66 ##increments next IP
67 ##params: ip
68 ##assumes a /24 subnet
69 function next_ip {
70   baseaddr="$(echo $1 | cut -d. -f1-3)"
71   lsv="$(echo $1 | cut -d. -f4)"
72   if [ "$lsv" -ge 254 ]; then
73     return 1
74   fi
75   ((lsv++))
76   echo $baseaddr.$lsv
77 }
78
79 ##removes the network interface config from Vagrantfile
80 ##params: interface
81 ##assumes you are in the directory of Vagrantfile
82 function remove_vagrant_network {
83   sed -i 's/^.*'"$1"'.*$//' Vagrantfile
84 }
85
86 ##check if IP is in use
87 ##params: ip
88 ##ping ip to get arp entry, then check arp
89 function is_ip_used {
90   ping -c 5 $1 > /dev/null 2>&1
91   arp -n | grep "$1 " | grep -iv incomplete > /dev/null 2>&1
92 }
93
94 ##find next usable IP
95 ##params: ip
96 function next_usable_ip {
97   new_ip=$(next_ip $1)
98   while [ "$new_ip" ]; do
99     if ! is_ip_used $new_ip; then
100       echo $new_ip
101       return 0
102     fi
103     new_ip=$(next_ip $new_ip)
104   done
105   return 1
106 }
107
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)"
114   incrval=$2
115   lsv=$((lsv+incrval))
116   if [ "$lsv" -ge 254 ]; then
117     return 1
118   fi
119   echo $baseaddr.$lsv
120 }
121
122 ##END FUNCTIONS
123
124 if [[ ( $1 == "--help") ||  $1 == "-h" ]]; then
125     display_usage
126     exit 0
127 fi
128
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"
131 sleep 2
132
133 while [ "`echo $1 | cut -c1`" = "-" ]
134 do
135     echo $1
136     case "$1" in
137         -base_config)
138                 base_config=$2
139                 shift 2
140             ;;
141         -no_parse)
142                 no_parse="TRUE"
143                 shift 1
144             ;;
145         *)
146                 display_usage
147                 exit 1
148             ;;
149 esac
150 done
151
152
153
154 ##disable selinux
155 /sbin/setenforce 0
156
157 ##install EPEL
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
161     exit 1
162   fi
163 else
164   printf '%s\n' 'deploy.sh: Skipping EPEL repo as it is already configured.'
165 fi
166
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
170   exit 1
171 fi
172
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
176 else
177   vboxurl=http://download.virtualbox.org/virtualbox/rpm/el/\$releasever/\$basearch
178 fi
179
180 cat > /etc/yum.repos.d/virtualbox.repo << EOM
181 [virtualbox]
182 name=Oracle Linux / RHEL / CentOS-\$releasever / \$basearch - VirtualBox
183 baseurl=$vboxurl
184 enabled=1
185 gpgcheck=1
186 gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
187 skip_if_unavailable = 1
188 keepcache = 0
189 EOM
190
191 ##install VirtualBox
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
195     exit 1
196   fi
197 fi
198
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
203     exit 1
204   fi
205 else
206   printf '%s\n' 'deploy.sh: Skipping kernel module for virtualbox.  Already Installed'
207 fi
208
209 ##install Vagrant
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
213     exit 1
214   fi
215 else
216   printf '%s\n' 'deploy.sh: Skipping Vagrant install as it is already installed.'
217 fi
218
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
223     exit 1
224   fi
225 else
226   printf '%s\n' 'deploy.sh: Skipping Vagrant box add as centos-7.0 is already installed.'
227 fi
228
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
233   fi
234 else
235   printf '%s\n' 'deploy.sh: Skipping Vagrant plugin as centos7 workaround is already installed.'
236 fi
237
238 cd /tmp/
239
240 ##remove bgs vagrant incase it wasn't cleaned up
241 rm -rf /tmp/bgs_vagrant
242
243 ##clone 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
247   exit 1
248 fi
249
250 cd bgs_vagrant
251
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/://'`
256
257 if [ ! "$output" ]; then
258   printf '%s\n' 'deploy.sh: Unable to detect interfaces to bridge to' >&2
259   exit 1
260 fi
261
262 ##find number of interfaces with ip and substitute in VagrantFile
263 if_counter=0
264 for interface in ${output}; do
265
266   if [ "$if_counter" -ge 4 ]; then
267     break
268   fi
269   interface_ip=$(find_ip $interface)
270   if [ ! "$interface_ip" ]; then
271     continue
272   fi
273   new_ip=$(next_usable_ip $interface_ip)
274   if [ ! "$new_ip" ]; then
275     continue
276   fi
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)
282   fi
283   if [ "$if_counter" -eq 3 ]; then
284     storage_subnet_mask=$subnet_mask
285   fi
286   sed -i 's/^.*eth_replace'"$if_counter"'.*$/  config.vm.network "public_network", ip: '\""$new_ip"\"', bridge: '\'"$interface"\'', netmask: '\""$subnet_mask"\"'/' Vagrantfile
287   ((if_counter++))
288 done
289
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
306 else
307   deployment_type="multi_network"
308 fi
309
310 echo "${blue}Network detected: ${deployment_type}! ${reset}"
311
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
316 else
317   defaultgw=`echo ${interface_arr_ip[0]} | cut -d. -f1-3`
318   firstip=.1
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
322 fi
323
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}"
327     exit 1
328   fi
329 fi
330
331 if [ $no_parse ]; then
332 echo "${blue}Skipping parsing variables into settings file as no_parse flag is set${reset}"
333
334 else
335
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
341
342 sed -i 's/^.*default_gw:.*$/default_gw:'" $defaultgw"'/' opnfv_ksgen_settings.yml
343
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}"
353   exit 0
354
355 elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_network" ]]; then
356
357   if [ "$deployment_type" == "three_network" ]; then
358     sed -i 's/^.*network_type:.*$/network_type: three_network/' opnfv_ksgen_settings.yml
359   fi
360
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]}
364   type=_private
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
369        exit 1
370     fi
371     sed -i 's/'"$node$type"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
372     controller_ip_array=$controller_ip_array$next_private_ip,
373   done
374
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
378
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)
383
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
389        exit 1
390     fi
391   done
392
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
403        exit 1
404     fi
405   done
406
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
413   else
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
417   fi
418
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
422 else
423   printf '%s\n' 'deploy.sh: Unknown network type: $deployment_type' >&2
424   exit 1
425 fi
426
427 echo "${blue}Parameters Complete.  Settings have been set for Foreman. ${reset}"
428
429 fi
430
431 echo "${blue}Starting Vagrant! ${reset}"
432
433 ##stand up vagrant
434 if ! vagrant up; then
435   printf '%s\n' 'deploy.sh: Unable to start vagrant' >&2
436   exit 1
437 fi
438
439