blob: 3dae8147b5b3239ec54c2bb303bc558692d3e917 [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
Dean Troyerdff49a22014-01-30 15:37:40 -0600545 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
546 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
547 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100548 # Find the interface used for the default route
549 host_ip_iface=${host_ip_iface:-$(ip route | awk '/default/ {print $5}' | head -1)}
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 {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800777 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600778 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800779
780 if [[ -z "$base_dir" ]]; then
781 base_dir=$FILES
782 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600783 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800784 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600785 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800786 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600787 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800788 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600789 else
790 exit_distro_not_supported "list of packages"
791 fi
792 echo "$pkg_dir"
793}
794
795# Wrapper for ``apt-get`` to set cache and proxy environment variables
796# Uses globals ``OFFLINE``, ``*_proxy``
797# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100798function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500799 local xtrace=$(set +o | grep xtrace)
800 set +o xtrace
801
Dean Troyerdff49a22014-01-30 15:37:40 -0600802 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
803 local sudo="sudo"
804 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500805
806 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500807
Dean Troyerdff49a22014-01-30 15:37:40 -0600808 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500809 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
810 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600811 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
812}
813
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800814function _parse_package_files {
815 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600816
Dean Troyerdff49a22014-01-30 15:37:40 -0600817 if [[ -z "$DISTRO" ]]; then
818 GetDistro
819 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600820
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800821 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600822 local OIFS line package distros distro
823 [[ -e $fname ]] || continue
824
825 OIFS=$IFS
826 IFS=$'\n'
827 for line in $(<${fname}); do
828 if [[ $line =~ "NOPRIME" ]]; then
829 continue
830 fi
831
832 # Assume we want this package
833 package=${line%#*}
834 inst_pkg=1
835
836 # Look for # dist:xxx in comment
837 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
838 # We are using BASH regexp matching feature.
839 package=${BASH_REMATCH[1]}
840 distros=${BASH_REMATCH[2]}
841 # In bash ${VAR,,} will lowecase VAR
842 # Look for a match in the distro list
843 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
844 # If no match then skip this package
845 inst_pkg=0
846 fi
847 fi
848
849 # Look for # testonly in comment
850 if [[ $line =~ (.*)#.*testonly.* ]]; then
851 package=${BASH_REMATCH[1]}
852 # Are we installing test packages? (test for the default value)
853 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
854 # If not installing test packages the skip this package
855 inst_pkg=0
856 fi
857 fi
858
859 if [[ $inst_pkg = 1 ]]; then
860 echo $package
861 fi
862 done
863 IFS=$OIFS
864 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800865}
866
867# get_packages() collects a list of package names of any type from the
868# prerequisite files in ``files/{debs|rpms}``. The list is intended
869# to be passed to a package installer such as apt or yum.
870#
871# Only packages required for the services in 1st argument will be
872# included. Two bits of metadata are recognized in the prerequisite files:
873#
874# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
875# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
876# of the package to the distros listed. The distro names are case insensitive.
877function get_packages {
878 local xtrace=$(set +o | grep xtrace)
879 set +o xtrace
880 local services=$@
881 local package_dir=$(_get_package_dir)
882 local file_to_parse=""
883 local service=""
884
885 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
886
887 if [[ -z "$package_dir" ]]; then
888 echo "No package directory supplied"
889 return 1
890 fi
891 for service in ${services//,/ }; do
892 # Allow individual services to specify dependencies
893 if [[ -e ${package_dir}/${service} ]]; then
894 file_to_parse="${file_to_parse} ${package_dir}/${service}"
895 fi
896 # NOTE(sdague) n-api needs glance for now because that's where
897 # glance client is
898 if [[ $service == n-api ]]; then
899 if [[ ! $file_to_parse =~ nova ]]; then
900 file_to_parse="${file_to_parse} ${package_dir}/nova"
901 fi
902 if [[ ! $file_to_parse =~ glance ]]; then
903 file_to_parse="${file_to_parse} ${package_dir}/glance"
904 fi
905 elif [[ $service == c-* ]]; then
906 if [[ ! $file_to_parse =~ cinder ]]; then
907 file_to_parse="${file_to_parse} ${package_dir}/cinder"
908 fi
909 elif [[ $service == ceilometer-* ]]; then
910 if [[ ! $file_to_parse =~ ceilometer ]]; then
911 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
912 fi
913 elif [[ $service == s-* ]]; then
914 if [[ ! $file_to_parse =~ swift ]]; then
915 file_to_parse="${file_to_parse} ${package_dir}/swift"
916 fi
917 elif [[ $service == n-* ]]; then
918 if [[ ! $file_to_parse =~ nova ]]; then
919 file_to_parse="${file_to_parse} ${package_dir}/nova"
920 fi
921 elif [[ $service == g-* ]]; then
922 if [[ ! $file_to_parse =~ glance ]]; then
923 file_to_parse="${file_to_parse} ${package_dir}/glance"
924 fi
925 elif [[ $service == key* ]]; then
926 if [[ ! $file_to_parse =~ keystone ]]; then
927 file_to_parse="${file_to_parse} ${package_dir}/keystone"
928 fi
929 elif [[ $service == q-* ]]; then
930 if [[ ! $file_to_parse =~ neutron ]]; then
931 file_to_parse="${file_to_parse} ${package_dir}/neutron"
932 fi
933 elif [[ $service == ir-* ]]; then
934 if [[ ! $file_to_parse =~ ironic ]]; then
935 file_to_parse="${file_to_parse} ${package_dir}/ironic"
936 fi
937 fi
938 done
939 echo "$(_parse_package_files $file_to_parse)"
940 $xtrace
941}
942
943# get_plugin_packages() collects a list of package names of any type from a
944# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
945# list is intended to be passed to a package installer such as apt or yum.
946#
947# Only packages required for enabled and collected plugins will included.
948#
949# The same metadata used in the main devstack prerequisite files may be used
950# in these prerequisite files, see get_packages() for more info.
951function get_plugin_packages {
952 local xtrace=$(set +o | grep xtrace)
953 set +o xtrace
954 local files_to_parse=""
955 local package_dir=""
956 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
957 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
958 files_to_parse+="$package_dir/$plugin"
959 done
960 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -0500961 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600962}
963
964# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500965# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600966# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400967function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -0500968 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
969 REPOS_UPDATED=${REPOS_UPDATED:-False}
970 RETRY_UPDATE=${RETRY_UPDATE:-False}
971
Paul Linchpiner9e179742014-07-13 22:23:00 -0700972 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400973 return 0
974 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600975
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400976 if is_ubuntu; then
977 local xtrace=$(set +o | grep xtrace)
978 set +o xtrace
979 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
980 # if there are transient errors pulling the updates, that's fine.
981 # It may be secondary repositories that we don't really care about.
982 apt_get update || /bin/true
983 REPOS_UPDATED=True
984 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500985 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400986 fi
987}
988
989function real_install_package {
990 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600991 apt_get install "$@"
992 elif is_fedora; then
993 yum_install "$@"
994 elif is_suse; then
995 zypper_install "$@"
996 else
997 exit_distro_not_supported "installing packages"
998 fi
999}
1000
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001001# Distro-agnostic package installer
1002# install_package package [package ...]
1003function install_package {
1004 update_package_repo
1005 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1006}
1007
Dean Troyerdff49a22014-01-30 15:37:40 -06001008# Distro-agnostic function to tell if a package is installed
1009# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001010function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001011 if [[ -z "$@" ]]; then
1012 return 1
1013 fi
1014
1015 if [[ -z "$os_PACKAGE" ]]; then
1016 GetOSVersion
1017 fi
1018
1019 if [[ "$os_PACKAGE" = "deb" ]]; then
1020 dpkg -s "$@" > /dev/null 2> /dev/null
1021 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1022 rpm --quiet -q "$@"
1023 else
1024 exit_distro_not_supported "finding if a package is installed"
1025 fi
1026}
1027
1028# Distro-agnostic package uninstaller
1029# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001030function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001031 if is_ubuntu; then
1032 apt_get purge "$@"
1033 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001034 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001035 elif is_suse; then
1036 sudo zypper rm "$@"
1037 else
1038 exit_distro_not_supported "uninstalling packages"
1039 fi
1040}
1041
1042# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001043# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001044# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001045function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001046 [[ "$OFFLINE" = "True" ]] && return
1047 local sudo="sudo"
1048 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001049
1050 # The manual check for missing packages is because yum -y assumes
1051 # missing packages are OK. See
1052 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001053 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1054 no_proxy=$no_proxy \
Ian Wienand36298ee2015-02-04 10:29:31 +11001055 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001056 awk '
1057 BEGIN { fail=0 }
1058 /No package/ { fail=1 }
1059 { print }
1060 END { exit fail }' || \
1061 die $LINENO "Missing packages detected"
1062
1063 # also ensure we catch a yum failure
1064 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001065 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001066 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001067}
1068
1069# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001070# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001071# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001072function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001073 [[ "$OFFLINE" = "True" ]] && return
1074 local sudo="sudo"
1075 [[ "$(id -u)" = "0" ]] && sudo="env"
1076 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1077 zypper --non-interactive install --auto-agree-with-licenses "$@"
1078}
1079
1080
1081# Process Functions
1082# =================
1083
1084# _run_process() is designed to be backgrounded by run_process() to simulate a
1085# fork. It includes the dirty work of closing extra filehandles and preparing log
1086# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001087# from the service name.
1088# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001089# If an optional group is provided sg will be used to set the group of
1090# the command.
1091# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001092function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001093 local service=$1
1094 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001095 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001096
1097 # Undo logging redirections and close the extra descriptors
1098 exec 1>&3
1099 exec 2>&3
1100 exec 3>&-
1101 exec 6>&-
1102
Dean Troyerdde41d02014-12-09 17:47:57 -06001103 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1104 if [[ -n ${LOGDIR} ]]; then
1105 exec 1>&"$real_logfile" 2>&1
1106 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1107 if [[ -n ${SCREEN_LOGDIR} ]]; then
1108 # Drop the backward-compat symlink
1109 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1110 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001111
1112 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1113 export PYTHONUNBUFFERED=1
1114 fi
1115
Dean Troyer3159a822014-08-27 14:13:58 -05001116 # Run under ``setsid`` to force the process to become a session and group leader.
1117 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001118 if [[ -n "$group" ]]; then
1119 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1120 else
1121 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1122 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001123
1124 # Just silently exit this process
1125 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001126}
1127
1128# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1129# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001130# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001131# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001132function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001133 SCREEN_NAME=${SCREEN_NAME:-stack}
1134 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1135
1136 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1137 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1138 fi
1139
1140 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1141}
1142
1143# Find out if a process exists by partial name.
1144# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001145function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001146 local name=$1
1147 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001148 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001149 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001150 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001151}
1152
Dean Troyer3159a822014-08-27 14:13:58 -05001153# Run a single service under screen or directly
1154# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001155# If an optional group is provided sg will be used to run the
1156# command as that group.
1157# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001158function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001159 local service=$1
1160 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001161 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001162
Dean Troyer3159a822014-08-27 14:13:58 -05001163 if is_service_enabled $service; then
1164 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001165 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001166 else
1167 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001168 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001169 fi
1170 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001171}
1172
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001173# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001174# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001175# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001176# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001177# Run a command in a shell in a screen window, if an optional group
1178# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001179function screen_process {
1180 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001181 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001182 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001183
Sean Dagueea22a4f2014-06-27 15:21:41 -04001184 SCREEN_NAME=${SCREEN_NAME:-stack}
1185 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001186 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001187
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001188 # Append the process to the screen rc file
1189 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001190
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001191 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001192
Dean Troyerdde41d02014-12-09 17:47:57 -06001193 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1194 echo "LOGDIR: $LOGDIR"
1195 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1196 echo "log: $real_logfile"
1197 if [[ -n ${LOGDIR} ]]; then
1198 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001199 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001200 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1201 if [[ -n ${SCREEN_LOGDIR} ]]; then
1202 # Drop the backward-compat symlink
1203 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1204 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001205 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001206
1207 # sleep to allow bash to be ready to be send the command - we are
1208 # creating a new window in screen and then sends characters, so if
1209 # bash isn't running by the time we send the command, nothing happens
1210 sleep 3
1211
1212 NL=`echo -ne '\015'`
1213 # This fun command does the following:
1214 # - the passed server command is backgrounded
1215 # - the pid of the background process is saved in the usual place
1216 # - the server process is brought back to the foreground
1217 # - if the server process exits prematurely the fg command errors
1218 # and a message is written to stdout and the process failure file
1219 #
1220 # The pid saved can be used in stop_process() as a process group
1221 # id to kill off all child processes
1222 if [[ -n "$group" ]]; then
1223 command="sg $group '$command'"
1224 fi
1225 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 -06001226}
1227
1228# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001229# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001230# screen_rc service "command-line"
1231function screen_rc {
1232 SCREEN_NAME=${SCREEN_NAME:-stack}
1233 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1234 if [[ ! -e $SCREENRC ]]; then
1235 # Name the screen session
1236 echo "sessionname $SCREEN_NAME" > $SCREENRC
1237 # Set a reasonable statusbar
1238 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1239 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1240 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1241 echo "screen -t shell bash" >> $SCREENRC
1242 fi
1243 # If this service doesn't already exist in the screenrc file
1244 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1245 NL=`echo -ne '\015'`
1246 echo "screen -t $1 bash" >> $SCREENRC
1247 echo "stuff \"$2$NL\"" >> $SCREENRC
1248
Dean Troyerdde41d02014-12-09 17:47:57 -06001249 if [[ -n ${LOGDIR} ]]; then
1250 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001251 echo "log on" >>$SCREENRC
1252 fi
1253 fi
1254}
1255
1256# Stop a service in screen
1257# If a PID is available use it, kill the whole process group via TERM
1258# If screen is being used kill the screen window; this will catch processes
1259# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001260# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001261# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001262function screen_stop_service {
1263 local service=$1
1264
Dean Troyerdff49a22014-01-30 15:37:40 -06001265 SCREEN_NAME=${SCREEN_NAME:-stack}
1266 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001267 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001268
Dean Troyer3159a822014-08-27 14:13:58 -05001269 if is_service_enabled $service; then
1270 # Clean up the screen window
1271 screen -S $SCREEN_NAME -p $service -X kill
1272 fi
1273}
1274
1275# Stop a service process
1276# If a PID is available use it, kill the whole process group via TERM
1277# If screen is being used kill the screen window; this will catch processes
1278# that did not leave a PID behind
1279# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1280# stop_process service
1281function stop_process {
1282 local service=$1
1283
1284 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001285 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001286
1287 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001288 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001289 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1290 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1291 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001292 fi
1293 if [[ "$USE_SCREEN" = "True" ]]; then
1294 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001295 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001296 fi
1297 fi
1298}
1299
1300# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001301# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001302# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001303function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001304 local service
1305 local failures
1306 SCREEN_NAME=${SCREEN_NAME:-stack}
1307 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1308
1309
1310 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1311 echo "No service status directory found"
1312 return
1313 fi
1314
1315 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001316 # make this -o errexit safe
1317 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001318
1319 for service in $failures; do
1320 service=`basename $service`
1321 service=${service%.failure}
1322 echo "Error: Service $service is not running"
1323 done
1324
1325 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001326 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001327 fi
1328}
1329
Chris Dent2f27a0e2014-09-09 13:46:02 +01001330# Tail a log file in a screen if USE_SCREEN is true.
1331function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001332 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001333 local logfile=$2
1334
Sean Dague53753292014-12-04 19:38:15 -05001335 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001336 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001337 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001338 fi
1339}
1340
Dean Troyerdff49a22014-01-30 15:37:40 -06001341
Dean Troyer3159a822014-08-27 14:13:58 -05001342# Deprecated Functions
1343# --------------------
1344
1345# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1346# fork. It includes the dirty work of closing extra filehandles and preparing log
1347# files to produce the same logs as screen_it(). The log filename is derived
1348# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1349# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1350# _old_run_process service "command-line"
1351function _old_run_process {
1352 local service=$1
1353 local command="$2"
1354
1355 # Undo logging redirections and close the extra descriptors
1356 exec 1>&3
1357 exec 2>&3
1358 exec 3>&-
1359 exec 6>&-
1360
1361 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001362 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1363 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001364
1365 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1366 export PYTHONUNBUFFERED=1
1367 fi
1368
1369 exec /bin/bash -c "$command"
1370 die "$service exec failure: $command"
1371}
1372
1373# old_run_process() launches a child process that closes all file descriptors and
1374# then exec's the passed in command. This is meant to duplicate the semantics
1375# of screen_it() without screen. PIDs are written to
1376# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1377# old_run_process service "command-line"
1378function old_run_process {
1379 local service=$1
1380 local command="$2"
1381
1382 # Spawn the child process
1383 _old_run_process "$service" "$command" &
1384 echo $!
1385}
1386
1387# Compatibility for existing start_XXXX() functions
1388# Uses global ``USE_SCREEN``
1389# screen_it service "command-line"
1390function screen_it {
1391 if is_service_enabled $1; then
1392 # Append the service to the screen rc file
1393 screen_rc "$1" "$2"
1394
1395 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001396 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001397 else
1398 # Spawn directly without screen
1399 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1400 fi
1401 fi
1402}
1403
1404# Compatibility for existing stop_XXXX() functions
1405# Stop a service in screen
1406# If a PID is available use it, kill the whole process group via TERM
1407# If screen is being used kill the screen window; this will catch processes
1408# that did not leave a PID behind
1409# screen_stop service
1410function screen_stop {
1411 # Clean up the screen window
1412 stop_process $1
1413}
1414
1415
Sean Dague2c65e712014-12-18 09:44:56 -05001416# Plugin Functions
1417# =================
1418
1419DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1420
1421# enable_plugin <name> <url> [branch]
1422#
1423# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1424# ``url`` is a git url
1425# ``branch`` is a gitref. If it's not set, defaults to master
1426function enable_plugin {
1427 local name=$1
1428 local url=$2
1429 local branch=${3:-master}
1430 DEVSTACK_PLUGINS+=",$name"
1431 GITREPO[$name]=$url
1432 GITDIR[$name]=$DEST/$name
1433 GITBRANCH[$name]=$branch
1434}
1435
1436# fetch_plugins
1437#
1438# clones all plugins
1439function fetch_plugins {
1440 local plugins="${DEVSTACK_PLUGINS}"
1441 local plugin
1442
1443 # short circuit if nothing to do
1444 if [[ -z $plugins ]]; then
1445 return
1446 fi
1447
1448 echo "Fetching devstack plugins"
1449 for plugin in ${plugins//,/ }; do
1450 git_clone_by_name $plugin
1451 done
1452}
1453
1454# load_plugin_settings
1455#
1456# Load settings from plugins in the order that they were registered
1457function load_plugin_settings {
1458 local plugins="${DEVSTACK_PLUGINS}"
1459 local plugin
1460
1461 # short circuit if nothing to do
1462 if [[ -z $plugins ]]; then
1463 return
1464 fi
1465
1466 echo "Loading plugin settings"
1467 for plugin in ${plugins//,/ }; do
1468 local dir=${GITDIR[$plugin]}
1469 # source any known settings
1470 if [[ -f $dir/devstack/settings ]]; then
1471 source $dir/devstack/settings
1472 fi
1473 done
1474}
1475
1476# run_plugins
1477#
1478# Run the devstack/plugin.sh in all the plugin directories. These are
1479# run in registration order.
1480function run_plugins {
1481 local mode=$1
1482 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301483
1484 local plugins="${DEVSTACK_PLUGINS}"
1485 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001486 for plugin in ${plugins//,/ }; do
1487 local dir=${GITDIR[$plugin]}
1488 if [[ -f $dir/devstack/plugin.sh ]]; then
1489 source $dir/devstack/plugin.sh $mode $phase
1490 fi
1491 done
1492}
1493
1494function run_phase {
1495 local mode=$1
1496 local phase=$2
1497 if [[ -d $TOP_DIR/extras.d ]]; then
1498 for i in $TOP_DIR/extras.d/*.sh; do
1499 [[ -r $i ]] && source $i $mode $phase
1500 done
1501 fi
1502 # the source phase corresponds to settings loading in plugins
1503 if [[ "$mode" == "source" ]]; then
1504 load_plugin_settings
1505 else
1506 run_plugins $mode $phase
1507 fi
1508}
1509
Dean Troyerdff49a22014-01-30 15:37:40 -06001510
1511# Service Functions
1512# =================
1513
1514# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1515# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001516function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001517 echo "$1" | sed -e '
1518 s/,,/,/g;
1519 s/^,//;
1520 s/,$//
1521 '
1522}
1523
1524# disable_all_services() removes all current services
1525# from ``ENABLED_SERVICES`` to reset the configuration
1526# before a minimal installation
1527# Uses global ``ENABLED_SERVICES``
1528# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001529function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001530 ENABLED_SERVICES=""
1531}
1532
1533# Remove all services starting with '-'. For example, to install all default
1534# services except rabbit (rabbit) set in ``localrc``:
1535# ENABLED_SERVICES+=",-rabbit"
1536# Uses global ``ENABLED_SERVICES``
1537# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001538function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001539 local tmpsvcs="${ENABLED_SERVICES}"
1540 local service
1541 for service in ${tmpsvcs//,/ }; do
1542 if [[ ${service} == -* ]]; then
1543 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1544 fi
1545 done
1546 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1547}
1548
1549# disable_service() removes the services passed as argument to the
1550# ``ENABLED_SERVICES`` list, if they are present.
1551#
1552# For example:
1553# disable_service rabbit
1554#
1555# This function does not know about the special cases
1556# for nova, glance, and neutron built into is_service_enabled().
1557# Uses global ``ENABLED_SERVICES``
1558# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001559function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001560 local tmpsvcs=",${ENABLED_SERVICES},"
1561 local service
1562 for service in $@; do
1563 if is_service_enabled $service; then
1564 tmpsvcs=${tmpsvcs//,$service,/,}
1565 fi
1566 done
1567 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1568}
1569
1570# enable_service() adds the services passed as argument to the
1571# ``ENABLED_SERVICES`` list, if they are not already present.
1572#
1573# For example:
1574# enable_service qpid
1575#
1576# This function does not know about the special cases
1577# for nova, glance, and neutron built into is_service_enabled().
1578# Uses global ``ENABLED_SERVICES``
1579# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001580function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001581 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001582 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001583 for service in $@; do
1584 if ! is_service_enabled $service; then
1585 tmpsvcs+=",$service"
1586 fi
1587 done
1588 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1589 disable_negated_services
1590}
1591
1592# is_service_enabled() checks if the service(s) specified as arguments are
1593# enabled by the user in ``ENABLED_SERVICES``.
1594#
1595# Multiple services specified as arguments are ``OR``'ed together; the test
1596# is a short-circuit boolean, i.e it returns on the first match.
1597#
1598# There are special cases for some 'catch-all' services::
1599# **nova** returns true if any service enabled start with **n-**
1600# **cinder** returns true if any service enabled start with **c-**
1601# **ceilometer** returns true if any service enabled start with **ceilometer**
1602# **glance** returns true if any service enabled start with **g-**
1603# **neutron** returns true if any service enabled start with **q-**
1604# **swift** returns true if any service enabled start with **s-**
1605# **trove** returns true if any service enabled start with **tr-**
1606# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1607# **s-** services will be enabled. This will be deprecated in the future.
1608#
1609# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1610# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1611# as enabled in this case.
1612#
1613# Uses global ``ENABLED_SERVICES``
1614# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001615function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001616 local xtrace=$(set +o | grep xtrace)
1617 set +o xtrace
1618 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001619 local services=$@
1620 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001621 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001622 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001623
1624 # Look for top-level 'enabled' function for this service
1625 if type is_${service}_enabled >/dev/null 2>&1; then
1626 # A function exists for this service, use it
1627 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001628 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001629 fi
1630
1631 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1632 # are implemented
1633
Sean Dague45917cc2014-02-24 16:09:14 -05001634 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001635 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001636 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1637 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1638 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1639 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1640 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1641 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1642 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1643 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1644 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001645 done
Sean Dague45917cc2014-02-24 16:09:14 -05001646 $xtrace
1647 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001648}
1649
1650# Toggle enable/disable_service for services that must run exclusive of each other
1651# $1 The name of a variable containing a space-separated list of services
1652# $2 The name of a variable in which to store the enabled service's name
1653# $3 The name of the service to enable
1654function use_exclusive_service {
1655 local options=${!1}
1656 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001657 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001658 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001659 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001660 for opt in $options;do
1661 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1662 done
1663 eval "$out=$selection"
1664 return 0
1665}
1666
1667
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001668# System Functions
1669# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001670
1671# Only run the command if the target file (the last arg) is not on an
1672# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001673function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001674 local xtrace=$(set +o | grep xtrace)
1675 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001676 local args=( $@ )
1677 local last
1678 local sudo_cmd
1679 local dir_to_check
1680
1681 let last="${#args[*]} - 1"
1682
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001683 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001684 if [ ! -d "$dir_to_check" ]; then
1685 dir_to_check=`dirname "$dir_to_check"`
1686 fi
1687
1688 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001689 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001690 return 0
1691 fi
1692
1693 if [[ $TRACK_DEPENDS = True ]]; then
1694 sudo_cmd="env"
1695 else
1696 sudo_cmd="sudo"
1697 fi
1698
Sean Dague45917cc2014-02-24 16:09:14 -05001699 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001700 $sudo_cmd $@
1701}
1702
1703# Exit 0 if address is in network or 1 if address is not in network
1704# ip-range is in CIDR notation: 1.2.3.4/20
1705# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001706function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001707 local ip=$1
1708 local range=$2
1709 local masklen=${range#*/}
1710 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1711 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1712 [[ $network == $subnet ]]
1713}
1714
1715# Add a user to a group.
1716# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001717function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001718 local user=$1
1719 local group=$2
1720
1721 if [[ -z "$os_VENDOR" ]]; then
1722 GetOSVersion
1723 fi
1724
1725 # SLE11 and openSUSE 12.2 don't have the usual usermod
1726 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1727 sudo usermod -a -G "$group" "$user"
1728 else
1729 sudo usermod -A "$group" "$user"
1730 fi
1731}
1732
1733# Convert CIDR notation to a IPv4 netmask
1734# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001735function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001736 local maskpat="255 255 255 255"
1737 local maskdgt="254 252 248 240 224 192 128"
1738 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1739 echo ${1-0}.${2-0}.${3-0}.${4-0}
1740}
1741
1742# Gracefully cp only if source file/dir exists
1743# cp_it source destination
1744function cp_it {
1745 if [ -e $1 ] || [ -d $1 ]; then
1746 cp -pRL $1 $2
1747 fi
1748}
1749
1750# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1751# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1752# ``localrc`` or on the command line if necessary::
1753#
1754# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1755#
1756# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1757
Ian Wienandaee18c72014-02-21 15:35:08 +11001758function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001759 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001760 export http_proxy=$http_proxy
1761 fi
Sean Dague53753292014-12-04 19:38:15 -05001762 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001763 export https_proxy=$https_proxy
1764 fi
Sean Dague53753292014-12-04 19:38:15 -05001765 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001766 export no_proxy=$no_proxy
1767 fi
1768}
1769
1770# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001771function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001772 local mount_type=`stat -f -L -c %T $1`
1773 test "$mount_type" == "nfs"
1774}
1775
1776# Return the network portion of the given IP address using netmask
1777# netmask is in the traditional dotted-quad format
1778# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001779function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001780 local ip=$1
1781 local mask=$2
1782 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1783 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1784 echo $subnet
1785}
1786
1787# Service wrapper to restart services
1788# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001789function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001790 if is_ubuntu; then
1791 sudo /usr/sbin/service $1 restart
1792 else
1793 sudo /sbin/service $1 restart
1794 fi
1795}
1796
1797# Only change permissions of a file or directory if it is not on an
1798# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001799function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001800 _safe_permission_operation chmod $@
1801}
1802
1803# Only change ownership of a file or directory if it is not on an NFS
1804# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001805function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001806 _safe_permission_operation chown $@
1807}
1808
1809# Service wrapper to start services
1810# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001811function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001812 if is_ubuntu; then
1813 sudo /usr/sbin/service $1 start
1814 else
1815 sudo /sbin/service $1 start
1816 fi
1817}
1818
1819# Service wrapper to stop services
1820# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001821function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001822 if is_ubuntu; then
1823 sudo /usr/sbin/service $1 stop
1824 else
1825 sudo /sbin/service $1 stop
1826 fi
1827}
1828
1829
1830# Restore xtrace
1831$XTRACE
1832
1833# Local variables:
1834# mode: shell-script
1835# End: