blob: ed3d8832fdf5dc230754bd00d859310d54298690 [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
438 elif [[ "$os_VENDOR" =~ (Red Hat) || "$os_VENDOR" =~ (CentOS) ]]; then
439 # Drop the . release as we assume it's compatible
440 DISTRO="rhel${os_RELEASE::1}"
441 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
442 DISTRO="xs$os_RELEASE"
443 else
444 # Catch-all for now is Vendor + Release + Update
445 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
446 fi
447 export DISTRO
448}
449
450# Utility function for checking machine architecture
451# is_arch arch-type
452function is_arch {
453 ARCH_TYPE=$1
454
455 [[ "$(uname -m)" == "$ARCH_TYPE" ]]
456}
457
458# Determine if current distribution is a Fedora-based distribution
459# (Fedora, RHEL, CentOS, etc).
460# is_fedora
461function is_fedora {
462 if [[ -z "$os_VENDOR" ]]; then
463 GetOSVersion
464 fi
465
466 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ]
467}
468
469
470# Determine if current distribution is a SUSE-based distribution
471# (openSUSE, SLE).
472# is_suse
473function is_suse {
474 if [[ -z "$os_VENDOR" ]]; then
475 GetOSVersion
476 fi
477
478 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
479}
480
481
482# Determine if current distribution is an Ubuntu-based distribution
483# It will also detect non-Ubuntu but Debian-based distros
484# is_ubuntu
485function is_ubuntu {
486 if [[ -z "$os_PACKAGE" ]]; then
487 GetOSVersion
488 fi
489 [ "$os_PACKAGE" = "deb" ]
490}
491
492
493# Git Functions
494# =============
495
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600496# Returns openstack release name for a given branch name
497# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100498function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600499 local branch=$1
500 if [[ $branch =~ "stable/" ]]; then
501 echo ${branch#*/}
502 else
503 echo "master"
504 fi
505}
506
Dean Troyerdff49a22014-01-30 15:37:40 -0600507# git clone only if directory doesn't exist already. Since ``DEST`` might not
508# be owned by the installation user, we create the directory and change the
509# ownership to the proper user.
510# Set global RECLONE=yes to simulate a clone when dest-dir exists
511# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
512# does not exist (default is False, meaning the repo will be cloned).
513# Uses global ``OFFLINE``
514# git_clone remote dest-dir branch
515function git_clone {
516 GIT_REMOTE=$1
517 GIT_DEST=$2
518 GIT_REF=$3
519 RECLONE=$(trueorfalse False $RECLONE)
520
521 if [[ "$OFFLINE" = "True" ]]; then
522 echo "Running in offline mode, clones already exist"
523 # print out the results so we know what change was used in the logs
524 cd $GIT_DEST
525 git show --oneline | head -1
526 return
527 fi
528
529 if echo $GIT_REF | egrep -q "^refs"; then
530 # If our branch name is a gerrit style refs/changes/...
531 if [[ ! -d $GIT_DEST ]]; then
532 [[ "$ERROR_ON_CLONE" = "True" ]] && \
533 die $LINENO "Cloning not allowed in this configuration"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100534 git_timed clone $GIT_REMOTE $GIT_DEST
Dean Troyerdff49a22014-01-30 15:37:40 -0600535 fi
536 cd $GIT_DEST
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100537 git_timed fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600538 else
539 # do a full clone only if the directory doesn't exist
540 if [[ ! -d $GIT_DEST ]]; then
541 [[ "$ERROR_ON_CLONE" = "True" ]] && \
542 die $LINENO "Cloning not allowed in this configuration"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100543 git_timed clone $GIT_REMOTE $GIT_DEST
Dean Troyerdff49a22014-01-30 15:37:40 -0600544 cd $GIT_DEST
545 # This checkout syntax works for both branches and tags
546 git checkout $GIT_REF
547 elif [[ "$RECLONE" = "True" ]]; then
548 # if it does exist then simulate what clone does if asked to RECLONE
549 cd $GIT_DEST
550 # set the url to pull from and fetch
551 git remote set-url origin $GIT_REMOTE
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100552 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600553 # remove the existing ignored files (like pyc) as they cause breakage
554 # (due to the py files having older timestamps than our pyc, so python
555 # thinks the pyc files are correct using them)
556 find $GIT_DEST -name '*.pyc' -delete
557
558 # handle GIT_REF accordingly to type (tag, branch)
559 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
560 git_update_tag $GIT_REF
561 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
562 git_update_branch $GIT_REF
563 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
564 git_update_remote_branch $GIT_REF
565 else
566 die $LINENO "$GIT_REF is neither branch nor tag"
567 fi
568
569 fi
570 fi
571
572 # print out the results so we know what change was used in the logs
573 cd $GIT_DEST
574 git show --oneline | head -1
575}
576
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100577# git can sometimes get itself infinitely stuck with transient network
578# errors or other issues with the remote end. This wraps git in a
579# timeout/retry loop and is intended to watch over non-local git
580# processes that might hang. GIT_TIMEOUT, if set, is passed directly
581# to timeout(1); otherwise the default value of 0 maintains the status
582# quo of waiting forever.
583# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100584function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100585 local count=0
586 local timeout=0
587
588 if [[ -n "${GIT_TIMEOUT}" ]]; then
589 timeout=${GIT_TIMEOUT}
590 fi
591
592 until timeout -s SIGINT ${timeout} git "$@"; do
593 # 124 is timeout(1)'s special return code when it reached the
594 # timeout; otherwise assume fatal failure
595 if [[ $? -ne 124 ]]; then
596 die $LINENO "git call failed: [git $@]"
597 fi
598
599 count=$(($count + 1))
600 warn "timeout ${count} for git call: [git $@]"
601 if [ $count -eq 3 ]; then
602 die $LINENO "Maximum of 3 git retries reached"
603 fi
604 sleep 5
605 done
606}
607
Dean Troyerdff49a22014-01-30 15:37:40 -0600608# git update using reference as a branch.
609# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100610function git_update_branch {
Dean Troyerdff49a22014-01-30 15:37:40 -0600611
612 GIT_BRANCH=$1
613
614 git checkout -f origin/$GIT_BRANCH
615 # a local branch might not exist
616 git branch -D $GIT_BRANCH || true
617 git checkout -b $GIT_BRANCH
618}
619
620# git update using reference as a branch.
621# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100622function git_update_remote_branch {
Dean Troyerdff49a22014-01-30 15:37:40 -0600623
624 GIT_BRANCH=$1
625
626 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
627}
628
629# git update using reference as a tag. Be careful editing source at that repo
630# as working copy will be in a detached mode
631# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100632function git_update_tag {
Dean Troyerdff49a22014-01-30 15:37:40 -0600633
634 GIT_TAG=$1
635
636 git tag -d $GIT_TAG
637 # fetching given tag only
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100638 git_timed fetch origin tag $GIT_TAG
Dean Troyerdff49a22014-01-30 15:37:40 -0600639 git checkout -f $GIT_TAG
640}
641
642
643# OpenStack Functions
644# ===================
645
646# Get the default value for HOST_IP
647# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100648function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600649 local fixed_range=$1
650 local floating_range=$2
651 local host_ip_iface=$3
652 local host_ip=$4
653
654 # Find the interface used for the default route
655 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
656 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
657 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
658 host_ip=""
659 host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}'`
660 for IP in $host_ips; do
661 # Attempt to filter out IP addresses that are part of the fixed and
662 # floating range. Note that this method only works if the ``netaddr``
663 # python library is installed. If it is not installed, an error
664 # will be printed and the first IP from the interface will be used.
665 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
666 # address.
667 if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then
668 host_ip=$IP
669 break;
670 fi
671 done
672 fi
673 echo $host_ip
674}
675
676# Grab a numbered field from python prettytable output
677# Fields are numbered starting with 1
678# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
679# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100680function get_field {
Dean Troyerdff49a22014-01-30 15:37:40 -0600681 while read data; do
682 if [ "$1" -lt 0 ]; then
683 field="(\$(NF$1))"
684 else
685 field="\$$(($1 + 1))"
686 fi
687 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
688 done
689}
690
691# Add a policy to a policy.json file
692# Do nothing if the policy already exists
693# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100694function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600695 local policy_file=$1
696 local policy_name=$2
697 local policy_perm=$3
698
699 if grep -q ${policy_name} ${policy_file}; then
700 echo "Policy ${policy_name} already exists in ${policy_file}"
701 return
702 fi
703
704 # Add a terminating comma to policy lines without one
705 # Remove the closing '}' and all lines following to the end-of-file
706 local tmpfile=$(mktemp)
707 uniq ${policy_file} | sed -e '
708 s/]$/],/
709 /^[}]/,$d
710 ' > ${tmpfile}
711
712 # Append policy and closing brace
713 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
714 echo "}" >>${tmpfile}
715
716 mv ${tmpfile} ${policy_file}
717}
718
719
720# Package Functions
721# =================
722
723# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100724function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600725 local pkg_dir
726 if is_ubuntu; then
727 pkg_dir=$FILES/apts
728 elif is_fedora; then
729 pkg_dir=$FILES/rpms
730 elif is_suse; then
731 pkg_dir=$FILES/rpms-suse
732 else
733 exit_distro_not_supported "list of packages"
734 fi
735 echo "$pkg_dir"
736}
737
738# Wrapper for ``apt-get`` to set cache and proxy environment variables
739# Uses globals ``OFFLINE``, ``*_proxy``
740# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100741function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500742 local xtrace=$(set +o | grep xtrace)
743 set +o xtrace
744
Dean Troyerdff49a22014-01-30 15:37:40 -0600745 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
746 local sudo="sudo"
747 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500748
749 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600750 $sudo DEBIAN_FRONTEND=noninteractive \
751 http_proxy=$http_proxy https_proxy=$https_proxy \
752 no_proxy=$no_proxy \
753 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
754}
755
756# get_packages() collects a list of package names of any type from the
757# prerequisite files in ``files/{apts|rpms}``. The list is intended
758# to be passed to a package installer such as apt or yum.
759#
760# Only packages required for the services in 1st argument will be
761# included. Two bits of metadata are recognized in the prerequisite files:
762#
763# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
764# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
765# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100766function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500767 local xtrace=$(set +o | grep xtrace)
768 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600769 local services=$@
770 local package_dir=$(_get_package_dir)
771 local file_to_parse
772 local service
773
774 if [[ -z "$package_dir" ]]; then
775 echo "No package directory supplied"
776 return 1
777 fi
778 if [[ -z "$DISTRO" ]]; then
779 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -0500780 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -0600781 fi
782 for service in ${services//,/ }; do
783 # Allow individual services to specify dependencies
784 if [[ -e ${package_dir}/${service} ]]; then
785 file_to_parse="${file_to_parse} $service"
786 fi
787 # NOTE(sdague) n-api needs glance for now because that's where
788 # glance client is
789 if [[ $service == n-api ]]; then
790 if [[ ! $file_to_parse =~ nova ]]; then
791 file_to_parse="${file_to_parse} nova"
792 fi
793 if [[ ! $file_to_parse =~ glance ]]; then
794 file_to_parse="${file_to_parse} glance"
795 fi
796 elif [[ $service == c-* ]]; then
797 if [[ ! $file_to_parse =~ cinder ]]; then
798 file_to_parse="${file_to_parse} cinder"
799 fi
800 elif [[ $service == ceilometer-* ]]; then
801 if [[ ! $file_to_parse =~ ceilometer ]]; then
802 file_to_parse="${file_to_parse} ceilometer"
803 fi
804 elif [[ $service == s-* ]]; then
805 if [[ ! $file_to_parse =~ swift ]]; then
806 file_to_parse="${file_to_parse} swift"
807 fi
808 elif [[ $service == n-* ]]; then
809 if [[ ! $file_to_parse =~ nova ]]; then
810 file_to_parse="${file_to_parse} nova"
811 fi
812 elif [[ $service == g-* ]]; then
813 if [[ ! $file_to_parse =~ glance ]]; then
814 file_to_parse="${file_to_parse} glance"
815 fi
816 elif [[ $service == key* ]]; then
817 if [[ ! $file_to_parse =~ keystone ]]; then
818 file_to_parse="${file_to_parse} keystone"
819 fi
820 elif [[ $service == q-* ]]; then
821 if [[ ! $file_to_parse =~ neutron ]]; then
822 file_to_parse="${file_to_parse} neutron"
823 fi
824 fi
825 done
826
827 for file in ${file_to_parse}; do
828 local fname=${package_dir}/${file}
829 local OIFS line package distros distro
830 [[ -e $fname ]] || continue
831
832 OIFS=$IFS
833 IFS=$'\n'
834 for line in $(<${fname}); do
835 if [[ $line =~ "NOPRIME" ]]; then
836 continue
837 fi
838
839 # Assume we want this package
840 package=${line%#*}
841 inst_pkg=1
842
843 # Look for # dist:xxx in comment
844 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
845 # We are using BASH regexp matching feature.
846 package=${BASH_REMATCH[1]}
847 distros=${BASH_REMATCH[2]}
848 # In bash ${VAR,,} will lowecase VAR
849 # Look for a match in the distro list
850 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
851 # If no match then skip this package
852 inst_pkg=0
853 fi
854 fi
855
856 # Look for # testonly in comment
857 if [[ $line =~ (.*)#.*testonly.* ]]; then
858 package=${BASH_REMATCH[1]}
859 # Are we installing test packages? (test for the default value)
860 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
861 # If not installing test packages the skip this package
862 inst_pkg=0
863 fi
864 fi
865
866 if [[ $inst_pkg = 1 ]]; then
867 echo $package
868 fi
869 done
870 IFS=$OIFS
871 done
Sean Dague45917cc2014-02-24 16:09:14 -0500872 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600873}
874
875# Distro-agnostic package installer
876# install_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100877function install_package {
Sean Dague45917cc2014-02-24 16:09:14 -0500878 local xtrace=$(set +o | grep xtrace)
879 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600880 if is_ubuntu; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600881 # if there are transient errors pulling the updates, that's fine. It may
882 # be secondary repositories that we don't really care about.
883 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update || /bin/true
Dean Troyerdff49a22014-01-30 15:37:40 -0600884 NO_UPDATE_REPOS=True
885
Sean Dague45917cc2014-02-24 16:09:14 -0500886 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600887 apt_get install "$@"
888 elif is_fedora; then
Sean Dague45917cc2014-02-24 16:09:14 -0500889 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600890 yum_install "$@"
891 elif is_suse; then
Sean Dague45917cc2014-02-24 16:09:14 -0500892 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600893 zypper_install "$@"
894 else
Sean Dague45917cc2014-02-24 16:09:14 -0500895 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600896 exit_distro_not_supported "installing packages"
897 fi
898}
899
900# Distro-agnostic function to tell if a package is installed
901# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100902function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -0600903 if [[ -z "$@" ]]; then
904 return 1
905 fi
906
907 if [[ -z "$os_PACKAGE" ]]; then
908 GetOSVersion
909 fi
910
911 if [[ "$os_PACKAGE" = "deb" ]]; then
912 dpkg -s "$@" > /dev/null 2> /dev/null
913 elif [[ "$os_PACKAGE" = "rpm" ]]; then
914 rpm --quiet -q "$@"
915 else
916 exit_distro_not_supported "finding if a package is installed"
917 fi
918}
919
920# Distro-agnostic package uninstaller
921# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100922function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -0600923 if is_ubuntu; then
924 apt_get purge "$@"
925 elif is_fedora; then
926 sudo yum remove -y "$@"
927 elif is_suse; then
928 sudo zypper rm "$@"
929 else
930 exit_distro_not_supported "uninstalling packages"
931 fi
932}
933
934# Wrapper for ``yum`` to set proxy environment variables
935# Uses globals ``OFFLINE``, ``*_proxy``
936# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100937function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -0600938 [[ "$OFFLINE" = "True" ]] && return
939 local sudo="sudo"
940 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +1100941
942 # The manual check for missing packages is because yum -y assumes
943 # missing packages are OK. See
944 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -0600945 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
946 no_proxy=$no_proxy \
Ian Wienandb27f16d2014-02-28 14:29:02 +1100947 yum install -y "$@" 2>&1 | \
948 awk '
949 BEGIN { fail=0 }
950 /No package/ { fail=1 }
951 { print }
952 END { exit fail }' || \
953 die $LINENO "Missing packages detected"
954
955 # also ensure we catch a yum failure
956 if [[ ${PIPESTATUS[0]} != 0 ]]; then
957 die $LINENO "Yum install failure"
958 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600959}
960
961# zypper wrapper to set arguments correctly
962# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100963function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -0600964 [[ "$OFFLINE" = "True" ]] && return
965 local sudo="sudo"
966 [[ "$(id -u)" = "0" ]] && sudo="env"
967 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
968 zypper --non-interactive install --auto-agree-with-licenses "$@"
969}
970
971
972# Process Functions
973# =================
974
975# _run_process() is designed to be backgrounded by run_process() to simulate a
976# fork. It includes the dirty work of closing extra filehandles and preparing log
977# files to produce the same logs as screen_it(). The log filename is derived
978# from the service name and global-and-now-misnamed SCREEN_LOGDIR
979# _run_process service "command-line"
Ian Wienandaee18c72014-02-21 15:35:08 +1100980function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -0600981 local service=$1
982 local command="$2"
983
984 # Undo logging redirections and close the extra descriptors
985 exec 1>&3
986 exec 2>&3
987 exec 3>&-
988 exec 6>&-
989
990 if [[ -n ${SCREEN_LOGDIR} ]]; then
991 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
992 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
993
994 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
995 export PYTHONUNBUFFERED=1
996 fi
997
998 exec /bin/bash -c "$command"
999 die "$service exec failure: $command"
1000}
1001
1002# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1003# This is used for ``service_check`` when all the ``screen_it`` are called finished
1004# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001005function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001006 SCREEN_NAME=${SCREEN_NAME:-stack}
1007 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1008
1009 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1010 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1011 fi
1012
1013 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1014}
1015
1016# Find out if a process exists by partial name.
1017# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001018function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001019 local name=$1
1020 ps auxw | grep -v grep | grep ${name} > /dev/null
1021 RC=$?
1022 # some times I really hate bash reverse binary logic
1023 return $RC
1024}
1025
1026# run_process() launches a child process that closes all file descriptors and
1027# then exec's the passed in command. This is meant to duplicate the semantics
1028# of screen_it() without screen. PIDs are written to
1029# $SERVICE_DIR/$SCREEN_NAME/$service.pid
1030# run_process service "command-line"
Ian Wienandaee18c72014-02-21 15:35:08 +11001031function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001032 local service=$1
1033 local command="$2"
1034
1035 # Spawn the child process
1036 _run_process "$service" "$command" &
1037 echo $!
1038}
1039
1040# Helper to launch a service in a named screen
1041# screen_it service "command-line"
1042function screen_it {
1043 SCREEN_NAME=${SCREEN_NAME:-stack}
1044 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1045 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1046
1047 if is_service_enabled $1; then
1048 # Append the service to the screen rc file
1049 screen_rc "$1" "$2"
1050
1051 if [[ "$USE_SCREEN" = "True" ]]; then
1052 screen -S $SCREEN_NAME -X screen -t $1
1053
1054 if [[ -n ${SCREEN_LOGDIR} ]]; then
1055 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
1056 screen -S $SCREEN_NAME -p $1 -X log on
1057 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1058 fi
1059
1060 # sleep to allow bash to be ready to be send the command - we are
1061 # creating a new window in screen and then sends characters, so if
1062 # bash isn't running by the time we send the command, nothing happens
1063 sleep 1.5
1064
1065 NL=`echo -ne '\015'`
1066 # This fun command does the following:
1067 # - the passed server command is backgrounded
1068 # - the pid of the background process is saved in the usual place
1069 # - the server process is brought back to the foreground
1070 # - if the server process exits prematurely the fg command errors
1071 # and a message is written to stdout and the service failure file
1072 # The pid saved can be used in screen_stop() as a process group
1073 # id to kill off all child processes
1074 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"
1075 else
1076 # Spawn directly without screen
1077 run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1078 fi
1079 fi
1080}
1081
1082# Screen rc file builder
1083# screen_rc service "command-line"
1084function screen_rc {
1085 SCREEN_NAME=${SCREEN_NAME:-stack}
1086 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1087 if [[ ! -e $SCREENRC ]]; then
1088 # Name the screen session
1089 echo "sessionname $SCREEN_NAME" > $SCREENRC
1090 # Set a reasonable statusbar
1091 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1092 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1093 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1094 echo "screen -t shell bash" >> $SCREENRC
1095 fi
1096 # If this service doesn't already exist in the screenrc file
1097 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1098 NL=`echo -ne '\015'`
1099 echo "screen -t $1 bash" >> $SCREENRC
1100 echo "stuff \"$2$NL\"" >> $SCREENRC
1101
1102 if [[ -n ${SCREEN_LOGDIR} ]]; then
1103 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1104 echo "log on" >>$SCREENRC
1105 fi
1106 fi
1107}
1108
1109# Stop a service in screen
1110# If a PID is available use it, kill the whole process group via TERM
1111# If screen is being used kill the screen window; this will catch processes
1112# that did not leave a PID behind
1113# screen_stop service
Ian Wienandaee18c72014-02-21 15:35:08 +11001114function screen_stop {
Dean Troyerdff49a22014-01-30 15:37:40 -06001115 SCREEN_NAME=${SCREEN_NAME:-stack}
1116 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1117 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1118
1119 if is_service_enabled $1; then
1120 # Kill via pid if we have one available
1121 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$1.pid ]]; then
1122 pkill -TERM -P -$(cat $SERVICE_DIR/$SCREEN_NAME/$1.pid)
1123 rm $SERVICE_DIR/$SCREEN_NAME/$1.pid
1124 fi
1125 if [[ "$USE_SCREEN" = "True" ]]; then
1126 # Clean up the screen window
1127 screen -S $SCREEN_NAME -p $1 -X kill
1128 fi
1129 fi
1130}
1131
1132# Helper to get the status of each running service
1133# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001134function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001135 local service
1136 local failures
1137 SCREEN_NAME=${SCREEN_NAME:-stack}
1138 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1139
1140
1141 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1142 echo "No service status directory found"
1143 return
1144 fi
1145
1146 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001147 # make this -o errexit safe
1148 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001149
1150 for service in $failures; do
1151 service=`basename $service`
1152 service=${service%.failure}
1153 echo "Error: Service $service is not running"
1154 done
1155
1156 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001157 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001158 fi
1159}
1160
1161
1162# Python Functions
1163# ================
1164
1165# Get the path to the pip command.
1166# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001167function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001168 which pip || which pip-python
1169
1170 if [ $? -ne 0 ]; then
1171 die $LINENO "Unable to find pip; cannot continue"
1172 fi
1173}
1174
1175# Get the path to the direcotry where python executables are installed.
1176# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001177function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001178 if is_fedora || is_suse; then
1179 echo "/usr/bin"
1180 else
1181 echo "/usr/local/bin"
1182 fi
1183}
1184
1185# Wrapper for ``pip install`` to set cache and proxy environment variables
1186# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
1187# ``TRACK_DEPENDS``, ``*_proxy``
1188# pip_install package [package ...]
1189function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001190 local xtrace=$(set +o | grep xtrace)
1191 set +o xtrace
1192 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1193 $xtrace
1194 return
1195 fi
1196
Dean Troyerdff49a22014-01-30 15:37:40 -06001197 if [[ -z "$os_PACKAGE" ]]; then
1198 GetOSVersion
1199 fi
1200 if [[ $TRACK_DEPENDS = True ]]; then
1201 source $DEST/.venv/bin/activate
1202 CMD_PIP=$DEST/.venv/bin/pip
1203 SUDO_PIP="env"
1204 else
1205 SUDO_PIP="sudo"
1206 CMD_PIP=$(get_pip_command)
1207 fi
1208
1209 # Mirror option not needed anymore because pypi has CDN available,
1210 # but it's useful in certain circumstances
1211 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
1212 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
1213 PIP_MIRROR_OPT="--use-mirrors"
1214 fi
1215
1216 # pip < 1.4 has a bug where it will use an already existing build
1217 # directory unconditionally. Say an earlier component installs
1218 # foo v1.1; pip will have built foo's source in
1219 # /tmp/$USER-pip-build. Even if a later component specifies foo <
1220 # 1.1, the existing extracted build will be used and cause
1221 # confusing errors. By creating unique build directories we avoid
1222 # this problem. See https://github.com/pypa/pip/issues/709
1223 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1224
Sean Dague45917cc2014-02-24 16:09:14 -05001225 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001226 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1227 HTTP_PROXY=$http_proxy \
1228 HTTPS_PROXY=$https_proxy \
1229 NO_PROXY=$no_proxy \
1230 $CMD_PIP install --build=${pip_build_tmp} \
1231 $PIP_MIRROR_OPT $@ \
1232 && $SUDO_PIP rm -rf ${pip_build_tmp}
1233}
1234
Dean Troyeraf616d92014-02-17 12:57:55 -06001235# ``pip install -e`` the package, which processes the dependencies
1236# using pip before running `setup.py develop`
1237#
1238# Updates the dependencies in project_dir from the
1239# openstack/requirements global list before installing anything.
1240#
1241# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1242# setup_develop directory
Ian Wienandaee18c72014-02-21 15:35:08 +11001243function setup_develop {
Dean Troyeraf616d92014-02-17 12:57:55 -06001244 local project_dir=$1
1245
Dean Troyeraf616d92014-02-17 12:57:55 -06001246 # Don't update repo if local changes exist
1247 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001248 # ``errexit`` requires us to trap the exit code when the repo is changed
1249 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001250
Dean Troyer83b6c992014-02-27 12:41:28 -06001251 if [[ $update_requirements = "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001252 (cd $REQUIREMENTS_DIR; \
1253 $SUDO_CMD python update.py $project_dir)
1254 fi
1255
1256 setup_develop_no_requirements_update $project_dir
1257
1258 # We've just gone and possibly modified the user's source tree in an
1259 # automated way, which is considered bad form if it's a development
1260 # tree because we've screwed up their next git checkin. So undo it.
1261 #
1262 # However... there are some circumstances, like running in the gate
1263 # where we really really want the overridden version to stick. So provide
1264 # a variable that tells us whether or not we should UNDO the requirements
1265 # changes (this will be set to False in the OpenStack ci gate)
1266 if [ $UNDO_REQUIREMENTS = "True" ]; then
Dean Troyer83b6c992014-02-27 12:41:28 -06001267 if [[ $update_requirements = "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001268 (cd $project_dir && git reset --hard)
1269 fi
1270 fi
1271}
1272
1273# ``pip install -e`` the package, which processes the dependencies
1274# using pip before running `setup.py develop`
1275# Uses globals ``STACK_USER``
1276# setup_develop_no_requirements_update directory
Ian Wienandaee18c72014-02-21 15:35:08 +11001277function setup_develop_no_requirements_update {
Dean Troyeraf616d92014-02-17 12:57:55 -06001278 local project_dir=$1
1279
1280 pip_install -e $project_dir
1281 # ensure that further actions can do things like setup.py sdist
1282 safe_chown -R $STACK_USER $1/*.egg-info
1283}
1284
Dean Troyerdff49a22014-01-30 15:37:40 -06001285
1286# Service Functions
1287# =================
1288
1289# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1290# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001291function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001292 echo "$1" | sed -e '
1293 s/,,/,/g;
1294 s/^,//;
1295 s/,$//
1296 '
1297}
1298
1299# disable_all_services() removes all current services
1300# from ``ENABLED_SERVICES`` to reset the configuration
1301# before a minimal installation
1302# Uses global ``ENABLED_SERVICES``
1303# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001304function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001305 ENABLED_SERVICES=""
1306}
1307
1308# Remove all services starting with '-'. For example, to install all default
1309# services except rabbit (rabbit) set in ``localrc``:
1310# ENABLED_SERVICES+=",-rabbit"
1311# Uses global ``ENABLED_SERVICES``
1312# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001313function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001314 local tmpsvcs="${ENABLED_SERVICES}"
1315 local service
1316 for service in ${tmpsvcs//,/ }; do
1317 if [[ ${service} == -* ]]; then
1318 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1319 fi
1320 done
1321 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1322}
1323
1324# disable_service() removes the services passed as argument to the
1325# ``ENABLED_SERVICES`` list, if they are present.
1326#
1327# For example:
1328# disable_service rabbit
1329#
1330# This function does not know about the special cases
1331# for nova, glance, and neutron built into is_service_enabled().
1332# Uses global ``ENABLED_SERVICES``
1333# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001334function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001335 local tmpsvcs=",${ENABLED_SERVICES},"
1336 local service
1337 for service in $@; do
1338 if is_service_enabled $service; then
1339 tmpsvcs=${tmpsvcs//,$service,/,}
1340 fi
1341 done
1342 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1343}
1344
1345# enable_service() adds the services passed as argument to the
1346# ``ENABLED_SERVICES`` list, if they are not already present.
1347#
1348# For example:
1349# enable_service qpid
1350#
1351# This function does not know about the special cases
1352# for nova, glance, and neutron built into is_service_enabled().
1353# Uses global ``ENABLED_SERVICES``
1354# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001355function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001356 local tmpsvcs="${ENABLED_SERVICES}"
1357 for service in $@; do
1358 if ! is_service_enabled $service; then
1359 tmpsvcs+=",$service"
1360 fi
1361 done
1362 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1363 disable_negated_services
1364}
1365
1366# is_service_enabled() checks if the service(s) specified as arguments are
1367# enabled by the user in ``ENABLED_SERVICES``.
1368#
1369# Multiple services specified as arguments are ``OR``'ed together; the test
1370# is a short-circuit boolean, i.e it returns on the first match.
1371#
1372# There are special cases for some 'catch-all' services::
1373# **nova** returns true if any service enabled start with **n-**
1374# **cinder** returns true if any service enabled start with **c-**
1375# **ceilometer** returns true if any service enabled start with **ceilometer**
1376# **glance** returns true if any service enabled start with **g-**
1377# **neutron** returns true if any service enabled start with **q-**
1378# **swift** returns true if any service enabled start with **s-**
1379# **trove** returns true if any service enabled start with **tr-**
1380# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1381# **s-** services will be enabled. This will be deprecated in the future.
1382#
1383# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1384# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1385# as enabled in this case.
1386#
1387# Uses global ``ENABLED_SERVICES``
1388# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001389function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001390 local xtrace=$(set +o | grep xtrace)
1391 set +o xtrace
1392 local enabled=1
Dean Troyerdff49a22014-01-30 15:37:40 -06001393 services=$@
1394 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001395 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001396
1397 # Look for top-level 'enabled' function for this service
1398 if type is_${service}_enabled >/dev/null 2>&1; then
1399 # A function exists for this service, use it
1400 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001401 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001402 fi
1403
1404 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1405 # are implemented
1406
Sean Dague45917cc2014-02-24 16:09:14 -05001407 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
1408 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1409 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1410 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1411 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1412 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1413 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1414 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1415 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1416 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001417 done
Sean Dague45917cc2014-02-24 16:09:14 -05001418 $xtrace
1419 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001420}
1421
1422# Toggle enable/disable_service for services that must run exclusive of each other
1423# $1 The name of a variable containing a space-separated list of services
1424# $2 The name of a variable in which to store the enabled service's name
1425# $3 The name of the service to enable
1426function use_exclusive_service {
1427 local options=${!1}
1428 local selection=$3
1429 out=$2
1430 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1431 for opt in $options;do
1432 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1433 done
1434 eval "$out=$selection"
1435 return 0
1436}
1437
1438
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001439# System Functions
1440# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001441
1442# Only run the command if the target file (the last arg) is not on an
1443# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001444function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001445 local xtrace=$(set +o | grep xtrace)
1446 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001447 local args=( $@ )
1448 local last
1449 local sudo_cmd
1450 local dir_to_check
1451
1452 let last="${#args[*]} - 1"
1453
1454 dir_to_check=${args[$last]}
1455 if [ ! -d "$dir_to_check" ]; then
1456 dir_to_check=`dirname "$dir_to_check"`
1457 fi
1458
1459 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001460 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001461 return 0
1462 fi
1463
1464 if [[ $TRACK_DEPENDS = True ]]; then
1465 sudo_cmd="env"
1466 else
1467 sudo_cmd="sudo"
1468 fi
1469
Sean Dague45917cc2014-02-24 16:09:14 -05001470 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001471 $sudo_cmd $@
1472}
1473
1474# Exit 0 if address is in network or 1 if address is not in network
1475# ip-range is in CIDR notation: 1.2.3.4/20
1476# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001477function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001478 local ip=$1
1479 local range=$2
1480 local masklen=${range#*/}
1481 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1482 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1483 [[ $network == $subnet ]]
1484}
1485
1486# Add a user to a group.
1487# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001488function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001489 local user=$1
1490 local group=$2
1491
1492 if [[ -z "$os_VENDOR" ]]; then
1493 GetOSVersion
1494 fi
1495
1496 # SLE11 and openSUSE 12.2 don't have the usual usermod
1497 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1498 sudo usermod -a -G "$group" "$user"
1499 else
1500 sudo usermod -A "$group" "$user"
1501 fi
1502}
1503
1504# Convert CIDR notation to a IPv4 netmask
1505# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001506function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001507 local maskpat="255 255 255 255"
1508 local maskdgt="254 252 248 240 224 192 128"
1509 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1510 echo ${1-0}.${2-0}.${3-0}.${4-0}
1511}
1512
1513# Gracefully cp only if source file/dir exists
1514# cp_it source destination
1515function cp_it {
1516 if [ -e $1 ] || [ -d $1 ]; then
1517 cp -pRL $1 $2
1518 fi
1519}
1520
1521# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1522# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1523# ``localrc`` or on the command line if necessary::
1524#
1525# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1526#
1527# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1528
Ian Wienandaee18c72014-02-21 15:35:08 +11001529function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001530 if [[ -n "$http_proxy" ]]; then
1531 export http_proxy=$http_proxy
1532 fi
1533 if [[ -n "$https_proxy" ]]; then
1534 export https_proxy=$https_proxy
1535 fi
1536 if [[ -n "$no_proxy" ]]; then
1537 export no_proxy=$no_proxy
1538 fi
1539}
1540
1541# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001542function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001543 local mount_type=`stat -f -L -c %T $1`
1544 test "$mount_type" == "nfs"
1545}
1546
1547# Return the network portion of the given IP address using netmask
1548# netmask is in the traditional dotted-quad format
1549# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001550function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001551 local ip=$1
1552 local mask=$2
1553 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1554 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1555 echo $subnet
1556}
1557
1558# Service wrapper to restart services
1559# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001560function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001561 if is_ubuntu; then
1562 sudo /usr/sbin/service $1 restart
1563 else
1564 sudo /sbin/service $1 restart
1565 fi
1566}
1567
1568# Only change permissions of a file or directory if it is not on an
1569# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001570function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001571 _safe_permission_operation chmod $@
1572}
1573
1574# Only change ownership of a file or directory if it is not on an NFS
1575# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001576function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001577 _safe_permission_operation chown $@
1578}
1579
1580# Service wrapper to start services
1581# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001582function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001583 if is_ubuntu; then
1584 sudo /usr/sbin/service $1 start
1585 else
1586 sudo /sbin/service $1 start
1587 fi
1588}
1589
1590# Service wrapper to stop services
1591# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001592function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001593 if is_ubuntu; then
1594 sudo /usr/sbin/service $1 stop
1595 else
1596 sudo /sbin/service $1 stop
1597 fi
1598}
1599
1600
1601# Restore xtrace
1602$XTRACE
1603
1604# Local variables:
1605# mode: shell-script
1606# End: