blob: b9eaae5db383007d7cae86a61f050b4b71c45ba2 [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``
28# - ``PIP_USE_MIRRORS``
29# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090030# - ``REQUIREMENTS_DIR``
31# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060032# - ``TRACK_DEPENDS``
Masayuki Igawad20f6322014-02-28 09:22:37 +090033# - ``UNDO_REQUIREMENTS``
Dean Troyerdff49a22014-01-30 15:37:40 -060034# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050035#
Dean Troyerdff49a22014-01-30 15:37:40 -060036
37# Save trace setting
38XTRACE=$(set +o | grep xtrace)
39set +o xtrace
40
Sean Daguecc524062014-10-01 09:06:43 -040041# Global Config Variables
42declare -A GITREPO
43declare -A GITBRANCH
44declare -A GITDIR
45
Dean Troyerdff49a22014-01-30 15:37:40 -060046
47# Config Functions
48# ================
49
50# Append a new option in an ini file without replacing the old value
51# iniadd config-file section option value1 value2 value3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +110052function iniadd {
Sean Dague45917cc2014-02-24 16:09:14 -050053 local xtrace=$(set +o | grep xtrace)
54 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060055 local file=$1
56 local section=$2
57 local option=$3
58 shift 3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050059
Dean Troyerdff49a22014-01-30 15:37:40 -060060 local values="$(iniget_multiline $file $section $option) $@"
61 iniset_multiline $file $section $option $values
Sean Dague45917cc2014-02-24 16:09:14 -050062 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060063}
64
65# Comment an option in an INI file
66# inicomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110067function inicomment {
Sean Dague45917cc2014-02-24 16:09:14 -050068 local xtrace=$(set +o | grep xtrace)
69 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060070 local file=$1
71 local section=$2
72 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050073
Dean Troyerdff49a22014-01-30 15:37:40 -060074 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -050075 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060076}
77
78# Get an option from an INI file
79# iniget config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110080function iniget {
Sean Dague45917cc2014-02-24 16:09:14 -050081 local xtrace=$(set +o | grep xtrace)
82 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060083 local file=$1
84 local section=$2
85 local option=$3
86 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050087
Dean Troyerdff49a22014-01-30 15:37:40 -060088 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
89 echo ${line#*=}
Sean Dague45917cc2014-02-24 16:09:14 -050090 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060091}
92
93# Get a multiple line option from an INI file
94# iniget_multiline config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110095function iniget_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -050096 local xtrace=$(set +o | grep xtrace)
97 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060098 local file=$1
99 local section=$2
100 local option=$3
101 local values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500102
Dean Troyerdff49a22014-01-30 15:37:40 -0600103 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
104 echo ${values}
Sean Dague45917cc2014-02-24 16:09:14 -0500105 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600106}
107
108# Determinate is the given option present in the INI file
109# ini_has_option config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100110function ini_has_option {
Sean Dague45917cc2014-02-24 16:09:14 -0500111 local xtrace=$(set +o | grep xtrace)
112 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600113 local file=$1
114 local section=$2
115 local option=$3
116 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500117
Dean Troyerdff49a22014-01-30 15:37:40 -0600118 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Sean Dague45917cc2014-02-24 16:09:14 -0500119 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600120 [ -n "$line" ]
121}
122
Robert Li751ad1a2014-10-15 21:40:53 -0400123# Add another config line for a multi-line option.
124# It's normally called after iniset of the same option and assumes
125# that the section already exists.
126#
127# Note that iniset_multiline requires all the 'lines' to be supplied
128# in the argument list. Doing that will cause incorrect configuration
129# if spaces are used in the config values.
130#
131# iniadd_literal config-file section option value
132function iniadd_literal {
133 local xtrace=$(set +o | grep xtrace)
134 set +o xtrace
135 local file=$1
136 local section=$2
137 local option=$3
138 local value=$4
139
140 [[ -z $section || -z $option ]] && return
141
142 # Add it
143 sed -i -e "/^\[$section\]/ a\\
144$option = $value
145" "$file"
146
147 $xtrace
148}
149
Dean Troyerdff49a22014-01-30 15:37:40 -0600150# Set an option in an INI file
151# iniset config-file section option value
Ian Wienandaee18c72014-02-21 15:35:08 +1100152function iniset {
Sean Dague45917cc2014-02-24 16:09:14 -0500153 local xtrace=$(set +o | grep xtrace)
154 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600155 local file=$1
156 local section=$2
157 local option=$3
158 local value=$4
159
160 [[ -z $section || -z $option ]] && return
161
162 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
163 # Add section at the end
164 echo -e "\n[$section]" >>"$file"
165 fi
166 if ! ini_has_option "$file" "$section" "$option"; then
167 # Add it
168 sed -i -e "/^\[$section\]/ a\\
169$option = $value
170" "$file"
171 else
172 local sep=$(echo -ne "\x01")
173 # Replace it
174 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
175 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500176 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600177}
178
179# Set a multiple line option in an INI file
180# iniset_multiline config-file section option value1 value2 valu3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +1100181function iniset_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -0500182 local xtrace=$(set +o | grep xtrace)
183 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600184 local file=$1
185 local section=$2
186 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500187
Dean Troyerdff49a22014-01-30 15:37:40 -0600188 shift 3
189 local values
190 for v in $@; do
191 # The later sed command inserts each new value in the line next to
192 # the section identifier, which causes the values to be inserted in
193 # the reverse order. Do a reverse here to keep the original order.
194 values="$v ${values}"
195 done
196 if ! grep -q "^\[$section\]" "$file"; then
197 # Add section at the end
198 echo -e "\n[$section]" >>"$file"
199 else
200 # Remove old values
201 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
202 fi
203 # Add new ones
204 for v in $values; do
205 sed -i -e "/^\[$section\]/ a\\
206$option = $v
207" "$file"
208 done
Sean Dague45917cc2014-02-24 16:09:14 -0500209 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600210}
211
212# Uncomment an option in an INI file
213# iniuncomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100214function iniuncomment {
Sean Dague45917cc2014-02-24 16:09:14 -0500215 local xtrace=$(set +o | grep xtrace)
216 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600217 local file=$1
218 local section=$2
219 local option=$3
220 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500221 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600222}
223
224# Normalize config values to True or False
225# Accepts as False: 0 no No NO false False FALSE
226# Accepts as True: 1 yes Yes YES true True TRUE
227# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100228function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500229 local xtrace=$(set +o | grep xtrace)
230 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600231 local default=$1
232 local testval=$2
233
234 [[ -z "$testval" ]] && { echo "$default"; return; }
235 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
236 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
237 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500238 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600239}
240
241
242# Control Functions
243# =================
244
245# Prints backtrace info
246# filename:lineno:function
247# backtrace level
248function backtrace {
249 local level=$1
250 local deep=$((${#BASH_SOURCE[@]} - 1))
251 echo "[Call Trace]"
252 while [ $level -le $deep ]; do
253 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
254 deep=$((deep - 1))
255 done
256}
257
258# Prints line number and "message" then exits
259# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100260function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600261 local exitcode=$?
262 set +o xtrace
263 local line=$1; shift
264 if [ $exitcode == 0 ]; then
265 exitcode=1
266 fi
267 backtrace 2
268 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600269 # Give buffers a second to flush
270 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600271 exit $exitcode
272}
273
274# Checks an environment variable is not set or has length 0 OR if the
275# exit code is non-zero and prints "message" and exits
276# NOTE: env-var is the variable name without a '$'
277# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100278function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600279 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500280 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600281 set +o xtrace
282 local line=$1; shift
283 local evar=$1; shift
284 if ! is_set $evar || [ $exitcode != 0 ]; then
285 die $line "$*"
286 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500287 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600288}
289
290# Prints line number and "message" in error format
291# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100292function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600293 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500294 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600295 set +o xtrace
296 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
297 echo $msg 1>&2;
298 if [[ -n ${SCREEN_LOGDIR} ]]; then
299 echo $msg >> "${SCREEN_LOGDIR}/error.log"
300 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500301 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600302 return $exitcode
303}
304
305# Checks an environment variable is not set or has length 0 OR if the
306# exit code is non-zero and prints "message"
307# NOTE: env-var is the variable name without a '$'
308# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100309function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600310 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500311 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600312 set +o xtrace
313 local line=$1; shift
314 local evar=$1; shift
315 if ! is_set $evar || [ $exitcode != 0 ]; then
316 err $line "$*"
317 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500318 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600319 return $exitcode
320}
321
322# Exit after outputting a message about the distribution not being supported.
323# exit_distro_not_supported [optional-string-telling-what-is-missing]
324function exit_distro_not_supported {
325 if [[ -z "$DISTRO" ]]; then
326 GetDistro
327 fi
328
329 if [ $# -gt 0 ]; then
330 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
331 else
332 die $LINENO "Support for $DISTRO is incomplete."
333 fi
334}
335
336# Test if the named environment variable is set and not zero length
337# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100338function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600339 local var=\$"$1"
340 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
341}
342
343# Prints line number and "message" in warning format
344# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100345function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600346 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500347 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600348 set +o xtrace
349 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
350 echo $msg 1>&2;
351 if [[ -n ${SCREEN_LOGDIR} ]]; then
352 echo $msg >> "${SCREEN_LOGDIR}/error.log"
353 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500354 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600355 return $exitcode
356}
357
358
359# Distro Functions
360# ================
361
362# Determine OS Vendor, Release and Update
363# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
364# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500365# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
366# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
367# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
368# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
369# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
370declare os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
371
Dean Troyerdff49a22014-01-30 15:37:40 -0600372# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100373function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500374
Dean Troyerdff49a22014-01-30 15:37:40 -0600375 # Figure out which vendor we are
376 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
377 # OS/X
378 os_VENDOR=`sw_vers -productName`
379 os_RELEASE=`sw_vers -productVersion`
380 os_UPDATE=${os_RELEASE##*.}
381 os_RELEASE=${os_RELEASE%.*}
382 os_PACKAGE=""
383 if [[ "$os_RELEASE" =~ "10.7" ]]; then
384 os_CODENAME="lion"
385 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
386 os_CODENAME="snow leopard"
387 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
388 os_CODENAME="leopard"
389 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
390 os_CODENAME="tiger"
391 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
392 os_CODENAME="panther"
393 else
394 os_CODENAME=""
395 fi
396 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
397 os_VENDOR=$(lsb_release -i -s)
398 os_RELEASE=$(lsb_release -r -s)
399 os_UPDATE=""
400 os_PACKAGE="rpm"
401 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
402 os_PACKAGE="deb"
403 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
404 lsb_release -d -s | grep -q openSUSE
405 if [[ $? -eq 0 ]]; then
406 os_VENDOR="openSUSE"
407 fi
408 elif [[ $os_VENDOR == "openSUSE project" ]]; then
409 os_VENDOR="openSUSE"
410 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
411 os_VENDOR="Red Hat"
412 fi
413 os_CODENAME=$(lsb_release -c -s)
414 elif [[ -r /etc/redhat-release ]]; then
415 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
416 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
417 # CentOS release 5.5 (Final)
418 # CentOS Linux release 6.0 (Final)
419 # Fedora release 16 (Verne)
420 # XenServer release 6.2.0-70446c (xenenterprise)
421 os_CODENAME=""
422 for r in "Red Hat" CentOS Fedora XenServer; do
423 os_VENDOR=$r
424 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
425 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
426 os_CODENAME=${ver#*|}
427 os_RELEASE=${ver%|*}
428 os_UPDATE=${os_RELEASE##*.}
429 os_RELEASE=${os_RELEASE%.*}
430 break
431 fi
432 os_VENDOR=""
433 done
434 os_PACKAGE="rpm"
435 elif [[ -r /etc/SuSE-release ]]; then
436 for r in openSUSE "SUSE Linux"; do
437 if [[ "$r" = "SUSE Linux" ]]; then
438 os_VENDOR="SUSE LINUX"
439 else
440 os_VENDOR=$r
441 fi
442
443 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
444 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
445 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
446 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
447 break
448 fi
449 os_VENDOR=""
450 done
451 os_PACKAGE="rpm"
452 # If lsb_release is not installed, we should be able to detect Debian OS
453 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
454 os_VENDOR="Debian"
455 os_PACKAGE="deb"
456 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
457 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
458 fi
459 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
460}
461
462# Translate the OS version values into common nomenclature
463# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500464declare DISTRO
465
Ian Wienandaee18c72014-02-21 15:35:08 +1100466function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600467 GetOSVersion
468 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
469 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
470 DISTRO=$os_CODENAME
471 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
472 # For Fedora, just use 'f' and the release
473 DISTRO="f$os_RELEASE"
474 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
475 DISTRO="opensuse-$os_RELEASE"
476 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
477 # For SLE, also use the service pack
478 if [[ -z "$os_UPDATE" ]]; then
479 DISTRO="sle${os_RELEASE}"
480 else
481 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
482 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530483 elif [[ "$os_VENDOR" =~ (Red Hat) || \
484 "$os_VENDOR" =~ (CentOS) || \
485 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600486 # Drop the . release as we assume it's compatible
487 DISTRO="rhel${os_RELEASE::1}"
488 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
489 DISTRO="xs$os_RELEASE"
490 else
491 # Catch-all for now is Vendor + Release + Update
492 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
493 fi
494 export DISTRO
495}
496
497# Utility function for checking machine architecture
498# is_arch arch-type
499function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500500 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600501}
502
Ian Wienandbdc90c52014-08-04 15:44:58 +1000503# Quick check for a rackspace host; n.b. rackspace provided images
504# have these Xen tools installed but a custom image may not.
505function is_rackspace {
506 [ -f /usr/bin/xenstore-ls ] && \
507 sudo /usr/bin/xenstore-ls vm-data | grep -q "Rackspace"
508}
509
Dean Troyerdff49a22014-01-30 15:37:40 -0600510# Determine if current distribution is a Fedora-based distribution
511# (Fedora, RHEL, CentOS, etc).
512# is_fedora
513function is_fedora {
514 if [[ -z "$os_VENDOR" ]]; then
515 GetOSVersion
516 fi
517
anju Tiwari6c639c92014-07-15 18:11:54 +0530518 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
519 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600520}
521
522
523# Determine if current distribution is a SUSE-based distribution
524# (openSUSE, SLE).
525# is_suse
526function is_suse {
527 if [[ -z "$os_VENDOR" ]]; then
528 GetOSVersion
529 fi
530
531 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
532}
533
534
535# Determine if current distribution is an Ubuntu-based distribution
536# It will also detect non-Ubuntu but Debian-based distros
537# is_ubuntu
538function is_ubuntu {
539 if [[ -z "$os_PACKAGE" ]]; then
540 GetOSVersion
541 fi
542 [ "$os_PACKAGE" = "deb" ]
543}
544
545
546# Git Functions
547# =============
548
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600549# Returns openstack release name for a given branch name
550# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100551function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600552 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700553 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600554 echo ${branch#*/}
555 else
556 echo "master"
557 fi
558}
559
Dean Troyerdff49a22014-01-30 15:37:40 -0600560# git clone only if directory doesn't exist already. Since ``DEST`` might not
561# be owned by the installation user, we create the directory and change the
562# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500563# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
564# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600565# does not exist (default is False, meaning the repo will be cloned).
Jamie Lennox51f0de52014-10-20 16:32:34 +0200566# Set global ``GIT_DEPTH=<number>`` to limit the history depth of the git clone
567# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``, ``GIT_DEPTH``
Dean Troyerdff49a22014-01-30 15:37:40 -0600568# git_clone remote dest-dir branch
569function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500570 local git_remote=$1
571 local git_dest=$2
572 local git_ref=$3
573 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200574 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500575
Dean Troyerdff49a22014-01-30 15:37:40 -0600576 RECLONE=$(trueorfalse False $RECLONE)
577
Jamie Lennox51f0de52014-10-20 16:32:34 +0200578 if [[ "$GIT_DEPTH" ]]; then
579 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
580 fi
581
Dean Troyerdff49a22014-01-30 15:37:40 -0600582 if [[ "$OFFLINE" = "True" ]]; then
583 echo "Running in offline mode, clones already exist"
584 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500585 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600586 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400587 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600588 return
589 fi
590
Dean Troyer50cda692014-07-25 11:57:20 -0500591 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600592 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500593 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600594 [[ "$ERROR_ON_CLONE" = "True" ]] && \
595 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200596 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600597 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500598 cd $git_dest
599 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600600 else
601 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500602 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600603 [[ "$ERROR_ON_CLONE" = "True" ]] && \
604 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200605 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500606 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600607 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500608 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600609 elif [[ "$RECLONE" = "True" ]]; then
610 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500611 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600612 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500613 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100614 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600615 # remove the existing ignored files (like pyc) as they cause breakage
616 # (due to the py files having older timestamps than our pyc, so python
617 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500618 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600619
Dean Troyer50cda692014-07-25 11:57:20 -0500620 # handle git_ref accordingly to type (tag, branch)
621 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
622 git_update_tag $git_ref
623 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
624 git_update_branch $git_ref
625 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
626 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600627 else
Dean Troyer50cda692014-07-25 11:57:20 -0500628 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600629 fi
630
631 fi
632 fi
633
634 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500635 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600636 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400637 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600638}
639
Sean Daguecc524062014-10-01 09:06:43 -0400640# A variation on git clone that lets us specify a project by it's
641# actual name, like oslo.config. This is exceptionally useful in the
642# library installation case
643function git_clone_by_name {
644 local name=$1
645 local repo=${GITREPO[$name]}
646 local dir=${GITDIR[$name]}
647 local branch=${GITBRANCH[$name]}
648 git_clone $repo $dir $branch
649}
650
651
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100652# git can sometimes get itself infinitely stuck with transient network
653# errors or other issues with the remote end. This wraps git in a
654# timeout/retry loop and is intended to watch over non-local git
655# processes that might hang. GIT_TIMEOUT, if set, is passed directly
656# to timeout(1); otherwise the default value of 0 maintains the status
657# quo of waiting forever.
658# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100659function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100660 local count=0
661 local timeout=0
662
663 if [[ -n "${GIT_TIMEOUT}" ]]; then
664 timeout=${GIT_TIMEOUT}
665 fi
666
667 until timeout -s SIGINT ${timeout} git "$@"; do
668 # 124 is timeout(1)'s special return code when it reached the
669 # timeout; otherwise assume fatal failure
670 if [[ $? -ne 124 ]]; then
671 die $LINENO "git call failed: [git $@]"
672 fi
673
674 count=$(($count + 1))
675 warn "timeout ${count} for git call: [git $@]"
676 if [ $count -eq 3 ]; then
677 die $LINENO "Maximum of 3 git retries reached"
678 fi
679 sleep 5
680 done
681}
682
Dean Troyerdff49a22014-01-30 15:37:40 -0600683# git update using reference as a branch.
684# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100685function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500686 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600687
Dean Troyer50cda692014-07-25 11:57:20 -0500688 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600689 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500690 git branch -D $git_branch || true
691 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600692}
693
694# git update using reference as a branch.
695# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100696function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500697 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600698
Dean Troyer50cda692014-07-25 11:57:20 -0500699 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600700}
701
702# git update using reference as a tag. Be careful editing source at that repo
703# as working copy will be in a detached mode
704# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100705function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500706 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600707
Dean Troyer50cda692014-07-25 11:57:20 -0500708 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600709 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500710 git_timed fetch origin tag $git_tag
711 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600712}
713
714
715# OpenStack Functions
716# ===================
717
718# Get the default value for HOST_IP
719# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100720function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600721 local fixed_range=$1
722 local floating_range=$2
723 local host_ip_iface=$3
724 local host_ip=$4
725
726 # Find the interface used for the default route
727 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
728 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
729 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
730 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500731 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
732 local ip
733 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600734 # Attempt to filter out IP addresses that are part of the fixed and
735 # floating range. Note that this method only works if the ``netaddr``
736 # python library is installed. If it is not installed, an error
737 # will be printed and the first IP from the interface will be used.
738 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
739 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500740 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
741 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600742 break;
743 fi
744 done
745 fi
746 echo $host_ip
747}
748
Attila Fazekasf71b5002014-05-28 09:52:22 +0200749# Generates hex string from ``size`` byte of pseudo random data
750# generate_hex_string size
751function generate_hex_string {
752 local size=$1
753 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
754}
755
Dean Troyerdff49a22014-01-30 15:37:40 -0600756# Grab a numbered field from python prettytable output
757# Fields are numbered starting with 1
758# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
759# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100760function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500761 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600762 while read data; do
763 if [ "$1" -lt 0 ]; then
764 field="(\$(NF$1))"
765 else
766 field="\$$(($1 + 1))"
767 fi
768 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
769 done
770}
771
772# Add a policy to a policy.json file
773# Do nothing if the policy already exists
774# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100775function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600776 local policy_file=$1
777 local policy_name=$2
778 local policy_perm=$3
779
780 if grep -q ${policy_name} ${policy_file}; then
781 echo "Policy ${policy_name} already exists in ${policy_file}"
782 return
783 fi
784
785 # Add a terminating comma to policy lines without one
786 # Remove the closing '}' and all lines following to the end-of-file
787 local tmpfile=$(mktemp)
788 uniq ${policy_file} | sed -e '
789 s/]$/],/
790 /^[}]/,$d
791 ' > ${tmpfile}
792
793 # Append policy and closing brace
794 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
795 echo "}" >>${tmpfile}
796
797 mv ${tmpfile} ${policy_file}
798}
799
Alistair Coles24779f62014-10-15 18:57:59 +0100800# Gets or creates a domain
801# Usage: get_or_create_domain <name> <description>
802function get_or_create_domain {
803 local os_url="$KEYSTONE_SERVICE_URI/v3"
804 # Gets domain id
805 local domain_id=$(
806 # Gets domain id
807 openstack --os-token=$OS_TOKEN --os-url=$os_url \
808 --os-identity-api-version=3 domain show $1 \
809 -f value -c id 2>/dev/null ||
810 # Creates new domain
811 openstack --os-token=$OS_TOKEN --os-url=$os_url \
812 --os-identity-api-version=3 domain create $1 \
813 --description "$2" \
814 -f value -c id
815 )
816 echo $domain_id
817}
818
Bartosz Górski0abde392014-02-28 14:15:19 +0100819# Gets or creates user
Alistair Coles24779f62014-10-15 18:57:59 +0100820# Usage: get_or_create_user <username> <password> <project> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100821function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200822 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500823 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200824 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500825 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200826 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100827 local os_cmd="openstack"
828 local domain=""
829 if [[ ! -z "$5" ]]; then
830 domain="--domain=$5"
831 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
832 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100833 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500834 local user_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100835 # Gets user id
Alistair Coles24779f62014-10-15 18:57:59 +0100836 $os_cmd user show $1 $domain -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100837 # Creates new user
Alistair Coles24779f62014-10-15 18:57:59 +0100838 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100839 $1 \
840 --password "$2" \
841 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500842 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100843 $domain \
Bartosz Górski0abde392014-02-28 14:15:19 +0100844 -f value -c id
845 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500846 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100847}
848
849# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100850# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100851function get_or_create_project {
852 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100853 local os_cmd="openstack"
854 local domain=""
855 if [[ ! -z "$2" ]]; then
856 domain="--domain=$2"
857 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
858 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500859 local project_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100860 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100861 $os_cmd project show $1 $domain -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100862 # Creates new project if not exists
Alistair Coles24779f62014-10-15 18:57:59 +0100863 $os_cmd project create $1 $domain -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100864 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500865 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100866}
867
868# Gets or creates role
869# Usage: get_or_create_role <name>
870function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500871 local role_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100872 # Gets role id
873 openstack role show $1 -f value -c id 2>/dev/null ||
874 # Creates role if not exists
875 openstack role create $1 -f value -c id
876 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500877 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100878}
879
880# Gets or adds user role
881# Usage: get_or_add_user_role <role> <user> <project>
882function get_or_add_user_role {
883 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500884 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100885 $2 \
886 --project $3 \
887 --column "ID" \
888 --column "Name" \
889 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500890 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100891 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500892 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100893 $1 \
894 --user $2 \
895 --project $3 \
896 | grep " id " | get_field 2)
897 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500898 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100899}
900
901# Gets or creates service
902# Usage: get_or_create_service <name> <type> <description>
903function get_or_create_service {
904 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500905 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100906 # Gets service id
907 openstack service show $1 -f value -c id 2>/dev/null ||
908 # Creates new service if not exists
909 openstack service create \
910 $1 \
911 --type=$2 \
912 --description="$3" \
913 -f value -c id
914 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500915 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100916}
917
918# Gets or creates endpoint
919# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
920function get_or_create_endpoint {
921 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500922 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100923 --column "ID" \
924 --column "Region" \
925 --column "Service Name" \
926 | grep " $2 " \
927 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500928 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100929 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500930 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100931 $1 \
932 --region $2 \
933 --publicurl $3 \
934 --adminurl $4 \
935 --internalurl $5 \
936 | grep " id " | get_field 2)
937 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500938 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100939}
Dean Troyerdff49a22014-01-30 15:37:40 -0600940
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500941
Dean Troyerdff49a22014-01-30 15:37:40 -0600942# Package Functions
943# =================
944
945# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100946function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600947 local pkg_dir
948 if is_ubuntu; then
949 pkg_dir=$FILES/apts
950 elif is_fedora; then
951 pkg_dir=$FILES/rpms
952 elif is_suse; then
953 pkg_dir=$FILES/rpms-suse
954 else
955 exit_distro_not_supported "list of packages"
956 fi
957 echo "$pkg_dir"
958}
959
960# Wrapper for ``apt-get`` to set cache and proxy environment variables
961# Uses globals ``OFFLINE``, ``*_proxy``
962# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100963function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500964 local xtrace=$(set +o | grep xtrace)
965 set +o xtrace
966
Dean Troyerdff49a22014-01-30 15:37:40 -0600967 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
968 local sudo="sudo"
969 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500970
971 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600972 $sudo DEBIAN_FRONTEND=noninteractive \
973 http_proxy=$http_proxy https_proxy=$https_proxy \
974 no_proxy=$no_proxy \
975 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
976}
977
978# get_packages() collects a list of package names of any type from the
979# prerequisite files in ``files/{apts|rpms}``. The list is intended
980# to be passed to a package installer such as apt or yum.
981#
982# Only packages required for the services in 1st argument will be
983# included. Two bits of metadata are recognized in the prerequisite files:
984#
985# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
986# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
987# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100988function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500989 local xtrace=$(set +o | grep xtrace)
990 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600991 local services=$@
992 local package_dir=$(_get_package_dir)
993 local file_to_parse
994 local service
995
Flavio Percoco5a91c352014-10-31 18:48:00 +0100996 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
997
Dean Troyerdff49a22014-01-30 15:37:40 -0600998 if [[ -z "$package_dir" ]]; then
999 echo "No package directory supplied"
1000 return 1
1001 fi
1002 if [[ -z "$DISTRO" ]]; then
1003 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -05001004 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -06001005 fi
1006 for service in ${services//,/ }; do
1007 # Allow individual services to specify dependencies
1008 if [[ -e ${package_dir}/${service} ]]; then
1009 file_to_parse="${file_to_parse} $service"
1010 fi
1011 # NOTE(sdague) n-api needs glance for now because that's where
1012 # glance client is
1013 if [[ $service == n-api ]]; then
1014 if [[ ! $file_to_parse =~ nova ]]; then
1015 file_to_parse="${file_to_parse} nova"
1016 fi
1017 if [[ ! $file_to_parse =~ glance ]]; then
1018 file_to_parse="${file_to_parse} glance"
1019 fi
1020 elif [[ $service == c-* ]]; then
1021 if [[ ! $file_to_parse =~ cinder ]]; then
1022 file_to_parse="${file_to_parse} cinder"
1023 fi
1024 elif [[ $service == ceilometer-* ]]; then
1025 if [[ ! $file_to_parse =~ ceilometer ]]; then
1026 file_to_parse="${file_to_parse} ceilometer"
1027 fi
1028 elif [[ $service == s-* ]]; then
1029 if [[ ! $file_to_parse =~ swift ]]; then
1030 file_to_parse="${file_to_parse} swift"
1031 fi
1032 elif [[ $service == n-* ]]; then
1033 if [[ ! $file_to_parse =~ nova ]]; then
1034 file_to_parse="${file_to_parse} nova"
1035 fi
1036 elif [[ $service == g-* ]]; then
1037 if [[ ! $file_to_parse =~ glance ]]; then
1038 file_to_parse="${file_to_parse} glance"
1039 fi
1040 elif [[ $service == key* ]]; then
1041 if [[ ! $file_to_parse =~ keystone ]]; then
1042 file_to_parse="${file_to_parse} keystone"
1043 fi
1044 elif [[ $service == q-* ]]; then
1045 if [[ ! $file_to_parse =~ neutron ]]; then
1046 file_to_parse="${file_to_parse} neutron"
1047 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001048 elif [[ $service == ir-* ]]; then
1049 if [[ ! $file_to_parse =~ ironic ]]; then
1050 file_to_parse="${file_to_parse} ironic"
1051 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001052 fi
1053 done
1054
1055 for file in ${file_to_parse}; do
1056 local fname=${package_dir}/${file}
1057 local OIFS line package distros distro
1058 [[ -e $fname ]] || continue
1059
1060 OIFS=$IFS
1061 IFS=$'\n'
1062 for line in $(<${fname}); do
1063 if [[ $line =~ "NOPRIME" ]]; then
1064 continue
1065 fi
1066
1067 # Assume we want this package
1068 package=${line%#*}
1069 inst_pkg=1
1070
1071 # Look for # dist:xxx in comment
1072 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1073 # We are using BASH regexp matching feature.
1074 package=${BASH_REMATCH[1]}
1075 distros=${BASH_REMATCH[2]}
1076 # In bash ${VAR,,} will lowecase VAR
1077 # Look for a match in the distro list
1078 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1079 # If no match then skip this package
1080 inst_pkg=0
1081 fi
1082 fi
1083
1084 # Look for # testonly in comment
1085 if [[ $line =~ (.*)#.*testonly.* ]]; then
1086 package=${BASH_REMATCH[1]}
1087 # Are we installing test packages? (test for the default value)
1088 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1089 # If not installing test packages the skip this package
1090 inst_pkg=0
1091 fi
1092 fi
1093
1094 if [[ $inst_pkg = 1 ]]; then
1095 echo $package
1096 fi
1097 done
1098 IFS=$OIFS
1099 done
Sean Dague45917cc2014-02-24 16:09:14 -05001100 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001101}
1102
1103# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001104# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001105# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001106function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001107 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001108 return 0
1109 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001110
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001111 if is_ubuntu; then
1112 local xtrace=$(set +o | grep xtrace)
1113 set +o xtrace
1114 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1115 # if there are transient errors pulling the updates, that's fine.
1116 # It may be secondary repositories that we don't really care about.
1117 apt_get update || /bin/true
1118 REPOS_UPDATED=True
1119 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001120 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001121 fi
1122}
1123
1124function real_install_package {
1125 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001126 apt_get install "$@"
1127 elif is_fedora; then
1128 yum_install "$@"
1129 elif is_suse; then
1130 zypper_install "$@"
1131 else
1132 exit_distro_not_supported "installing packages"
1133 fi
1134}
1135
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001136# Distro-agnostic package installer
1137# install_package package [package ...]
1138function install_package {
1139 update_package_repo
1140 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1141}
1142
Dean Troyerdff49a22014-01-30 15:37:40 -06001143# Distro-agnostic function to tell if a package is installed
1144# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001145function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001146 if [[ -z "$@" ]]; then
1147 return 1
1148 fi
1149
1150 if [[ -z "$os_PACKAGE" ]]; then
1151 GetOSVersion
1152 fi
1153
1154 if [[ "$os_PACKAGE" = "deb" ]]; then
1155 dpkg -s "$@" > /dev/null 2> /dev/null
1156 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1157 rpm --quiet -q "$@"
1158 else
1159 exit_distro_not_supported "finding if a package is installed"
1160 fi
1161}
1162
1163# Distro-agnostic package uninstaller
1164# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001165function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001166 if is_ubuntu; then
1167 apt_get purge "$@"
1168 elif is_fedora; then
1169 sudo yum remove -y "$@"
1170 elif is_suse; then
1171 sudo zypper rm "$@"
1172 else
1173 exit_distro_not_supported "uninstalling packages"
1174 fi
1175}
1176
1177# Wrapper for ``yum`` to set proxy environment variables
1178# Uses globals ``OFFLINE``, ``*_proxy``
1179# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001180function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001181 [[ "$OFFLINE" = "True" ]] && return
1182 local sudo="sudo"
1183 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001184
1185 # The manual check for missing packages is because yum -y assumes
1186 # missing packages are OK. See
1187 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001188 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1189 no_proxy=$no_proxy \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001190 yum install -y "$@" 2>&1 | \
1191 awk '
1192 BEGIN { fail=0 }
1193 /No package/ { fail=1 }
1194 { print }
1195 END { exit fail }' || \
1196 die $LINENO "Missing packages detected"
1197
1198 # also ensure we catch a yum failure
1199 if [[ ${PIPESTATUS[0]} != 0 ]]; then
1200 die $LINENO "Yum install failure"
1201 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001202}
1203
1204# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001205# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001206# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001207function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001208 [[ "$OFFLINE" = "True" ]] && return
1209 local sudo="sudo"
1210 [[ "$(id -u)" = "0" ]] && sudo="env"
1211 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1212 zypper --non-interactive install --auto-agree-with-licenses "$@"
1213}
1214
1215
1216# Process Functions
1217# =================
1218
1219# _run_process() is designed to be backgrounded by run_process() to simulate a
1220# fork. It includes the dirty work of closing extra filehandles and preparing log
1221# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001222# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001223# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001224# If an optional group is provided sg will be used to set the group of
1225# the command.
1226# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001227function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001228 local service=$1
1229 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001230 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001231
1232 # Undo logging redirections and close the extra descriptors
1233 exec 1>&3
1234 exec 2>&3
1235 exec 3>&-
1236 exec 6>&-
1237
1238 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001239 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1240 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001241
1242 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1243 export PYTHONUNBUFFERED=1
1244 fi
1245
Dean Troyer3159a822014-08-27 14:13:58 -05001246 # Run under ``setsid`` to force the process to become a session and group leader.
1247 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001248 if [[ -n "$group" ]]; then
1249 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1250 else
1251 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1252 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001253
1254 # Just silently exit this process
1255 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001256}
1257
1258# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1259# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001260# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001261# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001262function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001263 SCREEN_NAME=${SCREEN_NAME:-stack}
1264 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1265
1266 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1267 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1268 fi
1269
1270 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1271}
1272
1273# Find out if a process exists by partial name.
1274# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001275function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001276 local name=$1
1277 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001278 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001279 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001280 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001281}
1282
Dean Troyer3159a822014-08-27 14:13:58 -05001283# Run a single service under screen or directly
1284# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001285# If an optional group is provided sg will be used to run the
1286# command as that group.
1287# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001288function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001289 local service=$1
1290 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001291 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001292
Dean Troyer3159a822014-08-27 14:13:58 -05001293 if is_service_enabled $service; then
1294 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001295 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001296 else
1297 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001298 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001299 fi
1300 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001301}
1302
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001303# Helper to launch a process in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001304# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1305# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001306# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001307# Run a command in a shell in a screen window, if an optional group
1308# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001309function screen_process {
1310 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001311 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001312 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001313
Sean Dagueea22a4f2014-06-27 15:21:41 -04001314 SCREEN_NAME=${SCREEN_NAME:-stack}
1315 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1316 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001317
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001318 # Append the process to the screen rc file
1319 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001320
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001321 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001322
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001323 if [[ -n ${SCREEN_LOGDIR} ]]; then
1324 screen -S $SCREEN_NAME -p $name -X logfile ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log
1325 screen -S $SCREEN_NAME -p $name -X log on
1326 ln -sf ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${name}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001327 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001328
1329 # sleep to allow bash to be ready to be send the command - we are
1330 # creating a new window in screen and then sends characters, so if
1331 # bash isn't running by the time we send the command, nothing happens
1332 sleep 3
1333
1334 NL=`echo -ne '\015'`
1335 # This fun command does the following:
1336 # - the passed server command is backgrounded
1337 # - the pid of the background process is saved in the usual place
1338 # - the server process is brought back to the foreground
1339 # - if the server process exits prematurely the fg command errors
1340 # and a message is written to stdout and the process failure file
1341 #
1342 # The pid saved can be used in stop_process() as a process group
1343 # id to kill off all child processes
1344 if [[ -n "$group" ]]; then
1345 command="sg $group '$command'"
1346 fi
1347 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 -06001348}
1349
1350# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001351# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001352# screen_rc service "command-line"
1353function screen_rc {
1354 SCREEN_NAME=${SCREEN_NAME:-stack}
1355 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1356 if [[ ! -e $SCREENRC ]]; then
1357 # Name the screen session
1358 echo "sessionname $SCREEN_NAME" > $SCREENRC
1359 # Set a reasonable statusbar
1360 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1361 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1362 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1363 echo "screen -t shell bash" >> $SCREENRC
1364 fi
1365 # If this service doesn't already exist in the screenrc file
1366 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1367 NL=`echo -ne '\015'`
1368 echo "screen -t $1 bash" >> $SCREENRC
1369 echo "stuff \"$2$NL\"" >> $SCREENRC
1370
1371 if [[ -n ${SCREEN_LOGDIR} ]]; then
1372 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1373 echo "log on" >>$SCREENRC
1374 fi
1375 fi
1376}
1377
1378# Stop a service in screen
1379# If a PID is available use it, kill the whole process group via TERM
1380# If screen is being used kill the screen window; this will catch processes
1381# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001382# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001383# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001384function screen_stop_service {
1385 local service=$1
1386
Dean Troyerdff49a22014-01-30 15:37:40 -06001387 SCREEN_NAME=${SCREEN_NAME:-stack}
1388 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1389 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1390
Dean Troyer3159a822014-08-27 14:13:58 -05001391 if is_service_enabled $service; then
1392 # Clean up the screen window
1393 screen -S $SCREEN_NAME -p $service -X kill
1394 fi
1395}
1396
1397# Stop a service process
1398# If a PID is available use it, kill the whole process group via TERM
1399# If screen is being used kill the screen window; this will catch processes
1400# that did not leave a PID behind
1401# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1402# stop_process service
1403function stop_process {
1404 local service=$1
1405
1406 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1407 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1408
1409 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001410 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001411 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1412 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1413 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001414 fi
1415 if [[ "$USE_SCREEN" = "True" ]]; then
1416 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001417 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001418 fi
1419 fi
1420}
1421
1422# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001423# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001424# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001425function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001426 local service
1427 local failures
1428 SCREEN_NAME=${SCREEN_NAME:-stack}
1429 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1430
1431
1432 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1433 echo "No service status directory found"
1434 return
1435 fi
1436
1437 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001438 # make this -o errexit safe
1439 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001440
1441 for service in $failures; do
1442 service=`basename $service`
1443 service=${service%.failure}
1444 echo "Error: Service $service is not running"
1445 done
1446
1447 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001448 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001449 fi
1450}
1451
Chris Dent2f27a0e2014-09-09 13:46:02 +01001452# Tail a log file in a screen if USE_SCREEN is true.
1453function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001454 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001455 local logfile=$2
1456
1457 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1458 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001459 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001460 fi
1461}
1462
Dean Troyerdff49a22014-01-30 15:37:40 -06001463
Dean Troyer3159a822014-08-27 14:13:58 -05001464# Deprecated Functions
1465# --------------------
1466
1467# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1468# fork. It includes the dirty work of closing extra filehandles and preparing log
1469# files to produce the same logs as screen_it(). The log filename is derived
1470# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1471# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1472# _old_run_process service "command-line"
1473function _old_run_process {
1474 local service=$1
1475 local command="$2"
1476
1477 # Undo logging redirections and close the extra descriptors
1478 exec 1>&3
1479 exec 2>&3
1480 exec 3>&-
1481 exec 6>&-
1482
1483 if [[ -n ${SCREEN_LOGDIR} ]]; then
1484 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1485 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1486
1487 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1488 export PYTHONUNBUFFERED=1
1489 fi
1490
1491 exec /bin/bash -c "$command"
1492 die "$service exec failure: $command"
1493}
1494
1495# old_run_process() launches a child process that closes all file descriptors and
1496# then exec's the passed in command. This is meant to duplicate the semantics
1497# of screen_it() without screen. PIDs are written to
1498# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1499# old_run_process service "command-line"
1500function old_run_process {
1501 local service=$1
1502 local command="$2"
1503
1504 # Spawn the child process
1505 _old_run_process "$service" "$command" &
1506 echo $!
1507}
1508
1509# Compatibility for existing start_XXXX() functions
1510# Uses global ``USE_SCREEN``
1511# screen_it service "command-line"
1512function screen_it {
1513 if is_service_enabled $1; then
1514 # Append the service to the screen rc file
1515 screen_rc "$1" "$2"
1516
1517 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001518 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001519 else
1520 # Spawn directly without screen
1521 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1522 fi
1523 fi
1524}
1525
1526# Compatibility for existing stop_XXXX() functions
1527# Stop a service in screen
1528# If a PID is available use it, kill the whole process group via TERM
1529# If screen is being used kill the screen window; this will catch processes
1530# that did not leave a PID behind
1531# screen_stop service
1532function screen_stop {
1533 # Clean up the screen window
1534 stop_process $1
1535}
1536
1537
Dean Troyerdff49a22014-01-30 15:37:40 -06001538# Python Functions
1539# ================
1540
1541# Get the path to the pip command.
1542# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001543function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001544 which pip || which pip-python
1545
1546 if [ $? -ne 0 ]; then
1547 die $LINENO "Unable to find pip; cannot continue"
1548 fi
1549}
1550
1551# Get the path to the direcotry where python executables are installed.
1552# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001553function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001554 if is_fedora || is_suse; then
1555 echo "/usr/bin"
1556 else
1557 echo "/usr/local/bin"
1558 fi
1559}
1560
1561# Wrapper for ``pip install`` to set cache and proxy environment variables
1562# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
1563# ``TRACK_DEPENDS``, ``*_proxy``
1564# pip_install package [package ...]
1565function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001566 local xtrace=$(set +o | grep xtrace)
1567 set +o xtrace
1568 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1569 $xtrace
1570 return
1571 fi
1572
Dean Troyerdff49a22014-01-30 15:37:40 -06001573 if [[ -z "$os_PACKAGE" ]]; then
1574 GetOSVersion
1575 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001576 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1577 # TRACK_DEPENDS=True installation creates a circular dependency when
1578 # we attempt to install virtualenv into a virualenv, so we must global
1579 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001580 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001581 local cmd_pip=$DEST/.venv/bin/pip
1582 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001583 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001584 local cmd_pip=$(get_pip_command)
1585 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001586 fi
1587
1588 # Mirror option not needed anymore because pypi has CDN available,
1589 # but it's useful in certain circumstances
1590 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001591 local pip_mirror_opt=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001592 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001593 pip_mirror_opt="--use-mirrors"
Dean Troyerdff49a22014-01-30 15:37:40 -06001594 fi
1595
Sean Dague45917cc2014-02-24 16:09:14 -05001596 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001597 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001598 http_proxy=$http_proxy \
1599 https_proxy=$https_proxy \
1600 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001601 $cmd_pip install \
1602 $pip_mirror_opt $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001603
Flavio Percoco5a91c352014-10-31 18:48:00 +01001604 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001605 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1606 local test_req="$@/test-requirements.txt"
1607 if [[ -e "$test_req" ]]; then
1608 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1609 http_proxy=$http_proxy \
1610 https_proxy=$https_proxy \
1611 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001612 $cmd_pip install \
1613 $pip_mirror_opt -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001614 fi
1615 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001616}
1617
Sean Daguecc524062014-10-01 09:06:43 -04001618# should we use this library from their git repo, or should we let it
1619# get pulled in via pip dependencies.
1620function use_library_from_git {
1621 local name=$1
1622 local enabled=1
1623 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1624 return $enabled
1625}
1626
1627# setup a library by name. If we are trying to use the library from
1628# git, we'll do a git based install, otherwise we'll punt and the
1629# library should be installed by a requirements pull from another
1630# project.
1631function setup_lib {
1632 local name=$1
1633 local dir=${GITDIR[$name]}
1634 setup_install $dir
1635}
1636
1637
Sean Dague099e5e32014-03-31 10:35:43 -04001638# this should be used if you want to install globally, all libraries should
1639# use this, especially *oslo* ones
1640function setup_install {
1641 local project_dir=$1
1642 setup_package_with_req_sync $project_dir
1643}
1644
1645# this should be used for projects which run services, like all services
1646function setup_develop {
1647 local project_dir=$1
1648 setup_package_with_req_sync $project_dir -e
1649}
1650
Sean Daguedef15342014-10-27 12:26:04 -04001651# determine if a project as specified by directory is in
1652# projects.txt. This will not be an exact match because we throw away
1653# the namespacing when we clone, but it should be good enough in all
1654# practical ways.
1655function is_in_projects_txt {
1656 local project_dir=$1
1657 local project_name=$(basename $project_dir)
1658 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1659}
1660
Dean Troyeraf616d92014-02-17 12:57:55 -06001661# ``pip install -e`` the package, which processes the dependencies
1662# using pip before running `setup.py develop`
1663#
1664# Updates the dependencies in project_dir from the
1665# openstack/requirements global list before installing anything.
1666#
1667# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1668# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001669function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001670 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001671 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001672
Dean Troyeraf616d92014-02-17 12:57:55 -06001673 # Don't update repo if local changes exist
1674 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001675 # ``errexit`` requires us to trap the exit code when the repo is changed
1676 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001677
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001678 if [[ $update_requirements != "changed" ]]; then
Sean Daguedef15342014-10-27 12:26:04 -04001679 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1680 if is_in_projects_txt $project_dir; then
1681 (cd $REQUIREMENTS_DIR; \
1682 python update.py $project_dir)
1683 else
1684 # soft update projects not found in requirements project.txt
1685 (cd $REQUIREMENTS_DIR; \
1686 python update.py -s $project_dir)
1687 fi
1688 else
1689 (cd $REQUIREMENTS_DIR; \
1690 python update.py $project_dir)
1691 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001692 fi
1693
Sean Dague099e5e32014-03-31 10:35:43 -04001694 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001695
1696 # We've just gone and possibly modified the user's source tree in an
1697 # automated way, which is considered bad form if it's a development
1698 # tree because we've screwed up their next git checkin. So undo it.
1699 #
1700 # However... there are some circumstances, like running in the gate
1701 # where we really really want the overridden version to stick. So provide
1702 # a variable that tells us whether or not we should UNDO the requirements
1703 # changes (this will be set to False in the OpenStack ci gate)
1704 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001705 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001706 (cd $project_dir && git reset --hard)
1707 fi
1708 fi
1709}
1710
1711# ``pip install -e`` the package, which processes the dependencies
1712# using pip before running `setup.py develop`
1713# Uses globals ``STACK_USER``
1714# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001715function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001716 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001717 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001718
Sean Dague099e5e32014-03-31 10:35:43 -04001719 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001720 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001721 if [[ "$flags" == "-e" ]]; then
1722 safe_chown -R $STACK_USER $1/*.egg-info
1723 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001724}
1725
Dean Troyerdff49a22014-01-30 15:37:40 -06001726
1727# Service Functions
1728# =================
1729
1730# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1731# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001732function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001733 echo "$1" | sed -e '
1734 s/,,/,/g;
1735 s/^,//;
1736 s/,$//
1737 '
1738}
1739
1740# disable_all_services() removes all current services
1741# from ``ENABLED_SERVICES`` to reset the configuration
1742# before a minimal installation
1743# Uses global ``ENABLED_SERVICES``
1744# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001745function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001746 ENABLED_SERVICES=""
1747}
1748
1749# Remove all services starting with '-'. For example, to install all default
1750# services except rabbit (rabbit) set in ``localrc``:
1751# ENABLED_SERVICES+=",-rabbit"
1752# Uses global ``ENABLED_SERVICES``
1753# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001754function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001755 local tmpsvcs="${ENABLED_SERVICES}"
1756 local service
1757 for service in ${tmpsvcs//,/ }; do
1758 if [[ ${service} == -* ]]; then
1759 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1760 fi
1761 done
1762 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1763}
1764
1765# disable_service() removes the services passed as argument to the
1766# ``ENABLED_SERVICES`` list, if they are present.
1767#
1768# For example:
1769# disable_service rabbit
1770#
1771# This function does not know about the special cases
1772# for nova, glance, and neutron built into is_service_enabled().
1773# Uses global ``ENABLED_SERVICES``
1774# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001775function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001776 local tmpsvcs=",${ENABLED_SERVICES},"
1777 local service
1778 for service in $@; do
1779 if is_service_enabled $service; then
1780 tmpsvcs=${tmpsvcs//,$service,/,}
1781 fi
1782 done
1783 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1784}
1785
1786# enable_service() adds the services passed as argument to the
1787# ``ENABLED_SERVICES`` list, if they are not already present.
1788#
1789# For example:
1790# enable_service qpid
1791#
1792# This function does not know about the special cases
1793# for nova, glance, and neutron built into is_service_enabled().
1794# Uses global ``ENABLED_SERVICES``
1795# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001796function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001797 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001798 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001799 for service in $@; do
1800 if ! is_service_enabled $service; then
1801 tmpsvcs+=",$service"
1802 fi
1803 done
1804 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1805 disable_negated_services
1806}
1807
1808# is_service_enabled() checks if the service(s) specified as arguments are
1809# enabled by the user in ``ENABLED_SERVICES``.
1810#
1811# Multiple services specified as arguments are ``OR``'ed together; the test
1812# is a short-circuit boolean, i.e it returns on the first match.
1813#
1814# There are special cases for some 'catch-all' services::
1815# **nova** returns true if any service enabled start with **n-**
1816# **cinder** returns true if any service enabled start with **c-**
1817# **ceilometer** returns true if any service enabled start with **ceilometer**
1818# **glance** returns true if any service enabled start with **g-**
1819# **neutron** returns true if any service enabled start with **q-**
1820# **swift** returns true if any service enabled start with **s-**
1821# **trove** returns true if any service enabled start with **tr-**
1822# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1823# **s-** services will be enabled. This will be deprecated in the future.
1824#
1825# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1826# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1827# as enabled in this case.
1828#
1829# Uses global ``ENABLED_SERVICES``
1830# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001831function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001832 local xtrace=$(set +o | grep xtrace)
1833 set +o xtrace
1834 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001835 local services=$@
1836 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001837 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001838 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001839
1840 # Look for top-level 'enabled' function for this service
1841 if type is_${service}_enabled >/dev/null 2>&1; then
1842 # A function exists for this service, use it
1843 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001844 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001845 fi
1846
1847 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1848 # are implemented
1849
Sean Dague45917cc2014-02-24 16:09:14 -05001850 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001851 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001852 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1853 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1854 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1855 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1856 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1857 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1858 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1859 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1860 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001861 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001862 done
Sean Dague45917cc2014-02-24 16:09:14 -05001863 $xtrace
1864 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001865}
1866
1867# Toggle enable/disable_service for services that must run exclusive of each other
1868# $1 The name of a variable containing a space-separated list of services
1869# $2 The name of a variable in which to store the enabled service's name
1870# $3 The name of the service to enable
1871function use_exclusive_service {
1872 local options=${!1}
1873 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001874 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001875 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001876 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001877 for opt in $options;do
1878 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1879 done
1880 eval "$out=$selection"
1881 return 0
1882}
1883
1884
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001885# System Functions
1886# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001887
1888# Only run the command if the target file (the last arg) is not on an
1889# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001890function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001891 local xtrace=$(set +o | grep xtrace)
1892 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001893 local args=( $@ )
1894 local last
1895 local sudo_cmd
1896 local dir_to_check
1897
1898 let last="${#args[*]} - 1"
1899
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001900 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001901 if [ ! -d "$dir_to_check" ]; then
1902 dir_to_check=`dirname "$dir_to_check"`
1903 fi
1904
1905 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001906 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001907 return 0
1908 fi
1909
1910 if [[ $TRACK_DEPENDS = True ]]; then
1911 sudo_cmd="env"
1912 else
1913 sudo_cmd="sudo"
1914 fi
1915
Sean Dague45917cc2014-02-24 16:09:14 -05001916 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001917 $sudo_cmd $@
1918}
1919
1920# Exit 0 if address is in network or 1 if address is not in network
1921# ip-range is in CIDR notation: 1.2.3.4/20
1922# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001923function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001924 local ip=$1
1925 local range=$2
1926 local masklen=${range#*/}
1927 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1928 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1929 [[ $network == $subnet ]]
1930}
1931
1932# Add a user to a group.
1933# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001934function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001935 local user=$1
1936 local group=$2
1937
1938 if [[ -z "$os_VENDOR" ]]; then
1939 GetOSVersion
1940 fi
1941
1942 # SLE11 and openSUSE 12.2 don't have the usual usermod
1943 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1944 sudo usermod -a -G "$group" "$user"
1945 else
1946 sudo usermod -A "$group" "$user"
1947 fi
1948}
1949
1950# Convert CIDR notation to a IPv4 netmask
1951# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001952function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001953 local maskpat="255 255 255 255"
1954 local maskdgt="254 252 248 240 224 192 128"
1955 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1956 echo ${1-0}.${2-0}.${3-0}.${4-0}
1957}
1958
1959# Gracefully cp only if source file/dir exists
1960# cp_it source destination
1961function cp_it {
1962 if [ -e $1 ] || [ -d $1 ]; then
1963 cp -pRL $1 $2
1964 fi
1965}
1966
1967# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1968# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1969# ``localrc`` or on the command line if necessary::
1970#
1971# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1972#
1973# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1974
Ian Wienandaee18c72014-02-21 15:35:08 +11001975function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001976 if [[ -n "$http_proxy" ]]; then
1977 export http_proxy=$http_proxy
1978 fi
1979 if [[ -n "$https_proxy" ]]; then
1980 export https_proxy=$https_proxy
1981 fi
1982 if [[ -n "$no_proxy" ]]; then
1983 export no_proxy=$no_proxy
1984 fi
1985}
1986
1987# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001988function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001989 local mount_type=`stat -f -L -c %T $1`
1990 test "$mount_type" == "nfs"
1991}
1992
1993# Return the network portion of the given IP address using netmask
1994# netmask is in the traditional dotted-quad format
1995# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001996function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001997 local ip=$1
1998 local mask=$2
1999 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2000 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2001 echo $subnet
2002}
2003
2004# Service wrapper to restart services
2005# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002006function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002007 if is_ubuntu; then
2008 sudo /usr/sbin/service $1 restart
2009 else
2010 sudo /sbin/service $1 restart
2011 fi
2012}
2013
2014# Only change permissions of a file or directory if it is not on an
2015# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002016function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002017 _safe_permission_operation chmod $@
2018}
2019
2020# Only change ownership of a file or directory if it is not on an NFS
2021# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002022function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002023 _safe_permission_operation chown $@
2024}
2025
2026# Service wrapper to start services
2027# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002028function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002029 if is_ubuntu; then
2030 sudo /usr/sbin/service $1 start
2031 else
2032 sudo /sbin/service $1 start
2033 fi
2034}
2035
2036# Service wrapper to stop services
2037# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002038function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002039 if is_ubuntu; then
2040 sudo /usr/sbin/service $1 stop
2041 else
2042 sudo /sbin/service $1 stop
2043 fi
2044}
2045
2046
2047# Restore xtrace
2048$XTRACE
2049
2050# Local variables:
2051# mode: shell-script
2052# End: