blob: 838ca53f6acd7d73bbb43f606304d578c77ff790 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Dean Troyerdff49a22014-01-30 15:37:40 -06003# functions-common - Common functions used by DevStack components
4#
5# The canonical copy of this file is maintained in the DevStack repo.
6# All modifications should be made there and then sync'ed to other repos
7# as required.
8#
9# This file is sorted alphabetically within the function groups.
10#
11# - Config Functions
12# - Control Functions
13# - Distro Functions
14# - Git Functions
15# - OpenStack Functions
16# - Package Functions
17# - Process Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060018# - Service Functions
Masayuki Igawaf6368d32014-02-20 13:31:26 +090019# - System Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060020#
21# The following variables are assumed to be defined by certain functions:
22#
23# - ``ENABLED_SERVICES``
24# - ``ERROR_ON_CLONE``
25# - ``FILES``
26# - ``OFFLINE``
Dean Troyerdff49a22014-01-30 15:37:40 -060027# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090028# - ``REQUIREMENTS_DIR``
29# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060030# - ``TRACK_DEPENDS``
Masayuki Igawad20f6322014-02-28 09:22:37 +090031# - ``UNDO_REQUIREMENTS``
Dean Troyerdff49a22014-01-30 15:37:40 -060032# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050033#
Dean Troyerdff49a22014-01-30 15:37:40 -060034
35# Save trace setting
36XTRACE=$(set +o | grep xtrace)
37set +o xtrace
38
Sean Daguecc524062014-10-01 09:06:43 -040039# Global Config Variables
40declare -A GITREPO
41declare -A GITBRANCH
42declare -A GITDIR
43
Sean Dague53753292014-12-04 19:38:15 -050044TRACK_DEPENDS=${TRACK_DEPENDS:-False}
45
Dean Troyerdff49a22014-01-30 15:37:40 -060046
47# Normalize config values to True or False
48# Accepts as False: 0 no No NO false False FALSE
49# Accepts as True: 1 yes Yes YES true True TRUE
50# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +110051function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -050052 local xtrace=$(set +o | grep xtrace)
53 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060054 local default=$1
Sean Dague53753292014-12-04 19:38:15 -050055 local literal=$2
Dean Troyera1b82cc2015-01-30 13:54:40 -060056 local testval=${!literal:-}
Dean Troyerdff49a22014-01-30 15:37:40 -060057
58 [[ -z "$testval" ]] && { echo "$default"; return; }
59 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
60 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
61 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -050062 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060063}
64
65
66# Control Functions
67# =================
68
69# Prints backtrace info
70# filename:lineno:function
71# backtrace level
72function backtrace {
73 local level=$1
74 local deep=$((${#BASH_SOURCE[@]} - 1))
75 echo "[Call Trace]"
76 while [ $level -le $deep ]; do
77 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
78 deep=$((deep - 1))
79 done
80}
81
82# Prints line number and "message" then exits
83# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +110084function die {
Dean Troyerdff49a22014-01-30 15:37:40 -060085 local exitcode=$?
86 set +o xtrace
87 local line=$1; shift
88 if [ $exitcode == 0 ]; then
89 exitcode=1
90 fi
91 backtrace 2
92 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -060093 # Give buffers a second to flush
94 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -060095 exit $exitcode
96}
97
98# Checks an environment variable is not set or has length 0 OR if the
99# exit code is non-zero and prints "message" and exits
100# NOTE: env-var is the variable name without a '$'
101# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100102function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600103 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500104 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600105 set +o xtrace
106 local line=$1; shift
107 local evar=$1; shift
108 if ! is_set $evar || [ $exitcode != 0 ]; then
109 die $line "$*"
110 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500111 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600112}
113
114# Prints line number and "message" in error format
115# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100116function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600117 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500118 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600119 set +o xtrace
120 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
121 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600122 if [[ -n ${LOGDIR} ]]; then
123 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600124 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500125 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600126 return $exitcode
127}
128
129# Checks an environment variable is not set or has length 0 OR if the
130# exit code is non-zero and prints "message"
131# NOTE: env-var is the variable name without a '$'
132# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100133function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600134 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500135 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600136 set +o xtrace
137 local line=$1; shift
138 local evar=$1; shift
139 if ! is_set $evar || [ $exitcode != 0 ]; then
140 err $line "$*"
141 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500142 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600143 return $exitcode
144}
145
146# Exit after outputting a message about the distribution not being supported.
147# exit_distro_not_supported [optional-string-telling-what-is-missing]
148function exit_distro_not_supported {
149 if [[ -z "$DISTRO" ]]; then
150 GetDistro
151 fi
152
153 if [ $# -gt 0 ]; then
154 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
155 else
156 die $LINENO "Support for $DISTRO is incomplete."
157 fi
158}
159
160# Test if the named environment variable is set and not zero length
161# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100162function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600163 local var=\$"$1"
164 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
165}
166
167# Prints line number and "message" in warning format
168# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100169function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600170 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500171 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600172 set +o xtrace
173 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
174 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600175 if [[ -n ${LOGDIR} ]]; then
176 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600177 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500178 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600179 return $exitcode
180}
181
182
183# Distro Functions
184# ================
185
186# Determine OS Vendor, Release and Update
187# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
188# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500189# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
190# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
191# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
192# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
193# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500194os_VENDOR=""
195os_RELEASE=""
196os_UPDATE=""
197os_PACKAGE=""
198os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500199
Dean Troyerdff49a22014-01-30 15:37:40 -0600200# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100201function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500202
Dean Troyerdff49a22014-01-30 15:37:40 -0600203 # Figure out which vendor we are
204 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
205 # OS/X
206 os_VENDOR=`sw_vers -productName`
207 os_RELEASE=`sw_vers -productVersion`
208 os_UPDATE=${os_RELEASE##*.}
209 os_RELEASE=${os_RELEASE%.*}
210 os_PACKAGE=""
211 if [[ "$os_RELEASE" =~ "10.7" ]]; then
212 os_CODENAME="lion"
213 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
214 os_CODENAME="snow leopard"
215 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
216 os_CODENAME="leopard"
217 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
218 os_CODENAME="tiger"
219 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
220 os_CODENAME="panther"
221 else
222 os_CODENAME=""
223 fi
224 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
225 os_VENDOR=$(lsb_release -i -s)
226 os_RELEASE=$(lsb_release -r -s)
227 os_UPDATE=""
228 os_PACKAGE="rpm"
229 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
230 os_PACKAGE="deb"
231 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
232 lsb_release -d -s | grep -q openSUSE
233 if [[ $? -eq 0 ]]; then
234 os_VENDOR="openSUSE"
235 fi
236 elif [[ $os_VENDOR == "openSUSE project" ]]; then
237 os_VENDOR="openSUSE"
238 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
239 os_VENDOR="Red Hat"
240 fi
241 os_CODENAME=$(lsb_release -c -s)
242 elif [[ -r /etc/redhat-release ]]; then
243 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
244 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
245 # CentOS release 5.5 (Final)
246 # CentOS Linux release 6.0 (Final)
247 # Fedora release 16 (Verne)
248 # XenServer release 6.2.0-70446c (xenenterprise)
249 os_CODENAME=""
250 for r in "Red Hat" CentOS Fedora XenServer; do
251 os_VENDOR=$r
252 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
253 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
254 os_CODENAME=${ver#*|}
255 os_RELEASE=${ver%|*}
256 os_UPDATE=${os_RELEASE##*.}
257 os_RELEASE=${os_RELEASE%.*}
258 break
259 fi
260 os_VENDOR=""
261 done
262 os_PACKAGE="rpm"
263 elif [[ -r /etc/SuSE-release ]]; then
264 for r in openSUSE "SUSE Linux"; do
265 if [[ "$r" = "SUSE Linux" ]]; then
266 os_VENDOR="SUSE LINUX"
267 else
268 os_VENDOR=$r
269 fi
270
271 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
272 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
273 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
274 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
275 break
276 fi
277 os_VENDOR=""
278 done
279 os_PACKAGE="rpm"
280 # If lsb_release is not installed, we should be able to detect Debian OS
281 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
282 os_VENDOR="Debian"
283 os_PACKAGE="deb"
284 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
285 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
286 fi
287 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
288}
289
290# Translate the OS version values into common nomenclature
291# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500292declare DISTRO
293
Ian Wienandaee18c72014-02-21 15:35:08 +1100294function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600295 GetOSVersion
296 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
297 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
298 DISTRO=$os_CODENAME
299 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
300 # For Fedora, just use 'f' and the release
301 DISTRO="f$os_RELEASE"
302 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
303 DISTRO="opensuse-$os_RELEASE"
304 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
305 # For SLE, also use the service pack
306 if [[ -z "$os_UPDATE" ]]; then
307 DISTRO="sle${os_RELEASE}"
308 else
309 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
310 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530311 elif [[ "$os_VENDOR" =~ (Red Hat) || \
312 "$os_VENDOR" =~ (CentOS) || \
313 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600314 # Drop the . release as we assume it's compatible
315 DISTRO="rhel${os_RELEASE::1}"
316 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
317 DISTRO="xs$os_RELEASE"
318 else
319 # Catch-all for now is Vendor + Release + Update
320 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
321 fi
322 export DISTRO
323}
324
325# Utility function for checking machine architecture
326# is_arch arch-type
327function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500328 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600329}
330
331# Determine if current distribution is a Fedora-based distribution
332# (Fedora, RHEL, CentOS, etc).
333# is_fedora
334function is_fedora {
335 if [[ -z "$os_VENDOR" ]]; then
336 GetOSVersion
337 fi
338
anju Tiwari6c639c92014-07-15 18:11:54 +0530339 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
340 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600341}
342
343
344# Determine if current distribution is a SUSE-based distribution
345# (openSUSE, SLE).
346# is_suse
347function is_suse {
348 if [[ -z "$os_VENDOR" ]]; then
349 GetOSVersion
350 fi
351
352 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
353}
354
355
356# Determine if current distribution is an Ubuntu-based distribution
357# It will also detect non-Ubuntu but Debian-based distros
358# is_ubuntu
359function is_ubuntu {
360 if [[ -z "$os_PACKAGE" ]]; then
361 GetOSVersion
362 fi
363 [ "$os_PACKAGE" = "deb" ]
364}
365
366
367# Git Functions
368# =============
369
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600370# Returns openstack release name for a given branch name
371# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100372function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600373 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700374 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600375 echo ${branch#*/}
376 else
377 echo "master"
378 fi
379}
380
Dean Troyerdff49a22014-01-30 15:37:40 -0600381# git clone only if directory doesn't exist already. Since ``DEST`` might not
382# be owned by the installation user, we create the directory and change the
383# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500384# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
385# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600386# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500387# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600388# git_clone remote dest-dir branch
389function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500390 local git_remote=$1
391 local git_dest=$2
392 local git_ref=$3
393 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200394 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500395
Sean Dague53753292014-12-04 19:38:15 -0500396 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800397 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200398 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
399 fi
400
Dean Troyerdff49a22014-01-30 15:37:40 -0600401 if [[ "$OFFLINE" = "True" ]]; then
402 echo "Running in offline mode, clones already exist"
403 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500404 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600405 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400406 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600407 return
408 fi
409
Dean Troyer50cda692014-07-25 11:57:20 -0500410 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600411 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500412 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600413 [[ "$ERROR_ON_CLONE" = "True" ]] && \
414 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200415 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600416 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500417 cd $git_dest
418 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600419 else
420 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500421 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600422 [[ "$ERROR_ON_CLONE" = "True" ]] && \
423 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200424 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500425 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600426 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500427 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600428 elif [[ "$RECLONE" = "True" ]]; then
429 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500430 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600431 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500432 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100433 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600434 # remove the existing ignored files (like pyc) as they cause breakage
435 # (due to the py files having older timestamps than our pyc, so python
436 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500437 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600438
Dean Troyer50cda692014-07-25 11:57:20 -0500439 # handle git_ref accordingly to type (tag, branch)
440 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
441 git_update_tag $git_ref
442 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
443 git_update_branch $git_ref
444 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
445 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600446 else
Dean Troyer50cda692014-07-25 11:57:20 -0500447 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600448 fi
449
450 fi
451 fi
452
453 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500454 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600455 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400456 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600457}
458
Sean Daguecc524062014-10-01 09:06:43 -0400459# A variation on git clone that lets us specify a project by it's
460# actual name, like oslo.config. This is exceptionally useful in the
461# library installation case
462function git_clone_by_name {
463 local name=$1
464 local repo=${GITREPO[$name]}
465 local dir=${GITDIR[$name]}
466 local branch=${GITBRANCH[$name]}
467 git_clone $repo $dir $branch
468}
469
470
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100471# git can sometimes get itself infinitely stuck with transient network
472# errors or other issues with the remote end. This wraps git in a
473# timeout/retry loop and is intended to watch over non-local git
474# processes that might hang. GIT_TIMEOUT, if set, is passed directly
475# to timeout(1); otherwise the default value of 0 maintains the status
476# quo of waiting forever.
477# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100478function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100479 local count=0
480 local timeout=0
481
482 if [[ -n "${GIT_TIMEOUT}" ]]; then
483 timeout=${GIT_TIMEOUT}
484 fi
485
486 until timeout -s SIGINT ${timeout} git "$@"; do
487 # 124 is timeout(1)'s special return code when it reached the
488 # timeout; otherwise assume fatal failure
489 if [[ $? -ne 124 ]]; then
490 die $LINENO "git call failed: [git $@]"
491 fi
492
493 count=$(($count + 1))
494 warn "timeout ${count} for git call: [git $@]"
495 if [ $count -eq 3 ]; then
496 die $LINENO "Maximum of 3 git retries reached"
497 fi
498 sleep 5
499 done
500}
501
Dean Troyerdff49a22014-01-30 15:37:40 -0600502# git update using reference as a branch.
503# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100504function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500505 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600506
Dean Troyer50cda692014-07-25 11:57:20 -0500507 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600508 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500509 git branch -D $git_branch || true
510 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600511}
512
513# git update using reference as a branch.
514# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100515function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500516 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600517
Dean Troyer50cda692014-07-25 11:57:20 -0500518 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600519}
520
521# git update using reference as a tag. Be careful editing source at that repo
522# as working copy will be in a detached mode
523# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100524function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500525 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600526
Dean Troyer50cda692014-07-25 11:57:20 -0500527 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600528 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500529 git_timed fetch origin tag $git_tag
530 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600531}
532
533
534# OpenStack Functions
535# ===================
536
537# Get the default value for HOST_IP
538# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100539function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600540 local fixed_range=$1
541 local floating_range=$2
542 local host_ip_iface=$3
543 local host_ip=$4
544
545 # Find the interface used for the default route
546 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
547 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
548 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
549 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500550 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
551 local ip
552 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600553 # Attempt to filter out IP addresses that are part of the fixed and
554 # floating range. Note that this method only works if the ``netaddr``
555 # python library is installed. If it is not installed, an error
556 # will be printed and the first IP from the interface will be used.
557 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
558 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500559 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
560 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600561 break;
562 fi
563 done
564 fi
565 echo $host_ip
566}
567
Attila Fazekasf71b5002014-05-28 09:52:22 +0200568# Generates hex string from ``size`` byte of pseudo random data
569# generate_hex_string size
570function generate_hex_string {
571 local size=$1
572 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
573}
574
Dean Troyerdff49a22014-01-30 15:37:40 -0600575# Grab a numbered field from python prettytable output
576# Fields are numbered starting with 1
577# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
578# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100579function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500580 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600581 while read data; do
582 if [ "$1" -lt 0 ]; then
583 field="(\$(NF$1))"
584 else
585 field="\$$(($1 + 1))"
586 fi
587 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
588 done
589}
590
591# Add a policy to a policy.json file
592# Do nothing if the policy already exists
593# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100594function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600595 local policy_file=$1
596 local policy_name=$2
597 local policy_perm=$3
598
599 if grep -q ${policy_name} ${policy_file}; then
600 echo "Policy ${policy_name} already exists in ${policy_file}"
601 return
602 fi
603
604 # Add a terminating comma to policy lines without one
605 # Remove the closing '}' and all lines following to the end-of-file
606 local tmpfile=$(mktemp)
607 uniq ${policy_file} | sed -e '
608 s/]$/],/
609 /^[}]/,$d
610 ' > ${tmpfile}
611
612 # Append policy and closing brace
613 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
614 echo "}" >>${tmpfile}
615
616 mv ${tmpfile} ${policy_file}
617}
618
Alistair Coles24779f62014-10-15 18:57:59 +0100619# Gets or creates a domain
620# Usage: get_or_create_domain <name> <description>
621function get_or_create_domain {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500622 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100623 # Gets domain id
624 local domain_id=$(
625 # Gets domain id
626 openstack --os-token=$OS_TOKEN --os-url=$os_url \
627 --os-identity-api-version=3 domain show $1 \
628 -f value -c id 2>/dev/null ||
629 # Creates new domain
630 openstack --os-token=$OS_TOKEN --os-url=$os_url \
631 --os-identity-api-version=3 domain create $1 \
632 --description "$2" \
633 -f value -c id
634 )
635 echo $domain_id
636}
637
Steve Martinellib74e01c2014-12-18 01:35:35 -0500638# Gets or creates group
639# Usage: get_or_create_group <groupname> [<domain> <description>]
640function get_or_create_group {
641 local domain=${2:+--domain ${2}}
642 local desc="${3:-}"
643 local os_url="$KEYSTONE_SERVICE_URI_V3"
644 # Gets group id
645 local group_id=$(
646 # Creates new group with --or-show
647 openstack --os-token=$OS_TOKEN --os-url=$os_url \
648 --os-identity-api-version=3 group create $1 \
649 $domain --description "$desc" --or-show \
650 -f value -c id
651 )
652 echo $group_id
653}
654
Bartosz Górski0abde392014-02-28 14:15:19 +0100655# Gets or creates user
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000656# Usage: get_or_create_user <username> <password> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100657function get_or_create_user {
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000658 if [[ ! -z "$3" ]]; then
659 local email="--email=$3"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200660 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500661 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200662 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100663 local os_cmd="openstack"
664 local domain=""
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000665 if [[ ! -z "$4" ]]; then
666 domain="--domain=$4"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500667 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100668 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100669 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500670 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500671 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100672 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100673 $1 \
674 --password "$2" \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500675 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100676 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500677 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100678 -f value -c id
679 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500680 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100681}
682
683# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100684# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100685function get_or_create_project {
686 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100687 local os_cmd="openstack"
688 local domain=""
689 if [[ ! -z "$2" ]]; then
690 domain="--domain=$2"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500691 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100692 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500693 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500694 # Creates new project with --or-show
695 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100696 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500697 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100698}
699
700# Gets or creates role
701# Usage: get_or_create_role <name>
702function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500703 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500704 # Creates role with --or-show
705 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100706 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500707 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100708}
709
Jamie Lennox9b215db2015-02-10 18:19:57 +1100710# Gets or adds user role to project
711# Usage: get_or_add_user_project_role <role> <user> <project>
712function get_or_add_user_project_role {
Bartosz Górski0abde392014-02-28 14:15:19 +0100713 # Gets user role id
Steve Martinelli5541a612015-01-19 15:58:49 -0500714 local user_role_id=$(openstack role list \
715 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100716 --project $3 \
717 --column "ID" \
718 --column "Name" \
719 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500720 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100721 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500722 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100723 $1 \
724 --user $2 \
725 --project $3 \
726 | grep " id " | get_field 2)
727 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500728 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100729}
730
731# Gets or creates service
732# Usage: get_or_create_service <name> <type> <description>
733function get_or_create_service {
734 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500735 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100736 # Gets service id
737 openstack service show $1 -f value -c id 2>/dev/null ||
738 # Creates new service if not exists
739 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500740 $2 \
741 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100742 --description="$3" \
743 -f value -c id
744 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500745 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100746}
747
748# Gets or creates endpoint
749# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
750function get_or_create_endpoint {
751 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500752 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100753 --column "ID" \
754 --column "Region" \
755 --column "Service Name" \
756 | grep " $2 " \
757 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500758 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100759 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500760 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100761 $1 \
762 --region $2 \
763 --publicurl $3 \
764 --adminurl $4 \
765 --internalurl $5 \
766 | grep " id " | get_field 2)
767 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500768 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100769}
Dean Troyerdff49a22014-01-30 15:37:40 -0600770
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500771
Dean Troyerdff49a22014-01-30 15:37:40 -0600772# Package Functions
773# =================
774
775# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100776function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600777 local pkg_dir
778 if is_ubuntu; then
Monty Taylor81a016d2014-11-15 17:18:13 -0300779 pkg_dir=$FILES/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600780 elif is_fedora; then
781 pkg_dir=$FILES/rpms
782 elif is_suse; then
783 pkg_dir=$FILES/rpms-suse
784 else
785 exit_distro_not_supported "list of packages"
786 fi
787 echo "$pkg_dir"
788}
789
790# Wrapper for ``apt-get`` to set cache and proxy environment variables
791# Uses globals ``OFFLINE``, ``*_proxy``
792# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100793function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500794 local xtrace=$(set +o | grep xtrace)
795 set +o xtrace
796
Dean Troyerdff49a22014-01-30 15:37:40 -0600797 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
798 local sudo="sudo"
799 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500800
801 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500802
Dean Troyerdff49a22014-01-30 15:37:40 -0600803 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500804 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
805 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600806 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
807}
808
809# get_packages() collects a list of package names of any type from the
Monty Taylor81a016d2014-11-15 17:18:13 -0300810# prerequisite files in ``files/{debs|rpms}``. The list is intended
Dean Troyerdff49a22014-01-30 15:37:40 -0600811# to be passed to a package installer such as apt or yum.
812#
813# Only packages required for the services in 1st argument will be
814# included. Two bits of metadata are recognized in the prerequisite files:
815#
816# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
817# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
818# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100819function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500820 local xtrace=$(set +o | grep xtrace)
821 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600822 local services=$@
823 local package_dir=$(_get_package_dir)
Sean Dague53753292014-12-04 19:38:15 -0500824 local file_to_parse=""
825 local service=""
Dean Troyerdff49a22014-01-30 15:37:40 -0600826
Sean Dague53753292014-12-04 19:38:15 -0500827 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
Flavio Percoco5a91c352014-10-31 18:48:00 +0100828
Dean Troyerdff49a22014-01-30 15:37:40 -0600829 if [[ -z "$package_dir" ]]; then
830 echo "No package directory supplied"
831 return 1
832 fi
833 if [[ -z "$DISTRO" ]]; then
834 GetDistro
835 fi
836 for service in ${services//,/ }; do
837 # Allow individual services to specify dependencies
838 if [[ -e ${package_dir}/${service} ]]; then
839 file_to_parse="${file_to_parse} $service"
840 fi
841 # NOTE(sdague) n-api needs glance for now because that's where
842 # glance client is
843 if [[ $service == n-api ]]; then
844 if [[ ! $file_to_parse =~ nova ]]; then
845 file_to_parse="${file_to_parse} nova"
846 fi
847 if [[ ! $file_to_parse =~ glance ]]; then
848 file_to_parse="${file_to_parse} glance"
849 fi
850 elif [[ $service == c-* ]]; then
851 if [[ ! $file_to_parse =~ cinder ]]; then
852 file_to_parse="${file_to_parse} cinder"
853 fi
854 elif [[ $service == ceilometer-* ]]; then
855 if [[ ! $file_to_parse =~ ceilometer ]]; then
856 file_to_parse="${file_to_parse} ceilometer"
857 fi
858 elif [[ $service == s-* ]]; then
859 if [[ ! $file_to_parse =~ swift ]]; then
860 file_to_parse="${file_to_parse} swift"
861 fi
862 elif [[ $service == n-* ]]; then
863 if [[ ! $file_to_parse =~ nova ]]; then
864 file_to_parse="${file_to_parse} nova"
865 fi
866 elif [[ $service == g-* ]]; then
867 if [[ ! $file_to_parse =~ glance ]]; then
868 file_to_parse="${file_to_parse} glance"
869 fi
870 elif [[ $service == key* ]]; then
871 if [[ ! $file_to_parse =~ keystone ]]; then
872 file_to_parse="${file_to_parse} keystone"
873 fi
874 elif [[ $service == q-* ]]; then
875 if [[ ! $file_to_parse =~ neutron ]]; then
876 file_to_parse="${file_to_parse} neutron"
877 fi
Adam Gandelman539ec432014-03-18 18:57:43 -0700878 elif [[ $service == ir-* ]]; then
879 if [[ ! $file_to_parse =~ ironic ]]; then
880 file_to_parse="${file_to_parse} ironic"
881 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600882 fi
883 done
884
885 for file in ${file_to_parse}; do
886 local fname=${package_dir}/${file}
887 local OIFS line package distros distro
888 [[ -e $fname ]] || continue
889
890 OIFS=$IFS
891 IFS=$'\n'
892 for line in $(<${fname}); do
893 if [[ $line =~ "NOPRIME" ]]; then
894 continue
895 fi
896
897 # Assume we want this package
898 package=${line%#*}
899 inst_pkg=1
900
901 # Look for # dist:xxx in comment
902 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
903 # We are using BASH regexp matching feature.
904 package=${BASH_REMATCH[1]}
905 distros=${BASH_REMATCH[2]}
906 # In bash ${VAR,,} will lowecase VAR
907 # Look for a match in the distro list
908 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
909 # If no match then skip this package
910 inst_pkg=0
911 fi
912 fi
913
914 # Look for # testonly in comment
915 if [[ $line =~ (.*)#.*testonly.* ]]; then
916 package=${BASH_REMATCH[1]}
917 # Are we installing test packages? (test for the default value)
918 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
919 # If not installing test packages the skip this package
920 inst_pkg=0
921 fi
922 fi
923
924 if [[ $inst_pkg = 1 ]]; then
925 echo $package
926 fi
927 done
928 IFS=$OIFS
929 done
Sean Dague45917cc2014-02-24 16:09:14 -0500930 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600931}
932
933# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500934# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600935# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400936function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -0500937 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
938 REPOS_UPDATED=${REPOS_UPDATED:-False}
939 RETRY_UPDATE=${RETRY_UPDATE:-False}
940
Paul Linchpiner9e179742014-07-13 22:23:00 -0700941 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400942 return 0
943 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600944
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400945 if is_ubuntu; then
946 local xtrace=$(set +o | grep xtrace)
947 set +o xtrace
948 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
949 # if there are transient errors pulling the updates, that's fine.
950 # It may be secondary repositories that we don't really care about.
951 apt_get update || /bin/true
952 REPOS_UPDATED=True
953 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500954 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400955 fi
956}
957
958function real_install_package {
959 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600960 apt_get install "$@"
961 elif is_fedora; then
962 yum_install "$@"
963 elif is_suse; then
964 zypper_install "$@"
965 else
966 exit_distro_not_supported "installing packages"
967 fi
968}
969
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400970# Distro-agnostic package installer
971# install_package package [package ...]
972function install_package {
973 update_package_repo
974 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
975}
976
Dean Troyerdff49a22014-01-30 15:37:40 -0600977# Distro-agnostic function to tell if a package is installed
978# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100979function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -0600980 if [[ -z "$@" ]]; then
981 return 1
982 fi
983
984 if [[ -z "$os_PACKAGE" ]]; then
985 GetOSVersion
986 fi
987
988 if [[ "$os_PACKAGE" = "deb" ]]; then
989 dpkg -s "$@" > /dev/null 2> /dev/null
990 elif [[ "$os_PACKAGE" = "rpm" ]]; then
991 rpm --quiet -q "$@"
992 else
993 exit_distro_not_supported "finding if a package is installed"
994 fi
995}
996
997# Distro-agnostic package uninstaller
998# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100999function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001000 if is_ubuntu; then
1001 apt_get purge "$@"
1002 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001003 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001004 elif is_suse; then
1005 sudo zypper rm "$@"
1006 else
1007 exit_distro_not_supported "uninstalling packages"
1008 fi
1009}
1010
1011# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001012# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001013# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001014function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001015 [[ "$OFFLINE" = "True" ]] && return
1016 local sudo="sudo"
1017 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001018
1019 # The manual check for missing packages is because yum -y assumes
1020 # missing packages are OK. See
1021 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001022 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1023 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001024 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001025 awk '
1026 BEGIN { fail=0 }
1027 /No package/ { fail=1 }
1028 { print }
1029 END { exit fail }' || \
1030 die $LINENO "Missing packages detected"
1031
1032 # also ensure we catch a yum failure
1033 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001034 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001035 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001036}
1037
1038# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001039# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001040# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001041function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001042 [[ "$OFFLINE" = "True" ]] && return
1043 local sudo="sudo"
1044 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001045 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1046 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001047 zypper --non-interactive install --auto-agree-with-licenses "$@"
1048}
1049
1050
1051# Process Functions
1052# =================
1053
1054# _run_process() is designed to be backgrounded by run_process() to simulate a
1055# fork. It includes the dirty work of closing extra filehandles and preparing log
1056# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001057# from the service name.
1058# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001059# If an optional group is provided sg will be used to set the group of
1060# the command.
1061# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001062function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001063 local service=$1
1064 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001065 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001066
1067 # Undo logging redirections and close the extra descriptors
1068 exec 1>&3
1069 exec 2>&3
1070 exec 3>&-
1071 exec 6>&-
1072
Dean Troyerdde41d02014-12-09 17:47:57 -06001073 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1074 if [[ -n ${LOGDIR} ]]; then
1075 exec 1>&"$real_logfile" 2>&1
1076 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1077 if [[ -n ${SCREEN_LOGDIR} ]]; then
1078 # Drop the backward-compat symlink
1079 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1080 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001081
1082 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1083 export PYTHONUNBUFFERED=1
1084 fi
1085
Dean Troyer3159a822014-08-27 14:13:58 -05001086 # Run under ``setsid`` to force the process to become a session and group leader.
1087 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001088 if [[ -n "$group" ]]; then
1089 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1090 else
1091 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1092 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001093
1094 # Just silently exit this process
1095 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001096}
1097
1098# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1099# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001100# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001101# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001102function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001103 SCREEN_NAME=${SCREEN_NAME:-stack}
1104 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1105
1106 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1107 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1108 fi
1109
1110 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1111}
1112
1113# Find out if a process exists by partial name.
1114# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001115function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001116 local name=$1
1117 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001118 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001119 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001120 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001121}
1122
Dean Troyer3159a822014-08-27 14:13:58 -05001123# Run a single service under screen or directly
1124# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001125# If an optional group is provided sg will be used to run the
1126# command as that group.
1127# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001128function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001129 local service=$1
1130 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001131 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001132
Dean Troyer3159a822014-08-27 14:13:58 -05001133 if is_service_enabled $service; then
1134 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001135 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001136 else
1137 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001138 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001139 fi
1140 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001141}
1142
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001143# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001144# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001145# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001146# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001147# Run a command in a shell in a screen window, if an optional group
1148# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001149function screen_process {
1150 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001151 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001152 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001153
Sean Dagueea22a4f2014-06-27 15:21:41 -04001154 SCREEN_NAME=${SCREEN_NAME:-stack}
1155 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001156 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001157
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001158 # Append the process to the screen rc file
1159 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001160
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001161 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001162
Dean Troyerdde41d02014-12-09 17:47:57 -06001163 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1164 echo "LOGDIR: $LOGDIR"
1165 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1166 echo "log: $real_logfile"
1167 if [[ -n ${LOGDIR} ]]; then
1168 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001169 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001170 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1171 if [[ -n ${SCREEN_LOGDIR} ]]; then
1172 # Drop the backward-compat symlink
1173 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1174 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001175 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001176
1177 # sleep to allow bash to be ready to be send the command - we are
1178 # creating a new window in screen and then sends characters, so if
1179 # bash isn't running by the time we send the command, nothing happens
1180 sleep 3
1181
1182 NL=`echo -ne '\015'`
1183 # This fun command does the following:
1184 # - the passed server command is backgrounded
1185 # - the pid of the background process is saved in the usual place
1186 # - the server process is brought back to the foreground
1187 # - if the server process exits prematurely the fg command errors
1188 # and a message is written to stdout and the process failure file
1189 #
1190 # The pid saved can be used in stop_process() as a process group
1191 # id to kill off all child processes
1192 if [[ -n "$group" ]]; then
1193 command="sg $group '$command'"
1194 fi
1195 screen -S $SCREEN_NAME -p $name -X stuff "$command & echo \$! >$SERVICE_DIR/$SCREEN_NAME/${name}.pid; fg || echo \"$name failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/${name}.failure\"$NL"
Dean Troyerdff49a22014-01-30 15:37:40 -06001196}
1197
1198# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001199# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001200# screen_rc service "command-line"
1201function screen_rc {
1202 SCREEN_NAME=${SCREEN_NAME:-stack}
1203 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1204 if [[ ! -e $SCREENRC ]]; then
1205 # Name the screen session
1206 echo "sessionname $SCREEN_NAME" > $SCREENRC
1207 # Set a reasonable statusbar
1208 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1209 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1210 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1211 echo "screen -t shell bash" >> $SCREENRC
1212 fi
1213 # If this service doesn't already exist in the screenrc file
1214 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1215 NL=`echo -ne '\015'`
1216 echo "screen -t $1 bash" >> $SCREENRC
1217 echo "stuff \"$2$NL\"" >> $SCREENRC
1218
Dean Troyerdde41d02014-12-09 17:47:57 -06001219 if [[ -n ${LOGDIR} ]]; then
1220 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001221 echo "log on" >>$SCREENRC
1222 fi
1223 fi
1224}
1225
1226# Stop a service in screen
1227# If a PID is available use it, kill the whole process group via TERM
1228# If screen is being used kill the screen window; this will catch processes
1229# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001230# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001231# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001232function screen_stop_service {
1233 local service=$1
1234
Dean Troyerdff49a22014-01-30 15:37:40 -06001235 SCREEN_NAME=${SCREEN_NAME:-stack}
1236 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001237 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001238
Dean Troyer3159a822014-08-27 14:13:58 -05001239 if is_service_enabled $service; then
1240 # Clean up the screen window
1241 screen -S $SCREEN_NAME -p $service -X kill
1242 fi
1243}
1244
1245# Stop a service process
1246# If a PID is available use it, kill the whole process group via TERM
1247# If screen is being used kill the screen window; this will catch processes
1248# that did not leave a PID behind
1249# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1250# stop_process service
1251function stop_process {
1252 local service=$1
1253
1254 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001255 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001256
1257 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001258 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001259 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1260 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1261 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001262 fi
1263 if [[ "$USE_SCREEN" = "True" ]]; then
1264 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001265 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001266 fi
1267 fi
1268}
1269
1270# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001271# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001272# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001273function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001274 local service
1275 local failures
1276 SCREEN_NAME=${SCREEN_NAME:-stack}
1277 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1278
1279
1280 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1281 echo "No service status directory found"
1282 return
1283 fi
1284
1285 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001286 # make this -o errexit safe
1287 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001288
1289 for service in $failures; do
1290 service=`basename $service`
1291 service=${service%.failure}
1292 echo "Error: Service $service is not running"
1293 done
1294
1295 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001296 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001297 fi
1298}
1299
Chris Dent2f27a0e2014-09-09 13:46:02 +01001300# Tail a log file in a screen if USE_SCREEN is true.
1301function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001302 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001303 local logfile=$2
1304
Sean Dague53753292014-12-04 19:38:15 -05001305 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001306 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001307 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001308 fi
1309}
1310
Dean Troyerdff49a22014-01-30 15:37:40 -06001311
Dean Troyer3159a822014-08-27 14:13:58 -05001312# Deprecated Functions
1313# --------------------
1314
1315# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1316# fork. It includes the dirty work of closing extra filehandles and preparing log
1317# files to produce the same logs as screen_it(). The log filename is derived
1318# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1319# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1320# _old_run_process service "command-line"
1321function _old_run_process {
1322 local service=$1
1323 local command="$2"
1324
1325 # Undo logging redirections and close the extra descriptors
1326 exec 1>&3
1327 exec 2>&3
1328 exec 3>&-
1329 exec 6>&-
1330
1331 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001332 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1333 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001334
1335 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1336 export PYTHONUNBUFFERED=1
1337 fi
1338
1339 exec /bin/bash -c "$command"
1340 die "$service exec failure: $command"
1341}
1342
1343# old_run_process() launches a child process that closes all file descriptors and
1344# then exec's the passed in command. This is meant to duplicate the semantics
1345# of screen_it() without screen. PIDs are written to
1346# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1347# old_run_process service "command-line"
1348function old_run_process {
1349 local service=$1
1350 local command="$2"
1351
1352 # Spawn the child process
1353 _old_run_process "$service" "$command" &
1354 echo $!
1355}
1356
1357# Compatibility for existing start_XXXX() functions
1358# Uses global ``USE_SCREEN``
1359# screen_it service "command-line"
1360function screen_it {
1361 if is_service_enabled $1; then
1362 # Append the service to the screen rc file
1363 screen_rc "$1" "$2"
1364
1365 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001366 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001367 else
1368 # Spawn directly without screen
1369 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1370 fi
1371 fi
1372}
1373
1374# Compatibility for existing stop_XXXX() functions
1375# Stop a service in screen
1376# If a PID is available use it, kill the whole process group via TERM
1377# If screen is being used kill the screen window; this will catch processes
1378# that did not leave a PID behind
1379# screen_stop service
1380function screen_stop {
1381 # Clean up the screen window
1382 stop_process $1
1383}
1384
1385
Sean Dague2c65e712014-12-18 09:44:56 -05001386# Plugin Functions
1387# =================
1388
1389DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1390
1391# enable_plugin <name> <url> [branch]
1392#
1393# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1394# ``url`` is a git url
1395# ``branch`` is a gitref. If it's not set, defaults to master
1396function enable_plugin {
1397 local name=$1
1398 local url=$2
1399 local branch=${3:-master}
1400 DEVSTACK_PLUGINS+=",$name"
1401 GITREPO[$name]=$url
1402 GITDIR[$name]=$DEST/$name
1403 GITBRANCH[$name]=$branch
1404}
1405
1406# fetch_plugins
1407#
1408# clones all plugins
1409function fetch_plugins {
1410 local plugins="${DEVSTACK_PLUGINS}"
1411 local plugin
1412
1413 # short circuit if nothing to do
1414 if [[ -z $plugins ]]; then
1415 return
1416 fi
1417
1418 echo "Fetching devstack plugins"
1419 for plugin in ${plugins//,/ }; do
1420 git_clone_by_name $plugin
1421 done
1422}
1423
1424# load_plugin_settings
1425#
1426# Load settings from plugins in the order that they were registered
1427function load_plugin_settings {
1428 local plugins="${DEVSTACK_PLUGINS}"
1429 local plugin
1430
1431 # short circuit if nothing to do
1432 if [[ -z $plugins ]]; then
1433 return
1434 fi
1435
1436 echo "Loading plugin settings"
1437 for plugin in ${plugins//,/ }; do
1438 local dir=${GITDIR[$plugin]}
1439 # source any known settings
1440 if [[ -f $dir/devstack/settings ]]; then
1441 source $dir/devstack/settings
1442 fi
1443 done
1444}
1445
1446# run_plugins
1447#
1448# Run the devstack/plugin.sh in all the plugin directories. These are
1449# run in registration order.
1450function run_plugins {
1451 local mode=$1
1452 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301453
1454 local plugins="${DEVSTACK_PLUGINS}"
1455 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001456 for plugin in ${plugins//,/ }; do
1457 local dir=${GITDIR[$plugin]}
1458 if [[ -f $dir/devstack/plugin.sh ]]; then
1459 source $dir/devstack/plugin.sh $mode $phase
1460 fi
1461 done
1462}
1463
1464function run_phase {
1465 local mode=$1
1466 local phase=$2
1467 if [[ -d $TOP_DIR/extras.d ]]; then
1468 for i in $TOP_DIR/extras.d/*.sh; do
1469 [[ -r $i ]] && source $i $mode $phase
1470 done
1471 fi
1472 # the source phase corresponds to settings loading in plugins
1473 if [[ "$mode" == "source" ]]; then
1474 load_plugin_settings
1475 else
1476 run_plugins $mode $phase
1477 fi
1478}
1479
Dean Troyerdff49a22014-01-30 15:37:40 -06001480
1481# Service Functions
1482# =================
1483
1484# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1485# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001486function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001487 echo "$1" | sed -e '
1488 s/,,/,/g;
1489 s/^,//;
1490 s/,$//
1491 '
1492}
1493
1494# disable_all_services() removes all current services
1495# from ``ENABLED_SERVICES`` to reset the configuration
1496# before a minimal installation
1497# Uses global ``ENABLED_SERVICES``
1498# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001499function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001500 ENABLED_SERVICES=""
1501}
1502
1503# Remove all services starting with '-'. For example, to install all default
1504# services except rabbit (rabbit) set in ``localrc``:
1505# ENABLED_SERVICES+=",-rabbit"
1506# Uses global ``ENABLED_SERVICES``
1507# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001508function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001509 local tmpsvcs="${ENABLED_SERVICES}"
1510 local service
1511 for service in ${tmpsvcs//,/ }; do
1512 if [[ ${service} == -* ]]; then
1513 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1514 fi
1515 done
1516 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1517}
1518
1519# disable_service() removes the services passed as argument to the
1520# ``ENABLED_SERVICES`` list, if they are present.
1521#
1522# For example:
1523# disable_service rabbit
1524#
1525# This function does not know about the special cases
1526# for nova, glance, and neutron built into is_service_enabled().
1527# Uses global ``ENABLED_SERVICES``
1528# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001529function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001530 local tmpsvcs=",${ENABLED_SERVICES},"
1531 local service
1532 for service in $@; do
1533 if is_service_enabled $service; then
1534 tmpsvcs=${tmpsvcs//,$service,/,}
1535 fi
1536 done
1537 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1538}
1539
1540# enable_service() adds the services passed as argument to the
1541# ``ENABLED_SERVICES`` list, if they are not already present.
1542#
1543# For example:
1544# enable_service qpid
1545#
1546# This function does not know about the special cases
1547# for nova, glance, and neutron built into is_service_enabled().
1548# Uses global ``ENABLED_SERVICES``
1549# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001550function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001551 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001552 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001553 for service in $@; do
1554 if ! is_service_enabled $service; then
1555 tmpsvcs+=",$service"
1556 fi
1557 done
1558 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1559 disable_negated_services
1560}
1561
1562# is_service_enabled() checks if the service(s) specified as arguments are
1563# enabled by the user in ``ENABLED_SERVICES``.
1564#
1565# Multiple services specified as arguments are ``OR``'ed together; the test
1566# is a short-circuit boolean, i.e it returns on the first match.
1567#
1568# There are special cases for some 'catch-all' services::
1569# **nova** returns true if any service enabled start with **n-**
1570# **cinder** returns true if any service enabled start with **c-**
1571# **ceilometer** returns true if any service enabled start with **ceilometer**
1572# **glance** returns true if any service enabled start with **g-**
1573# **neutron** returns true if any service enabled start with **q-**
1574# **swift** returns true if any service enabled start with **s-**
1575# **trove** returns true if any service enabled start with **tr-**
1576# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1577# **s-** services will be enabled. This will be deprecated in the future.
1578#
1579# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1580# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1581# as enabled in this case.
1582#
1583# Uses global ``ENABLED_SERVICES``
1584# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001585function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001586 local xtrace=$(set +o | grep xtrace)
1587 set +o xtrace
1588 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001589 local services=$@
1590 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001591 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001592 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001593
1594 # Look for top-level 'enabled' function for this service
1595 if type is_${service}_enabled >/dev/null 2>&1; then
1596 # A function exists for this service, use it
1597 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001598 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001599 fi
1600
1601 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1602 # are implemented
1603
Sean Dague45917cc2014-02-24 16:09:14 -05001604 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001605 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001606 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1607 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1608 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1609 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1610 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1611 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1612 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1613 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1614 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001615 done
Sean Dague45917cc2014-02-24 16:09:14 -05001616 $xtrace
1617 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001618}
1619
1620# Toggle enable/disable_service for services that must run exclusive of each other
1621# $1 The name of a variable containing a space-separated list of services
1622# $2 The name of a variable in which to store the enabled service's name
1623# $3 The name of the service to enable
1624function use_exclusive_service {
1625 local options=${!1}
1626 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001627 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001628 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001629 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001630 for opt in $options;do
1631 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1632 done
1633 eval "$out=$selection"
1634 return 0
1635}
1636
1637
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001638# System Functions
1639# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001640
1641# Only run the command if the target file (the last arg) is not on an
1642# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001643function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001644 local xtrace=$(set +o | grep xtrace)
1645 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001646 local args=( $@ )
1647 local last
1648 local sudo_cmd
1649 local dir_to_check
1650
1651 let last="${#args[*]} - 1"
1652
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001653 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001654 if [ ! -d "$dir_to_check" ]; then
1655 dir_to_check=`dirname "$dir_to_check"`
1656 fi
1657
1658 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001659 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001660 return 0
1661 fi
1662
1663 if [[ $TRACK_DEPENDS = True ]]; then
1664 sudo_cmd="env"
1665 else
1666 sudo_cmd="sudo"
1667 fi
1668
Sean Dague45917cc2014-02-24 16:09:14 -05001669 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001670 $sudo_cmd $@
1671}
1672
1673# Exit 0 if address is in network or 1 if address is not in network
1674# ip-range is in CIDR notation: 1.2.3.4/20
1675# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001676function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001677 local ip=$1
1678 local range=$2
1679 local masklen=${range#*/}
1680 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1681 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1682 [[ $network == $subnet ]]
1683}
1684
1685# Add a user to a group.
1686# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001687function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001688 local user=$1
1689 local group=$2
1690
1691 if [[ -z "$os_VENDOR" ]]; then
1692 GetOSVersion
1693 fi
1694
1695 # SLE11 and openSUSE 12.2 don't have the usual usermod
1696 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1697 sudo usermod -a -G "$group" "$user"
1698 else
1699 sudo usermod -A "$group" "$user"
1700 fi
1701}
1702
1703# Convert CIDR notation to a IPv4 netmask
1704# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001705function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001706 local maskpat="255 255 255 255"
1707 local maskdgt="254 252 248 240 224 192 128"
1708 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1709 echo ${1-0}.${2-0}.${3-0}.${4-0}
1710}
1711
1712# Gracefully cp only if source file/dir exists
1713# cp_it source destination
1714function cp_it {
1715 if [ -e $1 ] || [ -d $1 ]; then
1716 cp -pRL $1 $2
1717 fi
1718}
1719
1720# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1721# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1722# ``localrc`` or on the command line if necessary::
1723#
1724# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1725#
1726# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1727
Ian Wienandaee18c72014-02-21 15:35:08 +11001728function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001729 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001730 export http_proxy=$http_proxy
1731 fi
Sean Dague53753292014-12-04 19:38:15 -05001732 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001733 export https_proxy=$https_proxy
1734 fi
Sean Dague53753292014-12-04 19:38:15 -05001735 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001736 export no_proxy=$no_proxy
1737 fi
1738}
1739
1740# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001741function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001742 local mount_type=`stat -f -L -c %T $1`
1743 test "$mount_type" == "nfs"
1744}
1745
1746# Return the network portion of the given IP address using netmask
1747# netmask is in the traditional dotted-quad format
1748# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001749function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001750 local ip=$1
1751 local mask=$2
1752 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1753 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1754 echo $subnet
1755}
1756
1757# Service wrapper to restart services
1758# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001759function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001760 if is_ubuntu; then
1761 sudo /usr/sbin/service $1 restart
1762 else
1763 sudo /sbin/service $1 restart
1764 fi
1765}
1766
1767# Only change permissions of a file or directory if it is not on an
1768# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001769function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001770 _safe_permission_operation chmod $@
1771}
1772
1773# Only change ownership of a file or directory if it is not on an NFS
1774# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001775function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001776 _safe_permission_operation chown $@
1777}
1778
1779# Service wrapper to start services
1780# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001781function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001782 if is_ubuntu; then
1783 sudo /usr/sbin/service $1 start
1784 else
1785 sudo /sbin/service $1 start
1786 fi
1787}
1788
1789# Service wrapper to stop services
1790# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001791function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001792 if is_ubuntu; then
1793 sudo /usr/sbin/service $1 stop
1794 else
1795 sudo /sbin/service $1 stop
1796 fi
1797}
1798
1799
1800# Restore xtrace
1801$XTRACE
1802
1803# Local variables:
1804# mode: shell-script
1805# End: