blob: 352102e24ad15236ef86e0bb88659cbe777f8d0b [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Dean Troyerdff49a22014-01-30 15:37:40 -06003# functions-common - Common functions used by DevStack components
4#
5# The canonical copy of this file is maintained in the DevStack repo.
6# All modifications should be made there and then sync'ed to other repos
7# as required.
8#
9# This file is sorted alphabetically within the function groups.
10#
11# - Config Functions
12# - Control Functions
13# - Distro Functions
14# - Git Functions
15# - OpenStack Functions
16# - Package Functions
17# - Process Functions
18# - Python Functions
19# - Service Functions
Masayuki Igawaf6368d32014-02-20 13:31:26 +090020# - System Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060021#
22# The following variables are assumed to be defined by certain functions:
23#
Jamie Lennox51f0de52014-10-20 16:32:34 +020024# - ``GIT_DEPTH``
Dean Troyerdff49a22014-01-30 15:37:40 -060025# - ``ENABLED_SERVICES``
26# - ``ERROR_ON_CLONE``
27# - ``FILES``
28# - ``OFFLINE``
29# - ``PIP_DOWNLOAD_CACHE``
Dean Troyerdff49a22014-01-30 15:37:40 -060030# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090031# - ``REQUIREMENTS_DIR``
32# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060033# - ``TRACK_DEPENDS``
Masayuki Igawad20f6322014-02-28 09:22:37 +090034# - ``UNDO_REQUIREMENTS``
Dean Troyerdff49a22014-01-30 15:37:40 -060035# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050036#
Dean Troyerdff49a22014-01-30 15:37:40 -060037
38# Save trace setting
39XTRACE=$(set +o | grep xtrace)
40set +o xtrace
41
Sean Daguecc524062014-10-01 09:06:43 -040042# Global Config Variables
43declare -A GITREPO
44declare -A GITBRANCH
45declare -A GITDIR
46
Dean Troyerdff49a22014-01-30 15:37:40 -060047
48# Config Functions
49# ================
50
51# Append a new option in an ini file without replacing the old value
52# iniadd config-file section option value1 value2 value3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +110053function iniadd {
Sean Dague45917cc2014-02-24 16:09:14 -050054 local xtrace=$(set +o | grep xtrace)
55 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060056 local file=$1
57 local section=$2
58 local option=$3
59 shift 3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050060
Dean Troyerdff49a22014-01-30 15:37:40 -060061 local values="$(iniget_multiline $file $section $option) $@"
62 iniset_multiline $file $section $option $values
Sean Dague45917cc2014-02-24 16:09:14 -050063 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060064}
65
66# Comment an option in an INI file
67# inicomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110068function inicomment {
Sean Dague45917cc2014-02-24 16:09:14 -050069 local xtrace=$(set +o | grep xtrace)
70 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060071 local file=$1
72 local section=$2
73 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050074
Dean Troyerdff49a22014-01-30 15:37:40 -060075 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -050076 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060077}
78
79# Get an option from an INI file
80# iniget config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110081function iniget {
Sean Dague45917cc2014-02-24 16:09:14 -050082 local xtrace=$(set +o | grep xtrace)
83 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060084 local file=$1
85 local section=$2
86 local option=$3
87 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050088
Dean Troyerdff49a22014-01-30 15:37:40 -060089 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
90 echo ${line#*=}
Sean Dague45917cc2014-02-24 16:09:14 -050091 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060092}
93
94# Get a multiple line option from an INI file
95# iniget_multiline config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110096function iniget_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -050097 local xtrace=$(set +o | grep xtrace)
98 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060099 local file=$1
100 local section=$2
101 local option=$3
102 local values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500103
Dean Troyerdff49a22014-01-30 15:37:40 -0600104 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
105 echo ${values}
Sean Dague45917cc2014-02-24 16:09:14 -0500106 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600107}
108
109# Determinate is the given option present in the INI file
110# ini_has_option config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100111function ini_has_option {
Sean Dague45917cc2014-02-24 16:09:14 -0500112 local xtrace=$(set +o | grep xtrace)
113 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600114 local file=$1
115 local section=$2
116 local option=$3
117 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500118
Dean Troyerdff49a22014-01-30 15:37:40 -0600119 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Sean Dague45917cc2014-02-24 16:09:14 -0500120 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600121 [ -n "$line" ]
122}
123
Robert Li751ad1a2014-10-15 21:40:53 -0400124# Add another config line for a multi-line option.
125# It's normally called after iniset of the same option and assumes
126# that the section already exists.
127#
128# Note that iniset_multiline requires all the 'lines' to be supplied
129# in the argument list. Doing that will cause incorrect configuration
130# if spaces are used in the config values.
131#
132# iniadd_literal config-file section option value
133function iniadd_literal {
134 local xtrace=$(set +o | grep xtrace)
135 set +o xtrace
136 local file=$1
137 local section=$2
138 local option=$3
139 local value=$4
140
141 [[ -z $section || -z $option ]] && return
142
143 # Add it
144 sed -i -e "/^\[$section\]/ a\\
145$option = $value
146" "$file"
147
148 $xtrace
149}
150
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700151function inidelete {
152 local xtrace=$(set +o | grep xtrace)
153 set +o xtrace
154 local file=$1
155 local section=$2
156 local option=$3
157
158 [[ -z $section || -z $option ]] && return
159
160 # Remove old values
161 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
162
163 $xtrace
164}
165
Dean Troyerdff49a22014-01-30 15:37:40 -0600166# Set an option in an INI file
167# iniset config-file section option value
Ian Wienandaee18c72014-02-21 15:35:08 +1100168function iniset {
Sean Dague45917cc2014-02-24 16:09:14 -0500169 local xtrace=$(set +o | grep xtrace)
170 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600171 local file=$1
172 local section=$2
173 local option=$3
174 local value=$4
175
176 [[ -z $section || -z $option ]] && return
177
178 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
179 # Add section at the end
180 echo -e "\n[$section]" >>"$file"
181 fi
182 if ! ini_has_option "$file" "$section" "$option"; then
183 # Add it
184 sed -i -e "/^\[$section\]/ a\\
185$option = $value
186" "$file"
187 else
188 local sep=$(echo -ne "\x01")
189 # Replace it
190 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
191 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500192 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600193}
194
195# Set a multiple line option in an INI file
196# iniset_multiline config-file section option value1 value2 valu3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +1100197function iniset_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -0500198 local xtrace=$(set +o | grep xtrace)
199 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600200 local file=$1
201 local section=$2
202 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500203
Dean Troyerdff49a22014-01-30 15:37:40 -0600204 shift 3
205 local values
206 for v in $@; do
207 # The later sed command inserts each new value in the line next to
208 # the section identifier, which causes the values to be inserted in
209 # the reverse order. Do a reverse here to keep the original order.
210 values="$v ${values}"
211 done
212 if ! grep -q "^\[$section\]" "$file"; then
213 # Add section at the end
214 echo -e "\n[$section]" >>"$file"
215 else
216 # Remove old values
217 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
218 fi
219 # Add new ones
220 for v in $values; do
221 sed -i -e "/^\[$section\]/ a\\
222$option = $v
223" "$file"
224 done
Sean Dague45917cc2014-02-24 16:09:14 -0500225 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600226}
227
228# Uncomment an option in an INI file
229# iniuncomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100230function iniuncomment {
Sean Dague45917cc2014-02-24 16:09:14 -0500231 local xtrace=$(set +o | grep xtrace)
232 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600233 local file=$1
234 local section=$2
235 local option=$3
236 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500237 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600238}
239
240# Normalize config values to True or False
241# Accepts as False: 0 no No NO false False FALSE
242# Accepts as True: 1 yes Yes YES true True TRUE
243# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100244function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500245 local xtrace=$(set +o | grep xtrace)
246 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600247 local default=$1
248 local testval=$2
249
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
257
258# Control Functions
259# =================
260
261# Prints backtrace info
262# filename:lineno:function
263# backtrace level
264function backtrace {
265 local level=$1
266 local deep=$((${#BASH_SOURCE[@]} - 1))
267 echo "[Call Trace]"
268 while [ $level -le $deep ]; do
269 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
270 deep=$((deep - 1))
271 done
272}
273
274# Prints line number and "message" then exits
275# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100276function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600277 local exitcode=$?
278 set +o xtrace
279 local line=$1; shift
280 if [ $exitcode == 0 ]; then
281 exitcode=1
282 fi
283 backtrace 2
284 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600285 # Give buffers a second to flush
286 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600287 exit $exitcode
288}
289
290# Checks an environment variable is not set or has length 0 OR if the
291# exit code is non-zero and prints "message" and exits
292# NOTE: env-var is the variable name without a '$'
293# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100294function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600295 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500296 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600297 set +o xtrace
298 local line=$1; shift
299 local evar=$1; shift
300 if ! is_set $evar || [ $exitcode != 0 ]; then
301 die $line "$*"
302 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500303 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600304}
305
306# Prints line number and "message" in error format
307# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100308function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600309 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500310 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600311 set +o xtrace
312 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
313 echo $msg 1>&2;
314 if [[ -n ${SCREEN_LOGDIR} ]]; then
315 echo $msg >> "${SCREEN_LOGDIR}/error.log"
316 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500317 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600318 return $exitcode
319}
320
321# Checks an environment variable is not set or has length 0 OR if the
322# exit code is non-zero and prints "message"
323# NOTE: env-var is the variable name without a '$'
324# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100325function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600326 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500327 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600328 set +o xtrace
329 local line=$1; shift
330 local evar=$1; shift
331 if ! is_set $evar || [ $exitcode != 0 ]; then
332 err $line "$*"
333 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500334 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600335 return $exitcode
336}
337
338# Exit after outputting a message about the distribution not being supported.
339# exit_distro_not_supported [optional-string-telling-what-is-missing]
340function exit_distro_not_supported {
341 if [[ -z "$DISTRO" ]]; then
342 GetDistro
343 fi
344
345 if [ $# -gt 0 ]; then
346 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
347 else
348 die $LINENO "Support for $DISTRO is incomplete."
349 fi
350}
351
352# Test if the named environment variable is set and not zero length
353# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100354function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600355 local var=\$"$1"
356 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
357}
358
359# Prints line number and "message" in warning format
360# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100361function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600362 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500363 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600364 set +o xtrace
365 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
366 echo $msg 1>&2;
367 if [[ -n ${SCREEN_LOGDIR} ]]; then
368 echo $msg >> "${SCREEN_LOGDIR}/error.log"
369 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500370 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600371 return $exitcode
372}
373
374
375# Distro Functions
376# ================
377
378# Determine OS Vendor, Release and Update
379# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
380# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500381# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
382# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
383# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
384# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
385# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
386declare os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
387
Dean Troyerdff49a22014-01-30 15:37:40 -0600388# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100389function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500390
Dean Troyerdff49a22014-01-30 15:37:40 -0600391 # Figure out which vendor we are
392 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
393 # OS/X
394 os_VENDOR=`sw_vers -productName`
395 os_RELEASE=`sw_vers -productVersion`
396 os_UPDATE=${os_RELEASE##*.}
397 os_RELEASE=${os_RELEASE%.*}
398 os_PACKAGE=""
399 if [[ "$os_RELEASE" =~ "10.7" ]]; then
400 os_CODENAME="lion"
401 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
402 os_CODENAME="snow leopard"
403 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
404 os_CODENAME="leopard"
405 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
406 os_CODENAME="tiger"
407 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
408 os_CODENAME="panther"
409 else
410 os_CODENAME=""
411 fi
412 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
413 os_VENDOR=$(lsb_release -i -s)
414 os_RELEASE=$(lsb_release -r -s)
415 os_UPDATE=""
416 os_PACKAGE="rpm"
417 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
418 os_PACKAGE="deb"
419 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
420 lsb_release -d -s | grep -q openSUSE
421 if [[ $? -eq 0 ]]; then
422 os_VENDOR="openSUSE"
423 fi
424 elif [[ $os_VENDOR == "openSUSE project" ]]; then
425 os_VENDOR="openSUSE"
426 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
427 os_VENDOR="Red Hat"
428 fi
429 os_CODENAME=$(lsb_release -c -s)
430 elif [[ -r /etc/redhat-release ]]; then
431 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
432 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
433 # CentOS release 5.5 (Final)
434 # CentOS Linux release 6.0 (Final)
435 # Fedora release 16 (Verne)
436 # XenServer release 6.2.0-70446c (xenenterprise)
437 os_CODENAME=""
438 for r in "Red Hat" CentOS Fedora XenServer; do
439 os_VENDOR=$r
440 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
441 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
442 os_CODENAME=${ver#*|}
443 os_RELEASE=${ver%|*}
444 os_UPDATE=${os_RELEASE##*.}
445 os_RELEASE=${os_RELEASE%.*}
446 break
447 fi
448 os_VENDOR=""
449 done
450 os_PACKAGE="rpm"
451 elif [[ -r /etc/SuSE-release ]]; then
452 for r in openSUSE "SUSE Linux"; do
453 if [[ "$r" = "SUSE Linux" ]]; then
454 os_VENDOR="SUSE LINUX"
455 else
456 os_VENDOR=$r
457 fi
458
459 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
460 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
461 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
462 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
463 break
464 fi
465 os_VENDOR=""
466 done
467 os_PACKAGE="rpm"
468 # If lsb_release is not installed, we should be able to detect Debian OS
469 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
470 os_VENDOR="Debian"
471 os_PACKAGE="deb"
472 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
473 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
474 fi
475 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
476}
477
478# Translate the OS version values into common nomenclature
479# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500480declare DISTRO
481
Ian Wienandaee18c72014-02-21 15:35:08 +1100482function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600483 GetOSVersion
484 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
485 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
486 DISTRO=$os_CODENAME
487 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
488 # For Fedora, just use 'f' and the release
489 DISTRO="f$os_RELEASE"
490 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
491 DISTRO="opensuse-$os_RELEASE"
492 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
493 # For SLE, also use the service pack
494 if [[ -z "$os_UPDATE" ]]; then
495 DISTRO="sle${os_RELEASE}"
496 else
497 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
498 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530499 elif [[ "$os_VENDOR" =~ (Red Hat) || \
500 "$os_VENDOR" =~ (CentOS) || \
501 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600502 # Drop the . release as we assume it's compatible
503 DISTRO="rhel${os_RELEASE::1}"
504 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
505 DISTRO="xs$os_RELEASE"
506 else
507 # Catch-all for now is Vendor + Release + Update
508 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
509 fi
510 export DISTRO
511}
512
513# Utility function for checking machine architecture
514# is_arch arch-type
515function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500516 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600517}
518
Ian Wienandbdc90c52014-08-04 15:44:58 +1000519# Quick check for a rackspace host; n.b. rackspace provided images
520# have these Xen tools installed but a custom image may not.
521function is_rackspace {
522 [ -f /usr/bin/xenstore-ls ] && \
523 sudo /usr/bin/xenstore-ls vm-data | grep -q "Rackspace"
524}
525
Dean Troyerdff49a22014-01-30 15:37:40 -0600526# Determine if current distribution is a Fedora-based distribution
527# (Fedora, RHEL, CentOS, etc).
528# is_fedora
529function is_fedora {
530 if [[ -z "$os_VENDOR" ]]; then
531 GetOSVersion
532 fi
533
anju Tiwari6c639c92014-07-15 18:11:54 +0530534 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
535 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600536}
537
538
539# Determine if current distribution is a SUSE-based distribution
540# (openSUSE, SLE).
541# is_suse
542function is_suse {
543 if [[ -z "$os_VENDOR" ]]; then
544 GetOSVersion
545 fi
546
547 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
548}
549
550
551# Determine if current distribution is an Ubuntu-based distribution
552# It will also detect non-Ubuntu but Debian-based distros
553# is_ubuntu
554function is_ubuntu {
555 if [[ -z "$os_PACKAGE" ]]; then
556 GetOSVersion
557 fi
558 [ "$os_PACKAGE" = "deb" ]
559}
560
561
562# Git Functions
563# =============
564
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600565# Returns openstack release name for a given branch name
566# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100567function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600568 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700569 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600570 echo ${branch#*/}
571 else
572 echo "master"
573 fi
574}
575
Dean Troyerdff49a22014-01-30 15:37:40 -0600576# git clone only if directory doesn't exist already. Since ``DEST`` might not
577# be owned by the installation user, we create the directory and change the
578# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500579# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
580# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600581# does not exist (default is False, meaning the repo will be cloned).
Jamie Lennox51f0de52014-10-20 16:32:34 +0200582# Set global ``GIT_DEPTH=<number>`` to limit the history depth of the git clone
583# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``, ``GIT_DEPTH``
Dean Troyerdff49a22014-01-30 15:37:40 -0600584# git_clone remote dest-dir branch
585function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500586 local git_remote=$1
587 local git_dest=$2
588 local git_ref=$3
589 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200590 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500591
Dean Troyerdff49a22014-01-30 15:37:40 -0600592 RECLONE=$(trueorfalse False $RECLONE)
593
YAMAMOTO Takashi292b2a72014-10-31 13:48:58 +0900594 if [[ -n "${GIT_DEPTH}" ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200595 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
596 fi
597
Dean Troyerdff49a22014-01-30 15:37:40 -0600598 if [[ "$OFFLINE" = "True" ]]; then
599 echo "Running in offline mode, clones already exist"
600 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500601 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600602 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400603 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600604 return
605 fi
606
Dean Troyer50cda692014-07-25 11:57:20 -0500607 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600608 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500609 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600610 [[ "$ERROR_ON_CLONE" = "True" ]] && \
611 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200612 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600613 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500614 cd $git_dest
615 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600616 else
617 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500618 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600619 [[ "$ERROR_ON_CLONE" = "True" ]] && \
620 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200621 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500622 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600623 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500624 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600625 elif [[ "$RECLONE" = "True" ]]; then
626 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500627 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600628 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500629 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100630 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600631 # remove the existing ignored files (like pyc) as they cause breakage
632 # (due to the py files having older timestamps than our pyc, so python
633 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500634 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600635
Dean Troyer50cda692014-07-25 11:57:20 -0500636 # handle git_ref accordingly to type (tag, branch)
637 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
638 git_update_tag $git_ref
639 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
640 git_update_branch $git_ref
641 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
642 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600643 else
Dean Troyer50cda692014-07-25 11:57:20 -0500644 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600645 fi
646
647 fi
648 fi
649
650 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500651 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600652 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400653 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600654}
655
Sean Daguecc524062014-10-01 09:06:43 -0400656# A variation on git clone that lets us specify a project by it's
657# actual name, like oslo.config. This is exceptionally useful in the
658# library installation case
659function git_clone_by_name {
660 local name=$1
661 local repo=${GITREPO[$name]}
662 local dir=${GITDIR[$name]}
663 local branch=${GITBRANCH[$name]}
664 git_clone $repo $dir $branch
665}
666
667
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100668# git can sometimes get itself infinitely stuck with transient network
669# errors or other issues with the remote end. This wraps git in a
670# timeout/retry loop and is intended to watch over non-local git
671# processes that might hang. GIT_TIMEOUT, if set, is passed directly
672# to timeout(1); otherwise the default value of 0 maintains the status
673# quo of waiting forever.
674# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100675function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100676 local count=0
677 local timeout=0
678
679 if [[ -n "${GIT_TIMEOUT}" ]]; then
680 timeout=${GIT_TIMEOUT}
681 fi
682
683 until timeout -s SIGINT ${timeout} git "$@"; do
684 # 124 is timeout(1)'s special return code when it reached the
685 # timeout; otherwise assume fatal failure
686 if [[ $? -ne 124 ]]; then
687 die $LINENO "git call failed: [git $@]"
688 fi
689
690 count=$(($count + 1))
691 warn "timeout ${count} for git call: [git $@]"
692 if [ $count -eq 3 ]; then
693 die $LINENO "Maximum of 3 git retries reached"
694 fi
695 sleep 5
696 done
697}
698
Dean Troyerdff49a22014-01-30 15:37:40 -0600699# git update using reference as a branch.
700# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100701function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500702 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600703
Dean Troyer50cda692014-07-25 11:57:20 -0500704 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600705 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500706 git branch -D $git_branch || true
707 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600708}
709
710# git update using reference as a branch.
711# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100712function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500713 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600714
Dean Troyer50cda692014-07-25 11:57:20 -0500715 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600716}
717
718# git update using reference as a tag. Be careful editing source at that repo
719# as working copy will be in a detached mode
720# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100721function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500722 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600723
Dean Troyer50cda692014-07-25 11:57:20 -0500724 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600725 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500726 git_timed fetch origin tag $git_tag
727 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600728}
729
730
731# OpenStack Functions
732# ===================
733
734# Get the default value for HOST_IP
735# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100736function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600737 local fixed_range=$1
738 local floating_range=$2
739 local host_ip_iface=$3
740 local host_ip=$4
741
742 # Find the interface used for the default route
743 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
744 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
745 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
746 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500747 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
748 local ip
749 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600750 # Attempt to filter out IP addresses that are part of the fixed and
751 # floating range. Note that this method only works if the ``netaddr``
752 # python library is installed. If it is not installed, an error
753 # will be printed and the first IP from the interface will be used.
754 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
755 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500756 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
757 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600758 break;
759 fi
760 done
761 fi
762 echo $host_ip
763}
764
Attila Fazekasf71b5002014-05-28 09:52:22 +0200765# Generates hex string from ``size`` byte of pseudo random data
766# generate_hex_string size
767function generate_hex_string {
768 local size=$1
769 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
770}
771
Dean Troyerdff49a22014-01-30 15:37:40 -0600772# Grab a numbered field from python prettytable output
773# Fields are numbered starting with 1
774# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
775# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100776function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500777 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600778 while read data; do
779 if [ "$1" -lt 0 ]; then
780 field="(\$(NF$1))"
781 else
782 field="\$$(($1 + 1))"
783 fi
784 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
785 done
786}
787
788# Add a policy to a policy.json file
789# Do nothing if the policy already exists
790# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100791function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600792 local policy_file=$1
793 local policy_name=$2
794 local policy_perm=$3
795
796 if grep -q ${policy_name} ${policy_file}; then
797 echo "Policy ${policy_name} already exists in ${policy_file}"
798 return
799 fi
800
801 # Add a terminating comma to policy lines without one
802 # Remove the closing '}' and all lines following to the end-of-file
803 local tmpfile=$(mktemp)
804 uniq ${policy_file} | sed -e '
805 s/]$/],/
806 /^[}]/,$d
807 ' > ${tmpfile}
808
809 # Append policy and closing brace
810 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
811 echo "}" >>${tmpfile}
812
813 mv ${tmpfile} ${policy_file}
814}
815
Alistair Coles24779f62014-10-15 18:57:59 +0100816# Gets or creates a domain
817# Usage: get_or_create_domain <name> <description>
818function get_or_create_domain {
819 local os_url="$KEYSTONE_SERVICE_URI/v3"
820 # Gets domain id
821 local domain_id=$(
822 # Gets domain id
823 openstack --os-token=$OS_TOKEN --os-url=$os_url \
824 --os-identity-api-version=3 domain show $1 \
825 -f value -c id 2>/dev/null ||
826 # Creates new domain
827 openstack --os-token=$OS_TOKEN --os-url=$os_url \
828 --os-identity-api-version=3 domain create $1 \
829 --description "$2" \
830 -f value -c id
831 )
832 echo $domain_id
833}
834
Bartosz Górski0abde392014-02-28 14:15:19 +0100835# Gets or creates user
Alistair Coles24779f62014-10-15 18:57:59 +0100836# Usage: get_or_create_user <username> <password> <project> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100837function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200838 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500839 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200840 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500841 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200842 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100843 local os_cmd="openstack"
844 local domain=""
845 if [[ ! -z "$5" ]]; then
846 domain="--domain=$5"
847 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
848 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100849 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500850 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500851 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100852 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100853 $1 \
854 --password "$2" \
855 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500856 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100857 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500858 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100859 -f value -c id
860 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500861 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100862}
863
864# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100865# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100866function get_or_create_project {
867 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100868 local os_cmd="openstack"
869 local domain=""
870 if [[ ! -z "$2" ]]; then
871 domain="--domain=$2"
872 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
873 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500874 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500875 # Creates new project with --or-show
876 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100877 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500878 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100879}
880
881# Gets or creates role
882# Usage: get_or_create_role <name>
883function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500884 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500885 # Creates role with --or-show
886 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100887 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500888 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100889}
890
891# Gets or adds user role
892# Usage: get_or_add_user_role <role> <user> <project>
893function get_or_add_user_role {
894 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500895 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100896 $2 \
897 --project $3 \
898 --column "ID" \
899 --column "Name" \
900 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500901 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100902 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500903 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100904 $1 \
905 --user $2 \
906 --project $3 \
907 | grep " id " | get_field 2)
908 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500909 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100910}
911
912# Gets or creates service
913# Usage: get_or_create_service <name> <type> <description>
914function get_or_create_service {
915 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500916 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100917 # Gets service id
918 openstack service show $1 -f value -c id 2>/dev/null ||
919 # Creates new service if not exists
920 openstack service create \
921 $1 \
922 --type=$2 \
923 --description="$3" \
924 -f value -c id
925 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500926 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100927}
928
929# Gets or creates endpoint
930# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
931function get_or_create_endpoint {
932 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500933 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100934 --column "ID" \
935 --column "Region" \
936 --column "Service Name" \
937 | grep " $2 " \
938 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500939 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100940 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500941 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100942 $1 \
943 --region $2 \
944 --publicurl $3 \
945 --adminurl $4 \
946 --internalurl $5 \
947 | grep " id " | get_field 2)
948 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500949 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100950}
Dean Troyerdff49a22014-01-30 15:37:40 -0600951
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500952
Dean Troyerdff49a22014-01-30 15:37:40 -0600953# Package Functions
954# =================
955
956# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100957function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600958 local pkg_dir
959 if is_ubuntu; then
Monty Taylor81a016d2014-11-15 17:18:13 -0300960 pkg_dir=$FILES/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600961 elif is_fedora; then
962 pkg_dir=$FILES/rpms
963 elif is_suse; then
964 pkg_dir=$FILES/rpms-suse
965 else
966 exit_distro_not_supported "list of packages"
967 fi
968 echo "$pkg_dir"
969}
970
971# Wrapper for ``apt-get`` to set cache and proxy environment variables
972# Uses globals ``OFFLINE``, ``*_proxy``
973# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100974function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500975 local xtrace=$(set +o | grep xtrace)
976 set +o xtrace
977
Dean Troyerdff49a22014-01-30 15:37:40 -0600978 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
979 local sudo="sudo"
980 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500981
982 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600983 $sudo DEBIAN_FRONTEND=noninteractive \
984 http_proxy=$http_proxy https_proxy=$https_proxy \
985 no_proxy=$no_proxy \
986 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
987}
988
989# get_packages() collects a list of package names of any type from the
Monty Taylor81a016d2014-11-15 17:18:13 -0300990# prerequisite files in ``files/{debs|rpms}``. The list is intended
Dean Troyerdff49a22014-01-30 15:37:40 -0600991# to be passed to a package installer such as apt or yum.
992#
993# Only packages required for the services in 1st argument will be
994# included. Two bits of metadata are recognized in the prerequisite files:
995#
996# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
997# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
998# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100999function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -05001000 local xtrace=$(set +o | grep xtrace)
1001 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001002 local services=$@
1003 local package_dir=$(_get_package_dir)
1004 local file_to_parse
1005 local service
1006
Flavio Percoco5a91c352014-10-31 18:48:00 +01001007 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
1008
Dean Troyerdff49a22014-01-30 15:37:40 -06001009 if [[ -z "$package_dir" ]]; then
1010 echo "No package directory supplied"
1011 return 1
1012 fi
1013 if [[ -z "$DISTRO" ]]; then
1014 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -05001015 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -06001016 fi
1017 for service in ${services//,/ }; do
1018 # Allow individual services to specify dependencies
1019 if [[ -e ${package_dir}/${service} ]]; then
1020 file_to_parse="${file_to_parse} $service"
1021 fi
1022 # NOTE(sdague) n-api needs glance for now because that's where
1023 # glance client is
1024 if [[ $service == n-api ]]; then
1025 if [[ ! $file_to_parse =~ nova ]]; then
1026 file_to_parse="${file_to_parse} nova"
1027 fi
1028 if [[ ! $file_to_parse =~ glance ]]; then
1029 file_to_parse="${file_to_parse} glance"
1030 fi
1031 elif [[ $service == c-* ]]; then
1032 if [[ ! $file_to_parse =~ cinder ]]; then
1033 file_to_parse="${file_to_parse} cinder"
1034 fi
1035 elif [[ $service == ceilometer-* ]]; then
1036 if [[ ! $file_to_parse =~ ceilometer ]]; then
1037 file_to_parse="${file_to_parse} ceilometer"
1038 fi
1039 elif [[ $service == s-* ]]; then
1040 if [[ ! $file_to_parse =~ swift ]]; then
1041 file_to_parse="${file_to_parse} swift"
1042 fi
1043 elif [[ $service == n-* ]]; then
1044 if [[ ! $file_to_parse =~ nova ]]; then
1045 file_to_parse="${file_to_parse} nova"
1046 fi
1047 elif [[ $service == g-* ]]; then
1048 if [[ ! $file_to_parse =~ glance ]]; then
1049 file_to_parse="${file_to_parse} glance"
1050 fi
1051 elif [[ $service == key* ]]; then
1052 if [[ ! $file_to_parse =~ keystone ]]; then
1053 file_to_parse="${file_to_parse} keystone"
1054 fi
1055 elif [[ $service == q-* ]]; then
1056 if [[ ! $file_to_parse =~ neutron ]]; then
1057 file_to_parse="${file_to_parse} neutron"
1058 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001059 elif [[ $service == ir-* ]]; then
1060 if [[ ! $file_to_parse =~ ironic ]]; then
1061 file_to_parse="${file_to_parse} ironic"
1062 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001063 fi
1064 done
1065
1066 for file in ${file_to_parse}; do
1067 local fname=${package_dir}/${file}
1068 local OIFS line package distros distro
1069 [[ -e $fname ]] || continue
1070
1071 OIFS=$IFS
1072 IFS=$'\n'
1073 for line in $(<${fname}); do
1074 if [[ $line =~ "NOPRIME" ]]; then
1075 continue
1076 fi
1077
1078 # Assume we want this package
1079 package=${line%#*}
1080 inst_pkg=1
1081
1082 # Look for # dist:xxx in comment
1083 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1084 # We are using BASH regexp matching feature.
1085 package=${BASH_REMATCH[1]}
1086 distros=${BASH_REMATCH[2]}
1087 # In bash ${VAR,,} will lowecase VAR
1088 # Look for a match in the distro list
1089 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1090 # If no match then skip this package
1091 inst_pkg=0
1092 fi
1093 fi
1094
1095 # Look for # testonly in comment
1096 if [[ $line =~ (.*)#.*testonly.* ]]; then
1097 package=${BASH_REMATCH[1]}
1098 # Are we installing test packages? (test for the default value)
1099 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1100 # If not installing test packages the skip this package
1101 inst_pkg=0
1102 fi
1103 fi
1104
1105 if [[ $inst_pkg = 1 ]]; then
1106 echo $package
1107 fi
1108 done
1109 IFS=$OIFS
1110 done
Sean Dague45917cc2014-02-24 16:09:14 -05001111 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001112}
1113
1114# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001115# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001116# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001117function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001118 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001119 return 0
1120 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001121
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001122 if is_ubuntu; then
1123 local xtrace=$(set +o | grep xtrace)
1124 set +o xtrace
1125 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1126 # if there are transient errors pulling the updates, that's fine.
1127 # It may be secondary repositories that we don't really care about.
1128 apt_get update || /bin/true
1129 REPOS_UPDATED=True
1130 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001131 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001132 fi
1133}
1134
1135function real_install_package {
1136 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001137 apt_get install "$@"
1138 elif is_fedora; then
1139 yum_install "$@"
1140 elif is_suse; then
1141 zypper_install "$@"
1142 else
1143 exit_distro_not_supported "installing packages"
1144 fi
1145}
1146
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001147# Distro-agnostic package installer
1148# install_package package [package ...]
1149function install_package {
1150 update_package_repo
1151 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1152}
1153
Dean Troyerdff49a22014-01-30 15:37:40 -06001154# Distro-agnostic function to tell if a package is installed
1155# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001156function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001157 if [[ -z "$@" ]]; then
1158 return 1
1159 fi
1160
1161 if [[ -z "$os_PACKAGE" ]]; then
1162 GetOSVersion
1163 fi
1164
1165 if [[ "$os_PACKAGE" = "deb" ]]; then
1166 dpkg -s "$@" > /dev/null 2> /dev/null
1167 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1168 rpm --quiet -q "$@"
1169 else
1170 exit_distro_not_supported "finding if a package is installed"
1171 fi
1172}
1173
1174# Distro-agnostic package uninstaller
1175# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001176function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001177 if is_ubuntu; then
1178 apt_get purge "$@"
1179 elif is_fedora; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001180 sudo $YUM remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001181 elif is_suse; then
1182 sudo zypper rm "$@"
1183 else
1184 exit_distro_not_supported "uninstalling packages"
1185 fi
1186}
1187
1188# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001189# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001190# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001191function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001192 [[ "$OFFLINE" = "True" ]] && return
1193 local sudo="sudo"
1194 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001195
1196 # The manual check for missing packages is because yum -y assumes
1197 # missing packages are OK. See
1198 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001199 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1200 no_proxy=$no_proxy \
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001201 $YUM install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001202 awk '
1203 BEGIN { fail=0 }
1204 /No package/ { fail=1 }
1205 { print }
1206 END { exit fail }' || \
1207 die $LINENO "Missing packages detected"
1208
1209 # also ensure we catch a yum failure
1210 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001211 die $LINENO "$YUM install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001212 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001213}
1214
1215# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001216# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001217# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001218function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001219 [[ "$OFFLINE" = "True" ]] && return
1220 local sudo="sudo"
1221 [[ "$(id -u)" = "0" ]] && sudo="env"
1222 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1223 zypper --non-interactive install --auto-agree-with-licenses "$@"
1224}
1225
1226
1227# Process Functions
1228# =================
1229
1230# _run_process() is designed to be backgrounded by run_process() to simulate a
1231# fork. It includes the dirty work of closing extra filehandles and preparing log
1232# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001233# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001234# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001235# If an optional group is provided sg will be used to set the group of
1236# the command.
1237# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001238function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001239 local service=$1
1240 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001241 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001242
1243 # Undo logging redirections and close the extra descriptors
1244 exec 1>&3
1245 exec 2>&3
1246 exec 3>&-
1247 exec 6>&-
1248
1249 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001250 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1251 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001252
1253 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1254 export PYTHONUNBUFFERED=1
1255 fi
1256
Dean Troyer3159a822014-08-27 14:13:58 -05001257 # Run under ``setsid`` to force the process to become a session and group leader.
1258 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001259 if [[ -n "$group" ]]; then
1260 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1261 else
1262 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1263 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001264
1265 # Just silently exit this process
1266 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001267}
1268
1269# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1270# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001271# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001272# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001273function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001274 SCREEN_NAME=${SCREEN_NAME:-stack}
1275 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1276
1277 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1278 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1279 fi
1280
1281 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1282}
1283
1284# Find out if a process exists by partial name.
1285# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001286function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001287 local name=$1
1288 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001289 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001290 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001291 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001292}
1293
Dean Troyer3159a822014-08-27 14:13:58 -05001294# Run a single service under screen or directly
1295# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001296# If an optional group is provided sg will be used to run the
1297# command as that group.
1298# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001299function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001300 local service=$1
1301 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001302 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001303
Dean Troyer3159a822014-08-27 14:13:58 -05001304 if is_service_enabled $service; then
1305 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001306 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001307 else
1308 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001309 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001310 fi
1311 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001312}
1313
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001314# Helper to launch a process in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001315# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1316# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001317# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001318# Run a command in a shell in a screen window, if an optional group
1319# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001320function screen_process {
1321 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001322 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001323 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001324
Sean Dagueea22a4f2014-06-27 15:21:41 -04001325 SCREEN_NAME=${SCREEN_NAME:-stack}
1326 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1327 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001328
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001329 # Append the process to the screen rc file
1330 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001331
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001332 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001333
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001334 if [[ -n ${SCREEN_LOGDIR} ]]; then
1335 screen -S $SCREEN_NAME -p $name -X logfile ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log
1336 screen -S $SCREEN_NAME -p $name -X log on
1337 ln -sf ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${name}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001338 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001339
1340 # sleep to allow bash to be ready to be send the command - we are
1341 # creating a new window in screen and then sends characters, so if
1342 # bash isn't running by the time we send the command, nothing happens
1343 sleep 3
1344
1345 NL=`echo -ne '\015'`
1346 # This fun command does the following:
1347 # - the passed server command is backgrounded
1348 # - the pid of the background process is saved in the usual place
1349 # - the server process is brought back to the foreground
1350 # - if the server process exits prematurely the fg command errors
1351 # and a message is written to stdout and the process failure file
1352 #
1353 # The pid saved can be used in stop_process() as a process group
1354 # id to kill off all child processes
1355 if [[ -n "$group" ]]; then
1356 command="sg $group '$command'"
1357 fi
1358 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 -06001359}
1360
1361# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001362# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001363# screen_rc service "command-line"
1364function screen_rc {
1365 SCREEN_NAME=${SCREEN_NAME:-stack}
1366 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1367 if [[ ! -e $SCREENRC ]]; then
1368 # Name the screen session
1369 echo "sessionname $SCREEN_NAME" > $SCREENRC
1370 # Set a reasonable statusbar
1371 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1372 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1373 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1374 echo "screen -t shell bash" >> $SCREENRC
1375 fi
1376 # If this service doesn't already exist in the screenrc file
1377 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1378 NL=`echo -ne '\015'`
1379 echo "screen -t $1 bash" >> $SCREENRC
1380 echo "stuff \"$2$NL\"" >> $SCREENRC
1381
1382 if [[ -n ${SCREEN_LOGDIR} ]]; then
1383 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1384 echo "log on" >>$SCREENRC
1385 fi
1386 fi
1387}
1388
1389# Stop a service in screen
1390# If a PID is available use it, kill the whole process group via TERM
1391# If screen is being used kill the screen window; this will catch processes
1392# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001393# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001394# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001395function screen_stop_service {
1396 local service=$1
1397
Dean Troyerdff49a22014-01-30 15:37:40 -06001398 SCREEN_NAME=${SCREEN_NAME:-stack}
1399 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1400 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1401
Dean Troyer3159a822014-08-27 14:13:58 -05001402 if is_service_enabled $service; then
1403 # Clean up the screen window
1404 screen -S $SCREEN_NAME -p $service -X kill
1405 fi
1406}
1407
1408# Stop a service process
1409# If a PID is available use it, kill the whole process group via TERM
1410# If screen is being used kill the screen window; this will catch processes
1411# that did not leave a PID behind
1412# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1413# stop_process service
1414function stop_process {
1415 local service=$1
1416
1417 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1418 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1419
1420 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001421 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001422 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1423 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1424 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001425 fi
1426 if [[ "$USE_SCREEN" = "True" ]]; then
1427 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001428 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001429 fi
1430 fi
1431}
1432
1433# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001434# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001435# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001436function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001437 local service
1438 local failures
1439 SCREEN_NAME=${SCREEN_NAME:-stack}
1440 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1441
1442
1443 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1444 echo "No service status directory found"
1445 return
1446 fi
1447
1448 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001449 # make this -o errexit safe
1450 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001451
1452 for service in $failures; do
1453 service=`basename $service`
1454 service=${service%.failure}
1455 echo "Error: Service $service is not running"
1456 done
1457
1458 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001459 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001460 fi
1461}
1462
Chris Dent2f27a0e2014-09-09 13:46:02 +01001463# Tail a log file in a screen if USE_SCREEN is true.
1464function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001465 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001466 local logfile=$2
1467
1468 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1469 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001470 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001471 fi
1472}
1473
Dean Troyerdff49a22014-01-30 15:37:40 -06001474
Dean Troyer3159a822014-08-27 14:13:58 -05001475# Deprecated Functions
1476# --------------------
1477
1478# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1479# fork. It includes the dirty work of closing extra filehandles and preparing log
1480# files to produce the same logs as screen_it(). The log filename is derived
1481# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1482# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1483# _old_run_process service "command-line"
1484function _old_run_process {
1485 local service=$1
1486 local command="$2"
1487
1488 # Undo logging redirections and close the extra descriptors
1489 exec 1>&3
1490 exec 2>&3
1491 exec 3>&-
1492 exec 6>&-
1493
1494 if [[ -n ${SCREEN_LOGDIR} ]]; then
1495 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1496 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1497
1498 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1499 export PYTHONUNBUFFERED=1
1500 fi
1501
1502 exec /bin/bash -c "$command"
1503 die "$service exec failure: $command"
1504}
1505
1506# old_run_process() launches a child process that closes all file descriptors and
1507# then exec's the passed in command. This is meant to duplicate the semantics
1508# of screen_it() without screen. PIDs are written to
1509# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1510# old_run_process service "command-line"
1511function old_run_process {
1512 local service=$1
1513 local command="$2"
1514
1515 # Spawn the child process
1516 _old_run_process "$service" "$command" &
1517 echo $!
1518}
1519
1520# Compatibility for existing start_XXXX() functions
1521# Uses global ``USE_SCREEN``
1522# screen_it service "command-line"
1523function screen_it {
1524 if is_service_enabled $1; then
1525 # Append the service to the screen rc file
1526 screen_rc "$1" "$2"
1527
1528 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001529 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001530 else
1531 # Spawn directly without screen
1532 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1533 fi
1534 fi
1535}
1536
1537# Compatibility for existing stop_XXXX() functions
1538# Stop a service in screen
1539# If a PID is available use it, kill the whole process group via TERM
1540# If screen is being used kill the screen window; this will catch processes
1541# that did not leave a PID behind
1542# screen_stop service
1543function screen_stop {
1544 # Clean up the screen window
1545 stop_process $1
1546}
1547
1548
Dean Troyerdff49a22014-01-30 15:37:40 -06001549# Python Functions
1550# ================
1551
1552# Get the path to the pip command.
1553# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001554function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001555 which pip || which pip-python
1556
1557 if [ $? -ne 0 ]; then
1558 die $LINENO "Unable to find pip; cannot continue"
1559 fi
1560}
1561
1562# Get the path to the direcotry where python executables are installed.
1563# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001564function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001565 if is_fedora || is_suse; then
1566 echo "/usr/bin"
1567 else
1568 echo "/usr/local/bin"
1569 fi
1570}
1571
1572# Wrapper for ``pip install`` to set cache and proxy environment variables
Attila Fazekasaf81d672014-11-10 09:04:54 +01001573# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``,
Dean Troyerdff49a22014-01-30 15:37:40 -06001574# ``TRACK_DEPENDS``, ``*_proxy``
1575# pip_install package [package ...]
1576function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001577 local xtrace=$(set +o | grep xtrace)
1578 set +o xtrace
1579 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1580 $xtrace
1581 return
1582 fi
1583
Dean Troyerdff49a22014-01-30 15:37:40 -06001584 if [[ -z "$os_PACKAGE" ]]; then
1585 GetOSVersion
1586 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001587 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1588 # TRACK_DEPENDS=True installation creates a circular dependency when
1589 # we attempt to install virtualenv into a virualenv, so we must global
1590 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001591 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001592 local cmd_pip=$DEST/.venv/bin/pip
1593 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001594 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001595 local cmd_pip=$(get_pip_command)
1596 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001597 fi
1598
Sean Dague45917cc2014-02-24 16:09:14 -05001599 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001600 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001601 http_proxy=$http_proxy \
1602 https_proxy=$https_proxy \
1603 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001604 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001605 $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001606
Flavio Percoco5a91c352014-10-31 18:48:00 +01001607 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001608 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1609 local test_req="$@/test-requirements.txt"
1610 if [[ -e "$test_req" ]]; then
1611 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1612 http_proxy=$http_proxy \
1613 https_proxy=$https_proxy \
1614 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001615 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001616 -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001617 fi
1618 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001619}
1620
Sean Daguecc524062014-10-01 09:06:43 -04001621# should we use this library from their git repo, or should we let it
1622# get pulled in via pip dependencies.
1623function use_library_from_git {
1624 local name=$1
1625 local enabled=1
1626 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1627 return $enabled
1628}
1629
1630# setup a library by name. If we are trying to use the library from
1631# git, we'll do a git based install, otherwise we'll punt and the
1632# library should be installed by a requirements pull from another
1633# project.
1634function setup_lib {
1635 local name=$1
1636 local dir=${GITDIR[$name]}
1637 setup_install $dir
1638}
1639
Sean Daguee08ab102014-11-13 17:09:28 -05001640# setup a library by name in editiable mode. If we are trying to use
1641# the library from git, we'll do a git based install, otherwise we'll
1642# punt and the library should be installed by a requirements pull from
1643# another project.
1644#
1645# use this for non namespaced libraries
1646function setup_dev_lib {
1647 local name=$1
1648 local dir=${GITDIR[$name]}
1649 setup_develop $dir
1650}
Sean Daguecc524062014-10-01 09:06:43 -04001651
Sean Dague099e5e32014-03-31 10:35:43 -04001652# this should be used if you want to install globally, all libraries should
1653# use this, especially *oslo* ones
1654function setup_install {
1655 local project_dir=$1
1656 setup_package_with_req_sync $project_dir
1657}
1658
1659# this should be used for projects which run services, like all services
1660function setup_develop {
1661 local project_dir=$1
1662 setup_package_with_req_sync $project_dir -e
1663}
1664
Sean Daguedef15342014-10-27 12:26:04 -04001665# determine if a project as specified by directory is in
1666# projects.txt. This will not be an exact match because we throw away
1667# the namespacing when we clone, but it should be good enough in all
1668# practical ways.
1669function is_in_projects_txt {
1670 local project_dir=$1
1671 local project_name=$(basename $project_dir)
1672 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1673}
1674
Dean Troyeraf616d92014-02-17 12:57:55 -06001675# ``pip install -e`` the package, which processes the dependencies
1676# using pip before running `setup.py develop`
1677#
1678# Updates the dependencies in project_dir from the
1679# openstack/requirements global list before installing anything.
1680#
1681# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1682# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001683function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001684 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001685 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001686
Dean Troyeraf616d92014-02-17 12:57:55 -06001687 # Don't update repo if local changes exist
1688 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001689 # ``errexit`` requires us to trap the exit code when the repo is changed
1690 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001691
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001692 if [[ $update_requirements != "changed" ]]; then
Sean Daguedef15342014-10-27 12:26:04 -04001693 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1694 if is_in_projects_txt $project_dir; then
1695 (cd $REQUIREMENTS_DIR; \
1696 python update.py $project_dir)
1697 else
1698 # soft update projects not found in requirements project.txt
1699 (cd $REQUIREMENTS_DIR; \
1700 python update.py -s $project_dir)
1701 fi
1702 else
1703 (cd $REQUIREMENTS_DIR; \
1704 python update.py $project_dir)
1705 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001706 fi
1707
Sean Dague099e5e32014-03-31 10:35:43 -04001708 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001709
1710 # We've just gone and possibly modified the user's source tree in an
1711 # automated way, which is considered bad form if it's a development
1712 # tree because we've screwed up their next git checkin. So undo it.
1713 #
1714 # However... there are some circumstances, like running in the gate
1715 # where we really really want the overridden version to stick. So provide
1716 # a variable that tells us whether or not we should UNDO the requirements
1717 # changes (this will be set to False in the OpenStack ci gate)
1718 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001719 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001720 (cd $project_dir && git reset --hard)
1721 fi
1722 fi
1723}
1724
1725# ``pip install -e`` the package, which processes the dependencies
1726# using pip before running `setup.py develop`
1727# Uses globals ``STACK_USER``
1728# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001729function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001730 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001731 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001732
Sean Dague099e5e32014-03-31 10:35:43 -04001733 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001734 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001735 if [[ "$flags" == "-e" ]]; then
1736 safe_chown -R $STACK_USER $1/*.egg-info
1737 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001738}
1739
Dean Troyerdff49a22014-01-30 15:37:40 -06001740
1741# Service Functions
1742# =================
1743
1744# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1745# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001746function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001747 echo "$1" | sed -e '
1748 s/,,/,/g;
1749 s/^,//;
1750 s/,$//
1751 '
1752}
1753
1754# disable_all_services() removes all current services
1755# from ``ENABLED_SERVICES`` to reset the configuration
1756# before a minimal installation
1757# Uses global ``ENABLED_SERVICES``
1758# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001759function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001760 ENABLED_SERVICES=""
1761}
1762
1763# Remove all services starting with '-'. For example, to install all default
1764# services except rabbit (rabbit) set in ``localrc``:
1765# ENABLED_SERVICES+=",-rabbit"
1766# Uses global ``ENABLED_SERVICES``
1767# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001768function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001769 local tmpsvcs="${ENABLED_SERVICES}"
1770 local service
1771 for service in ${tmpsvcs//,/ }; do
1772 if [[ ${service} == -* ]]; then
1773 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1774 fi
1775 done
1776 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1777}
1778
1779# disable_service() removes the services passed as argument to the
1780# ``ENABLED_SERVICES`` list, if they are present.
1781#
1782# For example:
1783# disable_service rabbit
1784#
1785# This function does not know about the special cases
1786# for nova, glance, and neutron built into is_service_enabled().
1787# Uses global ``ENABLED_SERVICES``
1788# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001789function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001790 local tmpsvcs=",${ENABLED_SERVICES},"
1791 local service
1792 for service in $@; do
1793 if is_service_enabled $service; then
1794 tmpsvcs=${tmpsvcs//,$service,/,}
1795 fi
1796 done
1797 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1798}
1799
1800# enable_service() adds the services passed as argument to the
1801# ``ENABLED_SERVICES`` list, if they are not already present.
1802#
1803# For example:
1804# enable_service qpid
1805#
1806# This function does not know about the special cases
1807# for nova, glance, and neutron built into is_service_enabled().
1808# Uses global ``ENABLED_SERVICES``
1809# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001810function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001811 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001812 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001813 for service in $@; do
1814 if ! is_service_enabled $service; then
1815 tmpsvcs+=",$service"
1816 fi
1817 done
1818 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1819 disable_negated_services
1820}
1821
1822# is_service_enabled() checks if the service(s) specified as arguments are
1823# enabled by the user in ``ENABLED_SERVICES``.
1824#
1825# Multiple services specified as arguments are ``OR``'ed together; the test
1826# is a short-circuit boolean, i.e it returns on the first match.
1827#
1828# There are special cases for some 'catch-all' services::
1829# **nova** returns true if any service enabled start with **n-**
1830# **cinder** returns true if any service enabled start with **c-**
1831# **ceilometer** returns true if any service enabled start with **ceilometer**
1832# **glance** returns true if any service enabled start with **g-**
1833# **neutron** returns true if any service enabled start with **q-**
1834# **swift** returns true if any service enabled start with **s-**
1835# **trove** returns true if any service enabled start with **tr-**
1836# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1837# **s-** services will be enabled. This will be deprecated in the future.
1838#
1839# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1840# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1841# as enabled in this case.
1842#
1843# Uses global ``ENABLED_SERVICES``
1844# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001845function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001846 local xtrace=$(set +o | grep xtrace)
1847 set +o xtrace
1848 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001849 local services=$@
1850 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001851 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001852 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001853
1854 # Look for top-level 'enabled' function for this service
1855 if type is_${service}_enabled >/dev/null 2>&1; then
1856 # A function exists for this service, use it
1857 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001858 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001859 fi
1860
1861 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1862 # are implemented
1863
Sean Dague45917cc2014-02-24 16:09:14 -05001864 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001865 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001866 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1867 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1868 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1869 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1870 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1871 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1872 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1873 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1874 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001875 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001876 done
Sean Dague45917cc2014-02-24 16:09:14 -05001877 $xtrace
1878 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001879}
1880
1881# Toggle enable/disable_service for services that must run exclusive of each other
1882# $1 The name of a variable containing a space-separated list of services
1883# $2 The name of a variable in which to store the enabled service's name
1884# $3 The name of the service to enable
1885function use_exclusive_service {
1886 local options=${!1}
1887 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001888 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001889 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001890 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001891 for opt in $options;do
1892 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1893 done
1894 eval "$out=$selection"
1895 return 0
1896}
1897
1898
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001899# System Functions
1900# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001901
1902# Only run the command if the target file (the last arg) is not on an
1903# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001904function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001905 local xtrace=$(set +o | grep xtrace)
1906 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001907 local args=( $@ )
1908 local last
1909 local sudo_cmd
1910 local dir_to_check
1911
1912 let last="${#args[*]} - 1"
1913
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001914 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001915 if [ ! -d "$dir_to_check" ]; then
1916 dir_to_check=`dirname "$dir_to_check"`
1917 fi
1918
1919 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001920 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001921 return 0
1922 fi
1923
1924 if [[ $TRACK_DEPENDS = True ]]; then
1925 sudo_cmd="env"
1926 else
1927 sudo_cmd="sudo"
1928 fi
1929
Sean Dague45917cc2014-02-24 16:09:14 -05001930 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001931 $sudo_cmd $@
1932}
1933
1934# Exit 0 if address is in network or 1 if address is not in network
1935# ip-range is in CIDR notation: 1.2.3.4/20
1936# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001937function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001938 local ip=$1
1939 local range=$2
1940 local masklen=${range#*/}
1941 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1942 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1943 [[ $network == $subnet ]]
1944}
1945
1946# Add a user to a group.
1947# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001948function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001949 local user=$1
1950 local group=$2
1951
1952 if [[ -z "$os_VENDOR" ]]; then
1953 GetOSVersion
1954 fi
1955
1956 # SLE11 and openSUSE 12.2 don't have the usual usermod
1957 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1958 sudo usermod -a -G "$group" "$user"
1959 else
1960 sudo usermod -A "$group" "$user"
1961 fi
1962}
1963
1964# Convert CIDR notation to a IPv4 netmask
1965# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001966function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001967 local maskpat="255 255 255 255"
1968 local maskdgt="254 252 248 240 224 192 128"
1969 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1970 echo ${1-0}.${2-0}.${3-0}.${4-0}
1971}
1972
1973# Gracefully cp only if source file/dir exists
1974# cp_it source destination
1975function cp_it {
1976 if [ -e $1 ] || [ -d $1 ]; then
1977 cp -pRL $1 $2
1978 fi
1979}
1980
1981# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1982# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1983# ``localrc`` or on the command line if necessary::
1984#
1985# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1986#
1987# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1988
Ian Wienandaee18c72014-02-21 15:35:08 +11001989function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001990 if [[ -n "$http_proxy" ]]; then
1991 export http_proxy=$http_proxy
1992 fi
1993 if [[ -n "$https_proxy" ]]; then
1994 export https_proxy=$https_proxy
1995 fi
1996 if [[ -n "$no_proxy" ]]; then
1997 export no_proxy=$no_proxy
1998 fi
1999}
2000
2001# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002002function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06002003 local mount_type=`stat -f -L -c %T $1`
2004 test "$mount_type" == "nfs"
2005}
2006
2007# Return the network portion of the given IP address using netmask
2008# netmask is in the traditional dotted-quad format
2009# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002010function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002011 local ip=$1
2012 local mask=$2
2013 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2014 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2015 echo $subnet
2016}
2017
2018# Service wrapper to restart services
2019# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002020function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002021 if is_ubuntu; then
2022 sudo /usr/sbin/service $1 restart
2023 else
2024 sudo /sbin/service $1 restart
2025 fi
2026}
2027
2028# Only change permissions of a file or directory if it is not on an
2029# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002030function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002031 _safe_permission_operation chmod $@
2032}
2033
2034# Only change ownership of a file or directory if it is not on an NFS
2035# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002036function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002037 _safe_permission_operation chown $@
2038}
2039
2040# Service wrapper to start services
2041# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002042function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002043 if is_ubuntu; then
2044 sudo /usr/sbin/service $1 start
2045 else
2046 sudo /sbin/service $1 start
2047 fi
2048}
2049
2050# Service wrapper to stop services
2051# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002052function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002053 if is_ubuntu; then
2054 sudo /usr/sbin/service $1 stop
2055 else
2056 sudo /sbin/service $1 stop
2057 fi
2058}
2059
2060
2061# Restore xtrace
2062$XTRACE
2063
2064# Local variables:
2065# mode: shell-script
2066# End: