d1bb24c1489d267124331e5b41d0032e5b080c50
[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 ##increments next IP
45 ##params: ip
46 ##assumes a /24 subnet
47 function next_ip {
48   baseaddr="$(echo $1 | cut -d. -f1-3)"
49   lsv="$(echo $1 | cut -d. -f4)"
50   if [ "$lsv" -ge 254 ]; then
51     return 1
52   fi
53   ((lsv++))
54   echo $baseaddr.$lsv
55 }
56
57 ##removes the network interface config from Vagrantfile
58 ##params: interface
59 ##assumes you are in the directory of Vagrantfile
60 function remove_vagrant_network {
61   sed -i 's/^.*'"$1"'.*$//' Vagrantfile
62 }
63
64 ##check if IP is in use
65 ##params: ip
66 ##ping ip to get arp entry, then check arp
67 function is_ip_used {
68   ping -c 5 $1 > /dev/null 2>&1
69   arp -n | grep "$1 " | grep -iv incomplete > /dev/null 2>&1
70 }
71
72 ##find next usable IP
73 ##params: ip
74 function next_usable_ip {
75   new_ip=$(next_ip $1)
76   while [ "$new_ip" ]; do
77     if ! is_ip_used $new_ip; then
78       echo $new_ip
79       return 0
80     fi
81     new_ip=$(next_ip $new_ip)
82   done
83   return 1
84 }
85
86 ##increment ip by value
87 ##params: ip, amount to increment by
88 ##increment_ip $next_private_ip 10
89 function increment_ip {
90   baseaddr="$(echo $1 | cut -d. -f1-3)"
91   lsv="$(echo $1 | cut -d. -f4)"
92   incrval=$2
93   lsv=$((lsv+incrval))
94   if [ "$lsv" -ge 254 ]; then
95     return 1
96   fi
97   echo $baseaddr.$lsv
98 }
99
100 ##END FUNCTIONS
101
102 if [[ ( $1 == "--help") ||  $1 == "-h" ]]; then
103     display_usage
104     exit 0
105 fi
106
107 echo -e "\n\n${blue}This script is used to deploy Foreman/QuickStack Installer and Provision OPNFV Target System${reset}\n\n"
108 echo "Use -h to display help"
109 sleep 2
110
111 while [ "`echo $1 | cut -c1`" = "-" ]
112 do
113     echo $1
114     case "$1" in
115         -base_config)
116                 base_config=$2
117                 shift 2
118             ;;
119         -no_parse)
120                 no_parse="TRUE"
121                 shift 1
122             ;;
123         *)
124                 display_usage
125                 exit 1
126             ;;
127 esac
128 done
129
130
131
132 ##disable selinux
133 /sbin/setenforce 0
134
135 ##install EPEL
136 if ! yum repolist | grep "epel/"; then
137   if ! rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm; then
138     printf '%s\n' 'deploy.sh: Unable to configure EPEL repo' >&2
139     exit 1
140   fi
141 else
142   printf '%s\n' 'deploy.sh: Skipping EPEL repo as it is already configured.'
143 fi
144
145 ##install dependencies
146 if ! yum -y install binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-devel dkms; then
147   printf '%s\n' 'deploy.sh: Unable to install depdency packages' >&2
148   exit 1
149 fi
150
151 ##install VirtualBox repo
152 if cat /etc/*release | grep -i "Fedora release"; then
153   vboxurl=http://download.virtualbox.org/virtualbox/rpm/fedora/\$releasever/\$basearch
154 else
155   vboxurl=http://download.virtualbox.org/virtualbox/rpm/el/\$releasever/\$basearch
156 fi
157
158 cat > /etc/yum.repos.d/virtualbox.repo << EOM
159 [virtualbox]
160 name=Oracle Linux / RHEL / CentOS-\$releasever / \$basearch - VirtualBox
161 baseurl=$vboxurl
162 enabled=1
163 gpgcheck=1
164 gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
165 skip_if_unavailable = 1
166 keepcache = 0
167 EOM
168
169 ##install VirtualBox
170 if ! yum list installed | grep -i virtualbox; then
171   if ! yum -y install VirtualBox-4.3; then
172     printf '%s\n' 'deploy.sh: Unable to install virtualbox package' >&2
173     exit 1
174   fi
175 fi
176
177 ##install kmod-VirtualBox
178 if ! lsmod | grep vboxdrv; then
179   if ! sudo /etc/init.d/vboxdrv setup; then
180     printf '%s\n' 'deploy.sh: Unable to install kernel module for virtualbox' >&2
181     exit 1
182   fi
183 else
184   printf '%s\n' 'deploy.sh: Skipping kernel module for virtualbox.  Already Installed'
185 fi
186
187 ##install Vagrant
188 if ! rpm -qa | grep vagrant; then
189   if ! rpm -Uvh https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.rpm; then
190     printf '%s\n' 'deploy.sh: Unable to install vagrant package' >&2
191     exit 1
192   fi
193 else
194   printf '%s\n' 'deploy.sh: Skipping Vagrant install as it is already installed.'
195 fi
196
197 ##add centos 7 box to vagrant
198 if ! vagrant box list | grep chef/centos-7.0; then
199   if ! vagrant box add chef/centos-7.0 --provider virtualbox; then
200     printf '%s\n' 'deploy.sh: Unable to download centos7 box for Vagrant' >&2
201     exit 1
202   fi
203 else
204   printf '%s\n' 'deploy.sh: Skipping Vagrant box add as centos-7.0 is already installed.'
205 fi
206
207 ##install workaround for centos7
208 if ! vagrant plugin list | grep vagrant-centos7_fix; then
209   if ! vagrant plugin install vagrant-centos7_fix; then
210     printf '%s\n' 'deploy.sh: Warning: unable to install vagrant centos7 workaround' >&2
211   fi
212 else
213   printf '%s\n' 'deploy.sh: Skipping Vagrant plugin as centos7 workaround is already installed.'
214 fi
215
216 cd /tmp/
217
218 ##remove bgs vagrant incase it wasn't cleaned up
219 rm -rf /tmp/bgs_vagrant
220
221 ##clone bgs vagrant
222 ##will change this to be opnfv repo when commit is done
223 if ! git clone https://github.com/trozet/bgs_vagrant.git; then
224   printf '%s\n' 'deploy.sh: Unable to clone vagrant repo' >&2
225   exit 1
226 fi
227
228 cd bgs_vagrant
229
230 echo "${blue}Detecting network configuration...${reset}"
231 ##detect host 1 or 3 interface configuration
232 #output=`ip link show | grep -E "^[0-9]" | grep -Ev ": lo|tun|virbr|vboxnet" | awk '{print $2}' | sed 's/://'`
233 output=`ifconfig | grep -E "^[a-Z0-9]+:"| grep -Ev "lo|tun|virbr|vboxnet:" | awk '{print $1}' | sed 's/://'`
234
235 if [ ! "$output" ]; then
236   printf '%s\n' 'deploy.sh: Unable to detect interfaces to bridge to' >&2
237   exit 1
238 fi
239
240 ##find number of interfaces with ip and substitute in VagrantFile
241 if_counter=0
242 for interface in ${output}; do
243
244   if [ "$if_counter" -ge 4 ]; then
245     break
246   fi
247   interface_ip=$(find_ip $interface)
248   if [ ! "$interface_ip" ]; then
249     continue
250   fi
251   new_ip=$(next_usable_ip $interface_ip)
252   if [ ! "$new_ip" ]; then
253     continue
254   fi
255   interface_ip_arr[$if_counter]=$new_ip
256   sed -i 's/^.*eth_replace'"$if_counter"'.*$/  config.vm.network "public_network", ip: '\""$new_ip"\"', bridge: '\'"$interface"\''/' Vagrantfile
257   ((if_counter++))
258 done
259
260 ##now remove interface config in Vagrantfile for 1 node
261 ##if 1, 3, or 4 interfaces set deployment type
262 ##if 2 interfaces remove 2nd interface and set deployment type
263 if [ "$if_counter" == 1 ]; then
264   deployment_type="single_network"
265   remove_vagrant_network eth_replace1
266   remove_vagrant_network eth_replace2
267   remove_vagrant_network eth_replace3
268 elif [ "$if_counter" == 2 ]; then
269   deployment_type="single_network"
270   second_interface=`echo $output | awk '{print $2}'`
271   remove_vagrant_network $second_interface
272   remove_vagrant_network eth_replace2
273 elif [ "$if_counter" == 3 ]; then
274   deployment_type="three_network"
275   remove_vagrant_network eth_replace3
276 else
277   deployment_type="multi_network"
278 fi
279
280 echo "${blue}Network detected: ${deployment_type}! ${reset}"
281
282 if route | grep default; then
283   defaultgw=$(route | grep default | awk '{print $2}')
284   echo "${blue}Default gateway detected: $defaultgw ${reset}"
285   sed -i 's/^.*default_gw =.*$/  default_gw = '\""$defaultgw"\"'/' Vagrantfile
286 else
287   defaultgw=`echo ${interface_arr_ip[0]} | cut -d. -f1-3`
288   firstip=.1
289   defaultgw=$defaultgw$firstip
290   echo "${blue}Unable to find default gateway.  Assuming it is $defaultgw ${reset}"
291   sed -i 's/^.*default_gw =.*$/  default_gw = '\""$defaultgw"\"'/' Vagrantfile
292 fi
293
294 sed -i 's/^.*default_gw:.*$/default_gw:'" $defaultgw"'/' opnfv_ksgen_settings.yml
295
296 if [ $base_config ]; then
297   if ! cp -f $base_config opnfv_ksgen_settings.yml; then
298     echo "{red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
299     exit 1
300   fi
301 fi
302
303 if [ $no_parse ]; then
304 echo "${blue}Skipping parsing variables into settings file as no_parse flag is set${reset}"
305
306 else
307
308 echo "${blue}Gathering network parameters for Target System...this may take a few minutes${reset}"
309 ##Edit the ksgen settings appropriately
310 ##ksgen settings will be stored in /vagrant on the vagrant machine
311 ##if single node deployment all the variables will have the same ip
312 ##interface names will be enp0s3, enp0s8, enp0s9 in chef/centos7
313
314 ##replace private interface parameter
315 ##private interface will be of hosts, so we need to know the provisioned host interface name
316 ##we add biosdevname=0, net.ifnames=0 to the kickstart to use regular interface naming convention on hosts
317 ##replace IP for parameters with next IP that will be given to controller
318 if [ "$deployment_type" == "single_network" ]; then
319   sed -i 's/^.*ovs_tunnel_if:.*$/  ovs_tunnel_if: eth0/' opnfv_ksgen_settings.yml
320   ##we also need to assign IP addresses to nodes
321   ##for single node, foreman is managing the single network, so we can't reserve them
322   ##not supporting single network anymore for now
323   echo "{blue}Single Network type is unsupported right now.  Please check your interface configuration.  Exiting. ${reset}"
324   exit 0
325
326 elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_network" ]]; then
327   sed -i 's/^.*ovs_tunnel_if:.*$/  ovs_tunnel_if: eth1/' opnfv_ksgen_settings.yml
328
329   if [ "$deployment_type" == "three_network" ]; then
330     sed -i 's/^.*storage_iface:.*$/  storage_iface: eth1/' opnfv_ksgen_settings.yml
331     sed -i 's/^.*network_type:.*$/  network_type: three_network/' opnfv_ksgen_settings.yml
332   else
333     sed -i 's/^.*storage_iface:.*$/  storage_iface: eth3/' opnfv_ksgen_settings.yml
334   fi
335
336   ##get ip addresses for private network on controllers to make dhcp entries
337   ##required for controllers_ip_array global param
338   next_private_ip=${interface_ip_arr[1]}
339   type=_private
340   for node in controller1 controller2 controller3; do
341     next_private_ip=$(next_usable_ip $next_private_ip)
342     if [ ! "$next_private_ip" ]; then
343        printf '%s\n' 'deploy.sh: Unable to find next ip for private network for control nodes' >&2
344        exit 1
345     fi
346     sed -i 's/'"$node$type"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
347     controller_ip_array=$controller_ip_array$next_private_ip,
348   done
349
350   ##replace global param for contollers_ip_array
351   controller_ip_array=${controller_ip_array%?}
352   sed -i 's/^.*controllers_ip_array:.*$/  controllers_ip_array: '"$controller_ip_array"'/' opnfv_ksgen_settings.yml
353
354   ##now replace all the VIP variables.  admin//private can be the same IP
355   ##we have to use IP's here that won't be allocated to hosts at provisioning time
356   ##therefore we increment the ip by 10 to make sure we have a safe buffer
357   next_private_ip=$(increment_ip $next_private_ip 10)
358
359   grep -E '*private_vip|loadbalancer_vip|db_vip|amqp_vip|*admin_vip' opnfv_ksgen_settings.yml | while read -r line ; do
360     sed -i 's/^.*'"$line"'.*$/  '"$line $next_private_ip"'/' opnfv_ksgen_settings.yml
361     next_private_ip=$(next_usable_ip $next_private_ip)
362     if [ ! "$next_private_ip" ]; then
363        printf '%s\n' 'deploy.sh: Unable to find next ip for private network for vip replacement' >&2
364        exit 1
365     fi
366   done
367
368   ##replace public_vips
369   next_public_ip=${interface_ip_arr[2]}
370   next_public_ip=$(increment_ip $next_public_ip 10)
371   grep -E '*public_vip' opnfv_ksgen_settings.yml | while read -r line ; do
372     sed -i 's/^.*'"$line"'.*$/  '"$line $next_public_ip"'/' opnfv_ksgen_settings.yml
373     next_public_ip=$(next_usable_ip $next_public_ip)
374     if [ ! "$next_public_ip" ]; then
375        printf '%s\n' 'deploy.sh: Unable to find next ip for public network for vip replcement' >&2
376        exit 1
377     fi
378   done
379
380   ##replace private_subnet param
381   network=.0
382   baseaddr="$(echo $next_private_ip | cut -d. -f1-3)"
383   private_subnet=$baseaddr$network
384   sed -i 's/^.*private_subnet:.*$/  private_subnet:'" $private_subnet"''"\/24"'/' opnfv_ksgen_settings.yml
385 else
386   printf '%s\n' 'deploy.sh: Unknown network type: $deployment_type' >&2
387   exit 1
388 fi
389
390 echo "${blue}Parameters Complete.  Settings have been set for Foreman. ${reset}"
391
392 fi
393
394 echo "${blue}Starting Vagrant! ${reset}"
395
396 ##stand up vagrant
397 if ! vagrant up; then
398   printf '%s\n' 'deploy.sh: Unable to start vagrant' >&2
399   exit 1
400 fi
401
402