blob: 3d66a0b8f5d675db429f8d5db8777af18f862711 [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
Ian Wienandbdc90c52014-08-04 15:44:58 +1000469# Quick check for a rackspace host; n.b. rackspace provided images
470# have these Xen tools installed but a custom image may not.
471function is_rackspace {
472 [ -f /usr/bin/xenstore-ls ] && \
473 sudo /usr/bin/xenstore-ls vm-data | grep -q "Rackspace"
474}
475
Dean Troyerdff49a22014-01-30 15:37:40 -0600476# Determine if current distribution is a Fedora-based distribution
477# (Fedora, RHEL, CentOS, etc).
478# is_fedora
479function is_fedora {
480 if [[ -z "$os_VENDOR" ]]; then
481 GetOSVersion
482 fi
483
anju Tiwari6c639c92014-07-15 18:11:54 +0530484 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
485 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600486}
487
488
489# Determine if current distribution is a SUSE-based distribution
490# (openSUSE, SLE).
491# is_suse
492function is_suse {
493 if [[ -z "$os_VENDOR" ]]; then
494 GetOSVersion
495 fi
496
497 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
498}
499
500
501# Determine if current distribution is an Ubuntu-based distribution
502# It will also detect non-Ubuntu but Debian-based distros
503# is_ubuntu
504function is_ubuntu {
505 if [[ -z "$os_PACKAGE" ]]; then
506 GetOSVersion
507 fi
508 [ "$os_PACKAGE" = "deb" ]
509}
510
511
512# Git Functions
513# =============
514
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600515# Returns openstack release name for a given branch name
516# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100517function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600518 local branch=$1
Dean Troyer50cda692014-07-25 11:57:20 -0500519
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600520 if [[ $branch =~ "stable/" ]]; then
521 echo ${branch#*/}
522 else
523 echo "master"
524 fi
525}
526
Dean Troyerdff49a22014-01-30 15:37:40 -0600527# git clone only if directory doesn't exist already. Since ``DEST`` might not
528# be owned by the installation user, we create the directory and change the
529# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500530# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
531# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600532# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer50cda692014-07-25 11:57:20 -0500533# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600534# git_clone remote dest-dir branch
535function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500536 local git_remote=$1
537 local git_dest=$2
538 local git_ref=$3
539 local orig_dir=$(pwd)
540
Dean Troyerdff49a22014-01-30 15:37:40 -0600541 RECLONE=$(trueorfalse False $RECLONE)
542
543 if [[ "$OFFLINE" = "True" ]]; then
544 echo "Running in offline mode, clones already exist"
545 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500546 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600547 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400548 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600549 return
550 fi
551
Dean Troyer50cda692014-07-25 11:57:20 -0500552 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600553 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500554 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600555 [[ "$ERROR_ON_CLONE" = "True" ]] && \
556 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer50cda692014-07-25 11:57:20 -0500557 git_timed clone $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600558 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500559 cd $git_dest
560 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600561 else
562 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500563 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600564 [[ "$ERROR_ON_CLONE" = "True" ]] && \
565 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer50cda692014-07-25 11:57:20 -0500566 git_timed clone $git_remote $git_dest
567 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600568 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500569 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600570 elif [[ "$RECLONE" = "True" ]]; then
571 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500572 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600573 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500574 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100575 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600576 # remove the existing ignored files (like pyc) as they cause breakage
577 # (due to the py files having older timestamps than our pyc, so python
578 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500579 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600580
Dean Troyer50cda692014-07-25 11:57:20 -0500581 # handle git_ref accordingly to type (tag, branch)
582 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
583 git_update_tag $git_ref
584 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
585 git_update_branch $git_ref
586 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
587 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600588 else
Dean Troyer50cda692014-07-25 11:57:20 -0500589 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600590 fi
591
592 fi
593 fi
594
595 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500596 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600597 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400598 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600599}
600
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100601# git can sometimes get itself infinitely stuck with transient network
602# errors or other issues with the remote end. This wraps git in a
603# timeout/retry loop and is intended to watch over non-local git
604# processes that might hang. GIT_TIMEOUT, if set, is passed directly
605# to timeout(1); otherwise the default value of 0 maintains the status
606# quo of waiting forever.
607# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100608function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100609 local count=0
610 local timeout=0
611
612 if [[ -n "${GIT_TIMEOUT}" ]]; then
613 timeout=${GIT_TIMEOUT}
614 fi
615
616 until timeout -s SIGINT ${timeout} git "$@"; do
617 # 124 is timeout(1)'s special return code when it reached the
618 # timeout; otherwise assume fatal failure
619 if [[ $? -ne 124 ]]; then
620 die $LINENO "git call failed: [git $@]"
621 fi
622
623 count=$(($count + 1))
624 warn "timeout ${count} for git call: [git $@]"
625 if [ $count -eq 3 ]; then
626 die $LINENO "Maximum of 3 git retries reached"
627 fi
628 sleep 5
629 done
630}
631
Dean Troyerdff49a22014-01-30 15:37:40 -0600632# git update using reference as a branch.
633# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100634function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500635 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600636
Dean Troyer50cda692014-07-25 11:57:20 -0500637 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600638 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500639 git branch -D $git_branch || true
640 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600641}
642
643# git update using reference as a branch.
644# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100645function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500646 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600647
Dean Troyer50cda692014-07-25 11:57:20 -0500648 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600649}
650
651# git update using reference as a tag. Be careful editing source at that repo
652# as working copy will be in a detached mode
653# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100654function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500655 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600656
Dean Troyer50cda692014-07-25 11:57:20 -0500657 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600658 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500659 git_timed fetch origin tag $git_tag
660 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600661}
662
663
664# OpenStack Functions
665# ===================
666
667# Get the default value for HOST_IP
668# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100669function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600670 local fixed_range=$1
671 local floating_range=$2
672 local host_ip_iface=$3
673 local host_ip=$4
674
675 # Find the interface used for the default route
676 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
677 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
678 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
679 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500680 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
681 local ip
682 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600683 # Attempt to filter out IP addresses that are part of the fixed and
684 # floating range. Note that this method only works if the ``netaddr``
685 # python library is installed. If it is not installed, an error
686 # will be printed and the first IP from the interface will be used.
687 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
688 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500689 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
690 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600691 break;
692 fi
693 done
694 fi
695 echo $host_ip
696}
697
Attila Fazekasf71b5002014-05-28 09:52:22 +0200698# Generates hex string from ``size`` byte of pseudo random data
699# generate_hex_string size
700function generate_hex_string {
701 local size=$1
702 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
703}
704
Dean Troyerdff49a22014-01-30 15:37:40 -0600705# Grab a numbered field from python prettytable output
706# Fields are numbered starting with 1
707# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
708# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100709function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500710 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600711 while read data; do
712 if [ "$1" -lt 0 ]; then
713 field="(\$(NF$1))"
714 else
715 field="\$$(($1 + 1))"
716 fi
717 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
718 done
719}
720
721# Add a policy to a policy.json file
722# Do nothing if the policy already exists
723# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100724function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600725 local policy_file=$1
726 local policy_name=$2
727 local policy_perm=$3
728
729 if grep -q ${policy_name} ${policy_file}; then
730 echo "Policy ${policy_name} already exists in ${policy_file}"
731 return
732 fi
733
734 # Add a terminating comma to policy lines without one
735 # Remove the closing '}' and all lines following to the end-of-file
736 local tmpfile=$(mktemp)
737 uniq ${policy_file} | sed -e '
738 s/]$/],/
739 /^[}]/,$d
740 ' > ${tmpfile}
741
742 # Append policy and closing brace
743 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
744 echo "}" >>${tmpfile}
745
746 mv ${tmpfile} ${policy_file}
747}
748
Bartosz Górski0abde392014-02-28 14:15:19 +0100749# Gets or creates user
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200750# Usage: get_or_create_user <username> <password> <project> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100751function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200752 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500753 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200754 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500755 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200756 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100757 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500758 local user_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100759 # Gets user id
760 openstack user show $1 -f value -c id 2>/dev/null ||
761 # Creates new user
762 openstack user create \
763 $1 \
764 --password "$2" \
765 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500766 $email \
Bartosz Górski0abde392014-02-28 14:15:19 +0100767 -f value -c id
768 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500769 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100770}
771
772# Gets or creates project
773# Usage: get_or_create_project <name>
774function get_or_create_project {
775 # Gets project id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500776 local project_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100777 # Gets project id
778 openstack project show $1 -f value -c id 2>/dev/null ||
779 # Creates new project if not exists
780 openstack project create $1 -f value -c id
781 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500782 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100783}
784
785# Gets or creates role
786# Usage: get_or_create_role <name>
787function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500788 local role_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100789 # Gets role id
790 openstack role show $1 -f value -c id 2>/dev/null ||
791 # Creates role if not exists
792 openstack role create $1 -f value -c id
793 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500794 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100795}
796
797# Gets or adds user role
798# Usage: get_or_add_user_role <role> <user> <project>
799function get_or_add_user_role {
800 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500801 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100802 $2 \
803 --project $3 \
804 --column "ID" \
805 --column "Name" \
806 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500807 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100808 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500809 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100810 $1 \
811 --user $2 \
812 --project $3 \
813 | grep " id " | get_field 2)
814 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500815 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100816}
817
818# Gets or creates service
819# Usage: get_or_create_service <name> <type> <description>
820function get_or_create_service {
821 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500822 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100823 # Gets service id
824 openstack service show $1 -f value -c id 2>/dev/null ||
825 # Creates new service if not exists
826 openstack service create \
827 $1 \
828 --type=$2 \
829 --description="$3" \
830 -f value -c id
831 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500832 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100833}
834
835# Gets or creates endpoint
836# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
837function get_or_create_endpoint {
838 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500839 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100840 --column "ID" \
841 --column "Region" \
842 --column "Service Name" \
843 | grep " $2 " \
844 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500845 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100846 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500847 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100848 $1 \
849 --region $2 \
850 --publicurl $3 \
851 --adminurl $4 \
852 --internalurl $5 \
853 | grep " id " | get_field 2)
854 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500855 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100856}
Dean Troyerdff49a22014-01-30 15:37:40 -0600857
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500858
Dean Troyerdff49a22014-01-30 15:37:40 -0600859# Package Functions
860# =================
861
862# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100863function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600864 local pkg_dir
865 if is_ubuntu; then
866 pkg_dir=$FILES/apts
867 elif is_fedora; then
868 pkg_dir=$FILES/rpms
869 elif is_suse; then
870 pkg_dir=$FILES/rpms-suse
871 else
872 exit_distro_not_supported "list of packages"
873 fi
874 echo "$pkg_dir"
875}
876
877# Wrapper for ``apt-get`` to set cache and proxy environment variables
878# Uses globals ``OFFLINE``, ``*_proxy``
879# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100880function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500881 local xtrace=$(set +o | grep xtrace)
882 set +o xtrace
883
Dean Troyerdff49a22014-01-30 15:37:40 -0600884 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
885 local sudo="sudo"
886 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500887
888 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600889 $sudo DEBIAN_FRONTEND=noninteractive \
890 http_proxy=$http_proxy https_proxy=$https_proxy \
891 no_proxy=$no_proxy \
892 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
893}
894
895# get_packages() collects a list of package names of any type from the
896# prerequisite files in ``files/{apts|rpms}``. The list is intended
897# to be passed to a package installer such as apt or yum.
898#
899# Only packages required for the services in 1st argument will be
900# included. Two bits of metadata are recognized in the prerequisite files:
901#
902# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
903# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
904# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100905function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500906 local xtrace=$(set +o | grep xtrace)
907 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600908 local services=$@
909 local package_dir=$(_get_package_dir)
910 local file_to_parse
911 local service
912
913 if [[ -z "$package_dir" ]]; then
914 echo "No package directory supplied"
915 return 1
916 fi
917 if [[ -z "$DISTRO" ]]; then
918 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -0500919 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -0600920 fi
921 for service in ${services//,/ }; do
922 # Allow individual services to specify dependencies
923 if [[ -e ${package_dir}/${service} ]]; then
924 file_to_parse="${file_to_parse} $service"
925 fi
926 # NOTE(sdague) n-api needs glance for now because that's where
927 # glance client is
928 if [[ $service == n-api ]]; then
929 if [[ ! $file_to_parse =~ nova ]]; then
930 file_to_parse="${file_to_parse} nova"
931 fi
932 if [[ ! $file_to_parse =~ glance ]]; then
933 file_to_parse="${file_to_parse} glance"
934 fi
935 elif [[ $service == c-* ]]; then
936 if [[ ! $file_to_parse =~ cinder ]]; then
937 file_to_parse="${file_to_parse} cinder"
938 fi
939 elif [[ $service == ceilometer-* ]]; then
940 if [[ ! $file_to_parse =~ ceilometer ]]; then
941 file_to_parse="${file_to_parse} ceilometer"
942 fi
943 elif [[ $service == s-* ]]; then
944 if [[ ! $file_to_parse =~ swift ]]; then
945 file_to_parse="${file_to_parse} swift"
946 fi
947 elif [[ $service == n-* ]]; then
948 if [[ ! $file_to_parse =~ nova ]]; then
949 file_to_parse="${file_to_parse} nova"
950 fi
951 elif [[ $service == g-* ]]; then
952 if [[ ! $file_to_parse =~ glance ]]; then
953 file_to_parse="${file_to_parse} glance"
954 fi
955 elif [[ $service == key* ]]; then
956 if [[ ! $file_to_parse =~ keystone ]]; then
957 file_to_parse="${file_to_parse} keystone"
958 fi
959 elif [[ $service == q-* ]]; then
960 if [[ ! $file_to_parse =~ neutron ]]; then
961 file_to_parse="${file_to_parse} neutron"
962 fi
Adam Gandelman539ec432014-03-18 18:57:43 -0700963 elif [[ $service == ir-* ]]; then
964 if [[ ! $file_to_parse =~ ironic ]]; then
965 file_to_parse="${file_to_parse} ironic"
966 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600967 fi
968 done
969
970 for file in ${file_to_parse}; do
971 local fname=${package_dir}/${file}
972 local OIFS line package distros distro
973 [[ -e $fname ]] || continue
974
975 OIFS=$IFS
976 IFS=$'\n'
977 for line in $(<${fname}); do
978 if [[ $line =~ "NOPRIME" ]]; then
979 continue
980 fi
981
982 # Assume we want this package
983 package=${line%#*}
984 inst_pkg=1
985
986 # Look for # dist:xxx in comment
987 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
988 # We are using BASH regexp matching feature.
989 package=${BASH_REMATCH[1]}
990 distros=${BASH_REMATCH[2]}
991 # In bash ${VAR,,} will lowecase VAR
992 # Look for a match in the distro list
993 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
994 # If no match then skip this package
995 inst_pkg=0
996 fi
997 fi
998
999 # Look for # testonly in comment
1000 if [[ $line =~ (.*)#.*testonly.* ]]; then
1001 package=${BASH_REMATCH[1]}
1002 # Are we installing test packages? (test for the default value)
1003 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1004 # If not installing test packages the skip this package
1005 inst_pkg=0
1006 fi
1007 fi
1008
1009 if [[ $inst_pkg = 1 ]]; then
1010 echo $package
1011 fi
1012 done
1013 IFS=$OIFS
1014 done
Sean Dague45917cc2014-02-24 16:09:14 -05001015 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001016}
1017
1018# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001019# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001020# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001021function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001022 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001023 return 0
1024 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001025
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001026 if is_ubuntu; then
1027 local xtrace=$(set +o | grep xtrace)
1028 set +o xtrace
1029 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1030 # if there are transient errors pulling the updates, that's fine.
1031 # It may be secondary repositories that we don't really care about.
1032 apt_get update || /bin/true
1033 REPOS_UPDATED=True
1034 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001035 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001036 fi
1037}
1038
1039function real_install_package {
1040 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001041 apt_get install "$@"
1042 elif is_fedora; then
1043 yum_install "$@"
1044 elif is_suse; then
1045 zypper_install "$@"
1046 else
1047 exit_distro_not_supported "installing packages"
1048 fi
1049}
1050
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001051# Distro-agnostic package installer
1052# install_package package [package ...]
1053function install_package {
1054 update_package_repo
1055 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1056}
1057
Dean Troyerdff49a22014-01-30 15:37:40 -06001058# Distro-agnostic function to tell if a package is installed
1059# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001060function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001061 if [[ -z "$@" ]]; then
1062 return 1
1063 fi
1064
1065 if [[ -z "$os_PACKAGE" ]]; then
1066 GetOSVersion
1067 fi
1068
1069 if [[ "$os_PACKAGE" = "deb" ]]; then
1070 dpkg -s "$@" > /dev/null 2> /dev/null
1071 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1072 rpm --quiet -q "$@"
1073 else
1074 exit_distro_not_supported "finding if a package is installed"
1075 fi
1076}
1077
1078# Distro-agnostic package uninstaller
1079# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001080function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001081 if is_ubuntu; then
1082 apt_get purge "$@"
1083 elif is_fedora; then
1084 sudo yum remove -y "$@"
1085 elif is_suse; then
1086 sudo zypper rm "$@"
1087 else
1088 exit_distro_not_supported "uninstalling packages"
1089 fi
1090}
1091
1092# Wrapper for ``yum`` to set proxy environment variables
1093# Uses globals ``OFFLINE``, ``*_proxy``
1094# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001095function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001096 [[ "$OFFLINE" = "True" ]] && return
1097 local sudo="sudo"
1098 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001099
1100 # The manual check for missing packages is because yum -y assumes
1101 # missing packages are OK. See
1102 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001103 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1104 no_proxy=$no_proxy \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001105 yum install -y "$@" 2>&1 | \
1106 awk '
1107 BEGIN { fail=0 }
1108 /No package/ { fail=1 }
1109 { print }
1110 END { exit fail }' || \
1111 die $LINENO "Missing packages detected"
1112
1113 # also ensure we catch a yum failure
1114 if [[ ${PIPESTATUS[0]} != 0 ]]; then
1115 die $LINENO "Yum install failure"
1116 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001117}
1118
1119# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001120# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001121# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001122function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001123 [[ "$OFFLINE" = "True" ]] && return
1124 local sudo="sudo"
1125 [[ "$(id -u)" = "0" ]] && sudo="env"
1126 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1127 zypper --non-interactive install --auto-agree-with-licenses "$@"
1128}
1129
1130
1131# Process Functions
1132# =================
1133
1134# _run_process() is designed to be backgrounded by run_process() to simulate a
1135# fork. It includes the dirty work of closing extra filehandles and preparing log
1136# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001137# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1138# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001139# _run_process service "command-line"
Ian Wienandaee18c72014-02-21 15:35:08 +11001140function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001141 local service=$1
1142 local command="$2"
1143
1144 # Undo logging redirections and close the extra descriptors
1145 exec 1>&3
1146 exec 2>&3
1147 exec 3>&-
1148 exec 6>&-
1149
1150 if [[ -n ${SCREEN_LOGDIR} ]]; then
1151 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1152 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1153
1154 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1155 export PYTHONUNBUFFERED=1
1156 fi
1157
1158 exec /bin/bash -c "$command"
1159 die "$service exec failure: $command"
1160}
1161
1162# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1163# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001164# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001165# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001166function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001167 SCREEN_NAME=${SCREEN_NAME:-stack}
1168 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1169
1170 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1171 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1172 fi
1173
1174 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1175}
1176
1177# Find out if a process exists by partial name.
1178# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001179function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001180 local name=$1
1181 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001182 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001183 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001184 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001185}
1186
1187# run_process() launches a child process that closes all file descriptors and
1188# then exec's the passed in command. This is meant to duplicate the semantics
1189# of screen_it() without screen. PIDs are written to
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001190# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid``
Dean Troyerdff49a22014-01-30 15:37:40 -06001191# run_process service "command-line"
Ian Wienandaee18c72014-02-21 15:35:08 +11001192function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001193 local service=$1
1194 local command="$2"
1195
1196 # Spawn the child process
1197 _run_process "$service" "$command" &
1198 echo $!
1199}
1200
1201# Helper to launch a service in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001202# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1203# ``SERVICE_DIR``, ``USE_SCREEN``
Dean Troyerdff49a22014-01-30 15:37:40 -06001204# screen_it service "command-line"
1205function screen_it {
Sean Dagueea22a4f2014-06-27 15:21:41 -04001206 SCREEN_NAME=${SCREEN_NAME:-stack}
1207 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1208 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001209
1210 if is_service_enabled $1; then
1211 # Append the service to the screen rc file
Sean Dagueea22a4f2014-06-27 15:21:41 -04001212 screen_rc "$1" "$2"
Dean Troyerdff49a22014-01-30 15:37:40 -06001213
Sean Dagueea22a4f2014-06-27 15:21:41 -04001214 if [[ "$USE_SCREEN" = "True" ]]; then
1215 screen -S $SCREEN_NAME -X screen -t $1
Dean Troyerdff49a22014-01-30 15:37:40 -06001216
Sean Dagueea22a4f2014-06-27 15:21:41 -04001217 if [[ -n ${SCREEN_LOGDIR} ]]; then
1218 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
1219 screen -S $SCREEN_NAME -p $1 -X log on
1220 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1221 fi
1222
1223 # sleep to allow bash to be ready to be send the command - we are
1224 # creating a new window in screen and then sends characters, so if
1225 # bash isn't running by the time we send the command, nothing happens
1226 sleep 3
1227
1228 NL=`echo -ne '\015'`
1229 # This fun command does the following:
1230 # - the passed server command is backgrounded
1231 # - the pid of the background process is saved in the usual place
1232 # - the server process is brought back to the foreground
1233 # - if the server process exits prematurely the fg command errors
1234 # and a message is written to stdout and the service failure file
1235 # The pid saved can be used in screen_stop() as a process group
1236 # id to kill off all child processes
1237 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 -06001238 else
1239 # Spawn directly without screen
Sean Dagueea22a4f2014-06-27 15:21:41 -04001240 run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001241 fi
1242 fi
1243}
1244
1245# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001246# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001247# screen_rc service "command-line"
1248function screen_rc {
1249 SCREEN_NAME=${SCREEN_NAME:-stack}
1250 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1251 if [[ ! -e $SCREENRC ]]; then
1252 # Name the screen session
1253 echo "sessionname $SCREEN_NAME" > $SCREENRC
1254 # Set a reasonable statusbar
1255 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1256 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1257 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1258 echo "screen -t shell bash" >> $SCREENRC
1259 fi
1260 # If this service doesn't already exist in the screenrc file
1261 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1262 NL=`echo -ne '\015'`
1263 echo "screen -t $1 bash" >> $SCREENRC
1264 echo "stuff \"$2$NL\"" >> $SCREENRC
1265
1266 if [[ -n ${SCREEN_LOGDIR} ]]; then
1267 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1268 echo "log on" >>$SCREENRC
1269 fi
1270 fi
1271}
1272
1273# Stop a service in screen
1274# If a PID is available use it, kill the whole process group via TERM
1275# If screen is being used kill the screen window; this will catch processes
1276# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001277# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Dean Troyerdff49a22014-01-30 15:37:40 -06001278# screen_stop service
Ian Wienandaee18c72014-02-21 15:35:08 +11001279function screen_stop {
Dean Troyerdff49a22014-01-30 15:37:40 -06001280 SCREEN_NAME=${SCREEN_NAME:-stack}
1281 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1282 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1283
1284 if is_service_enabled $1; then
1285 # Kill via pid if we have one available
1286 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$1.pid ]]; then
1287 pkill -TERM -P -$(cat $SERVICE_DIR/$SCREEN_NAME/$1.pid)
1288 rm $SERVICE_DIR/$SCREEN_NAME/$1.pid
1289 fi
1290 if [[ "$USE_SCREEN" = "True" ]]; then
1291 # Clean up the screen window
1292 screen -S $SCREEN_NAME -p $1 -X kill
1293 fi
1294 fi
1295}
1296
1297# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001298# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001299# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001300function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001301 local service
1302 local failures
1303 SCREEN_NAME=${SCREEN_NAME:-stack}
1304 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1305
1306
1307 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1308 echo "No service status directory found"
1309 return
1310 fi
1311
1312 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001313 # make this -o errexit safe
1314 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001315
1316 for service in $failures; do
1317 service=`basename $service`
1318 service=${service%.failure}
1319 echo "Error: Service $service is not running"
1320 done
1321
1322 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001323 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001324 fi
1325}
1326
1327
1328# Python Functions
1329# ================
1330
1331# Get the path to the pip command.
1332# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001333function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001334 which pip || which pip-python
1335
1336 if [ $? -ne 0 ]; then
1337 die $LINENO "Unable to find pip; cannot continue"
1338 fi
1339}
1340
1341# Get the path to the direcotry where python executables are installed.
1342# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001343function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001344 if is_fedora || is_suse; then
1345 echo "/usr/bin"
1346 else
1347 echo "/usr/local/bin"
1348 fi
1349}
1350
1351# Wrapper for ``pip install`` to set cache and proxy environment variables
1352# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
1353# ``TRACK_DEPENDS``, ``*_proxy``
1354# pip_install package [package ...]
1355function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001356 local xtrace=$(set +o | grep xtrace)
1357 set +o xtrace
1358 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1359 $xtrace
1360 return
1361 fi
1362
Dean Troyerdff49a22014-01-30 15:37:40 -06001363 if [[ -z "$os_PACKAGE" ]]; then
1364 GetOSVersion
1365 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001366 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1367 # TRACK_DEPENDS=True installation creates a circular dependency when
1368 # we attempt to install virtualenv into a virualenv, so we must global
1369 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001370 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001371 local cmd_pip=$DEST/.venv/bin/pip
1372 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001373 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001374 local cmd_pip=$(get_pip_command)
1375 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001376 fi
1377
1378 # Mirror option not needed anymore because pypi has CDN available,
1379 # but it's useful in certain circumstances
1380 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001381 local pip_mirror_opt=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001382 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001383 pip_mirror_opt="--use-mirrors"
Dean Troyerdff49a22014-01-30 15:37:40 -06001384 fi
1385
1386 # pip < 1.4 has a bug where it will use an already existing build
1387 # directory unconditionally. Say an earlier component installs
1388 # foo v1.1; pip will have built foo's source in
1389 # /tmp/$USER-pip-build. Even if a later component specifies foo <
1390 # 1.1, the existing extracted build will be used and cause
1391 # confusing errors. By creating unique build directories we avoid
1392 # this problem. See https://github.com/pypa/pip/issues/709
1393 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1394
Sean Dague45917cc2014-02-24 16:09:14 -05001395 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001396 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001397 http_proxy=$http_proxy \
1398 https_proxy=$https_proxy \
1399 no_proxy=$no_proxy \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001400 $cmd_pip install --build=${pip_build_tmp} \
1401 $pip_mirror_opt $@ \
1402 && $sudo_pip rm -rf ${pip_build_tmp}
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001403
1404 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1405 local test_req="$@/test-requirements.txt"
1406 if [[ -e "$test_req" ]]; then
1407 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1408 http_proxy=$http_proxy \
1409 https_proxy=$https_proxy \
1410 no_proxy=$no_proxy \
1411 $cmd_pip install --build=${pip_build_tmp} \
1412 $pip_mirror_opt -r $test_req \
1413 && $sudo_pip rm -rf ${pip_build_tmp}
1414 fi
1415 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001416}
1417
Sean Dague099e5e32014-03-31 10:35:43 -04001418# this should be used if you want to install globally, all libraries should
1419# use this, especially *oslo* ones
1420function setup_install {
1421 local project_dir=$1
1422 setup_package_with_req_sync $project_dir
1423}
1424
1425# this should be used for projects which run services, like all services
1426function setup_develop {
1427 local project_dir=$1
1428 setup_package_with_req_sync $project_dir -e
1429}
1430
Dean Troyeraf616d92014-02-17 12:57:55 -06001431# ``pip install -e`` the package, which processes the dependencies
1432# using pip before running `setup.py develop`
1433#
1434# Updates the dependencies in project_dir from the
1435# openstack/requirements global list before installing anything.
1436#
1437# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1438# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001439function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001440 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001441 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001442
Dean Troyeraf616d92014-02-17 12:57:55 -06001443 # Don't update repo if local changes exist
1444 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001445 # ``errexit`` requires us to trap the exit code when the repo is changed
1446 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001447
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001448 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001449 (cd $REQUIREMENTS_DIR; \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001450 python update.py $project_dir)
Dean Troyeraf616d92014-02-17 12:57:55 -06001451 fi
1452
Sean Dague099e5e32014-03-31 10:35:43 -04001453 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001454
1455 # We've just gone and possibly modified the user's source tree in an
1456 # automated way, which is considered bad form if it's a development
1457 # tree because we've screwed up their next git checkin. So undo it.
1458 #
1459 # However... there are some circumstances, like running in the gate
1460 # where we really really want the overridden version to stick. So provide
1461 # a variable that tells us whether or not we should UNDO the requirements
1462 # changes (this will be set to False in the OpenStack ci gate)
1463 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001464 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001465 (cd $project_dir && git reset --hard)
1466 fi
1467 fi
1468}
1469
1470# ``pip install -e`` the package, which processes the dependencies
1471# using pip before running `setup.py develop`
1472# Uses globals ``STACK_USER``
1473# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001474function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001475 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001476 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001477
Sean Dague099e5e32014-03-31 10:35:43 -04001478 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001479 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001480 if [[ "$flags" == "-e" ]]; then
1481 safe_chown -R $STACK_USER $1/*.egg-info
1482 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001483}
1484
Dean Troyerdff49a22014-01-30 15:37:40 -06001485
1486# Service Functions
1487# =================
1488
1489# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1490# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001491function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001492 echo "$1" | sed -e '
1493 s/,,/,/g;
1494 s/^,//;
1495 s/,$//
1496 '
1497}
1498
1499# disable_all_services() removes all current services
1500# from ``ENABLED_SERVICES`` to reset the configuration
1501# before a minimal installation
1502# Uses global ``ENABLED_SERVICES``
1503# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001504function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001505 ENABLED_SERVICES=""
1506}
1507
1508# Remove all services starting with '-'. For example, to install all default
1509# services except rabbit (rabbit) set in ``localrc``:
1510# ENABLED_SERVICES+=",-rabbit"
1511# Uses global ``ENABLED_SERVICES``
1512# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001513function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001514 local tmpsvcs="${ENABLED_SERVICES}"
1515 local service
1516 for service in ${tmpsvcs//,/ }; do
1517 if [[ ${service} == -* ]]; then
1518 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1519 fi
1520 done
1521 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1522}
1523
1524# disable_service() removes the services passed as argument to the
1525# ``ENABLED_SERVICES`` list, if they are present.
1526#
1527# For example:
1528# disable_service rabbit
1529#
1530# This function does not know about the special cases
1531# for nova, glance, and neutron built into is_service_enabled().
1532# Uses global ``ENABLED_SERVICES``
1533# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001534function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001535 local tmpsvcs=",${ENABLED_SERVICES},"
1536 local service
1537 for service in $@; do
1538 if is_service_enabled $service; then
1539 tmpsvcs=${tmpsvcs//,$service,/,}
1540 fi
1541 done
1542 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1543}
1544
1545# enable_service() adds the services passed as argument to the
1546# ``ENABLED_SERVICES`` list, if they are not already present.
1547#
1548# For example:
1549# enable_service qpid
1550#
1551# This function does not know about the special cases
1552# for nova, glance, and neutron built into is_service_enabled().
1553# Uses global ``ENABLED_SERVICES``
1554# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001555function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001556 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001557 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001558 for service in $@; do
1559 if ! is_service_enabled $service; then
1560 tmpsvcs+=",$service"
1561 fi
1562 done
1563 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1564 disable_negated_services
1565}
1566
1567# is_service_enabled() checks if the service(s) specified as arguments are
1568# enabled by the user in ``ENABLED_SERVICES``.
1569#
1570# Multiple services specified as arguments are ``OR``'ed together; the test
1571# is a short-circuit boolean, i.e it returns on the first match.
1572#
1573# There are special cases for some 'catch-all' services::
1574# **nova** returns true if any service enabled start with **n-**
1575# **cinder** returns true if any service enabled start with **c-**
1576# **ceilometer** returns true if any service enabled start with **ceilometer**
1577# **glance** returns true if any service enabled start with **g-**
1578# **neutron** returns true if any service enabled start with **q-**
1579# **swift** returns true if any service enabled start with **s-**
1580# **trove** returns true if any service enabled start with **tr-**
1581# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1582# **s-** services will be enabled. This will be deprecated in the future.
1583#
1584# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1585# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1586# as enabled in this case.
1587#
1588# Uses global ``ENABLED_SERVICES``
1589# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001590function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001591 local xtrace=$(set +o | grep xtrace)
1592 set +o xtrace
1593 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001594 local services=$@
1595 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001596 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001597 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001598
1599 # Look for top-level 'enabled' function for this service
1600 if type is_${service}_enabled >/dev/null 2>&1; then
1601 # A function exists for this service, use it
1602 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001603 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001604 fi
1605
1606 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1607 # are implemented
1608
Sean Dague45917cc2014-02-24 16:09:14 -05001609 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
1610 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1611 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1612 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1613 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1614 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1615 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1616 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1617 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1618 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001619 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001620 done
Sean Dague45917cc2014-02-24 16:09:14 -05001621 $xtrace
1622 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001623}
1624
1625# Toggle enable/disable_service for services that must run exclusive of each other
1626# $1 The name of a variable containing a space-separated list of services
1627# $2 The name of a variable in which to store the enabled service's name
1628# $3 The name of the service to enable
1629function use_exclusive_service {
1630 local options=${!1}
1631 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001632 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001633 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001634 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001635 for opt in $options;do
1636 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1637 done
1638 eval "$out=$selection"
1639 return 0
1640}
1641
1642
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001643# System Functions
1644# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001645
1646# Only run the command if the target file (the last arg) is not on an
1647# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001648function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001649 local xtrace=$(set +o | grep xtrace)
1650 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001651 local args=( $@ )
1652 local last
1653 local sudo_cmd
1654 local dir_to_check
1655
1656 let last="${#args[*]} - 1"
1657
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001658 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001659 if [ ! -d "$dir_to_check" ]; then
1660 dir_to_check=`dirname "$dir_to_check"`
1661 fi
1662
1663 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001664 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001665 return 0
1666 fi
1667
1668 if [[ $TRACK_DEPENDS = True ]]; then
1669 sudo_cmd="env"
1670 else
1671 sudo_cmd="sudo"
1672 fi
1673
Sean Dague45917cc2014-02-24 16:09:14 -05001674 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001675 $sudo_cmd $@
1676}
1677
1678# Exit 0 if address is in network or 1 if address is not in network
1679# ip-range is in CIDR notation: 1.2.3.4/20
1680# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001681function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001682 local ip=$1
1683 local range=$2
1684 local masklen=${range#*/}
1685 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1686 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1687 [[ $network == $subnet ]]
1688}
1689
1690# Add a user to a group.
1691# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001692function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001693 local user=$1
1694 local group=$2
1695
1696 if [[ -z "$os_VENDOR" ]]; then
1697 GetOSVersion
1698 fi
1699
1700 # SLE11 and openSUSE 12.2 don't have the usual usermod
1701 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1702 sudo usermod -a -G "$group" "$user"
1703 else
1704 sudo usermod -A "$group" "$user"
1705 fi
1706}
1707
1708# Convert CIDR notation to a IPv4 netmask
1709# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001710function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001711 local maskpat="255 255 255 255"
1712 local maskdgt="254 252 248 240 224 192 128"
1713 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1714 echo ${1-0}.${2-0}.${3-0}.${4-0}
1715}
1716
1717# Gracefully cp only if source file/dir exists
1718# cp_it source destination
1719function cp_it {
1720 if [ -e $1 ] || [ -d $1 ]; then
1721 cp -pRL $1 $2
1722 fi
1723}
1724
1725# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1726# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1727# ``localrc`` or on the command line if necessary::
1728#
1729# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1730#
1731# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1732
Ian Wienandaee18c72014-02-21 15:35:08 +11001733function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001734 if [[ -n "$http_proxy" ]]; then
1735 export http_proxy=$http_proxy
1736 fi
1737 if [[ -n "$https_proxy" ]]; then
1738 export https_proxy=$https_proxy
1739 fi
1740 if [[ -n "$no_proxy" ]]; then
1741 export no_proxy=$no_proxy
1742 fi
1743}
1744
1745# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001746function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001747 local mount_type=`stat -f -L -c %T $1`
1748 test "$mount_type" == "nfs"
1749}
1750
1751# Return the network portion of the given IP address using netmask
1752# netmask is in the traditional dotted-quad format
1753# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001754function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001755 local ip=$1
1756 local mask=$2
1757 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1758 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1759 echo $subnet
1760}
1761
1762# Service wrapper to restart services
1763# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001764function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001765 if is_ubuntu; then
1766 sudo /usr/sbin/service $1 restart
1767 else
1768 sudo /sbin/service $1 restart
1769 fi
1770}
1771
1772# Only change permissions of a file or directory if it is not on an
1773# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001774function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001775 _safe_permission_operation chmod $@
1776}
1777
1778# Only change ownership of a file or directory if it is not on an NFS
1779# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001780function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001781 _safe_permission_operation chown $@
1782}
1783
1784# Service wrapper to start services
1785# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001786function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001787 if is_ubuntu; then
1788 sudo /usr/sbin/service $1 start
1789 else
1790 sudo /sbin/service $1 start
1791 fi
1792}
1793
1794# Service wrapper to stop services
1795# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001796function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001797 if is_ubuntu; then
1798 sudo /usr/sbin/service $1 stop
1799 else
1800 sudo /sbin/service $1 stop
1801 fi
1802}
1803
1804
1805# Restore xtrace
1806$XTRACE
1807
1808# Local variables:
1809# mode: shell-script
1810# End: