blob: 333f31da0257916d3f86408d429ce60b850c6aca [file] [log] [blame]
Dean Troyerdff49a22014-01-30 15:37:40 -06001# functions-common - Common functions used by DevStack components
2#
3# The canonical copy of this file is maintained in the DevStack repo.
4# All modifications should be made there and then sync'ed to other repos
5# as required.
6#
7# This file is sorted alphabetically within the function groups.
8#
9# - Config Functions
10# - Control Functions
11# - Distro Functions
12# - Git Functions
13# - OpenStack Functions
14# - Package Functions
15# - Process Functions
16# - Python Functions
17# - Service Functions
Masayuki Igawaf6368d32014-02-20 13:31:26 +090018# - System Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060019#
20# The following variables are assumed to be defined by certain functions:
21#
22# - ``ENABLED_SERVICES``
23# - ``ERROR_ON_CLONE``
24# - ``FILES``
25# - ``OFFLINE``
26# - ``PIP_DOWNLOAD_CACHE``
27# - ``PIP_USE_MIRRORS``
28# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090029# - ``REQUIREMENTS_DIR``
30# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060031# - ``TRACK_DEPENDS``
Masayuki Igawad20f6322014-02-28 09:22:37 +090032# - ``UNDO_REQUIREMENTS``
Dean Troyerdff49a22014-01-30 15:37:40 -060033# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050034#
Dean Troyerdff49a22014-01-30 15:37:40 -060035
36# Save trace setting
37XTRACE=$(set +o | grep xtrace)
38set +o xtrace
39
Sean Daguecc524062014-10-01 09:06:43 -040040# Global Config Variables
41declare -A GITREPO
42declare -A GITBRANCH
43declare -A GITDIR
44
Dean Troyerdff49a22014-01-30 15:37:40 -060045
46# Config Functions
47# ================
48
49# Append a new option in an ini file without replacing the old value
50# iniadd config-file section option value1 value2 value3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +110051function iniadd {
Sean Dague45917cc2014-02-24 16:09:14 -050052 local xtrace=$(set +o | grep xtrace)
53 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060054 local file=$1
55 local section=$2
56 local option=$3
57 shift 3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050058
Dean Troyerdff49a22014-01-30 15:37:40 -060059 local values="$(iniget_multiline $file $section $option) $@"
60 iniset_multiline $file $section $option $values
Sean Dague45917cc2014-02-24 16:09:14 -050061 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060062}
63
64# Comment an option in an INI file
65# inicomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110066function inicomment {
Sean Dague45917cc2014-02-24 16:09:14 -050067 local xtrace=$(set +o | grep xtrace)
68 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060069 local file=$1
70 local section=$2
71 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050072
Dean Troyerdff49a22014-01-30 15:37:40 -060073 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -050074 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060075}
76
77# Get an option from an INI file
78# iniget config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110079function iniget {
Sean Dague45917cc2014-02-24 16:09:14 -050080 local xtrace=$(set +o | grep xtrace)
81 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060082 local file=$1
83 local section=$2
84 local option=$3
85 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050086
Dean Troyerdff49a22014-01-30 15:37:40 -060087 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
88 echo ${line#*=}
Sean Dague45917cc2014-02-24 16:09:14 -050089 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060090}
91
92# Get a multiple line option from an INI file
93# iniget_multiline config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110094function iniget_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -050095 local xtrace=$(set +o | grep xtrace)
96 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060097 local file=$1
98 local section=$2
99 local option=$3
100 local values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500101
Dean Troyerdff49a22014-01-30 15:37:40 -0600102 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
103 echo ${values}
Sean Dague45917cc2014-02-24 16:09:14 -0500104 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600105}
106
107# Determinate is the given option present in the INI file
108# ini_has_option config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100109function ini_has_option {
Sean Dague45917cc2014-02-24 16:09:14 -0500110 local xtrace=$(set +o | grep xtrace)
111 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600112 local file=$1
113 local section=$2
114 local option=$3
115 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500116
Dean Troyerdff49a22014-01-30 15:37:40 -0600117 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Sean Dague45917cc2014-02-24 16:09:14 -0500118 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600119 [ -n "$line" ]
120}
121
Robert Li751ad1a2014-10-15 21:40:53 -0400122# Add another config line for a multi-line option.
123# It's normally called after iniset of the same option and assumes
124# that the section already exists.
125#
126# Note that iniset_multiline requires all the 'lines' to be supplied
127# in the argument list. Doing that will cause incorrect configuration
128# if spaces are used in the config values.
129#
130# iniadd_literal config-file section option value
131function iniadd_literal {
132 local xtrace=$(set +o | grep xtrace)
133 set +o xtrace
134 local file=$1
135 local section=$2
136 local option=$3
137 local value=$4
138
139 [[ -z $section || -z $option ]] && return
140
141 # Add it
142 sed -i -e "/^\[$section\]/ a\\
143$option = $value
144" "$file"
145
146 $xtrace
147}
148
Dean Troyerdff49a22014-01-30 15:37:40 -0600149# Set an option in an INI file
150# iniset config-file section option value
Ian Wienandaee18c72014-02-21 15:35:08 +1100151function iniset {
Sean Dague45917cc2014-02-24 16:09:14 -0500152 local xtrace=$(set +o | grep xtrace)
153 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600154 local file=$1
155 local section=$2
156 local option=$3
157 local value=$4
158
159 [[ -z $section || -z $option ]] && return
160
161 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
162 # Add section at the end
163 echo -e "\n[$section]" >>"$file"
164 fi
165 if ! ini_has_option "$file" "$section" "$option"; then
166 # Add it
167 sed -i -e "/^\[$section\]/ a\\
168$option = $value
169" "$file"
170 else
171 local sep=$(echo -ne "\x01")
172 # Replace it
173 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
174 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500175 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600176}
177
178# Set a multiple line option in an INI file
179# iniset_multiline config-file section option value1 value2 valu3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +1100180function iniset_multiline {
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
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500186
Dean Troyerdff49a22014-01-30 15:37:40 -0600187 shift 3
188 local values
189 for v in $@; do
190 # The later sed command inserts each new value in the line next to
191 # the section identifier, which causes the values to be inserted in
192 # the reverse order. Do a reverse here to keep the original order.
193 values="$v ${values}"
194 done
195 if ! grep -q "^\[$section\]" "$file"; then
196 # Add section at the end
197 echo -e "\n[$section]" >>"$file"
198 else
199 # Remove old values
200 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
201 fi
202 # Add new ones
203 for v in $values; do
204 sed -i -e "/^\[$section\]/ a\\
205$option = $v
206" "$file"
207 done
Sean Dague45917cc2014-02-24 16:09:14 -0500208 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600209}
210
211# Uncomment an option in an INI file
212# iniuncomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100213function iniuncomment {
Sean Dague45917cc2014-02-24 16:09:14 -0500214 local xtrace=$(set +o | grep xtrace)
215 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600216 local file=$1
217 local section=$2
218 local option=$3
219 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500220 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600221}
222
223# Normalize config values to True or False
224# Accepts as False: 0 no No NO false False FALSE
225# Accepts as True: 1 yes Yes YES true True TRUE
226# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100227function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500228 local xtrace=$(set +o | grep xtrace)
229 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600230 local default=$1
231 local testval=$2
232
233 [[ -z "$testval" ]] && { echo "$default"; return; }
234 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
235 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
236 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500237 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600238}
239
240
241# Control Functions
242# =================
243
244# Prints backtrace info
245# filename:lineno:function
246# backtrace level
247function backtrace {
248 local level=$1
249 local deep=$((${#BASH_SOURCE[@]} - 1))
250 echo "[Call Trace]"
251 while [ $level -le $deep ]; do
252 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
253 deep=$((deep - 1))
254 done
255}
256
257# Prints line number and "message" then exits
258# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100259function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600260 local exitcode=$?
261 set +o xtrace
262 local line=$1; shift
263 if [ $exitcode == 0 ]; then
264 exitcode=1
265 fi
266 backtrace 2
267 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600268 # Give buffers a second to flush
269 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600270 exit $exitcode
271}
272
273# Checks an environment variable is not set or has length 0 OR if the
274# exit code is non-zero and prints "message" and exits
275# NOTE: env-var is the variable name without a '$'
276# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100277function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600278 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500279 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600280 set +o xtrace
281 local line=$1; shift
282 local evar=$1; shift
283 if ! is_set $evar || [ $exitcode != 0 ]; then
284 die $line "$*"
285 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500286 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600287}
288
289# Prints line number and "message" in error format
290# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100291function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600292 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500293 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600294 set +o xtrace
295 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
296 echo $msg 1>&2;
297 if [[ -n ${SCREEN_LOGDIR} ]]; then
298 echo $msg >> "${SCREEN_LOGDIR}/error.log"
299 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500300 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600301 return $exitcode
302}
303
304# Checks an environment variable is not set or has length 0 OR if the
305# exit code is non-zero and prints "message"
306# NOTE: env-var is the variable name without a '$'
307# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100308function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600309 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500310 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600311 set +o xtrace
312 local line=$1; shift
313 local evar=$1; shift
314 if ! is_set $evar || [ $exitcode != 0 ]; then
315 err $line "$*"
316 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500317 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600318 return $exitcode
319}
320
321# Exit after outputting a message about the distribution not being supported.
322# exit_distro_not_supported [optional-string-telling-what-is-missing]
323function exit_distro_not_supported {
324 if [[ -z "$DISTRO" ]]; then
325 GetDistro
326 fi
327
328 if [ $# -gt 0 ]; then
329 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
330 else
331 die $LINENO "Support for $DISTRO is incomplete."
332 fi
333}
334
335# Test if the named environment variable is set and not zero length
336# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100337function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600338 local var=\$"$1"
339 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
340}
341
342# Prints line number and "message" in warning format
343# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100344function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600345 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500346 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600347 set +o xtrace
348 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
349 echo $msg 1>&2;
350 if [[ -n ${SCREEN_LOGDIR} ]]; then
351 echo $msg >> "${SCREEN_LOGDIR}/error.log"
352 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500353 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600354 return $exitcode
355}
356
357
358# Distro Functions
359# ================
360
361# Determine OS Vendor, Release and Update
362# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
363# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500364# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
365# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
366# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
367# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
368# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
369declare os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
370
Dean Troyerdff49a22014-01-30 15:37:40 -0600371# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100372function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500373
Dean Troyerdff49a22014-01-30 15:37:40 -0600374 # Figure out which vendor we are
375 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
376 # OS/X
377 os_VENDOR=`sw_vers -productName`
378 os_RELEASE=`sw_vers -productVersion`
379 os_UPDATE=${os_RELEASE##*.}
380 os_RELEASE=${os_RELEASE%.*}
381 os_PACKAGE=""
382 if [[ "$os_RELEASE" =~ "10.7" ]]; then
383 os_CODENAME="lion"
384 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
385 os_CODENAME="snow leopard"
386 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
387 os_CODENAME="leopard"
388 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
389 os_CODENAME="tiger"
390 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
391 os_CODENAME="panther"
392 else
393 os_CODENAME=""
394 fi
395 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
396 os_VENDOR=$(lsb_release -i -s)
397 os_RELEASE=$(lsb_release -r -s)
398 os_UPDATE=""
399 os_PACKAGE="rpm"
400 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
401 os_PACKAGE="deb"
402 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
403 lsb_release -d -s | grep -q openSUSE
404 if [[ $? -eq 0 ]]; then
405 os_VENDOR="openSUSE"
406 fi
407 elif [[ $os_VENDOR == "openSUSE project" ]]; then
408 os_VENDOR="openSUSE"
409 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
410 os_VENDOR="Red Hat"
411 fi
412 os_CODENAME=$(lsb_release -c -s)
413 elif [[ -r /etc/redhat-release ]]; then
414 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
415 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
416 # CentOS release 5.5 (Final)
417 # CentOS Linux release 6.0 (Final)
418 # Fedora release 16 (Verne)
419 # XenServer release 6.2.0-70446c (xenenterprise)
420 os_CODENAME=""
421 for r in "Red Hat" CentOS Fedora XenServer; do
422 os_VENDOR=$r
423 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
424 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
425 os_CODENAME=${ver#*|}
426 os_RELEASE=${ver%|*}
427 os_UPDATE=${os_RELEASE##*.}
428 os_RELEASE=${os_RELEASE%.*}
429 break
430 fi
431 os_VENDOR=""
432 done
433 os_PACKAGE="rpm"
434 elif [[ -r /etc/SuSE-release ]]; then
435 for r in openSUSE "SUSE Linux"; do
436 if [[ "$r" = "SUSE Linux" ]]; then
437 os_VENDOR="SUSE LINUX"
438 else
439 os_VENDOR=$r
440 fi
441
442 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
443 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
444 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
445 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
446 break
447 fi
448 os_VENDOR=""
449 done
450 os_PACKAGE="rpm"
451 # If lsb_release is not installed, we should be able to detect Debian OS
452 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
453 os_VENDOR="Debian"
454 os_PACKAGE="deb"
455 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
456 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
457 fi
458 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
459}
460
461# Translate the OS version values into common nomenclature
462# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500463declare DISTRO
464
Ian Wienandaee18c72014-02-21 15:35:08 +1100465function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600466 GetOSVersion
467 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
468 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
469 DISTRO=$os_CODENAME
470 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
471 # For Fedora, just use 'f' and the release
472 DISTRO="f$os_RELEASE"
473 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
474 DISTRO="opensuse-$os_RELEASE"
475 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
476 # For SLE, also use the service pack
477 if [[ -z "$os_UPDATE" ]]; then
478 DISTRO="sle${os_RELEASE}"
479 else
480 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
481 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530482 elif [[ "$os_VENDOR" =~ (Red Hat) || \
483 "$os_VENDOR" =~ (CentOS) || \
484 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600485 # Drop the . release as we assume it's compatible
486 DISTRO="rhel${os_RELEASE::1}"
487 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
488 DISTRO="xs$os_RELEASE"
489 else
490 # Catch-all for now is Vendor + Release + Update
491 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
492 fi
493 export DISTRO
494}
495
496# Utility function for checking machine architecture
497# is_arch arch-type
498function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500499 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600500}
501
Ian Wienandbdc90c52014-08-04 15:44:58 +1000502# Quick check for a rackspace host; n.b. rackspace provided images
503# have these Xen tools installed but a custom image may not.
504function is_rackspace {
505 [ -f /usr/bin/xenstore-ls ] && \
506 sudo /usr/bin/xenstore-ls vm-data | grep -q "Rackspace"
507}
508
Dean Troyerdff49a22014-01-30 15:37:40 -0600509# Determine if current distribution is a Fedora-based distribution
510# (Fedora, RHEL, CentOS, etc).
511# is_fedora
512function is_fedora {
513 if [[ -z "$os_VENDOR" ]]; then
514 GetOSVersion
515 fi
516
anju Tiwari6c639c92014-07-15 18:11:54 +0530517 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
518 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600519}
520
521
522# Determine if current distribution is a SUSE-based distribution
523# (openSUSE, SLE).
524# is_suse
525function is_suse {
526 if [[ -z "$os_VENDOR" ]]; then
527 GetOSVersion
528 fi
529
530 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
531}
532
533
534# Determine if current distribution is an Ubuntu-based distribution
535# It will also detect non-Ubuntu but Debian-based distros
536# is_ubuntu
537function is_ubuntu {
538 if [[ -z "$os_PACKAGE" ]]; then
539 GetOSVersion
540 fi
541 [ "$os_PACKAGE" = "deb" ]
542}
543
544
545# Git Functions
546# =============
547
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600548# Returns openstack release name for a given branch name
549# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100550function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600551 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700552 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600553 echo ${branch#*/}
554 else
555 echo "master"
556 fi
557}
558
Dean Troyerdff49a22014-01-30 15:37:40 -0600559# git clone only if directory doesn't exist already. Since ``DEST`` might not
560# be owned by the installation user, we create the directory and change the
561# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500562# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
563# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600564# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer50cda692014-07-25 11:57:20 -0500565# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600566# git_clone remote dest-dir branch
567function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500568 local git_remote=$1
569 local git_dest=$2
570 local git_ref=$3
571 local orig_dir=$(pwd)
572
Dean Troyerdff49a22014-01-30 15:37:40 -0600573 RECLONE=$(trueorfalse False $RECLONE)
574
575 if [[ "$OFFLINE" = "True" ]]; then
576 echo "Running in offline mode, clones already exist"
577 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500578 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600579 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400580 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600581 return
582 fi
583
Dean Troyer50cda692014-07-25 11:57:20 -0500584 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600585 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500586 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600587 [[ "$ERROR_ON_CLONE" = "True" ]] && \
588 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer50cda692014-07-25 11:57:20 -0500589 git_timed clone $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600590 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500591 cd $git_dest
592 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600593 else
594 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500595 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600596 [[ "$ERROR_ON_CLONE" = "True" ]] && \
597 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer50cda692014-07-25 11:57:20 -0500598 git_timed clone $git_remote $git_dest
599 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600600 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500601 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600602 elif [[ "$RECLONE" = "True" ]]; then
603 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500604 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600605 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500606 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100607 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600608 # remove the existing ignored files (like pyc) as they cause breakage
609 # (due to the py files having older timestamps than our pyc, so python
610 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500611 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600612
Dean Troyer50cda692014-07-25 11:57:20 -0500613 # handle git_ref accordingly to type (tag, branch)
614 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
615 git_update_tag $git_ref
616 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
617 git_update_branch $git_ref
618 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
619 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600620 else
Dean Troyer50cda692014-07-25 11:57:20 -0500621 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600622 fi
623
624 fi
625 fi
626
627 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500628 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600629 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400630 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600631}
632
Sean Daguecc524062014-10-01 09:06:43 -0400633# A variation on git clone that lets us specify a project by it's
634# actual name, like oslo.config. This is exceptionally useful in the
635# library installation case
636function git_clone_by_name {
637 local name=$1
638 local repo=${GITREPO[$name]}
639 local dir=${GITDIR[$name]}
640 local branch=${GITBRANCH[$name]}
641 git_clone $repo $dir $branch
642}
643
644
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100645# git can sometimes get itself infinitely stuck with transient network
646# errors or other issues with the remote end. This wraps git in a
647# timeout/retry loop and is intended to watch over non-local git
648# processes that might hang. GIT_TIMEOUT, if set, is passed directly
649# to timeout(1); otherwise the default value of 0 maintains the status
650# quo of waiting forever.
651# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100652function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100653 local count=0
654 local timeout=0
655
656 if [[ -n "${GIT_TIMEOUT}" ]]; then
657 timeout=${GIT_TIMEOUT}
658 fi
659
660 until timeout -s SIGINT ${timeout} git "$@"; do
661 # 124 is timeout(1)'s special return code when it reached the
662 # timeout; otherwise assume fatal failure
663 if [[ $? -ne 124 ]]; then
664 die $LINENO "git call failed: [git $@]"
665 fi
666
667 count=$(($count + 1))
668 warn "timeout ${count} for git call: [git $@]"
669 if [ $count -eq 3 ]; then
670 die $LINENO "Maximum of 3 git retries reached"
671 fi
672 sleep 5
673 done
674}
675
Dean Troyerdff49a22014-01-30 15:37:40 -0600676# git update using reference as a branch.
677# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100678function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500679 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600680
Dean Troyer50cda692014-07-25 11:57:20 -0500681 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600682 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500683 git branch -D $git_branch || true
684 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600685}
686
687# git update using reference as a branch.
688# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100689function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500690 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600691
Dean Troyer50cda692014-07-25 11:57:20 -0500692 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600693}
694
695# git update using reference as a tag. Be careful editing source at that repo
696# as working copy will be in a detached mode
697# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100698function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500699 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600700
Dean Troyer50cda692014-07-25 11:57:20 -0500701 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600702 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500703 git_timed fetch origin tag $git_tag
704 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600705}
706
707
708# OpenStack Functions
709# ===================
710
711# Get the default value for HOST_IP
712# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100713function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600714 local fixed_range=$1
715 local floating_range=$2
716 local host_ip_iface=$3
717 local host_ip=$4
718
719 # Find the interface used for the default route
720 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
721 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
722 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
723 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500724 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
725 local ip
726 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600727 # Attempt to filter out IP addresses that are part of the fixed and
728 # floating range. Note that this method only works if the ``netaddr``
729 # python library is installed. If it is not installed, an error
730 # will be printed and the first IP from the interface will be used.
731 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
732 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500733 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
734 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600735 break;
736 fi
737 done
738 fi
739 echo $host_ip
740}
741
Attila Fazekasf71b5002014-05-28 09:52:22 +0200742# Generates hex string from ``size`` byte of pseudo random data
743# generate_hex_string size
744function generate_hex_string {
745 local size=$1
746 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
747}
748
Dean Troyerdff49a22014-01-30 15:37:40 -0600749# Grab a numbered field from python prettytable output
750# Fields are numbered starting with 1
751# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
752# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100753function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500754 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600755 while read data; do
756 if [ "$1" -lt 0 ]; then
757 field="(\$(NF$1))"
758 else
759 field="\$$(($1 + 1))"
760 fi
761 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
762 done
763}
764
765# Add a policy to a policy.json file
766# Do nothing if the policy already exists
767# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100768function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600769 local policy_file=$1
770 local policy_name=$2
771 local policy_perm=$3
772
773 if grep -q ${policy_name} ${policy_file}; then
774 echo "Policy ${policy_name} already exists in ${policy_file}"
775 return
776 fi
777
778 # Add a terminating comma to policy lines without one
779 # Remove the closing '}' and all lines following to the end-of-file
780 local tmpfile=$(mktemp)
781 uniq ${policy_file} | sed -e '
782 s/]$/],/
783 /^[}]/,$d
784 ' > ${tmpfile}
785
786 # Append policy and closing brace
787 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
788 echo "}" >>${tmpfile}
789
790 mv ${tmpfile} ${policy_file}
791}
792
Bartosz Górski0abde392014-02-28 14:15:19 +0100793# Gets or creates user
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200794# Usage: get_or_create_user <username> <password> <project> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100795function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200796 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500797 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200798 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500799 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200800 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100801 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500802 local user_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100803 # Gets user id
804 openstack user show $1 -f value -c id 2>/dev/null ||
805 # Creates new user
806 openstack user create \
807 $1 \
808 --password "$2" \
809 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500810 $email \
Bartosz Górski0abde392014-02-28 14:15:19 +0100811 -f value -c id
812 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500813 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100814}
815
816# Gets or creates project
817# Usage: get_or_create_project <name>
818function get_or_create_project {
819 # Gets project id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500820 local project_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100821 # Gets project id
822 openstack project show $1 -f value -c id 2>/dev/null ||
823 # Creates new project if not exists
824 openstack project create $1 -f value -c id
825 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500826 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100827}
828
829# Gets or creates role
830# Usage: get_or_create_role <name>
831function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500832 local role_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100833 # Gets role id
834 openstack role show $1 -f value -c id 2>/dev/null ||
835 # Creates role if not exists
836 openstack role create $1 -f value -c id
837 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500838 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100839}
840
841# Gets or adds user role
842# Usage: get_or_add_user_role <role> <user> <project>
843function get_or_add_user_role {
844 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500845 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100846 $2 \
847 --project $3 \
848 --column "ID" \
849 --column "Name" \
850 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500851 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100852 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500853 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100854 $1 \
855 --user $2 \
856 --project $3 \
857 | grep " id " | get_field 2)
858 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500859 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100860}
861
862# Gets or creates service
863# Usage: get_or_create_service <name> <type> <description>
864function get_or_create_service {
865 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500866 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100867 # Gets service id
868 openstack service show $1 -f value -c id 2>/dev/null ||
869 # Creates new service if not exists
870 openstack service create \
871 $1 \
872 --type=$2 \
873 --description="$3" \
874 -f value -c id
875 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500876 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100877}
878
879# Gets or creates endpoint
880# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
881function get_or_create_endpoint {
882 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500883 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100884 --column "ID" \
885 --column "Region" \
886 --column "Service Name" \
887 | grep " $2 " \
888 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500889 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100890 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500891 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100892 $1 \
893 --region $2 \
894 --publicurl $3 \
895 --adminurl $4 \
896 --internalurl $5 \
897 | grep " id " | get_field 2)
898 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500899 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100900}
Dean Troyerdff49a22014-01-30 15:37:40 -0600901
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500902
Dean Troyerdff49a22014-01-30 15:37:40 -0600903# Package Functions
904# =================
905
906# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100907function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600908 local pkg_dir
909 if is_ubuntu; then
910 pkg_dir=$FILES/apts
911 elif is_fedora; then
912 pkg_dir=$FILES/rpms
913 elif is_suse; then
914 pkg_dir=$FILES/rpms-suse
915 else
916 exit_distro_not_supported "list of packages"
917 fi
918 echo "$pkg_dir"
919}
920
921# Wrapper for ``apt-get`` to set cache and proxy environment variables
922# Uses globals ``OFFLINE``, ``*_proxy``
923# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100924function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500925 local xtrace=$(set +o | grep xtrace)
926 set +o xtrace
927
Dean Troyerdff49a22014-01-30 15:37:40 -0600928 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
929 local sudo="sudo"
930 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500931
932 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600933 $sudo DEBIAN_FRONTEND=noninteractive \
934 http_proxy=$http_proxy https_proxy=$https_proxy \
935 no_proxy=$no_proxy \
936 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
937}
938
939# get_packages() collects a list of package names of any type from the
940# prerequisite files in ``files/{apts|rpms}``. The list is intended
941# to be passed to a package installer such as apt or yum.
942#
943# Only packages required for the services in 1st argument will be
944# included. Two bits of metadata are recognized in the prerequisite files:
945#
946# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
947# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
948# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100949function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500950 local xtrace=$(set +o | grep xtrace)
951 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600952 local services=$@
953 local package_dir=$(_get_package_dir)
954 local file_to_parse
955 local service
956
957 if [[ -z "$package_dir" ]]; then
958 echo "No package directory supplied"
959 return 1
960 fi
961 if [[ -z "$DISTRO" ]]; then
962 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -0500963 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -0600964 fi
965 for service in ${services//,/ }; do
966 # Allow individual services to specify dependencies
967 if [[ -e ${package_dir}/${service} ]]; then
968 file_to_parse="${file_to_parse} $service"
969 fi
970 # NOTE(sdague) n-api needs glance for now because that's where
971 # glance client is
972 if [[ $service == n-api ]]; then
973 if [[ ! $file_to_parse =~ nova ]]; then
974 file_to_parse="${file_to_parse} nova"
975 fi
976 if [[ ! $file_to_parse =~ glance ]]; then
977 file_to_parse="${file_to_parse} glance"
978 fi
979 elif [[ $service == c-* ]]; then
980 if [[ ! $file_to_parse =~ cinder ]]; then
981 file_to_parse="${file_to_parse} cinder"
982 fi
983 elif [[ $service == ceilometer-* ]]; then
984 if [[ ! $file_to_parse =~ ceilometer ]]; then
985 file_to_parse="${file_to_parse} ceilometer"
986 fi
987 elif [[ $service == s-* ]]; then
988 if [[ ! $file_to_parse =~ swift ]]; then
989 file_to_parse="${file_to_parse} swift"
990 fi
991 elif [[ $service == n-* ]]; then
992 if [[ ! $file_to_parse =~ nova ]]; then
993 file_to_parse="${file_to_parse} nova"
994 fi
995 elif [[ $service == g-* ]]; then
996 if [[ ! $file_to_parse =~ glance ]]; then
997 file_to_parse="${file_to_parse} glance"
998 fi
999 elif [[ $service == key* ]]; then
1000 if [[ ! $file_to_parse =~ keystone ]]; then
1001 file_to_parse="${file_to_parse} keystone"
1002 fi
1003 elif [[ $service == q-* ]]; then
1004 if [[ ! $file_to_parse =~ neutron ]]; then
1005 file_to_parse="${file_to_parse} neutron"
1006 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001007 elif [[ $service == ir-* ]]; then
1008 if [[ ! $file_to_parse =~ ironic ]]; then
1009 file_to_parse="${file_to_parse} ironic"
1010 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001011 fi
1012 done
1013
1014 for file in ${file_to_parse}; do
1015 local fname=${package_dir}/${file}
1016 local OIFS line package distros distro
1017 [[ -e $fname ]] || continue
1018
1019 OIFS=$IFS
1020 IFS=$'\n'
1021 for line in $(<${fname}); do
1022 if [[ $line =~ "NOPRIME" ]]; then
1023 continue
1024 fi
1025
1026 # Assume we want this package
1027 package=${line%#*}
1028 inst_pkg=1
1029
1030 # Look for # dist:xxx in comment
1031 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1032 # We are using BASH regexp matching feature.
1033 package=${BASH_REMATCH[1]}
1034 distros=${BASH_REMATCH[2]}
1035 # In bash ${VAR,,} will lowecase VAR
1036 # Look for a match in the distro list
1037 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1038 # If no match then skip this package
1039 inst_pkg=0
1040 fi
1041 fi
1042
1043 # Look for # testonly in comment
1044 if [[ $line =~ (.*)#.*testonly.* ]]; then
1045 package=${BASH_REMATCH[1]}
1046 # Are we installing test packages? (test for the default value)
1047 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1048 # If not installing test packages the skip this package
1049 inst_pkg=0
1050 fi
1051 fi
1052
1053 if [[ $inst_pkg = 1 ]]; then
1054 echo $package
1055 fi
1056 done
1057 IFS=$OIFS
1058 done
Sean Dague45917cc2014-02-24 16:09:14 -05001059 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001060}
1061
1062# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001063# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001064# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001065function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001066 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001067 return 0
1068 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001069
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001070 if is_ubuntu; then
1071 local xtrace=$(set +o | grep xtrace)
1072 set +o xtrace
1073 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1074 # if there are transient errors pulling the updates, that's fine.
1075 # It may be secondary repositories that we don't really care about.
1076 apt_get update || /bin/true
1077 REPOS_UPDATED=True
1078 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001079 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001080 fi
1081}
1082
1083function real_install_package {
1084 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001085 apt_get install "$@"
1086 elif is_fedora; then
1087 yum_install "$@"
1088 elif is_suse; then
1089 zypper_install "$@"
1090 else
1091 exit_distro_not_supported "installing packages"
1092 fi
1093}
1094
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001095# Distro-agnostic package installer
1096# install_package package [package ...]
1097function install_package {
1098 update_package_repo
1099 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1100}
1101
Dean Troyerdff49a22014-01-30 15:37:40 -06001102# Distro-agnostic function to tell if a package is installed
1103# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001104function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001105 if [[ -z "$@" ]]; then
1106 return 1
1107 fi
1108
1109 if [[ -z "$os_PACKAGE" ]]; then
1110 GetOSVersion
1111 fi
1112
1113 if [[ "$os_PACKAGE" = "deb" ]]; then
1114 dpkg -s "$@" > /dev/null 2> /dev/null
1115 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1116 rpm --quiet -q "$@"
1117 else
1118 exit_distro_not_supported "finding if a package is installed"
1119 fi
1120}
1121
1122# Distro-agnostic package uninstaller
1123# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001124function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001125 if is_ubuntu; then
1126 apt_get purge "$@"
1127 elif is_fedora; then
1128 sudo yum remove -y "$@"
1129 elif is_suse; then
1130 sudo zypper rm "$@"
1131 else
1132 exit_distro_not_supported "uninstalling packages"
1133 fi
1134}
1135
1136# Wrapper for ``yum`` to set proxy environment variables
1137# Uses globals ``OFFLINE``, ``*_proxy``
1138# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001139function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001140 [[ "$OFFLINE" = "True" ]] && return
1141 local sudo="sudo"
1142 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001143
1144 # The manual check for missing packages is because yum -y assumes
1145 # missing packages are OK. See
1146 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001147 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1148 no_proxy=$no_proxy \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001149 yum install -y "$@" 2>&1 | \
1150 awk '
1151 BEGIN { fail=0 }
1152 /No package/ { fail=1 }
1153 { print }
1154 END { exit fail }' || \
1155 die $LINENO "Missing packages detected"
1156
1157 # also ensure we catch a yum failure
1158 if [[ ${PIPESTATUS[0]} != 0 ]]; then
1159 die $LINENO "Yum install failure"
1160 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001161}
1162
1163# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001164# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001165# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001166function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001167 [[ "$OFFLINE" = "True" ]] && return
1168 local sudo="sudo"
1169 [[ "$(id -u)" = "0" ]] && sudo="env"
1170 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1171 zypper --non-interactive install --auto-agree-with-licenses "$@"
1172}
1173
1174
1175# Process Functions
1176# =================
1177
1178# _run_process() is designed to be backgrounded by run_process() to simulate a
1179# fork. It includes the dirty work of closing extra filehandles and preparing log
1180# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001181# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001182# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001183# If an optional group is provided sg will be used to set the group of
1184# the command.
1185# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001186function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001187 local service=$1
1188 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001189 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001190
1191 # Undo logging redirections and close the extra descriptors
1192 exec 1>&3
1193 exec 2>&3
1194 exec 3>&-
1195 exec 6>&-
1196
1197 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001198 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1199 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001200
1201 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1202 export PYTHONUNBUFFERED=1
1203 fi
1204
Dean Troyer3159a822014-08-27 14:13:58 -05001205 # Run under ``setsid`` to force the process to become a session and group leader.
1206 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001207 if [[ -n "$group" ]]; then
1208 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1209 else
1210 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1211 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001212
1213 # Just silently exit this process
1214 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001215}
1216
1217# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1218# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001219# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001220# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001221function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001222 SCREEN_NAME=${SCREEN_NAME:-stack}
1223 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1224
1225 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1226 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1227 fi
1228
1229 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1230}
1231
1232# Find out if a process exists by partial name.
1233# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001234function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001235 local name=$1
1236 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001237 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001238 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001239 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001240}
1241
Dean Troyer3159a822014-08-27 14:13:58 -05001242# Run a single service under screen or directly
1243# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001244# If an optional group is provided sg will be used to run the
1245# command as that group.
1246# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001247function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001248 local service=$1
1249 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001250 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001251
Dean Troyer3159a822014-08-27 14:13:58 -05001252 if is_service_enabled $service; then
1253 if [[ "$USE_SCREEN" = "True" ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001254 screen_service "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001255 else
1256 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001257 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001258 fi
1259 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001260}
1261
1262# Helper to launch a service in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001263# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1264# ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001265# screen_service service "command-line" [group]
1266# Run a command in a shell in a screen window, if an optional group
1267# is provided, use sg to set the group of the command.
Dean Troyer3159a822014-08-27 14:13:58 -05001268function screen_service {
1269 local service=$1
1270 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001271 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001272
Sean Dagueea22a4f2014-06-27 15:21:41 -04001273 SCREEN_NAME=${SCREEN_NAME:-stack}
1274 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1275 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001276
Dean Troyer3159a822014-08-27 14:13:58 -05001277 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001278 # Append the service to the screen rc file
Dean Troyer3159a822014-08-27 14:13:58 -05001279 screen_rc "$service" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001280
Dean Troyer3159a822014-08-27 14:13:58 -05001281 screen -S $SCREEN_NAME -X screen -t $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001282
Dean Troyer3159a822014-08-27 14:13:58 -05001283 if [[ -n ${SCREEN_LOGDIR} ]]; then
1284 screen -S $SCREEN_NAME -p $service -X logfile ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log
1285 screen -S $SCREEN_NAME -p $service -X log on
1286 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001287 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001288
1289 # sleep to allow bash to be ready to be send the command - we are
1290 # creating a new window in screen and then sends characters, so if
1291 # bash isn't running by the time we send the command, nothing happens
1292 sleep 3
1293
1294 NL=`echo -ne '\015'`
1295 # This fun command does the following:
1296 # - the passed server command is backgrounded
1297 # - the pid of the background process is saved in the usual place
1298 # - the server process is brought back to the foreground
1299 # - if the server process exits prematurely the fg command errors
Dean Troyer3324f192014-09-18 09:26:39 -05001300 # and a message is written to stdout and the service failure file
1301 #
Chris Dent2f27a0e2014-09-09 13:46:02 +01001302 # The pid saved can be used in stop_process() as a process group
Dean Troyer3159a822014-08-27 14:13:58 -05001303 # id to kill off all child processes
Chris Dent2f27a0e2014-09-09 13:46:02 +01001304 if [[ -n "$group" ]]; then
1305 command="sg $group '$command'"
1306 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001307 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 -06001308 fi
1309}
1310
1311# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001312# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001313# screen_rc service "command-line"
1314function screen_rc {
1315 SCREEN_NAME=${SCREEN_NAME:-stack}
1316 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1317 if [[ ! -e $SCREENRC ]]; then
1318 # Name the screen session
1319 echo "sessionname $SCREEN_NAME" > $SCREENRC
1320 # Set a reasonable statusbar
1321 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1322 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1323 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1324 echo "screen -t shell bash" >> $SCREENRC
1325 fi
1326 # If this service doesn't already exist in the screenrc file
1327 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1328 NL=`echo -ne '\015'`
1329 echo "screen -t $1 bash" >> $SCREENRC
1330 echo "stuff \"$2$NL\"" >> $SCREENRC
1331
1332 if [[ -n ${SCREEN_LOGDIR} ]]; then
1333 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1334 echo "log on" >>$SCREENRC
1335 fi
1336 fi
1337}
1338
1339# Stop a service in screen
1340# If a PID is available use it, kill the whole process group via TERM
1341# If screen is being used kill the screen window; this will catch processes
1342# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001343# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001344# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001345function screen_stop_service {
1346 local service=$1
1347
Dean Troyerdff49a22014-01-30 15:37:40 -06001348 SCREEN_NAME=${SCREEN_NAME:-stack}
1349 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1350 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1351
Dean Troyer3159a822014-08-27 14:13:58 -05001352 if is_service_enabled $service; then
1353 # Clean up the screen window
1354 screen -S $SCREEN_NAME -p $service -X kill
1355 fi
1356}
1357
1358# Stop a service process
1359# If a PID is available use it, kill the whole process group via TERM
1360# If screen is being used kill the screen window; this will catch processes
1361# that did not leave a PID behind
1362# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1363# stop_process service
1364function stop_process {
1365 local service=$1
1366
1367 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1368 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1369
1370 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001371 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001372 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1373 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1374 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001375 fi
1376 if [[ "$USE_SCREEN" = "True" ]]; then
1377 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001378 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001379 fi
1380 fi
1381}
1382
1383# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001384# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001385# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001386function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001387 local service
1388 local failures
1389 SCREEN_NAME=${SCREEN_NAME:-stack}
1390 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1391
1392
1393 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1394 echo "No service status directory found"
1395 return
1396 fi
1397
1398 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001399 # make this -o errexit safe
1400 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001401
1402 for service in $failures; do
1403 service=`basename $service`
1404 service=${service%.failure}
1405 echo "Error: Service $service is not running"
1406 done
1407
1408 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001409 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001410 fi
1411}
1412
Chris Dent2f27a0e2014-09-09 13:46:02 +01001413# Tail a log file in a screen if USE_SCREEN is true.
1414function tail_log {
1415 local service=$1
1416 local logfile=$2
1417
1418 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1419 if [[ "$USE_SCREEN" = "True" ]]; then
1420 screen_service "$service" "sudo tail -f $logfile"
1421 fi
1422}
1423
Dean Troyerdff49a22014-01-30 15:37:40 -06001424
Dean Troyer3159a822014-08-27 14:13:58 -05001425# Deprecated Functions
1426# --------------------
1427
1428# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1429# fork. It includes the dirty work of closing extra filehandles and preparing log
1430# files to produce the same logs as screen_it(). The log filename is derived
1431# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1432# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1433# _old_run_process service "command-line"
1434function _old_run_process {
1435 local service=$1
1436 local command="$2"
1437
1438 # Undo logging redirections and close the extra descriptors
1439 exec 1>&3
1440 exec 2>&3
1441 exec 3>&-
1442 exec 6>&-
1443
1444 if [[ -n ${SCREEN_LOGDIR} ]]; then
1445 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1446 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1447
1448 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1449 export PYTHONUNBUFFERED=1
1450 fi
1451
1452 exec /bin/bash -c "$command"
1453 die "$service exec failure: $command"
1454}
1455
1456# old_run_process() launches a child process that closes all file descriptors and
1457# then exec's the passed in command. This is meant to duplicate the semantics
1458# of screen_it() without screen. PIDs are written to
1459# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1460# old_run_process service "command-line"
1461function old_run_process {
1462 local service=$1
1463 local command="$2"
1464
1465 # Spawn the child process
1466 _old_run_process "$service" "$command" &
1467 echo $!
1468}
1469
1470# Compatibility for existing start_XXXX() functions
1471# Uses global ``USE_SCREEN``
1472# screen_it service "command-line"
1473function screen_it {
1474 if is_service_enabled $1; then
1475 # Append the service to the screen rc file
1476 screen_rc "$1" "$2"
1477
1478 if [[ "$USE_SCREEN" = "True" ]]; then
1479 screen_service "$1" "$2"
1480 else
1481 # Spawn directly without screen
1482 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1483 fi
1484 fi
1485}
1486
1487# Compatibility for existing stop_XXXX() functions
1488# Stop a service in screen
1489# If a PID is available use it, kill the whole process group via TERM
1490# If screen is being used kill the screen window; this will catch processes
1491# that did not leave a PID behind
1492# screen_stop service
1493function screen_stop {
1494 # Clean up the screen window
1495 stop_process $1
1496}
1497
1498
Dean Troyerdff49a22014-01-30 15:37:40 -06001499# Python Functions
1500# ================
1501
1502# Get the path to the pip command.
1503# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001504function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001505 which pip || which pip-python
1506
1507 if [ $? -ne 0 ]; then
1508 die $LINENO "Unable to find pip; cannot continue"
1509 fi
1510}
1511
1512# Get the path to the direcotry where python executables are installed.
1513# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001514function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001515 if is_fedora || is_suse; then
1516 echo "/usr/bin"
1517 else
1518 echo "/usr/local/bin"
1519 fi
1520}
1521
1522# Wrapper for ``pip install`` to set cache and proxy environment variables
1523# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
1524# ``TRACK_DEPENDS``, ``*_proxy``
1525# pip_install package [package ...]
1526function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001527 local xtrace=$(set +o | grep xtrace)
1528 set +o xtrace
1529 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1530 $xtrace
1531 return
1532 fi
1533
Dean Troyerdff49a22014-01-30 15:37:40 -06001534 if [[ -z "$os_PACKAGE" ]]; then
1535 GetOSVersion
1536 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001537 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1538 # TRACK_DEPENDS=True installation creates a circular dependency when
1539 # we attempt to install virtualenv into a virualenv, so we must global
1540 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001541 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001542 local cmd_pip=$DEST/.venv/bin/pip
1543 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001544 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001545 local cmd_pip=$(get_pip_command)
1546 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001547 fi
1548
1549 # Mirror option not needed anymore because pypi has CDN available,
1550 # but it's useful in certain circumstances
1551 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001552 local pip_mirror_opt=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001553 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001554 pip_mirror_opt="--use-mirrors"
Dean Troyerdff49a22014-01-30 15:37:40 -06001555 fi
1556
Sean Dague45917cc2014-02-24 16:09:14 -05001557 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001558 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001559 http_proxy=$http_proxy \
1560 https_proxy=$https_proxy \
1561 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001562 $cmd_pip install \
1563 $pip_mirror_opt $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001564
1565 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1566 local test_req="$@/test-requirements.txt"
1567 if [[ -e "$test_req" ]]; then
1568 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1569 http_proxy=$http_proxy \
1570 https_proxy=$https_proxy \
1571 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001572 $cmd_pip install \
1573 $pip_mirror_opt -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001574 fi
1575 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001576}
1577
Sean Daguecc524062014-10-01 09:06:43 -04001578# should we use this library from their git repo, or should we let it
1579# get pulled in via pip dependencies.
1580function use_library_from_git {
1581 local name=$1
1582 local enabled=1
1583 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1584 return $enabled
1585}
1586
1587# setup a library by name. If we are trying to use the library from
1588# git, we'll do a git based install, otherwise we'll punt and the
1589# library should be installed by a requirements pull from another
1590# project.
1591function setup_lib {
1592 local name=$1
1593 local dir=${GITDIR[$name]}
1594 setup_install $dir
1595}
1596
1597
Sean Dague099e5e32014-03-31 10:35:43 -04001598# this should be used if you want to install globally, all libraries should
1599# use this, especially *oslo* ones
1600function setup_install {
1601 local project_dir=$1
1602 setup_package_with_req_sync $project_dir
1603}
1604
1605# this should be used for projects which run services, like all services
1606function setup_develop {
1607 local project_dir=$1
1608 setup_package_with_req_sync $project_dir -e
1609}
1610
Dean Troyeraf616d92014-02-17 12:57:55 -06001611# ``pip install -e`` the package, which processes the dependencies
1612# using pip before running `setup.py develop`
1613#
1614# Updates the dependencies in project_dir from the
1615# openstack/requirements global list before installing anything.
1616#
1617# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1618# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001619function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001620 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001621 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001622
Dean Troyeraf616d92014-02-17 12:57:55 -06001623 # Don't update repo if local changes exist
1624 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001625 # ``errexit`` requires us to trap the exit code when the repo is changed
1626 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001627
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001628 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001629 (cd $REQUIREMENTS_DIR; \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001630 python update.py $project_dir)
Dean Troyeraf616d92014-02-17 12:57:55 -06001631 fi
1632
Sean Dague099e5e32014-03-31 10:35:43 -04001633 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001634
1635 # We've just gone and possibly modified the user's source tree in an
1636 # automated way, which is considered bad form if it's a development
1637 # tree because we've screwed up their next git checkin. So undo it.
1638 #
1639 # However... there are some circumstances, like running in the gate
1640 # where we really really want the overridden version to stick. So provide
1641 # a variable that tells us whether or not we should UNDO the requirements
1642 # changes (this will be set to False in the OpenStack ci gate)
1643 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001644 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001645 (cd $project_dir && git reset --hard)
1646 fi
1647 fi
1648}
1649
1650# ``pip install -e`` the package, which processes the dependencies
1651# using pip before running `setup.py develop`
1652# Uses globals ``STACK_USER``
1653# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001654function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001655 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001656 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001657
Sean Dague099e5e32014-03-31 10:35:43 -04001658 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001659 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001660 if [[ "$flags" == "-e" ]]; then
1661 safe_chown -R $STACK_USER $1/*.egg-info
1662 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001663}
1664
Dean Troyerdff49a22014-01-30 15:37:40 -06001665
1666# Service Functions
1667# =================
1668
1669# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1670# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001671function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001672 echo "$1" | sed -e '
1673 s/,,/,/g;
1674 s/^,//;
1675 s/,$//
1676 '
1677}
1678
1679# disable_all_services() removes all current services
1680# from ``ENABLED_SERVICES`` to reset the configuration
1681# before a minimal installation
1682# Uses global ``ENABLED_SERVICES``
1683# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001684function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001685 ENABLED_SERVICES=""
1686}
1687
1688# Remove all services starting with '-'. For example, to install all default
1689# services except rabbit (rabbit) set in ``localrc``:
1690# ENABLED_SERVICES+=",-rabbit"
1691# Uses global ``ENABLED_SERVICES``
1692# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001693function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001694 local tmpsvcs="${ENABLED_SERVICES}"
1695 local service
1696 for service in ${tmpsvcs//,/ }; do
1697 if [[ ${service} == -* ]]; then
1698 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1699 fi
1700 done
1701 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1702}
1703
1704# disable_service() removes the services passed as argument to the
1705# ``ENABLED_SERVICES`` list, if they are present.
1706#
1707# For example:
1708# disable_service rabbit
1709#
1710# This function does not know about the special cases
1711# for nova, glance, and neutron built into is_service_enabled().
1712# Uses global ``ENABLED_SERVICES``
1713# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001714function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001715 local tmpsvcs=",${ENABLED_SERVICES},"
1716 local service
1717 for service in $@; do
1718 if is_service_enabled $service; then
1719 tmpsvcs=${tmpsvcs//,$service,/,}
1720 fi
1721 done
1722 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1723}
1724
1725# enable_service() adds the services passed as argument to the
1726# ``ENABLED_SERVICES`` list, if they are not already present.
1727#
1728# For example:
1729# enable_service qpid
1730#
1731# This function does not know about the special cases
1732# for nova, glance, and neutron built into is_service_enabled().
1733# Uses global ``ENABLED_SERVICES``
1734# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001735function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001736 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001737 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001738 for service in $@; do
1739 if ! is_service_enabled $service; then
1740 tmpsvcs+=",$service"
1741 fi
1742 done
1743 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1744 disable_negated_services
1745}
1746
1747# is_service_enabled() checks if the service(s) specified as arguments are
1748# enabled by the user in ``ENABLED_SERVICES``.
1749#
1750# Multiple services specified as arguments are ``OR``'ed together; the test
1751# is a short-circuit boolean, i.e it returns on the first match.
1752#
1753# There are special cases for some 'catch-all' services::
1754# **nova** returns true if any service enabled start with **n-**
1755# **cinder** returns true if any service enabled start with **c-**
1756# **ceilometer** returns true if any service enabled start with **ceilometer**
1757# **glance** returns true if any service enabled start with **g-**
1758# **neutron** returns true if any service enabled start with **q-**
1759# **swift** returns true if any service enabled start with **s-**
1760# **trove** returns true if any service enabled start with **tr-**
1761# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1762# **s-** services will be enabled. This will be deprecated in the future.
1763#
1764# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1765# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1766# as enabled in this case.
1767#
1768# Uses global ``ENABLED_SERVICES``
1769# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001770function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001771 local xtrace=$(set +o | grep xtrace)
1772 set +o xtrace
1773 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001774 local services=$@
1775 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001776 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001777 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001778
1779 # Look for top-level 'enabled' function for this service
1780 if type is_${service}_enabled >/dev/null 2>&1; then
1781 # A function exists for this service, use it
1782 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001783 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001784 fi
1785
1786 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1787 # are implemented
1788
Sean Dague45917cc2014-02-24 16:09:14 -05001789 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001790 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001791 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1792 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1793 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1794 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1795 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1796 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1797 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1798 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1799 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001800 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001801 done
Sean Dague45917cc2014-02-24 16:09:14 -05001802 $xtrace
1803 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001804}
1805
1806# Toggle enable/disable_service for services that must run exclusive of each other
1807# $1 The name of a variable containing a space-separated list of services
1808# $2 The name of a variable in which to store the enabled service's name
1809# $3 The name of the service to enable
1810function use_exclusive_service {
1811 local options=${!1}
1812 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001813 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001814 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001815 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001816 for opt in $options;do
1817 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1818 done
1819 eval "$out=$selection"
1820 return 0
1821}
1822
1823
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001824# System Functions
1825# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001826
1827# Only run the command if the target file (the last arg) is not on an
1828# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001829function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001830 local xtrace=$(set +o | grep xtrace)
1831 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001832 local args=( $@ )
1833 local last
1834 local sudo_cmd
1835 local dir_to_check
1836
1837 let last="${#args[*]} - 1"
1838
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001839 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001840 if [ ! -d "$dir_to_check" ]; then
1841 dir_to_check=`dirname "$dir_to_check"`
1842 fi
1843
1844 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001845 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001846 return 0
1847 fi
1848
1849 if [[ $TRACK_DEPENDS = True ]]; then
1850 sudo_cmd="env"
1851 else
1852 sudo_cmd="sudo"
1853 fi
1854
Sean Dague45917cc2014-02-24 16:09:14 -05001855 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001856 $sudo_cmd $@
1857}
1858
1859# Exit 0 if address is in network or 1 if address is not in network
1860# ip-range is in CIDR notation: 1.2.3.4/20
1861# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001862function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001863 local ip=$1
1864 local range=$2
1865 local masklen=${range#*/}
1866 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1867 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1868 [[ $network == $subnet ]]
1869}
1870
1871# Add a user to a group.
1872# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001873function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001874 local user=$1
1875 local group=$2
1876
1877 if [[ -z "$os_VENDOR" ]]; then
1878 GetOSVersion
1879 fi
1880
1881 # SLE11 and openSUSE 12.2 don't have the usual usermod
1882 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1883 sudo usermod -a -G "$group" "$user"
1884 else
1885 sudo usermod -A "$group" "$user"
1886 fi
1887}
1888
1889# Convert CIDR notation to a IPv4 netmask
1890# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001891function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001892 local maskpat="255 255 255 255"
1893 local maskdgt="254 252 248 240 224 192 128"
1894 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1895 echo ${1-0}.${2-0}.${3-0}.${4-0}
1896}
1897
1898# Gracefully cp only if source file/dir exists
1899# cp_it source destination
1900function cp_it {
1901 if [ -e $1 ] || [ -d $1 ]; then
1902 cp -pRL $1 $2
1903 fi
1904}
1905
1906# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1907# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1908# ``localrc`` or on the command line if necessary::
1909#
1910# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1911#
1912# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1913
Ian Wienandaee18c72014-02-21 15:35:08 +11001914function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001915 if [[ -n "$http_proxy" ]]; then
1916 export http_proxy=$http_proxy
1917 fi
1918 if [[ -n "$https_proxy" ]]; then
1919 export https_proxy=$https_proxy
1920 fi
1921 if [[ -n "$no_proxy" ]]; then
1922 export no_proxy=$no_proxy
1923 fi
1924}
1925
1926# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001927function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001928 local mount_type=`stat -f -L -c %T $1`
1929 test "$mount_type" == "nfs"
1930}
1931
1932# Return the network portion of the given IP address using netmask
1933# netmask is in the traditional dotted-quad format
1934# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001935function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001936 local ip=$1
1937 local mask=$2
1938 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1939 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1940 echo $subnet
1941}
1942
1943# Service wrapper to restart services
1944# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001945function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001946 if is_ubuntu; then
1947 sudo /usr/sbin/service $1 restart
1948 else
1949 sudo /sbin/service $1 restart
1950 fi
1951}
1952
1953# Only change permissions of a file or directory if it is not on an
1954# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001955function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001956 _safe_permission_operation chmod $@
1957}
1958
1959# Only change ownership of a file or directory if it is not on an NFS
1960# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001961function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001962 _safe_permission_operation chown $@
1963}
1964
1965# Service wrapper to start services
1966# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001967function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001968 if is_ubuntu; then
1969 sudo /usr/sbin/service $1 start
1970 else
1971 sudo /sbin/service $1 start
1972 fi
1973}
1974
1975# Service wrapper to stop services
1976# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001977function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001978 if is_ubuntu; then
1979 sudo /usr/sbin/service $1 stop
1980 else
1981 sudo /sbin/service $1 stop
1982 fi
1983}
1984
1985
1986# Restore xtrace
1987$XTRACE
1988
1989# Local variables:
1990# mode: shell-script
1991# End: