blob: 29c28f45812513f4188505917ba2f4939b64e4b8 [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
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000863# Usage: get_or_create_user <username> <password> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100864function get_or_create_user {
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000865 if [[ ! -z "$3" ]]; then
866 local email="--email=$3"
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=""
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000872 if [[ ! -z "$4" ]]; then
873 domain="--domain=$4"
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" \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500882 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100883 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500884 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100885 -f value -c id
886 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500887 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100888}
889
890# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100891# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100892function get_or_create_project {
893 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100894 local os_cmd="openstack"
895 local domain=""
896 if [[ ! -z "$2" ]]; then
897 domain="--domain=$2"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500898 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100899 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500900 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500901 # Creates new project with --or-show
902 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100903 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500904 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100905}
906
907# Gets or creates role
908# Usage: get_or_create_role <name>
909function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500910 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500911 # Creates role with --or-show
912 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100913 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500914 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100915}
916
917# Gets or adds user role
918# Usage: get_or_add_user_role <role> <user> <project>
919function get_or_add_user_role {
920 # Gets user role id
Steve Martinelli5541a612015-01-19 15:58:49 -0500921 local user_role_id=$(openstack role list \
922 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100923 --project $3 \
924 --column "ID" \
925 --column "Name" \
926 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500927 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100928 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500929 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100930 $1 \
931 --user $2 \
932 --project $3 \
933 | grep " id " | get_field 2)
934 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500935 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100936}
937
938# Gets or creates service
939# Usage: get_or_create_service <name> <type> <description>
940function get_or_create_service {
941 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500942 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100943 # Gets service id
944 openstack service show $1 -f value -c id 2>/dev/null ||
945 # Creates new service if not exists
946 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500947 $2 \
948 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100949 --description="$3" \
950 -f value -c id
951 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500952 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100953}
954
955# Gets or creates endpoint
956# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
957function get_or_create_endpoint {
958 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500959 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100960 --column "ID" \
961 --column "Region" \
962 --column "Service Name" \
963 | grep " $2 " \
964 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500965 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100966 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500967 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100968 $1 \
969 --region $2 \
970 --publicurl $3 \
971 --adminurl $4 \
972 --internalurl $5 \
973 | grep " id " | get_field 2)
974 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500975 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100976}
Dean Troyerdff49a22014-01-30 15:37:40 -0600977
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500978
Dean Troyerdff49a22014-01-30 15:37:40 -0600979# Package Functions
980# =================
981
982# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100983function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600984 local pkg_dir
985 if is_ubuntu; then
Monty Taylor81a016d2014-11-15 17:18:13 -0300986 pkg_dir=$FILES/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600987 elif is_fedora; then
988 pkg_dir=$FILES/rpms
989 elif is_suse; then
990 pkg_dir=$FILES/rpms-suse
991 else
992 exit_distro_not_supported "list of packages"
993 fi
994 echo "$pkg_dir"
995}
996
997# Wrapper for ``apt-get`` to set cache and proxy environment variables
998# Uses globals ``OFFLINE``, ``*_proxy``
999# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001000function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -05001001 local xtrace=$(set +o | grep xtrace)
1002 set +o xtrace
1003
Dean Troyerdff49a22014-01-30 15:37:40 -06001004 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
1005 local sudo="sudo"
1006 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -05001007
1008 $xtrace
Sean Dague53753292014-12-04 19:38:15 -05001009
Dean Troyerdff49a22014-01-30 15:37:40 -06001010 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -05001011 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
1012 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -06001013 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
1014}
1015
1016# get_packages() collects a list of package names of any type from the
Monty Taylor81a016d2014-11-15 17:18:13 -03001017# prerequisite files in ``files/{debs|rpms}``. The list is intended
Dean Troyerdff49a22014-01-30 15:37:40 -06001018# to be passed to a package installer such as apt or yum.
1019#
1020# Only packages required for the services in 1st argument will be
1021# included. Two bits of metadata are recognized in the prerequisite files:
1022#
1023# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1024# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1025# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +11001026function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -05001027 local xtrace=$(set +o | grep xtrace)
1028 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001029 local services=$@
1030 local package_dir=$(_get_package_dir)
Sean Dague53753292014-12-04 19:38:15 -05001031 local file_to_parse=""
1032 local service=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001033
Sean Dague53753292014-12-04 19:38:15 -05001034 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
Flavio Percoco5a91c352014-10-31 18:48:00 +01001035
Dean Troyerdff49a22014-01-30 15:37:40 -06001036 if [[ -z "$package_dir" ]]; then
1037 echo "No package directory supplied"
1038 return 1
1039 fi
1040 if [[ -z "$DISTRO" ]]; then
1041 GetDistro
1042 fi
1043 for service in ${services//,/ }; do
1044 # Allow individual services to specify dependencies
1045 if [[ -e ${package_dir}/${service} ]]; then
1046 file_to_parse="${file_to_parse} $service"
1047 fi
1048 # NOTE(sdague) n-api needs glance for now because that's where
1049 # glance client is
1050 if [[ $service == n-api ]]; then
1051 if [[ ! $file_to_parse =~ nova ]]; then
1052 file_to_parse="${file_to_parse} nova"
1053 fi
1054 if [[ ! $file_to_parse =~ glance ]]; then
1055 file_to_parse="${file_to_parse} glance"
1056 fi
1057 elif [[ $service == c-* ]]; then
1058 if [[ ! $file_to_parse =~ cinder ]]; then
1059 file_to_parse="${file_to_parse} cinder"
1060 fi
1061 elif [[ $service == ceilometer-* ]]; then
1062 if [[ ! $file_to_parse =~ ceilometer ]]; then
1063 file_to_parse="${file_to_parse} ceilometer"
1064 fi
1065 elif [[ $service == s-* ]]; then
1066 if [[ ! $file_to_parse =~ swift ]]; then
1067 file_to_parse="${file_to_parse} swift"
1068 fi
1069 elif [[ $service == n-* ]]; then
1070 if [[ ! $file_to_parse =~ nova ]]; then
1071 file_to_parse="${file_to_parse} nova"
1072 fi
1073 elif [[ $service == g-* ]]; then
1074 if [[ ! $file_to_parse =~ glance ]]; then
1075 file_to_parse="${file_to_parse} glance"
1076 fi
1077 elif [[ $service == key* ]]; then
1078 if [[ ! $file_to_parse =~ keystone ]]; then
1079 file_to_parse="${file_to_parse} keystone"
1080 fi
1081 elif [[ $service == q-* ]]; then
1082 if [[ ! $file_to_parse =~ neutron ]]; then
1083 file_to_parse="${file_to_parse} neutron"
1084 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001085 elif [[ $service == ir-* ]]; then
1086 if [[ ! $file_to_parse =~ ironic ]]; then
1087 file_to_parse="${file_to_parse} ironic"
1088 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001089 fi
1090 done
1091
1092 for file in ${file_to_parse}; do
1093 local fname=${package_dir}/${file}
1094 local OIFS line package distros distro
1095 [[ -e $fname ]] || continue
1096
1097 OIFS=$IFS
1098 IFS=$'\n'
1099 for line in $(<${fname}); do
1100 if [[ $line =~ "NOPRIME" ]]; then
1101 continue
1102 fi
1103
1104 # Assume we want this package
1105 package=${line%#*}
1106 inst_pkg=1
1107
1108 # Look for # dist:xxx in comment
1109 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1110 # We are using BASH regexp matching feature.
1111 package=${BASH_REMATCH[1]}
1112 distros=${BASH_REMATCH[2]}
1113 # In bash ${VAR,,} will lowecase VAR
1114 # Look for a match in the distro list
1115 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1116 # If no match then skip this package
1117 inst_pkg=0
1118 fi
1119 fi
1120
1121 # Look for # testonly in comment
1122 if [[ $line =~ (.*)#.*testonly.* ]]; then
1123 package=${BASH_REMATCH[1]}
1124 # Are we installing test packages? (test for the default value)
1125 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1126 # If not installing test packages the skip this package
1127 inst_pkg=0
1128 fi
1129 fi
1130
1131 if [[ $inst_pkg = 1 ]]; then
1132 echo $package
1133 fi
1134 done
1135 IFS=$OIFS
1136 done
Sean Dague45917cc2014-02-24 16:09:14 -05001137 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001138}
1139
1140# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001141# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001142# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001143function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001144 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1145 REPOS_UPDATED=${REPOS_UPDATED:-False}
1146 RETRY_UPDATE=${RETRY_UPDATE:-False}
1147
Paul Linchpiner9e179742014-07-13 22:23:00 -07001148 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001149 return 0
1150 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001151
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001152 if is_ubuntu; then
1153 local xtrace=$(set +o | grep xtrace)
1154 set +o xtrace
1155 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1156 # if there are transient errors pulling the updates, that's fine.
1157 # It may be secondary repositories that we don't really care about.
1158 apt_get update || /bin/true
1159 REPOS_UPDATED=True
1160 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001161 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001162 fi
1163}
1164
1165function real_install_package {
1166 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001167 apt_get install "$@"
1168 elif is_fedora; then
1169 yum_install "$@"
1170 elif is_suse; then
1171 zypper_install "$@"
1172 else
1173 exit_distro_not_supported "installing packages"
1174 fi
1175}
1176
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001177# Distro-agnostic package installer
1178# install_package package [package ...]
1179function install_package {
1180 update_package_repo
1181 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1182}
1183
Dean Troyerdff49a22014-01-30 15:37:40 -06001184# Distro-agnostic function to tell if a package is installed
1185# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001186function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001187 if [[ -z "$@" ]]; then
1188 return 1
1189 fi
1190
1191 if [[ -z "$os_PACKAGE" ]]; then
1192 GetOSVersion
1193 fi
1194
1195 if [[ "$os_PACKAGE" = "deb" ]]; then
1196 dpkg -s "$@" > /dev/null 2> /dev/null
1197 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1198 rpm --quiet -q "$@"
1199 else
1200 exit_distro_not_supported "finding if a package is installed"
1201 fi
1202}
1203
1204# Distro-agnostic package uninstaller
1205# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001206function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001207 if is_ubuntu; then
1208 apt_get purge "$@"
1209 elif is_fedora; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001210 sudo $YUM remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001211 elif is_suse; then
1212 sudo zypper rm "$@"
1213 else
1214 exit_distro_not_supported "uninstalling packages"
1215 fi
1216}
1217
1218# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001219# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001220# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001221function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001222 [[ "$OFFLINE" = "True" ]] && return
1223 local sudo="sudo"
1224 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001225
1226 # The manual check for missing packages is because yum -y assumes
1227 # missing packages are OK. See
1228 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001229 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1230 no_proxy=$no_proxy \
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001231 $YUM install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001232 awk '
1233 BEGIN { fail=0 }
1234 /No package/ { fail=1 }
1235 { print }
1236 END { exit fail }' || \
1237 die $LINENO "Missing packages detected"
1238
1239 # also ensure we catch a yum failure
1240 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001241 die $LINENO "$YUM install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001242 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001243}
1244
1245# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001246# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001247# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001248function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001249 [[ "$OFFLINE" = "True" ]] && return
1250 local sudo="sudo"
1251 [[ "$(id -u)" = "0" ]] && sudo="env"
1252 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1253 zypper --non-interactive install --auto-agree-with-licenses "$@"
1254}
1255
1256
1257# Process Functions
1258# =================
1259
1260# _run_process() is designed to be backgrounded by run_process() to simulate a
1261# fork. It includes the dirty work of closing extra filehandles and preparing log
1262# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001263# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001264# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001265# If an optional group is provided sg will be used to set the group of
1266# the command.
1267# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001268function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001269 local service=$1
1270 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001271 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001272
1273 # Undo logging redirections and close the extra descriptors
1274 exec 1>&3
1275 exec 2>&3
1276 exec 3>&-
1277 exec 6>&-
1278
1279 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001280 exec 1>&${SCREEN_LOGDIR}/screen-${service}.log.${CURRENT_LOG_TIME} 2>&1
1281 ln -sf ${SCREEN_LOGDIR}/screen-${service}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001282
1283 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1284 export PYTHONUNBUFFERED=1
1285 fi
1286
Dean Troyer3159a822014-08-27 14:13:58 -05001287 # Run under ``setsid`` to force the process to become a session and group leader.
1288 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001289 if [[ -n "$group" ]]; then
1290 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1291 else
1292 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1293 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001294
1295 # Just silently exit this process
1296 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001297}
1298
1299# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1300# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001301# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001302# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001303function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001304 SCREEN_NAME=${SCREEN_NAME:-stack}
1305 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1306
1307 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1308 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1309 fi
1310
1311 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1312}
1313
1314# Find out if a process exists by partial name.
1315# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001316function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001317 local name=$1
1318 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001319 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001320 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001321 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001322}
1323
Dean Troyer3159a822014-08-27 14:13:58 -05001324# Run a single service under screen or directly
1325# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001326# If an optional group is provided sg will be used to run the
1327# command as that group.
1328# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001329function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001330 local service=$1
1331 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001332 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001333
Dean Troyer3159a822014-08-27 14:13:58 -05001334 if is_service_enabled $service; then
1335 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001336 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001337 else
1338 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001339 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001340 fi
1341 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001342}
1343
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001344# Helper to launch a process in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001345# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1346# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001347# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001348# Run a command in a shell in a screen window, if an optional group
1349# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001350function screen_process {
1351 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001352 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001353 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001354
Sean Dagueea22a4f2014-06-27 15:21:41 -04001355 SCREEN_NAME=${SCREEN_NAME:-stack}
1356 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001357 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001358
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001359 # Append the process to the screen rc file
1360 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001361
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001362 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001363
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001364 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001365 screen -S $SCREEN_NAME -p $name -X logfile ${SCREEN_LOGDIR}/screen-${name}.log.${CURRENT_LOG_TIME}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001366 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerad5cc982014-12-10 16:35:32 -06001367 ln -sf ${SCREEN_LOGDIR}/screen-${name}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${name}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001368 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001369
1370 # sleep to allow bash to be ready to be send the command - we are
1371 # creating a new window in screen and then sends characters, so if
1372 # bash isn't running by the time we send the command, nothing happens
1373 sleep 3
1374
1375 NL=`echo -ne '\015'`
1376 # This fun command does the following:
1377 # - the passed server command is backgrounded
1378 # - the pid of the background process is saved in the usual place
1379 # - the server process is brought back to the foreground
1380 # - if the server process exits prematurely the fg command errors
1381 # and a message is written to stdout and the process failure file
1382 #
1383 # The pid saved can be used in stop_process() as a process group
1384 # id to kill off all child processes
1385 if [[ -n "$group" ]]; then
1386 command="sg $group '$command'"
1387 fi
1388 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 -06001389}
1390
1391# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001392# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001393# screen_rc service "command-line"
1394function screen_rc {
1395 SCREEN_NAME=${SCREEN_NAME:-stack}
1396 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1397 if [[ ! -e $SCREENRC ]]; then
1398 # Name the screen session
1399 echo "sessionname $SCREEN_NAME" > $SCREENRC
1400 # Set a reasonable statusbar
1401 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1402 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1403 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1404 echo "screen -t shell bash" >> $SCREENRC
1405 fi
1406 # If this service doesn't already exist in the screenrc file
1407 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1408 NL=`echo -ne '\015'`
1409 echo "screen -t $1 bash" >> $SCREENRC
1410 echo "stuff \"$2$NL\"" >> $SCREENRC
1411
1412 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001413 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001414 echo "log on" >>$SCREENRC
1415 fi
1416 fi
1417}
1418
1419# Stop a service in screen
1420# If a PID is available use it, kill the whole process group via TERM
1421# If screen is being used kill the screen window; this will catch processes
1422# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001423# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001424# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001425function screen_stop_service {
1426 local service=$1
1427
Dean Troyerdff49a22014-01-30 15:37:40 -06001428 SCREEN_NAME=${SCREEN_NAME:-stack}
1429 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001430 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001431
Dean Troyer3159a822014-08-27 14:13:58 -05001432 if is_service_enabled $service; then
1433 # Clean up the screen window
1434 screen -S $SCREEN_NAME -p $service -X kill
1435 fi
1436}
1437
1438# Stop a service process
1439# If a PID is available use it, kill the whole process group via TERM
1440# If screen is being used kill the screen window; this will catch processes
1441# that did not leave a PID behind
1442# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1443# stop_process service
1444function stop_process {
1445 local service=$1
1446
1447 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001448 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001449
1450 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001451 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001452 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1453 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1454 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001455 fi
1456 if [[ "$USE_SCREEN" = "True" ]]; then
1457 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001458 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001459 fi
1460 fi
1461}
1462
1463# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001464# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001465# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001466function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001467 local service
1468 local failures
1469 SCREEN_NAME=${SCREEN_NAME:-stack}
1470 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1471
1472
1473 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1474 echo "No service status directory found"
1475 return
1476 fi
1477
1478 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001479 # make this -o errexit safe
1480 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001481
1482 for service in $failures; do
1483 service=`basename $service`
1484 service=${service%.failure}
1485 echo "Error: Service $service is not running"
1486 done
1487
1488 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001489 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001490 fi
1491}
1492
Chris Dent2f27a0e2014-09-09 13:46:02 +01001493# Tail a log file in a screen if USE_SCREEN is true.
1494function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001495 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001496 local logfile=$2
1497
Sean Dague53753292014-12-04 19:38:15 -05001498 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001499 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001500 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001501 fi
1502}
1503
Dean Troyerdff49a22014-01-30 15:37:40 -06001504
Dean Troyer3159a822014-08-27 14:13:58 -05001505# Deprecated Functions
1506# --------------------
1507
1508# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1509# fork. It includes the dirty work of closing extra filehandles and preparing log
1510# files to produce the same logs as screen_it(). The log filename is derived
1511# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1512# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1513# _old_run_process service "command-line"
1514function _old_run_process {
1515 local service=$1
1516 local command="$2"
1517
1518 # Undo logging redirections and close the extra descriptors
1519 exec 1>&3
1520 exec 2>&3
1521 exec 3>&-
1522 exec 6>&-
1523
1524 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001525 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1526 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001527
1528 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1529 export PYTHONUNBUFFERED=1
1530 fi
1531
1532 exec /bin/bash -c "$command"
1533 die "$service exec failure: $command"
1534}
1535
1536# old_run_process() launches a child process that closes all file descriptors and
1537# then exec's the passed in command. This is meant to duplicate the semantics
1538# of screen_it() without screen. PIDs are written to
1539# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1540# old_run_process service "command-line"
1541function old_run_process {
1542 local service=$1
1543 local command="$2"
1544
1545 # Spawn the child process
1546 _old_run_process "$service" "$command" &
1547 echo $!
1548}
1549
1550# Compatibility for existing start_XXXX() functions
1551# Uses global ``USE_SCREEN``
1552# screen_it service "command-line"
1553function screen_it {
1554 if is_service_enabled $1; then
1555 # Append the service to the screen rc file
1556 screen_rc "$1" "$2"
1557
1558 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001559 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001560 else
1561 # Spawn directly without screen
1562 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1563 fi
1564 fi
1565}
1566
1567# Compatibility for existing stop_XXXX() functions
1568# Stop a service in screen
1569# If a PID is available use it, kill the whole process group via TERM
1570# If screen is being used kill the screen window; this will catch processes
1571# that did not leave a PID behind
1572# screen_stop service
1573function screen_stop {
1574 # Clean up the screen window
1575 stop_process $1
1576}
1577
1578
Dean Troyerdff49a22014-01-30 15:37:40 -06001579# Python Functions
1580# ================
1581
1582# Get the path to the pip command.
1583# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001584function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001585 which pip || which pip-python
1586
1587 if [ $? -ne 0 ]; then
1588 die $LINENO "Unable to find pip; cannot continue"
1589 fi
1590}
1591
1592# Get the path to the direcotry where python executables are installed.
1593# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001594function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001595 if is_fedora || is_suse; then
1596 echo "/usr/bin"
1597 else
1598 echo "/usr/local/bin"
1599 fi
1600}
1601
1602# Wrapper for ``pip install`` to set cache and proxy environment variables
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001603# Uses globals ``OFFLINE``, ``TRACK_DEPENDS``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001604# pip_install package [package ...]
1605function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001606 local xtrace=$(set +o | grep xtrace)
1607 set +o xtrace
Sean Dague53753292014-12-04 19:38:15 -05001608 local offline=${OFFLINE:-False}
1609 if [[ "$offline" == "True" || -z "$@" ]]; then
Sean Dague45917cc2014-02-24 16:09:14 -05001610 $xtrace
1611 return
1612 fi
1613
Dean Troyerdff49a22014-01-30 15:37:40 -06001614 if [[ -z "$os_PACKAGE" ]]; then
1615 GetOSVersion
1616 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001617 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1618 # TRACK_DEPENDS=True installation creates a circular dependency when
1619 # we attempt to install virtualenv into a virualenv, so we must global
1620 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001621 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001622 local cmd_pip=$DEST/.venv/bin/pip
1623 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001624 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001625 local cmd_pip=$(get_pip_command)
Jeremy Stanley6ec66bb2014-12-22 17:17:51 +00001626 local sudo_pip="sudo -H"
Dean Troyerdff49a22014-01-30 15:37:40 -06001627 fi
1628
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001629 local pip_version=$(python -c "import pip; \
1630 print(pip.__version__.strip('.')[0])")
1631 if (( pip_version<6 )); then
1632 die $LINENO "Currently installed pip version ${pip_version} does not" \
1633 "meet minimum requirements (>=6)."
1634 fi
1635
Sean Dague45917cc2014-02-24 16:09:14 -05001636 $xtrace
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001637 $sudo_pip \
Sean Dague53753292014-12-04 19:38:15 -05001638 http_proxy=${http_proxy:-} \
1639 https_proxy=${https_proxy:-} \
1640 no_proxy=${no_proxy:-} \
Sean Daguec53e8362014-09-30 22:37:52 -04001641 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001642 $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001643
Sean Dague53753292014-12-04 19:38:15 -05001644 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001645 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1646 local test_req="$@/test-requirements.txt"
1647 if [[ -e "$test_req" ]]; then
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001648 $sudo_pip \
Sean Dague53753292014-12-04 19:38:15 -05001649 http_proxy=${http_proxy:-} \
1650 https_proxy=${https_proxy:-} \
1651 no_proxy=${no_proxy:-} \
Sean Daguec53e8362014-09-30 22:37:52 -04001652 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001653 -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001654 fi
1655 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001656}
1657
Sean Daguecc524062014-10-01 09:06:43 -04001658# should we use this library from their git repo, or should we let it
1659# get pulled in via pip dependencies.
1660function use_library_from_git {
1661 local name=$1
1662 local enabled=1
1663 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1664 return $enabled
1665}
1666
1667# setup a library by name. If we are trying to use the library from
1668# git, we'll do a git based install, otherwise we'll punt and the
1669# library should be installed by a requirements pull from another
1670# project.
1671function setup_lib {
1672 local name=$1
1673 local dir=${GITDIR[$name]}
1674 setup_install $dir
1675}
1676
Sean Daguee08ab102014-11-13 17:09:28 -05001677# setup a library by name in editiable mode. If we are trying to use
1678# the library from git, we'll do a git based install, otherwise we'll
1679# punt and the library should be installed by a requirements pull from
1680# another project.
1681#
1682# use this for non namespaced libraries
1683function setup_dev_lib {
1684 local name=$1
1685 local dir=${GITDIR[$name]}
1686 setup_develop $dir
1687}
Sean Daguecc524062014-10-01 09:06:43 -04001688
Sean Dague099e5e32014-03-31 10:35:43 -04001689# this should be used if you want to install globally, all libraries should
1690# use this, especially *oslo* ones
1691function setup_install {
1692 local project_dir=$1
1693 setup_package_with_req_sync $project_dir
1694}
1695
1696# this should be used for projects which run services, like all services
1697function setup_develop {
1698 local project_dir=$1
1699 setup_package_with_req_sync $project_dir -e
1700}
1701
Sean Daguedef15342014-10-27 12:26:04 -04001702# determine if a project as specified by directory is in
1703# projects.txt. This will not be an exact match because we throw away
1704# the namespacing when we clone, but it should be good enough in all
1705# practical ways.
1706function is_in_projects_txt {
1707 local project_dir=$1
1708 local project_name=$(basename $project_dir)
1709 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1710}
1711
Dean Troyeraf616d92014-02-17 12:57:55 -06001712# ``pip install -e`` the package, which processes the dependencies
1713# using pip before running `setup.py develop`
1714#
1715# Updates the dependencies in project_dir from the
1716# openstack/requirements global list before installing anything.
1717#
1718# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1719# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001720function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001721 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001722 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001723
Dean Troyeraf616d92014-02-17 12:57:55 -06001724 # Don't update repo if local changes exist
1725 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001726 # ``errexit`` requires us to trap the exit code when the repo is changed
1727 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001728
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001729 if [[ $update_requirements != "changed" ]]; then
Sean Daguedef15342014-10-27 12:26:04 -04001730 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1731 if is_in_projects_txt $project_dir; then
1732 (cd $REQUIREMENTS_DIR; \
1733 python update.py $project_dir)
1734 else
1735 # soft update projects not found in requirements project.txt
1736 (cd $REQUIREMENTS_DIR; \
1737 python update.py -s $project_dir)
1738 fi
1739 else
1740 (cd $REQUIREMENTS_DIR; \
1741 python update.py $project_dir)
1742 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001743 fi
1744
Sean Dague099e5e32014-03-31 10:35:43 -04001745 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001746
1747 # We've just gone and possibly modified the user's source tree in an
1748 # automated way, which is considered bad form if it's a development
1749 # tree because we've screwed up their next git checkin. So undo it.
1750 #
1751 # However... there are some circumstances, like running in the gate
1752 # where we really really want the overridden version to stick. So provide
1753 # a variable that tells us whether or not we should UNDO the requirements
1754 # changes (this will be set to False in the OpenStack ci gate)
1755 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001756 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001757 (cd $project_dir && git reset --hard)
1758 fi
1759 fi
1760}
1761
1762# ``pip install -e`` the package, which processes the dependencies
1763# using pip before running `setup.py develop`
1764# Uses globals ``STACK_USER``
1765# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001766function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001767 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001768 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001769
Sean Dague099e5e32014-03-31 10:35:43 -04001770 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001771 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001772 if [[ "$flags" == "-e" ]]; then
1773 safe_chown -R $STACK_USER $1/*.egg-info
1774 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001775}
1776
Sean Dague2c65e712014-12-18 09:44:56 -05001777# Plugin Functions
1778# =================
1779
1780DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1781
1782# enable_plugin <name> <url> [branch]
1783#
1784# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1785# ``url`` is a git url
1786# ``branch`` is a gitref. If it's not set, defaults to master
1787function enable_plugin {
1788 local name=$1
1789 local url=$2
1790 local branch=${3:-master}
1791 DEVSTACK_PLUGINS+=",$name"
1792 GITREPO[$name]=$url
1793 GITDIR[$name]=$DEST/$name
1794 GITBRANCH[$name]=$branch
1795}
1796
1797# fetch_plugins
1798#
1799# clones all plugins
1800function fetch_plugins {
1801 local plugins="${DEVSTACK_PLUGINS}"
1802 local plugin
1803
1804 # short circuit if nothing to do
1805 if [[ -z $plugins ]]; then
1806 return
1807 fi
1808
1809 echo "Fetching devstack plugins"
1810 for plugin in ${plugins//,/ }; do
1811 git_clone_by_name $plugin
1812 done
1813}
1814
1815# load_plugin_settings
1816#
1817# Load settings from plugins in the order that they were registered
1818function load_plugin_settings {
1819 local plugins="${DEVSTACK_PLUGINS}"
1820 local plugin
1821
1822 # short circuit if nothing to do
1823 if [[ -z $plugins ]]; then
1824 return
1825 fi
1826
1827 echo "Loading plugin settings"
1828 for plugin in ${plugins//,/ }; do
1829 local dir=${GITDIR[$plugin]}
1830 # source any known settings
1831 if [[ -f $dir/devstack/settings ]]; then
1832 source $dir/devstack/settings
1833 fi
1834 done
1835}
1836
1837# run_plugins
1838#
1839# Run the devstack/plugin.sh in all the plugin directories. These are
1840# run in registration order.
1841function run_plugins {
1842 local mode=$1
1843 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301844
1845 local plugins="${DEVSTACK_PLUGINS}"
1846 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001847 for plugin in ${plugins//,/ }; do
1848 local dir=${GITDIR[$plugin]}
1849 if [[ -f $dir/devstack/plugin.sh ]]; then
1850 source $dir/devstack/plugin.sh $mode $phase
1851 fi
1852 done
1853}
1854
1855function run_phase {
1856 local mode=$1
1857 local phase=$2
1858 if [[ -d $TOP_DIR/extras.d ]]; then
1859 for i in $TOP_DIR/extras.d/*.sh; do
1860 [[ -r $i ]] && source $i $mode $phase
1861 done
1862 fi
1863 # the source phase corresponds to settings loading in plugins
1864 if [[ "$mode" == "source" ]]; then
1865 load_plugin_settings
1866 else
1867 run_plugins $mode $phase
1868 fi
1869}
1870
Dean Troyerdff49a22014-01-30 15:37:40 -06001871
1872# Service Functions
1873# =================
1874
1875# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1876# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001877function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001878 echo "$1" | sed -e '
1879 s/,,/,/g;
1880 s/^,//;
1881 s/,$//
1882 '
1883}
1884
1885# disable_all_services() removes all current services
1886# from ``ENABLED_SERVICES`` to reset the configuration
1887# before a minimal installation
1888# Uses global ``ENABLED_SERVICES``
1889# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001890function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001891 ENABLED_SERVICES=""
1892}
1893
1894# Remove all services starting with '-'. For example, to install all default
1895# services except rabbit (rabbit) set in ``localrc``:
1896# ENABLED_SERVICES+=",-rabbit"
1897# Uses global ``ENABLED_SERVICES``
1898# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001899function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001900 local tmpsvcs="${ENABLED_SERVICES}"
1901 local service
1902 for service in ${tmpsvcs//,/ }; do
1903 if [[ ${service} == -* ]]; then
1904 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1905 fi
1906 done
1907 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1908}
1909
1910# disable_service() removes the services passed as argument to the
1911# ``ENABLED_SERVICES`` list, if they are present.
1912#
1913# For example:
1914# disable_service rabbit
1915#
1916# This function does not know about the special cases
1917# for nova, glance, and neutron built into is_service_enabled().
1918# Uses global ``ENABLED_SERVICES``
1919# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001920function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001921 local tmpsvcs=",${ENABLED_SERVICES},"
1922 local service
1923 for service in $@; do
1924 if is_service_enabled $service; then
1925 tmpsvcs=${tmpsvcs//,$service,/,}
1926 fi
1927 done
1928 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1929}
1930
1931# enable_service() adds the services passed as argument to the
1932# ``ENABLED_SERVICES`` list, if they are not already present.
1933#
1934# For example:
1935# enable_service qpid
1936#
1937# This function does not know about the special cases
1938# for nova, glance, and neutron built into is_service_enabled().
1939# Uses global ``ENABLED_SERVICES``
1940# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001941function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001942 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001943 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001944 for service in $@; do
1945 if ! is_service_enabled $service; then
1946 tmpsvcs+=",$service"
1947 fi
1948 done
1949 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1950 disable_negated_services
1951}
1952
1953# is_service_enabled() checks if the service(s) specified as arguments are
1954# enabled by the user in ``ENABLED_SERVICES``.
1955#
1956# Multiple services specified as arguments are ``OR``'ed together; the test
1957# is a short-circuit boolean, i.e it returns on the first match.
1958#
1959# There are special cases for some 'catch-all' services::
1960# **nova** returns true if any service enabled start with **n-**
1961# **cinder** returns true if any service enabled start with **c-**
1962# **ceilometer** returns true if any service enabled start with **ceilometer**
1963# **glance** returns true if any service enabled start with **g-**
1964# **neutron** returns true if any service enabled start with **q-**
1965# **swift** returns true if any service enabled start with **s-**
1966# **trove** returns true if any service enabled start with **tr-**
1967# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1968# **s-** services will be enabled. This will be deprecated in the future.
1969#
1970# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1971# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1972# as enabled in this case.
1973#
1974# Uses global ``ENABLED_SERVICES``
1975# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001976function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001977 local xtrace=$(set +o | grep xtrace)
1978 set +o xtrace
1979 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001980 local services=$@
1981 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001982 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001983 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001984
1985 # Look for top-level 'enabled' function for this service
1986 if type is_${service}_enabled >/dev/null 2>&1; then
1987 # A function exists for this service, use it
1988 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001989 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001990 fi
1991
1992 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1993 # are implemented
1994
Sean Dague45917cc2014-02-24 16:09:14 -05001995 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001996 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001997 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1998 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1999 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
2000 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
2001 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
2002 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
2003 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
2004 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
2005 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05002006 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002007 done
Sean Dague45917cc2014-02-24 16:09:14 -05002008 $xtrace
2009 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06002010}
2011
2012# Toggle enable/disable_service for services that must run exclusive of each other
2013# $1 The name of a variable containing a space-separated list of services
2014# $2 The name of a variable in which to store the enabled service's name
2015# $3 The name of the service to enable
2016function use_exclusive_service {
2017 local options=${!1}
2018 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002019 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06002020 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002021 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06002022 for opt in $options;do
2023 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
2024 done
2025 eval "$out=$selection"
2026 return 0
2027}
2028
2029
Masayuki Igawaf6368d32014-02-20 13:31:26 +09002030# System Functions
2031# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06002032
2033# Only run the command if the target file (the last arg) is not on an
2034# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002035function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05002036 local xtrace=$(set +o | grep xtrace)
2037 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002038 local args=( $@ )
2039 local last
2040 local sudo_cmd
2041 local dir_to_check
2042
2043 let last="${#args[*]} - 1"
2044
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002045 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002046 if [ ! -d "$dir_to_check" ]; then
2047 dir_to_check=`dirname "$dir_to_check"`
2048 fi
2049
2050 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002051 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002052 return 0
2053 fi
2054
2055 if [[ $TRACK_DEPENDS = True ]]; then
2056 sudo_cmd="env"
2057 else
2058 sudo_cmd="sudo"
2059 fi
2060
Sean Dague45917cc2014-02-24 16:09:14 -05002061 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002062 $sudo_cmd $@
2063}
2064
2065# Exit 0 if address is in network or 1 if address is not in network
2066# ip-range is in CIDR notation: 1.2.3.4/20
2067# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002068function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002069 local ip=$1
2070 local range=$2
2071 local masklen=${range#*/}
2072 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2073 local subnet=$(maskip $ip $(cidr2netmask $masklen))
2074 [[ $network == $subnet ]]
2075}
2076
2077# Add a user to a group.
2078# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002079function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002080 local user=$1
2081 local group=$2
2082
2083 if [[ -z "$os_VENDOR" ]]; then
2084 GetOSVersion
2085 fi
2086
2087 # SLE11 and openSUSE 12.2 don't have the usual usermod
2088 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
2089 sudo usermod -a -G "$group" "$user"
2090 else
2091 sudo usermod -A "$group" "$user"
2092 fi
2093}
2094
2095# Convert CIDR notation to a IPv4 netmask
2096# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002097function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002098 local maskpat="255 255 255 255"
2099 local maskdgt="254 252 248 240 224 192 128"
2100 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2101 echo ${1-0}.${2-0}.${3-0}.${4-0}
2102}
2103
2104# Gracefully cp only if source file/dir exists
2105# cp_it source destination
2106function cp_it {
2107 if [ -e $1 ] || [ -d $1 ]; then
2108 cp -pRL $1 $2
2109 fi
2110}
2111
2112# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2113# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2114# ``localrc`` or on the command line if necessary::
2115#
2116# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2117#
2118# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2119
Ian Wienandaee18c72014-02-21 15:35:08 +11002120function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05002121 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002122 export http_proxy=$http_proxy
2123 fi
Sean Dague53753292014-12-04 19:38:15 -05002124 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002125 export https_proxy=$https_proxy
2126 fi
Sean Dague53753292014-12-04 19:38:15 -05002127 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002128 export no_proxy=$no_proxy
2129 fi
2130}
2131
2132# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002133function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06002134 local mount_type=`stat -f -L -c %T $1`
2135 test "$mount_type" == "nfs"
2136}
2137
2138# Return the network portion of the given IP address using netmask
2139# netmask is in the traditional dotted-quad format
2140# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002141function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002142 local ip=$1
2143 local mask=$2
2144 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2145 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2146 echo $subnet
2147}
2148
2149# Service wrapper to restart services
2150# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002151function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002152 if is_ubuntu; then
2153 sudo /usr/sbin/service $1 restart
2154 else
2155 sudo /sbin/service $1 restart
2156 fi
2157}
2158
2159# Only change permissions of a file or directory if it is not on an
2160# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002161function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002162 _safe_permission_operation chmod $@
2163}
2164
2165# Only change ownership of a file or directory if it is not on an NFS
2166# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002167function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002168 _safe_permission_operation chown $@
2169}
2170
2171# Service wrapper to start services
2172# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002173function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002174 if is_ubuntu; then
2175 sudo /usr/sbin/service $1 start
2176 else
2177 sudo /sbin/service $1 start
2178 fi
2179}
2180
2181# Service wrapper to stop services
2182# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002183function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002184 if is_ubuntu; then
2185 sudo /usr/sbin/service $1 stop
2186 else
2187 sudo /sbin/service $1 stop
2188 fi
2189}
2190
2191
2192# Restore xtrace
2193$XTRACE
2194
2195# Local variables:
2196# mode: shell-script
2197# End: