blob: 4bc3bbaac56496bbda965d3e70903d8eaa4fca19 [file] [log] [blame]
Dean Troyerdff49a22014-01-30 15:37:40 -06001# functions-common - Common functions used by DevStack components
2#
3# The canonical copy of this file is maintained in the DevStack repo.
4# All modifications should be made there and then sync'ed to other repos
5# as required.
6#
7# This file is sorted alphabetically within the function groups.
8#
9# - Config Functions
10# - Control Functions
11# - Distro Functions
12# - Git Functions
13# - OpenStack Functions
14# - Package Functions
15# - Process Functions
16# - Python Functions
17# - Service Functions
18#
19# The following variables are assumed to be defined by certain functions:
20#
21# - ``ENABLED_SERVICES``
22# - ``ERROR_ON_CLONE``
23# - ``FILES``
24# - ``OFFLINE``
25# - ``PIP_DOWNLOAD_CACHE``
26# - ``PIP_USE_MIRRORS``
27# - ``RECLONE``
28# - ``TRACK_DEPENDS``
29# - ``http_proxy``, ``https_proxy``, ``no_proxy``
30
31# Save trace setting
32XTRACE=$(set +o | grep xtrace)
33set +o xtrace
34
35
36# Config Functions
37# ================
38
39# Append a new option in an ini file without replacing the old value
40# iniadd config-file section option value1 value2 value3 ...
41function iniadd() {
Sean Dague45917cc2014-02-24 16:09:14 -050042 local xtrace=$(set +o | grep xtrace)
43 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060044 local file=$1
45 local section=$2
46 local option=$3
47 shift 3
48 local values="$(iniget_multiline $file $section $option) $@"
49 iniset_multiline $file $section $option $values
Sean Dague45917cc2014-02-24 16:09:14 -050050 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060051}
52
53# Comment an option in an INI file
54# inicomment config-file section option
55function inicomment() {
Sean Dague45917cc2014-02-24 16:09:14 -050056 local xtrace=$(set +o | grep xtrace)
57 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060058 local file=$1
59 local section=$2
60 local option=$3
61 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -050062 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060063}
64
65# Get an option from an INI file
66# iniget config-file section option
67function iniget() {
Sean Dague45917cc2014-02-24 16:09:14 -050068 local xtrace=$(set +o | grep xtrace)
69 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060070 local file=$1
71 local section=$2
72 local option=$3
73 local line
74 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
75 echo ${line#*=}
Sean Dague45917cc2014-02-24 16:09:14 -050076 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060077}
78
79# Get a multiple line option from an INI file
80# iniget_multiline config-file section option
81function iniget_multiline() {
Sean Dague45917cc2014-02-24 16:09:14 -050082 local xtrace=$(set +o | grep xtrace)
83 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060084 local file=$1
85 local section=$2
86 local option=$3
87 local values
88 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
89 echo ${values}
Sean Dague45917cc2014-02-24 16:09:14 -050090 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060091}
92
93# Determinate is the given option present in the INI file
94# ini_has_option config-file section option
95function ini_has_option() {
Sean Dague45917cc2014-02-24 16:09:14 -050096 local xtrace=$(set +o | grep xtrace)
97 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060098 local file=$1
99 local section=$2
100 local option=$3
101 local line
102 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Sean Dague45917cc2014-02-24 16:09:14 -0500103 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600104 [ -n "$line" ]
105}
106
107# Set an option in an INI file
108# iniset config-file section option value
109function iniset() {
Sean Dague45917cc2014-02-24 16:09:14 -0500110 local xtrace=$(set +o | grep xtrace)
111 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600112 local file=$1
113 local section=$2
114 local option=$3
115 local value=$4
116
117 [[ -z $section || -z $option ]] && return
118
119 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
120 # Add section at the end
121 echo -e "\n[$section]" >>"$file"
122 fi
123 if ! ini_has_option "$file" "$section" "$option"; then
124 # Add it
125 sed -i -e "/^\[$section\]/ a\\
126$option = $value
127" "$file"
128 else
129 local sep=$(echo -ne "\x01")
130 # Replace it
131 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
132 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500133 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600134}
135
136# Set a multiple line option in an INI file
137# iniset_multiline config-file section option value1 value2 valu3 ...
138function iniset_multiline() {
Sean Dague45917cc2014-02-24 16:09:14 -0500139 local xtrace=$(set +o | grep xtrace)
140 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600141 local file=$1
142 local section=$2
143 local option=$3
144 shift 3
145 local values
146 for v in $@; do
147 # The later sed command inserts each new value in the line next to
148 # the section identifier, which causes the values to be inserted in
149 # the reverse order. Do a reverse here to keep the original order.
150 values="$v ${values}"
151 done
152 if ! grep -q "^\[$section\]" "$file"; then
153 # Add section at the end
154 echo -e "\n[$section]" >>"$file"
155 else
156 # Remove old values
157 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
158 fi
159 # Add new ones
160 for v in $values; do
161 sed -i -e "/^\[$section\]/ a\\
162$option = $v
163" "$file"
164 done
Sean Dague45917cc2014-02-24 16:09:14 -0500165 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600166}
167
168# Uncomment an option in an INI file
169# iniuncomment config-file section option
170function iniuncomment() {
Sean Dague45917cc2014-02-24 16:09:14 -0500171 local xtrace=$(set +o | grep xtrace)
172 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600173 local file=$1
174 local section=$2
175 local option=$3
176 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500177 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600178}
179
180# Normalize config values to True or False
181# Accepts as False: 0 no No NO false False FALSE
182# Accepts as True: 1 yes Yes YES true True TRUE
183# VAR=$(trueorfalse default-value test-value)
184function trueorfalse() {
Sean Dague45917cc2014-02-24 16:09:14 -0500185 local xtrace=$(set +o | grep xtrace)
186 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600187 local default=$1
188 local testval=$2
189
190 [[ -z "$testval" ]] && { echo "$default"; return; }
191 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
192 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
193 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500194 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600195}
196
197
198# Control Functions
199# =================
200
201# Prints backtrace info
202# filename:lineno:function
203# backtrace level
204function backtrace {
205 local level=$1
206 local deep=$((${#BASH_SOURCE[@]} - 1))
207 echo "[Call Trace]"
208 while [ $level -le $deep ]; do
209 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
210 deep=$((deep - 1))
211 done
212}
213
214# Prints line number and "message" then exits
215# die $LINENO "message"
216function die() {
217 local exitcode=$?
218 set +o xtrace
219 local line=$1; shift
220 if [ $exitcode == 0 ]; then
221 exitcode=1
222 fi
223 backtrace 2
224 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600225 # Give buffers a second to flush
226 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600227 exit $exitcode
228}
229
230# Checks an environment variable is not set or has length 0 OR if the
231# exit code is non-zero and prints "message" and exits
232# NOTE: env-var is the variable name without a '$'
233# die_if_not_set $LINENO env-var "message"
234function die_if_not_set() {
235 local exitcode=$?
236 FXTRACE=$(set +o | grep xtrace)
237 set +o xtrace
238 local line=$1; shift
239 local evar=$1; shift
240 if ! is_set $evar || [ $exitcode != 0 ]; then
241 die $line "$*"
242 fi
243 $FXTRACE
244}
245
246# Prints line number and "message" in error format
247# err $LINENO "message"
248function err() {
249 local exitcode=$?
250 errXTRACE=$(set +o | grep xtrace)
251 set +o xtrace
252 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
253 echo $msg 1>&2;
254 if [[ -n ${SCREEN_LOGDIR} ]]; then
255 echo $msg >> "${SCREEN_LOGDIR}/error.log"
256 fi
257 $errXTRACE
258 return $exitcode
259}
260
261# Checks an environment variable is not set or has length 0 OR if the
262# exit code is non-zero and prints "message"
263# NOTE: env-var is the variable name without a '$'
264# err_if_not_set $LINENO env-var "message"
265function err_if_not_set() {
266 local exitcode=$?
267 errinsXTRACE=$(set +o | grep xtrace)
268 set +o xtrace
269 local line=$1; shift
270 local evar=$1; shift
271 if ! is_set $evar || [ $exitcode != 0 ]; then
272 err $line "$*"
273 fi
274 $errinsXTRACE
275 return $exitcode
276}
277
278# Exit after outputting a message about the distribution not being supported.
279# exit_distro_not_supported [optional-string-telling-what-is-missing]
280function exit_distro_not_supported {
281 if [[ -z "$DISTRO" ]]; then
282 GetDistro
283 fi
284
285 if [ $# -gt 0 ]; then
286 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
287 else
288 die $LINENO "Support for $DISTRO is incomplete."
289 fi
290}
291
292# Test if the named environment variable is set and not zero length
293# is_set env-var
294function is_set() {
295 local var=\$"$1"
296 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
297}
298
299# Prints line number and "message" in warning format
300# warn $LINENO "message"
301function warn() {
302 local exitcode=$?
303 errXTRACE=$(set +o | grep xtrace)
304 set +o xtrace
305 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
306 echo $msg 1>&2;
307 if [[ -n ${SCREEN_LOGDIR} ]]; then
308 echo $msg >> "${SCREEN_LOGDIR}/error.log"
309 fi
310 $errXTRACE
311 return $exitcode
312}
313
314
315# Distro Functions
316# ================
317
318# Determine OS Vendor, Release and Update
319# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
320# Returns results in global variables:
321# os_VENDOR - vendor name
322# os_RELEASE - release
323# os_UPDATE - update
324# os_PACKAGE - package type
325# os_CODENAME - vendor's codename for release
326# GetOSVersion
327GetOSVersion() {
328 # Figure out which vendor we are
329 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
330 # OS/X
331 os_VENDOR=`sw_vers -productName`
332 os_RELEASE=`sw_vers -productVersion`
333 os_UPDATE=${os_RELEASE##*.}
334 os_RELEASE=${os_RELEASE%.*}
335 os_PACKAGE=""
336 if [[ "$os_RELEASE" =~ "10.7" ]]; then
337 os_CODENAME="lion"
338 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
339 os_CODENAME="snow leopard"
340 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
341 os_CODENAME="leopard"
342 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
343 os_CODENAME="tiger"
344 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
345 os_CODENAME="panther"
346 else
347 os_CODENAME=""
348 fi
349 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
350 os_VENDOR=$(lsb_release -i -s)
351 os_RELEASE=$(lsb_release -r -s)
352 os_UPDATE=""
353 os_PACKAGE="rpm"
354 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
355 os_PACKAGE="deb"
356 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
357 lsb_release -d -s | grep -q openSUSE
358 if [[ $? -eq 0 ]]; then
359 os_VENDOR="openSUSE"
360 fi
361 elif [[ $os_VENDOR == "openSUSE project" ]]; then
362 os_VENDOR="openSUSE"
363 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
364 os_VENDOR="Red Hat"
365 fi
366 os_CODENAME=$(lsb_release -c -s)
367 elif [[ -r /etc/redhat-release ]]; then
368 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
369 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
370 # CentOS release 5.5 (Final)
371 # CentOS Linux release 6.0 (Final)
372 # Fedora release 16 (Verne)
373 # XenServer release 6.2.0-70446c (xenenterprise)
374 os_CODENAME=""
375 for r in "Red Hat" CentOS Fedora XenServer; do
376 os_VENDOR=$r
377 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
378 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
379 os_CODENAME=${ver#*|}
380 os_RELEASE=${ver%|*}
381 os_UPDATE=${os_RELEASE##*.}
382 os_RELEASE=${os_RELEASE%.*}
383 break
384 fi
385 os_VENDOR=""
386 done
387 os_PACKAGE="rpm"
388 elif [[ -r /etc/SuSE-release ]]; then
389 for r in openSUSE "SUSE Linux"; do
390 if [[ "$r" = "SUSE Linux" ]]; then
391 os_VENDOR="SUSE LINUX"
392 else
393 os_VENDOR=$r
394 fi
395
396 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
397 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
398 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
399 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
400 break
401 fi
402 os_VENDOR=""
403 done
404 os_PACKAGE="rpm"
405 # If lsb_release is not installed, we should be able to detect Debian OS
406 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
407 os_VENDOR="Debian"
408 os_PACKAGE="deb"
409 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
410 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
411 fi
412 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
413}
414
415# Translate the OS version values into common nomenclature
416# Sets global ``DISTRO`` from the ``os_*`` values
417function GetDistro() {
418 GetOSVersion
419 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
420 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
421 DISTRO=$os_CODENAME
422 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
423 # For Fedora, just use 'f' and the release
424 DISTRO="f$os_RELEASE"
425 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
426 DISTRO="opensuse-$os_RELEASE"
427 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
428 # For SLE, also use the service pack
429 if [[ -z "$os_UPDATE" ]]; then
430 DISTRO="sle${os_RELEASE}"
431 else
432 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
433 fi
434 elif [[ "$os_VENDOR" =~ (Red Hat) || "$os_VENDOR" =~ (CentOS) ]]; then
435 # Drop the . release as we assume it's compatible
436 DISTRO="rhel${os_RELEASE::1}"
437 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
438 DISTRO="xs$os_RELEASE"
439 else
440 # Catch-all for now is Vendor + Release + Update
441 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
442 fi
443 export DISTRO
444}
445
446# Utility function for checking machine architecture
447# is_arch arch-type
448function is_arch {
449 ARCH_TYPE=$1
450
451 [[ "$(uname -m)" == "$ARCH_TYPE" ]]
452}
453
454# Determine if current distribution is a Fedora-based distribution
455# (Fedora, RHEL, CentOS, etc).
456# is_fedora
457function is_fedora {
458 if [[ -z "$os_VENDOR" ]]; then
459 GetOSVersion
460 fi
461
462 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ]
463}
464
465
466# Determine if current distribution is a SUSE-based distribution
467# (openSUSE, SLE).
468# is_suse
469function is_suse {
470 if [[ -z "$os_VENDOR" ]]; then
471 GetOSVersion
472 fi
473
474 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
475}
476
477
478# Determine if current distribution is an Ubuntu-based distribution
479# It will also detect non-Ubuntu but Debian-based distros
480# is_ubuntu
481function is_ubuntu {
482 if [[ -z "$os_PACKAGE" ]]; then
483 GetOSVersion
484 fi
485 [ "$os_PACKAGE" = "deb" ]
486}
487
488
489# Git Functions
490# =============
491
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600492# Returns openstack release name for a given branch name
493# ``get_release_name_from_branch branch-name``
494function get_release_name_from_branch(){
495 local branch=$1
496 if [[ $branch =~ "stable/" ]]; then
497 echo ${branch#*/}
498 else
499 echo "master"
500 fi
501}
502
Dean Troyerdff49a22014-01-30 15:37:40 -0600503# git clone only if directory doesn't exist already. Since ``DEST`` might not
504# be owned by the installation user, we create the directory and change the
505# ownership to the proper user.
506# Set global RECLONE=yes to simulate a clone when dest-dir exists
507# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
508# does not exist (default is False, meaning the repo will be cloned).
509# Uses global ``OFFLINE``
510# git_clone remote dest-dir branch
511function git_clone {
512 GIT_REMOTE=$1
513 GIT_DEST=$2
514 GIT_REF=$3
515 RECLONE=$(trueorfalse False $RECLONE)
516
517 if [[ "$OFFLINE" = "True" ]]; then
518 echo "Running in offline mode, clones already exist"
519 # print out the results so we know what change was used in the logs
520 cd $GIT_DEST
521 git show --oneline | head -1
522 return
523 fi
524
525 if echo $GIT_REF | egrep -q "^refs"; then
526 # If our branch name is a gerrit style refs/changes/...
527 if [[ ! -d $GIT_DEST ]]; then
528 [[ "$ERROR_ON_CLONE" = "True" ]] && \
529 die $LINENO "Cloning not allowed in this configuration"
530 git clone $GIT_REMOTE $GIT_DEST
531 fi
532 cd $GIT_DEST
533 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
534 else
535 # do a full clone only if the directory doesn't exist
536 if [[ ! -d $GIT_DEST ]]; then
537 [[ "$ERROR_ON_CLONE" = "True" ]] && \
538 die $LINENO "Cloning not allowed in this configuration"
539 git clone $GIT_REMOTE $GIT_DEST
540 cd $GIT_DEST
541 # This checkout syntax works for both branches and tags
542 git checkout $GIT_REF
543 elif [[ "$RECLONE" = "True" ]]; then
544 # if it does exist then simulate what clone does if asked to RECLONE
545 cd $GIT_DEST
546 # set the url to pull from and fetch
547 git remote set-url origin $GIT_REMOTE
548 git fetch origin
549 # remove the existing ignored files (like pyc) as they cause breakage
550 # (due to the py files having older timestamps than our pyc, so python
551 # thinks the pyc files are correct using them)
552 find $GIT_DEST -name '*.pyc' -delete
553
554 # handle GIT_REF accordingly to type (tag, branch)
555 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
556 git_update_tag $GIT_REF
557 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
558 git_update_branch $GIT_REF
559 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
560 git_update_remote_branch $GIT_REF
561 else
562 die $LINENO "$GIT_REF is neither branch nor tag"
563 fi
564
565 fi
566 fi
567
568 # print out the results so we know what change was used in the logs
569 cd $GIT_DEST
570 git show --oneline | head -1
571}
572
573# git update using reference as a branch.
574# git_update_branch ref
575function git_update_branch() {
576
577 GIT_BRANCH=$1
578
579 git checkout -f origin/$GIT_BRANCH
580 # a local branch might not exist
581 git branch -D $GIT_BRANCH || true
582 git checkout -b $GIT_BRANCH
583}
584
585# git update using reference as a branch.
586# git_update_remote_branch ref
587function git_update_remote_branch() {
588
589 GIT_BRANCH=$1
590
591 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
592}
593
594# git update using reference as a tag. Be careful editing source at that repo
595# as working copy will be in a detached mode
596# git_update_tag ref
597function git_update_tag() {
598
599 GIT_TAG=$1
600
601 git tag -d $GIT_TAG
602 # fetching given tag only
603 git fetch origin tag $GIT_TAG
604 git checkout -f $GIT_TAG
605}
606
607
608# OpenStack Functions
609# ===================
610
611# Get the default value for HOST_IP
612# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
613function get_default_host_ip() {
614 local fixed_range=$1
615 local floating_range=$2
616 local host_ip_iface=$3
617 local host_ip=$4
618
619 # Find the interface used for the default route
620 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
621 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
622 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
623 host_ip=""
624 host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}'`
625 for IP in $host_ips; do
626 # Attempt to filter out IP addresses that are part of the fixed and
627 # floating range. Note that this method only works if the ``netaddr``
628 # python library is installed. If it is not installed, an error
629 # will be printed and the first IP from the interface will be used.
630 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
631 # address.
632 if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then
633 host_ip=$IP
634 break;
635 fi
636 done
637 fi
638 echo $host_ip
639}
640
641# Grab a numbered field from python prettytable output
642# Fields are numbered starting with 1
643# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
644# get_field field-number
645function get_field() {
646 while read data; do
647 if [ "$1" -lt 0 ]; then
648 field="(\$(NF$1))"
649 else
650 field="\$$(($1 + 1))"
651 fi
652 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
653 done
654}
655
656# Add a policy to a policy.json file
657# Do nothing if the policy already exists
658# ``policy_add policy_file policy_name policy_permissions``
659function policy_add() {
660 local policy_file=$1
661 local policy_name=$2
662 local policy_perm=$3
663
664 if grep -q ${policy_name} ${policy_file}; then
665 echo "Policy ${policy_name} already exists in ${policy_file}"
666 return
667 fi
668
669 # Add a terminating comma to policy lines without one
670 # Remove the closing '}' and all lines following to the end-of-file
671 local tmpfile=$(mktemp)
672 uniq ${policy_file} | sed -e '
673 s/]$/],/
674 /^[}]/,$d
675 ' > ${tmpfile}
676
677 # Append policy and closing brace
678 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
679 echo "}" >>${tmpfile}
680
681 mv ${tmpfile} ${policy_file}
682}
683
684
685# Package Functions
686# =================
687
688# _get_package_dir
689function _get_package_dir() {
690 local pkg_dir
691 if is_ubuntu; then
692 pkg_dir=$FILES/apts
693 elif is_fedora; then
694 pkg_dir=$FILES/rpms
695 elif is_suse; then
696 pkg_dir=$FILES/rpms-suse
697 else
698 exit_distro_not_supported "list of packages"
699 fi
700 echo "$pkg_dir"
701}
702
703# Wrapper for ``apt-get`` to set cache and proxy environment variables
704# Uses globals ``OFFLINE``, ``*_proxy``
705# apt_get operation package [package ...]
706function apt_get() {
Sean Dague45917cc2014-02-24 16:09:14 -0500707 local xtrace=$(set +o | grep xtrace)
708 set +o xtrace
709
Dean Troyerdff49a22014-01-30 15:37:40 -0600710 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
711 local sudo="sudo"
712 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500713
714 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600715 $sudo DEBIAN_FRONTEND=noninteractive \
716 http_proxy=$http_proxy https_proxy=$https_proxy \
717 no_proxy=$no_proxy \
718 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
719}
720
721# get_packages() collects a list of package names of any type from the
722# prerequisite files in ``files/{apts|rpms}``. The list is intended
723# to be passed to a package installer such as apt or yum.
724#
725# Only packages required for the services in 1st argument will be
726# included. Two bits of metadata are recognized in the prerequisite files:
727#
728# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
729# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
730# of the package to the distros listed. The distro names are case insensitive.
731function get_packages() {
Sean Dague45917cc2014-02-24 16:09:14 -0500732 local xtrace=$(set +o | grep xtrace)
733 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600734 local services=$@
735 local package_dir=$(_get_package_dir)
736 local file_to_parse
737 local service
738
739 if [[ -z "$package_dir" ]]; then
740 echo "No package directory supplied"
741 return 1
742 fi
743 if [[ -z "$DISTRO" ]]; then
744 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -0500745 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -0600746 fi
747 for service in ${services//,/ }; do
748 # Allow individual services to specify dependencies
749 if [[ -e ${package_dir}/${service} ]]; then
750 file_to_parse="${file_to_parse} $service"
751 fi
752 # NOTE(sdague) n-api needs glance for now because that's where
753 # glance client is
754 if [[ $service == n-api ]]; then
755 if [[ ! $file_to_parse =~ nova ]]; then
756 file_to_parse="${file_to_parse} nova"
757 fi
758 if [[ ! $file_to_parse =~ glance ]]; then
759 file_to_parse="${file_to_parse} glance"
760 fi
761 elif [[ $service == c-* ]]; then
762 if [[ ! $file_to_parse =~ cinder ]]; then
763 file_to_parse="${file_to_parse} cinder"
764 fi
765 elif [[ $service == ceilometer-* ]]; then
766 if [[ ! $file_to_parse =~ ceilometer ]]; then
767 file_to_parse="${file_to_parse} ceilometer"
768 fi
769 elif [[ $service == s-* ]]; then
770 if [[ ! $file_to_parse =~ swift ]]; then
771 file_to_parse="${file_to_parse} swift"
772 fi
773 elif [[ $service == n-* ]]; then
774 if [[ ! $file_to_parse =~ nova ]]; then
775 file_to_parse="${file_to_parse} nova"
776 fi
777 elif [[ $service == g-* ]]; then
778 if [[ ! $file_to_parse =~ glance ]]; then
779 file_to_parse="${file_to_parse} glance"
780 fi
781 elif [[ $service == key* ]]; then
782 if [[ ! $file_to_parse =~ keystone ]]; then
783 file_to_parse="${file_to_parse} keystone"
784 fi
785 elif [[ $service == q-* ]]; then
786 if [[ ! $file_to_parse =~ neutron ]]; then
787 file_to_parse="${file_to_parse} neutron"
788 fi
789 fi
790 done
791
792 for file in ${file_to_parse}; do
793 local fname=${package_dir}/${file}
794 local OIFS line package distros distro
795 [[ -e $fname ]] || continue
796
797 OIFS=$IFS
798 IFS=$'\n'
799 for line in $(<${fname}); do
800 if [[ $line =~ "NOPRIME" ]]; then
801 continue
802 fi
803
804 # Assume we want this package
805 package=${line%#*}
806 inst_pkg=1
807
808 # Look for # dist:xxx in comment
809 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
810 # We are using BASH regexp matching feature.
811 package=${BASH_REMATCH[1]}
812 distros=${BASH_REMATCH[2]}
813 # In bash ${VAR,,} will lowecase VAR
814 # Look for a match in the distro list
815 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
816 # If no match then skip this package
817 inst_pkg=0
818 fi
819 fi
820
821 # Look for # testonly in comment
822 if [[ $line =~ (.*)#.*testonly.* ]]; then
823 package=${BASH_REMATCH[1]}
824 # Are we installing test packages? (test for the default value)
825 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
826 # If not installing test packages the skip this package
827 inst_pkg=0
828 fi
829 fi
830
831 if [[ $inst_pkg = 1 ]]; then
832 echo $package
833 fi
834 done
835 IFS=$OIFS
836 done
Sean Dague45917cc2014-02-24 16:09:14 -0500837 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600838}
839
840# Distro-agnostic package installer
841# install_package package [package ...]
842function install_package() {
Sean Dague45917cc2014-02-24 16:09:14 -0500843 local xtrace=$(set +o | grep xtrace)
844 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600845 if is_ubuntu; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600846 # if there are transient errors pulling the updates, that's fine. It may
847 # be secondary repositories that we don't really care about.
848 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update || /bin/true
Dean Troyerdff49a22014-01-30 15:37:40 -0600849 NO_UPDATE_REPOS=True
850
Sean Dague45917cc2014-02-24 16:09:14 -0500851 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600852 apt_get install "$@"
853 elif is_fedora; then
Sean Dague45917cc2014-02-24 16:09:14 -0500854 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600855 yum_install "$@"
856 elif is_suse; then
Sean Dague45917cc2014-02-24 16:09:14 -0500857 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600858 zypper_install "$@"
859 else
Sean Dague45917cc2014-02-24 16:09:14 -0500860 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600861 exit_distro_not_supported "installing packages"
862 fi
863}
864
865# Distro-agnostic function to tell if a package is installed
866# is_package_installed package [package ...]
867function is_package_installed() {
868 if [[ -z "$@" ]]; then
869 return 1
870 fi
871
872 if [[ -z "$os_PACKAGE" ]]; then
873 GetOSVersion
874 fi
875
876 if [[ "$os_PACKAGE" = "deb" ]]; then
877 dpkg -s "$@" > /dev/null 2> /dev/null
878 elif [[ "$os_PACKAGE" = "rpm" ]]; then
879 rpm --quiet -q "$@"
880 else
881 exit_distro_not_supported "finding if a package is installed"
882 fi
883}
884
885# Distro-agnostic package uninstaller
886# uninstall_package package [package ...]
887function uninstall_package() {
888 if is_ubuntu; then
889 apt_get purge "$@"
890 elif is_fedora; then
891 sudo yum remove -y "$@"
892 elif is_suse; then
893 sudo zypper rm "$@"
894 else
895 exit_distro_not_supported "uninstalling packages"
896 fi
897}
898
899# Wrapper for ``yum`` to set proxy environment variables
900# Uses globals ``OFFLINE``, ``*_proxy``
901# yum_install package [package ...]
902function yum_install() {
903 [[ "$OFFLINE" = "True" ]] && return
904 local sudo="sudo"
905 [[ "$(id -u)" = "0" ]] && sudo="env"
906 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
907 no_proxy=$no_proxy \
908 yum install -y "$@"
909}
910
911# zypper wrapper to set arguments correctly
912# zypper_install package [package ...]
913function zypper_install() {
914 [[ "$OFFLINE" = "True" ]] && return
915 local sudo="sudo"
916 [[ "$(id -u)" = "0" ]] && sudo="env"
917 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
918 zypper --non-interactive install --auto-agree-with-licenses "$@"
919}
920
921
922# Process Functions
923# =================
924
925# _run_process() is designed to be backgrounded by run_process() to simulate a
926# fork. It includes the dirty work of closing extra filehandles and preparing log
927# files to produce the same logs as screen_it(). The log filename is derived
928# from the service name and global-and-now-misnamed SCREEN_LOGDIR
929# _run_process service "command-line"
930function _run_process() {
931 local service=$1
932 local command="$2"
933
934 # Undo logging redirections and close the extra descriptors
935 exec 1>&3
936 exec 2>&3
937 exec 3>&-
938 exec 6>&-
939
940 if [[ -n ${SCREEN_LOGDIR} ]]; then
941 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
942 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
943
944 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
945 export PYTHONUNBUFFERED=1
946 fi
947
948 exec /bin/bash -c "$command"
949 die "$service exec failure: $command"
950}
951
952# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
953# This is used for ``service_check`` when all the ``screen_it`` are called finished
954# init_service_check
955function init_service_check() {
956 SCREEN_NAME=${SCREEN_NAME:-stack}
957 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
958
959 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
960 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
961 fi
962
963 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
964}
965
966# Find out if a process exists by partial name.
967# is_running name
968function is_running() {
969 local name=$1
970 ps auxw | grep -v grep | grep ${name} > /dev/null
971 RC=$?
972 # some times I really hate bash reverse binary logic
973 return $RC
974}
975
976# run_process() launches a child process that closes all file descriptors and
977# then exec's the passed in command. This is meant to duplicate the semantics
978# of screen_it() without screen. PIDs are written to
979# $SERVICE_DIR/$SCREEN_NAME/$service.pid
980# run_process service "command-line"
981function run_process() {
982 local service=$1
983 local command="$2"
984
985 # Spawn the child process
986 _run_process "$service" "$command" &
987 echo $!
988}
989
990# Helper to launch a service in a named screen
991# screen_it service "command-line"
992function screen_it {
993 SCREEN_NAME=${SCREEN_NAME:-stack}
994 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
995 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
996
997 if is_service_enabled $1; then
998 # Append the service to the screen rc file
999 screen_rc "$1" "$2"
1000
1001 if [[ "$USE_SCREEN" = "True" ]]; then
1002 screen -S $SCREEN_NAME -X screen -t $1
1003
1004 if [[ -n ${SCREEN_LOGDIR} ]]; then
1005 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
1006 screen -S $SCREEN_NAME -p $1 -X log on
1007 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1008 fi
1009
1010 # sleep to allow bash to be ready to be send the command - we are
1011 # creating a new window in screen and then sends characters, so if
1012 # bash isn't running by the time we send the command, nothing happens
1013 sleep 1.5
1014
1015 NL=`echo -ne '\015'`
1016 # This fun command does the following:
1017 # - the passed server command is backgrounded
1018 # - the pid of the background process is saved in the usual place
1019 # - the server process is brought back to the foreground
1020 # - if the server process exits prematurely the fg command errors
1021 # and a message is written to stdout and the service failure file
1022 # The pid saved can be used in screen_stop() as a process group
1023 # id to kill off all child processes
1024 screen -S $SCREEN_NAME -p $1 -X stuff "$2 & echo \$! >$SERVICE_DIR/$SCREEN_NAME/$1.pid; fg || echo \"$1 failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
1025 else
1026 # Spawn directly without screen
1027 run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1028 fi
1029 fi
1030}
1031
1032# Screen rc file builder
1033# screen_rc service "command-line"
1034function screen_rc {
1035 SCREEN_NAME=${SCREEN_NAME:-stack}
1036 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1037 if [[ ! -e $SCREENRC ]]; then
1038 # Name the screen session
1039 echo "sessionname $SCREEN_NAME" > $SCREENRC
1040 # Set a reasonable statusbar
1041 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1042 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1043 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1044 echo "screen -t shell bash" >> $SCREENRC
1045 fi
1046 # If this service doesn't already exist in the screenrc file
1047 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1048 NL=`echo -ne '\015'`
1049 echo "screen -t $1 bash" >> $SCREENRC
1050 echo "stuff \"$2$NL\"" >> $SCREENRC
1051
1052 if [[ -n ${SCREEN_LOGDIR} ]]; then
1053 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1054 echo "log on" >>$SCREENRC
1055 fi
1056 fi
1057}
1058
1059# Stop a service in screen
1060# If a PID is available use it, kill the whole process group via TERM
1061# If screen is being used kill the screen window; this will catch processes
1062# that did not leave a PID behind
1063# screen_stop service
1064function screen_stop() {
1065 SCREEN_NAME=${SCREEN_NAME:-stack}
1066 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1067 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1068
1069 if is_service_enabled $1; then
1070 # Kill via pid if we have one available
1071 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$1.pid ]]; then
1072 pkill -TERM -P -$(cat $SERVICE_DIR/$SCREEN_NAME/$1.pid)
1073 rm $SERVICE_DIR/$SCREEN_NAME/$1.pid
1074 fi
1075 if [[ "$USE_SCREEN" = "True" ]]; then
1076 # Clean up the screen window
1077 screen -S $SCREEN_NAME -p $1 -X kill
1078 fi
1079 fi
1080}
1081
1082# Helper to get the status of each running service
1083# service_check
1084function service_check() {
1085 local service
1086 local failures
1087 SCREEN_NAME=${SCREEN_NAME:-stack}
1088 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1089
1090
1091 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1092 echo "No service status directory found"
1093 return
1094 fi
1095
1096 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
1097 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null`
1098
1099 for service in $failures; do
1100 service=`basename $service`
1101 service=${service%.failure}
1102 echo "Error: Service $service is not running"
1103 done
1104
1105 if [ -n "$failures" ]; then
1106 echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
1107 fi
1108}
1109
1110
1111# Python Functions
1112# ================
1113
1114# Get the path to the pip command.
1115# get_pip_command
1116function get_pip_command() {
1117 which pip || which pip-python
1118
1119 if [ $? -ne 0 ]; then
1120 die $LINENO "Unable to find pip; cannot continue"
1121 fi
1122}
1123
1124# Get the path to the direcotry where python executables are installed.
1125# get_python_exec_prefix
1126function get_python_exec_prefix() {
1127 if is_fedora || is_suse; then
1128 echo "/usr/bin"
1129 else
1130 echo "/usr/local/bin"
1131 fi
1132}
1133
1134# Wrapper for ``pip install`` to set cache and proxy environment variables
1135# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
1136# ``TRACK_DEPENDS``, ``*_proxy``
1137# pip_install package [package ...]
1138function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001139 local xtrace=$(set +o | grep xtrace)
1140 set +o xtrace
1141 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1142 $xtrace
1143 return
1144 fi
1145
Dean Troyerdff49a22014-01-30 15:37:40 -06001146 if [[ -z "$os_PACKAGE" ]]; then
1147 GetOSVersion
1148 fi
1149 if [[ $TRACK_DEPENDS = True ]]; then
1150 source $DEST/.venv/bin/activate
1151 CMD_PIP=$DEST/.venv/bin/pip
1152 SUDO_PIP="env"
1153 else
1154 SUDO_PIP="sudo"
1155 CMD_PIP=$(get_pip_command)
1156 fi
1157
1158 # Mirror option not needed anymore because pypi has CDN available,
1159 # but it's useful in certain circumstances
1160 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
1161 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
1162 PIP_MIRROR_OPT="--use-mirrors"
1163 fi
1164
1165 # pip < 1.4 has a bug where it will use an already existing build
1166 # directory unconditionally. Say an earlier component installs
1167 # foo v1.1; pip will have built foo's source in
1168 # /tmp/$USER-pip-build. Even if a later component specifies foo <
1169 # 1.1, the existing extracted build will be used and cause
1170 # confusing errors. By creating unique build directories we avoid
1171 # this problem. See https://github.com/pypa/pip/issues/709
1172 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1173
Sean Dague45917cc2014-02-24 16:09:14 -05001174 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001175 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1176 HTTP_PROXY=$http_proxy \
1177 HTTPS_PROXY=$https_proxy \
1178 NO_PROXY=$no_proxy \
1179 $CMD_PIP install --build=${pip_build_tmp} \
1180 $PIP_MIRROR_OPT $@ \
1181 && $SUDO_PIP rm -rf ${pip_build_tmp}
1182}
1183
1184
1185# Service Functions
1186# =================
1187
1188# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1189# _cleanup_service_list service-list
1190function _cleanup_service_list () {
1191 echo "$1" | sed -e '
1192 s/,,/,/g;
1193 s/^,//;
1194 s/,$//
1195 '
1196}
1197
1198# disable_all_services() removes all current services
1199# from ``ENABLED_SERVICES`` to reset the configuration
1200# before a minimal installation
1201# Uses global ``ENABLED_SERVICES``
1202# disable_all_services
1203function disable_all_services() {
1204 ENABLED_SERVICES=""
1205}
1206
1207# Remove all services starting with '-'. For example, to install all default
1208# services except rabbit (rabbit) set in ``localrc``:
1209# ENABLED_SERVICES+=",-rabbit"
1210# Uses global ``ENABLED_SERVICES``
1211# disable_negated_services
1212function disable_negated_services() {
1213 local tmpsvcs="${ENABLED_SERVICES}"
1214 local service
1215 for service in ${tmpsvcs//,/ }; do
1216 if [[ ${service} == -* ]]; then
1217 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1218 fi
1219 done
1220 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1221}
1222
1223# disable_service() removes the services passed as argument to the
1224# ``ENABLED_SERVICES`` list, if they are present.
1225#
1226# For example:
1227# disable_service rabbit
1228#
1229# This function does not know about the special cases
1230# for nova, glance, and neutron built into is_service_enabled().
1231# Uses global ``ENABLED_SERVICES``
1232# disable_service service [service ...]
1233function disable_service() {
1234 local tmpsvcs=",${ENABLED_SERVICES},"
1235 local service
1236 for service in $@; do
1237 if is_service_enabled $service; then
1238 tmpsvcs=${tmpsvcs//,$service,/,}
1239 fi
1240 done
1241 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1242}
1243
1244# enable_service() adds the services passed as argument to the
1245# ``ENABLED_SERVICES`` list, if they are not already present.
1246#
1247# For example:
1248# enable_service qpid
1249#
1250# This function does not know about the special cases
1251# for nova, glance, and neutron built into is_service_enabled().
1252# Uses global ``ENABLED_SERVICES``
1253# enable_service service [service ...]
1254function enable_service() {
1255 local tmpsvcs="${ENABLED_SERVICES}"
1256 for service in $@; do
1257 if ! is_service_enabled $service; then
1258 tmpsvcs+=",$service"
1259 fi
1260 done
1261 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1262 disable_negated_services
1263}
1264
1265# is_service_enabled() checks if the service(s) specified as arguments are
1266# enabled by the user in ``ENABLED_SERVICES``.
1267#
1268# Multiple services specified as arguments are ``OR``'ed together; the test
1269# is a short-circuit boolean, i.e it returns on the first match.
1270#
1271# There are special cases for some 'catch-all' services::
1272# **nova** returns true if any service enabled start with **n-**
1273# **cinder** returns true if any service enabled start with **c-**
1274# **ceilometer** returns true if any service enabled start with **ceilometer**
1275# **glance** returns true if any service enabled start with **g-**
1276# **neutron** returns true if any service enabled start with **q-**
1277# **swift** returns true if any service enabled start with **s-**
1278# **trove** returns true if any service enabled start with **tr-**
1279# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1280# **s-** services will be enabled. This will be deprecated in the future.
1281#
1282# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1283# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1284# as enabled in this case.
1285#
1286# Uses global ``ENABLED_SERVICES``
1287# is_service_enabled service [service ...]
1288function is_service_enabled() {
Sean Dague45917cc2014-02-24 16:09:14 -05001289 local xtrace=$(set +o | grep xtrace)
1290 set +o xtrace
1291 local enabled=1
Dean Troyerdff49a22014-01-30 15:37:40 -06001292 services=$@
1293 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001294 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001295
1296 # Look for top-level 'enabled' function for this service
1297 if type is_${service}_enabled >/dev/null 2>&1; then
1298 # A function exists for this service, use it
1299 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001300 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001301 fi
1302
1303 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1304 # are implemented
1305
Sean Dague45917cc2014-02-24 16:09:14 -05001306 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
1307 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1308 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1309 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1310 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1311 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1312 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1313 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1314 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1315 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001316 done
Sean Dague45917cc2014-02-24 16:09:14 -05001317 $xtrace
1318 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001319}
1320
1321# Toggle enable/disable_service for services that must run exclusive of each other
1322# $1 The name of a variable containing a space-separated list of services
1323# $2 The name of a variable in which to store the enabled service's name
1324# $3 The name of the service to enable
1325function use_exclusive_service {
1326 local options=${!1}
1327 local selection=$3
1328 out=$2
1329 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1330 for opt in $options;do
1331 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1332 done
1333 eval "$out=$selection"
1334 return 0
1335}
1336
1337
1338# System Function
1339# ===============
1340
1341# Only run the command if the target file (the last arg) is not on an
1342# NFS filesystem.
1343function _safe_permission_operation() {
Sean Dague45917cc2014-02-24 16:09:14 -05001344 local xtrace=$(set +o | grep xtrace)
1345 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001346 local args=( $@ )
1347 local last
1348 local sudo_cmd
1349 local dir_to_check
1350
1351 let last="${#args[*]} - 1"
1352
1353 dir_to_check=${args[$last]}
1354 if [ ! -d "$dir_to_check" ]; then
1355 dir_to_check=`dirname "$dir_to_check"`
1356 fi
1357
1358 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001359 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001360 return 0
1361 fi
1362
1363 if [[ $TRACK_DEPENDS = True ]]; then
1364 sudo_cmd="env"
1365 else
1366 sudo_cmd="sudo"
1367 fi
1368
Sean Dague45917cc2014-02-24 16:09:14 -05001369 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001370 $sudo_cmd $@
1371}
1372
1373# Exit 0 if address is in network or 1 if address is not in network
1374# ip-range is in CIDR notation: 1.2.3.4/20
1375# address_in_net ip-address ip-range
1376function address_in_net() {
1377 local ip=$1
1378 local range=$2
1379 local masklen=${range#*/}
1380 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1381 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1382 [[ $network == $subnet ]]
1383}
1384
1385# Add a user to a group.
1386# add_user_to_group user group
1387function add_user_to_group() {
1388 local user=$1
1389 local group=$2
1390
1391 if [[ -z "$os_VENDOR" ]]; then
1392 GetOSVersion
1393 fi
1394
1395 # SLE11 and openSUSE 12.2 don't have the usual usermod
1396 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1397 sudo usermod -a -G "$group" "$user"
1398 else
1399 sudo usermod -A "$group" "$user"
1400 fi
1401}
1402
1403# Convert CIDR notation to a IPv4 netmask
1404# cidr2netmask cidr-bits
1405function cidr2netmask() {
1406 local maskpat="255 255 255 255"
1407 local maskdgt="254 252 248 240 224 192 128"
1408 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1409 echo ${1-0}.${2-0}.${3-0}.${4-0}
1410}
1411
1412# Gracefully cp only if source file/dir exists
1413# cp_it source destination
1414function cp_it {
1415 if [ -e $1 ] || [ -d $1 ]; then
1416 cp -pRL $1 $2
1417 fi
1418}
1419
1420# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1421# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1422# ``localrc`` or on the command line if necessary::
1423#
1424# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1425#
1426# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1427
1428function export_proxy_variables() {
1429 if [[ -n "$http_proxy" ]]; then
1430 export http_proxy=$http_proxy
1431 fi
1432 if [[ -n "$https_proxy" ]]; then
1433 export https_proxy=$https_proxy
1434 fi
1435 if [[ -n "$no_proxy" ]]; then
1436 export no_proxy=$no_proxy
1437 fi
1438}
1439
1440# Returns true if the directory is on a filesystem mounted via NFS.
1441function is_nfs_directory() {
1442 local mount_type=`stat -f -L -c %T $1`
1443 test "$mount_type" == "nfs"
1444}
1445
1446# Return the network portion of the given IP address using netmask
1447# netmask is in the traditional dotted-quad format
1448# maskip ip-address netmask
1449function maskip() {
1450 local ip=$1
1451 local mask=$2
1452 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1453 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1454 echo $subnet
1455}
1456
1457# Service wrapper to restart services
1458# restart_service service-name
1459function restart_service() {
1460 if is_ubuntu; then
1461 sudo /usr/sbin/service $1 restart
1462 else
1463 sudo /sbin/service $1 restart
1464 fi
1465}
1466
1467# Only change permissions of a file or directory if it is not on an
1468# NFS filesystem.
1469function safe_chmod() {
1470 _safe_permission_operation chmod $@
1471}
1472
1473# Only change ownership of a file or directory if it is not on an NFS
1474# filesystem.
1475function safe_chown() {
1476 _safe_permission_operation chown $@
1477}
1478
1479# Service wrapper to start services
1480# start_service service-name
1481function start_service() {
1482 if is_ubuntu; then
1483 sudo /usr/sbin/service $1 start
1484 else
1485 sudo /sbin/service $1 start
1486 fi
1487}
1488
1489# Service wrapper to stop services
1490# stop_service service-name
1491function stop_service() {
1492 if is_ubuntu; then
1493 sudo /usr/sbin/service $1 stop
1494 else
1495 sudo /sbin/service $1 stop
1496 fi
1497}
1498
1499
1500# Restore xtrace
1501$XTRACE
1502
1503# Local variables:
1504# mode: shell-script
1505# End: