blob: b1879adeff6b1b26c2d9f96388484f6422c0b600 [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``
34
35# Save trace setting
36XTRACE=$(set +o | grep xtrace)
37set +o xtrace
38
Sean Daguecc524062014-10-01 09:06:43 -040039# Global Config Variables
40declare -A GITREPO
41declare -A GITBRANCH
42declare -A GITDIR
43
Dean Troyerdff49a22014-01-30 15:37:40 -060044
45# Config Functions
46# ================
47
48# Append a new option in an ini file without replacing the old value
49# iniadd config-file section option value1 value2 value3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +110050function iniadd {
Sean Dague45917cc2014-02-24 16:09:14 -050051 local xtrace=$(set +o | grep xtrace)
52 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060053 local file=$1
54 local section=$2
55 local option=$3
56 shift 3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050057
Dean Troyerdff49a22014-01-30 15:37:40 -060058 local values="$(iniget_multiline $file $section $option) $@"
59 iniset_multiline $file $section $option $values
Sean Dague45917cc2014-02-24 16:09:14 -050060 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060061}
62
63# Comment an option in an INI file
64# inicomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110065function inicomment {
Sean Dague45917cc2014-02-24 16:09:14 -050066 local xtrace=$(set +o | grep xtrace)
67 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060068 local file=$1
69 local section=$2
70 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050071
Dean Troyerdff49a22014-01-30 15:37:40 -060072 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -050073 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060074}
75
76# Get an option from an INI file
77# iniget config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110078function iniget {
Sean Dague45917cc2014-02-24 16:09:14 -050079 local xtrace=$(set +o | grep xtrace)
80 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060081 local file=$1
82 local section=$2
83 local option=$3
84 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050085
Dean Troyerdff49a22014-01-30 15:37:40 -060086 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
87 echo ${line#*=}
Sean Dague45917cc2014-02-24 16:09:14 -050088 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060089}
90
91# Get a multiple line option from an INI file
92# iniget_multiline config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110093function iniget_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -050094 local xtrace=$(set +o | grep xtrace)
95 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060096 local file=$1
97 local section=$2
98 local option=$3
99 local values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500100
Dean Troyerdff49a22014-01-30 15:37:40 -0600101 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
102 echo ${values}
Sean Dague45917cc2014-02-24 16:09:14 -0500103 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600104}
105
106# Determinate is the given option present in the INI file
107# ini_has_option config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100108function ini_has_option {
Sean Dague45917cc2014-02-24 16:09:14 -0500109 local xtrace=$(set +o | grep xtrace)
110 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600111 local file=$1
112 local section=$2
113 local option=$3
114 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500115
Dean Troyerdff49a22014-01-30 15:37:40 -0600116 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Sean Dague45917cc2014-02-24 16:09:14 -0500117 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600118 [ -n "$line" ]
119}
120
121# Set an option in an INI file
122# iniset config-file section option value
Ian Wienandaee18c72014-02-21 15:35:08 +1100123function iniset {
Sean Dague45917cc2014-02-24 16:09:14 -0500124 local xtrace=$(set +o | grep xtrace)
125 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600126 local file=$1
127 local section=$2
128 local option=$3
129 local value=$4
130
131 [[ -z $section || -z $option ]] && return
132
133 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
134 # Add section at the end
135 echo -e "\n[$section]" >>"$file"
136 fi
137 if ! ini_has_option "$file" "$section" "$option"; then
138 # Add it
139 sed -i -e "/^\[$section\]/ a\\
140$option = $value
141" "$file"
142 else
143 local sep=$(echo -ne "\x01")
144 # Replace it
145 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
146 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500147 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600148}
149
150# Set a multiple line option in an INI file
151# iniset_multiline config-file section option value1 value2 valu3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +1100152function iniset_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -0500153 local xtrace=$(set +o | grep xtrace)
154 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600155 local file=$1
156 local section=$2
157 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500158
Dean Troyerdff49a22014-01-30 15:37:40 -0600159 shift 3
160 local values
161 for v in $@; do
162 # The later sed command inserts each new value in the line next to
163 # the section identifier, which causes the values to be inserted in
164 # the reverse order. Do a reverse here to keep the original order.
165 values="$v ${values}"
166 done
167 if ! grep -q "^\[$section\]" "$file"; then
168 # Add section at the end
169 echo -e "\n[$section]" >>"$file"
170 else
171 # Remove old values
172 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
173 fi
174 # Add new ones
175 for v in $values; do
176 sed -i -e "/^\[$section\]/ a\\
177$option = $v
178" "$file"
179 done
Sean Dague45917cc2014-02-24 16:09:14 -0500180 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600181}
182
183# Uncomment an option in an INI file
184# iniuncomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100185function iniuncomment {
Sean Dague45917cc2014-02-24 16:09:14 -0500186 local xtrace=$(set +o | grep xtrace)
187 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600188 local file=$1
189 local section=$2
190 local option=$3
191 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500192 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600193}
194
195# Normalize config values to True or False
196# Accepts as False: 0 no No NO false False FALSE
197# Accepts as True: 1 yes Yes YES true True TRUE
198# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100199function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500200 local xtrace=$(set +o | grep xtrace)
201 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600202 local default=$1
203 local testval=$2
204
205 [[ -z "$testval" ]] && { echo "$default"; return; }
206 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
207 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
208 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500209 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600210}
211
212
213# Control Functions
214# =================
215
216# Prints backtrace info
217# filename:lineno:function
218# backtrace level
219function backtrace {
220 local level=$1
221 local deep=$((${#BASH_SOURCE[@]} - 1))
222 echo "[Call Trace]"
223 while [ $level -le $deep ]; do
224 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
225 deep=$((deep - 1))
226 done
227}
228
229# Prints line number and "message" then exits
230# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100231function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600232 local exitcode=$?
233 set +o xtrace
234 local line=$1; shift
235 if [ $exitcode == 0 ]; then
236 exitcode=1
237 fi
238 backtrace 2
239 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600240 # Give buffers a second to flush
241 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600242 exit $exitcode
243}
244
245# Checks an environment variable is not set or has length 0 OR if the
246# exit code is non-zero and prints "message" and exits
247# NOTE: env-var is the variable name without a '$'
248# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100249function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600250 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500251 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600252 set +o xtrace
253 local line=$1; shift
254 local evar=$1; shift
255 if ! is_set $evar || [ $exitcode != 0 ]; then
256 die $line "$*"
257 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500258 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600259}
260
261# Prints line number and "message" in error format
262# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100263function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600264 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500265 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600266 set +o xtrace
267 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
268 echo $msg 1>&2;
269 if [[ -n ${SCREEN_LOGDIR} ]]; then
270 echo $msg >> "${SCREEN_LOGDIR}/error.log"
271 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500272 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600273 return $exitcode
274}
275
276# Checks an environment variable is not set or has length 0 OR if the
277# exit code is non-zero and prints "message"
278# NOTE: env-var is the variable name without a '$'
279# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100280function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600281 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500282 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600283 set +o xtrace
284 local line=$1; shift
285 local evar=$1; shift
286 if ! is_set $evar || [ $exitcode != 0 ]; then
287 err $line "$*"
288 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500289 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600290 return $exitcode
291}
292
293# Exit after outputting a message about the distribution not being supported.
294# exit_distro_not_supported [optional-string-telling-what-is-missing]
295function exit_distro_not_supported {
296 if [[ -z "$DISTRO" ]]; then
297 GetDistro
298 fi
299
300 if [ $# -gt 0 ]; then
301 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
302 else
303 die $LINENO "Support for $DISTRO is incomplete."
304 fi
305}
306
307# Test if the named environment variable is set and not zero length
308# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100309function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600310 local var=\$"$1"
311 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
312}
313
314# Prints line number and "message" in warning format
315# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100316function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600317 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500318 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600319 set +o xtrace
320 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
321 echo $msg 1>&2;
322 if [[ -n ${SCREEN_LOGDIR} ]]; then
323 echo $msg >> "${SCREEN_LOGDIR}/error.log"
324 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500325 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600326 return $exitcode
327}
328
329
330# Distro Functions
331# ================
332
333# Determine OS Vendor, Release and Update
334# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
335# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500336# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
337# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
338# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
339# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
340# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
341declare os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
342
Dean Troyerdff49a22014-01-30 15:37:40 -0600343# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100344function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500345
Dean Troyerdff49a22014-01-30 15:37:40 -0600346 # Figure out which vendor we are
347 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
348 # OS/X
349 os_VENDOR=`sw_vers -productName`
350 os_RELEASE=`sw_vers -productVersion`
351 os_UPDATE=${os_RELEASE##*.}
352 os_RELEASE=${os_RELEASE%.*}
353 os_PACKAGE=""
354 if [[ "$os_RELEASE" =~ "10.7" ]]; then
355 os_CODENAME="lion"
356 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
357 os_CODENAME="snow leopard"
358 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
359 os_CODENAME="leopard"
360 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
361 os_CODENAME="tiger"
362 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
363 os_CODENAME="panther"
364 else
365 os_CODENAME=""
366 fi
367 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
368 os_VENDOR=$(lsb_release -i -s)
369 os_RELEASE=$(lsb_release -r -s)
370 os_UPDATE=""
371 os_PACKAGE="rpm"
372 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
373 os_PACKAGE="deb"
374 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
375 lsb_release -d -s | grep -q openSUSE
376 if [[ $? -eq 0 ]]; then
377 os_VENDOR="openSUSE"
378 fi
379 elif [[ $os_VENDOR == "openSUSE project" ]]; then
380 os_VENDOR="openSUSE"
381 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
382 os_VENDOR="Red Hat"
383 fi
384 os_CODENAME=$(lsb_release -c -s)
385 elif [[ -r /etc/redhat-release ]]; then
386 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
387 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
388 # CentOS release 5.5 (Final)
389 # CentOS Linux release 6.0 (Final)
390 # Fedora release 16 (Verne)
391 # XenServer release 6.2.0-70446c (xenenterprise)
392 os_CODENAME=""
393 for r in "Red Hat" CentOS Fedora XenServer; do
394 os_VENDOR=$r
395 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
396 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
397 os_CODENAME=${ver#*|}
398 os_RELEASE=${ver%|*}
399 os_UPDATE=${os_RELEASE##*.}
400 os_RELEASE=${os_RELEASE%.*}
401 break
402 fi
403 os_VENDOR=""
404 done
405 os_PACKAGE="rpm"
406 elif [[ -r /etc/SuSE-release ]]; then
407 for r in openSUSE "SUSE Linux"; do
408 if [[ "$r" = "SUSE Linux" ]]; then
409 os_VENDOR="SUSE LINUX"
410 else
411 os_VENDOR=$r
412 fi
413
414 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
415 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
416 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
417 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
418 break
419 fi
420 os_VENDOR=""
421 done
422 os_PACKAGE="rpm"
423 # If lsb_release is not installed, we should be able to detect Debian OS
424 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
425 os_VENDOR="Debian"
426 os_PACKAGE="deb"
427 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
428 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
429 fi
430 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
431}
432
433# Translate the OS version values into common nomenclature
434# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500435declare DISTRO
436
Ian Wienandaee18c72014-02-21 15:35:08 +1100437function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600438 GetOSVersion
439 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
440 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
441 DISTRO=$os_CODENAME
442 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
443 # For Fedora, just use 'f' and the release
444 DISTRO="f$os_RELEASE"
445 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
446 DISTRO="opensuse-$os_RELEASE"
447 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
448 # For SLE, also use the service pack
449 if [[ -z "$os_UPDATE" ]]; then
450 DISTRO="sle${os_RELEASE}"
451 else
452 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
453 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530454 elif [[ "$os_VENDOR" =~ (Red Hat) || \
455 "$os_VENDOR" =~ (CentOS) || \
456 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600457 # Drop the . release as we assume it's compatible
458 DISTRO="rhel${os_RELEASE::1}"
459 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
460 DISTRO="xs$os_RELEASE"
461 else
462 # Catch-all for now is Vendor + Release + Update
463 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
464 fi
465 export DISTRO
466}
467
468# Utility function for checking machine architecture
469# is_arch arch-type
470function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500471 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600472}
473
Ian Wienandbdc90c52014-08-04 15:44:58 +1000474# Quick check for a rackspace host; n.b. rackspace provided images
475# have these Xen tools installed but a custom image may not.
476function is_rackspace {
477 [ -f /usr/bin/xenstore-ls ] && \
478 sudo /usr/bin/xenstore-ls vm-data | grep -q "Rackspace"
479}
480
Dean Troyerdff49a22014-01-30 15:37:40 -0600481# Determine if current distribution is a Fedora-based distribution
482# (Fedora, RHEL, CentOS, etc).
483# is_fedora
484function is_fedora {
485 if [[ -z "$os_VENDOR" ]]; then
486 GetOSVersion
487 fi
488
anju Tiwari6c639c92014-07-15 18:11:54 +0530489 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
490 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600491}
492
493
494# Determine if current distribution is a SUSE-based distribution
495# (openSUSE, SLE).
496# is_suse
497function is_suse {
498 if [[ -z "$os_VENDOR" ]]; then
499 GetOSVersion
500 fi
501
502 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
503}
504
505
506# Determine if current distribution is an Ubuntu-based distribution
507# It will also detect non-Ubuntu but Debian-based distros
508# is_ubuntu
509function is_ubuntu {
510 if [[ -z "$os_PACKAGE" ]]; then
511 GetOSVersion
512 fi
513 [ "$os_PACKAGE" = "deb" ]
514}
515
516
517# Git Functions
518# =============
519
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600520# Returns openstack release name for a given branch name
521# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100522function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600523 local branch=$1
Dean Troyer50cda692014-07-25 11:57:20 -0500524
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600525 if [[ $branch =~ "stable/" ]]; then
526 echo ${branch#*/}
527 else
528 echo "master"
529 fi
530}
531
Dean Troyerdff49a22014-01-30 15:37:40 -0600532# git clone only if directory doesn't exist already. Since ``DEST`` might not
533# be owned by the installation user, we create the directory and change the
534# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500535# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
536# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600537# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer50cda692014-07-25 11:57:20 -0500538# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600539# git_clone remote dest-dir branch
540function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500541 local git_remote=$1
542 local git_dest=$2
543 local git_ref=$3
544 local orig_dir=$(pwd)
545
Dean Troyerdff49a22014-01-30 15:37:40 -0600546 RECLONE=$(trueorfalse False $RECLONE)
547
548 if [[ "$OFFLINE" = "True" ]]; then
549 echo "Running in offline mode, clones already exist"
550 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500551 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600552 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400553 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600554 return
555 fi
556
Dean Troyer50cda692014-07-25 11:57:20 -0500557 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600558 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500559 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600560 [[ "$ERROR_ON_CLONE" = "True" ]] && \
561 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer50cda692014-07-25 11:57:20 -0500562 git_timed clone $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600563 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500564 cd $git_dest
565 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600566 else
567 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500568 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600569 [[ "$ERROR_ON_CLONE" = "True" ]] && \
570 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer50cda692014-07-25 11:57:20 -0500571 git_timed clone $git_remote $git_dest
572 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600573 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500574 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600575 elif [[ "$RECLONE" = "True" ]]; then
576 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500577 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600578 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500579 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100580 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600581 # remove the existing ignored files (like pyc) as they cause breakage
582 # (due to the py files having older timestamps than our pyc, so python
583 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500584 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600585
Dean Troyer50cda692014-07-25 11:57:20 -0500586 # handle git_ref accordingly to type (tag, branch)
587 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
588 git_update_tag $git_ref
589 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
590 git_update_branch $git_ref
591 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
592 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600593 else
Dean Troyer50cda692014-07-25 11:57:20 -0500594 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600595 fi
596
597 fi
598 fi
599
600 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500601 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600602 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400603 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600604}
605
Sean Daguecc524062014-10-01 09:06:43 -0400606# A variation on git clone that lets us specify a project by it's
607# actual name, like oslo.config. This is exceptionally useful in the
608# library installation case
609function git_clone_by_name {
610 local name=$1
611 local repo=${GITREPO[$name]}
612 local dir=${GITDIR[$name]}
613 local branch=${GITBRANCH[$name]}
614 git_clone $repo $dir $branch
615}
616
617
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100618# git can sometimes get itself infinitely stuck with transient network
619# errors or other issues with the remote end. This wraps git in a
620# timeout/retry loop and is intended to watch over non-local git
621# processes that might hang. GIT_TIMEOUT, if set, is passed directly
622# to timeout(1); otherwise the default value of 0 maintains the status
623# quo of waiting forever.
624# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100625function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100626 local count=0
627 local timeout=0
628
629 if [[ -n "${GIT_TIMEOUT}" ]]; then
630 timeout=${GIT_TIMEOUT}
631 fi
632
633 until timeout -s SIGINT ${timeout} git "$@"; do
634 # 124 is timeout(1)'s special return code when it reached the
635 # timeout; otherwise assume fatal failure
636 if [[ $? -ne 124 ]]; then
637 die $LINENO "git call failed: [git $@]"
638 fi
639
640 count=$(($count + 1))
641 warn "timeout ${count} for git call: [git $@]"
642 if [ $count -eq 3 ]; then
643 die $LINENO "Maximum of 3 git retries reached"
644 fi
645 sleep 5
646 done
647}
648
Dean Troyerdff49a22014-01-30 15:37:40 -0600649# git update using reference as a branch.
650# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100651function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500652 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600653
Dean Troyer50cda692014-07-25 11:57:20 -0500654 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600655 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500656 git branch -D $git_branch || true
657 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600658}
659
660# git update using reference as a branch.
661# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100662function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500663 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600664
Dean Troyer50cda692014-07-25 11:57:20 -0500665 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600666}
667
668# git update using reference as a tag. Be careful editing source at that repo
669# as working copy will be in a detached mode
670# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100671function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500672 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600673
Dean Troyer50cda692014-07-25 11:57:20 -0500674 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600675 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500676 git_timed fetch origin tag $git_tag
677 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600678}
679
680
681# OpenStack Functions
682# ===================
683
684# Get the default value for HOST_IP
685# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100686function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600687 local fixed_range=$1
688 local floating_range=$2
689 local host_ip_iface=$3
690 local host_ip=$4
691
692 # Find the interface used for the default route
693 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
694 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
695 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
696 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500697 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
698 local ip
699 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600700 # Attempt to filter out IP addresses that are part of the fixed and
701 # floating range. Note that this method only works if the ``netaddr``
702 # python library is installed. If it is not installed, an error
703 # will be printed and the first IP from the interface will be used.
704 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
705 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500706 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
707 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600708 break;
709 fi
710 done
711 fi
712 echo $host_ip
713}
714
Attila Fazekasf71b5002014-05-28 09:52:22 +0200715# Generates hex string from ``size`` byte of pseudo random data
716# generate_hex_string size
717function generate_hex_string {
718 local size=$1
719 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
720}
721
Dean Troyerdff49a22014-01-30 15:37:40 -0600722# Grab a numbered field from python prettytable output
723# Fields are numbered starting with 1
724# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
725# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100726function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500727 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600728 while read data; do
729 if [ "$1" -lt 0 ]; then
730 field="(\$(NF$1))"
731 else
732 field="\$$(($1 + 1))"
733 fi
734 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
735 done
736}
737
738# Add a policy to a policy.json file
739# Do nothing if the policy already exists
740# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100741function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600742 local policy_file=$1
743 local policy_name=$2
744 local policy_perm=$3
745
746 if grep -q ${policy_name} ${policy_file}; then
747 echo "Policy ${policy_name} already exists in ${policy_file}"
748 return
749 fi
750
751 # Add a terminating comma to policy lines without one
752 # Remove the closing '}' and all lines following to the end-of-file
753 local tmpfile=$(mktemp)
754 uniq ${policy_file} | sed -e '
755 s/]$/],/
756 /^[}]/,$d
757 ' > ${tmpfile}
758
759 # Append policy and closing brace
760 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
761 echo "}" >>${tmpfile}
762
763 mv ${tmpfile} ${policy_file}
764}
765
Bartosz Górski0abde392014-02-28 14:15:19 +0100766# Gets or creates user
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200767# Usage: get_or_create_user <username> <password> <project> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100768function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200769 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500770 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200771 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500772 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200773 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100774 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500775 local user_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100776 # Gets user id
777 openstack user show $1 -f value -c id 2>/dev/null ||
778 # Creates new user
779 openstack user create \
780 $1 \
781 --password "$2" \
782 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500783 $email \
Bartosz Górski0abde392014-02-28 14:15:19 +0100784 -f value -c id
785 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500786 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100787}
788
789# Gets or creates project
790# Usage: get_or_create_project <name>
791function get_or_create_project {
792 # Gets project id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500793 local project_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100794 # Gets project id
795 openstack project show $1 -f value -c id 2>/dev/null ||
796 # Creates new project if not exists
797 openstack project create $1 -f value -c id
798 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500799 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100800}
801
802# Gets or creates role
803# Usage: get_or_create_role <name>
804function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500805 local role_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100806 # Gets role id
807 openstack role show $1 -f value -c id 2>/dev/null ||
808 # Creates role if not exists
809 openstack role create $1 -f value -c id
810 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500811 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100812}
813
814# Gets or adds user role
815# Usage: get_or_add_user_role <role> <user> <project>
816function get_or_add_user_role {
817 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500818 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100819 $2 \
820 --project $3 \
821 --column "ID" \
822 --column "Name" \
823 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500824 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100825 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500826 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100827 $1 \
828 --user $2 \
829 --project $3 \
830 | grep " id " | get_field 2)
831 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500832 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100833}
834
835# Gets or creates service
836# Usage: get_or_create_service <name> <type> <description>
837function get_or_create_service {
838 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500839 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100840 # Gets service id
841 openstack service show $1 -f value -c id 2>/dev/null ||
842 # Creates new service if not exists
843 openstack service create \
844 $1 \
845 --type=$2 \
846 --description="$3" \
847 -f value -c id
848 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500849 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100850}
851
852# Gets or creates endpoint
853# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
854function get_or_create_endpoint {
855 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500856 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100857 --column "ID" \
858 --column "Region" \
859 --column "Service Name" \
860 | grep " $2 " \
861 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500862 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100863 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500864 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100865 $1 \
866 --region $2 \
867 --publicurl $3 \
868 --adminurl $4 \
869 --internalurl $5 \
870 | grep " id " | get_field 2)
871 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500872 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100873}
Dean Troyerdff49a22014-01-30 15:37:40 -0600874
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500875
Dean Troyerdff49a22014-01-30 15:37:40 -0600876# Package Functions
877# =================
878
879# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100880function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600881 local pkg_dir
882 if is_ubuntu; then
883 pkg_dir=$FILES/apts
884 elif is_fedora; then
885 pkg_dir=$FILES/rpms
886 elif is_suse; then
887 pkg_dir=$FILES/rpms-suse
888 else
889 exit_distro_not_supported "list of packages"
890 fi
891 echo "$pkg_dir"
892}
893
894# Wrapper for ``apt-get`` to set cache and proxy environment variables
895# Uses globals ``OFFLINE``, ``*_proxy``
896# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100897function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500898 local xtrace=$(set +o | grep xtrace)
899 set +o xtrace
900
Dean Troyerdff49a22014-01-30 15:37:40 -0600901 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
902 local sudo="sudo"
903 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500904
905 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600906 $sudo DEBIAN_FRONTEND=noninteractive \
907 http_proxy=$http_proxy https_proxy=$https_proxy \
908 no_proxy=$no_proxy \
909 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
910}
911
912# get_packages() collects a list of package names of any type from the
913# prerequisite files in ``files/{apts|rpms}``. The list is intended
914# to be passed to a package installer such as apt or yum.
915#
916# Only packages required for the services in 1st argument will be
917# included. Two bits of metadata are recognized in the prerequisite files:
918#
919# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
920# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
921# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100922function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500923 local xtrace=$(set +o | grep xtrace)
924 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600925 local services=$@
926 local package_dir=$(_get_package_dir)
927 local file_to_parse
928 local service
929
930 if [[ -z "$package_dir" ]]; then
931 echo "No package directory supplied"
932 return 1
933 fi
934 if [[ -z "$DISTRO" ]]; then
935 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -0500936 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -0600937 fi
938 for service in ${services//,/ }; do
939 # Allow individual services to specify dependencies
940 if [[ -e ${package_dir}/${service} ]]; then
941 file_to_parse="${file_to_parse} $service"
942 fi
943 # NOTE(sdague) n-api needs glance for now because that's where
944 # glance client is
945 if [[ $service == n-api ]]; then
946 if [[ ! $file_to_parse =~ nova ]]; then
947 file_to_parse="${file_to_parse} nova"
948 fi
949 if [[ ! $file_to_parse =~ glance ]]; then
950 file_to_parse="${file_to_parse} glance"
951 fi
952 elif [[ $service == c-* ]]; then
953 if [[ ! $file_to_parse =~ cinder ]]; then
954 file_to_parse="${file_to_parse} cinder"
955 fi
956 elif [[ $service == ceilometer-* ]]; then
957 if [[ ! $file_to_parse =~ ceilometer ]]; then
958 file_to_parse="${file_to_parse} ceilometer"
959 fi
960 elif [[ $service == s-* ]]; then
961 if [[ ! $file_to_parse =~ swift ]]; then
962 file_to_parse="${file_to_parse} swift"
963 fi
964 elif [[ $service == n-* ]]; then
965 if [[ ! $file_to_parse =~ nova ]]; then
966 file_to_parse="${file_to_parse} nova"
967 fi
968 elif [[ $service == g-* ]]; then
969 if [[ ! $file_to_parse =~ glance ]]; then
970 file_to_parse="${file_to_parse} glance"
971 fi
972 elif [[ $service == key* ]]; then
973 if [[ ! $file_to_parse =~ keystone ]]; then
974 file_to_parse="${file_to_parse} keystone"
975 fi
976 elif [[ $service == q-* ]]; then
977 if [[ ! $file_to_parse =~ neutron ]]; then
978 file_to_parse="${file_to_parse} neutron"
979 fi
Adam Gandelman539ec432014-03-18 18:57:43 -0700980 elif [[ $service == ir-* ]]; then
981 if [[ ! $file_to_parse =~ ironic ]]; then
982 file_to_parse="${file_to_parse} ironic"
983 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600984 fi
985 done
986
987 for file in ${file_to_parse}; do
988 local fname=${package_dir}/${file}
989 local OIFS line package distros distro
990 [[ -e $fname ]] || continue
991
992 OIFS=$IFS
993 IFS=$'\n'
994 for line in $(<${fname}); do
995 if [[ $line =~ "NOPRIME" ]]; then
996 continue
997 fi
998
999 # Assume we want this package
1000 package=${line%#*}
1001 inst_pkg=1
1002
1003 # Look for # dist:xxx in comment
1004 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1005 # We are using BASH regexp matching feature.
1006 package=${BASH_REMATCH[1]}
1007 distros=${BASH_REMATCH[2]}
1008 # In bash ${VAR,,} will lowecase VAR
1009 # Look for a match in the distro list
1010 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1011 # If no match then skip this package
1012 inst_pkg=0
1013 fi
1014 fi
1015
1016 # Look for # testonly in comment
1017 if [[ $line =~ (.*)#.*testonly.* ]]; then
1018 package=${BASH_REMATCH[1]}
1019 # Are we installing test packages? (test for the default value)
1020 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1021 # If not installing test packages the skip this package
1022 inst_pkg=0
1023 fi
1024 fi
1025
1026 if [[ $inst_pkg = 1 ]]; then
1027 echo $package
1028 fi
1029 done
1030 IFS=$OIFS
1031 done
Sean Dague45917cc2014-02-24 16:09:14 -05001032 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001033}
1034
1035# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001036# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001037# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001038function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001039 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001040 return 0
1041 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001042
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001043 if is_ubuntu; then
1044 local xtrace=$(set +o | grep xtrace)
1045 set +o xtrace
1046 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1047 # if there are transient errors pulling the updates, that's fine.
1048 # It may be secondary repositories that we don't really care about.
1049 apt_get update || /bin/true
1050 REPOS_UPDATED=True
1051 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001052 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001053 fi
1054}
1055
1056function real_install_package {
1057 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001058 apt_get install "$@"
1059 elif is_fedora; then
1060 yum_install "$@"
1061 elif is_suse; then
1062 zypper_install "$@"
1063 else
1064 exit_distro_not_supported "installing packages"
1065 fi
1066}
1067
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001068# Distro-agnostic package installer
1069# install_package package [package ...]
1070function install_package {
1071 update_package_repo
1072 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1073}
1074
Dean Troyerdff49a22014-01-30 15:37:40 -06001075# Distro-agnostic function to tell if a package is installed
1076# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001077function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001078 if [[ -z "$@" ]]; then
1079 return 1
1080 fi
1081
1082 if [[ -z "$os_PACKAGE" ]]; then
1083 GetOSVersion
1084 fi
1085
1086 if [[ "$os_PACKAGE" = "deb" ]]; then
1087 dpkg -s "$@" > /dev/null 2> /dev/null
1088 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1089 rpm --quiet -q "$@"
1090 else
1091 exit_distro_not_supported "finding if a package is installed"
1092 fi
1093}
1094
1095# Distro-agnostic package uninstaller
1096# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001097function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001098 if is_ubuntu; then
1099 apt_get purge "$@"
1100 elif is_fedora; then
1101 sudo yum remove -y "$@"
1102 elif is_suse; then
1103 sudo zypper rm "$@"
1104 else
1105 exit_distro_not_supported "uninstalling packages"
1106 fi
1107}
1108
1109# Wrapper for ``yum`` to set proxy environment variables
1110# Uses globals ``OFFLINE``, ``*_proxy``
1111# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001112function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001113 [[ "$OFFLINE" = "True" ]] && return
1114 local sudo="sudo"
1115 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001116
1117 # The manual check for missing packages is because yum -y assumes
1118 # missing packages are OK. See
1119 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001120 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1121 no_proxy=$no_proxy \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001122 yum install -y "$@" 2>&1 | \
1123 awk '
1124 BEGIN { fail=0 }
1125 /No package/ { fail=1 }
1126 { print }
1127 END { exit fail }' || \
1128 die $LINENO "Missing packages detected"
1129
1130 # also ensure we catch a yum failure
1131 if [[ ${PIPESTATUS[0]} != 0 ]]; then
1132 die $LINENO "Yum install failure"
1133 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001134}
1135
1136# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001137# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001138# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001139function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001140 [[ "$OFFLINE" = "True" ]] && return
1141 local sudo="sudo"
1142 [[ "$(id -u)" = "0" ]] && sudo="env"
1143 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1144 zypper --non-interactive install --auto-agree-with-licenses "$@"
1145}
1146
1147
1148# Process Functions
1149# =================
1150
1151# _run_process() is designed to be backgrounded by run_process() to simulate a
1152# fork. It includes the dirty work of closing extra filehandles and preparing log
1153# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001154# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001155# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001156# If an optional group is provided sg will be used to set the group of
1157# the command.
1158# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001159function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001160 local service=$1
1161 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001162 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001163
1164 # Undo logging redirections and close the extra descriptors
1165 exec 1>&3
1166 exec 2>&3
1167 exec 3>&-
1168 exec 6>&-
1169
1170 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001171 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1172 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001173
1174 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1175 export PYTHONUNBUFFERED=1
1176 fi
1177
Dean Troyer3159a822014-08-27 14:13:58 -05001178 # Run under ``setsid`` to force the process to become a session and group leader.
1179 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001180 if [[ -n "$group" ]]; then
1181 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1182 else
1183 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1184 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001185
1186 # Just silently exit this process
1187 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001188}
1189
1190# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1191# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001192# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001193# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001194function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001195 SCREEN_NAME=${SCREEN_NAME:-stack}
1196 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1197
1198 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1199 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1200 fi
1201
1202 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1203}
1204
1205# Find out if a process exists by partial name.
1206# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001207function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001208 local name=$1
1209 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001210 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001211 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001212 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001213}
1214
Dean Troyer3159a822014-08-27 14:13:58 -05001215# Run a single service under screen or directly
1216# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001217# If an optional group is provided sg will be used to run the
1218# command as that group.
1219# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001220function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001221 local service=$1
1222 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001223 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001224
Dean Troyer3159a822014-08-27 14:13:58 -05001225 if is_service_enabled $service; then
1226 if [[ "$USE_SCREEN" = "True" ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001227 screen_service "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001228 else
1229 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001230 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001231 fi
1232 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001233}
1234
1235# Helper to launch a service in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001236# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1237# ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001238# screen_service service "command-line" [group]
1239# Run a command in a shell in a screen window, if an optional group
1240# is provided, use sg to set the group of the command.
Dean Troyer3159a822014-08-27 14:13:58 -05001241function screen_service {
1242 local service=$1
1243 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001244 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001245
Sean Dagueea22a4f2014-06-27 15:21:41 -04001246 SCREEN_NAME=${SCREEN_NAME:-stack}
1247 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1248 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001249
Dean Troyer3159a822014-08-27 14:13:58 -05001250 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001251 # Append the service to the screen rc file
Dean Troyer3159a822014-08-27 14:13:58 -05001252 screen_rc "$service" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001253
Dean Troyer3159a822014-08-27 14:13:58 -05001254 screen -S $SCREEN_NAME -X screen -t $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001255
Dean Troyer3159a822014-08-27 14:13:58 -05001256 if [[ -n ${SCREEN_LOGDIR} ]]; then
1257 screen -S $SCREEN_NAME -p $service -X logfile ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log
1258 screen -S $SCREEN_NAME -p $service -X log on
1259 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001260 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001261
1262 # sleep to allow bash to be ready to be send the command - we are
1263 # creating a new window in screen and then sends characters, so if
1264 # bash isn't running by the time we send the command, nothing happens
1265 sleep 3
1266
1267 NL=`echo -ne '\015'`
1268 # This fun command does the following:
1269 # - the passed server command is backgrounded
1270 # - the pid of the background process is saved in the usual place
1271 # - the server process is brought back to the foreground
1272 # - if the server process exits prematurely the fg command errors
1273 # and a message is written to stdout and the service failure file
Chris Dent2f27a0e2014-09-09 13:46:02 +01001274 # The pid saved can be used in stop_process() as a process group
Dean Troyer3159a822014-08-27 14:13:58 -05001275 # id to kill off all child processes
Chris Dent2f27a0e2014-09-09 13:46:02 +01001276 if [[ -n "$group" ]]; then
1277 command="sg $group '$command'"
1278 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001279 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 -06001280 fi
1281}
1282
1283# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001284# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001285# screen_rc service "command-line"
1286function screen_rc {
1287 SCREEN_NAME=${SCREEN_NAME:-stack}
1288 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1289 if [[ ! -e $SCREENRC ]]; then
1290 # Name the screen session
1291 echo "sessionname $SCREEN_NAME" > $SCREENRC
1292 # Set a reasonable statusbar
1293 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1294 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1295 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1296 echo "screen -t shell bash" >> $SCREENRC
1297 fi
1298 # If this service doesn't already exist in the screenrc file
1299 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1300 NL=`echo -ne '\015'`
1301 echo "screen -t $1 bash" >> $SCREENRC
1302 echo "stuff \"$2$NL\"" >> $SCREENRC
1303
1304 if [[ -n ${SCREEN_LOGDIR} ]]; then
1305 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1306 echo "log on" >>$SCREENRC
1307 fi
1308 fi
1309}
1310
1311# Stop a service in screen
1312# If a PID is available use it, kill the whole process group via TERM
1313# If screen is being used kill the screen window; this will catch processes
1314# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001315# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001316# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001317function screen_stop_service {
1318 local service=$1
1319
Dean Troyerdff49a22014-01-30 15:37:40 -06001320 SCREEN_NAME=${SCREEN_NAME:-stack}
1321 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1322 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1323
Dean Troyer3159a822014-08-27 14:13:58 -05001324 if is_service_enabled $service; then
1325 # Clean up the screen window
1326 screen -S $SCREEN_NAME -p $service -X kill
1327 fi
1328}
1329
1330# Stop a service process
1331# If a PID is available use it, kill the whole process group via TERM
1332# If screen is being used kill the screen window; this will catch processes
1333# that did not leave a PID behind
1334# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1335# stop_process service
1336function stop_process {
1337 local service=$1
1338
1339 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1340 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1341
1342 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001343 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001344 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1345 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1346 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001347 fi
1348 if [[ "$USE_SCREEN" = "True" ]]; then
1349 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001350 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001351 fi
1352 fi
1353}
1354
1355# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001356# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001357# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001358function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001359 local service
1360 local failures
1361 SCREEN_NAME=${SCREEN_NAME:-stack}
1362 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1363
1364
1365 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1366 echo "No service status directory found"
1367 return
1368 fi
1369
1370 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001371 # make this -o errexit safe
1372 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001373
1374 for service in $failures; do
1375 service=`basename $service`
1376 service=${service%.failure}
1377 echo "Error: Service $service is not running"
1378 done
1379
1380 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001381 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001382 fi
1383}
1384
Chris Dent2f27a0e2014-09-09 13:46:02 +01001385# Tail a log file in a screen if USE_SCREEN is true.
1386function tail_log {
1387 local service=$1
1388 local logfile=$2
1389
1390 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1391 if [[ "$USE_SCREEN" = "True" ]]; then
1392 screen_service "$service" "sudo tail -f $logfile"
1393 fi
1394}
1395
Dean Troyerdff49a22014-01-30 15:37:40 -06001396
Dean Troyer3159a822014-08-27 14:13:58 -05001397# Deprecated Functions
1398# --------------------
1399
1400# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1401# fork. It includes the dirty work of closing extra filehandles and preparing log
1402# files to produce the same logs as screen_it(). The log filename is derived
1403# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1404# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1405# _old_run_process service "command-line"
1406function _old_run_process {
1407 local service=$1
1408 local command="$2"
1409
1410 # Undo logging redirections and close the extra descriptors
1411 exec 1>&3
1412 exec 2>&3
1413 exec 3>&-
1414 exec 6>&-
1415
1416 if [[ -n ${SCREEN_LOGDIR} ]]; then
1417 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1418 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1419
1420 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1421 export PYTHONUNBUFFERED=1
1422 fi
1423
1424 exec /bin/bash -c "$command"
1425 die "$service exec failure: $command"
1426}
1427
1428# old_run_process() launches a child process that closes all file descriptors and
1429# then exec's the passed in command. This is meant to duplicate the semantics
1430# of screen_it() without screen. PIDs are written to
1431# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1432# old_run_process service "command-line"
1433function old_run_process {
1434 local service=$1
1435 local command="$2"
1436
1437 # Spawn the child process
1438 _old_run_process "$service" "$command" &
1439 echo $!
1440}
1441
1442# Compatibility for existing start_XXXX() functions
1443# Uses global ``USE_SCREEN``
1444# screen_it service "command-line"
1445function screen_it {
1446 if is_service_enabled $1; then
1447 # Append the service to the screen rc file
1448 screen_rc "$1" "$2"
1449
1450 if [[ "$USE_SCREEN" = "True" ]]; then
1451 screen_service "$1" "$2"
1452 else
1453 # Spawn directly without screen
1454 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1455 fi
1456 fi
1457}
1458
1459# Compatibility for existing stop_XXXX() functions
1460# Stop a service in screen
1461# If a PID is available use it, kill the whole process group via TERM
1462# If screen is being used kill the screen window; this will catch processes
1463# that did not leave a PID behind
1464# screen_stop service
1465function screen_stop {
1466 # Clean up the screen window
1467 stop_process $1
1468}
1469
1470
Dean Troyerdff49a22014-01-30 15:37:40 -06001471# Python Functions
1472# ================
1473
1474# Get the path to the pip command.
1475# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001476function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001477 which pip || which pip-python
1478
1479 if [ $? -ne 0 ]; then
1480 die $LINENO "Unable to find pip; cannot continue"
1481 fi
1482}
1483
1484# Get the path to the direcotry where python executables are installed.
1485# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001486function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001487 if is_fedora || is_suse; then
1488 echo "/usr/bin"
1489 else
1490 echo "/usr/local/bin"
1491 fi
1492}
1493
1494# Wrapper for ``pip install`` to set cache and proxy environment variables
1495# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
1496# ``TRACK_DEPENDS``, ``*_proxy``
1497# pip_install package [package ...]
1498function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001499 local xtrace=$(set +o | grep xtrace)
1500 set +o xtrace
1501 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1502 $xtrace
1503 return
1504 fi
1505
Dean Troyerdff49a22014-01-30 15:37:40 -06001506 if [[ -z "$os_PACKAGE" ]]; then
1507 GetOSVersion
1508 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001509 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1510 # TRACK_DEPENDS=True installation creates a circular dependency when
1511 # we attempt to install virtualenv into a virualenv, so we must global
1512 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001513 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001514 local cmd_pip=$DEST/.venv/bin/pip
1515 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001516 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001517 local cmd_pip=$(get_pip_command)
1518 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001519 fi
1520
1521 # Mirror option not needed anymore because pypi has CDN available,
1522 # but it's useful in certain circumstances
1523 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001524 local pip_mirror_opt=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001525 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001526 pip_mirror_opt="--use-mirrors"
Dean Troyerdff49a22014-01-30 15:37:40 -06001527 fi
1528
1529 # pip < 1.4 has a bug where it will use an already existing build
1530 # directory unconditionally. Say an earlier component installs
1531 # foo v1.1; pip will have built foo's source in
1532 # /tmp/$USER-pip-build. Even if a later component specifies foo <
1533 # 1.1, the existing extracted build will be used and cause
1534 # confusing errors. By creating unique build directories we avoid
1535 # this problem. See https://github.com/pypa/pip/issues/709
1536 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1537
Sean Dague45917cc2014-02-24 16:09:14 -05001538 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001539 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001540 http_proxy=$http_proxy \
1541 https_proxy=$https_proxy \
1542 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001543 $cmd_pip install \
1544 $pip_mirror_opt $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001545
1546 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1547 local test_req="$@/test-requirements.txt"
1548 if [[ -e "$test_req" ]]; then
1549 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1550 http_proxy=$http_proxy \
1551 https_proxy=$https_proxy \
1552 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001553 $cmd_pip install \
1554 $pip_mirror_opt -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001555 fi
1556 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001557}
1558
Sean Daguecc524062014-10-01 09:06:43 -04001559# should we use this library from their git repo, or should we let it
1560# get pulled in via pip dependencies.
1561function use_library_from_git {
1562 local name=$1
1563 local enabled=1
1564 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1565 return $enabled
1566}
1567
1568# setup a library by name. If we are trying to use the library from
1569# git, we'll do a git based install, otherwise we'll punt and the
1570# library should be installed by a requirements pull from another
1571# project.
1572function setup_lib {
1573 local name=$1
1574 local dir=${GITDIR[$name]}
1575 setup_install $dir
1576}
1577
1578
Sean Dague099e5e32014-03-31 10:35:43 -04001579# this should be used if you want to install globally, all libraries should
1580# use this, especially *oslo* ones
1581function setup_install {
1582 local project_dir=$1
1583 setup_package_with_req_sync $project_dir
1584}
1585
1586# this should be used for projects which run services, like all services
1587function setup_develop {
1588 local project_dir=$1
1589 setup_package_with_req_sync $project_dir -e
1590}
1591
Dean Troyeraf616d92014-02-17 12:57:55 -06001592# ``pip install -e`` the package, which processes the dependencies
1593# using pip before running `setup.py develop`
1594#
1595# Updates the dependencies in project_dir from the
1596# openstack/requirements global list before installing anything.
1597#
1598# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1599# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001600function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001601 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001602 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001603
Dean Troyeraf616d92014-02-17 12:57:55 -06001604 # Don't update repo if local changes exist
1605 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001606 # ``errexit`` requires us to trap the exit code when the repo is changed
1607 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001608
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001609 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001610 (cd $REQUIREMENTS_DIR; \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001611 python update.py $project_dir)
Dean Troyeraf616d92014-02-17 12:57:55 -06001612 fi
1613
Sean Dague099e5e32014-03-31 10:35:43 -04001614 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001615
1616 # We've just gone and possibly modified the user's source tree in an
1617 # automated way, which is considered bad form if it's a development
1618 # tree because we've screwed up their next git checkin. So undo it.
1619 #
1620 # However... there are some circumstances, like running in the gate
1621 # where we really really want the overridden version to stick. So provide
1622 # a variable that tells us whether or not we should UNDO the requirements
1623 # changes (this will be set to False in the OpenStack ci gate)
1624 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001625 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001626 (cd $project_dir && git reset --hard)
1627 fi
1628 fi
1629}
1630
1631# ``pip install -e`` the package, which processes the dependencies
1632# using pip before running `setup.py develop`
1633# Uses globals ``STACK_USER``
1634# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001635function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001636 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001637 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001638
Sean Dague099e5e32014-03-31 10:35:43 -04001639 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001640 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001641 if [[ "$flags" == "-e" ]]; then
1642 safe_chown -R $STACK_USER $1/*.egg-info
1643 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001644}
1645
Dean Troyerdff49a22014-01-30 15:37:40 -06001646
1647# Service Functions
1648# =================
1649
1650# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1651# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001652function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001653 echo "$1" | sed -e '
1654 s/,,/,/g;
1655 s/^,//;
1656 s/,$//
1657 '
1658}
1659
1660# disable_all_services() removes all current services
1661# from ``ENABLED_SERVICES`` to reset the configuration
1662# before a minimal installation
1663# Uses global ``ENABLED_SERVICES``
1664# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001665function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001666 ENABLED_SERVICES=""
1667}
1668
1669# Remove all services starting with '-'. For example, to install all default
1670# services except rabbit (rabbit) set in ``localrc``:
1671# ENABLED_SERVICES+=",-rabbit"
1672# Uses global ``ENABLED_SERVICES``
1673# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001674function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001675 local tmpsvcs="${ENABLED_SERVICES}"
1676 local service
1677 for service in ${tmpsvcs//,/ }; do
1678 if [[ ${service} == -* ]]; then
1679 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1680 fi
1681 done
1682 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1683}
1684
1685# disable_service() removes the services passed as argument to the
1686# ``ENABLED_SERVICES`` list, if they are present.
1687#
1688# For example:
1689# disable_service rabbit
1690#
1691# This function does not know about the special cases
1692# for nova, glance, and neutron built into is_service_enabled().
1693# Uses global ``ENABLED_SERVICES``
1694# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001695function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001696 local tmpsvcs=",${ENABLED_SERVICES},"
1697 local service
1698 for service in $@; do
1699 if is_service_enabled $service; then
1700 tmpsvcs=${tmpsvcs//,$service,/,}
1701 fi
1702 done
1703 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1704}
1705
1706# enable_service() adds the services passed as argument to the
1707# ``ENABLED_SERVICES`` list, if they are not already present.
1708#
1709# For example:
1710# enable_service qpid
1711#
1712# This function does not know about the special cases
1713# for nova, glance, and neutron built into is_service_enabled().
1714# Uses global ``ENABLED_SERVICES``
1715# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001716function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001717 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001718 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001719 for service in $@; do
1720 if ! is_service_enabled $service; then
1721 tmpsvcs+=",$service"
1722 fi
1723 done
1724 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1725 disable_negated_services
1726}
1727
1728# is_service_enabled() checks if the service(s) specified as arguments are
1729# enabled by the user in ``ENABLED_SERVICES``.
1730#
1731# Multiple services specified as arguments are ``OR``'ed together; the test
1732# is a short-circuit boolean, i.e it returns on the first match.
1733#
1734# There are special cases for some 'catch-all' services::
1735# **nova** returns true if any service enabled start with **n-**
1736# **cinder** returns true if any service enabled start with **c-**
1737# **ceilometer** returns true if any service enabled start with **ceilometer**
1738# **glance** returns true if any service enabled start with **g-**
1739# **neutron** returns true if any service enabled start with **q-**
1740# **swift** returns true if any service enabled start with **s-**
1741# **trove** returns true if any service enabled start with **tr-**
1742# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1743# **s-** services will be enabled. This will be deprecated in the future.
1744#
1745# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1746# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1747# as enabled in this case.
1748#
1749# Uses global ``ENABLED_SERVICES``
1750# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001751function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001752 local xtrace=$(set +o | grep xtrace)
1753 set +o xtrace
1754 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001755 local services=$@
1756 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001757 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001758 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001759
1760 # Look for top-level 'enabled' function for this service
1761 if type is_${service}_enabled >/dev/null 2>&1; then
1762 # A function exists for this service, use it
1763 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001764 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001765 fi
1766
1767 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1768 # are implemented
1769
Sean Dague45917cc2014-02-24 16:09:14 -05001770 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001771 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001772 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1773 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1774 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1775 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1776 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1777 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1778 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1779 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1780 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001781 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001782 done
Sean Dague45917cc2014-02-24 16:09:14 -05001783 $xtrace
1784 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001785}
1786
1787# Toggle enable/disable_service for services that must run exclusive of each other
1788# $1 The name of a variable containing a space-separated list of services
1789# $2 The name of a variable in which to store the enabled service's name
1790# $3 The name of the service to enable
1791function use_exclusive_service {
1792 local options=${!1}
1793 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001794 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001795 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001796 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001797 for opt in $options;do
1798 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1799 done
1800 eval "$out=$selection"
1801 return 0
1802}
1803
1804
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001805# System Functions
1806# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001807
1808# Only run the command if the target file (the last arg) is not on an
1809# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001810function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001811 local xtrace=$(set +o | grep xtrace)
1812 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001813 local args=( $@ )
1814 local last
1815 local sudo_cmd
1816 local dir_to_check
1817
1818 let last="${#args[*]} - 1"
1819
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001820 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001821 if [ ! -d "$dir_to_check" ]; then
1822 dir_to_check=`dirname "$dir_to_check"`
1823 fi
1824
1825 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001826 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001827 return 0
1828 fi
1829
1830 if [[ $TRACK_DEPENDS = True ]]; then
1831 sudo_cmd="env"
1832 else
1833 sudo_cmd="sudo"
1834 fi
1835
Sean Dague45917cc2014-02-24 16:09:14 -05001836 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001837 $sudo_cmd $@
1838}
1839
1840# Exit 0 if address is in network or 1 if address is not in network
1841# ip-range is in CIDR notation: 1.2.3.4/20
1842# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001843function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001844 local ip=$1
1845 local range=$2
1846 local masklen=${range#*/}
1847 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1848 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1849 [[ $network == $subnet ]]
1850}
1851
1852# Add a user to a group.
1853# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001854function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001855 local user=$1
1856 local group=$2
1857
1858 if [[ -z "$os_VENDOR" ]]; then
1859 GetOSVersion
1860 fi
1861
1862 # SLE11 and openSUSE 12.2 don't have the usual usermod
1863 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1864 sudo usermod -a -G "$group" "$user"
1865 else
1866 sudo usermod -A "$group" "$user"
1867 fi
1868}
1869
1870# Convert CIDR notation to a IPv4 netmask
1871# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001872function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001873 local maskpat="255 255 255 255"
1874 local maskdgt="254 252 248 240 224 192 128"
1875 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1876 echo ${1-0}.${2-0}.${3-0}.${4-0}
1877}
1878
1879# Gracefully cp only if source file/dir exists
1880# cp_it source destination
1881function cp_it {
1882 if [ -e $1 ] || [ -d $1 ]; then
1883 cp -pRL $1 $2
1884 fi
1885}
1886
1887# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1888# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1889# ``localrc`` or on the command line if necessary::
1890#
1891# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1892#
1893# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1894
Ian Wienandaee18c72014-02-21 15:35:08 +11001895function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001896 if [[ -n "$http_proxy" ]]; then
1897 export http_proxy=$http_proxy
1898 fi
1899 if [[ -n "$https_proxy" ]]; then
1900 export https_proxy=$https_proxy
1901 fi
1902 if [[ -n "$no_proxy" ]]; then
1903 export no_proxy=$no_proxy
1904 fi
1905}
1906
1907# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001908function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001909 local mount_type=`stat -f -L -c %T $1`
1910 test "$mount_type" == "nfs"
1911}
1912
1913# Return the network portion of the given IP address using netmask
1914# netmask is in the traditional dotted-quad format
1915# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001916function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001917 local ip=$1
1918 local mask=$2
1919 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1920 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1921 echo $subnet
1922}
1923
1924# Service wrapper to restart services
1925# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001926function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001927 if is_ubuntu; then
1928 sudo /usr/sbin/service $1 restart
1929 else
1930 sudo /sbin/service $1 restart
1931 fi
1932}
1933
1934# Only change permissions of a file or directory if it is not on an
1935# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001936function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001937 _safe_permission_operation chmod $@
1938}
1939
1940# Only change ownership of a file or directory if it is not on an NFS
1941# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001942function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001943 _safe_permission_operation chown $@
1944}
1945
1946# Service wrapper to start services
1947# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001948function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001949 if is_ubuntu; then
1950 sudo /usr/sbin/service $1 start
1951 else
1952 sudo /sbin/service $1 start
1953 fi
1954}
1955
1956# Service wrapper to stop services
1957# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001958function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001959 if is_ubuntu; then
1960 sudo /usr/sbin/service $1 stop
1961 else
1962 sudo /sbin/service $1 stop
1963 fi
1964}
1965
1966
1967# Restore xtrace
1968$XTRACE
1969
1970# Local variables:
1971# mode: shell-script
1972# End: