blob: 813d164272fa79cf6270409dc12e4b03df7dc127 [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 Troyer68162342015-05-13 15:41:03 -050046# Save these variables to .stackenv
47STACK_ENV_VARS="BASE_SQL_CONN DATA_DIR DEST ENABLED_SERVICES HOST_IP \
48 KEYSTONE_AUTH_PROTOCOL KEYSTONE_AUTH_URI KEYSTONE_SERVICE_URI \
49 LOGFILE OS_CACERT SERVICE_HOST SERVICE_PROTOCOL STACK_USER TLS_IP"
50
51
52# Saves significant environment variables to .stackenv for later use
53# Refers to a lot of globals, only TOP_DIR and STACK_ENV_VARS are required to
54# function, the rest are simply saved and do not cause problems if they are undefined.
55# save_stackenv [tag]
56function save_stackenv {
57 local tag=${1:-""}
58 # Save some values we generated for later use
59 time_stamp=$(date "+$TIMESTAMP_FORMAT")
60 echo "# $time_stamp $tag" >$TOP_DIR/.stackenv
61 for i in $STACK_ENV_VARS; do
62 echo $i=${!i} >>$TOP_DIR/.stackenv
63 done
64}
Dean Troyerdff49a22014-01-30 15:37:40 -060065
66# Normalize config values to True or False
67# Accepts as False: 0 no No NO false False FALSE
68# Accepts as True: 1 yes Yes YES true True TRUE
69# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +110070function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -050071 local xtrace=$(set +o | grep xtrace)
72 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060073
Mahito OGURA98f59aa2015-05-11 18:02:34 +090074 local default=$1
75 local testval=${!2:-}
76
77 case "$testval" in
78 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
79 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
80 * ) echo "$default" ;;
81 esac
82
Sean Dague45917cc2014-02-24 16:09:14 -050083 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060084}
85
Attila Fazekas1bd79592015-02-24 14:06:56 +010086function isset {
87 [[ -v "$1" ]]
88}
Dean Troyerdff49a22014-01-30 15:37:40 -060089
Dean Troyer68162342015-05-13 15:41:03 -050090
Dean Troyerdff49a22014-01-30 15:37:40 -060091# Control Functions
92# =================
93
94# Prints backtrace info
95# filename:lineno:function
96# backtrace level
97function backtrace {
98 local level=$1
99 local deep=$((${#BASH_SOURCE[@]} - 1))
100 echo "[Call Trace]"
101 while [ $level -le $deep ]; do
102 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
103 deep=$((deep - 1))
104 done
105}
106
107# Prints line number and "message" then exits
108# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100109function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600110 local exitcode=$?
111 set +o xtrace
112 local line=$1; shift
113 if [ $exitcode == 0 ]; then
114 exitcode=1
115 fi
116 backtrace 2
117 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600118 # Give buffers a second to flush
119 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600120 exit $exitcode
121}
122
123# Checks an environment variable is not set or has length 0 OR if the
124# exit code is non-zero and prints "message" and exits
125# NOTE: env-var is the variable name without a '$'
126# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100127function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600128 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500129 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600130 set +o xtrace
131 local line=$1; shift
132 local evar=$1; shift
133 if ! is_set $evar || [ $exitcode != 0 ]; then
134 die $line "$*"
135 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500136 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600137}
138
139# Prints line number and "message" in error format
140# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100141function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600142 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500143 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600144 set +o xtrace
145 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
146 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600147 if [[ -n ${LOGDIR} ]]; then
148 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600149 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500150 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600151 return $exitcode
152}
153
154# Checks an environment variable is not set or has length 0 OR if the
155# exit code is non-zero and prints "message"
156# NOTE: env-var is the variable name without a '$'
157# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100158function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600159 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500160 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600161 set +o xtrace
162 local line=$1; shift
163 local evar=$1; shift
164 if ! is_set $evar || [ $exitcode != 0 ]; then
165 err $line "$*"
166 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500167 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600168 return $exitcode
169}
170
171# Exit after outputting a message about the distribution not being supported.
172# exit_distro_not_supported [optional-string-telling-what-is-missing]
173function exit_distro_not_supported {
174 if [[ -z "$DISTRO" ]]; then
175 GetDistro
176 fi
177
178 if [ $# -gt 0 ]; then
179 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
180 else
181 die $LINENO "Support for $DISTRO is incomplete."
182 fi
183}
184
185# Test if the named environment variable is set and not zero length
186# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100187function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600188 local var=\$"$1"
189 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
190}
191
192# Prints line number and "message" in warning format
193# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100194function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600195 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500196 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600197 set +o xtrace
198 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Sean Daguee4af9292015-04-28 08:57:57 -0400199 echo $msg
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500200 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600201 return $exitcode
202}
203
204
205# Distro Functions
206# ================
207
208# Determine OS Vendor, Release and Update
209# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
210# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500211# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
212# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
213# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
214# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
215# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500216os_VENDOR=""
217os_RELEASE=""
218os_UPDATE=""
219os_PACKAGE=""
220os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500221
Dean Troyerdff49a22014-01-30 15:37:40 -0600222# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100223function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500224
Dean Troyerdff49a22014-01-30 15:37:40 -0600225 # Figure out which vendor we are
226 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
227 # OS/X
228 os_VENDOR=`sw_vers -productName`
229 os_RELEASE=`sw_vers -productVersion`
230 os_UPDATE=${os_RELEASE##*.}
231 os_RELEASE=${os_RELEASE%.*}
232 os_PACKAGE=""
233 if [[ "$os_RELEASE" =~ "10.7" ]]; then
234 os_CODENAME="lion"
235 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
236 os_CODENAME="snow leopard"
237 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
238 os_CODENAME="leopard"
239 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
240 os_CODENAME="tiger"
241 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
242 os_CODENAME="panther"
243 else
244 os_CODENAME=""
245 fi
246 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
247 os_VENDOR=$(lsb_release -i -s)
248 os_RELEASE=$(lsb_release -r -s)
249 os_UPDATE=""
250 os_PACKAGE="rpm"
251 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
252 os_PACKAGE="deb"
253 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
254 lsb_release -d -s | grep -q openSUSE
255 if [[ $? -eq 0 ]]; then
256 os_VENDOR="openSUSE"
257 fi
258 elif [[ $os_VENDOR == "openSUSE project" ]]; then
259 os_VENDOR="openSUSE"
260 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
261 os_VENDOR="Red Hat"
262 fi
263 os_CODENAME=$(lsb_release -c -s)
264 elif [[ -r /etc/redhat-release ]]; then
265 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
266 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
267 # CentOS release 5.5 (Final)
268 # CentOS Linux release 6.0 (Final)
269 # Fedora release 16 (Verne)
270 # XenServer release 6.2.0-70446c (xenenterprise)
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700271 # Oracle Linux release 7
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300272 # CloudLinux release 7.1
Dean Troyerdff49a22014-01-30 15:37:40 -0600273 os_CODENAME=""
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300274 for r in "Red Hat" CentOS Fedora XenServer CloudLinux; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600275 os_VENDOR=$r
276 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
277 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
278 os_CODENAME=${ver#*|}
279 os_RELEASE=${ver%|*}
280 os_UPDATE=${os_RELEASE##*.}
281 os_RELEASE=${os_RELEASE%.*}
282 break
283 fi
284 os_VENDOR=""
285 done
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700286 if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
287 os_VENDOR=OracleLinux
288 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600289 os_PACKAGE="rpm"
290 elif [[ -r /etc/SuSE-release ]]; then
291 for r in openSUSE "SUSE Linux"; do
292 if [[ "$r" = "SUSE Linux" ]]; then
293 os_VENDOR="SUSE LINUX"
294 else
295 os_VENDOR=$r
296 fi
297
298 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
299 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
300 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
301 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
302 break
303 fi
304 os_VENDOR=""
305 done
306 os_PACKAGE="rpm"
307 # If lsb_release is not installed, we should be able to detect Debian OS
308 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
309 os_VENDOR="Debian"
310 os_PACKAGE="deb"
311 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
312 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
313 fi
314 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
315}
316
317# Translate the OS version values into common nomenclature
318# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500319declare DISTRO
320
Ian Wienandaee18c72014-02-21 15:35:08 +1100321function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600322 GetOSVersion
323 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
324 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
325 DISTRO=$os_CODENAME
326 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
327 # For Fedora, just use 'f' and the release
328 DISTRO="f$os_RELEASE"
329 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
330 DISTRO="opensuse-$os_RELEASE"
331 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
332 # For SLE, also use the service pack
333 if [[ -z "$os_UPDATE" ]]; then
334 DISTRO="sle${os_RELEASE}"
335 else
336 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
337 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530338 elif [[ "$os_VENDOR" =~ (Red Hat) || \
339 "$os_VENDOR" =~ (CentOS) || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700340 "$os_VENDOR" =~ (OracleLinux) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600341 # Drop the . release as we assume it's compatible
342 DISTRO="rhel${os_RELEASE::1}"
343 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
344 DISTRO="xs$os_RELEASE"
345 else
346 # Catch-all for now is Vendor + Release + Update
347 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
348 fi
349 export DISTRO
350}
351
352# Utility function for checking machine architecture
353# is_arch arch-type
354function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500355 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600356}
357
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700358# Determine if current distribution is an Oracle distribution
359# is_oraclelinux
360function is_oraclelinux {
361 if [[ -z "$os_VENDOR" ]]; then
362 GetOSVersion
363 fi
364
365 [ "$os_VENDOR" = "OracleLinux" ]
366}
367
368
Dean Troyerdff49a22014-01-30 15:37:40 -0600369# Determine if current distribution is a Fedora-based distribution
370# (Fedora, RHEL, CentOS, etc).
371# is_fedora
372function is_fedora {
373 if [[ -z "$os_VENDOR" ]]; then
374 GetOSVersion
375 fi
376
anju Tiwari6c639c92014-07-15 18:11:54 +0530377 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300378 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ] || \
379 [ "$os_VENDOR" = "CloudLinux" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600380}
381
382
383# Determine if current distribution is a SUSE-based distribution
384# (openSUSE, SLE).
385# is_suse
386function is_suse {
387 if [[ -z "$os_VENDOR" ]]; then
388 GetOSVersion
389 fi
390
391 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
392}
393
394
395# Determine if current distribution is an Ubuntu-based distribution
396# It will also detect non-Ubuntu but Debian-based distros
397# is_ubuntu
398function is_ubuntu {
399 if [[ -z "$os_PACKAGE" ]]; then
400 GetOSVersion
401 fi
402 [ "$os_PACKAGE" = "deb" ]
403}
404
405
406# Git Functions
407# =============
408
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600409# Returns openstack release name for a given branch name
410# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100411function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600412 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700413 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600414 echo ${branch#*/}
415 else
416 echo "master"
417 fi
418}
419
Dean Troyerdff49a22014-01-30 15:37:40 -0600420# git clone only if directory doesn't exist already. Since ``DEST`` might not
421# be owned by the installation user, we create the directory and change the
422# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500423# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
424# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600425# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500426# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600427# git_clone remote dest-dir branch
428function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500429 local git_remote=$1
430 local git_dest=$2
431 local git_ref=$3
432 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200433 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500434
Sean Dague53753292014-12-04 19:38:15 -0500435 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800436 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200437 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
438 fi
439
Dean Troyerdff49a22014-01-30 15:37:40 -0600440 if [[ "$OFFLINE" = "True" ]]; then
441 echo "Running in offline mode, clones already exist"
442 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500443 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600444 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400445 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600446 return
447 fi
448
Dean Troyer50cda692014-07-25 11:57:20 -0500449 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600450 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500451 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600452 [[ "$ERROR_ON_CLONE" = "True" ]] && \
453 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200454 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600455 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500456 cd $git_dest
457 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600458 else
459 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500460 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600461 [[ "$ERROR_ON_CLONE" = "True" ]] && \
462 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200463 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500464 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600465 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500466 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600467 elif [[ "$RECLONE" = "True" ]]; then
468 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500469 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600470 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500471 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100472 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600473 # remove the existing ignored files (like pyc) as they cause breakage
474 # (due to the py files having older timestamps than our pyc, so python
475 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500476 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600477
Dean Troyer50cda692014-07-25 11:57:20 -0500478 # handle git_ref accordingly to type (tag, branch)
479 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
480 git_update_tag $git_ref
481 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
482 git_update_branch $git_ref
483 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
484 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600485 else
Dean Troyer50cda692014-07-25 11:57:20 -0500486 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600487 fi
488
489 fi
490 fi
491
492 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500493 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600494 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400495 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600496}
497
Sean Daguecc524062014-10-01 09:06:43 -0400498# A variation on git clone that lets us specify a project by it's
499# actual name, like oslo.config. This is exceptionally useful in the
500# library installation case
501function git_clone_by_name {
502 local name=$1
503 local repo=${GITREPO[$name]}
504 local dir=${GITDIR[$name]}
505 local branch=${GITBRANCH[$name]}
506 git_clone $repo $dir $branch
507}
508
509
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100510# git can sometimes get itself infinitely stuck with transient network
511# errors or other issues with the remote end. This wraps git in a
512# timeout/retry loop and is intended to watch over non-local git
513# processes that might hang. GIT_TIMEOUT, if set, is passed directly
514# to timeout(1); otherwise the default value of 0 maintains the status
515# quo of waiting forever.
516# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100517function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100518 local count=0
519 local timeout=0
520
521 if [[ -n "${GIT_TIMEOUT}" ]]; then
522 timeout=${GIT_TIMEOUT}
523 fi
524
525 until timeout -s SIGINT ${timeout} git "$@"; do
526 # 124 is timeout(1)'s special return code when it reached the
527 # timeout; otherwise assume fatal failure
528 if [[ $? -ne 124 ]]; then
529 die $LINENO "git call failed: [git $@]"
530 fi
531
532 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400533 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100534 if [ $count -eq 3 ]; then
535 die $LINENO "Maximum of 3 git retries reached"
536 fi
537 sleep 5
538 done
539}
540
Dean Troyerdff49a22014-01-30 15:37:40 -0600541# git update using reference as a branch.
542# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100543function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500544 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600545
Dean Troyer50cda692014-07-25 11:57:20 -0500546 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600547 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500548 git branch -D $git_branch || true
549 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600550}
551
552# git update using reference as a branch.
553# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100554function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500555 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600556
Dean Troyer50cda692014-07-25 11:57:20 -0500557 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600558}
559
560# git update using reference as a tag. Be careful editing source at that repo
561# as working copy will be in a detached mode
562# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100563function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500564 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600565
Dean Troyer50cda692014-07-25 11:57:20 -0500566 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600567 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500568 git_timed fetch origin tag $git_tag
569 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600570}
571
572
573# OpenStack Functions
574# ===================
575
576# Get the default value for HOST_IP
577# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100578function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600579 local fixed_range=$1
580 local floating_range=$2
581 local host_ip_iface=$3
582 local host_ip=$4
583
Dean Troyerdff49a22014-01-30 15:37:40 -0600584 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
585 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
586 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100587 # Find the interface used for the default route
588 host_ip_iface=${host_ip_iface:-$(ip route | awk '/default/ {print $5}' | head -1)}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500589 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
590 local ip
591 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600592 # Attempt to filter out IP addresses that are part of the fixed and
593 # floating range. Note that this method only works if the ``netaddr``
594 # python library is installed. If it is not installed, an error
595 # will be printed and the first IP from the interface will be used.
596 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
597 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500598 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
599 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600600 break;
601 fi
602 done
603 fi
604 echo $host_ip
605}
606
Attila Fazekasf71b5002014-05-28 09:52:22 +0200607# Generates hex string from ``size`` byte of pseudo random data
608# generate_hex_string size
609function generate_hex_string {
610 local size=$1
611 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
612}
613
Dean Troyerdff49a22014-01-30 15:37:40 -0600614# Grab a numbered field from python prettytable output
615# Fields are numbered starting with 1
616# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
617# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100618function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500619 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600620 while read data; do
621 if [ "$1" -lt 0 ]; then
622 field="(\$(NF$1))"
623 else
624 field="\$$(($1 + 1))"
625 fi
626 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
627 done
628}
629
yuntongjinf26deea2015-02-28 10:50:34 +0800630# install default policy
631# copy over a default policy.json and policy.d for projects
632function install_default_policy {
633 local project=$1
634 local project_uc=$(echo $1|tr a-z A-Z)
635 local conf_dir="${project_uc}_CONF_DIR"
636 # eval conf dir to get the variable
637 conf_dir="${!conf_dir}"
638 local project_dir="${project_uc}_DIR"
639 # eval project dir to get the variable
640 project_dir="${!project_dir}"
641 local sample_conf_dir="${project_dir}/etc/${project}"
642 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
643
644 # first copy any policy.json
645 cp -p $sample_conf_dir/policy.json $conf_dir
646 # then optionally copy over policy.d
647 if [[ -d $sample_policy_dir ]]; then
648 cp -r $sample_policy_dir $conf_dir/policy.d
649 fi
650}
651
Dean Troyerdff49a22014-01-30 15:37:40 -0600652# Add a policy to a policy.json file
653# Do nothing if the policy already exists
654# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100655function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600656 local policy_file=$1
657 local policy_name=$2
658 local policy_perm=$3
659
660 if grep -q ${policy_name} ${policy_file}; then
661 echo "Policy ${policy_name} already exists in ${policy_file}"
662 return
663 fi
664
665 # Add a terminating comma to policy lines without one
666 # Remove the closing '}' and all lines following to the end-of-file
667 local tmpfile=$(mktemp)
668 uniq ${policy_file} | sed -e '
669 s/]$/],/
670 /^[}]/,$d
671 ' > ${tmpfile}
672
673 # Append policy and closing brace
674 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
675 echo "}" >>${tmpfile}
676
677 mv ${tmpfile} ${policy_file}
678}
679
Alistair Coles24779f62014-10-15 18:57:59 +0100680# Gets or creates a domain
681# Usage: get_or_create_domain <name> <description>
682function get_or_create_domain {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500683 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100684 # Gets domain id
685 local domain_id=$(
686 # Gets domain id
687 openstack --os-token=$OS_TOKEN --os-url=$os_url \
688 --os-identity-api-version=3 domain show $1 \
689 -f value -c id 2>/dev/null ||
690 # Creates new domain
691 openstack --os-token=$OS_TOKEN --os-url=$os_url \
692 --os-identity-api-version=3 domain create $1 \
693 --description "$2" \
694 -f value -c id
695 )
696 echo $domain_id
697}
698
Steve Martinellib74e01c2014-12-18 01:35:35 -0500699# Gets or creates group
700# Usage: get_or_create_group <groupname> [<domain> <description>]
701function get_or_create_group {
702 local domain=${2:+--domain ${2}}
703 local desc="${3:-}"
704 local os_url="$KEYSTONE_SERVICE_URI_V3"
705 # Gets group id
706 local group_id=$(
707 # Creates new group with --or-show
708 openstack --os-token=$OS_TOKEN --os-url=$os_url \
709 --os-identity-api-version=3 group create $1 \
710 $domain --description "$desc" --or-show \
711 -f value -c id
712 )
713 echo $group_id
714}
715
Bartosz Górski0abde392014-02-28 14:15:19 +0100716# Gets or creates user
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000717# Usage: get_or_create_user <username> <password> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100718function get_or_create_user {
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000719 if [[ ! -z "$3" ]]; then
720 local email="--email=$3"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200721 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500722 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200723 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100724 local os_cmd="openstack"
725 local domain=""
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000726 if [[ ! -z "$4" ]]; then
727 domain="--domain=$4"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500728 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100729 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100730 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500731 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500732 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100733 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100734 $1 \
735 --password "$2" \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500736 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100737 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500738 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100739 -f value -c id
740 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500741 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100742}
743
744# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100745# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100746function get_or_create_project {
747 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100748 local os_cmd="openstack"
749 local domain=""
750 if [[ ! -z "$2" ]]; then
751 domain="--domain=$2"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500752 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100753 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500754 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500755 # Creates new project with --or-show
756 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100757 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500758 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100759}
760
761# Gets or creates role
762# Usage: get_or_create_role <name>
763function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500764 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500765 # Creates role with --or-show
766 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100767 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500768 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100769}
770
Jamie Lennox9b215db2015-02-10 18:19:57 +1100771# Gets or adds user role to project
772# Usage: get_or_add_user_project_role <role> <user> <project>
773function get_or_add_user_project_role {
Bartosz Górski0abde392014-02-28 14:15:19 +0100774 # Gets user role id
Steve Martinelli5541a612015-01-19 15:58:49 -0500775 local user_role_id=$(openstack role list \
776 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100777 --project $3 \
778 --column "ID" \
779 --column "Name" \
780 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500781 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100782 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500783 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100784 $1 \
785 --user $2 \
786 --project $3 \
787 | grep " id " | get_field 2)
788 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500789 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100790}
791
Steve Martinelli4599fd12015-03-12 21:30:58 -0400792# Gets or adds group role to project
793# Usage: get_or_add_group_project_role <role> <group> <project>
794function get_or_add_group_project_role {
795 # Gets group role id
796 local group_role_id=$(openstack role list \
797 --group $2 \
798 --project $3 \
799 --column "ID" \
800 --column "Name" \
801 | grep " $1 " | get_field 1)
802 if [[ -z "$group_role_id" ]]; then
803 # Adds role to group
804 group_role_id=$(openstack role add \
805 $1 \
806 --group $2 \
807 --project $3 \
808 | grep " id " | get_field 2)
809 fi
810 echo $group_role_id
811}
812
Bartosz Górski0abde392014-02-28 14:15:19 +0100813# Gets or creates service
814# Usage: get_or_create_service <name> <type> <description>
815function get_or_create_service {
816 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500817 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100818 # Gets service id
819 openstack service show $1 -f value -c id 2>/dev/null ||
820 # Creates new service if not exists
821 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500822 $2 \
823 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100824 --description="$3" \
825 -f value -c id
826 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500827 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100828}
829
830# Gets or creates endpoint
831# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
832function get_or_create_endpoint {
833 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500834 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100835 --column "ID" \
836 --column "Region" \
837 --column "Service Name" \
838 | grep " $2 " \
839 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500840 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100841 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500842 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100843 $1 \
844 --region $2 \
845 --publicurl $3 \
846 --adminurl $4 \
847 --internalurl $5 \
848 | grep " id " | get_field 2)
849 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500850 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100851}
Dean Troyerdff49a22014-01-30 15:37:40 -0600852
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500853
Dean Troyerdff49a22014-01-30 15:37:40 -0600854# Package Functions
855# =================
856
857# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100858function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800859 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600860 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800861
862 if [[ -z "$base_dir" ]]; then
863 base_dir=$FILES
864 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600865 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800866 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600867 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800868 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600869 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800870 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600871 else
872 exit_distro_not_supported "list of packages"
873 fi
874 echo "$pkg_dir"
875}
876
877# Wrapper for ``apt-get`` to set cache and proxy environment variables
878# Uses globals ``OFFLINE``, ``*_proxy``
879# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100880function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500881 local xtrace=$(set +o | grep xtrace)
882 set +o xtrace
883
Dean Troyerdff49a22014-01-30 15:37:40 -0600884 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
885 local sudo="sudo"
886 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500887
888 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500889
Dean Troyerdff49a22014-01-30 15:37:40 -0600890 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500891 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
892 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600893 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
894}
895
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800896function _parse_package_files {
897 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600898
Dean Troyerdff49a22014-01-30 15:37:40 -0600899 if [[ -z "$DISTRO" ]]; then
900 GetDistro
901 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600902
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800903 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600904 local OIFS line package distros distro
905 [[ -e $fname ]] || continue
906
907 OIFS=$IFS
908 IFS=$'\n'
909 for line in $(<${fname}); do
910 if [[ $line =~ "NOPRIME" ]]; then
911 continue
912 fi
913
914 # Assume we want this package
915 package=${line%#*}
916 inst_pkg=1
917
918 # Look for # dist:xxx in comment
919 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
920 # We are using BASH regexp matching feature.
921 package=${BASH_REMATCH[1]}
922 distros=${BASH_REMATCH[2]}
923 # In bash ${VAR,,} will lowecase VAR
924 # Look for a match in the distro list
925 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
926 # If no match then skip this package
927 inst_pkg=0
928 fi
929 fi
930
Dean Troyerdff49a22014-01-30 15:37:40 -0600931 if [[ $inst_pkg = 1 ]]; then
932 echo $package
933 fi
934 done
935 IFS=$OIFS
936 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800937}
938
939# get_packages() collects a list of package names of any type from the
940# prerequisite files in ``files/{debs|rpms}``. The list is intended
941# to be passed to a package installer such as apt or yum.
942#
943# Only packages required for the services in 1st argument will be
944# included. Two bits of metadata are recognized in the prerequisite files:
945#
946# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
947# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
948# of the package to the distros listed. The distro names are case insensitive.
949function get_packages {
950 local xtrace=$(set +o | grep xtrace)
951 set +o xtrace
952 local services=$@
953 local package_dir=$(_get_package_dir)
954 local file_to_parse=""
955 local service=""
956
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800957 if [[ -z "$package_dir" ]]; then
958 echo "No package directory supplied"
959 return 1
960 fi
961 for service in ${services//,/ }; do
962 # Allow individual services to specify dependencies
963 if [[ -e ${package_dir}/${service} ]]; then
964 file_to_parse="${file_to_parse} ${package_dir}/${service}"
965 fi
966 # NOTE(sdague) n-api needs glance for now because that's where
967 # glance client is
968 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700969 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800970 file_to_parse="${file_to_parse} ${package_dir}/nova"
971 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700972 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800973 file_to_parse="${file_to_parse} ${package_dir}/glance"
974 fi
975 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700976 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800977 file_to_parse="${file_to_parse} ${package_dir}/cinder"
978 fi
979 elif [[ $service == ceilometer-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700980 if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800981 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
982 fi
983 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700984 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800985 file_to_parse="${file_to_parse} ${package_dir}/swift"
986 fi
987 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700988 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800989 file_to_parse="${file_to_parse} ${package_dir}/nova"
990 fi
991 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700992 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800993 file_to_parse="${file_to_parse} ${package_dir}/glance"
994 fi
995 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700996 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800997 file_to_parse="${file_to_parse} ${package_dir}/keystone"
998 fi
999 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001000 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001001 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1002 fi
1003 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001004 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001005 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1006 fi
1007 fi
1008 done
1009 echo "$(_parse_package_files $file_to_parse)"
1010 $xtrace
1011}
1012
1013# get_plugin_packages() collects a list of package names of any type from a
1014# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1015# list is intended to be passed to a package installer such as apt or yum.
1016#
1017# Only packages required for enabled and collected plugins will included.
1018#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001019# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001020# in these prerequisite files, see get_packages() for more info.
1021function get_plugin_packages {
1022 local xtrace=$(set +o | grep xtrace)
1023 set +o xtrace
1024 local files_to_parse=""
1025 local package_dir=""
1026 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1027 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1028 files_to_parse+="$package_dir/$plugin"
1029 done
1030 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001031 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001032}
1033
1034# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001035# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001036# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001037function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001038 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1039 REPOS_UPDATED=${REPOS_UPDATED:-False}
1040 RETRY_UPDATE=${RETRY_UPDATE:-False}
1041
Paul Linchpiner9e179742014-07-13 22:23:00 -07001042 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001043 return 0
1044 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001045
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001046 if is_ubuntu; then
1047 local xtrace=$(set +o | grep xtrace)
1048 set +o xtrace
1049 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1050 # if there are transient errors pulling the updates, that's fine.
1051 # It may be secondary repositories that we don't really care about.
1052 apt_get update || /bin/true
1053 REPOS_UPDATED=True
1054 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001055 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001056 fi
1057}
1058
1059function real_install_package {
1060 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001061 apt_get install "$@"
1062 elif is_fedora; then
1063 yum_install "$@"
1064 elif is_suse; then
1065 zypper_install "$@"
1066 else
1067 exit_distro_not_supported "installing packages"
1068 fi
1069}
1070
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001071# Distro-agnostic package installer
1072# install_package package [package ...]
1073function install_package {
1074 update_package_repo
1075 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1076}
1077
Dean Troyerdff49a22014-01-30 15:37:40 -06001078# Distro-agnostic function to tell if a package is installed
1079# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001080function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001081 if [[ -z "$@" ]]; then
1082 return 1
1083 fi
1084
1085 if [[ -z "$os_PACKAGE" ]]; then
1086 GetOSVersion
1087 fi
1088
1089 if [[ "$os_PACKAGE" = "deb" ]]; then
1090 dpkg -s "$@" > /dev/null 2> /dev/null
1091 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1092 rpm --quiet -q "$@"
1093 else
1094 exit_distro_not_supported "finding if a package is installed"
1095 fi
1096}
1097
1098# Distro-agnostic package uninstaller
1099# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001100function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001101 if is_ubuntu; then
1102 apt_get purge "$@"
1103 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001104 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001105 elif is_suse; then
1106 sudo zypper rm "$@"
1107 else
1108 exit_distro_not_supported "uninstalling packages"
1109 fi
1110}
1111
1112# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001113# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001114# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001115function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001116 [[ "$OFFLINE" = "True" ]] && return
1117 local sudo="sudo"
1118 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001119
1120 # The manual check for missing packages is because yum -y assumes
1121 # missing packages are OK. See
1122 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001123 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1124 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001125 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001126 awk '
1127 BEGIN { fail=0 }
1128 /No package/ { fail=1 }
1129 { print }
1130 END { exit fail }' || \
1131 die $LINENO "Missing packages detected"
1132
1133 # also ensure we catch a yum failure
1134 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001135 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001136 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001137}
1138
1139# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001140# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001141# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001142function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001143 [[ "$OFFLINE" = "True" ]] && return
1144 local sudo="sudo"
1145 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001146 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1147 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001148 zypper --non-interactive install --auto-agree-with-licenses "$@"
1149}
1150
1151
1152# Process Functions
1153# =================
1154
1155# _run_process() is designed to be backgrounded by run_process() to simulate a
1156# fork. It includes the dirty work of closing extra filehandles and preparing log
1157# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001158# from the service name.
1159# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001160# If an optional group is provided sg will be used to set the group of
1161# the command.
1162# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001163function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001164 # disable tracing through the exec redirects, it's just confusing in the logs.
1165 xtrace=$(set +o | grep xtrace)
1166 set +o xtrace
1167
Dean Troyerdff49a22014-01-30 15:37:40 -06001168 local service=$1
1169 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001170 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001171
1172 # Undo logging redirections and close the extra descriptors
1173 exec 1>&3
1174 exec 2>&3
1175 exec 3>&-
1176 exec 6>&-
1177
Dean Troyerdde41d02014-12-09 17:47:57 -06001178 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1179 if [[ -n ${LOGDIR} ]]; then
1180 exec 1>&"$real_logfile" 2>&1
1181 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1182 if [[ -n ${SCREEN_LOGDIR} ]]; then
1183 # Drop the backward-compat symlink
1184 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1185 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001186
1187 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1188 export PYTHONUNBUFFERED=1
1189 fi
1190
Sean Dague6e137ab2015-04-29 08:22:24 -04001191 # reenable xtrace before we do *real* work
1192 $xtrace
1193
Dean Troyer3159a822014-08-27 14:13:58 -05001194 # Run under ``setsid`` to force the process to become a session and group leader.
1195 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001196 if [[ -n "$group" ]]; then
1197 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1198 else
1199 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1200 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001201
1202 # Just silently exit this process
1203 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001204}
1205
1206# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1207# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001208# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001209# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001210function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001211 SCREEN_NAME=${SCREEN_NAME:-stack}
1212 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1213
1214 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1215 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1216 fi
1217
1218 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1219}
1220
1221# Find out if a process exists by partial name.
1222# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001223function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001224 local name=$1
1225 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001226 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001227 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001228 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001229}
1230
Dean Troyer3159a822014-08-27 14:13:58 -05001231# Run a single service under screen or directly
1232# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001233# If an optional group is provided sg will be used to run the
1234# command as that group.
1235# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001236function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001237 local service=$1
1238 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001239 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001240
Dean Troyer3159a822014-08-27 14:13:58 -05001241 if is_service_enabled $service; then
1242 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001243 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001244 else
1245 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001246 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001247 fi
1248 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001249}
1250
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001251# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001252# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001253# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001254# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001255# Run a command in a shell in a screen window, if an optional group
1256# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001257function screen_process {
1258 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001259 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001260 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001261
Sean Dagueea22a4f2014-06-27 15:21:41 -04001262 SCREEN_NAME=${SCREEN_NAME:-stack}
1263 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001264 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001265
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001266 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001267
Dean Troyerdde41d02014-12-09 17:47:57 -06001268 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1269 echo "LOGDIR: $LOGDIR"
1270 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1271 echo "log: $real_logfile"
1272 if [[ -n ${LOGDIR} ]]; then
1273 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001274 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001275 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1276 if [[ -n ${SCREEN_LOGDIR} ]]; then
1277 # Drop the backward-compat symlink
1278 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1279 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001280 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001281
1282 # sleep to allow bash to be ready to be send the command - we are
1283 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001284 # bash isn't running by the time we send the command, nothing
1285 # happens. This sleep was added originally to handle gate runs
1286 # where we needed this to be at least 3 seconds to pass
1287 # consistently on slow clouds. Now this is configurable so that we
1288 # can determine a reasonable value for the local case which should
1289 # be much smaller.
1290 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001291
1292 NL=`echo -ne '\015'`
1293 # This fun command does the following:
1294 # - the passed server command is backgrounded
1295 # - the pid of the background process is saved in the usual place
1296 # - the server process is brought back to the foreground
1297 # - if the server process exits prematurely the fg command errors
1298 # and a message is written to stdout and the process failure file
1299 #
1300 # The pid saved can be used in stop_process() as a process group
1301 # id to kill off all child processes
1302 if [[ -n "$group" ]]; then
1303 command="sg $group '$command'"
1304 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001305
1306 # Append the process to the screen rc file
1307 screen_rc "$name" "$command"
1308
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001309 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 -06001310}
1311
1312# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001313# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001314# screen_rc service "command-line"
1315function screen_rc {
1316 SCREEN_NAME=${SCREEN_NAME:-stack}
1317 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1318 if [[ ! -e $SCREENRC ]]; then
1319 # Name the screen session
1320 echo "sessionname $SCREEN_NAME" > $SCREENRC
1321 # Set a reasonable statusbar
1322 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1323 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1324 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1325 echo "screen -t shell bash" >> $SCREENRC
1326 fi
1327 # If this service doesn't already exist in the screenrc file
1328 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1329 NL=`echo -ne '\015'`
1330 echo "screen -t $1 bash" >> $SCREENRC
1331 echo "stuff \"$2$NL\"" >> $SCREENRC
1332
Dean Troyerdde41d02014-12-09 17:47:57 -06001333 if [[ -n ${LOGDIR} ]]; then
1334 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001335 echo "log on" >>$SCREENRC
1336 fi
1337 fi
1338}
1339
1340# Stop a service in screen
1341# If a PID is available use it, kill the whole process group via TERM
1342# If screen is being used kill the screen window; this will catch processes
1343# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001344# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001345# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001346function screen_stop_service {
1347 local service=$1
1348
Dean Troyerdff49a22014-01-30 15:37:40 -06001349 SCREEN_NAME=${SCREEN_NAME:-stack}
1350 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001351 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001352
Dean Troyer3159a822014-08-27 14:13:58 -05001353 if is_service_enabled $service; then
1354 # Clean up the screen window
1355 screen -S $SCREEN_NAME -p $service -X kill
1356 fi
1357}
1358
1359# Stop a service process
1360# If a PID is available use it, kill the whole process group via TERM
1361# If screen is being used kill the screen window; this will catch processes
1362# that did not leave a PID behind
1363# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1364# stop_process service
1365function stop_process {
1366 local service=$1
1367
1368 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001369 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001370
1371 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001372 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001373 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1374 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1375 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001376 fi
1377 if [[ "$USE_SCREEN" = "True" ]]; then
1378 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001379 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001380 fi
1381 fi
1382}
1383
1384# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001385# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001386# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001387function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001388 local service
1389 local failures
1390 SCREEN_NAME=${SCREEN_NAME:-stack}
1391 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1392
1393
1394 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1395 echo "No service status directory found"
1396 return
1397 fi
1398
1399 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001400 # make this -o errexit safe
1401 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001402
1403 for service in $failures; do
1404 service=`basename $service`
1405 service=${service%.failure}
1406 echo "Error: Service $service is not running"
1407 done
1408
1409 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001410 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001411 fi
1412}
1413
Chris Dent2f27a0e2014-09-09 13:46:02 +01001414# Tail a log file in a screen if USE_SCREEN is true.
1415function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001416 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001417 local logfile=$2
1418
Sean Dague53753292014-12-04 19:38:15 -05001419 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001420 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001421 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001422 fi
1423}
1424
Dean Troyerdff49a22014-01-30 15:37:40 -06001425
Dean Troyer3159a822014-08-27 14:13:58 -05001426# Deprecated Functions
1427# --------------------
1428
1429# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1430# fork. It includes the dirty work of closing extra filehandles and preparing log
1431# files to produce the same logs as screen_it(). The log filename is derived
1432# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1433# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1434# _old_run_process service "command-line"
1435function _old_run_process {
1436 local service=$1
1437 local command="$2"
1438
1439 # Undo logging redirections and close the extra descriptors
1440 exec 1>&3
1441 exec 2>&3
1442 exec 3>&-
1443 exec 6>&-
1444
1445 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001446 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1447 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001448
1449 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1450 export PYTHONUNBUFFERED=1
1451 fi
1452
1453 exec /bin/bash -c "$command"
1454 die "$service exec failure: $command"
1455}
1456
1457# old_run_process() launches a child process that closes all file descriptors and
1458# then exec's the passed in command. This is meant to duplicate the semantics
1459# of screen_it() without screen. PIDs are written to
1460# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1461# old_run_process service "command-line"
1462function old_run_process {
1463 local service=$1
1464 local command="$2"
1465
1466 # Spawn the child process
1467 _old_run_process "$service" "$command" &
1468 echo $!
1469}
1470
1471# Compatibility for existing start_XXXX() functions
1472# Uses global ``USE_SCREEN``
1473# screen_it service "command-line"
1474function screen_it {
1475 if is_service_enabled $1; then
1476 # Append the service to the screen rc file
1477 screen_rc "$1" "$2"
1478
1479 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001480 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001481 else
1482 # Spawn directly without screen
1483 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1484 fi
1485 fi
1486}
1487
1488# Compatibility for existing stop_XXXX() functions
1489# Stop a service in screen
1490# If a PID is available use it, kill the whole process group via TERM
1491# If screen is being used kill the screen window; this will catch processes
1492# that did not leave a PID behind
1493# screen_stop service
1494function screen_stop {
1495 # Clean up the screen window
1496 stop_process $1
1497}
1498
1499
Sean Dague2c65e712014-12-18 09:44:56 -05001500# Plugin Functions
1501# =================
1502
1503DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1504
1505# enable_plugin <name> <url> [branch]
1506#
1507# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1508# ``url`` is a git url
1509# ``branch`` is a gitref. If it's not set, defaults to master
1510function enable_plugin {
1511 local name=$1
1512 local url=$2
1513 local branch=${3:-master}
1514 DEVSTACK_PLUGINS+=",$name"
1515 GITREPO[$name]=$url
1516 GITDIR[$name]=$DEST/$name
1517 GITBRANCH[$name]=$branch
1518}
1519
1520# fetch_plugins
1521#
1522# clones all plugins
1523function fetch_plugins {
1524 local plugins="${DEVSTACK_PLUGINS}"
1525 local plugin
1526
1527 # short circuit if nothing to do
1528 if [[ -z $plugins ]]; then
1529 return
1530 fi
1531
Dean Troyerdc97cb72015-03-28 08:20:50 -05001532 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001533 for plugin in ${plugins//,/ }; do
1534 git_clone_by_name $plugin
1535 done
1536}
1537
1538# load_plugin_settings
1539#
1540# Load settings from plugins in the order that they were registered
1541function load_plugin_settings {
1542 local plugins="${DEVSTACK_PLUGINS}"
1543 local plugin
1544
1545 # short circuit if nothing to do
1546 if [[ -z $plugins ]]; then
1547 return
1548 fi
1549
1550 echo "Loading plugin settings"
1551 for plugin in ${plugins//,/ }; do
1552 local dir=${GITDIR[$plugin]}
1553 # source any known settings
1554 if [[ -f $dir/devstack/settings ]]; then
1555 source $dir/devstack/settings
1556 fi
1557 done
1558}
1559
Sean Dague6e275e12015-03-26 05:54:28 -04001560# plugin_override_defaults
1561#
1562# Run an extremely early setting phase for plugins that allows default
1563# overriding of services.
1564function plugin_override_defaults {
1565 local plugins="${DEVSTACK_PLUGINS}"
1566 local plugin
1567
1568 # short circuit if nothing to do
1569 if [[ -z $plugins ]]; then
1570 return
1571 fi
1572
1573 echo "Overriding Configuration Defaults"
1574 for plugin in ${plugins//,/ }; do
1575 local dir=${GITDIR[$plugin]}
1576 # source any overrides
1577 if [[ -f $dir/devstack/override-defaults ]]; then
1578 # be really verbose that an override is happening, as it
1579 # may not be obvious if things fail later.
1580 echo "$plugin has overriden the following defaults"
1581 cat $dir/devstack/override-defaults
1582 source $dir/devstack/override-defaults
1583 fi
1584 done
1585}
1586
Sean Dague2c65e712014-12-18 09:44:56 -05001587# run_plugins
1588#
1589# Run the devstack/plugin.sh in all the plugin directories. These are
1590# run in registration order.
1591function run_plugins {
1592 local mode=$1
1593 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301594
1595 local plugins="${DEVSTACK_PLUGINS}"
1596 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001597 for plugin in ${plugins//,/ }; do
1598 local dir=${GITDIR[$plugin]}
1599 if [[ -f $dir/devstack/plugin.sh ]]; then
1600 source $dir/devstack/plugin.sh $mode $phase
1601 fi
1602 done
1603}
1604
1605function run_phase {
1606 local mode=$1
1607 local phase=$2
1608 if [[ -d $TOP_DIR/extras.d ]]; then
1609 for i in $TOP_DIR/extras.d/*.sh; do
1610 [[ -r $i ]] && source $i $mode $phase
1611 done
1612 fi
1613 # the source phase corresponds to settings loading in plugins
1614 if [[ "$mode" == "source" ]]; then
1615 load_plugin_settings
Sean Dague6e275e12015-03-26 05:54:28 -04001616 elif [[ "$mode" == "override_defaults" ]]; then
1617 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001618 else
1619 run_plugins $mode $phase
1620 fi
1621}
1622
Dean Troyerdff49a22014-01-30 15:37:40 -06001623
1624# Service Functions
1625# =================
1626
1627# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1628# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001629function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001630 echo "$1" | sed -e '
1631 s/,,/,/g;
1632 s/^,//;
1633 s/,$//
1634 '
1635}
1636
1637# disable_all_services() removes all current services
1638# from ``ENABLED_SERVICES`` to reset the configuration
1639# before a minimal installation
1640# Uses global ``ENABLED_SERVICES``
1641# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001642function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001643 ENABLED_SERVICES=""
1644}
1645
1646# Remove all services starting with '-'. For example, to install all default
1647# services except rabbit (rabbit) set in ``localrc``:
1648# ENABLED_SERVICES+=",-rabbit"
1649# Uses global ``ENABLED_SERVICES``
1650# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001651function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001652 local to_remove=""
1653 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001654 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001655
1656 # build up list of services that should be removed; i.e. they
1657 # begin with "-"
1658 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001659 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001660 to_remove+=",${service#-}"
1661 else
1662 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001663 fi
1664 done
Ian Wienand2796a822015-04-15 08:59:04 +10001665
1666 # go through the service list. if this service appears in the "to
1667 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001668 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Dean Troyerdff49a22014-01-30 15:37:40 -06001669}
1670
1671# disable_service() removes the services passed as argument to the
1672# ``ENABLED_SERVICES`` list, if they are present.
1673#
1674# For example:
1675# disable_service rabbit
1676#
1677# This function does not know about the special cases
1678# for nova, glance, and neutron built into is_service_enabled().
1679# Uses global ``ENABLED_SERVICES``
1680# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001681function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001682 local tmpsvcs=",${ENABLED_SERVICES},"
1683 local service
1684 for service in $@; do
1685 if is_service_enabled $service; then
1686 tmpsvcs=${tmpsvcs//,$service,/,}
1687 fi
1688 done
1689 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1690}
1691
1692# enable_service() adds the services passed as argument to the
1693# ``ENABLED_SERVICES`` list, if they are not already present.
1694#
1695# For example:
1696# enable_service qpid
1697#
1698# This function does not know about the special cases
1699# for nova, glance, and neutron built into is_service_enabled().
1700# Uses global ``ENABLED_SERVICES``
1701# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001702function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001703 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001704 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001705 for service in $@; do
1706 if ! is_service_enabled $service; then
1707 tmpsvcs+=",$service"
1708 fi
1709 done
1710 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1711 disable_negated_services
1712}
1713
1714# is_service_enabled() checks if the service(s) specified as arguments are
1715# enabled by the user in ``ENABLED_SERVICES``.
1716#
1717# Multiple services specified as arguments are ``OR``'ed together; the test
1718# is a short-circuit boolean, i.e it returns on the first match.
1719#
1720# There are special cases for some 'catch-all' services::
1721# **nova** returns true if any service enabled start with **n-**
1722# **cinder** returns true if any service enabled start with **c-**
1723# **ceilometer** returns true if any service enabled start with **ceilometer**
1724# **glance** returns true if any service enabled start with **g-**
1725# **neutron** returns true if any service enabled start with **q-**
1726# **swift** returns true if any service enabled start with **s-**
1727# **trove** returns true if any service enabled start with **tr-**
1728# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1729# **s-** services will be enabled. This will be deprecated in the future.
1730#
1731# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1732# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1733# as enabled in this case.
1734#
1735# Uses global ``ENABLED_SERVICES``
1736# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001737function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001738 local xtrace=$(set +o | grep xtrace)
1739 set +o xtrace
1740 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001741 local services=$@
1742 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001743 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001744 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001745
1746 # Look for top-level 'enabled' function for this service
1747 if type is_${service}_enabled >/dev/null 2>&1; then
1748 # A function exists for this service, use it
1749 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001750 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001751 fi
1752
1753 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1754 # are implemented
1755
Sean Dague45917cc2014-02-24 16:09:14 -05001756 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001757 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001758 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1759 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1760 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1761 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1762 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1763 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1764 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1765 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1766 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001767 done
Sean Dague45917cc2014-02-24 16:09:14 -05001768 $xtrace
1769 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001770}
1771
fumihiko kakuma8606c982015-04-13 09:55:06 +09001772# remove specified list from the input string
1773# remove_disabled_services service-list remove-list
1774function remove_disabled_services {
1775 local service_list=$1
1776 local remove_list=$2
1777 local service
1778 local enabled=""
1779
1780 for service in ${service_list//,/ }; do
1781 local remove
1782 local add=1
1783 for remove in ${remove_list//,/ }; do
1784 if [[ ${remove} == ${service} ]]; then
1785 add=0
1786 break
1787 fi
1788 done
1789 if [[ $add == 1 ]]; then
1790 enabled="${enabled},$service"
1791 fi
1792 done
1793 _cleanup_service_list "$enabled"
1794}
1795
Dean Troyerdff49a22014-01-30 15:37:40 -06001796# Toggle enable/disable_service for services that must run exclusive of each other
1797# $1 The name of a variable containing a space-separated list of services
1798# $2 The name of a variable in which to store the enabled service's name
1799# $3 The name of the service to enable
1800function use_exclusive_service {
1801 local options=${!1}
1802 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001803 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001804 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001805 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001806 for opt in $options;do
1807 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1808 done
1809 eval "$out=$selection"
1810 return 0
1811}
1812
1813
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001814# System Functions
1815# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001816
1817# Only run the command if the target file (the last arg) is not on an
1818# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001819function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001820 local xtrace=$(set +o | grep xtrace)
1821 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001822 local args=( $@ )
1823 local last
1824 local sudo_cmd
1825 local dir_to_check
1826
1827 let last="${#args[*]} - 1"
1828
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001829 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001830 if [ ! -d "$dir_to_check" ]; then
1831 dir_to_check=`dirname "$dir_to_check"`
1832 fi
1833
1834 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001835 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001836 return 0
1837 fi
1838
1839 if [[ $TRACK_DEPENDS = True ]]; then
1840 sudo_cmd="env"
1841 else
1842 sudo_cmd="sudo"
1843 fi
1844
Sean Dague45917cc2014-02-24 16:09:14 -05001845 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001846 $sudo_cmd $@
1847}
1848
1849# Exit 0 if address is in network or 1 if address is not in network
1850# ip-range is in CIDR notation: 1.2.3.4/20
1851# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001852function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001853 local ip=$1
1854 local range=$2
1855 local masklen=${range#*/}
1856 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1857 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1858 [[ $network == $subnet ]]
1859}
1860
1861# Add a user to a group.
1862# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001863function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001864 local user=$1
1865 local group=$2
1866
Thomas Bechtolda8580852015-05-31 00:04:33 +02001867 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06001868}
1869
1870# Convert CIDR notation to a IPv4 netmask
1871# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001872function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001873 local maskpat="255 255 255 255"
1874 local maskdgt="254 252 248 240 224 192 128"
1875 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1876 echo ${1-0}.${2-0}.${3-0}.${4-0}
1877}
1878
1879# Gracefully cp only if source file/dir exists
1880# cp_it source destination
1881function cp_it {
1882 if [ -e $1 ] || [ -d $1 ]; then
1883 cp -pRL $1 $2
1884 fi
1885}
1886
1887# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1888# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1889# ``localrc`` or on the command line if necessary::
1890#
1891# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1892#
1893# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1894
Ian Wienandaee18c72014-02-21 15:35:08 +11001895function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001896 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001897 export http_proxy=$http_proxy
1898 fi
Sean Dague53753292014-12-04 19:38:15 -05001899 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001900 export https_proxy=$https_proxy
1901 fi
Sean Dague53753292014-12-04 19:38:15 -05001902 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001903 export no_proxy=$no_proxy
1904 fi
1905}
1906
1907# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001908function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001909 local mount_type=`stat -f -L -c %T $1`
1910 test "$mount_type" == "nfs"
1911}
1912
1913# Return the network portion of the given IP address using netmask
1914# netmask is in the traditional dotted-quad format
1915# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001916function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001917 local ip=$1
1918 local mask=$2
1919 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1920 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1921 echo $subnet
1922}
1923
Chris Dent3a2c86a2015-05-12 13:41:25 +00001924# Return the current python as "python<major>.<minor>"
1925function python_version {
1926 local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
1927 echo "python${python_version}"
1928}
1929
Dean Troyerdff49a22014-01-30 15:37:40 -06001930# Service wrapper to restart services
1931# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001932function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001933 if is_ubuntu; then
1934 sudo /usr/sbin/service $1 restart
1935 else
1936 sudo /sbin/service $1 restart
1937 fi
1938}
1939
1940# Only change permissions of a file or directory if it is not on an
1941# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001942function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001943 _safe_permission_operation chmod $@
1944}
1945
1946# Only change ownership of a file or directory if it is not on an NFS
1947# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001948function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001949 _safe_permission_operation chown $@
1950}
1951
1952# Service wrapper to start services
1953# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001954function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001955 if is_ubuntu; then
1956 sudo /usr/sbin/service $1 start
1957 else
1958 sudo /sbin/service $1 start
1959 fi
1960}
1961
1962# Service wrapper to stop services
1963# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001964function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001965 if is_ubuntu; then
1966 sudo /usr/sbin/service $1 stop
1967 else
1968 sudo /sbin/service $1 stop
1969 fi
1970}
1971
Sean Dague442e4e92015-06-24 13:24:02 -04001972# Test with a finite retry loop.
1973#
1974function test_with_retry {
1975 local testcmd=$1
1976 local failmsg=$2
1977 local until=${3:-10}
1978 local sleep=${4:-0.5}
1979
1980 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
1981 die $LINENO "$failmsg"
1982 fi
1983}
1984
Dean Troyerdff49a22014-01-30 15:37:40 -06001985
1986# Restore xtrace
1987$XTRACE
1988
1989# Local variables:
1990# mode: shell-script
1991# End: