blob: 7a7406d47461d7be83e3944b9233505d6637541c [file] [log] [blame]
Doug Hellmannf04178f2012-07-05 17:10:03 -04001# -*- mode: Shell-script -*-
Dean Troyer7f9aa712012-01-31 12:11:56 -06002# functions - Common functions used by DevStack components
Dean Troyer13dc5cc2012-03-27 14:50:45 -05003#
4# ENABLED_SERVICES is used by is_service_enabled()
5
Dean Troyer7f9aa712012-01-31 12:11:56 -06006
Dean Troyer27e32692012-03-16 16:16:56 -05007# Save trace setting
8XTRACE=$(set +o | grep xtrace)
9set +o xtrace
10
Dean Troyer7f9aa712012-01-31 12:11:56 -060011
Vishvananda Ishayac9ad14b2012-07-03 20:29:01 +000012# Exit 0 if address is in network or 1 if
13# address is not in network or netaddr library
14# is not installed.
15function address_in_net() {
16 python -c "
17import netaddr
18import sys
19sys.exit(netaddr.IPAddress('$1') not in netaddr.IPNetwork('$2'))
20"
21}
22
23
Dean Troyer7f9aa712012-01-31 12:11:56 -060024# apt-get wrapper to set arguments correctly
Dean Troyer13dc5cc2012-03-27 14:50:45 -050025# apt_get operation package [package ...]
Dean Troyer7f9aa712012-01-31 12:11:56 -060026function apt_get() {
Dean Troyerd0b21e22012-03-07 14:52:25 -060027 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer7f9aa712012-01-31 12:11:56 -060028 local sudo="sudo"
29 [[ "$(id -u)" = "0" ]] && sudo="env"
30 $sudo DEBIAN_FRONTEND=noninteractive \
31 http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +090032 no_proxy=$no_proxy \
Dean Troyer7f9aa712012-01-31 12:11:56 -060033 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
34}
35
36
37# Gracefully cp only if source file/dir exists
38# cp_it source destination
39function cp_it {
40 if [ -e $1 ] || [ -d $1 ]; then
41 cp -pRL $1 $2
42 fi
43}
44
45
Dean Troyer27e32692012-03-16 16:16:56 -050046# Prints "message" and exits
47# die "message"
48function die() {
Dean Troyer489bd2a2012-03-02 10:44:29 -060049 local exitcode=$?
Dean Troyer27e32692012-03-16 16:16:56 -050050 set +o xtrace
51 echo $@
52 exit $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -060053}
54
55
56# Checks an environment variable is not set or has length 0 OR if the
57# exit code is non-zero and prints "message" and exits
58# NOTE: env-var is the variable name without a '$'
59# die_if_not_set env-var "message"
60function die_if_not_set() {
Dean Troyer27e32692012-03-16 16:16:56 -050061 (
62 local exitcode=$?
63 set +o xtrace
64 local evar=$1; shift
65 if ! is_set $evar || [ $exitcode != 0 ]; then
66 set +o xtrace
67 echo $@
68 exit -1
69 fi
70 )
Dean Troyer489bd2a2012-03-02 10:44:29 -060071}
72
73
74# Grab a numbered field from python prettytable output
75# Fields are numbered starting with 1
76# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
77# get_field field-number
78function get_field() {
79 while read data; do
80 if [ "$1" -lt 0 ]; then
81 field="(\$(NF$1))"
82 else
83 field="\$$(($1 + 1))"
84 fi
85 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
86 done
87}
88
89
Dean Troyer7e270512012-06-14 15:23:24 -050090# get_packages() collects a list of package names of any type from the
91# prerequisite files in ``files/{apts|pips}``. The list is intended
92# to be passed to a package installer such as apt or pip.
93#
94# Only packages required for the services in ENABLED_SERVICES will be
95# included. Two bits of metadata are recognized in the prerequisite files:
96# - ``# NOPRIME`` defers installation to be performed later in stack.sh
97# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
98# of the package to the distros listed. The distro names are case insensitive.
99#
100# get_packages dir
101function get_packages() {
102 local package_dir=$1
103 local file_to_parse
104 local service
105
106 if [[ -z "$package_dir" ]]; then
107 echo "No package directory supplied"
108 return 1
109 fi
110 if [[ -z "$DISTRO" ]]; then
111 echo "No distro set in DISTRO"
112 return 1
113 fi
114 for service in general ${ENABLED_SERVICES//,/ }; do
115 # Allow individual services to specify dependencies
116 if [[ -e ${package_dir}/${service} ]]; then
117 file_to_parse="${file_to_parse} $service"
118 fi
119 # NOTE(sdague) n-api needs glance for now because that's where
120 # glance client is
121 if [[ $service == n-api ]]; then
122 if [[ ! $file_to_parse =~ nova ]]; then
123 file_to_parse="${file_to_parse} nova"
124 fi
125 if [[ ! $file_to_parse =~ glance ]]; then
126 file_to_parse="${file_to_parse} glance"
127 fi
128 elif [[ $service == c-* ]]; then
129 if [[ ! $file_to_parse =~ cinder ]]; then
130 file_to_parse="${file_to_parse} cinder"
131 fi
132 elif [[ $service == n-* ]]; then
133 if [[ ! $file_to_parse =~ nova ]]; then
134 file_to_parse="${file_to_parse} nova"
135 fi
136 elif [[ $service == g-* ]]; then
137 if [[ ! $file_to_parse =~ glance ]]; then
138 file_to_parse="${file_to_parse} glance"
139 fi
140 elif [[ $service == key* ]]; then
141 if [[ ! $file_to_parse =~ keystone ]]; then
142 file_to_parse="${file_to_parse} keystone"
143 fi
144 fi
145 done
146
147 for file in ${file_to_parse}; do
148 local fname=${package_dir}/${file}
149 local OIFS line package distros distro
150 [[ -e $fname ]] || continue
151
152 OIFS=$IFS
153 IFS=$'\n'
154 for line in $(<${fname}); do
155 if [[ $line =~ "NOPRIME" ]]; then
156 continue
157 fi
158
159 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
160 # We are using BASH regexp matching feature.
161 package=${BASH_REMATCH[1]}
162 distros=${BASH_REMATCH[2]}
163 # In bash ${VAR,,} will lowecase VAR
164 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
165 continue
166 fi
167
168 echo ${line%#*}
169 done
170 IFS=$OIFS
171 done
172}
173
174
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500175# Determine OS Vendor, Release and Update
176# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
177# Returns results in global variables:
178# os_VENDOR - vendor name
179# os_RELEASE - release
180# os_UPDATE - update
181# os_PACKAGE - package type
182# os_CODENAME - vendor's codename for release
183# GetOSVersion
184GetOSVersion() {
185 # Figure out which vendor we are
186 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
187 # OS/X
188 os_VENDOR=`sw_vers -productName`
189 os_RELEASE=`sw_vers -productVersion`
190 os_UPDATE=${os_RELEASE##*.}
191 os_RELEASE=${os_RELEASE%.*}
192 os_PACKAGE=""
193 if [[ "$os_RELEASE" =~ "10.7" ]]; then
194 os_CODENAME="lion"
195 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
196 os_CODENAME="snow leopard"
197 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
198 os_CODENAME="leopard"
199 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
200 os_CODENAME="tiger"
201 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
202 os_CODENAME="panther"
203 else
204 os_CODENAME=""
205 fi
206 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
207 os_VENDOR=$(lsb_release -i -s)
208 os_RELEASE=$(lsb_release -r -s)
209 os_UPDATE=""
210 if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
211 os_PACKAGE="deb"
212 else
213 os_PACKAGE="rpm"
214 fi
215 os_CODENAME=$(lsb_release -c -s)
216 elif [[ -r /etc/redhat-release ]]; then
217 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
218 # CentOS release 5.5 (Final)
219 # CentOS Linux release 6.0 (Final)
220 # Fedora release 16 (Verne)
221 os_CODENAME=""
222 for r in "Red Hat" CentOS Fedora; do
223 os_VENDOR=$r
224 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
225 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
226 os_CODENAME=${ver#*|}
227 os_RELEASE=${ver%|*}
228 os_UPDATE=${os_RELEASE##*.}
229 os_RELEASE=${os_RELEASE%.*}
230 break
231 fi
232 os_VENDOR=""
233 done
234 os_PACKAGE="rpm"
235 fi
236 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
237}
238
239
Dean Troyera9e0a482012-07-09 14:07:23 -0500240# Translate the OS version values into common nomenclature
241# Sets ``DISTRO`` from the ``os_*`` values
242function GetDistro() {
243 GetOSVersion
244 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
245 # 'Everyone' refers to Ubuntu releases by the code name adjective
246 DISTRO=$os_CODENAME
247 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
248 # For Fedora, just use 'f' and the release
249 DISTRO="f$os_RELEASE"
250 else
251 # Catch-all for now is Vendor + Release + Update
252 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
253 fi
254 export DISTRO
255}
256
257
Dean Troyer7f9aa712012-01-31 12:11:56 -0600258# git clone only if directory doesn't exist already. Since ``DEST`` might not
259# be owned by the installation user, we create the directory and change the
260# ownership to the proper user.
261# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700262# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
263# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer7f9aa712012-01-31 12:11:56 -0600264# git_clone remote dest-dir branch
265function git_clone {
266 [[ "$OFFLINE" = "True" ]] && return
267
268 GIT_REMOTE=$1
269 GIT_DEST=$2
270 GIT_BRANCH=$3
271
272 if echo $GIT_BRANCH | egrep -q "^refs"; then
273 # If our branch name is a gerrit style refs/changes/...
274 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700275 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600276 git clone $GIT_REMOTE $GIT_DEST
277 fi
278 cd $GIT_DEST
279 git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD
280 else
281 # do a full clone only if the directory doesn't exist
282 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700283 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600284 git clone $GIT_REMOTE $GIT_DEST
285 cd $GIT_DEST
286 # This checkout syntax works for both branches and tags
287 git checkout $GIT_BRANCH
288 elif [[ "$RECLONE" == "yes" ]]; then
289 # if it does exist then simulate what clone does if asked to RECLONE
290 cd $GIT_DEST
291 # set the url to pull from and fetch
292 git remote set-url origin $GIT_REMOTE
293 git fetch origin
294 # remove the existing ignored files (like pyc) as they cause breakage
295 # (due to the py files having older timestamps than our pyc, so python
296 # thinks the pyc files are correct using them)
297 find $GIT_DEST -name '*.pyc' -delete
298 git checkout -f origin/$GIT_BRANCH
299 # a local branch might not exist
300 git branch -D $GIT_BRANCH || true
301 git checkout -b $GIT_BRANCH
302 fi
303 fi
304}
305
306
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500307# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200308# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500309function inicomment() {
310 local file=$1
311 local section=$2
312 local option=$3
313 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
314}
315
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200316# Uncomment an option in an INI file
317# iniuncomment config-file section option
318function iniuncomment() {
319 local file=$1
320 local section=$2
321 local option=$3
322 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
323}
324
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500325
326# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500327# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500328function iniget() {
329 local file=$1
330 local section=$2
331 local option=$3
332 local line
333 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
334 echo ${line#*=}
335}
336
337
338# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500339# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500340function iniset() {
341 local file=$1
342 local section=$2
343 local option=$3
344 local value=$4
Dean Troyer09e636e2012-03-19 16:31:12 -0500345 if ! grep -q "^\[$section\]" $file; then
346 # Add section at the end
347 echo -e "\n[$section]" >>$file
348 fi
349 if [[ -z "$(iniget $file $section $option)" ]]; then
350 # Add it
351 sed -i -e "/^\[$section\]/ a\\
352$option = $value
353" $file
354 else
355 # Replace it
356 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
357 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500358}
359
360
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000361# is_service_enabled() checks if the service(s) specified as arguments are
362# enabled by the user in **ENABLED_SERVICES**.
363#
364# If there are multiple services specified as arguments the test performs a
365# boolean OR or if any of the services specified on the command line
366# return true.
367#
368# There is a special cases for some 'catch-all' services::
369# **nova** returns true if any service enabled start with **n-**
370# **glance** returns true if any service enabled start with **g-**
371# **quantum** returns true if any service enabled start with **q-**
372function is_service_enabled() {
373 services=$@
374 for service in ${services}; do
375 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
376 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500377 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000378 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
379 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
380 done
381 return 1
382}
383
Doug Hellmannf04178f2012-07-05 17:10:03 -0400384# remove extra commas from the input string (ENABLED_SERVICES)
385function _cleanup_service_list () {
386 echo "$1" | sed -e '
387 s/,,/,/g;
388 s/^,//;
389 s/,$//
390 '
391}
392
393# enable_service() adds the services passed as argument to the
394# **ENABLED_SERVICES** list, if they are not already present.
395#
396# For example:
397#
398# enable_service n-vol
399#
400# This function does not know about the special cases
401# for nova, glance, and quantum built into is_service_enabled().
402function enable_service() {
403 local tmpsvcs="${ENABLED_SERVICES}"
404 for service in $@; do
405 if ! is_service_enabled $service; then
406 tmpsvcs+=",$service"
407 fi
408 done
409 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
410 disable_negated_services
411}
412
413# disable_service() removes the services passed as argument to the
414# **ENABLED_SERVICES** list, if they are present.
415#
416# For example:
417#
418# disable_service n-vol
419#
420# This function does not know about the special cases
421# for nova, glance, and quantum built into is_service_enabled().
422function disable_service() {
423 local tmpsvcs=",${ENABLED_SERVICES},"
424 local service
425 for service in $@; do
426 if is_service_enabled $service; then
427 tmpsvcs=${tmpsvcs//,$service,/,}
428 fi
429 done
430 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
431}
432
433# disable_all_services() removes all current services
434# from **ENABLED_SERVICES** to reset the configuration
435# before a minimal installation
436function disable_all_services() {
437 ENABLED_SERVICES=""
438}
439
440# We are looking for services with a - at the beginning to force
441# excluding those services. For example if you want to install all the default
442# services but not nova-volume (n-vol) you can have this set in your localrc :
443# ENABLED_SERVICES+=",-n-vol"
444function disable_negated_services() {
445 local tmpsvcs="${ENABLED_SERVICES}"
446 local service
447 for service in ${tmpsvcs//,/ }; do
448 if [[ ${service} == -* ]]; then
449 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
450 fi
451 done
452 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
453}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600454
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500455# Distro-agnostic package installer
456# install_package package [package ...]
457function install_package() {
458 if [[ -z "$os_PACKAGE" ]]; then
459 GetOSVersion
460 fi
461 if [[ "$os_PACKAGE" = "deb" ]]; then
462 apt_get install "$@"
463 else
464 yum_install "$@"
465 fi
466}
467
468
Dean Troyer489bd2a2012-03-02 10:44:29 -0600469# Test if the named environment variable is set and not zero length
470# is_set env-var
471function is_set() {
472 local var=\$"$1"
473 if eval "[ -z $var ]"; then
474 return 1
475 fi
476 return 0
477}
478
479
Dean Troyer7f9aa712012-01-31 12:11:56 -0600480# pip install wrapper to set cache and proxy environment variables
481# pip_install package [package ...]
482function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600483 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500484 if [[ -z "$os_PACKAGE" ]]; then
485 GetOSVersion
486 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500487 if [[ $TRACK_DEPENDS = True ]] ; then
488 source $DEST/.venv/bin/activate
489 CMD_PIP=$DEST/.venv/bin/pip
490 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500491 else
Monty Taylor47f02062012-07-26 11:09:24 -0500492 SUDO_PIP="sudo"
493 if [[ "$os_PACKAGE" = "deb" ]]; then
494 CMD_PIP=/usr/bin/pip
495 else
496 CMD_PIP=/usr/bin/pip-python
497 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500498 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500499 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600500 HTTP_PROXY=$http_proxy \
501 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900502 NO_PROXY=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500503 $CMD_PIP install --use-mirrors $@
504}
505
506
507# Service wrapper to restart services
508# restart_service service-name
509function restart_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600510 if [[ -z "$os_PACKAGE" ]]; then
511 GetOSVersion
512 fi
513 if [[ "$os_PACKAGE" = "deb" ]]; then
514 sudo /usr/sbin/service $1 restart
515 else
516 sudo /sbin/service $1 restart
517 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500518}
519
520
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500521# pip install the dependencies of the package before we do the setup.py
522# develop, so that pip and not distutils process the dependency chain
523# setup_develop directory
524function setup_develop() {
Monty Taylor47f02062012-07-26 11:09:24 -0500525 if [[ $TRACK_DEPENDS = True ]] ; then
526 SUDO_CMD="env"
527 else
528 SUDO_CMD="sudo"
529 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500530 (cd $1; \
531 python setup.py egg_info; \
532 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
533 depend_links=$(echo $raw_links | xargs); \
534 pip_install -r *-info/requires.txt $depend_links; \
Monty Taylor47f02062012-07-26 11:09:24 -0500535 $SUDO_CMD \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500536 HTTP_PROXY=$http_proxy \
537 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900538 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500539 python setup.py develop \
540 )
541}
542
543
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500544# Service wrapper to start services
545# start_service service-name
546function start_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600547 if [[ -z "$os_PACKAGE" ]]; then
548 GetOSVersion
549 fi
550 if [[ "$os_PACKAGE" = "deb" ]]; then
551 sudo /usr/sbin/service $1 start
552 else
553 sudo /sbin/service $1 start
554 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500555}
556
557
558# Service wrapper to stop services
559# stop_service service-name
560function stop_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600561 if [[ -z "$os_PACKAGE" ]]; then
562 GetOSVersion
563 fi
564 if [[ "$os_PACKAGE" = "deb" ]]; then
565 sudo /usr/sbin/service $1 stop
566 else
567 sudo /sbin/service $1 stop
568 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600569}
570
571
572# Normalize config values to True or False
573# VAR=`trueorfalse default-value test-value`
574function trueorfalse() {
575 local default=$1
576 local testval=$2
577
578 [[ -z "$testval" ]] && { echo "$default"; return; }
579 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
580 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
581 echo "$default"
582}
Dean Troyer27e32692012-03-16 16:16:56 -0500583
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500584
585# yum wrapper to set arguments correctly
586# yum_install package [package ...]
587function yum_install() {
588 [[ "$OFFLINE" = "True" ]] && return
589 local sudo="sudo"
590 [[ "$(id -u)" = "0" ]] && sudo="env"
591 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900592 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500593 yum install -y "$@"
594}
595
596
Dean Troyer27e32692012-03-16 16:16:56 -0500597# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000598$XTRACE