blob: c3c8486f137bd0deafad9facbafe4ce77b9f366a [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 {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500818 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100819 # 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
Steve Martinellib74e01c2014-12-18 01:35:35 -0500834# Gets or creates group
835# Usage: get_or_create_group <groupname> [<domain> <description>]
836function get_or_create_group {
837 local domain=${2:+--domain ${2}}
838 local desc="${3:-}"
839 local os_url="$KEYSTONE_SERVICE_URI_V3"
840 # Gets group id
841 local group_id=$(
842 # Creates new group with --or-show
843 openstack --os-token=$OS_TOKEN --os-url=$os_url \
844 --os-identity-api-version=3 group create $1 \
845 $domain --description "$desc" --or-show \
846 -f value -c id
847 )
848 echo $group_id
849}
850
Bartosz Górski0abde392014-02-28 14:15:19 +0100851# Gets or creates user
Alistair Coles24779f62014-10-15 18:57:59 +0100852# Usage: get_or_create_user <username> <password> <project> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100853function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200854 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500855 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200856 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500857 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200858 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100859 local os_cmd="openstack"
860 local domain=""
861 if [[ ! -z "$5" ]]; then
862 domain="--domain=$5"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500863 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100864 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100865 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500866 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500867 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100868 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100869 $1 \
870 --password "$2" \
871 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500872 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100873 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500874 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100875 -f value -c id
876 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500877 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100878}
879
880# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100881# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100882function get_or_create_project {
883 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100884 local os_cmd="openstack"
885 local domain=""
886 if [[ ! -z "$2" ]]; then
887 domain="--domain=$2"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500888 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100889 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500890 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500891 # Creates new project with --or-show
892 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100893 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500894 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100895}
896
897# Gets or creates role
898# Usage: get_or_create_role <name>
899function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500900 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500901 # Creates role with --or-show
902 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100903 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500904 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100905}
906
907# Gets or adds user role
908# Usage: get_or_add_user_role <role> <user> <project>
909function get_or_add_user_role {
910 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500911 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100912 $2 \
913 --project $3 \
914 --column "ID" \
915 --column "Name" \
916 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500917 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100918 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500919 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100920 $1 \
921 --user $2 \
922 --project $3 \
923 | grep " id " | get_field 2)
924 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500925 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100926}
927
928# Gets or creates service
929# Usage: get_or_create_service <name> <type> <description>
930function get_or_create_service {
931 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500932 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100933 # Gets service id
934 openstack service show $1 -f value -c id 2>/dev/null ||
935 # Creates new service if not exists
936 openstack service create \
937 $1 \
938 --type=$2 \
939 --description="$3" \
940 -f value -c id
941 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500942 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100943}
944
945# Gets or creates endpoint
946# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
947function get_or_create_endpoint {
948 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500949 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100950 --column "ID" \
951 --column "Region" \
952 --column "Service Name" \
953 | grep " $2 " \
954 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500955 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100956 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500957 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100958 $1 \
959 --region $2 \
960 --publicurl $3 \
961 --adminurl $4 \
962 --internalurl $5 \
963 | grep " id " | get_field 2)
964 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500965 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100966}
Dean Troyerdff49a22014-01-30 15:37:40 -0600967
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500968
Dean Troyerdff49a22014-01-30 15:37:40 -0600969# Package Functions
970# =================
971
972# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100973function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600974 local pkg_dir
975 if is_ubuntu; then
Monty Taylor81a016d2014-11-15 17:18:13 -0300976 pkg_dir=$FILES/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600977 elif is_fedora; then
978 pkg_dir=$FILES/rpms
979 elif is_suse; then
980 pkg_dir=$FILES/rpms-suse
981 else
982 exit_distro_not_supported "list of packages"
983 fi
984 echo "$pkg_dir"
985}
986
987# Wrapper for ``apt-get`` to set cache and proxy environment variables
988# Uses globals ``OFFLINE``, ``*_proxy``
989# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100990function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500991 local xtrace=$(set +o | grep xtrace)
992 set +o xtrace
993
Dean Troyerdff49a22014-01-30 15:37:40 -0600994 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
995 local sudo="sudo"
996 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500997
998 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600999 $sudo DEBIAN_FRONTEND=noninteractive \
1000 http_proxy=$http_proxy https_proxy=$https_proxy \
1001 no_proxy=$no_proxy \
1002 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
1003}
1004
1005# get_packages() collects a list of package names of any type from the
Monty Taylor81a016d2014-11-15 17:18:13 -03001006# prerequisite files in ``files/{debs|rpms}``. The list is intended
Dean Troyerdff49a22014-01-30 15:37:40 -06001007# to be passed to a package installer such as apt or yum.
1008#
1009# Only packages required for the services in 1st argument will be
1010# included. Two bits of metadata are recognized in the prerequisite files:
1011#
1012# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1013# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1014# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +11001015function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -05001016 local xtrace=$(set +o | grep xtrace)
1017 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001018 local services=$@
1019 local package_dir=$(_get_package_dir)
1020 local file_to_parse
1021 local service
1022
Flavio Percoco5a91c352014-10-31 18:48:00 +01001023 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
1024
Dean Troyerdff49a22014-01-30 15:37:40 -06001025 if [[ -z "$package_dir" ]]; then
1026 echo "No package directory supplied"
1027 return 1
1028 fi
1029 if [[ -z "$DISTRO" ]]; then
1030 GetDistro
1031 fi
1032 for service in ${services//,/ }; do
1033 # Allow individual services to specify dependencies
1034 if [[ -e ${package_dir}/${service} ]]; then
1035 file_to_parse="${file_to_parse} $service"
1036 fi
1037 # NOTE(sdague) n-api needs glance for now because that's where
1038 # glance client is
1039 if [[ $service == n-api ]]; then
1040 if [[ ! $file_to_parse =~ nova ]]; then
1041 file_to_parse="${file_to_parse} nova"
1042 fi
1043 if [[ ! $file_to_parse =~ glance ]]; then
1044 file_to_parse="${file_to_parse} glance"
1045 fi
1046 elif [[ $service == c-* ]]; then
1047 if [[ ! $file_to_parse =~ cinder ]]; then
1048 file_to_parse="${file_to_parse} cinder"
1049 fi
1050 elif [[ $service == ceilometer-* ]]; then
1051 if [[ ! $file_to_parse =~ ceilometer ]]; then
1052 file_to_parse="${file_to_parse} ceilometer"
1053 fi
1054 elif [[ $service == s-* ]]; then
1055 if [[ ! $file_to_parse =~ swift ]]; then
1056 file_to_parse="${file_to_parse} swift"
1057 fi
1058 elif [[ $service == n-* ]]; then
1059 if [[ ! $file_to_parse =~ nova ]]; then
1060 file_to_parse="${file_to_parse} nova"
1061 fi
1062 elif [[ $service == g-* ]]; then
1063 if [[ ! $file_to_parse =~ glance ]]; then
1064 file_to_parse="${file_to_parse} glance"
1065 fi
1066 elif [[ $service == key* ]]; then
1067 if [[ ! $file_to_parse =~ keystone ]]; then
1068 file_to_parse="${file_to_parse} keystone"
1069 fi
1070 elif [[ $service == q-* ]]; then
1071 if [[ ! $file_to_parse =~ neutron ]]; then
1072 file_to_parse="${file_to_parse} neutron"
1073 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001074 elif [[ $service == ir-* ]]; then
1075 if [[ ! $file_to_parse =~ ironic ]]; then
1076 file_to_parse="${file_to_parse} ironic"
1077 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001078 fi
1079 done
1080
1081 for file in ${file_to_parse}; do
1082 local fname=${package_dir}/${file}
1083 local OIFS line package distros distro
1084 [[ -e $fname ]] || continue
1085
1086 OIFS=$IFS
1087 IFS=$'\n'
1088 for line in $(<${fname}); do
1089 if [[ $line =~ "NOPRIME" ]]; then
1090 continue
1091 fi
1092
1093 # Assume we want this package
1094 package=${line%#*}
1095 inst_pkg=1
1096
1097 # Look for # dist:xxx in comment
1098 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1099 # We are using BASH regexp matching feature.
1100 package=${BASH_REMATCH[1]}
1101 distros=${BASH_REMATCH[2]}
1102 # In bash ${VAR,,} will lowecase VAR
1103 # Look for a match in the distro list
1104 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1105 # If no match then skip this package
1106 inst_pkg=0
1107 fi
1108 fi
1109
1110 # Look for # testonly in comment
1111 if [[ $line =~ (.*)#.*testonly.* ]]; then
1112 package=${BASH_REMATCH[1]}
1113 # Are we installing test packages? (test for the default value)
1114 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1115 # If not installing test packages the skip this package
1116 inst_pkg=0
1117 fi
1118 fi
1119
1120 if [[ $inst_pkg = 1 ]]; then
1121 echo $package
1122 fi
1123 done
1124 IFS=$OIFS
1125 done
Sean Dague45917cc2014-02-24 16:09:14 -05001126 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001127}
1128
1129# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001130# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001131# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001132function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001133 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001134 return 0
1135 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001136
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001137 if is_ubuntu; then
1138 local xtrace=$(set +o | grep xtrace)
1139 set +o xtrace
1140 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1141 # if there are transient errors pulling the updates, that's fine.
1142 # It may be secondary repositories that we don't really care about.
1143 apt_get update || /bin/true
1144 REPOS_UPDATED=True
1145 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001146 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001147 fi
1148}
1149
1150function real_install_package {
1151 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001152 apt_get install "$@"
1153 elif is_fedora; then
1154 yum_install "$@"
1155 elif is_suse; then
1156 zypper_install "$@"
1157 else
1158 exit_distro_not_supported "installing packages"
1159 fi
1160}
1161
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001162# Distro-agnostic package installer
1163# install_package package [package ...]
1164function install_package {
1165 update_package_repo
1166 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1167}
1168
Dean Troyerdff49a22014-01-30 15:37:40 -06001169# Distro-agnostic function to tell if a package is installed
1170# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001171function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001172 if [[ -z "$@" ]]; then
1173 return 1
1174 fi
1175
1176 if [[ -z "$os_PACKAGE" ]]; then
1177 GetOSVersion
1178 fi
1179
1180 if [[ "$os_PACKAGE" = "deb" ]]; then
1181 dpkg -s "$@" > /dev/null 2> /dev/null
1182 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1183 rpm --quiet -q "$@"
1184 else
1185 exit_distro_not_supported "finding if a package is installed"
1186 fi
1187}
1188
1189# Distro-agnostic package uninstaller
1190# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001191function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001192 if is_ubuntu; then
1193 apt_get purge "$@"
1194 elif is_fedora; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001195 sudo $YUM remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001196 elif is_suse; then
1197 sudo zypper rm "$@"
1198 else
1199 exit_distro_not_supported "uninstalling packages"
1200 fi
1201}
1202
1203# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001204# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001205# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001206function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001207 [[ "$OFFLINE" = "True" ]] && return
1208 local sudo="sudo"
1209 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001210
1211 # The manual check for missing packages is because yum -y assumes
1212 # missing packages are OK. See
1213 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001214 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1215 no_proxy=$no_proxy \
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001216 $YUM install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001217 awk '
1218 BEGIN { fail=0 }
1219 /No package/ { fail=1 }
1220 { print }
1221 END { exit fail }' || \
1222 die $LINENO "Missing packages detected"
1223
1224 # also ensure we catch a yum failure
1225 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001226 die $LINENO "$YUM install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001227 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001228}
1229
1230# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001231# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001232# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001233function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001234 [[ "$OFFLINE" = "True" ]] && return
1235 local sudo="sudo"
1236 [[ "$(id -u)" = "0" ]] && sudo="env"
1237 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1238 zypper --non-interactive install --auto-agree-with-licenses "$@"
1239}
1240
1241
1242# Process Functions
1243# =================
1244
1245# _run_process() is designed to be backgrounded by run_process() to simulate a
1246# fork. It includes the dirty work of closing extra filehandles and preparing log
1247# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001248# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001249# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001250# If an optional group is provided sg will be used to set the group of
1251# the command.
1252# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001253function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001254 local service=$1
1255 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001256 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001257
1258 # Undo logging redirections and close the extra descriptors
1259 exec 1>&3
1260 exec 2>&3
1261 exec 3>&-
1262 exec 6>&-
1263
1264 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001265 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1266 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001267
1268 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1269 export PYTHONUNBUFFERED=1
1270 fi
1271
Dean Troyer3159a822014-08-27 14:13:58 -05001272 # Run under ``setsid`` to force the process to become a session and group leader.
1273 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001274 if [[ -n "$group" ]]; then
1275 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1276 else
1277 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1278 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001279
1280 # Just silently exit this process
1281 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001282}
1283
1284# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1285# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001286# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001287# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001288function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001289 SCREEN_NAME=${SCREEN_NAME:-stack}
1290 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1291
1292 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1293 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1294 fi
1295
1296 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1297}
1298
1299# Find out if a process exists by partial name.
1300# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001301function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001302 local name=$1
1303 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001304 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001305 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001306 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001307}
1308
Dean Troyer3159a822014-08-27 14:13:58 -05001309# Run a single service under screen or directly
1310# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001311# If an optional group is provided sg will be used to run the
1312# command as that group.
1313# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001314function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001315 local service=$1
1316 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001317 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001318
Dean Troyer3159a822014-08-27 14:13:58 -05001319 if is_service_enabled $service; then
1320 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001321 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001322 else
1323 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001324 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001325 fi
1326 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001327}
1328
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001329# Helper to launch a process in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001330# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1331# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001332# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001333# Run a command in a shell in a screen window, if an optional group
1334# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001335function screen_process {
1336 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001337 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001338 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001339
Sean Dagueea22a4f2014-06-27 15:21:41 -04001340 SCREEN_NAME=${SCREEN_NAME:-stack}
1341 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1342 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001343
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001344 # Append the process to the screen rc file
1345 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001346
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001347 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001348
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001349 if [[ -n ${SCREEN_LOGDIR} ]]; then
1350 screen -S $SCREEN_NAME -p $name -X logfile ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log
1351 screen -S $SCREEN_NAME -p $name -X log on
1352 ln -sf ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${name}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001353 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001354
1355 # sleep to allow bash to be ready to be send the command - we are
1356 # creating a new window in screen and then sends characters, so if
1357 # bash isn't running by the time we send the command, nothing happens
1358 sleep 3
1359
1360 NL=`echo -ne '\015'`
1361 # This fun command does the following:
1362 # - the passed server command is backgrounded
1363 # - the pid of the background process is saved in the usual place
1364 # - the server process is brought back to the foreground
1365 # - if the server process exits prematurely the fg command errors
1366 # and a message is written to stdout and the process failure file
1367 #
1368 # The pid saved can be used in stop_process() as a process group
1369 # id to kill off all child processes
1370 if [[ -n "$group" ]]; then
1371 command="sg $group '$command'"
1372 fi
1373 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 -06001374}
1375
1376# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001377# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001378# screen_rc service "command-line"
1379function screen_rc {
1380 SCREEN_NAME=${SCREEN_NAME:-stack}
1381 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1382 if [[ ! -e $SCREENRC ]]; then
1383 # Name the screen session
1384 echo "sessionname $SCREEN_NAME" > $SCREENRC
1385 # Set a reasonable statusbar
1386 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1387 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1388 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1389 echo "screen -t shell bash" >> $SCREENRC
1390 fi
1391 # If this service doesn't already exist in the screenrc file
1392 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1393 NL=`echo -ne '\015'`
1394 echo "screen -t $1 bash" >> $SCREENRC
1395 echo "stuff \"$2$NL\"" >> $SCREENRC
1396
1397 if [[ -n ${SCREEN_LOGDIR} ]]; then
1398 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1399 echo "log on" >>$SCREENRC
1400 fi
1401 fi
1402}
1403
1404# Stop a service in screen
1405# If a PID is available use it, kill the whole process group via TERM
1406# If screen is being used kill the screen window; this will catch processes
1407# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001408# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001409# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001410function screen_stop_service {
1411 local service=$1
1412
Dean Troyerdff49a22014-01-30 15:37:40 -06001413 SCREEN_NAME=${SCREEN_NAME:-stack}
1414 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1415 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1416
Dean Troyer3159a822014-08-27 14:13:58 -05001417 if is_service_enabled $service; then
1418 # Clean up the screen window
1419 screen -S $SCREEN_NAME -p $service -X kill
1420 fi
1421}
1422
1423# Stop a service process
1424# If a PID is available use it, kill the whole process group via TERM
1425# If screen is being used kill the screen window; this will catch processes
1426# that did not leave a PID behind
1427# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1428# stop_process service
1429function stop_process {
1430 local service=$1
1431
1432 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1433 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1434
1435 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001436 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001437 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1438 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1439 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001440 fi
1441 if [[ "$USE_SCREEN" = "True" ]]; then
1442 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001443 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001444 fi
1445 fi
1446}
1447
1448# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001449# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001450# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001451function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001452 local service
1453 local failures
1454 SCREEN_NAME=${SCREEN_NAME:-stack}
1455 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1456
1457
1458 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1459 echo "No service status directory found"
1460 return
1461 fi
1462
1463 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001464 # make this -o errexit safe
1465 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001466
1467 for service in $failures; do
1468 service=`basename $service`
1469 service=${service%.failure}
1470 echo "Error: Service $service is not running"
1471 done
1472
1473 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001474 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001475 fi
1476}
1477
Chris Dent2f27a0e2014-09-09 13:46:02 +01001478# Tail a log file in a screen if USE_SCREEN is true.
1479function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001480 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001481 local logfile=$2
1482
1483 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1484 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001485 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001486 fi
1487}
1488
Dean Troyerdff49a22014-01-30 15:37:40 -06001489
Dean Troyer3159a822014-08-27 14:13:58 -05001490# Deprecated Functions
1491# --------------------
1492
1493# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1494# fork. It includes the dirty work of closing extra filehandles and preparing log
1495# files to produce the same logs as screen_it(). The log filename is derived
1496# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1497# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1498# _old_run_process service "command-line"
1499function _old_run_process {
1500 local service=$1
1501 local command="$2"
1502
1503 # Undo logging redirections and close the extra descriptors
1504 exec 1>&3
1505 exec 2>&3
1506 exec 3>&-
1507 exec 6>&-
1508
1509 if [[ -n ${SCREEN_LOGDIR} ]]; then
1510 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1511 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1512
1513 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1514 export PYTHONUNBUFFERED=1
1515 fi
1516
1517 exec /bin/bash -c "$command"
1518 die "$service exec failure: $command"
1519}
1520
1521# old_run_process() launches a child process that closes all file descriptors and
1522# then exec's the passed in command. This is meant to duplicate the semantics
1523# of screen_it() without screen. PIDs are written to
1524# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1525# old_run_process service "command-line"
1526function old_run_process {
1527 local service=$1
1528 local command="$2"
1529
1530 # Spawn the child process
1531 _old_run_process "$service" "$command" &
1532 echo $!
1533}
1534
1535# Compatibility for existing start_XXXX() functions
1536# Uses global ``USE_SCREEN``
1537# screen_it service "command-line"
1538function screen_it {
1539 if is_service_enabled $1; then
1540 # Append the service to the screen rc file
1541 screen_rc "$1" "$2"
1542
1543 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001544 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001545 else
1546 # Spawn directly without screen
1547 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1548 fi
1549 fi
1550}
1551
1552# Compatibility for existing stop_XXXX() functions
1553# Stop a service in screen
1554# If a PID is available use it, kill the whole process group via TERM
1555# If screen is being used kill the screen window; this will catch processes
1556# that did not leave a PID behind
1557# screen_stop service
1558function screen_stop {
1559 # Clean up the screen window
1560 stop_process $1
1561}
1562
1563
Dean Troyerdff49a22014-01-30 15:37:40 -06001564# Python Functions
1565# ================
1566
1567# Get the path to the pip command.
1568# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001569function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001570 which pip || which pip-python
1571
1572 if [ $? -ne 0 ]; then
1573 die $LINENO "Unable to find pip; cannot continue"
1574 fi
1575}
1576
1577# Get the path to the direcotry where python executables are installed.
1578# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001579function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001580 if is_fedora || is_suse; then
1581 echo "/usr/bin"
1582 else
1583 echo "/usr/local/bin"
1584 fi
1585}
1586
1587# Wrapper for ``pip install`` to set cache and proxy environment variables
Attila Fazekasaf81d672014-11-10 09:04:54 +01001588# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``,
Dean Troyerdff49a22014-01-30 15:37:40 -06001589# ``TRACK_DEPENDS``, ``*_proxy``
1590# pip_install package [package ...]
1591function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001592 local xtrace=$(set +o | grep xtrace)
1593 set +o xtrace
1594 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1595 $xtrace
1596 return
1597 fi
1598
Dean Troyerdff49a22014-01-30 15:37:40 -06001599 if [[ -z "$os_PACKAGE" ]]; then
1600 GetOSVersion
1601 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001602 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1603 # TRACK_DEPENDS=True installation creates a circular dependency when
1604 # we attempt to install virtualenv into a virualenv, so we must global
1605 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001606 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001607 local cmd_pip=$DEST/.venv/bin/pip
1608 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001609 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001610 local cmd_pip=$(get_pip_command)
Jeremy Stanley6ec66bb2014-12-22 17:17:51 +00001611 local sudo_pip="sudo -H"
Dean Troyerdff49a22014-01-30 15:37:40 -06001612 fi
1613
Sean Dague45917cc2014-02-24 16:09:14 -05001614 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001615 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001616 http_proxy=$http_proxy \
1617 https_proxy=$https_proxy \
1618 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001619 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001620 $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001621
Flavio Percoco5a91c352014-10-31 18:48:00 +01001622 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001623 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1624 local test_req="$@/test-requirements.txt"
1625 if [[ -e "$test_req" ]]; then
1626 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1627 http_proxy=$http_proxy \
1628 https_proxy=$https_proxy \
1629 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001630 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001631 -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001632 fi
1633 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001634}
1635
Sean Daguecc524062014-10-01 09:06:43 -04001636# should we use this library from their git repo, or should we let it
1637# get pulled in via pip dependencies.
1638function use_library_from_git {
1639 local name=$1
1640 local enabled=1
1641 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1642 return $enabled
1643}
1644
1645# setup a library by name. If we are trying to use the library from
1646# git, we'll do a git based install, otherwise we'll punt and the
1647# library should be installed by a requirements pull from another
1648# project.
1649function setup_lib {
1650 local name=$1
1651 local dir=${GITDIR[$name]}
1652 setup_install $dir
1653}
1654
Sean Daguee08ab102014-11-13 17:09:28 -05001655# setup a library by name in editiable mode. If we are trying to use
1656# the library from git, we'll do a git based install, otherwise we'll
1657# punt and the library should be installed by a requirements pull from
1658# another project.
1659#
1660# use this for non namespaced libraries
1661function setup_dev_lib {
1662 local name=$1
1663 local dir=${GITDIR[$name]}
1664 setup_develop $dir
1665}
Sean Daguecc524062014-10-01 09:06:43 -04001666
Sean Dague099e5e32014-03-31 10:35:43 -04001667# this should be used if you want to install globally, all libraries should
1668# use this, especially *oslo* ones
1669function setup_install {
1670 local project_dir=$1
1671 setup_package_with_req_sync $project_dir
1672}
1673
1674# this should be used for projects which run services, like all services
1675function setup_develop {
1676 local project_dir=$1
1677 setup_package_with_req_sync $project_dir -e
1678}
1679
Sean Daguedef15342014-10-27 12:26:04 -04001680# determine if a project as specified by directory is in
1681# projects.txt. This will not be an exact match because we throw away
1682# the namespacing when we clone, but it should be good enough in all
1683# practical ways.
1684function is_in_projects_txt {
1685 local project_dir=$1
1686 local project_name=$(basename $project_dir)
1687 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1688}
1689
Dean Troyeraf616d92014-02-17 12:57:55 -06001690# ``pip install -e`` the package, which processes the dependencies
1691# using pip before running `setup.py develop`
1692#
1693# Updates the dependencies in project_dir from the
1694# openstack/requirements global list before installing anything.
1695#
1696# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1697# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001698function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001699 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001700 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001701
Dean Troyeraf616d92014-02-17 12:57:55 -06001702 # Don't update repo if local changes exist
1703 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001704 # ``errexit`` requires us to trap the exit code when the repo is changed
1705 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001706
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001707 if [[ $update_requirements != "changed" ]]; then
Sean Daguedef15342014-10-27 12:26:04 -04001708 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1709 if is_in_projects_txt $project_dir; then
1710 (cd $REQUIREMENTS_DIR; \
1711 python update.py $project_dir)
1712 else
1713 # soft update projects not found in requirements project.txt
1714 (cd $REQUIREMENTS_DIR; \
1715 python update.py -s $project_dir)
1716 fi
1717 else
1718 (cd $REQUIREMENTS_DIR; \
1719 python update.py $project_dir)
1720 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001721 fi
1722
Sean Dague099e5e32014-03-31 10:35:43 -04001723 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001724
1725 # We've just gone and possibly modified the user's source tree in an
1726 # automated way, which is considered bad form if it's a development
1727 # tree because we've screwed up their next git checkin. So undo it.
1728 #
1729 # However... there are some circumstances, like running in the gate
1730 # where we really really want the overridden version to stick. So provide
1731 # a variable that tells us whether or not we should UNDO the requirements
1732 # changes (this will be set to False in the OpenStack ci gate)
1733 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001734 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001735 (cd $project_dir && git reset --hard)
1736 fi
1737 fi
1738}
1739
1740# ``pip install -e`` the package, which processes the dependencies
1741# using pip before running `setup.py develop`
1742# Uses globals ``STACK_USER``
1743# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001744function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001745 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001746 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001747
Sean Dague099e5e32014-03-31 10:35:43 -04001748 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001749 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001750 if [[ "$flags" == "-e" ]]; then
1751 safe_chown -R $STACK_USER $1/*.egg-info
1752 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001753}
1754
Sean Dague2c65e712014-12-18 09:44:56 -05001755# Plugin Functions
1756# =================
1757
1758DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1759
1760# enable_plugin <name> <url> [branch]
1761#
1762# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1763# ``url`` is a git url
1764# ``branch`` is a gitref. If it's not set, defaults to master
1765function enable_plugin {
1766 local name=$1
1767 local url=$2
1768 local branch=${3:-master}
1769 DEVSTACK_PLUGINS+=",$name"
1770 GITREPO[$name]=$url
1771 GITDIR[$name]=$DEST/$name
1772 GITBRANCH[$name]=$branch
1773}
1774
1775# fetch_plugins
1776#
1777# clones all plugins
1778function fetch_plugins {
1779 local plugins="${DEVSTACK_PLUGINS}"
1780 local plugin
1781
1782 # short circuit if nothing to do
1783 if [[ -z $plugins ]]; then
1784 return
1785 fi
1786
1787 echo "Fetching devstack plugins"
1788 for plugin in ${plugins//,/ }; do
1789 git_clone_by_name $plugin
1790 done
1791}
1792
1793# load_plugin_settings
1794#
1795# Load settings from plugins in the order that they were registered
1796function load_plugin_settings {
1797 local plugins="${DEVSTACK_PLUGINS}"
1798 local plugin
1799
1800 # short circuit if nothing to do
1801 if [[ -z $plugins ]]; then
1802 return
1803 fi
1804
1805 echo "Loading plugin settings"
1806 for plugin in ${plugins//,/ }; do
1807 local dir=${GITDIR[$plugin]}
1808 # source any known settings
1809 if [[ -f $dir/devstack/settings ]]; then
1810 source $dir/devstack/settings
1811 fi
1812 done
1813}
1814
1815# run_plugins
1816#
1817# Run the devstack/plugin.sh in all the plugin directories. These are
1818# run in registration order.
1819function run_plugins {
1820 local mode=$1
1821 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301822
1823 local plugins="${DEVSTACK_PLUGINS}"
1824 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001825 for plugin in ${plugins//,/ }; do
1826 local dir=${GITDIR[$plugin]}
1827 if [[ -f $dir/devstack/plugin.sh ]]; then
1828 source $dir/devstack/plugin.sh $mode $phase
1829 fi
1830 done
1831}
1832
1833function run_phase {
1834 local mode=$1
1835 local phase=$2
1836 if [[ -d $TOP_DIR/extras.d ]]; then
1837 for i in $TOP_DIR/extras.d/*.sh; do
1838 [[ -r $i ]] && source $i $mode $phase
1839 done
1840 fi
1841 # the source phase corresponds to settings loading in plugins
1842 if [[ "$mode" == "source" ]]; then
1843 load_plugin_settings
1844 else
1845 run_plugins $mode $phase
1846 fi
1847}
1848
Dean Troyerdff49a22014-01-30 15:37:40 -06001849
1850# Service Functions
1851# =================
1852
1853# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1854# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001855function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001856 echo "$1" | sed -e '
1857 s/,,/,/g;
1858 s/^,//;
1859 s/,$//
1860 '
1861}
1862
1863# disable_all_services() removes all current services
1864# from ``ENABLED_SERVICES`` to reset the configuration
1865# before a minimal installation
1866# Uses global ``ENABLED_SERVICES``
1867# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001868function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001869 ENABLED_SERVICES=""
1870}
1871
1872# Remove all services starting with '-'. For example, to install all default
1873# services except rabbit (rabbit) set in ``localrc``:
1874# ENABLED_SERVICES+=",-rabbit"
1875# Uses global ``ENABLED_SERVICES``
1876# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001877function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001878 local tmpsvcs="${ENABLED_SERVICES}"
1879 local service
1880 for service in ${tmpsvcs//,/ }; do
1881 if [[ ${service} == -* ]]; then
1882 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1883 fi
1884 done
1885 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1886}
1887
1888# disable_service() removes the services passed as argument to the
1889# ``ENABLED_SERVICES`` list, if they are present.
1890#
1891# For example:
1892# disable_service rabbit
1893#
1894# This function does not know about the special cases
1895# for nova, glance, and neutron built into is_service_enabled().
1896# Uses global ``ENABLED_SERVICES``
1897# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001898function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001899 local tmpsvcs=",${ENABLED_SERVICES},"
1900 local service
1901 for service in $@; do
1902 if is_service_enabled $service; then
1903 tmpsvcs=${tmpsvcs//,$service,/,}
1904 fi
1905 done
1906 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1907}
1908
1909# enable_service() adds the services passed as argument to the
1910# ``ENABLED_SERVICES`` list, if they are not already present.
1911#
1912# For example:
1913# enable_service qpid
1914#
1915# This function does not know about the special cases
1916# for nova, glance, and neutron built into is_service_enabled().
1917# Uses global ``ENABLED_SERVICES``
1918# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001919function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001920 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001921 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001922 for service in $@; do
1923 if ! is_service_enabled $service; then
1924 tmpsvcs+=",$service"
1925 fi
1926 done
1927 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1928 disable_negated_services
1929}
1930
1931# is_service_enabled() checks if the service(s) specified as arguments are
1932# enabled by the user in ``ENABLED_SERVICES``.
1933#
1934# Multiple services specified as arguments are ``OR``'ed together; the test
1935# is a short-circuit boolean, i.e it returns on the first match.
1936#
1937# There are special cases for some 'catch-all' services::
1938# **nova** returns true if any service enabled start with **n-**
1939# **cinder** returns true if any service enabled start with **c-**
1940# **ceilometer** returns true if any service enabled start with **ceilometer**
1941# **glance** returns true if any service enabled start with **g-**
1942# **neutron** returns true if any service enabled start with **q-**
1943# **swift** returns true if any service enabled start with **s-**
1944# **trove** returns true if any service enabled start with **tr-**
1945# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1946# **s-** services will be enabled. This will be deprecated in the future.
1947#
1948# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1949# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1950# as enabled in this case.
1951#
1952# Uses global ``ENABLED_SERVICES``
1953# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001954function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001955 local xtrace=$(set +o | grep xtrace)
1956 set +o xtrace
1957 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001958 local services=$@
1959 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001960 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001961 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001962
1963 # Look for top-level 'enabled' function for this service
1964 if type is_${service}_enabled >/dev/null 2>&1; then
1965 # A function exists for this service, use it
1966 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001967 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001968 fi
1969
1970 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1971 # are implemented
1972
Sean Dague45917cc2014-02-24 16:09:14 -05001973 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001974 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001975 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1976 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1977 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1978 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1979 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1980 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1981 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1982 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1983 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001984 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001985 done
Sean Dague45917cc2014-02-24 16:09:14 -05001986 $xtrace
1987 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001988}
1989
1990# Toggle enable/disable_service for services that must run exclusive of each other
1991# $1 The name of a variable containing a space-separated list of services
1992# $2 The name of a variable in which to store the enabled service's name
1993# $3 The name of the service to enable
1994function use_exclusive_service {
1995 local options=${!1}
1996 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001997 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001998 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001999 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06002000 for opt in $options;do
2001 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
2002 done
2003 eval "$out=$selection"
2004 return 0
2005}
2006
2007
Masayuki Igawaf6368d32014-02-20 13:31:26 +09002008# System Functions
2009# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06002010
2011# Only run the command if the target file (the last arg) is not on an
2012# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002013function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05002014 local xtrace=$(set +o | grep xtrace)
2015 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002016 local args=( $@ )
2017 local last
2018 local sudo_cmd
2019 local dir_to_check
2020
2021 let last="${#args[*]} - 1"
2022
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002023 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002024 if [ ! -d "$dir_to_check" ]; then
2025 dir_to_check=`dirname "$dir_to_check"`
2026 fi
2027
2028 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002029 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002030 return 0
2031 fi
2032
2033 if [[ $TRACK_DEPENDS = True ]]; then
2034 sudo_cmd="env"
2035 else
2036 sudo_cmd="sudo"
2037 fi
2038
Sean Dague45917cc2014-02-24 16:09:14 -05002039 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002040 $sudo_cmd $@
2041}
2042
2043# Exit 0 if address is in network or 1 if address is not in network
2044# ip-range is in CIDR notation: 1.2.3.4/20
2045# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002046function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002047 local ip=$1
2048 local range=$2
2049 local masklen=${range#*/}
2050 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2051 local subnet=$(maskip $ip $(cidr2netmask $masklen))
2052 [[ $network == $subnet ]]
2053}
2054
2055# Add a user to a group.
2056# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002057function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002058 local user=$1
2059 local group=$2
2060
2061 if [[ -z "$os_VENDOR" ]]; then
2062 GetOSVersion
2063 fi
2064
2065 # SLE11 and openSUSE 12.2 don't have the usual usermod
2066 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
2067 sudo usermod -a -G "$group" "$user"
2068 else
2069 sudo usermod -A "$group" "$user"
2070 fi
2071}
2072
2073# Convert CIDR notation to a IPv4 netmask
2074# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002075function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002076 local maskpat="255 255 255 255"
2077 local maskdgt="254 252 248 240 224 192 128"
2078 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2079 echo ${1-0}.${2-0}.${3-0}.${4-0}
2080}
2081
2082# Gracefully cp only if source file/dir exists
2083# cp_it source destination
2084function cp_it {
2085 if [ -e $1 ] || [ -d $1 ]; then
2086 cp -pRL $1 $2
2087 fi
2088}
2089
2090# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2091# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2092# ``localrc`` or on the command line if necessary::
2093#
2094# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2095#
2096# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2097
Ian Wienandaee18c72014-02-21 15:35:08 +11002098function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06002099 if [[ -n "$http_proxy" ]]; then
2100 export http_proxy=$http_proxy
2101 fi
2102 if [[ -n "$https_proxy" ]]; then
2103 export https_proxy=$https_proxy
2104 fi
2105 if [[ -n "$no_proxy" ]]; then
2106 export no_proxy=$no_proxy
2107 fi
2108}
2109
2110# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002111function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06002112 local mount_type=`stat -f -L -c %T $1`
2113 test "$mount_type" == "nfs"
2114}
2115
2116# Return the network portion of the given IP address using netmask
2117# netmask is in the traditional dotted-quad format
2118# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002119function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002120 local ip=$1
2121 local mask=$2
2122 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2123 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2124 echo $subnet
2125}
2126
2127# Service wrapper to restart services
2128# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002129function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002130 if is_ubuntu; then
2131 sudo /usr/sbin/service $1 restart
2132 else
2133 sudo /sbin/service $1 restart
2134 fi
2135}
2136
2137# Only change permissions of a file or directory if it is not on an
2138# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002139function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002140 _safe_permission_operation chmod $@
2141}
2142
2143# Only change ownership of a file or directory if it is not on an NFS
2144# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002145function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002146 _safe_permission_operation chown $@
2147}
2148
2149# Service wrapper to start services
2150# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002151function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002152 if is_ubuntu; then
2153 sudo /usr/sbin/service $1 start
2154 else
2155 sudo /sbin/service $1 start
2156 fi
2157}
2158
2159# Service wrapper to stop services
2160# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002161function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002162 if is_ubuntu; then
2163 sudo /usr/sbin/service $1 stop
2164 else
2165 sudo /sbin/service $1 stop
2166 fi
2167}
2168
2169
2170# Restore xtrace
2171$XTRACE
2172
2173# Local variables:
2174# mode: shell-script
2175# End: