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