blob: 6b1f473004736d2766d31b088adff5715b017001 [file] [log] [blame]
Dean Troyerdff49a22014-01-30 15:37:40 -06001# functions-common - Common functions used by DevStack components
2#
3# The canonical copy of this file is maintained in the DevStack repo.
4# All modifications should be made there and then sync'ed to other repos
5# as required.
6#
7# This file is sorted alphabetically within the function groups.
8#
9# - Config Functions
10# - Control Functions
11# - Distro Functions
12# - Git Functions
13# - OpenStack Functions
14# - Package Functions
15# - Process Functions
16# - Python Functions
17# - Service Functions
Masayuki Igawaf6368d32014-02-20 13:31:26 +090018# - System Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060019#
20# The following variables are assumed to be defined by certain functions:
21#
22# - ``ENABLED_SERVICES``
23# - ``ERROR_ON_CLONE``
24# - ``FILES``
25# - ``OFFLINE``
26# - ``PIP_DOWNLOAD_CACHE``
27# - ``PIP_USE_MIRRORS``
28# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090029# - ``REQUIREMENTS_DIR``
30# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060031# - ``TRACK_DEPENDS``
Masayuki Igawad20f6322014-02-28 09:22:37 +090032# - ``UNDO_REQUIREMENTS``
Dean Troyerdff49a22014-01-30 15:37:40 -060033# - ``http_proxy``, ``https_proxy``, ``no_proxy``
34
35# Save trace setting
36XTRACE=$(set +o | grep xtrace)
37set +o xtrace
38
39
40# Config Functions
41# ================
42
43# Append a new option in an ini file without replacing the old value
44# iniadd config-file section option value1 value2 value3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +110045function iniadd {
Sean Dague45917cc2014-02-24 16:09:14 -050046 local xtrace=$(set +o | grep xtrace)
47 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060048 local file=$1
49 local section=$2
50 local option=$3
51 shift 3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050052
Dean Troyerdff49a22014-01-30 15:37:40 -060053 local values="$(iniget_multiline $file $section $option) $@"
54 iniset_multiline $file $section $option $values
Sean Dague45917cc2014-02-24 16:09:14 -050055 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060056}
57
58# Comment an option in an INI file
59# inicomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110060function inicomment {
Sean Dague45917cc2014-02-24 16:09:14 -050061 local xtrace=$(set +o | grep xtrace)
62 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060063 local file=$1
64 local section=$2
65 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050066
Dean Troyerdff49a22014-01-30 15:37:40 -060067 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -050068 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060069}
70
71# Get an option from an INI file
72# iniget config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110073function iniget {
Sean Dague45917cc2014-02-24 16:09:14 -050074 local xtrace=$(set +o | grep xtrace)
75 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060076 local file=$1
77 local section=$2
78 local option=$3
79 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050080
Dean Troyerdff49a22014-01-30 15:37:40 -060081 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
82 echo ${line#*=}
Sean Dague45917cc2014-02-24 16:09:14 -050083 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060084}
85
86# Get a multiple line option from an INI file
87# iniget_multiline config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110088function iniget_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -050089 local xtrace=$(set +o | grep xtrace)
90 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060091 local file=$1
92 local section=$2
93 local option=$3
94 local values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050095
Dean Troyerdff49a22014-01-30 15:37:40 -060096 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
97 echo ${values}
Sean Dague45917cc2014-02-24 16:09:14 -050098 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060099}
100
101# Determinate is the given option present in the INI file
102# ini_has_option config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100103function ini_has_option {
Sean Dague45917cc2014-02-24 16:09:14 -0500104 local xtrace=$(set +o | grep xtrace)
105 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600106 local file=$1
107 local section=$2
108 local option=$3
109 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500110
Dean Troyerdff49a22014-01-30 15:37:40 -0600111 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Sean Dague45917cc2014-02-24 16:09:14 -0500112 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600113 [ -n "$line" ]
114}
115
116# Set an option in an INI file
117# iniset config-file section option value
Ian Wienandaee18c72014-02-21 15:35:08 +1100118function iniset {
Sean Dague45917cc2014-02-24 16:09:14 -0500119 local xtrace=$(set +o | grep xtrace)
120 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600121 local file=$1
122 local section=$2
123 local option=$3
124 local value=$4
125
126 [[ -z $section || -z $option ]] && return
127
128 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
129 # Add section at the end
130 echo -e "\n[$section]" >>"$file"
131 fi
132 if ! ini_has_option "$file" "$section" "$option"; then
133 # Add it
134 sed -i -e "/^\[$section\]/ a\\
135$option = $value
136" "$file"
137 else
138 local sep=$(echo -ne "\x01")
139 # Replace it
140 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
141 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500142 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600143}
144
145# Set a multiple line option in an INI file
146# iniset_multiline config-file section option value1 value2 valu3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +1100147function iniset_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -0500148 local xtrace=$(set +o | grep xtrace)
149 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600150 local file=$1
151 local section=$2
152 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500153
Dean Troyerdff49a22014-01-30 15:37:40 -0600154 shift 3
155 local values
156 for v in $@; do
157 # The later sed command inserts each new value in the line next to
158 # the section identifier, which causes the values to be inserted in
159 # the reverse order. Do a reverse here to keep the original order.
160 values="$v ${values}"
161 done
162 if ! grep -q "^\[$section\]" "$file"; then
163 # Add section at the end
164 echo -e "\n[$section]" >>"$file"
165 else
166 # Remove old values
167 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
168 fi
169 # Add new ones
170 for v in $values; do
171 sed -i -e "/^\[$section\]/ a\\
172$option = $v
173" "$file"
174 done
Sean Dague45917cc2014-02-24 16:09:14 -0500175 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600176}
177
178# Uncomment an option in an INI file
179# iniuncomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100180function iniuncomment {
Sean Dague45917cc2014-02-24 16:09:14 -0500181 local xtrace=$(set +o | grep xtrace)
182 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600183 local file=$1
184 local section=$2
185 local option=$3
186 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500187 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600188}
189
190# Normalize config values to True or False
191# Accepts as False: 0 no No NO false False FALSE
192# Accepts as True: 1 yes Yes YES true True TRUE
193# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100194function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500195 local xtrace=$(set +o | grep xtrace)
196 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600197 local default=$1
198 local testval=$2
199
200 [[ -z "$testval" ]] && { echo "$default"; return; }
201 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
202 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
203 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500204 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600205}
206
207
208# Control Functions
209# =================
210
211# Prints backtrace info
212# filename:lineno:function
213# backtrace level
214function backtrace {
215 local level=$1
216 local deep=$((${#BASH_SOURCE[@]} - 1))
217 echo "[Call Trace]"
218 while [ $level -le $deep ]; do
219 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
220 deep=$((deep - 1))
221 done
222}
223
224# Prints line number and "message" then exits
225# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100226function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600227 local exitcode=$?
228 set +o xtrace
229 local line=$1; shift
230 if [ $exitcode == 0 ]; then
231 exitcode=1
232 fi
233 backtrace 2
234 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600235 # Give buffers a second to flush
236 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600237 exit $exitcode
238}
239
240# Checks an environment variable is not set or has length 0 OR if the
241# exit code is non-zero and prints "message" and exits
242# NOTE: env-var is the variable name without a '$'
243# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100244function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600245 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500246 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600247 set +o xtrace
248 local line=$1; shift
249 local evar=$1; shift
250 if ! is_set $evar || [ $exitcode != 0 ]; then
251 die $line "$*"
252 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500253 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600254}
255
256# Prints line number and "message" in error format
257# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100258function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600259 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500260 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600261 set +o xtrace
262 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
263 echo $msg 1>&2;
264 if [[ -n ${SCREEN_LOGDIR} ]]; then
265 echo $msg >> "${SCREEN_LOGDIR}/error.log"
266 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500267 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600268 return $exitcode
269}
270
271# Checks an environment variable is not set or has length 0 OR if the
272# exit code is non-zero and prints "message"
273# NOTE: env-var is the variable name without a '$'
274# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100275function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600276 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500277 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600278 set +o xtrace
279 local line=$1; shift
280 local evar=$1; shift
281 if ! is_set $evar || [ $exitcode != 0 ]; then
282 err $line "$*"
283 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500284 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600285 return $exitcode
286}
287
288# Exit after outputting a message about the distribution not being supported.
289# exit_distro_not_supported [optional-string-telling-what-is-missing]
290function exit_distro_not_supported {
291 if [[ -z "$DISTRO" ]]; then
292 GetDistro
293 fi
294
295 if [ $# -gt 0 ]; then
296 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
297 else
298 die $LINENO "Support for $DISTRO is incomplete."
299 fi
300}
301
302# Test if the named environment variable is set and not zero length
303# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100304function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600305 local var=\$"$1"
306 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
307}
308
309# Prints line number and "message" in warning format
310# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100311function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600312 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500313 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600314 set +o xtrace
315 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
316 echo $msg 1>&2;
317 if [[ -n ${SCREEN_LOGDIR} ]]; then
318 echo $msg >> "${SCREEN_LOGDIR}/error.log"
319 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500320 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600321 return $exitcode
322}
323
324
325# Distro Functions
326# ================
327
328# Determine OS Vendor, Release and Update
329# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
330# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500331# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
332# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
333# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
334# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
335# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
336declare os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
337
Dean Troyerdff49a22014-01-30 15:37:40 -0600338# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100339function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500340
Dean Troyerdff49a22014-01-30 15:37:40 -0600341 # Figure out which vendor we are
342 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
343 # OS/X
344 os_VENDOR=`sw_vers -productName`
345 os_RELEASE=`sw_vers -productVersion`
346 os_UPDATE=${os_RELEASE##*.}
347 os_RELEASE=${os_RELEASE%.*}
348 os_PACKAGE=""
349 if [[ "$os_RELEASE" =~ "10.7" ]]; then
350 os_CODENAME="lion"
351 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
352 os_CODENAME="snow leopard"
353 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
354 os_CODENAME="leopard"
355 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
356 os_CODENAME="tiger"
357 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
358 os_CODENAME="panther"
359 else
360 os_CODENAME=""
361 fi
362 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
363 os_VENDOR=$(lsb_release -i -s)
364 os_RELEASE=$(lsb_release -r -s)
365 os_UPDATE=""
366 os_PACKAGE="rpm"
367 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
368 os_PACKAGE="deb"
369 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
370 lsb_release -d -s | grep -q openSUSE
371 if [[ $? -eq 0 ]]; then
372 os_VENDOR="openSUSE"
373 fi
374 elif [[ $os_VENDOR == "openSUSE project" ]]; then
375 os_VENDOR="openSUSE"
376 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
377 os_VENDOR="Red Hat"
378 fi
379 os_CODENAME=$(lsb_release -c -s)
380 elif [[ -r /etc/redhat-release ]]; then
381 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
382 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
383 # CentOS release 5.5 (Final)
384 # CentOS Linux release 6.0 (Final)
385 # Fedora release 16 (Verne)
386 # XenServer release 6.2.0-70446c (xenenterprise)
387 os_CODENAME=""
388 for r in "Red Hat" CentOS Fedora XenServer; do
389 os_VENDOR=$r
390 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
391 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
392 os_CODENAME=${ver#*|}
393 os_RELEASE=${ver%|*}
394 os_UPDATE=${os_RELEASE##*.}
395 os_RELEASE=${os_RELEASE%.*}
396 break
397 fi
398 os_VENDOR=""
399 done
400 os_PACKAGE="rpm"
401 elif [[ -r /etc/SuSE-release ]]; then
402 for r in openSUSE "SUSE Linux"; do
403 if [[ "$r" = "SUSE Linux" ]]; then
404 os_VENDOR="SUSE LINUX"
405 else
406 os_VENDOR=$r
407 fi
408
409 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
410 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
411 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
412 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
413 break
414 fi
415 os_VENDOR=""
416 done
417 os_PACKAGE="rpm"
418 # If lsb_release is not installed, we should be able to detect Debian OS
419 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
420 os_VENDOR="Debian"
421 os_PACKAGE="deb"
422 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
423 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
424 fi
425 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
426}
427
428# Translate the OS version values into common nomenclature
429# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500430declare DISTRO
431
Ian Wienandaee18c72014-02-21 15:35:08 +1100432function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600433 GetOSVersion
434 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
435 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
436 DISTRO=$os_CODENAME
437 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
438 # For Fedora, just use 'f' and the release
439 DISTRO="f$os_RELEASE"
440 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
441 DISTRO="opensuse-$os_RELEASE"
442 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
443 # For SLE, also use the service pack
444 if [[ -z "$os_UPDATE" ]]; then
445 DISTRO="sle${os_RELEASE}"
446 else
447 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
448 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530449 elif [[ "$os_VENDOR" =~ (Red Hat) || \
450 "$os_VENDOR" =~ (CentOS) || \
451 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600452 # Drop the . release as we assume it's compatible
453 DISTRO="rhel${os_RELEASE::1}"
454 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
455 DISTRO="xs$os_RELEASE"
456 else
457 # Catch-all for now is Vendor + Release + Update
458 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
459 fi
460 export DISTRO
461}
462
463# Utility function for checking machine architecture
464# is_arch arch-type
465function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500466 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600467}
468
Ian Wienandbdc90c52014-08-04 15:44:58 +1000469# Quick check for a rackspace host; n.b. rackspace provided images
470# have these Xen tools installed but a custom image may not.
471function is_rackspace {
472 [ -f /usr/bin/xenstore-ls ] && \
473 sudo /usr/bin/xenstore-ls vm-data | grep -q "Rackspace"
474}
475
Dean Troyerdff49a22014-01-30 15:37:40 -0600476# Determine if current distribution is a Fedora-based distribution
477# (Fedora, RHEL, CentOS, etc).
478# is_fedora
479function is_fedora {
480 if [[ -z "$os_VENDOR" ]]; then
481 GetOSVersion
482 fi
483
anju Tiwari6c639c92014-07-15 18:11:54 +0530484 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
485 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600486}
487
488
489# Determine if current distribution is a SUSE-based distribution
490# (openSUSE, SLE).
491# is_suse
492function is_suse {
493 if [[ -z "$os_VENDOR" ]]; then
494 GetOSVersion
495 fi
496
497 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
498}
499
500
501# Determine if current distribution is an Ubuntu-based distribution
502# It will also detect non-Ubuntu but Debian-based distros
503# is_ubuntu
504function is_ubuntu {
505 if [[ -z "$os_PACKAGE" ]]; then
506 GetOSVersion
507 fi
508 [ "$os_PACKAGE" = "deb" ]
509}
510
511
512# Git Functions
513# =============
514
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600515# Returns openstack release name for a given branch name
516# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100517function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600518 local branch=$1
Dean Troyer50cda692014-07-25 11:57:20 -0500519
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600520 if [[ $branch =~ "stable/" ]]; then
521 echo ${branch#*/}
522 else
523 echo "master"
524 fi
525}
526
Dean Troyerdff49a22014-01-30 15:37:40 -0600527# git clone only if directory doesn't exist already. Since ``DEST`` might not
528# be owned by the installation user, we create the directory and change the
529# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500530# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
531# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600532# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer50cda692014-07-25 11:57:20 -0500533# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600534# git_clone remote dest-dir branch
535function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500536 local git_remote=$1
537 local git_dest=$2
538 local git_ref=$3
539 local orig_dir=$(pwd)
540
Dean Troyerdff49a22014-01-30 15:37:40 -0600541 RECLONE=$(trueorfalse False $RECLONE)
542
543 if [[ "$OFFLINE" = "True" ]]; then
544 echo "Running in offline mode, clones already exist"
545 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500546 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600547 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400548 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600549 return
550 fi
551
Dean Troyer50cda692014-07-25 11:57:20 -0500552 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600553 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500554 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600555 [[ "$ERROR_ON_CLONE" = "True" ]] && \
556 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer50cda692014-07-25 11:57:20 -0500557 git_timed clone $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600558 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500559 cd $git_dest
560 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600561 else
562 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500563 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600564 [[ "$ERROR_ON_CLONE" = "True" ]] && \
565 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer50cda692014-07-25 11:57:20 -0500566 git_timed clone $git_remote $git_dest
567 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600568 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500569 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600570 elif [[ "$RECLONE" = "True" ]]; then
571 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500572 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600573 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500574 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100575 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600576 # remove the existing ignored files (like pyc) as they cause breakage
577 # (due to the py files having older timestamps than our pyc, so python
578 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500579 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600580
Dean Troyer50cda692014-07-25 11:57:20 -0500581 # handle git_ref accordingly to type (tag, branch)
582 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
583 git_update_tag $git_ref
584 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
585 git_update_branch $git_ref
586 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
587 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600588 else
Dean Troyer50cda692014-07-25 11:57:20 -0500589 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600590 fi
591
592 fi
593 fi
594
595 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500596 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600597 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400598 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600599}
600
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100601# git can sometimes get itself infinitely stuck with transient network
602# errors or other issues with the remote end. This wraps git in a
603# timeout/retry loop and is intended to watch over non-local git
604# processes that might hang. GIT_TIMEOUT, if set, is passed directly
605# to timeout(1); otherwise the default value of 0 maintains the status
606# quo of waiting forever.
607# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100608function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100609 local count=0
610 local timeout=0
611
612 if [[ -n "${GIT_TIMEOUT}" ]]; then
613 timeout=${GIT_TIMEOUT}
614 fi
615
616 until timeout -s SIGINT ${timeout} git "$@"; do
617 # 124 is timeout(1)'s special return code when it reached the
618 # timeout; otherwise assume fatal failure
619 if [[ $? -ne 124 ]]; then
620 die $LINENO "git call failed: [git $@]"
621 fi
622
623 count=$(($count + 1))
624 warn "timeout ${count} for git call: [git $@]"
625 if [ $count -eq 3 ]; then
626 die $LINENO "Maximum of 3 git retries reached"
627 fi
628 sleep 5
629 done
630}
631
Dean Troyerdff49a22014-01-30 15:37:40 -0600632# git update using reference as a branch.
633# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100634function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500635 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600636
Dean Troyer50cda692014-07-25 11:57:20 -0500637 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600638 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500639 git branch -D $git_branch || true
640 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600641}
642
643# git update using reference as a branch.
644# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100645function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500646 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600647
Dean Troyer50cda692014-07-25 11:57:20 -0500648 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600649}
650
651# git update using reference as a tag. Be careful editing source at that repo
652# as working copy will be in a detached mode
653# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100654function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500655 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600656
Dean Troyer50cda692014-07-25 11:57:20 -0500657 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600658 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500659 git_timed fetch origin tag $git_tag
660 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600661}
662
663
664# OpenStack Functions
665# ===================
666
667# Get the default value for HOST_IP
668# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100669function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600670 local fixed_range=$1
671 local floating_range=$2
672 local host_ip_iface=$3
673 local host_ip=$4
674
675 # Find the interface used for the default route
676 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
677 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
678 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
679 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500680 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
681 local ip
682 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600683 # Attempt to filter out IP addresses that are part of the fixed and
684 # floating range. Note that this method only works if the ``netaddr``
685 # python library is installed. If it is not installed, an error
686 # will be printed and the first IP from the interface will be used.
687 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
688 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500689 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
690 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600691 break;
692 fi
693 done
694 fi
695 echo $host_ip
696}
697
Attila Fazekasf71b5002014-05-28 09:52:22 +0200698# Generates hex string from ``size`` byte of pseudo random data
699# generate_hex_string size
700function generate_hex_string {
701 local size=$1
702 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
703}
704
Dean Troyerdff49a22014-01-30 15:37:40 -0600705# Grab a numbered field from python prettytable output
706# Fields are numbered starting with 1
707# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
708# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100709function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500710 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600711 while read data; do
712 if [ "$1" -lt 0 ]; then
713 field="(\$(NF$1))"
714 else
715 field="\$$(($1 + 1))"
716 fi
717 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
718 done
719}
720
721# Add a policy to a policy.json file
722# Do nothing if the policy already exists
723# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100724function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600725 local policy_file=$1
726 local policy_name=$2
727 local policy_perm=$3
728
729 if grep -q ${policy_name} ${policy_file}; then
730 echo "Policy ${policy_name} already exists in ${policy_file}"
731 return
732 fi
733
734 # Add a terminating comma to policy lines without one
735 # Remove the closing '}' and all lines following to the end-of-file
736 local tmpfile=$(mktemp)
737 uniq ${policy_file} | sed -e '
738 s/]$/],/
739 /^[}]/,$d
740 ' > ${tmpfile}
741
742 # Append policy and closing brace
743 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
744 echo "}" >>${tmpfile}
745
746 mv ${tmpfile} ${policy_file}
747}
748
Bartosz Górski0abde392014-02-28 14:15:19 +0100749# Gets or creates user
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200750# Usage: get_or_create_user <username> <password> <project> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100751function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200752 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500753 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200754 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500755 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200756 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100757 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500758 local user_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100759 # Gets user id
760 openstack user show $1 -f value -c id 2>/dev/null ||
761 # Creates new user
762 openstack user create \
763 $1 \
764 --password "$2" \
765 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500766 $email \
Bartosz Górski0abde392014-02-28 14:15:19 +0100767 -f value -c id
768 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500769 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100770}
771
772# Gets or creates project
773# Usage: get_or_create_project <name>
774function get_or_create_project {
775 # Gets project id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500776 local project_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100777 # Gets project id
778 openstack project show $1 -f value -c id 2>/dev/null ||
779 # Creates new project if not exists
780 openstack project create $1 -f value -c id
781 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500782 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100783}
784
785# Gets or creates role
786# Usage: get_or_create_role <name>
787function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500788 local role_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100789 # Gets role id
790 openstack role show $1 -f value -c id 2>/dev/null ||
791 # Creates role if not exists
792 openstack role create $1 -f value -c id
793 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500794 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100795}
796
797# Gets or adds user role
798# Usage: get_or_add_user_role <role> <user> <project>
799function get_or_add_user_role {
800 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500801 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100802 $2 \
803 --project $3 \
804 --column "ID" \
805 --column "Name" \
806 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500807 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100808 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500809 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100810 $1 \
811 --user $2 \
812 --project $3 \
813 | grep " id " | get_field 2)
814 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500815 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100816}
817
818# Gets or creates service
819# Usage: get_or_create_service <name> <type> <description>
820function get_or_create_service {
821 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500822 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100823 # Gets service id
824 openstack service show $1 -f value -c id 2>/dev/null ||
825 # Creates new service if not exists
826 openstack service create \
827 $1 \
828 --type=$2 \
829 --description="$3" \
830 -f value -c id
831 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500832 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100833}
834
835# Gets or creates endpoint
836# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
837function get_or_create_endpoint {
838 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500839 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100840 --column "ID" \
841 --column "Region" \
842 --column "Service Name" \
843 | grep " $2 " \
844 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500845 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100846 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500847 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100848 $1 \
849 --region $2 \
850 --publicurl $3 \
851 --adminurl $4 \
852 --internalurl $5 \
853 | grep " id " | get_field 2)
854 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500855 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100856}
Dean Troyerdff49a22014-01-30 15:37:40 -0600857
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500858
Dean Troyerdff49a22014-01-30 15:37:40 -0600859# Package Functions
860# =================
861
862# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100863function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600864 local pkg_dir
865 if is_ubuntu; then
866 pkg_dir=$FILES/apts
867 elif is_fedora; then
868 pkg_dir=$FILES/rpms
869 elif is_suse; then
870 pkg_dir=$FILES/rpms-suse
871 else
872 exit_distro_not_supported "list of packages"
873 fi
874 echo "$pkg_dir"
875}
876
877# Wrapper for ``apt-get`` to set cache and proxy environment variables
878# Uses globals ``OFFLINE``, ``*_proxy``
879# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100880function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500881 local xtrace=$(set +o | grep xtrace)
882 set +o xtrace
883
Dean Troyerdff49a22014-01-30 15:37:40 -0600884 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
885 local sudo="sudo"
886 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500887
888 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600889 $sudo DEBIAN_FRONTEND=noninteractive \
890 http_proxy=$http_proxy https_proxy=$https_proxy \
891 no_proxy=$no_proxy \
892 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
893}
894
895# get_packages() collects a list of package names of any type from the
896# prerequisite files in ``files/{apts|rpms}``. The list is intended
897# to be passed to a package installer such as apt or yum.
898#
899# Only packages required for the services in 1st argument will be
900# included. Two bits of metadata are recognized in the prerequisite files:
901#
902# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
903# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
904# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100905function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500906 local xtrace=$(set +o | grep xtrace)
907 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600908 local services=$@
909 local package_dir=$(_get_package_dir)
910 local file_to_parse
911 local service
912
913 if [[ -z "$package_dir" ]]; then
914 echo "No package directory supplied"
915 return 1
916 fi
917 if [[ -z "$DISTRO" ]]; then
918 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -0500919 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -0600920 fi
921 for service in ${services//,/ }; do
922 # Allow individual services to specify dependencies
923 if [[ -e ${package_dir}/${service} ]]; then
924 file_to_parse="${file_to_parse} $service"
925 fi
926 # NOTE(sdague) n-api needs glance for now because that's where
927 # glance client is
928 if [[ $service == n-api ]]; then
929 if [[ ! $file_to_parse =~ nova ]]; then
930 file_to_parse="${file_to_parse} nova"
931 fi
932 if [[ ! $file_to_parse =~ glance ]]; then
933 file_to_parse="${file_to_parse} glance"
934 fi
935 elif [[ $service == c-* ]]; then
936 if [[ ! $file_to_parse =~ cinder ]]; then
937 file_to_parse="${file_to_parse} cinder"
938 fi
939 elif [[ $service == ceilometer-* ]]; then
940 if [[ ! $file_to_parse =~ ceilometer ]]; then
941 file_to_parse="${file_to_parse} ceilometer"
942 fi
943 elif [[ $service == s-* ]]; then
944 if [[ ! $file_to_parse =~ swift ]]; then
945 file_to_parse="${file_to_parse} swift"
946 fi
947 elif [[ $service == n-* ]]; then
948 if [[ ! $file_to_parse =~ nova ]]; then
949 file_to_parse="${file_to_parse} nova"
950 fi
951 elif [[ $service == g-* ]]; then
952 if [[ ! $file_to_parse =~ glance ]]; then
953 file_to_parse="${file_to_parse} glance"
954 fi
955 elif [[ $service == key* ]]; then
956 if [[ ! $file_to_parse =~ keystone ]]; then
957 file_to_parse="${file_to_parse} keystone"
958 fi
959 elif [[ $service == q-* ]]; then
960 if [[ ! $file_to_parse =~ neutron ]]; then
961 file_to_parse="${file_to_parse} neutron"
962 fi
Adam Gandelman539ec432014-03-18 18:57:43 -0700963 elif [[ $service == ir-* ]]; then
964 if [[ ! $file_to_parse =~ ironic ]]; then
965 file_to_parse="${file_to_parse} ironic"
966 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600967 fi
968 done
969
970 for file in ${file_to_parse}; do
971 local fname=${package_dir}/${file}
972 local OIFS line package distros distro
973 [[ -e $fname ]] || continue
974
975 OIFS=$IFS
976 IFS=$'\n'
977 for line in $(<${fname}); do
978 if [[ $line =~ "NOPRIME" ]]; then
979 continue
980 fi
981
982 # Assume we want this package
983 package=${line%#*}
984 inst_pkg=1
985
986 # Look for # dist:xxx in comment
987 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
988 # We are using BASH regexp matching feature.
989 package=${BASH_REMATCH[1]}
990 distros=${BASH_REMATCH[2]}
991 # In bash ${VAR,,} will lowecase VAR
992 # Look for a match in the distro list
993 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
994 # If no match then skip this package
995 inst_pkg=0
996 fi
997 fi
998
999 # Look for # testonly in comment
1000 if [[ $line =~ (.*)#.*testonly.* ]]; then
1001 package=${BASH_REMATCH[1]}
1002 # Are we installing test packages? (test for the default value)
1003 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1004 # If not installing test packages the skip this package
1005 inst_pkg=0
1006 fi
1007 fi
1008
1009 if [[ $inst_pkg = 1 ]]; then
1010 echo $package
1011 fi
1012 done
1013 IFS=$OIFS
1014 done
Sean Dague45917cc2014-02-24 16:09:14 -05001015 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001016}
1017
1018# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001019# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001020# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001021function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001022 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001023 return 0
1024 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001025
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001026 if is_ubuntu; then
1027 local xtrace=$(set +o | grep xtrace)
1028 set +o xtrace
1029 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1030 # if there are transient errors pulling the updates, that's fine.
1031 # It may be secondary repositories that we don't really care about.
1032 apt_get update || /bin/true
1033 REPOS_UPDATED=True
1034 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001035 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001036 fi
1037}
1038
1039function real_install_package {
1040 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001041 apt_get install "$@"
1042 elif is_fedora; then
1043 yum_install "$@"
1044 elif is_suse; then
1045 zypper_install "$@"
1046 else
1047 exit_distro_not_supported "installing packages"
1048 fi
1049}
1050
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001051# Distro-agnostic package installer
1052# install_package package [package ...]
1053function install_package {
1054 update_package_repo
1055 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1056}
1057
Dean Troyerdff49a22014-01-30 15:37:40 -06001058# Distro-agnostic function to tell if a package is installed
1059# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001060function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001061 if [[ -z "$@" ]]; then
1062 return 1
1063 fi
1064
1065 if [[ -z "$os_PACKAGE" ]]; then
1066 GetOSVersion
1067 fi
1068
1069 if [[ "$os_PACKAGE" = "deb" ]]; then
1070 dpkg -s "$@" > /dev/null 2> /dev/null
1071 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1072 rpm --quiet -q "$@"
1073 else
1074 exit_distro_not_supported "finding if a package is installed"
1075 fi
1076}
1077
1078# Distro-agnostic package uninstaller
1079# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001080function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001081 if is_ubuntu; then
1082 apt_get purge "$@"
1083 elif is_fedora; then
1084 sudo yum remove -y "$@"
1085 elif is_suse; then
1086 sudo zypper rm "$@"
1087 else
1088 exit_distro_not_supported "uninstalling packages"
1089 fi
1090}
1091
1092# Wrapper for ``yum`` to set proxy environment variables
1093# Uses globals ``OFFLINE``, ``*_proxy``
1094# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001095function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001096 [[ "$OFFLINE" = "True" ]] && return
1097 local sudo="sudo"
1098 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001099
1100 # The manual check for missing packages is because yum -y assumes
1101 # missing packages are OK. See
1102 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001103 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1104 no_proxy=$no_proxy \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001105 yum install -y "$@" 2>&1 | \
1106 awk '
1107 BEGIN { fail=0 }
1108 /No package/ { fail=1 }
1109 { print }
1110 END { exit fail }' || \
1111 die $LINENO "Missing packages detected"
1112
1113 # also ensure we catch a yum failure
1114 if [[ ${PIPESTATUS[0]} != 0 ]]; then
1115 die $LINENO "Yum install failure"
1116 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001117}
1118
1119# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001120# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001121# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001122function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001123 [[ "$OFFLINE" = "True" ]] && return
1124 local sudo="sudo"
1125 [[ "$(id -u)" = "0" ]] && sudo="env"
1126 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1127 zypper --non-interactive install --auto-agree-with-licenses "$@"
1128}
1129
1130
1131# Process Functions
1132# =================
1133
1134# _run_process() is designed to be backgrounded by run_process() to simulate a
1135# fork. It includes the dirty work of closing extra filehandles and preparing log
1136# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001137# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001138# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001139# If an optional group is provided sg will be used to set the group of
1140# the command.
1141# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001142function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001143 local service=$1
1144 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001145 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001146
1147 # Undo logging redirections and close the extra descriptors
1148 exec 1>&3
1149 exec 2>&3
1150 exec 3>&-
1151 exec 6>&-
1152
1153 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001154 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1155 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001156
1157 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1158 export PYTHONUNBUFFERED=1
1159 fi
1160
Dean Troyer3159a822014-08-27 14:13:58 -05001161 # Run under ``setsid`` to force the process to become a session and group leader.
1162 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001163 if [[ -n "$group" ]]; then
1164 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1165 else
1166 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1167 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001168
1169 # Just silently exit this process
1170 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001171}
1172
1173# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1174# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001175# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001176# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001177function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001178 SCREEN_NAME=${SCREEN_NAME:-stack}
1179 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1180
1181 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1182 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1183 fi
1184
1185 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1186}
1187
1188# Find out if a process exists by partial name.
1189# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001190function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001191 local name=$1
1192 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001193 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001194 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001195 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001196}
1197
Dean Troyer3159a822014-08-27 14:13:58 -05001198# Run a single service under screen or directly
1199# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001200# If an optional group is provided sg will be used to run the
1201# command as that group.
1202# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001203function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001204 local service=$1
1205 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001206 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001207
Dean Troyer3159a822014-08-27 14:13:58 -05001208 if is_service_enabled $service; then
1209 if [[ "$USE_SCREEN" = "True" ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001210 screen_service "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001211 else
1212 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001213 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001214 fi
1215 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001216}
1217
1218# Helper to launch a service in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001219# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1220# ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001221# screen_service service "command-line" [group]
1222# Run a command in a shell in a screen window, if an optional group
1223# is provided, use sg to set the group of the command.
Dean Troyer3159a822014-08-27 14:13:58 -05001224function screen_service {
1225 local service=$1
1226 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001227 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001228
Sean Dagueea22a4f2014-06-27 15:21:41 -04001229 SCREEN_NAME=${SCREEN_NAME:-stack}
1230 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1231 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001232
Dean Troyer3159a822014-08-27 14:13:58 -05001233 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001234 # Append the service to the screen rc file
Dean Troyer3159a822014-08-27 14:13:58 -05001235 screen_rc "$service" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001236
Dean Troyer3159a822014-08-27 14:13:58 -05001237 screen -S $SCREEN_NAME -X screen -t $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001238
Dean Troyer3159a822014-08-27 14:13:58 -05001239 if [[ -n ${SCREEN_LOGDIR} ]]; then
1240 screen -S $SCREEN_NAME -p $service -X logfile ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log
1241 screen -S $SCREEN_NAME -p $service -X log on
1242 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001243 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001244
1245 # sleep to allow bash to be ready to be send the command - we are
1246 # creating a new window in screen and then sends characters, so if
1247 # bash isn't running by the time we send the command, nothing happens
1248 sleep 3
1249
1250 NL=`echo -ne '\015'`
1251 # This fun command does the following:
1252 # - the passed server command is backgrounded
1253 # - the pid of the background process is saved in the usual place
1254 # - the server process is brought back to the foreground
1255 # - if the server process exits prematurely the fg command errors
1256 # and a message is written to stdout and the service failure file
Chris Dent2f27a0e2014-09-09 13:46:02 +01001257 # The pid saved can be used in stop_process() as a process group
Dean Troyer3159a822014-08-27 14:13:58 -05001258 # id to kill off all child processes
Chris Dent2f27a0e2014-09-09 13:46:02 +01001259 if [[ -n "$group" ]]; then
1260 command="sg $group '$command'"
1261 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001262 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 -06001263 fi
1264}
1265
1266# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001267# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001268# screen_rc service "command-line"
1269function screen_rc {
1270 SCREEN_NAME=${SCREEN_NAME:-stack}
1271 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1272 if [[ ! -e $SCREENRC ]]; then
1273 # Name the screen session
1274 echo "sessionname $SCREEN_NAME" > $SCREENRC
1275 # Set a reasonable statusbar
1276 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1277 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1278 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1279 echo "screen -t shell bash" >> $SCREENRC
1280 fi
1281 # If this service doesn't already exist in the screenrc file
1282 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1283 NL=`echo -ne '\015'`
1284 echo "screen -t $1 bash" >> $SCREENRC
1285 echo "stuff \"$2$NL\"" >> $SCREENRC
1286
1287 if [[ -n ${SCREEN_LOGDIR} ]]; then
1288 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1289 echo "log on" >>$SCREENRC
1290 fi
1291 fi
1292}
1293
1294# Stop a service in screen
1295# If a PID is available use it, kill the whole process group via TERM
1296# If screen is being used kill the screen window; this will catch processes
1297# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001298# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001299# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001300function screen_stop_service {
1301 local service=$1
1302
Dean Troyerdff49a22014-01-30 15:37:40 -06001303 SCREEN_NAME=${SCREEN_NAME:-stack}
1304 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1305 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1306
Dean Troyer3159a822014-08-27 14:13:58 -05001307 if is_service_enabled $service; then
1308 # Clean up the screen window
1309 screen -S $SCREEN_NAME -p $service -X kill
1310 fi
1311}
1312
1313# Stop a service process
1314# If a PID is available use it, kill the whole process group via TERM
1315# If screen is being used kill the screen window; this will catch processes
1316# that did not leave a PID behind
1317# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1318# stop_process service
1319function stop_process {
1320 local service=$1
1321
1322 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1323 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1324
1325 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001326 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001327 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1328 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1329 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001330 fi
1331 if [[ "$USE_SCREEN" = "True" ]]; then
1332 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001333 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001334 fi
1335 fi
1336}
1337
1338# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001339# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001340# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001341function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001342 local service
1343 local failures
1344 SCREEN_NAME=${SCREEN_NAME:-stack}
1345 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1346
1347
1348 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1349 echo "No service status directory found"
1350 return
1351 fi
1352
1353 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001354 # make this -o errexit safe
1355 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001356
1357 for service in $failures; do
1358 service=`basename $service`
1359 service=${service%.failure}
1360 echo "Error: Service $service is not running"
1361 done
1362
1363 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001364 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001365 fi
1366}
1367
Chris Dent2f27a0e2014-09-09 13:46:02 +01001368# Tail a log file in a screen if USE_SCREEN is true.
1369function tail_log {
1370 local service=$1
1371 local logfile=$2
1372
1373 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1374 if [[ "$USE_SCREEN" = "True" ]]; then
1375 screen_service "$service" "sudo tail -f $logfile"
1376 fi
1377}
1378
Dean Troyerdff49a22014-01-30 15:37:40 -06001379
Dean Troyer3159a822014-08-27 14:13:58 -05001380# Deprecated Functions
1381# --------------------
1382
1383# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1384# fork. It includes the dirty work of closing extra filehandles and preparing log
1385# files to produce the same logs as screen_it(). The log filename is derived
1386# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1387# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1388# _old_run_process service "command-line"
1389function _old_run_process {
1390 local service=$1
1391 local command="$2"
1392
1393 # Undo logging redirections and close the extra descriptors
1394 exec 1>&3
1395 exec 2>&3
1396 exec 3>&-
1397 exec 6>&-
1398
1399 if [[ -n ${SCREEN_LOGDIR} ]]; then
1400 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1401 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1402
1403 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1404 export PYTHONUNBUFFERED=1
1405 fi
1406
1407 exec /bin/bash -c "$command"
1408 die "$service exec failure: $command"
1409}
1410
1411# old_run_process() launches a child process that closes all file descriptors and
1412# then exec's the passed in command. This is meant to duplicate the semantics
1413# of screen_it() without screen. PIDs are written to
1414# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1415# old_run_process service "command-line"
1416function old_run_process {
1417 local service=$1
1418 local command="$2"
1419
1420 # Spawn the child process
1421 _old_run_process "$service" "$command" &
1422 echo $!
1423}
1424
1425# Compatibility for existing start_XXXX() functions
1426# Uses global ``USE_SCREEN``
1427# screen_it service "command-line"
1428function screen_it {
1429 if is_service_enabled $1; then
1430 # Append the service to the screen rc file
1431 screen_rc "$1" "$2"
1432
1433 if [[ "$USE_SCREEN" = "True" ]]; then
1434 screen_service "$1" "$2"
1435 else
1436 # Spawn directly without screen
1437 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1438 fi
1439 fi
1440}
1441
1442# Compatibility for existing stop_XXXX() functions
1443# Stop a service in screen
1444# If a PID is available use it, kill the whole process group via TERM
1445# If screen is being used kill the screen window; this will catch processes
1446# that did not leave a PID behind
1447# screen_stop service
1448function screen_stop {
1449 # Clean up the screen window
1450 stop_process $1
1451}
1452
1453
Dean Troyerdff49a22014-01-30 15:37:40 -06001454# Python Functions
1455# ================
1456
1457# Get the path to the pip command.
1458# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001459function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001460 which pip || which pip-python
1461
1462 if [ $? -ne 0 ]; then
1463 die $LINENO "Unable to find pip; cannot continue"
1464 fi
1465}
1466
1467# Get the path to the direcotry where python executables are installed.
1468# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001469function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001470 if is_fedora || is_suse; then
1471 echo "/usr/bin"
1472 else
1473 echo "/usr/local/bin"
1474 fi
1475}
1476
1477# Wrapper for ``pip install`` to set cache and proxy environment variables
1478# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
1479# ``TRACK_DEPENDS``, ``*_proxy``
1480# pip_install package [package ...]
1481function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001482 local xtrace=$(set +o | grep xtrace)
1483 set +o xtrace
1484 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1485 $xtrace
1486 return
1487 fi
1488
Dean Troyerdff49a22014-01-30 15:37:40 -06001489 if [[ -z "$os_PACKAGE" ]]; then
1490 GetOSVersion
1491 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001492 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1493 # TRACK_DEPENDS=True installation creates a circular dependency when
1494 # we attempt to install virtualenv into a virualenv, so we must global
1495 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001496 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001497 local cmd_pip=$DEST/.venv/bin/pip
1498 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001499 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001500 local cmd_pip=$(get_pip_command)
1501 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001502 fi
1503
1504 # Mirror option not needed anymore because pypi has CDN available,
1505 # but it's useful in certain circumstances
1506 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001507 local pip_mirror_opt=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001508 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001509 pip_mirror_opt="--use-mirrors"
Dean Troyerdff49a22014-01-30 15:37:40 -06001510 fi
1511
1512 # pip < 1.4 has a bug where it will use an already existing build
1513 # directory unconditionally. Say an earlier component installs
1514 # foo v1.1; pip will have built foo's source in
1515 # /tmp/$USER-pip-build. Even if a later component specifies foo <
1516 # 1.1, the existing extracted build will be used and cause
1517 # confusing errors. By creating unique build directories we avoid
1518 # this problem. See https://github.com/pypa/pip/issues/709
1519 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1520
Sean Dague45917cc2014-02-24 16:09:14 -05001521 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001522 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001523 http_proxy=$http_proxy \
1524 https_proxy=$https_proxy \
1525 no_proxy=$no_proxy \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001526 $cmd_pip install --build=${pip_build_tmp} \
1527 $pip_mirror_opt $@ \
1528 && $sudo_pip rm -rf ${pip_build_tmp}
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001529
1530 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1531 local test_req="$@/test-requirements.txt"
1532 if [[ -e "$test_req" ]]; then
1533 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1534 http_proxy=$http_proxy \
1535 https_proxy=$https_proxy \
1536 no_proxy=$no_proxy \
1537 $cmd_pip install --build=${pip_build_tmp} \
1538 $pip_mirror_opt -r $test_req \
1539 && $sudo_pip rm -rf ${pip_build_tmp}
1540 fi
1541 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001542}
1543
Sean Dague099e5e32014-03-31 10:35:43 -04001544# this should be used if you want to install globally, all libraries should
1545# use this, especially *oslo* ones
1546function setup_install {
1547 local project_dir=$1
1548 setup_package_with_req_sync $project_dir
1549}
1550
1551# this should be used for projects which run services, like all services
1552function setup_develop {
1553 local project_dir=$1
1554 setup_package_with_req_sync $project_dir -e
1555}
1556
Dean Troyeraf616d92014-02-17 12:57:55 -06001557# ``pip install -e`` the package, which processes the dependencies
1558# using pip before running `setup.py develop`
1559#
1560# Updates the dependencies in project_dir from the
1561# openstack/requirements global list before installing anything.
1562#
1563# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1564# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001565function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001566 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001567 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001568
Dean Troyeraf616d92014-02-17 12:57:55 -06001569 # Don't update repo if local changes exist
1570 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001571 # ``errexit`` requires us to trap the exit code when the repo is changed
1572 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001573
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001574 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001575 (cd $REQUIREMENTS_DIR; \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001576 python update.py $project_dir)
Dean Troyeraf616d92014-02-17 12:57:55 -06001577 fi
1578
Sean Dague099e5e32014-03-31 10:35:43 -04001579 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001580
1581 # We've just gone and possibly modified the user's source tree in an
1582 # automated way, which is considered bad form if it's a development
1583 # tree because we've screwed up their next git checkin. So undo it.
1584 #
1585 # However... there are some circumstances, like running in the gate
1586 # where we really really want the overridden version to stick. So provide
1587 # a variable that tells us whether or not we should UNDO the requirements
1588 # changes (this will be set to False in the OpenStack ci gate)
1589 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001590 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001591 (cd $project_dir && git reset --hard)
1592 fi
1593 fi
1594}
1595
1596# ``pip install -e`` the package, which processes the dependencies
1597# using pip before running `setup.py develop`
1598# Uses globals ``STACK_USER``
1599# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001600function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001601 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001602 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001603
Sean Dague099e5e32014-03-31 10:35:43 -04001604 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001605 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001606 if [[ "$flags" == "-e" ]]; then
1607 safe_chown -R $STACK_USER $1/*.egg-info
1608 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001609}
1610
Dean Troyerdff49a22014-01-30 15:37:40 -06001611
1612# Service Functions
1613# =================
1614
1615# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1616# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001617function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001618 echo "$1" | sed -e '
1619 s/,,/,/g;
1620 s/^,//;
1621 s/,$//
1622 '
1623}
1624
1625# disable_all_services() removes all current services
1626# from ``ENABLED_SERVICES`` to reset the configuration
1627# before a minimal installation
1628# Uses global ``ENABLED_SERVICES``
1629# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001630function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001631 ENABLED_SERVICES=""
1632}
1633
1634# Remove all services starting with '-'. For example, to install all default
1635# services except rabbit (rabbit) set in ``localrc``:
1636# ENABLED_SERVICES+=",-rabbit"
1637# Uses global ``ENABLED_SERVICES``
1638# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001639function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001640 local tmpsvcs="${ENABLED_SERVICES}"
1641 local service
1642 for service in ${tmpsvcs//,/ }; do
1643 if [[ ${service} == -* ]]; then
1644 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1645 fi
1646 done
1647 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1648}
1649
1650# disable_service() removes the services passed as argument to the
1651# ``ENABLED_SERVICES`` list, if they are present.
1652#
1653# For example:
1654# disable_service rabbit
1655#
1656# This function does not know about the special cases
1657# for nova, glance, and neutron built into is_service_enabled().
1658# Uses global ``ENABLED_SERVICES``
1659# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001660function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001661 local tmpsvcs=",${ENABLED_SERVICES},"
1662 local service
1663 for service in $@; do
1664 if is_service_enabled $service; then
1665 tmpsvcs=${tmpsvcs//,$service,/,}
1666 fi
1667 done
1668 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1669}
1670
1671# enable_service() adds the services passed as argument to the
1672# ``ENABLED_SERVICES`` list, if they are not already present.
1673#
1674# For example:
1675# enable_service qpid
1676#
1677# This function does not know about the special cases
1678# for nova, glance, and neutron built into is_service_enabled().
1679# Uses global ``ENABLED_SERVICES``
1680# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001681function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001682 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001683 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001684 for service in $@; do
1685 if ! is_service_enabled $service; then
1686 tmpsvcs+=",$service"
1687 fi
1688 done
1689 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1690 disable_negated_services
1691}
1692
1693# is_service_enabled() checks if the service(s) specified as arguments are
1694# enabled by the user in ``ENABLED_SERVICES``.
1695#
1696# Multiple services specified as arguments are ``OR``'ed together; the test
1697# is a short-circuit boolean, i.e it returns on the first match.
1698#
1699# There are special cases for some 'catch-all' services::
1700# **nova** returns true if any service enabled start with **n-**
1701# **cinder** returns true if any service enabled start with **c-**
1702# **ceilometer** returns true if any service enabled start with **ceilometer**
1703# **glance** returns true if any service enabled start with **g-**
1704# **neutron** returns true if any service enabled start with **q-**
1705# **swift** returns true if any service enabled start with **s-**
1706# **trove** returns true if any service enabled start with **tr-**
1707# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1708# **s-** services will be enabled. This will be deprecated in the future.
1709#
1710# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1711# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1712# as enabled in this case.
1713#
1714# Uses global ``ENABLED_SERVICES``
1715# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001716function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001717 local xtrace=$(set +o | grep xtrace)
1718 set +o xtrace
1719 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001720 local services=$@
1721 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001722 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001723 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001724
1725 # Look for top-level 'enabled' function for this service
1726 if type is_${service}_enabled >/dev/null 2>&1; then
1727 # A function exists for this service, use it
1728 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001729 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001730 fi
1731
1732 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1733 # are implemented
1734
Sean Dague45917cc2014-02-24 16:09:14 -05001735 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001736 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001737 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1738 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1739 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1740 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1741 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1742 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1743 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1744 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1745 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001746 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001747 done
Sean Dague45917cc2014-02-24 16:09:14 -05001748 $xtrace
1749 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001750}
1751
1752# Toggle enable/disable_service for services that must run exclusive of each other
1753# $1 The name of a variable containing a space-separated list of services
1754# $2 The name of a variable in which to store the enabled service's name
1755# $3 The name of the service to enable
1756function use_exclusive_service {
1757 local options=${!1}
1758 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001759 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001760 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001761 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001762 for opt in $options;do
1763 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1764 done
1765 eval "$out=$selection"
1766 return 0
1767}
1768
1769
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001770# System Functions
1771# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001772
1773# Only run the command if the target file (the last arg) is not on an
1774# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001775function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001776 local xtrace=$(set +o | grep xtrace)
1777 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001778 local args=( $@ )
1779 local last
1780 local sudo_cmd
1781 local dir_to_check
1782
1783 let last="${#args[*]} - 1"
1784
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001785 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001786 if [ ! -d "$dir_to_check" ]; then
1787 dir_to_check=`dirname "$dir_to_check"`
1788 fi
1789
1790 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001791 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001792 return 0
1793 fi
1794
1795 if [[ $TRACK_DEPENDS = True ]]; then
1796 sudo_cmd="env"
1797 else
1798 sudo_cmd="sudo"
1799 fi
1800
Sean Dague45917cc2014-02-24 16:09:14 -05001801 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001802 $sudo_cmd $@
1803}
1804
1805# Exit 0 if address is in network or 1 if address is not in network
1806# ip-range is in CIDR notation: 1.2.3.4/20
1807# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001808function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001809 local ip=$1
1810 local range=$2
1811 local masklen=${range#*/}
1812 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1813 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1814 [[ $network == $subnet ]]
1815}
1816
1817# Add a user to a group.
1818# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001819function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001820 local user=$1
1821 local group=$2
1822
1823 if [[ -z "$os_VENDOR" ]]; then
1824 GetOSVersion
1825 fi
1826
1827 # SLE11 and openSUSE 12.2 don't have the usual usermod
1828 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1829 sudo usermod -a -G "$group" "$user"
1830 else
1831 sudo usermod -A "$group" "$user"
1832 fi
1833}
1834
1835# Convert CIDR notation to a IPv4 netmask
1836# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001837function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001838 local maskpat="255 255 255 255"
1839 local maskdgt="254 252 248 240 224 192 128"
1840 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1841 echo ${1-0}.${2-0}.${3-0}.${4-0}
1842}
1843
1844# Gracefully cp only if source file/dir exists
1845# cp_it source destination
1846function cp_it {
1847 if [ -e $1 ] || [ -d $1 ]; then
1848 cp -pRL $1 $2
1849 fi
1850}
1851
1852# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1853# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1854# ``localrc`` or on the command line if necessary::
1855#
1856# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1857#
1858# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1859
Ian Wienandaee18c72014-02-21 15:35:08 +11001860function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001861 if [[ -n "$http_proxy" ]]; then
1862 export http_proxy=$http_proxy
1863 fi
1864 if [[ -n "$https_proxy" ]]; then
1865 export https_proxy=$https_proxy
1866 fi
1867 if [[ -n "$no_proxy" ]]; then
1868 export no_proxy=$no_proxy
1869 fi
1870}
1871
1872# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001873function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001874 local mount_type=`stat -f -L -c %T $1`
1875 test "$mount_type" == "nfs"
1876}
1877
1878# Return the network portion of the given IP address using netmask
1879# netmask is in the traditional dotted-quad format
1880# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001881function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001882 local ip=$1
1883 local mask=$2
1884 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1885 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1886 echo $subnet
1887}
1888
1889# Service wrapper to restart services
1890# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001891function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001892 if is_ubuntu; then
1893 sudo /usr/sbin/service $1 restart
1894 else
1895 sudo /sbin/service $1 restart
1896 fi
1897}
1898
1899# Only change permissions of a file or directory if it is not on an
1900# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001901function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001902 _safe_permission_operation chmod $@
1903}
1904
1905# Only change ownership of a file or directory if it is not on an NFS
1906# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001907function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001908 _safe_permission_operation chown $@
1909}
1910
1911# Service wrapper to start services
1912# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001913function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001914 if is_ubuntu; then
1915 sudo /usr/sbin/service $1 start
1916 else
1917 sudo /sbin/service $1 start
1918 fi
1919}
1920
1921# Service wrapper to stop services
1922# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001923function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001924 if is_ubuntu; then
1925 sudo /usr/sbin/service $1 stop
1926 else
1927 sudo /sbin/service $1 stop
1928 fi
1929}
1930
1931
1932# Restore xtrace
1933$XTRACE
1934
1935# Local variables:
1936# mode: shell-script
1937# End: