blob: c09666458f857883452ccea51b5222a220916de2 [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
698# Grab a numbered field from python prettytable output
699# Fields are numbered starting with 1
700# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
701# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100702function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500703 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600704 while read data; do
705 if [ "$1" -lt 0 ]; then
706 field="(\$(NF$1))"
707 else
708 field="\$$(($1 + 1))"
709 fi
710 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
711 done
712}
713
714# Add a policy to a policy.json file
715# Do nothing if the policy already exists
716# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100717function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600718 local policy_file=$1
719 local policy_name=$2
720 local policy_perm=$3
721
722 if grep -q ${policy_name} ${policy_file}; then
723 echo "Policy ${policy_name} already exists in ${policy_file}"
724 return
725 fi
726
727 # Add a terminating comma to policy lines without one
728 # Remove the closing '}' and all lines following to the end-of-file
729 local tmpfile=$(mktemp)
730 uniq ${policy_file} | sed -e '
731 s/]$/],/
732 /^[}]/,$d
733 ' > ${tmpfile}
734
735 # Append policy and closing brace
736 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
737 echo "}" >>${tmpfile}
738
739 mv ${tmpfile} ${policy_file}
740}
741
Bartosz Górski0abde392014-02-28 14:15:19 +0100742# Gets or creates user
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200743# Usage: get_or_create_user <username> <password> <project> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100744function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200745 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500746 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200747 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500748 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200749 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100750 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500751 local user_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100752 # Gets user id
753 openstack user show $1 -f value -c id 2>/dev/null ||
754 # Creates new user
755 openstack user create \
756 $1 \
757 --password "$2" \
758 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500759 $email \
Bartosz Górski0abde392014-02-28 14:15:19 +0100760 -f value -c id
761 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500762 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100763}
764
765# Gets or creates project
766# Usage: get_or_create_project <name>
767function get_or_create_project {
768 # Gets project id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500769 local project_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100770 # Gets project id
771 openstack project show $1 -f value -c id 2>/dev/null ||
772 # Creates new project if not exists
773 openstack project create $1 -f value -c id
774 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500775 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100776}
777
778# Gets or creates role
779# Usage: get_or_create_role <name>
780function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500781 local role_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100782 # Gets role id
783 openstack role show $1 -f value -c id 2>/dev/null ||
784 # Creates role if not exists
785 openstack role create $1 -f value -c id
786 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500787 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100788}
789
790# Gets or adds user role
791# Usage: get_or_add_user_role <role> <user> <project>
792function get_or_add_user_role {
793 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500794 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100795 $2 \
796 --project $3 \
797 --column "ID" \
798 --column "Name" \
799 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500800 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100801 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500802 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100803 $1 \
804 --user $2 \
805 --project $3 \
806 | grep " id " | get_field 2)
807 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500808 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100809}
810
811# Gets or creates service
812# Usage: get_or_create_service <name> <type> <description>
813function get_or_create_service {
814 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500815 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100816 # Gets service id
817 openstack service show $1 -f value -c id 2>/dev/null ||
818 # Creates new service if not exists
819 openstack service create \
820 $1 \
821 --type=$2 \
822 --description="$3" \
823 -f value -c id
824 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500825 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100826}
827
828# Gets or creates endpoint
829# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
830function get_or_create_endpoint {
831 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500832 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100833 --column "ID" \
834 --column "Region" \
835 --column "Service Name" \
836 | grep " $2 " \
837 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500838 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100839 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500840 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100841 $1 \
842 --region $2 \
843 --publicurl $3 \
844 --adminurl $4 \
845 --internalurl $5 \
846 | grep " id " | get_field 2)
847 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500848 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100849}
Dean Troyerdff49a22014-01-30 15:37:40 -0600850
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500851
Dean Troyerdff49a22014-01-30 15:37:40 -0600852# Package Functions
853# =================
854
855# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100856function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600857 local pkg_dir
858 if is_ubuntu; then
859 pkg_dir=$FILES/apts
860 elif is_fedora; then
861 pkg_dir=$FILES/rpms
862 elif is_suse; then
863 pkg_dir=$FILES/rpms-suse
864 else
865 exit_distro_not_supported "list of packages"
866 fi
867 echo "$pkg_dir"
868}
869
870# Wrapper for ``apt-get`` to set cache and proxy environment variables
871# Uses globals ``OFFLINE``, ``*_proxy``
872# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100873function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500874 local xtrace=$(set +o | grep xtrace)
875 set +o xtrace
876
Dean Troyerdff49a22014-01-30 15:37:40 -0600877 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
878 local sudo="sudo"
879 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500880
881 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600882 $sudo DEBIAN_FRONTEND=noninteractive \
883 http_proxy=$http_proxy https_proxy=$https_proxy \
884 no_proxy=$no_proxy \
885 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
886}
887
888# get_packages() collects a list of package names of any type from the
889# prerequisite files in ``files/{apts|rpms}``. The list is intended
890# to be passed to a package installer such as apt or yum.
891#
892# Only packages required for the services in 1st argument will be
893# included. Two bits of metadata are recognized in the prerequisite files:
894#
895# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
896# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
897# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100898function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500899 local xtrace=$(set +o | grep xtrace)
900 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600901 local services=$@
902 local package_dir=$(_get_package_dir)
903 local file_to_parse
904 local service
905
906 if [[ -z "$package_dir" ]]; then
907 echo "No package directory supplied"
908 return 1
909 fi
910 if [[ -z "$DISTRO" ]]; then
911 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -0500912 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -0600913 fi
914 for service in ${services//,/ }; do
915 # Allow individual services to specify dependencies
916 if [[ -e ${package_dir}/${service} ]]; then
917 file_to_parse="${file_to_parse} $service"
918 fi
919 # NOTE(sdague) n-api needs glance for now because that's where
920 # glance client is
921 if [[ $service == n-api ]]; then
922 if [[ ! $file_to_parse =~ nova ]]; then
923 file_to_parse="${file_to_parse} nova"
924 fi
925 if [[ ! $file_to_parse =~ glance ]]; then
926 file_to_parse="${file_to_parse} glance"
927 fi
928 elif [[ $service == c-* ]]; then
929 if [[ ! $file_to_parse =~ cinder ]]; then
930 file_to_parse="${file_to_parse} cinder"
931 fi
932 elif [[ $service == ceilometer-* ]]; then
933 if [[ ! $file_to_parse =~ ceilometer ]]; then
934 file_to_parse="${file_to_parse} ceilometer"
935 fi
936 elif [[ $service == s-* ]]; then
937 if [[ ! $file_to_parse =~ swift ]]; then
938 file_to_parse="${file_to_parse} swift"
939 fi
940 elif [[ $service == n-* ]]; then
941 if [[ ! $file_to_parse =~ nova ]]; then
942 file_to_parse="${file_to_parse} nova"
943 fi
944 elif [[ $service == g-* ]]; then
945 if [[ ! $file_to_parse =~ glance ]]; then
946 file_to_parse="${file_to_parse} glance"
947 fi
948 elif [[ $service == key* ]]; then
949 if [[ ! $file_to_parse =~ keystone ]]; then
950 file_to_parse="${file_to_parse} keystone"
951 fi
952 elif [[ $service == q-* ]]; then
953 if [[ ! $file_to_parse =~ neutron ]]; then
954 file_to_parse="${file_to_parse} neutron"
955 fi
Adam Gandelman539ec432014-03-18 18:57:43 -0700956 elif [[ $service == ir-* ]]; then
957 if [[ ! $file_to_parse =~ ironic ]]; then
958 file_to_parse="${file_to_parse} ironic"
959 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600960 fi
961 done
962
963 for file in ${file_to_parse}; do
964 local fname=${package_dir}/${file}
965 local OIFS line package distros distro
966 [[ -e $fname ]] || continue
967
968 OIFS=$IFS
969 IFS=$'\n'
970 for line in $(<${fname}); do
971 if [[ $line =~ "NOPRIME" ]]; then
972 continue
973 fi
974
975 # Assume we want this package
976 package=${line%#*}
977 inst_pkg=1
978
979 # Look for # dist:xxx in comment
980 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
981 # We are using BASH regexp matching feature.
982 package=${BASH_REMATCH[1]}
983 distros=${BASH_REMATCH[2]}
984 # In bash ${VAR,,} will lowecase VAR
985 # Look for a match in the distro list
986 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
987 # If no match then skip this package
988 inst_pkg=0
989 fi
990 fi
991
992 # Look for # testonly in comment
993 if [[ $line =~ (.*)#.*testonly.* ]]; then
994 package=${BASH_REMATCH[1]}
995 # Are we installing test packages? (test for the default value)
996 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
997 # If not installing test packages the skip this package
998 inst_pkg=0
999 fi
1000 fi
1001
1002 if [[ $inst_pkg = 1 ]]; then
1003 echo $package
1004 fi
1005 done
1006 IFS=$OIFS
1007 done
Sean Dague45917cc2014-02-24 16:09:14 -05001008 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001009}
1010
1011# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001012# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001013# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001014function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001015 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001016 return 0
1017 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001018
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001019 if is_ubuntu; then
1020 local xtrace=$(set +o | grep xtrace)
1021 set +o xtrace
1022 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1023 # if there are transient errors pulling the updates, that's fine.
1024 # It may be secondary repositories that we don't really care about.
1025 apt_get update || /bin/true
1026 REPOS_UPDATED=True
1027 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001028 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001029 fi
1030}
1031
1032function real_install_package {
1033 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001034 apt_get install "$@"
1035 elif is_fedora; then
1036 yum_install "$@"
1037 elif is_suse; then
1038 zypper_install "$@"
1039 else
1040 exit_distro_not_supported "installing packages"
1041 fi
1042}
1043
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001044# Distro-agnostic package installer
1045# install_package package [package ...]
1046function install_package {
1047 update_package_repo
1048 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1049}
1050
Dean Troyerdff49a22014-01-30 15:37:40 -06001051# Distro-agnostic function to tell if a package is installed
1052# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001053function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001054 if [[ -z "$@" ]]; then
1055 return 1
1056 fi
1057
1058 if [[ -z "$os_PACKAGE" ]]; then
1059 GetOSVersion
1060 fi
1061
1062 if [[ "$os_PACKAGE" = "deb" ]]; then
1063 dpkg -s "$@" > /dev/null 2> /dev/null
1064 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1065 rpm --quiet -q "$@"
1066 else
1067 exit_distro_not_supported "finding if a package is installed"
1068 fi
1069}
1070
1071# Distro-agnostic package uninstaller
1072# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001073function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001074 if is_ubuntu; then
1075 apt_get purge "$@"
1076 elif is_fedora; then
1077 sudo yum remove -y "$@"
1078 elif is_suse; then
1079 sudo zypper rm "$@"
1080 else
1081 exit_distro_not_supported "uninstalling packages"
1082 fi
1083}
1084
1085# Wrapper for ``yum`` to set proxy environment variables
1086# Uses globals ``OFFLINE``, ``*_proxy``
1087# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001088function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001089 [[ "$OFFLINE" = "True" ]] && return
1090 local sudo="sudo"
1091 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001092
1093 # The manual check for missing packages is because yum -y assumes
1094 # missing packages are OK. See
1095 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001096 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1097 no_proxy=$no_proxy \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001098 yum install -y "$@" 2>&1 | \
1099 awk '
1100 BEGIN { fail=0 }
1101 /No package/ { fail=1 }
1102 { print }
1103 END { exit fail }' || \
1104 die $LINENO "Missing packages detected"
1105
1106 # also ensure we catch a yum failure
1107 if [[ ${PIPESTATUS[0]} != 0 ]]; then
1108 die $LINENO "Yum install failure"
1109 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001110}
1111
1112# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001113# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001114# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001115function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001116 [[ "$OFFLINE" = "True" ]] && return
1117 local sudo="sudo"
1118 [[ "$(id -u)" = "0" ]] && sudo="env"
1119 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1120 zypper --non-interactive install --auto-agree-with-licenses "$@"
1121}
1122
1123
1124# Process Functions
1125# =================
1126
1127# _run_process() is designed to be backgrounded by run_process() to simulate a
1128# fork. It includes the dirty work of closing extra filehandles and preparing log
1129# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001130# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1131# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001132# _run_process service "command-line"
Ian Wienandaee18c72014-02-21 15:35:08 +11001133function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001134 local service=$1
1135 local command="$2"
1136
1137 # Undo logging redirections and close the extra descriptors
1138 exec 1>&3
1139 exec 2>&3
1140 exec 3>&-
1141 exec 6>&-
1142
1143 if [[ -n ${SCREEN_LOGDIR} ]]; then
1144 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1145 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1146
1147 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1148 export PYTHONUNBUFFERED=1
1149 fi
1150
1151 exec /bin/bash -c "$command"
1152 die "$service exec failure: $command"
1153}
1154
1155# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1156# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001157# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001158# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001159function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001160 SCREEN_NAME=${SCREEN_NAME:-stack}
1161 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1162
1163 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1164 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1165 fi
1166
1167 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1168}
1169
1170# Find out if a process exists by partial name.
1171# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001172function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001173 local name=$1
1174 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001175 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001176 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001177 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001178}
1179
1180# run_process() launches a child process that closes all file descriptors and
1181# then exec's the passed in command. This is meant to duplicate the semantics
1182# of screen_it() without screen. PIDs are written to
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001183# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid``
Dean Troyerdff49a22014-01-30 15:37:40 -06001184# run_process service "command-line"
Ian Wienandaee18c72014-02-21 15:35:08 +11001185function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001186 local service=$1
1187 local command="$2"
1188
1189 # Spawn the child process
1190 _run_process "$service" "$command" &
1191 echo $!
1192}
1193
1194# Helper to launch a service in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001195# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1196# ``SERVICE_DIR``, ``USE_SCREEN``
Dean Troyerdff49a22014-01-30 15:37:40 -06001197# screen_it service "command-line"
1198function screen_it {
Sean Dagueea22a4f2014-06-27 15:21:41 -04001199 SCREEN_NAME=${SCREEN_NAME:-stack}
1200 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1201 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001202
1203 if is_service_enabled $1; then
1204 # Append the service to the screen rc file
Sean Dagueea22a4f2014-06-27 15:21:41 -04001205 screen_rc "$1" "$2"
Dean Troyerdff49a22014-01-30 15:37:40 -06001206
Sean Dagueea22a4f2014-06-27 15:21:41 -04001207 if [[ "$USE_SCREEN" = "True" ]]; then
1208 screen -S $SCREEN_NAME -X screen -t $1
Dean Troyerdff49a22014-01-30 15:37:40 -06001209
Sean Dagueea22a4f2014-06-27 15:21:41 -04001210 if [[ -n ${SCREEN_LOGDIR} ]]; then
1211 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
1212 screen -S $SCREEN_NAME -p $1 -X log on
1213 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1214 fi
1215
1216 # sleep to allow bash to be ready to be send the command - we are
1217 # creating a new window in screen and then sends characters, so if
1218 # bash isn't running by the time we send the command, nothing happens
1219 sleep 3
1220
1221 NL=`echo -ne '\015'`
1222 # This fun command does the following:
1223 # - the passed server command is backgrounded
1224 # - the pid of the background process is saved in the usual place
1225 # - the server process is brought back to the foreground
1226 # - if the server process exits prematurely the fg command errors
1227 # and a message is written to stdout and the service failure file
1228 # The pid saved can be used in screen_stop() as a process group
1229 # id to kill off all child processes
1230 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 -06001231 else
1232 # Spawn directly without screen
Sean Dagueea22a4f2014-06-27 15:21:41 -04001233 run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001234 fi
1235 fi
1236}
1237
1238# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001239# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001240# screen_rc service "command-line"
1241function screen_rc {
1242 SCREEN_NAME=${SCREEN_NAME:-stack}
1243 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1244 if [[ ! -e $SCREENRC ]]; then
1245 # Name the screen session
1246 echo "sessionname $SCREEN_NAME" > $SCREENRC
1247 # Set a reasonable statusbar
1248 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1249 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1250 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1251 echo "screen -t shell bash" >> $SCREENRC
1252 fi
1253 # If this service doesn't already exist in the screenrc file
1254 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1255 NL=`echo -ne '\015'`
1256 echo "screen -t $1 bash" >> $SCREENRC
1257 echo "stuff \"$2$NL\"" >> $SCREENRC
1258
1259 if [[ -n ${SCREEN_LOGDIR} ]]; then
1260 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1261 echo "log on" >>$SCREENRC
1262 fi
1263 fi
1264}
1265
1266# Stop a service in screen
1267# If a PID is available use it, kill the whole process group via TERM
1268# If screen is being used kill the screen window; this will catch processes
1269# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001270# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Dean Troyerdff49a22014-01-30 15:37:40 -06001271# screen_stop service
Ian Wienandaee18c72014-02-21 15:35:08 +11001272function screen_stop {
Dean Troyerdff49a22014-01-30 15:37:40 -06001273 SCREEN_NAME=${SCREEN_NAME:-stack}
1274 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1275 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1276
1277 if is_service_enabled $1; then
1278 # Kill via pid if we have one available
1279 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$1.pid ]]; then
1280 pkill -TERM -P -$(cat $SERVICE_DIR/$SCREEN_NAME/$1.pid)
1281 rm $SERVICE_DIR/$SCREEN_NAME/$1.pid
1282 fi
1283 if [[ "$USE_SCREEN" = "True" ]]; then
1284 # Clean up the screen window
1285 screen -S $SCREEN_NAME -p $1 -X kill
1286 fi
1287 fi
1288}
1289
1290# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001291# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001292# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001293function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001294 local service
1295 local failures
1296 SCREEN_NAME=${SCREEN_NAME:-stack}
1297 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1298
1299
1300 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1301 echo "No service status directory found"
1302 return
1303 fi
1304
1305 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001306 # make this -o errexit safe
1307 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001308
1309 for service in $failures; do
1310 service=`basename $service`
1311 service=${service%.failure}
1312 echo "Error: Service $service is not running"
1313 done
1314
1315 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001316 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001317 fi
1318}
1319
1320
1321# Python Functions
1322# ================
1323
1324# Get the path to the pip command.
1325# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001326function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001327 which pip || which pip-python
1328
1329 if [ $? -ne 0 ]; then
1330 die $LINENO "Unable to find pip; cannot continue"
1331 fi
1332}
1333
1334# Get the path to the direcotry where python executables are installed.
1335# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001336function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001337 if is_fedora || is_suse; then
1338 echo "/usr/bin"
1339 else
1340 echo "/usr/local/bin"
1341 fi
1342}
1343
1344# Wrapper for ``pip install`` to set cache and proxy environment variables
1345# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
1346# ``TRACK_DEPENDS``, ``*_proxy``
1347# pip_install package [package ...]
1348function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001349 local xtrace=$(set +o | grep xtrace)
1350 set +o xtrace
1351 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1352 $xtrace
1353 return
1354 fi
1355
Dean Troyerdff49a22014-01-30 15:37:40 -06001356 if [[ -z "$os_PACKAGE" ]]; then
1357 GetOSVersion
1358 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001359 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1360 # TRACK_DEPENDS=True installation creates a circular dependency when
1361 # we attempt to install virtualenv into a virualenv, so we must global
1362 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001363 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001364 local cmd_pip=$DEST/.venv/bin/pip
1365 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001366 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001367 local cmd_pip=$(get_pip_command)
1368 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001369 fi
1370
1371 # Mirror option not needed anymore because pypi has CDN available,
1372 # but it's useful in certain circumstances
1373 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001374 local pip_mirror_opt=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001375 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001376 pip_mirror_opt="--use-mirrors"
Dean Troyerdff49a22014-01-30 15:37:40 -06001377 fi
1378
1379 # pip < 1.4 has a bug where it will use an already existing build
1380 # directory unconditionally. Say an earlier component installs
1381 # foo v1.1; pip will have built foo's source in
1382 # /tmp/$USER-pip-build. Even if a later component specifies foo <
1383 # 1.1, the existing extracted build will be used and cause
1384 # confusing errors. By creating unique build directories we avoid
1385 # this problem. See https://github.com/pypa/pip/issues/709
1386 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1387
Sean Dague45917cc2014-02-24 16:09:14 -05001388 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001389 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001390 http_proxy=$http_proxy \
1391 https_proxy=$https_proxy \
1392 no_proxy=$no_proxy \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001393 $cmd_pip install --build=${pip_build_tmp} \
1394 $pip_mirror_opt $@ \
1395 && $sudo_pip rm -rf ${pip_build_tmp}
Dean Troyerdff49a22014-01-30 15:37:40 -06001396}
1397
Sean Dague099e5e32014-03-31 10:35:43 -04001398# this should be used if you want to install globally, all libraries should
1399# use this, especially *oslo* ones
1400function setup_install {
1401 local project_dir=$1
1402 setup_package_with_req_sync $project_dir
1403}
1404
1405# this should be used for projects which run services, like all services
1406function setup_develop {
1407 local project_dir=$1
1408 setup_package_with_req_sync $project_dir -e
1409}
1410
Dean Troyeraf616d92014-02-17 12:57:55 -06001411# ``pip install -e`` the package, which processes the dependencies
1412# using pip before running `setup.py develop`
1413#
1414# Updates the dependencies in project_dir from the
1415# openstack/requirements global list before installing anything.
1416#
1417# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1418# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001419function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001420 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001421 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001422
Dean Troyeraf616d92014-02-17 12:57:55 -06001423 # Don't update repo if local changes exist
1424 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001425 # ``errexit`` requires us to trap the exit code when the repo is changed
1426 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001427
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001428 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001429 (cd $REQUIREMENTS_DIR; \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001430 python update.py $project_dir)
Dean Troyeraf616d92014-02-17 12:57:55 -06001431 fi
1432
Sean Dague099e5e32014-03-31 10:35:43 -04001433 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001434
1435 # We've just gone and possibly modified the user's source tree in an
1436 # automated way, which is considered bad form if it's a development
1437 # tree because we've screwed up their next git checkin. So undo it.
1438 #
1439 # However... there are some circumstances, like running in the gate
1440 # where we really really want the overridden version to stick. So provide
1441 # a variable that tells us whether or not we should UNDO the requirements
1442 # changes (this will be set to False in the OpenStack ci gate)
1443 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001444 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001445 (cd $project_dir && git reset --hard)
1446 fi
1447 fi
1448}
1449
1450# ``pip install -e`` the package, which processes the dependencies
1451# using pip before running `setup.py develop`
1452# Uses globals ``STACK_USER``
1453# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001454function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001455 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001456 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001457
Sean Dague099e5e32014-03-31 10:35:43 -04001458 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001459 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001460 if [[ "$flags" == "-e" ]]; then
1461 safe_chown -R $STACK_USER $1/*.egg-info
1462 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001463}
1464
Dean Troyerdff49a22014-01-30 15:37:40 -06001465
1466# Service Functions
1467# =================
1468
1469# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1470# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001471function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001472 echo "$1" | sed -e '
1473 s/,,/,/g;
1474 s/^,//;
1475 s/,$//
1476 '
1477}
1478
1479# disable_all_services() removes all current services
1480# from ``ENABLED_SERVICES`` to reset the configuration
1481# before a minimal installation
1482# Uses global ``ENABLED_SERVICES``
1483# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001484function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001485 ENABLED_SERVICES=""
1486}
1487
1488# Remove all services starting with '-'. For example, to install all default
1489# services except rabbit (rabbit) set in ``localrc``:
1490# ENABLED_SERVICES+=",-rabbit"
1491# Uses global ``ENABLED_SERVICES``
1492# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001493function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001494 local tmpsvcs="${ENABLED_SERVICES}"
1495 local service
1496 for service in ${tmpsvcs//,/ }; do
1497 if [[ ${service} == -* ]]; then
1498 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1499 fi
1500 done
1501 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1502}
1503
1504# disable_service() removes the services passed as argument to the
1505# ``ENABLED_SERVICES`` list, if they are present.
1506#
1507# For example:
1508# disable_service rabbit
1509#
1510# This function does not know about the special cases
1511# for nova, glance, and neutron built into is_service_enabled().
1512# Uses global ``ENABLED_SERVICES``
1513# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001514function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001515 local tmpsvcs=",${ENABLED_SERVICES},"
1516 local service
1517 for service in $@; do
1518 if is_service_enabled $service; then
1519 tmpsvcs=${tmpsvcs//,$service,/,}
1520 fi
1521 done
1522 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1523}
1524
1525# enable_service() adds the services passed as argument to the
1526# ``ENABLED_SERVICES`` list, if they are not already present.
1527#
1528# For example:
1529# enable_service qpid
1530#
1531# This function does not know about the special cases
1532# for nova, glance, and neutron built into is_service_enabled().
1533# Uses global ``ENABLED_SERVICES``
1534# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001535function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001536 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001537 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001538 for service in $@; do
1539 if ! is_service_enabled $service; then
1540 tmpsvcs+=",$service"
1541 fi
1542 done
1543 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1544 disable_negated_services
1545}
1546
1547# is_service_enabled() checks if the service(s) specified as arguments are
1548# enabled by the user in ``ENABLED_SERVICES``.
1549#
1550# Multiple services specified as arguments are ``OR``'ed together; the test
1551# is a short-circuit boolean, i.e it returns on the first match.
1552#
1553# There are special cases for some 'catch-all' services::
1554# **nova** returns true if any service enabled start with **n-**
1555# **cinder** returns true if any service enabled start with **c-**
1556# **ceilometer** returns true if any service enabled start with **ceilometer**
1557# **glance** returns true if any service enabled start with **g-**
1558# **neutron** returns true if any service enabled start with **q-**
1559# **swift** returns true if any service enabled start with **s-**
1560# **trove** returns true if any service enabled start with **tr-**
1561# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1562# **s-** services will be enabled. This will be deprecated in the future.
1563#
1564# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1565# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1566# as enabled in this case.
1567#
1568# Uses global ``ENABLED_SERVICES``
1569# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001570function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001571 local xtrace=$(set +o | grep xtrace)
1572 set +o xtrace
1573 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001574 local services=$@
1575 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001576 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001577 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001578
1579 # Look for top-level 'enabled' function for this service
1580 if type is_${service}_enabled >/dev/null 2>&1; then
1581 # A function exists for this service, use it
1582 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001583 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001584 fi
1585
1586 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1587 # are implemented
1588
Sean Dague45917cc2014-02-24 16:09:14 -05001589 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
1590 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1591 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1592 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1593 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1594 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1595 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1596 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1597 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1598 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001599 done
Sean Dague45917cc2014-02-24 16:09:14 -05001600 $xtrace
1601 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001602}
1603
1604# Toggle enable/disable_service for services that must run exclusive of each other
1605# $1 The name of a variable containing a space-separated list of services
1606# $2 The name of a variable in which to store the enabled service's name
1607# $3 The name of the service to enable
1608function use_exclusive_service {
1609 local options=${!1}
1610 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001611 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001612 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001613 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001614 for opt in $options;do
1615 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1616 done
1617 eval "$out=$selection"
1618 return 0
1619}
1620
1621
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001622# System Functions
1623# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001624
1625# Only run the command if the target file (the last arg) is not on an
1626# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001627function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001628 local xtrace=$(set +o | grep xtrace)
1629 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001630 local args=( $@ )
1631 local last
1632 local sudo_cmd
1633 local dir_to_check
1634
1635 let last="${#args[*]} - 1"
1636
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001637 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001638 if [ ! -d "$dir_to_check" ]; then
1639 dir_to_check=`dirname "$dir_to_check"`
1640 fi
1641
1642 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001643 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001644 return 0
1645 fi
1646
1647 if [[ $TRACK_DEPENDS = True ]]; then
1648 sudo_cmd="env"
1649 else
1650 sudo_cmd="sudo"
1651 fi
1652
Sean Dague45917cc2014-02-24 16:09:14 -05001653 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001654 $sudo_cmd $@
1655}
1656
1657# Exit 0 if address is in network or 1 if address is not in network
1658# ip-range is in CIDR notation: 1.2.3.4/20
1659# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001660function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001661 local ip=$1
1662 local range=$2
1663 local masklen=${range#*/}
1664 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1665 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1666 [[ $network == $subnet ]]
1667}
1668
1669# Add a user to a group.
1670# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001671function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001672 local user=$1
1673 local group=$2
1674
1675 if [[ -z "$os_VENDOR" ]]; then
1676 GetOSVersion
1677 fi
1678
1679 # SLE11 and openSUSE 12.2 don't have the usual usermod
1680 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1681 sudo usermod -a -G "$group" "$user"
1682 else
1683 sudo usermod -A "$group" "$user"
1684 fi
1685}
1686
1687# Convert CIDR notation to a IPv4 netmask
1688# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001689function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001690 local maskpat="255 255 255 255"
1691 local maskdgt="254 252 248 240 224 192 128"
1692 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1693 echo ${1-0}.${2-0}.${3-0}.${4-0}
1694}
1695
1696# Gracefully cp only if source file/dir exists
1697# cp_it source destination
1698function cp_it {
1699 if [ -e $1 ] || [ -d $1 ]; then
1700 cp -pRL $1 $2
1701 fi
1702}
1703
1704# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1705# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1706# ``localrc`` or on the command line if necessary::
1707#
1708# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1709#
1710# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1711
Ian Wienandaee18c72014-02-21 15:35:08 +11001712function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001713 if [[ -n "$http_proxy" ]]; then
1714 export http_proxy=$http_proxy
1715 fi
1716 if [[ -n "$https_proxy" ]]; then
1717 export https_proxy=$https_proxy
1718 fi
1719 if [[ -n "$no_proxy" ]]; then
1720 export no_proxy=$no_proxy
1721 fi
1722}
1723
1724# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001725function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001726 local mount_type=`stat -f -L -c %T $1`
1727 test "$mount_type" == "nfs"
1728}
1729
1730# Return the network portion of the given IP address using netmask
1731# netmask is in the traditional dotted-quad format
1732# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001733function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001734 local ip=$1
1735 local mask=$2
1736 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1737 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1738 echo $subnet
1739}
1740
1741# Service wrapper to restart services
1742# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001743function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001744 if is_ubuntu; then
1745 sudo /usr/sbin/service $1 restart
1746 else
1747 sudo /sbin/service $1 restart
1748 fi
1749}
1750
1751# Only change permissions of a file or directory if it is not on an
1752# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001753function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001754 _safe_permission_operation chmod $@
1755}
1756
1757# Only change ownership of a file or directory if it is not on an NFS
1758# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001759function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001760 _safe_permission_operation chown $@
1761}
1762
1763# Service wrapper to start services
1764# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001765function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001766 if is_ubuntu; then
1767 sudo /usr/sbin/service $1 start
1768 else
1769 sudo /sbin/service $1 start
1770 fi
1771}
1772
1773# Service wrapper to stop services
1774# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001775function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001776 if is_ubuntu; then
1777 sudo /usr/sbin/service $1 stop
1778 else
1779 sudo /sbin/service $1 stop
1780 fi
1781}
1782
1783
1784# Restore xtrace
1785$XTRACE
1786
1787# Local variables:
1788# mode: shell-script
1789# End: