blob: 7a6c3a127c1787587f7ddd3de93feca0141eec9e [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
Sean Dague45917cc2014-02-24 16:09:14 -05001014 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -06001015 fi
1016 for service in ${services//,/ }; do
1017 # Allow individual services to specify dependencies
1018 if [[ -e ${package_dir}/${service} ]]; then
1019 file_to_parse="${file_to_parse} $service"
1020 fi
1021 # NOTE(sdague) n-api needs glance for now because that's where
1022 # glance client is
1023 if [[ $service == n-api ]]; then
1024 if [[ ! $file_to_parse =~ nova ]]; then
1025 file_to_parse="${file_to_parse} nova"
1026 fi
1027 if [[ ! $file_to_parse =~ glance ]]; then
1028 file_to_parse="${file_to_parse} glance"
1029 fi
1030 elif [[ $service == c-* ]]; then
1031 if [[ ! $file_to_parse =~ cinder ]]; then
1032 file_to_parse="${file_to_parse} cinder"
1033 fi
1034 elif [[ $service == ceilometer-* ]]; then
1035 if [[ ! $file_to_parse =~ ceilometer ]]; then
1036 file_to_parse="${file_to_parse} ceilometer"
1037 fi
1038 elif [[ $service == s-* ]]; then
1039 if [[ ! $file_to_parse =~ swift ]]; then
1040 file_to_parse="${file_to_parse} swift"
1041 fi
1042 elif [[ $service == n-* ]]; then
1043 if [[ ! $file_to_parse =~ nova ]]; then
1044 file_to_parse="${file_to_parse} nova"
1045 fi
1046 elif [[ $service == g-* ]]; then
1047 if [[ ! $file_to_parse =~ glance ]]; then
1048 file_to_parse="${file_to_parse} glance"
1049 fi
1050 elif [[ $service == key* ]]; then
1051 if [[ ! $file_to_parse =~ keystone ]]; then
1052 file_to_parse="${file_to_parse} keystone"
1053 fi
1054 elif [[ $service == q-* ]]; then
1055 if [[ ! $file_to_parse =~ neutron ]]; then
1056 file_to_parse="${file_to_parse} neutron"
1057 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001058 elif [[ $service == ir-* ]]; then
1059 if [[ ! $file_to_parse =~ ironic ]]; then
1060 file_to_parse="${file_to_parse} ironic"
1061 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001062 fi
1063 done
1064
1065 for file in ${file_to_parse}; do
1066 local fname=${package_dir}/${file}
1067 local OIFS line package distros distro
1068 [[ -e $fname ]] || continue
1069
1070 OIFS=$IFS
1071 IFS=$'\n'
1072 for line in $(<${fname}); do
1073 if [[ $line =~ "NOPRIME" ]]; then
1074 continue
1075 fi
1076
1077 # Assume we want this package
1078 package=${line%#*}
1079 inst_pkg=1
1080
1081 # Look for # dist:xxx in comment
1082 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1083 # We are using BASH regexp matching feature.
1084 package=${BASH_REMATCH[1]}
1085 distros=${BASH_REMATCH[2]}
1086 # In bash ${VAR,,} will lowecase VAR
1087 # Look for a match in the distro list
1088 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1089 # If no match then skip this package
1090 inst_pkg=0
1091 fi
1092 fi
1093
1094 # Look for # testonly in comment
1095 if [[ $line =~ (.*)#.*testonly.* ]]; then
1096 package=${BASH_REMATCH[1]}
1097 # Are we installing test packages? (test for the default value)
1098 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1099 # If not installing test packages the skip this package
1100 inst_pkg=0
1101 fi
1102 fi
1103
1104 if [[ $inst_pkg = 1 ]]; then
1105 echo $package
1106 fi
1107 done
1108 IFS=$OIFS
1109 done
Sean Dague45917cc2014-02-24 16:09:14 -05001110 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001111}
1112
1113# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001114# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001115# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001116function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001117 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001118 return 0
1119 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001120
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001121 if is_ubuntu; then
1122 local xtrace=$(set +o | grep xtrace)
1123 set +o xtrace
1124 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1125 # if there are transient errors pulling the updates, that's fine.
1126 # It may be secondary repositories that we don't really care about.
1127 apt_get update || /bin/true
1128 REPOS_UPDATED=True
1129 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001130 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001131 fi
1132}
1133
1134function real_install_package {
1135 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001136 apt_get install "$@"
1137 elif is_fedora; then
1138 yum_install "$@"
1139 elif is_suse; then
1140 zypper_install "$@"
1141 else
1142 exit_distro_not_supported "installing packages"
1143 fi
1144}
1145
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001146# Distro-agnostic package installer
1147# install_package package [package ...]
1148function install_package {
1149 update_package_repo
1150 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1151}
1152
Dean Troyerdff49a22014-01-30 15:37:40 -06001153# Distro-agnostic function to tell if a package is installed
1154# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001155function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001156 if [[ -z "$@" ]]; then
1157 return 1
1158 fi
1159
1160 if [[ -z "$os_PACKAGE" ]]; then
1161 GetOSVersion
1162 fi
1163
1164 if [[ "$os_PACKAGE" = "deb" ]]; then
1165 dpkg -s "$@" > /dev/null 2> /dev/null
1166 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1167 rpm --quiet -q "$@"
1168 else
1169 exit_distro_not_supported "finding if a package is installed"
1170 fi
1171}
1172
1173# Distro-agnostic package uninstaller
1174# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001175function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001176 if is_ubuntu; then
1177 apt_get purge "$@"
1178 elif is_fedora; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001179 sudo $YUM remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001180 elif is_suse; then
1181 sudo zypper rm "$@"
1182 else
1183 exit_distro_not_supported "uninstalling packages"
1184 fi
1185}
1186
1187# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001188# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001189# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001190function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001191 [[ "$OFFLINE" = "True" ]] && return
1192 local sudo="sudo"
1193 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001194
1195 # The manual check for missing packages is because yum -y assumes
1196 # missing packages are OK. See
1197 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001198 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1199 no_proxy=$no_proxy \
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001200 $YUM install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001201 awk '
1202 BEGIN { fail=0 }
1203 /No package/ { fail=1 }
1204 { print }
1205 END { exit fail }' || \
1206 die $LINENO "Missing packages detected"
1207
1208 # also ensure we catch a yum failure
1209 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001210 die $LINENO "$YUM install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001211 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001212}
1213
1214# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001215# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001216# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001217function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001218 [[ "$OFFLINE" = "True" ]] && return
1219 local sudo="sudo"
1220 [[ "$(id -u)" = "0" ]] && sudo="env"
1221 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1222 zypper --non-interactive install --auto-agree-with-licenses "$@"
1223}
1224
1225
1226# Process Functions
1227# =================
1228
1229# _run_process() is designed to be backgrounded by run_process() to simulate a
1230# fork. It includes the dirty work of closing extra filehandles and preparing log
1231# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001232# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001233# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001234# If an optional group is provided sg will be used to set the group of
1235# the command.
1236# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001237function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001238 local service=$1
1239 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001240 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001241
1242 # Undo logging redirections and close the extra descriptors
1243 exec 1>&3
1244 exec 2>&3
1245 exec 3>&-
1246 exec 6>&-
1247
1248 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001249 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1250 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001251
1252 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1253 export PYTHONUNBUFFERED=1
1254 fi
1255
Dean Troyer3159a822014-08-27 14:13:58 -05001256 # Run under ``setsid`` to force the process to become a session and group leader.
1257 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001258 if [[ -n "$group" ]]; then
1259 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1260 else
1261 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1262 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001263
1264 # Just silently exit this process
1265 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001266}
1267
1268# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1269# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001270# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001271# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001272function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001273 SCREEN_NAME=${SCREEN_NAME:-stack}
1274 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1275
1276 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1277 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1278 fi
1279
1280 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1281}
1282
1283# Find out if a process exists by partial name.
1284# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001285function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001286 local name=$1
1287 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001288 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001289 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001290 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001291}
1292
Dean Troyer3159a822014-08-27 14:13:58 -05001293# Run a single service under screen or directly
1294# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001295# If an optional group is provided sg will be used to run the
1296# command as that group.
1297# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001298function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001299 local service=$1
1300 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001301 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001302
Dean Troyer3159a822014-08-27 14:13:58 -05001303 if is_service_enabled $service; then
1304 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001305 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001306 else
1307 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001308 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001309 fi
1310 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001311}
1312
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001313# Helper to launch a process in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001314# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1315# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001316# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001317# Run a command in a shell in a screen window, if an optional group
1318# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001319function screen_process {
1320 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001321 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001322 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001323
Sean Dagueea22a4f2014-06-27 15:21:41 -04001324 SCREEN_NAME=${SCREEN_NAME:-stack}
1325 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1326 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001327
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001328 # Append the process to the screen rc file
1329 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001330
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001331 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001332
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001333 if [[ -n ${SCREEN_LOGDIR} ]]; then
1334 screen -S $SCREEN_NAME -p $name -X logfile ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log
1335 screen -S $SCREEN_NAME -p $name -X log on
1336 ln -sf ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${name}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001337 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001338
1339 # sleep to allow bash to be ready to be send the command - we are
1340 # creating a new window in screen and then sends characters, so if
1341 # bash isn't running by the time we send the command, nothing happens
1342 sleep 3
1343
1344 NL=`echo -ne '\015'`
1345 # This fun command does the following:
1346 # - the passed server command is backgrounded
1347 # - the pid of the background process is saved in the usual place
1348 # - the server process is brought back to the foreground
1349 # - if the server process exits prematurely the fg command errors
1350 # and a message is written to stdout and the process failure file
1351 #
1352 # The pid saved can be used in stop_process() as a process group
1353 # id to kill off all child processes
1354 if [[ -n "$group" ]]; then
1355 command="sg $group '$command'"
1356 fi
1357 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 -06001358}
1359
1360# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001361# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001362# screen_rc service "command-line"
1363function screen_rc {
1364 SCREEN_NAME=${SCREEN_NAME:-stack}
1365 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1366 if [[ ! -e $SCREENRC ]]; then
1367 # Name the screen session
1368 echo "sessionname $SCREEN_NAME" > $SCREENRC
1369 # Set a reasonable statusbar
1370 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1371 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1372 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1373 echo "screen -t shell bash" >> $SCREENRC
1374 fi
1375 # If this service doesn't already exist in the screenrc file
1376 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1377 NL=`echo -ne '\015'`
1378 echo "screen -t $1 bash" >> $SCREENRC
1379 echo "stuff \"$2$NL\"" >> $SCREENRC
1380
1381 if [[ -n ${SCREEN_LOGDIR} ]]; then
1382 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1383 echo "log on" >>$SCREENRC
1384 fi
1385 fi
1386}
1387
1388# Stop a service in screen
1389# If a PID is available use it, kill the whole process group via TERM
1390# If screen is being used kill the screen window; this will catch processes
1391# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001392# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001393# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001394function screen_stop_service {
1395 local service=$1
1396
Dean Troyerdff49a22014-01-30 15:37:40 -06001397 SCREEN_NAME=${SCREEN_NAME:-stack}
1398 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1399 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1400
Dean Troyer3159a822014-08-27 14:13:58 -05001401 if is_service_enabled $service; then
1402 # Clean up the screen window
1403 screen -S $SCREEN_NAME -p $service -X kill
1404 fi
1405}
1406
1407# Stop a service process
1408# If a PID is available use it, kill the whole process group via TERM
1409# If screen is being used kill the screen window; this will catch processes
1410# that did not leave a PID behind
1411# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1412# stop_process service
1413function stop_process {
1414 local service=$1
1415
1416 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1417 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1418
1419 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001420 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001421 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1422 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1423 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001424 fi
1425 if [[ "$USE_SCREEN" = "True" ]]; then
1426 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001427 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001428 fi
1429 fi
1430}
1431
1432# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001433# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001434# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001435function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001436 local service
1437 local failures
1438 SCREEN_NAME=${SCREEN_NAME:-stack}
1439 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1440
1441
1442 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1443 echo "No service status directory found"
1444 return
1445 fi
1446
1447 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001448 # make this -o errexit safe
1449 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001450
1451 for service in $failures; do
1452 service=`basename $service`
1453 service=${service%.failure}
1454 echo "Error: Service $service is not running"
1455 done
1456
1457 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001458 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001459 fi
1460}
1461
Chris Dent2f27a0e2014-09-09 13:46:02 +01001462# Tail a log file in a screen if USE_SCREEN is true.
1463function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001464 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001465 local logfile=$2
1466
1467 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1468 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001469 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001470 fi
1471}
1472
Dean Troyerdff49a22014-01-30 15:37:40 -06001473
Dean Troyer3159a822014-08-27 14:13:58 -05001474# Deprecated Functions
1475# --------------------
1476
1477# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1478# fork. It includes the dirty work of closing extra filehandles and preparing log
1479# files to produce the same logs as screen_it(). The log filename is derived
1480# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1481# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1482# _old_run_process service "command-line"
1483function _old_run_process {
1484 local service=$1
1485 local command="$2"
1486
1487 # Undo logging redirections and close the extra descriptors
1488 exec 1>&3
1489 exec 2>&3
1490 exec 3>&-
1491 exec 6>&-
1492
1493 if [[ -n ${SCREEN_LOGDIR} ]]; then
1494 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1495 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1496
1497 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1498 export PYTHONUNBUFFERED=1
1499 fi
1500
1501 exec /bin/bash -c "$command"
1502 die "$service exec failure: $command"
1503}
1504
1505# old_run_process() launches a child process that closes all file descriptors and
1506# then exec's the passed in command. This is meant to duplicate the semantics
1507# of screen_it() without screen. PIDs are written to
1508# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1509# old_run_process service "command-line"
1510function old_run_process {
1511 local service=$1
1512 local command="$2"
1513
1514 # Spawn the child process
1515 _old_run_process "$service" "$command" &
1516 echo $!
1517}
1518
1519# Compatibility for existing start_XXXX() functions
1520# Uses global ``USE_SCREEN``
1521# screen_it service "command-line"
1522function screen_it {
1523 if is_service_enabled $1; then
1524 # Append the service to the screen rc file
1525 screen_rc "$1" "$2"
1526
1527 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001528 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001529 else
1530 # Spawn directly without screen
1531 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1532 fi
1533 fi
1534}
1535
1536# Compatibility for existing stop_XXXX() functions
1537# Stop a service in screen
1538# If a PID is available use it, kill the whole process group via TERM
1539# If screen is being used kill the screen window; this will catch processes
1540# that did not leave a PID behind
1541# screen_stop service
1542function screen_stop {
1543 # Clean up the screen window
1544 stop_process $1
1545}
1546
1547
Dean Troyerdff49a22014-01-30 15:37:40 -06001548# Python Functions
1549# ================
1550
1551# Get the path to the pip command.
1552# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001553function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001554 which pip || which pip-python
1555
1556 if [ $? -ne 0 ]; then
1557 die $LINENO "Unable to find pip; cannot continue"
1558 fi
1559}
1560
1561# Get the path to the direcotry where python executables are installed.
1562# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001563function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001564 if is_fedora || is_suse; then
1565 echo "/usr/bin"
1566 else
1567 echo "/usr/local/bin"
1568 fi
1569}
1570
1571# Wrapper for ``pip install`` to set cache and proxy environment variables
Attila Fazekasaf81d672014-11-10 09:04:54 +01001572# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``,
Dean Troyerdff49a22014-01-30 15:37:40 -06001573# ``TRACK_DEPENDS``, ``*_proxy``
1574# pip_install package [package ...]
1575function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001576 local xtrace=$(set +o | grep xtrace)
1577 set +o xtrace
1578 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1579 $xtrace
1580 return
1581 fi
1582
Dean Troyerdff49a22014-01-30 15:37:40 -06001583 if [[ -z "$os_PACKAGE" ]]; then
1584 GetOSVersion
1585 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001586 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1587 # TRACK_DEPENDS=True installation creates a circular dependency when
1588 # we attempt to install virtualenv into a virualenv, so we must global
1589 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001590 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001591 local cmd_pip=$DEST/.venv/bin/pip
1592 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001593 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001594 local cmd_pip=$(get_pip_command)
Jeremy Stanley6ec66bb2014-12-22 17:17:51 +00001595 local sudo_pip="sudo -H"
Dean Troyerdff49a22014-01-30 15:37:40 -06001596 fi
1597
Sean Dague45917cc2014-02-24 16:09:14 -05001598 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001599 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001600 http_proxy=$http_proxy \
1601 https_proxy=$https_proxy \
1602 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001603 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001604 $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001605
Flavio Percoco5a91c352014-10-31 18:48:00 +01001606 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001607 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1608 local test_req="$@/test-requirements.txt"
1609 if [[ -e "$test_req" ]]; then
1610 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1611 http_proxy=$http_proxy \
1612 https_proxy=$https_proxy \
1613 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001614 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001615 -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001616 fi
1617 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001618}
1619
Sean Daguecc524062014-10-01 09:06:43 -04001620# should we use this library from their git repo, or should we let it
1621# get pulled in via pip dependencies.
1622function use_library_from_git {
1623 local name=$1
1624 local enabled=1
1625 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1626 return $enabled
1627}
1628
1629# setup a library by name. If we are trying to use the library from
1630# git, we'll do a git based install, otherwise we'll punt and the
1631# library should be installed by a requirements pull from another
1632# project.
1633function setup_lib {
1634 local name=$1
1635 local dir=${GITDIR[$name]}
1636 setup_install $dir
1637}
1638
Sean Daguee08ab102014-11-13 17:09:28 -05001639# setup a library by name in editiable mode. If we are trying to use
1640# the library from git, we'll do a git based install, otherwise we'll
1641# punt and the library should be installed by a requirements pull from
1642# another project.
1643#
1644# use this for non namespaced libraries
1645function setup_dev_lib {
1646 local name=$1
1647 local dir=${GITDIR[$name]}
1648 setup_develop $dir
1649}
Sean Daguecc524062014-10-01 09:06:43 -04001650
Sean Dague099e5e32014-03-31 10:35:43 -04001651# this should be used if you want to install globally, all libraries should
1652# use this, especially *oslo* ones
1653function setup_install {
1654 local project_dir=$1
1655 setup_package_with_req_sync $project_dir
1656}
1657
1658# this should be used for projects which run services, like all services
1659function setup_develop {
1660 local project_dir=$1
1661 setup_package_with_req_sync $project_dir -e
1662}
1663
Sean Daguedef15342014-10-27 12:26:04 -04001664# determine if a project as specified by directory is in
1665# projects.txt. This will not be an exact match because we throw away
1666# the namespacing when we clone, but it should be good enough in all
1667# practical ways.
1668function is_in_projects_txt {
1669 local project_dir=$1
1670 local project_name=$(basename $project_dir)
1671 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1672}
1673
Dean Troyeraf616d92014-02-17 12:57:55 -06001674# ``pip install -e`` the package, which processes the dependencies
1675# using pip before running `setup.py develop`
1676#
1677# Updates the dependencies in project_dir from the
1678# openstack/requirements global list before installing anything.
1679#
1680# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1681# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001682function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001683 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001684 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001685
Dean Troyeraf616d92014-02-17 12:57:55 -06001686 # Don't update repo if local changes exist
1687 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001688 # ``errexit`` requires us to trap the exit code when the repo is changed
1689 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001690
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001691 if [[ $update_requirements != "changed" ]]; then
Sean Daguedef15342014-10-27 12:26:04 -04001692 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1693 if is_in_projects_txt $project_dir; then
1694 (cd $REQUIREMENTS_DIR; \
1695 python update.py $project_dir)
1696 else
1697 # soft update projects not found in requirements project.txt
1698 (cd $REQUIREMENTS_DIR; \
1699 python update.py -s $project_dir)
1700 fi
1701 else
1702 (cd $REQUIREMENTS_DIR; \
1703 python update.py $project_dir)
1704 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001705 fi
1706
Sean Dague099e5e32014-03-31 10:35:43 -04001707 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001708
1709 # We've just gone and possibly modified the user's source tree in an
1710 # automated way, which is considered bad form if it's a development
1711 # tree because we've screwed up their next git checkin. So undo it.
1712 #
1713 # However... there are some circumstances, like running in the gate
1714 # where we really really want the overridden version to stick. So provide
1715 # a variable that tells us whether or not we should UNDO the requirements
1716 # changes (this will be set to False in the OpenStack ci gate)
1717 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001718 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001719 (cd $project_dir && git reset --hard)
1720 fi
1721 fi
1722}
1723
1724# ``pip install -e`` the package, which processes the dependencies
1725# using pip before running `setup.py develop`
1726# Uses globals ``STACK_USER``
1727# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001728function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001729 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001730 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001731
Sean Dague099e5e32014-03-31 10:35:43 -04001732 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001733 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001734 if [[ "$flags" == "-e" ]]; then
1735 safe_chown -R $STACK_USER $1/*.egg-info
1736 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001737}
1738
Sean Dague2c65e712014-12-18 09:44:56 -05001739# Plugin Functions
1740# =================
1741
1742DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1743
1744# enable_plugin <name> <url> [branch]
1745#
1746# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1747# ``url`` is a git url
1748# ``branch`` is a gitref. If it's not set, defaults to master
1749function enable_plugin {
1750 local name=$1
1751 local url=$2
1752 local branch=${3:-master}
1753 DEVSTACK_PLUGINS+=",$name"
1754 GITREPO[$name]=$url
1755 GITDIR[$name]=$DEST/$name
1756 GITBRANCH[$name]=$branch
1757}
1758
1759# fetch_plugins
1760#
1761# clones all plugins
1762function fetch_plugins {
1763 local plugins="${DEVSTACK_PLUGINS}"
1764 local plugin
1765
1766 # short circuit if nothing to do
1767 if [[ -z $plugins ]]; then
1768 return
1769 fi
1770
1771 echo "Fetching devstack plugins"
1772 for plugin in ${plugins//,/ }; do
1773 git_clone_by_name $plugin
1774 done
1775}
1776
1777# load_plugin_settings
1778#
1779# Load settings from plugins in the order that they were registered
1780function load_plugin_settings {
1781 local plugins="${DEVSTACK_PLUGINS}"
1782 local plugin
1783
1784 # short circuit if nothing to do
1785 if [[ -z $plugins ]]; then
1786 return
1787 fi
1788
1789 echo "Loading plugin settings"
1790 for plugin in ${plugins//,/ }; do
1791 local dir=${GITDIR[$plugin]}
1792 # source any known settings
1793 if [[ -f $dir/devstack/settings ]]; then
1794 source $dir/devstack/settings
1795 fi
1796 done
1797}
1798
1799# run_plugins
1800#
1801# Run the devstack/plugin.sh in all the plugin directories. These are
1802# run in registration order.
1803function run_plugins {
1804 local mode=$1
1805 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301806
1807 local plugins="${DEVSTACK_PLUGINS}"
1808 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001809 for plugin in ${plugins//,/ }; do
1810 local dir=${GITDIR[$plugin]}
1811 if [[ -f $dir/devstack/plugin.sh ]]; then
1812 source $dir/devstack/plugin.sh $mode $phase
1813 fi
1814 done
1815}
1816
1817function run_phase {
1818 local mode=$1
1819 local phase=$2
1820 if [[ -d $TOP_DIR/extras.d ]]; then
1821 for i in $TOP_DIR/extras.d/*.sh; do
1822 [[ -r $i ]] && source $i $mode $phase
1823 done
1824 fi
1825 # the source phase corresponds to settings loading in plugins
1826 if [[ "$mode" == "source" ]]; then
1827 load_plugin_settings
1828 else
1829 run_plugins $mode $phase
1830 fi
1831}
1832
Dean Troyerdff49a22014-01-30 15:37:40 -06001833
1834# Service Functions
1835# =================
1836
1837# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1838# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001839function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001840 echo "$1" | sed -e '
1841 s/,,/,/g;
1842 s/^,//;
1843 s/,$//
1844 '
1845}
1846
1847# disable_all_services() removes all current services
1848# from ``ENABLED_SERVICES`` to reset the configuration
1849# before a minimal installation
1850# Uses global ``ENABLED_SERVICES``
1851# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001852function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001853 ENABLED_SERVICES=""
1854}
1855
1856# Remove all services starting with '-'. For example, to install all default
1857# services except rabbit (rabbit) set in ``localrc``:
1858# ENABLED_SERVICES+=",-rabbit"
1859# Uses global ``ENABLED_SERVICES``
1860# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001861function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001862 local tmpsvcs="${ENABLED_SERVICES}"
1863 local service
1864 for service in ${tmpsvcs//,/ }; do
1865 if [[ ${service} == -* ]]; then
1866 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1867 fi
1868 done
1869 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1870}
1871
1872# disable_service() removes the services passed as argument to the
1873# ``ENABLED_SERVICES`` list, if they are present.
1874#
1875# For example:
1876# disable_service rabbit
1877#
1878# This function does not know about the special cases
1879# for nova, glance, and neutron built into is_service_enabled().
1880# Uses global ``ENABLED_SERVICES``
1881# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001882function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001883 local tmpsvcs=",${ENABLED_SERVICES},"
1884 local service
1885 for service in $@; do
1886 if is_service_enabled $service; then
1887 tmpsvcs=${tmpsvcs//,$service,/,}
1888 fi
1889 done
1890 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1891}
1892
1893# enable_service() adds the services passed as argument to the
1894# ``ENABLED_SERVICES`` list, if they are not already present.
1895#
1896# For example:
1897# enable_service qpid
1898#
1899# This function does not know about the special cases
1900# for nova, glance, and neutron built into is_service_enabled().
1901# Uses global ``ENABLED_SERVICES``
1902# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001903function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001904 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001905 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001906 for service in $@; do
1907 if ! is_service_enabled $service; then
1908 tmpsvcs+=",$service"
1909 fi
1910 done
1911 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1912 disable_negated_services
1913}
1914
1915# is_service_enabled() checks if the service(s) specified as arguments are
1916# enabled by the user in ``ENABLED_SERVICES``.
1917#
1918# Multiple services specified as arguments are ``OR``'ed together; the test
1919# is a short-circuit boolean, i.e it returns on the first match.
1920#
1921# There are special cases for some 'catch-all' services::
1922# **nova** returns true if any service enabled start with **n-**
1923# **cinder** returns true if any service enabled start with **c-**
1924# **ceilometer** returns true if any service enabled start with **ceilometer**
1925# **glance** returns true if any service enabled start with **g-**
1926# **neutron** returns true if any service enabled start with **q-**
1927# **swift** returns true if any service enabled start with **s-**
1928# **trove** returns true if any service enabled start with **tr-**
1929# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1930# **s-** services will be enabled. This will be deprecated in the future.
1931#
1932# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1933# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1934# as enabled in this case.
1935#
1936# Uses global ``ENABLED_SERVICES``
1937# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001938function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001939 local xtrace=$(set +o | grep xtrace)
1940 set +o xtrace
1941 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001942 local services=$@
1943 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001944 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001945 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001946
1947 # Look for top-level 'enabled' function for this service
1948 if type is_${service}_enabled >/dev/null 2>&1; then
1949 # A function exists for this service, use it
1950 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001951 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001952 fi
1953
1954 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1955 # are implemented
1956
Sean Dague45917cc2014-02-24 16:09:14 -05001957 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001958 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001959 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1960 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1961 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1962 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1963 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1964 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1965 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1966 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1967 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001968 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001969 done
Sean Dague45917cc2014-02-24 16:09:14 -05001970 $xtrace
1971 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001972}
1973
1974# Toggle enable/disable_service for services that must run exclusive of each other
1975# $1 The name of a variable containing a space-separated list of services
1976# $2 The name of a variable in which to store the enabled service's name
1977# $3 The name of the service to enable
1978function use_exclusive_service {
1979 local options=${!1}
1980 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001981 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001982 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001983 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001984 for opt in $options;do
1985 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1986 done
1987 eval "$out=$selection"
1988 return 0
1989}
1990
1991
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001992# System Functions
1993# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001994
1995# Only run the command if the target file (the last arg) is not on an
1996# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001997function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001998 local xtrace=$(set +o | grep xtrace)
1999 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002000 local args=( $@ )
2001 local last
2002 local sudo_cmd
2003 local dir_to_check
2004
2005 let last="${#args[*]} - 1"
2006
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002007 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002008 if [ ! -d "$dir_to_check" ]; then
2009 dir_to_check=`dirname "$dir_to_check"`
2010 fi
2011
2012 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002013 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002014 return 0
2015 fi
2016
2017 if [[ $TRACK_DEPENDS = True ]]; then
2018 sudo_cmd="env"
2019 else
2020 sudo_cmd="sudo"
2021 fi
2022
Sean Dague45917cc2014-02-24 16:09:14 -05002023 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002024 $sudo_cmd $@
2025}
2026
2027# Exit 0 if address is in network or 1 if address is not in network
2028# ip-range is in CIDR notation: 1.2.3.4/20
2029# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002030function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002031 local ip=$1
2032 local range=$2
2033 local masklen=${range#*/}
2034 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2035 local subnet=$(maskip $ip $(cidr2netmask $masklen))
2036 [[ $network == $subnet ]]
2037}
2038
2039# Add a user to a group.
2040# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002041function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002042 local user=$1
2043 local group=$2
2044
2045 if [[ -z "$os_VENDOR" ]]; then
2046 GetOSVersion
2047 fi
2048
2049 # SLE11 and openSUSE 12.2 don't have the usual usermod
2050 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
2051 sudo usermod -a -G "$group" "$user"
2052 else
2053 sudo usermod -A "$group" "$user"
2054 fi
2055}
2056
2057# Convert CIDR notation to a IPv4 netmask
2058# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002059function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002060 local maskpat="255 255 255 255"
2061 local maskdgt="254 252 248 240 224 192 128"
2062 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2063 echo ${1-0}.${2-0}.${3-0}.${4-0}
2064}
2065
2066# Gracefully cp only if source file/dir exists
2067# cp_it source destination
2068function cp_it {
2069 if [ -e $1 ] || [ -d $1 ]; then
2070 cp -pRL $1 $2
2071 fi
2072}
2073
2074# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2075# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2076# ``localrc`` or on the command line if necessary::
2077#
2078# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2079#
2080# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2081
Ian Wienandaee18c72014-02-21 15:35:08 +11002082function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06002083 if [[ -n "$http_proxy" ]]; then
2084 export http_proxy=$http_proxy
2085 fi
2086 if [[ -n "$https_proxy" ]]; then
2087 export https_proxy=$https_proxy
2088 fi
2089 if [[ -n "$no_proxy" ]]; then
2090 export no_proxy=$no_proxy
2091 fi
2092}
2093
2094# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002095function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06002096 local mount_type=`stat -f -L -c %T $1`
2097 test "$mount_type" == "nfs"
2098}
2099
2100# Return the network portion of the given IP address using netmask
2101# netmask is in the traditional dotted-quad format
2102# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002103function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002104 local ip=$1
2105 local mask=$2
2106 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2107 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2108 echo $subnet
2109}
2110
2111# Service wrapper to restart services
2112# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002113function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002114 if is_ubuntu; then
2115 sudo /usr/sbin/service $1 restart
2116 else
2117 sudo /sbin/service $1 restart
2118 fi
2119}
2120
2121# Only change permissions of a file or directory if it is not on an
2122# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002123function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002124 _safe_permission_operation chmod $@
2125}
2126
2127# Only change ownership of a file or directory if it is not on an NFS
2128# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002129function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002130 _safe_permission_operation chown $@
2131}
2132
2133# Service wrapper to start services
2134# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002135function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002136 if is_ubuntu; then
2137 sudo /usr/sbin/service $1 start
2138 else
2139 sudo /sbin/service $1 start
2140 fi
2141}
2142
2143# Service wrapper to stop services
2144# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002145function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002146 if is_ubuntu; then
2147 sudo /usr/sbin/service $1 stop
2148 else
2149 sudo /sbin/service $1 stop
2150 fi
2151}
2152
2153
2154# Restore xtrace
2155$XTRACE
2156
2157# Local variables:
2158# mode: shell-script
2159# End: