blob: 13bc1cf558749b94b8aee14a0bc5d2ce0c4a0822 [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
52 local values="$(iniget_multiline $file $section $option) $@"
53 iniset_multiline $file $section $option $values
Sean Dague45917cc2014-02-24 16:09:14 -050054 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060055}
56
57# Comment an option in an INI file
58# inicomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110059function inicomment {
Sean Dague45917cc2014-02-24 16:09:14 -050060 local xtrace=$(set +o | grep xtrace)
61 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060062 local file=$1
63 local section=$2
64 local option=$3
65 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -050066 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060067}
68
69# Get an option from an INI file
70# iniget config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110071function iniget {
Sean Dague45917cc2014-02-24 16:09:14 -050072 local xtrace=$(set +o | grep xtrace)
73 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060074 local file=$1
75 local section=$2
76 local option=$3
77 local line
78 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
79 echo ${line#*=}
Sean Dague45917cc2014-02-24 16:09:14 -050080 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060081}
82
83# Get a multiple line option from an INI file
84# iniget_multiline config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110085function iniget_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -050086 local xtrace=$(set +o | grep xtrace)
87 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060088 local file=$1
89 local section=$2
90 local option=$3
91 local values
92 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
93 echo ${values}
Sean Dague45917cc2014-02-24 16:09:14 -050094 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060095}
96
97# Determinate is the given option present in the INI file
98# ini_has_option config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110099function ini_has_option {
Sean Dague45917cc2014-02-24 16:09:14 -0500100 local xtrace=$(set +o | grep xtrace)
101 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600102 local file=$1
103 local section=$2
104 local option=$3
105 local line
106 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Sean Dague45917cc2014-02-24 16:09:14 -0500107 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600108 [ -n "$line" ]
109}
110
111# Set an option in an INI file
112# iniset config-file section option value
Ian Wienandaee18c72014-02-21 15:35:08 +1100113function iniset {
Sean Dague45917cc2014-02-24 16:09:14 -0500114 local xtrace=$(set +o | grep xtrace)
115 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600116 local file=$1
117 local section=$2
118 local option=$3
119 local value=$4
120
121 [[ -z $section || -z $option ]] && return
122
123 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
124 # Add section at the end
125 echo -e "\n[$section]" >>"$file"
126 fi
127 if ! ini_has_option "$file" "$section" "$option"; then
128 # Add it
129 sed -i -e "/^\[$section\]/ a\\
130$option = $value
131" "$file"
132 else
133 local sep=$(echo -ne "\x01")
134 # Replace it
135 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
136 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500137 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600138}
139
140# Set a multiple line option in an INI file
141# iniset_multiline config-file section option value1 value2 valu3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +1100142function iniset_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -0500143 local xtrace=$(set +o | grep xtrace)
144 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600145 local file=$1
146 local section=$2
147 local option=$3
148 shift 3
149 local values
150 for v in $@; do
151 # The later sed command inserts each new value in the line next to
152 # the section identifier, which causes the values to be inserted in
153 # the reverse order. Do a reverse here to keep the original order.
154 values="$v ${values}"
155 done
156 if ! grep -q "^\[$section\]" "$file"; then
157 # Add section at the end
158 echo -e "\n[$section]" >>"$file"
159 else
160 # Remove old values
161 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
162 fi
163 # Add new ones
164 for v in $values; do
165 sed -i -e "/^\[$section\]/ a\\
166$option = $v
167" "$file"
168 done
Sean Dague45917cc2014-02-24 16:09:14 -0500169 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600170}
171
172# Uncomment an option in an INI file
173# iniuncomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100174function iniuncomment {
Sean Dague45917cc2014-02-24 16:09:14 -0500175 local xtrace=$(set +o | grep xtrace)
176 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600177 local file=$1
178 local section=$2
179 local option=$3
180 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500181 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600182}
183
184# Normalize config values to True or False
185# Accepts as False: 0 no No NO false False FALSE
186# Accepts as True: 1 yes Yes YES true True TRUE
187# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100188function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500189 local xtrace=$(set +o | grep xtrace)
190 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600191 local default=$1
192 local testval=$2
193
194 [[ -z "$testval" ]] && { echo "$default"; return; }
195 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
196 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
197 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500198 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600199}
200
201
202# Control Functions
203# =================
204
205# Prints backtrace info
206# filename:lineno:function
207# backtrace level
208function backtrace {
209 local level=$1
210 local deep=$((${#BASH_SOURCE[@]} - 1))
211 echo "[Call Trace]"
212 while [ $level -le $deep ]; do
213 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
214 deep=$((deep - 1))
215 done
216}
217
218# Prints line number and "message" then exits
219# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100220function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600221 local exitcode=$?
222 set +o xtrace
223 local line=$1; shift
224 if [ $exitcode == 0 ]; then
225 exitcode=1
226 fi
227 backtrace 2
228 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600229 # Give buffers a second to flush
230 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600231 exit $exitcode
232}
233
234# Checks an environment variable is not set or has length 0 OR if the
235# exit code is non-zero and prints "message" and exits
236# NOTE: env-var is the variable name without a '$'
237# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100238function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600239 local exitcode=$?
240 FXTRACE=$(set +o | grep xtrace)
241 set +o xtrace
242 local line=$1; shift
243 local evar=$1; shift
244 if ! is_set $evar || [ $exitcode != 0 ]; then
245 die $line "$*"
246 fi
247 $FXTRACE
248}
249
250# Prints line number and "message" in error format
251# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100252function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600253 local exitcode=$?
254 errXTRACE=$(set +o | grep xtrace)
255 set +o xtrace
256 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
257 echo $msg 1>&2;
258 if [[ -n ${SCREEN_LOGDIR} ]]; then
259 echo $msg >> "${SCREEN_LOGDIR}/error.log"
260 fi
261 $errXTRACE
262 return $exitcode
263}
264
265# Checks an environment variable is not set or has length 0 OR if the
266# exit code is non-zero and prints "message"
267# NOTE: env-var is the variable name without a '$'
268# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100269function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600270 local exitcode=$?
271 errinsXTRACE=$(set +o | grep xtrace)
272 set +o xtrace
273 local line=$1; shift
274 local evar=$1; shift
275 if ! is_set $evar || [ $exitcode != 0 ]; then
276 err $line "$*"
277 fi
278 $errinsXTRACE
279 return $exitcode
280}
281
282# Exit after outputting a message about the distribution not being supported.
283# exit_distro_not_supported [optional-string-telling-what-is-missing]
284function exit_distro_not_supported {
285 if [[ -z "$DISTRO" ]]; then
286 GetDistro
287 fi
288
289 if [ $# -gt 0 ]; then
290 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
291 else
292 die $LINENO "Support for $DISTRO is incomplete."
293 fi
294}
295
296# Test if the named environment variable is set and not zero length
297# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100298function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600299 local var=\$"$1"
300 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
301}
302
303# Prints line number and "message" in warning format
304# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100305function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600306 local exitcode=$?
307 errXTRACE=$(set +o | grep xtrace)
308 set +o xtrace
309 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
310 echo $msg 1>&2;
311 if [[ -n ${SCREEN_LOGDIR} ]]; then
312 echo $msg >> "${SCREEN_LOGDIR}/error.log"
313 fi
314 $errXTRACE
315 return $exitcode
316}
317
318
319# Distro Functions
320# ================
321
322# Determine OS Vendor, Release and Update
323# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
324# Returns results in global variables:
325# os_VENDOR - vendor name
326# os_RELEASE - release
327# os_UPDATE - update
328# os_PACKAGE - package type
329# os_CODENAME - vendor's codename for release
330# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100331function GetOSVersion {
Dean Troyerdff49a22014-01-30 15:37:40 -0600332 # Figure out which vendor we are
333 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
334 # OS/X
335 os_VENDOR=`sw_vers -productName`
336 os_RELEASE=`sw_vers -productVersion`
337 os_UPDATE=${os_RELEASE##*.}
338 os_RELEASE=${os_RELEASE%.*}
339 os_PACKAGE=""
340 if [[ "$os_RELEASE" =~ "10.7" ]]; then
341 os_CODENAME="lion"
342 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
343 os_CODENAME="snow leopard"
344 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
345 os_CODENAME="leopard"
346 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
347 os_CODENAME="tiger"
348 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
349 os_CODENAME="panther"
350 else
351 os_CODENAME=""
352 fi
353 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
354 os_VENDOR=$(lsb_release -i -s)
355 os_RELEASE=$(lsb_release -r -s)
356 os_UPDATE=""
357 os_PACKAGE="rpm"
358 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
359 os_PACKAGE="deb"
360 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
361 lsb_release -d -s | grep -q openSUSE
362 if [[ $? -eq 0 ]]; then
363 os_VENDOR="openSUSE"
364 fi
365 elif [[ $os_VENDOR == "openSUSE project" ]]; then
366 os_VENDOR="openSUSE"
367 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
368 os_VENDOR="Red Hat"
369 fi
370 os_CODENAME=$(lsb_release -c -s)
371 elif [[ -r /etc/redhat-release ]]; then
372 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
373 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
374 # CentOS release 5.5 (Final)
375 # CentOS Linux release 6.0 (Final)
376 # Fedora release 16 (Verne)
377 # XenServer release 6.2.0-70446c (xenenterprise)
378 os_CODENAME=""
379 for r in "Red Hat" CentOS Fedora XenServer; do
380 os_VENDOR=$r
381 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
382 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
383 os_CODENAME=${ver#*|}
384 os_RELEASE=${ver%|*}
385 os_UPDATE=${os_RELEASE##*.}
386 os_RELEASE=${os_RELEASE%.*}
387 break
388 fi
389 os_VENDOR=""
390 done
391 os_PACKAGE="rpm"
392 elif [[ -r /etc/SuSE-release ]]; then
393 for r in openSUSE "SUSE Linux"; do
394 if [[ "$r" = "SUSE Linux" ]]; then
395 os_VENDOR="SUSE LINUX"
396 else
397 os_VENDOR=$r
398 fi
399
400 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
401 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
402 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
403 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
404 break
405 fi
406 os_VENDOR=""
407 done
408 os_PACKAGE="rpm"
409 # If lsb_release is not installed, we should be able to detect Debian OS
410 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
411 os_VENDOR="Debian"
412 os_PACKAGE="deb"
413 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
414 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
415 fi
416 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
417}
418
419# Translate the OS version values into common nomenclature
420# Sets global ``DISTRO`` from the ``os_*`` values
Ian Wienandaee18c72014-02-21 15:35:08 +1100421function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600422 GetOSVersion
423 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
424 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
425 DISTRO=$os_CODENAME
426 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
427 # For Fedora, just use 'f' and the release
428 DISTRO="f$os_RELEASE"
429 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
430 DISTRO="opensuse-$os_RELEASE"
431 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
432 # For SLE, also use the service pack
433 if [[ -z "$os_UPDATE" ]]; then
434 DISTRO="sle${os_RELEASE}"
435 else
436 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
437 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530438 elif [[ "$os_VENDOR" =~ (Red Hat) || \
439 "$os_VENDOR" =~ (CentOS) || \
440 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600441 # Drop the . release as we assume it's compatible
442 DISTRO="rhel${os_RELEASE::1}"
443 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
444 DISTRO="xs$os_RELEASE"
445 else
446 # Catch-all for now is Vendor + Release + Update
447 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
448 fi
449 export DISTRO
450}
451
452# Utility function for checking machine architecture
453# is_arch arch-type
454function is_arch {
455 ARCH_TYPE=$1
456
457 [[ "$(uname -m)" == "$ARCH_TYPE" ]]
458}
459
460# Determine if current distribution is a Fedora-based distribution
461# (Fedora, RHEL, CentOS, etc).
462# is_fedora
463function is_fedora {
464 if [[ -z "$os_VENDOR" ]]; then
465 GetOSVersion
466 fi
467
anju Tiwari6c639c92014-07-15 18:11:54 +0530468 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
469 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600470}
471
472
473# Determine if current distribution is a SUSE-based distribution
474# (openSUSE, SLE).
475# is_suse
476function is_suse {
477 if [[ -z "$os_VENDOR" ]]; then
478 GetOSVersion
479 fi
480
481 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
482}
483
484
485# Determine if current distribution is an Ubuntu-based distribution
486# It will also detect non-Ubuntu but Debian-based distros
487# is_ubuntu
488function is_ubuntu {
489 if [[ -z "$os_PACKAGE" ]]; then
490 GetOSVersion
491 fi
492 [ "$os_PACKAGE" = "deb" ]
493}
494
495
496# Git Functions
497# =============
498
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600499# Returns openstack release name for a given branch name
500# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100501function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600502 local branch=$1
Dean Troyer50cda692014-07-25 11:57:20 -0500503
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600504 if [[ $branch =~ "stable/" ]]; then
505 echo ${branch#*/}
506 else
507 echo "master"
508 fi
509}
510
Dean Troyerdff49a22014-01-30 15:37:40 -0600511# git clone only if directory doesn't exist already. Since ``DEST`` might not
512# be owned by the installation user, we create the directory and change the
513# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500514# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
515# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600516# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer50cda692014-07-25 11:57:20 -0500517# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600518# git_clone remote dest-dir branch
519function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500520 local git_remote=$1
521 local git_dest=$2
522 local git_ref=$3
523 local orig_dir=$(pwd)
524
Dean Troyerdff49a22014-01-30 15:37:40 -0600525 RECLONE=$(trueorfalse False $RECLONE)
526
527 if [[ "$OFFLINE" = "True" ]]; then
528 echo "Running in offline mode, clones already exist"
529 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500530 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600531 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400532 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600533 return
534 fi
535
Dean Troyer50cda692014-07-25 11:57:20 -0500536 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600537 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500538 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600539 [[ "$ERROR_ON_CLONE" = "True" ]] && \
540 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer50cda692014-07-25 11:57:20 -0500541 git_timed clone $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600542 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500543 cd $git_dest
544 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600545 else
546 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500547 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600548 [[ "$ERROR_ON_CLONE" = "True" ]] && \
549 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer50cda692014-07-25 11:57:20 -0500550 git_timed clone $git_remote $git_dest
551 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600552 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500553 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600554 elif [[ "$RECLONE" = "True" ]]; then
555 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500556 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600557 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500558 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100559 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600560 # remove the existing ignored files (like pyc) as they cause breakage
561 # (due to the py files having older timestamps than our pyc, so python
562 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500563 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600564
Dean Troyer50cda692014-07-25 11:57:20 -0500565 # handle git_ref accordingly to type (tag, branch)
566 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
567 git_update_tag $git_ref
568 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
569 git_update_branch $git_ref
570 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
571 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600572 else
Dean Troyer50cda692014-07-25 11:57:20 -0500573 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600574 fi
575
576 fi
577 fi
578
579 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500580 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600581 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400582 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600583}
584
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100585# git can sometimes get itself infinitely stuck with transient network
586# errors or other issues with the remote end. This wraps git in a
587# timeout/retry loop and is intended to watch over non-local git
588# processes that might hang. GIT_TIMEOUT, if set, is passed directly
589# to timeout(1); otherwise the default value of 0 maintains the status
590# quo of waiting forever.
591# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100592function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100593 local count=0
594 local timeout=0
595
596 if [[ -n "${GIT_TIMEOUT}" ]]; then
597 timeout=${GIT_TIMEOUT}
598 fi
599
600 until timeout -s SIGINT ${timeout} git "$@"; do
601 # 124 is timeout(1)'s special return code when it reached the
602 # timeout; otherwise assume fatal failure
603 if [[ $? -ne 124 ]]; then
604 die $LINENO "git call failed: [git $@]"
605 fi
606
607 count=$(($count + 1))
608 warn "timeout ${count} for git call: [git $@]"
609 if [ $count -eq 3 ]; then
610 die $LINENO "Maximum of 3 git retries reached"
611 fi
612 sleep 5
613 done
614}
615
Dean Troyerdff49a22014-01-30 15:37:40 -0600616# git update using reference as a branch.
617# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100618function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500619 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600620
Dean Troyer50cda692014-07-25 11:57:20 -0500621 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600622 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500623 git branch -D $git_branch || true
624 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600625}
626
627# git update using reference as a branch.
628# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100629function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500630 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600631
Dean Troyer50cda692014-07-25 11:57:20 -0500632 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600633}
634
635# git update using reference as a tag. Be careful editing source at that repo
636# as working copy will be in a detached mode
637# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100638function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500639 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600640
Dean Troyer50cda692014-07-25 11:57:20 -0500641 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600642 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500643 git_timed fetch origin tag $git_tag
644 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600645}
646
647
648# OpenStack Functions
649# ===================
650
651# Get the default value for HOST_IP
652# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100653function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600654 local fixed_range=$1
655 local floating_range=$2
656 local host_ip_iface=$3
657 local host_ip=$4
658
659 # Find the interface used for the default route
660 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
661 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
662 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
663 host_ip=""
664 host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}'`
665 for IP in $host_ips; do
666 # Attempt to filter out IP addresses that are part of the fixed and
667 # floating range. Note that this method only works if the ``netaddr``
668 # python library is installed. If it is not installed, an error
669 # will be printed and the first IP from the interface will be used.
670 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
671 # address.
672 if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then
673 host_ip=$IP
674 break;
675 fi
676 done
677 fi
678 echo $host_ip
679}
680
681# Grab a numbered field from python prettytable output
682# Fields are numbered starting with 1
683# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
684# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100685function get_field {
Dean Troyerdff49a22014-01-30 15:37:40 -0600686 while read data; do
687 if [ "$1" -lt 0 ]; then
688 field="(\$(NF$1))"
689 else
690 field="\$$(($1 + 1))"
691 fi
692 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
693 done
694}
695
696# Add a policy to a policy.json file
697# Do nothing if the policy already exists
698# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100699function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600700 local policy_file=$1
701 local policy_name=$2
702 local policy_perm=$3
703
704 if grep -q ${policy_name} ${policy_file}; then
705 echo "Policy ${policy_name} already exists in ${policy_file}"
706 return
707 fi
708
709 # Add a terminating comma to policy lines without one
710 # Remove the closing '}' and all lines following to the end-of-file
711 local tmpfile=$(mktemp)
712 uniq ${policy_file} | sed -e '
713 s/]$/],/
714 /^[}]/,$d
715 ' > ${tmpfile}
716
717 # Append policy and closing brace
718 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
719 echo "}" >>${tmpfile}
720
721 mv ${tmpfile} ${policy_file}
722}
723
Bartosz Górski0abde392014-02-28 14:15:19 +0100724# Gets or creates user
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200725# Usage: get_or_create_user <username> <password> <project> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100726function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200727 if [[ ! -z "$4" ]]; then
728 local EMAIL="--email=$4"
729 else
730 local EMAIL=""
731 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100732 # Gets user id
733 USER_ID=$(
734 # Gets user id
735 openstack user show $1 -f value -c id 2>/dev/null ||
736 # Creates new user
737 openstack user create \
738 $1 \
739 --password "$2" \
740 --project $3 \
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200741 $EMAIL \
Bartosz Górski0abde392014-02-28 14:15:19 +0100742 -f value -c id
743 )
744 echo $USER_ID
745}
746
747# Gets or creates project
748# Usage: get_or_create_project <name>
749function get_or_create_project {
750 # Gets project id
751 PROJECT_ID=$(
752 # Gets project id
753 openstack project show $1 -f value -c id 2>/dev/null ||
754 # Creates new project if not exists
755 openstack project create $1 -f value -c id
756 )
757 echo $PROJECT_ID
758}
759
760# Gets or creates role
761# Usage: get_or_create_role <name>
762function get_or_create_role {
763 ROLE_ID=$(
764 # Gets role id
765 openstack role show $1 -f value -c id 2>/dev/null ||
766 # Creates role if not exists
767 openstack role create $1 -f value -c id
768 )
769 echo $ROLE_ID
770}
771
772# Gets or adds user role
773# Usage: get_or_add_user_role <role> <user> <project>
774function get_or_add_user_role {
775 # Gets user role id
776 USER_ROLE_ID=$(openstack user role list \
777 $2 \
778 --project $3 \
779 --column "ID" \
780 --column "Name" \
781 | grep " $1 " | get_field 1)
782 if [[ -z "$USER_ROLE_ID" ]]; then
783 # Adds role to user
784 USER_ROLE_ID=$(openstack role add \
785 $1 \
786 --user $2 \
787 --project $3 \
788 | grep " id " | get_field 2)
789 fi
790 echo $USER_ROLE_ID
791}
792
793# Gets or creates service
794# Usage: get_or_create_service <name> <type> <description>
795function get_or_create_service {
796 # Gets service id
797 SERVICE_ID=$(
798 # Gets service id
799 openstack service show $1 -f value -c id 2>/dev/null ||
800 # Creates new service if not exists
801 openstack service create \
802 $1 \
803 --type=$2 \
804 --description="$3" \
805 -f value -c id
806 )
807 echo $SERVICE_ID
808}
809
810# Gets or creates endpoint
811# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
812function get_or_create_endpoint {
813 # Gets endpoint id
814 ENDPOINT_ID=$(openstack endpoint list \
815 --column "ID" \
816 --column "Region" \
817 --column "Service Name" \
818 | grep " $2 " \
819 | grep " $1 " | get_field 1)
820 if [[ -z "$ENDPOINT_ID" ]]; then
821 # Creates new endpoint
822 ENDPOINT_ID=$(openstack endpoint create \
823 $1 \
824 --region $2 \
825 --publicurl $3 \
826 --adminurl $4 \
827 --internalurl $5 \
828 | grep " id " | get_field 2)
829 fi
830 echo $ENDPOINT_ID
831}
Dean Troyerdff49a22014-01-30 15:37:40 -0600832
833# Package Functions
834# =================
835
836# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100837function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600838 local pkg_dir
839 if is_ubuntu; then
840 pkg_dir=$FILES/apts
841 elif is_fedora; then
842 pkg_dir=$FILES/rpms
843 elif is_suse; then
844 pkg_dir=$FILES/rpms-suse
845 else
846 exit_distro_not_supported "list of packages"
847 fi
848 echo "$pkg_dir"
849}
850
851# Wrapper for ``apt-get`` to set cache and proxy environment variables
852# Uses globals ``OFFLINE``, ``*_proxy``
853# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100854function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500855 local xtrace=$(set +o | grep xtrace)
856 set +o xtrace
857
Dean Troyerdff49a22014-01-30 15:37:40 -0600858 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
859 local sudo="sudo"
860 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500861
862 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600863 $sudo DEBIAN_FRONTEND=noninteractive \
864 http_proxy=$http_proxy https_proxy=$https_proxy \
865 no_proxy=$no_proxy \
866 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
867}
868
869# get_packages() collects a list of package names of any type from the
870# prerequisite files in ``files/{apts|rpms}``. The list is intended
871# to be passed to a package installer such as apt or yum.
872#
873# Only packages required for the services in 1st argument will be
874# included. Two bits of metadata are recognized in the prerequisite files:
875#
876# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
877# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
878# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100879function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500880 local xtrace=$(set +o | grep xtrace)
881 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600882 local services=$@
883 local package_dir=$(_get_package_dir)
884 local file_to_parse
885 local service
886
887 if [[ -z "$package_dir" ]]; then
888 echo "No package directory supplied"
889 return 1
890 fi
891 if [[ -z "$DISTRO" ]]; then
892 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -0500893 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -0600894 fi
895 for service in ${services//,/ }; do
896 # Allow individual services to specify dependencies
897 if [[ -e ${package_dir}/${service} ]]; then
898 file_to_parse="${file_to_parse} $service"
899 fi
900 # NOTE(sdague) n-api needs glance for now because that's where
901 # glance client is
902 if [[ $service == n-api ]]; then
903 if [[ ! $file_to_parse =~ nova ]]; then
904 file_to_parse="${file_to_parse} nova"
905 fi
906 if [[ ! $file_to_parse =~ glance ]]; then
907 file_to_parse="${file_to_parse} glance"
908 fi
909 elif [[ $service == c-* ]]; then
910 if [[ ! $file_to_parse =~ cinder ]]; then
911 file_to_parse="${file_to_parse} cinder"
912 fi
913 elif [[ $service == ceilometer-* ]]; then
914 if [[ ! $file_to_parse =~ ceilometer ]]; then
915 file_to_parse="${file_to_parse} ceilometer"
916 fi
917 elif [[ $service == s-* ]]; then
918 if [[ ! $file_to_parse =~ swift ]]; then
919 file_to_parse="${file_to_parse} swift"
920 fi
921 elif [[ $service == n-* ]]; then
922 if [[ ! $file_to_parse =~ nova ]]; then
923 file_to_parse="${file_to_parse} nova"
924 fi
925 elif [[ $service == g-* ]]; then
926 if [[ ! $file_to_parse =~ glance ]]; then
927 file_to_parse="${file_to_parse} glance"
928 fi
929 elif [[ $service == key* ]]; then
930 if [[ ! $file_to_parse =~ keystone ]]; then
931 file_to_parse="${file_to_parse} keystone"
932 fi
933 elif [[ $service == q-* ]]; then
934 if [[ ! $file_to_parse =~ neutron ]]; then
935 file_to_parse="${file_to_parse} neutron"
936 fi
Adam Gandelman539ec432014-03-18 18:57:43 -0700937 elif [[ $service == ir-* ]]; then
938 if [[ ! $file_to_parse =~ ironic ]]; then
939 file_to_parse="${file_to_parse} ironic"
940 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600941 fi
942 done
943
944 for file in ${file_to_parse}; do
945 local fname=${package_dir}/${file}
946 local OIFS line package distros distro
947 [[ -e $fname ]] || continue
948
949 OIFS=$IFS
950 IFS=$'\n'
951 for line in $(<${fname}); do
952 if [[ $line =~ "NOPRIME" ]]; then
953 continue
954 fi
955
956 # Assume we want this package
957 package=${line%#*}
958 inst_pkg=1
959
960 # Look for # dist:xxx in comment
961 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
962 # We are using BASH regexp matching feature.
963 package=${BASH_REMATCH[1]}
964 distros=${BASH_REMATCH[2]}
965 # In bash ${VAR,,} will lowecase VAR
966 # Look for a match in the distro list
967 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
968 # If no match then skip this package
969 inst_pkg=0
970 fi
971 fi
972
973 # Look for # testonly in comment
974 if [[ $line =~ (.*)#.*testonly.* ]]; then
975 package=${BASH_REMATCH[1]}
976 # Are we installing test packages? (test for the default value)
977 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
978 # If not installing test packages the skip this package
979 inst_pkg=0
980 fi
981 fi
982
983 if [[ $inst_pkg = 1 ]]; then
984 echo $package
985 fi
986 done
987 IFS=$OIFS
988 done
Sean Dague45917cc2014-02-24 16:09:14 -0500989 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600990}
991
992# Distro-agnostic package installer
993# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400994function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -0700995 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400996 return 0
997 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600998
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400999 if is_ubuntu; then
1000 local xtrace=$(set +o | grep xtrace)
1001 set +o xtrace
1002 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1003 # if there are transient errors pulling the updates, that's fine.
1004 # It may be secondary repositories that we don't really care about.
1005 apt_get update || /bin/true
1006 REPOS_UPDATED=True
1007 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001008 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001009 fi
1010}
1011
1012function real_install_package {
1013 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001014 apt_get install "$@"
1015 elif is_fedora; then
1016 yum_install "$@"
1017 elif is_suse; then
1018 zypper_install "$@"
1019 else
1020 exit_distro_not_supported "installing packages"
1021 fi
1022}
1023
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001024# Distro-agnostic package installer
1025# install_package package [package ...]
1026function install_package {
1027 update_package_repo
1028 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1029}
1030
Dean Troyerdff49a22014-01-30 15:37:40 -06001031# Distro-agnostic function to tell if a package is installed
1032# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001033function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001034 if [[ -z "$@" ]]; then
1035 return 1
1036 fi
1037
1038 if [[ -z "$os_PACKAGE" ]]; then
1039 GetOSVersion
1040 fi
1041
1042 if [[ "$os_PACKAGE" = "deb" ]]; then
1043 dpkg -s "$@" > /dev/null 2> /dev/null
1044 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1045 rpm --quiet -q "$@"
1046 else
1047 exit_distro_not_supported "finding if a package is installed"
1048 fi
1049}
1050
1051# Distro-agnostic package uninstaller
1052# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001053function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001054 if is_ubuntu; then
1055 apt_get purge "$@"
1056 elif is_fedora; then
1057 sudo yum remove -y "$@"
1058 elif is_suse; then
1059 sudo zypper rm "$@"
1060 else
1061 exit_distro_not_supported "uninstalling packages"
1062 fi
1063}
1064
1065# Wrapper for ``yum`` to set proxy environment variables
1066# Uses globals ``OFFLINE``, ``*_proxy``
1067# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001068function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001069 [[ "$OFFLINE" = "True" ]] && return
1070 local sudo="sudo"
1071 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001072
1073 # The manual check for missing packages is because yum -y assumes
1074 # missing packages are OK. See
1075 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001076 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1077 no_proxy=$no_proxy \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001078 yum install -y "$@" 2>&1 | \
1079 awk '
1080 BEGIN { fail=0 }
1081 /No package/ { fail=1 }
1082 { print }
1083 END { exit fail }' || \
1084 die $LINENO "Missing packages detected"
1085
1086 # also ensure we catch a yum failure
1087 if [[ ${PIPESTATUS[0]} != 0 ]]; then
1088 die $LINENO "Yum install failure"
1089 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001090}
1091
1092# zypper wrapper to set arguments correctly
1093# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001094function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001095 [[ "$OFFLINE" = "True" ]] && return
1096 local sudo="sudo"
1097 [[ "$(id -u)" = "0" ]] && sudo="env"
1098 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1099 zypper --non-interactive install --auto-agree-with-licenses "$@"
1100}
1101
1102
1103# Process Functions
1104# =================
1105
1106# _run_process() is designed to be backgrounded by run_process() to simulate a
1107# fork. It includes the dirty work of closing extra filehandles and preparing log
1108# files to produce the same logs as screen_it(). The log filename is derived
1109# from the service name and global-and-now-misnamed SCREEN_LOGDIR
1110# _run_process service "command-line"
Ian Wienandaee18c72014-02-21 15:35:08 +11001111function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001112 local service=$1
1113 local command="$2"
1114
1115 # Undo logging redirections and close the extra descriptors
1116 exec 1>&3
1117 exec 2>&3
1118 exec 3>&-
1119 exec 6>&-
1120
1121 if [[ -n ${SCREEN_LOGDIR} ]]; then
1122 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1123 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1124
1125 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1126 export PYTHONUNBUFFERED=1
1127 fi
1128
1129 exec /bin/bash -c "$command"
1130 die "$service exec failure: $command"
1131}
1132
1133# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1134# This is used for ``service_check`` when all the ``screen_it`` are called finished
1135# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001136function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001137 SCREEN_NAME=${SCREEN_NAME:-stack}
1138 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1139
1140 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1141 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1142 fi
1143
1144 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1145}
1146
1147# Find out if a process exists by partial name.
1148# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001149function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001150 local name=$1
1151 ps auxw | grep -v grep | grep ${name} > /dev/null
1152 RC=$?
1153 # some times I really hate bash reverse binary logic
1154 return $RC
1155}
1156
1157# run_process() launches a child process that closes all file descriptors and
1158# then exec's the passed in command. This is meant to duplicate the semantics
1159# of screen_it() without screen. PIDs are written to
1160# $SERVICE_DIR/$SCREEN_NAME/$service.pid
1161# run_process service "command-line"
Ian Wienandaee18c72014-02-21 15:35:08 +11001162function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001163 local service=$1
1164 local command="$2"
1165
1166 # Spawn the child process
1167 _run_process "$service" "$command" &
1168 echo $!
1169}
1170
1171# Helper to launch a service in a named screen
1172# screen_it service "command-line"
1173function screen_it {
Sean Dagueea22a4f2014-06-27 15:21:41 -04001174 SCREEN_NAME=${SCREEN_NAME:-stack}
1175 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1176 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001177
1178 if is_service_enabled $1; then
1179 # Append the service to the screen rc file
Sean Dagueea22a4f2014-06-27 15:21:41 -04001180 screen_rc "$1" "$2"
Dean Troyerdff49a22014-01-30 15:37:40 -06001181
Sean Dagueea22a4f2014-06-27 15:21:41 -04001182 if [[ "$USE_SCREEN" = "True" ]]; then
1183 screen -S $SCREEN_NAME -X screen -t $1
Dean Troyerdff49a22014-01-30 15:37:40 -06001184
Sean Dagueea22a4f2014-06-27 15:21:41 -04001185 if [[ -n ${SCREEN_LOGDIR} ]]; then
1186 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
1187 screen -S $SCREEN_NAME -p $1 -X log on
1188 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1189 fi
1190
1191 # sleep to allow bash to be ready to be send the command - we are
1192 # creating a new window in screen and then sends characters, so if
1193 # bash isn't running by the time we send the command, nothing happens
1194 sleep 3
1195
1196 NL=`echo -ne '\015'`
1197 # This fun command does the following:
1198 # - the passed server command is backgrounded
1199 # - the pid of the background process is saved in the usual place
1200 # - the server process is brought back to the foreground
1201 # - if the server process exits prematurely the fg command errors
1202 # and a message is written to stdout and the service failure file
1203 # The pid saved can be used in screen_stop() as a process group
1204 # id to kill off all child processes
1205 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 -06001206 else
1207 # Spawn directly without screen
Sean Dagueea22a4f2014-06-27 15:21:41 -04001208 run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001209 fi
1210 fi
1211}
1212
1213# Screen rc file builder
1214# screen_rc service "command-line"
1215function screen_rc {
1216 SCREEN_NAME=${SCREEN_NAME:-stack}
1217 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1218 if [[ ! -e $SCREENRC ]]; then
1219 # Name the screen session
1220 echo "sessionname $SCREEN_NAME" > $SCREENRC
1221 # Set a reasonable statusbar
1222 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1223 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1224 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1225 echo "screen -t shell bash" >> $SCREENRC
1226 fi
1227 # If this service doesn't already exist in the screenrc file
1228 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1229 NL=`echo -ne '\015'`
1230 echo "screen -t $1 bash" >> $SCREENRC
1231 echo "stuff \"$2$NL\"" >> $SCREENRC
1232
1233 if [[ -n ${SCREEN_LOGDIR} ]]; then
1234 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1235 echo "log on" >>$SCREENRC
1236 fi
1237 fi
1238}
1239
1240# Stop a service in screen
1241# If a PID is available use it, kill the whole process group via TERM
1242# If screen is being used kill the screen window; this will catch processes
1243# that did not leave a PID behind
1244# screen_stop service
Ian Wienandaee18c72014-02-21 15:35:08 +11001245function screen_stop {
Dean Troyerdff49a22014-01-30 15:37:40 -06001246 SCREEN_NAME=${SCREEN_NAME:-stack}
1247 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1248 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1249
1250 if is_service_enabled $1; then
1251 # Kill via pid if we have one available
1252 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$1.pid ]]; then
1253 pkill -TERM -P -$(cat $SERVICE_DIR/$SCREEN_NAME/$1.pid)
1254 rm $SERVICE_DIR/$SCREEN_NAME/$1.pid
1255 fi
1256 if [[ "$USE_SCREEN" = "True" ]]; then
1257 # Clean up the screen window
1258 screen -S $SCREEN_NAME -p $1 -X kill
1259 fi
1260 fi
1261}
1262
1263# Helper to get the status of each running service
1264# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001265function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001266 local service
1267 local failures
1268 SCREEN_NAME=${SCREEN_NAME:-stack}
1269 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1270
1271
1272 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1273 echo "No service status directory found"
1274 return
1275 fi
1276
1277 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001278 # make this -o errexit safe
1279 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001280
1281 for service in $failures; do
1282 service=`basename $service`
1283 service=${service%.failure}
1284 echo "Error: Service $service is not running"
1285 done
1286
1287 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001288 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001289 fi
1290}
1291
1292
1293# Python Functions
1294# ================
1295
1296# Get the path to the pip command.
1297# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001298function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001299 which pip || which pip-python
1300
1301 if [ $? -ne 0 ]; then
1302 die $LINENO "Unable to find pip; cannot continue"
1303 fi
1304}
1305
1306# Get the path to the direcotry where python executables are installed.
1307# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001308function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001309 if is_fedora || is_suse; then
1310 echo "/usr/bin"
1311 else
1312 echo "/usr/local/bin"
1313 fi
1314}
1315
1316# Wrapper for ``pip install`` to set cache and proxy environment variables
1317# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
1318# ``TRACK_DEPENDS``, ``*_proxy``
1319# pip_install package [package ...]
1320function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001321 local xtrace=$(set +o | grep xtrace)
1322 set +o xtrace
1323 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1324 $xtrace
1325 return
1326 fi
1327
Dean Troyerdff49a22014-01-30 15:37:40 -06001328 if [[ -z "$os_PACKAGE" ]]; then
1329 GetOSVersion
1330 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001331 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1332 # TRACK_DEPENDS=True installation creates a circular dependency when
1333 # we attempt to install virtualenv into a virualenv, so we must global
1334 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001335 source $DEST/.venv/bin/activate
1336 CMD_PIP=$DEST/.venv/bin/pip
1337 SUDO_PIP="env"
1338 else
1339 SUDO_PIP="sudo"
1340 CMD_PIP=$(get_pip_command)
1341 fi
1342
1343 # Mirror option not needed anymore because pypi has CDN available,
1344 # but it's useful in certain circumstances
1345 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
1346 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
1347 PIP_MIRROR_OPT="--use-mirrors"
1348 fi
1349
1350 # pip < 1.4 has a bug where it will use an already existing build
1351 # directory unconditionally. Say an earlier component installs
1352 # foo v1.1; pip will have built foo's source in
1353 # /tmp/$USER-pip-build. Even if a later component specifies foo <
1354 # 1.1, the existing extracted build will be used and cause
1355 # confusing errors. By creating unique build directories we avoid
1356 # this problem. See https://github.com/pypa/pip/issues/709
1357 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1358
Sean Dague45917cc2014-02-24 16:09:14 -05001359 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001360 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001361 http_proxy=$http_proxy \
1362 https_proxy=$https_proxy \
1363 no_proxy=$no_proxy \
Dean Troyerdff49a22014-01-30 15:37:40 -06001364 $CMD_PIP install --build=${pip_build_tmp} \
1365 $PIP_MIRROR_OPT $@ \
1366 && $SUDO_PIP rm -rf ${pip_build_tmp}
1367}
1368
Sean Dague099e5e32014-03-31 10:35:43 -04001369# this should be used if you want to install globally, all libraries should
1370# use this, especially *oslo* ones
1371function setup_install {
1372 local project_dir=$1
1373 setup_package_with_req_sync $project_dir
1374}
1375
1376# this should be used for projects which run services, like all services
1377function setup_develop {
1378 local project_dir=$1
1379 setup_package_with_req_sync $project_dir -e
1380}
1381
Dean Troyeraf616d92014-02-17 12:57:55 -06001382# ``pip install -e`` the package, which processes the dependencies
1383# using pip before running `setup.py develop`
1384#
1385# Updates the dependencies in project_dir from the
1386# openstack/requirements global list before installing anything.
1387#
1388# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1389# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001390function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001391 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001392 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001393
Dean Troyeraf616d92014-02-17 12:57:55 -06001394 # Don't update repo if local changes exist
1395 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001396 # ``errexit`` requires us to trap the exit code when the repo is changed
1397 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001398
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001399 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001400 (cd $REQUIREMENTS_DIR; \
1401 $SUDO_CMD python update.py $project_dir)
1402 fi
1403
Sean Dague099e5e32014-03-31 10:35:43 -04001404 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001405
1406 # We've just gone and possibly modified the user's source tree in an
1407 # automated way, which is considered bad form if it's a development
1408 # tree because we've screwed up their next git checkin. So undo it.
1409 #
1410 # However... there are some circumstances, like running in the gate
1411 # where we really really want the overridden version to stick. So provide
1412 # a variable that tells us whether or not we should UNDO the requirements
1413 # changes (this will be set to False in the OpenStack ci gate)
1414 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001415 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001416 (cd $project_dir && git reset --hard)
1417 fi
1418 fi
1419}
1420
1421# ``pip install -e`` the package, which processes the dependencies
1422# using pip before running `setup.py develop`
1423# Uses globals ``STACK_USER``
1424# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001425function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001426 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001427 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001428
Sean Dague099e5e32014-03-31 10:35:43 -04001429 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001430 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001431 if [[ "$flags" == "-e" ]]; then
1432 safe_chown -R $STACK_USER $1/*.egg-info
1433 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001434}
1435
Dean Troyerdff49a22014-01-30 15:37:40 -06001436
1437# Service Functions
1438# =================
1439
1440# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1441# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001442function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001443 echo "$1" | sed -e '
1444 s/,,/,/g;
1445 s/^,//;
1446 s/,$//
1447 '
1448}
1449
1450# disable_all_services() removes all current services
1451# from ``ENABLED_SERVICES`` to reset the configuration
1452# before a minimal installation
1453# Uses global ``ENABLED_SERVICES``
1454# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001455function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001456 ENABLED_SERVICES=""
1457}
1458
1459# Remove all services starting with '-'. For example, to install all default
1460# services except rabbit (rabbit) set in ``localrc``:
1461# ENABLED_SERVICES+=",-rabbit"
1462# Uses global ``ENABLED_SERVICES``
1463# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001464function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001465 local tmpsvcs="${ENABLED_SERVICES}"
1466 local service
1467 for service in ${tmpsvcs//,/ }; do
1468 if [[ ${service} == -* ]]; then
1469 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1470 fi
1471 done
1472 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1473}
1474
1475# disable_service() removes the services passed as argument to the
1476# ``ENABLED_SERVICES`` list, if they are present.
1477#
1478# For example:
1479# disable_service rabbit
1480#
1481# This function does not know about the special cases
1482# for nova, glance, and neutron built into is_service_enabled().
1483# Uses global ``ENABLED_SERVICES``
1484# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001485function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001486 local tmpsvcs=",${ENABLED_SERVICES},"
1487 local service
1488 for service in $@; do
1489 if is_service_enabled $service; then
1490 tmpsvcs=${tmpsvcs//,$service,/,}
1491 fi
1492 done
1493 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1494}
1495
1496# enable_service() adds the services passed as argument to the
1497# ``ENABLED_SERVICES`` list, if they are not already present.
1498#
1499# For example:
1500# enable_service qpid
1501#
1502# This function does not know about the special cases
1503# for nova, glance, and neutron built into is_service_enabled().
1504# Uses global ``ENABLED_SERVICES``
1505# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001506function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001507 local tmpsvcs="${ENABLED_SERVICES}"
1508 for service in $@; do
1509 if ! is_service_enabled $service; then
1510 tmpsvcs+=",$service"
1511 fi
1512 done
1513 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1514 disable_negated_services
1515}
1516
1517# is_service_enabled() checks if the service(s) specified as arguments are
1518# enabled by the user in ``ENABLED_SERVICES``.
1519#
1520# Multiple services specified as arguments are ``OR``'ed together; the test
1521# is a short-circuit boolean, i.e it returns on the first match.
1522#
1523# There are special cases for some 'catch-all' services::
1524# **nova** returns true if any service enabled start with **n-**
1525# **cinder** returns true if any service enabled start with **c-**
1526# **ceilometer** returns true if any service enabled start with **ceilometer**
1527# **glance** returns true if any service enabled start with **g-**
1528# **neutron** returns true if any service enabled start with **q-**
1529# **swift** returns true if any service enabled start with **s-**
1530# **trove** returns true if any service enabled start with **tr-**
1531# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1532# **s-** services will be enabled. This will be deprecated in the future.
1533#
1534# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1535# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1536# as enabled in this case.
1537#
1538# Uses global ``ENABLED_SERVICES``
1539# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001540function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001541 local xtrace=$(set +o | grep xtrace)
1542 set +o xtrace
1543 local enabled=1
Dean Troyerdff49a22014-01-30 15:37:40 -06001544 services=$@
1545 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001546 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001547
1548 # Look for top-level 'enabled' function for this service
1549 if type is_${service}_enabled >/dev/null 2>&1; then
1550 # A function exists for this service, use it
1551 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001552 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001553 fi
1554
1555 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1556 # are implemented
1557
Sean Dague45917cc2014-02-24 16:09:14 -05001558 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
1559 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1560 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1561 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1562 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1563 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1564 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1565 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1566 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1567 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001568 done
Sean Dague45917cc2014-02-24 16:09:14 -05001569 $xtrace
1570 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001571}
1572
1573# Toggle enable/disable_service for services that must run exclusive of each other
1574# $1 The name of a variable containing a space-separated list of services
1575# $2 The name of a variable in which to store the enabled service's name
1576# $3 The name of the service to enable
1577function use_exclusive_service {
1578 local options=${!1}
1579 local selection=$3
1580 out=$2
1581 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1582 for opt in $options;do
1583 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1584 done
1585 eval "$out=$selection"
1586 return 0
1587}
1588
1589
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001590# System Functions
1591# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001592
1593# Only run the command if the target file (the last arg) is not on an
1594# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001595function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001596 local xtrace=$(set +o | grep xtrace)
1597 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001598 local args=( $@ )
1599 local last
1600 local sudo_cmd
1601 local dir_to_check
1602
1603 let last="${#args[*]} - 1"
1604
1605 dir_to_check=${args[$last]}
1606 if [ ! -d "$dir_to_check" ]; then
1607 dir_to_check=`dirname "$dir_to_check"`
1608 fi
1609
1610 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001611 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001612 return 0
1613 fi
1614
1615 if [[ $TRACK_DEPENDS = True ]]; then
1616 sudo_cmd="env"
1617 else
1618 sudo_cmd="sudo"
1619 fi
1620
Sean Dague45917cc2014-02-24 16:09:14 -05001621 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001622 $sudo_cmd $@
1623}
1624
1625# Exit 0 if address is in network or 1 if address is not in network
1626# ip-range is in CIDR notation: 1.2.3.4/20
1627# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001628function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001629 local ip=$1
1630 local range=$2
1631 local masklen=${range#*/}
1632 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1633 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1634 [[ $network == $subnet ]]
1635}
1636
1637# Add a user to a group.
1638# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001639function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001640 local user=$1
1641 local group=$2
1642
1643 if [[ -z "$os_VENDOR" ]]; then
1644 GetOSVersion
1645 fi
1646
1647 # SLE11 and openSUSE 12.2 don't have the usual usermod
1648 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1649 sudo usermod -a -G "$group" "$user"
1650 else
1651 sudo usermod -A "$group" "$user"
1652 fi
1653}
1654
1655# Convert CIDR notation to a IPv4 netmask
1656# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001657function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001658 local maskpat="255 255 255 255"
1659 local maskdgt="254 252 248 240 224 192 128"
1660 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1661 echo ${1-0}.${2-0}.${3-0}.${4-0}
1662}
1663
1664# Gracefully cp only if source file/dir exists
1665# cp_it source destination
1666function cp_it {
1667 if [ -e $1 ] || [ -d $1 ]; then
1668 cp -pRL $1 $2
1669 fi
1670}
1671
1672# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1673# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1674# ``localrc`` or on the command line if necessary::
1675#
1676# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1677#
1678# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1679
Ian Wienandaee18c72014-02-21 15:35:08 +11001680function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001681 if [[ -n "$http_proxy" ]]; then
1682 export http_proxy=$http_proxy
1683 fi
1684 if [[ -n "$https_proxy" ]]; then
1685 export https_proxy=$https_proxy
1686 fi
1687 if [[ -n "$no_proxy" ]]; then
1688 export no_proxy=$no_proxy
1689 fi
1690}
1691
1692# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001693function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001694 local mount_type=`stat -f -L -c %T $1`
1695 test "$mount_type" == "nfs"
1696}
1697
1698# Return the network portion of the given IP address using netmask
1699# netmask is in the traditional dotted-quad format
1700# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001701function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001702 local ip=$1
1703 local mask=$2
1704 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1705 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1706 echo $subnet
1707}
1708
1709# Service wrapper to restart services
1710# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001711function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001712 if is_ubuntu; then
1713 sudo /usr/sbin/service $1 restart
1714 else
1715 sudo /sbin/service $1 restart
1716 fi
1717}
1718
1719# Only change permissions of a file or directory if it is not on an
1720# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001721function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001722 _safe_permission_operation chmod $@
1723}
1724
1725# Only change ownership of a file or directory if it is not on an NFS
1726# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001727function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001728 _safe_permission_operation chown $@
1729}
1730
1731# Service wrapper to start services
1732# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001733function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001734 if is_ubuntu; then
1735 sudo /usr/sbin/service $1 start
1736 else
1737 sudo /sbin/service $1 start
1738 fi
1739}
1740
1741# Service wrapper to stop services
1742# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001743function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001744 if is_ubuntu; then
1745 sudo /usr/sbin/service $1 stop
1746 else
1747 sudo /sbin/service $1 stop
1748 fi
1749}
1750
1751
1752# Restore xtrace
1753$XTRACE
1754
1755# Local variables:
1756# mode: shell-script
1757# End: