blob: 44d56712367174d03fd007e17568fa5ffd54607e [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;
322 if [[ -n ${SCREEN_LOGDIR} ]]; then
323 echo $msg >> "${SCREEN_LOGDIR}/error.log"
324 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;
375 if [[ -n ${SCREEN_LOGDIR} ]]; then
376 echo $msg >> "${SCREEN_LOGDIR}/error.log"
377 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 \
948 $1 \
949 --type=$2 \
950 --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
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001211 sudo $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 \
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001232 $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
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001242 die $LINENO "$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 Troyerd5dfa4c2014-07-25 11:13:11 -05001264# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001265# Uses globals ``CURRENT_LOG_TIME``, ``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
1280 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001281 exec 1>&${SCREEN_LOGDIR}/screen-${service}.log.${CURRENT_LOG_TIME} 2>&1
1282 ln -sf ${SCREEN_LOGDIR}/screen-${service}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001283
1284 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1285 export PYTHONUNBUFFERED=1
1286 fi
1287
Dean Troyer3159a822014-08-27 14:13:58 -05001288 # Run under ``setsid`` to force the process to become a session and group leader.
1289 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001290 if [[ -n "$group" ]]; then
1291 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1292 else
1293 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1294 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001295
1296 # Just silently exit this process
1297 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001298}
1299
1300# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1301# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001302# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001303# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001304function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001305 SCREEN_NAME=${SCREEN_NAME:-stack}
1306 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1307
1308 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1309 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1310 fi
1311
1312 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1313}
1314
1315# Find out if a process exists by partial name.
1316# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001317function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001318 local name=$1
1319 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001320 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001321 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001322 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001323}
1324
Dean Troyer3159a822014-08-27 14:13:58 -05001325# Run a single service under screen or directly
1326# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001327# If an optional group is provided sg will be used to run the
1328# command as that group.
1329# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001330function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001331 local service=$1
1332 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001333 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001334
Dean Troyer3159a822014-08-27 14:13:58 -05001335 if is_service_enabled $service; then
1336 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001337 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001338 else
1339 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001340 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001341 fi
1342 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001343}
1344
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001345# Helper to launch a process in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001346# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1347# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001348# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001349# Run a command in a shell in a screen window, if an optional group
1350# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001351function screen_process {
1352 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001353 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001354 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001355
Sean Dagueea22a4f2014-06-27 15:21:41 -04001356 SCREEN_NAME=${SCREEN_NAME:-stack}
1357 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001358 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001359
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001360 # Append the process to the screen rc file
1361 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001362
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001363 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001364
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001365 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001366 screen -S $SCREEN_NAME -p $name -X logfile ${SCREEN_LOGDIR}/screen-${name}.log.${CURRENT_LOG_TIME}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001367 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerad5cc982014-12-10 16:35:32 -06001368 ln -sf ${SCREEN_LOGDIR}/screen-${name}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${name}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001369 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001370
1371 # sleep to allow bash to be ready to be send the command - we are
1372 # creating a new window in screen and then sends characters, so if
1373 # bash isn't running by the time we send the command, nothing happens
1374 sleep 3
1375
1376 NL=`echo -ne '\015'`
1377 # This fun command does the following:
1378 # - the passed server command is backgrounded
1379 # - the pid of the background process is saved in the usual place
1380 # - the server process is brought back to the foreground
1381 # - if the server process exits prematurely the fg command errors
1382 # and a message is written to stdout and the process failure file
1383 #
1384 # The pid saved can be used in stop_process() as a process group
1385 # id to kill off all child processes
1386 if [[ -n "$group" ]]; then
1387 command="sg $group '$command'"
1388 fi
1389 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 -06001390}
1391
1392# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001393# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001394# screen_rc service "command-line"
1395function screen_rc {
1396 SCREEN_NAME=${SCREEN_NAME:-stack}
1397 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1398 if [[ ! -e $SCREENRC ]]; then
1399 # Name the screen session
1400 echo "sessionname $SCREEN_NAME" > $SCREENRC
1401 # Set a reasonable statusbar
1402 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1403 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1404 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1405 echo "screen -t shell bash" >> $SCREENRC
1406 fi
1407 # If this service doesn't already exist in the screenrc file
1408 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1409 NL=`echo -ne '\015'`
1410 echo "screen -t $1 bash" >> $SCREENRC
1411 echo "stuff \"$2$NL\"" >> $SCREENRC
1412
1413 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001414 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001415 echo "log on" >>$SCREENRC
1416 fi
1417 fi
1418}
1419
1420# Stop a service in screen
1421# If a PID is available use it, kill the whole process group via TERM
1422# If screen is being used kill the screen window; this will catch processes
1423# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001424# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001425# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001426function screen_stop_service {
1427 local service=$1
1428
Dean Troyerdff49a22014-01-30 15:37:40 -06001429 SCREEN_NAME=${SCREEN_NAME:-stack}
1430 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001431 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001432
Dean Troyer3159a822014-08-27 14:13:58 -05001433 if is_service_enabled $service; then
1434 # Clean up the screen window
1435 screen -S $SCREEN_NAME -p $service -X kill
1436 fi
1437}
1438
1439# Stop a service process
1440# If a PID is available use it, kill the whole process group via TERM
1441# If screen is being used kill the screen window; this will catch processes
1442# that did not leave a PID behind
1443# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1444# stop_process service
1445function stop_process {
1446 local service=$1
1447
1448 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001449 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001450
1451 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001452 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001453 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1454 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1455 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001456 fi
1457 if [[ "$USE_SCREEN" = "True" ]]; then
1458 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001459 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001460 fi
1461 fi
1462}
1463
1464# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001465# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001466# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001467function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001468 local service
1469 local failures
1470 SCREEN_NAME=${SCREEN_NAME:-stack}
1471 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1472
1473
1474 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1475 echo "No service status directory found"
1476 return
1477 fi
1478
1479 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001480 # make this -o errexit safe
1481 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001482
1483 for service in $failures; do
1484 service=`basename $service`
1485 service=${service%.failure}
1486 echo "Error: Service $service is not running"
1487 done
1488
1489 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001490 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001491 fi
1492}
1493
Chris Dent2f27a0e2014-09-09 13:46:02 +01001494# Tail a log file in a screen if USE_SCREEN is true.
1495function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001496 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001497 local logfile=$2
1498
Sean Dague53753292014-12-04 19:38:15 -05001499 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001500 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001501 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001502 fi
1503}
1504
Dean Troyerdff49a22014-01-30 15:37:40 -06001505
Dean Troyer3159a822014-08-27 14:13:58 -05001506# Deprecated Functions
1507# --------------------
1508
1509# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1510# fork. It includes the dirty work of closing extra filehandles and preparing log
1511# files to produce the same logs as screen_it(). The log filename is derived
1512# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1513# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1514# _old_run_process service "command-line"
1515function _old_run_process {
1516 local service=$1
1517 local command="$2"
1518
1519 # Undo logging redirections and close the extra descriptors
1520 exec 1>&3
1521 exec 2>&3
1522 exec 3>&-
1523 exec 6>&-
1524
1525 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001526 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1527 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001528
1529 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1530 export PYTHONUNBUFFERED=1
1531 fi
1532
1533 exec /bin/bash -c "$command"
1534 die "$service exec failure: $command"
1535}
1536
1537# old_run_process() launches a child process that closes all file descriptors and
1538# then exec's the passed in command. This is meant to duplicate the semantics
1539# of screen_it() without screen. PIDs are written to
1540# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1541# old_run_process service "command-line"
1542function old_run_process {
1543 local service=$1
1544 local command="$2"
1545
1546 # Spawn the child process
1547 _old_run_process "$service" "$command" &
1548 echo $!
1549}
1550
1551# Compatibility for existing start_XXXX() functions
1552# Uses global ``USE_SCREEN``
1553# screen_it service "command-line"
1554function screen_it {
1555 if is_service_enabled $1; then
1556 # Append the service to the screen rc file
1557 screen_rc "$1" "$2"
1558
1559 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001560 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001561 else
1562 # Spawn directly without screen
1563 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1564 fi
1565 fi
1566}
1567
1568# Compatibility for existing stop_XXXX() functions
1569# Stop a service in screen
1570# If a PID is available use it, kill the whole process group via TERM
1571# If screen is being used kill the screen window; this will catch processes
1572# that did not leave a PID behind
1573# screen_stop service
1574function screen_stop {
1575 # Clean up the screen window
1576 stop_process $1
1577}
1578
1579
Dean Troyerdff49a22014-01-30 15:37:40 -06001580# Python Functions
1581# ================
1582
1583# Get the path to the pip command.
1584# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001585function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001586 which pip || which pip-python
1587
1588 if [ $? -ne 0 ]; then
1589 die $LINENO "Unable to find pip; cannot continue"
1590 fi
1591}
1592
1593# Get the path to the direcotry where python executables are installed.
1594# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001595function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001596 if is_fedora || is_suse; then
1597 echo "/usr/bin"
1598 else
1599 echo "/usr/local/bin"
1600 fi
1601}
1602
1603# Wrapper for ``pip install`` to set cache and proxy environment variables
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001604# Uses globals ``OFFLINE``, ``TRACK_DEPENDS``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001605# pip_install package [package ...]
1606function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001607 local xtrace=$(set +o | grep xtrace)
1608 set +o xtrace
Sean Dague53753292014-12-04 19:38:15 -05001609 local offline=${OFFLINE:-False}
1610 if [[ "$offline" == "True" || -z "$@" ]]; then
Sean Dague45917cc2014-02-24 16:09:14 -05001611 $xtrace
1612 return
1613 fi
1614
Dean Troyerdff49a22014-01-30 15:37:40 -06001615 if [[ -z "$os_PACKAGE" ]]; then
1616 GetOSVersion
1617 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001618 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1619 # TRACK_DEPENDS=True installation creates a circular dependency when
1620 # we attempt to install virtualenv into a virualenv, so we must global
1621 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001622 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001623 local cmd_pip=$DEST/.venv/bin/pip
1624 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001625 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001626 local cmd_pip=$(get_pip_command)
Jeremy Stanley6ec66bb2014-12-22 17:17:51 +00001627 local sudo_pip="sudo -H"
Dean Troyerdff49a22014-01-30 15:37:40 -06001628 fi
1629
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001630 local pip_version=$(python -c "import pip; \
1631 print(pip.__version__.strip('.')[0])")
1632 if (( pip_version<6 )); then
1633 die $LINENO "Currently installed pip version ${pip_version} does not" \
1634 "meet minimum requirements (>=6)."
1635 fi
1636
Sean Dague45917cc2014-02-24 16:09:14 -05001637 $xtrace
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001638 $sudo_pip \
Sean Dague53753292014-12-04 19:38:15 -05001639 http_proxy=${http_proxy:-} \
1640 https_proxy=${https_proxy:-} \
1641 no_proxy=${no_proxy:-} \
Sean Daguec53e8362014-09-30 22:37:52 -04001642 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001643 $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001644
Sean Dague53753292014-12-04 19:38:15 -05001645 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001646 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1647 local test_req="$@/test-requirements.txt"
1648 if [[ -e "$test_req" ]]; then
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001649 $sudo_pip \
Sean Dague53753292014-12-04 19:38:15 -05001650 http_proxy=${http_proxy:-} \
1651 https_proxy=${https_proxy:-} \
1652 no_proxy=${no_proxy:-} \
Sean Daguec53e8362014-09-30 22:37:52 -04001653 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001654 -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001655 fi
1656 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001657}
1658
Sean Daguecc524062014-10-01 09:06:43 -04001659# should we use this library from their git repo, or should we let it
1660# get pulled in via pip dependencies.
1661function use_library_from_git {
1662 local name=$1
1663 local enabled=1
1664 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1665 return $enabled
1666}
1667
1668# setup a library by name. If we are trying to use the library from
1669# git, we'll do a git based install, otherwise we'll punt and the
1670# library should be installed by a requirements pull from another
1671# project.
1672function setup_lib {
1673 local name=$1
1674 local dir=${GITDIR[$name]}
1675 setup_install $dir
1676}
1677
Sean Daguee08ab102014-11-13 17:09:28 -05001678# setup a library by name in editiable mode. If we are trying to use
1679# the library from git, we'll do a git based install, otherwise we'll
1680# punt and the library should be installed by a requirements pull from
1681# another project.
1682#
1683# use this for non namespaced libraries
1684function setup_dev_lib {
1685 local name=$1
1686 local dir=${GITDIR[$name]}
1687 setup_develop $dir
1688}
Sean Daguecc524062014-10-01 09:06:43 -04001689
Sean Dague099e5e32014-03-31 10:35:43 -04001690# this should be used if you want to install globally, all libraries should
1691# use this, especially *oslo* ones
1692function setup_install {
1693 local project_dir=$1
1694 setup_package_with_req_sync $project_dir
1695}
1696
1697# this should be used for projects which run services, like all services
1698function setup_develop {
1699 local project_dir=$1
1700 setup_package_with_req_sync $project_dir -e
1701}
1702
Sean Daguedef15342014-10-27 12:26:04 -04001703# determine if a project as specified by directory is in
1704# projects.txt. This will not be an exact match because we throw away
1705# the namespacing when we clone, but it should be good enough in all
1706# practical ways.
1707function is_in_projects_txt {
1708 local project_dir=$1
1709 local project_name=$(basename $project_dir)
1710 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1711}
1712
Dean Troyeraf616d92014-02-17 12:57:55 -06001713# ``pip install -e`` the package, which processes the dependencies
1714# using pip before running `setup.py develop`
1715#
1716# Updates the dependencies in project_dir from the
1717# openstack/requirements global list before installing anything.
1718#
1719# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1720# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001721function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001722 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001723 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001724
Dean Troyeraf616d92014-02-17 12:57:55 -06001725 # Don't update repo if local changes exist
1726 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001727 # ``errexit`` requires us to trap the exit code when the repo is changed
1728 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001729
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001730 if [[ $update_requirements != "changed" ]]; then
Sean Daguedef15342014-10-27 12:26:04 -04001731 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1732 if is_in_projects_txt $project_dir; then
1733 (cd $REQUIREMENTS_DIR; \
1734 python update.py $project_dir)
1735 else
1736 # soft update projects not found in requirements project.txt
1737 (cd $REQUIREMENTS_DIR; \
1738 python update.py -s $project_dir)
1739 fi
1740 else
1741 (cd $REQUIREMENTS_DIR; \
1742 python update.py $project_dir)
1743 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001744 fi
1745
Sean Dague099e5e32014-03-31 10:35:43 -04001746 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001747
1748 # We've just gone and possibly modified the user's source tree in an
1749 # automated way, which is considered bad form if it's a development
1750 # tree because we've screwed up their next git checkin. So undo it.
1751 #
1752 # However... there are some circumstances, like running in the gate
1753 # where we really really want the overridden version to stick. So provide
1754 # a variable that tells us whether or not we should UNDO the requirements
1755 # changes (this will be set to False in the OpenStack ci gate)
1756 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001757 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001758 (cd $project_dir && git reset --hard)
1759 fi
1760 fi
1761}
1762
1763# ``pip install -e`` the package, which processes the dependencies
1764# using pip before running `setup.py develop`
1765# Uses globals ``STACK_USER``
1766# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001767function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001768 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001769 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001770
Sean Dague099e5e32014-03-31 10:35:43 -04001771 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001772 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001773 if [[ "$flags" == "-e" ]]; then
1774 safe_chown -R $STACK_USER $1/*.egg-info
1775 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001776}
1777
Sean Dague2c65e712014-12-18 09:44:56 -05001778# Plugin Functions
1779# =================
1780
1781DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1782
1783# enable_plugin <name> <url> [branch]
1784#
1785# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1786# ``url`` is a git url
1787# ``branch`` is a gitref. If it's not set, defaults to master
1788function enable_plugin {
1789 local name=$1
1790 local url=$2
1791 local branch=${3:-master}
1792 DEVSTACK_PLUGINS+=",$name"
1793 GITREPO[$name]=$url
1794 GITDIR[$name]=$DEST/$name
1795 GITBRANCH[$name]=$branch
1796}
1797
1798# fetch_plugins
1799#
1800# clones all plugins
1801function fetch_plugins {
1802 local plugins="${DEVSTACK_PLUGINS}"
1803 local plugin
1804
1805 # short circuit if nothing to do
1806 if [[ -z $plugins ]]; then
1807 return
1808 fi
1809
1810 echo "Fetching devstack plugins"
1811 for plugin in ${plugins//,/ }; do
1812 git_clone_by_name $plugin
1813 done
1814}
1815
1816# load_plugin_settings
1817#
1818# Load settings from plugins in the order that they were registered
1819function load_plugin_settings {
1820 local plugins="${DEVSTACK_PLUGINS}"
1821 local plugin
1822
1823 # short circuit if nothing to do
1824 if [[ -z $plugins ]]; then
1825 return
1826 fi
1827
1828 echo "Loading plugin settings"
1829 for plugin in ${plugins//,/ }; do
1830 local dir=${GITDIR[$plugin]}
1831 # source any known settings
1832 if [[ -f $dir/devstack/settings ]]; then
1833 source $dir/devstack/settings
1834 fi
1835 done
1836}
1837
1838# run_plugins
1839#
1840# Run the devstack/plugin.sh in all the plugin directories. These are
1841# run in registration order.
1842function run_plugins {
1843 local mode=$1
1844 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301845
1846 local plugins="${DEVSTACK_PLUGINS}"
1847 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001848 for plugin in ${plugins//,/ }; do
1849 local dir=${GITDIR[$plugin]}
1850 if [[ -f $dir/devstack/plugin.sh ]]; then
1851 source $dir/devstack/plugin.sh $mode $phase
1852 fi
1853 done
1854}
1855
1856function run_phase {
1857 local mode=$1
1858 local phase=$2
1859 if [[ -d $TOP_DIR/extras.d ]]; then
1860 for i in $TOP_DIR/extras.d/*.sh; do
1861 [[ -r $i ]] && source $i $mode $phase
1862 done
1863 fi
1864 # the source phase corresponds to settings loading in plugins
1865 if [[ "$mode" == "source" ]]; then
1866 load_plugin_settings
1867 else
1868 run_plugins $mode $phase
1869 fi
1870}
1871
Dean Troyerdff49a22014-01-30 15:37:40 -06001872
1873# Service Functions
1874# =================
1875
1876# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1877# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001878function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001879 echo "$1" | sed -e '
1880 s/,,/,/g;
1881 s/^,//;
1882 s/,$//
1883 '
1884}
1885
1886# disable_all_services() removes all current services
1887# from ``ENABLED_SERVICES`` to reset the configuration
1888# before a minimal installation
1889# Uses global ``ENABLED_SERVICES``
1890# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001891function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001892 ENABLED_SERVICES=""
1893}
1894
1895# Remove all services starting with '-'. For example, to install all default
1896# services except rabbit (rabbit) set in ``localrc``:
1897# ENABLED_SERVICES+=",-rabbit"
1898# Uses global ``ENABLED_SERVICES``
1899# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001900function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001901 local tmpsvcs="${ENABLED_SERVICES}"
1902 local service
1903 for service in ${tmpsvcs//,/ }; do
1904 if [[ ${service} == -* ]]; then
1905 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1906 fi
1907 done
1908 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1909}
1910
1911# disable_service() removes the services passed as argument to the
1912# ``ENABLED_SERVICES`` list, if they are present.
1913#
1914# For example:
1915# disable_service rabbit
1916#
1917# This function does not know about the special cases
1918# for nova, glance, and neutron built into is_service_enabled().
1919# Uses global ``ENABLED_SERVICES``
1920# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001921function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001922 local tmpsvcs=",${ENABLED_SERVICES},"
1923 local service
1924 for service in $@; do
1925 if is_service_enabled $service; then
1926 tmpsvcs=${tmpsvcs//,$service,/,}
1927 fi
1928 done
1929 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1930}
1931
1932# enable_service() adds the services passed as argument to the
1933# ``ENABLED_SERVICES`` list, if they are not already present.
1934#
1935# For example:
1936# enable_service qpid
1937#
1938# This function does not know about the special cases
1939# for nova, glance, and neutron built into is_service_enabled().
1940# Uses global ``ENABLED_SERVICES``
1941# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001942function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001943 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001944 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001945 for service in $@; do
1946 if ! is_service_enabled $service; then
1947 tmpsvcs+=",$service"
1948 fi
1949 done
1950 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1951 disable_negated_services
1952}
1953
1954# is_service_enabled() checks if the service(s) specified as arguments are
1955# enabled by the user in ``ENABLED_SERVICES``.
1956#
1957# Multiple services specified as arguments are ``OR``'ed together; the test
1958# is a short-circuit boolean, i.e it returns on the first match.
1959#
1960# There are special cases for some 'catch-all' services::
1961# **nova** returns true if any service enabled start with **n-**
1962# **cinder** returns true if any service enabled start with **c-**
1963# **ceilometer** returns true if any service enabled start with **ceilometer**
1964# **glance** returns true if any service enabled start with **g-**
1965# **neutron** returns true if any service enabled start with **q-**
1966# **swift** returns true if any service enabled start with **s-**
1967# **trove** returns true if any service enabled start with **tr-**
1968# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1969# **s-** services will be enabled. This will be deprecated in the future.
1970#
1971# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1972# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1973# as enabled in this case.
1974#
1975# Uses global ``ENABLED_SERVICES``
1976# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001977function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001978 local xtrace=$(set +o | grep xtrace)
1979 set +o xtrace
1980 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001981 local services=$@
1982 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001983 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001984 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001985
1986 # Look for top-level 'enabled' function for this service
1987 if type is_${service}_enabled >/dev/null 2>&1; then
1988 # A function exists for this service, use it
1989 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001990 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001991 fi
1992
1993 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1994 # are implemented
1995
Sean Dague45917cc2014-02-24 16:09:14 -05001996 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001997 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001998 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1999 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
2000 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
2001 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
2002 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
2003 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
2004 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
2005 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
2006 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05002007 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002008 done
Sean Dague45917cc2014-02-24 16:09:14 -05002009 $xtrace
2010 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06002011}
2012
2013# Toggle enable/disable_service for services that must run exclusive of each other
2014# $1 The name of a variable containing a space-separated list of services
2015# $2 The name of a variable in which to store the enabled service's name
2016# $3 The name of the service to enable
2017function use_exclusive_service {
2018 local options=${!1}
2019 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002020 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06002021 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002022 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06002023 for opt in $options;do
2024 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
2025 done
2026 eval "$out=$selection"
2027 return 0
2028}
2029
2030
Masayuki Igawaf6368d32014-02-20 13:31:26 +09002031# System Functions
2032# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06002033
2034# Only run the command if the target file (the last arg) is not on an
2035# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002036function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05002037 local xtrace=$(set +o | grep xtrace)
2038 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002039 local args=( $@ )
2040 local last
2041 local sudo_cmd
2042 local dir_to_check
2043
2044 let last="${#args[*]} - 1"
2045
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002046 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002047 if [ ! -d "$dir_to_check" ]; then
2048 dir_to_check=`dirname "$dir_to_check"`
2049 fi
2050
2051 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002052 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002053 return 0
2054 fi
2055
2056 if [[ $TRACK_DEPENDS = True ]]; then
2057 sudo_cmd="env"
2058 else
2059 sudo_cmd="sudo"
2060 fi
2061
Sean Dague45917cc2014-02-24 16:09:14 -05002062 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002063 $sudo_cmd $@
2064}
2065
2066# Exit 0 if address is in network or 1 if address is not in network
2067# ip-range is in CIDR notation: 1.2.3.4/20
2068# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002069function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002070 local ip=$1
2071 local range=$2
2072 local masklen=${range#*/}
2073 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2074 local subnet=$(maskip $ip $(cidr2netmask $masklen))
2075 [[ $network == $subnet ]]
2076}
2077
2078# Add a user to a group.
2079# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002080function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002081 local user=$1
2082 local group=$2
2083
2084 if [[ -z "$os_VENDOR" ]]; then
2085 GetOSVersion
2086 fi
2087
2088 # SLE11 and openSUSE 12.2 don't have the usual usermod
2089 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
2090 sudo usermod -a -G "$group" "$user"
2091 else
2092 sudo usermod -A "$group" "$user"
2093 fi
2094}
2095
2096# Convert CIDR notation to a IPv4 netmask
2097# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002098function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002099 local maskpat="255 255 255 255"
2100 local maskdgt="254 252 248 240 224 192 128"
2101 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2102 echo ${1-0}.${2-0}.${3-0}.${4-0}
2103}
2104
2105# Gracefully cp only if source file/dir exists
2106# cp_it source destination
2107function cp_it {
2108 if [ -e $1 ] || [ -d $1 ]; then
2109 cp -pRL $1 $2
2110 fi
2111}
2112
2113# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2114# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2115# ``localrc`` or on the command line if necessary::
2116#
2117# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2118#
2119# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2120
Ian Wienandaee18c72014-02-21 15:35:08 +11002121function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05002122 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002123 export http_proxy=$http_proxy
2124 fi
Sean Dague53753292014-12-04 19:38:15 -05002125 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002126 export https_proxy=$https_proxy
2127 fi
Sean Dague53753292014-12-04 19:38:15 -05002128 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002129 export no_proxy=$no_proxy
2130 fi
2131}
2132
2133# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002134function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06002135 local mount_type=`stat -f -L -c %T $1`
2136 test "$mount_type" == "nfs"
2137}
2138
2139# Return the network portion of the given IP address using netmask
2140# netmask is in the traditional dotted-quad format
2141# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002142function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002143 local ip=$1
2144 local mask=$2
2145 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2146 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2147 echo $subnet
2148}
2149
2150# Service wrapper to restart services
2151# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002152function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002153 if is_ubuntu; then
2154 sudo /usr/sbin/service $1 restart
2155 else
2156 sudo /sbin/service $1 restart
2157 fi
2158}
2159
2160# Only change permissions of a file or directory if it is not on an
2161# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002162function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002163 _safe_permission_operation chmod $@
2164}
2165
2166# Only change ownership of a file or directory if it is not on an NFS
2167# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002168function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002169 _safe_permission_operation chown $@
2170}
2171
2172# Service wrapper to start services
2173# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002174function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002175 if is_ubuntu; then
2176 sudo /usr/sbin/service $1 start
2177 else
2178 sudo /sbin/service $1 start
2179 fi
2180}
2181
2182# Service wrapper to stop services
2183# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002184function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002185 if is_ubuntu; then
2186 sudo /usr/sbin/service $1 stop
2187 else
2188 sudo /sbin/service $1 stop
2189 fi
2190}
2191
2192
2193# Restore xtrace
2194$XTRACE
2195
2196# Local variables:
2197# mode: shell-script
2198# End: