blob: 5b29fd3bf4d6a72378cb7df71c1d89f7ef1a5d79 [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
Masayuki Igawaf6368d32014-02-20 13:31:26 +090018# - System Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060019#
20# The following variables are assumed to be defined by certain functions:
21#
22# - ``ENABLED_SERVICES``
23# - ``ERROR_ON_CLONE``
24# - ``FILES``
25# - ``OFFLINE``
26# - ``PIP_DOWNLOAD_CACHE``
27# - ``PIP_USE_MIRRORS``
28# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090029# - ``REQUIREMENTS_DIR``
30# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060031# - ``TRACK_DEPENDS``
Masayuki Igawad20f6322014-02-28 09:22:37 +090032# - ``UNDO_REQUIREMENTS``
Dean Troyerdff49a22014-01-30 15:37:40 -060033# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050034#
Dean Troyerdff49a22014-01-30 15:37:40 -060035
36# Save trace setting
37XTRACE=$(set +o | grep xtrace)
38set +o xtrace
39
Sean Daguecc524062014-10-01 09:06:43 -040040# Global Config Variables
41declare -A GITREPO
42declare -A GITBRANCH
43declare -A GITDIR
44
Dean Troyerdff49a22014-01-30 15:37:40 -060045
46# Config Functions
47# ================
48
49# Append a new option in an ini file without replacing the old value
50# iniadd config-file section option value1 value2 value3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +110051function iniadd {
Sean Dague45917cc2014-02-24 16:09:14 -050052 local xtrace=$(set +o | grep xtrace)
53 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060054 local file=$1
55 local section=$2
56 local option=$3
57 shift 3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050058
Dean Troyerdff49a22014-01-30 15:37:40 -060059 local values="$(iniget_multiline $file $section $option) $@"
60 iniset_multiline $file $section $option $values
Sean Dague45917cc2014-02-24 16:09:14 -050061 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060062}
63
64# Comment an option in an INI file
65# inicomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110066function inicomment {
Sean Dague45917cc2014-02-24 16:09:14 -050067 local xtrace=$(set +o | grep xtrace)
68 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060069 local file=$1
70 local section=$2
71 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050072
Dean Troyerdff49a22014-01-30 15:37:40 -060073 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -050074 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060075}
76
77# Get an option from an INI file
78# iniget config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110079function iniget {
Sean Dague45917cc2014-02-24 16:09:14 -050080 local xtrace=$(set +o | grep xtrace)
81 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060082 local file=$1
83 local section=$2
84 local option=$3
85 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050086
Dean Troyerdff49a22014-01-30 15:37:40 -060087 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
88 echo ${line#*=}
Sean Dague45917cc2014-02-24 16:09:14 -050089 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060090}
91
92# Get a multiple line option from an INI file
93# iniget_multiline config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110094function iniget_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -050095 local xtrace=$(set +o | grep xtrace)
96 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060097 local file=$1
98 local section=$2
99 local option=$3
100 local values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500101
Dean Troyerdff49a22014-01-30 15:37:40 -0600102 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
103 echo ${values}
Sean Dague45917cc2014-02-24 16:09:14 -0500104 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600105}
106
107# Determinate is the given option present in the INI file
108# ini_has_option config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100109function ini_has_option {
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 line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500116
Dean Troyerdff49a22014-01-30 15:37:40 -0600117 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Sean Dague45917cc2014-02-24 16:09:14 -0500118 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600119 [ -n "$line" ]
120}
121
122# Set an option in an INI file
123# iniset config-file section option value
Ian Wienandaee18c72014-02-21 15:35:08 +1100124function iniset {
Sean Dague45917cc2014-02-24 16:09:14 -0500125 local xtrace=$(set +o | grep xtrace)
126 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600127 local file=$1
128 local section=$2
129 local option=$3
130 local value=$4
131
132 [[ -z $section || -z $option ]] && return
133
134 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
135 # Add section at the end
136 echo -e "\n[$section]" >>"$file"
137 fi
138 if ! ini_has_option "$file" "$section" "$option"; then
139 # Add it
140 sed -i -e "/^\[$section\]/ a\\
141$option = $value
142" "$file"
143 else
144 local sep=$(echo -ne "\x01")
145 # Replace it
146 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
147 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500148 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600149}
150
151# Set a multiple line option in an INI file
152# iniset_multiline config-file section option value1 value2 valu3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +1100153function iniset_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -0500154 local xtrace=$(set +o | grep xtrace)
155 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600156 local file=$1
157 local section=$2
158 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500159
Dean Troyerdff49a22014-01-30 15:37:40 -0600160 shift 3
161 local values
162 for v in $@; do
163 # The later sed command inserts each new value in the line next to
164 # the section identifier, which causes the values to be inserted in
165 # the reverse order. Do a reverse here to keep the original order.
166 values="$v ${values}"
167 done
168 if ! grep -q "^\[$section\]" "$file"; then
169 # Add section at the end
170 echo -e "\n[$section]" >>"$file"
171 else
172 # Remove old values
173 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
174 fi
175 # Add new ones
176 for v in $values; do
177 sed -i -e "/^\[$section\]/ a\\
178$option = $v
179" "$file"
180 done
Sean Dague45917cc2014-02-24 16:09:14 -0500181 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600182}
183
184# Uncomment an option in an INI file
185# iniuncomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100186function iniuncomment {
Sean Dague45917cc2014-02-24 16:09:14 -0500187 local xtrace=$(set +o | grep xtrace)
188 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600189 local file=$1
190 local section=$2
191 local option=$3
192 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500193 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600194}
195
196# Normalize config values to True or False
197# Accepts as False: 0 no No NO false False FALSE
198# Accepts as True: 1 yes Yes YES true True TRUE
199# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100200function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500201 local xtrace=$(set +o | grep xtrace)
202 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600203 local default=$1
204 local testval=$2
205
206 [[ -z "$testval" ]] && { echo "$default"; return; }
207 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
208 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
209 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500210 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600211}
212
213
214# Control Functions
215# =================
216
217# Prints backtrace info
218# filename:lineno:function
219# backtrace level
220function backtrace {
221 local level=$1
222 local deep=$((${#BASH_SOURCE[@]} - 1))
223 echo "[Call Trace]"
224 while [ $level -le $deep ]; do
225 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
226 deep=$((deep - 1))
227 done
228}
229
230# Prints line number and "message" then exits
231# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100232function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600233 local exitcode=$?
234 set +o xtrace
235 local line=$1; shift
236 if [ $exitcode == 0 ]; then
237 exitcode=1
238 fi
239 backtrace 2
240 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600241 # Give buffers a second to flush
242 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600243 exit $exitcode
244}
245
246# Checks an environment variable is not set or has length 0 OR if the
247# exit code is non-zero and prints "message" and exits
248# NOTE: env-var is the variable name without a '$'
249# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100250function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600251 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500252 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600253 set +o xtrace
254 local line=$1; shift
255 local evar=$1; shift
256 if ! is_set $evar || [ $exitcode != 0 ]; then
257 die $line "$*"
258 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500259 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600260}
261
262# Prints line number and "message" in error format
263# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100264function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600265 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500266 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600267 set +o xtrace
268 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
269 echo $msg 1>&2;
270 if [[ -n ${SCREEN_LOGDIR} ]]; then
271 echo $msg >> "${SCREEN_LOGDIR}/error.log"
272 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500273 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600274 return $exitcode
275}
276
277# Checks an environment variable is not set or has length 0 OR if the
278# exit code is non-zero and prints "message"
279# NOTE: env-var is the variable name without a '$'
280# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100281function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600282 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500283 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600284 set +o xtrace
285 local line=$1; shift
286 local evar=$1; shift
287 if ! is_set $evar || [ $exitcode != 0 ]; then
288 err $line "$*"
289 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500290 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600291 return $exitcode
292}
293
294# Exit after outputting a message about the distribution not being supported.
295# exit_distro_not_supported [optional-string-telling-what-is-missing]
296function exit_distro_not_supported {
297 if [[ -z "$DISTRO" ]]; then
298 GetDistro
299 fi
300
301 if [ $# -gt 0 ]; then
302 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
303 else
304 die $LINENO "Support for $DISTRO is incomplete."
305 fi
306}
307
308# Test if the named environment variable is set and not zero length
309# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100310function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600311 local var=\$"$1"
312 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
313}
314
315# Prints line number and "message" in warning format
316# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100317function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600318 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500319 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600320 set +o xtrace
321 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
322 echo $msg 1>&2;
323 if [[ -n ${SCREEN_LOGDIR} ]]; then
324 echo $msg >> "${SCREEN_LOGDIR}/error.log"
325 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500326 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600327 return $exitcode
328}
329
330
331# Distro Functions
332# ================
333
334# Determine OS Vendor, Release and Update
335# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
336# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500337# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
338# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
339# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
340# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
341# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
342declare os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
343
Dean Troyerdff49a22014-01-30 15:37:40 -0600344# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100345function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500346
Dean Troyerdff49a22014-01-30 15:37:40 -0600347 # Figure out which vendor we are
348 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
349 # OS/X
350 os_VENDOR=`sw_vers -productName`
351 os_RELEASE=`sw_vers -productVersion`
352 os_UPDATE=${os_RELEASE##*.}
353 os_RELEASE=${os_RELEASE%.*}
354 os_PACKAGE=""
355 if [[ "$os_RELEASE" =~ "10.7" ]]; then
356 os_CODENAME="lion"
357 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
358 os_CODENAME="snow leopard"
359 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
360 os_CODENAME="leopard"
361 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
362 os_CODENAME="tiger"
363 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
364 os_CODENAME="panther"
365 else
366 os_CODENAME=""
367 fi
368 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
369 os_VENDOR=$(lsb_release -i -s)
370 os_RELEASE=$(lsb_release -r -s)
371 os_UPDATE=""
372 os_PACKAGE="rpm"
373 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
374 os_PACKAGE="deb"
375 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
376 lsb_release -d -s | grep -q openSUSE
377 if [[ $? -eq 0 ]]; then
378 os_VENDOR="openSUSE"
379 fi
380 elif [[ $os_VENDOR == "openSUSE project" ]]; then
381 os_VENDOR="openSUSE"
382 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
383 os_VENDOR="Red Hat"
384 fi
385 os_CODENAME=$(lsb_release -c -s)
386 elif [[ -r /etc/redhat-release ]]; then
387 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
388 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
389 # CentOS release 5.5 (Final)
390 # CentOS Linux release 6.0 (Final)
391 # Fedora release 16 (Verne)
392 # XenServer release 6.2.0-70446c (xenenterprise)
393 os_CODENAME=""
394 for r in "Red Hat" CentOS Fedora XenServer; do
395 os_VENDOR=$r
396 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
397 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
398 os_CODENAME=${ver#*|}
399 os_RELEASE=${ver%|*}
400 os_UPDATE=${os_RELEASE##*.}
401 os_RELEASE=${os_RELEASE%.*}
402 break
403 fi
404 os_VENDOR=""
405 done
406 os_PACKAGE="rpm"
407 elif [[ -r /etc/SuSE-release ]]; then
408 for r in openSUSE "SUSE Linux"; do
409 if [[ "$r" = "SUSE Linux" ]]; then
410 os_VENDOR="SUSE LINUX"
411 else
412 os_VENDOR=$r
413 fi
414
415 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
416 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
417 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
418 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
419 break
420 fi
421 os_VENDOR=""
422 done
423 os_PACKAGE="rpm"
424 # If lsb_release is not installed, we should be able to detect Debian OS
425 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
426 os_VENDOR="Debian"
427 os_PACKAGE="deb"
428 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
429 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
430 fi
431 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
432}
433
434# Translate the OS version values into common nomenclature
435# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500436declare DISTRO
437
Ian Wienandaee18c72014-02-21 15:35:08 +1100438function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600439 GetOSVersion
440 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
441 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
442 DISTRO=$os_CODENAME
443 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
444 # For Fedora, just use 'f' and the release
445 DISTRO="f$os_RELEASE"
446 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
447 DISTRO="opensuse-$os_RELEASE"
448 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
449 # For SLE, also use the service pack
450 if [[ -z "$os_UPDATE" ]]; then
451 DISTRO="sle${os_RELEASE}"
452 else
453 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
454 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530455 elif [[ "$os_VENDOR" =~ (Red Hat) || \
456 "$os_VENDOR" =~ (CentOS) || \
457 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600458 # Drop the . release as we assume it's compatible
459 DISTRO="rhel${os_RELEASE::1}"
460 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
461 DISTRO="xs$os_RELEASE"
462 else
463 # Catch-all for now is Vendor + Release + Update
464 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
465 fi
466 export DISTRO
467}
468
469# Utility function for checking machine architecture
470# is_arch arch-type
471function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500472 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600473}
474
Ian Wienandbdc90c52014-08-04 15:44:58 +1000475# Quick check for a rackspace host; n.b. rackspace provided images
476# have these Xen tools installed but a custom image may not.
477function is_rackspace {
478 [ -f /usr/bin/xenstore-ls ] && \
479 sudo /usr/bin/xenstore-ls vm-data | grep -q "Rackspace"
480}
481
Dean Troyerdff49a22014-01-30 15:37:40 -0600482# Determine if current distribution is a Fedora-based distribution
483# (Fedora, RHEL, CentOS, etc).
484# is_fedora
485function is_fedora {
486 if [[ -z "$os_VENDOR" ]]; then
487 GetOSVersion
488 fi
489
anju Tiwari6c639c92014-07-15 18:11:54 +0530490 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
491 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600492}
493
494
495# Determine if current distribution is a SUSE-based distribution
496# (openSUSE, SLE).
497# is_suse
498function is_suse {
499 if [[ -z "$os_VENDOR" ]]; then
500 GetOSVersion
501 fi
502
503 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
504}
505
506
507# Determine if current distribution is an Ubuntu-based distribution
508# It will also detect non-Ubuntu but Debian-based distros
509# is_ubuntu
510function is_ubuntu {
511 if [[ -z "$os_PACKAGE" ]]; then
512 GetOSVersion
513 fi
514 [ "$os_PACKAGE" = "deb" ]
515}
516
517
518# Git Functions
519# =============
520
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600521# Returns openstack release name for a given branch name
522# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100523function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600524 local branch=$1
Dean Troyer50cda692014-07-25 11:57:20 -0500525
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600526 if [[ $branch =~ "stable/" ]]; then
527 echo ${branch#*/}
528 else
529 echo "master"
530 fi
531}
532
Dean Troyerdff49a22014-01-30 15:37:40 -0600533# git clone only if directory doesn't exist already. Since ``DEST`` might not
534# be owned by the installation user, we create the directory and change the
535# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500536# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
537# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600538# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer50cda692014-07-25 11:57:20 -0500539# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600540# git_clone remote dest-dir branch
541function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500542 local git_remote=$1
543 local git_dest=$2
544 local git_ref=$3
545 local orig_dir=$(pwd)
546
Dean Troyerdff49a22014-01-30 15:37:40 -0600547 RECLONE=$(trueorfalse False $RECLONE)
548
549 if [[ "$OFFLINE" = "True" ]]; then
550 echo "Running in offline mode, clones already exist"
551 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500552 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600553 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400554 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600555 return
556 fi
557
Dean Troyer50cda692014-07-25 11:57:20 -0500558 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600559 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500560 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600561 [[ "$ERROR_ON_CLONE" = "True" ]] && \
562 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer50cda692014-07-25 11:57:20 -0500563 git_timed clone $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600564 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500565 cd $git_dest
566 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600567 else
568 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500569 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600570 [[ "$ERROR_ON_CLONE" = "True" ]] && \
571 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer50cda692014-07-25 11:57:20 -0500572 git_timed clone $git_remote $git_dest
573 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600574 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500575 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600576 elif [[ "$RECLONE" = "True" ]]; then
577 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500578 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600579 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500580 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100581 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600582 # remove the existing ignored files (like pyc) as they cause breakage
583 # (due to the py files having older timestamps than our pyc, so python
584 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500585 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600586
Dean Troyer50cda692014-07-25 11:57:20 -0500587 # handle git_ref accordingly to type (tag, branch)
588 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
589 git_update_tag $git_ref
590 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
591 git_update_branch $git_ref
592 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
593 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600594 else
Dean Troyer50cda692014-07-25 11:57:20 -0500595 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600596 fi
597
598 fi
599 fi
600
601 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500602 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600603 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400604 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600605}
606
Sean Daguecc524062014-10-01 09:06:43 -0400607# A variation on git clone that lets us specify a project by it's
608# actual name, like oslo.config. This is exceptionally useful in the
609# library installation case
610function git_clone_by_name {
611 local name=$1
612 local repo=${GITREPO[$name]}
613 local dir=${GITDIR[$name]}
614 local branch=${GITBRANCH[$name]}
615 git_clone $repo $dir $branch
616}
617
618
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100619# git can sometimes get itself infinitely stuck with transient network
620# errors or other issues with the remote end. This wraps git in a
621# timeout/retry loop and is intended to watch over non-local git
622# processes that might hang. GIT_TIMEOUT, if set, is passed directly
623# to timeout(1); otherwise the default value of 0 maintains the status
624# quo of waiting forever.
625# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100626function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100627 local count=0
628 local timeout=0
629
630 if [[ -n "${GIT_TIMEOUT}" ]]; then
631 timeout=${GIT_TIMEOUT}
632 fi
633
634 until timeout -s SIGINT ${timeout} git "$@"; do
635 # 124 is timeout(1)'s special return code when it reached the
636 # timeout; otherwise assume fatal failure
637 if [[ $? -ne 124 ]]; then
638 die $LINENO "git call failed: [git $@]"
639 fi
640
641 count=$(($count + 1))
642 warn "timeout ${count} for git call: [git $@]"
643 if [ $count -eq 3 ]; then
644 die $LINENO "Maximum of 3 git retries reached"
645 fi
646 sleep 5
647 done
648}
649
Dean Troyerdff49a22014-01-30 15:37:40 -0600650# git update using reference as a branch.
651# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100652function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500653 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600654
Dean Troyer50cda692014-07-25 11:57:20 -0500655 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600656 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500657 git branch -D $git_branch || true
658 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600659}
660
661# git update using reference as a branch.
662# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100663function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500664 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600665
Dean Troyer50cda692014-07-25 11:57:20 -0500666 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600667}
668
669# git update using reference as a tag. Be careful editing source at that repo
670# as working copy will be in a detached mode
671# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100672function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500673 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600674
Dean Troyer50cda692014-07-25 11:57:20 -0500675 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600676 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500677 git_timed fetch origin tag $git_tag
678 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600679}
680
681
682# OpenStack Functions
683# ===================
684
685# Get the default value for HOST_IP
686# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100687function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600688 local fixed_range=$1
689 local floating_range=$2
690 local host_ip_iface=$3
691 local host_ip=$4
692
693 # Find the interface used for the default route
694 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
695 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
696 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
697 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500698 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
699 local ip
700 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600701 # Attempt to filter out IP addresses that are part of the fixed and
702 # floating range. Note that this method only works if the ``netaddr``
703 # python library is installed. If it is not installed, an error
704 # will be printed and the first IP from the interface will be used.
705 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
706 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500707 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
708 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600709 break;
710 fi
711 done
712 fi
713 echo $host_ip
714}
715
Attila Fazekasf71b5002014-05-28 09:52:22 +0200716# Generates hex string from ``size`` byte of pseudo random data
717# generate_hex_string size
718function generate_hex_string {
719 local size=$1
720 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
721}
722
Dean Troyerdff49a22014-01-30 15:37:40 -0600723# Grab a numbered field from python prettytable output
724# Fields are numbered starting with 1
725# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
726# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100727function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500728 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600729 while read data; do
730 if [ "$1" -lt 0 ]; then
731 field="(\$(NF$1))"
732 else
733 field="\$$(($1 + 1))"
734 fi
735 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
736 done
737}
738
739# Add a policy to a policy.json file
740# Do nothing if the policy already exists
741# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100742function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600743 local policy_file=$1
744 local policy_name=$2
745 local policy_perm=$3
746
747 if grep -q ${policy_name} ${policy_file}; then
748 echo "Policy ${policy_name} already exists in ${policy_file}"
749 return
750 fi
751
752 # Add a terminating comma to policy lines without one
753 # Remove the closing '}' and all lines following to the end-of-file
754 local tmpfile=$(mktemp)
755 uniq ${policy_file} | sed -e '
756 s/]$/],/
757 /^[}]/,$d
758 ' > ${tmpfile}
759
760 # Append policy and closing brace
761 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
762 echo "}" >>${tmpfile}
763
764 mv ${tmpfile} ${policy_file}
765}
766
Bartosz Górski0abde392014-02-28 14:15:19 +0100767# Gets or creates user
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200768# Usage: get_or_create_user <username> <password> <project> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100769function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200770 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500771 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200772 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500773 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200774 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100775 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500776 local user_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100777 # Gets user id
778 openstack user show $1 -f value -c id 2>/dev/null ||
779 # Creates new user
780 openstack user create \
781 $1 \
782 --password "$2" \
783 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500784 $email \
Bartosz Górski0abde392014-02-28 14:15:19 +0100785 -f value -c id
786 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500787 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100788}
789
790# Gets or creates project
791# Usage: get_or_create_project <name>
792function get_or_create_project {
793 # Gets project id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500794 local project_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100795 # Gets project id
796 openstack project show $1 -f value -c id 2>/dev/null ||
797 # Creates new project if not exists
798 openstack project create $1 -f value -c id
799 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500800 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100801}
802
803# Gets or creates role
804# Usage: get_or_create_role <name>
805function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500806 local role_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100807 # Gets role id
808 openstack role show $1 -f value -c id 2>/dev/null ||
809 # Creates role if not exists
810 openstack role create $1 -f value -c id
811 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500812 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100813}
814
815# Gets or adds user role
816# Usage: get_or_add_user_role <role> <user> <project>
817function get_or_add_user_role {
818 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500819 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100820 $2 \
821 --project $3 \
822 --column "ID" \
823 --column "Name" \
824 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500825 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100826 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500827 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100828 $1 \
829 --user $2 \
830 --project $3 \
831 | grep " id " | get_field 2)
832 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500833 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100834}
835
836# Gets or creates service
837# Usage: get_or_create_service <name> <type> <description>
838function get_or_create_service {
839 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500840 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100841 # Gets service id
842 openstack service show $1 -f value -c id 2>/dev/null ||
843 # Creates new service if not exists
844 openstack service create \
845 $1 \
846 --type=$2 \
847 --description="$3" \
848 -f value -c id
849 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500850 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100851}
852
853# Gets or creates endpoint
854# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
855function get_or_create_endpoint {
856 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500857 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100858 --column "ID" \
859 --column "Region" \
860 --column "Service Name" \
861 | grep " $2 " \
862 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500863 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100864 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500865 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100866 $1 \
867 --region $2 \
868 --publicurl $3 \
869 --adminurl $4 \
870 --internalurl $5 \
871 | grep " id " | get_field 2)
872 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500873 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100874}
Dean Troyerdff49a22014-01-30 15:37:40 -0600875
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500876
Dean Troyerdff49a22014-01-30 15:37:40 -0600877# Package Functions
878# =================
879
880# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100881function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600882 local pkg_dir
883 if is_ubuntu; then
884 pkg_dir=$FILES/apts
885 elif is_fedora; then
886 pkg_dir=$FILES/rpms
887 elif is_suse; then
888 pkg_dir=$FILES/rpms-suse
889 else
890 exit_distro_not_supported "list of packages"
891 fi
892 echo "$pkg_dir"
893}
894
895# Wrapper for ``apt-get`` to set cache and proxy environment variables
896# Uses globals ``OFFLINE``, ``*_proxy``
897# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100898function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500899 local xtrace=$(set +o | grep xtrace)
900 set +o xtrace
901
Dean Troyerdff49a22014-01-30 15:37:40 -0600902 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
903 local sudo="sudo"
904 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500905
906 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600907 $sudo DEBIAN_FRONTEND=noninteractive \
908 http_proxy=$http_proxy https_proxy=$https_proxy \
909 no_proxy=$no_proxy \
910 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
911}
912
913# get_packages() collects a list of package names of any type from the
914# prerequisite files in ``files/{apts|rpms}``. The list is intended
915# to be passed to a package installer such as apt or yum.
916#
917# Only packages required for the services in 1st argument will be
918# included. Two bits of metadata are recognized in the prerequisite files:
919#
920# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
921# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
922# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100923function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500924 local xtrace=$(set +o | grep xtrace)
925 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600926 local services=$@
927 local package_dir=$(_get_package_dir)
928 local file_to_parse
929 local service
930
931 if [[ -z "$package_dir" ]]; then
932 echo "No package directory supplied"
933 return 1
934 fi
935 if [[ -z "$DISTRO" ]]; then
936 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -0500937 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -0600938 fi
939 for service in ${services//,/ }; do
940 # Allow individual services to specify dependencies
941 if [[ -e ${package_dir}/${service} ]]; then
942 file_to_parse="${file_to_parse} $service"
943 fi
944 # NOTE(sdague) n-api needs glance for now because that's where
945 # glance client is
946 if [[ $service == n-api ]]; then
947 if [[ ! $file_to_parse =~ nova ]]; then
948 file_to_parse="${file_to_parse} nova"
949 fi
950 if [[ ! $file_to_parse =~ glance ]]; then
951 file_to_parse="${file_to_parse} glance"
952 fi
953 elif [[ $service == c-* ]]; then
954 if [[ ! $file_to_parse =~ cinder ]]; then
955 file_to_parse="${file_to_parse} cinder"
956 fi
957 elif [[ $service == ceilometer-* ]]; then
958 if [[ ! $file_to_parse =~ ceilometer ]]; then
959 file_to_parse="${file_to_parse} ceilometer"
960 fi
961 elif [[ $service == s-* ]]; then
962 if [[ ! $file_to_parse =~ swift ]]; then
963 file_to_parse="${file_to_parse} swift"
964 fi
965 elif [[ $service == n-* ]]; then
966 if [[ ! $file_to_parse =~ nova ]]; then
967 file_to_parse="${file_to_parse} nova"
968 fi
969 elif [[ $service == g-* ]]; then
970 if [[ ! $file_to_parse =~ glance ]]; then
971 file_to_parse="${file_to_parse} glance"
972 fi
973 elif [[ $service == key* ]]; then
974 if [[ ! $file_to_parse =~ keystone ]]; then
975 file_to_parse="${file_to_parse} keystone"
976 fi
977 elif [[ $service == q-* ]]; then
978 if [[ ! $file_to_parse =~ neutron ]]; then
979 file_to_parse="${file_to_parse} neutron"
980 fi
Adam Gandelman539ec432014-03-18 18:57:43 -0700981 elif [[ $service == ir-* ]]; then
982 if [[ ! $file_to_parse =~ ironic ]]; then
983 file_to_parse="${file_to_parse} ironic"
984 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600985 fi
986 done
987
988 for file in ${file_to_parse}; do
989 local fname=${package_dir}/${file}
990 local OIFS line package distros distro
991 [[ -e $fname ]] || continue
992
993 OIFS=$IFS
994 IFS=$'\n'
995 for line in $(<${fname}); do
996 if [[ $line =~ "NOPRIME" ]]; then
997 continue
998 fi
999
1000 # Assume we want this package
1001 package=${line%#*}
1002 inst_pkg=1
1003
1004 # Look for # dist:xxx in comment
1005 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1006 # We are using BASH regexp matching feature.
1007 package=${BASH_REMATCH[1]}
1008 distros=${BASH_REMATCH[2]}
1009 # In bash ${VAR,,} will lowecase VAR
1010 # Look for a match in the distro list
1011 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1012 # If no match then skip this package
1013 inst_pkg=0
1014 fi
1015 fi
1016
1017 # Look for # testonly in comment
1018 if [[ $line =~ (.*)#.*testonly.* ]]; then
1019 package=${BASH_REMATCH[1]}
1020 # Are we installing test packages? (test for the default value)
1021 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1022 # If not installing test packages the skip this package
1023 inst_pkg=0
1024 fi
1025 fi
1026
1027 if [[ $inst_pkg = 1 ]]; then
1028 echo $package
1029 fi
1030 done
1031 IFS=$OIFS
1032 done
Sean Dague45917cc2014-02-24 16:09:14 -05001033 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001034}
1035
1036# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001037# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001038# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001039function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001040 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001041 return 0
1042 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001043
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001044 if is_ubuntu; then
1045 local xtrace=$(set +o | grep xtrace)
1046 set +o xtrace
1047 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1048 # if there are transient errors pulling the updates, that's fine.
1049 # It may be secondary repositories that we don't really care about.
1050 apt_get update || /bin/true
1051 REPOS_UPDATED=True
1052 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001053 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001054 fi
1055}
1056
1057function real_install_package {
1058 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001059 apt_get install "$@"
1060 elif is_fedora; then
1061 yum_install "$@"
1062 elif is_suse; then
1063 zypper_install "$@"
1064 else
1065 exit_distro_not_supported "installing packages"
1066 fi
1067}
1068
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001069# Distro-agnostic package installer
1070# install_package package [package ...]
1071function install_package {
1072 update_package_repo
1073 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1074}
1075
Dean Troyerdff49a22014-01-30 15:37:40 -06001076# Distro-agnostic function to tell if a package is installed
1077# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001078function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001079 if [[ -z "$@" ]]; then
1080 return 1
1081 fi
1082
1083 if [[ -z "$os_PACKAGE" ]]; then
1084 GetOSVersion
1085 fi
1086
1087 if [[ "$os_PACKAGE" = "deb" ]]; then
1088 dpkg -s "$@" > /dev/null 2> /dev/null
1089 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1090 rpm --quiet -q "$@"
1091 else
1092 exit_distro_not_supported "finding if a package is installed"
1093 fi
1094}
1095
1096# Distro-agnostic package uninstaller
1097# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001098function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001099 if is_ubuntu; then
1100 apt_get purge "$@"
1101 elif is_fedora; then
1102 sudo yum remove -y "$@"
1103 elif is_suse; then
1104 sudo zypper rm "$@"
1105 else
1106 exit_distro_not_supported "uninstalling packages"
1107 fi
1108}
1109
1110# Wrapper for ``yum`` to set proxy environment variables
1111# Uses globals ``OFFLINE``, ``*_proxy``
1112# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001113function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001114 [[ "$OFFLINE" = "True" ]] && return
1115 local sudo="sudo"
1116 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001117
1118 # The manual check for missing packages is because yum -y assumes
1119 # missing packages are OK. See
1120 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001121 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1122 no_proxy=$no_proxy \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001123 yum install -y "$@" 2>&1 | \
1124 awk '
1125 BEGIN { fail=0 }
1126 /No package/ { fail=1 }
1127 { print }
1128 END { exit fail }' || \
1129 die $LINENO "Missing packages detected"
1130
1131 # also ensure we catch a yum failure
1132 if [[ ${PIPESTATUS[0]} != 0 ]]; then
1133 die $LINENO "Yum install failure"
1134 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001135}
1136
1137# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001138# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001139# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001140function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001141 [[ "$OFFLINE" = "True" ]] && return
1142 local sudo="sudo"
1143 [[ "$(id -u)" = "0" ]] && sudo="env"
1144 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1145 zypper --non-interactive install --auto-agree-with-licenses "$@"
1146}
1147
1148
1149# Process Functions
1150# =================
1151
1152# _run_process() is designed to be backgrounded by run_process() to simulate a
1153# fork. It includes the dirty work of closing extra filehandles and preparing log
1154# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001155# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001156# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001157# If an optional group is provided sg will be used to set the group of
1158# the command.
1159# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001160function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001161 local service=$1
1162 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001163 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001164
1165 # Undo logging redirections and close the extra descriptors
1166 exec 1>&3
1167 exec 2>&3
1168 exec 3>&-
1169 exec 6>&-
1170
1171 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001172 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1173 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001174
1175 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1176 export PYTHONUNBUFFERED=1
1177 fi
1178
Dean Troyer3159a822014-08-27 14:13:58 -05001179 # Run under ``setsid`` to force the process to become a session and group leader.
1180 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001181 if [[ -n "$group" ]]; then
1182 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1183 else
1184 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1185 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001186
1187 # Just silently exit this process
1188 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001189}
1190
1191# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1192# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001193# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001194# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001195function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001196 SCREEN_NAME=${SCREEN_NAME:-stack}
1197 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1198
1199 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1200 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1201 fi
1202
1203 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1204}
1205
1206# Find out if a process exists by partial name.
1207# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001208function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001209 local name=$1
1210 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001211 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001212 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001213 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001214}
1215
Dean Troyer3159a822014-08-27 14:13:58 -05001216# Run a single service under screen or directly
1217# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001218# If an optional group is provided sg will be used to run the
1219# command as that group.
1220# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001221function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001222 local service=$1
1223 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001224 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001225
Dean Troyer3159a822014-08-27 14:13:58 -05001226 if is_service_enabled $service; then
1227 if [[ "$USE_SCREEN" = "True" ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001228 screen_service "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001229 else
1230 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001231 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001232 fi
1233 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001234}
1235
1236# Helper to launch a service in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001237# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1238# ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001239# screen_service service "command-line" [group]
1240# Run a command in a shell in a screen window, if an optional group
1241# is provided, use sg to set the group of the command.
Dean Troyer3159a822014-08-27 14:13:58 -05001242function screen_service {
1243 local service=$1
1244 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001245 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001246
Sean Dagueea22a4f2014-06-27 15:21:41 -04001247 SCREEN_NAME=${SCREEN_NAME:-stack}
1248 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1249 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001250
Dean Troyer3159a822014-08-27 14:13:58 -05001251 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001252 # Append the service to the screen rc file
Dean Troyer3159a822014-08-27 14:13:58 -05001253 screen_rc "$service" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001254
Dean Troyer3159a822014-08-27 14:13:58 -05001255 screen -S $SCREEN_NAME -X screen -t $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001256
Dean Troyer3159a822014-08-27 14:13:58 -05001257 if [[ -n ${SCREEN_LOGDIR} ]]; then
1258 screen -S $SCREEN_NAME -p $service -X logfile ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log
1259 screen -S $SCREEN_NAME -p $service -X log on
1260 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001261 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001262
1263 # sleep to allow bash to be ready to be send the command - we are
1264 # creating a new window in screen and then sends characters, so if
1265 # bash isn't running by the time we send the command, nothing happens
1266 sleep 3
1267
1268 NL=`echo -ne '\015'`
1269 # This fun command does the following:
1270 # - the passed server command is backgrounded
1271 # - the pid of the background process is saved in the usual place
1272 # - the server process is brought back to the foreground
1273 # - if the server process exits prematurely the fg command errors
Dean Troyer3324f192014-09-18 09:26:39 -05001274 # and a message is written to stdout and the service failure file
1275 #
Chris Dent2f27a0e2014-09-09 13:46:02 +01001276 # The pid saved can be used in stop_process() as a process group
Dean Troyer3159a822014-08-27 14:13:58 -05001277 # id to kill off all child processes
Chris Dent2f27a0e2014-09-09 13:46:02 +01001278 if [[ -n "$group" ]]; then
1279 command="sg $group '$command'"
1280 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001281 screen -S $SCREEN_NAME -p $service -X stuff "$command & echo \$! >$SERVICE_DIR/$SCREEN_NAME/${service}.pid; fg || echo \"$service failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/${service}.failure\"$NL"
Dean Troyerdff49a22014-01-30 15:37:40 -06001282 fi
1283}
1284
1285# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001286# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001287# screen_rc service "command-line"
1288function screen_rc {
1289 SCREEN_NAME=${SCREEN_NAME:-stack}
1290 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1291 if [[ ! -e $SCREENRC ]]; then
1292 # Name the screen session
1293 echo "sessionname $SCREEN_NAME" > $SCREENRC
1294 # Set a reasonable statusbar
1295 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1296 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1297 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1298 echo "screen -t shell bash" >> $SCREENRC
1299 fi
1300 # If this service doesn't already exist in the screenrc file
1301 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1302 NL=`echo -ne '\015'`
1303 echo "screen -t $1 bash" >> $SCREENRC
1304 echo "stuff \"$2$NL\"" >> $SCREENRC
1305
1306 if [[ -n ${SCREEN_LOGDIR} ]]; then
1307 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1308 echo "log on" >>$SCREENRC
1309 fi
1310 fi
1311}
1312
1313# Stop a service in screen
1314# If a PID is available use it, kill the whole process group via TERM
1315# If screen is being used kill the screen window; this will catch processes
1316# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001317# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001318# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001319function screen_stop_service {
1320 local service=$1
1321
Dean Troyerdff49a22014-01-30 15:37:40 -06001322 SCREEN_NAME=${SCREEN_NAME:-stack}
1323 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1324 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1325
Dean Troyer3159a822014-08-27 14:13:58 -05001326 if is_service_enabled $service; then
1327 # Clean up the screen window
1328 screen -S $SCREEN_NAME -p $service -X kill
1329 fi
1330}
1331
1332# Stop a service process
1333# If a PID is available use it, kill the whole process group via TERM
1334# If screen is being used kill the screen window; this will catch processes
1335# that did not leave a PID behind
1336# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1337# stop_process service
1338function stop_process {
1339 local service=$1
1340
1341 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1342 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1343
1344 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001345 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001346 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1347 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1348 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001349 fi
1350 if [[ "$USE_SCREEN" = "True" ]]; then
1351 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001352 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001353 fi
1354 fi
1355}
1356
1357# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001358# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001359# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001360function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001361 local service
1362 local failures
1363 SCREEN_NAME=${SCREEN_NAME:-stack}
1364 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1365
1366
1367 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1368 echo "No service status directory found"
1369 return
1370 fi
1371
1372 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001373 # make this -o errexit safe
1374 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001375
1376 for service in $failures; do
1377 service=`basename $service`
1378 service=${service%.failure}
1379 echo "Error: Service $service is not running"
1380 done
1381
1382 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001383 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001384 fi
1385}
1386
Chris Dent2f27a0e2014-09-09 13:46:02 +01001387# Tail a log file in a screen if USE_SCREEN is true.
1388function tail_log {
1389 local service=$1
1390 local logfile=$2
1391
1392 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1393 if [[ "$USE_SCREEN" = "True" ]]; then
1394 screen_service "$service" "sudo tail -f $logfile"
1395 fi
1396}
1397
Dean Troyerdff49a22014-01-30 15:37:40 -06001398
Dean Troyer3159a822014-08-27 14:13:58 -05001399# Deprecated Functions
1400# --------------------
1401
1402# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1403# fork. It includes the dirty work of closing extra filehandles and preparing log
1404# files to produce the same logs as screen_it(). The log filename is derived
1405# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1406# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1407# _old_run_process service "command-line"
1408function _old_run_process {
1409 local service=$1
1410 local command="$2"
1411
1412 # Undo logging redirections and close the extra descriptors
1413 exec 1>&3
1414 exec 2>&3
1415 exec 3>&-
1416 exec 6>&-
1417
1418 if [[ -n ${SCREEN_LOGDIR} ]]; then
1419 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1420 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1421
1422 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1423 export PYTHONUNBUFFERED=1
1424 fi
1425
1426 exec /bin/bash -c "$command"
1427 die "$service exec failure: $command"
1428}
1429
1430# old_run_process() launches a child process that closes all file descriptors and
1431# then exec's the passed in command. This is meant to duplicate the semantics
1432# of screen_it() without screen. PIDs are written to
1433# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1434# old_run_process service "command-line"
1435function old_run_process {
1436 local service=$1
1437 local command="$2"
1438
1439 # Spawn the child process
1440 _old_run_process "$service" "$command" &
1441 echo $!
1442}
1443
1444# Compatibility for existing start_XXXX() functions
1445# Uses global ``USE_SCREEN``
1446# screen_it service "command-line"
1447function screen_it {
1448 if is_service_enabled $1; then
1449 # Append the service to the screen rc file
1450 screen_rc "$1" "$2"
1451
1452 if [[ "$USE_SCREEN" = "True" ]]; then
1453 screen_service "$1" "$2"
1454 else
1455 # Spawn directly without screen
1456 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1457 fi
1458 fi
1459}
1460
1461# Compatibility for existing stop_XXXX() functions
1462# Stop a service in screen
1463# If a PID is available use it, kill the whole process group via TERM
1464# If screen is being used kill the screen window; this will catch processes
1465# that did not leave a PID behind
1466# screen_stop service
1467function screen_stop {
1468 # Clean up the screen window
1469 stop_process $1
1470}
1471
1472
Dean Troyerdff49a22014-01-30 15:37:40 -06001473# Python Functions
1474# ================
1475
1476# Get the path to the pip command.
1477# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001478function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001479 which pip || which pip-python
1480
1481 if [ $? -ne 0 ]; then
1482 die $LINENO "Unable to find pip; cannot continue"
1483 fi
1484}
1485
1486# Get the path to the direcotry where python executables are installed.
1487# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001488function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001489 if is_fedora || is_suse; then
1490 echo "/usr/bin"
1491 else
1492 echo "/usr/local/bin"
1493 fi
1494}
1495
1496# Wrapper for ``pip install`` to set cache and proxy environment variables
1497# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
1498# ``TRACK_DEPENDS``, ``*_proxy``
1499# pip_install package [package ...]
1500function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001501 local xtrace=$(set +o | grep xtrace)
1502 set +o xtrace
1503 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1504 $xtrace
1505 return
1506 fi
1507
Dean Troyerdff49a22014-01-30 15:37:40 -06001508 if [[ -z "$os_PACKAGE" ]]; then
1509 GetOSVersion
1510 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001511 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1512 # TRACK_DEPENDS=True installation creates a circular dependency when
1513 # we attempt to install virtualenv into a virualenv, so we must global
1514 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001515 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001516 local cmd_pip=$DEST/.venv/bin/pip
1517 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001518 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001519 local cmd_pip=$(get_pip_command)
1520 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001521 fi
1522
1523 # Mirror option not needed anymore because pypi has CDN available,
1524 # but it's useful in certain circumstances
1525 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001526 local pip_mirror_opt=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001527 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001528 pip_mirror_opt="--use-mirrors"
Dean Troyerdff49a22014-01-30 15:37:40 -06001529 fi
1530
Sean Dague45917cc2014-02-24 16:09:14 -05001531 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001532 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001533 http_proxy=$http_proxy \
1534 https_proxy=$https_proxy \
1535 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001536 $cmd_pip install \
1537 $pip_mirror_opt $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001538
1539 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1540 local test_req="$@/test-requirements.txt"
1541 if [[ -e "$test_req" ]]; then
1542 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1543 http_proxy=$http_proxy \
1544 https_proxy=$https_proxy \
1545 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001546 $cmd_pip install \
1547 $pip_mirror_opt -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001548 fi
1549 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001550}
1551
Sean Daguecc524062014-10-01 09:06:43 -04001552# should we use this library from their git repo, or should we let it
1553# get pulled in via pip dependencies.
1554function use_library_from_git {
1555 local name=$1
1556 local enabled=1
1557 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1558 return $enabled
1559}
1560
1561# setup a library by name. If we are trying to use the library from
1562# git, we'll do a git based install, otherwise we'll punt and the
1563# library should be installed by a requirements pull from another
1564# project.
1565function setup_lib {
1566 local name=$1
1567 local dir=${GITDIR[$name]}
1568 setup_install $dir
1569}
1570
1571
Sean Dague099e5e32014-03-31 10:35:43 -04001572# this should be used if you want to install globally, all libraries should
1573# use this, especially *oslo* ones
1574function setup_install {
1575 local project_dir=$1
1576 setup_package_with_req_sync $project_dir
1577}
1578
1579# this should be used for projects which run services, like all services
1580function setup_develop {
1581 local project_dir=$1
1582 setup_package_with_req_sync $project_dir -e
1583}
1584
Dean Troyeraf616d92014-02-17 12:57:55 -06001585# ``pip install -e`` the package, which processes the dependencies
1586# using pip before running `setup.py develop`
1587#
1588# Updates the dependencies in project_dir from the
1589# openstack/requirements global list before installing anything.
1590#
1591# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1592# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001593function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001594 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001595 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001596
Dean Troyeraf616d92014-02-17 12:57:55 -06001597 # Don't update repo if local changes exist
1598 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001599 # ``errexit`` requires us to trap the exit code when the repo is changed
1600 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001601
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001602 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001603 (cd $REQUIREMENTS_DIR; \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001604 python update.py $project_dir)
Dean Troyeraf616d92014-02-17 12:57:55 -06001605 fi
1606
Sean Dague099e5e32014-03-31 10:35:43 -04001607 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001608
1609 # We've just gone and possibly modified the user's source tree in an
1610 # automated way, which is considered bad form if it's a development
1611 # tree because we've screwed up their next git checkin. So undo it.
1612 #
1613 # However... there are some circumstances, like running in the gate
1614 # where we really really want the overridden version to stick. So provide
1615 # a variable that tells us whether or not we should UNDO the requirements
1616 # changes (this will be set to False in the OpenStack ci gate)
1617 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001618 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001619 (cd $project_dir && git reset --hard)
1620 fi
1621 fi
1622}
1623
1624# ``pip install -e`` the package, which processes the dependencies
1625# using pip before running `setup.py develop`
1626# Uses globals ``STACK_USER``
1627# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001628function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001629 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001630 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001631
Sean Dague099e5e32014-03-31 10:35:43 -04001632 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001633 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001634 if [[ "$flags" == "-e" ]]; then
1635 safe_chown -R $STACK_USER $1/*.egg-info
1636 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001637}
1638
Dean Troyerdff49a22014-01-30 15:37:40 -06001639
1640# Service Functions
1641# =================
1642
1643# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1644# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001645function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001646 echo "$1" | sed -e '
1647 s/,,/,/g;
1648 s/^,//;
1649 s/,$//
1650 '
1651}
1652
1653# disable_all_services() removes all current services
1654# from ``ENABLED_SERVICES`` to reset the configuration
1655# before a minimal installation
1656# Uses global ``ENABLED_SERVICES``
1657# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001658function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001659 ENABLED_SERVICES=""
1660}
1661
1662# Remove all services starting with '-'. For example, to install all default
1663# services except rabbit (rabbit) set in ``localrc``:
1664# ENABLED_SERVICES+=",-rabbit"
1665# Uses global ``ENABLED_SERVICES``
1666# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001667function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001668 local tmpsvcs="${ENABLED_SERVICES}"
1669 local service
1670 for service in ${tmpsvcs//,/ }; do
1671 if [[ ${service} == -* ]]; then
1672 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1673 fi
1674 done
1675 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1676}
1677
1678# disable_service() removes the services passed as argument to the
1679# ``ENABLED_SERVICES`` list, if they are present.
1680#
1681# For example:
1682# disable_service rabbit
1683#
1684# This function does not know about the special cases
1685# for nova, glance, and neutron built into is_service_enabled().
1686# Uses global ``ENABLED_SERVICES``
1687# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001688function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001689 local tmpsvcs=",${ENABLED_SERVICES},"
1690 local service
1691 for service in $@; do
1692 if is_service_enabled $service; then
1693 tmpsvcs=${tmpsvcs//,$service,/,}
1694 fi
1695 done
1696 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1697}
1698
1699# enable_service() adds the services passed as argument to the
1700# ``ENABLED_SERVICES`` list, if they are not already present.
1701#
1702# For example:
1703# enable_service qpid
1704#
1705# This function does not know about the special cases
1706# for nova, glance, and neutron built into is_service_enabled().
1707# Uses global ``ENABLED_SERVICES``
1708# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001709function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001710 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001711 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001712 for service in $@; do
1713 if ! is_service_enabled $service; then
1714 tmpsvcs+=",$service"
1715 fi
1716 done
1717 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1718 disable_negated_services
1719}
1720
1721# is_service_enabled() checks if the service(s) specified as arguments are
1722# enabled by the user in ``ENABLED_SERVICES``.
1723#
1724# Multiple services specified as arguments are ``OR``'ed together; the test
1725# is a short-circuit boolean, i.e it returns on the first match.
1726#
1727# There are special cases for some 'catch-all' services::
1728# **nova** returns true if any service enabled start with **n-**
1729# **cinder** returns true if any service enabled start with **c-**
1730# **ceilometer** returns true if any service enabled start with **ceilometer**
1731# **glance** returns true if any service enabled start with **g-**
1732# **neutron** returns true if any service enabled start with **q-**
1733# **swift** returns true if any service enabled start with **s-**
1734# **trove** returns true if any service enabled start with **tr-**
1735# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1736# **s-** services will be enabled. This will be deprecated in the future.
1737#
1738# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1739# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1740# as enabled in this case.
1741#
1742# Uses global ``ENABLED_SERVICES``
1743# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001744function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001745 local xtrace=$(set +o | grep xtrace)
1746 set +o xtrace
1747 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001748 local services=$@
1749 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001750 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001751 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001752
1753 # Look for top-level 'enabled' function for this service
1754 if type is_${service}_enabled >/dev/null 2>&1; then
1755 # A function exists for this service, use it
1756 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001757 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001758 fi
1759
1760 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1761 # are implemented
1762
Sean Dague45917cc2014-02-24 16:09:14 -05001763 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001764 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001765 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1766 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1767 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1768 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1769 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1770 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1771 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1772 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1773 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001774 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001775 done
Sean Dague45917cc2014-02-24 16:09:14 -05001776 $xtrace
1777 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001778}
1779
1780# Toggle enable/disable_service for services that must run exclusive of each other
1781# $1 The name of a variable containing a space-separated list of services
1782# $2 The name of a variable in which to store the enabled service's name
1783# $3 The name of the service to enable
1784function use_exclusive_service {
1785 local options=${!1}
1786 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001787 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001788 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001789 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001790 for opt in $options;do
1791 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1792 done
1793 eval "$out=$selection"
1794 return 0
1795}
1796
1797
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001798# System Functions
1799# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001800
1801# Only run the command if the target file (the last arg) is not on an
1802# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001803function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001804 local xtrace=$(set +o | grep xtrace)
1805 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001806 local args=( $@ )
1807 local last
1808 local sudo_cmd
1809 local dir_to_check
1810
1811 let last="${#args[*]} - 1"
1812
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001813 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001814 if [ ! -d "$dir_to_check" ]; then
1815 dir_to_check=`dirname "$dir_to_check"`
1816 fi
1817
1818 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001819 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001820 return 0
1821 fi
1822
1823 if [[ $TRACK_DEPENDS = True ]]; then
1824 sudo_cmd="env"
1825 else
1826 sudo_cmd="sudo"
1827 fi
1828
Sean Dague45917cc2014-02-24 16:09:14 -05001829 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001830 $sudo_cmd $@
1831}
1832
1833# Exit 0 if address is in network or 1 if address is not in network
1834# ip-range is in CIDR notation: 1.2.3.4/20
1835# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001836function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001837 local ip=$1
1838 local range=$2
1839 local masklen=${range#*/}
1840 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1841 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1842 [[ $network == $subnet ]]
1843}
1844
1845# Add a user to a group.
1846# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001847function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001848 local user=$1
1849 local group=$2
1850
1851 if [[ -z "$os_VENDOR" ]]; then
1852 GetOSVersion
1853 fi
1854
1855 # SLE11 and openSUSE 12.2 don't have the usual usermod
1856 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1857 sudo usermod -a -G "$group" "$user"
1858 else
1859 sudo usermod -A "$group" "$user"
1860 fi
1861}
1862
1863# Convert CIDR notation to a IPv4 netmask
1864# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001865function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001866 local maskpat="255 255 255 255"
1867 local maskdgt="254 252 248 240 224 192 128"
1868 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1869 echo ${1-0}.${2-0}.${3-0}.${4-0}
1870}
1871
1872# Gracefully cp only if source file/dir exists
1873# cp_it source destination
1874function cp_it {
1875 if [ -e $1 ] || [ -d $1 ]; then
1876 cp -pRL $1 $2
1877 fi
1878}
1879
1880# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1881# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1882# ``localrc`` or on the command line if necessary::
1883#
1884# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1885#
1886# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1887
Ian Wienandaee18c72014-02-21 15:35:08 +11001888function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001889 if [[ -n "$http_proxy" ]]; then
1890 export http_proxy=$http_proxy
1891 fi
1892 if [[ -n "$https_proxy" ]]; then
1893 export https_proxy=$https_proxy
1894 fi
1895 if [[ -n "$no_proxy" ]]; then
1896 export no_proxy=$no_proxy
1897 fi
1898}
1899
1900# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001901function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001902 local mount_type=`stat -f -L -c %T $1`
1903 test "$mount_type" == "nfs"
1904}
1905
1906# Return the network portion of the given IP address using netmask
1907# netmask is in the traditional dotted-quad format
1908# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001909function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001910 local ip=$1
1911 local mask=$2
1912 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1913 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1914 echo $subnet
1915}
1916
1917# Service wrapper to restart services
1918# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001919function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001920 if is_ubuntu; then
1921 sudo /usr/sbin/service $1 restart
1922 else
1923 sudo /sbin/service $1 restart
1924 fi
1925}
1926
1927# Only change permissions of a file or directory if it is not on an
1928# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001929function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001930 _safe_permission_operation chmod $@
1931}
1932
1933# Only change ownership of a file or directory if it is not on an NFS
1934# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001935function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001936 _safe_permission_operation chown $@
1937}
1938
1939# Service wrapper to start services
1940# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001941function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001942 if is_ubuntu; then
1943 sudo /usr/sbin/service $1 start
1944 else
1945 sudo /sbin/service $1 start
1946 fi
1947}
1948
1949# Service wrapper to stop services
1950# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001951function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001952 if is_ubuntu; then
1953 sudo /usr/sbin/service $1 stop
1954 else
1955 sudo /sbin/service $1 stop
1956 fi
1957}
1958
1959
1960# Restore xtrace
1961$XTRACE
1962
1963# Local variables:
1964# mode: shell-script
1965# End: