blob: 0ada622e89b5f9eb7467983b0f99460ef41e0170 [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
40
41# Config Functions
42# ================
43
44# Append a new option in an ini file without replacing the old value
45# iniadd config-file section option value1 value2 value3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +110046function iniadd {
Sean Dague45917cc2014-02-24 16:09:14 -050047 local xtrace=$(set +o | grep xtrace)
48 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060049 local file=$1
50 local section=$2
51 local option=$3
52 shift 3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050053
Dean Troyerdff49a22014-01-30 15:37:40 -060054 local values="$(iniget_multiline $file $section $option) $@"
55 iniset_multiline $file $section $option $values
Sean Dague45917cc2014-02-24 16:09:14 -050056 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060057}
58
59# Comment an option in an INI file
60# inicomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110061function inicomment {
Sean Dague45917cc2014-02-24 16:09:14 -050062 local xtrace=$(set +o | grep xtrace)
63 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060064 local file=$1
65 local section=$2
66 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050067
Dean Troyerdff49a22014-01-30 15:37:40 -060068 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -050069 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060070}
71
72# Get an option from an INI file
73# iniget config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110074function iniget {
Sean Dague45917cc2014-02-24 16:09:14 -050075 local xtrace=$(set +o | grep xtrace)
76 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060077 local file=$1
78 local section=$2
79 local option=$3
80 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050081
Dean Troyerdff49a22014-01-30 15:37:40 -060082 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
83 echo ${line#*=}
Sean Dague45917cc2014-02-24 16:09:14 -050084 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060085}
86
87# Get a multiple line option from an INI file
88# iniget_multiline config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110089function iniget_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -050090 local xtrace=$(set +o | grep xtrace)
91 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060092 local file=$1
93 local section=$2
94 local option=$3
95 local values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050096
Dean Troyerdff49a22014-01-30 15:37:40 -060097 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
98 echo ${values}
Sean Dague45917cc2014-02-24 16:09:14 -050099 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600100}
101
102# Determinate is the given option present in the INI file
103# ini_has_option config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100104function ini_has_option {
Sean Dague45917cc2014-02-24 16:09:14 -0500105 local xtrace=$(set +o | grep xtrace)
106 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600107 local file=$1
108 local section=$2
109 local option=$3
110 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500111
Dean Troyerdff49a22014-01-30 15:37:40 -0600112 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Sean Dague45917cc2014-02-24 16:09:14 -0500113 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600114 [ -n "$line" ]
115}
116
117# Set an option in an INI file
118# iniset config-file section option value
Ian Wienandaee18c72014-02-21 15:35:08 +1100119function iniset {
Sean Dague45917cc2014-02-24 16:09:14 -0500120 local xtrace=$(set +o | grep xtrace)
121 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600122 local file=$1
123 local section=$2
124 local option=$3
125 local value=$4
126
127 [[ -z $section || -z $option ]] && return
128
129 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
130 # Add section at the end
131 echo -e "\n[$section]" >>"$file"
132 fi
133 if ! ini_has_option "$file" "$section" "$option"; then
134 # Add it
135 sed -i -e "/^\[$section\]/ a\\
136$option = $value
137" "$file"
138 else
139 local sep=$(echo -ne "\x01")
140 # Replace it
141 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
142 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500143 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600144}
145
146# Set a multiple line option in an INI file
147# iniset_multiline config-file section option value1 value2 valu3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +1100148function iniset_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -0500149 local xtrace=$(set +o | grep xtrace)
150 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600151 local file=$1
152 local section=$2
153 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500154
Dean Troyerdff49a22014-01-30 15:37:40 -0600155 shift 3
156 local values
157 for v in $@; do
158 # The later sed command inserts each new value in the line next to
159 # the section identifier, which causes the values to be inserted in
160 # the reverse order. Do a reverse here to keep the original order.
161 values="$v ${values}"
162 done
163 if ! grep -q "^\[$section\]" "$file"; then
164 # Add section at the end
165 echo -e "\n[$section]" >>"$file"
166 else
167 # Remove old values
168 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
169 fi
170 # Add new ones
171 for v in $values; do
172 sed -i -e "/^\[$section\]/ a\\
173$option = $v
174" "$file"
175 done
Sean Dague45917cc2014-02-24 16:09:14 -0500176 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600177}
178
179# Uncomment an option in an INI file
180# iniuncomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100181function iniuncomment {
Sean Dague45917cc2014-02-24 16:09:14 -0500182 local xtrace=$(set +o | grep xtrace)
183 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600184 local file=$1
185 local section=$2
186 local option=$3
187 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500188 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600189}
190
191# Normalize config values to True or False
192# Accepts as False: 0 no No NO false False FALSE
193# Accepts as True: 1 yes Yes YES true True TRUE
194# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100195function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500196 local xtrace=$(set +o | grep xtrace)
197 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600198 local default=$1
199 local testval=$2
200
201 [[ -z "$testval" ]] && { echo "$default"; return; }
202 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
203 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
204 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500205 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600206}
207
208
209# Control Functions
210# =================
211
212# Prints backtrace info
213# filename:lineno:function
214# backtrace level
215function backtrace {
216 local level=$1
217 local deep=$((${#BASH_SOURCE[@]} - 1))
218 echo "[Call Trace]"
219 while [ $level -le $deep ]; do
220 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
221 deep=$((deep - 1))
222 done
223}
224
225# Prints line number and "message" then exits
226# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100227function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600228 local exitcode=$?
229 set +o xtrace
230 local line=$1; shift
231 if [ $exitcode == 0 ]; then
232 exitcode=1
233 fi
234 backtrace 2
235 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600236 # Give buffers a second to flush
237 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600238 exit $exitcode
239}
240
241# Checks an environment variable is not set or has length 0 OR if the
242# exit code is non-zero and prints "message" and exits
243# NOTE: env-var is the variable name without a '$'
244# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100245function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600246 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500247 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600248 set +o xtrace
249 local line=$1; shift
250 local evar=$1; shift
251 if ! is_set $evar || [ $exitcode != 0 ]; then
252 die $line "$*"
253 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500254 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600255}
256
257# Prints line number and "message" in error format
258# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100259function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600260 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500261 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600262 set +o xtrace
263 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
264 echo $msg 1>&2;
265 if [[ -n ${SCREEN_LOGDIR} ]]; then
266 echo $msg >> "${SCREEN_LOGDIR}/error.log"
267 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500268 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600269 return $exitcode
270}
271
272# Checks an environment variable is not set or has length 0 OR if the
273# exit code is non-zero and prints "message"
274# NOTE: env-var is the variable name without a '$'
275# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100276function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600277 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500278 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600279 set +o xtrace
280 local line=$1; shift
281 local evar=$1; shift
282 if ! is_set $evar || [ $exitcode != 0 ]; then
283 err $line "$*"
284 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500285 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600286 return $exitcode
287}
288
289# Exit after outputting a message about the distribution not being supported.
290# exit_distro_not_supported [optional-string-telling-what-is-missing]
291function exit_distro_not_supported {
292 if [[ -z "$DISTRO" ]]; then
293 GetDistro
294 fi
295
296 if [ $# -gt 0 ]; then
297 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
298 else
299 die $LINENO "Support for $DISTRO is incomplete."
300 fi
301}
302
303# Test if the named environment variable is set and not zero length
304# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100305function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600306 local var=\$"$1"
307 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
308}
309
310# Prints line number and "message" in warning format
311# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100312function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600313 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500314 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600315 set +o xtrace
316 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
317 echo $msg 1>&2;
318 if [[ -n ${SCREEN_LOGDIR} ]]; then
319 echo $msg >> "${SCREEN_LOGDIR}/error.log"
320 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500321 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600322 return $exitcode
323}
324
325
326# Distro Functions
327# ================
328
329# Determine OS Vendor, Release and Update
330# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
331# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500332# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
333# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
334# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
335# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
336# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
337declare os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
338
Dean Troyerdff49a22014-01-30 15:37:40 -0600339# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100340function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500341
Dean Troyerdff49a22014-01-30 15:37:40 -0600342 # Figure out which vendor we are
343 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
344 # OS/X
345 os_VENDOR=`sw_vers -productName`
346 os_RELEASE=`sw_vers -productVersion`
347 os_UPDATE=${os_RELEASE##*.}
348 os_RELEASE=${os_RELEASE%.*}
349 os_PACKAGE=""
350 if [[ "$os_RELEASE" =~ "10.7" ]]; then
351 os_CODENAME="lion"
352 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
353 os_CODENAME="snow leopard"
354 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
355 os_CODENAME="leopard"
356 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
357 os_CODENAME="tiger"
358 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
359 os_CODENAME="panther"
360 else
361 os_CODENAME=""
362 fi
363 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
364 os_VENDOR=$(lsb_release -i -s)
365 os_RELEASE=$(lsb_release -r -s)
366 os_UPDATE=""
367 os_PACKAGE="rpm"
368 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
369 os_PACKAGE="deb"
370 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
371 lsb_release -d -s | grep -q openSUSE
372 if [[ $? -eq 0 ]]; then
373 os_VENDOR="openSUSE"
374 fi
375 elif [[ $os_VENDOR == "openSUSE project" ]]; then
376 os_VENDOR="openSUSE"
377 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
378 os_VENDOR="Red Hat"
379 fi
380 os_CODENAME=$(lsb_release -c -s)
381 elif [[ -r /etc/redhat-release ]]; then
382 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
383 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
384 # CentOS release 5.5 (Final)
385 # CentOS Linux release 6.0 (Final)
386 # Fedora release 16 (Verne)
387 # XenServer release 6.2.0-70446c (xenenterprise)
388 os_CODENAME=""
389 for r in "Red Hat" CentOS Fedora XenServer; do
390 os_VENDOR=$r
391 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
392 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
393 os_CODENAME=${ver#*|}
394 os_RELEASE=${ver%|*}
395 os_UPDATE=${os_RELEASE##*.}
396 os_RELEASE=${os_RELEASE%.*}
397 break
398 fi
399 os_VENDOR=""
400 done
401 os_PACKAGE="rpm"
402 elif [[ -r /etc/SuSE-release ]]; then
403 for r in openSUSE "SUSE Linux"; do
404 if [[ "$r" = "SUSE Linux" ]]; then
405 os_VENDOR="SUSE LINUX"
406 else
407 os_VENDOR=$r
408 fi
409
410 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
411 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
412 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
413 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
414 break
415 fi
416 os_VENDOR=""
417 done
418 os_PACKAGE="rpm"
419 # If lsb_release is not installed, we should be able to detect Debian OS
420 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
421 os_VENDOR="Debian"
422 os_PACKAGE="deb"
423 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
424 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
425 fi
426 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
427}
428
429# Translate the OS version values into common nomenclature
430# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500431declare DISTRO
432
Ian Wienandaee18c72014-02-21 15:35:08 +1100433function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600434 GetOSVersion
435 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
436 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
437 DISTRO=$os_CODENAME
438 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
439 # For Fedora, just use 'f' and the release
440 DISTRO="f$os_RELEASE"
441 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
442 DISTRO="opensuse-$os_RELEASE"
443 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
444 # For SLE, also use the service pack
445 if [[ -z "$os_UPDATE" ]]; then
446 DISTRO="sle${os_RELEASE}"
447 else
448 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
449 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530450 elif [[ "$os_VENDOR" =~ (Red Hat) || \
451 "$os_VENDOR" =~ (CentOS) || \
452 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600453 # Drop the . release as we assume it's compatible
454 DISTRO="rhel${os_RELEASE::1}"
455 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
456 DISTRO="xs$os_RELEASE"
457 else
458 # Catch-all for now is Vendor + Release + Update
459 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
460 fi
461 export DISTRO
462}
463
464# Utility function for checking machine architecture
465# is_arch arch-type
466function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500467 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600468}
469
Ian Wienandbdc90c52014-08-04 15:44:58 +1000470# Quick check for a rackspace host; n.b. rackspace provided images
471# have these Xen tools installed but a custom image may not.
472function is_rackspace {
473 [ -f /usr/bin/xenstore-ls ] && \
474 sudo /usr/bin/xenstore-ls vm-data | grep -q "Rackspace"
475}
476
Dean Troyerdff49a22014-01-30 15:37:40 -0600477# Determine if current distribution is a Fedora-based distribution
478# (Fedora, RHEL, CentOS, etc).
479# is_fedora
480function is_fedora {
481 if [[ -z "$os_VENDOR" ]]; then
482 GetOSVersion
483 fi
484
anju Tiwari6c639c92014-07-15 18:11:54 +0530485 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
486 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600487}
488
489
490# Determine if current distribution is a SUSE-based distribution
491# (openSUSE, SLE).
492# is_suse
493function is_suse {
494 if [[ -z "$os_VENDOR" ]]; then
495 GetOSVersion
496 fi
497
498 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
499}
500
501
502# Determine if current distribution is an Ubuntu-based distribution
503# It will also detect non-Ubuntu but Debian-based distros
504# is_ubuntu
505function is_ubuntu {
506 if [[ -z "$os_PACKAGE" ]]; then
507 GetOSVersion
508 fi
509 [ "$os_PACKAGE" = "deb" ]
510}
511
512
513# Git Functions
514# =============
515
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600516# Returns openstack release name for a given branch name
517# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100518function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600519 local branch=$1
Dean Troyer50cda692014-07-25 11:57:20 -0500520
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600521 if [[ $branch =~ "stable/" ]]; then
522 echo ${branch#*/}
523 else
524 echo "master"
525 fi
526}
527
Dean Troyerdff49a22014-01-30 15:37:40 -0600528# git clone only if directory doesn't exist already. Since ``DEST`` might not
529# be owned by the installation user, we create the directory and change the
530# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500531# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
532# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600533# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer50cda692014-07-25 11:57:20 -0500534# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600535# git_clone remote dest-dir branch
536function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500537 local git_remote=$1
538 local git_dest=$2
539 local git_ref=$3
540 local orig_dir=$(pwd)
541
Dean Troyerdff49a22014-01-30 15:37:40 -0600542 RECLONE=$(trueorfalse False $RECLONE)
543
544 if [[ "$OFFLINE" = "True" ]]; then
545 echo "Running in offline mode, clones already exist"
546 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500547 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600548 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400549 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600550 return
551 fi
552
Dean Troyer50cda692014-07-25 11:57:20 -0500553 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600554 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500555 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600556 [[ "$ERROR_ON_CLONE" = "True" ]] && \
557 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer50cda692014-07-25 11:57:20 -0500558 git_timed clone $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600559 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500560 cd $git_dest
561 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600562 else
563 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500564 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600565 [[ "$ERROR_ON_CLONE" = "True" ]] && \
566 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer50cda692014-07-25 11:57:20 -0500567 git_timed clone $git_remote $git_dest
568 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600569 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500570 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600571 elif [[ "$RECLONE" = "True" ]]; then
572 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500573 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600574 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500575 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100576 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600577 # remove the existing ignored files (like pyc) as they cause breakage
578 # (due to the py files having older timestamps than our pyc, so python
579 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500580 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600581
Dean Troyer50cda692014-07-25 11:57:20 -0500582 # handle git_ref accordingly to type (tag, branch)
583 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
584 git_update_tag $git_ref
585 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
586 git_update_branch $git_ref
587 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
588 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600589 else
Dean Troyer50cda692014-07-25 11:57:20 -0500590 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600591 fi
592
593 fi
594 fi
595
596 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500597 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600598 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400599 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600600}
601
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100602# git can sometimes get itself infinitely stuck with transient network
603# errors or other issues with the remote end. This wraps git in a
604# timeout/retry loop and is intended to watch over non-local git
605# processes that might hang. GIT_TIMEOUT, if set, is passed directly
606# to timeout(1); otherwise the default value of 0 maintains the status
607# quo of waiting forever.
608# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100609function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100610 local count=0
611 local timeout=0
612
613 if [[ -n "${GIT_TIMEOUT}" ]]; then
614 timeout=${GIT_TIMEOUT}
615 fi
616
617 until timeout -s SIGINT ${timeout} git "$@"; do
618 # 124 is timeout(1)'s special return code when it reached the
619 # timeout; otherwise assume fatal failure
620 if [[ $? -ne 124 ]]; then
621 die $LINENO "git call failed: [git $@]"
622 fi
623
624 count=$(($count + 1))
625 warn "timeout ${count} for git call: [git $@]"
626 if [ $count -eq 3 ]; then
627 die $LINENO "Maximum of 3 git retries reached"
628 fi
629 sleep 5
630 done
631}
632
Dean Troyerdff49a22014-01-30 15:37:40 -0600633# git update using reference as a branch.
634# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100635function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500636 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600637
Dean Troyer50cda692014-07-25 11:57:20 -0500638 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600639 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500640 git branch -D $git_branch || true
641 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600642}
643
644# git update using reference as a branch.
645# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100646function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500647 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600648
Dean Troyer50cda692014-07-25 11:57:20 -0500649 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600650}
651
652# git update using reference as a tag. Be careful editing source at that repo
653# as working copy will be in a detached mode
654# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100655function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500656 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600657
Dean Troyer50cda692014-07-25 11:57:20 -0500658 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600659 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500660 git_timed fetch origin tag $git_tag
661 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600662}
663
664
665# OpenStack Functions
666# ===================
667
668# Get the default value for HOST_IP
669# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100670function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600671 local fixed_range=$1
672 local floating_range=$2
673 local host_ip_iface=$3
674 local host_ip=$4
675
676 # Find the interface used for the default route
677 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
678 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
679 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
680 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500681 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
682 local ip
683 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600684 # Attempt to filter out IP addresses that are part of the fixed and
685 # floating range. Note that this method only works if the ``netaddr``
686 # python library is installed. If it is not installed, an error
687 # will be printed and the first IP from the interface will be used.
688 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
689 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500690 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
691 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600692 break;
693 fi
694 done
695 fi
696 echo $host_ip
697}
698
Attila Fazekasf71b5002014-05-28 09:52:22 +0200699# Generates hex string from ``size`` byte of pseudo random data
700# generate_hex_string size
701function generate_hex_string {
702 local size=$1
703 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
704}
705
Dean Troyerdff49a22014-01-30 15:37:40 -0600706# Grab a numbered field from python prettytable output
707# Fields are numbered starting with 1
708# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
709# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100710function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500711 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600712 while read data; do
713 if [ "$1" -lt 0 ]; then
714 field="(\$(NF$1))"
715 else
716 field="\$$(($1 + 1))"
717 fi
718 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
719 done
720}
721
722# Add a policy to a policy.json file
723# Do nothing if the policy already exists
724# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100725function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600726 local policy_file=$1
727 local policy_name=$2
728 local policy_perm=$3
729
730 if grep -q ${policy_name} ${policy_file}; then
731 echo "Policy ${policy_name} already exists in ${policy_file}"
732 return
733 fi
734
735 # Add a terminating comma to policy lines without one
736 # Remove the closing '}' and all lines following to the end-of-file
737 local tmpfile=$(mktemp)
738 uniq ${policy_file} | sed -e '
739 s/]$/],/
740 /^[}]/,$d
741 ' > ${tmpfile}
742
743 # Append policy and closing brace
744 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
745 echo "}" >>${tmpfile}
746
747 mv ${tmpfile} ${policy_file}
748}
749
Bartosz Górski0abde392014-02-28 14:15:19 +0100750# Gets or creates user
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200751# Usage: get_or_create_user <username> <password> <project> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100752function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200753 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500754 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200755 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500756 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200757 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100758 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500759 local user_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100760 # Gets user id
761 openstack user show $1 -f value -c id 2>/dev/null ||
762 # Creates new user
763 openstack user create \
764 $1 \
765 --password "$2" \
766 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500767 $email \
Bartosz Górski0abde392014-02-28 14:15:19 +0100768 -f value -c id
769 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500770 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100771}
772
773# Gets or creates project
774# Usage: get_or_create_project <name>
775function get_or_create_project {
776 # Gets project id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500777 local project_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100778 # Gets project id
779 openstack project show $1 -f value -c id 2>/dev/null ||
780 # Creates new project if not exists
781 openstack project create $1 -f value -c id
782 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500783 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100784}
785
786# Gets or creates role
787# Usage: get_or_create_role <name>
788function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500789 local role_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100790 # Gets role id
791 openstack role show $1 -f value -c id 2>/dev/null ||
792 # Creates role if not exists
793 openstack role create $1 -f value -c id
794 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500795 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100796}
797
798# Gets or adds user role
799# Usage: get_or_add_user_role <role> <user> <project>
800function get_or_add_user_role {
801 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500802 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100803 $2 \
804 --project $3 \
805 --column "ID" \
806 --column "Name" \
807 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500808 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100809 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500810 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100811 $1 \
812 --user $2 \
813 --project $3 \
814 | grep " id " | get_field 2)
815 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500816 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100817}
818
819# Gets or creates service
820# Usage: get_or_create_service <name> <type> <description>
821function get_or_create_service {
822 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500823 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100824 # Gets service id
825 openstack service show $1 -f value -c id 2>/dev/null ||
826 # Creates new service if not exists
827 openstack service create \
828 $1 \
829 --type=$2 \
830 --description="$3" \
831 -f value -c id
832 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500833 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100834}
835
836# Gets or creates endpoint
837# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
838function get_or_create_endpoint {
839 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500840 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100841 --column "ID" \
842 --column "Region" \
843 --column "Service Name" \
844 | grep " $2 " \
845 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500846 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100847 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500848 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100849 $1 \
850 --region $2 \
851 --publicurl $3 \
852 --adminurl $4 \
853 --internalurl $5 \
854 | grep " id " | get_field 2)
855 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500856 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100857}
Dean Troyerdff49a22014-01-30 15:37:40 -0600858
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500859
Dean Troyerdff49a22014-01-30 15:37:40 -0600860# Package Functions
861# =================
862
863# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100864function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600865 local pkg_dir
866 if is_ubuntu; then
867 pkg_dir=$FILES/apts
868 elif is_fedora; then
869 pkg_dir=$FILES/rpms
870 elif is_suse; then
871 pkg_dir=$FILES/rpms-suse
872 else
873 exit_distro_not_supported "list of packages"
874 fi
875 echo "$pkg_dir"
876}
877
878# Wrapper for ``apt-get`` to set cache and proxy environment variables
879# Uses globals ``OFFLINE``, ``*_proxy``
880# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100881function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500882 local xtrace=$(set +o | grep xtrace)
883 set +o xtrace
884
Dean Troyerdff49a22014-01-30 15:37:40 -0600885 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
886 local sudo="sudo"
887 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500888
889 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600890 $sudo DEBIAN_FRONTEND=noninteractive \
891 http_proxy=$http_proxy https_proxy=$https_proxy \
892 no_proxy=$no_proxy \
893 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
894}
895
896# get_packages() collects a list of package names of any type from the
897# prerequisite files in ``files/{apts|rpms}``. The list is intended
898# to be passed to a package installer such as apt or yum.
899#
900# Only packages required for the services in 1st argument will be
901# included. Two bits of metadata are recognized in the prerequisite files:
902#
903# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
904# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
905# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100906function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500907 local xtrace=$(set +o | grep xtrace)
908 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600909 local services=$@
910 local package_dir=$(_get_package_dir)
911 local file_to_parse
912 local service
913
914 if [[ -z "$package_dir" ]]; then
915 echo "No package directory supplied"
916 return 1
917 fi
918 if [[ -z "$DISTRO" ]]; then
919 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -0500920 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -0600921 fi
922 for service in ${services//,/ }; do
923 # Allow individual services to specify dependencies
924 if [[ -e ${package_dir}/${service} ]]; then
925 file_to_parse="${file_to_parse} $service"
926 fi
927 # NOTE(sdague) n-api needs glance for now because that's where
928 # glance client is
929 if [[ $service == n-api ]]; then
930 if [[ ! $file_to_parse =~ nova ]]; then
931 file_to_parse="${file_to_parse} nova"
932 fi
933 if [[ ! $file_to_parse =~ glance ]]; then
934 file_to_parse="${file_to_parse} glance"
935 fi
936 elif [[ $service == c-* ]]; then
937 if [[ ! $file_to_parse =~ cinder ]]; then
938 file_to_parse="${file_to_parse} cinder"
939 fi
940 elif [[ $service == ceilometer-* ]]; then
941 if [[ ! $file_to_parse =~ ceilometer ]]; then
942 file_to_parse="${file_to_parse} ceilometer"
943 fi
944 elif [[ $service == s-* ]]; then
945 if [[ ! $file_to_parse =~ swift ]]; then
946 file_to_parse="${file_to_parse} swift"
947 fi
948 elif [[ $service == n-* ]]; then
949 if [[ ! $file_to_parse =~ nova ]]; then
950 file_to_parse="${file_to_parse} nova"
951 fi
952 elif [[ $service == g-* ]]; then
953 if [[ ! $file_to_parse =~ glance ]]; then
954 file_to_parse="${file_to_parse} glance"
955 fi
956 elif [[ $service == key* ]]; then
957 if [[ ! $file_to_parse =~ keystone ]]; then
958 file_to_parse="${file_to_parse} keystone"
959 fi
960 elif [[ $service == q-* ]]; then
961 if [[ ! $file_to_parse =~ neutron ]]; then
962 file_to_parse="${file_to_parse} neutron"
963 fi
Adam Gandelman539ec432014-03-18 18:57:43 -0700964 elif [[ $service == ir-* ]]; then
965 if [[ ! $file_to_parse =~ ironic ]]; then
966 file_to_parse="${file_to_parse} ironic"
967 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600968 fi
969 done
970
971 for file in ${file_to_parse}; do
972 local fname=${package_dir}/${file}
973 local OIFS line package distros distro
974 [[ -e $fname ]] || continue
975
976 OIFS=$IFS
977 IFS=$'\n'
978 for line in $(<${fname}); do
979 if [[ $line =~ "NOPRIME" ]]; then
980 continue
981 fi
982
983 # Assume we want this package
984 package=${line%#*}
985 inst_pkg=1
986
987 # Look for # dist:xxx in comment
988 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
989 # We are using BASH regexp matching feature.
990 package=${BASH_REMATCH[1]}
991 distros=${BASH_REMATCH[2]}
992 # In bash ${VAR,,} will lowecase VAR
993 # Look for a match in the distro list
994 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
995 # If no match then skip this package
996 inst_pkg=0
997 fi
998 fi
999
1000 # Look for # testonly in comment
1001 if [[ $line =~ (.*)#.*testonly.* ]]; then
1002 package=${BASH_REMATCH[1]}
1003 # Are we installing test packages? (test for the default value)
1004 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1005 # If not installing test packages the skip this package
1006 inst_pkg=0
1007 fi
1008 fi
1009
1010 if [[ $inst_pkg = 1 ]]; then
1011 echo $package
1012 fi
1013 done
1014 IFS=$OIFS
1015 done
Sean Dague45917cc2014-02-24 16:09:14 -05001016 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001017}
1018
1019# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001020# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001021# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001022function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001023 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001024 return 0
1025 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001026
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001027 if is_ubuntu; then
1028 local xtrace=$(set +o | grep xtrace)
1029 set +o xtrace
1030 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1031 # if there are transient errors pulling the updates, that's fine.
1032 # It may be secondary repositories that we don't really care about.
1033 apt_get update || /bin/true
1034 REPOS_UPDATED=True
1035 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001036 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001037 fi
1038}
1039
1040function real_install_package {
1041 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001042 apt_get install "$@"
1043 elif is_fedora; then
1044 yum_install "$@"
1045 elif is_suse; then
1046 zypper_install "$@"
1047 else
1048 exit_distro_not_supported "installing packages"
1049 fi
1050}
1051
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001052# Distro-agnostic package installer
1053# install_package package [package ...]
1054function install_package {
1055 update_package_repo
1056 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1057}
1058
Dean Troyerdff49a22014-01-30 15:37:40 -06001059# Distro-agnostic function to tell if a package is installed
1060# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001061function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001062 if [[ -z "$@" ]]; then
1063 return 1
1064 fi
1065
1066 if [[ -z "$os_PACKAGE" ]]; then
1067 GetOSVersion
1068 fi
1069
1070 if [[ "$os_PACKAGE" = "deb" ]]; then
1071 dpkg -s "$@" > /dev/null 2> /dev/null
1072 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1073 rpm --quiet -q "$@"
1074 else
1075 exit_distro_not_supported "finding if a package is installed"
1076 fi
1077}
1078
1079# Distro-agnostic package uninstaller
1080# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001081function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001082 if is_ubuntu; then
1083 apt_get purge "$@"
1084 elif is_fedora; then
1085 sudo yum remove -y "$@"
1086 elif is_suse; then
1087 sudo zypper rm "$@"
1088 else
1089 exit_distro_not_supported "uninstalling packages"
1090 fi
1091}
1092
1093# Wrapper for ``yum`` to set proxy environment variables
1094# Uses globals ``OFFLINE``, ``*_proxy``
1095# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001096function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001097 [[ "$OFFLINE" = "True" ]] && return
1098 local sudo="sudo"
1099 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001100
1101 # The manual check for missing packages is because yum -y assumes
1102 # missing packages are OK. See
1103 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001104 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1105 no_proxy=$no_proxy \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001106 yum install -y "$@" 2>&1 | \
1107 awk '
1108 BEGIN { fail=0 }
1109 /No package/ { fail=1 }
1110 { print }
1111 END { exit fail }' || \
1112 die $LINENO "Missing packages detected"
1113
1114 # also ensure we catch a yum failure
1115 if [[ ${PIPESTATUS[0]} != 0 ]]; then
1116 die $LINENO "Yum install failure"
1117 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001118}
1119
1120# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001121# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001122# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001123function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001124 [[ "$OFFLINE" = "True" ]] && return
1125 local sudo="sudo"
1126 [[ "$(id -u)" = "0" ]] && sudo="env"
1127 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1128 zypper --non-interactive install --auto-agree-with-licenses "$@"
1129}
1130
1131
1132# Process Functions
1133# =================
1134
1135# _run_process() is designed to be backgrounded by run_process() to simulate a
1136# fork. It includes the dirty work of closing extra filehandles and preparing log
1137# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001138# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001139# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001140# If an optional group is provided sg will be used to set the group of
1141# the command.
1142# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001143function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001144 local service=$1
1145 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001146 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001147
1148 # Undo logging redirections and close the extra descriptors
1149 exec 1>&3
1150 exec 2>&3
1151 exec 3>&-
1152 exec 6>&-
1153
1154 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001155 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1156 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001157
1158 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1159 export PYTHONUNBUFFERED=1
1160 fi
1161
Dean Troyer3159a822014-08-27 14:13:58 -05001162 # Run under ``setsid`` to force the process to become a session and group leader.
1163 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001164 if [[ -n "$group" ]]; then
1165 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1166 else
1167 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1168 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001169
1170 # Just silently exit this process
1171 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001172}
1173
1174# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1175# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001176# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001177# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001178function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001179 SCREEN_NAME=${SCREEN_NAME:-stack}
1180 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1181
1182 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1183 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1184 fi
1185
1186 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1187}
1188
1189# Find out if a process exists by partial name.
1190# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001191function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001192 local name=$1
1193 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001194 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001195 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001196 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001197}
1198
Dean Troyer3159a822014-08-27 14:13:58 -05001199# Run a single service under screen or directly
1200# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001201# If an optional group is provided sg will be used to run the
1202# command as that group.
1203# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001204function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001205 local service=$1
1206 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001207 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001208
Dean Troyer3159a822014-08-27 14:13:58 -05001209 if is_service_enabled $service; then
1210 if [[ "$USE_SCREEN" = "True" ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001211 screen_service "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001212 else
1213 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001214 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001215 fi
1216 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001217}
1218
1219# Helper to launch a service in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001220# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1221# ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001222# screen_service service "command-line" [group]
1223# Run a command in a shell in a screen window, if an optional group
1224# is provided, use sg to set the group of the command.
Dean Troyer3159a822014-08-27 14:13:58 -05001225function screen_service {
1226 local service=$1
1227 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001228 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001229
Sean Dagueea22a4f2014-06-27 15:21:41 -04001230 SCREEN_NAME=${SCREEN_NAME:-stack}
1231 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1232 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001233
Dean Troyer3159a822014-08-27 14:13:58 -05001234 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001235 # Append the service to the screen rc file
Dean Troyer3159a822014-08-27 14:13:58 -05001236 screen_rc "$service" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001237
Dean Troyer3159a822014-08-27 14:13:58 -05001238 screen -S $SCREEN_NAME -X screen -t $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001239
Dean Troyer3159a822014-08-27 14:13:58 -05001240 if [[ -n ${SCREEN_LOGDIR} ]]; then
1241 screen -S $SCREEN_NAME -p $service -X logfile ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log
1242 screen -S $SCREEN_NAME -p $service -X log on
1243 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001244 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001245
1246 # sleep to allow bash to be ready to be send the command - we are
1247 # creating a new window in screen and then sends characters, so if
1248 # bash isn't running by the time we send the command, nothing happens
1249 sleep 3
1250
1251 NL=`echo -ne '\015'`
1252 # This fun command does the following:
1253 # - the passed server command is backgrounded
1254 # - the pid of the background process is saved in the usual place
1255 # - the server process is brought back to the foreground
1256 # - if the server process exits prematurely the fg command errors
Dean Troyer3324f192014-09-18 09:26:39 -05001257 # and a message is written to stdout and the service failure file
1258 #
Chris Dent2f27a0e2014-09-09 13:46:02 +01001259 # The pid saved can be used in stop_process() as a process group
Dean Troyer3159a822014-08-27 14:13:58 -05001260 # id to kill off all child processes
Chris Dent2f27a0e2014-09-09 13:46:02 +01001261 if [[ -n "$group" ]]; then
1262 command="sg $group '$command'"
1263 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001264 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 -06001265 fi
1266}
1267
1268# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001269# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001270# screen_rc service "command-line"
1271function screen_rc {
1272 SCREEN_NAME=${SCREEN_NAME:-stack}
1273 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1274 if [[ ! -e $SCREENRC ]]; then
1275 # Name the screen session
1276 echo "sessionname $SCREEN_NAME" > $SCREENRC
1277 # Set a reasonable statusbar
1278 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1279 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1280 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1281 echo "screen -t shell bash" >> $SCREENRC
1282 fi
1283 # If this service doesn't already exist in the screenrc file
1284 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1285 NL=`echo -ne '\015'`
1286 echo "screen -t $1 bash" >> $SCREENRC
1287 echo "stuff \"$2$NL\"" >> $SCREENRC
1288
1289 if [[ -n ${SCREEN_LOGDIR} ]]; then
1290 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1291 echo "log on" >>$SCREENRC
1292 fi
1293 fi
1294}
1295
1296# Stop a service in screen
1297# If a PID is available use it, kill the whole process group via TERM
1298# If screen is being used kill the screen window; this will catch processes
1299# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001300# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001301# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001302function screen_stop_service {
1303 local service=$1
1304
Dean Troyerdff49a22014-01-30 15:37:40 -06001305 SCREEN_NAME=${SCREEN_NAME:-stack}
1306 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1307 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1308
Dean Troyer3159a822014-08-27 14:13:58 -05001309 if is_service_enabled $service; then
1310 # Clean up the screen window
1311 screen -S $SCREEN_NAME -p $service -X kill
1312 fi
1313}
1314
1315# Stop a service process
1316# If a PID is available use it, kill the whole process group via TERM
1317# If screen is being used kill the screen window; this will catch processes
1318# that did not leave a PID behind
1319# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1320# stop_process service
1321function stop_process {
1322 local service=$1
1323
1324 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1325 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1326
1327 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001328 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001329 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1330 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1331 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001332 fi
1333 if [[ "$USE_SCREEN" = "True" ]]; then
1334 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001335 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001336 fi
1337 fi
1338}
1339
1340# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001341# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001342# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001343function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001344 local service
1345 local failures
1346 SCREEN_NAME=${SCREEN_NAME:-stack}
1347 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1348
1349
1350 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1351 echo "No service status directory found"
1352 return
1353 fi
1354
1355 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001356 # make this -o errexit safe
1357 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001358
1359 for service in $failures; do
1360 service=`basename $service`
1361 service=${service%.failure}
1362 echo "Error: Service $service is not running"
1363 done
1364
1365 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001366 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001367 fi
1368}
1369
Chris Dent2f27a0e2014-09-09 13:46:02 +01001370# Tail a log file in a screen if USE_SCREEN is true.
1371function tail_log {
1372 local service=$1
1373 local logfile=$2
1374
1375 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1376 if [[ "$USE_SCREEN" = "True" ]]; then
1377 screen_service "$service" "sudo tail -f $logfile"
1378 fi
1379}
1380
Dean Troyerdff49a22014-01-30 15:37:40 -06001381
Dean Troyer3159a822014-08-27 14:13:58 -05001382# Deprecated Functions
1383# --------------------
1384
1385# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1386# fork. It includes the dirty work of closing extra filehandles and preparing log
1387# files to produce the same logs as screen_it(). The log filename is derived
1388# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1389# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1390# _old_run_process service "command-line"
1391function _old_run_process {
1392 local service=$1
1393 local command="$2"
1394
1395 # Undo logging redirections and close the extra descriptors
1396 exec 1>&3
1397 exec 2>&3
1398 exec 3>&-
1399 exec 6>&-
1400
1401 if [[ -n ${SCREEN_LOGDIR} ]]; then
1402 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1403 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1404
1405 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1406 export PYTHONUNBUFFERED=1
1407 fi
1408
1409 exec /bin/bash -c "$command"
1410 die "$service exec failure: $command"
1411}
1412
1413# old_run_process() launches a child process that closes all file descriptors and
1414# then exec's the passed in command. This is meant to duplicate the semantics
1415# of screen_it() without screen. PIDs are written to
1416# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1417# old_run_process service "command-line"
1418function old_run_process {
1419 local service=$1
1420 local command="$2"
1421
1422 # Spawn the child process
1423 _old_run_process "$service" "$command" &
1424 echo $!
1425}
1426
1427# Compatibility for existing start_XXXX() functions
1428# Uses global ``USE_SCREEN``
1429# screen_it service "command-line"
1430function screen_it {
1431 if is_service_enabled $1; then
1432 # Append the service to the screen rc file
1433 screen_rc "$1" "$2"
1434
1435 if [[ "$USE_SCREEN" = "True" ]]; then
1436 screen_service "$1" "$2"
1437 else
1438 # Spawn directly without screen
1439 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1440 fi
1441 fi
1442}
1443
1444# Compatibility for existing stop_XXXX() functions
1445# Stop a service in screen
1446# If a PID is available use it, kill the whole process group via TERM
1447# If screen is being used kill the screen window; this will catch processes
1448# that did not leave a PID behind
1449# screen_stop service
1450function screen_stop {
1451 # Clean up the screen window
1452 stop_process $1
1453}
1454
1455
Dean Troyerdff49a22014-01-30 15:37:40 -06001456# Python Functions
1457# ================
1458
1459# Get the path to the pip command.
1460# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001461function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001462 which pip || which pip-python
1463
1464 if [ $? -ne 0 ]; then
1465 die $LINENO "Unable to find pip; cannot continue"
1466 fi
1467}
1468
1469# Get the path to the direcotry where python executables are installed.
1470# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001471function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001472 if is_fedora || is_suse; then
1473 echo "/usr/bin"
1474 else
1475 echo "/usr/local/bin"
1476 fi
1477}
1478
1479# Wrapper for ``pip install`` to set cache and proxy environment variables
1480# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
1481# ``TRACK_DEPENDS``, ``*_proxy``
1482# pip_install package [package ...]
1483function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001484 local xtrace=$(set +o | grep xtrace)
1485 set +o xtrace
1486 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1487 $xtrace
1488 return
1489 fi
1490
Dean Troyerdff49a22014-01-30 15:37:40 -06001491 if [[ -z "$os_PACKAGE" ]]; then
1492 GetOSVersion
1493 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001494 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1495 # TRACK_DEPENDS=True installation creates a circular dependency when
1496 # we attempt to install virtualenv into a virualenv, so we must global
1497 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001498 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001499 local cmd_pip=$DEST/.venv/bin/pip
1500 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001501 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001502 local cmd_pip=$(get_pip_command)
1503 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001504 fi
1505
1506 # Mirror option not needed anymore because pypi has CDN available,
1507 # but it's useful in certain circumstances
1508 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001509 local pip_mirror_opt=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001510 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001511 pip_mirror_opt="--use-mirrors"
Dean Troyerdff49a22014-01-30 15:37:40 -06001512 fi
1513
1514 # pip < 1.4 has a bug where it will use an already existing build
1515 # directory unconditionally. Say an earlier component installs
1516 # foo v1.1; pip will have built foo's source in
1517 # /tmp/$USER-pip-build. Even if a later component specifies foo <
1518 # 1.1, the existing extracted build will be used and cause
1519 # confusing errors. By creating unique build directories we avoid
1520 # this problem. See https://github.com/pypa/pip/issues/709
1521 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1522
Sean Dague45917cc2014-02-24 16:09:14 -05001523 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001524 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001525 http_proxy=$http_proxy \
1526 https_proxy=$https_proxy \
1527 no_proxy=$no_proxy \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001528 $cmd_pip install --build=${pip_build_tmp} \
1529 $pip_mirror_opt $@ \
1530 && $sudo_pip rm -rf ${pip_build_tmp}
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001531
1532 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1533 local test_req="$@/test-requirements.txt"
1534 if [[ -e "$test_req" ]]; then
1535 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1536 http_proxy=$http_proxy \
1537 https_proxy=$https_proxy \
1538 no_proxy=$no_proxy \
1539 $cmd_pip install --build=${pip_build_tmp} \
1540 $pip_mirror_opt -r $test_req \
1541 && $sudo_pip rm -rf ${pip_build_tmp}
1542 fi
1543 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001544}
1545
Sean Dague099e5e32014-03-31 10:35:43 -04001546# this should be used if you want to install globally, all libraries should
1547# use this, especially *oslo* ones
1548function setup_install {
1549 local project_dir=$1
1550 setup_package_with_req_sync $project_dir
1551}
1552
1553# this should be used for projects which run services, like all services
1554function setup_develop {
1555 local project_dir=$1
1556 setup_package_with_req_sync $project_dir -e
1557}
1558
Dean Troyeraf616d92014-02-17 12:57:55 -06001559# ``pip install -e`` the package, which processes the dependencies
1560# using pip before running `setup.py develop`
1561#
1562# Updates the dependencies in project_dir from the
1563# openstack/requirements global list before installing anything.
1564#
1565# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1566# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001567function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001568 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001569 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001570
Dean Troyeraf616d92014-02-17 12:57:55 -06001571 # Don't update repo if local changes exist
1572 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001573 # ``errexit`` requires us to trap the exit code when the repo is changed
1574 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001575
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001576 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001577 (cd $REQUIREMENTS_DIR; \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001578 python update.py $project_dir)
Dean Troyeraf616d92014-02-17 12:57:55 -06001579 fi
1580
Sean Dague099e5e32014-03-31 10:35:43 -04001581 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001582
1583 # We've just gone and possibly modified the user's source tree in an
1584 # automated way, which is considered bad form if it's a development
1585 # tree because we've screwed up their next git checkin. So undo it.
1586 #
1587 # However... there are some circumstances, like running in the gate
1588 # where we really really want the overridden version to stick. So provide
1589 # a variable that tells us whether or not we should UNDO the requirements
1590 # changes (this will be set to False in the OpenStack ci gate)
1591 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001592 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001593 (cd $project_dir && git reset --hard)
1594 fi
1595 fi
1596}
1597
1598# ``pip install -e`` the package, which processes the dependencies
1599# using pip before running `setup.py develop`
1600# Uses globals ``STACK_USER``
1601# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001602function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001603 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001604 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001605
Sean Dague099e5e32014-03-31 10:35:43 -04001606 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001607 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001608 if [[ "$flags" == "-e" ]]; then
1609 safe_chown -R $STACK_USER $1/*.egg-info
1610 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001611}
1612
Dean Troyerdff49a22014-01-30 15:37:40 -06001613
1614# Service Functions
1615# =================
1616
1617# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1618# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001619function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001620 echo "$1" | sed -e '
1621 s/,,/,/g;
1622 s/^,//;
1623 s/,$//
1624 '
1625}
1626
1627# disable_all_services() removes all current services
1628# from ``ENABLED_SERVICES`` to reset the configuration
1629# before a minimal installation
1630# Uses global ``ENABLED_SERVICES``
1631# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001632function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001633 ENABLED_SERVICES=""
1634}
1635
1636# Remove all services starting with '-'. For example, to install all default
1637# services except rabbit (rabbit) set in ``localrc``:
1638# ENABLED_SERVICES+=",-rabbit"
1639# Uses global ``ENABLED_SERVICES``
1640# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001641function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001642 local tmpsvcs="${ENABLED_SERVICES}"
1643 local service
1644 for service in ${tmpsvcs//,/ }; do
1645 if [[ ${service} == -* ]]; then
1646 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1647 fi
1648 done
1649 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1650}
1651
1652# disable_service() removes the services passed as argument to the
1653# ``ENABLED_SERVICES`` list, if they are present.
1654#
1655# For example:
1656# disable_service rabbit
1657#
1658# This function does not know about the special cases
1659# for nova, glance, and neutron built into is_service_enabled().
1660# Uses global ``ENABLED_SERVICES``
1661# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001662function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001663 local tmpsvcs=",${ENABLED_SERVICES},"
1664 local service
1665 for service in $@; do
1666 if is_service_enabled $service; then
1667 tmpsvcs=${tmpsvcs//,$service,/,}
1668 fi
1669 done
1670 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1671}
1672
1673# enable_service() adds the services passed as argument to the
1674# ``ENABLED_SERVICES`` list, if they are not already present.
1675#
1676# For example:
1677# enable_service qpid
1678#
1679# This function does not know about the special cases
1680# for nova, glance, and neutron built into is_service_enabled().
1681# Uses global ``ENABLED_SERVICES``
1682# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001683function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001684 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001685 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001686 for service in $@; do
1687 if ! is_service_enabled $service; then
1688 tmpsvcs+=",$service"
1689 fi
1690 done
1691 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1692 disable_negated_services
1693}
1694
1695# is_service_enabled() checks if the service(s) specified as arguments are
1696# enabled by the user in ``ENABLED_SERVICES``.
1697#
1698# Multiple services specified as arguments are ``OR``'ed together; the test
1699# is a short-circuit boolean, i.e it returns on the first match.
1700#
1701# There are special cases for some 'catch-all' services::
1702# **nova** returns true if any service enabled start with **n-**
1703# **cinder** returns true if any service enabled start with **c-**
1704# **ceilometer** returns true if any service enabled start with **ceilometer**
1705# **glance** returns true if any service enabled start with **g-**
1706# **neutron** returns true if any service enabled start with **q-**
1707# **swift** returns true if any service enabled start with **s-**
1708# **trove** returns true if any service enabled start with **tr-**
1709# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1710# **s-** services will be enabled. This will be deprecated in the future.
1711#
1712# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1713# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1714# as enabled in this case.
1715#
1716# Uses global ``ENABLED_SERVICES``
1717# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001718function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001719 local xtrace=$(set +o | grep xtrace)
1720 set +o xtrace
1721 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001722 local services=$@
1723 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001724 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001725 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001726
1727 # Look for top-level 'enabled' function for this service
1728 if type is_${service}_enabled >/dev/null 2>&1; then
1729 # A function exists for this service, use it
1730 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001731 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001732 fi
1733
1734 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1735 # are implemented
1736
Sean Dague45917cc2014-02-24 16:09:14 -05001737 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001738 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001739 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1740 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1741 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1742 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1743 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1744 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1745 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1746 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1747 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001748 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001749 done
Sean Dague45917cc2014-02-24 16:09:14 -05001750 $xtrace
1751 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001752}
1753
1754# Toggle enable/disable_service for services that must run exclusive of each other
1755# $1 The name of a variable containing a space-separated list of services
1756# $2 The name of a variable in which to store the enabled service's name
1757# $3 The name of the service to enable
1758function use_exclusive_service {
1759 local options=${!1}
1760 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001761 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001762 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001763 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001764 for opt in $options;do
1765 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1766 done
1767 eval "$out=$selection"
1768 return 0
1769}
1770
1771
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001772# System Functions
1773# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001774
1775# Only run the command if the target file (the last arg) is not on an
1776# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001777function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001778 local xtrace=$(set +o | grep xtrace)
1779 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001780 local args=( $@ )
1781 local last
1782 local sudo_cmd
1783 local dir_to_check
1784
1785 let last="${#args[*]} - 1"
1786
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001787 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001788 if [ ! -d "$dir_to_check" ]; then
1789 dir_to_check=`dirname "$dir_to_check"`
1790 fi
1791
1792 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001793 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001794 return 0
1795 fi
1796
1797 if [[ $TRACK_DEPENDS = True ]]; then
1798 sudo_cmd="env"
1799 else
1800 sudo_cmd="sudo"
1801 fi
1802
Sean Dague45917cc2014-02-24 16:09:14 -05001803 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001804 $sudo_cmd $@
1805}
1806
1807# Exit 0 if address is in network or 1 if address is not in network
1808# ip-range is in CIDR notation: 1.2.3.4/20
1809# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001810function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001811 local ip=$1
1812 local range=$2
1813 local masklen=${range#*/}
1814 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1815 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1816 [[ $network == $subnet ]]
1817}
1818
1819# Add a user to a group.
1820# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001821function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001822 local user=$1
1823 local group=$2
1824
1825 if [[ -z "$os_VENDOR" ]]; then
1826 GetOSVersion
1827 fi
1828
1829 # SLE11 and openSUSE 12.2 don't have the usual usermod
1830 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1831 sudo usermod -a -G "$group" "$user"
1832 else
1833 sudo usermod -A "$group" "$user"
1834 fi
1835}
1836
1837# Convert CIDR notation to a IPv4 netmask
1838# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001839function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001840 local maskpat="255 255 255 255"
1841 local maskdgt="254 252 248 240 224 192 128"
1842 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1843 echo ${1-0}.${2-0}.${3-0}.${4-0}
1844}
1845
1846# Gracefully cp only if source file/dir exists
1847# cp_it source destination
1848function cp_it {
1849 if [ -e $1 ] || [ -d $1 ]; then
1850 cp -pRL $1 $2
1851 fi
1852}
1853
1854# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1855# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1856# ``localrc`` or on the command line if necessary::
1857#
1858# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1859#
1860# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1861
Ian Wienandaee18c72014-02-21 15:35:08 +11001862function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001863 if [[ -n "$http_proxy" ]]; then
1864 export http_proxy=$http_proxy
1865 fi
1866 if [[ -n "$https_proxy" ]]; then
1867 export https_proxy=$https_proxy
1868 fi
1869 if [[ -n "$no_proxy" ]]; then
1870 export no_proxy=$no_proxy
1871 fi
1872}
1873
1874# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001875function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001876 local mount_type=`stat -f -L -c %T $1`
1877 test "$mount_type" == "nfs"
1878}
1879
1880# Return the network portion of the given IP address using netmask
1881# netmask is in the traditional dotted-quad format
1882# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001883function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001884 local ip=$1
1885 local mask=$2
1886 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1887 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1888 echo $subnet
1889}
1890
1891# Service wrapper to restart services
1892# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001893function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001894 if is_ubuntu; then
1895 sudo /usr/sbin/service $1 restart
1896 else
1897 sudo /sbin/service $1 restart
1898 fi
1899}
1900
1901# Only change permissions of a file or directory if it is not on an
1902# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001903function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001904 _safe_permission_operation chmod $@
1905}
1906
1907# Only change ownership of a file or directory if it is not on an NFS
1908# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001909function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001910 _safe_permission_operation chown $@
1911}
1912
1913# Service wrapper to start services
1914# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001915function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001916 if is_ubuntu; then
1917 sudo /usr/sbin/service $1 start
1918 else
1919 sudo /sbin/service $1 start
1920 fi
1921}
1922
1923# Service wrapper to stop services
1924# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001925function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001926 if is_ubuntu; then
1927 sudo /usr/sbin/service $1 stop
1928 else
1929 sudo /sbin/service $1 stop
1930 fi
1931}
1932
1933
1934# Restore xtrace
1935$XTRACE
1936
1937# Local variables:
1938# mode: shell-script
1939# End: