blob: 67697671d3c0c8278533a3db46317479912f7546 [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
Steve Martinelli4599fd12015-03-12 21:30:58 -0400731# Gets or adds group role to project
732# Usage: get_or_add_group_project_role <role> <group> <project>
733function get_or_add_group_project_role {
734 # Gets group role id
735 local group_role_id=$(openstack role list \
736 --group $2 \
737 --project $3 \
738 --column "ID" \
739 --column "Name" \
740 | grep " $1 " | get_field 1)
741 if [[ -z "$group_role_id" ]]; then
742 # Adds role to group
743 group_role_id=$(openstack role add \
744 $1 \
745 --group $2 \
746 --project $3 \
747 | grep " id " | get_field 2)
748 fi
749 echo $group_role_id
750}
751
Bartosz Górski0abde392014-02-28 14:15:19 +0100752# Gets or creates service
753# Usage: get_or_create_service <name> <type> <description>
754function get_or_create_service {
755 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500756 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100757 # Gets service id
758 openstack service show $1 -f value -c id 2>/dev/null ||
759 # Creates new service if not exists
760 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500761 $2 \
762 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100763 --description="$3" \
764 -f value -c id
765 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500766 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100767}
768
769# Gets or creates endpoint
770# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
771function get_or_create_endpoint {
772 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500773 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100774 --column "ID" \
775 --column "Region" \
776 --column "Service Name" \
777 | grep " $2 " \
778 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500779 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100780 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500781 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100782 $1 \
783 --region $2 \
784 --publicurl $3 \
785 --adminurl $4 \
786 --internalurl $5 \
787 | grep " id " | get_field 2)
788 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500789 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100790}
Dean Troyerdff49a22014-01-30 15:37:40 -0600791
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500792
Dean Troyerdff49a22014-01-30 15:37:40 -0600793# Package Functions
794# =================
795
796# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100797function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600798 local pkg_dir
799 if is_ubuntu; then
Monty Taylor81a016d2014-11-15 17:18:13 -0300800 pkg_dir=$FILES/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600801 elif is_fedora; then
802 pkg_dir=$FILES/rpms
803 elif is_suse; then
804 pkg_dir=$FILES/rpms-suse
805 else
806 exit_distro_not_supported "list of packages"
807 fi
808 echo "$pkg_dir"
809}
810
811# Wrapper for ``apt-get`` to set cache and proxy environment variables
812# Uses globals ``OFFLINE``, ``*_proxy``
813# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100814function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500815 local xtrace=$(set +o | grep xtrace)
816 set +o xtrace
817
Dean Troyerdff49a22014-01-30 15:37:40 -0600818 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
819 local sudo="sudo"
820 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500821
822 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500823
Dean Troyerdff49a22014-01-30 15:37:40 -0600824 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500825 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
826 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600827 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
828}
829
830# get_packages() collects a list of package names of any type from the
Monty Taylor81a016d2014-11-15 17:18:13 -0300831# prerequisite files in ``files/{debs|rpms}``. The list is intended
Dean Troyerdff49a22014-01-30 15:37:40 -0600832# to be passed to a package installer such as apt or yum.
833#
834# Only packages required for the services in 1st argument will be
835# included. Two bits of metadata are recognized in the prerequisite files:
836#
837# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
838# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
839# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100840function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500841 local xtrace=$(set +o | grep xtrace)
842 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600843 local services=$@
844 local package_dir=$(_get_package_dir)
Sean Dague53753292014-12-04 19:38:15 -0500845 local file_to_parse=""
846 local service=""
Dean Troyerdff49a22014-01-30 15:37:40 -0600847
Sean Dague53753292014-12-04 19:38:15 -0500848 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
Flavio Percoco5a91c352014-10-31 18:48:00 +0100849
Dean Troyerdff49a22014-01-30 15:37:40 -0600850 if [[ -z "$package_dir" ]]; then
851 echo "No package directory supplied"
852 return 1
853 fi
854 if [[ -z "$DISTRO" ]]; then
855 GetDistro
856 fi
857 for service in ${services//,/ }; do
858 # Allow individual services to specify dependencies
859 if [[ -e ${package_dir}/${service} ]]; then
860 file_to_parse="${file_to_parse} $service"
861 fi
862 # NOTE(sdague) n-api needs glance for now because that's where
863 # glance client is
864 if [[ $service == n-api ]]; then
865 if [[ ! $file_to_parse =~ nova ]]; then
866 file_to_parse="${file_to_parse} nova"
867 fi
868 if [[ ! $file_to_parse =~ glance ]]; then
869 file_to_parse="${file_to_parse} glance"
870 fi
871 elif [[ $service == c-* ]]; then
872 if [[ ! $file_to_parse =~ cinder ]]; then
873 file_to_parse="${file_to_parse} cinder"
874 fi
875 elif [[ $service == ceilometer-* ]]; then
876 if [[ ! $file_to_parse =~ ceilometer ]]; then
877 file_to_parse="${file_to_parse} ceilometer"
878 fi
879 elif [[ $service == s-* ]]; then
880 if [[ ! $file_to_parse =~ swift ]]; then
881 file_to_parse="${file_to_parse} swift"
882 fi
883 elif [[ $service == n-* ]]; then
884 if [[ ! $file_to_parse =~ nova ]]; then
885 file_to_parse="${file_to_parse} nova"
886 fi
887 elif [[ $service == g-* ]]; then
888 if [[ ! $file_to_parse =~ glance ]]; then
889 file_to_parse="${file_to_parse} glance"
890 fi
891 elif [[ $service == key* ]]; then
892 if [[ ! $file_to_parse =~ keystone ]]; then
893 file_to_parse="${file_to_parse} keystone"
894 fi
895 elif [[ $service == q-* ]]; then
896 if [[ ! $file_to_parse =~ neutron ]]; then
897 file_to_parse="${file_to_parse} neutron"
898 fi
Adam Gandelman539ec432014-03-18 18:57:43 -0700899 elif [[ $service == ir-* ]]; then
900 if [[ ! $file_to_parse =~ ironic ]]; then
901 file_to_parse="${file_to_parse} ironic"
902 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600903 fi
904 done
905
906 for file in ${file_to_parse}; do
907 local fname=${package_dir}/${file}
908 local OIFS line package distros distro
909 [[ -e $fname ]] || continue
910
911 OIFS=$IFS
912 IFS=$'\n'
913 for line in $(<${fname}); do
914 if [[ $line =~ "NOPRIME" ]]; then
915 continue
916 fi
917
918 # Assume we want this package
919 package=${line%#*}
920 inst_pkg=1
921
922 # Look for # dist:xxx in comment
923 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
924 # We are using BASH regexp matching feature.
925 package=${BASH_REMATCH[1]}
926 distros=${BASH_REMATCH[2]}
927 # In bash ${VAR,,} will lowecase VAR
928 # Look for a match in the distro list
929 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
930 # If no match then skip this package
931 inst_pkg=0
932 fi
933 fi
934
935 # Look for # testonly in comment
936 if [[ $line =~ (.*)#.*testonly.* ]]; then
937 package=${BASH_REMATCH[1]}
938 # Are we installing test packages? (test for the default value)
939 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
940 # If not installing test packages the skip this package
941 inst_pkg=0
942 fi
943 fi
944
945 if [[ $inst_pkg = 1 ]]; then
946 echo $package
947 fi
948 done
949 IFS=$OIFS
950 done
Sean Dague45917cc2014-02-24 16:09:14 -0500951 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600952}
953
954# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500955# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600956# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400957function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -0500958 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
959 REPOS_UPDATED=${REPOS_UPDATED:-False}
960 RETRY_UPDATE=${RETRY_UPDATE:-False}
961
Paul Linchpiner9e179742014-07-13 22:23:00 -0700962 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400963 return 0
964 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600965
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400966 if is_ubuntu; then
967 local xtrace=$(set +o | grep xtrace)
968 set +o xtrace
969 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
970 # if there are transient errors pulling the updates, that's fine.
971 # It may be secondary repositories that we don't really care about.
972 apt_get update || /bin/true
973 REPOS_UPDATED=True
974 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500975 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400976 fi
977}
978
979function real_install_package {
980 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600981 apt_get install "$@"
982 elif is_fedora; then
983 yum_install "$@"
984 elif is_suse; then
985 zypper_install "$@"
986 else
987 exit_distro_not_supported "installing packages"
988 fi
989}
990
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400991# Distro-agnostic package installer
992# install_package package [package ...]
993function install_package {
994 update_package_repo
995 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
996}
997
Dean Troyerdff49a22014-01-30 15:37:40 -0600998# Distro-agnostic function to tell if a package is installed
999# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001000function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001001 if [[ -z "$@" ]]; then
1002 return 1
1003 fi
1004
1005 if [[ -z "$os_PACKAGE" ]]; then
1006 GetOSVersion
1007 fi
1008
1009 if [[ "$os_PACKAGE" = "deb" ]]; then
1010 dpkg -s "$@" > /dev/null 2> /dev/null
1011 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1012 rpm --quiet -q "$@"
1013 else
1014 exit_distro_not_supported "finding if a package is installed"
1015 fi
1016}
1017
1018# Distro-agnostic package uninstaller
1019# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001020function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001021 if is_ubuntu; then
1022 apt_get purge "$@"
1023 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001024 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001025 elif is_suse; then
1026 sudo zypper rm "$@"
1027 else
1028 exit_distro_not_supported "uninstalling packages"
1029 fi
1030}
1031
1032# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001033# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001034# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001035function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001036 [[ "$OFFLINE" = "True" ]] && return
1037 local sudo="sudo"
1038 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001039
1040 # The manual check for missing packages is because yum -y assumes
1041 # missing packages are OK. See
1042 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001043 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1044 no_proxy=$no_proxy \
Ian Wienand36298ee2015-02-04 10:29:31 +11001045 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001046 awk '
1047 BEGIN { fail=0 }
1048 /No package/ { fail=1 }
1049 { print }
1050 END { exit fail }' || \
1051 die $LINENO "Missing packages detected"
1052
1053 # also ensure we catch a yum failure
1054 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001055 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001056 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001057}
1058
1059# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001060# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001061# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001062function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001063 [[ "$OFFLINE" = "True" ]] && return
1064 local sudo="sudo"
1065 [[ "$(id -u)" = "0" ]] && sudo="env"
1066 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1067 zypper --non-interactive install --auto-agree-with-licenses "$@"
1068}
1069
1070
1071# Process Functions
1072# =================
1073
1074# _run_process() is designed to be backgrounded by run_process() to simulate a
1075# fork. It includes the dirty work of closing extra filehandles and preparing log
1076# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001077# from the service name.
1078# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001079# If an optional group is provided sg will be used to set the group of
1080# the command.
1081# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001082function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001083 local service=$1
1084 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001085 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001086
1087 # Undo logging redirections and close the extra descriptors
1088 exec 1>&3
1089 exec 2>&3
1090 exec 3>&-
1091 exec 6>&-
1092
Dean Troyerdde41d02014-12-09 17:47:57 -06001093 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1094 if [[ -n ${LOGDIR} ]]; then
1095 exec 1>&"$real_logfile" 2>&1
1096 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1097 if [[ -n ${SCREEN_LOGDIR} ]]; then
1098 # Drop the backward-compat symlink
1099 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1100 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001101
1102 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1103 export PYTHONUNBUFFERED=1
1104 fi
1105
Dean Troyer3159a822014-08-27 14:13:58 -05001106 # Run under ``setsid`` to force the process to become a session and group leader.
1107 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001108 if [[ -n "$group" ]]; then
1109 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1110 else
1111 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1112 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001113
1114 # Just silently exit this process
1115 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001116}
1117
1118# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1119# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001120# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001121# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001122function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001123 SCREEN_NAME=${SCREEN_NAME:-stack}
1124 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1125
1126 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1127 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1128 fi
1129
1130 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1131}
1132
1133# Find out if a process exists by partial name.
1134# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001135function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001136 local name=$1
1137 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001138 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001139 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001140 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001141}
1142
Dean Troyer3159a822014-08-27 14:13:58 -05001143# Run a single service under screen or directly
1144# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001145# If an optional group is provided sg will be used to run the
1146# command as that group.
1147# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001148function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001149 local service=$1
1150 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001151 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001152
Dean Troyer3159a822014-08-27 14:13:58 -05001153 if is_service_enabled $service; then
1154 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001155 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001156 else
1157 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001158 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001159 fi
1160 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001161}
1162
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001163# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001164# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001165# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001166# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001167# Run a command in a shell in a screen window, if an optional group
1168# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001169function screen_process {
1170 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001171 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001172 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001173
Sean Dagueea22a4f2014-06-27 15:21:41 -04001174 SCREEN_NAME=${SCREEN_NAME:-stack}
1175 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001176 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001177
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001178 # Append the process to the screen rc file
1179 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001180
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001181 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001182
Dean Troyerdde41d02014-12-09 17:47:57 -06001183 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1184 echo "LOGDIR: $LOGDIR"
1185 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1186 echo "log: $real_logfile"
1187 if [[ -n ${LOGDIR} ]]; then
1188 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001189 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001190 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1191 if [[ -n ${SCREEN_LOGDIR} ]]; then
1192 # Drop the backward-compat symlink
1193 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1194 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001195 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001196
1197 # sleep to allow bash to be ready to be send the command - we are
1198 # creating a new window in screen and then sends characters, so if
1199 # bash isn't running by the time we send the command, nothing happens
1200 sleep 3
1201
1202 NL=`echo -ne '\015'`
1203 # This fun command does the following:
1204 # - the passed server command is backgrounded
1205 # - the pid of the background process is saved in the usual place
1206 # - the server process is brought back to the foreground
1207 # - if the server process exits prematurely the fg command errors
1208 # and a message is written to stdout and the process failure file
1209 #
1210 # The pid saved can be used in stop_process() as a process group
1211 # id to kill off all child processes
1212 if [[ -n "$group" ]]; then
1213 command="sg $group '$command'"
1214 fi
1215 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 -06001216}
1217
1218# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001219# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001220# screen_rc service "command-line"
1221function screen_rc {
1222 SCREEN_NAME=${SCREEN_NAME:-stack}
1223 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1224 if [[ ! -e $SCREENRC ]]; then
1225 # Name the screen session
1226 echo "sessionname $SCREEN_NAME" > $SCREENRC
1227 # Set a reasonable statusbar
1228 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1229 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1230 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1231 echo "screen -t shell bash" >> $SCREENRC
1232 fi
1233 # If this service doesn't already exist in the screenrc file
1234 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1235 NL=`echo -ne '\015'`
1236 echo "screen -t $1 bash" >> $SCREENRC
1237 echo "stuff \"$2$NL\"" >> $SCREENRC
1238
Dean Troyerdde41d02014-12-09 17:47:57 -06001239 if [[ -n ${LOGDIR} ]]; then
1240 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001241 echo "log on" >>$SCREENRC
1242 fi
1243 fi
1244}
1245
1246# Stop a service in screen
1247# If a PID is available use it, kill the whole process group via TERM
1248# If screen is being used kill the screen window; this will catch processes
1249# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001250# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001251# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001252function screen_stop_service {
1253 local service=$1
1254
Dean Troyerdff49a22014-01-30 15:37:40 -06001255 SCREEN_NAME=${SCREEN_NAME:-stack}
1256 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001257 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001258
Dean Troyer3159a822014-08-27 14:13:58 -05001259 if is_service_enabled $service; then
1260 # Clean up the screen window
1261 screen -S $SCREEN_NAME -p $service -X kill
1262 fi
1263}
1264
1265# Stop a service process
1266# If a PID is available use it, kill the whole process group via TERM
1267# If screen is being used kill the screen window; this will catch processes
1268# that did not leave a PID behind
1269# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1270# stop_process service
1271function stop_process {
1272 local service=$1
1273
1274 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001275 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001276
1277 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001278 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001279 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1280 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1281 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001282 fi
1283 if [[ "$USE_SCREEN" = "True" ]]; then
1284 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001285 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001286 fi
1287 fi
1288}
1289
1290# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001291# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001292# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001293function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001294 local service
1295 local failures
1296 SCREEN_NAME=${SCREEN_NAME:-stack}
1297 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1298
1299
1300 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1301 echo "No service status directory found"
1302 return
1303 fi
1304
1305 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001306 # make this -o errexit safe
1307 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001308
1309 for service in $failures; do
1310 service=`basename $service`
1311 service=${service%.failure}
1312 echo "Error: Service $service is not running"
1313 done
1314
1315 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001316 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001317 fi
1318}
1319
Chris Dent2f27a0e2014-09-09 13:46:02 +01001320# Tail a log file in a screen if USE_SCREEN is true.
1321function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001322 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001323 local logfile=$2
1324
Sean Dague53753292014-12-04 19:38:15 -05001325 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001326 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001327 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001328 fi
1329}
1330
Dean Troyerdff49a22014-01-30 15:37:40 -06001331
Dean Troyer3159a822014-08-27 14:13:58 -05001332# Deprecated Functions
1333# --------------------
1334
1335# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1336# fork. It includes the dirty work of closing extra filehandles and preparing log
1337# files to produce the same logs as screen_it(). The log filename is derived
1338# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1339# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1340# _old_run_process service "command-line"
1341function _old_run_process {
1342 local service=$1
1343 local command="$2"
1344
1345 # Undo logging redirections and close the extra descriptors
1346 exec 1>&3
1347 exec 2>&3
1348 exec 3>&-
1349 exec 6>&-
1350
1351 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001352 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1353 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001354
1355 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1356 export PYTHONUNBUFFERED=1
1357 fi
1358
1359 exec /bin/bash -c "$command"
1360 die "$service exec failure: $command"
1361}
1362
1363# old_run_process() launches a child process that closes all file descriptors and
1364# then exec's the passed in command. This is meant to duplicate the semantics
1365# of screen_it() without screen. PIDs are written to
1366# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1367# old_run_process service "command-line"
1368function old_run_process {
1369 local service=$1
1370 local command="$2"
1371
1372 # Spawn the child process
1373 _old_run_process "$service" "$command" &
1374 echo $!
1375}
1376
1377# Compatibility for existing start_XXXX() functions
1378# Uses global ``USE_SCREEN``
1379# screen_it service "command-line"
1380function screen_it {
1381 if is_service_enabled $1; then
1382 # Append the service to the screen rc file
1383 screen_rc "$1" "$2"
1384
1385 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001386 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001387 else
1388 # Spawn directly without screen
1389 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1390 fi
1391 fi
1392}
1393
1394# Compatibility for existing stop_XXXX() functions
1395# Stop a service in screen
1396# If a PID is available use it, kill the whole process group via TERM
1397# If screen is being used kill the screen window; this will catch processes
1398# that did not leave a PID behind
1399# screen_stop service
1400function screen_stop {
1401 # Clean up the screen window
1402 stop_process $1
1403}
1404
1405
Sean Dague2c65e712014-12-18 09:44:56 -05001406# Plugin Functions
1407# =================
1408
1409DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1410
1411# enable_plugin <name> <url> [branch]
1412#
1413# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1414# ``url`` is a git url
1415# ``branch`` is a gitref. If it's not set, defaults to master
1416function enable_plugin {
1417 local name=$1
1418 local url=$2
1419 local branch=${3:-master}
1420 DEVSTACK_PLUGINS+=",$name"
1421 GITREPO[$name]=$url
1422 GITDIR[$name]=$DEST/$name
1423 GITBRANCH[$name]=$branch
1424}
1425
1426# fetch_plugins
1427#
1428# clones all plugins
1429function fetch_plugins {
1430 local plugins="${DEVSTACK_PLUGINS}"
1431 local plugin
1432
1433 # short circuit if nothing to do
1434 if [[ -z $plugins ]]; then
1435 return
1436 fi
1437
1438 echo "Fetching devstack plugins"
1439 for plugin in ${plugins//,/ }; do
1440 git_clone_by_name $plugin
1441 done
1442}
1443
1444# load_plugin_settings
1445#
1446# Load settings from plugins in the order that they were registered
1447function load_plugin_settings {
1448 local plugins="${DEVSTACK_PLUGINS}"
1449 local plugin
1450
1451 # short circuit if nothing to do
1452 if [[ -z $plugins ]]; then
1453 return
1454 fi
1455
1456 echo "Loading plugin settings"
1457 for plugin in ${plugins//,/ }; do
1458 local dir=${GITDIR[$plugin]}
1459 # source any known settings
1460 if [[ -f $dir/devstack/settings ]]; then
1461 source $dir/devstack/settings
1462 fi
1463 done
1464}
1465
1466# run_plugins
1467#
1468# Run the devstack/plugin.sh in all the plugin directories. These are
1469# run in registration order.
1470function run_plugins {
1471 local mode=$1
1472 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301473
1474 local plugins="${DEVSTACK_PLUGINS}"
1475 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001476 for plugin in ${plugins//,/ }; do
1477 local dir=${GITDIR[$plugin]}
1478 if [[ -f $dir/devstack/plugin.sh ]]; then
1479 source $dir/devstack/plugin.sh $mode $phase
1480 fi
1481 done
1482}
1483
1484function run_phase {
1485 local mode=$1
1486 local phase=$2
1487 if [[ -d $TOP_DIR/extras.d ]]; then
1488 for i in $TOP_DIR/extras.d/*.sh; do
1489 [[ -r $i ]] && source $i $mode $phase
1490 done
1491 fi
1492 # the source phase corresponds to settings loading in plugins
1493 if [[ "$mode" == "source" ]]; then
1494 load_plugin_settings
1495 else
1496 run_plugins $mode $phase
1497 fi
1498}
1499
Dean Troyerdff49a22014-01-30 15:37:40 -06001500
1501# Service Functions
1502# =================
1503
1504# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1505# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001506function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001507 echo "$1" | sed -e '
1508 s/,,/,/g;
1509 s/^,//;
1510 s/,$//
1511 '
1512}
1513
1514# disable_all_services() removes all current services
1515# from ``ENABLED_SERVICES`` to reset the configuration
1516# before a minimal installation
1517# Uses global ``ENABLED_SERVICES``
1518# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001519function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001520 ENABLED_SERVICES=""
1521}
1522
1523# Remove all services starting with '-'. For example, to install all default
1524# services except rabbit (rabbit) set in ``localrc``:
1525# ENABLED_SERVICES+=",-rabbit"
1526# Uses global ``ENABLED_SERVICES``
1527# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001528function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001529 local tmpsvcs="${ENABLED_SERVICES}"
1530 local service
1531 for service in ${tmpsvcs//,/ }; do
1532 if [[ ${service} == -* ]]; then
1533 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1534 fi
1535 done
1536 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1537}
1538
1539# disable_service() removes the services passed as argument to the
1540# ``ENABLED_SERVICES`` list, if they are present.
1541#
1542# For example:
1543# disable_service rabbit
1544#
1545# This function does not know about the special cases
1546# for nova, glance, and neutron built into is_service_enabled().
1547# Uses global ``ENABLED_SERVICES``
1548# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001549function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001550 local tmpsvcs=",${ENABLED_SERVICES},"
1551 local service
1552 for service in $@; do
1553 if is_service_enabled $service; then
1554 tmpsvcs=${tmpsvcs//,$service,/,}
1555 fi
1556 done
1557 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1558}
1559
1560# enable_service() adds the services passed as argument to the
1561# ``ENABLED_SERVICES`` list, if they are not already present.
1562#
1563# For example:
1564# enable_service qpid
1565#
1566# This function does not know about the special cases
1567# for nova, glance, and neutron built into is_service_enabled().
1568# Uses global ``ENABLED_SERVICES``
1569# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001570function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001571 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001572 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001573 for service in $@; do
1574 if ! is_service_enabled $service; then
1575 tmpsvcs+=",$service"
1576 fi
1577 done
1578 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1579 disable_negated_services
1580}
1581
1582# is_service_enabled() checks if the service(s) specified as arguments are
1583# enabled by the user in ``ENABLED_SERVICES``.
1584#
1585# Multiple services specified as arguments are ``OR``'ed together; the test
1586# is a short-circuit boolean, i.e it returns on the first match.
1587#
1588# There are special cases for some 'catch-all' services::
1589# **nova** returns true if any service enabled start with **n-**
1590# **cinder** returns true if any service enabled start with **c-**
1591# **ceilometer** returns true if any service enabled start with **ceilometer**
1592# **glance** returns true if any service enabled start with **g-**
1593# **neutron** returns true if any service enabled start with **q-**
1594# **swift** returns true if any service enabled start with **s-**
1595# **trove** returns true if any service enabled start with **tr-**
1596# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1597# **s-** services will be enabled. This will be deprecated in the future.
1598#
1599# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1600# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1601# as enabled in this case.
1602#
1603# Uses global ``ENABLED_SERVICES``
1604# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001605function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001606 local xtrace=$(set +o | grep xtrace)
1607 set +o xtrace
1608 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001609 local services=$@
1610 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001611 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001612 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001613
1614 # Look for top-level 'enabled' function for this service
1615 if type is_${service}_enabled >/dev/null 2>&1; then
1616 # A function exists for this service, use it
1617 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001618 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001619 fi
1620
1621 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1622 # are implemented
1623
Sean Dague45917cc2014-02-24 16:09:14 -05001624 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001625 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001626 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1627 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1628 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1629 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1630 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1631 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1632 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1633 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1634 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001635 done
Sean Dague45917cc2014-02-24 16:09:14 -05001636 $xtrace
1637 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001638}
1639
1640# Toggle enable/disable_service for services that must run exclusive of each other
1641# $1 The name of a variable containing a space-separated list of services
1642# $2 The name of a variable in which to store the enabled service's name
1643# $3 The name of the service to enable
1644function use_exclusive_service {
1645 local options=${!1}
1646 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001647 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001648 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001649 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001650 for opt in $options;do
1651 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1652 done
1653 eval "$out=$selection"
1654 return 0
1655}
1656
1657
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001658# System Functions
1659# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001660
1661# Only run the command if the target file (the last arg) is not on an
1662# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001663function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001664 local xtrace=$(set +o | grep xtrace)
1665 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001666 local args=( $@ )
1667 local last
1668 local sudo_cmd
1669 local dir_to_check
1670
1671 let last="${#args[*]} - 1"
1672
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001673 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001674 if [ ! -d "$dir_to_check" ]; then
1675 dir_to_check=`dirname "$dir_to_check"`
1676 fi
1677
1678 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001679 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001680 return 0
1681 fi
1682
1683 if [[ $TRACK_DEPENDS = True ]]; then
1684 sudo_cmd="env"
1685 else
1686 sudo_cmd="sudo"
1687 fi
1688
Sean Dague45917cc2014-02-24 16:09:14 -05001689 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001690 $sudo_cmd $@
1691}
1692
1693# Exit 0 if address is in network or 1 if address is not in network
1694# ip-range is in CIDR notation: 1.2.3.4/20
1695# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001696function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001697 local ip=$1
1698 local range=$2
1699 local masklen=${range#*/}
1700 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1701 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1702 [[ $network == $subnet ]]
1703}
1704
1705# Add a user to a group.
1706# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001707function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001708 local user=$1
1709 local group=$2
1710
1711 if [[ -z "$os_VENDOR" ]]; then
1712 GetOSVersion
1713 fi
1714
1715 # SLE11 and openSUSE 12.2 don't have the usual usermod
1716 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1717 sudo usermod -a -G "$group" "$user"
1718 else
1719 sudo usermod -A "$group" "$user"
1720 fi
1721}
1722
1723# Convert CIDR notation to a IPv4 netmask
1724# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001725function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001726 local maskpat="255 255 255 255"
1727 local maskdgt="254 252 248 240 224 192 128"
1728 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1729 echo ${1-0}.${2-0}.${3-0}.${4-0}
1730}
1731
1732# Gracefully cp only if source file/dir exists
1733# cp_it source destination
1734function cp_it {
1735 if [ -e $1 ] || [ -d $1 ]; then
1736 cp -pRL $1 $2
1737 fi
1738}
1739
1740# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1741# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1742# ``localrc`` or on the command line if necessary::
1743#
1744# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1745#
1746# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1747
Ian Wienandaee18c72014-02-21 15:35:08 +11001748function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001749 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001750 export http_proxy=$http_proxy
1751 fi
Sean Dague53753292014-12-04 19:38:15 -05001752 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001753 export https_proxy=$https_proxy
1754 fi
Sean Dague53753292014-12-04 19:38:15 -05001755 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001756 export no_proxy=$no_proxy
1757 fi
1758}
1759
1760# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001761function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001762 local mount_type=`stat -f -L -c %T $1`
1763 test "$mount_type" == "nfs"
1764}
1765
1766# Return the network portion of the given IP address using netmask
1767# netmask is in the traditional dotted-quad format
1768# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001769function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001770 local ip=$1
1771 local mask=$2
1772 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1773 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1774 echo $subnet
1775}
1776
1777# Service wrapper to restart services
1778# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001779function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001780 if is_ubuntu; then
1781 sudo /usr/sbin/service $1 restart
1782 else
1783 sudo /sbin/service $1 restart
1784 fi
1785}
1786
1787# Only change permissions of a file or directory if it is not on an
1788# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001789function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001790 _safe_permission_operation chmod $@
1791}
1792
1793# Only change ownership of a file or directory if it is not on an NFS
1794# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001795function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001796 _safe_permission_operation chown $@
1797}
1798
1799# Service wrapper to start services
1800# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001801function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001802 if is_ubuntu; then
1803 sudo /usr/sbin/service $1 start
1804 else
1805 sudo /sbin/service $1 start
1806 fi
1807}
1808
1809# Service wrapper to stop services
1810# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001811function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001812 if is_ubuntu; then
1813 sudo /usr/sbin/service $1 stop
1814 else
1815 sudo /sbin/service $1 stop
1816 fi
1817}
1818
1819
1820# Restore xtrace
1821$XTRACE
1822
1823# Local variables:
1824# mode: shell-script
1825# End: