blob: f8f8f63fee63fc384764c8d478f3d43e92dbf8d6 [file] [log] [blame]
Dean Troyerdff49a22014-01-30 15:37:40 -06001# functions-common - Common functions used by DevStack components
2#
3# The canonical copy of this file is maintained in the DevStack repo.
4# All modifications should be made there and then sync'ed to other repos
5# as required.
6#
7# This file is sorted alphabetically within the function groups.
8#
9# - Config Functions
10# - Control Functions
11# - Distro Functions
12# - Git Functions
13# - OpenStack Functions
14# - Package Functions
15# - Process Functions
16# - Python Functions
17# - Service Functions
Masayuki Igawaf6368d32014-02-20 13:31:26 +090018# - System Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060019#
20# The following variables are assumed to be defined by certain functions:
21#
22# - ``ENABLED_SERVICES``
23# - ``ERROR_ON_CLONE``
24# - ``FILES``
25# - ``OFFLINE``
26# - ``PIP_DOWNLOAD_CACHE``
27# - ``PIP_USE_MIRRORS``
28# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090029# - ``REQUIREMENTS_DIR``
30# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060031# - ``TRACK_DEPENDS``
Masayuki Igawad20f6322014-02-28 09:22:37 +090032# - ``UNDO_REQUIREMENTS``
Dean Troyerdff49a22014-01-30 15:37:40 -060033# - ``http_proxy``, ``https_proxy``, ``no_proxy``
34
35# Save trace setting
36XTRACE=$(set +o | grep xtrace)
37set +o xtrace
38
39
40# Config Functions
41# ================
42
43# Append a new option in an ini file without replacing the old value
44# iniadd config-file section option value1 value2 value3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +110045function iniadd {
Sean Dague45917cc2014-02-24 16:09:14 -050046 local xtrace=$(set +o | grep xtrace)
47 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060048 local file=$1
49 local section=$2
50 local option=$3
51 shift 3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050052
Dean Troyerdff49a22014-01-30 15:37:40 -060053 local values="$(iniget_multiline $file $section $option) $@"
54 iniset_multiline $file $section $option $values
Sean Dague45917cc2014-02-24 16:09:14 -050055 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060056}
57
58# Comment an option in an INI file
59# inicomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110060function inicomment {
Sean Dague45917cc2014-02-24 16:09:14 -050061 local xtrace=$(set +o | grep xtrace)
62 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060063 local file=$1
64 local section=$2
65 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050066
Dean Troyerdff49a22014-01-30 15:37:40 -060067 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -050068 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060069}
70
71# Get an option from an INI file
72# iniget config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110073function iniget {
Sean Dague45917cc2014-02-24 16:09:14 -050074 local xtrace=$(set +o | grep xtrace)
75 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060076 local file=$1
77 local section=$2
78 local option=$3
79 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050080
Dean Troyerdff49a22014-01-30 15:37:40 -060081 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
82 echo ${line#*=}
Sean Dague45917cc2014-02-24 16:09:14 -050083 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060084}
85
86# Get a multiple line option from an INI file
87# iniget_multiline config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110088function iniget_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -050089 local xtrace=$(set +o | grep xtrace)
90 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060091 local file=$1
92 local section=$2
93 local option=$3
94 local values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050095
Dean Troyerdff49a22014-01-30 15:37:40 -060096 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
97 echo ${values}
Sean Dague45917cc2014-02-24 16:09:14 -050098 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060099}
100
101# Determinate is the given option present in the INI file
102# ini_has_option config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100103function ini_has_option {
Sean Dague45917cc2014-02-24 16:09:14 -0500104 local xtrace=$(set +o | grep xtrace)
105 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600106 local file=$1
107 local section=$2
108 local option=$3
109 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500110
Dean Troyerdff49a22014-01-30 15:37:40 -0600111 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Sean Dague45917cc2014-02-24 16:09:14 -0500112 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600113 [ -n "$line" ]
114}
115
116# Set an option in an INI file
117# iniset config-file section option value
Ian Wienandaee18c72014-02-21 15:35:08 +1100118function iniset {
Sean Dague45917cc2014-02-24 16:09:14 -0500119 local xtrace=$(set +o | grep xtrace)
120 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600121 local file=$1
122 local section=$2
123 local option=$3
124 local value=$4
125
126 [[ -z $section || -z $option ]] && return
127
128 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
129 # Add section at the end
130 echo -e "\n[$section]" >>"$file"
131 fi
132 if ! ini_has_option "$file" "$section" "$option"; then
133 # Add it
134 sed -i -e "/^\[$section\]/ a\\
135$option = $value
136" "$file"
137 else
138 local sep=$(echo -ne "\x01")
139 # Replace it
140 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
141 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500142 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600143}
144
145# Set a multiple line option in an INI file
146# iniset_multiline config-file section option value1 value2 valu3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +1100147function iniset_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -0500148 local xtrace=$(set +o | grep xtrace)
149 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600150 local file=$1
151 local section=$2
152 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500153
Dean Troyerdff49a22014-01-30 15:37:40 -0600154 shift 3
155 local values
156 for v in $@; do
157 # The later sed command inserts each new value in the line next to
158 # the section identifier, which causes the values to be inserted in
159 # the reverse order. Do a reverse here to keep the original order.
160 values="$v ${values}"
161 done
162 if ! grep -q "^\[$section\]" "$file"; then
163 # Add section at the end
164 echo -e "\n[$section]" >>"$file"
165 else
166 # Remove old values
167 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
168 fi
169 # Add new ones
170 for v in $values; do
171 sed -i -e "/^\[$section\]/ a\\
172$option = $v
173" "$file"
174 done
Sean Dague45917cc2014-02-24 16:09:14 -0500175 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600176}
177
178# Uncomment an option in an INI file
179# iniuncomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100180function iniuncomment {
Sean Dague45917cc2014-02-24 16:09:14 -0500181 local xtrace=$(set +o | grep xtrace)
182 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600183 local file=$1
184 local section=$2
185 local option=$3
186 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500187 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600188}
189
190# Normalize config values to True or False
191# Accepts as False: 0 no No NO false False FALSE
192# Accepts as True: 1 yes Yes YES true True TRUE
193# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100194function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500195 local xtrace=$(set +o | grep xtrace)
196 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600197 local default=$1
198 local testval=$2
199
200 [[ -z "$testval" ]] && { echo "$default"; return; }
201 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
202 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
203 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500204 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600205}
206
207
208# Control Functions
209# =================
210
211# Prints backtrace info
212# filename:lineno:function
213# backtrace level
214function backtrace {
215 local level=$1
216 local deep=$((${#BASH_SOURCE[@]} - 1))
217 echo "[Call Trace]"
218 while [ $level -le $deep ]; do
219 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
220 deep=$((deep - 1))
221 done
222}
223
224# Prints line number and "message" then exits
225# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100226function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600227 local exitcode=$?
228 set +o xtrace
229 local line=$1; shift
230 if [ $exitcode == 0 ]; then
231 exitcode=1
232 fi
233 backtrace 2
234 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600235 # Give buffers a second to flush
236 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600237 exit $exitcode
238}
239
240# Checks an environment variable is not set or has length 0 OR if the
241# exit code is non-zero and prints "message" and exits
242# NOTE: env-var is the variable name without a '$'
243# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100244function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600245 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500246 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600247 set +o xtrace
248 local line=$1; shift
249 local evar=$1; shift
250 if ! is_set $evar || [ $exitcode != 0 ]; then
251 die $line "$*"
252 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500253 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600254}
255
256# Prints line number and "message" in error format
257# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100258function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600259 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500260 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600261 set +o xtrace
262 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
263 echo $msg 1>&2;
264 if [[ -n ${SCREEN_LOGDIR} ]]; then
265 echo $msg >> "${SCREEN_LOGDIR}/error.log"
266 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500267 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600268 return $exitcode
269}
270
271# Checks an environment variable is not set or has length 0 OR if the
272# exit code is non-zero and prints "message"
273# NOTE: env-var is the variable name without a '$'
274# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100275function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600276 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500277 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600278 set +o xtrace
279 local line=$1; shift
280 local evar=$1; shift
281 if ! is_set $evar || [ $exitcode != 0 ]; then
282 err $line "$*"
283 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500284 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600285 return $exitcode
286}
287
288# Exit after outputting a message about the distribution not being supported.
289# exit_distro_not_supported [optional-string-telling-what-is-missing]
290function exit_distro_not_supported {
291 if [[ -z "$DISTRO" ]]; then
292 GetDistro
293 fi
294
295 if [ $# -gt 0 ]; then
296 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
297 else
298 die $LINENO "Support for $DISTRO is incomplete."
299 fi
300}
301
302# Test if the named environment variable is set and not zero length
303# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100304function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600305 local var=\$"$1"
306 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
307}
308
309# Prints line number and "message" in warning format
310# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100311function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600312 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500313 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600314 set +o xtrace
315 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
316 echo $msg 1>&2;
317 if [[ -n ${SCREEN_LOGDIR} ]]; then
318 echo $msg >> "${SCREEN_LOGDIR}/error.log"
319 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500320 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600321 return $exitcode
322}
323
324
325# Distro Functions
326# ================
327
328# Determine OS Vendor, Release and Update
329# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
330# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500331# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
332# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
333# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
334# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
335# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
336declare os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
337
Dean Troyerdff49a22014-01-30 15:37:40 -0600338# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100339function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500340
Dean Troyerdff49a22014-01-30 15:37:40 -0600341 # Figure out which vendor we are
342 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
343 # OS/X
344 os_VENDOR=`sw_vers -productName`
345 os_RELEASE=`sw_vers -productVersion`
346 os_UPDATE=${os_RELEASE##*.}
347 os_RELEASE=${os_RELEASE%.*}
348 os_PACKAGE=""
349 if [[ "$os_RELEASE" =~ "10.7" ]]; then
350 os_CODENAME="lion"
351 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
352 os_CODENAME="snow leopard"
353 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
354 os_CODENAME="leopard"
355 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
356 os_CODENAME="tiger"
357 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
358 os_CODENAME="panther"
359 else
360 os_CODENAME=""
361 fi
362 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
363 os_VENDOR=$(lsb_release -i -s)
364 os_RELEASE=$(lsb_release -r -s)
365 os_UPDATE=""
366 os_PACKAGE="rpm"
367 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
368 os_PACKAGE="deb"
369 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
370 lsb_release -d -s | grep -q openSUSE
371 if [[ $? -eq 0 ]]; then
372 os_VENDOR="openSUSE"
373 fi
374 elif [[ $os_VENDOR == "openSUSE project" ]]; then
375 os_VENDOR="openSUSE"
376 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
377 os_VENDOR="Red Hat"
378 fi
379 os_CODENAME=$(lsb_release -c -s)
380 elif [[ -r /etc/redhat-release ]]; then
381 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
382 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
383 # CentOS release 5.5 (Final)
384 # CentOS Linux release 6.0 (Final)
385 # Fedora release 16 (Verne)
386 # XenServer release 6.2.0-70446c (xenenterprise)
387 os_CODENAME=""
388 for r in "Red Hat" CentOS Fedora XenServer; do
389 os_VENDOR=$r
390 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
391 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
392 os_CODENAME=${ver#*|}
393 os_RELEASE=${ver%|*}
394 os_UPDATE=${os_RELEASE##*.}
395 os_RELEASE=${os_RELEASE%.*}
396 break
397 fi
398 os_VENDOR=""
399 done
400 os_PACKAGE="rpm"
401 elif [[ -r /etc/SuSE-release ]]; then
402 for r in openSUSE "SUSE Linux"; do
403 if [[ "$r" = "SUSE Linux" ]]; then
404 os_VENDOR="SUSE LINUX"
405 else
406 os_VENDOR=$r
407 fi
408
409 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
410 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
411 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
412 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
413 break
414 fi
415 os_VENDOR=""
416 done
417 os_PACKAGE="rpm"
418 # If lsb_release is not installed, we should be able to detect Debian OS
419 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
420 os_VENDOR="Debian"
421 os_PACKAGE="deb"
422 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
423 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
424 fi
425 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
426}
427
428# Translate the OS version values into common nomenclature
429# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500430declare DISTRO
431
Ian Wienandaee18c72014-02-21 15:35:08 +1100432function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600433 GetOSVersion
434 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
435 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
436 DISTRO=$os_CODENAME
437 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
438 # For Fedora, just use 'f' and the release
439 DISTRO="f$os_RELEASE"
440 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
441 DISTRO="opensuse-$os_RELEASE"
442 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
443 # For SLE, also use the service pack
444 if [[ -z "$os_UPDATE" ]]; then
445 DISTRO="sle${os_RELEASE}"
446 else
447 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
448 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530449 elif [[ "$os_VENDOR" =~ (Red Hat) || \
450 "$os_VENDOR" =~ (CentOS) || \
451 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600452 # Drop the . release as we assume it's compatible
453 DISTRO="rhel${os_RELEASE::1}"
454 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
455 DISTRO="xs$os_RELEASE"
456 else
457 # Catch-all for now is Vendor + Release + Update
458 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
459 fi
460 export DISTRO
461}
462
463# Utility function for checking machine architecture
464# is_arch arch-type
465function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500466 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600467}
468
469# Determine if current distribution is a Fedora-based distribution
470# (Fedora, RHEL, CentOS, etc).
471# is_fedora
472function is_fedora {
473 if [[ -z "$os_VENDOR" ]]; then
474 GetOSVersion
475 fi
476
anju Tiwari6c639c92014-07-15 18:11:54 +0530477 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
478 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600479}
480
481
482# Determine if current distribution is a SUSE-based distribution
483# (openSUSE, SLE).
484# is_suse
485function is_suse {
486 if [[ -z "$os_VENDOR" ]]; then
487 GetOSVersion
488 fi
489
490 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
491}
492
493
494# Determine if current distribution is an Ubuntu-based distribution
495# It will also detect non-Ubuntu but Debian-based distros
496# is_ubuntu
497function is_ubuntu {
498 if [[ -z "$os_PACKAGE" ]]; then
499 GetOSVersion
500 fi
501 [ "$os_PACKAGE" = "deb" ]
502}
503
504
505# Git Functions
506# =============
507
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600508# Returns openstack release name for a given branch name
509# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100510function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600511 local branch=$1
512 if [[ $branch =~ "stable/" ]]; then
513 echo ${branch#*/}
514 else
515 echo "master"
516 fi
517}
518
Dean Troyerdff49a22014-01-30 15:37:40 -0600519# git clone only if directory doesn't exist already. Since ``DEST`` might not
520# be owned by the installation user, we create the directory and change the
521# ownership to the proper user.
522# Set global RECLONE=yes to simulate a clone when dest-dir exists
523# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
524# does not exist (default is False, meaning the repo will be cloned).
525# Uses global ``OFFLINE``
526# git_clone remote dest-dir branch
527function git_clone {
528 GIT_REMOTE=$1
529 GIT_DEST=$2
530 GIT_REF=$3
531 RECLONE=$(trueorfalse False $RECLONE)
Sean Dague64bd0162014-03-12 13:04:22 -0400532 local orig_dir=`pwd`
Dean Troyerdff49a22014-01-30 15:37:40 -0600533
534 if [[ "$OFFLINE" = "True" ]]; then
535 echo "Running in offline mode, clones already exist"
536 # print out the results so we know what change was used in the logs
537 cd $GIT_DEST
538 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400539 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600540 return
541 fi
542
543 if echo $GIT_REF | egrep -q "^refs"; then
544 # If our branch name is a gerrit style refs/changes/...
545 if [[ ! -d $GIT_DEST ]]; then
546 [[ "$ERROR_ON_CLONE" = "True" ]] && \
547 die $LINENO "Cloning not allowed in this configuration"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100548 git_timed clone $GIT_REMOTE $GIT_DEST
Dean Troyerdff49a22014-01-30 15:37:40 -0600549 fi
550 cd $GIT_DEST
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100551 git_timed fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600552 else
553 # do a full clone only if the directory doesn't exist
554 if [[ ! -d $GIT_DEST ]]; then
555 [[ "$ERROR_ON_CLONE" = "True" ]] && \
556 die $LINENO "Cloning not allowed in this configuration"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100557 git_timed clone $GIT_REMOTE $GIT_DEST
Dean Troyerdff49a22014-01-30 15:37:40 -0600558 cd $GIT_DEST
559 # This checkout syntax works for both branches and tags
560 git checkout $GIT_REF
561 elif [[ "$RECLONE" = "True" ]]; then
562 # if it does exist then simulate what clone does if asked to RECLONE
563 cd $GIT_DEST
564 # set the url to pull from and fetch
565 git remote set-url origin $GIT_REMOTE
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100566 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600567 # remove the existing ignored files (like pyc) as they cause breakage
568 # (due to the py files having older timestamps than our pyc, so python
569 # thinks the pyc files are correct using them)
570 find $GIT_DEST -name '*.pyc' -delete
571
572 # handle GIT_REF accordingly to type (tag, branch)
573 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
574 git_update_tag $GIT_REF
575 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
576 git_update_branch $GIT_REF
577 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
578 git_update_remote_branch $GIT_REF
579 else
580 die $LINENO "$GIT_REF is neither branch nor tag"
581 fi
582
583 fi
584 fi
585
586 # print out the results so we know what change was used in the logs
587 cd $GIT_DEST
588 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400589 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600590}
591
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100592# git can sometimes get itself infinitely stuck with transient network
593# errors or other issues with the remote end. This wraps git in a
594# timeout/retry loop and is intended to watch over non-local git
595# processes that might hang. GIT_TIMEOUT, if set, is passed directly
596# to timeout(1); otherwise the default value of 0 maintains the status
597# quo of waiting forever.
598# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100599function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100600 local count=0
601 local timeout=0
602
603 if [[ -n "${GIT_TIMEOUT}" ]]; then
604 timeout=${GIT_TIMEOUT}
605 fi
606
607 until timeout -s SIGINT ${timeout} git "$@"; do
608 # 124 is timeout(1)'s special return code when it reached the
609 # timeout; otherwise assume fatal failure
610 if [[ $? -ne 124 ]]; then
611 die $LINENO "git call failed: [git $@]"
612 fi
613
614 count=$(($count + 1))
615 warn "timeout ${count} for git call: [git $@]"
616 if [ $count -eq 3 ]; then
617 die $LINENO "Maximum of 3 git retries reached"
618 fi
619 sleep 5
620 done
621}
622
Dean Troyerdff49a22014-01-30 15:37:40 -0600623# git update using reference as a branch.
624# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100625function git_update_branch {
Dean Troyerdff49a22014-01-30 15:37:40 -0600626
627 GIT_BRANCH=$1
628
629 git checkout -f origin/$GIT_BRANCH
630 # a local branch might not exist
631 git branch -D $GIT_BRANCH || true
632 git checkout -b $GIT_BRANCH
633}
634
635# git update using reference as a branch.
636# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100637function git_update_remote_branch {
Dean Troyerdff49a22014-01-30 15:37:40 -0600638
639 GIT_BRANCH=$1
640
641 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
642}
643
644# git update using reference as a tag. Be careful editing source at that repo
645# as working copy will be in a detached mode
646# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100647function git_update_tag {
Dean Troyerdff49a22014-01-30 15:37:40 -0600648
649 GIT_TAG=$1
650
651 git tag -d $GIT_TAG
652 # fetching given tag only
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100653 git_timed fetch origin tag $GIT_TAG
Dean Troyerdff49a22014-01-30 15:37:40 -0600654 git checkout -f $GIT_TAG
655}
656
657
658# OpenStack Functions
659# ===================
660
661# Get the default value for HOST_IP
662# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100663function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600664 local fixed_range=$1
665 local floating_range=$2
666 local host_ip_iface=$3
667 local host_ip=$4
668
669 # Find the interface used for the default route
670 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
671 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
672 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
673 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500674 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
675 local ip
676 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600677 # Attempt to filter out IP addresses that are part of the fixed and
678 # floating range. Note that this method only works if the ``netaddr``
679 # python library is installed. If it is not installed, an error
680 # will be printed and the first IP from the interface will be used.
681 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
682 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500683 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
684 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600685 break;
686 fi
687 done
688 fi
689 echo $host_ip
690}
691
692# Grab a numbered field from python prettytable output
693# Fields are numbered starting with 1
694# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
695# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100696function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500697 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600698 while read data; do
699 if [ "$1" -lt 0 ]; then
700 field="(\$(NF$1))"
701 else
702 field="\$$(($1 + 1))"
703 fi
704 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
705 done
706}
707
708# Add a policy to a policy.json file
709# Do nothing if the policy already exists
710# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100711function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600712 local policy_file=$1
713 local policy_name=$2
714 local policy_perm=$3
715
716 if grep -q ${policy_name} ${policy_file}; then
717 echo "Policy ${policy_name} already exists in ${policy_file}"
718 return
719 fi
720
721 # Add a terminating comma to policy lines without one
722 # Remove the closing '}' and all lines following to the end-of-file
723 local tmpfile=$(mktemp)
724 uniq ${policy_file} | sed -e '
725 s/]$/],/
726 /^[}]/,$d
727 ' > ${tmpfile}
728
729 # Append policy and closing brace
730 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
731 echo "}" >>${tmpfile}
732
733 mv ${tmpfile} ${policy_file}
734}
735
Bartosz Górski0abde392014-02-28 14:15:19 +0100736# Gets or creates user
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200737# Usage: get_or_create_user <username> <password> <project> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100738function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200739 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500740 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200741 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500742 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200743 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100744 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500745 local user_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100746 # Gets user id
747 openstack user show $1 -f value -c id 2>/dev/null ||
748 # Creates new user
749 openstack user create \
750 $1 \
751 --password "$2" \
752 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500753 $email \
Bartosz Górski0abde392014-02-28 14:15:19 +0100754 -f value -c id
755 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500756 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100757}
758
759# Gets or creates project
760# Usage: get_or_create_project <name>
761function get_or_create_project {
762 # Gets project id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500763 local project_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100764 # Gets project id
765 openstack project show $1 -f value -c id 2>/dev/null ||
766 # Creates new project if not exists
767 openstack project create $1 -f value -c id
768 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500769 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100770}
771
772# Gets or creates role
773# Usage: get_or_create_role <name>
774function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500775 local role_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100776 # Gets role id
777 openstack role show $1 -f value -c id 2>/dev/null ||
778 # Creates role if not exists
779 openstack role create $1 -f value -c id
780 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500781 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100782}
783
784# Gets or adds user role
785# Usage: get_or_add_user_role <role> <user> <project>
786function get_or_add_user_role {
787 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500788 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100789 $2 \
790 --project $3 \
791 --column "ID" \
792 --column "Name" \
793 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500794 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100795 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500796 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100797 $1 \
798 --user $2 \
799 --project $3 \
800 | grep " id " | get_field 2)
801 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500802 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100803}
804
805# Gets or creates service
806# Usage: get_or_create_service <name> <type> <description>
807function get_or_create_service {
808 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500809 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100810 # Gets service id
811 openstack service show $1 -f value -c id 2>/dev/null ||
812 # Creates new service if not exists
813 openstack service create \
814 $1 \
815 --type=$2 \
816 --description="$3" \
817 -f value -c id
818 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500819 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100820}
821
822# Gets or creates endpoint
823# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
824function get_or_create_endpoint {
825 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500826 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100827 --column "ID" \
828 --column "Region" \
829 --column "Service Name" \
830 | grep " $2 " \
831 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500832 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100833 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500834 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100835 $1 \
836 --region $2 \
837 --publicurl $3 \
838 --adminurl $4 \
839 --internalurl $5 \
840 | grep " id " | get_field 2)
841 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500842 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100843}
Dean Troyerdff49a22014-01-30 15:37:40 -0600844
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500845
Dean Troyerdff49a22014-01-30 15:37:40 -0600846# Package Functions
847# =================
848
849# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100850function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600851 local pkg_dir
852 if is_ubuntu; then
853 pkg_dir=$FILES/apts
854 elif is_fedora; then
855 pkg_dir=$FILES/rpms
856 elif is_suse; then
857 pkg_dir=$FILES/rpms-suse
858 else
859 exit_distro_not_supported "list of packages"
860 fi
861 echo "$pkg_dir"
862}
863
864# Wrapper for ``apt-get`` to set cache and proxy environment variables
865# Uses globals ``OFFLINE``, ``*_proxy``
866# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100867function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500868 local xtrace=$(set +o | grep xtrace)
869 set +o xtrace
870
Dean Troyerdff49a22014-01-30 15:37:40 -0600871 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
872 local sudo="sudo"
873 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500874
875 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600876 $sudo DEBIAN_FRONTEND=noninteractive \
877 http_proxy=$http_proxy https_proxy=$https_proxy \
878 no_proxy=$no_proxy \
879 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
880}
881
882# get_packages() collects a list of package names of any type from the
883# prerequisite files in ``files/{apts|rpms}``. The list is intended
884# to be passed to a package installer such as apt or yum.
885#
886# Only packages required for the services in 1st argument will be
887# included. Two bits of metadata are recognized in the prerequisite files:
888#
889# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
890# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
891# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100892function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500893 local xtrace=$(set +o | grep xtrace)
894 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600895 local services=$@
896 local package_dir=$(_get_package_dir)
897 local file_to_parse
898 local service
899
900 if [[ -z "$package_dir" ]]; then
901 echo "No package directory supplied"
902 return 1
903 fi
904 if [[ -z "$DISTRO" ]]; then
905 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -0500906 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -0600907 fi
908 for service in ${services//,/ }; do
909 # Allow individual services to specify dependencies
910 if [[ -e ${package_dir}/${service} ]]; then
911 file_to_parse="${file_to_parse} $service"
912 fi
913 # NOTE(sdague) n-api needs glance for now because that's where
914 # glance client is
915 if [[ $service == n-api ]]; then
916 if [[ ! $file_to_parse =~ nova ]]; then
917 file_to_parse="${file_to_parse} nova"
918 fi
919 if [[ ! $file_to_parse =~ glance ]]; then
920 file_to_parse="${file_to_parse} glance"
921 fi
922 elif [[ $service == c-* ]]; then
923 if [[ ! $file_to_parse =~ cinder ]]; then
924 file_to_parse="${file_to_parse} cinder"
925 fi
926 elif [[ $service == ceilometer-* ]]; then
927 if [[ ! $file_to_parse =~ ceilometer ]]; then
928 file_to_parse="${file_to_parse} ceilometer"
929 fi
930 elif [[ $service == s-* ]]; then
931 if [[ ! $file_to_parse =~ swift ]]; then
932 file_to_parse="${file_to_parse} swift"
933 fi
934 elif [[ $service == n-* ]]; then
935 if [[ ! $file_to_parse =~ nova ]]; then
936 file_to_parse="${file_to_parse} nova"
937 fi
938 elif [[ $service == g-* ]]; then
939 if [[ ! $file_to_parse =~ glance ]]; then
940 file_to_parse="${file_to_parse} glance"
941 fi
942 elif [[ $service == key* ]]; then
943 if [[ ! $file_to_parse =~ keystone ]]; then
944 file_to_parse="${file_to_parse} keystone"
945 fi
946 elif [[ $service == q-* ]]; then
947 if [[ ! $file_to_parse =~ neutron ]]; then
948 file_to_parse="${file_to_parse} neutron"
949 fi
Adam Gandelman539ec432014-03-18 18:57:43 -0700950 elif [[ $service == ir-* ]]; then
951 if [[ ! $file_to_parse =~ ironic ]]; then
952 file_to_parse="${file_to_parse} ironic"
953 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600954 fi
955 done
956
957 for file in ${file_to_parse}; do
958 local fname=${package_dir}/${file}
959 local OIFS line package distros distro
960 [[ -e $fname ]] || continue
961
962 OIFS=$IFS
963 IFS=$'\n'
964 for line in $(<${fname}); do
965 if [[ $line =~ "NOPRIME" ]]; then
966 continue
967 fi
968
969 # Assume we want this package
970 package=${line%#*}
971 inst_pkg=1
972
973 # Look for # dist:xxx in comment
974 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
975 # We are using BASH regexp matching feature.
976 package=${BASH_REMATCH[1]}
977 distros=${BASH_REMATCH[2]}
978 # In bash ${VAR,,} will lowecase VAR
979 # Look for a match in the distro list
980 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
981 # If no match then skip this package
982 inst_pkg=0
983 fi
984 fi
985
986 # Look for # testonly in comment
987 if [[ $line =~ (.*)#.*testonly.* ]]; then
988 package=${BASH_REMATCH[1]}
989 # Are we installing test packages? (test for the default value)
990 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
991 # If not installing test packages the skip this package
992 inst_pkg=0
993 fi
994 fi
995
996 if [[ $inst_pkg = 1 ]]; then
997 echo $package
998 fi
999 done
1000 IFS=$OIFS
1001 done
Sean Dague45917cc2014-02-24 16:09:14 -05001002 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001003}
1004
1005# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001006# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001007# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001008function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001009 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001010 return 0
1011 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001012
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001013 if is_ubuntu; then
1014 local xtrace=$(set +o | grep xtrace)
1015 set +o xtrace
1016 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1017 # if there are transient errors pulling the updates, that's fine.
1018 # It may be secondary repositories that we don't really care about.
1019 apt_get update || /bin/true
1020 REPOS_UPDATED=True
1021 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001022 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001023 fi
1024}
1025
1026function real_install_package {
1027 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001028 apt_get install "$@"
1029 elif is_fedora; then
1030 yum_install "$@"
1031 elif is_suse; then
1032 zypper_install "$@"
1033 else
1034 exit_distro_not_supported "installing packages"
1035 fi
1036}
1037
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001038# Distro-agnostic package installer
1039# install_package package [package ...]
1040function install_package {
1041 update_package_repo
1042 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1043}
1044
Dean Troyerdff49a22014-01-30 15:37:40 -06001045# Distro-agnostic function to tell if a package is installed
1046# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001047function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001048 if [[ -z "$@" ]]; then
1049 return 1
1050 fi
1051
1052 if [[ -z "$os_PACKAGE" ]]; then
1053 GetOSVersion
1054 fi
1055
1056 if [[ "$os_PACKAGE" = "deb" ]]; then
1057 dpkg -s "$@" > /dev/null 2> /dev/null
1058 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1059 rpm --quiet -q "$@"
1060 else
1061 exit_distro_not_supported "finding if a package is installed"
1062 fi
1063}
1064
1065# Distro-agnostic package uninstaller
1066# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001067function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001068 if is_ubuntu; then
1069 apt_get purge "$@"
1070 elif is_fedora; then
1071 sudo yum remove -y "$@"
1072 elif is_suse; then
1073 sudo zypper rm "$@"
1074 else
1075 exit_distro_not_supported "uninstalling packages"
1076 fi
1077}
1078
1079# Wrapper for ``yum`` to set proxy environment variables
1080# Uses globals ``OFFLINE``, ``*_proxy``
1081# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001082function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001083 [[ "$OFFLINE" = "True" ]] && return
1084 local sudo="sudo"
1085 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001086
1087 # The manual check for missing packages is because yum -y assumes
1088 # missing packages are OK. See
1089 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001090 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1091 no_proxy=$no_proxy \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001092 yum install -y "$@" 2>&1 | \
1093 awk '
1094 BEGIN { fail=0 }
1095 /No package/ { fail=1 }
1096 { print }
1097 END { exit fail }' || \
1098 die $LINENO "Missing packages detected"
1099
1100 # also ensure we catch a yum failure
1101 if [[ ${PIPESTATUS[0]} != 0 ]]; then
1102 die $LINENO "Yum install failure"
1103 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001104}
1105
1106# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001107# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001108# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001109function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001110 [[ "$OFFLINE" = "True" ]] && return
1111 local sudo="sudo"
1112 [[ "$(id -u)" = "0" ]] && sudo="env"
1113 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1114 zypper --non-interactive install --auto-agree-with-licenses "$@"
1115}
1116
1117
1118# Process Functions
1119# =================
1120
1121# _run_process() is designed to be backgrounded by run_process() to simulate a
1122# fork. It includes the dirty work of closing extra filehandles and preparing log
1123# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001124# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1125# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001126# _run_process service "command-line"
Ian Wienandaee18c72014-02-21 15:35:08 +11001127function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001128 local service=$1
1129 local command="$2"
1130
1131 # Undo logging redirections and close the extra descriptors
1132 exec 1>&3
1133 exec 2>&3
1134 exec 3>&-
1135 exec 6>&-
1136
1137 if [[ -n ${SCREEN_LOGDIR} ]]; then
1138 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1139 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1140
1141 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1142 export PYTHONUNBUFFERED=1
1143 fi
1144
1145 exec /bin/bash -c "$command"
1146 die "$service exec failure: $command"
1147}
1148
1149# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1150# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001151# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001152# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001153function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001154 SCREEN_NAME=${SCREEN_NAME:-stack}
1155 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1156
1157 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1158 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1159 fi
1160
1161 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1162}
1163
1164# Find out if a process exists by partial name.
1165# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001166function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001167 local name=$1
1168 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001169 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001170 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001171 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001172}
1173
1174# run_process() launches a child process that closes all file descriptors and
1175# then exec's the passed in command. This is meant to duplicate the semantics
1176# of screen_it() without screen. PIDs are written to
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001177# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid``
Dean Troyerdff49a22014-01-30 15:37:40 -06001178# run_process service "command-line"
Ian Wienandaee18c72014-02-21 15:35:08 +11001179function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001180 local service=$1
1181 local command="$2"
1182
1183 # Spawn the child process
1184 _run_process "$service" "$command" &
1185 echo $!
1186}
1187
1188# Helper to launch a service in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001189# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1190# ``SERVICE_DIR``, ``USE_SCREEN``
Dean Troyerdff49a22014-01-30 15:37:40 -06001191# screen_it service "command-line"
1192function screen_it {
Sean Dagueea22a4f2014-06-27 15:21:41 -04001193 SCREEN_NAME=${SCREEN_NAME:-stack}
1194 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1195 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001196
1197 if is_service_enabled $1; then
1198 # Append the service to the screen rc file
Sean Dagueea22a4f2014-06-27 15:21:41 -04001199 screen_rc "$1" "$2"
Dean Troyerdff49a22014-01-30 15:37:40 -06001200
Sean Dagueea22a4f2014-06-27 15:21:41 -04001201 if [[ "$USE_SCREEN" = "True" ]]; then
1202 screen -S $SCREEN_NAME -X screen -t $1
Dean Troyerdff49a22014-01-30 15:37:40 -06001203
Sean Dagueea22a4f2014-06-27 15:21:41 -04001204 if [[ -n ${SCREEN_LOGDIR} ]]; then
1205 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
1206 screen -S $SCREEN_NAME -p $1 -X log on
1207 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1208 fi
1209
1210 # sleep to allow bash to be ready to be send the command - we are
1211 # creating a new window in screen and then sends characters, so if
1212 # bash isn't running by the time we send the command, nothing happens
1213 sleep 3
1214
1215 NL=`echo -ne '\015'`
1216 # This fun command does the following:
1217 # - the passed server command is backgrounded
1218 # - the pid of the background process is saved in the usual place
1219 # - the server process is brought back to the foreground
1220 # - if the server process exits prematurely the fg command errors
1221 # and a message is written to stdout and the service failure file
1222 # The pid saved can be used in screen_stop() as a process group
1223 # id to kill off all child processes
1224 screen -S $SCREEN_NAME -p $1 -X stuff "$2 & echo \$! >$SERVICE_DIR/$SCREEN_NAME/$1.pid; fg || echo \"$1 failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
Dean Troyerdff49a22014-01-30 15:37:40 -06001225 else
1226 # Spawn directly without screen
Sean Dagueea22a4f2014-06-27 15:21:41 -04001227 run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001228 fi
1229 fi
1230}
1231
1232# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001233# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001234# screen_rc service "command-line"
1235function screen_rc {
1236 SCREEN_NAME=${SCREEN_NAME:-stack}
1237 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1238 if [[ ! -e $SCREENRC ]]; then
1239 # Name the screen session
1240 echo "sessionname $SCREEN_NAME" > $SCREENRC
1241 # Set a reasonable statusbar
1242 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1243 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1244 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1245 echo "screen -t shell bash" >> $SCREENRC
1246 fi
1247 # If this service doesn't already exist in the screenrc file
1248 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1249 NL=`echo -ne '\015'`
1250 echo "screen -t $1 bash" >> $SCREENRC
1251 echo "stuff \"$2$NL\"" >> $SCREENRC
1252
1253 if [[ -n ${SCREEN_LOGDIR} ]]; then
1254 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1255 echo "log on" >>$SCREENRC
1256 fi
1257 fi
1258}
1259
1260# Stop a service in screen
1261# If a PID is available use it, kill the whole process group via TERM
1262# If screen is being used kill the screen window; this will catch processes
1263# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001264# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Dean Troyerdff49a22014-01-30 15:37:40 -06001265# screen_stop service
Ian Wienandaee18c72014-02-21 15:35:08 +11001266function screen_stop {
Dean Troyerdff49a22014-01-30 15:37:40 -06001267 SCREEN_NAME=${SCREEN_NAME:-stack}
1268 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1269 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1270
1271 if is_service_enabled $1; then
1272 # Kill via pid if we have one available
1273 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$1.pid ]]; then
1274 pkill -TERM -P -$(cat $SERVICE_DIR/$SCREEN_NAME/$1.pid)
1275 rm $SERVICE_DIR/$SCREEN_NAME/$1.pid
1276 fi
1277 if [[ "$USE_SCREEN" = "True" ]]; then
1278 # Clean up the screen window
1279 screen -S $SCREEN_NAME -p $1 -X kill
1280 fi
1281 fi
1282}
1283
1284# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001285# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001286# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001287function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001288 local service
1289 local failures
1290 SCREEN_NAME=${SCREEN_NAME:-stack}
1291 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1292
1293
1294 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1295 echo "No service status directory found"
1296 return
1297 fi
1298
1299 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001300 # make this -o errexit safe
1301 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001302
1303 for service in $failures; do
1304 service=`basename $service`
1305 service=${service%.failure}
1306 echo "Error: Service $service is not running"
1307 done
1308
1309 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001310 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001311 fi
1312}
1313
1314
1315# Python Functions
1316# ================
1317
1318# Get the path to the pip command.
1319# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001320function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001321 which pip || which pip-python
1322
1323 if [ $? -ne 0 ]; then
1324 die $LINENO "Unable to find pip; cannot continue"
1325 fi
1326}
1327
1328# Get the path to the direcotry where python executables are installed.
1329# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001330function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001331 if is_fedora || is_suse; then
1332 echo "/usr/bin"
1333 else
1334 echo "/usr/local/bin"
1335 fi
1336}
1337
1338# Wrapper for ``pip install`` to set cache and proxy environment variables
1339# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
1340# ``TRACK_DEPENDS``, ``*_proxy``
1341# pip_install package [package ...]
1342function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001343 local xtrace=$(set +o | grep xtrace)
1344 set +o xtrace
1345 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1346 $xtrace
1347 return
1348 fi
1349
Dean Troyerdff49a22014-01-30 15:37:40 -06001350 if [[ -z "$os_PACKAGE" ]]; then
1351 GetOSVersion
1352 fi
1353 if [[ $TRACK_DEPENDS = True ]]; then
1354 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001355 local cmd_pip=$DEST/.venv/bin/pip
1356 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001357 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001358 local cmd_pip=$(get_pip_command)
1359 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001360 fi
1361
1362 # Mirror option not needed anymore because pypi has CDN available,
1363 # but it's useful in certain circumstances
1364 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001365 local pip_mirror_opt=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001366 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001367 pip_mirror_opt="--use-mirrors"
Dean Troyerdff49a22014-01-30 15:37:40 -06001368 fi
1369
1370 # pip < 1.4 has a bug where it will use an already existing build
1371 # directory unconditionally. Say an earlier component installs
1372 # foo v1.1; pip will have built foo's source in
1373 # /tmp/$USER-pip-build. Even if a later component specifies foo <
1374 # 1.1, the existing extracted build will be used and cause
1375 # confusing errors. By creating unique build directories we avoid
1376 # this problem. See https://github.com/pypa/pip/issues/709
1377 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1378
Sean Dague45917cc2014-02-24 16:09:14 -05001379 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001380 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001381 http_proxy=$http_proxy \
1382 https_proxy=$https_proxy \
1383 no_proxy=$no_proxy \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001384 $cmd_pip install --build=${pip_build_tmp} \
1385 $pip_mirror_opt $@ \
1386 && $sudo_pip rm -rf ${pip_build_tmp}
Dean Troyerdff49a22014-01-30 15:37:40 -06001387}
1388
Sean Dague099e5e32014-03-31 10:35:43 -04001389# this should be used if you want to install globally, all libraries should
1390# use this, especially *oslo* ones
1391function setup_install {
1392 local project_dir=$1
1393 setup_package_with_req_sync $project_dir
1394}
1395
1396# this should be used for projects which run services, like all services
1397function setup_develop {
1398 local project_dir=$1
1399 setup_package_with_req_sync $project_dir -e
1400}
1401
Dean Troyeraf616d92014-02-17 12:57:55 -06001402# ``pip install -e`` the package, which processes the dependencies
1403# using pip before running `setup.py develop`
1404#
1405# Updates the dependencies in project_dir from the
1406# openstack/requirements global list before installing anything.
1407#
1408# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1409# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001410function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001411 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001412 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001413
Dean Troyeraf616d92014-02-17 12:57:55 -06001414 # Don't update repo if local changes exist
1415 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001416 # ``errexit`` requires us to trap the exit code when the repo is changed
1417 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001418
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001419 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001420 (cd $REQUIREMENTS_DIR; \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001421 python update.py $project_dir)
Dean Troyeraf616d92014-02-17 12:57:55 -06001422 fi
1423
Sean Dague099e5e32014-03-31 10:35:43 -04001424 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001425
1426 # We've just gone and possibly modified the user's source tree in an
1427 # automated way, which is considered bad form if it's a development
1428 # tree because we've screwed up their next git checkin. So undo it.
1429 #
1430 # However... there are some circumstances, like running in the gate
1431 # where we really really want the overridden version to stick. So provide
1432 # a variable that tells us whether or not we should UNDO the requirements
1433 # changes (this will be set to False in the OpenStack ci gate)
1434 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001435 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001436 (cd $project_dir && git reset --hard)
1437 fi
1438 fi
1439}
1440
1441# ``pip install -e`` the package, which processes the dependencies
1442# using pip before running `setup.py develop`
1443# Uses globals ``STACK_USER``
1444# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001445function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001446 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001447 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001448
Sean Dague099e5e32014-03-31 10:35:43 -04001449 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001450 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001451 if [[ "$flags" == "-e" ]]; then
1452 safe_chown -R $STACK_USER $1/*.egg-info
1453 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001454}
1455
Dean Troyerdff49a22014-01-30 15:37:40 -06001456
1457# Service Functions
1458# =================
1459
1460# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1461# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001462function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001463 echo "$1" | sed -e '
1464 s/,,/,/g;
1465 s/^,//;
1466 s/,$//
1467 '
1468}
1469
1470# disable_all_services() removes all current services
1471# from ``ENABLED_SERVICES`` to reset the configuration
1472# before a minimal installation
1473# Uses global ``ENABLED_SERVICES``
1474# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001475function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001476 ENABLED_SERVICES=""
1477}
1478
1479# Remove all services starting with '-'. For example, to install all default
1480# services except rabbit (rabbit) set in ``localrc``:
1481# ENABLED_SERVICES+=",-rabbit"
1482# Uses global ``ENABLED_SERVICES``
1483# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001484function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001485 local tmpsvcs="${ENABLED_SERVICES}"
1486 local service
1487 for service in ${tmpsvcs//,/ }; do
1488 if [[ ${service} == -* ]]; then
1489 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1490 fi
1491 done
1492 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1493}
1494
1495# disable_service() removes the services passed as argument to the
1496# ``ENABLED_SERVICES`` list, if they are present.
1497#
1498# For example:
1499# disable_service rabbit
1500#
1501# This function does not know about the special cases
1502# for nova, glance, and neutron built into is_service_enabled().
1503# Uses global ``ENABLED_SERVICES``
1504# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001505function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001506 local tmpsvcs=",${ENABLED_SERVICES},"
1507 local service
1508 for service in $@; do
1509 if is_service_enabled $service; then
1510 tmpsvcs=${tmpsvcs//,$service,/,}
1511 fi
1512 done
1513 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1514}
1515
1516# enable_service() adds the services passed as argument to the
1517# ``ENABLED_SERVICES`` list, if they are not already present.
1518#
1519# For example:
1520# enable_service qpid
1521#
1522# This function does not know about the special cases
1523# for nova, glance, and neutron built into is_service_enabled().
1524# Uses global ``ENABLED_SERVICES``
1525# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001526function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001527 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001528 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001529 for service in $@; do
1530 if ! is_service_enabled $service; then
1531 tmpsvcs+=",$service"
1532 fi
1533 done
1534 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1535 disable_negated_services
1536}
1537
1538# is_service_enabled() checks if the service(s) specified as arguments are
1539# enabled by the user in ``ENABLED_SERVICES``.
1540#
1541# Multiple services specified as arguments are ``OR``'ed together; the test
1542# is a short-circuit boolean, i.e it returns on the first match.
1543#
1544# There are special cases for some 'catch-all' services::
1545# **nova** returns true if any service enabled start with **n-**
1546# **cinder** returns true if any service enabled start with **c-**
1547# **ceilometer** returns true if any service enabled start with **ceilometer**
1548# **glance** returns true if any service enabled start with **g-**
1549# **neutron** returns true if any service enabled start with **q-**
1550# **swift** returns true if any service enabled start with **s-**
1551# **trove** returns true if any service enabled start with **tr-**
1552# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1553# **s-** services will be enabled. This will be deprecated in the future.
1554#
1555# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1556# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1557# as enabled in this case.
1558#
1559# Uses global ``ENABLED_SERVICES``
1560# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001561function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001562 local xtrace=$(set +o | grep xtrace)
1563 set +o xtrace
1564 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001565 local services=$@
1566 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001567 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001568 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001569
1570 # Look for top-level 'enabled' function for this service
1571 if type is_${service}_enabled >/dev/null 2>&1; then
1572 # A function exists for this service, use it
1573 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001574 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001575 fi
1576
1577 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1578 # are implemented
1579
Sean Dague45917cc2014-02-24 16:09:14 -05001580 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
1581 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1582 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1583 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1584 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1585 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1586 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1587 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1588 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1589 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001590 done
Sean Dague45917cc2014-02-24 16:09:14 -05001591 $xtrace
1592 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001593}
1594
1595# Toggle enable/disable_service for services that must run exclusive of each other
1596# $1 The name of a variable containing a space-separated list of services
1597# $2 The name of a variable in which to store the enabled service's name
1598# $3 The name of the service to enable
1599function use_exclusive_service {
1600 local options=${!1}
1601 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001602 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001603 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001604 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001605 for opt in $options;do
1606 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1607 done
1608 eval "$out=$selection"
1609 return 0
1610}
1611
1612
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001613# System Functions
1614# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001615
1616# Only run the command if the target file (the last arg) is not on an
1617# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001618function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001619 local xtrace=$(set +o | grep xtrace)
1620 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001621 local args=( $@ )
1622 local last
1623 local sudo_cmd
1624 local dir_to_check
1625
1626 let last="${#args[*]} - 1"
1627
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001628 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001629 if [ ! -d "$dir_to_check" ]; then
1630 dir_to_check=`dirname "$dir_to_check"`
1631 fi
1632
1633 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001634 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001635 return 0
1636 fi
1637
1638 if [[ $TRACK_DEPENDS = True ]]; then
1639 sudo_cmd="env"
1640 else
1641 sudo_cmd="sudo"
1642 fi
1643
Sean Dague45917cc2014-02-24 16:09:14 -05001644 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001645 $sudo_cmd $@
1646}
1647
1648# Exit 0 if address is in network or 1 if address is not in network
1649# ip-range is in CIDR notation: 1.2.3.4/20
1650# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001651function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001652 local ip=$1
1653 local range=$2
1654 local masklen=${range#*/}
1655 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1656 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1657 [[ $network == $subnet ]]
1658}
1659
1660# Add a user to a group.
1661# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001662function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001663 local user=$1
1664 local group=$2
1665
1666 if [[ -z "$os_VENDOR" ]]; then
1667 GetOSVersion
1668 fi
1669
1670 # SLE11 and openSUSE 12.2 don't have the usual usermod
1671 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1672 sudo usermod -a -G "$group" "$user"
1673 else
1674 sudo usermod -A "$group" "$user"
1675 fi
1676}
1677
1678# Convert CIDR notation to a IPv4 netmask
1679# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001680function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001681 local maskpat="255 255 255 255"
1682 local maskdgt="254 252 248 240 224 192 128"
1683 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1684 echo ${1-0}.${2-0}.${3-0}.${4-0}
1685}
1686
1687# Gracefully cp only if source file/dir exists
1688# cp_it source destination
1689function cp_it {
1690 if [ -e $1 ] || [ -d $1 ]; then
1691 cp -pRL $1 $2
1692 fi
1693}
1694
1695# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1696# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1697# ``localrc`` or on the command line if necessary::
1698#
1699# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1700#
1701# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1702
Ian Wienandaee18c72014-02-21 15:35:08 +11001703function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001704 if [[ -n "$http_proxy" ]]; then
1705 export http_proxy=$http_proxy
1706 fi
1707 if [[ -n "$https_proxy" ]]; then
1708 export https_proxy=$https_proxy
1709 fi
1710 if [[ -n "$no_proxy" ]]; then
1711 export no_proxy=$no_proxy
1712 fi
1713}
1714
1715# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001716function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001717 local mount_type=`stat -f -L -c %T $1`
1718 test "$mount_type" == "nfs"
1719}
1720
1721# Return the network portion of the given IP address using netmask
1722# netmask is in the traditional dotted-quad format
1723# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001724function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001725 local ip=$1
1726 local mask=$2
1727 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1728 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1729 echo $subnet
1730}
1731
1732# Service wrapper to restart services
1733# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001734function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001735 if is_ubuntu; then
1736 sudo /usr/sbin/service $1 restart
1737 else
1738 sudo /sbin/service $1 restart
1739 fi
1740}
1741
1742# Only change permissions of a file or directory if it is not on an
1743# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001744function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001745 _safe_permission_operation chmod $@
1746}
1747
1748# Only change ownership of a file or directory if it is not on an NFS
1749# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001750function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001751 _safe_permission_operation chown $@
1752}
1753
1754# Service wrapper to start services
1755# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001756function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001757 if is_ubuntu; then
1758 sudo /usr/sbin/service $1 start
1759 else
1760 sudo /sbin/service $1 start
1761 fi
1762}
1763
1764# Service wrapper to stop services
1765# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001766function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001767 if is_ubuntu; then
1768 sudo /usr/sbin/service $1 stop
1769 else
1770 sudo /sbin/service $1 stop
1771 fi
1772}
1773
1774
1775# Restore xtrace
1776$XTRACE
1777
1778# Local variables:
1779# mode: shell-script
1780# End: