blob: 1c114a156dba8b407f6463281fcd0ec3260a3061 [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#
24# - ``ENABLED_SERVICES``
25# - ``ERROR_ON_CLONE``
26# - ``FILES``
27# - ``OFFLINE``
Dean Troyerdff49a22014-01-30 15:37:40 -060028# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090029# - ``REQUIREMENTS_DIR``
30# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060031# - ``TRACK_DEPENDS``
Masayuki Igawad20f6322014-02-28 09:22:37 +090032# - ``UNDO_REQUIREMENTS``
Dean Troyerdff49a22014-01-30 15:37:40 -060033# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050034#
Dean Troyerdff49a22014-01-30 15:37:40 -060035
36# Save trace setting
37XTRACE=$(set +o | grep xtrace)
38set +o xtrace
39
Sean Daguecc524062014-10-01 09:06:43 -040040# Global Config Variables
41declare -A GITREPO
42declare -A GITBRANCH
43declare -A GITDIR
44
Sean Dague53753292014-12-04 19:38:15 -050045TRACK_DEPENDS=${TRACK_DEPENDS:-False}
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
Sean Dague53753292014-12-04 19:38:15 -0500247 local literal=$2
248 local testval=${!literal}
Dean Troyerdff49a22014-01-30 15:37:40 -0600249
250 [[ -z "$testval" ]] && { echo "$default"; return; }
251 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
252 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
253 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500254 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600255}
256
Sean Dague53753292014-12-04 19:38:15 -0500257function isset {
258 nounset=$(set +o | grep nounset)
259 set +o nounset
260 [[ -n "${!1+x}" ]]
261 result=$?
262 $nounset
263 return $result
264}
Dean Troyerdff49a22014-01-30 15:37:40 -0600265
266# Control Functions
267# =================
268
269# Prints backtrace info
270# filename:lineno:function
271# backtrace level
272function backtrace {
273 local level=$1
274 local deep=$((${#BASH_SOURCE[@]} - 1))
275 echo "[Call Trace]"
276 while [ $level -le $deep ]; do
277 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
278 deep=$((deep - 1))
279 done
280}
281
282# Prints line number and "message" then exits
283# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100284function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600285 local exitcode=$?
286 set +o xtrace
287 local line=$1; shift
288 if [ $exitcode == 0 ]; then
289 exitcode=1
290 fi
291 backtrace 2
292 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600293 # Give buffers a second to flush
294 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600295 exit $exitcode
296}
297
298# Checks an environment variable is not set or has length 0 OR if the
299# exit code is non-zero and prints "message" and exits
300# NOTE: env-var is the variable name without a '$'
301# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100302function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600303 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500304 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600305 set +o xtrace
306 local line=$1; shift
307 local evar=$1; shift
308 if ! is_set $evar || [ $exitcode != 0 ]; then
309 die $line "$*"
310 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500311 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600312}
313
314# Prints line number and "message" in error format
315# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100316function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600317 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500318 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600319 set +o xtrace
320 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
321 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600322 if [[ -n ${LOGDIR} ]]; then
323 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600324 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500325 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600326 return $exitcode
327}
328
329# Checks an environment variable is not set or has length 0 OR if the
330# exit code is non-zero and prints "message"
331# NOTE: env-var is the variable name without a '$'
332# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100333function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600334 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500335 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600336 set +o xtrace
337 local line=$1; shift
338 local evar=$1; shift
339 if ! is_set $evar || [ $exitcode != 0 ]; then
340 err $line "$*"
341 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500342 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600343 return $exitcode
344}
345
346# Exit after outputting a message about the distribution not being supported.
347# exit_distro_not_supported [optional-string-telling-what-is-missing]
348function exit_distro_not_supported {
349 if [[ -z "$DISTRO" ]]; then
350 GetDistro
351 fi
352
353 if [ $# -gt 0 ]; then
354 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
355 else
356 die $LINENO "Support for $DISTRO is incomplete."
357 fi
358}
359
360# Test if the named environment variable is set and not zero length
361# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100362function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600363 local var=\$"$1"
364 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
365}
366
367# Prints line number and "message" in warning format
368# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100369function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600370 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500371 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600372 set +o xtrace
373 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
374 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600375 if [[ -n ${LOGDIR} ]]; then
376 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600377 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500378 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600379 return $exitcode
380}
381
382
383# Distro Functions
384# ================
385
386# Determine OS Vendor, Release and Update
387# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
388# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500389# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
390# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
391# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
392# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
393# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500394os_VENDOR=""
395os_RELEASE=""
396os_UPDATE=""
397os_PACKAGE=""
398os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500399
Dean Troyerdff49a22014-01-30 15:37:40 -0600400# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100401function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500402
Dean Troyerdff49a22014-01-30 15:37:40 -0600403 # Figure out which vendor we are
404 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
405 # OS/X
406 os_VENDOR=`sw_vers -productName`
407 os_RELEASE=`sw_vers -productVersion`
408 os_UPDATE=${os_RELEASE##*.}
409 os_RELEASE=${os_RELEASE%.*}
410 os_PACKAGE=""
411 if [[ "$os_RELEASE" =~ "10.7" ]]; then
412 os_CODENAME="lion"
413 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
414 os_CODENAME="snow leopard"
415 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
416 os_CODENAME="leopard"
417 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
418 os_CODENAME="tiger"
419 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
420 os_CODENAME="panther"
421 else
422 os_CODENAME=""
423 fi
424 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
425 os_VENDOR=$(lsb_release -i -s)
426 os_RELEASE=$(lsb_release -r -s)
427 os_UPDATE=""
428 os_PACKAGE="rpm"
429 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
430 os_PACKAGE="deb"
431 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
432 lsb_release -d -s | grep -q openSUSE
433 if [[ $? -eq 0 ]]; then
434 os_VENDOR="openSUSE"
435 fi
436 elif [[ $os_VENDOR == "openSUSE project" ]]; then
437 os_VENDOR="openSUSE"
438 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
439 os_VENDOR="Red Hat"
440 fi
441 os_CODENAME=$(lsb_release -c -s)
442 elif [[ -r /etc/redhat-release ]]; then
443 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
444 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
445 # CentOS release 5.5 (Final)
446 # CentOS Linux release 6.0 (Final)
447 # Fedora release 16 (Verne)
448 # XenServer release 6.2.0-70446c (xenenterprise)
449 os_CODENAME=""
450 for r in "Red Hat" CentOS Fedora XenServer; do
451 os_VENDOR=$r
452 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
453 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
454 os_CODENAME=${ver#*|}
455 os_RELEASE=${ver%|*}
456 os_UPDATE=${os_RELEASE##*.}
457 os_RELEASE=${os_RELEASE%.*}
458 break
459 fi
460 os_VENDOR=""
461 done
462 os_PACKAGE="rpm"
463 elif [[ -r /etc/SuSE-release ]]; then
464 for r in openSUSE "SUSE Linux"; do
465 if [[ "$r" = "SUSE Linux" ]]; then
466 os_VENDOR="SUSE LINUX"
467 else
468 os_VENDOR=$r
469 fi
470
471 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
472 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
473 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
474 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
475 break
476 fi
477 os_VENDOR=""
478 done
479 os_PACKAGE="rpm"
480 # If lsb_release is not installed, we should be able to detect Debian OS
481 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
482 os_VENDOR="Debian"
483 os_PACKAGE="deb"
484 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
485 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
486 fi
487 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
488}
489
490# Translate the OS version values into common nomenclature
491# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500492declare DISTRO
493
Ian Wienandaee18c72014-02-21 15:35:08 +1100494function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600495 GetOSVersion
496 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
497 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
498 DISTRO=$os_CODENAME
499 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
500 # For Fedora, just use 'f' and the release
501 DISTRO="f$os_RELEASE"
502 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
503 DISTRO="opensuse-$os_RELEASE"
504 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
505 # For SLE, also use the service pack
506 if [[ -z "$os_UPDATE" ]]; then
507 DISTRO="sle${os_RELEASE}"
508 else
509 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
510 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530511 elif [[ "$os_VENDOR" =~ (Red Hat) || \
512 "$os_VENDOR" =~ (CentOS) || \
513 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600514 # Drop the . release as we assume it's compatible
515 DISTRO="rhel${os_RELEASE::1}"
516 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
517 DISTRO="xs$os_RELEASE"
518 else
519 # Catch-all for now is Vendor + Release + Update
520 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
521 fi
522 export DISTRO
523}
524
525# Utility function for checking machine architecture
526# is_arch arch-type
527function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500528 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600529}
530
Ian Wienandbdc90c52014-08-04 15:44:58 +1000531# Quick check for a rackspace host; n.b. rackspace provided images
532# have these Xen tools installed but a custom image may not.
533function is_rackspace {
534 [ -f /usr/bin/xenstore-ls ] && \
535 sudo /usr/bin/xenstore-ls vm-data | grep -q "Rackspace"
536}
537
Dean Troyerdff49a22014-01-30 15:37:40 -0600538# Determine if current distribution is a Fedora-based distribution
539# (Fedora, RHEL, CentOS, etc).
540# is_fedora
541function is_fedora {
542 if [[ -z "$os_VENDOR" ]]; then
543 GetOSVersion
544 fi
545
anju Tiwari6c639c92014-07-15 18:11:54 +0530546 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
547 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600548}
549
550
551# Determine if current distribution is a SUSE-based distribution
552# (openSUSE, SLE).
553# is_suse
554function is_suse {
555 if [[ -z "$os_VENDOR" ]]; then
556 GetOSVersion
557 fi
558
559 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
560}
561
562
563# Determine if current distribution is an Ubuntu-based distribution
564# It will also detect non-Ubuntu but Debian-based distros
565# is_ubuntu
566function is_ubuntu {
567 if [[ -z "$os_PACKAGE" ]]; then
568 GetOSVersion
569 fi
570 [ "$os_PACKAGE" = "deb" ]
571}
572
573
574# Git Functions
575# =============
576
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600577# Returns openstack release name for a given branch name
578# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100579function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600580 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700581 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600582 echo ${branch#*/}
583 else
584 echo "master"
585 fi
586}
587
Dean Troyerdff49a22014-01-30 15:37:40 -0600588# git clone only if directory doesn't exist already. Since ``DEST`` might not
589# be owned by the installation user, we create the directory and change the
590# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500591# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
592# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600593# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500594# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600595# git_clone remote dest-dir branch
596function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500597 local git_remote=$1
598 local git_dest=$2
599 local git_ref=$3
600 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200601 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500602
Sean Dague53753292014-12-04 19:38:15 -0500603 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800604 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200605 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
606 fi
607
Dean Troyerdff49a22014-01-30 15:37:40 -0600608 if [[ "$OFFLINE" = "True" ]]; then
609 echo "Running in offline mode, clones already exist"
610 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500611 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600612 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400613 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600614 return
615 fi
616
Dean Troyer50cda692014-07-25 11:57:20 -0500617 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600618 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500619 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600620 [[ "$ERROR_ON_CLONE" = "True" ]] && \
621 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200622 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600623 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500624 cd $git_dest
625 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600626 else
627 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500628 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600629 [[ "$ERROR_ON_CLONE" = "True" ]] && \
630 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200631 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500632 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600633 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500634 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600635 elif [[ "$RECLONE" = "True" ]]; then
636 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500637 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600638 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500639 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100640 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600641 # remove the existing ignored files (like pyc) as they cause breakage
642 # (due to the py files having older timestamps than our pyc, so python
643 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500644 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600645
Dean Troyer50cda692014-07-25 11:57:20 -0500646 # handle git_ref accordingly to type (tag, branch)
647 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
648 git_update_tag $git_ref
649 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
650 git_update_branch $git_ref
651 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
652 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600653 else
Dean Troyer50cda692014-07-25 11:57:20 -0500654 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600655 fi
656
657 fi
658 fi
659
660 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500661 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600662 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400663 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600664}
665
Sean Daguecc524062014-10-01 09:06:43 -0400666# A variation on git clone that lets us specify a project by it's
667# actual name, like oslo.config. This is exceptionally useful in the
668# library installation case
669function git_clone_by_name {
670 local name=$1
671 local repo=${GITREPO[$name]}
672 local dir=${GITDIR[$name]}
673 local branch=${GITBRANCH[$name]}
674 git_clone $repo $dir $branch
675}
676
677
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100678# git can sometimes get itself infinitely stuck with transient network
679# errors or other issues with the remote end. This wraps git in a
680# timeout/retry loop and is intended to watch over non-local git
681# processes that might hang. GIT_TIMEOUT, if set, is passed directly
682# to timeout(1); otherwise the default value of 0 maintains the status
683# quo of waiting forever.
684# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100685function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100686 local count=0
687 local timeout=0
688
689 if [[ -n "${GIT_TIMEOUT}" ]]; then
690 timeout=${GIT_TIMEOUT}
691 fi
692
693 until timeout -s SIGINT ${timeout} git "$@"; do
694 # 124 is timeout(1)'s special return code when it reached the
695 # timeout; otherwise assume fatal failure
696 if [[ $? -ne 124 ]]; then
697 die $LINENO "git call failed: [git $@]"
698 fi
699
700 count=$(($count + 1))
701 warn "timeout ${count} for git call: [git $@]"
702 if [ $count -eq 3 ]; then
703 die $LINENO "Maximum of 3 git retries reached"
704 fi
705 sleep 5
706 done
707}
708
Dean Troyerdff49a22014-01-30 15:37:40 -0600709# git update using reference as a branch.
710# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100711function git_update_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 -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600715 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500716 git branch -D $git_branch || true
717 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600718}
719
720# git update using reference as a branch.
721# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100722function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500723 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600724
Dean Troyer50cda692014-07-25 11:57:20 -0500725 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600726}
727
728# git update using reference as a tag. Be careful editing source at that repo
729# as working copy will be in a detached mode
730# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100731function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500732 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600733
Dean Troyer50cda692014-07-25 11:57:20 -0500734 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600735 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500736 git_timed fetch origin tag $git_tag
737 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600738}
739
740
741# OpenStack Functions
742# ===================
743
744# Get the default value for HOST_IP
745# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100746function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600747 local fixed_range=$1
748 local floating_range=$2
749 local host_ip_iface=$3
750 local host_ip=$4
751
752 # Find the interface used for the default route
753 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
754 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
755 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
756 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500757 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
758 local ip
759 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600760 # Attempt to filter out IP addresses that are part of the fixed and
761 # floating range. Note that this method only works if the ``netaddr``
762 # python library is installed. If it is not installed, an error
763 # will be printed and the first IP from the interface will be used.
764 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
765 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500766 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
767 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600768 break;
769 fi
770 done
771 fi
772 echo $host_ip
773}
774
Attila Fazekasf71b5002014-05-28 09:52:22 +0200775# Generates hex string from ``size`` byte of pseudo random data
776# generate_hex_string size
777function generate_hex_string {
778 local size=$1
779 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
780}
781
Dean Troyerdff49a22014-01-30 15:37:40 -0600782# Grab a numbered field from python prettytable output
783# Fields are numbered starting with 1
784# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
785# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100786function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500787 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600788 while read data; do
789 if [ "$1" -lt 0 ]; then
790 field="(\$(NF$1))"
791 else
792 field="\$$(($1 + 1))"
793 fi
794 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
795 done
796}
797
798# Add a policy to a policy.json file
799# Do nothing if the policy already exists
800# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100801function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600802 local policy_file=$1
803 local policy_name=$2
804 local policy_perm=$3
805
806 if grep -q ${policy_name} ${policy_file}; then
807 echo "Policy ${policy_name} already exists in ${policy_file}"
808 return
809 fi
810
811 # Add a terminating comma to policy lines without one
812 # Remove the closing '}' and all lines following to the end-of-file
813 local tmpfile=$(mktemp)
814 uniq ${policy_file} | sed -e '
815 s/]$/],/
816 /^[}]/,$d
817 ' > ${tmpfile}
818
819 # Append policy and closing brace
820 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
821 echo "}" >>${tmpfile}
822
823 mv ${tmpfile} ${policy_file}
824}
825
Alistair Coles24779f62014-10-15 18:57:59 +0100826# Gets or creates a domain
827# Usage: get_or_create_domain <name> <description>
828function get_or_create_domain {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500829 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100830 # Gets domain id
831 local domain_id=$(
832 # Gets domain id
833 openstack --os-token=$OS_TOKEN --os-url=$os_url \
834 --os-identity-api-version=3 domain show $1 \
835 -f value -c id 2>/dev/null ||
836 # Creates new domain
837 openstack --os-token=$OS_TOKEN --os-url=$os_url \
838 --os-identity-api-version=3 domain create $1 \
839 --description "$2" \
840 -f value -c id
841 )
842 echo $domain_id
843}
844
Steve Martinellib74e01c2014-12-18 01:35:35 -0500845# Gets or creates group
846# Usage: get_or_create_group <groupname> [<domain> <description>]
847function get_or_create_group {
848 local domain=${2:+--domain ${2}}
849 local desc="${3:-}"
850 local os_url="$KEYSTONE_SERVICE_URI_V3"
851 # Gets group id
852 local group_id=$(
853 # Creates new group with --or-show
854 openstack --os-token=$OS_TOKEN --os-url=$os_url \
855 --os-identity-api-version=3 group create $1 \
856 $domain --description "$desc" --or-show \
857 -f value -c id
858 )
859 echo $group_id
860}
861
Bartosz Górski0abde392014-02-28 14:15:19 +0100862# Gets or creates user
Alistair Coles24779f62014-10-15 18:57:59 +0100863# Usage: get_or_create_user <username> <password> <project> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100864function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200865 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500866 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200867 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500868 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200869 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100870 local os_cmd="openstack"
871 local domain=""
872 if [[ ! -z "$5" ]]; then
873 domain="--domain=$5"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500874 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100875 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100876 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500877 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500878 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100879 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100880 $1 \
881 --password "$2" \
882 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500883 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100884 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500885 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100886 -f value -c id
887 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500888 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100889}
890
891# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100892# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100893function get_or_create_project {
894 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100895 local os_cmd="openstack"
896 local domain=""
897 if [[ ! -z "$2" ]]; then
898 domain="--domain=$2"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500899 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100900 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500901 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500902 # Creates new project with --or-show
903 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100904 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500905 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100906}
907
908# Gets or creates role
909# Usage: get_or_create_role <name>
910function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500911 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500912 # Creates role with --or-show
913 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100914 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500915 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100916}
917
918# Gets or adds user role
919# Usage: get_or_add_user_role <role> <user> <project>
920function get_or_add_user_role {
921 # Gets user role id
Steve Martinelli5541a612015-01-19 15:58:49 -0500922 local user_role_id=$(openstack role list \
923 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100924 --project $3 \
925 --column "ID" \
926 --column "Name" \
927 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500928 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100929 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500930 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100931 $1 \
932 --user $2 \
933 --project $3 \
934 | grep " id " | get_field 2)
935 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500936 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100937}
938
939# Gets or creates service
940# Usage: get_or_create_service <name> <type> <description>
941function get_or_create_service {
942 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500943 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100944 # Gets service id
945 openstack service show $1 -f value -c id 2>/dev/null ||
946 # Creates new service if not exists
947 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500948 $2 \
949 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100950 --description="$3" \
951 -f value -c id
952 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500953 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100954}
955
956# Gets or creates endpoint
957# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
958function get_or_create_endpoint {
959 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500960 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100961 --column "ID" \
962 --column "Region" \
963 --column "Service Name" \
964 | grep " $2 " \
965 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500966 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100967 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500968 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100969 $1 \
970 --region $2 \
971 --publicurl $3 \
972 --adminurl $4 \
973 --internalurl $5 \
974 | grep " id " | get_field 2)
975 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500976 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100977}
Dean Troyerdff49a22014-01-30 15:37:40 -0600978
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500979
Dean Troyerdff49a22014-01-30 15:37:40 -0600980# Package Functions
981# =================
982
983# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100984function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600985 local pkg_dir
986 if is_ubuntu; then
Monty Taylor81a016d2014-11-15 17:18:13 -0300987 pkg_dir=$FILES/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600988 elif is_fedora; then
989 pkg_dir=$FILES/rpms
990 elif is_suse; then
991 pkg_dir=$FILES/rpms-suse
992 else
993 exit_distro_not_supported "list of packages"
994 fi
995 echo "$pkg_dir"
996}
997
998# Wrapper for ``apt-get`` to set cache and proxy environment variables
999# Uses globals ``OFFLINE``, ``*_proxy``
1000# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001001function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -05001002 local xtrace=$(set +o | grep xtrace)
1003 set +o xtrace
1004
Dean Troyerdff49a22014-01-30 15:37:40 -06001005 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
1006 local sudo="sudo"
1007 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -05001008
1009 $xtrace
Sean Dague53753292014-12-04 19:38:15 -05001010
Dean Troyerdff49a22014-01-30 15:37:40 -06001011 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -05001012 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
1013 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -06001014 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
1015}
1016
1017# get_packages() collects a list of package names of any type from the
Monty Taylor81a016d2014-11-15 17:18:13 -03001018# prerequisite files in ``files/{debs|rpms}``. The list is intended
Dean Troyerdff49a22014-01-30 15:37:40 -06001019# to be passed to a package installer such as apt or yum.
1020#
1021# Only packages required for the services in 1st argument will be
1022# included. Two bits of metadata are recognized in the prerequisite files:
1023#
1024# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1025# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1026# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +11001027function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -05001028 local xtrace=$(set +o | grep xtrace)
1029 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001030 local services=$@
1031 local package_dir=$(_get_package_dir)
Sean Dague53753292014-12-04 19:38:15 -05001032 local file_to_parse=""
1033 local service=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001034
Sean Dague53753292014-12-04 19:38:15 -05001035 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
Flavio Percoco5a91c352014-10-31 18:48:00 +01001036
Dean Troyerdff49a22014-01-30 15:37:40 -06001037 if [[ -z "$package_dir" ]]; then
1038 echo "No package directory supplied"
1039 return 1
1040 fi
1041 if [[ -z "$DISTRO" ]]; then
1042 GetDistro
1043 fi
1044 for service in ${services//,/ }; do
1045 # Allow individual services to specify dependencies
1046 if [[ -e ${package_dir}/${service} ]]; then
1047 file_to_parse="${file_to_parse} $service"
1048 fi
1049 # NOTE(sdague) n-api needs glance for now because that's where
1050 # glance client is
1051 if [[ $service == n-api ]]; then
1052 if [[ ! $file_to_parse =~ nova ]]; then
1053 file_to_parse="${file_to_parse} nova"
1054 fi
1055 if [[ ! $file_to_parse =~ glance ]]; then
1056 file_to_parse="${file_to_parse} glance"
1057 fi
1058 elif [[ $service == c-* ]]; then
1059 if [[ ! $file_to_parse =~ cinder ]]; then
1060 file_to_parse="${file_to_parse} cinder"
1061 fi
1062 elif [[ $service == ceilometer-* ]]; then
1063 if [[ ! $file_to_parse =~ ceilometer ]]; then
1064 file_to_parse="${file_to_parse} ceilometer"
1065 fi
1066 elif [[ $service == s-* ]]; then
1067 if [[ ! $file_to_parse =~ swift ]]; then
1068 file_to_parse="${file_to_parse} swift"
1069 fi
1070 elif [[ $service == n-* ]]; then
1071 if [[ ! $file_to_parse =~ nova ]]; then
1072 file_to_parse="${file_to_parse} nova"
1073 fi
1074 elif [[ $service == g-* ]]; then
1075 if [[ ! $file_to_parse =~ glance ]]; then
1076 file_to_parse="${file_to_parse} glance"
1077 fi
1078 elif [[ $service == key* ]]; then
1079 if [[ ! $file_to_parse =~ keystone ]]; then
1080 file_to_parse="${file_to_parse} keystone"
1081 fi
1082 elif [[ $service == q-* ]]; then
1083 if [[ ! $file_to_parse =~ neutron ]]; then
1084 file_to_parse="${file_to_parse} neutron"
1085 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001086 elif [[ $service == ir-* ]]; then
1087 if [[ ! $file_to_parse =~ ironic ]]; then
1088 file_to_parse="${file_to_parse} ironic"
1089 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001090 fi
1091 done
1092
1093 for file in ${file_to_parse}; do
1094 local fname=${package_dir}/${file}
1095 local OIFS line package distros distro
1096 [[ -e $fname ]] || continue
1097
1098 OIFS=$IFS
1099 IFS=$'\n'
1100 for line in $(<${fname}); do
1101 if [[ $line =~ "NOPRIME" ]]; then
1102 continue
1103 fi
1104
1105 # Assume we want this package
1106 package=${line%#*}
1107 inst_pkg=1
1108
1109 # Look for # dist:xxx in comment
1110 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1111 # We are using BASH regexp matching feature.
1112 package=${BASH_REMATCH[1]}
1113 distros=${BASH_REMATCH[2]}
1114 # In bash ${VAR,,} will lowecase VAR
1115 # Look for a match in the distro list
1116 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1117 # If no match then skip this package
1118 inst_pkg=0
1119 fi
1120 fi
1121
1122 # Look for # testonly in comment
1123 if [[ $line =~ (.*)#.*testonly.* ]]; then
1124 package=${BASH_REMATCH[1]}
1125 # Are we installing test packages? (test for the default value)
1126 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1127 # If not installing test packages the skip this package
1128 inst_pkg=0
1129 fi
1130 fi
1131
1132 if [[ $inst_pkg = 1 ]]; then
1133 echo $package
1134 fi
1135 done
1136 IFS=$OIFS
1137 done
Sean Dague45917cc2014-02-24 16:09:14 -05001138 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001139}
1140
1141# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001142# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001143# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001144function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001145 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1146 REPOS_UPDATED=${REPOS_UPDATED:-False}
1147 RETRY_UPDATE=${RETRY_UPDATE:-False}
1148
Paul Linchpiner9e179742014-07-13 22:23:00 -07001149 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001150 return 0
1151 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001152
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001153 if is_ubuntu; then
1154 local xtrace=$(set +o | grep xtrace)
1155 set +o xtrace
1156 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1157 # if there are transient errors pulling the updates, that's fine.
1158 # It may be secondary repositories that we don't really care about.
1159 apt_get update || /bin/true
1160 REPOS_UPDATED=True
1161 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001162 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001163 fi
1164}
1165
1166function real_install_package {
1167 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001168 apt_get install "$@"
1169 elif is_fedora; then
1170 yum_install "$@"
1171 elif is_suse; then
1172 zypper_install "$@"
1173 else
1174 exit_distro_not_supported "installing packages"
1175 fi
1176}
1177
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001178# Distro-agnostic package installer
1179# install_package package [package ...]
1180function install_package {
1181 update_package_repo
1182 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1183}
1184
Dean Troyerdff49a22014-01-30 15:37:40 -06001185# Distro-agnostic function to tell if a package is installed
1186# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001187function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001188 if [[ -z "$@" ]]; then
1189 return 1
1190 fi
1191
1192 if [[ -z "$os_PACKAGE" ]]; then
1193 GetOSVersion
1194 fi
1195
1196 if [[ "$os_PACKAGE" = "deb" ]]; then
1197 dpkg -s "$@" > /dev/null 2> /dev/null
1198 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1199 rpm --quiet -q "$@"
1200 else
1201 exit_distro_not_supported "finding if a package is installed"
1202 fi
1203}
1204
1205# Distro-agnostic package uninstaller
1206# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001207function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001208 if is_ubuntu; then
1209 apt_get purge "$@"
1210 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001211 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001212 elif is_suse; then
1213 sudo zypper rm "$@"
1214 else
1215 exit_distro_not_supported "uninstalling packages"
1216 fi
1217}
1218
1219# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001220# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001221# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001222function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001223 [[ "$OFFLINE" = "True" ]] && return
1224 local sudo="sudo"
1225 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001226
1227 # The manual check for missing packages is because yum -y assumes
1228 # missing packages are OK. See
1229 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001230 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1231 no_proxy=$no_proxy \
Ian Wienand36298ee2015-02-04 10:29:31 +11001232 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001233 awk '
1234 BEGIN { fail=0 }
1235 /No package/ { fail=1 }
1236 { print }
1237 END { exit fail }' || \
1238 die $LINENO "Missing packages detected"
1239
1240 # also ensure we catch a yum failure
1241 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001242 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001243 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001244}
1245
1246# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001247# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001248# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001249function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001250 [[ "$OFFLINE" = "True" ]] && return
1251 local sudo="sudo"
1252 [[ "$(id -u)" = "0" ]] && sudo="env"
1253 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1254 zypper --non-interactive install --auto-agree-with-licenses "$@"
1255}
1256
1257
1258# Process Functions
1259# =================
1260
1261# _run_process() is designed to be backgrounded by run_process() to simulate a
1262# fork. It includes the dirty work of closing extra filehandles and preparing log
1263# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001264# from the service name.
1265# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001266# If an optional group is provided sg will be used to set the group of
1267# the command.
1268# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001269function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001270 local service=$1
1271 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001272 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001273
1274 # Undo logging redirections and close the extra descriptors
1275 exec 1>&3
1276 exec 2>&3
1277 exec 3>&-
1278 exec 6>&-
1279
Dean Troyerdde41d02014-12-09 17:47:57 -06001280 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1281 if [[ -n ${LOGDIR} ]]; then
1282 exec 1>&"$real_logfile" 2>&1
1283 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1284 if [[ -n ${SCREEN_LOGDIR} ]]; then
1285 # Drop the backward-compat symlink
1286 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1287 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001288
1289 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1290 export PYTHONUNBUFFERED=1
1291 fi
1292
Dean Troyer3159a822014-08-27 14:13:58 -05001293 # Run under ``setsid`` to force the process to become a session and group leader.
1294 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001295 if [[ -n "$group" ]]; then
1296 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1297 else
1298 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1299 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001300
1301 # Just silently exit this process
1302 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001303}
1304
1305# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1306# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001307# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001308# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001309function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001310 SCREEN_NAME=${SCREEN_NAME:-stack}
1311 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1312
1313 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1314 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1315 fi
1316
1317 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1318}
1319
1320# Find out if a process exists by partial name.
1321# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001322function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001323 local name=$1
1324 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001325 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001326 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001327 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001328}
1329
Dean Troyer3159a822014-08-27 14:13:58 -05001330# Run a single service under screen or directly
1331# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001332# If an optional group is provided sg will be used to run the
1333# command as that group.
1334# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001335function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001336 local service=$1
1337 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001338 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001339
Dean Troyer3159a822014-08-27 14:13:58 -05001340 if is_service_enabled $service; then
1341 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001342 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001343 else
1344 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001345 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001346 fi
1347 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001348}
1349
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001350# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001351# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001352# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001353# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001354# Run a command in a shell in a screen window, if an optional group
1355# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001356function screen_process {
1357 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001358 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001359 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001360
Sean Dagueea22a4f2014-06-27 15:21:41 -04001361 SCREEN_NAME=${SCREEN_NAME:-stack}
1362 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001363 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001364
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001365 # Append the process to the screen rc file
1366 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001367
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001368 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001369
Dean Troyerdde41d02014-12-09 17:47:57 -06001370 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1371 echo "LOGDIR: $LOGDIR"
1372 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1373 echo "log: $real_logfile"
1374 if [[ -n ${LOGDIR} ]]; then
1375 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001376 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001377 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1378 if [[ -n ${SCREEN_LOGDIR} ]]; then
1379 # Drop the backward-compat symlink
1380 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1381 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001382 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001383
1384 # sleep to allow bash to be ready to be send the command - we are
1385 # creating a new window in screen and then sends characters, so if
1386 # bash isn't running by the time we send the command, nothing happens
1387 sleep 3
1388
1389 NL=`echo -ne '\015'`
1390 # This fun command does the following:
1391 # - the passed server command is backgrounded
1392 # - the pid of the background process is saved in the usual place
1393 # - the server process is brought back to the foreground
1394 # - if the server process exits prematurely the fg command errors
1395 # and a message is written to stdout and the process failure file
1396 #
1397 # The pid saved can be used in stop_process() as a process group
1398 # id to kill off all child processes
1399 if [[ -n "$group" ]]; then
1400 command="sg $group '$command'"
1401 fi
1402 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 -06001403}
1404
1405# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001406# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001407# screen_rc service "command-line"
1408function screen_rc {
1409 SCREEN_NAME=${SCREEN_NAME:-stack}
1410 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1411 if [[ ! -e $SCREENRC ]]; then
1412 # Name the screen session
1413 echo "sessionname $SCREEN_NAME" > $SCREENRC
1414 # Set a reasonable statusbar
1415 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1416 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1417 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1418 echo "screen -t shell bash" >> $SCREENRC
1419 fi
1420 # If this service doesn't already exist in the screenrc file
1421 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1422 NL=`echo -ne '\015'`
1423 echo "screen -t $1 bash" >> $SCREENRC
1424 echo "stuff \"$2$NL\"" >> $SCREENRC
1425
Dean Troyerdde41d02014-12-09 17:47:57 -06001426 if [[ -n ${LOGDIR} ]]; then
1427 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001428 echo "log on" >>$SCREENRC
1429 fi
1430 fi
1431}
1432
1433# Stop a service in screen
1434# If a PID is available use it, kill the whole process group via TERM
1435# If screen is being used kill the screen window; this will catch processes
1436# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001437# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001438# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001439function screen_stop_service {
1440 local service=$1
1441
Dean Troyerdff49a22014-01-30 15:37:40 -06001442 SCREEN_NAME=${SCREEN_NAME:-stack}
1443 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001444 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001445
Dean Troyer3159a822014-08-27 14:13:58 -05001446 if is_service_enabled $service; then
1447 # Clean up the screen window
1448 screen -S $SCREEN_NAME -p $service -X kill
1449 fi
1450}
1451
1452# Stop a service process
1453# If a PID is available use it, kill the whole process group via TERM
1454# If screen is being used kill the screen window; this will catch processes
1455# that did not leave a PID behind
1456# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1457# stop_process service
1458function stop_process {
1459 local service=$1
1460
1461 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001462 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001463
1464 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001465 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001466 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1467 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1468 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001469 fi
1470 if [[ "$USE_SCREEN" = "True" ]]; then
1471 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001472 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001473 fi
1474 fi
1475}
1476
1477# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001478# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001479# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001480function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001481 local service
1482 local failures
1483 SCREEN_NAME=${SCREEN_NAME:-stack}
1484 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1485
1486
1487 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1488 echo "No service status directory found"
1489 return
1490 fi
1491
1492 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001493 # make this -o errexit safe
1494 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001495
1496 for service in $failures; do
1497 service=`basename $service`
1498 service=${service%.failure}
1499 echo "Error: Service $service is not running"
1500 done
1501
1502 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001503 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001504 fi
1505}
1506
Chris Dent2f27a0e2014-09-09 13:46:02 +01001507# Tail a log file in a screen if USE_SCREEN is true.
1508function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001509 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001510 local logfile=$2
1511
Sean Dague53753292014-12-04 19:38:15 -05001512 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001513 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001514 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001515 fi
1516}
1517
Dean Troyerdff49a22014-01-30 15:37:40 -06001518
Dean Troyer3159a822014-08-27 14:13:58 -05001519# Deprecated Functions
1520# --------------------
1521
1522# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1523# fork. It includes the dirty work of closing extra filehandles and preparing log
1524# files to produce the same logs as screen_it(). The log filename is derived
1525# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1526# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1527# _old_run_process service "command-line"
1528function _old_run_process {
1529 local service=$1
1530 local command="$2"
1531
1532 # Undo logging redirections and close the extra descriptors
1533 exec 1>&3
1534 exec 2>&3
1535 exec 3>&-
1536 exec 6>&-
1537
1538 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001539 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1540 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001541
1542 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1543 export PYTHONUNBUFFERED=1
1544 fi
1545
1546 exec /bin/bash -c "$command"
1547 die "$service exec failure: $command"
1548}
1549
1550# old_run_process() launches a child process that closes all file descriptors and
1551# then exec's the passed in command. This is meant to duplicate the semantics
1552# of screen_it() without screen. PIDs are written to
1553# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1554# old_run_process service "command-line"
1555function old_run_process {
1556 local service=$1
1557 local command="$2"
1558
1559 # Spawn the child process
1560 _old_run_process "$service" "$command" &
1561 echo $!
1562}
1563
1564# Compatibility for existing start_XXXX() functions
1565# Uses global ``USE_SCREEN``
1566# screen_it service "command-line"
1567function screen_it {
1568 if is_service_enabled $1; then
1569 # Append the service to the screen rc file
1570 screen_rc "$1" "$2"
1571
1572 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001573 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001574 else
1575 # Spawn directly without screen
1576 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1577 fi
1578 fi
1579}
1580
1581# Compatibility for existing stop_XXXX() functions
1582# Stop a service in screen
1583# If a PID is available use it, kill the whole process group via TERM
1584# If screen is being used kill the screen window; this will catch processes
1585# that did not leave a PID behind
1586# screen_stop service
1587function screen_stop {
1588 # Clean up the screen window
1589 stop_process $1
1590}
1591
1592
Dean Troyerdff49a22014-01-30 15:37:40 -06001593# Python Functions
1594# ================
1595
1596# Get the path to the pip command.
1597# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001598function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001599 which pip || which pip-python
1600
1601 if [ $? -ne 0 ]; then
1602 die $LINENO "Unable to find pip; cannot continue"
1603 fi
1604}
1605
1606# Get the path to the direcotry where python executables are installed.
1607# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001608function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001609 if is_fedora || is_suse; then
1610 echo "/usr/bin"
1611 else
1612 echo "/usr/local/bin"
1613 fi
1614}
1615
1616# Wrapper for ``pip install`` to set cache and proxy environment variables
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001617# Uses globals ``OFFLINE``, ``TRACK_DEPENDS``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001618# pip_install package [package ...]
1619function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001620 local xtrace=$(set +o | grep xtrace)
1621 set +o xtrace
Sean Dague53753292014-12-04 19:38:15 -05001622 local offline=${OFFLINE:-False}
1623 if [[ "$offline" == "True" || -z "$@" ]]; then
Sean Dague45917cc2014-02-24 16:09:14 -05001624 $xtrace
1625 return
1626 fi
1627
Dean Troyerdff49a22014-01-30 15:37:40 -06001628 if [[ -z "$os_PACKAGE" ]]; then
1629 GetOSVersion
1630 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001631 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1632 # TRACK_DEPENDS=True installation creates a circular dependency when
1633 # we attempt to install virtualenv into a virualenv, so we must global
1634 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001635 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001636 local cmd_pip=$DEST/.venv/bin/pip
1637 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001638 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001639 local cmd_pip=$(get_pip_command)
Jeremy Stanley6ec66bb2014-12-22 17:17:51 +00001640 local sudo_pip="sudo -H"
Dean Troyerdff49a22014-01-30 15:37:40 -06001641 fi
1642
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001643 local pip_version=$(python -c "import pip; \
1644 print(pip.__version__.strip('.')[0])")
1645 if (( pip_version<6 )); then
1646 die $LINENO "Currently installed pip version ${pip_version} does not" \
1647 "meet minimum requirements (>=6)."
1648 fi
1649
Sean Dague45917cc2014-02-24 16:09:14 -05001650 $xtrace
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001651 $sudo_pip \
Sean Dague53753292014-12-04 19:38:15 -05001652 http_proxy=${http_proxy:-} \
1653 https_proxy=${https_proxy:-} \
1654 no_proxy=${no_proxy:-} \
Sean Daguec53e8362014-09-30 22:37:52 -04001655 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001656 $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001657
Sean Dague53753292014-12-04 19:38:15 -05001658 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001659 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1660 local test_req="$@/test-requirements.txt"
1661 if [[ -e "$test_req" ]]; then
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001662 $sudo_pip \
Sean Dague53753292014-12-04 19:38:15 -05001663 http_proxy=${http_proxy:-} \
1664 https_proxy=${https_proxy:-} \
1665 no_proxy=${no_proxy:-} \
Sean Daguec53e8362014-09-30 22:37:52 -04001666 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001667 -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001668 fi
1669 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001670}
1671
Sean Daguecc524062014-10-01 09:06:43 -04001672# should we use this library from their git repo, or should we let it
1673# get pulled in via pip dependencies.
1674function use_library_from_git {
1675 local name=$1
1676 local enabled=1
1677 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1678 return $enabled
1679}
1680
1681# setup a library by name. If we are trying to use the library from
1682# git, we'll do a git based install, otherwise we'll punt and the
1683# library should be installed by a requirements pull from another
1684# project.
1685function setup_lib {
1686 local name=$1
1687 local dir=${GITDIR[$name]}
1688 setup_install $dir
1689}
1690
Sean Daguee08ab102014-11-13 17:09:28 -05001691# setup a library by name in editiable mode. If we are trying to use
1692# the library from git, we'll do a git based install, otherwise we'll
1693# punt and the library should be installed by a requirements pull from
1694# another project.
1695#
1696# use this for non namespaced libraries
1697function setup_dev_lib {
1698 local name=$1
1699 local dir=${GITDIR[$name]}
1700 setup_develop $dir
1701}
Sean Daguecc524062014-10-01 09:06:43 -04001702
Sean Dague099e5e32014-03-31 10:35:43 -04001703# this should be used if you want to install globally, all libraries should
1704# use this, especially *oslo* ones
1705function setup_install {
1706 local project_dir=$1
1707 setup_package_with_req_sync $project_dir
1708}
1709
1710# this should be used for projects which run services, like all services
1711function setup_develop {
1712 local project_dir=$1
1713 setup_package_with_req_sync $project_dir -e
1714}
1715
Sean Daguedef15342014-10-27 12:26:04 -04001716# determine if a project as specified by directory is in
1717# projects.txt. This will not be an exact match because we throw away
1718# the namespacing when we clone, but it should be good enough in all
1719# practical ways.
1720function is_in_projects_txt {
1721 local project_dir=$1
1722 local project_name=$(basename $project_dir)
1723 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1724}
1725
Dean Troyeraf616d92014-02-17 12:57:55 -06001726# ``pip install -e`` the package, which processes the dependencies
1727# using pip before running `setup.py develop`
1728#
1729# Updates the dependencies in project_dir from the
1730# openstack/requirements global list before installing anything.
1731#
1732# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1733# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001734function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001735 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001736 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001737
Dean Troyeraf616d92014-02-17 12:57:55 -06001738 # Don't update repo if local changes exist
1739 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001740 # ``errexit`` requires us to trap the exit code when the repo is changed
1741 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001742
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001743 if [[ $update_requirements != "changed" ]]; then
Sean Daguedef15342014-10-27 12:26:04 -04001744 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1745 if is_in_projects_txt $project_dir; then
1746 (cd $REQUIREMENTS_DIR; \
1747 python update.py $project_dir)
1748 else
1749 # soft update projects not found in requirements project.txt
1750 (cd $REQUIREMENTS_DIR; \
1751 python update.py -s $project_dir)
1752 fi
1753 else
1754 (cd $REQUIREMENTS_DIR; \
1755 python update.py $project_dir)
1756 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001757 fi
1758
Sean Dague099e5e32014-03-31 10:35:43 -04001759 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001760
1761 # We've just gone and possibly modified the user's source tree in an
1762 # automated way, which is considered bad form if it's a development
1763 # tree because we've screwed up their next git checkin. So undo it.
1764 #
1765 # However... there are some circumstances, like running in the gate
1766 # where we really really want the overridden version to stick. So provide
1767 # a variable that tells us whether or not we should UNDO the requirements
1768 # changes (this will be set to False in the OpenStack ci gate)
1769 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001770 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001771 (cd $project_dir && git reset --hard)
1772 fi
1773 fi
1774}
1775
1776# ``pip install -e`` the package, which processes the dependencies
1777# using pip before running `setup.py develop`
1778# Uses globals ``STACK_USER``
1779# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001780function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001781 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001782 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001783
Sean Dague099e5e32014-03-31 10:35:43 -04001784 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001785 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001786 if [[ "$flags" == "-e" ]]; then
1787 safe_chown -R $STACK_USER $1/*.egg-info
1788 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001789}
1790
Sean Dague2c65e712014-12-18 09:44:56 -05001791# Plugin Functions
1792# =================
1793
1794DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1795
1796# enable_plugin <name> <url> [branch]
1797#
1798# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1799# ``url`` is a git url
1800# ``branch`` is a gitref. If it's not set, defaults to master
1801function enable_plugin {
1802 local name=$1
1803 local url=$2
1804 local branch=${3:-master}
1805 DEVSTACK_PLUGINS+=",$name"
1806 GITREPO[$name]=$url
1807 GITDIR[$name]=$DEST/$name
1808 GITBRANCH[$name]=$branch
1809}
1810
1811# fetch_plugins
1812#
1813# clones all plugins
1814function fetch_plugins {
1815 local plugins="${DEVSTACK_PLUGINS}"
1816 local plugin
1817
1818 # short circuit if nothing to do
1819 if [[ -z $plugins ]]; then
1820 return
1821 fi
1822
1823 echo "Fetching devstack plugins"
1824 for plugin in ${plugins//,/ }; do
1825 git_clone_by_name $plugin
1826 done
1827}
1828
1829# load_plugin_settings
1830#
1831# Load settings from plugins in the order that they were registered
1832function load_plugin_settings {
1833 local plugins="${DEVSTACK_PLUGINS}"
1834 local plugin
1835
1836 # short circuit if nothing to do
1837 if [[ -z $plugins ]]; then
1838 return
1839 fi
1840
1841 echo "Loading plugin settings"
1842 for plugin in ${plugins//,/ }; do
1843 local dir=${GITDIR[$plugin]}
1844 # source any known settings
1845 if [[ -f $dir/devstack/settings ]]; then
1846 source $dir/devstack/settings
1847 fi
1848 done
1849}
1850
1851# run_plugins
1852#
1853# Run the devstack/plugin.sh in all the plugin directories. These are
1854# run in registration order.
1855function run_plugins {
1856 local mode=$1
1857 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301858
1859 local plugins="${DEVSTACK_PLUGINS}"
1860 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001861 for plugin in ${plugins//,/ }; do
1862 local dir=${GITDIR[$plugin]}
1863 if [[ -f $dir/devstack/plugin.sh ]]; then
1864 source $dir/devstack/plugin.sh $mode $phase
1865 fi
1866 done
1867}
1868
1869function run_phase {
1870 local mode=$1
1871 local phase=$2
1872 if [[ -d $TOP_DIR/extras.d ]]; then
1873 for i in $TOP_DIR/extras.d/*.sh; do
1874 [[ -r $i ]] && source $i $mode $phase
1875 done
1876 fi
1877 # the source phase corresponds to settings loading in plugins
1878 if [[ "$mode" == "source" ]]; then
1879 load_plugin_settings
1880 else
1881 run_plugins $mode $phase
1882 fi
1883}
1884
Dean Troyerdff49a22014-01-30 15:37:40 -06001885
1886# Service Functions
1887# =================
1888
1889# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1890# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001891function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001892 echo "$1" | sed -e '
1893 s/,,/,/g;
1894 s/^,//;
1895 s/,$//
1896 '
1897}
1898
1899# disable_all_services() removes all current services
1900# from ``ENABLED_SERVICES`` to reset the configuration
1901# before a minimal installation
1902# Uses global ``ENABLED_SERVICES``
1903# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001904function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001905 ENABLED_SERVICES=""
1906}
1907
1908# Remove all services starting with '-'. For example, to install all default
1909# services except rabbit (rabbit) set in ``localrc``:
1910# ENABLED_SERVICES+=",-rabbit"
1911# Uses global ``ENABLED_SERVICES``
1912# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001913function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001914 local tmpsvcs="${ENABLED_SERVICES}"
1915 local service
1916 for service in ${tmpsvcs//,/ }; do
1917 if [[ ${service} == -* ]]; then
1918 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1919 fi
1920 done
1921 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1922}
1923
1924# disable_service() removes the services passed as argument to the
1925# ``ENABLED_SERVICES`` list, if they are present.
1926#
1927# For example:
1928# disable_service rabbit
1929#
1930# This function does not know about the special cases
1931# for nova, glance, and neutron built into is_service_enabled().
1932# Uses global ``ENABLED_SERVICES``
1933# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001934function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001935 local tmpsvcs=",${ENABLED_SERVICES},"
1936 local service
1937 for service in $@; do
1938 if is_service_enabled $service; then
1939 tmpsvcs=${tmpsvcs//,$service,/,}
1940 fi
1941 done
1942 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1943}
1944
1945# enable_service() adds the services passed as argument to the
1946# ``ENABLED_SERVICES`` list, if they are not already present.
1947#
1948# For example:
1949# enable_service qpid
1950#
1951# This function does not know about the special cases
1952# for nova, glance, and neutron built into is_service_enabled().
1953# Uses global ``ENABLED_SERVICES``
1954# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001955function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001956 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001957 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001958 for service in $@; do
1959 if ! is_service_enabled $service; then
1960 tmpsvcs+=",$service"
1961 fi
1962 done
1963 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1964 disable_negated_services
1965}
1966
1967# is_service_enabled() checks if the service(s) specified as arguments are
1968# enabled by the user in ``ENABLED_SERVICES``.
1969#
1970# Multiple services specified as arguments are ``OR``'ed together; the test
1971# is a short-circuit boolean, i.e it returns on the first match.
1972#
1973# There are special cases for some 'catch-all' services::
1974# **nova** returns true if any service enabled start with **n-**
1975# **cinder** returns true if any service enabled start with **c-**
1976# **ceilometer** returns true if any service enabled start with **ceilometer**
1977# **glance** returns true if any service enabled start with **g-**
1978# **neutron** returns true if any service enabled start with **q-**
1979# **swift** returns true if any service enabled start with **s-**
1980# **trove** returns true if any service enabled start with **tr-**
1981# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1982# **s-** services will be enabled. This will be deprecated in the future.
1983#
1984# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1985# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1986# as enabled in this case.
1987#
1988# Uses global ``ENABLED_SERVICES``
1989# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001990function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001991 local xtrace=$(set +o | grep xtrace)
1992 set +o xtrace
1993 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001994 local services=$@
1995 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001996 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001997 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001998
1999 # Look for top-level 'enabled' function for this service
2000 if type is_${service}_enabled >/dev/null 2>&1; then
2001 # A function exists for this service, use it
2002 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05002003 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06002004 fi
2005
2006 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
2007 # are implemented
2008
Sean Dague45917cc2014-02-24 16:09:14 -05002009 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01002010 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05002011 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
2012 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
2013 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
2014 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
2015 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
2016 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
2017 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
2018 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
2019 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05002020 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002021 done
Sean Dague45917cc2014-02-24 16:09:14 -05002022 $xtrace
2023 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06002024}
2025
2026# Toggle enable/disable_service for services that must run exclusive of each other
2027# $1 The name of a variable containing a space-separated list of services
2028# $2 The name of a variable in which to store the enabled service's name
2029# $3 The name of the service to enable
2030function use_exclusive_service {
2031 local options=${!1}
2032 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002033 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06002034 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002035 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06002036 for opt in $options;do
2037 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
2038 done
2039 eval "$out=$selection"
2040 return 0
2041}
2042
2043
Masayuki Igawaf6368d32014-02-20 13:31:26 +09002044# System Functions
2045# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06002046
2047# Only run the command if the target file (the last arg) is not on an
2048# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002049function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05002050 local xtrace=$(set +o | grep xtrace)
2051 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002052 local args=( $@ )
2053 local last
2054 local sudo_cmd
2055 local dir_to_check
2056
2057 let last="${#args[*]} - 1"
2058
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002059 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002060 if [ ! -d "$dir_to_check" ]; then
2061 dir_to_check=`dirname "$dir_to_check"`
2062 fi
2063
2064 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002065 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002066 return 0
2067 fi
2068
2069 if [[ $TRACK_DEPENDS = True ]]; then
2070 sudo_cmd="env"
2071 else
2072 sudo_cmd="sudo"
2073 fi
2074
Sean Dague45917cc2014-02-24 16:09:14 -05002075 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002076 $sudo_cmd $@
2077}
2078
2079# Exit 0 if address is in network or 1 if address is not in network
2080# ip-range is in CIDR notation: 1.2.3.4/20
2081# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002082function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002083 local ip=$1
2084 local range=$2
2085 local masklen=${range#*/}
2086 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2087 local subnet=$(maskip $ip $(cidr2netmask $masklen))
2088 [[ $network == $subnet ]]
2089}
2090
2091# Add a user to a group.
2092# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002093function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002094 local user=$1
2095 local group=$2
2096
2097 if [[ -z "$os_VENDOR" ]]; then
2098 GetOSVersion
2099 fi
2100
2101 # SLE11 and openSUSE 12.2 don't have the usual usermod
2102 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
2103 sudo usermod -a -G "$group" "$user"
2104 else
2105 sudo usermod -A "$group" "$user"
2106 fi
2107}
2108
2109# Convert CIDR notation to a IPv4 netmask
2110# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002111function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002112 local maskpat="255 255 255 255"
2113 local maskdgt="254 252 248 240 224 192 128"
2114 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2115 echo ${1-0}.${2-0}.${3-0}.${4-0}
2116}
2117
2118# Gracefully cp only if source file/dir exists
2119# cp_it source destination
2120function cp_it {
2121 if [ -e $1 ] || [ -d $1 ]; then
2122 cp -pRL $1 $2
2123 fi
2124}
2125
2126# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2127# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2128# ``localrc`` or on the command line if necessary::
2129#
2130# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2131#
2132# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2133
Ian Wienandaee18c72014-02-21 15:35:08 +11002134function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05002135 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002136 export http_proxy=$http_proxy
2137 fi
Sean Dague53753292014-12-04 19:38:15 -05002138 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002139 export https_proxy=$https_proxy
2140 fi
Sean Dague53753292014-12-04 19:38:15 -05002141 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002142 export no_proxy=$no_proxy
2143 fi
2144}
2145
2146# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002147function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06002148 local mount_type=`stat -f -L -c %T $1`
2149 test "$mount_type" == "nfs"
2150}
2151
2152# Return the network portion of the given IP address using netmask
2153# netmask is in the traditional dotted-quad format
2154# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002155function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002156 local ip=$1
2157 local mask=$2
2158 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2159 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2160 echo $subnet
2161}
2162
2163# Service wrapper to restart services
2164# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002165function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002166 if is_ubuntu; then
2167 sudo /usr/sbin/service $1 restart
2168 else
2169 sudo /sbin/service $1 restart
2170 fi
2171}
2172
2173# Only change permissions of a file or directory if it is not on an
2174# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002175function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002176 _safe_permission_operation chmod $@
2177}
2178
2179# Only change ownership of a file or directory if it is not on an NFS
2180# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002181function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002182 _safe_permission_operation chown $@
2183}
2184
2185# Service wrapper to start services
2186# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002187function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002188 if is_ubuntu; then
2189 sudo /usr/sbin/service $1 start
2190 else
2191 sudo /sbin/service $1 start
2192 fi
2193}
2194
2195# Service wrapper to stop services
2196# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002197function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002198 if is_ubuntu; then
2199 sudo /usr/sbin/service $1 stop
2200 else
2201 sudo /sbin/service $1 stop
2202 fi
2203}
2204
2205
2206# Restore xtrace
2207$XTRACE
2208
2209# Local variables:
2210# mode: shell-script
2211# End: