blob: 1e6ae5cc76866e950006ee5eee54e96db7005338 [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
Sean Dagueafef8bf2017-03-06 14:07:23 -050040declare -r -g _DEVSTACK_FUNCTIONS_COMMON=1
Ian Wienand4ffb4542015-06-30 11:00:32 +100041
Sean Daguecc524062014-10-01 09:06:43 -040042# Global Config Variables
Sean Dagueafef8bf2017-03-06 14:07:23 -050043declare -A -g GITREPO
44declare -A -g GITBRANCH
45declare -A -g GITDIR
Sean Daguecc524062014-10-01 09:06:43 -040046
Sean Dague53753292014-12-04 19:38:15 -050047TRACK_DEPENDS=${TRACK_DEPENDS:-False}
Dinesh Bhoref60f2b2017-09-05 14:40:32 +053048KILL_PATH="$(which kill)"
Sean Dague53753292014-12-04 19:38:15 -050049
Dean Troyer68162342015-05-13 15:41:03 -050050# Save these variables to .stackenv
51STACK_ENV_VARS="BASE_SQL_CONN DATA_DIR DEST ENABLED_SERVICES HOST_IP \
Sean Dague0effa1a2017-04-18 15:16:37 -040052 KEYSTONE_AUTH_URI KEYSTONE_SERVICE_URI \
53 LOGFILE OS_CACERT SERVICE_HOST STACK_USER TLS_IP \
Brian Haley5d04db22015-06-16 13:14:31 -040054 HOST_IPV6 SERVICE_IP_VERSION"
Dean Troyer68162342015-05-13 15:41:03 -050055
56
57# Saves significant environment variables to .stackenv for later use
58# Refers to a lot of globals, only TOP_DIR and STACK_ENV_VARS are required to
59# function, the rest are simply saved and do not cause problems if they are undefined.
60# save_stackenv [tag]
61function save_stackenv {
62 local tag=${1:-""}
63 # Save some values we generated for later use
64 time_stamp=$(date "+$TIMESTAMP_FORMAT")
65 echo "# $time_stamp $tag" >$TOP_DIR/.stackenv
66 for i in $STACK_ENV_VARS; do
67 echo $i=${!i} >>$TOP_DIR/.stackenv
68 done
69}
Dean Troyerdff49a22014-01-30 15:37:40 -060070
Monty Taylor7224eec2015-09-19 11:26:18 -040071# Update/create user clouds.yaml file.
72# clouds.yaml will have
73# - A `devstack` entry for the `demo` user for the `demo` project.
74# - A `devstack-admin` entry for the `admin` user for the `admin` project.
75# write_clouds_yaml
76function write_clouds_yaml {
Monty Tayloree9bb762015-10-19 15:16:18 -040077 # The location is a variable to allow for easier refactoring later to make it
78 # overridable. There is currently no usecase where doing so makes sense, so
79 # it's not currently configurable.
Monty Taylor7224eec2015-09-19 11:26:18 -040080
Monty Tayloree9bb762015-10-19 15:16:18 -040081 CLOUDS_YAML=/etc/openstack/clouds.yaml
82
83 sudo mkdir -p $(dirname $CLOUDS_YAML)
Lenny Verkhovsky313ddae2015-10-20 11:26:34 +030084 sudo chown -R $STACK_USER /etc/openstack
Lenny Verkhovsky313ddae2015-10-20 11:26:34 +030085
Monty Tayloree9bb762015-10-19 15:16:18 -040086 CA_CERT_ARG=''
87 if [ -f "$SSL_BUNDLE_FILE" ]; then
88 CA_CERT_ARG="--os-cacert $SSL_BUNDLE_FILE"
89 fi
Sean Daguec67d22e2016-02-02 05:51:14 -050090 # demo -> devstack
Davanum Srinivas51ecf0a2017-01-05 16:11:17 -050091 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
Monty Tayloree9bb762015-10-19 15:16:18 -040092 --file $CLOUDS_YAML \
93 --os-cloud devstack \
94 --os-region-name $REGION_NAME \
95 --os-identity-api-version 3 \
96 $CA_CERT_ARG \
Monty Taylore43f60b2017-04-27 18:27:36 -050097 --os-auth-url $KEYSTONE_SERVICE_URI \
Monty Tayloree9bb762015-10-19 15:16:18 -040098 --os-username demo \
99 --os-password $ADMIN_PASSWORD \
100 --os-project-name demo
Sean Daguec67d22e2016-02-02 05:51:14 -0500101
102 # alt_demo -> devstack-alt
Davanum Srinivas51ecf0a2017-01-05 16:11:17 -0500103 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
Sean Daguec67d22e2016-02-02 05:51:14 -0500104 --file $CLOUDS_YAML \
Clark Boylane20cb432016-02-05 12:00:18 -0800105 --os-cloud devstack-alt \
Sean Daguec67d22e2016-02-02 05:51:14 -0500106 --os-region-name $REGION_NAME \
107 --os-identity-api-version 3 \
108 $CA_CERT_ARG \
Monty Taylore43f60b2017-04-27 18:27:36 -0500109 --os-auth-url $KEYSTONE_SERVICE_URI \
Sean Daguec67d22e2016-02-02 05:51:14 -0500110 --os-username alt_demo \
111 --os-password $ADMIN_PASSWORD \
112 --os-project-name alt_demo
113
114 # admin -> devstack-admin
Davanum Srinivas51ecf0a2017-01-05 16:11:17 -0500115 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
Monty Tayloree9bb762015-10-19 15:16:18 -0400116 --file $CLOUDS_YAML \
117 --os-cloud devstack-admin \
118 --os-region-name $REGION_NAME \
119 --os-identity-api-version 3 \
120 $CA_CERT_ARG \
Monty Taylore43f60b2017-04-27 18:27:36 -0500121 --os-auth-url $KEYSTONE_SERVICE_URI \
Monty Tayloree9bb762015-10-19 15:16:18 -0400122 --os-username admin \
123 --os-password $ADMIN_PASSWORD \
124 --os-project-name admin
Monty Taylor74379df2016-01-21 10:20:08 -0500125
126 # CLean up any old clouds.yaml files we had laying around
ZhiQiang Fan0b4a0092016-04-12 20:26:33 +0800127 rm -f $(eval echo ~"$STACK_USER")/.config/openstack/clouds.yaml
Monty Taylor7224eec2015-09-19 11:26:18 -0400128}
129
Ian Wienande82bac02015-08-25 14:29:08 +1000130# trueorfalse <True|False> <VAR>
131#
132# Normalize config-value provided in variable VAR to either "True" or
133# "False". If VAR is unset (i.e. $VAR evaluates as empty), the value
134# of the second argument will be used as the default value.
135#
136# Accepts as False: 0 no No NO false False FALSE
137# Accepts as True: 1 yes Yes YES true True TRUE
138#
139# usage:
140# VAL=$(trueorfalse False VAL)
Ian Wienandaee18c72014-02-21 15:35:08 +1100141function trueorfalse {
Ian Wienand433a9b12015-10-07 13:29:31 +1100142 local xtrace
143 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -0500144 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600145
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900146 local default=$1
Ian Wienande82bac02015-08-25 14:29:08 +1000147
148 if [ -z $2 ]; then
149 die $LINENO "variable to normalize required"
150 fi
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900151 local testval=${!2:-}
152
153 case "$testval" in
154 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
155 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
156 * ) echo "$default" ;;
157 esac
158
Sean Dague45917cc2014-02-24 16:09:14 -0500159 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600160}
161
Attila Fazekas1bd79592015-02-24 14:06:56 +0100162function isset {
163 [[ -v "$1" ]]
164}
Dean Troyerdff49a22014-01-30 15:37:40 -0600165
Dean Troyer68162342015-05-13 15:41:03 -0500166
Dean Troyerdff49a22014-01-30 15:37:40 -0600167# Control Functions
168# =================
169
170# Prints backtrace info
171# filename:lineno:function
172# backtrace level
173function backtrace {
174 local level=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100175 local deep
176 deep=$((${#BASH_SOURCE[@]} - 1))
Dean Troyerdff49a22014-01-30 15:37:40 -0600177 echo "[Call Trace]"
178 while [ $level -le $deep ]; do
179 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
180 deep=$((deep - 1))
181 done
182}
183
184# Prints line number and "message" then exits
185# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100186function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600187 local exitcode=$?
188 set +o xtrace
189 local line=$1; shift
190 if [ $exitcode == 0 ]; then
191 exitcode=1
192 fi
193 backtrace 2
194 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600195 # Give buffers a second to flush
196 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600197 exit $exitcode
198}
199
200# Checks an environment variable is not set or has length 0 OR if the
201# exit code is non-zero and prints "message" and exits
202# NOTE: env-var is the variable name without a '$'
203# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100204function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600205 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100206 local xtrace
207 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600208 set +o xtrace
209 local line=$1; shift
210 local evar=$1; shift
211 if ! is_set $evar || [ $exitcode != 0 ]; then
212 die $line "$*"
213 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500214 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600215}
216
Sean Dague1de9e332015-10-07 08:46:13 -0400217function deprecated {
218 local text=$1
219 DEPRECATED_TEXT+="\n$text"
YAMAMOTO Takashi8b1bbd62016-12-01 22:29:12 +0900220 echo "WARNING: $text" >&2
Sean Dague1de9e332015-10-07 08:46:13 -0400221}
222
Dean Troyerdff49a22014-01-30 15:37:40 -0600223# Prints line number and "message" in error format
224# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100225function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600226 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100227 local xtrace
228 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600229 set +o xtrace
230 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
231 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600232 if [[ -n ${LOGDIR} ]]; then
233 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600234 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500235 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600236 return $exitcode
237}
238
239# Checks an environment variable is not set or has length 0 OR if the
240# exit code is non-zero and prints "message"
241# NOTE: env-var is the variable name without a '$'
242# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100243function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600244 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100245 local xtrace
246 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600247 set +o xtrace
248 local line=$1; shift
249 local evar=$1; shift
250 if ! is_set $evar || [ $exitcode != 0 ]; then
251 err $line "$*"
252 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500253 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600254 return $exitcode
255}
256
257# Exit after outputting a message about the distribution not being supported.
258# exit_distro_not_supported [optional-string-telling-what-is-missing]
259function exit_distro_not_supported {
260 if [[ -z "$DISTRO" ]]; then
261 GetDistro
262 fi
263
264 if [ $# -gt 0 ]; then
265 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
266 else
267 die $LINENO "Support for $DISTRO is incomplete."
268 fi
269}
270
271# Test if the named environment variable is set and not zero length
272# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100273function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600274 local var=\$"$1"
275 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
276}
277
278# Prints line number and "message" in warning format
279# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100280function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600281 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100282 local xtrace
283 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600284 set +o xtrace
285 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Sean Daguee4af9292015-04-28 08:57:57 -0400286 echo $msg
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500287 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600288 return $exitcode
289}
290
291
292# Distro Functions
293# ================
294
295# Determine OS Vendor, Release and Update
Ian Wienand7710e7f2014-08-27 16:15:32 +1000296
297#
298# NOTE : For portability, you almost certainly do not want to use
299# these variables directly! The "is_*" functions defined below this
300# bundle up compatible platforms under larger umbrellas that we have
301# determinted are compatible enough (e.g. is_ubuntu covers Ubuntu &
302# Debian, is_fedora covers RPM-based distros). Higher-level functions
303# such as "install_package" further abstract things in better ways.
304#
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500305# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
Matt Riedemannff10ac32017-02-13 12:44:24 -0500306# ``os_RELEASE`` - major release: ``16.04`` (Ubuntu), ``23`` (Fedora)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500307# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
Matt Riedemannff10ac32017-02-13 12:44:24 -0500308# ``os_CODENAME`` - vendor's codename for release: ``xenial``
Ian Wienand7710e7f2014-08-27 16:15:32 +1000309
Sean Dagueafef8bf2017-03-06 14:07:23 -0500310declare -g os_VENDOR os_RELEASE os_PACKAGE os_CODENAME
Ian Wienand7710e7f2014-08-27 16:15:32 +1000311
312# Make a *best effort* attempt to install lsb_release packages for the
313# user if not available. Note can't use generic install_package*
314# because they depend on this!
315function _ensure_lsb_release {
Thomas Bechtolddab39012016-03-03 12:11:15 +0100316 if [[ -x $(command -v lsb_release 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000317 return
318 fi
319
Thomas Bechtolddab39012016-03-03 12:11:15 +0100320 if [[ -x $(command -v apt-get 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000321 sudo apt-get install -y lsb-release
Thomas Bechtolddab39012016-03-03 12:11:15 +0100322 elif [[ -x $(command -v zypper 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000323 # XXX: old code paths seem to have assumed SUSE platforms also
324 # had "yum". Keep this ordered above yum so we don't try to
325 # install the rh package. suse calls it just "lsb"
Thomas Bechtold899dfee2016-03-03 11:27:53 +0100326 sudo zypper -n install lsb
Thomas Bechtolddab39012016-03-03 12:11:15 +0100327 elif [[ -x $(command -v dnf 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000328 sudo dnf install -y redhat-lsb-core
Thomas Bechtolddab39012016-03-03 12:11:15 +0100329 elif [[ -x $(command -v yum 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000330 # all rh patforms (fedora, centos, rhel) have this pkg
331 sudo yum install -y redhat-lsb-core
332 else
333 die $LINENO "Unable to find or auto-install lsb_release"
334 fi
335}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500336
Dean Troyerdff49a22014-01-30 15:37:40 -0600337# GetOSVersion
Ian Wienand7710e7f2014-08-27 16:15:32 +1000338# Set the following variables:
339# - os_RELEASE
340# - os_CODENAME
341# - os_VENDOR
342# - os_PACKAGE
Ian Wienandaee18c72014-02-21 15:35:08 +1100343function GetOSVersion {
Ian Wienand7710e7f2014-08-27 16:15:32 +1000344 # We only support distros that provide a sane lsb_release
345 _ensure_lsb_release
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500346
Ian Wienand7710e7f2014-08-27 16:15:32 +1000347 os_RELEASE=$(lsb_release -r -s)
348 os_CODENAME=$(lsb_release -c -s)
349 os_VENDOR=$(lsb_release -i -s)
Dean Troyerdff49a22014-01-30 15:37:40 -0600350
Ian Wienand7710e7f2014-08-27 16:15:32 +1000351 if [[ $os_VENDOR =~ (Debian|Ubuntu|LinuxMint) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600352 os_PACKAGE="deb"
Ian Wienand7710e7f2014-08-27 16:15:32 +1000353 else
354 os_PACKAGE="rpm"
Dean Troyerdff49a22014-01-30 15:37:40 -0600355 fi
Ian Wienand7710e7f2014-08-27 16:15:32 +1000356
357 typeset -xr os_VENDOR
358 typeset -xr os_RELEASE
359 typeset -xr os_PACKAGE
360 typeset -xr os_CODENAME
Dean Troyerdff49a22014-01-30 15:37:40 -0600361}
362
363# Translate the OS version values into common nomenclature
364# Sets global ``DISTRO`` from the ``os_*`` values
Sean Dagueafef8bf2017-03-06 14:07:23 -0500365declare -g DISTRO
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500366
Ian Wienandaee18c72014-02-21 15:35:08 +1100367function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600368 GetOSVersion
ptoohill196006652016-02-15 16:07:50 -0600369 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) || \
370 "$os_VENDOR" =~ (LinuxMint) ]]; then
371 # 'Everyone' refers to Ubuntu / Debian / Mint releases by
Ian Wienand7710e7f2014-08-27 16:15:32 +1000372 # the code name adjective
Dean Troyerdff49a22014-01-30 15:37:40 -0600373 DISTRO=$os_CODENAME
374 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
375 # For Fedora, just use 'f' and the release
376 DISTRO="f$os_RELEASE"
377 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
378 DISTRO="opensuse-$os_RELEASE"
379 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000380 # just use major release
381 DISTRO="sle${os_RELEASE%.*}"
382 elif [[ "$os_VENDOR" =~ (Red.*Hat) || \
anju Tiwari6c639c92014-07-15 18:11:54 +0530383 "$os_VENDOR" =~ (CentOS) || \
Mike Trimm1da4e792016-04-27 11:40:19 -0500384 "$os_VENDOR" =~ (Scientific) || \
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300385 "$os_VENDOR" =~ (OracleServer) || \
Evgeny Antyshev718512c2016-03-03 14:47:58 +0000386 "$os_VENDOR" =~ (Virtuozzo) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600387 # Drop the . release as we assume it's compatible
Ian Wienand7710e7f2014-08-27 16:15:32 +1000388 # XXX re-evaluate when we get RHEL10
Dean Troyerdff49a22014-01-30 15:37:40 -0600389 DISTRO="rhel${os_RELEASE::1}"
390 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000391 DISTRO="xs${os_RELEASE%.*}"
Alexander Schmidta5ea08b2015-12-02 15:30:44 +0000392 elif [[ "$os_VENDOR" =~ (kvmibm) ]]; then
393 DISTRO="${os_VENDOR}${os_RELEASE::1}"
Dean Troyerdff49a22014-01-30 15:37:40 -0600394 else
Ian Wienanded92e432016-02-16 10:56:56 +1100395 # We can't make a good choice here. Setting a sensible DISTRO
396 # is part of the problem, but not the major issue -- we really
397 # only use DISTRO in the code as a fine-filter.
398 #
399 # The bigger problem is categorising the system into one of
400 # our two big categories as Ubuntu/Debian-ish or
401 # Fedora/CentOS-ish.
402 #
403 # The setting of os_PACKAGE above is only set to "deb" based
404 # on a hard-coded list of vendor names ... thus we will
405 # default to thinking unknown distros are RPM based
406 # (ie. is_ubuntu does not match). But the platform will then
407 # also not match in is_fedora, because that also has a list of
408 # names.
409 #
410 # So, if you are reading this, getting your distro supported
411 # is really about making sure it matches correctly in these
412 # functions. Then you can choose a sensible way to construct
413 # DISTRO based on your distros release approach.
414 die $LINENO "Unable to determine DISTRO, can not continue."
Dean Troyerdff49a22014-01-30 15:37:40 -0600415 fi
Ian Wienand7710e7f2014-08-27 16:15:32 +1000416 typeset -xr DISTRO
Dean Troyerdff49a22014-01-30 15:37:40 -0600417}
418
419# Utility function for checking machine architecture
420# is_arch arch-type
421function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500422 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600423}
424
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700425# Determine if current distribution is an Oracle distribution
426# is_oraclelinux
427function is_oraclelinux {
428 if [[ -z "$os_VENDOR" ]]; then
429 GetOSVersion
430 fi
431
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300432 [ "$os_VENDOR" = "OracleServer" ]
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700433}
434
435
Dean Troyerdff49a22014-01-30 15:37:40 -0600436# Determine if current distribution is a Fedora-based distribution
437# (Fedora, RHEL, CentOS, etc).
438# is_fedora
439function is_fedora {
440 if [[ -z "$os_VENDOR" ]]; then
441 GetOSVersion
442 fi
443
anju Tiwari6c639c92014-07-15 18:11:54 +0530444 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
tengqm78d37392016-03-15 23:08:00 -0400445 [ "$os_VENDOR" = "RedHatEnterpriseServer" ] || \
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300446 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ] || \
Evgeny Antyshev718512c2016-03-03 14:47:58 +0000447 [ "$os_VENDOR" = "Virtuozzo" ] || [ "$os_VENDOR" = "kvmibm" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600448}
449
450
451# Determine if current distribution is a SUSE-based distribution
452# (openSUSE, SLE).
453# is_suse
454function is_suse {
455 if [[ -z "$os_VENDOR" ]]; then
456 GetOSVersion
457 fi
458
Ralf Haferkamp37dddb72016-03-02 15:25:57 +0100459 [[ "$os_VENDOR" =~ (openSUSE) || "$os_VENDOR" == "SUSE LINUX" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600460}
461
462
463# Determine if current distribution is an Ubuntu-based distribution
464# It will also detect non-Ubuntu but Debian-based distros
465# is_ubuntu
466function is_ubuntu {
467 if [[ -z "$os_PACKAGE" ]]; then
468 GetOSVersion
469 fi
470 [ "$os_PACKAGE" = "deb" ]
471}
472
473
474# Git Functions
475# =============
476
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600477# Returns openstack release name for a given branch name
478# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100479function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600480 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700481 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600482 echo ${branch#*/}
483 else
484 echo "master"
485 fi
486}
487
Dean Troyerdff49a22014-01-30 15:37:40 -0600488# git clone only if directory doesn't exist already. Since ``DEST`` might not
489# be owned by the installation user, we create the directory and change the
490# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500491# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
492# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600493# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500494# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600495# git_clone remote dest-dir branch
496function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500497 local git_remote=$1
498 local git_dest=$2
499 local git_ref=$3
Ian Wienandada886d2015-10-07 14:06:26 +1100500 local orig_dir
501 orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200502 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500503
Sean Dague53753292014-12-04 19:38:15 -0500504 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800505 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200506 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
507 fi
508
Dean Troyerdff49a22014-01-30 15:37:40 -0600509 if [[ "$OFFLINE" = "True" ]]; then
510 echo "Running in offline mode, clones already exist"
511 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500512 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600513 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400514 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600515 return
516 fi
517
Dean Troyer50cda692014-07-25 11:57:20 -0500518 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600519 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500520 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700521 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
522 echo "The $git_dest project was not found; if this is a gate job, add"
523 echo "the project to the \$PROJECTS variable in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600524 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700525 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200526 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600527 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500528 cd $git_dest
529 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600530 else
531 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500532 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700533 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
534 echo "The $git_dest project was not found; if this is a gate job, add"
535 echo "the project to the \$PROJECTS variable in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600536 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700537 fi
Jakub Wachowski90742fc2016-11-18 14:28:47 +0100538 # '--branch' can also take tags
539 git_timed clone $git_clone_flags $git_remote $git_dest --branch $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
Mike Perezc271b3e2016-10-03 16:00:33 -0700867 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700868 --role $1 \
Steve Martinelli5541a612015-01-19 15:58:49 -0500869 --user $2 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000870 --project $3 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600871 $domain_args \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700872 | grep '^|\s[a-f0-9]\+' | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500873 if [[ -z "$user_role_id" ]]; then
Roxana Gherle50821be2015-09-22 10:52:46 -0700874 # Adds role to user and get it
875 openstack role add $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100876 --user $2 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600877 --project $3 \
878 $domain_args
Mike Perezc271b3e2016-10-03 16:00:33 -0700879 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700880 --role $1 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700881 --user $2 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700882 --project $3 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600883 $domain_args \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700884 | grep '^|\s[a-f0-9]\+' | get_field 1)
Bartosz Górski0abde392014-02-28 14:15:19 +0100885 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500886 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100887}
888
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500889# Gets or adds user role to domain
890# Usage: get_or_add_user_domain_role <role> <user> <domain>
891function get_or_add_user_domain_role {
892 local user_role_id
893 # Gets user role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700894 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700895 --role $1 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500896 --user $2 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500897 --domain $3 \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700898 | grep '^|\s[a-f0-9]\+' | get_field 1)
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500899 if [[ -z "$user_role_id" ]]; then
900 # Adds role to user and get it
901 openstack role add $1 \
902 --user $2 \
903 --domain $3
Mike Perezc271b3e2016-10-03 16:00:33 -0700904 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700905 --role $1 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500906 --user $2 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500907 --domain $3 \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700908 | grep '^|\s[a-f0-9]\+' | get_field 1)
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500909 fi
910 echo $user_role_id
911}
912
Steve Martinelli4599fd12015-03-12 21:30:58 -0400913# Gets or adds group role to project
914# Usage: get_or_add_group_project_role <role> <group> <project>
915function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000916 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -0400917 # Gets group role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700918 group_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700919 --role $1 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400920 --group $2 \
921 --project $3 \
Mike Perezc271b3e2016-10-03 16:00:33 -0700922 -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400923 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000924 # Adds role to group and get it
925 openstack role add $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000926 --group $2 \
927 --project $3
Mike Perezc271b3e2016-10-03 16:00:33 -0700928 group_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700929 --role $1 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400930 --group $2 \
931 --project $3 \
Mike Perezc271b3e2016-10-03 16:00:33 -0700932 -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400933 fi
934 echo $group_role_id
935}
936
Bartosz Górski0abde392014-02-28 14:15:19 +0100937# Gets or creates service
938# Usage: get_or_create_service <name> <type> <description>
939function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +1000940 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100941 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +1000942 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100943 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +1000944 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100945 # Creates new service if not exists
946 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500947 $2 \
948 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100949 --description="$3" \
950 -f value -c id
951 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500952 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100953}
954
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000955# Create an endpoint with a specific interface
Matt Riedemannae4578b2016-04-23 01:45:40 +0000956# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
957function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +1000958 local endpoint_id
Matt Riedemannae4578b2016-04-23 01:45:40 +0000959 endpoint_id=$(openstack endpoint list \
960 --service $1 \
961 --interface $2 \
962 --region $4 \
963 -c ID -f value)
964 if [[ -z "$endpoint_id" ]]; then
965 # Creates new endpoint
966 endpoint_id=$(openstack endpoint create \
967 $1 $2 $3 --region $4 -f value -c id)
968 fi
969
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500970 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100971}
Dean Troyerdff49a22014-01-30 15:37:40 -0600972
Sean Dague7d1ec432016-04-22 09:19:10 -0400973# Gets or creates endpoint
Sean Dague11eb2012017-02-13 16:16:59 -0500974# Usage: get_or_create_endpoint <service> <region> <publicurl> [adminurl] [internalurl]
Matt Riedemannae4578b2016-04-23 01:45:40 +0000975function get_or_create_endpoint {
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000976 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
977 # creating one endpoint with multiple urls to multiple endpoints each with
978 # a different interface. To maintain the existing function interface we
Matt Riedemannae4578b2016-04-23 01:45:40 +0000979 # create 3 endpoints and return the id of the public one. In reality
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000980 # returning the public id will not make a lot of difference as there are no
981 # scenarios currently that use the returned id. Ideally this behaviour
982 # should be pushed out to the service setups and let them create the
983 # endpoints they need.
Ian Wienandada886d2015-10-07 14:06:26 +1100984 local public_id
Matt Riedemannae4578b2016-04-23 01:45:40 +0000985 public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
Sean Dague11eb2012017-02-13 16:16:59 -0500986 # only create admin/internal urls if provided content for them
987 if [[ -n "$4" ]]; then
988 _get_or_create_endpoint_with_interface $1 admin $4 $2
989 fi
990 if [[ -n "$5" ]]; then
991 _get_or_create_endpoint_with_interface $1 internal $5 $2
992 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000993 # return the public id to indicate success, and this is the endpoint most likely wanted
994 echo $public_id
995}
996
997# Get a URL from the identity service
998# Usage: get_endpoint_url <service> <interface>
999function get_endpoint_url {
1000 echo $(openstack endpoint list \
1001 --service $1 --interface $2 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001002 -c URL -f value)
1003}
1004
Jim Rollenhagen47367072015-12-10 14:24:00 +00001005# check if we are using ironic with hardware
1006# TODO(jroll) this is a kludge left behind when ripping ironic code
1007# out of tree, as it is used by nova and neutron.
1008# figure out a way to refactor nova/neutron code to eliminate this
1009function is_ironic_hardware {
Lucas Alvares Gomes8a4dea22016-02-18 15:52:22 +00001010 is_service_enabled ironic && [[ "$IRONIC_IS_HARDWARE" == "True" ]] && return 0
Jim Rollenhagen47367072015-12-10 14:24:00 +00001011 return 1
1012}
1013
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001014
Dean Troyerdff49a22014-01-30 15:37:40 -06001015# Package Functions
1016# =================
1017
1018# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +11001019function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001020 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -06001021 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001022
1023 if [[ -z "$base_dir" ]]; then
1024 base_dir=$FILES
1025 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001026 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001027 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -06001028 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001029 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -06001030 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001031 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -06001032 else
1033 exit_distro_not_supported "list of packages"
1034 fi
1035 echo "$pkg_dir"
1036}
1037
Sean Dague88ee8ce2015-12-02 07:47:31 -05001038# Wrapper for ``apt-get update`` to try multiple times on the update
1039# to address bad package mirrors (which happen all the time).
1040function apt_get_update {
1041 # only do this once per run
1042 if [[ "$REPOS_UPDATED" == "True" && "$RETRY_UPDATE" != "True" ]]; then
1043 return
1044 fi
1045
1046 # bail if we are offline
1047 [[ "$OFFLINE" = "True" ]] && return
1048
1049 local sudo="sudo"
1050 [[ "$(id -u)" = "0" ]] && sudo="env"
1051
1052 # time all the apt operations
1053 time_start "apt-get-update"
1054
1055 local proxies="http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} no_proxy=${no_proxy:-} "
1056 local update_cmd="$sudo $proxies apt-get update"
1057 if ! timeout 300 sh -c "while ! $update_cmd; do sleep 30; done"; then
1058 die $LINENO "Failed to update apt repos, we're dead now"
1059 fi
1060
1061 REPOS_UPDATED=True
1062 # stop the clock
1063 time_stop "apt-get-update"
1064}
1065
Dean Troyerdff49a22014-01-30 15:37:40 -06001066# Wrapper for ``apt-get`` to set cache and proxy environment variables
1067# Uses globals ``OFFLINE``, ``*_proxy``
1068# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001069function apt_get {
Federico Ressie208d062015-11-21 11:15:39 +00001070 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +11001071 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001072 set +o xtrace
1073
Dean Troyerdff49a22014-01-30 15:37:40 -06001074 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
1075 local sudo="sudo"
1076 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -05001077
Sean Dague95c33d52015-10-07 11:05:59 -04001078 # time all the apt operations
1079 time_start "apt-get"
1080
Sean Dague45917cc2014-02-24 16:09:14 -05001081 $xtrace
Sean Dague53753292014-12-04 19:38:15 -05001082
Dean Troyerdff49a22014-01-30 15:37:40 -06001083 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -05001084 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
1085 no_proxy=${no_proxy:-} \
John L. Villalovosf568c3a2016-01-07 19:10:50 -08001086 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" < /dev/null
Federico Ressie208d062015-11-21 11:15:39 +00001087 result=$?
Sean Dague95c33d52015-10-07 11:05:59 -04001088
1089 # stop the clock
1090 time_stop "apt-get"
Federico Ressie208d062015-11-21 11:15:39 +00001091 return $result
Dean Troyerdff49a22014-01-30 15:37:40 -06001092}
1093
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001094function _parse_package_files {
1095 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -06001096
Dean Troyerdff49a22014-01-30 15:37:40 -06001097 if [[ -z "$DISTRO" ]]; then
1098 GetDistro
1099 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001100
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001101 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001102 local OIFS line package distros distro
1103 [[ -e $fname ]] || continue
1104
1105 OIFS=$IFS
1106 IFS=$'\n'
1107 for line in $(<${fname}); do
1108 if [[ $line =~ "NOPRIME" ]]; then
1109 continue
1110 fi
1111
Ian Wienand1ff75ff2016-02-19 14:28:37 +11001112 # Assume we want this package; free-form
1113 # comments allowed after a #
1114 package=${line%%#*}
Dean Troyerdff49a22014-01-30 15:37:40 -06001115 inst_pkg=1
1116
1117 # Look for # dist:xxx in comment
1118 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1119 # We are using BASH regexp matching feature.
1120 package=${BASH_REMATCH[1]}
1121 distros=${BASH_REMATCH[2]}
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001122 # In bash ${VAR,,} will lowercase VAR
Dean Troyerdff49a22014-01-30 15:37:40 -06001123 # Look for a match in the distro list
1124 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1125 # If no match then skip this package
1126 inst_pkg=0
1127 fi
1128 fi
1129
David Rabel682e0ab2017-03-17 19:19:00 +01001130 # Look for # not:xxx in comment
1131 if [[ $line =~ (.*)#.*not:([^ ]*) ]]; then
1132 # We are using BASH regexp matching feature.
1133 package=${BASH_REMATCH[1]}
1134 distros=${BASH_REMATCH[2]}
1135 # In bash ${VAR,,} will lowercase VAR
1136 # Look for a match in the distro list
1137 if [[ ${distros,,} =~ ${DISTRO,,} ]]; then
1138 # If match then skip this package
1139 inst_pkg=0
1140 fi
1141 fi
1142
Dean Troyerdff49a22014-01-30 15:37:40 -06001143 if [[ $inst_pkg = 1 ]]; then
1144 echo $package
1145 fi
1146 done
1147 IFS=$OIFS
1148 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001149}
1150
1151# get_packages() collects a list of package names of any type from the
1152# prerequisite files in ``files/{debs|rpms}``. The list is intended
1153# to be passed to a package installer such as apt or yum.
1154#
1155# Only packages required for the services in 1st argument will be
1156# included. Two bits of metadata are recognized in the prerequisite files:
1157#
1158# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1159# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1160# of the package to the distros listed. The distro names are case insensitive.
David Rabel682e0ab2017-03-17 19:19:00 +01001161# - ``# not:DISTRO`` or ``not:DISTRO1,DISTRO2`` limits the selection
1162# of the package to the distros not listed. The distro names are case insensitive.
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001163function get_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001164 local xtrace
1165 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001166 set +o xtrace
1167 local services=$@
Ian Wienandada886d2015-10-07 14:06:26 +11001168 local package_dir
1169 package_dir=$(_get_package_dir)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001170 local file_to_parse=""
1171 local service=""
1172
Ian Wienand7d515b52015-11-09 15:04:32 +11001173 if [ $# -ne 1 ]; then
1174 die $LINENO "get_packages takes a single, comma-separated argument"
1175 fi
1176
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001177 if [[ -z "$package_dir" ]]; then
1178 echo "No package directory supplied"
1179 return 1
1180 fi
1181 for service in ${services//,/ }; do
1182 # Allow individual services to specify dependencies
1183 if [[ -e ${package_dir}/${service} ]]; then
1184 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1185 fi
1186 # NOTE(sdague) n-api needs glance for now because that's where
1187 # glance client is
1188 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001189 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001190 file_to_parse="${file_to_parse} ${package_dir}/nova"
1191 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001192 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001193 file_to_parse="${file_to_parse} ${package_dir}/glance"
1194 fi
1195 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001196 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001197 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1198 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001199 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001200 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001201 file_to_parse="${file_to_parse} ${package_dir}/swift"
1202 fi
1203 elif [[ $service == n-* ]]; 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
1207 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001208 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001209 file_to_parse="${file_to_parse} ${package_dir}/glance"
1210 fi
1211 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001212 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001213 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1214 fi
1215 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001216 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001217 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1218 fi
1219 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001220 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001221 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1222 fi
1223 fi
1224 done
1225 echo "$(_parse_package_files $file_to_parse)"
1226 $xtrace
1227}
1228
1229# get_plugin_packages() collects a list of package names of any type from a
1230# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1231# list is intended to be passed to a package installer such as apt or yum.
1232#
1233# Only packages required for enabled and collected plugins will included.
1234#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001235# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001236# in these prerequisite files, see get_packages() for more info.
1237function get_plugin_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001238 local xtrace
1239 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001240 set +o xtrace
1241 local files_to_parse=""
1242 local package_dir=""
1243 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
Ian Wienand7ae97292016-02-16 14:50:53 +11001244 package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
Dmitry Tantsur5979f472016-01-03 18:08:14 +01001245 files_to_parse+=" $package_dir/$plugin"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001246 done
1247 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001248 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001249}
1250
1251# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001252# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001253# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001254function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001255 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1256 REPOS_UPDATED=${REPOS_UPDATED:-False}
1257 RETRY_UPDATE=${RETRY_UPDATE:-False}
1258
Paul Linchpiner9e179742014-07-13 22:23:00 -07001259 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001260 return 0
1261 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001262
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001263 if is_ubuntu; then
Sean Dague88ee8ce2015-12-02 07:47:31 -05001264 apt_get_update
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001265 fi
1266}
1267
1268function real_install_package {
1269 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001270 apt_get install "$@"
1271 elif is_fedora; then
1272 yum_install "$@"
1273 elif is_suse; then
1274 zypper_install "$@"
1275 else
1276 exit_distro_not_supported "installing packages"
1277 fi
1278}
1279
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001280# Distro-agnostic package installer
1281# install_package package [package ...]
1282function install_package {
1283 update_package_repo
Federico Ressiecc1f412015-12-28 15:14:13 +01001284 if ! real_install_package "$@"; then
1285 RETRY_UPDATE=True update_package_repo && real_install_package "$@"
1286 fi
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001287}
1288
Dean Troyerdff49a22014-01-30 15:37:40 -06001289# Distro-agnostic function to tell if a package is installed
1290# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001291function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001292 if [[ -z "$@" ]]; then
1293 return 1
1294 fi
1295
1296 if [[ -z "$os_PACKAGE" ]]; then
1297 GetOSVersion
1298 fi
1299
1300 if [[ "$os_PACKAGE" = "deb" ]]; then
1301 dpkg -s "$@" > /dev/null 2> /dev/null
1302 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1303 rpm --quiet -q "$@"
1304 else
1305 exit_distro_not_supported "finding if a package is installed"
1306 fi
1307}
1308
1309# Distro-agnostic package uninstaller
1310# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001311function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001312 if is_ubuntu; then
1313 apt_get purge "$@"
1314 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001315 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001316 elif is_suse; then
Gary W. Smith56b39122016-11-16 22:03:43 -08001317 sudo zypper remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001318 else
1319 exit_distro_not_supported "uninstalling packages"
1320 fi
1321}
1322
1323# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001324# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001325# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001326function yum_install {
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001327 local result parse_yum_result
1328
Dean Troyerdff49a22014-01-30 15:37:40 -06001329 [[ "$OFFLINE" = "True" ]] && return
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001330
1331 time_start "yum_install"
1332
Ian Wienanda4705402016-09-23 15:08:48 +10001333 # This is a bit tricky, because yum -y assumes missing or failed
1334 # packages are OK (see [1]). We want devstack to stop if we are
1335 # installing missing packages.
1336 #
1337 # Thus we manually match on the output (stack.sh runs in a fixed
1338 # locale, so lang shouldn't change).
1339 #
1340 # If yum returns !0, we echo the result as "YUM_FAILED" and return
1341 # that from the awk (we're subverting -e with this trick).
1342 # Otherwise we use awk to look for failure strings and return "2"
1343 # to indicate a terminal failure.
1344 #
1345 # [1] https://bugzilla.redhat.com/show_bug.cgi?id=965567
1346 parse_yum_result=' \
1347 BEGIN { result=0 } \
1348 /^YUM_FAILED/ { result=$2 } \
1349 /^No package/ { result=2 } \
1350 /^Failed:/ { result=2 } \
1351 //{ print } \
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001352 END { exit result }'
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001353 (sudo_with_proxies "${YUM:-yum}" install -y "$@" 2>&1 || echo YUM_FAILED $?) \
Ian Wienand1b1cc8c2016-03-01 13:14:39 +11001354 | awk "$parse_yum_result" && result=$? || result=$?
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001355
1356 time_stop "yum_install"
1357
Ian Wienand1b1cc8c2016-03-01 13:14:39 +11001358 # if we return 1, then the wrapper functions will run an update
1359 # and try installing the package again as a defense against bad
1360 # mirrors. This can hide failures, especially when we have
1361 # packages that are in the "Failed:" section because their rpm
1362 # install scripts failed to run correctly (in this case, the
1363 # package looks installed, so when the retry happens we just think
1364 # the package is OK, and incorrectly continue on).
1365 if [ "$result" == 2 ]; then
1366 die "Detected fatal package install failure"
1367 fi
1368
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001369 return "$result"
Dean Troyerdff49a22014-01-30 15:37:40 -06001370}
1371
1372# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001373# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001374# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001375function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001376 [[ "$OFFLINE" = "True" ]] && return
1377 local sudo="sudo"
1378 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001379 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1380 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001381 zypper --non-interactive install --auto-agree-with-licenses "$@"
1382}
1383
1384
1385# Process Functions
1386# =================
1387
1388# _run_process() is designed to be backgrounded by run_process() to simulate a
1389# fork. It includes the dirty work of closing extra filehandles and preparing log
1390# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001391# from the service name.
1392# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001393# If an optional group is provided sg will be used to set the group of
1394# the command.
1395# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001396function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001397 # disable tracing through the exec redirects, it's just confusing in the logs.
1398 xtrace=$(set +o | grep xtrace)
1399 set +o xtrace
1400
Dean Troyerdff49a22014-01-30 15:37:40 -06001401 local service=$1
1402 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001403 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001404
1405 # Undo logging redirections and close the extra descriptors
1406 exec 1>&3
1407 exec 2>&3
1408 exec 3>&-
1409 exec 6>&-
1410
Philipp Marekaf0801d2015-12-09 13:51:56 +01001411 local logfile="${service}.log.${CURRENT_LOG_TIME}"
1412 local real_logfile="${LOGDIR}/${logfile}"
Dean Troyerdde41d02014-12-09 17:47:57 -06001413 if [[ -n ${LOGDIR} ]]; then
1414 exec 1>&"$real_logfile" 2>&1
Philipp Marekaf0801d2015-12-09 13:51:56 +01001415 bash -c "cd '$LOGDIR' && ln -sf '$logfile' ${service}.log"
Dean Troyerdde41d02014-12-09 17:47:57 -06001416 if [[ -n ${SCREEN_LOGDIR} ]]; then
1417 # Drop the backward-compat symlink
1418 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1419 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001420
1421 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1422 export PYTHONUNBUFFERED=1
1423 fi
1424
Sean Dague6e137ab2015-04-29 08:22:24 -04001425 # reenable xtrace before we do *real* work
1426 $xtrace
1427
Dean Troyer3159a822014-08-27 14:13:58 -05001428 # Run under ``setsid`` to force the process to become a session and group leader.
1429 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001430 if [[ -n "$group" ]]; then
1431 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1432 else
1433 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1434 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001435
1436 # Just silently exit this process
1437 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001438}
1439
Sean Dague5edae542017-03-21 20:50:24 -04001440function write_user_unit_file {
1441 local service=$1
1442 local command="$2"
1443 local group=$3
1444 local user=$4
1445 local extra=""
1446 if [[ -n "$group" ]]; then
1447 extra="Group=$group"
1448 fi
1449 local unitfile="$SYSTEMD_DIR/$service"
1450 mkdir -p $SYSTEMD_DIR
1451
1452 iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
1453 iniset -sudo $unitfile "Service" "User" "$user"
1454 iniset -sudo $unitfile "Service" "ExecStart" "$command"
Matthew Treinish477a9622017-08-04 11:09:26 -04001455 iniset -sudo $unitfile "Service" "KillMode" "process"
1456 iniset -sudo $unitfile "Service" "TimeoutStopSec" "infinity"
Dinesh Bhoref60f2b2017-09-05 14:40:32 +05301457 iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
Sean Dague5edae542017-03-21 20:50:24 -04001458 if [[ -n "$group" ]]; then
1459 iniset -sudo $unitfile "Service" "Group" "$group"
1460 fi
1461 iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
1462
1463 # changes to existing units sometimes need a refresh
1464 $SYSTEMCTL daemon-reload
1465}
1466
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001467function write_uwsgi_user_unit_file {
1468 local service=$1
1469 local command="$2"
1470 local group=$3
1471 local user=$4
1472 local unitfile="$SYSTEMD_DIR/$service"
1473 mkdir -p $SYSTEMD_DIR
1474
1475 iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
Sean Daguef41bf2a2017-05-05 07:50:52 -04001476 iniset -sudo $unitfile "Service" "SyslogIdentifier" "$service"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001477 iniset -sudo $unitfile "Service" "User" "$user"
1478 iniset -sudo $unitfile "Service" "ExecStart" "$command"
Dinesh Bhoref60f2b2017-09-05 14:40:32 +05301479 iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001480 iniset -sudo $unitfile "Service" "Type" "notify"
Matthew Treinish477a9622017-08-04 11:09:26 -04001481 iniset -sudo $unitfile "Service" "KillMode" "process"
gong yong sheng07b3bc22017-06-05 14:02:28 +08001482 iniset -sudo $unitfile "Service" "Restart" "always"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001483 iniset -sudo $unitfile "Service" "NotifyAccess" "all"
1484 iniset -sudo $unitfile "Service" "RestartForceExitStatus" "100"
1485
1486 if [[ -n "$group" ]]; then
1487 iniset -sudo $unitfile "Service" "Group" "$group"
1488 fi
1489 iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
1490
1491 # changes to existing units sometimes need a refresh
1492 $SYSTEMCTL daemon-reload
1493}
1494
Sean Dague148d58c2017-05-04 13:08:25 -04001495function _common_systemd_pitfalls {
1496 local cmd=$1
1497 # do some sanity checks on $cmd to see things we don't expect to work
1498
1499 if [[ "$cmd" =~ "sudo" ]]; then
1500 local msg=<<EOF
1501You are trying to use run_process with sudo, this is not going to work under systemd.
1502
1503If you need to run a service as a user other than $STACK_USER call it with:
1504
1505 run_process \$name \$cmd \$group \$user
1506EOF
1507 die $LINENO $msg
1508 fi
1509
1510 if [[ ! "$cmd" =~ ^/ ]]; then
1511 local msg=<<EOF
1512The cmd="$cmd" does not start with an absolute path. It will fail to
1513start under systemd.
1514
1515Please update your run_process stanza to have an absolute path.
1516EOF
1517 die $LINENO $msg
1518 fi
1519
1520}
1521
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001522# Helper function to build a basic unit file and run it under systemd.
1523function _run_under_systemd {
Sean Dague5edae542017-03-21 20:50:24 -04001524 local service=$1
1525 local command="$2"
1526 local cmd=$command
Sean Dague148d58c2017-05-04 13:08:25 -04001527 # sanity check the command
1528 _common_systemd_pitfalls "$cmd"
1529
Sean Dague5edae542017-03-21 20:50:24 -04001530 local systemd_service="devstack@$service.service"
1531 local group=$3
1532 local user=${4:-$STACK_USER}
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001533 if [[ "$command" =~ "uwsgi" ]] ; then
1534 write_uwsgi_user_unit_file $systemd_service "$cmd" "$group" "$user"
1535 else
1536 write_user_unit_file $systemd_service "$cmd" "$group" "$user"
1537 fi
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001538
1539 $SYSTEMCTL enable $systemd_service
1540 $SYSTEMCTL start $systemd_service
Sean Dague5edae542017-03-21 20:50:24 -04001541}
1542
Dean Troyerdff49a22014-01-30 15:37:40 -06001543# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1544# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001545# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001546# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001547function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001548 SCREEN_NAME=${SCREEN_NAME:-stack}
1549 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1550
1551 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1552 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1553 fi
1554
1555 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1556}
1557
1558# Find out if a process exists by partial name.
1559# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001560function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001561 local name=$1
1562 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001563 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001564 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001565 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001566}
1567
Dean Troyer3159a822014-08-27 14:13:58 -05001568# Run a single service under screen or directly
1569# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001570# If an optional group is provided sg will be used to run the
1571# command as that group.
Tony Breeds93292902015-11-09 15:45:04 +11001572# Uses globals ``USE_SCREEN``
Sean Dague148d58c2017-05-04 13:08:25 -04001573# run_process service "command-line" [group] [user]
Ian Wienandaee18c72014-02-21 15:35:08 +11001574function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001575 local service=$1
1576 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001577 local group=$3
Sean Dague5edae542017-03-21 20:50:24 -04001578 local user=$4
Brant Knudsonedc11c22015-12-14 15:32:05 -06001579
Sean Dague5edae542017-03-21 20:50:24 -04001580 local name=$service
Dean Troyerdff49a22014-01-30 15:37:40 -06001581
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001582 time_start "run_process"
Dean Troyer3159a822014-08-27 14:13:58 -05001583 if is_service_enabled $service; then
Sean Dague5edae542017-03-21 20:50:24 -04001584 if [[ "$USE_SYSTEMD" = "True" ]]; then
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001585 _run_under_systemd "$name" "$command" "$group" "$user"
Sean Dague5edae542017-03-21 20:50:24 -04001586 elif [[ "$USE_SCREEN" = "True" ]]; then
1587 if [[ "$user" == "root" ]]; then
1588 command="sudo $command"
1589 fi
Brant Knudsonedc11c22015-12-14 15:32:05 -06001590 screen_process "$name" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001591 else
1592 # Spawn directly without screen
Sean Dague5edae542017-03-21 20:50:24 -04001593 if [[ "$user" == "root" ]]; then
1594 command="sudo $command"
1595 fi
Brant Knudsonedc11c22015-12-14 15:32:05 -06001596 _run_process "$name" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001597 fi
1598 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001599 time_stop "run_process"
Dean Troyerdff49a22014-01-30 15:37:40 -06001600}
1601
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001602# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001603# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Tony Breeds93292902015-11-09 15:45:04 +11001604# ``SERVICE_DIR``, ``SCREEN_IS_LOGGING``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001605# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001606# Run a command in a shell in a screen window, if an optional group
1607# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001608function screen_process {
1609 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001610 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001611 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001612
Sean Dagueea22a4f2014-06-27 15:21:41 -04001613 SCREEN_NAME=${SCREEN_NAME:-stack}
1614 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyerdff49a22014-01-30 15:37:40 -06001615
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001616 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001617
Philipp Marekaf0801d2015-12-09 13:51:56 +01001618 local logfile="${name}.log.${CURRENT_LOG_TIME}"
1619 local real_logfile="${LOGDIR}/${logfile}"
Dean Troyerdde41d02014-12-09 17:47:57 -06001620 echo "LOGDIR: $LOGDIR"
1621 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1622 echo "log: $real_logfile"
1623 if [[ -n ${LOGDIR} ]]; then
Tony Breeds536b8c12015-11-09 10:05:37 +11001624 if [[ "$SCREEN_IS_LOGGING" == "True" ]]; then
1625 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
1626 screen -S $SCREEN_NAME -p $name -X log on
1627 fi
1628 # If logging isn't active then avoid a broken symlink
1629 touch "$real_logfile"
Philipp Marekaf0801d2015-12-09 13:51:56 +01001630 bash -c "cd '$LOGDIR' && ln -sf '$logfile' ${name}.log"
Dean Troyerdde41d02014-12-09 17:47:57 -06001631 if [[ -n ${SCREEN_LOGDIR} ]]; then
1632 # Drop the backward-compat symlink
1633 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1634 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001635 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001636
1637 # sleep to allow bash to be ready to be send the command - we are
1638 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001639 # bash isn't running by the time we send the command, nothing
1640 # happens. This sleep was added originally to handle gate runs
1641 # where we needed this to be at least 3 seconds to pass
1642 # consistently on slow clouds. Now this is configurable so that we
1643 # can determine a reasonable value for the local case which should
1644 # be much smaller.
1645 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001646
1647 NL=`echo -ne '\015'`
1648 # This fun command does the following:
1649 # - the passed server command is backgrounded
1650 # - the pid of the background process is saved in the usual place
1651 # - the server process is brought back to the foreground
1652 # - if the server process exits prematurely the fg command errors
1653 # and a message is written to stdout and the process failure file
1654 #
1655 # The pid saved can be used in stop_process() as a process group
1656 # id to kill off all child processes
1657 if [[ -n "$group" ]]; then
1658 command="sg $group '$command'"
1659 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001660
1661 # Append the process to the screen rc file
1662 screen_rc "$name" "$command"
1663
Joanna Taryma9d49ed92017-02-06 11:30:15 -08001664 screen -S $SCREEN_NAME -p $name -X stuff "$command & echo \$! >$SERVICE_DIR/$SCREEN_NAME/${name}.pid; fg || echo \"$name failed to start. Exit code: \$?\" | tee \"$SERVICE_DIR/$SCREEN_NAME/${name}.failure\"$NL"
Dean Troyerdff49a22014-01-30 15:37:40 -06001665}
1666
1667# Screen rc file builder
Tony Breeds536b8c12015-11-09 10:05:37 +11001668# Uses globals ``SCREEN_NAME``, ``SCREENRC``, ``SCREEN_IS_LOGGING``
Dean Troyerdff49a22014-01-30 15:37:40 -06001669# screen_rc service "command-line"
1670function screen_rc {
1671 SCREEN_NAME=${SCREEN_NAME:-stack}
1672 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1673 if [[ ! -e $SCREENRC ]]; then
1674 # Name the screen session
1675 echo "sessionname $SCREEN_NAME" > $SCREENRC
1676 # Set a reasonable statusbar
1677 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1678 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1679 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1680 echo "screen -t shell bash" >> $SCREENRC
1681 fi
1682 # If this service doesn't already exist in the screenrc file
1683 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1684 NL=`echo -ne '\015'`
1685 echo "screen -t $1 bash" >> $SCREENRC
1686 echo "stuff \"$2$NL\"" >> $SCREENRC
1687
Tony Breeds536b8c12015-11-09 10:05:37 +11001688 if [[ -n ${LOGDIR} ]] && [[ "$SCREEN_IS_LOGGING" == "True" ]]; then
Dean Troyerdde41d02014-12-09 17:47:57 -06001689 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001690 echo "log on" >>$SCREENRC
1691 fi
1692 fi
1693}
1694
1695# Stop a service in screen
1696# If a PID is available use it, kill the whole process group via TERM
1697# If screen is being used kill the screen window; this will catch processes
1698# that did not leave a PID behind
Tony Breeds93292902015-11-09 15:45:04 +11001699# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001700# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001701function screen_stop_service {
1702 local service=$1
1703
Dean Troyerdff49a22014-01-30 15:37:40 -06001704 SCREEN_NAME=${SCREEN_NAME:-stack}
1705 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyerdff49a22014-01-30 15:37:40 -06001706
Dean Troyer3159a822014-08-27 14:13:58 -05001707 if is_service_enabled $service; then
1708 # Clean up the screen window
Attila Fazekasf750a6f2015-07-01 12:17:35 +02001709 screen -S $SCREEN_NAME -p $service -X kill || true
Dean Troyer3159a822014-08-27 14:13:58 -05001710 fi
1711}
1712
1713# Stop a service process
1714# If a PID is available use it, kill the whole process group via TERM
1715# If screen is being used kill the screen window; this will catch processes
1716# that did not leave a PID behind
1717# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1718# stop_process service
1719function stop_process {
1720 local service=$1
1721
1722 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer3159a822014-08-27 14:13:58 -05001723
1724 if is_service_enabled $service; then
Sean Dague803acff2017-05-01 10:52:38 -04001725 # Only do this for units which appear enabled, this also
1726 # catches units that don't really exist for cases like
1727 # keystone without a failure.
1728 if $SYSTEMCTL is-enabled devstack@$service.service; then
1729 $SYSTEMCTL stop devstack@$service.service
1730 $SYSTEMCTL disable devstack@$service.service
Sean Dague5edae542017-03-21 20:50:24 -04001731 fi
1732
Dean Troyer3159a822014-08-27 14:13:58 -05001733 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1734 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
Dan Smithce7246a2015-04-23 09:41:06 -07001735 # oslo.service tends to stop actually shutting down
1736 # reliably in between releases because someone believes it
1737 # is dying too early due to some inflight work they
1738 # have. This is a tension. It happens often enough we're
1739 # going to just account for it in devstack and assume it
1740 # doesn't work.
1741 #
1742 # Set OSLO_SERVICE_WORKS=True to skip this block
1743 if [[ -z "$OSLO_SERVICE_WORKS" ]]; then
1744 # TODO(danms): Remove this double-kill when we have
1745 # this fixed in all services:
1746 # https://bugs.launchpad.net/oslo-incubator/+bug/1446583
1747 sleep 1
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001748 # /bin/true because pkill on a non existent process returns an error
Dan Smithce7246a2015-04-23 09:41:06 -07001749 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid) || /bin/true
1750 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001751 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001752 fi
1753 if [[ "$USE_SCREEN" = "True" ]]; then
1754 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001755 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001756 fi
1757 fi
1758}
1759
1760# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001761# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001762# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001763function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001764 local service
1765 local failures
1766 SCREEN_NAME=${SCREEN_NAME:-stack}
1767 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1768
1769
1770 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1771 echo "No service status directory found"
1772 return
1773 fi
1774
Wei Jiangange3340f12015-09-21 17:52:14 +08001775 # Check if there is any failure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001776 # make this -o errexit safe
1777 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001778
1779 for service in $failures; do
1780 service=`basename $service`
1781 service=${service%.failure}
1782 echo "Error: Service $service is not running"
1783 done
1784
1785 if [ -n "$failures" ]; then
Dean Troyer9ba49cd2016-03-10 15:45:40 -06001786 die $LINENO "More details about the above errors can be found with screen"
Dean Troyerdff49a22014-01-30 15:37:40 -06001787 fi
1788}
1789
Chris Dent2f27a0e2014-09-09 13:46:02 +01001790# Tail a log file in a screen if USE_SCREEN is true.
Tony Breeds93292902015-11-09 15:45:04 +11001791# Uses globals ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001792function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001793 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001794 local logfile=$2
1795
Chris Dent2f27a0e2014-09-09 13:46:02 +01001796 if [[ "$USE_SCREEN" = "True" ]]; then
Zane Bitter95ed7c62016-12-20 17:29:14 -05001797 screen_process "$name" "sudo tail -f $logfile | sed -u 's/\\\\\\\\x1b/\o033/g'"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001798 fi
1799}
1800
Dean Troyerdff49a22014-01-30 15:37:40 -06001801
Dean Troyer3159a822014-08-27 14:13:58 -05001802# Deprecated Functions
1803# --------------------
1804
1805# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1806# fork. It includes the dirty work of closing extra filehandles and preparing log
1807# files to produce the same logs as screen_it(). The log filename is derived
1808# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1809# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1810# _old_run_process service "command-line"
1811function _old_run_process {
1812 local service=$1
1813 local command="$2"
1814
1815 # Undo logging redirections and close the extra descriptors
1816 exec 1>&3
1817 exec 2>&3
1818 exec 3>&-
1819 exec 6>&-
1820
1821 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001822 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1823 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001824
1825 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1826 export PYTHONUNBUFFERED=1
1827 fi
1828
1829 exec /bin/bash -c "$command"
1830 die "$service exec failure: $command"
1831}
1832
1833# old_run_process() launches a child process that closes all file descriptors and
1834# then exec's the passed in command. This is meant to duplicate the semantics
1835# of screen_it() without screen. PIDs are written to
1836# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1837# old_run_process service "command-line"
1838function old_run_process {
1839 local service=$1
1840 local command="$2"
1841
1842 # Spawn the child process
1843 _old_run_process "$service" "$command" &
1844 echo $!
1845}
1846
1847# Compatibility for existing start_XXXX() functions
1848# Uses global ``USE_SCREEN``
1849# screen_it service "command-line"
1850function screen_it {
1851 if is_service_enabled $1; then
1852 # Append the service to the screen rc file
1853 screen_rc "$1" "$2"
1854
1855 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001856 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001857 else
1858 # Spawn directly without screen
1859 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1860 fi
1861 fi
1862}
1863
1864# Compatibility for existing stop_XXXX() functions
1865# Stop a service in screen
1866# If a PID is available use it, kill the whole process group via TERM
1867# If screen is being used kill the screen window; this will catch processes
1868# that did not leave a PID behind
1869# screen_stop service
1870function screen_stop {
1871 # Clean up the screen window
1872 stop_process $1
1873}
1874
1875
Sean Dague2c65e712014-12-18 09:44:56 -05001876# Plugin Functions
1877# =================
1878
1879DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1880
1881# enable_plugin <name> <url> [branch]
1882#
1883# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1884# ``url`` is a git url
1885# ``branch`` is a gitref. If it's not set, defaults to master
1886function enable_plugin {
1887 local name=$1
1888 local url=$2
1889 local branch=${3:-master}
Omer Anson51584862017-08-24 17:47:37 +03001890 if is_plugin_enabled $name; then
John L. Villalovos21d84c22016-11-11 15:26:11 -08001891 die $LINENO "Plugin attempted to be enabled twice: ${name} ${url} ${branch}"
1892 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001893 DEVSTACK_PLUGINS+=",$name"
1894 GITREPO[$name]=$url
1895 GITDIR[$name]=$DEST/$name
1896 GITBRANCH[$name]=$branch
1897}
1898
Omer Anson51584862017-08-24 17:47:37 +03001899# is_plugin_enabled <name>
1900#
1901# Check if the plugin was enabled, e.g. using enable_plugin
1902#
1903# ``name`` The name with which the plugin was enabled
1904function is_plugin_enabled {
1905 local name=$1
1906 if [[ ",${DEVSTACK_PLUGINS}," =~ ",${name}," ]]; then
1907 return 0
1908 fi
1909 return 1
1910}
1911
Sean Dague2c65e712014-12-18 09:44:56 -05001912# fetch_plugins
1913#
1914# clones all plugins
1915function fetch_plugins {
1916 local plugins="${DEVSTACK_PLUGINS}"
1917 local plugin
1918
1919 # short circuit if nothing to do
1920 if [[ -z $plugins ]]; then
1921 return
1922 fi
1923
Dean Troyerdc97cb72015-03-28 08:20:50 -05001924 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001925 for plugin in ${plugins//,/ }; do
1926 git_clone_by_name $plugin
1927 done
1928}
1929
1930# load_plugin_settings
1931#
1932# Load settings from plugins in the order that they were registered
1933function load_plugin_settings {
1934 local plugins="${DEVSTACK_PLUGINS}"
1935 local plugin
1936
1937 # short circuit if nothing to do
1938 if [[ -z $plugins ]]; then
1939 return
1940 fi
1941
1942 echo "Loading plugin settings"
1943 for plugin in ${plugins//,/ }; do
1944 local dir=${GITDIR[$plugin]}
1945 # source any known settings
1946 if [[ -f $dir/devstack/settings ]]; then
1947 source $dir/devstack/settings
1948 fi
1949 done
1950}
1951
Sean Dague6e275e12015-03-26 05:54:28 -04001952# plugin_override_defaults
1953#
1954# Run an extremely early setting phase for plugins that allows default
1955# overriding of services.
1956function plugin_override_defaults {
1957 local plugins="${DEVSTACK_PLUGINS}"
1958 local plugin
1959
1960 # short circuit if nothing to do
1961 if [[ -z $plugins ]]; then
1962 return
1963 fi
1964
1965 echo "Overriding Configuration Defaults"
1966 for plugin in ${plugins//,/ }; do
1967 local dir=${GITDIR[$plugin]}
1968 # source any overrides
1969 if [[ -f $dir/devstack/override-defaults ]]; then
1970 # be really verbose that an override is happening, as it
1971 # may not be obvious if things fail later.
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001972 echo "$plugin has overridden the following defaults"
Sean Dague6e275e12015-03-26 05:54:28 -04001973 cat $dir/devstack/override-defaults
1974 source $dir/devstack/override-defaults
1975 fi
1976 done
1977}
1978
Sean Dague2c65e712014-12-18 09:44:56 -05001979# run_plugins
1980#
1981# Run the devstack/plugin.sh in all the plugin directories. These are
1982# run in registration order.
1983function run_plugins {
1984 local mode=$1
1985 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301986
1987 local plugins="${DEVSTACK_PLUGINS}"
1988 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001989 for plugin in ${plugins//,/ }; do
1990 local dir=${GITDIR[$plugin]}
1991 if [[ -f $dir/devstack/plugin.sh ]]; then
1992 source $dir/devstack/plugin.sh $mode $phase
1993 fi
1994 done
1995}
1996
1997function run_phase {
1998 local mode=$1
1999 local phase=$2
2000 if [[ -d $TOP_DIR/extras.d ]]; then
Dmitry Tantsur64be3212015-10-12 13:10:24 +02002001 local extra_plugin_file_name
2002 for extra_plugin_file_name in $TOP_DIR/extras.d/*.sh; do
Sean Dague41d01102015-12-03 08:12:23 -05002003 # NOTE(sdague): only process extras.d for the 3 explicitly
2004 # white listed elements in tree. We want these to move out
2005 # over time as well, but they are in tree, so we need to
2006 # manage that.
Matt Riedemannc9f63272016-08-30 17:21:30 -04002007 local exceptions="80-tempest.sh"
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002008 local extra
2009 extra=$(basename $extra_plugin_file_name)
Sean Dague1de9e332015-10-07 08:46:13 -04002010 if [[ ! ( $exceptions =~ "$extra" ) ]]; then
Sean Dague41d01102015-12-03 08:12:23 -05002011 warn "use of extras.d is no longer supported"
2012 warn "processing of project $extra is skipped"
2013 else
2014 [[ -r $extra_plugin_file_name ]] && source $extra_plugin_file_name $mode $phase
Sean Dague1de9e332015-10-07 08:46:13 -04002015 fi
Sean Dague2c65e712014-12-18 09:44:56 -05002016 done
2017 fi
2018 # the source phase corresponds to settings loading in plugins
2019 if [[ "$mode" == "source" ]]; then
2020 load_plugin_settings
Chris Dentc6d47012015-10-09 14:57:05 +00002021 verify_disabled_services
Sean Dague6e275e12015-03-26 05:54:28 -04002022 elif [[ "$mode" == "override_defaults" ]]; then
2023 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05002024 else
2025 run_plugins $mode $phase
2026 fi
2027}
2028
Dean Troyerdff49a22014-01-30 15:37:40 -06002029
2030# Service Functions
2031# =================
2032
2033# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
2034# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11002035function _cleanup_service_list {
Ian Wienand8043bfa2015-10-14 14:53:18 +11002036 local xtrace
2037 xtrace=$(set +o | grep xtrace)
2038 set +o xtrace
2039
Dean Troyerdff49a22014-01-30 15:37:40 -06002040 echo "$1" | sed -e '
2041 s/,,/,/g;
2042 s/^,//;
2043 s/,$//
2044 '
Ian Wienand8043bfa2015-10-14 14:53:18 +11002045
2046 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002047}
2048
2049# disable_all_services() removes all current services
2050# from ``ENABLED_SERVICES`` to reset the configuration
2051# before a minimal installation
2052# Uses global ``ENABLED_SERVICES``
2053# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11002054function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06002055 ENABLED_SERVICES=""
2056}
2057
2058# Remove all services starting with '-'. For example, to install all default
2059# services except rabbit (rabbit) set in ``localrc``:
2060# ENABLED_SERVICES+=",-rabbit"
2061# Uses global ``ENABLED_SERVICES``
2062# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11002063function disable_negated_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11002064 local xtrace
2065 xtrace=$(set +o | grep xtrace)
2066 set +o xtrace
2067
Ian Wienand2796a822015-04-15 08:59:04 +10002068 local to_remove=""
2069 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06002070 local service
Ian Wienand2796a822015-04-15 08:59:04 +10002071
2072 # build up list of services that should be removed; i.e. they
2073 # begin with "-"
2074 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06002075 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10002076 to_remove+=",${service#-}"
2077 else
2078 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06002079 fi
2080 done
Ian Wienand2796a822015-04-15 08:59:04 +10002081
2082 # go through the service list. if this service appears in the "to
2083 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09002084 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Ian Wienand8043bfa2015-10-14 14:53:18 +11002085
2086 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002087}
2088
Chris Dentc6d47012015-10-09 14:57:05 +00002089# disable_service() prepares the services passed as argument to be
2090# removed from the ``ENABLED_SERVICES`` list, if they are present.
Dean Troyerdff49a22014-01-30 15:37:40 -06002091#
2092# For example:
2093# disable_service rabbit
2094#
Chris Dentc6d47012015-10-09 14:57:05 +00002095# Uses global ``DISABLED_SERVICES``
Dean Troyerdff49a22014-01-30 15:37:40 -06002096# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11002097function disable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11002098 local xtrace
2099 xtrace=$(set +o | grep xtrace)
2100 set +o xtrace
2101
Chris Dentc6d47012015-10-09 14:57:05 +00002102 local disabled_svcs="${DISABLED_SERVICES}"
2103 local enabled_svcs=",${ENABLED_SERVICES},"
Dean Troyerdff49a22014-01-30 15:37:40 -06002104 local service
2105 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00002106 disabled_svcs+=",$service"
Dean Troyerdff49a22014-01-30 15:37:40 -06002107 if is_service_enabled $service; then
Chris Dentc6d47012015-10-09 14:57:05 +00002108 enabled_svcs=${enabled_svcs//,$service,/,}
Dean Troyerdff49a22014-01-30 15:37:40 -06002109 fi
2110 done
Chris Dentc6d47012015-10-09 14:57:05 +00002111 DISABLED_SERVICES=$(_cleanup_service_list "$disabled_svcs")
2112 ENABLED_SERVICES=$(_cleanup_service_list "$enabled_svcs")
Ian Wienand8043bfa2015-10-14 14:53:18 +11002113
2114 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002115}
2116
2117# enable_service() adds the services passed as argument to the
2118# ``ENABLED_SERVICES`` list, if they are not already present.
2119#
2120# For example:
Sean Dague37eca482015-06-16 07:19:22 -04002121# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06002122#
2123# This function does not know about the special cases
2124# for nova, glance, and neutron built into is_service_enabled().
2125# Uses global ``ENABLED_SERVICES``
2126# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11002127function enable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11002128 local xtrace
2129 xtrace=$(set +o | grep xtrace)
2130 set +o xtrace
2131
Dean Troyerdff49a22014-01-30 15:37:40 -06002132 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002133 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06002134 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00002135 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
2136 warn $LINENO "Attempt to enable_service ${service} when it has been disabled"
2137 continue
2138 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06002139 if ! is_service_enabled $service; then
2140 tmpsvcs+=",$service"
2141 fi
2142 done
2143 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
2144 disable_negated_services
Ian Wienand8043bfa2015-10-14 14:53:18 +11002145
2146 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002147}
2148
2149# is_service_enabled() checks if the service(s) specified as arguments are
2150# enabled by the user in ``ENABLED_SERVICES``.
2151#
2152# Multiple services specified as arguments are ``OR``'ed together; the test
2153# is a short-circuit boolean, i.e it returns on the first match.
2154#
2155# There are special cases for some 'catch-all' services::
2156# **nova** returns true if any service enabled start with **n-**
2157# **cinder** returns true if any service enabled start with **c-**
Dean Troyerdff49a22014-01-30 15:37:40 -06002158# **glance** returns true if any service enabled start with **g-**
2159# **neutron** returns true if any service enabled start with **q-**
2160# **swift** returns true if any service enabled start with **s-**
2161# **trove** returns true if any service enabled start with **tr-**
2162# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
2163# **s-** services will be enabled. This will be deprecated in the future.
2164#
2165# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
2166# We also need to make sure to treat **n-cell-region** and **n-cell-child**
2167# as enabled in this case.
2168#
2169# Uses global ``ENABLED_SERVICES``
2170# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11002171function is_service_enabled {
Ian Wienand433a9b12015-10-07 13:29:31 +11002172 local xtrace
2173 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05002174 set +o xtrace
Ian Wienand8043bfa2015-10-14 14:53:18 +11002175
Sean Dague45917cc2014-02-24 16:09:14 -05002176 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002177 local services=$@
2178 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06002179 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05002180 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002181
2182 # Look for top-level 'enabled' function for this service
2183 if type is_${service}_enabled >/dev/null 2>&1; then
2184 # A function exists for this service, use it
Christian Schwede3db0aad2015-09-10 11:15:39 +00002185 is_${service}_enabled && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002186 fi
2187
2188 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
2189 # are implemented
2190
John Kasperski7b3ae532016-04-24 22:56:04 -05002191 [[ ${service} == n-cell-* && ,${ENABLED_SERVICES} =~ ,"n-cell" ]] && enabled=0
2192 [[ ${service} == n-cpu-* && ,${ENABLED_SERVICES} =~ ,"n-cpu" ]] && enabled=0
2193 [[ ${service} == "nova" && ,${ENABLED_SERVICES} =~ ,"n-" ]] && enabled=0
2194 [[ ${service} == "glance" && ,${ENABLED_SERVICES} =~ ,"g-" ]] && enabled=0
2195 [[ ${service} == "neutron" && ,${ENABLED_SERVICES} =~ ,"q-" ]] && enabled=0
2196 [[ ${service} == "trove" && ,${ENABLED_SERVICES} =~ ,"tr-" ]] && enabled=0
2197 [[ ${service} == "swift" && ,${ENABLED_SERVICES} =~ ,"s-" ]] && enabled=0
2198 [[ ${service} == s-* && ,${ENABLED_SERVICES} =~ ,"swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002199 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11002200
Sean Dague45917cc2014-02-24 16:09:14 -05002201 $xtrace
2202 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06002203}
2204
fumihiko kakuma8606c982015-04-13 09:55:06 +09002205# remove specified list from the input string
2206# remove_disabled_services service-list remove-list
2207function remove_disabled_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11002208 local xtrace
2209 xtrace=$(set +o | grep xtrace)
2210 set +o xtrace
2211
fumihiko kakuma8606c982015-04-13 09:55:06 +09002212 local service_list=$1
2213 local remove_list=$2
2214 local service
2215 local enabled=""
2216
2217 for service in ${service_list//,/ }; do
2218 local remove
2219 local add=1
2220 for remove in ${remove_list//,/ }; do
2221 if [[ ${remove} == ${service} ]]; then
2222 add=0
2223 break
2224 fi
2225 done
2226 if [[ $add == 1 ]]; then
2227 enabled="${enabled},$service"
2228 fi
2229 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11002230
2231 $xtrace
2232
fumihiko kakuma8606c982015-04-13 09:55:06 +09002233 _cleanup_service_list "$enabled"
2234}
2235
Dean Troyerdff49a22014-01-30 15:37:40 -06002236# Toggle enable/disable_service for services that must run exclusive of each other
2237# $1 The name of a variable containing a space-separated list of services
2238# $2 The name of a variable in which to store the enabled service's name
2239# $3 The name of the service to enable
2240function use_exclusive_service {
2241 local options=${!1}
2242 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002243 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06002244 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002245 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06002246 for opt in $options;do
2247 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
2248 done
2249 eval "$out=$selection"
2250 return 0
2251}
2252
Chris Dentc6d47012015-10-09 14:57:05 +00002253# Make sure that nothing has manipulated ENABLED_SERVICES in a way
2254# that conflicts with prior calls to disable_service.
2255# Uses global ``ENABLED_SERVICES``
2256function verify_disabled_services {
2257 local service
2258 for service in ${ENABLED_SERVICES//,/ }; do
2259 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
2260 die $LINENO "ENABLED_SERVICES directly modified to overcome 'disable_service ${service}'"
2261 fi
2262 done
2263}
2264
Dean Troyerdff49a22014-01-30 15:37:40 -06002265
Masayuki Igawaf6368d32014-02-20 13:31:26 +09002266# System Functions
2267# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06002268
2269# Only run the command if the target file (the last arg) is not on an
2270# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002271function _safe_permission_operation {
Ian Wienand433a9b12015-10-07 13:29:31 +11002272 local xtrace
2273 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05002274 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002275 local args=( $@ )
2276 local last
2277 local sudo_cmd
2278 local dir_to_check
2279
2280 let last="${#args[*]} - 1"
2281
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002282 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002283 if [ ! -d "$dir_to_check" ]; then
2284 dir_to_check=`dirname "$dir_to_check"`
2285 fi
2286
2287 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002288 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002289 return 0
2290 fi
2291
2292 if [[ $TRACK_DEPENDS = True ]]; then
2293 sudo_cmd="env"
2294 else
2295 sudo_cmd="sudo"
2296 fi
2297
Sean Dague45917cc2014-02-24 16:09:14 -05002298 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002299 $sudo_cmd $@
2300}
2301
2302# Exit 0 if address is in network or 1 if address is not in network
2303# ip-range is in CIDR notation: 1.2.3.4/20
2304# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002305function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002306 local ip=$1
2307 local range=$2
2308 local masklen=${range#*/}
Ian Wienandada886d2015-10-07 14:06:26 +11002309 local network
2310 network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2311 local subnet
2312 subnet=$(maskip $ip $(cidr2netmask $masklen))
Dean Troyerdff49a22014-01-30 15:37:40 -06002313 [[ $network == $subnet ]]
2314}
2315
2316# Add a user to a group.
2317# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002318function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002319 local user=$1
2320 local group=$2
2321
Thomas Bechtolda8580852015-05-31 00:04:33 +02002322 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06002323}
2324
2325# Convert CIDR notation to a IPv4 netmask
2326# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002327function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002328 local maskpat="255 255 255 255"
2329 local maskdgt="254 252 248 240 224 192 128"
2330 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2331 echo ${1-0}.${2-0}.${3-0}.${4-0}
2332}
2333
Ian Cordasco69e3c0a2016-09-26 12:53:14 -05002334# Check if this is a valid ipv4 address string
2335function is_ipv4_address {
2336 local address=$1
2337 local regex='([0-9]{1,3}.){3}[0-9]{1,3}'
2338 # TODO(clarkb) make this more robust
2339 if [[ "$address" =~ $regex ]] ; then
2340 return 0
2341 else
2342 return 1
2343 fi
2344}
2345
Dean Troyerdff49a22014-01-30 15:37:40 -06002346# Gracefully cp only if source file/dir exists
2347# cp_it source destination
2348function cp_it {
2349 if [ -e $1 ] || [ -d $1 ]; then
2350 cp -pRL $1 $2
2351 fi
2352}
2353
2354# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2355# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2356# ``localrc`` or on the command line if necessary::
2357#
2358# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2359#
2360# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2361
Ian Wienandaee18c72014-02-21 15:35:08 +11002362function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05002363 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002364 export http_proxy=$http_proxy
2365 fi
Sean Dague53753292014-12-04 19:38:15 -05002366 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002367 export https_proxy=$https_proxy
2368 fi
Sean Dague53753292014-12-04 19:38:15 -05002369 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002370 export no_proxy=$no_proxy
2371 fi
2372}
2373
2374# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002375function is_nfs_directory {
Ian Wienandada886d2015-10-07 14:06:26 +11002376 local mount_type
2377 mount_type=`stat -f -L -c %T $1`
Dean Troyerdff49a22014-01-30 15:37:40 -06002378 test "$mount_type" == "nfs"
2379}
2380
2381# Return the network portion of the given IP address using netmask
2382# netmask is in the traditional dotted-quad format
2383# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002384function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002385 local ip=$1
2386 local mask=$2
2387 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
Ian Wienandada886d2015-10-07 14:06:26 +11002388 local subnet
2389 subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
Dean Troyerdff49a22014-01-30 15:37:40 -06002390 echo $subnet
2391}
2392
Michael Turek7938d832016-04-12 14:55:21 -04002393function is_provider_network {
2394 if [ "$Q_USE_PROVIDER_NETWORKING" == "True" ]; then
2395 return 0
2396 fi
2397 return 1
2398}
2399
2400
Chris Dent3a2c86a2015-05-12 13:41:25 +00002401# Return the current python as "python<major>.<minor>"
2402function python_version {
Ian Wienandada886d2015-10-07 14:06:26 +11002403 local python_version
2404 python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
Chris Dent3a2c86a2015-05-12 13:41:25 +00002405 echo "python${python_version}"
2406}
2407
Dean Troyerdff49a22014-01-30 15:37:40 -06002408# Service wrapper to restart services
2409# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002410function restart_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002411 if [ -x /bin/systemctl ]; then
2412 sudo /bin/systemctl restart $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002413 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002414 sudo service $1 restart
Dean Troyerdff49a22014-01-30 15:37:40 -06002415 fi
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002416
Dean Troyerdff49a22014-01-30 15:37:40 -06002417}
2418
2419# Only change permissions of a file or directory if it is not on an
2420# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002421function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002422 _safe_permission_operation chmod $@
2423}
2424
2425# Only change ownership of a file or directory if it is not on an NFS
2426# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002427function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002428 _safe_permission_operation chown $@
2429}
2430
2431# Service wrapper to start services
2432# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002433function start_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002434 if [ -x /bin/systemctl ]; then
2435 sudo /bin/systemctl start $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002436 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002437 sudo service $1 start
Dean Troyerdff49a22014-01-30 15:37:40 -06002438 fi
2439}
2440
2441# Service wrapper to stop services
2442# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002443function stop_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002444 if [ -x /bin/systemctl ]; then
2445 sudo /bin/systemctl stop $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002446 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002447 sudo service $1 stop
Dean Troyerdff49a22014-01-30 15:37:40 -06002448 fi
2449}
2450
Attila Fazekasbcaadd62016-11-23 12:43:02 +01002451# Service wrapper to reload services
2452# If the service was not in running state it will start it
Gregory Haynes4b49e402016-08-31 18:19:51 -07002453# reload_service service-name
2454function reload_service {
2455 if [ -x /bin/systemctl ]; then
Attila Fazekasbcaadd62016-11-23 12:43:02 +01002456 sudo /bin/systemctl reload-or-restart $1
Gregory Haynes4b49e402016-08-31 18:19:51 -07002457 else
2458 sudo service $1 reload
2459 fi
2460}
2461
Sean Dague442e4e92015-06-24 13:24:02 -04002462# Test with a finite retry loop.
2463#
2464function test_with_retry {
2465 local testcmd=$1
2466 local failmsg=$2
2467 local until=${3:-10}
2468 local sleep=${4:-0.5}
2469
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002470 time_start "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002471 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2472 die $LINENO "$failmsg"
2473 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002474 time_stop "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002475}
2476
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00002477# Like sudo but forwarding http_proxy https_proxy no_proxy environment vars.
2478# If it is run as superuser then sudo is replaced by env.
2479#
2480function sudo_with_proxies {
2481 local sudo
2482
2483 [[ "$(id -u)" = "0" ]] && sudo="env" || sudo="sudo"
2484
2485 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}"\
2486 no_proxy="${no_proxy:-}" "$@"
2487}
2488
Sean Dague95c33d52015-10-07 11:05:59 -04002489# Timing infrastructure - figure out where large blocks of time are
2490# used in DevStack
2491#
2492# The timing infrastructure for DevStack is about collecting buckets
2493# of time that are spend in some subtask. For instance, that might be
2494# 'apt', 'pip', 'osc', even database migrations. We do this by a pair
2495# of functions: time_start / time_stop.
2496#
2497# These take a single parameter: $name - which specifies the name of
2498# the bucket to be accounted against. time_totals function spits out
2499# the results.
2500#
2501# Resolution is only in whole seconds, so should be used for long
2502# running activities.
2503
Sean Dagueafef8bf2017-03-06 14:07:23 -05002504declare -A -g _TIME_TOTAL
2505declare -A -g _TIME_START
2506declare -r -g _TIME_BEGIN=$(date +%s)
Sean Dague95c33d52015-10-07 11:05:59 -04002507
2508# time_start $name
2509#
2510# starts the clock for a timer by name. Errors if that clock is
2511# already started.
2512function time_start {
2513 local name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002514 local start_time=${_TIME_START[$name]}
Sean Dague95c33d52015-10-07 11:05:59 -04002515 if [[ -n "$start_time" ]]; then
2516 die $LINENO "Trying to start the clock on $name, but it's already been started"
2517 fi
Sean Dague85cf2932017-03-27 15:35:13 -04002518 _TIME_START[$name]=$(date +%s%3N)
Sean Dague95c33d52015-10-07 11:05:59 -04002519}
2520
2521# time_stop $name
2522#
2523# stops the clock for a timer by name, and accumulate that time in the
2524# global counter for that name. Errors if that clock had not
2525# previously been started.
2526function time_stop {
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002527 local name
2528 local end_time
Rob Crittenden38245da2016-05-26 17:55:14 -04002529 local elapsed_time
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002530 local total
2531 local start_time
2532
2533 name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002534 start_time=${_TIME_START[$name]}
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002535
Sean Dague95c33d52015-10-07 11:05:59 -04002536 if [[ -z "$start_time" ]]; then
2537 die $LINENO "Trying to stop the clock on $name, but it was never started"
2538 fi
Sean Dague85cf2932017-03-27 15:35:13 -04002539 end_time=$(date +%s%3N)
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002540 elapsed_time=$(($end_time - $start_time))
Ian Wienand908a3a92016-03-29 14:47:09 +11002541 total=${_TIME_TOTAL[$name]:-0}
Sean Dague95c33d52015-10-07 11:05:59 -04002542 # reset the clock so we can start it in the future
Ian Wienand908a3a92016-03-29 14:47:09 +11002543 _TIME_START[$name]=""
2544 _TIME_TOTAL[$name]=$(($total + $elapsed_time))
Sean Dague95c33d52015-10-07 11:05:59 -04002545}
2546
Sean Dague85cf2932017-03-27 15:35:13 -04002547function oscwrap {
2548 local out
2549 local rc
2550 local start
2551 local end
2552 # Cannot use timer_start and timer_stop as we run in subshells
2553 # and those rely on modifying vars in the same process (which cannot
2554 # happen from a subshell.
2555 start=$(date +%s%3N)
2556 out=$(command openstack "$@")
2557 rc=$?
2558 end=$(date +%s%3N)
2559 echo $((end - start)) >> $OSCWRAP_TIMER_FILE
2560
2561 echo "$out"
2562 return $rc
2563}
2564
2565function install_oscwrap {
2566 # File to accumulate our timing data
2567 OSCWRAP_TIMER_FILE=$(mktemp)
2568 # Bash by default doesn't expand aliases, allow it for the aliases
2569 # we want to whitelist.
2570 shopt -s expand_aliases
2571 # Remove all aliases that might be expanded to preserve old unexpanded
2572 # behavior
2573 unalias -a
2574 # Add only the alias we want for openstack
2575 alias openstack=oscwrap
2576}
2577
2578function cleanup_oscwrap {
2579 local total=0
2580 if python3_enabled ; then
2581 local python=python3
2582 else
2583 local python=python
2584 fi
2585 total=$(cat $OSCWRAP_TIMER_FILE | $python -c "import sys; print(sum(int(l) for l in sys.stdin))")
2586 _TIME_TOTAL["osc"]=$total
2587 rm $OSCWRAP_TIMER_FILE
2588}
2589
Sean Dague95c33d52015-10-07 11:05:59 -04002590# time_totals
Ian Wienand908a3a92016-03-29 14:47:09 +11002591# Print out total time summary
Sean Dague95c33d52015-10-07 11:05:59 -04002592function time_totals {
Ian Wienand908a3a92016-03-29 14:47:09 +11002593 local elapsed_time
2594 local end_time
2595 local len=15
2596 local xtrace
2597
2598 end_time=$(date +%s)
2599 elapsed_time=$(($end_time - $_TIME_BEGIN))
2600
2601 # pad 1st column this far
2602 for t in ${!_TIME_TOTAL[*]}; do
2603 if [[ ${#t} -gt $len ]]; then
2604 len=${#t}
2605 fi
Sean Dague95c33d52015-10-07 11:05:59 -04002606 done
Ian Wienand908a3a92016-03-29 14:47:09 +11002607
Sean Dague85cf2932017-03-27 15:35:13 -04002608 cleanup_oscwrap
2609
Ian Wienand908a3a92016-03-29 14:47:09 +11002610 xtrace=$(set +o | grep xtrace)
2611 set +o xtrace
2612
2613 echo
2614 echo "========================="
2615 echo "DevStack Component Timing"
2616 echo "========================="
2617 printf "%-${len}s %3d\n" "Total runtime" "$elapsed_time"
2618 echo
2619 for t in ${!_TIME_TOTAL[*]}; do
2620 local v=${_TIME_TOTAL[$t]}
Sean Dague85cf2932017-03-27 15:35:13 -04002621 # because we're recording in milliseconds
2622 v=$(($v / 1000))
Ian Wienand908a3a92016-03-29 14:47:09 +11002623 printf "%-${len}s %3d\n" "$t" "$v"
2624 done
2625 echo "========================="
2626
2627 $xtrace
Sean Dague95c33d52015-10-07 11:05:59 -04002628}
Dean Troyerdff49a22014-01-30 15:37:40 -06002629
2630# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +11002631$_XTRACE_FUNCTIONS_COMMON
Dean Troyerdff49a22014-01-30 15:37:40 -06002632
2633# Local variables:
2634# mode: shell-script
2635# End: