blob: 3b4309a15d8ad92718db0075e13f3963b7bbecef [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Dean Troyerdff49a22014-01-30 15:37:40 -06003# functions-common - Common functions used by DevStack components
4#
5# The canonical copy of this file is maintained in the DevStack repo.
6# All modifications should be made there and then sync'ed to other repos
7# as required.
8#
9# This file is sorted alphabetically within the function groups.
10#
11# - Config Functions
12# - Control Functions
13# - Distro Functions
14# - Git Functions
15# - OpenStack Functions
16# - Package Functions
17# - Process Functions
18# - Python Functions
19# - Service Functions
Masayuki Igawaf6368d32014-02-20 13:31:26 +090020# - System Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060021#
22# The following variables are assumed to be defined by certain functions:
23#
Jamie Lennox51f0de52014-10-20 16:32:34 +020024# - ``GIT_DEPTH``
Dean Troyerdff49a22014-01-30 15:37:40 -060025# - ``ENABLED_SERVICES``
26# - ``ERROR_ON_CLONE``
27# - ``FILES``
28# - ``OFFLINE``
29# - ``PIP_DOWNLOAD_CACHE``
Dean Troyerdff49a22014-01-30 15:37:40 -060030# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090031# - ``REQUIREMENTS_DIR``
32# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060033# - ``TRACK_DEPENDS``
Masayuki Igawad20f6322014-02-28 09:22:37 +090034# - ``UNDO_REQUIREMENTS``
Dean Troyerdff49a22014-01-30 15:37:40 -060035# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050036#
Dean Troyerdff49a22014-01-30 15:37:40 -060037
38# Save trace setting
39XTRACE=$(set +o | grep xtrace)
40set +o xtrace
41
Sean Daguecc524062014-10-01 09:06:43 -040042# Global Config Variables
43declare -A GITREPO
44declare -A GITBRANCH
45declare -A GITDIR
46
Dean Troyerdff49a22014-01-30 15:37:40 -060047# 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
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700150function inidelete {
151 local xtrace=$(set +o | grep xtrace)
152 set +o xtrace
153 local file=$1
154 local section=$2
155 local option=$3
156
157 [[ -z $section || -z $option ]] && return
158
159 # Remove old values
160 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
161
162 $xtrace
163}
164
Dean Troyerdff49a22014-01-30 15:37:40 -0600165# Set an option in an INI file
166# iniset config-file section option value
Ian Wienandaee18c72014-02-21 15:35:08 +1100167function iniset {
Sean Dague45917cc2014-02-24 16:09:14 -0500168 local xtrace=$(set +o | grep xtrace)
169 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600170 local file=$1
171 local section=$2
172 local option=$3
173 local value=$4
174
175 [[ -z $section || -z $option ]] && return
176
177 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
178 # Add section at the end
179 echo -e "\n[$section]" >>"$file"
180 fi
181 if ! ini_has_option "$file" "$section" "$option"; then
182 # Add it
183 sed -i -e "/^\[$section\]/ a\\
184$option = $value
185" "$file"
186 else
187 local sep=$(echo -ne "\x01")
188 # Replace it
189 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
190 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500191 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600192}
193
194# Set a multiple line option in an INI file
195# iniset_multiline config-file section option value1 value2 valu3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +1100196function iniset_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -0500197 local xtrace=$(set +o | grep xtrace)
198 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600199 local file=$1
200 local section=$2
201 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500202
Dean Troyerdff49a22014-01-30 15:37:40 -0600203 shift 3
204 local values
205 for v in $@; do
206 # The later sed command inserts each new value in the line next to
207 # the section identifier, which causes the values to be inserted in
208 # the reverse order. Do a reverse here to keep the original order.
209 values="$v ${values}"
210 done
211 if ! grep -q "^\[$section\]" "$file"; then
212 # Add section at the end
213 echo -e "\n[$section]" >>"$file"
214 else
215 # Remove old values
216 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
217 fi
218 # Add new ones
219 for v in $values; do
220 sed -i -e "/^\[$section\]/ a\\
221$option = $v
222" "$file"
223 done
Sean Dague45917cc2014-02-24 16:09:14 -0500224 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600225}
226
227# Uncomment an option in an INI file
228# iniuncomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100229function iniuncomment {
Sean Dague45917cc2014-02-24 16:09:14 -0500230 local xtrace=$(set +o | grep xtrace)
231 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600232 local file=$1
233 local section=$2
234 local option=$3
235 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500236 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600237}
238
239# Normalize config values to True or False
240# Accepts as False: 0 no No NO false False FALSE
241# Accepts as True: 1 yes Yes YES true True TRUE
242# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100243function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500244 local xtrace=$(set +o | grep xtrace)
245 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600246 local default=$1
247 local testval=$2
248
249 [[ -z "$testval" ]] && { echo "$default"; return; }
250 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
251 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
252 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500253 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600254}
255
256
257# Control Functions
258# =================
259
260# Prints backtrace info
261# filename:lineno:function
262# backtrace level
263function backtrace {
264 local level=$1
265 local deep=$((${#BASH_SOURCE[@]} - 1))
266 echo "[Call Trace]"
267 while [ $level -le $deep ]; do
268 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
269 deep=$((deep - 1))
270 done
271}
272
273# Prints line number and "message" then exits
274# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100275function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600276 local exitcode=$?
277 set +o xtrace
278 local line=$1; shift
279 if [ $exitcode == 0 ]; then
280 exitcode=1
281 fi
282 backtrace 2
283 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600284 # Give buffers a second to flush
285 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600286 exit $exitcode
287}
288
289# Checks an environment variable is not set or has length 0 OR if the
290# exit code is non-zero and prints "message" and exits
291# NOTE: env-var is the variable name without a '$'
292# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100293function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600294 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500295 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600296 set +o xtrace
297 local line=$1; shift
298 local evar=$1; shift
299 if ! is_set $evar || [ $exitcode != 0 ]; then
300 die $line "$*"
301 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500302 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600303}
304
305# Prints line number and "message" in error format
306# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100307function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600308 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500309 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600310 set +o xtrace
311 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
312 echo $msg 1>&2;
313 if [[ -n ${SCREEN_LOGDIR} ]]; then
314 echo $msg >> "${SCREEN_LOGDIR}/error.log"
315 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500316 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600317 return $exitcode
318}
319
320# Checks an environment variable is not set or has length 0 OR if the
321# exit code is non-zero and prints "message"
322# NOTE: env-var is the variable name without a '$'
323# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100324function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600325 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500326 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600327 set +o xtrace
328 local line=$1; shift
329 local evar=$1; shift
330 if ! is_set $evar || [ $exitcode != 0 ]; then
331 err $line "$*"
332 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500333 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600334 return $exitcode
335}
336
337# Exit after outputting a message about the distribution not being supported.
338# exit_distro_not_supported [optional-string-telling-what-is-missing]
339function exit_distro_not_supported {
340 if [[ -z "$DISTRO" ]]; then
341 GetDistro
342 fi
343
344 if [ $# -gt 0 ]; then
345 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
346 else
347 die $LINENO "Support for $DISTRO is incomplete."
348 fi
349}
350
351# Test if the named environment variable is set and not zero length
352# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100353function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600354 local var=\$"$1"
355 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
356}
357
358# Prints line number and "message" in warning format
359# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100360function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600361 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500362 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600363 set +o xtrace
364 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
365 echo $msg 1>&2;
366 if [[ -n ${SCREEN_LOGDIR} ]]; then
367 echo $msg >> "${SCREEN_LOGDIR}/error.log"
368 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500369 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600370 return $exitcode
371}
372
373
374# Distro Functions
375# ================
376
377# Determine OS Vendor, Release and Update
378# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
379# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500380# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
381# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
382# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
383# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
384# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
385declare os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
386
Dean Troyerdff49a22014-01-30 15:37:40 -0600387# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100388function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500389
Dean Troyerdff49a22014-01-30 15:37:40 -0600390 # Figure out which vendor we are
391 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
392 # OS/X
393 os_VENDOR=`sw_vers -productName`
394 os_RELEASE=`sw_vers -productVersion`
395 os_UPDATE=${os_RELEASE##*.}
396 os_RELEASE=${os_RELEASE%.*}
397 os_PACKAGE=""
398 if [[ "$os_RELEASE" =~ "10.7" ]]; then
399 os_CODENAME="lion"
400 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
401 os_CODENAME="snow leopard"
402 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
403 os_CODENAME="leopard"
404 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
405 os_CODENAME="tiger"
406 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
407 os_CODENAME="panther"
408 else
409 os_CODENAME=""
410 fi
411 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
412 os_VENDOR=$(lsb_release -i -s)
413 os_RELEASE=$(lsb_release -r -s)
414 os_UPDATE=""
415 os_PACKAGE="rpm"
416 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
417 os_PACKAGE="deb"
418 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
419 lsb_release -d -s | grep -q openSUSE
420 if [[ $? -eq 0 ]]; then
421 os_VENDOR="openSUSE"
422 fi
423 elif [[ $os_VENDOR == "openSUSE project" ]]; then
424 os_VENDOR="openSUSE"
425 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
426 os_VENDOR="Red Hat"
427 fi
428 os_CODENAME=$(lsb_release -c -s)
429 elif [[ -r /etc/redhat-release ]]; then
430 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
431 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
432 # CentOS release 5.5 (Final)
433 # CentOS Linux release 6.0 (Final)
434 # Fedora release 16 (Verne)
435 # XenServer release 6.2.0-70446c (xenenterprise)
436 os_CODENAME=""
437 for r in "Red Hat" CentOS Fedora XenServer; do
438 os_VENDOR=$r
439 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
440 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
441 os_CODENAME=${ver#*|}
442 os_RELEASE=${ver%|*}
443 os_UPDATE=${os_RELEASE##*.}
444 os_RELEASE=${os_RELEASE%.*}
445 break
446 fi
447 os_VENDOR=""
448 done
449 os_PACKAGE="rpm"
450 elif [[ -r /etc/SuSE-release ]]; then
451 for r in openSUSE "SUSE Linux"; do
452 if [[ "$r" = "SUSE Linux" ]]; then
453 os_VENDOR="SUSE LINUX"
454 else
455 os_VENDOR=$r
456 fi
457
458 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
459 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
460 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
461 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
462 break
463 fi
464 os_VENDOR=""
465 done
466 os_PACKAGE="rpm"
467 # If lsb_release is not installed, we should be able to detect Debian OS
468 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
469 os_VENDOR="Debian"
470 os_PACKAGE="deb"
471 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
472 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
473 fi
474 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
475}
476
477# Translate the OS version values into common nomenclature
478# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500479declare DISTRO
480
Ian Wienandaee18c72014-02-21 15:35:08 +1100481function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600482 GetOSVersion
483 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
484 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
485 DISTRO=$os_CODENAME
486 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
487 # For Fedora, just use 'f' and the release
488 DISTRO="f$os_RELEASE"
489 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
490 DISTRO="opensuse-$os_RELEASE"
491 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
492 # For SLE, also use the service pack
493 if [[ -z "$os_UPDATE" ]]; then
494 DISTRO="sle${os_RELEASE}"
495 else
496 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
497 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530498 elif [[ "$os_VENDOR" =~ (Red Hat) || \
499 "$os_VENDOR" =~ (CentOS) || \
500 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600501 # Drop the . release as we assume it's compatible
502 DISTRO="rhel${os_RELEASE::1}"
503 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
504 DISTRO="xs$os_RELEASE"
505 else
506 # Catch-all for now is Vendor + Release + Update
507 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
508 fi
509 export DISTRO
510}
511
512# Utility function for checking machine architecture
513# is_arch arch-type
514function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500515 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600516}
517
Ian Wienandbdc90c52014-08-04 15:44:58 +1000518# Quick check for a rackspace host; n.b. rackspace provided images
519# have these Xen tools installed but a custom image may not.
520function is_rackspace {
521 [ -f /usr/bin/xenstore-ls ] && \
522 sudo /usr/bin/xenstore-ls vm-data | grep -q "Rackspace"
523}
524
Dean Troyerdff49a22014-01-30 15:37:40 -0600525# Determine if current distribution is a Fedora-based distribution
526# (Fedora, RHEL, CentOS, etc).
527# is_fedora
528function is_fedora {
529 if [[ -z "$os_VENDOR" ]]; then
530 GetOSVersion
531 fi
532
anju Tiwari6c639c92014-07-15 18:11:54 +0530533 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
534 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600535}
536
537
538# Determine if current distribution is a SUSE-based distribution
539# (openSUSE, SLE).
540# is_suse
541function is_suse {
542 if [[ -z "$os_VENDOR" ]]; then
543 GetOSVersion
544 fi
545
546 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
547}
548
549
550# Determine if current distribution is an Ubuntu-based distribution
551# It will also detect non-Ubuntu but Debian-based distros
552# is_ubuntu
553function is_ubuntu {
554 if [[ -z "$os_PACKAGE" ]]; then
555 GetOSVersion
556 fi
557 [ "$os_PACKAGE" = "deb" ]
558}
559
560
561# Git Functions
562# =============
563
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600564# Returns openstack release name for a given branch name
565# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100566function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600567 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700568 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600569 echo ${branch#*/}
570 else
571 echo "master"
572 fi
573}
574
Dean Troyerdff49a22014-01-30 15:37:40 -0600575# git clone only if directory doesn't exist already. Since ``DEST`` might not
576# be owned by the installation user, we create the directory and change the
577# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500578# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
579# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600580# does not exist (default is False, meaning the repo will be cloned).
Jamie Lennox51f0de52014-10-20 16:32:34 +0200581# Set global ``GIT_DEPTH=<number>`` to limit the history depth of the git clone
582# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``, ``GIT_DEPTH``
Dean Troyerdff49a22014-01-30 15:37:40 -0600583# git_clone remote dest-dir branch
584function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500585 local git_remote=$1
586 local git_dest=$2
587 local git_ref=$3
588 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200589 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500590
Dean Troyerdff49a22014-01-30 15:37:40 -0600591 RECLONE=$(trueorfalse False $RECLONE)
592
YAMAMOTO Takashi292b2a72014-10-31 13:48:58 +0900593 if [[ -n "${GIT_DEPTH}" ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200594 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
595 fi
596
Dean Troyerdff49a22014-01-30 15:37:40 -0600597 if [[ "$OFFLINE" = "True" ]]; then
598 echo "Running in offline mode, clones already exist"
599 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500600 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600601 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400602 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600603 return
604 fi
605
Dean Troyer50cda692014-07-25 11:57:20 -0500606 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600607 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500608 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600609 [[ "$ERROR_ON_CLONE" = "True" ]] && \
610 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200611 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600612 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500613 cd $git_dest
614 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600615 else
616 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500617 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600618 [[ "$ERROR_ON_CLONE" = "True" ]] && \
619 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200620 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500621 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600622 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500623 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600624 elif [[ "$RECLONE" = "True" ]]; then
625 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500626 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600627 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500628 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100629 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600630 # remove the existing ignored files (like pyc) as they cause breakage
631 # (due to the py files having older timestamps than our pyc, so python
632 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500633 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600634
Dean Troyer50cda692014-07-25 11:57:20 -0500635 # handle git_ref accordingly to type (tag, branch)
636 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
637 git_update_tag $git_ref
638 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
639 git_update_branch $git_ref
640 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
641 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600642 else
Dean Troyer50cda692014-07-25 11:57:20 -0500643 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600644 fi
645
646 fi
647 fi
648
649 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500650 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600651 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400652 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600653}
654
Sean Daguecc524062014-10-01 09:06:43 -0400655# A variation on git clone that lets us specify a project by it's
656# actual name, like oslo.config. This is exceptionally useful in the
657# library installation case
658function git_clone_by_name {
659 local name=$1
660 local repo=${GITREPO[$name]}
661 local dir=${GITDIR[$name]}
662 local branch=${GITBRANCH[$name]}
663 git_clone $repo $dir $branch
664}
665
666
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100667# git can sometimes get itself infinitely stuck with transient network
668# errors or other issues with the remote end. This wraps git in a
669# timeout/retry loop and is intended to watch over non-local git
670# processes that might hang. GIT_TIMEOUT, if set, is passed directly
671# to timeout(1); otherwise the default value of 0 maintains the status
672# quo of waiting forever.
673# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100674function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100675 local count=0
676 local timeout=0
677
678 if [[ -n "${GIT_TIMEOUT}" ]]; then
679 timeout=${GIT_TIMEOUT}
680 fi
681
682 until timeout -s SIGINT ${timeout} git "$@"; do
683 # 124 is timeout(1)'s special return code when it reached the
684 # timeout; otherwise assume fatal failure
685 if [[ $? -ne 124 ]]; then
686 die $LINENO "git call failed: [git $@]"
687 fi
688
689 count=$(($count + 1))
690 warn "timeout ${count} for git call: [git $@]"
691 if [ $count -eq 3 ]; then
692 die $LINENO "Maximum of 3 git retries reached"
693 fi
694 sleep 5
695 done
696}
697
Dean Troyerdff49a22014-01-30 15:37:40 -0600698# git update using reference as a branch.
699# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100700function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500701 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600702
Dean Troyer50cda692014-07-25 11:57:20 -0500703 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600704 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500705 git branch -D $git_branch || true
706 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600707}
708
709# git update using reference as a branch.
710# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100711function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500712 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600713
Dean Troyer50cda692014-07-25 11:57:20 -0500714 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600715}
716
717# git update using reference as a tag. Be careful editing source at that repo
718# as working copy will be in a detached mode
719# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100720function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500721 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600722
Dean Troyer50cda692014-07-25 11:57:20 -0500723 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600724 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500725 git_timed fetch origin tag $git_tag
726 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600727}
728
729
730# OpenStack Functions
731# ===================
732
733# Get the default value for HOST_IP
734# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100735function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600736 local fixed_range=$1
737 local floating_range=$2
738 local host_ip_iface=$3
739 local host_ip=$4
740
741 # Find the interface used for the default route
742 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
743 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
744 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
745 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500746 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
747 local ip
748 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600749 # Attempt to filter out IP addresses that are part of the fixed and
750 # floating range. Note that this method only works if the ``netaddr``
751 # python library is installed. If it is not installed, an error
752 # will be printed and the first IP from the interface will be used.
753 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
754 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500755 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
756 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600757 break;
758 fi
759 done
760 fi
761 echo $host_ip
762}
763
Attila Fazekasf71b5002014-05-28 09:52:22 +0200764# Generates hex string from ``size`` byte of pseudo random data
765# generate_hex_string size
766function generate_hex_string {
767 local size=$1
768 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
769}
770
Dean Troyerdff49a22014-01-30 15:37:40 -0600771# Grab a numbered field from python prettytable output
772# Fields are numbered starting with 1
773# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
774# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100775function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500776 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600777 while read data; do
778 if [ "$1" -lt 0 ]; then
779 field="(\$(NF$1))"
780 else
781 field="\$$(($1 + 1))"
782 fi
783 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
784 done
785}
786
787# Add a policy to a policy.json file
788# Do nothing if the policy already exists
789# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100790function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600791 local policy_file=$1
792 local policy_name=$2
793 local policy_perm=$3
794
795 if grep -q ${policy_name} ${policy_file}; then
796 echo "Policy ${policy_name} already exists in ${policy_file}"
797 return
798 fi
799
800 # Add a terminating comma to policy lines without one
801 # Remove the closing '}' and all lines following to the end-of-file
802 local tmpfile=$(mktemp)
803 uniq ${policy_file} | sed -e '
804 s/]$/],/
805 /^[}]/,$d
806 ' > ${tmpfile}
807
808 # Append policy and closing brace
809 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
810 echo "}" >>${tmpfile}
811
812 mv ${tmpfile} ${policy_file}
813}
814
Alistair Coles24779f62014-10-15 18:57:59 +0100815# Gets or creates a domain
816# Usage: get_or_create_domain <name> <description>
817function get_or_create_domain {
818 local os_url="$KEYSTONE_SERVICE_URI/v3"
819 # Gets domain id
820 local domain_id=$(
821 # Gets domain id
822 openstack --os-token=$OS_TOKEN --os-url=$os_url \
823 --os-identity-api-version=3 domain show $1 \
824 -f value -c id 2>/dev/null ||
825 # Creates new domain
826 openstack --os-token=$OS_TOKEN --os-url=$os_url \
827 --os-identity-api-version=3 domain create $1 \
828 --description "$2" \
829 -f value -c id
830 )
831 echo $domain_id
832}
833
Bartosz Górski0abde392014-02-28 14:15:19 +0100834# Gets or creates user
Alistair Coles24779f62014-10-15 18:57:59 +0100835# Usage: get_or_create_user <username> <password> <project> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100836function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200837 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500838 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200839 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500840 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200841 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100842 local os_cmd="openstack"
843 local domain=""
844 if [[ ! -z "$5" ]]; then
845 domain="--domain=$5"
846 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
847 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100848 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500849 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500850 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100851 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100852 $1 \
853 --password "$2" \
854 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500855 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100856 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500857 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100858 -f value -c id
859 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500860 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100861}
862
863# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100864# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100865function get_or_create_project {
866 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100867 local os_cmd="openstack"
868 local domain=""
869 if [[ ! -z "$2" ]]; then
870 domain="--domain=$2"
871 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
872 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500873 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500874 # Creates new project with --or-show
875 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100876 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500877 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100878}
879
880# Gets or creates role
881# Usage: get_or_create_role <name>
882function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500883 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500884 # Creates role with --or-show
885 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100886 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500887 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100888}
889
890# Gets or adds user role
891# Usage: get_or_add_user_role <role> <user> <project>
892function get_or_add_user_role {
893 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500894 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100895 $2 \
896 --project $3 \
897 --column "ID" \
898 --column "Name" \
899 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500900 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100901 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500902 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100903 $1 \
904 --user $2 \
905 --project $3 \
906 | grep " id " | get_field 2)
907 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500908 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100909}
910
911# Gets or creates service
912# Usage: get_or_create_service <name> <type> <description>
913function get_or_create_service {
914 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500915 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100916 # Gets service id
917 openstack service show $1 -f value -c id 2>/dev/null ||
918 # Creates new service if not exists
919 openstack service create \
920 $1 \
921 --type=$2 \
922 --description="$3" \
923 -f value -c id
924 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500925 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100926}
927
928# Gets or creates endpoint
929# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
930function get_or_create_endpoint {
931 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500932 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100933 --column "ID" \
934 --column "Region" \
935 --column "Service Name" \
936 | grep " $2 " \
937 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500938 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100939 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500940 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100941 $1 \
942 --region $2 \
943 --publicurl $3 \
944 --adminurl $4 \
945 --internalurl $5 \
946 | grep " id " | get_field 2)
947 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500948 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100949}
Dean Troyerdff49a22014-01-30 15:37:40 -0600950
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500951
Dean Troyerdff49a22014-01-30 15:37:40 -0600952# Package Functions
953# =================
954
955# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100956function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600957 local pkg_dir
958 if is_ubuntu; then
Monty Taylor81a016d2014-11-15 17:18:13 -0300959 pkg_dir=$FILES/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600960 elif is_fedora; then
961 pkg_dir=$FILES/rpms
962 elif is_suse; then
963 pkg_dir=$FILES/rpms-suse
964 else
965 exit_distro_not_supported "list of packages"
966 fi
967 echo "$pkg_dir"
968}
969
970# Wrapper for ``apt-get`` to set cache and proxy environment variables
971# Uses globals ``OFFLINE``, ``*_proxy``
972# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100973function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500974 local xtrace=$(set +o | grep xtrace)
975 set +o xtrace
976
Dean Troyerdff49a22014-01-30 15:37:40 -0600977 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
978 local sudo="sudo"
979 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500980
981 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600982 $sudo DEBIAN_FRONTEND=noninteractive \
983 http_proxy=$http_proxy https_proxy=$https_proxy \
984 no_proxy=$no_proxy \
985 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
986}
987
988# get_packages() collects a list of package names of any type from the
Monty Taylor81a016d2014-11-15 17:18:13 -0300989# prerequisite files in ``files/{debs|rpms}``. The list is intended
Dean Troyerdff49a22014-01-30 15:37:40 -0600990# to be passed to a package installer such as apt or yum.
991#
992# Only packages required for the services in 1st argument will be
993# included. Two bits of metadata are recognized in the prerequisite files:
994#
995# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
996# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
997# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100998function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500999 local xtrace=$(set +o | grep xtrace)
1000 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001001 local services=$@
1002 local package_dir=$(_get_package_dir)
1003 local file_to_parse
1004 local service
1005
Flavio Percoco5a91c352014-10-31 18:48:00 +01001006 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
1007
Dean Troyerdff49a22014-01-30 15:37:40 -06001008 if [[ -z "$package_dir" ]]; then
1009 echo "No package directory supplied"
1010 return 1
1011 fi
1012 if [[ -z "$DISTRO" ]]; then
1013 GetDistro
1014 fi
1015 for service in ${services//,/ }; do
1016 # Allow individual services to specify dependencies
1017 if [[ -e ${package_dir}/${service} ]]; then
1018 file_to_parse="${file_to_parse} $service"
1019 fi
1020 # NOTE(sdague) n-api needs glance for now because that's where
1021 # glance client is
1022 if [[ $service == n-api ]]; then
1023 if [[ ! $file_to_parse =~ nova ]]; then
1024 file_to_parse="${file_to_parse} nova"
1025 fi
1026 if [[ ! $file_to_parse =~ glance ]]; then
1027 file_to_parse="${file_to_parse} glance"
1028 fi
1029 elif [[ $service == c-* ]]; then
1030 if [[ ! $file_to_parse =~ cinder ]]; then
1031 file_to_parse="${file_to_parse} cinder"
1032 fi
1033 elif [[ $service == ceilometer-* ]]; then
1034 if [[ ! $file_to_parse =~ ceilometer ]]; then
1035 file_to_parse="${file_to_parse} ceilometer"
1036 fi
1037 elif [[ $service == s-* ]]; then
1038 if [[ ! $file_to_parse =~ swift ]]; then
1039 file_to_parse="${file_to_parse} swift"
1040 fi
1041 elif [[ $service == n-* ]]; then
1042 if [[ ! $file_to_parse =~ nova ]]; then
1043 file_to_parse="${file_to_parse} nova"
1044 fi
1045 elif [[ $service == g-* ]]; then
1046 if [[ ! $file_to_parse =~ glance ]]; then
1047 file_to_parse="${file_to_parse} glance"
1048 fi
1049 elif [[ $service == key* ]]; then
1050 if [[ ! $file_to_parse =~ keystone ]]; then
1051 file_to_parse="${file_to_parse} keystone"
1052 fi
1053 elif [[ $service == q-* ]]; then
1054 if [[ ! $file_to_parse =~ neutron ]]; then
1055 file_to_parse="${file_to_parse} neutron"
1056 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001057 elif [[ $service == ir-* ]]; then
1058 if [[ ! $file_to_parse =~ ironic ]]; then
1059 file_to_parse="${file_to_parse} ironic"
1060 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001061 fi
1062 done
1063
1064 for file in ${file_to_parse}; do
1065 local fname=${package_dir}/${file}
1066 local OIFS line package distros distro
1067 [[ -e $fname ]] || continue
1068
1069 OIFS=$IFS
1070 IFS=$'\n'
1071 for line in $(<${fname}); do
1072 if [[ $line =~ "NOPRIME" ]]; then
1073 continue
1074 fi
1075
1076 # Assume we want this package
1077 package=${line%#*}
1078 inst_pkg=1
1079
1080 # Look for # dist:xxx in comment
1081 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1082 # We are using BASH regexp matching feature.
1083 package=${BASH_REMATCH[1]}
1084 distros=${BASH_REMATCH[2]}
1085 # In bash ${VAR,,} will lowecase VAR
1086 # Look for a match in the distro list
1087 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1088 # If no match then skip this package
1089 inst_pkg=0
1090 fi
1091 fi
1092
1093 # Look for # testonly in comment
1094 if [[ $line =~ (.*)#.*testonly.* ]]; then
1095 package=${BASH_REMATCH[1]}
1096 # Are we installing test packages? (test for the default value)
1097 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1098 # If not installing test packages the skip this package
1099 inst_pkg=0
1100 fi
1101 fi
1102
1103 if [[ $inst_pkg = 1 ]]; then
1104 echo $package
1105 fi
1106 done
1107 IFS=$OIFS
1108 done
Sean Dague45917cc2014-02-24 16:09:14 -05001109 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001110}
1111
1112# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001113# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001114# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001115function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001116 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001117 return 0
1118 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001119
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001120 if is_ubuntu; then
1121 local xtrace=$(set +o | grep xtrace)
1122 set +o xtrace
1123 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1124 # if there are transient errors pulling the updates, that's fine.
1125 # It may be secondary repositories that we don't really care about.
1126 apt_get update || /bin/true
1127 REPOS_UPDATED=True
1128 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001129 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001130 fi
1131}
1132
1133function real_install_package {
1134 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001135 apt_get install "$@"
1136 elif is_fedora; then
1137 yum_install "$@"
1138 elif is_suse; then
1139 zypper_install "$@"
1140 else
1141 exit_distro_not_supported "installing packages"
1142 fi
1143}
1144
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001145# Distro-agnostic package installer
1146# install_package package [package ...]
1147function install_package {
1148 update_package_repo
1149 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1150}
1151
Dean Troyerdff49a22014-01-30 15:37:40 -06001152# Distro-agnostic function to tell if a package is installed
1153# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001154function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001155 if [[ -z "$@" ]]; then
1156 return 1
1157 fi
1158
1159 if [[ -z "$os_PACKAGE" ]]; then
1160 GetOSVersion
1161 fi
1162
1163 if [[ "$os_PACKAGE" = "deb" ]]; then
1164 dpkg -s "$@" > /dev/null 2> /dev/null
1165 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1166 rpm --quiet -q "$@"
1167 else
1168 exit_distro_not_supported "finding if a package is installed"
1169 fi
1170}
1171
1172# Distro-agnostic package uninstaller
1173# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001174function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001175 if is_ubuntu; then
1176 apt_get purge "$@"
1177 elif is_fedora; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001178 sudo $YUM remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001179 elif is_suse; then
1180 sudo zypper rm "$@"
1181 else
1182 exit_distro_not_supported "uninstalling packages"
1183 fi
1184}
1185
1186# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001187# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001188# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001189function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001190 [[ "$OFFLINE" = "True" ]] && return
1191 local sudo="sudo"
1192 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001193
1194 # The manual check for missing packages is because yum -y assumes
1195 # missing packages are OK. See
1196 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001197 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1198 no_proxy=$no_proxy \
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001199 $YUM install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001200 awk '
1201 BEGIN { fail=0 }
1202 /No package/ { fail=1 }
1203 { print }
1204 END { exit fail }' || \
1205 die $LINENO "Missing packages detected"
1206
1207 # also ensure we catch a yum failure
1208 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001209 die $LINENO "$YUM install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001210 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001211}
1212
1213# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001214# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001215# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001216function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001217 [[ "$OFFLINE" = "True" ]] && return
1218 local sudo="sudo"
1219 [[ "$(id -u)" = "0" ]] && sudo="env"
1220 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1221 zypper --non-interactive install --auto-agree-with-licenses "$@"
1222}
1223
1224
1225# Process Functions
1226# =================
1227
1228# _run_process() is designed to be backgrounded by run_process() to simulate a
1229# fork. It includes the dirty work of closing extra filehandles and preparing log
1230# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001231# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001232# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001233# If an optional group is provided sg will be used to set the group of
1234# the command.
1235# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001236function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001237 local service=$1
1238 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001239 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001240
1241 # Undo logging redirections and close the extra descriptors
1242 exec 1>&3
1243 exec 2>&3
1244 exec 3>&-
1245 exec 6>&-
1246
1247 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001248 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1249 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001250
1251 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1252 export PYTHONUNBUFFERED=1
1253 fi
1254
Dean Troyer3159a822014-08-27 14:13:58 -05001255 # Run under ``setsid`` to force the process to become a session and group leader.
1256 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001257 if [[ -n "$group" ]]; then
1258 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1259 else
1260 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1261 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001262
1263 # Just silently exit this process
1264 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001265}
1266
1267# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1268# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001269# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001270# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001271function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001272 SCREEN_NAME=${SCREEN_NAME:-stack}
1273 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1274
1275 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1276 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1277 fi
1278
1279 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1280}
1281
1282# Find out if a process exists by partial name.
1283# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001284function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001285 local name=$1
1286 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001287 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001288 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001289 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001290}
1291
Dean Troyer3159a822014-08-27 14:13:58 -05001292# Run a single service under screen or directly
1293# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001294# If an optional group is provided sg will be used to run the
1295# command as that group.
1296# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001297function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001298 local service=$1
1299 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001300 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001301
Dean Troyer3159a822014-08-27 14:13:58 -05001302 if is_service_enabled $service; then
1303 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001304 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001305 else
1306 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001307 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001308 fi
1309 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001310}
1311
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001312# Helper to launch a process in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001313# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1314# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001315# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001316# Run a command in a shell in a screen window, if an optional group
1317# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001318function screen_process {
1319 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001320 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001321 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001322
Sean Dagueea22a4f2014-06-27 15:21:41 -04001323 SCREEN_NAME=${SCREEN_NAME:-stack}
1324 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1325 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001326
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001327 # Append the process to the screen rc file
1328 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001329
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001330 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001331
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001332 if [[ -n ${SCREEN_LOGDIR} ]]; then
1333 screen -S $SCREEN_NAME -p $name -X logfile ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log
1334 screen -S $SCREEN_NAME -p $name -X log on
1335 ln -sf ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${name}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001336 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001337
1338 # sleep to allow bash to be ready to be send the command - we are
1339 # creating a new window in screen and then sends characters, so if
1340 # bash isn't running by the time we send the command, nothing happens
1341 sleep 3
1342
1343 NL=`echo -ne '\015'`
1344 # This fun command does the following:
1345 # - the passed server command is backgrounded
1346 # - the pid of the background process is saved in the usual place
1347 # - the server process is brought back to the foreground
1348 # - if the server process exits prematurely the fg command errors
1349 # and a message is written to stdout and the process failure file
1350 #
1351 # The pid saved can be used in stop_process() as a process group
1352 # id to kill off all child processes
1353 if [[ -n "$group" ]]; then
1354 command="sg $group '$command'"
1355 fi
1356 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 -06001357}
1358
1359# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001360# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001361# screen_rc service "command-line"
1362function screen_rc {
1363 SCREEN_NAME=${SCREEN_NAME:-stack}
1364 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1365 if [[ ! -e $SCREENRC ]]; then
1366 # Name the screen session
1367 echo "sessionname $SCREEN_NAME" > $SCREENRC
1368 # Set a reasonable statusbar
1369 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1370 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1371 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1372 echo "screen -t shell bash" >> $SCREENRC
1373 fi
1374 # If this service doesn't already exist in the screenrc file
1375 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1376 NL=`echo -ne '\015'`
1377 echo "screen -t $1 bash" >> $SCREENRC
1378 echo "stuff \"$2$NL\"" >> $SCREENRC
1379
1380 if [[ -n ${SCREEN_LOGDIR} ]]; then
1381 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1382 echo "log on" >>$SCREENRC
1383 fi
1384 fi
1385}
1386
1387# Stop a service in screen
1388# If a PID is available use it, kill the whole process group via TERM
1389# If screen is being used kill the screen window; this will catch processes
1390# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001391# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001392# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001393function screen_stop_service {
1394 local service=$1
1395
Dean Troyerdff49a22014-01-30 15:37:40 -06001396 SCREEN_NAME=${SCREEN_NAME:-stack}
1397 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1398 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1399
Dean Troyer3159a822014-08-27 14:13:58 -05001400 if is_service_enabled $service; then
1401 # Clean up the screen window
1402 screen -S $SCREEN_NAME -p $service -X kill
1403 fi
1404}
1405
1406# Stop a service process
1407# If a PID is available use it, kill the whole process group via TERM
1408# If screen is being used kill the screen window; this will catch processes
1409# that did not leave a PID behind
1410# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1411# stop_process service
1412function stop_process {
1413 local service=$1
1414
1415 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1416 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1417
1418 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001419 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001420 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1421 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1422 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001423 fi
1424 if [[ "$USE_SCREEN" = "True" ]]; then
1425 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001426 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001427 fi
1428 fi
1429}
1430
1431# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001432# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001433# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001434function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001435 local service
1436 local failures
1437 SCREEN_NAME=${SCREEN_NAME:-stack}
1438 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1439
1440
1441 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1442 echo "No service status directory found"
1443 return
1444 fi
1445
1446 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001447 # make this -o errexit safe
1448 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001449
1450 for service in $failures; do
1451 service=`basename $service`
1452 service=${service%.failure}
1453 echo "Error: Service $service is not running"
1454 done
1455
1456 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001457 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001458 fi
1459}
1460
Chris Dent2f27a0e2014-09-09 13:46:02 +01001461# Tail a log file in a screen if USE_SCREEN is true.
1462function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001463 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001464 local logfile=$2
1465
1466 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1467 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001468 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001469 fi
1470}
1471
Dean Troyerdff49a22014-01-30 15:37:40 -06001472
Dean Troyer3159a822014-08-27 14:13:58 -05001473# Deprecated Functions
1474# --------------------
1475
1476# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1477# fork. It includes the dirty work of closing extra filehandles and preparing log
1478# files to produce the same logs as screen_it(). The log filename is derived
1479# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1480# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1481# _old_run_process service "command-line"
1482function _old_run_process {
1483 local service=$1
1484 local command="$2"
1485
1486 # Undo logging redirections and close the extra descriptors
1487 exec 1>&3
1488 exec 2>&3
1489 exec 3>&-
1490 exec 6>&-
1491
1492 if [[ -n ${SCREEN_LOGDIR} ]]; then
1493 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1494 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1495
1496 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1497 export PYTHONUNBUFFERED=1
1498 fi
1499
1500 exec /bin/bash -c "$command"
1501 die "$service exec failure: $command"
1502}
1503
1504# old_run_process() launches a child process that closes all file descriptors and
1505# then exec's the passed in command. This is meant to duplicate the semantics
1506# of screen_it() without screen. PIDs are written to
1507# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1508# old_run_process service "command-line"
1509function old_run_process {
1510 local service=$1
1511 local command="$2"
1512
1513 # Spawn the child process
1514 _old_run_process "$service" "$command" &
1515 echo $!
1516}
1517
1518# Compatibility for existing start_XXXX() functions
1519# Uses global ``USE_SCREEN``
1520# screen_it service "command-line"
1521function screen_it {
1522 if is_service_enabled $1; then
1523 # Append the service to the screen rc file
1524 screen_rc "$1" "$2"
1525
1526 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001527 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001528 else
1529 # Spawn directly without screen
1530 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1531 fi
1532 fi
1533}
1534
1535# Compatibility for existing stop_XXXX() functions
1536# Stop a service in screen
1537# If a PID is available use it, kill the whole process group via TERM
1538# If screen is being used kill the screen window; this will catch processes
1539# that did not leave a PID behind
1540# screen_stop service
1541function screen_stop {
1542 # Clean up the screen window
1543 stop_process $1
1544}
1545
1546
Dean Troyerdff49a22014-01-30 15:37:40 -06001547# Python Functions
1548# ================
1549
1550# Get the path to the pip command.
1551# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001552function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001553 which pip || which pip-python
1554
1555 if [ $? -ne 0 ]; then
1556 die $LINENO "Unable to find pip; cannot continue"
1557 fi
1558}
1559
1560# Get the path to the direcotry where python executables are installed.
1561# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001562function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001563 if is_fedora || is_suse; then
1564 echo "/usr/bin"
1565 else
1566 echo "/usr/local/bin"
1567 fi
1568}
1569
1570# Wrapper for ``pip install`` to set cache and proxy environment variables
Attila Fazekasaf81d672014-11-10 09:04:54 +01001571# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``,
Dean Troyerdff49a22014-01-30 15:37:40 -06001572# ``TRACK_DEPENDS``, ``*_proxy``
1573# pip_install package [package ...]
1574function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001575 local xtrace=$(set +o | grep xtrace)
1576 set +o xtrace
1577 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1578 $xtrace
1579 return
1580 fi
1581
Dean Troyerdff49a22014-01-30 15:37:40 -06001582 if [[ -z "$os_PACKAGE" ]]; then
1583 GetOSVersion
1584 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001585 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1586 # TRACK_DEPENDS=True installation creates a circular dependency when
1587 # we attempt to install virtualenv into a virualenv, so we must global
1588 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001589 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001590 local cmd_pip=$DEST/.venv/bin/pip
1591 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001592 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001593 local cmd_pip=$(get_pip_command)
Jeremy Stanley6ec66bb2014-12-22 17:17:51 +00001594 local sudo_pip="sudo -H"
Dean Troyerdff49a22014-01-30 15:37:40 -06001595 fi
1596
Sean Dague45917cc2014-02-24 16:09:14 -05001597 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001598 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001599 http_proxy=$http_proxy \
1600 https_proxy=$https_proxy \
1601 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001602 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001603 $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001604
Flavio Percoco5a91c352014-10-31 18:48:00 +01001605 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001606 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1607 local test_req="$@/test-requirements.txt"
1608 if [[ -e "$test_req" ]]; then
1609 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1610 http_proxy=$http_proxy \
1611 https_proxy=$https_proxy \
1612 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001613 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001614 -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001615 fi
1616 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001617}
1618
Sean Daguecc524062014-10-01 09:06:43 -04001619# should we use this library from their git repo, or should we let it
1620# get pulled in via pip dependencies.
1621function use_library_from_git {
1622 local name=$1
1623 local enabled=1
1624 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1625 return $enabled
1626}
1627
1628# setup a library by name. If we are trying to use the library from
1629# git, we'll do a git based install, otherwise we'll punt and the
1630# library should be installed by a requirements pull from another
1631# project.
1632function setup_lib {
1633 local name=$1
1634 local dir=${GITDIR[$name]}
1635 setup_install $dir
1636}
1637
Sean Daguee08ab102014-11-13 17:09:28 -05001638# setup a library by name in editiable mode. If we are trying to use
1639# the library from git, we'll do a git based install, otherwise we'll
1640# punt and the library should be installed by a requirements pull from
1641# another project.
1642#
1643# use this for non namespaced libraries
1644function setup_dev_lib {
1645 local name=$1
1646 local dir=${GITDIR[$name]}
1647 setup_develop $dir
1648}
Sean Daguecc524062014-10-01 09:06:43 -04001649
Sean Dague099e5e32014-03-31 10:35:43 -04001650# this should be used if you want to install globally, all libraries should
1651# use this, especially *oslo* ones
1652function setup_install {
1653 local project_dir=$1
1654 setup_package_with_req_sync $project_dir
1655}
1656
1657# this should be used for projects which run services, like all services
1658function setup_develop {
1659 local project_dir=$1
1660 setup_package_with_req_sync $project_dir -e
1661}
1662
Sean Daguedef15342014-10-27 12:26:04 -04001663# determine if a project as specified by directory is in
1664# projects.txt. This will not be an exact match because we throw away
1665# the namespacing when we clone, but it should be good enough in all
1666# practical ways.
1667function is_in_projects_txt {
1668 local project_dir=$1
1669 local project_name=$(basename $project_dir)
1670 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1671}
1672
Dean Troyeraf616d92014-02-17 12:57:55 -06001673# ``pip install -e`` the package, which processes the dependencies
1674# using pip before running `setup.py develop`
1675#
1676# Updates the dependencies in project_dir from the
1677# openstack/requirements global list before installing anything.
1678#
1679# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1680# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001681function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001682 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001683 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001684
Dean Troyeraf616d92014-02-17 12:57:55 -06001685 # Don't update repo if local changes exist
1686 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001687 # ``errexit`` requires us to trap the exit code when the repo is changed
1688 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001689
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001690 if [[ $update_requirements != "changed" ]]; then
Sean Daguedef15342014-10-27 12:26:04 -04001691 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1692 if is_in_projects_txt $project_dir; then
1693 (cd $REQUIREMENTS_DIR; \
1694 python update.py $project_dir)
1695 else
1696 # soft update projects not found in requirements project.txt
1697 (cd $REQUIREMENTS_DIR; \
1698 python update.py -s $project_dir)
1699 fi
1700 else
1701 (cd $REQUIREMENTS_DIR; \
1702 python update.py $project_dir)
1703 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001704 fi
1705
Sean Dague099e5e32014-03-31 10:35:43 -04001706 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001707
1708 # We've just gone and possibly modified the user's source tree in an
1709 # automated way, which is considered bad form if it's a development
1710 # tree because we've screwed up their next git checkin. So undo it.
1711 #
1712 # However... there are some circumstances, like running in the gate
1713 # where we really really want the overridden version to stick. So provide
1714 # a variable that tells us whether or not we should UNDO the requirements
1715 # changes (this will be set to False in the OpenStack ci gate)
1716 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001717 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001718 (cd $project_dir && git reset --hard)
1719 fi
1720 fi
1721}
1722
1723# ``pip install -e`` the package, which processes the dependencies
1724# using pip before running `setup.py develop`
1725# Uses globals ``STACK_USER``
1726# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001727function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001728 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001729 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001730
Sean Dague099e5e32014-03-31 10:35:43 -04001731 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001732 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001733 if [[ "$flags" == "-e" ]]; then
1734 safe_chown -R $STACK_USER $1/*.egg-info
1735 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001736}
1737
Sean Dague2c65e712014-12-18 09:44:56 -05001738# Plugin Functions
1739# =================
1740
1741DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1742
1743# enable_plugin <name> <url> [branch]
1744#
1745# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1746# ``url`` is a git url
1747# ``branch`` is a gitref. If it's not set, defaults to master
1748function enable_plugin {
1749 local name=$1
1750 local url=$2
1751 local branch=${3:-master}
1752 DEVSTACK_PLUGINS+=",$name"
1753 GITREPO[$name]=$url
1754 GITDIR[$name]=$DEST/$name
1755 GITBRANCH[$name]=$branch
1756}
1757
1758# fetch_plugins
1759#
1760# clones all plugins
1761function fetch_plugins {
1762 local plugins="${DEVSTACK_PLUGINS}"
1763 local plugin
1764
1765 # short circuit if nothing to do
1766 if [[ -z $plugins ]]; then
1767 return
1768 fi
1769
1770 echo "Fetching devstack plugins"
1771 for plugin in ${plugins//,/ }; do
1772 git_clone_by_name $plugin
1773 done
1774}
1775
1776# load_plugin_settings
1777#
1778# Load settings from plugins in the order that they were registered
1779function load_plugin_settings {
1780 local plugins="${DEVSTACK_PLUGINS}"
1781 local plugin
1782
1783 # short circuit if nothing to do
1784 if [[ -z $plugins ]]; then
1785 return
1786 fi
1787
1788 echo "Loading plugin settings"
1789 for plugin in ${plugins//,/ }; do
1790 local dir=${GITDIR[$plugin]}
1791 # source any known settings
1792 if [[ -f $dir/devstack/settings ]]; then
1793 source $dir/devstack/settings
1794 fi
1795 done
1796}
1797
1798# run_plugins
1799#
1800# Run the devstack/plugin.sh in all the plugin directories. These are
1801# run in registration order.
1802function run_plugins {
1803 local mode=$1
1804 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301805
1806 local plugins="${DEVSTACK_PLUGINS}"
1807 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001808 for plugin in ${plugins//,/ }; do
1809 local dir=${GITDIR[$plugin]}
1810 if [[ -f $dir/devstack/plugin.sh ]]; then
1811 source $dir/devstack/plugin.sh $mode $phase
1812 fi
1813 done
1814}
1815
1816function run_phase {
1817 local mode=$1
1818 local phase=$2
1819 if [[ -d $TOP_DIR/extras.d ]]; then
1820 for i in $TOP_DIR/extras.d/*.sh; do
1821 [[ -r $i ]] && source $i $mode $phase
1822 done
1823 fi
1824 # the source phase corresponds to settings loading in plugins
1825 if [[ "$mode" == "source" ]]; then
1826 load_plugin_settings
1827 else
1828 run_plugins $mode $phase
1829 fi
1830}
1831
Dean Troyerdff49a22014-01-30 15:37:40 -06001832
1833# Service Functions
1834# =================
1835
1836# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1837# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001838function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001839 echo "$1" | sed -e '
1840 s/,,/,/g;
1841 s/^,//;
1842 s/,$//
1843 '
1844}
1845
1846# disable_all_services() removes all current services
1847# from ``ENABLED_SERVICES`` to reset the configuration
1848# before a minimal installation
1849# Uses global ``ENABLED_SERVICES``
1850# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001851function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001852 ENABLED_SERVICES=""
1853}
1854
1855# Remove all services starting with '-'. For example, to install all default
1856# services except rabbit (rabbit) set in ``localrc``:
1857# ENABLED_SERVICES+=",-rabbit"
1858# Uses global ``ENABLED_SERVICES``
1859# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001860function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001861 local tmpsvcs="${ENABLED_SERVICES}"
1862 local service
1863 for service in ${tmpsvcs//,/ }; do
1864 if [[ ${service} == -* ]]; then
1865 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1866 fi
1867 done
1868 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1869}
1870
1871# disable_service() removes the services passed as argument to the
1872# ``ENABLED_SERVICES`` list, if they are present.
1873#
1874# For example:
1875# disable_service rabbit
1876#
1877# This function does not know about the special cases
1878# for nova, glance, and neutron built into is_service_enabled().
1879# Uses global ``ENABLED_SERVICES``
1880# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001881function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001882 local tmpsvcs=",${ENABLED_SERVICES},"
1883 local service
1884 for service in $@; do
1885 if is_service_enabled $service; then
1886 tmpsvcs=${tmpsvcs//,$service,/,}
1887 fi
1888 done
1889 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1890}
1891
1892# enable_service() adds the services passed as argument to the
1893# ``ENABLED_SERVICES`` list, if they are not already present.
1894#
1895# For example:
1896# enable_service qpid
1897#
1898# This function does not know about the special cases
1899# for nova, glance, and neutron built into is_service_enabled().
1900# Uses global ``ENABLED_SERVICES``
1901# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001902function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001903 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001904 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001905 for service in $@; do
1906 if ! is_service_enabled $service; then
1907 tmpsvcs+=",$service"
1908 fi
1909 done
1910 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1911 disable_negated_services
1912}
1913
1914# is_service_enabled() checks if the service(s) specified as arguments are
1915# enabled by the user in ``ENABLED_SERVICES``.
1916#
1917# Multiple services specified as arguments are ``OR``'ed together; the test
1918# is a short-circuit boolean, i.e it returns on the first match.
1919#
1920# There are special cases for some 'catch-all' services::
1921# **nova** returns true if any service enabled start with **n-**
1922# **cinder** returns true if any service enabled start with **c-**
1923# **ceilometer** returns true if any service enabled start with **ceilometer**
1924# **glance** returns true if any service enabled start with **g-**
1925# **neutron** returns true if any service enabled start with **q-**
1926# **swift** returns true if any service enabled start with **s-**
1927# **trove** returns true if any service enabled start with **tr-**
1928# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1929# **s-** services will be enabled. This will be deprecated in the future.
1930#
1931# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1932# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1933# as enabled in this case.
1934#
1935# Uses global ``ENABLED_SERVICES``
1936# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001937function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001938 local xtrace=$(set +o | grep xtrace)
1939 set +o xtrace
1940 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001941 local services=$@
1942 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001943 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001944 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001945
1946 # Look for top-level 'enabled' function for this service
1947 if type is_${service}_enabled >/dev/null 2>&1; then
1948 # A function exists for this service, use it
1949 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001950 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001951 fi
1952
1953 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1954 # are implemented
1955
Sean Dague45917cc2014-02-24 16:09:14 -05001956 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001957 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001958 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1959 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1960 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1961 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1962 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1963 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1964 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1965 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1966 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001967 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001968 done
Sean Dague45917cc2014-02-24 16:09:14 -05001969 $xtrace
1970 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001971}
1972
1973# Toggle enable/disable_service for services that must run exclusive of each other
1974# $1 The name of a variable containing a space-separated list of services
1975# $2 The name of a variable in which to store the enabled service's name
1976# $3 The name of the service to enable
1977function use_exclusive_service {
1978 local options=${!1}
1979 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001980 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001981 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001982 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001983 for opt in $options;do
1984 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1985 done
1986 eval "$out=$selection"
1987 return 0
1988}
1989
1990
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001991# System Functions
1992# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001993
1994# Only run the command if the target file (the last arg) is not on an
1995# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001996function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001997 local xtrace=$(set +o | grep xtrace)
1998 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001999 local args=( $@ )
2000 local last
2001 local sudo_cmd
2002 local dir_to_check
2003
2004 let last="${#args[*]} - 1"
2005
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002006 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002007 if [ ! -d "$dir_to_check" ]; then
2008 dir_to_check=`dirname "$dir_to_check"`
2009 fi
2010
2011 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002012 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002013 return 0
2014 fi
2015
2016 if [[ $TRACK_DEPENDS = True ]]; then
2017 sudo_cmd="env"
2018 else
2019 sudo_cmd="sudo"
2020 fi
2021
Sean Dague45917cc2014-02-24 16:09:14 -05002022 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002023 $sudo_cmd $@
2024}
2025
2026# Exit 0 if address is in network or 1 if address is not in network
2027# ip-range is in CIDR notation: 1.2.3.4/20
2028# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002029function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002030 local ip=$1
2031 local range=$2
2032 local masklen=${range#*/}
2033 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2034 local subnet=$(maskip $ip $(cidr2netmask $masklen))
2035 [[ $network == $subnet ]]
2036}
2037
2038# Add a user to a group.
2039# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002040function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002041 local user=$1
2042 local group=$2
2043
2044 if [[ -z "$os_VENDOR" ]]; then
2045 GetOSVersion
2046 fi
2047
2048 # SLE11 and openSUSE 12.2 don't have the usual usermod
2049 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
2050 sudo usermod -a -G "$group" "$user"
2051 else
2052 sudo usermod -A "$group" "$user"
2053 fi
2054}
2055
2056# Convert CIDR notation to a IPv4 netmask
2057# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002058function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002059 local maskpat="255 255 255 255"
2060 local maskdgt="254 252 248 240 224 192 128"
2061 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2062 echo ${1-0}.${2-0}.${3-0}.${4-0}
2063}
2064
2065# Gracefully cp only if source file/dir exists
2066# cp_it source destination
2067function cp_it {
2068 if [ -e $1 ] || [ -d $1 ]; then
2069 cp -pRL $1 $2
2070 fi
2071}
2072
2073# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2074# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2075# ``localrc`` or on the command line if necessary::
2076#
2077# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2078#
2079# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2080
Ian Wienandaee18c72014-02-21 15:35:08 +11002081function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06002082 if [[ -n "$http_proxy" ]]; then
2083 export http_proxy=$http_proxy
2084 fi
2085 if [[ -n "$https_proxy" ]]; then
2086 export https_proxy=$https_proxy
2087 fi
2088 if [[ -n "$no_proxy" ]]; then
2089 export no_proxy=$no_proxy
2090 fi
2091}
2092
2093# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002094function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06002095 local mount_type=`stat -f -L -c %T $1`
2096 test "$mount_type" == "nfs"
2097}
2098
2099# Return the network portion of the given IP address using netmask
2100# netmask is in the traditional dotted-quad format
2101# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002102function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002103 local ip=$1
2104 local mask=$2
2105 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2106 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2107 echo $subnet
2108}
2109
2110# Service wrapper to restart services
2111# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002112function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002113 if is_ubuntu; then
2114 sudo /usr/sbin/service $1 restart
2115 else
2116 sudo /sbin/service $1 restart
2117 fi
2118}
2119
2120# Only change permissions of a file or directory if it is not on an
2121# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002122function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002123 _safe_permission_operation chmod $@
2124}
2125
2126# Only change ownership of a file or directory if it is not on an NFS
2127# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002128function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002129 _safe_permission_operation chown $@
2130}
2131
2132# Service wrapper to start services
2133# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002134function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002135 if is_ubuntu; then
2136 sudo /usr/sbin/service $1 start
2137 else
2138 sudo /sbin/service $1 start
2139 fi
2140}
2141
2142# Service wrapper to stop services
2143# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002144function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002145 if is_ubuntu; then
2146 sudo /usr/sbin/service $1 stop
2147 else
2148 sudo /sbin/service $1 stop
2149 fi
2150}
2151
2152
2153# Restore xtrace
2154$XTRACE
2155
2156# Local variables:
2157# mode: shell-script
2158# End: