blob: a6ecdd6983a10871c856eeafa87406f27866a4a6 [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
Ian Wienandbdc90c52014-08-04 15:44:58 +1000530# Quick check for a rackspace host; n.b. rackspace provided images
531# have these Xen tools installed but a custom image may not.
532function is_rackspace {
533 [ -f /usr/bin/xenstore-ls ] && \
534 sudo /usr/bin/xenstore-ls vm-data | grep -q "Rackspace"
535}
536
Dean Troyerdff49a22014-01-30 15:37:40 -0600537# Determine if current distribution is a Fedora-based distribution
538# (Fedora, RHEL, CentOS, etc).
539# is_fedora
540function is_fedora {
541 if [[ -z "$os_VENDOR" ]]; then
542 GetOSVersion
543 fi
544
anju Tiwari6c639c92014-07-15 18:11:54 +0530545 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
546 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600547}
548
549
550# Determine if current distribution is a SUSE-based distribution
551# (openSUSE, SLE).
552# is_suse
553function is_suse {
554 if [[ -z "$os_VENDOR" ]]; then
555 GetOSVersion
556 fi
557
558 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
559}
560
561
562# Determine if current distribution is an Ubuntu-based distribution
563# It will also detect non-Ubuntu but Debian-based distros
564# is_ubuntu
565function is_ubuntu {
566 if [[ -z "$os_PACKAGE" ]]; then
567 GetOSVersion
568 fi
569 [ "$os_PACKAGE" = "deb" ]
570}
571
572
573# Git Functions
574# =============
575
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600576# Returns openstack release name for a given branch name
577# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100578function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600579 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700580 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600581 echo ${branch#*/}
582 else
583 echo "master"
584 fi
585}
586
Dean Troyerdff49a22014-01-30 15:37:40 -0600587# git clone only if directory doesn't exist already. Since ``DEST`` might not
588# be owned by the installation user, we create the directory and change the
589# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500590# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
591# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600592# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500593# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600594# git_clone remote dest-dir branch
595function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500596 local git_remote=$1
597 local git_dest=$2
598 local git_ref=$3
599 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200600 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500601
Sean Dague53753292014-12-04 19:38:15 -0500602 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800603 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200604 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
605 fi
606
Dean Troyerdff49a22014-01-30 15:37:40 -0600607 if [[ "$OFFLINE" = "True" ]]; then
608 echo "Running in offline mode, clones already exist"
609 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500610 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600611 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400612 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600613 return
614 fi
615
Dean Troyer50cda692014-07-25 11:57:20 -0500616 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600617 # If our branch name is a gerrit style refs/changes/...
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 Troyerdff49a22014-01-30 15:37:40 -0600622 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500623 cd $git_dest
624 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600625 else
626 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500627 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600628 [[ "$ERROR_ON_CLONE" = "True" ]] && \
629 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200630 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500631 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600632 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500633 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600634 elif [[ "$RECLONE" = "True" ]]; then
635 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500636 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600637 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500638 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100639 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600640 # remove the existing ignored files (like pyc) as they cause breakage
641 # (due to the py files having older timestamps than our pyc, so python
642 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500643 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600644
Dean Troyer50cda692014-07-25 11:57:20 -0500645 # handle git_ref accordingly to type (tag, branch)
646 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
647 git_update_tag $git_ref
648 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
649 git_update_branch $git_ref
650 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
651 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600652 else
Dean Troyer50cda692014-07-25 11:57:20 -0500653 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600654 fi
655
656 fi
657 fi
658
659 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500660 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600661 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400662 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600663}
664
Sean Daguecc524062014-10-01 09:06:43 -0400665# A variation on git clone that lets us specify a project by it's
666# actual name, like oslo.config. This is exceptionally useful in the
667# library installation case
668function git_clone_by_name {
669 local name=$1
670 local repo=${GITREPO[$name]}
671 local dir=${GITDIR[$name]}
672 local branch=${GITBRANCH[$name]}
673 git_clone $repo $dir $branch
674}
675
676
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100677# git can sometimes get itself infinitely stuck with transient network
678# errors or other issues with the remote end. This wraps git in a
679# timeout/retry loop and is intended to watch over non-local git
680# processes that might hang. GIT_TIMEOUT, if set, is passed directly
681# to timeout(1); otherwise the default value of 0 maintains the status
682# quo of waiting forever.
683# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100684function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100685 local count=0
686 local timeout=0
687
688 if [[ -n "${GIT_TIMEOUT}" ]]; then
689 timeout=${GIT_TIMEOUT}
690 fi
691
692 until timeout -s SIGINT ${timeout} git "$@"; do
693 # 124 is timeout(1)'s special return code when it reached the
694 # timeout; otherwise assume fatal failure
695 if [[ $? -ne 124 ]]; then
696 die $LINENO "git call failed: [git $@]"
697 fi
698
699 count=$(($count + 1))
700 warn "timeout ${count} for git call: [git $@]"
701 if [ $count -eq 3 ]; then
702 die $LINENO "Maximum of 3 git retries reached"
703 fi
704 sleep 5
705 done
706}
707
Dean Troyerdff49a22014-01-30 15:37:40 -0600708# git update using reference as a branch.
709# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100710function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500711 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600712
Dean Troyer50cda692014-07-25 11:57:20 -0500713 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600714 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500715 git branch -D $git_branch || true
716 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600717}
718
719# git update using reference as a branch.
720# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100721function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500722 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600723
Dean Troyer50cda692014-07-25 11:57:20 -0500724 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600725}
726
727# git update using reference as a tag. Be careful editing source at that repo
728# as working copy will be in a detached mode
729# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100730function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500731 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600732
Dean Troyer50cda692014-07-25 11:57:20 -0500733 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600734 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500735 git_timed fetch origin tag $git_tag
736 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600737}
738
739
740# OpenStack Functions
741# ===================
742
743# Get the default value for HOST_IP
744# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100745function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600746 local fixed_range=$1
747 local floating_range=$2
748 local host_ip_iface=$3
749 local host_ip=$4
750
751 # Find the interface used for the default route
752 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
753 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
754 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
755 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500756 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
757 local ip
758 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600759 # Attempt to filter out IP addresses that are part of the fixed and
760 # floating range. Note that this method only works if the ``netaddr``
761 # python library is installed. If it is not installed, an error
762 # will be printed and the first IP from the interface will be used.
763 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
764 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500765 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
766 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600767 break;
768 fi
769 done
770 fi
771 echo $host_ip
772}
773
Attila Fazekasf71b5002014-05-28 09:52:22 +0200774# Generates hex string from ``size`` byte of pseudo random data
775# generate_hex_string size
776function generate_hex_string {
777 local size=$1
778 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
779}
780
Dean Troyerdff49a22014-01-30 15:37:40 -0600781# Grab a numbered field from python prettytable output
782# Fields are numbered starting with 1
783# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
784# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100785function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500786 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600787 while read data; do
788 if [ "$1" -lt 0 ]; then
789 field="(\$(NF$1))"
790 else
791 field="\$$(($1 + 1))"
792 fi
793 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
794 done
795}
796
797# Add a policy to a policy.json file
798# Do nothing if the policy already exists
799# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100800function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600801 local policy_file=$1
802 local policy_name=$2
803 local policy_perm=$3
804
805 if grep -q ${policy_name} ${policy_file}; then
806 echo "Policy ${policy_name} already exists in ${policy_file}"
807 return
808 fi
809
810 # Add a terminating comma to policy lines without one
811 # Remove the closing '}' and all lines following to the end-of-file
812 local tmpfile=$(mktemp)
813 uniq ${policy_file} | sed -e '
814 s/]$/],/
815 /^[}]/,$d
816 ' > ${tmpfile}
817
818 # Append policy and closing brace
819 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
820 echo "}" >>${tmpfile}
821
822 mv ${tmpfile} ${policy_file}
823}
824
Alistair Coles24779f62014-10-15 18:57:59 +0100825# Gets or creates a domain
826# Usage: get_or_create_domain <name> <description>
827function get_or_create_domain {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500828 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100829 # Gets domain id
830 local domain_id=$(
831 # Gets domain id
832 openstack --os-token=$OS_TOKEN --os-url=$os_url \
833 --os-identity-api-version=3 domain show $1 \
834 -f value -c id 2>/dev/null ||
835 # Creates new domain
836 openstack --os-token=$OS_TOKEN --os-url=$os_url \
837 --os-identity-api-version=3 domain create $1 \
838 --description "$2" \
839 -f value -c id
840 )
841 echo $domain_id
842}
843
Steve Martinellib74e01c2014-12-18 01:35:35 -0500844# Gets or creates group
845# Usage: get_or_create_group <groupname> [<domain> <description>]
846function get_or_create_group {
847 local domain=${2:+--domain ${2}}
848 local desc="${3:-}"
849 local os_url="$KEYSTONE_SERVICE_URI_V3"
850 # Gets group id
851 local group_id=$(
852 # Creates new group with --or-show
853 openstack --os-token=$OS_TOKEN --os-url=$os_url \
854 --os-identity-api-version=3 group create $1 \
855 $domain --description "$desc" --or-show \
856 -f value -c id
857 )
858 echo $group_id
859}
860
Bartosz Górski0abde392014-02-28 14:15:19 +0100861# Gets or creates user
Alistair Coles24779f62014-10-15 18:57:59 +0100862# Usage: get_or_create_user <username> <password> <project> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100863function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200864 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500865 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200866 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500867 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200868 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100869 local os_cmd="openstack"
870 local domain=""
871 if [[ ! -z "$5" ]]; then
872 domain="--domain=$5"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500873 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100874 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100875 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500876 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500877 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100878 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100879 $1 \
880 --password "$2" \
881 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500882 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100883 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500884 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100885 -f value -c id
886 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500887 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100888}
889
890# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100891# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100892function get_or_create_project {
893 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100894 local os_cmd="openstack"
895 local domain=""
896 if [[ ! -z "$2" ]]; then
897 domain="--domain=$2"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500898 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100899 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500900 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500901 # Creates new project with --or-show
902 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100903 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500904 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100905}
906
907# Gets or creates role
908# Usage: get_or_create_role <name>
909function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500910 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500911 # Creates role with --or-show
912 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100913 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500914 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100915}
916
917# Gets or adds user role
918# Usage: get_or_add_user_role <role> <user> <project>
919function get_or_add_user_role {
920 # Gets user role id
Steve Martinelli5541a612015-01-19 15:58:49 -0500921 local user_role_id=$(openstack role list \
922 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100923 --project $3 \
924 --column "ID" \
925 --column "Name" \
926 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500927 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100928 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500929 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100930 $1 \
931 --user $2 \
932 --project $3 \
933 | grep " id " | get_field 2)
934 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500935 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100936}
937
938# Gets or creates service
939# Usage: get_or_create_service <name> <type> <description>
940function get_or_create_service {
941 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500942 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100943 # Gets service id
944 openstack service show $1 -f value -c id 2>/dev/null ||
945 # Creates new service if not exists
946 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500947 $2 \
948 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100949 --description="$3" \
950 -f value -c id
951 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500952 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100953}
954
955# Gets or creates endpoint
956# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
957function get_or_create_endpoint {
958 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500959 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100960 --column "ID" \
961 --column "Region" \
962 --column "Service Name" \
963 | grep " $2 " \
964 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500965 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100966 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500967 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100968 $1 \
969 --region $2 \
970 --publicurl $3 \
971 --adminurl $4 \
972 --internalurl $5 \
973 | grep " id " | get_field 2)
974 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500975 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100976}
Dean Troyerdff49a22014-01-30 15:37:40 -0600977
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500978
Dean Troyerdff49a22014-01-30 15:37:40 -0600979# Package Functions
980# =================
981
982# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100983function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600984 local pkg_dir
985 if is_ubuntu; then
Monty Taylor81a016d2014-11-15 17:18:13 -0300986 pkg_dir=$FILES/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600987 elif is_fedora; then
988 pkg_dir=$FILES/rpms
989 elif is_suse; then
990 pkg_dir=$FILES/rpms-suse
991 else
992 exit_distro_not_supported "list of packages"
993 fi
994 echo "$pkg_dir"
995}
996
997# Wrapper for ``apt-get`` to set cache and proxy environment variables
998# Uses globals ``OFFLINE``, ``*_proxy``
999# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001000function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -05001001 local xtrace=$(set +o | grep xtrace)
1002 set +o xtrace
1003
Dean Troyerdff49a22014-01-30 15:37:40 -06001004 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
1005 local sudo="sudo"
1006 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -05001007
1008 $xtrace
Sean Dague53753292014-12-04 19:38:15 -05001009
Dean Troyerdff49a22014-01-30 15:37:40 -06001010 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -05001011 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
1012 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -06001013 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
1014}
1015
1016# get_packages() collects a list of package names of any type from the
Monty Taylor81a016d2014-11-15 17:18:13 -03001017# prerequisite files in ``files/{debs|rpms}``. The list is intended
Dean Troyerdff49a22014-01-30 15:37:40 -06001018# to be passed to a package installer such as apt or yum.
1019#
1020# Only packages required for the services in 1st argument will be
1021# included. Two bits of metadata are recognized in the prerequisite files:
1022#
1023# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1024# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1025# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +11001026function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -05001027 local xtrace=$(set +o | grep xtrace)
1028 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001029 local services=$@
1030 local package_dir=$(_get_package_dir)
Sean Dague53753292014-12-04 19:38:15 -05001031 local file_to_parse=""
1032 local service=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001033
Sean Dague53753292014-12-04 19:38:15 -05001034 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
Flavio Percoco5a91c352014-10-31 18:48:00 +01001035
Dean Troyerdff49a22014-01-30 15:37:40 -06001036 if [[ -z "$package_dir" ]]; then
1037 echo "No package directory supplied"
1038 return 1
1039 fi
1040 if [[ -z "$DISTRO" ]]; then
1041 GetDistro
1042 fi
1043 for service in ${services//,/ }; do
1044 # Allow individual services to specify dependencies
1045 if [[ -e ${package_dir}/${service} ]]; then
1046 file_to_parse="${file_to_parse} $service"
1047 fi
1048 # NOTE(sdague) n-api needs glance for now because that's where
1049 # glance client is
1050 if [[ $service == n-api ]]; then
1051 if [[ ! $file_to_parse =~ nova ]]; then
1052 file_to_parse="${file_to_parse} nova"
1053 fi
1054 if [[ ! $file_to_parse =~ glance ]]; then
1055 file_to_parse="${file_to_parse} glance"
1056 fi
1057 elif [[ $service == c-* ]]; then
1058 if [[ ! $file_to_parse =~ cinder ]]; then
1059 file_to_parse="${file_to_parse} cinder"
1060 fi
1061 elif [[ $service == ceilometer-* ]]; then
1062 if [[ ! $file_to_parse =~ ceilometer ]]; then
1063 file_to_parse="${file_to_parse} ceilometer"
1064 fi
1065 elif [[ $service == s-* ]]; then
1066 if [[ ! $file_to_parse =~ swift ]]; then
1067 file_to_parse="${file_to_parse} swift"
1068 fi
1069 elif [[ $service == n-* ]]; then
1070 if [[ ! $file_to_parse =~ nova ]]; then
1071 file_to_parse="${file_to_parse} nova"
1072 fi
1073 elif [[ $service == g-* ]]; then
1074 if [[ ! $file_to_parse =~ glance ]]; then
1075 file_to_parse="${file_to_parse} glance"
1076 fi
1077 elif [[ $service == key* ]]; then
1078 if [[ ! $file_to_parse =~ keystone ]]; then
1079 file_to_parse="${file_to_parse} keystone"
1080 fi
1081 elif [[ $service == q-* ]]; then
1082 if [[ ! $file_to_parse =~ neutron ]]; then
1083 file_to_parse="${file_to_parse} neutron"
1084 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001085 elif [[ $service == ir-* ]]; then
1086 if [[ ! $file_to_parse =~ ironic ]]; then
1087 file_to_parse="${file_to_parse} ironic"
1088 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001089 fi
1090 done
1091
1092 for file in ${file_to_parse}; do
1093 local fname=${package_dir}/${file}
1094 local OIFS line package distros distro
1095 [[ -e $fname ]] || continue
1096
1097 OIFS=$IFS
1098 IFS=$'\n'
1099 for line in $(<${fname}); do
1100 if [[ $line =~ "NOPRIME" ]]; then
1101 continue
1102 fi
1103
1104 # Assume we want this package
1105 package=${line%#*}
1106 inst_pkg=1
1107
1108 # Look for # dist:xxx in comment
1109 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1110 # We are using BASH regexp matching feature.
1111 package=${BASH_REMATCH[1]}
1112 distros=${BASH_REMATCH[2]}
1113 # In bash ${VAR,,} will lowecase VAR
1114 # Look for a match in the distro list
1115 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1116 # If no match then skip this package
1117 inst_pkg=0
1118 fi
1119 fi
1120
1121 # Look for # testonly in comment
1122 if [[ $line =~ (.*)#.*testonly.* ]]; then
1123 package=${BASH_REMATCH[1]}
1124 # Are we installing test packages? (test for the default value)
1125 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1126 # If not installing test packages the skip this package
1127 inst_pkg=0
1128 fi
1129 fi
1130
1131 if [[ $inst_pkg = 1 ]]; then
1132 echo $package
1133 fi
1134 done
1135 IFS=$OIFS
1136 done
Sean Dague45917cc2014-02-24 16:09:14 -05001137 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001138}
1139
1140# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001141# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001142# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001143function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001144 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1145 REPOS_UPDATED=${REPOS_UPDATED:-False}
1146 RETRY_UPDATE=${RETRY_UPDATE:-False}
1147
Paul Linchpiner9e179742014-07-13 22:23:00 -07001148 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001149 return 0
1150 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001151
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001152 if is_ubuntu; then
1153 local xtrace=$(set +o | grep xtrace)
1154 set +o xtrace
1155 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1156 # if there are transient errors pulling the updates, that's fine.
1157 # It may be secondary repositories that we don't really care about.
1158 apt_get update || /bin/true
1159 REPOS_UPDATED=True
1160 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001161 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001162 fi
1163}
1164
1165function real_install_package {
1166 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001167 apt_get install "$@"
1168 elif is_fedora; then
1169 yum_install "$@"
1170 elif is_suse; then
1171 zypper_install "$@"
1172 else
1173 exit_distro_not_supported "installing packages"
1174 fi
1175}
1176
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001177# Distro-agnostic package installer
1178# install_package package [package ...]
1179function install_package {
1180 update_package_repo
1181 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1182}
1183
Dean Troyerdff49a22014-01-30 15:37:40 -06001184# Distro-agnostic function to tell if a package is installed
1185# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001186function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001187 if [[ -z "$@" ]]; then
1188 return 1
1189 fi
1190
1191 if [[ -z "$os_PACKAGE" ]]; then
1192 GetOSVersion
1193 fi
1194
1195 if [[ "$os_PACKAGE" = "deb" ]]; then
1196 dpkg -s "$@" > /dev/null 2> /dev/null
1197 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1198 rpm --quiet -q "$@"
1199 else
1200 exit_distro_not_supported "finding if a package is installed"
1201 fi
1202}
1203
1204# Distro-agnostic package uninstaller
1205# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001206function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001207 if is_ubuntu; then
1208 apt_get purge "$@"
1209 elif is_fedora; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001210 sudo $YUM remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001211 elif is_suse; then
1212 sudo zypper rm "$@"
1213 else
1214 exit_distro_not_supported "uninstalling packages"
1215 fi
1216}
1217
1218# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001219# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001220# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001221function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001222 [[ "$OFFLINE" = "True" ]] && return
1223 local sudo="sudo"
1224 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001225
1226 # The manual check for missing packages is because yum -y assumes
1227 # missing packages are OK. See
1228 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001229 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1230 no_proxy=$no_proxy \
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001231 $YUM install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001232 awk '
1233 BEGIN { fail=0 }
1234 /No package/ { fail=1 }
1235 { print }
1236 END { exit fail }' || \
1237 die $LINENO "Missing packages detected"
1238
1239 # also ensure we catch a yum failure
1240 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001241 die $LINENO "$YUM install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001242 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001243}
1244
1245# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001246# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001247# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001248function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001249 [[ "$OFFLINE" = "True" ]] && return
1250 local sudo="sudo"
1251 [[ "$(id -u)" = "0" ]] && sudo="env"
1252 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1253 zypper --non-interactive install --auto-agree-with-licenses "$@"
1254}
1255
1256
1257# Process Functions
1258# =================
1259
1260# _run_process() is designed to be backgrounded by run_process() to simulate a
1261# fork. It includes the dirty work of closing extra filehandles and preparing log
1262# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001263# from the service name.
1264# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001265# If an optional group is provided sg will be used to set the group of
1266# the command.
1267# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001268function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001269 local service=$1
1270 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001271 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001272
1273 # Undo logging redirections and close the extra descriptors
1274 exec 1>&3
1275 exec 2>&3
1276 exec 3>&-
1277 exec 6>&-
1278
Dean Troyerdde41d02014-12-09 17:47:57 -06001279 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1280 if [[ -n ${LOGDIR} ]]; then
1281 exec 1>&"$real_logfile" 2>&1
1282 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1283 if [[ -n ${SCREEN_LOGDIR} ]]; then
1284 # Drop the backward-compat symlink
1285 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1286 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001287
1288 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1289 export PYTHONUNBUFFERED=1
1290 fi
1291
Dean Troyer3159a822014-08-27 14:13:58 -05001292 # Run under ``setsid`` to force the process to become a session and group leader.
1293 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001294 if [[ -n "$group" ]]; then
1295 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1296 else
1297 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1298 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001299
1300 # Just silently exit this process
1301 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001302}
1303
1304# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1305# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001306# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001307# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001308function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001309 SCREEN_NAME=${SCREEN_NAME:-stack}
1310 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1311
1312 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1313 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1314 fi
1315
1316 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1317}
1318
1319# Find out if a process exists by partial name.
1320# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001321function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001322 local name=$1
1323 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001324 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001325 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001326 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001327}
1328
Dean Troyer3159a822014-08-27 14:13:58 -05001329# Run a single service under screen or directly
1330# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001331# If an optional group is provided sg will be used to run the
1332# command as that group.
1333# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001334function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001335 local service=$1
1336 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001337 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001338
Dean Troyer3159a822014-08-27 14:13:58 -05001339 if is_service_enabled $service; then
1340 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001341 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001342 else
1343 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001344 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001345 fi
1346 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001347}
1348
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001349# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001350# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001351# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001352# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001353# Run a command in a shell in a screen window, if an optional group
1354# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001355function screen_process {
1356 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001357 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001358 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001359
Sean Dagueea22a4f2014-06-27 15:21:41 -04001360 SCREEN_NAME=${SCREEN_NAME:-stack}
1361 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001362 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001363
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001364 # Append the process to the screen rc file
1365 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001366
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001367 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001368
Dean Troyerdde41d02014-12-09 17:47:57 -06001369 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1370 echo "LOGDIR: $LOGDIR"
1371 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1372 echo "log: $real_logfile"
1373 if [[ -n ${LOGDIR} ]]; then
1374 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001375 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001376 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1377 if [[ -n ${SCREEN_LOGDIR} ]]; then
1378 # Drop the backward-compat symlink
1379 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1380 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001381 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001382
1383 # sleep to allow bash to be ready to be send the command - we are
1384 # creating a new window in screen and then sends characters, so if
1385 # bash isn't running by the time we send the command, nothing happens
1386 sleep 3
1387
1388 NL=`echo -ne '\015'`
1389 # This fun command does the following:
1390 # - the passed server command is backgrounded
1391 # - the pid of the background process is saved in the usual place
1392 # - the server process is brought back to the foreground
1393 # - if the server process exits prematurely the fg command errors
1394 # and a message is written to stdout and the process failure file
1395 #
1396 # The pid saved can be used in stop_process() as a process group
1397 # id to kill off all child processes
1398 if [[ -n "$group" ]]; then
1399 command="sg $group '$command'"
1400 fi
1401 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 -06001402}
1403
1404# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001405# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001406# screen_rc service "command-line"
1407function screen_rc {
1408 SCREEN_NAME=${SCREEN_NAME:-stack}
1409 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1410 if [[ ! -e $SCREENRC ]]; then
1411 # Name the screen session
1412 echo "sessionname $SCREEN_NAME" > $SCREENRC
1413 # Set a reasonable statusbar
1414 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1415 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1416 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1417 echo "screen -t shell bash" >> $SCREENRC
1418 fi
1419 # If this service doesn't already exist in the screenrc file
1420 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1421 NL=`echo -ne '\015'`
1422 echo "screen -t $1 bash" >> $SCREENRC
1423 echo "stuff \"$2$NL\"" >> $SCREENRC
1424
Dean Troyerdde41d02014-12-09 17:47:57 -06001425 if [[ -n ${LOGDIR} ]]; then
1426 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001427 echo "log on" >>$SCREENRC
1428 fi
1429 fi
1430}
1431
1432# Stop a service in screen
1433# If a PID is available use it, kill the whole process group via TERM
1434# If screen is being used kill the screen window; this will catch processes
1435# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001436# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001437# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001438function screen_stop_service {
1439 local service=$1
1440
Dean Troyerdff49a22014-01-30 15:37:40 -06001441 SCREEN_NAME=${SCREEN_NAME:-stack}
1442 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001443 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001444
Dean Troyer3159a822014-08-27 14:13:58 -05001445 if is_service_enabled $service; then
1446 # Clean up the screen window
1447 screen -S $SCREEN_NAME -p $service -X kill
1448 fi
1449}
1450
1451# Stop a service process
1452# If a PID is available use it, kill the whole process group via TERM
1453# If screen is being used kill the screen window; this will catch processes
1454# that did not leave a PID behind
1455# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1456# stop_process service
1457function stop_process {
1458 local service=$1
1459
1460 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001461 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001462
1463 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001464 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001465 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1466 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1467 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001468 fi
1469 if [[ "$USE_SCREEN" = "True" ]]; then
1470 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001471 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001472 fi
1473 fi
1474}
1475
1476# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001477# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001478# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001479function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001480 local service
1481 local failures
1482 SCREEN_NAME=${SCREEN_NAME:-stack}
1483 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1484
1485
1486 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1487 echo "No service status directory found"
1488 return
1489 fi
1490
1491 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001492 # make this -o errexit safe
1493 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001494
1495 for service in $failures; do
1496 service=`basename $service`
1497 service=${service%.failure}
1498 echo "Error: Service $service is not running"
1499 done
1500
1501 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001502 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001503 fi
1504}
1505
Chris Dent2f27a0e2014-09-09 13:46:02 +01001506# Tail a log file in a screen if USE_SCREEN is true.
1507function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001508 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001509 local logfile=$2
1510
Sean Dague53753292014-12-04 19:38:15 -05001511 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001512 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001513 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001514 fi
1515}
1516
Dean Troyerdff49a22014-01-30 15:37:40 -06001517
Dean Troyer3159a822014-08-27 14:13:58 -05001518# Deprecated Functions
1519# --------------------
1520
1521# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1522# fork. It includes the dirty work of closing extra filehandles and preparing log
1523# files to produce the same logs as screen_it(). The log filename is derived
1524# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1525# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1526# _old_run_process service "command-line"
1527function _old_run_process {
1528 local service=$1
1529 local command="$2"
1530
1531 # Undo logging redirections and close the extra descriptors
1532 exec 1>&3
1533 exec 2>&3
1534 exec 3>&-
1535 exec 6>&-
1536
1537 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001538 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1539 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001540
1541 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1542 export PYTHONUNBUFFERED=1
1543 fi
1544
1545 exec /bin/bash -c "$command"
1546 die "$service exec failure: $command"
1547}
1548
1549# old_run_process() launches a child process that closes all file descriptors and
1550# then exec's the passed in command. This is meant to duplicate the semantics
1551# of screen_it() without screen. PIDs are written to
1552# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1553# old_run_process service "command-line"
1554function old_run_process {
1555 local service=$1
1556 local command="$2"
1557
1558 # Spawn the child process
1559 _old_run_process "$service" "$command" &
1560 echo $!
1561}
1562
1563# Compatibility for existing start_XXXX() functions
1564# Uses global ``USE_SCREEN``
1565# screen_it service "command-line"
1566function screen_it {
1567 if is_service_enabled $1; then
1568 # Append the service to the screen rc file
1569 screen_rc "$1" "$2"
1570
1571 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001572 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001573 else
1574 # Spawn directly without screen
1575 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1576 fi
1577 fi
1578}
1579
1580# Compatibility for existing stop_XXXX() functions
1581# Stop a service in screen
1582# If a PID is available use it, kill the whole process group via TERM
1583# If screen is being used kill the screen window; this will catch processes
1584# that did not leave a PID behind
1585# screen_stop service
1586function screen_stop {
1587 # Clean up the screen window
1588 stop_process $1
1589}
1590
1591
Sean Dague2c65e712014-12-18 09:44:56 -05001592# Plugin Functions
1593# =================
1594
1595DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1596
1597# enable_plugin <name> <url> [branch]
1598#
1599# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1600# ``url`` is a git url
1601# ``branch`` is a gitref. If it's not set, defaults to master
1602function enable_plugin {
1603 local name=$1
1604 local url=$2
1605 local branch=${3:-master}
1606 DEVSTACK_PLUGINS+=",$name"
1607 GITREPO[$name]=$url
1608 GITDIR[$name]=$DEST/$name
1609 GITBRANCH[$name]=$branch
1610}
1611
1612# fetch_plugins
1613#
1614# clones all plugins
1615function fetch_plugins {
1616 local plugins="${DEVSTACK_PLUGINS}"
1617 local plugin
1618
1619 # short circuit if nothing to do
1620 if [[ -z $plugins ]]; then
1621 return
1622 fi
1623
1624 echo "Fetching devstack plugins"
1625 for plugin in ${plugins//,/ }; do
1626 git_clone_by_name $plugin
1627 done
1628}
1629
1630# load_plugin_settings
1631#
1632# Load settings from plugins in the order that they were registered
1633function load_plugin_settings {
1634 local plugins="${DEVSTACK_PLUGINS}"
1635 local plugin
1636
1637 # short circuit if nothing to do
1638 if [[ -z $plugins ]]; then
1639 return
1640 fi
1641
1642 echo "Loading plugin settings"
1643 for plugin in ${plugins//,/ }; do
1644 local dir=${GITDIR[$plugin]}
1645 # source any known settings
1646 if [[ -f $dir/devstack/settings ]]; then
1647 source $dir/devstack/settings
1648 fi
1649 done
1650}
1651
1652# run_plugins
1653#
1654# Run the devstack/plugin.sh in all the plugin directories. These are
1655# run in registration order.
1656function run_plugins {
1657 local mode=$1
1658 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301659
1660 local plugins="${DEVSTACK_PLUGINS}"
1661 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001662 for plugin in ${plugins//,/ }; do
1663 local dir=${GITDIR[$plugin]}
1664 if [[ -f $dir/devstack/plugin.sh ]]; then
1665 source $dir/devstack/plugin.sh $mode $phase
1666 fi
1667 done
1668}
1669
1670function run_phase {
1671 local mode=$1
1672 local phase=$2
1673 if [[ -d $TOP_DIR/extras.d ]]; then
1674 for i in $TOP_DIR/extras.d/*.sh; do
1675 [[ -r $i ]] && source $i $mode $phase
1676 done
1677 fi
1678 # the source phase corresponds to settings loading in plugins
1679 if [[ "$mode" == "source" ]]; then
1680 load_plugin_settings
1681 else
1682 run_plugins $mode $phase
1683 fi
1684}
1685
Dean Troyerdff49a22014-01-30 15:37:40 -06001686
1687# Service Functions
1688# =================
1689
1690# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1691# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001692function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001693 echo "$1" | sed -e '
1694 s/,,/,/g;
1695 s/^,//;
1696 s/,$//
1697 '
1698}
1699
1700# disable_all_services() removes all current services
1701# from ``ENABLED_SERVICES`` to reset the configuration
1702# before a minimal installation
1703# Uses global ``ENABLED_SERVICES``
1704# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001705function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001706 ENABLED_SERVICES=""
1707}
1708
1709# Remove all services starting with '-'. For example, to install all default
1710# services except rabbit (rabbit) set in ``localrc``:
1711# ENABLED_SERVICES+=",-rabbit"
1712# Uses global ``ENABLED_SERVICES``
1713# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001714function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001715 local tmpsvcs="${ENABLED_SERVICES}"
1716 local service
1717 for service in ${tmpsvcs//,/ }; do
1718 if [[ ${service} == -* ]]; then
1719 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1720 fi
1721 done
1722 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1723}
1724
1725# disable_service() removes the services passed as argument to the
1726# ``ENABLED_SERVICES`` list, if they are present.
1727#
1728# For example:
1729# disable_service rabbit
1730#
1731# This function does not know about the special cases
1732# for nova, glance, and neutron built into is_service_enabled().
1733# Uses global ``ENABLED_SERVICES``
1734# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001735function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001736 local tmpsvcs=",${ENABLED_SERVICES},"
1737 local service
1738 for service in $@; do
1739 if is_service_enabled $service; then
1740 tmpsvcs=${tmpsvcs//,$service,/,}
1741 fi
1742 done
1743 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1744}
1745
1746# enable_service() adds the services passed as argument to the
1747# ``ENABLED_SERVICES`` list, if they are not already present.
1748#
1749# For example:
1750# enable_service qpid
1751#
1752# This function does not know about the special cases
1753# for nova, glance, and neutron built into is_service_enabled().
1754# Uses global ``ENABLED_SERVICES``
1755# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001756function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001757 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001758 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001759 for service in $@; do
1760 if ! is_service_enabled $service; then
1761 tmpsvcs+=",$service"
1762 fi
1763 done
1764 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1765 disable_negated_services
1766}
1767
1768# is_service_enabled() checks if the service(s) specified as arguments are
1769# enabled by the user in ``ENABLED_SERVICES``.
1770#
1771# Multiple services specified as arguments are ``OR``'ed together; the test
1772# is a short-circuit boolean, i.e it returns on the first match.
1773#
1774# There are special cases for some 'catch-all' services::
1775# **nova** returns true if any service enabled start with **n-**
1776# **cinder** returns true if any service enabled start with **c-**
1777# **ceilometer** returns true if any service enabled start with **ceilometer**
1778# **glance** returns true if any service enabled start with **g-**
1779# **neutron** returns true if any service enabled start with **q-**
1780# **swift** returns true if any service enabled start with **s-**
1781# **trove** returns true if any service enabled start with **tr-**
1782# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1783# **s-** services will be enabled. This will be deprecated in the future.
1784#
1785# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1786# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1787# as enabled in this case.
1788#
1789# Uses global ``ENABLED_SERVICES``
1790# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001791function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001792 local xtrace=$(set +o | grep xtrace)
1793 set +o xtrace
1794 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001795 local services=$@
1796 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001797 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001798 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001799
1800 # Look for top-level 'enabled' function for this service
1801 if type is_${service}_enabled >/dev/null 2>&1; then
1802 # A function exists for this service, use it
1803 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001804 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001805 fi
1806
1807 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1808 # are implemented
1809
Sean Dague45917cc2014-02-24 16:09:14 -05001810 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001811 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001812 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1813 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1814 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1815 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1816 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1817 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1818 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1819 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1820 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001821 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001822 done
Sean Dague45917cc2014-02-24 16:09:14 -05001823 $xtrace
1824 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001825}
1826
1827# Toggle enable/disable_service for services that must run exclusive of each other
1828# $1 The name of a variable containing a space-separated list of services
1829# $2 The name of a variable in which to store the enabled service's name
1830# $3 The name of the service to enable
1831function use_exclusive_service {
1832 local options=${!1}
1833 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001834 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001835 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001836 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001837 for opt in $options;do
1838 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1839 done
1840 eval "$out=$selection"
1841 return 0
1842}
1843
1844
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001845# System Functions
1846# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001847
1848# Only run the command if the target file (the last arg) is not on an
1849# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001850function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001851 local xtrace=$(set +o | grep xtrace)
1852 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001853 local args=( $@ )
1854 local last
1855 local sudo_cmd
1856 local dir_to_check
1857
1858 let last="${#args[*]} - 1"
1859
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001860 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001861 if [ ! -d "$dir_to_check" ]; then
1862 dir_to_check=`dirname "$dir_to_check"`
1863 fi
1864
1865 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001866 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001867 return 0
1868 fi
1869
1870 if [[ $TRACK_DEPENDS = True ]]; then
1871 sudo_cmd="env"
1872 else
1873 sudo_cmd="sudo"
1874 fi
1875
Sean Dague45917cc2014-02-24 16:09:14 -05001876 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001877 $sudo_cmd $@
1878}
1879
1880# Exit 0 if address is in network or 1 if address is not in network
1881# ip-range is in CIDR notation: 1.2.3.4/20
1882# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001883function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001884 local ip=$1
1885 local range=$2
1886 local masklen=${range#*/}
1887 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1888 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1889 [[ $network == $subnet ]]
1890}
1891
1892# Add a user to a group.
1893# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001894function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001895 local user=$1
1896 local group=$2
1897
1898 if [[ -z "$os_VENDOR" ]]; then
1899 GetOSVersion
1900 fi
1901
1902 # SLE11 and openSUSE 12.2 don't have the usual usermod
1903 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1904 sudo usermod -a -G "$group" "$user"
1905 else
1906 sudo usermod -A "$group" "$user"
1907 fi
1908}
1909
1910# Convert CIDR notation to a IPv4 netmask
1911# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001912function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001913 local maskpat="255 255 255 255"
1914 local maskdgt="254 252 248 240 224 192 128"
1915 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1916 echo ${1-0}.${2-0}.${3-0}.${4-0}
1917}
1918
1919# Gracefully cp only if source file/dir exists
1920# cp_it source destination
1921function cp_it {
1922 if [ -e $1 ] || [ -d $1 ]; then
1923 cp -pRL $1 $2
1924 fi
1925}
1926
1927# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1928# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1929# ``localrc`` or on the command line if necessary::
1930#
1931# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1932#
1933# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1934
Ian Wienandaee18c72014-02-21 15:35:08 +11001935function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001936 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001937 export http_proxy=$http_proxy
1938 fi
Sean Dague53753292014-12-04 19:38:15 -05001939 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001940 export https_proxy=$https_proxy
1941 fi
Sean Dague53753292014-12-04 19:38:15 -05001942 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001943 export no_proxy=$no_proxy
1944 fi
1945}
1946
1947# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001948function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001949 local mount_type=`stat -f -L -c %T $1`
1950 test "$mount_type" == "nfs"
1951}
1952
1953# Return the network portion of the given IP address using netmask
1954# netmask is in the traditional dotted-quad format
1955# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001956function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001957 local ip=$1
1958 local mask=$2
1959 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1960 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1961 echo $subnet
1962}
1963
1964# Service wrapper to restart services
1965# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001966function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001967 if is_ubuntu; then
1968 sudo /usr/sbin/service $1 restart
1969 else
1970 sudo /sbin/service $1 restart
1971 fi
1972}
1973
1974# Only change permissions of a file or directory if it is not on an
1975# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001976function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001977 _safe_permission_operation chmod $@
1978}
1979
1980# Only change ownership of a file or directory if it is not on an NFS
1981# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001982function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001983 _safe_permission_operation chown $@
1984}
1985
1986# Service wrapper to start services
1987# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001988function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001989 if is_ubuntu; then
1990 sudo /usr/sbin/service $1 start
1991 else
1992 sudo /sbin/service $1 start
1993 fi
1994}
1995
1996# Service wrapper to stop services
1997# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001998function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001999 if is_ubuntu; then
2000 sudo /usr/sbin/service $1 stop
2001 else
2002 sudo /sbin/service $1 stop
2003 fi
2004}
2005
2006
2007# Restore xtrace
2008$XTRACE
2009
2010# Local variables:
2011# mode: shell-script
2012# End: