blob: e44fea112db0b5617a368a582fd3d2f58c6e6cab [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``
31# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050032#
Dean Troyerdff49a22014-01-30 15:37:40 -060033
34# Save trace setting
Ian Wienand523f4882015-10-13 11:03:03 +110035_XTRACE_FUNCTIONS_COMMON=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -060036set +o xtrace
37
Ian Wienand4ffb4542015-06-30 11:00:32 +100038# ensure we don't re-source this in the same environment
39[[ -z "$_DEVSTACK_FUNCTIONS_COMMON" ]] || return 0
40declare -r _DEVSTACK_FUNCTIONS_COMMON=1
41
Sean Daguecc524062014-10-01 09:06:43 -040042# Global Config Variables
43declare -A GITREPO
44declare -A GITBRANCH
45declare -A GITDIR
46
Sean Dague53753292014-12-04 19:38:15 -050047TRACK_DEPENDS=${TRACK_DEPENDS:-False}
48
Dean Troyer68162342015-05-13 15:41:03 -050049# Save these variables to .stackenv
50STACK_ENV_VARS="BASE_SQL_CONN DATA_DIR DEST ENABLED_SERVICES HOST_IP \
51 KEYSTONE_AUTH_PROTOCOL KEYSTONE_AUTH_URI KEYSTONE_SERVICE_URI \
Brian Haley180f5eb2015-06-16 13:14:31 -040052 LOGFILE OS_CACERT SERVICE_HOST SERVICE_PROTOCOL STACK_USER TLS_IP \
Brian Haley5d04db22015-06-16 13:14:31 -040053 HOST_IPV6 SERVICE_IP_VERSION"
Dean Troyer68162342015-05-13 15:41:03 -050054
55
56# Saves significant environment variables to .stackenv for later use
57# Refers to a lot of globals, only TOP_DIR and STACK_ENV_VARS are required to
58# function, the rest are simply saved and do not cause problems if they are undefined.
59# save_stackenv [tag]
60function save_stackenv {
61 local tag=${1:-""}
62 # Save some values we generated for later use
63 time_stamp=$(date "+$TIMESTAMP_FORMAT")
64 echo "# $time_stamp $tag" >$TOP_DIR/.stackenv
65 for i in $STACK_ENV_VARS; do
66 echo $i=${!i} >>$TOP_DIR/.stackenv
67 done
68}
Dean Troyerdff49a22014-01-30 15:37:40 -060069
Monty Taylor7224eec2015-09-19 11:26:18 -040070# Update/create user clouds.yaml file.
71# clouds.yaml will have
72# - A `devstack` entry for the `demo` user for the `demo` project.
73# - A `devstack-admin` entry for the `admin` user for the `admin` project.
74# write_clouds_yaml
75function write_clouds_yaml {
Monty Tayloree9bb762015-10-19 15:16:18 -040076 # The location is a variable to allow for easier refactoring later to make it
77 # overridable. There is currently no usecase where doing so makes sense, so
78 # it's not currently configurable.
Monty Taylor7224eec2015-09-19 11:26:18 -040079
Monty Tayloree9bb762015-10-19 15:16:18 -040080 CLOUDS_YAML=/etc/openstack/clouds.yaml
81
82 sudo mkdir -p $(dirname $CLOUDS_YAML)
Lenny Verkhovsky313ddae2015-10-20 11:26:34 +030083 sudo chown -R $STACK_USER /etc/openstack
Lenny Verkhovsky313ddae2015-10-20 11:26:34 +030084
Monty Tayloree9bb762015-10-19 15:16:18 -040085 CA_CERT_ARG=''
86 if [ -f "$SSL_BUNDLE_FILE" ]; then
87 CA_CERT_ARG="--os-cacert $SSL_BUNDLE_FILE"
88 fi
Sean Daguec67d22e2016-02-02 05:51:14 -050089 # demo -> devstack
Monty Tayloree9bb762015-10-19 15:16:18 -040090 $TOP_DIR/tools/update_clouds_yaml.py \
91 --file $CLOUDS_YAML \
92 --os-cloud devstack \
93 --os-region-name $REGION_NAME \
94 --os-identity-api-version 3 \
95 $CA_CERT_ARG \
96 --os-auth-url $KEYSTONE_AUTH_URI \
97 --os-username demo \
98 --os-password $ADMIN_PASSWORD \
99 --os-project-name demo
Sean Daguec67d22e2016-02-02 05:51:14 -0500100
101 # alt_demo -> devstack-alt
102 $TOP_DIR/tools/update_clouds_yaml.py \
103 --file $CLOUDS_YAML \
Clark Boylane20cb432016-02-05 12:00:18 -0800104 --os-cloud devstack-alt \
Sean Daguec67d22e2016-02-02 05:51:14 -0500105 --os-region-name $REGION_NAME \
106 --os-identity-api-version 3 \
107 $CA_CERT_ARG \
108 --os-auth-url $KEYSTONE_AUTH_URI \
109 --os-username alt_demo \
110 --os-password $ADMIN_PASSWORD \
111 --os-project-name alt_demo
112
113 # admin -> devstack-admin
Monty Tayloree9bb762015-10-19 15:16:18 -0400114 $TOP_DIR/tools/update_clouds_yaml.py \
115 --file $CLOUDS_YAML \
116 --os-cloud devstack-admin \
117 --os-region-name $REGION_NAME \
118 --os-identity-api-version 3 \
119 $CA_CERT_ARG \
120 --os-auth-url $KEYSTONE_AUTH_URI \
121 --os-username admin \
122 --os-password $ADMIN_PASSWORD \
123 --os-project-name admin
Monty Taylor74379df2016-01-21 10:20:08 -0500124
125 # CLean up any old clouds.yaml files we had laying around
ZhiQiang Fan0b4a0092016-04-12 20:26:33 +0800126 rm -f $(eval echo ~"$STACK_USER")/.config/openstack/clouds.yaml
Monty Taylor7224eec2015-09-19 11:26:18 -0400127}
128
Ian Wienande82bac02015-08-25 14:29:08 +1000129# trueorfalse <True|False> <VAR>
130#
131# Normalize config-value provided in variable VAR to either "True" or
132# "False". If VAR is unset (i.e. $VAR evaluates as empty), the value
133# of the second argument will be used as the default value.
134#
135# Accepts as False: 0 no No NO false False FALSE
136# Accepts as True: 1 yes Yes YES true True TRUE
137#
138# usage:
139# VAL=$(trueorfalse False VAL)
Ian Wienandaee18c72014-02-21 15:35:08 +1100140function trueorfalse {
Ian Wienand433a9b12015-10-07 13:29:31 +1100141 local xtrace
142 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -0500143 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600144
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900145 local default=$1
Ian Wienande82bac02015-08-25 14:29:08 +1000146
147 if [ -z $2 ]; then
148 die $LINENO "variable to normalize required"
149 fi
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900150 local testval=${!2:-}
151
152 case "$testval" in
153 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
154 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
155 * ) echo "$default" ;;
156 esac
157
Sean Dague45917cc2014-02-24 16:09:14 -0500158 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600159}
160
Attila Fazekas1bd79592015-02-24 14:06:56 +0100161function isset {
162 [[ -v "$1" ]]
163}
Dean Troyerdff49a22014-01-30 15:37:40 -0600164
Dean Troyer68162342015-05-13 15:41:03 -0500165
Dean Troyerdff49a22014-01-30 15:37:40 -0600166# Control Functions
167# =================
168
169# Prints backtrace info
170# filename:lineno:function
171# backtrace level
172function backtrace {
173 local level=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100174 local deep
175 deep=$((${#BASH_SOURCE[@]} - 1))
Dean Troyerdff49a22014-01-30 15:37:40 -0600176 echo "[Call Trace]"
177 while [ $level -le $deep ]; do
178 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
179 deep=$((deep - 1))
180 done
181}
182
183# Prints line number and "message" then exits
184# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100185function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600186 local exitcode=$?
187 set +o xtrace
188 local line=$1; shift
189 if [ $exitcode == 0 ]; then
190 exitcode=1
191 fi
192 backtrace 2
193 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600194 # Give buffers a second to flush
195 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600196 exit $exitcode
197}
198
199# Checks an environment variable is not set or has length 0 OR if the
200# exit code is non-zero and prints "message" and exits
201# NOTE: env-var is the variable name without a '$'
202# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100203function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600204 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100205 local xtrace
206 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600207 set +o xtrace
208 local line=$1; shift
209 local evar=$1; shift
210 if ! is_set $evar || [ $exitcode != 0 ]; then
211 die $line "$*"
212 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500213 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600214}
215
Sean Dague1de9e332015-10-07 08:46:13 -0400216function deprecated {
217 local text=$1
218 DEPRECATED_TEXT+="\n$text"
219 echo "WARNING: $text"
220}
221
Dean Troyerdff49a22014-01-30 15:37:40 -0600222# Prints line number and "message" in error format
223# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100224function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600225 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100226 local xtrace
227 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600228 set +o xtrace
229 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
230 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600231 if [[ -n ${LOGDIR} ]]; then
232 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600233 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500234 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600235 return $exitcode
236}
237
238# Checks an environment variable is not set or has length 0 OR if the
239# exit code is non-zero and prints "message"
240# NOTE: env-var is the variable name without a '$'
241# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100242function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600243 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100244 local xtrace
245 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600246 set +o xtrace
247 local line=$1; shift
248 local evar=$1; shift
249 if ! is_set $evar || [ $exitcode != 0 ]; then
250 err $line "$*"
251 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500252 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600253 return $exitcode
254}
255
256# Exit after outputting a message about the distribution not being supported.
257# exit_distro_not_supported [optional-string-telling-what-is-missing]
258function exit_distro_not_supported {
259 if [[ -z "$DISTRO" ]]; then
260 GetDistro
261 fi
262
263 if [ $# -gt 0 ]; then
264 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
265 else
266 die $LINENO "Support for $DISTRO is incomplete."
267 fi
268}
269
270# Test if the named environment variable is set and not zero length
271# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100272function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600273 local var=\$"$1"
274 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
275}
276
277# Prints line number and "message" in warning format
278# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100279function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600280 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100281 local xtrace
282 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600283 set +o xtrace
284 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Sean Daguee4af9292015-04-28 08:57:57 -0400285 echo $msg
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500286 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600287 return $exitcode
288}
289
290
291# Distro Functions
292# ================
293
294# Determine OS Vendor, Release and Update
Ian Wienand7710e7f2014-08-27 16:15:32 +1000295
296#
297# NOTE : For portability, you almost certainly do not want to use
298# these variables directly! The "is_*" functions defined below this
299# bundle up compatible platforms under larger umbrellas that we have
300# determinted are compatible enough (e.g. is_ubuntu covers Ubuntu &
301# Debian, is_fedora covers RPM-based distros). Higher-level functions
302# such as "install_package" further abstract things in better ways.
303#
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500304# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
305# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500306# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
Ian Wienand7710e7f2014-08-27 16:15:32 +1000307# ``os_CODENAME`` - vendor's codename for release: ``trusty``
308
309declare os_VENDOR os_RELEASE os_PACKAGE os_CODENAME
310
311# Make a *best effort* attempt to install lsb_release packages for the
312# user if not available. Note can't use generic install_package*
313# because they depend on this!
314function _ensure_lsb_release {
Thomas Bechtolddab39012016-03-03 12:11:15 +0100315 if [[ -x $(command -v lsb_release 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000316 return
317 fi
318
Thomas Bechtolddab39012016-03-03 12:11:15 +0100319 if [[ -x $(command -v apt-get 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000320 sudo apt-get install -y lsb-release
Thomas Bechtolddab39012016-03-03 12:11:15 +0100321 elif [[ -x $(command -v zypper 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000322 # XXX: old code paths seem to have assumed SUSE platforms also
323 # had "yum". Keep this ordered above yum so we don't try to
324 # install the rh package. suse calls it just "lsb"
Thomas Bechtold899dfee2016-03-03 11:27:53 +0100325 sudo zypper -n install lsb
Thomas Bechtolddab39012016-03-03 12:11:15 +0100326 elif [[ -x $(command -v dnf 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000327 sudo dnf install -y redhat-lsb-core
Thomas Bechtolddab39012016-03-03 12:11:15 +0100328 elif [[ -x $(command -v yum 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000329 # all rh patforms (fedora, centos, rhel) have this pkg
330 sudo yum install -y redhat-lsb-core
331 else
332 die $LINENO "Unable to find or auto-install lsb_release"
333 fi
334}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500335
Dean Troyerdff49a22014-01-30 15:37:40 -0600336# GetOSVersion
Ian Wienand7710e7f2014-08-27 16:15:32 +1000337# Set the following variables:
338# - os_RELEASE
339# - os_CODENAME
340# - os_VENDOR
341# - os_PACKAGE
Ian Wienandaee18c72014-02-21 15:35:08 +1100342function GetOSVersion {
Ian Wienand7710e7f2014-08-27 16:15:32 +1000343 # We only support distros that provide a sane lsb_release
344 _ensure_lsb_release
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500345
Ian Wienand7710e7f2014-08-27 16:15:32 +1000346 os_RELEASE=$(lsb_release -r -s)
347 os_CODENAME=$(lsb_release -c -s)
348 os_VENDOR=$(lsb_release -i -s)
Dean Troyerdff49a22014-01-30 15:37:40 -0600349
Ian Wienand7710e7f2014-08-27 16:15:32 +1000350 if [[ $os_VENDOR =~ (Debian|Ubuntu|LinuxMint) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600351 os_PACKAGE="deb"
Ian Wienand7710e7f2014-08-27 16:15:32 +1000352 else
353 os_PACKAGE="rpm"
Dean Troyerdff49a22014-01-30 15:37:40 -0600354 fi
Ian Wienand7710e7f2014-08-27 16:15:32 +1000355
356 typeset -xr os_VENDOR
357 typeset -xr os_RELEASE
358 typeset -xr os_PACKAGE
359 typeset -xr os_CODENAME
Dean Troyerdff49a22014-01-30 15:37:40 -0600360}
361
362# Translate the OS version values into common nomenclature
363# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500364declare DISTRO
365
Ian Wienandaee18c72014-02-21 15:35:08 +1100366function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600367 GetOSVersion
ptoohill196006652016-02-15 16:07:50 -0600368 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) || \
369 "$os_VENDOR" =~ (LinuxMint) ]]; then
370 # 'Everyone' refers to Ubuntu / Debian / Mint releases by
Ian Wienand7710e7f2014-08-27 16:15:32 +1000371 # the code name adjective
Dean Troyerdff49a22014-01-30 15:37:40 -0600372 DISTRO=$os_CODENAME
373 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
374 # For Fedora, just use 'f' and the release
375 DISTRO="f$os_RELEASE"
376 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
377 DISTRO="opensuse-$os_RELEASE"
378 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000379 # just use major release
380 DISTRO="sle${os_RELEASE%.*}"
381 elif [[ "$os_VENDOR" =~ (Red.*Hat) || \
anju Tiwari6c639c92014-07-15 18:11:54 +0530382 "$os_VENDOR" =~ (CentOS) || \
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300383 "$os_VENDOR" =~ (OracleServer) || \
Evgeny Antyshev718512c2016-03-03 14:47:58 +0000384 "$os_VENDOR" =~ (Virtuozzo) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600385 # Drop the . release as we assume it's compatible
Ian Wienand7710e7f2014-08-27 16:15:32 +1000386 # XXX re-evaluate when we get RHEL10
Dean Troyerdff49a22014-01-30 15:37:40 -0600387 DISTRO="rhel${os_RELEASE::1}"
388 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000389 DISTRO="xs${os_RELEASE%.*}"
Alexander Schmidta5ea08b2015-12-02 15:30:44 +0000390 elif [[ "$os_VENDOR" =~ (kvmibm) ]]; then
391 DISTRO="${os_VENDOR}${os_RELEASE::1}"
Dean Troyerdff49a22014-01-30 15:37:40 -0600392 else
Ian Wienanded92e432016-02-16 10:56:56 +1100393 # We can't make a good choice here. Setting a sensible DISTRO
394 # is part of the problem, but not the major issue -- we really
395 # only use DISTRO in the code as a fine-filter.
396 #
397 # The bigger problem is categorising the system into one of
398 # our two big categories as Ubuntu/Debian-ish or
399 # Fedora/CentOS-ish.
400 #
401 # The setting of os_PACKAGE above is only set to "deb" based
402 # on a hard-coded list of vendor names ... thus we will
403 # default to thinking unknown distros are RPM based
404 # (ie. is_ubuntu does not match). But the platform will then
405 # also not match in is_fedora, because that also has a list of
406 # names.
407 #
408 # So, if you are reading this, getting your distro supported
409 # is really about making sure it matches correctly in these
410 # functions. Then you can choose a sensible way to construct
411 # DISTRO based on your distros release approach.
412 die $LINENO "Unable to determine DISTRO, can not continue."
Dean Troyerdff49a22014-01-30 15:37:40 -0600413 fi
Ian Wienand7710e7f2014-08-27 16:15:32 +1000414 typeset -xr DISTRO
Dean Troyerdff49a22014-01-30 15:37:40 -0600415}
416
417# Utility function for checking machine architecture
418# is_arch arch-type
419function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500420 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600421}
422
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700423# Determine if current distribution is an Oracle distribution
424# is_oraclelinux
425function is_oraclelinux {
426 if [[ -z "$os_VENDOR" ]]; then
427 GetOSVersion
428 fi
429
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300430 [ "$os_VENDOR" = "OracleServer" ]
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700431}
432
433
Dean Troyerdff49a22014-01-30 15:37:40 -0600434# Determine if current distribution is a Fedora-based distribution
435# (Fedora, RHEL, CentOS, etc).
436# is_fedora
437function is_fedora {
438 if [[ -z "$os_VENDOR" ]]; then
439 GetOSVersion
440 fi
441
anju Tiwari6c639c92014-07-15 18:11:54 +0530442 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
tengqm78d37392016-03-15 23:08:00 -0400443 [ "$os_VENDOR" = "RedHatEnterpriseServer" ] || \
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300444 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ] || \
Evgeny Antyshev718512c2016-03-03 14:47:58 +0000445 [ "$os_VENDOR" = "Virtuozzo" ] || [ "$os_VENDOR" = "kvmibm" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600446}
447
448
449# Determine if current distribution is a SUSE-based distribution
450# (openSUSE, SLE).
451# is_suse
452function is_suse {
453 if [[ -z "$os_VENDOR" ]]; then
454 GetOSVersion
455 fi
456
Ralf Haferkamp37dddb72016-03-02 15:25:57 +0100457 [[ "$os_VENDOR" =~ (openSUSE) || "$os_VENDOR" == "SUSE LINUX" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600458}
459
460
461# Determine if current distribution is an Ubuntu-based distribution
462# It will also detect non-Ubuntu but Debian-based distros
463# is_ubuntu
464function is_ubuntu {
465 if [[ -z "$os_PACKAGE" ]]; then
466 GetOSVersion
467 fi
468 [ "$os_PACKAGE" = "deb" ]
469}
470
471
472# Git Functions
473# =============
474
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600475# Returns openstack release name for a given branch name
476# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100477function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600478 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700479 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600480 echo ${branch#*/}
481 else
482 echo "master"
483 fi
484}
485
Dean Troyerdff49a22014-01-30 15:37:40 -0600486# git clone only if directory doesn't exist already. Since ``DEST`` might not
487# be owned by the installation user, we create the directory and change the
488# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500489# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
490# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600491# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500492# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600493# git_clone remote dest-dir branch
494function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500495 local git_remote=$1
496 local git_dest=$2
497 local git_ref=$3
Ian Wienandada886d2015-10-07 14:06:26 +1100498 local orig_dir
499 orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200500 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500501
Sean Dague53753292014-12-04 19:38:15 -0500502 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800503 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200504 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
505 fi
506
Dean Troyerdff49a22014-01-30 15:37:40 -0600507 if [[ "$OFFLINE" = "True" ]]; then
508 echo "Running in offline mode, clones already exist"
509 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500510 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600511 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400512 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600513 return
514 fi
515
Dean Troyer50cda692014-07-25 11:57:20 -0500516 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600517 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500518 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700519 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
520 echo "The $git_dest project was not found; if this is a gate job, add"
521 echo "the project to the \$PROJECTS variable in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600522 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700523 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200524 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600525 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500526 cd $git_dest
527 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600528 else
529 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500530 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700531 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
532 echo "The $git_dest project was not found; if this is a gate job, add"
533 echo "the project to the \$PROJECTS variable in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600534 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700535 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200536 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500537 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600538 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500539 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600540 elif [[ "$RECLONE" = "True" ]]; then
541 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500542 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600543 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500544 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100545 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600546 # remove the existing ignored files (like pyc) as they cause breakage
547 # (due to the py files having older timestamps than our pyc, so python
548 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500549 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600550
Dean Troyer50cda692014-07-25 11:57:20 -0500551 # handle git_ref accordingly to type (tag, branch)
552 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
553 git_update_tag $git_ref
554 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
555 git_update_branch $git_ref
556 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
557 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600558 else
Dean Troyer50cda692014-07-25 11:57:20 -0500559 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600560 fi
561
562 fi
563 fi
564
565 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500566 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600567 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400568 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600569}
570
Sean Daguecc524062014-10-01 09:06:43 -0400571# A variation on git clone that lets us specify a project by it's
572# actual name, like oslo.config. This is exceptionally useful in the
573# library installation case
574function git_clone_by_name {
575 local name=$1
576 local repo=${GITREPO[$name]}
577 local dir=${GITDIR[$name]}
578 local branch=${GITBRANCH[$name]}
579 git_clone $repo $dir $branch
580}
581
582
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100583# git can sometimes get itself infinitely stuck with transient network
584# errors or other issues with the remote end. This wraps git in a
585# timeout/retry loop and is intended to watch over non-local git
586# processes that might hang. GIT_TIMEOUT, if set, is passed directly
587# to timeout(1); otherwise the default value of 0 maintains the status
588# quo of waiting forever.
589# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100590function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100591 local count=0
592 local timeout=0
593
594 if [[ -n "${GIT_TIMEOUT}" ]]; then
595 timeout=${GIT_TIMEOUT}
596 fi
597
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900598 time_start "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100599 until timeout -s SIGINT ${timeout} git "$@"; do
600 # 124 is timeout(1)'s special return code when it reached the
601 # timeout; otherwise assume fatal failure
602 if [[ $? -ne 124 ]]; then
603 die $LINENO "git call failed: [git $@]"
604 fi
605
606 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400607 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100608 if [ $count -eq 3 ]; then
609 die $LINENO "Maximum of 3 git retries reached"
610 fi
611 sleep 5
612 done
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900613 time_stop "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100614}
615
Dean Troyerdff49a22014-01-30 15:37:40 -0600616# git update using reference as a branch.
617# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100618function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500619 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600620
Dean Troyer50cda692014-07-25 11:57:20 -0500621 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600622 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500623 git branch -D $git_branch || true
624 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600625}
626
627# git update using reference as a branch.
628# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100629function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500630 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600631
Dean Troyer50cda692014-07-25 11:57:20 -0500632 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600633}
634
635# git update using reference as a tag. Be careful editing source at that repo
636# as working copy will be in a detached mode
637# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100638function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500639 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600640
Dean Troyer50cda692014-07-25 11:57:20 -0500641 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600642 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500643 git_timed fetch origin tag $git_tag
644 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600645}
646
647
648# OpenStack Functions
649# ===================
650
651# Get the default value for HOST_IP
652# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100653function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600654 local fixed_range=$1
655 local floating_range=$2
656 local host_ip_iface=$3
657 local host_ip=$4
Brian Haley180f5eb2015-06-16 13:14:31 -0400658 local af=$5
Dean Troyerdff49a22014-01-30 15:37:40 -0600659
Dean Troyerdff49a22014-01-30 15:37:40 -0600660 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
661 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
662 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100663 # Find the interface used for the default route
Brian Haley180f5eb2015-06-16 13:14:31 -0400664 host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
Ian Wienandada886d2015-10-07 14:06:26 +1100665 local host_ips
666 host_ips=$(LC_ALL=C ip -f $af addr show ${host_ip_iface} | sed /temporary/d |awk /$af'/ {split($2,parts,"/"); print parts[1]}')
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500667 local ip
668 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600669 # Attempt to filter out IP addresses that are part of the fixed and
670 # floating range. Note that this method only works if the ``netaddr``
671 # python library is installed. If it is not installed, an error
672 # will be printed and the first IP from the interface will be used.
673 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
674 # address.
Brian Haley180f5eb2015-06-16 13:14:31 -0400675 if [[ "$af" == "inet6" ]]; then
676 host_ip=$ip
677 break;
678 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500679 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
680 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600681 break;
682 fi
683 done
684 fi
685 echo $host_ip
686}
687
Attila Fazekasf71b5002014-05-28 09:52:22 +0200688# Generates hex string from ``size`` byte of pseudo random data
689# generate_hex_string size
690function generate_hex_string {
691 local size=$1
692 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
693}
694
Dean Troyerdff49a22014-01-30 15:37:40 -0600695# Grab a numbered field from python prettytable output
696# Fields are numbered starting with 1
697# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
698# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100699function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500700 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600701 while read data; do
702 if [ "$1" -lt 0 ]; then
703 field="(\$(NF$1))"
704 else
705 field="\$$(($1 + 1))"
706 fi
707 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
708 done
709}
710
yuntongjinf26deea2015-02-28 10:50:34 +0800711# install default policy
712# copy over a default policy.json and policy.d for projects
713function install_default_policy {
714 local project=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100715 local project_uc
716 project_uc=$(echo $1|tr a-z A-Z)
yuntongjinf26deea2015-02-28 10:50:34 +0800717 local conf_dir="${project_uc}_CONF_DIR"
718 # eval conf dir to get the variable
719 conf_dir="${!conf_dir}"
720 local project_dir="${project_uc}_DIR"
721 # eval project dir to get the variable
722 project_dir="${!project_dir}"
723 local sample_conf_dir="${project_dir}/etc/${project}"
724 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
725
726 # first copy any policy.json
727 cp -p $sample_conf_dir/policy.json $conf_dir
728 # then optionally copy over policy.d
729 if [[ -d $sample_policy_dir ]]; then
730 cp -r $sample_policy_dir $conf_dir/policy.d
731 fi
732}
733
Dean Troyerdff49a22014-01-30 15:37:40 -0600734# Add a policy to a policy.json file
735# Do nothing if the policy already exists
736# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100737function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600738 local policy_file=$1
739 local policy_name=$2
740 local policy_perm=$3
741
742 if grep -q ${policy_name} ${policy_file}; then
743 echo "Policy ${policy_name} already exists in ${policy_file}"
744 return
745 fi
746
747 # Add a terminating comma to policy lines without one
748 # Remove the closing '}' and all lines following to the end-of-file
Ian Wienandada886d2015-10-07 14:06:26 +1100749 local tmpfile
750 tmpfile=$(mktemp)
Dean Troyerdff49a22014-01-30 15:37:40 -0600751 uniq ${policy_file} | sed -e '
752 s/]$/],/
753 /^[}]/,$d
754 ' > ${tmpfile}
755
756 # Append policy and closing brace
757 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
758 echo "}" >>${tmpfile}
759
760 mv ${tmpfile} ${policy_file}
761}
762
Alistair Coles24779f62014-10-15 18:57:59 +0100763# Gets or creates a domain
764# Usage: get_or_create_domain <name> <description>
765function get_or_create_domain {
Ian Wienand11d276c2015-07-02 09:34:34 +1000766 local domain_id
Alistair Coles24779f62014-10-15 18:57:59 +0100767 # Gets domain id
Ian Wienand11d276c2015-07-02 09:34:34 +1000768 domain_id=$(
Alistair Coles24779f62014-10-15 18:57:59 +0100769 # Gets domain id
Steve Martinelli050a0d52015-09-06 22:03:54 +0000770 openstack domain show $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100771 -f value -c id 2>/dev/null ||
772 # Creates new domain
Steve Martinelli050a0d52015-09-06 22:03:54 +0000773 openstack domain create $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100774 --description "$2" \
775 -f value -c id
776 )
777 echo $domain_id
778}
779
Steve Martinellib74e01c2014-12-18 01:35:35 -0500780# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000781# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500782function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500783 local desc="${3:-}"
Ian Wienand11d276c2015-07-02 09:34:34 +1000784 local group_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500785 # Gets group id
Ian Wienand11d276c2015-07-02 09:34:34 +1000786 group_id=$(
Steve Martinellib74e01c2014-12-18 01:35:35 -0500787 # Creates new group with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000788 openstack group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000789 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500790 -f value -c id
791 )
792 echo $group_id
793}
794
Bartosz Górski0abde392014-02-28 14:15:19 +0100795# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000796# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100797function get_or_create_user {
Ian Wienand11d276c2015-07-02 09:34:34 +1000798 local user_id
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000799 if [[ ! -z "$4" ]]; then
800 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200801 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500802 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200803 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100804 # Gets user id
Ian Wienand11d276c2015-07-02 09:34:34 +1000805 user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500806 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000807 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100808 $1 \
809 --password "$2" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000810 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500811 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500812 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100813 -f value -c id
814 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500815 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100816}
817
818# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000819# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100820function get_or_create_project {
Ian Wienand11d276c2015-07-02 09:34:34 +1000821 local project_id
822 project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500823 # Creates new project with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000824 openstack project create $1 \
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000825 --domain=$2 \
826 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100827 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500828 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100829}
830
831# Gets or creates role
832# Usage: get_or_create_role <name>
833function get_or_create_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000834 local role_id
835 role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500836 # Creates role with --or-show
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000837 openstack role create $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000838 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100839 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500840 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100841}
842
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600843# Returns the domain parts of a function call if present
844# Usage: _get_domain_args [<user_domain> <project_domain>]
845function _get_domain_args {
846 local domain
847 domain=""
848
849 if [[ -n "$1" ]]; then
850 domain="$domain --user-domain $1"
851 fi
852 if [[ -n "$2" ]]; then
853 domain="$domain --project-domain $2"
854 fi
855
856 echo $domain
857}
858
Jamie Lennox9b215db2015-02-10 18:19:57 +1100859# Gets or adds user role to project
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600860# Usage: get_or_add_user_project_role <role> <user> <project> [<user_domain> <project_domain>]
Jamie Lennox9b215db2015-02-10 18:19:57 +1100861function get_or_add_user_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000862 local user_role_id
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600863
864 domain_args=$(_get_domain_args $4 $5)
865
Bartosz Górski0abde392014-02-28 14:15:19 +0100866 # Gets user role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000867 user_role_id=$(openstack role list \
Steve Martinelli5541a612015-01-19 15:58:49 -0500868 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100869 --column "ID" \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000870 --project $3 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100871 --column "Name" \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600872 $domain_args \
Bartosz Górski0abde392014-02-28 14:15:19 +0100873 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500874 if [[ -z "$user_role_id" ]]; then
Roxana Gherle50821be2015-09-22 10:52:46 -0700875 # Adds role to user and get it
876 openstack role add $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100877 --user $2 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600878 --project $3 \
879 $domain_args
Roxana Gherle50821be2015-09-22 10:52:46 -0700880 user_role_id=$(openstack role list \
881 --user $2 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700882 --column "ID" \
883 --project $3 \
884 --column "Name" \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600885 $domain_args \
Roxana Gherle50821be2015-09-22 10:52:46 -0700886 | grep " $1 " | get_field 1)
Bartosz Górski0abde392014-02-28 14:15:19 +0100887 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500888 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100889}
890
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500891# Gets or adds user role to domain
892# Usage: get_or_add_user_domain_role <role> <user> <domain>
893function get_or_add_user_domain_role {
894 local user_role_id
895 # Gets user role id
896 user_role_id=$(openstack role list \
897 --user $2 \
898 --column "ID" \
899 --domain $3 \
900 --column "Name" \
901 | grep " $1 " | get_field 1)
902 if [[ -z "$user_role_id" ]]; then
903 # Adds role to user and get it
904 openstack role add $1 \
905 --user $2 \
906 --domain $3
907 user_role_id=$(openstack role list \
908 --user $2 \
909 --column "ID" \
910 --domain $3 \
911 --column "Name" \
912 | grep " $1 " | get_field 1)
913 fi
914 echo $user_role_id
915}
916
Roxana Gherle59c63772015-09-09 18:22:31 -0700917# Gets or adds user role to domain
918# Usage: get_or_add_user_domain_role <role> <user> <domain>
919function get_or_add_user_domain_role {
920 local user_role_id
921 # Gets user role id
922 user_role_id=$(openstack role list \
923 --user $2 \
924 --os-url=$KEYSTONE_SERVICE_URI_V3 \
925 --os-identity-api-version=3 \
926 --column "ID" \
927 --domain $3 \
928 --column "Name" \
929 | grep " $1 " | get_field 1)
930 if [[ -z "$user_role_id" ]]; then
931 # Adds role to user and get it
932 openstack role add $1 \
933 --user $2 \
934 --domain $3 \
935 --os-url=$KEYSTONE_SERVICE_URI_V3 \
936 --os-identity-api-version=3
937 user_role_id=$(openstack role list \
938 --user $2 \
939 --os-url=$KEYSTONE_SERVICE_URI_V3 \
940 --os-identity-api-version=3 \
941 --column "ID" \
942 --domain $3 \
943 --column "Name" \
944 | grep " $1 " | get_field 1)
945 fi
946 echo $user_role_id
947}
948
Steve Martinelli4599fd12015-03-12 21:30:58 -0400949# Gets or adds group role to project
950# Usage: get_or_add_group_project_role <role> <group> <project>
951function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000952 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -0400953 # Gets group role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000954 group_role_id=$(openstack role list \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400955 --group $2 \
956 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000957 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400958 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000959 # Adds role to group and get it
960 openstack role add $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000961 --group $2 \
962 --project $3
963 group_role_id=$(openstack role list \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400964 --group $2 \
965 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000966 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400967 fi
968 echo $group_role_id
969}
970
Bartosz Górski0abde392014-02-28 14:15:19 +0100971# Gets or creates service
972# Usage: get_or_create_service <name> <type> <description>
973function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +1000974 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100975 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +1000976 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100977 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +1000978 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100979 # Creates new service if not exists
980 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500981 $2 \
982 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100983 --description="$3" \
984 -f value -c id
985 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500986 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100987}
988
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000989# Create an endpoint with a specific interface
Matt Riedemannae4578b2016-04-23 01:45:40 +0000990# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
991function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +1000992 local endpoint_id
Matt Riedemannae4578b2016-04-23 01:45:40 +0000993 endpoint_id=$(openstack endpoint list \
994 --service $1 \
995 --interface $2 \
996 --region $4 \
997 -c ID -f value)
998 if [[ -z "$endpoint_id" ]]; then
999 # Creates new endpoint
1000 endpoint_id=$(openstack endpoint create \
1001 $1 $2 $3 --region $4 -f value -c id)
1002 fi
1003
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001004 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +01001005}
Dean Troyerdff49a22014-01-30 15:37:40 -06001006
Sean Dague7d1ec432016-04-22 09:19:10 -04001007# Gets or creates endpoint
Matt Riedemannae4578b2016-04-23 01:45:40 +00001008# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
1009function get_or_create_endpoint {
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001010 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
1011 # creating one endpoint with multiple urls to multiple endpoints each with
1012 # a different interface. To maintain the existing function interface we
Matt Riedemannae4578b2016-04-23 01:45:40 +00001013 # create 3 endpoints and return the id of the public one. In reality
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001014 # returning the public id will not make a lot of difference as there are no
1015 # scenarios currently that use the returned id. Ideally this behaviour
1016 # should be pushed out to the service setups and let them create the
1017 # endpoints they need.
Ian Wienandada886d2015-10-07 14:06:26 +11001018 local public_id
Matt Riedemannae4578b2016-04-23 01:45:40 +00001019 public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
1020 _get_or_create_endpoint_with_interface $1 admin $4 $2
1021 _get_or_create_endpoint_with_interface $1 internal $5 $2
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001022
1023 # return the public id to indicate success, and this is the endpoint most likely wanted
1024 echo $public_id
1025}
1026
1027# Get a URL from the identity service
1028# Usage: get_endpoint_url <service> <interface>
1029function get_endpoint_url {
1030 echo $(openstack endpoint list \
1031 --service $1 --interface $2 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001032 -c URL -f value)
1033}
1034
Jim Rollenhagen47367072015-12-10 14:24:00 +00001035# check if we are using ironic with hardware
1036# TODO(jroll) this is a kludge left behind when ripping ironic code
1037# out of tree, as it is used by nova and neutron.
1038# figure out a way to refactor nova/neutron code to eliminate this
1039function is_ironic_hardware {
Lucas Alvares Gomes8a4dea22016-02-18 15:52:22 +00001040 is_service_enabled ironic && [[ "$IRONIC_IS_HARDWARE" == "True" ]] && return 0
Jim Rollenhagen47367072015-12-10 14:24:00 +00001041 return 1
1042}
1043
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001044
Dean Troyerdff49a22014-01-30 15:37:40 -06001045# Package Functions
1046# =================
1047
1048# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +11001049function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001050 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -06001051 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001052
1053 if [[ -z "$base_dir" ]]; then
1054 base_dir=$FILES
1055 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001056 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001057 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -06001058 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001059 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -06001060 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001061 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -06001062 else
1063 exit_distro_not_supported "list of packages"
1064 fi
1065 echo "$pkg_dir"
1066}
1067
Sean Dague88ee8ce2015-12-02 07:47:31 -05001068# Wrapper for ``apt-get update`` to try multiple times on the update
1069# to address bad package mirrors (which happen all the time).
1070function apt_get_update {
1071 # only do this once per run
1072 if [[ "$REPOS_UPDATED" == "True" && "$RETRY_UPDATE" != "True" ]]; then
1073 return
1074 fi
1075
1076 # bail if we are offline
1077 [[ "$OFFLINE" = "True" ]] && return
1078
1079 local sudo="sudo"
1080 [[ "$(id -u)" = "0" ]] && sudo="env"
1081
1082 # time all the apt operations
1083 time_start "apt-get-update"
1084
1085 local proxies="http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} no_proxy=${no_proxy:-} "
1086 local update_cmd="$sudo $proxies apt-get update"
1087 if ! timeout 300 sh -c "while ! $update_cmd; do sleep 30; done"; then
1088 die $LINENO "Failed to update apt repos, we're dead now"
1089 fi
1090
1091 REPOS_UPDATED=True
1092 # stop the clock
1093 time_stop "apt-get-update"
1094}
1095
Dean Troyerdff49a22014-01-30 15:37:40 -06001096# Wrapper for ``apt-get`` to set cache and proxy environment variables
1097# Uses globals ``OFFLINE``, ``*_proxy``
1098# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001099function apt_get {
Federico Ressie208d062015-11-21 11:15:39 +00001100 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +11001101 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001102 set +o xtrace
1103
Dean Troyerdff49a22014-01-30 15:37:40 -06001104 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
1105 local sudo="sudo"
1106 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -05001107
Sean Dague95c33d52015-10-07 11:05:59 -04001108 # time all the apt operations
1109 time_start "apt-get"
1110
Sean Dague45917cc2014-02-24 16:09:14 -05001111 $xtrace
Sean Dague53753292014-12-04 19:38:15 -05001112
Dean Troyerdff49a22014-01-30 15:37:40 -06001113 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -05001114 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
1115 no_proxy=${no_proxy:-} \
John L. Villalovosf568c3a2016-01-07 19:10:50 -08001116 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" < /dev/null
Federico Ressie208d062015-11-21 11:15:39 +00001117 result=$?
Sean Dague95c33d52015-10-07 11:05:59 -04001118
1119 # stop the clock
1120 time_stop "apt-get"
Federico Ressie208d062015-11-21 11:15:39 +00001121 return $result
Dean Troyerdff49a22014-01-30 15:37:40 -06001122}
1123
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001124function _parse_package_files {
1125 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -06001126
Dean Troyerdff49a22014-01-30 15:37:40 -06001127 if [[ -z "$DISTRO" ]]; then
1128 GetDistro
1129 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001130
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001131 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001132 local OIFS line package distros distro
1133 [[ -e $fname ]] || continue
1134
1135 OIFS=$IFS
1136 IFS=$'\n'
1137 for line in $(<${fname}); do
1138 if [[ $line =~ "NOPRIME" ]]; then
1139 continue
1140 fi
1141
Ian Wienand1ff75ff2016-02-19 14:28:37 +11001142 # Assume we want this package; free-form
1143 # comments allowed after a #
1144 package=${line%%#*}
Dean Troyerdff49a22014-01-30 15:37:40 -06001145 inst_pkg=1
1146
1147 # Look for # dist:xxx in comment
1148 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1149 # We are using BASH regexp matching feature.
1150 package=${BASH_REMATCH[1]}
1151 distros=${BASH_REMATCH[2]}
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001152 # In bash ${VAR,,} will lowercase VAR
Dean Troyerdff49a22014-01-30 15:37:40 -06001153 # Look for a match in the distro list
1154 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1155 # If no match then skip this package
1156 inst_pkg=0
1157 fi
1158 fi
1159
Dean Troyerdff49a22014-01-30 15:37:40 -06001160 if [[ $inst_pkg = 1 ]]; then
1161 echo $package
1162 fi
1163 done
1164 IFS=$OIFS
1165 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001166}
1167
1168# get_packages() collects a list of package names of any type from the
1169# prerequisite files in ``files/{debs|rpms}``. The list is intended
1170# to be passed to a package installer such as apt or yum.
1171#
1172# Only packages required for the services in 1st argument will be
1173# included. Two bits of metadata are recognized in the prerequisite files:
1174#
1175# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1176# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1177# of the package to the distros listed. The distro names are case insensitive.
1178function get_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001179 local xtrace
1180 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001181 set +o xtrace
1182 local services=$@
Ian Wienandada886d2015-10-07 14:06:26 +11001183 local package_dir
1184 package_dir=$(_get_package_dir)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001185 local file_to_parse=""
1186 local service=""
1187
Ian Wienand7d515b52015-11-09 15:04:32 +11001188 if [ $# -ne 1 ]; then
1189 die $LINENO "get_packages takes a single, comma-separated argument"
1190 fi
1191
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001192 if [[ -z "$package_dir" ]]; then
1193 echo "No package directory supplied"
1194 return 1
1195 fi
1196 for service in ${services//,/ }; do
1197 # Allow individual services to specify dependencies
1198 if [[ -e ${package_dir}/${service} ]]; then
1199 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1200 fi
1201 # NOTE(sdague) n-api needs glance for now because that's where
1202 # glance client is
1203 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001204 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001205 file_to_parse="${file_to_parse} ${package_dir}/nova"
1206 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001207 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001208 file_to_parse="${file_to_parse} ${package_dir}/glance"
1209 fi
1210 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001211 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001212 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1213 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001214 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001215 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001216 file_to_parse="${file_to_parse} ${package_dir}/swift"
1217 fi
1218 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001219 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001220 file_to_parse="${file_to_parse} ${package_dir}/nova"
1221 fi
1222 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001223 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001224 file_to_parse="${file_to_parse} ${package_dir}/glance"
1225 fi
1226 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001227 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001228 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1229 fi
1230 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001231 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001232 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1233 fi
1234 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001235 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001236 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1237 fi
1238 fi
1239 done
1240 echo "$(_parse_package_files $file_to_parse)"
1241 $xtrace
1242}
1243
1244# get_plugin_packages() collects a list of package names of any type from a
1245# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1246# list is intended to be passed to a package installer such as apt or yum.
1247#
1248# Only packages required for enabled and collected plugins will included.
1249#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001250# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001251# in these prerequisite files, see get_packages() for more info.
1252function get_plugin_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001253 local xtrace
1254 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001255 set +o xtrace
1256 local files_to_parse=""
1257 local package_dir=""
1258 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
Ian Wienand7ae97292016-02-16 14:50:53 +11001259 package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
Dmitry Tantsur5979f472016-01-03 18:08:14 +01001260 files_to_parse+=" $package_dir/$plugin"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001261 done
1262 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001263 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001264}
1265
1266# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001267# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001268# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001269function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001270 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1271 REPOS_UPDATED=${REPOS_UPDATED:-False}
1272 RETRY_UPDATE=${RETRY_UPDATE:-False}
1273
Paul Linchpiner9e179742014-07-13 22:23:00 -07001274 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001275 return 0
1276 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001277
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001278 if is_ubuntu; then
Sean Dague88ee8ce2015-12-02 07:47:31 -05001279 apt_get_update
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001280 fi
1281}
1282
1283function real_install_package {
1284 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001285 apt_get install "$@"
1286 elif is_fedora; then
1287 yum_install "$@"
1288 elif is_suse; then
1289 zypper_install "$@"
1290 else
1291 exit_distro_not_supported "installing packages"
1292 fi
1293}
1294
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001295# Distro-agnostic package installer
1296# install_package package [package ...]
1297function install_package {
1298 update_package_repo
Federico Ressiecc1f412015-12-28 15:14:13 +01001299 if ! real_install_package "$@"; then
1300 RETRY_UPDATE=True update_package_repo && real_install_package "$@"
1301 fi
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001302}
1303
Dean Troyerdff49a22014-01-30 15:37:40 -06001304# Distro-agnostic function to tell if a package is installed
1305# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001306function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001307 if [[ -z "$@" ]]; then
1308 return 1
1309 fi
1310
1311 if [[ -z "$os_PACKAGE" ]]; then
1312 GetOSVersion
1313 fi
1314
1315 if [[ "$os_PACKAGE" = "deb" ]]; then
1316 dpkg -s "$@" > /dev/null 2> /dev/null
1317 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1318 rpm --quiet -q "$@"
1319 else
1320 exit_distro_not_supported "finding if a package is installed"
1321 fi
1322}
1323
1324# Distro-agnostic package uninstaller
1325# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001326function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001327 if is_ubuntu; then
1328 apt_get purge "$@"
1329 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001330 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001331 elif is_suse; then
Thomas Bechtoldc15d5912016-03-04 16:06:24 +01001332 sudo zypper rm "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001333 else
1334 exit_distro_not_supported "uninstalling packages"
1335 fi
1336}
1337
1338# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001339# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001340# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001341function yum_install {
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001342 local result parse_yum_result
1343
Dean Troyerdff49a22014-01-30 15:37:40 -06001344 [[ "$OFFLINE" = "True" ]] && return
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001345
1346 time_start "yum_install"
1347
Ian Wienand1b1cc8c2016-03-01 13:14:39 +11001348 # - We run with LC_ALL=C so string matching *should* be OK
1349 # - Exit 1 if the failure might get better with a retry.
1350 # - Exit 2 if it is fatal.
Ian Wienand3c601682016-02-29 15:15:39 +11001351 parse_yum_result=' \
1352 BEGIN { result=0 } \
1353 /^YUM_FAILED/ { exit $2 } \
Ian Wienand1b1cc8c2016-03-01 13:14:39 +11001354 /^No package/ { result=2 } \
1355 /^Failed:/ { result=2 } \
Ian Wienand3c601682016-02-29 15:15:39 +11001356 //{ print } \
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001357 END { exit result }'
Ian Wienandb27f16d2014-02-28 14:29:02 +11001358
1359 # The manual check for missing packages is because yum -y assumes
Ian Wienand3c601682016-02-29 15:15:39 +11001360 # missing or failed packages are OK.
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001361 # See https://bugzilla.redhat.com/show_bug.cgi?id=965567
1362 (sudo_with_proxies "${YUM:-yum}" install -y "$@" 2>&1 || echo YUM_FAILED $?) \
Ian Wienand1b1cc8c2016-03-01 13:14:39 +11001363 | awk "$parse_yum_result" && result=$? || result=$?
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001364
1365 time_stop "yum_install"
1366
Ian Wienand1b1cc8c2016-03-01 13:14:39 +11001367 # if we return 1, then the wrapper functions will run an update
1368 # and try installing the package again as a defense against bad
1369 # mirrors. This can hide failures, especially when we have
1370 # packages that are in the "Failed:" section because their rpm
1371 # install scripts failed to run correctly (in this case, the
1372 # package looks installed, so when the retry happens we just think
1373 # the package is OK, and incorrectly continue on).
1374 if [ "$result" == 2 ]; then
1375 die "Detected fatal package install failure"
1376 fi
1377
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001378 return "$result"
Dean Troyerdff49a22014-01-30 15:37:40 -06001379}
1380
1381# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001382# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001383# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001384function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001385 [[ "$OFFLINE" = "True" ]] && return
1386 local sudo="sudo"
1387 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001388 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1389 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001390 zypper --non-interactive install --auto-agree-with-licenses "$@"
1391}
1392
1393
1394# Process Functions
1395# =================
1396
1397# _run_process() is designed to be backgrounded by run_process() to simulate a
1398# fork. It includes the dirty work of closing extra filehandles and preparing log
1399# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001400# from the service name.
1401# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001402# If an optional group is provided sg will be used to set the group of
1403# the command.
1404# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001405function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001406 # disable tracing through the exec redirects, it's just confusing in the logs.
1407 xtrace=$(set +o | grep xtrace)
1408 set +o xtrace
1409
Dean Troyerdff49a22014-01-30 15:37:40 -06001410 local service=$1
1411 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001412 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001413
1414 # Undo logging redirections and close the extra descriptors
1415 exec 1>&3
1416 exec 2>&3
1417 exec 3>&-
1418 exec 6>&-
1419
Philipp Marekaf0801d2015-12-09 13:51:56 +01001420 local logfile="${service}.log.${CURRENT_LOG_TIME}"
1421 local real_logfile="${LOGDIR}/${logfile}"
Dean Troyerdde41d02014-12-09 17:47:57 -06001422 if [[ -n ${LOGDIR} ]]; then
1423 exec 1>&"$real_logfile" 2>&1
Philipp Marekaf0801d2015-12-09 13:51:56 +01001424 bash -c "cd '$LOGDIR' && ln -sf '$logfile' ${service}.log"
Dean Troyerdde41d02014-12-09 17:47:57 -06001425 if [[ -n ${SCREEN_LOGDIR} ]]; then
1426 # Drop the backward-compat symlink
1427 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1428 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001429
1430 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1431 export PYTHONUNBUFFERED=1
1432 fi
1433
Sean Dague6e137ab2015-04-29 08:22:24 -04001434 # reenable xtrace before we do *real* work
1435 $xtrace
1436
Dean Troyer3159a822014-08-27 14:13:58 -05001437 # Run under ``setsid`` to force the process to become a session and group leader.
1438 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001439 if [[ -n "$group" ]]; then
1440 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1441 else
1442 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1443 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001444
1445 # Just silently exit this process
1446 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001447}
1448
1449# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1450# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001451# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001452# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001453function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001454 SCREEN_NAME=${SCREEN_NAME:-stack}
1455 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1456
1457 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1458 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1459 fi
1460
1461 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1462}
1463
1464# Find out if a process exists by partial name.
1465# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001466function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001467 local name=$1
1468 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001469 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001470 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001471 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001472}
1473
Dean Troyer3159a822014-08-27 14:13:58 -05001474# Run a single service under screen or directly
1475# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001476# If an optional group is provided sg will be used to run the
1477# command as that group.
Tony Breeds93292902015-11-09 15:45:04 +11001478# Uses globals ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001479# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001480function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001481 local service=$1
1482 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001483 local group=$3
Brant Knudsonedc11c22015-12-14 15:32:05 -06001484 local subservice=$4
1485
1486 local name=${subservice:-$service}
Dean Troyerdff49a22014-01-30 15:37:40 -06001487
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001488 time_start "run_process"
Dean Troyer3159a822014-08-27 14:13:58 -05001489 if is_service_enabled $service; then
1490 if [[ "$USE_SCREEN" = "True" ]]; then
Brant Knudsonedc11c22015-12-14 15:32:05 -06001491 screen_process "$name" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001492 else
1493 # Spawn directly without screen
Brant Knudsonedc11c22015-12-14 15:32:05 -06001494 _run_process "$name" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001495 fi
1496 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001497 time_stop "run_process"
Dean Troyerdff49a22014-01-30 15:37:40 -06001498}
1499
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001500# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001501# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Tony Breeds93292902015-11-09 15:45:04 +11001502# ``SERVICE_DIR``, ``SCREEN_IS_LOGGING``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001503# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001504# Run a command in a shell in a screen window, if an optional group
1505# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001506function screen_process {
1507 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001508 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001509 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001510
Sean Dagueea22a4f2014-06-27 15:21:41 -04001511 SCREEN_NAME=${SCREEN_NAME:-stack}
1512 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyerdff49a22014-01-30 15:37:40 -06001513
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001514 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001515
Philipp Marekaf0801d2015-12-09 13:51:56 +01001516 local logfile="${name}.log.${CURRENT_LOG_TIME}"
1517 local real_logfile="${LOGDIR}/${logfile}"
Dean Troyerdde41d02014-12-09 17:47:57 -06001518 echo "LOGDIR: $LOGDIR"
1519 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1520 echo "log: $real_logfile"
1521 if [[ -n ${LOGDIR} ]]; then
Tony Breeds536b8c12015-11-09 10:05:37 +11001522 if [[ "$SCREEN_IS_LOGGING" == "True" ]]; then
1523 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
1524 screen -S $SCREEN_NAME -p $name -X log on
1525 fi
1526 # If logging isn't active then avoid a broken symlink
1527 touch "$real_logfile"
Philipp Marekaf0801d2015-12-09 13:51:56 +01001528 bash -c "cd '$LOGDIR' && ln -sf '$logfile' ${name}.log"
Dean Troyerdde41d02014-12-09 17:47:57 -06001529 if [[ -n ${SCREEN_LOGDIR} ]]; then
1530 # Drop the backward-compat symlink
1531 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1532 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001533 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001534
1535 # sleep to allow bash to be ready to be send the command - we are
1536 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001537 # bash isn't running by the time we send the command, nothing
1538 # happens. This sleep was added originally to handle gate runs
1539 # where we needed this to be at least 3 seconds to pass
1540 # consistently on slow clouds. Now this is configurable so that we
1541 # can determine a reasonable value for the local case which should
1542 # be much smaller.
1543 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001544
1545 NL=`echo -ne '\015'`
1546 # This fun command does the following:
1547 # - the passed server command is backgrounded
1548 # - the pid of the background process is saved in the usual place
1549 # - the server process is brought back to the foreground
1550 # - if the server process exits prematurely the fg command errors
1551 # and a message is written to stdout and the process failure file
1552 #
1553 # The pid saved can be used in stop_process() as a process group
1554 # id to kill off all child processes
1555 if [[ -n "$group" ]]; then
1556 command="sg $group '$command'"
1557 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001558
1559 # Append the process to the screen rc file
1560 screen_rc "$name" "$command"
1561
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001562 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 -06001563}
1564
1565# Screen rc file builder
Tony Breeds536b8c12015-11-09 10:05:37 +11001566# Uses globals ``SCREEN_NAME``, ``SCREENRC``, ``SCREEN_IS_LOGGING``
Dean Troyerdff49a22014-01-30 15:37:40 -06001567# screen_rc service "command-line"
1568function screen_rc {
1569 SCREEN_NAME=${SCREEN_NAME:-stack}
1570 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1571 if [[ ! -e $SCREENRC ]]; then
1572 # Name the screen session
1573 echo "sessionname $SCREEN_NAME" > $SCREENRC
1574 # Set a reasonable statusbar
1575 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1576 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1577 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1578 echo "screen -t shell bash" >> $SCREENRC
1579 fi
1580 # If this service doesn't already exist in the screenrc file
1581 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1582 NL=`echo -ne '\015'`
1583 echo "screen -t $1 bash" >> $SCREENRC
1584 echo "stuff \"$2$NL\"" >> $SCREENRC
1585
Tony Breeds536b8c12015-11-09 10:05:37 +11001586 if [[ -n ${LOGDIR} ]] && [[ "$SCREEN_IS_LOGGING" == "True" ]]; then
Dean Troyerdde41d02014-12-09 17:47:57 -06001587 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001588 echo "log on" >>$SCREENRC
1589 fi
1590 fi
1591}
1592
1593# Stop a service in screen
1594# If a PID is available use it, kill the whole process group via TERM
1595# If screen is being used kill the screen window; this will catch processes
1596# that did not leave a PID behind
Tony Breeds93292902015-11-09 15:45:04 +11001597# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001598# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001599function screen_stop_service {
1600 local service=$1
1601
Dean Troyerdff49a22014-01-30 15:37:40 -06001602 SCREEN_NAME=${SCREEN_NAME:-stack}
1603 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyerdff49a22014-01-30 15:37:40 -06001604
Dean Troyer3159a822014-08-27 14:13:58 -05001605 if is_service_enabled $service; then
1606 # Clean up the screen window
Attila Fazekasf750a6f2015-07-01 12:17:35 +02001607 screen -S $SCREEN_NAME -p $service -X kill || true
Dean Troyer3159a822014-08-27 14:13:58 -05001608 fi
1609}
1610
1611# Stop a service process
1612# If a PID is available use it, kill the whole process group via TERM
1613# If screen is being used kill the screen window; this will catch processes
1614# that did not leave a PID behind
1615# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1616# stop_process service
1617function stop_process {
1618 local service=$1
1619
1620 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer3159a822014-08-27 14:13:58 -05001621
1622 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001623 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001624 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1625 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
Dan Smithce7246a2015-04-23 09:41:06 -07001626 # oslo.service tends to stop actually shutting down
1627 # reliably in between releases because someone believes it
1628 # is dying too early due to some inflight work they
1629 # have. This is a tension. It happens often enough we're
1630 # going to just account for it in devstack and assume it
1631 # doesn't work.
1632 #
1633 # Set OSLO_SERVICE_WORKS=True to skip this block
1634 if [[ -z "$OSLO_SERVICE_WORKS" ]]; then
1635 # TODO(danms): Remove this double-kill when we have
1636 # this fixed in all services:
1637 # https://bugs.launchpad.net/oslo-incubator/+bug/1446583
1638 sleep 1
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001639 # /bin/true because pkill on a non existent process returns an error
Dan Smithce7246a2015-04-23 09:41:06 -07001640 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid) || /bin/true
1641 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001642 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001643 fi
1644 if [[ "$USE_SCREEN" = "True" ]]; then
1645 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001646 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001647 fi
1648 fi
1649}
1650
1651# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001652# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001653# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001654function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001655 local service
1656 local failures
1657 SCREEN_NAME=${SCREEN_NAME:-stack}
1658 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1659
1660
1661 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1662 echo "No service status directory found"
1663 return
1664 fi
1665
Wei Jiangange3340f12015-09-21 17:52:14 +08001666 # Check if there is any failure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001667 # make this -o errexit safe
1668 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001669
1670 for service in $failures; do
1671 service=`basename $service`
1672 service=${service%.failure}
1673 echo "Error: Service $service is not running"
1674 done
1675
1676 if [ -n "$failures" ]; then
Dean Troyer9ba49cd2016-03-10 15:45:40 -06001677 die $LINENO "More details about the above errors can be found with screen"
Dean Troyerdff49a22014-01-30 15:37:40 -06001678 fi
1679}
1680
Chris Dent2f27a0e2014-09-09 13:46:02 +01001681# Tail a log file in a screen if USE_SCREEN is true.
Tony Breeds93292902015-11-09 15:45:04 +11001682# Uses globals ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001683function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001684 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001685 local logfile=$2
1686
Chris Dent2f27a0e2014-09-09 13:46:02 +01001687 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001688 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001689 fi
1690}
1691
Dean Troyerdff49a22014-01-30 15:37:40 -06001692
Dean Troyer3159a822014-08-27 14:13:58 -05001693# Deprecated Functions
1694# --------------------
1695
1696# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1697# fork. It includes the dirty work of closing extra filehandles and preparing log
1698# files to produce the same logs as screen_it(). The log filename is derived
1699# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1700# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1701# _old_run_process service "command-line"
1702function _old_run_process {
1703 local service=$1
1704 local command="$2"
1705
1706 # Undo logging redirections and close the extra descriptors
1707 exec 1>&3
1708 exec 2>&3
1709 exec 3>&-
1710 exec 6>&-
1711
1712 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001713 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1714 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001715
1716 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1717 export PYTHONUNBUFFERED=1
1718 fi
1719
1720 exec /bin/bash -c "$command"
1721 die "$service exec failure: $command"
1722}
1723
1724# old_run_process() launches a child process that closes all file descriptors and
1725# then exec's the passed in command. This is meant to duplicate the semantics
1726# of screen_it() without screen. PIDs are written to
1727# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1728# old_run_process service "command-line"
1729function old_run_process {
1730 local service=$1
1731 local command="$2"
1732
1733 # Spawn the child process
1734 _old_run_process "$service" "$command" &
1735 echo $!
1736}
1737
1738# Compatibility for existing start_XXXX() functions
1739# Uses global ``USE_SCREEN``
1740# screen_it service "command-line"
1741function screen_it {
1742 if is_service_enabled $1; then
1743 # Append the service to the screen rc file
1744 screen_rc "$1" "$2"
1745
1746 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001747 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001748 else
1749 # Spawn directly without screen
1750 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1751 fi
1752 fi
1753}
1754
1755# Compatibility for existing stop_XXXX() functions
1756# Stop a service in screen
1757# If a PID is available use it, kill the whole process group via TERM
1758# If screen is being used kill the screen window; this will catch processes
1759# that did not leave a PID behind
1760# screen_stop service
1761function screen_stop {
1762 # Clean up the screen window
1763 stop_process $1
1764}
1765
1766
Sean Dague2c65e712014-12-18 09:44:56 -05001767# Plugin Functions
1768# =================
1769
1770DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1771
1772# enable_plugin <name> <url> [branch]
1773#
1774# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1775# ``url`` is a git url
1776# ``branch`` is a gitref. If it's not set, defaults to master
1777function enable_plugin {
1778 local name=$1
1779 local url=$2
1780 local branch=${3:-master}
1781 DEVSTACK_PLUGINS+=",$name"
1782 GITREPO[$name]=$url
1783 GITDIR[$name]=$DEST/$name
1784 GITBRANCH[$name]=$branch
1785}
1786
1787# fetch_plugins
1788#
1789# clones all plugins
1790function fetch_plugins {
1791 local plugins="${DEVSTACK_PLUGINS}"
1792 local plugin
1793
1794 # short circuit if nothing to do
1795 if [[ -z $plugins ]]; then
1796 return
1797 fi
1798
Dean Troyerdc97cb72015-03-28 08:20:50 -05001799 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001800 for plugin in ${plugins//,/ }; do
1801 git_clone_by_name $plugin
1802 done
1803}
1804
1805# load_plugin_settings
1806#
1807# Load settings from plugins in the order that they were registered
1808function load_plugin_settings {
1809 local plugins="${DEVSTACK_PLUGINS}"
1810 local plugin
1811
1812 # short circuit if nothing to do
1813 if [[ -z $plugins ]]; then
1814 return
1815 fi
1816
1817 echo "Loading plugin settings"
1818 for plugin in ${plugins//,/ }; do
1819 local dir=${GITDIR[$plugin]}
1820 # source any known settings
1821 if [[ -f $dir/devstack/settings ]]; then
1822 source $dir/devstack/settings
1823 fi
1824 done
1825}
1826
Sean Dague6e275e12015-03-26 05:54:28 -04001827# plugin_override_defaults
1828#
1829# Run an extremely early setting phase for plugins that allows default
1830# overriding of services.
1831function plugin_override_defaults {
1832 local plugins="${DEVSTACK_PLUGINS}"
1833 local plugin
1834
1835 # short circuit if nothing to do
1836 if [[ -z $plugins ]]; then
1837 return
1838 fi
1839
1840 echo "Overriding Configuration Defaults"
1841 for plugin in ${plugins//,/ }; do
1842 local dir=${GITDIR[$plugin]}
1843 # source any overrides
1844 if [[ -f $dir/devstack/override-defaults ]]; then
1845 # be really verbose that an override is happening, as it
1846 # may not be obvious if things fail later.
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001847 echo "$plugin has overridden the following defaults"
Sean Dague6e275e12015-03-26 05:54:28 -04001848 cat $dir/devstack/override-defaults
1849 source $dir/devstack/override-defaults
1850 fi
1851 done
1852}
1853
Sean Dague2c65e712014-12-18 09:44:56 -05001854# run_plugins
1855#
1856# Run the devstack/plugin.sh in all the plugin directories. These are
1857# run in registration order.
1858function run_plugins {
1859 local mode=$1
1860 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301861
1862 local plugins="${DEVSTACK_PLUGINS}"
1863 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001864 for plugin in ${plugins//,/ }; do
1865 local dir=${GITDIR[$plugin]}
1866 if [[ -f $dir/devstack/plugin.sh ]]; then
1867 source $dir/devstack/plugin.sh $mode $phase
1868 fi
1869 done
1870}
1871
1872function run_phase {
1873 local mode=$1
1874 local phase=$2
1875 if [[ -d $TOP_DIR/extras.d ]]; then
Dmitry Tantsur64be3212015-10-12 13:10:24 +02001876 local extra_plugin_file_name
1877 for extra_plugin_file_name in $TOP_DIR/extras.d/*.sh; do
Sean Dague41d01102015-12-03 08:12:23 -05001878 # NOTE(sdague): only process extras.d for the 3 explicitly
1879 # white listed elements in tree. We want these to move out
1880 # over time as well, but they are in tree, so we need to
1881 # manage that.
Jim Rollenhagen47367072015-12-10 14:24:00 +00001882 local exceptions="60-ceph.sh 80-tempest.sh"
Ian Wienand5cdee8d2015-10-19 14:17:18 +11001883 local extra
1884 extra=$(basename $extra_plugin_file_name)
Sean Dague1de9e332015-10-07 08:46:13 -04001885 if [[ ! ( $exceptions =~ "$extra" ) ]]; then
Sean Dague41d01102015-12-03 08:12:23 -05001886 warn "use of extras.d is no longer supported"
1887 warn "processing of project $extra is skipped"
1888 else
1889 [[ -r $extra_plugin_file_name ]] && source $extra_plugin_file_name $mode $phase
Sean Dague1de9e332015-10-07 08:46:13 -04001890 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001891 done
1892 fi
1893 # the source phase corresponds to settings loading in plugins
1894 if [[ "$mode" == "source" ]]; then
1895 load_plugin_settings
Chris Dentc6d47012015-10-09 14:57:05 +00001896 verify_disabled_services
Sean Dague6e275e12015-03-26 05:54:28 -04001897 elif [[ "$mode" == "override_defaults" ]]; then
1898 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001899 else
1900 run_plugins $mode $phase
1901 fi
1902}
1903
Dean Troyerdff49a22014-01-30 15:37:40 -06001904
1905# Service Functions
1906# =================
1907
1908# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1909# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001910function _cleanup_service_list {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001911 local xtrace
1912 xtrace=$(set +o | grep xtrace)
1913 set +o xtrace
1914
Dean Troyerdff49a22014-01-30 15:37:40 -06001915 echo "$1" | sed -e '
1916 s/,,/,/g;
1917 s/^,//;
1918 s/,$//
1919 '
Ian Wienand8043bfa2015-10-14 14:53:18 +11001920
1921 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001922}
1923
1924# disable_all_services() removes all current services
1925# from ``ENABLED_SERVICES`` to reset the configuration
1926# before a minimal installation
1927# Uses global ``ENABLED_SERVICES``
1928# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001929function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001930 ENABLED_SERVICES=""
1931}
1932
1933# Remove all services starting with '-'. For example, to install all default
1934# services except rabbit (rabbit) set in ``localrc``:
1935# ENABLED_SERVICES+=",-rabbit"
1936# Uses global ``ENABLED_SERVICES``
1937# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001938function disable_negated_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001939 local xtrace
1940 xtrace=$(set +o | grep xtrace)
1941 set +o xtrace
1942
Ian Wienand2796a822015-04-15 08:59:04 +10001943 local to_remove=""
1944 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001945 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001946
1947 # build up list of services that should be removed; i.e. they
1948 # begin with "-"
1949 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001950 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001951 to_remove+=",${service#-}"
1952 else
1953 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001954 fi
1955 done
Ian Wienand2796a822015-04-15 08:59:04 +10001956
1957 # go through the service list. if this service appears in the "to
1958 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001959 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001960
1961 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001962}
1963
Chris Dentc6d47012015-10-09 14:57:05 +00001964# disable_service() prepares the services passed as argument to be
1965# removed from the ``ENABLED_SERVICES`` list, if they are present.
Dean Troyerdff49a22014-01-30 15:37:40 -06001966#
1967# For example:
1968# disable_service rabbit
1969#
Chris Dentc6d47012015-10-09 14:57:05 +00001970# Uses global ``DISABLED_SERVICES``
Dean Troyerdff49a22014-01-30 15:37:40 -06001971# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001972function disable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001973 local xtrace
1974 xtrace=$(set +o | grep xtrace)
1975 set +o xtrace
1976
Chris Dentc6d47012015-10-09 14:57:05 +00001977 local disabled_svcs="${DISABLED_SERVICES}"
1978 local enabled_svcs=",${ENABLED_SERVICES},"
Dean Troyerdff49a22014-01-30 15:37:40 -06001979 local service
1980 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001981 disabled_svcs+=",$service"
Dean Troyerdff49a22014-01-30 15:37:40 -06001982 if is_service_enabled $service; then
Chris Dentc6d47012015-10-09 14:57:05 +00001983 enabled_svcs=${enabled_svcs//,$service,/,}
Dean Troyerdff49a22014-01-30 15:37:40 -06001984 fi
1985 done
Chris Dentc6d47012015-10-09 14:57:05 +00001986 DISABLED_SERVICES=$(_cleanup_service_list "$disabled_svcs")
1987 ENABLED_SERVICES=$(_cleanup_service_list "$enabled_svcs")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001988
1989 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001990}
1991
1992# enable_service() adds the services passed as argument to the
1993# ``ENABLED_SERVICES`` list, if they are not already present.
1994#
1995# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001996# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001997#
1998# This function does not know about the special cases
1999# for nova, glance, and neutron built into is_service_enabled().
2000# Uses global ``ENABLED_SERVICES``
2001# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11002002function enable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11002003 local xtrace
2004 xtrace=$(set +o | grep xtrace)
2005 set +o xtrace
2006
Dean Troyerdff49a22014-01-30 15:37:40 -06002007 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002008 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06002009 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00002010 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
2011 warn $LINENO "Attempt to enable_service ${service} when it has been disabled"
2012 continue
2013 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06002014 if ! is_service_enabled $service; then
2015 tmpsvcs+=",$service"
2016 fi
2017 done
2018 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
2019 disable_negated_services
Ian Wienand8043bfa2015-10-14 14:53:18 +11002020
2021 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002022}
2023
2024# is_service_enabled() checks if the service(s) specified as arguments are
2025# enabled by the user in ``ENABLED_SERVICES``.
2026#
2027# Multiple services specified as arguments are ``OR``'ed together; the test
2028# is a short-circuit boolean, i.e it returns on the first match.
2029#
2030# There are special cases for some 'catch-all' services::
2031# **nova** returns true if any service enabled start with **n-**
2032# **cinder** returns true if any service enabled start with **c-**
Dean Troyerdff49a22014-01-30 15:37:40 -06002033# **glance** returns true if any service enabled start with **g-**
2034# **neutron** returns true if any service enabled start with **q-**
2035# **swift** returns true if any service enabled start with **s-**
2036# **trove** returns true if any service enabled start with **tr-**
2037# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
2038# **s-** services will be enabled. This will be deprecated in the future.
2039#
2040# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
2041# We also need to make sure to treat **n-cell-region** and **n-cell-child**
2042# as enabled in this case.
2043#
2044# Uses global ``ENABLED_SERVICES``
2045# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11002046function is_service_enabled {
Ian Wienand433a9b12015-10-07 13:29:31 +11002047 local xtrace
2048 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05002049 set +o xtrace
Ian Wienand8043bfa2015-10-14 14:53:18 +11002050
Sean Dague45917cc2014-02-24 16:09:14 -05002051 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002052 local services=$@
2053 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06002054 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05002055 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002056
2057 # Look for top-level 'enabled' function for this service
2058 if type is_${service}_enabled >/dev/null 2>&1; then
2059 # A function exists for this service, use it
Christian Schwede3db0aad2015-09-10 11:15:39 +00002060 is_${service}_enabled && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002061 fi
2062
2063 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
2064 # are implemented
2065
Sean Dague45917cc2014-02-24 16:09:14 -05002066 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01002067 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05002068 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05002069 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05002070 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
2071 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
2072 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
2073 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002074 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11002075
Sean Dague45917cc2014-02-24 16:09:14 -05002076 $xtrace
2077 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06002078}
2079
fumihiko kakuma8606c982015-04-13 09:55:06 +09002080# remove specified list from the input string
2081# remove_disabled_services service-list remove-list
2082function remove_disabled_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11002083 local xtrace
2084 xtrace=$(set +o | grep xtrace)
2085 set +o xtrace
2086
fumihiko kakuma8606c982015-04-13 09:55:06 +09002087 local service_list=$1
2088 local remove_list=$2
2089 local service
2090 local enabled=""
2091
2092 for service in ${service_list//,/ }; do
2093 local remove
2094 local add=1
2095 for remove in ${remove_list//,/ }; do
2096 if [[ ${remove} == ${service} ]]; then
2097 add=0
2098 break
2099 fi
2100 done
2101 if [[ $add == 1 ]]; then
2102 enabled="${enabled},$service"
2103 fi
2104 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11002105
2106 $xtrace
2107
fumihiko kakuma8606c982015-04-13 09:55:06 +09002108 _cleanup_service_list "$enabled"
2109}
2110
Dean Troyerdff49a22014-01-30 15:37:40 -06002111# Toggle enable/disable_service for services that must run exclusive of each other
2112# $1 The name of a variable containing a space-separated list of services
2113# $2 The name of a variable in which to store the enabled service's name
2114# $3 The name of the service to enable
2115function use_exclusive_service {
2116 local options=${!1}
2117 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002118 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06002119 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002120 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06002121 for opt in $options;do
2122 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
2123 done
2124 eval "$out=$selection"
2125 return 0
2126}
2127
Chris Dentc6d47012015-10-09 14:57:05 +00002128# Make sure that nothing has manipulated ENABLED_SERVICES in a way
2129# that conflicts with prior calls to disable_service.
2130# Uses global ``ENABLED_SERVICES``
2131function verify_disabled_services {
2132 local service
2133 for service in ${ENABLED_SERVICES//,/ }; do
2134 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
2135 die $LINENO "ENABLED_SERVICES directly modified to overcome 'disable_service ${service}'"
2136 fi
2137 done
2138}
2139
Dean Troyerdff49a22014-01-30 15:37:40 -06002140
Masayuki Igawaf6368d32014-02-20 13:31:26 +09002141# System Functions
2142# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06002143
2144# Only run the command if the target file (the last arg) is not on an
2145# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002146function _safe_permission_operation {
Ian Wienand433a9b12015-10-07 13:29:31 +11002147 local xtrace
2148 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05002149 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002150 local args=( $@ )
2151 local last
2152 local sudo_cmd
2153 local dir_to_check
2154
2155 let last="${#args[*]} - 1"
2156
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002157 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002158 if [ ! -d "$dir_to_check" ]; then
2159 dir_to_check=`dirname "$dir_to_check"`
2160 fi
2161
2162 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002163 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002164 return 0
2165 fi
2166
2167 if [[ $TRACK_DEPENDS = True ]]; then
2168 sudo_cmd="env"
2169 else
2170 sudo_cmd="sudo"
2171 fi
2172
Sean Dague45917cc2014-02-24 16:09:14 -05002173 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002174 $sudo_cmd $@
2175}
2176
2177# Exit 0 if address is in network or 1 if address is not in network
2178# ip-range is in CIDR notation: 1.2.3.4/20
2179# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002180function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002181 local ip=$1
2182 local range=$2
2183 local masklen=${range#*/}
Ian Wienandada886d2015-10-07 14:06:26 +11002184 local network
2185 network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2186 local subnet
2187 subnet=$(maskip $ip $(cidr2netmask $masklen))
Dean Troyerdff49a22014-01-30 15:37:40 -06002188 [[ $network == $subnet ]]
2189}
2190
2191# Add a user to a group.
2192# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002193function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002194 local user=$1
2195 local group=$2
2196
Thomas Bechtolda8580852015-05-31 00:04:33 +02002197 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06002198}
2199
2200# Convert CIDR notation to a IPv4 netmask
2201# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002202function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002203 local maskpat="255 255 255 255"
2204 local maskdgt="254 252 248 240 224 192 128"
2205 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2206 echo ${1-0}.${2-0}.${3-0}.${4-0}
2207}
2208
2209# Gracefully cp only if source file/dir exists
2210# cp_it source destination
2211function cp_it {
2212 if [ -e $1 ] || [ -d $1 ]; then
2213 cp -pRL $1 $2
2214 fi
2215}
2216
2217# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2218# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2219# ``localrc`` or on the command line if necessary::
2220#
2221# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2222#
2223# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2224
Ian Wienandaee18c72014-02-21 15:35:08 +11002225function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05002226 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002227 export http_proxy=$http_proxy
2228 fi
Sean Dague53753292014-12-04 19:38:15 -05002229 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002230 export https_proxy=$https_proxy
2231 fi
Sean Dague53753292014-12-04 19:38:15 -05002232 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002233 export no_proxy=$no_proxy
2234 fi
2235}
2236
2237# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002238function is_nfs_directory {
Ian Wienandada886d2015-10-07 14:06:26 +11002239 local mount_type
2240 mount_type=`stat -f -L -c %T $1`
Dean Troyerdff49a22014-01-30 15:37:40 -06002241 test "$mount_type" == "nfs"
2242}
2243
2244# Return the network portion of the given IP address using netmask
2245# netmask is in the traditional dotted-quad format
2246# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002247function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002248 local ip=$1
2249 local mask=$2
2250 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
Ian Wienandada886d2015-10-07 14:06:26 +11002251 local subnet
2252 subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
Dean Troyerdff49a22014-01-30 15:37:40 -06002253 echo $subnet
2254}
2255
Chris Dent3a2c86a2015-05-12 13:41:25 +00002256# Return the current python as "python<major>.<minor>"
2257function python_version {
Ian Wienandada886d2015-10-07 14:06:26 +11002258 local python_version
2259 python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
Chris Dent3a2c86a2015-05-12 13:41:25 +00002260 echo "python${python_version}"
2261}
2262
Dean Troyerdff49a22014-01-30 15:37:40 -06002263# Service wrapper to restart services
2264# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002265function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002266 if is_ubuntu; then
2267 sudo /usr/sbin/service $1 restart
2268 else
2269 sudo /sbin/service $1 restart
2270 fi
2271}
2272
2273# Only change permissions of a file or directory if it is not on an
2274# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002275function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002276 _safe_permission_operation chmod $@
2277}
2278
2279# Only change ownership of a file or directory if it is not on an NFS
2280# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002281function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002282 _safe_permission_operation chown $@
2283}
2284
2285# Service wrapper to start services
2286# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002287function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002288 if is_ubuntu; then
2289 sudo /usr/sbin/service $1 start
2290 else
2291 sudo /sbin/service $1 start
2292 fi
2293}
2294
2295# Service wrapper to stop services
2296# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002297function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002298 if is_ubuntu; then
2299 sudo /usr/sbin/service $1 stop
2300 else
2301 sudo /sbin/service $1 stop
2302 fi
2303}
2304
Sean Dague442e4e92015-06-24 13:24:02 -04002305# Test with a finite retry loop.
2306#
2307function test_with_retry {
2308 local testcmd=$1
2309 local failmsg=$2
2310 local until=${3:-10}
2311 local sleep=${4:-0.5}
2312
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002313 time_start "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002314 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2315 die $LINENO "$failmsg"
2316 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002317 time_stop "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002318}
2319
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00002320# Like sudo but forwarding http_proxy https_proxy no_proxy environment vars.
2321# If it is run as superuser then sudo is replaced by env.
2322#
2323function sudo_with_proxies {
2324 local sudo
2325
2326 [[ "$(id -u)" = "0" ]] && sudo="env" || sudo="sudo"
2327
2328 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}"\
2329 no_proxy="${no_proxy:-}" "$@"
2330}
2331
Sean Dague95c33d52015-10-07 11:05:59 -04002332# Timing infrastructure - figure out where large blocks of time are
2333# used in DevStack
2334#
2335# The timing infrastructure for DevStack is about collecting buckets
2336# of time that are spend in some subtask. For instance, that might be
2337# 'apt', 'pip', 'osc', even database migrations. We do this by a pair
2338# of functions: time_start / time_stop.
2339#
2340# These take a single parameter: $name - which specifies the name of
2341# the bucket to be accounted against. time_totals function spits out
2342# the results.
2343#
2344# Resolution is only in whole seconds, so should be used for long
2345# running activities.
2346
Ian Wienand908a3a92016-03-29 14:47:09 +11002347declare -A _TIME_TOTAL
2348declare -A _TIME_START
2349declare -r _TIME_BEGIN=$(date +%s)
Sean Dague95c33d52015-10-07 11:05:59 -04002350
2351# time_start $name
2352#
2353# starts the clock for a timer by name. Errors if that clock is
2354# already started.
2355function time_start {
2356 local name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002357 local start_time=${_TIME_START[$name]}
Sean Dague95c33d52015-10-07 11:05:59 -04002358 if [[ -n "$start_time" ]]; then
2359 die $LINENO "Trying to start the clock on $name, but it's already been started"
2360 fi
Ian Wienand908a3a92016-03-29 14:47:09 +11002361 _TIME_START[$name]=$(date +%s)
Sean Dague95c33d52015-10-07 11:05:59 -04002362}
2363
2364# time_stop $name
2365#
2366# stops the clock for a timer by name, and accumulate that time in the
2367# global counter for that name. Errors if that clock had not
2368# previously been started.
2369function time_stop {
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002370 local name
2371 local end_time
2372 local elpased_time
2373 local total
2374 local start_time
2375
2376 name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002377 start_time=${_TIME_START[$name]}
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002378
Sean Dague95c33d52015-10-07 11:05:59 -04002379 if [[ -z "$start_time" ]]; then
2380 die $LINENO "Trying to stop the clock on $name, but it was never started"
2381 fi
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002382 end_time=$(date +%s)
2383 elapsed_time=$(($end_time - $start_time))
Ian Wienand908a3a92016-03-29 14:47:09 +11002384 total=${_TIME_TOTAL[$name]:-0}
Sean Dague95c33d52015-10-07 11:05:59 -04002385 # reset the clock so we can start it in the future
Ian Wienand908a3a92016-03-29 14:47:09 +11002386 _TIME_START[$name]=""
2387 _TIME_TOTAL[$name]=$(($total + $elapsed_time))
Sean Dague95c33d52015-10-07 11:05:59 -04002388}
2389
2390# time_totals
Ian Wienand908a3a92016-03-29 14:47:09 +11002391# Print out total time summary
Sean Dague95c33d52015-10-07 11:05:59 -04002392function time_totals {
Ian Wienand908a3a92016-03-29 14:47:09 +11002393 local elapsed_time
2394 local end_time
2395 local len=15
2396 local xtrace
2397
2398 end_time=$(date +%s)
2399 elapsed_time=$(($end_time - $_TIME_BEGIN))
2400
2401 # pad 1st column this far
2402 for t in ${!_TIME_TOTAL[*]}; do
2403 if [[ ${#t} -gt $len ]]; then
2404 len=${#t}
2405 fi
Sean Dague95c33d52015-10-07 11:05:59 -04002406 done
Ian Wienand908a3a92016-03-29 14:47:09 +11002407
2408 xtrace=$(set +o | grep xtrace)
2409 set +o xtrace
2410
2411 echo
2412 echo "========================="
2413 echo "DevStack Component Timing"
2414 echo "========================="
2415 printf "%-${len}s %3d\n" "Total runtime" "$elapsed_time"
2416 echo
2417 for t in ${!_TIME_TOTAL[*]}; do
2418 local v=${_TIME_TOTAL[$t]}
2419 printf "%-${len}s %3d\n" "$t" "$v"
2420 done
2421 echo "========================="
2422
2423 $xtrace
Sean Dague95c33d52015-10-07 11:05:59 -04002424}
Dean Troyerdff49a22014-01-30 15:37:40 -06002425
2426# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +11002427$_XTRACE_FUNCTIONS_COMMON
Dean Troyerdff49a22014-01-30 15:37:40 -06002428
2429# Local variables:
2430# mode: shell-script
2431# End: