blob: 9f4acfec1eda3d41df9b4fec4dd61e7abc3686e7 [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
Adam Gandelman8f385722014-10-14 15:50:18 -0700525 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600526 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
Dean Troyer3324f192014-09-18 09:26:39 -05001273 # and a message is written to stdout and the service failure file
1274 #
Chris Dent2f27a0e2014-09-09 13:46:02 +01001275 # The pid saved can be used in stop_process() as a process group
Dean Troyer3159a822014-08-27 14:13:58 -05001276 # id to kill off all child processes
Chris Dent2f27a0e2014-09-09 13:46:02 +01001277 if [[ -n "$group" ]]; then
1278 command="sg $group '$command'"
1279 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001280 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 -06001281 fi
1282}
1283
1284# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001285# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001286# screen_rc service "command-line"
1287function screen_rc {
1288 SCREEN_NAME=${SCREEN_NAME:-stack}
1289 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1290 if [[ ! -e $SCREENRC ]]; then
1291 # Name the screen session
1292 echo "sessionname $SCREEN_NAME" > $SCREENRC
1293 # Set a reasonable statusbar
1294 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1295 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1296 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1297 echo "screen -t shell bash" >> $SCREENRC
1298 fi
1299 # If this service doesn't already exist in the screenrc file
1300 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1301 NL=`echo -ne '\015'`
1302 echo "screen -t $1 bash" >> $SCREENRC
1303 echo "stuff \"$2$NL\"" >> $SCREENRC
1304
1305 if [[ -n ${SCREEN_LOGDIR} ]]; then
1306 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1307 echo "log on" >>$SCREENRC
1308 fi
1309 fi
1310}
1311
1312# Stop a service in screen
1313# If a PID is available use it, kill the whole process group via TERM
1314# If screen is being used kill the screen window; this will catch processes
1315# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001316# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001317# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001318function screen_stop_service {
1319 local service=$1
1320
Dean Troyerdff49a22014-01-30 15:37:40 -06001321 SCREEN_NAME=${SCREEN_NAME:-stack}
1322 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1323 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1324
Dean Troyer3159a822014-08-27 14:13:58 -05001325 if is_service_enabled $service; then
1326 # Clean up the screen window
1327 screen -S $SCREEN_NAME -p $service -X kill
1328 fi
1329}
1330
1331# Stop a service process
1332# If a PID is available use it, kill the whole process group via TERM
1333# If screen is being used kill the screen window; this will catch processes
1334# that did not leave a PID behind
1335# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1336# stop_process service
1337function stop_process {
1338 local service=$1
1339
1340 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1341 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1342
1343 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001344 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001345 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1346 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1347 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001348 fi
1349 if [[ "$USE_SCREEN" = "True" ]]; then
1350 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001351 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001352 fi
1353 fi
1354}
1355
1356# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001357# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001358# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001359function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001360 local service
1361 local failures
1362 SCREEN_NAME=${SCREEN_NAME:-stack}
1363 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1364
1365
1366 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1367 echo "No service status directory found"
1368 return
1369 fi
1370
1371 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001372 # make this -o errexit safe
1373 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001374
1375 for service in $failures; do
1376 service=`basename $service`
1377 service=${service%.failure}
1378 echo "Error: Service $service is not running"
1379 done
1380
1381 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001382 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001383 fi
1384}
1385
Chris Dent2f27a0e2014-09-09 13:46:02 +01001386# Tail a log file in a screen if USE_SCREEN is true.
1387function tail_log {
1388 local service=$1
1389 local logfile=$2
1390
1391 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1392 if [[ "$USE_SCREEN" = "True" ]]; then
1393 screen_service "$service" "sudo tail -f $logfile"
1394 fi
1395}
1396
Dean Troyerdff49a22014-01-30 15:37:40 -06001397
Dean Troyer3159a822014-08-27 14:13:58 -05001398# Deprecated Functions
1399# --------------------
1400
1401# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1402# fork. It includes the dirty work of closing extra filehandles and preparing log
1403# files to produce the same logs as screen_it(). The log filename is derived
1404# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1405# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1406# _old_run_process service "command-line"
1407function _old_run_process {
1408 local service=$1
1409 local command="$2"
1410
1411 # Undo logging redirections and close the extra descriptors
1412 exec 1>&3
1413 exec 2>&3
1414 exec 3>&-
1415 exec 6>&-
1416
1417 if [[ -n ${SCREEN_LOGDIR} ]]; then
1418 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1419 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1420
1421 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1422 export PYTHONUNBUFFERED=1
1423 fi
1424
1425 exec /bin/bash -c "$command"
1426 die "$service exec failure: $command"
1427}
1428
1429# old_run_process() launches a child process that closes all file descriptors and
1430# then exec's the passed in command. This is meant to duplicate the semantics
1431# of screen_it() without screen. PIDs are written to
1432# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1433# old_run_process service "command-line"
1434function old_run_process {
1435 local service=$1
1436 local command="$2"
1437
1438 # Spawn the child process
1439 _old_run_process "$service" "$command" &
1440 echo $!
1441}
1442
1443# Compatibility for existing start_XXXX() functions
1444# Uses global ``USE_SCREEN``
1445# screen_it service "command-line"
1446function screen_it {
1447 if is_service_enabled $1; then
1448 # Append the service to the screen rc file
1449 screen_rc "$1" "$2"
1450
1451 if [[ "$USE_SCREEN" = "True" ]]; then
1452 screen_service "$1" "$2"
1453 else
1454 # Spawn directly without screen
1455 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1456 fi
1457 fi
1458}
1459
1460# Compatibility for existing stop_XXXX() functions
1461# Stop a service in screen
1462# If a PID is available use it, kill the whole process group via TERM
1463# If screen is being used kill the screen window; this will catch processes
1464# that did not leave a PID behind
1465# screen_stop service
1466function screen_stop {
1467 # Clean up the screen window
1468 stop_process $1
1469}
1470
1471
Dean Troyerdff49a22014-01-30 15:37:40 -06001472# Python Functions
1473# ================
1474
1475# Get the path to the pip command.
1476# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001477function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001478 which pip || which pip-python
1479
1480 if [ $? -ne 0 ]; then
1481 die $LINENO "Unable to find pip; cannot continue"
1482 fi
1483}
1484
1485# Get the path to the direcotry where python executables are installed.
1486# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001487function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001488 if is_fedora || is_suse; then
1489 echo "/usr/bin"
1490 else
1491 echo "/usr/local/bin"
1492 fi
1493}
1494
1495# Wrapper for ``pip install`` to set cache and proxy environment variables
1496# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
1497# ``TRACK_DEPENDS``, ``*_proxy``
1498# pip_install package [package ...]
1499function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001500 local xtrace=$(set +o | grep xtrace)
1501 set +o xtrace
1502 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1503 $xtrace
1504 return
1505 fi
1506
Dean Troyerdff49a22014-01-30 15:37:40 -06001507 if [[ -z "$os_PACKAGE" ]]; then
1508 GetOSVersion
1509 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001510 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1511 # TRACK_DEPENDS=True installation creates a circular dependency when
1512 # we attempt to install virtualenv into a virualenv, so we must global
1513 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001514 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001515 local cmd_pip=$DEST/.venv/bin/pip
1516 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001517 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001518 local cmd_pip=$(get_pip_command)
1519 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001520 fi
1521
1522 # Mirror option not needed anymore because pypi has CDN available,
1523 # but it's useful in certain circumstances
1524 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001525 local pip_mirror_opt=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001526 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001527 pip_mirror_opt="--use-mirrors"
Dean Troyerdff49a22014-01-30 15:37:40 -06001528 fi
1529
Sean Dague45917cc2014-02-24 16:09:14 -05001530 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001531 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001532 http_proxy=$http_proxy \
1533 https_proxy=$https_proxy \
1534 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001535 $cmd_pip install \
1536 $pip_mirror_opt $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001537
1538 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1539 local test_req="$@/test-requirements.txt"
1540 if [[ -e "$test_req" ]]; then
1541 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1542 http_proxy=$http_proxy \
1543 https_proxy=$https_proxy \
1544 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001545 $cmd_pip install \
1546 $pip_mirror_opt -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001547 fi
1548 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001549}
1550
Sean Daguecc524062014-10-01 09:06:43 -04001551# should we use this library from their git repo, or should we let it
1552# get pulled in via pip dependencies.
1553function use_library_from_git {
1554 local name=$1
1555 local enabled=1
1556 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1557 return $enabled
1558}
1559
1560# setup a library by name. If we are trying to use the library from
1561# git, we'll do a git based install, otherwise we'll punt and the
1562# library should be installed by a requirements pull from another
1563# project.
1564function setup_lib {
1565 local name=$1
1566 local dir=${GITDIR[$name]}
1567 setup_install $dir
1568}
1569
1570
Sean Dague099e5e32014-03-31 10:35:43 -04001571# this should be used if you want to install globally, all libraries should
1572# use this, especially *oslo* ones
1573function setup_install {
1574 local project_dir=$1
1575 setup_package_with_req_sync $project_dir
1576}
1577
1578# this should be used for projects which run services, like all services
1579function setup_develop {
1580 local project_dir=$1
1581 setup_package_with_req_sync $project_dir -e
1582}
1583
Dean Troyeraf616d92014-02-17 12:57:55 -06001584# ``pip install -e`` the package, which processes the dependencies
1585# using pip before running `setup.py develop`
1586#
1587# Updates the dependencies in project_dir from the
1588# openstack/requirements global list before installing anything.
1589#
1590# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1591# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001592function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001593 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001594 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001595
Dean Troyeraf616d92014-02-17 12:57:55 -06001596 # Don't update repo if local changes exist
1597 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001598 # ``errexit`` requires us to trap the exit code when the repo is changed
1599 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001600
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001601 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001602 (cd $REQUIREMENTS_DIR; \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001603 python update.py $project_dir)
Dean Troyeraf616d92014-02-17 12:57:55 -06001604 fi
1605
Sean Dague099e5e32014-03-31 10:35:43 -04001606 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001607
1608 # We've just gone and possibly modified the user's source tree in an
1609 # automated way, which is considered bad form if it's a development
1610 # tree because we've screwed up their next git checkin. So undo it.
1611 #
1612 # However... there are some circumstances, like running in the gate
1613 # where we really really want the overridden version to stick. So provide
1614 # a variable that tells us whether or not we should UNDO the requirements
1615 # changes (this will be set to False in the OpenStack ci gate)
1616 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001617 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001618 (cd $project_dir && git reset --hard)
1619 fi
1620 fi
1621}
1622
1623# ``pip install -e`` the package, which processes the dependencies
1624# using pip before running `setup.py develop`
1625# Uses globals ``STACK_USER``
1626# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001627function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001628 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001629 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001630
Sean Dague099e5e32014-03-31 10:35:43 -04001631 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001632 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001633 if [[ "$flags" == "-e" ]]; then
1634 safe_chown -R $STACK_USER $1/*.egg-info
1635 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001636}
1637
Dean Troyerdff49a22014-01-30 15:37:40 -06001638
1639# Service Functions
1640# =================
1641
1642# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1643# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001644function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001645 echo "$1" | sed -e '
1646 s/,,/,/g;
1647 s/^,//;
1648 s/,$//
1649 '
1650}
1651
1652# disable_all_services() removes all current services
1653# from ``ENABLED_SERVICES`` to reset the configuration
1654# before a minimal installation
1655# Uses global ``ENABLED_SERVICES``
1656# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001657function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001658 ENABLED_SERVICES=""
1659}
1660
1661# Remove all services starting with '-'. For example, to install all default
1662# services except rabbit (rabbit) set in ``localrc``:
1663# ENABLED_SERVICES+=",-rabbit"
1664# Uses global ``ENABLED_SERVICES``
1665# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001666function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001667 local tmpsvcs="${ENABLED_SERVICES}"
1668 local service
1669 for service in ${tmpsvcs//,/ }; do
1670 if [[ ${service} == -* ]]; then
1671 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1672 fi
1673 done
1674 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1675}
1676
1677# disable_service() removes the services passed as argument to the
1678# ``ENABLED_SERVICES`` list, if they are present.
1679#
1680# For example:
1681# disable_service rabbit
1682#
1683# This function does not know about the special cases
1684# for nova, glance, and neutron built into is_service_enabled().
1685# Uses global ``ENABLED_SERVICES``
1686# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001687function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001688 local tmpsvcs=",${ENABLED_SERVICES},"
1689 local service
1690 for service in $@; do
1691 if is_service_enabled $service; then
1692 tmpsvcs=${tmpsvcs//,$service,/,}
1693 fi
1694 done
1695 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1696}
1697
1698# enable_service() adds the services passed as argument to the
1699# ``ENABLED_SERVICES`` list, if they are not already present.
1700#
1701# For example:
1702# enable_service qpid
1703#
1704# This function does not know about the special cases
1705# for nova, glance, and neutron built into is_service_enabled().
1706# Uses global ``ENABLED_SERVICES``
1707# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001708function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001709 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001710 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001711 for service in $@; do
1712 if ! is_service_enabled $service; then
1713 tmpsvcs+=",$service"
1714 fi
1715 done
1716 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1717 disable_negated_services
1718}
1719
1720# is_service_enabled() checks if the service(s) specified as arguments are
1721# enabled by the user in ``ENABLED_SERVICES``.
1722#
1723# Multiple services specified as arguments are ``OR``'ed together; the test
1724# is a short-circuit boolean, i.e it returns on the first match.
1725#
1726# There are special cases for some 'catch-all' services::
1727# **nova** returns true if any service enabled start with **n-**
1728# **cinder** returns true if any service enabled start with **c-**
1729# **ceilometer** returns true if any service enabled start with **ceilometer**
1730# **glance** returns true if any service enabled start with **g-**
1731# **neutron** returns true if any service enabled start with **q-**
1732# **swift** returns true if any service enabled start with **s-**
1733# **trove** returns true if any service enabled start with **tr-**
1734# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1735# **s-** services will be enabled. This will be deprecated in the future.
1736#
1737# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1738# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1739# as enabled in this case.
1740#
1741# Uses global ``ENABLED_SERVICES``
1742# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001743function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001744 local xtrace=$(set +o | grep xtrace)
1745 set +o xtrace
1746 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001747 local services=$@
1748 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001749 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001750 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001751
1752 # Look for top-level 'enabled' function for this service
1753 if type is_${service}_enabled >/dev/null 2>&1; then
1754 # A function exists for this service, use it
1755 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001756 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001757 fi
1758
1759 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1760 # are implemented
1761
Sean Dague45917cc2014-02-24 16:09:14 -05001762 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001763 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001764 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1765 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1766 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1767 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1768 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1769 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1770 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1771 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1772 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001773 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001774 done
Sean Dague45917cc2014-02-24 16:09:14 -05001775 $xtrace
1776 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001777}
1778
1779# Toggle enable/disable_service for services that must run exclusive of each other
1780# $1 The name of a variable containing a space-separated list of services
1781# $2 The name of a variable in which to store the enabled service's name
1782# $3 The name of the service to enable
1783function use_exclusive_service {
1784 local options=${!1}
1785 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001786 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001787 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001788 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001789 for opt in $options;do
1790 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1791 done
1792 eval "$out=$selection"
1793 return 0
1794}
1795
1796
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001797# System Functions
1798# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001799
1800# Only run the command if the target file (the last arg) is not on an
1801# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001802function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001803 local xtrace=$(set +o | grep xtrace)
1804 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001805 local args=( $@ )
1806 local last
1807 local sudo_cmd
1808 local dir_to_check
1809
1810 let last="${#args[*]} - 1"
1811
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001812 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001813 if [ ! -d "$dir_to_check" ]; then
1814 dir_to_check=`dirname "$dir_to_check"`
1815 fi
1816
1817 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001818 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001819 return 0
1820 fi
1821
1822 if [[ $TRACK_DEPENDS = True ]]; then
1823 sudo_cmd="env"
1824 else
1825 sudo_cmd="sudo"
1826 fi
1827
Sean Dague45917cc2014-02-24 16:09:14 -05001828 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001829 $sudo_cmd $@
1830}
1831
1832# Exit 0 if address is in network or 1 if address is not in network
1833# ip-range is in CIDR notation: 1.2.3.4/20
1834# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001835function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001836 local ip=$1
1837 local range=$2
1838 local masklen=${range#*/}
1839 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1840 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1841 [[ $network == $subnet ]]
1842}
1843
1844# Add a user to a group.
1845# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001846function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001847 local user=$1
1848 local group=$2
1849
1850 if [[ -z "$os_VENDOR" ]]; then
1851 GetOSVersion
1852 fi
1853
1854 # SLE11 and openSUSE 12.2 don't have the usual usermod
1855 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1856 sudo usermod -a -G "$group" "$user"
1857 else
1858 sudo usermod -A "$group" "$user"
1859 fi
1860}
1861
1862# Convert CIDR notation to a IPv4 netmask
1863# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001864function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001865 local maskpat="255 255 255 255"
1866 local maskdgt="254 252 248 240 224 192 128"
1867 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1868 echo ${1-0}.${2-0}.${3-0}.${4-0}
1869}
1870
1871# Gracefully cp only if source file/dir exists
1872# cp_it source destination
1873function cp_it {
1874 if [ -e $1 ] || [ -d $1 ]; then
1875 cp -pRL $1 $2
1876 fi
1877}
1878
1879# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1880# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1881# ``localrc`` or on the command line if necessary::
1882#
1883# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1884#
1885# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1886
Ian Wienandaee18c72014-02-21 15:35:08 +11001887function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001888 if [[ -n "$http_proxy" ]]; then
1889 export http_proxy=$http_proxy
1890 fi
1891 if [[ -n "$https_proxy" ]]; then
1892 export https_proxy=$https_proxy
1893 fi
1894 if [[ -n "$no_proxy" ]]; then
1895 export no_proxy=$no_proxy
1896 fi
1897}
1898
1899# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001900function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001901 local mount_type=`stat -f -L -c %T $1`
1902 test "$mount_type" == "nfs"
1903}
1904
1905# Return the network portion of the given IP address using netmask
1906# netmask is in the traditional dotted-quad format
1907# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001908function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001909 local ip=$1
1910 local mask=$2
1911 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1912 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1913 echo $subnet
1914}
1915
1916# Service wrapper to restart services
1917# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001918function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001919 if is_ubuntu; then
1920 sudo /usr/sbin/service $1 restart
1921 else
1922 sudo /sbin/service $1 restart
1923 fi
1924}
1925
1926# Only change permissions of a file or directory if it is not on an
1927# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001928function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001929 _safe_permission_operation chmod $@
1930}
1931
1932# Only change ownership of a file or directory if it is not on an NFS
1933# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001934function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001935 _safe_permission_operation chown $@
1936}
1937
1938# Service wrapper to start services
1939# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001940function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001941 if is_ubuntu; then
1942 sudo /usr/sbin/service $1 start
1943 else
1944 sudo /sbin/service $1 start
1945 fi
1946}
1947
1948# Service wrapper to stop services
1949# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001950function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001951 if is_ubuntu; then
1952 sudo /usr/sbin/service $1 stop
1953 else
1954 sudo /sbin/service $1 stop
1955 fi
1956}
1957
1958
1959# Restore xtrace
1960$XTRACE
1961
1962# Local variables:
1963# mode: shell-script
1964# End: