blob: a96c702728ae6f1fa89d48bcdebd918d987a8f7a [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#
Jamie Lennox51f0de52014-10-20 16:32:34 +020022# - ``GIT_DEPTH``
Dean Troyerdff49a22014-01-30 15:37:40 -060023# - ``ENABLED_SERVICES``
24# - ``ERROR_ON_CLONE``
25# - ``FILES``
26# - ``OFFLINE``
27# - ``PIP_DOWNLOAD_CACHE``
Dean Troyerdff49a22014-01-30 15:37:40 -060028# - ``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
502# Determine if current distribution is a Fedora-based distribution
503# (Fedora, RHEL, CentOS, etc).
504# is_fedora
505function is_fedora {
506 if [[ -z "$os_VENDOR" ]]; then
507 GetOSVersion
508 fi
509
anju Tiwari6c639c92014-07-15 18:11:54 +0530510 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
511 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600512}
513
514
515# Determine if current distribution is a SUSE-based distribution
516# (openSUSE, SLE).
517# is_suse
518function is_suse {
519 if [[ -z "$os_VENDOR" ]]; then
520 GetOSVersion
521 fi
522
523 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
524}
525
526
527# Determine if current distribution is an Ubuntu-based distribution
528# It will also detect non-Ubuntu but Debian-based distros
529# is_ubuntu
530function is_ubuntu {
531 if [[ -z "$os_PACKAGE" ]]; then
532 GetOSVersion
533 fi
534 [ "$os_PACKAGE" = "deb" ]
535}
536
537
538# Git Functions
539# =============
540
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600541# Returns openstack release name for a given branch name
542# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100543function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600544 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700545 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600546 echo ${branch#*/}
547 else
548 echo "master"
549 fi
550}
551
Dean Troyerdff49a22014-01-30 15:37:40 -0600552# git clone only if directory doesn't exist already. Since ``DEST`` might not
553# be owned by the installation user, we create the directory and change the
554# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500555# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
556# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600557# does not exist (default is False, meaning the repo will be cloned).
Jamie Lennox51f0de52014-10-20 16:32:34 +0200558# Set global ``GIT_DEPTH=<number>`` to limit the history depth of the git clone
559# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``, ``GIT_DEPTH``
Dean Troyerdff49a22014-01-30 15:37:40 -0600560# git_clone remote dest-dir branch
561function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500562 local git_remote=$1
563 local git_dest=$2
564 local git_ref=$3
565 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200566 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500567
Dean Troyerdff49a22014-01-30 15:37:40 -0600568 RECLONE=$(trueorfalse False $RECLONE)
569
YAMAMOTO Takashi292b2a72014-10-31 13:48:58 +0900570 if [[ -n "${GIT_DEPTH}" ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200571 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
572 fi
573
Dean Troyerdff49a22014-01-30 15:37:40 -0600574 if [[ "$OFFLINE" = "True" ]]; then
575 echo "Running in offline mode, clones already exist"
576 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500577 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600578 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400579 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600580 return
581 fi
582
Dean Troyer50cda692014-07-25 11:57:20 -0500583 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600584 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500585 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600586 [[ "$ERROR_ON_CLONE" = "True" ]] && \
587 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200588 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600589 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500590 cd $git_dest
591 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600592 else
593 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500594 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600595 [[ "$ERROR_ON_CLONE" = "True" ]] && \
596 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200597 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500598 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600599 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500600 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600601 elif [[ "$RECLONE" = "True" ]]; then
602 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500603 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600604 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500605 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100606 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600607 # remove the existing ignored files (like pyc) as they cause breakage
608 # (due to the py files having older timestamps than our pyc, so python
609 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500610 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600611
Dean Troyer50cda692014-07-25 11:57:20 -0500612 # handle git_ref accordingly to type (tag, branch)
613 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
614 git_update_tag $git_ref
615 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
616 git_update_branch $git_ref
617 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
618 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600619 else
Dean Troyer50cda692014-07-25 11:57:20 -0500620 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600621 fi
622
623 fi
624 fi
625
626 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500627 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600628 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400629 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600630}
631
Sean Daguecc524062014-10-01 09:06:43 -0400632# A variation on git clone that lets us specify a project by it's
633# actual name, like oslo.config. This is exceptionally useful in the
634# library installation case
635function git_clone_by_name {
636 local name=$1
637 local repo=${GITREPO[$name]}
638 local dir=${GITDIR[$name]}
639 local branch=${GITBRANCH[$name]}
640 git_clone $repo $dir $branch
641}
642
643
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100644# git can sometimes get itself infinitely stuck with transient network
645# errors or other issues with the remote end. This wraps git in a
646# timeout/retry loop and is intended to watch over non-local git
647# processes that might hang. GIT_TIMEOUT, if set, is passed directly
648# to timeout(1); otherwise the default value of 0 maintains the status
649# quo of waiting forever.
650# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100651function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100652 local count=0
653 local timeout=0
654
655 if [[ -n "${GIT_TIMEOUT}" ]]; then
656 timeout=${GIT_TIMEOUT}
657 fi
658
659 until timeout -s SIGINT ${timeout} git "$@"; do
660 # 124 is timeout(1)'s special return code when it reached the
661 # timeout; otherwise assume fatal failure
662 if [[ $? -ne 124 ]]; then
663 die $LINENO "git call failed: [git $@]"
664 fi
665
666 count=$(($count + 1))
667 warn "timeout ${count} for git call: [git $@]"
668 if [ $count -eq 3 ]; then
669 die $LINENO "Maximum of 3 git retries reached"
670 fi
671 sleep 5
672 done
673}
674
Dean Troyerdff49a22014-01-30 15:37:40 -0600675# git update using reference as a branch.
676# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100677function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500678 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600679
Dean Troyer50cda692014-07-25 11:57:20 -0500680 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600681 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500682 git branch -D $git_branch || true
683 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600684}
685
686# git update using reference as a branch.
687# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100688function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500689 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600690
Dean Troyer50cda692014-07-25 11:57:20 -0500691 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600692}
693
694# git update using reference as a tag. Be careful editing source at that repo
695# as working copy will be in a detached mode
696# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100697function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500698 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600699
Dean Troyer50cda692014-07-25 11:57:20 -0500700 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600701 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500702 git_timed fetch origin tag $git_tag
703 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600704}
705
706
707# OpenStack Functions
708# ===================
709
710# Get the default value for HOST_IP
711# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100712function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600713 local fixed_range=$1
714 local floating_range=$2
715 local host_ip_iface=$3
716 local host_ip=$4
717
718 # Find the interface used for the default route
719 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
720 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
721 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
722 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500723 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
724 local ip
725 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600726 # Attempt to filter out IP addresses that are part of the fixed and
727 # floating range. Note that this method only works if the ``netaddr``
728 # python library is installed. If it is not installed, an error
729 # will be printed and the first IP from the interface will be used.
730 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
731 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500732 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
733 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600734 break;
735 fi
736 done
737 fi
738 echo $host_ip
739}
740
Attila Fazekasf71b5002014-05-28 09:52:22 +0200741# Generates hex string from ``size`` byte of pseudo random data
742# generate_hex_string size
743function generate_hex_string {
744 local size=$1
745 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
746}
747
Dean Troyerdff49a22014-01-30 15:37:40 -0600748# Grab a numbered field from python prettytable output
749# Fields are numbered starting with 1
750# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
751# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100752function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500753 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600754 while read data; do
755 if [ "$1" -lt 0 ]; then
756 field="(\$(NF$1))"
757 else
758 field="\$$(($1 + 1))"
759 fi
760 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
761 done
762}
763
764# Add a policy to a policy.json file
765# Do nothing if the policy already exists
766# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100767function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600768 local policy_file=$1
769 local policy_name=$2
770 local policy_perm=$3
771
772 if grep -q ${policy_name} ${policy_file}; then
773 echo "Policy ${policy_name} already exists in ${policy_file}"
774 return
775 fi
776
777 # Add a terminating comma to policy lines without one
778 # Remove the closing '}' and all lines following to the end-of-file
779 local tmpfile=$(mktemp)
780 uniq ${policy_file} | sed -e '
781 s/]$/],/
782 /^[}]/,$d
783 ' > ${tmpfile}
784
785 # Append policy and closing brace
786 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
787 echo "}" >>${tmpfile}
788
789 mv ${tmpfile} ${policy_file}
790}
791
Alistair Coles24779f62014-10-15 18:57:59 +0100792# Gets or creates a domain
793# Usage: get_or_create_domain <name> <description>
794function get_or_create_domain {
795 local os_url="$KEYSTONE_SERVICE_URI/v3"
796 # Gets domain id
797 local domain_id=$(
798 # Gets domain id
799 openstack --os-token=$OS_TOKEN --os-url=$os_url \
800 --os-identity-api-version=3 domain show $1 \
801 -f value -c id 2>/dev/null ||
802 # Creates new domain
803 openstack --os-token=$OS_TOKEN --os-url=$os_url \
804 --os-identity-api-version=3 domain create $1 \
805 --description "$2" \
806 -f value -c id
807 )
808 echo $domain_id
809}
810
Bartosz Górski0abde392014-02-28 14:15:19 +0100811# Gets or creates user
Alistair Coles24779f62014-10-15 18:57:59 +0100812# Usage: get_or_create_user <username> <password> <project> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100813function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200814 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500815 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200816 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500817 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200818 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100819 local os_cmd="openstack"
820 local domain=""
821 if [[ ! -z "$5" ]]; then
822 domain="--domain=$5"
823 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
824 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100825 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500826 local user_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100827 # Gets user id
Alistair Coles24779f62014-10-15 18:57:59 +0100828 $os_cmd user show $1 $domain -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100829 # Creates new user
Alistair Coles24779f62014-10-15 18:57:59 +0100830 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100831 $1 \
832 --password "$2" \
833 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500834 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100835 $domain \
Bartosz Górski0abde392014-02-28 14:15:19 +0100836 -f value -c id
837 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500838 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100839}
840
841# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100842# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100843function get_or_create_project {
844 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100845 local os_cmd="openstack"
846 local domain=""
847 if [[ ! -z "$2" ]]; then
848 domain="--domain=$2"
849 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
850 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500851 local project_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100852 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100853 $os_cmd project show $1 $domain -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100854 # Creates new project if not exists
Alistair Coles24779f62014-10-15 18:57:59 +0100855 $os_cmd project create $1 $domain -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100856 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500857 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100858}
859
860# Gets or creates role
861# Usage: get_or_create_role <name>
862function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500863 local role_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100864 # Gets role id
865 openstack role show $1 -f value -c id 2>/dev/null ||
866 # Creates role if not exists
867 openstack role create $1 -f value -c id
868 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500869 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100870}
871
872# Gets or adds user role
873# Usage: get_or_add_user_role <role> <user> <project>
874function get_or_add_user_role {
875 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500876 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100877 $2 \
878 --project $3 \
879 --column "ID" \
880 --column "Name" \
881 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500882 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100883 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500884 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100885 $1 \
886 --user $2 \
887 --project $3 \
888 | grep " id " | get_field 2)
889 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500890 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100891}
892
893# Gets or creates service
894# Usage: get_or_create_service <name> <type> <description>
895function get_or_create_service {
896 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500897 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100898 # Gets service id
899 openstack service show $1 -f value -c id 2>/dev/null ||
900 # Creates new service if not exists
901 openstack service create \
902 $1 \
903 --type=$2 \
904 --description="$3" \
905 -f value -c id
906 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500907 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100908}
909
910# Gets or creates endpoint
911# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
912function get_or_create_endpoint {
913 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500914 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100915 --column "ID" \
916 --column "Region" \
917 --column "Service Name" \
918 | grep " $2 " \
919 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500920 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100921 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500922 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100923 $1 \
924 --region $2 \
925 --publicurl $3 \
926 --adminurl $4 \
927 --internalurl $5 \
928 | grep " id " | get_field 2)
929 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500930 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100931}
Dean Troyerdff49a22014-01-30 15:37:40 -0600932
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500933
Dean Troyerdff49a22014-01-30 15:37:40 -0600934# Package Functions
935# =================
936
937# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100938function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600939 local pkg_dir
940 if is_ubuntu; then
941 pkg_dir=$FILES/apts
942 elif is_fedora; then
943 pkg_dir=$FILES/rpms
944 elif is_suse; then
945 pkg_dir=$FILES/rpms-suse
946 else
947 exit_distro_not_supported "list of packages"
948 fi
949 echo "$pkg_dir"
950}
951
952# Wrapper for ``apt-get`` to set cache and proxy environment variables
953# Uses globals ``OFFLINE``, ``*_proxy``
954# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100955function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500956 local xtrace=$(set +o | grep xtrace)
957 set +o xtrace
958
Dean Troyerdff49a22014-01-30 15:37:40 -0600959 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
960 local sudo="sudo"
961 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500962
963 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600964 $sudo DEBIAN_FRONTEND=noninteractive \
965 http_proxy=$http_proxy https_proxy=$https_proxy \
966 no_proxy=$no_proxy \
967 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
968}
969
970# get_packages() collects a list of package names of any type from the
971# prerequisite files in ``files/{apts|rpms}``. The list is intended
972# to be passed to a package installer such as apt or yum.
973#
974# Only packages required for the services in 1st argument will be
975# included. Two bits of metadata are recognized in the prerequisite files:
976#
977# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
978# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
979# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100980function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500981 local xtrace=$(set +o | grep xtrace)
982 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600983 local services=$@
984 local package_dir=$(_get_package_dir)
985 local file_to_parse
986 local service
987
Flavio Percoco5a91c352014-10-31 18:48:00 +0100988 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
989
Dean Troyerdff49a22014-01-30 15:37:40 -0600990 if [[ -z "$package_dir" ]]; then
991 echo "No package directory supplied"
992 return 1
993 fi
994 if [[ -z "$DISTRO" ]]; then
995 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -0500996 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -0600997 fi
998 for service in ${services//,/ }; do
999 # Allow individual services to specify dependencies
1000 if [[ -e ${package_dir}/${service} ]]; then
1001 file_to_parse="${file_to_parse} $service"
1002 fi
1003 # NOTE(sdague) n-api needs glance for now because that's where
1004 # glance client is
1005 if [[ $service == n-api ]]; then
1006 if [[ ! $file_to_parse =~ nova ]]; then
1007 file_to_parse="${file_to_parse} nova"
1008 fi
1009 if [[ ! $file_to_parse =~ glance ]]; then
1010 file_to_parse="${file_to_parse} glance"
1011 fi
1012 elif [[ $service == c-* ]]; then
1013 if [[ ! $file_to_parse =~ cinder ]]; then
1014 file_to_parse="${file_to_parse} cinder"
1015 fi
1016 elif [[ $service == ceilometer-* ]]; then
1017 if [[ ! $file_to_parse =~ ceilometer ]]; then
1018 file_to_parse="${file_to_parse} ceilometer"
1019 fi
1020 elif [[ $service == s-* ]]; then
1021 if [[ ! $file_to_parse =~ swift ]]; then
1022 file_to_parse="${file_to_parse} swift"
1023 fi
1024 elif [[ $service == n-* ]]; then
1025 if [[ ! $file_to_parse =~ nova ]]; then
1026 file_to_parse="${file_to_parse} nova"
1027 fi
1028 elif [[ $service == g-* ]]; then
1029 if [[ ! $file_to_parse =~ glance ]]; then
1030 file_to_parse="${file_to_parse} glance"
1031 fi
1032 elif [[ $service == key* ]]; then
1033 if [[ ! $file_to_parse =~ keystone ]]; then
1034 file_to_parse="${file_to_parse} keystone"
1035 fi
1036 elif [[ $service == q-* ]]; then
1037 if [[ ! $file_to_parse =~ neutron ]]; then
1038 file_to_parse="${file_to_parse} neutron"
1039 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001040 elif [[ $service == ir-* ]]; then
1041 if [[ ! $file_to_parse =~ ironic ]]; then
1042 file_to_parse="${file_to_parse} ironic"
1043 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001044 fi
1045 done
1046
1047 for file in ${file_to_parse}; do
1048 local fname=${package_dir}/${file}
1049 local OIFS line package distros distro
1050 [[ -e $fname ]] || continue
1051
1052 OIFS=$IFS
1053 IFS=$'\n'
1054 for line in $(<${fname}); do
1055 if [[ $line =~ "NOPRIME" ]]; then
1056 continue
1057 fi
1058
1059 # Assume we want this package
1060 package=${line%#*}
1061 inst_pkg=1
1062
1063 # Look for # dist:xxx in comment
1064 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1065 # We are using BASH regexp matching feature.
1066 package=${BASH_REMATCH[1]}
1067 distros=${BASH_REMATCH[2]}
1068 # In bash ${VAR,,} will lowecase VAR
1069 # Look for a match in the distro list
1070 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1071 # If no match then skip this package
1072 inst_pkg=0
1073 fi
1074 fi
1075
1076 # Look for # testonly in comment
1077 if [[ $line =~ (.*)#.*testonly.* ]]; then
1078 package=${BASH_REMATCH[1]}
1079 # Are we installing test packages? (test for the default value)
1080 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1081 # If not installing test packages the skip this package
1082 inst_pkg=0
1083 fi
1084 fi
1085
1086 if [[ $inst_pkg = 1 ]]; then
1087 echo $package
1088 fi
1089 done
1090 IFS=$OIFS
1091 done
Sean Dague45917cc2014-02-24 16:09:14 -05001092 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001093}
1094
1095# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001096# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001097# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001098function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001099 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001100 return 0
1101 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001102
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001103 if is_ubuntu; then
1104 local xtrace=$(set +o | grep xtrace)
1105 set +o xtrace
1106 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1107 # if there are transient errors pulling the updates, that's fine.
1108 # It may be secondary repositories that we don't really care about.
1109 apt_get update || /bin/true
1110 REPOS_UPDATED=True
1111 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001112 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001113 fi
1114}
1115
1116function real_install_package {
1117 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001118 apt_get install "$@"
1119 elif is_fedora; then
1120 yum_install "$@"
1121 elif is_suse; then
1122 zypper_install "$@"
1123 else
1124 exit_distro_not_supported "installing packages"
1125 fi
1126}
1127
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001128# Distro-agnostic package installer
1129# install_package package [package ...]
1130function install_package {
1131 update_package_repo
1132 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1133}
1134
Dean Troyerdff49a22014-01-30 15:37:40 -06001135# Distro-agnostic function to tell if a package is installed
1136# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001137function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001138 if [[ -z "$@" ]]; then
1139 return 1
1140 fi
1141
1142 if [[ -z "$os_PACKAGE" ]]; then
1143 GetOSVersion
1144 fi
1145
1146 if [[ "$os_PACKAGE" = "deb" ]]; then
1147 dpkg -s "$@" > /dev/null 2> /dev/null
1148 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1149 rpm --quiet -q "$@"
1150 else
1151 exit_distro_not_supported "finding if a package is installed"
1152 fi
1153}
1154
1155# Distro-agnostic package uninstaller
1156# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001157function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001158 if is_ubuntu; then
1159 apt_get purge "$@"
1160 elif is_fedora; then
1161 sudo yum remove -y "$@"
1162 elif is_suse; then
1163 sudo zypper rm "$@"
1164 else
1165 exit_distro_not_supported "uninstalling packages"
1166 fi
1167}
1168
1169# Wrapper for ``yum`` to set proxy environment variables
1170# Uses globals ``OFFLINE``, ``*_proxy``
1171# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001172function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001173 [[ "$OFFLINE" = "True" ]] && return
1174 local sudo="sudo"
1175 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001176
1177 # The manual check for missing packages is because yum -y assumes
1178 # missing packages are OK. See
1179 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001180 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1181 no_proxy=$no_proxy \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001182 yum install -y "$@" 2>&1 | \
1183 awk '
1184 BEGIN { fail=0 }
1185 /No package/ { fail=1 }
1186 { print }
1187 END { exit fail }' || \
1188 die $LINENO "Missing packages detected"
1189
1190 # also ensure we catch a yum failure
1191 if [[ ${PIPESTATUS[0]} != 0 ]]; then
1192 die $LINENO "Yum install failure"
1193 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001194}
1195
1196# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001197# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001198# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001199function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001200 [[ "$OFFLINE" = "True" ]] && return
1201 local sudo="sudo"
1202 [[ "$(id -u)" = "0" ]] && sudo="env"
1203 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1204 zypper --non-interactive install --auto-agree-with-licenses "$@"
1205}
1206
1207
1208# Process Functions
1209# =================
1210
1211# _run_process() is designed to be backgrounded by run_process() to simulate a
1212# fork. It includes the dirty work of closing extra filehandles and preparing log
1213# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001214# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001215# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001216# If an optional group is provided sg will be used to set the group of
1217# the command.
1218# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001219function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001220 local service=$1
1221 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001222 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001223
1224 # Undo logging redirections and close the extra descriptors
1225 exec 1>&3
1226 exec 2>&3
1227 exec 3>&-
1228 exec 6>&-
1229
1230 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001231 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1232 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001233
1234 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1235 export PYTHONUNBUFFERED=1
1236 fi
1237
Dean Troyer3159a822014-08-27 14:13:58 -05001238 # Run under ``setsid`` to force the process to become a session and group leader.
1239 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001240 if [[ -n "$group" ]]; then
1241 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1242 else
1243 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1244 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001245
1246 # Just silently exit this process
1247 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001248}
1249
1250# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1251# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001252# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001253# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001254function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001255 SCREEN_NAME=${SCREEN_NAME:-stack}
1256 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1257
1258 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1259 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1260 fi
1261
1262 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1263}
1264
1265# Find out if a process exists by partial name.
1266# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001267function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001268 local name=$1
1269 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001270 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001271 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001272 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001273}
1274
Dean Troyer3159a822014-08-27 14:13:58 -05001275# Run a single service under screen or directly
1276# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001277# If an optional group is provided sg will be used to run the
1278# command as that group.
1279# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001280function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001281 local service=$1
1282 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001283 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001284
Dean Troyer3159a822014-08-27 14:13:58 -05001285 if is_service_enabled $service; then
1286 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001287 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001288 else
1289 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001290 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001291 fi
1292 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001293}
1294
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001295# Helper to launch a process in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001296# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1297# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001298# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001299# Run a command in a shell in a screen window, if an optional group
1300# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001301function screen_process {
1302 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001303 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001304 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001305
Sean Dagueea22a4f2014-06-27 15:21:41 -04001306 SCREEN_NAME=${SCREEN_NAME:-stack}
1307 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1308 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001309
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001310 # Append the process to the screen rc file
1311 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001312
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001313 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001314
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001315 if [[ -n ${SCREEN_LOGDIR} ]]; then
1316 screen -S $SCREEN_NAME -p $name -X logfile ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log
1317 screen -S $SCREEN_NAME -p $name -X log on
1318 ln -sf ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${name}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001319 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001320
1321 # sleep to allow bash to be ready to be send the command - we are
1322 # creating a new window in screen and then sends characters, so if
1323 # bash isn't running by the time we send the command, nothing happens
1324 sleep 3
1325
1326 NL=`echo -ne '\015'`
1327 # This fun command does the following:
1328 # - the passed server command is backgrounded
1329 # - the pid of the background process is saved in the usual place
1330 # - the server process is brought back to the foreground
1331 # - if the server process exits prematurely the fg command errors
1332 # and a message is written to stdout and the process failure file
1333 #
1334 # The pid saved can be used in stop_process() as a process group
1335 # id to kill off all child processes
1336 if [[ -n "$group" ]]; then
1337 command="sg $group '$command'"
1338 fi
1339 screen -S $SCREEN_NAME -p $name -X stuff "$command & echo \$! >$SERVICE_DIR/$SCREEN_NAME/${name}.pid; fg || echo \"$name failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/${name}.failure\"$NL"
Dean Troyerdff49a22014-01-30 15:37:40 -06001340}
1341
1342# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001343# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001344# screen_rc service "command-line"
1345function screen_rc {
1346 SCREEN_NAME=${SCREEN_NAME:-stack}
1347 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1348 if [[ ! -e $SCREENRC ]]; then
1349 # Name the screen session
1350 echo "sessionname $SCREEN_NAME" > $SCREENRC
1351 # Set a reasonable statusbar
1352 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1353 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1354 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1355 echo "screen -t shell bash" >> $SCREENRC
1356 fi
1357 # If this service doesn't already exist in the screenrc file
1358 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1359 NL=`echo -ne '\015'`
1360 echo "screen -t $1 bash" >> $SCREENRC
1361 echo "stuff \"$2$NL\"" >> $SCREENRC
1362
1363 if [[ -n ${SCREEN_LOGDIR} ]]; then
1364 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1365 echo "log on" >>$SCREENRC
1366 fi
1367 fi
1368}
1369
1370# Stop a service in screen
1371# If a PID is available use it, kill the whole process group via TERM
1372# If screen is being used kill the screen window; this will catch processes
1373# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001374# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001375# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001376function screen_stop_service {
1377 local service=$1
1378
Dean Troyerdff49a22014-01-30 15:37:40 -06001379 SCREEN_NAME=${SCREEN_NAME:-stack}
1380 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1381 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1382
Dean Troyer3159a822014-08-27 14:13:58 -05001383 if is_service_enabled $service; then
1384 # Clean up the screen window
1385 screen -S $SCREEN_NAME -p $service -X kill
1386 fi
1387}
1388
1389# Stop a service process
1390# If a PID is available use it, kill the whole process group via TERM
1391# If screen is being used kill the screen window; this will catch processes
1392# that did not leave a PID behind
1393# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1394# stop_process service
1395function stop_process {
1396 local service=$1
1397
1398 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1399 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1400
1401 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001402 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001403 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1404 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1405 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001406 fi
1407 if [[ "$USE_SCREEN" = "True" ]]; then
1408 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001409 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001410 fi
1411 fi
1412}
1413
1414# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001415# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001416# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001417function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001418 local service
1419 local failures
1420 SCREEN_NAME=${SCREEN_NAME:-stack}
1421 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1422
1423
1424 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1425 echo "No service status directory found"
1426 return
1427 fi
1428
1429 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001430 # make this -o errexit safe
1431 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001432
1433 for service in $failures; do
1434 service=`basename $service`
1435 service=${service%.failure}
1436 echo "Error: Service $service is not running"
1437 done
1438
1439 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001440 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001441 fi
1442}
1443
Chris Dent2f27a0e2014-09-09 13:46:02 +01001444# Tail a log file in a screen if USE_SCREEN is true.
1445function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001446 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001447 local logfile=$2
1448
1449 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1450 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001451 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001452 fi
1453}
1454
Dean Troyerdff49a22014-01-30 15:37:40 -06001455
Dean Troyer3159a822014-08-27 14:13:58 -05001456# Deprecated Functions
1457# --------------------
1458
1459# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1460# fork. It includes the dirty work of closing extra filehandles and preparing log
1461# files to produce the same logs as screen_it(). The log filename is derived
1462# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1463# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1464# _old_run_process service "command-line"
1465function _old_run_process {
1466 local service=$1
1467 local command="$2"
1468
1469 # Undo logging redirections and close the extra descriptors
1470 exec 1>&3
1471 exec 2>&3
1472 exec 3>&-
1473 exec 6>&-
1474
1475 if [[ -n ${SCREEN_LOGDIR} ]]; then
1476 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1477 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1478
1479 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1480 export PYTHONUNBUFFERED=1
1481 fi
1482
1483 exec /bin/bash -c "$command"
1484 die "$service exec failure: $command"
1485}
1486
1487# old_run_process() launches a child process that closes all file descriptors and
1488# then exec's the passed in command. This is meant to duplicate the semantics
1489# of screen_it() without screen. PIDs are written to
1490# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1491# old_run_process service "command-line"
1492function old_run_process {
1493 local service=$1
1494 local command="$2"
1495
1496 # Spawn the child process
1497 _old_run_process "$service" "$command" &
1498 echo $!
1499}
1500
1501# Compatibility for existing start_XXXX() functions
1502# Uses global ``USE_SCREEN``
1503# screen_it service "command-line"
1504function screen_it {
1505 if is_service_enabled $1; then
1506 # Append the service to the screen rc file
1507 screen_rc "$1" "$2"
1508
1509 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001510 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001511 else
1512 # Spawn directly without screen
1513 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1514 fi
1515 fi
1516}
1517
1518# Compatibility for existing stop_XXXX() functions
1519# Stop a service in screen
1520# If a PID is available use it, kill the whole process group via TERM
1521# If screen is being used kill the screen window; this will catch processes
1522# that did not leave a PID behind
1523# screen_stop service
1524function screen_stop {
1525 # Clean up the screen window
1526 stop_process $1
1527}
1528
1529
Dean Troyerdff49a22014-01-30 15:37:40 -06001530# Python Functions
1531# ================
1532
1533# Get the path to the pip command.
1534# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001535function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001536 which pip || which pip-python
1537
1538 if [ $? -ne 0 ]; then
1539 die $LINENO "Unable to find pip; cannot continue"
1540 fi
1541}
1542
1543# Get the path to the direcotry where python executables are installed.
1544# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001545function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001546 if is_fedora || is_suse; then
1547 echo "/usr/bin"
1548 else
1549 echo "/usr/local/bin"
1550 fi
1551}
1552
1553# Wrapper for ``pip install`` to set cache and proxy environment variables
Attila Fazekasaf81d672014-11-10 09:04:54 +01001554# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``,
Dean Troyerdff49a22014-01-30 15:37:40 -06001555# ``TRACK_DEPENDS``, ``*_proxy``
1556# pip_install package [package ...]
1557function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001558 local xtrace=$(set +o | grep xtrace)
1559 set +o xtrace
1560 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1561 $xtrace
1562 return
1563 fi
1564
Dean Troyerdff49a22014-01-30 15:37:40 -06001565 if [[ -z "$os_PACKAGE" ]]; then
1566 GetOSVersion
1567 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001568 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1569 # TRACK_DEPENDS=True installation creates a circular dependency when
1570 # we attempt to install virtualenv into a virualenv, so we must global
1571 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001572 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001573 local cmd_pip=$DEST/.venv/bin/pip
1574 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001575 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001576 local cmd_pip=$(get_pip_command)
1577 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001578 fi
1579
Sean Dague45917cc2014-02-24 16:09:14 -05001580 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001581 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001582 http_proxy=$http_proxy \
1583 https_proxy=$https_proxy \
1584 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001585 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001586 $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001587
Flavio Percoco5a91c352014-10-31 18:48:00 +01001588 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001589 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1590 local test_req="$@/test-requirements.txt"
1591 if [[ -e "$test_req" ]]; then
1592 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1593 http_proxy=$http_proxy \
1594 https_proxy=$https_proxy \
1595 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001596 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001597 -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001598 fi
1599 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001600}
1601
Sean Daguecc524062014-10-01 09:06:43 -04001602# should we use this library from their git repo, or should we let it
1603# get pulled in via pip dependencies.
1604function use_library_from_git {
1605 local name=$1
1606 local enabled=1
1607 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1608 return $enabled
1609}
1610
1611# setup a library by name. If we are trying to use the library from
1612# git, we'll do a git based install, otherwise we'll punt and the
1613# library should be installed by a requirements pull from another
1614# project.
1615function setup_lib {
1616 local name=$1
1617 local dir=${GITDIR[$name]}
1618 setup_install $dir
1619}
1620
Sean Daguee08ab102014-11-13 17:09:28 -05001621# setup a library by name in editiable mode. If we are trying to use
1622# the library from git, we'll do a git based install, otherwise we'll
1623# punt and the library should be installed by a requirements pull from
1624# another project.
1625#
1626# use this for non namespaced libraries
1627function setup_dev_lib {
1628 local name=$1
1629 local dir=${GITDIR[$name]}
1630 setup_develop $dir
1631}
Sean Daguecc524062014-10-01 09:06:43 -04001632
Sean Dague099e5e32014-03-31 10:35:43 -04001633# this should be used if you want to install globally, all libraries should
1634# use this, especially *oslo* ones
1635function setup_install {
1636 local project_dir=$1
1637 setup_package_with_req_sync $project_dir
1638}
1639
1640# this should be used for projects which run services, like all services
1641function setup_develop {
1642 local project_dir=$1
1643 setup_package_with_req_sync $project_dir -e
1644}
1645
Sean Daguedef15342014-10-27 12:26:04 -04001646# determine if a project as specified by directory is in
1647# projects.txt. This will not be an exact match because we throw away
1648# the namespacing when we clone, but it should be good enough in all
1649# practical ways.
1650function is_in_projects_txt {
1651 local project_dir=$1
1652 local project_name=$(basename $project_dir)
1653 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1654}
1655
Dean Troyeraf616d92014-02-17 12:57:55 -06001656# ``pip install -e`` the package, which processes the dependencies
1657# using pip before running `setup.py develop`
1658#
1659# Updates the dependencies in project_dir from the
1660# openstack/requirements global list before installing anything.
1661#
1662# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1663# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001664function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001665 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001666 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001667
Dean Troyeraf616d92014-02-17 12:57:55 -06001668 # Don't update repo if local changes exist
1669 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001670 # ``errexit`` requires us to trap the exit code when the repo is changed
1671 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001672
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001673 if [[ $update_requirements != "changed" ]]; then
Sean Daguedef15342014-10-27 12:26:04 -04001674 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1675 if is_in_projects_txt $project_dir; then
1676 (cd $REQUIREMENTS_DIR; \
1677 python update.py $project_dir)
1678 else
1679 # soft update projects not found in requirements project.txt
1680 (cd $REQUIREMENTS_DIR; \
1681 python update.py -s $project_dir)
1682 fi
1683 else
1684 (cd $REQUIREMENTS_DIR; \
1685 python update.py $project_dir)
1686 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001687 fi
1688
Sean Dague099e5e32014-03-31 10:35:43 -04001689 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001690
1691 # We've just gone and possibly modified the user's source tree in an
1692 # automated way, which is considered bad form if it's a development
1693 # tree because we've screwed up their next git checkin. So undo it.
1694 #
1695 # However... there are some circumstances, like running in the gate
1696 # where we really really want the overridden version to stick. So provide
1697 # a variable that tells us whether or not we should UNDO the requirements
1698 # changes (this will be set to False in the OpenStack ci gate)
1699 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001700 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001701 (cd $project_dir && git reset --hard)
1702 fi
1703 fi
1704}
1705
1706# ``pip install -e`` the package, which processes the dependencies
1707# using pip before running `setup.py develop`
1708# Uses globals ``STACK_USER``
1709# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001710function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001711 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001712 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001713
Sean Dague099e5e32014-03-31 10:35:43 -04001714 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001715 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001716 if [[ "$flags" == "-e" ]]; then
1717 safe_chown -R $STACK_USER $1/*.egg-info
1718 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001719}
1720
Dean Troyerdff49a22014-01-30 15:37:40 -06001721
1722# Service Functions
1723# =================
1724
1725# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1726# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001727function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001728 echo "$1" | sed -e '
1729 s/,,/,/g;
1730 s/^,//;
1731 s/,$//
1732 '
1733}
1734
1735# disable_all_services() removes all current services
1736# from ``ENABLED_SERVICES`` to reset the configuration
1737# before a minimal installation
1738# Uses global ``ENABLED_SERVICES``
1739# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001740function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001741 ENABLED_SERVICES=""
1742}
1743
1744# Remove all services starting with '-'. For example, to install all default
1745# services except rabbit (rabbit) set in ``localrc``:
1746# ENABLED_SERVICES+=",-rabbit"
1747# Uses global ``ENABLED_SERVICES``
1748# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001749function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001750 local tmpsvcs="${ENABLED_SERVICES}"
1751 local service
1752 for service in ${tmpsvcs//,/ }; do
1753 if [[ ${service} == -* ]]; then
1754 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1755 fi
1756 done
1757 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1758}
1759
1760# disable_service() removes the services passed as argument to the
1761# ``ENABLED_SERVICES`` list, if they are present.
1762#
1763# For example:
1764# disable_service rabbit
1765#
1766# This function does not know about the special cases
1767# for nova, glance, and neutron built into is_service_enabled().
1768# Uses global ``ENABLED_SERVICES``
1769# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001770function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001771 local tmpsvcs=",${ENABLED_SERVICES},"
1772 local service
1773 for service in $@; do
1774 if is_service_enabled $service; then
1775 tmpsvcs=${tmpsvcs//,$service,/,}
1776 fi
1777 done
1778 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1779}
1780
1781# enable_service() adds the services passed as argument to the
1782# ``ENABLED_SERVICES`` list, if they are not already present.
1783#
1784# For example:
1785# enable_service qpid
1786#
1787# This function does not know about the special cases
1788# for nova, glance, and neutron built into is_service_enabled().
1789# Uses global ``ENABLED_SERVICES``
1790# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001791function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001792 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001793 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001794 for service in $@; do
1795 if ! is_service_enabled $service; then
1796 tmpsvcs+=",$service"
1797 fi
1798 done
1799 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1800 disable_negated_services
1801}
1802
1803# is_service_enabled() checks if the service(s) specified as arguments are
1804# enabled by the user in ``ENABLED_SERVICES``.
1805#
1806# Multiple services specified as arguments are ``OR``'ed together; the test
1807# is a short-circuit boolean, i.e it returns on the first match.
1808#
1809# There are special cases for some 'catch-all' services::
1810# **nova** returns true if any service enabled start with **n-**
1811# **cinder** returns true if any service enabled start with **c-**
1812# **ceilometer** returns true if any service enabled start with **ceilometer**
1813# **glance** returns true if any service enabled start with **g-**
1814# **neutron** returns true if any service enabled start with **q-**
1815# **swift** returns true if any service enabled start with **s-**
1816# **trove** returns true if any service enabled start with **tr-**
1817# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1818# **s-** services will be enabled. This will be deprecated in the future.
1819#
1820# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1821# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1822# as enabled in this case.
1823#
1824# Uses global ``ENABLED_SERVICES``
1825# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001826function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001827 local xtrace=$(set +o | grep xtrace)
1828 set +o xtrace
1829 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001830 local services=$@
1831 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001832 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001833 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001834
1835 # Look for top-level 'enabled' function for this service
1836 if type is_${service}_enabled >/dev/null 2>&1; then
1837 # A function exists for this service, use it
1838 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001839 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001840 fi
1841
1842 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1843 # are implemented
1844
Sean Dague45917cc2014-02-24 16:09:14 -05001845 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001846 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001847 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1848 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1849 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1850 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1851 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1852 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1853 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1854 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1855 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001856 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001857 done
Sean Dague45917cc2014-02-24 16:09:14 -05001858 $xtrace
1859 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001860}
1861
1862# Toggle enable/disable_service for services that must run exclusive of each other
1863# $1 The name of a variable containing a space-separated list of services
1864# $2 The name of a variable in which to store the enabled service's name
1865# $3 The name of the service to enable
1866function use_exclusive_service {
1867 local options=${!1}
1868 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001869 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001870 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001871 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001872 for opt in $options;do
1873 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1874 done
1875 eval "$out=$selection"
1876 return 0
1877}
1878
1879
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001880# System Functions
1881# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001882
1883# Only run the command if the target file (the last arg) is not on an
1884# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001885function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001886 local xtrace=$(set +o | grep xtrace)
1887 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001888 local args=( $@ )
1889 local last
1890 local sudo_cmd
1891 local dir_to_check
1892
1893 let last="${#args[*]} - 1"
1894
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001895 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001896 if [ ! -d "$dir_to_check" ]; then
1897 dir_to_check=`dirname "$dir_to_check"`
1898 fi
1899
1900 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001901 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001902 return 0
1903 fi
1904
1905 if [[ $TRACK_DEPENDS = True ]]; then
1906 sudo_cmd="env"
1907 else
1908 sudo_cmd="sudo"
1909 fi
1910
Sean Dague45917cc2014-02-24 16:09:14 -05001911 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001912 $sudo_cmd $@
1913}
1914
1915# Exit 0 if address is in network or 1 if address is not in network
1916# ip-range is in CIDR notation: 1.2.3.4/20
1917# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001918function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001919 local ip=$1
1920 local range=$2
1921 local masklen=${range#*/}
1922 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1923 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1924 [[ $network == $subnet ]]
1925}
1926
1927# Add a user to a group.
1928# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001929function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001930 local user=$1
1931 local group=$2
1932
1933 if [[ -z "$os_VENDOR" ]]; then
1934 GetOSVersion
1935 fi
1936
1937 # SLE11 and openSUSE 12.2 don't have the usual usermod
1938 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1939 sudo usermod -a -G "$group" "$user"
1940 else
1941 sudo usermod -A "$group" "$user"
1942 fi
1943}
1944
1945# Convert CIDR notation to a IPv4 netmask
1946# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001947function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001948 local maskpat="255 255 255 255"
1949 local maskdgt="254 252 248 240 224 192 128"
1950 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1951 echo ${1-0}.${2-0}.${3-0}.${4-0}
1952}
1953
1954# Gracefully cp only if source file/dir exists
1955# cp_it source destination
1956function cp_it {
1957 if [ -e $1 ] || [ -d $1 ]; then
1958 cp -pRL $1 $2
1959 fi
1960}
1961
1962# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1963# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1964# ``localrc`` or on the command line if necessary::
1965#
1966# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1967#
1968# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1969
Ian Wienandaee18c72014-02-21 15:35:08 +11001970function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001971 if [[ -n "$http_proxy" ]]; then
1972 export http_proxy=$http_proxy
1973 fi
1974 if [[ -n "$https_proxy" ]]; then
1975 export https_proxy=$https_proxy
1976 fi
1977 if [[ -n "$no_proxy" ]]; then
1978 export no_proxy=$no_proxy
1979 fi
1980}
1981
1982# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001983function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001984 local mount_type=`stat -f -L -c %T $1`
1985 test "$mount_type" == "nfs"
1986}
1987
1988# Return the network portion of the given IP address using netmask
1989# netmask is in the traditional dotted-quad format
1990# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001991function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001992 local ip=$1
1993 local mask=$2
1994 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1995 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1996 echo $subnet
1997}
1998
1999# Service wrapper to restart services
2000# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002001function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002002 if is_ubuntu; then
2003 sudo /usr/sbin/service $1 restart
2004 else
2005 sudo /sbin/service $1 restart
2006 fi
2007}
2008
2009# Only change permissions of a file or directory if it is not on an
2010# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002011function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002012 _safe_permission_operation chmod $@
2013}
2014
2015# Only change ownership of a file or directory if it is not on an NFS
2016# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002017function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002018 _safe_permission_operation chown $@
2019}
2020
2021# Service wrapper to start services
2022# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002023function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002024 if is_ubuntu; then
2025 sudo /usr/sbin/service $1 start
2026 else
2027 sudo /sbin/service $1 start
2028 fi
2029}
2030
2031# Service wrapper to stop services
2032# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002033function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002034 if is_ubuntu; then
2035 sudo /usr/sbin/service $1 stop
2036 else
2037 sudo /sbin/service $1 stop
2038 fi
2039}
2040
2041
2042# Restore xtrace
2043$XTRACE
2044
2045# Local variables:
2046# mode: shell-script
2047# End: