blob: e791ad79c8b6c2f653bf98da956ab8efe0705f6b [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
Dean Troyerdff49a22014-01-30 15:37:40 -060018# - Service Functions
Masayuki Igawaf6368d32014-02-20 13:31:26 +090019# - System Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060020#
21# The following variables are assumed to be defined by certain functions:
22#
23# - ``ENABLED_SERVICES``
24# - ``ERROR_ON_CLONE``
25# - ``FILES``
26# - ``OFFLINE``
Dean Troyerdff49a22014-01-30 15:37:40 -060027# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090028# - ``REQUIREMENTS_DIR``
29# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060030# - ``TRACK_DEPENDS``
Masayuki Igawad20f6322014-02-28 09:22:37 +090031# - ``UNDO_REQUIREMENTS``
Dean Troyerdff49a22014-01-30 15:37:40 -060032# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050033#
Dean Troyerdff49a22014-01-30 15:37:40 -060034
35# Save trace setting
36XTRACE=$(set +o | grep xtrace)
37set +o xtrace
38
Sean Daguecc524062014-10-01 09:06:43 -040039# Global Config Variables
40declare -A GITREPO
41declare -A GITBRANCH
42declare -A GITDIR
43
Sean Dague53753292014-12-04 19:38:15 -050044TRACK_DEPENDS=${TRACK_DEPENDS:-False}
45
Dean Troyerdff49a22014-01-30 15:37:40 -060046# Config Functions
47# ================
48
49# Append a new option in an ini file without replacing the old value
50# iniadd config-file section option value1 value2 value3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +110051function iniadd {
Sean Dague45917cc2014-02-24 16:09:14 -050052 local xtrace=$(set +o | grep xtrace)
53 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060054 local file=$1
55 local section=$2
56 local option=$3
57 shift 3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050058
Dean Troyerdff49a22014-01-30 15:37:40 -060059 local values="$(iniget_multiline $file $section $option) $@"
60 iniset_multiline $file $section $option $values
Sean Dague45917cc2014-02-24 16:09:14 -050061 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060062}
63
64# Comment an option in an INI file
65# inicomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110066function inicomment {
Sean Dague45917cc2014-02-24 16:09:14 -050067 local xtrace=$(set +o | grep xtrace)
68 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060069 local file=$1
70 local section=$2
71 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050072
Dean Troyerdff49a22014-01-30 15:37:40 -060073 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -050074 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060075}
76
77# Get an option from an INI file
78# iniget config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110079function iniget {
Sean Dague45917cc2014-02-24 16:09:14 -050080 local xtrace=$(set +o | grep xtrace)
81 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060082 local file=$1
83 local section=$2
84 local option=$3
85 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050086
Dean Troyerdff49a22014-01-30 15:37:40 -060087 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
88 echo ${line#*=}
Sean Dague45917cc2014-02-24 16:09:14 -050089 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060090}
91
92# Get a multiple line option from an INI file
93# iniget_multiline config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110094function iniget_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -050095 local xtrace=$(set +o | grep xtrace)
96 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060097 local file=$1
98 local section=$2
99 local option=$3
100 local values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500101
Dean Troyerdff49a22014-01-30 15:37:40 -0600102 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
103 echo ${values}
Sean Dague45917cc2014-02-24 16:09:14 -0500104 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600105}
106
107# Determinate is the given option present in the INI file
108# ini_has_option config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100109function ini_has_option {
Sean Dague45917cc2014-02-24 16:09:14 -0500110 local xtrace=$(set +o | grep xtrace)
111 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600112 local file=$1
113 local section=$2
114 local option=$3
115 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500116
Dean Troyerdff49a22014-01-30 15:37:40 -0600117 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Sean Dague45917cc2014-02-24 16:09:14 -0500118 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600119 [ -n "$line" ]
120}
121
Robert Li751ad1a2014-10-15 21:40:53 -0400122# Add another config line for a multi-line option.
123# It's normally called after iniset of the same option and assumes
124# that the section already exists.
125#
126# Note that iniset_multiline requires all the 'lines' to be supplied
127# in the argument list. Doing that will cause incorrect configuration
128# if spaces are used in the config values.
129#
130# iniadd_literal config-file section option value
131function iniadd_literal {
132 local xtrace=$(set +o | grep xtrace)
133 set +o xtrace
134 local file=$1
135 local section=$2
136 local option=$3
137 local value=$4
138
139 [[ -z $section || -z $option ]] && return
140
141 # Add it
142 sed -i -e "/^\[$section\]/ a\\
143$option = $value
144" "$file"
145
146 $xtrace
147}
148
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700149function inidelete {
150 local xtrace=$(set +o | grep xtrace)
151 set +o xtrace
152 local file=$1
153 local section=$2
154 local option=$3
155
156 [[ -z $section || -z $option ]] && return
157
158 # Remove old values
159 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
160
161 $xtrace
162}
163
Dean Troyerdff49a22014-01-30 15:37:40 -0600164# Set an option in an INI file
165# iniset config-file section option value
Ian Wienandaee18c72014-02-21 15:35:08 +1100166function iniset {
Sean Dague45917cc2014-02-24 16:09:14 -0500167 local xtrace=$(set +o | grep xtrace)
168 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600169 local file=$1
170 local section=$2
171 local option=$3
172 local value=$4
173
174 [[ -z $section || -z $option ]] && return
175
176 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
177 # Add section at the end
178 echo -e "\n[$section]" >>"$file"
179 fi
180 if ! ini_has_option "$file" "$section" "$option"; then
181 # Add it
182 sed -i -e "/^\[$section\]/ a\\
183$option = $value
184" "$file"
185 else
186 local sep=$(echo -ne "\x01")
187 # Replace it
188 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
189 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500190 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600191}
192
193# Set a multiple line option in an INI file
194# iniset_multiline config-file section option value1 value2 valu3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +1100195function iniset_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -0500196 local xtrace=$(set +o | grep xtrace)
197 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600198 local file=$1
199 local section=$2
200 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500201
Dean Troyerdff49a22014-01-30 15:37:40 -0600202 shift 3
203 local values
204 for v in $@; do
205 # The later sed command inserts each new value in the line next to
206 # the section identifier, which causes the values to be inserted in
207 # the reverse order. Do a reverse here to keep the original order.
208 values="$v ${values}"
209 done
210 if ! grep -q "^\[$section\]" "$file"; then
211 # Add section at the end
212 echo -e "\n[$section]" >>"$file"
213 else
214 # Remove old values
215 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
216 fi
217 # Add new ones
218 for v in $values; do
219 sed -i -e "/^\[$section\]/ a\\
220$option = $v
221" "$file"
222 done
Sean Dague45917cc2014-02-24 16:09:14 -0500223 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600224}
225
226# Uncomment an option in an INI file
227# iniuncomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100228function iniuncomment {
Sean Dague45917cc2014-02-24 16:09:14 -0500229 local xtrace=$(set +o | grep xtrace)
230 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600231 local file=$1
232 local section=$2
233 local option=$3
234 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500235 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600236}
237
238# Normalize config values to True or False
239# Accepts as False: 0 no No NO false False FALSE
240# Accepts as True: 1 yes Yes YES true True TRUE
241# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100242function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500243 local xtrace=$(set +o | grep xtrace)
244 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600245 local default=$1
Sean Dague53753292014-12-04 19:38:15 -0500246 local literal=$2
Dean Troyera1b82cc2015-01-30 13:54:40 -0600247 local testval=${!literal:-}
Dean Troyerdff49a22014-01-30 15:37:40 -0600248
249 [[ -z "$testval" ]] && { echo "$default"; return; }
250 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
251 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
252 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500253 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600254}
255
Sean Dague53753292014-12-04 19:38:15 -0500256function isset {
257 nounset=$(set +o | grep nounset)
258 set +o nounset
259 [[ -n "${!1+x}" ]]
260 result=$?
261 $nounset
262 return $result
263}
Dean Troyerdff49a22014-01-30 15:37:40 -0600264
265# Control Functions
266# =================
267
268# Prints backtrace info
269# filename:lineno:function
270# backtrace level
271function backtrace {
272 local level=$1
273 local deep=$((${#BASH_SOURCE[@]} - 1))
274 echo "[Call Trace]"
275 while [ $level -le $deep ]; do
276 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
277 deep=$((deep - 1))
278 done
279}
280
281# Prints line number and "message" then exits
282# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100283function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600284 local exitcode=$?
285 set +o xtrace
286 local line=$1; shift
287 if [ $exitcode == 0 ]; then
288 exitcode=1
289 fi
290 backtrace 2
291 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600292 # Give buffers a second to flush
293 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600294 exit $exitcode
295}
296
297# Checks an environment variable is not set or has length 0 OR if the
298# exit code is non-zero and prints "message" and exits
299# NOTE: env-var is the variable name without a '$'
300# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100301function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600302 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500303 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600304 set +o xtrace
305 local line=$1; shift
306 local evar=$1; shift
307 if ! is_set $evar || [ $exitcode != 0 ]; then
308 die $line "$*"
309 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500310 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600311}
312
313# Prints line number and "message" in error format
314# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100315function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600316 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500317 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600318 set +o xtrace
319 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
320 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600321 if [[ -n ${LOGDIR} ]]; then
322 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600323 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500324 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600325 return $exitcode
326}
327
328# Checks an environment variable is not set or has length 0 OR if the
329# exit code is non-zero and prints "message"
330# NOTE: env-var is the variable name without a '$'
331# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100332function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600333 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500334 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600335 set +o xtrace
336 local line=$1; shift
337 local evar=$1; shift
338 if ! is_set $evar || [ $exitcode != 0 ]; then
339 err $line "$*"
340 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500341 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600342 return $exitcode
343}
344
345# Exit after outputting a message about the distribution not being supported.
346# exit_distro_not_supported [optional-string-telling-what-is-missing]
347function exit_distro_not_supported {
348 if [[ -z "$DISTRO" ]]; then
349 GetDistro
350 fi
351
352 if [ $# -gt 0 ]; then
353 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
354 else
355 die $LINENO "Support for $DISTRO is incomplete."
356 fi
357}
358
359# Test if the named environment variable is set and not zero length
360# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100361function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600362 local var=\$"$1"
363 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
364}
365
366# Prints line number and "message" in warning format
367# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100368function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600369 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500370 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600371 set +o xtrace
372 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
373 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600374 if [[ -n ${LOGDIR} ]]; then
375 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600376 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500377 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600378 return $exitcode
379}
380
381
382# Distro Functions
383# ================
384
385# Determine OS Vendor, Release and Update
386# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
387# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500388# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
389# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
390# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
391# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
392# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500393os_VENDOR=""
394os_RELEASE=""
395os_UPDATE=""
396os_PACKAGE=""
397os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500398
Dean Troyerdff49a22014-01-30 15:37:40 -0600399# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100400function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500401
Dean Troyerdff49a22014-01-30 15:37:40 -0600402 # Figure out which vendor we are
403 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
404 # OS/X
405 os_VENDOR=`sw_vers -productName`
406 os_RELEASE=`sw_vers -productVersion`
407 os_UPDATE=${os_RELEASE##*.}
408 os_RELEASE=${os_RELEASE%.*}
409 os_PACKAGE=""
410 if [[ "$os_RELEASE" =~ "10.7" ]]; then
411 os_CODENAME="lion"
412 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
413 os_CODENAME="snow leopard"
414 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
415 os_CODENAME="leopard"
416 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
417 os_CODENAME="tiger"
418 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
419 os_CODENAME="panther"
420 else
421 os_CODENAME=""
422 fi
423 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
424 os_VENDOR=$(lsb_release -i -s)
425 os_RELEASE=$(lsb_release -r -s)
426 os_UPDATE=""
427 os_PACKAGE="rpm"
428 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
429 os_PACKAGE="deb"
430 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
431 lsb_release -d -s | grep -q openSUSE
432 if [[ $? -eq 0 ]]; then
433 os_VENDOR="openSUSE"
434 fi
435 elif [[ $os_VENDOR == "openSUSE project" ]]; then
436 os_VENDOR="openSUSE"
437 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
438 os_VENDOR="Red Hat"
439 fi
440 os_CODENAME=$(lsb_release -c -s)
441 elif [[ -r /etc/redhat-release ]]; then
442 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
443 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
444 # CentOS release 5.5 (Final)
445 # CentOS Linux release 6.0 (Final)
446 # Fedora release 16 (Verne)
447 # XenServer release 6.2.0-70446c (xenenterprise)
448 os_CODENAME=""
449 for r in "Red Hat" CentOS Fedora XenServer; do
450 os_VENDOR=$r
451 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
452 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
453 os_CODENAME=${ver#*|}
454 os_RELEASE=${ver%|*}
455 os_UPDATE=${os_RELEASE##*.}
456 os_RELEASE=${os_RELEASE%.*}
457 break
458 fi
459 os_VENDOR=""
460 done
461 os_PACKAGE="rpm"
462 elif [[ -r /etc/SuSE-release ]]; then
463 for r in openSUSE "SUSE Linux"; do
464 if [[ "$r" = "SUSE Linux" ]]; then
465 os_VENDOR="SUSE LINUX"
466 else
467 os_VENDOR=$r
468 fi
469
470 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
471 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
472 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
473 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
474 break
475 fi
476 os_VENDOR=""
477 done
478 os_PACKAGE="rpm"
479 # If lsb_release is not installed, we should be able to detect Debian OS
480 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
481 os_VENDOR="Debian"
482 os_PACKAGE="deb"
483 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
484 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
485 fi
486 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
487}
488
489# Translate the OS version values into common nomenclature
490# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500491declare DISTRO
492
Ian Wienandaee18c72014-02-21 15:35:08 +1100493function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600494 GetOSVersion
495 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
496 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
497 DISTRO=$os_CODENAME
498 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
499 # For Fedora, just use 'f' and the release
500 DISTRO="f$os_RELEASE"
501 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
502 DISTRO="opensuse-$os_RELEASE"
503 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
504 # For SLE, also use the service pack
505 if [[ -z "$os_UPDATE" ]]; then
506 DISTRO="sle${os_RELEASE}"
507 else
508 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
509 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530510 elif [[ "$os_VENDOR" =~ (Red Hat) || \
511 "$os_VENDOR" =~ (CentOS) || \
512 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600513 # Drop the . release as we assume it's compatible
514 DISTRO="rhel${os_RELEASE::1}"
515 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
516 DISTRO="xs$os_RELEASE"
517 else
518 # Catch-all for now is Vendor + Release + Update
519 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
520 fi
521 export DISTRO
522}
523
524# Utility function for checking machine architecture
525# is_arch arch-type
526function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500527 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600528}
529
530# Determine if current distribution is a Fedora-based distribution
531# (Fedora, RHEL, CentOS, etc).
532# is_fedora
533function is_fedora {
534 if [[ -z "$os_VENDOR" ]]; then
535 GetOSVersion
536 fi
537
anju Tiwari6c639c92014-07-15 18:11:54 +0530538 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
539 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600540}
541
542
543# Determine if current distribution is a SUSE-based distribution
544# (openSUSE, SLE).
545# is_suse
546function is_suse {
547 if [[ -z "$os_VENDOR" ]]; then
548 GetOSVersion
549 fi
550
551 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
552}
553
554
555# Determine if current distribution is an Ubuntu-based distribution
556# It will also detect non-Ubuntu but Debian-based distros
557# is_ubuntu
558function is_ubuntu {
559 if [[ -z "$os_PACKAGE" ]]; then
560 GetOSVersion
561 fi
562 [ "$os_PACKAGE" = "deb" ]
563}
564
565
566# Git Functions
567# =============
568
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600569# Returns openstack release name for a given branch name
570# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100571function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600572 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700573 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600574 echo ${branch#*/}
575 else
576 echo "master"
577 fi
578}
579
Dean Troyerdff49a22014-01-30 15:37:40 -0600580# git clone only if directory doesn't exist already. Since ``DEST`` might not
581# be owned by the installation user, we create the directory and change the
582# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500583# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
584# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600585# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500586# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600587# git_clone remote dest-dir branch
588function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500589 local git_remote=$1
590 local git_dest=$2
591 local git_ref=$3
592 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200593 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500594
Sean Dague53753292014-12-04 19:38:15 -0500595 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800596 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200597 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
598 fi
599
Dean Troyerdff49a22014-01-30 15:37:40 -0600600 if [[ "$OFFLINE" = "True" ]]; then
601 echo "Running in offline mode, clones already exist"
602 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500603 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600604 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400605 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600606 return
607 fi
608
Dean Troyer50cda692014-07-25 11:57:20 -0500609 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600610 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500611 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600612 [[ "$ERROR_ON_CLONE" = "True" ]] && \
613 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200614 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600615 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500616 cd $git_dest
617 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600618 else
619 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500620 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600621 [[ "$ERROR_ON_CLONE" = "True" ]] && \
622 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200623 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500624 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600625 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500626 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600627 elif [[ "$RECLONE" = "True" ]]; then
628 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500629 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600630 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500631 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100632 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600633 # remove the existing ignored files (like pyc) as they cause breakage
634 # (due to the py files having older timestamps than our pyc, so python
635 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500636 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600637
Dean Troyer50cda692014-07-25 11:57:20 -0500638 # handle git_ref accordingly to type (tag, branch)
639 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
640 git_update_tag $git_ref
641 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
642 git_update_branch $git_ref
643 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
644 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600645 else
Dean Troyer50cda692014-07-25 11:57:20 -0500646 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600647 fi
648
649 fi
650 fi
651
652 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500653 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600654 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400655 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600656}
657
Sean Daguecc524062014-10-01 09:06:43 -0400658# A variation on git clone that lets us specify a project by it's
659# actual name, like oslo.config. This is exceptionally useful in the
660# library installation case
661function git_clone_by_name {
662 local name=$1
663 local repo=${GITREPO[$name]}
664 local dir=${GITDIR[$name]}
665 local branch=${GITBRANCH[$name]}
666 git_clone $repo $dir $branch
667}
668
669
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100670# git can sometimes get itself infinitely stuck with transient network
671# errors or other issues with the remote end. This wraps git in a
672# timeout/retry loop and is intended to watch over non-local git
673# processes that might hang. GIT_TIMEOUT, if set, is passed directly
674# to timeout(1); otherwise the default value of 0 maintains the status
675# quo of waiting forever.
676# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100677function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100678 local count=0
679 local timeout=0
680
681 if [[ -n "${GIT_TIMEOUT}" ]]; then
682 timeout=${GIT_TIMEOUT}
683 fi
684
685 until timeout -s SIGINT ${timeout} git "$@"; do
686 # 124 is timeout(1)'s special return code when it reached the
687 # timeout; otherwise assume fatal failure
688 if [[ $? -ne 124 ]]; then
689 die $LINENO "git call failed: [git $@]"
690 fi
691
692 count=$(($count + 1))
693 warn "timeout ${count} for git call: [git $@]"
694 if [ $count -eq 3 ]; then
695 die $LINENO "Maximum of 3 git retries reached"
696 fi
697 sleep 5
698 done
699}
700
Dean Troyerdff49a22014-01-30 15:37:40 -0600701# git update using reference as a branch.
702# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100703function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500704 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600705
Dean Troyer50cda692014-07-25 11:57:20 -0500706 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600707 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500708 git branch -D $git_branch || true
709 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600710}
711
712# git update using reference as a branch.
713# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100714function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500715 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600716
Dean Troyer50cda692014-07-25 11:57:20 -0500717 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600718}
719
720# git update using reference as a tag. Be careful editing source at that repo
721# as working copy will be in a detached mode
722# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100723function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500724 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600725
Dean Troyer50cda692014-07-25 11:57:20 -0500726 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600727 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500728 git_timed fetch origin tag $git_tag
729 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600730}
731
732
733# OpenStack Functions
734# ===================
735
736# Get the default value for HOST_IP
737# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100738function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600739 local fixed_range=$1
740 local floating_range=$2
741 local host_ip_iface=$3
742 local host_ip=$4
743
744 # Find the interface used for the default route
745 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
746 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
747 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
748 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500749 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
750 local ip
751 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600752 # Attempt to filter out IP addresses that are part of the fixed and
753 # floating range. Note that this method only works if the ``netaddr``
754 # python library is installed. If it is not installed, an error
755 # will be printed and the first IP from the interface will be used.
756 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
757 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500758 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
759 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600760 break;
761 fi
762 done
763 fi
764 echo $host_ip
765}
766
Attila Fazekasf71b5002014-05-28 09:52:22 +0200767# Generates hex string from ``size`` byte of pseudo random data
768# generate_hex_string size
769function generate_hex_string {
770 local size=$1
771 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
772}
773
Dean Troyerdff49a22014-01-30 15:37:40 -0600774# Grab a numbered field from python prettytable output
775# Fields are numbered starting with 1
776# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
777# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100778function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500779 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600780 while read data; do
781 if [ "$1" -lt 0 ]; then
782 field="(\$(NF$1))"
783 else
784 field="\$$(($1 + 1))"
785 fi
786 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
787 done
788}
789
790# Add a policy to a policy.json file
791# Do nothing if the policy already exists
792# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100793function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600794 local policy_file=$1
795 local policy_name=$2
796 local policy_perm=$3
797
798 if grep -q ${policy_name} ${policy_file}; then
799 echo "Policy ${policy_name} already exists in ${policy_file}"
800 return
801 fi
802
803 # Add a terminating comma to policy lines without one
804 # Remove the closing '}' and all lines following to the end-of-file
805 local tmpfile=$(mktemp)
806 uniq ${policy_file} | sed -e '
807 s/]$/],/
808 /^[}]/,$d
809 ' > ${tmpfile}
810
811 # Append policy and closing brace
812 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
813 echo "}" >>${tmpfile}
814
815 mv ${tmpfile} ${policy_file}
816}
817
Alistair Coles24779f62014-10-15 18:57:59 +0100818# Gets or creates a domain
819# Usage: get_or_create_domain <name> <description>
820function get_or_create_domain {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500821 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100822 # Gets domain id
823 local domain_id=$(
824 # Gets domain id
825 openstack --os-token=$OS_TOKEN --os-url=$os_url \
826 --os-identity-api-version=3 domain show $1 \
827 -f value -c id 2>/dev/null ||
828 # Creates new domain
829 openstack --os-token=$OS_TOKEN --os-url=$os_url \
830 --os-identity-api-version=3 domain create $1 \
831 --description "$2" \
832 -f value -c id
833 )
834 echo $domain_id
835}
836
Steve Martinellib74e01c2014-12-18 01:35:35 -0500837# Gets or creates group
838# Usage: get_or_create_group <groupname> [<domain> <description>]
839function get_or_create_group {
840 local domain=${2:+--domain ${2}}
841 local desc="${3:-}"
842 local os_url="$KEYSTONE_SERVICE_URI_V3"
843 # Gets group id
844 local group_id=$(
845 # Creates new group with --or-show
846 openstack --os-token=$OS_TOKEN --os-url=$os_url \
847 --os-identity-api-version=3 group create $1 \
848 $domain --description "$desc" --or-show \
849 -f value -c id
850 )
851 echo $group_id
852}
853
Bartosz Górski0abde392014-02-28 14:15:19 +0100854# Gets or creates user
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000855# Usage: get_or_create_user <username> <password> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100856function get_or_create_user {
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000857 if [[ ! -z "$3" ]]; then
858 local email="--email=$3"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200859 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500860 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200861 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100862 local os_cmd="openstack"
863 local domain=""
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000864 if [[ ! -z "$4" ]]; then
865 domain="--domain=$4"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500866 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100867 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100868 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500869 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500870 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100871 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100872 $1 \
873 --password "$2" \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500874 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100875 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500876 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100877 -f value -c id
878 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500879 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100880}
881
882# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100883# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100884function get_or_create_project {
885 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100886 local os_cmd="openstack"
887 local domain=""
888 if [[ ! -z "$2" ]]; then
889 domain="--domain=$2"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500890 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100891 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500892 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500893 # Creates new project with --or-show
894 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100895 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500896 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100897}
898
899# Gets or creates role
900# Usage: get_or_create_role <name>
901function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500902 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500903 # Creates role with --or-show
904 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100905 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500906 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100907}
908
Jamie Lennox9b215db2015-02-10 18:19:57 +1100909# Gets or adds user role to project
910# Usage: get_or_add_user_project_role <role> <user> <project>
911function get_or_add_user_project_role {
Bartosz Górski0abde392014-02-28 14:15:19 +0100912 # Gets user role id
Steve Martinelli5541a612015-01-19 15:58:49 -0500913 local user_role_id=$(openstack role list \
914 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100915 --project $3 \
916 --column "ID" \
917 --column "Name" \
918 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500919 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100920 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500921 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100922 $1 \
923 --user $2 \
924 --project $3 \
925 | grep " id " | get_field 2)
926 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500927 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100928}
929
930# Gets or creates service
931# Usage: get_or_create_service <name> <type> <description>
932function get_or_create_service {
933 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500934 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100935 # Gets service id
936 openstack service show $1 -f value -c id 2>/dev/null ||
937 # Creates new service if not exists
938 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500939 $2 \
940 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100941 --description="$3" \
942 -f value -c id
943 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500944 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100945}
946
947# Gets or creates endpoint
948# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
949function get_or_create_endpoint {
950 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500951 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100952 --column "ID" \
953 --column "Region" \
954 --column "Service Name" \
955 | grep " $2 " \
956 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500957 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100958 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500959 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100960 $1 \
961 --region $2 \
962 --publicurl $3 \
963 --adminurl $4 \
964 --internalurl $5 \
965 | grep " id " | get_field 2)
966 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500967 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100968}
Dean Troyerdff49a22014-01-30 15:37:40 -0600969
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500970
Dean Troyerdff49a22014-01-30 15:37:40 -0600971# Package Functions
972# =================
973
974# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100975function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800976 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600977 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800978
979 if [[ -z "$base_dir" ]]; then
980 base_dir=$FILES
981 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600982 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800983 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600984 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800985 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600986 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800987 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600988 else
989 exit_distro_not_supported "list of packages"
990 fi
991 echo "$pkg_dir"
992}
993
994# Wrapper for ``apt-get`` to set cache and proxy environment variables
995# Uses globals ``OFFLINE``, ``*_proxy``
996# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100997function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500998 local xtrace=$(set +o | grep xtrace)
999 set +o xtrace
1000
Dean Troyerdff49a22014-01-30 15:37:40 -06001001 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
1002 local sudo="sudo"
1003 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -05001004
1005 $xtrace
Sean Dague53753292014-12-04 19:38:15 -05001006
Dean Troyerdff49a22014-01-30 15:37:40 -06001007 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -05001008 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
1009 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -06001010 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
1011}
1012
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001013function _parse_package_files {
1014 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -06001015
Dean Troyerdff49a22014-01-30 15:37:40 -06001016 if [[ -z "$DISTRO" ]]; then
1017 GetDistro
1018 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001019
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001020 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001021 local OIFS line package distros distro
1022 [[ -e $fname ]] || continue
1023
1024 OIFS=$IFS
1025 IFS=$'\n'
1026 for line in $(<${fname}); do
1027 if [[ $line =~ "NOPRIME" ]]; then
1028 continue
1029 fi
1030
1031 # Assume we want this package
1032 package=${line%#*}
1033 inst_pkg=1
1034
1035 # Look for # dist:xxx in comment
1036 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1037 # We are using BASH regexp matching feature.
1038 package=${BASH_REMATCH[1]}
1039 distros=${BASH_REMATCH[2]}
1040 # In bash ${VAR,,} will lowecase VAR
1041 # Look for a match in the distro list
1042 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1043 # If no match then skip this package
1044 inst_pkg=0
1045 fi
1046 fi
1047
1048 # Look for # testonly in comment
1049 if [[ $line =~ (.*)#.*testonly.* ]]; then
1050 package=${BASH_REMATCH[1]}
1051 # Are we installing test packages? (test for the default value)
1052 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1053 # If not installing test packages the skip this package
1054 inst_pkg=0
1055 fi
1056 fi
1057
1058 if [[ $inst_pkg = 1 ]]; then
1059 echo $package
1060 fi
1061 done
1062 IFS=$OIFS
1063 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001064}
1065
1066# get_packages() collects a list of package names of any type from the
1067# prerequisite files in ``files/{debs|rpms}``. The list is intended
1068# to be passed to a package installer such as apt or yum.
1069#
1070# Only packages required for the services in 1st argument will be
1071# included. Two bits of metadata are recognized in the prerequisite files:
1072#
1073# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1074# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1075# of the package to the distros listed. The distro names are case insensitive.
1076function get_packages {
1077 local xtrace=$(set +o | grep xtrace)
1078 set +o xtrace
1079 local services=$@
1080 local package_dir=$(_get_package_dir)
1081 local file_to_parse=""
1082 local service=""
1083
1084 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
1085
1086 if [[ -z "$package_dir" ]]; then
1087 echo "No package directory supplied"
1088 return 1
1089 fi
1090 for service in ${services//,/ }; do
1091 # Allow individual services to specify dependencies
1092 if [[ -e ${package_dir}/${service} ]]; then
1093 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1094 fi
1095 # NOTE(sdague) n-api needs glance for now because that's where
1096 # glance client is
1097 if [[ $service == n-api ]]; then
1098 if [[ ! $file_to_parse =~ nova ]]; then
1099 file_to_parse="${file_to_parse} ${package_dir}/nova"
1100 fi
1101 if [[ ! $file_to_parse =~ glance ]]; then
1102 file_to_parse="${file_to_parse} ${package_dir}/glance"
1103 fi
1104 elif [[ $service == c-* ]]; then
1105 if [[ ! $file_to_parse =~ cinder ]]; then
1106 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1107 fi
1108 elif [[ $service == ceilometer-* ]]; then
1109 if [[ ! $file_to_parse =~ ceilometer ]]; then
1110 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
1111 fi
1112 elif [[ $service == s-* ]]; then
1113 if [[ ! $file_to_parse =~ swift ]]; then
1114 file_to_parse="${file_to_parse} ${package_dir}/swift"
1115 fi
1116 elif [[ $service == n-* ]]; then
1117 if [[ ! $file_to_parse =~ nova ]]; then
1118 file_to_parse="${file_to_parse} ${package_dir}/nova"
1119 fi
1120 elif [[ $service == g-* ]]; then
1121 if [[ ! $file_to_parse =~ glance ]]; then
1122 file_to_parse="${file_to_parse} ${package_dir}/glance"
1123 fi
1124 elif [[ $service == key* ]]; then
1125 if [[ ! $file_to_parse =~ keystone ]]; then
1126 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1127 fi
1128 elif [[ $service == q-* ]]; then
1129 if [[ ! $file_to_parse =~ neutron ]]; then
1130 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1131 fi
1132 elif [[ $service == ir-* ]]; then
1133 if [[ ! $file_to_parse =~ ironic ]]; then
1134 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1135 fi
1136 fi
1137 done
1138 echo "$(_parse_package_files $file_to_parse)"
1139 $xtrace
1140}
1141
1142# get_plugin_packages() collects a list of package names of any type from a
1143# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1144# list is intended to be passed to a package installer such as apt or yum.
1145#
1146# Only packages required for enabled and collected plugins will included.
1147#
1148# The same metadata used in the main devstack prerequisite files may be used
1149# in these prerequisite files, see get_packages() for more info.
1150function get_plugin_packages {
1151 local xtrace=$(set +o | grep xtrace)
1152 set +o xtrace
1153 local files_to_parse=""
1154 local package_dir=""
1155 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1156 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1157 files_to_parse+="$package_dir/$plugin"
1158 done
1159 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001160 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001161}
1162
1163# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001164# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001165# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001166function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001167 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1168 REPOS_UPDATED=${REPOS_UPDATED:-False}
1169 RETRY_UPDATE=${RETRY_UPDATE:-False}
1170
Paul Linchpiner9e179742014-07-13 22:23:00 -07001171 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001172 return 0
1173 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001174
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001175 if is_ubuntu; then
1176 local xtrace=$(set +o | grep xtrace)
1177 set +o xtrace
1178 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1179 # if there are transient errors pulling the updates, that's fine.
1180 # It may be secondary repositories that we don't really care about.
1181 apt_get update || /bin/true
1182 REPOS_UPDATED=True
1183 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001184 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001185 fi
1186}
1187
1188function real_install_package {
1189 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001190 apt_get install "$@"
1191 elif is_fedora; then
1192 yum_install "$@"
1193 elif is_suse; then
1194 zypper_install "$@"
1195 else
1196 exit_distro_not_supported "installing packages"
1197 fi
1198}
1199
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001200# Distro-agnostic package installer
1201# install_package package [package ...]
1202function install_package {
1203 update_package_repo
1204 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1205}
1206
Dean Troyerdff49a22014-01-30 15:37:40 -06001207# Distro-agnostic function to tell if a package is installed
1208# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001209function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001210 if [[ -z "$@" ]]; then
1211 return 1
1212 fi
1213
1214 if [[ -z "$os_PACKAGE" ]]; then
1215 GetOSVersion
1216 fi
1217
1218 if [[ "$os_PACKAGE" = "deb" ]]; then
1219 dpkg -s "$@" > /dev/null 2> /dev/null
1220 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1221 rpm --quiet -q "$@"
1222 else
1223 exit_distro_not_supported "finding if a package is installed"
1224 fi
1225}
1226
1227# Distro-agnostic package uninstaller
1228# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001229function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001230 if is_ubuntu; then
1231 apt_get purge "$@"
1232 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001233 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001234 elif is_suse; then
1235 sudo zypper rm "$@"
1236 else
1237 exit_distro_not_supported "uninstalling packages"
1238 fi
1239}
1240
1241# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001242# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001243# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001244function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001245 [[ "$OFFLINE" = "True" ]] && return
1246 local sudo="sudo"
1247 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001248
1249 # The manual check for missing packages is because yum -y assumes
1250 # missing packages are OK. See
1251 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001252 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1253 no_proxy=$no_proxy \
Ian Wienand36298ee2015-02-04 10:29:31 +11001254 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001255 awk '
1256 BEGIN { fail=0 }
1257 /No package/ { fail=1 }
1258 { print }
1259 END { exit fail }' || \
1260 die $LINENO "Missing packages detected"
1261
1262 # also ensure we catch a yum failure
1263 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001264 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001265 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001266}
1267
1268# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001269# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001270# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001271function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001272 [[ "$OFFLINE" = "True" ]] && return
1273 local sudo="sudo"
1274 [[ "$(id -u)" = "0" ]] && sudo="env"
1275 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1276 zypper --non-interactive install --auto-agree-with-licenses "$@"
1277}
1278
1279
1280# Process Functions
1281# =================
1282
1283# _run_process() is designed to be backgrounded by run_process() to simulate a
1284# fork. It includes the dirty work of closing extra filehandles and preparing log
1285# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001286# from the service name.
1287# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001288# If an optional group is provided sg will be used to set the group of
1289# the command.
1290# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001291function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001292 local service=$1
1293 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001294 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001295
1296 # Undo logging redirections and close the extra descriptors
1297 exec 1>&3
1298 exec 2>&3
1299 exec 3>&-
1300 exec 6>&-
1301
Dean Troyerdde41d02014-12-09 17:47:57 -06001302 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1303 if [[ -n ${LOGDIR} ]]; then
1304 exec 1>&"$real_logfile" 2>&1
1305 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1306 if [[ -n ${SCREEN_LOGDIR} ]]; then
1307 # Drop the backward-compat symlink
1308 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1309 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001310
1311 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1312 export PYTHONUNBUFFERED=1
1313 fi
1314
Dean Troyer3159a822014-08-27 14:13:58 -05001315 # Run under ``setsid`` to force the process to become a session and group leader.
1316 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001317 if [[ -n "$group" ]]; then
1318 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1319 else
1320 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1321 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001322
1323 # Just silently exit this process
1324 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001325}
1326
1327# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1328# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001329# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001330# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001331function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001332 SCREEN_NAME=${SCREEN_NAME:-stack}
1333 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1334
1335 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1336 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1337 fi
1338
1339 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1340}
1341
1342# Find out if a process exists by partial name.
1343# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001344function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001345 local name=$1
1346 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001347 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001348 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001349 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001350}
1351
Dean Troyer3159a822014-08-27 14:13:58 -05001352# Run a single service under screen or directly
1353# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001354# If an optional group is provided sg will be used to run the
1355# command as that group.
1356# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001357function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001358 local service=$1
1359 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001360 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001361
Dean Troyer3159a822014-08-27 14:13:58 -05001362 if is_service_enabled $service; then
1363 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001364 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001365 else
1366 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001367 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001368 fi
1369 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001370}
1371
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001372# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001373# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001374# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001375# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001376# Run a command in a shell in a screen window, if an optional group
1377# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001378function screen_process {
1379 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001380 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001381 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001382
Sean Dagueea22a4f2014-06-27 15:21:41 -04001383 SCREEN_NAME=${SCREEN_NAME:-stack}
1384 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001385 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001386
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001387 # Append the process to the screen rc file
1388 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001389
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001390 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001391
Dean Troyerdde41d02014-12-09 17:47:57 -06001392 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1393 echo "LOGDIR: $LOGDIR"
1394 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1395 echo "log: $real_logfile"
1396 if [[ -n ${LOGDIR} ]]; then
1397 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001398 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001399 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1400 if [[ -n ${SCREEN_LOGDIR} ]]; then
1401 # Drop the backward-compat symlink
1402 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1403 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001404 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001405
1406 # sleep to allow bash to be ready to be send the command - we are
1407 # creating a new window in screen and then sends characters, so if
1408 # bash isn't running by the time we send the command, nothing happens
1409 sleep 3
1410
1411 NL=`echo -ne '\015'`
1412 # This fun command does the following:
1413 # - the passed server command is backgrounded
1414 # - the pid of the background process is saved in the usual place
1415 # - the server process is brought back to the foreground
1416 # - if the server process exits prematurely the fg command errors
1417 # and a message is written to stdout and the process failure file
1418 #
1419 # The pid saved can be used in stop_process() as a process group
1420 # id to kill off all child processes
1421 if [[ -n "$group" ]]; then
1422 command="sg $group '$command'"
1423 fi
1424 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 -06001425}
1426
1427# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001428# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001429# screen_rc service "command-line"
1430function screen_rc {
1431 SCREEN_NAME=${SCREEN_NAME:-stack}
1432 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1433 if [[ ! -e $SCREENRC ]]; then
1434 # Name the screen session
1435 echo "sessionname $SCREEN_NAME" > $SCREENRC
1436 # Set a reasonable statusbar
1437 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1438 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1439 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1440 echo "screen -t shell bash" >> $SCREENRC
1441 fi
1442 # If this service doesn't already exist in the screenrc file
1443 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1444 NL=`echo -ne '\015'`
1445 echo "screen -t $1 bash" >> $SCREENRC
1446 echo "stuff \"$2$NL\"" >> $SCREENRC
1447
Dean Troyerdde41d02014-12-09 17:47:57 -06001448 if [[ -n ${LOGDIR} ]]; then
1449 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001450 echo "log on" >>$SCREENRC
1451 fi
1452 fi
1453}
1454
1455# Stop a service in screen
1456# If a PID is available use it, kill the whole process group via TERM
1457# If screen is being used kill the screen window; this will catch processes
1458# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001459# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001460# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001461function screen_stop_service {
1462 local service=$1
1463
Dean Troyerdff49a22014-01-30 15:37:40 -06001464 SCREEN_NAME=${SCREEN_NAME:-stack}
1465 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001466 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001467
Dean Troyer3159a822014-08-27 14:13:58 -05001468 if is_service_enabled $service; then
1469 # Clean up the screen window
1470 screen -S $SCREEN_NAME -p $service -X kill
1471 fi
1472}
1473
1474# Stop a service process
1475# If a PID is available use it, kill the whole process group via TERM
1476# If screen is being used kill the screen window; this will catch processes
1477# that did not leave a PID behind
1478# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1479# stop_process service
1480function stop_process {
1481 local service=$1
1482
1483 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001484 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001485
1486 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001487 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001488 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1489 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1490 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001491 fi
1492 if [[ "$USE_SCREEN" = "True" ]]; then
1493 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001494 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001495 fi
1496 fi
1497}
1498
1499# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001500# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001501# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001502function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001503 local service
1504 local failures
1505 SCREEN_NAME=${SCREEN_NAME:-stack}
1506 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1507
1508
1509 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1510 echo "No service status directory found"
1511 return
1512 fi
1513
1514 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001515 # make this -o errexit safe
1516 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001517
1518 for service in $failures; do
1519 service=`basename $service`
1520 service=${service%.failure}
1521 echo "Error: Service $service is not running"
1522 done
1523
1524 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001525 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001526 fi
1527}
1528
Chris Dent2f27a0e2014-09-09 13:46:02 +01001529# Tail a log file in a screen if USE_SCREEN is true.
1530function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001531 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001532 local logfile=$2
1533
Sean Dague53753292014-12-04 19:38:15 -05001534 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001535 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001536 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001537 fi
1538}
1539
Dean Troyerdff49a22014-01-30 15:37:40 -06001540
Dean Troyer3159a822014-08-27 14:13:58 -05001541# Deprecated Functions
1542# --------------------
1543
1544# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1545# fork. It includes the dirty work of closing extra filehandles and preparing log
1546# files to produce the same logs as screen_it(). The log filename is derived
1547# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1548# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1549# _old_run_process service "command-line"
1550function _old_run_process {
1551 local service=$1
1552 local command="$2"
1553
1554 # Undo logging redirections and close the extra descriptors
1555 exec 1>&3
1556 exec 2>&3
1557 exec 3>&-
1558 exec 6>&-
1559
1560 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001561 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1562 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001563
1564 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1565 export PYTHONUNBUFFERED=1
1566 fi
1567
1568 exec /bin/bash -c "$command"
1569 die "$service exec failure: $command"
1570}
1571
1572# old_run_process() launches a child process that closes all file descriptors and
1573# then exec's the passed in command. This is meant to duplicate the semantics
1574# of screen_it() without screen. PIDs are written to
1575# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1576# old_run_process service "command-line"
1577function old_run_process {
1578 local service=$1
1579 local command="$2"
1580
1581 # Spawn the child process
1582 _old_run_process "$service" "$command" &
1583 echo $!
1584}
1585
1586# Compatibility for existing start_XXXX() functions
1587# Uses global ``USE_SCREEN``
1588# screen_it service "command-line"
1589function screen_it {
1590 if is_service_enabled $1; then
1591 # Append the service to the screen rc file
1592 screen_rc "$1" "$2"
1593
1594 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001595 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001596 else
1597 # Spawn directly without screen
1598 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1599 fi
1600 fi
1601}
1602
1603# Compatibility for existing stop_XXXX() functions
1604# Stop a service in screen
1605# If a PID is available use it, kill the whole process group via TERM
1606# If screen is being used kill the screen window; this will catch processes
1607# that did not leave a PID behind
1608# screen_stop service
1609function screen_stop {
1610 # Clean up the screen window
1611 stop_process $1
1612}
1613
1614
Sean Dague2c65e712014-12-18 09:44:56 -05001615# Plugin Functions
1616# =================
1617
1618DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1619
1620# enable_plugin <name> <url> [branch]
1621#
1622# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1623# ``url`` is a git url
1624# ``branch`` is a gitref. If it's not set, defaults to master
1625function enable_plugin {
1626 local name=$1
1627 local url=$2
1628 local branch=${3:-master}
1629 DEVSTACK_PLUGINS+=",$name"
1630 GITREPO[$name]=$url
1631 GITDIR[$name]=$DEST/$name
1632 GITBRANCH[$name]=$branch
1633}
1634
1635# fetch_plugins
1636#
1637# clones all plugins
1638function fetch_plugins {
1639 local plugins="${DEVSTACK_PLUGINS}"
1640 local plugin
1641
1642 # short circuit if nothing to do
1643 if [[ -z $plugins ]]; then
1644 return
1645 fi
1646
1647 echo "Fetching devstack plugins"
1648 for plugin in ${plugins//,/ }; do
1649 git_clone_by_name $plugin
1650 done
1651}
1652
1653# load_plugin_settings
1654#
1655# Load settings from plugins in the order that they were registered
1656function load_plugin_settings {
1657 local plugins="${DEVSTACK_PLUGINS}"
1658 local plugin
1659
1660 # short circuit if nothing to do
1661 if [[ -z $plugins ]]; then
1662 return
1663 fi
1664
1665 echo "Loading plugin settings"
1666 for plugin in ${plugins//,/ }; do
1667 local dir=${GITDIR[$plugin]}
1668 # source any known settings
1669 if [[ -f $dir/devstack/settings ]]; then
1670 source $dir/devstack/settings
1671 fi
1672 done
1673}
1674
1675# run_plugins
1676#
1677# Run the devstack/plugin.sh in all the plugin directories. These are
1678# run in registration order.
1679function run_plugins {
1680 local mode=$1
1681 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301682
1683 local plugins="${DEVSTACK_PLUGINS}"
1684 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001685 for plugin in ${plugins//,/ }; do
1686 local dir=${GITDIR[$plugin]}
1687 if [[ -f $dir/devstack/plugin.sh ]]; then
1688 source $dir/devstack/plugin.sh $mode $phase
1689 fi
1690 done
1691}
1692
1693function run_phase {
1694 local mode=$1
1695 local phase=$2
1696 if [[ -d $TOP_DIR/extras.d ]]; then
1697 for i in $TOP_DIR/extras.d/*.sh; do
1698 [[ -r $i ]] && source $i $mode $phase
1699 done
1700 fi
1701 # the source phase corresponds to settings loading in plugins
1702 if [[ "$mode" == "source" ]]; then
1703 load_plugin_settings
1704 else
1705 run_plugins $mode $phase
1706 fi
1707}
1708
Dean Troyerdff49a22014-01-30 15:37:40 -06001709
1710# Service Functions
1711# =================
1712
1713# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1714# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001715function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001716 echo "$1" | sed -e '
1717 s/,,/,/g;
1718 s/^,//;
1719 s/,$//
1720 '
1721}
1722
1723# disable_all_services() removes all current services
1724# from ``ENABLED_SERVICES`` to reset the configuration
1725# before a minimal installation
1726# Uses global ``ENABLED_SERVICES``
1727# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001728function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001729 ENABLED_SERVICES=""
1730}
1731
1732# Remove all services starting with '-'. For example, to install all default
1733# services except rabbit (rabbit) set in ``localrc``:
1734# ENABLED_SERVICES+=",-rabbit"
1735# Uses global ``ENABLED_SERVICES``
1736# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001737function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001738 local tmpsvcs="${ENABLED_SERVICES}"
1739 local service
1740 for service in ${tmpsvcs//,/ }; do
1741 if [[ ${service} == -* ]]; then
1742 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1743 fi
1744 done
1745 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1746}
1747
1748# disable_service() removes the services passed as argument to the
1749# ``ENABLED_SERVICES`` list, if they are present.
1750#
1751# For example:
1752# disable_service rabbit
1753#
1754# This function does not know about the special cases
1755# for nova, glance, and neutron built into is_service_enabled().
1756# Uses global ``ENABLED_SERVICES``
1757# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001758function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001759 local tmpsvcs=",${ENABLED_SERVICES},"
1760 local service
1761 for service in $@; do
1762 if is_service_enabled $service; then
1763 tmpsvcs=${tmpsvcs//,$service,/,}
1764 fi
1765 done
1766 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1767}
1768
1769# enable_service() adds the services passed as argument to the
1770# ``ENABLED_SERVICES`` list, if they are not already present.
1771#
1772# For example:
1773# enable_service qpid
1774#
1775# This function does not know about the special cases
1776# for nova, glance, and neutron built into is_service_enabled().
1777# Uses global ``ENABLED_SERVICES``
1778# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001779function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001780 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001781 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001782 for service in $@; do
1783 if ! is_service_enabled $service; then
1784 tmpsvcs+=",$service"
1785 fi
1786 done
1787 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1788 disable_negated_services
1789}
1790
1791# is_service_enabled() checks if the service(s) specified as arguments are
1792# enabled by the user in ``ENABLED_SERVICES``.
1793#
1794# Multiple services specified as arguments are ``OR``'ed together; the test
1795# is a short-circuit boolean, i.e it returns on the first match.
1796#
1797# There are special cases for some 'catch-all' services::
1798# **nova** returns true if any service enabled start with **n-**
1799# **cinder** returns true if any service enabled start with **c-**
1800# **ceilometer** returns true if any service enabled start with **ceilometer**
1801# **glance** returns true if any service enabled start with **g-**
1802# **neutron** returns true if any service enabled start with **q-**
1803# **swift** returns true if any service enabled start with **s-**
1804# **trove** returns true if any service enabled start with **tr-**
1805# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1806# **s-** services will be enabled. This will be deprecated in the future.
1807#
1808# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1809# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1810# as enabled in this case.
1811#
1812# Uses global ``ENABLED_SERVICES``
1813# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001814function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001815 local xtrace=$(set +o | grep xtrace)
1816 set +o xtrace
1817 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001818 local services=$@
1819 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001820 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001821 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001822
1823 # Look for top-level 'enabled' function for this service
1824 if type is_${service}_enabled >/dev/null 2>&1; then
1825 # A function exists for this service, use it
1826 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001827 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001828 fi
1829
1830 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1831 # are implemented
1832
Sean Dague45917cc2014-02-24 16:09:14 -05001833 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001834 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001835 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1836 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1837 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1838 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1839 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1840 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1841 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1842 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1843 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001844 done
Sean Dague45917cc2014-02-24 16:09:14 -05001845 $xtrace
1846 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001847}
1848
1849# Toggle enable/disable_service for services that must run exclusive of each other
1850# $1 The name of a variable containing a space-separated list of services
1851# $2 The name of a variable in which to store the enabled service's name
1852# $3 The name of the service to enable
1853function use_exclusive_service {
1854 local options=${!1}
1855 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001856 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001857 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001858 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001859 for opt in $options;do
1860 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1861 done
1862 eval "$out=$selection"
1863 return 0
1864}
1865
1866
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001867# System Functions
1868# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001869
1870# Only run the command if the target file (the last arg) is not on an
1871# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001872function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001873 local xtrace=$(set +o | grep xtrace)
1874 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001875 local args=( $@ )
1876 local last
1877 local sudo_cmd
1878 local dir_to_check
1879
1880 let last="${#args[*]} - 1"
1881
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001882 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001883 if [ ! -d "$dir_to_check" ]; then
1884 dir_to_check=`dirname "$dir_to_check"`
1885 fi
1886
1887 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001888 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001889 return 0
1890 fi
1891
1892 if [[ $TRACK_DEPENDS = True ]]; then
1893 sudo_cmd="env"
1894 else
1895 sudo_cmd="sudo"
1896 fi
1897
Sean Dague45917cc2014-02-24 16:09:14 -05001898 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001899 $sudo_cmd $@
1900}
1901
1902# Exit 0 if address is in network or 1 if address is not in network
1903# ip-range is in CIDR notation: 1.2.3.4/20
1904# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001905function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001906 local ip=$1
1907 local range=$2
1908 local masklen=${range#*/}
1909 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1910 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1911 [[ $network == $subnet ]]
1912}
1913
1914# Add a user to a group.
1915# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001916function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001917 local user=$1
1918 local group=$2
1919
1920 if [[ -z "$os_VENDOR" ]]; then
1921 GetOSVersion
1922 fi
1923
1924 # SLE11 and openSUSE 12.2 don't have the usual usermod
1925 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1926 sudo usermod -a -G "$group" "$user"
1927 else
1928 sudo usermod -A "$group" "$user"
1929 fi
1930}
1931
1932# Convert CIDR notation to a IPv4 netmask
1933# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001934function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001935 local maskpat="255 255 255 255"
1936 local maskdgt="254 252 248 240 224 192 128"
1937 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1938 echo ${1-0}.${2-0}.${3-0}.${4-0}
1939}
1940
1941# Gracefully cp only if source file/dir exists
1942# cp_it source destination
1943function cp_it {
1944 if [ -e $1 ] || [ -d $1 ]; then
1945 cp -pRL $1 $2
1946 fi
1947}
1948
1949# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1950# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1951# ``localrc`` or on the command line if necessary::
1952#
1953# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1954#
1955# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1956
Ian Wienandaee18c72014-02-21 15:35:08 +11001957function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001958 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001959 export http_proxy=$http_proxy
1960 fi
Sean Dague53753292014-12-04 19:38:15 -05001961 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001962 export https_proxy=$https_proxy
1963 fi
Sean Dague53753292014-12-04 19:38:15 -05001964 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001965 export no_proxy=$no_proxy
1966 fi
1967}
1968
1969# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001970function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001971 local mount_type=`stat -f -L -c %T $1`
1972 test "$mount_type" == "nfs"
1973}
1974
1975# Return the network portion of the given IP address using netmask
1976# netmask is in the traditional dotted-quad format
1977# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001978function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001979 local ip=$1
1980 local mask=$2
1981 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1982 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1983 echo $subnet
1984}
1985
1986# Service wrapper to restart services
1987# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001988function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001989 if is_ubuntu; then
1990 sudo /usr/sbin/service $1 restart
1991 else
1992 sudo /sbin/service $1 restart
1993 fi
1994}
1995
1996# Only change permissions of a file or directory if it is not on an
1997# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001998function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001999 _safe_permission_operation chmod $@
2000}
2001
2002# Only change ownership of a file or directory if it is not on an NFS
2003# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002004function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002005 _safe_permission_operation chown $@
2006}
2007
2008# Service wrapper to start services
2009# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002010function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002011 if is_ubuntu; then
2012 sudo /usr/sbin/service $1 start
2013 else
2014 sudo /sbin/service $1 start
2015 fi
2016}
2017
2018# Service wrapper to stop services
2019# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002020function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002021 if is_ubuntu; then
2022 sudo /usr/sbin/service $1 stop
2023 else
2024 sudo /sbin/service $1 stop
2025 fi
2026}
2027
2028
2029# Restore xtrace
2030$XTRACE
2031
2032# Local variables:
2033# mode: shell-script
2034# End: