blob: 7042408f40d1cd48492a7ffac046ac5266a77b72 [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# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050031#
Dean Troyerdff49a22014-01-30 15:37:40 -060032
33# Save trace setting
Ian Wienand523f4882015-10-13 11:03:03 +110034_XTRACE_FUNCTIONS_COMMON=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -060035set +o xtrace
36
Ian Wienand4ffb4542015-06-30 11:00:32 +100037# ensure we don't re-source this in the same environment
38[[ -z "$_DEVSTACK_FUNCTIONS_COMMON" ]] || return 0
Sean Dagueafef8bf2017-03-06 14:07:23 -050039declare -r -g _DEVSTACK_FUNCTIONS_COMMON=1
Ian Wienand4ffb4542015-06-30 11:00:32 +100040
Sean Daguecc524062014-10-01 09:06:43 -040041# Global Config Variables
Sean Dagueafef8bf2017-03-06 14:07:23 -050042declare -A -g GITREPO
43declare -A -g GITBRANCH
44declare -A -g GITDIR
Sean Daguecc524062014-10-01 09:06:43 -040045
Dinesh Bhoref60f2b2017-09-05 14:40:32 +053046KILL_PATH="$(which kill)"
Sean Dague53753292014-12-04 19:38:15 -050047
Dean Troyer68162342015-05-13 15:41:03 -050048# Save these variables to .stackenv
49STACK_ENV_VARS="BASE_SQL_CONN DATA_DIR DEST ENABLED_SERVICES HOST_IP \
Jens Harbott32c00892019-04-10 10:33:39 +000050 KEYSTONE_SERVICE_URI \
Sean Dague0effa1a2017-04-18 15:16:37 -040051 LOGFILE OS_CACERT SERVICE_HOST STACK_USER TLS_IP \
Brian Haley5d04db22015-06-16 13:14:31 -040052 HOST_IPV6 SERVICE_IP_VERSION"
Dean Troyer68162342015-05-13 15:41:03 -050053
54
55# Saves significant environment variables to .stackenv for later use
56# Refers to a lot of globals, only TOP_DIR and STACK_ENV_VARS are required to
57# function, the rest are simply saved and do not cause problems if they are undefined.
58# save_stackenv [tag]
59function save_stackenv {
60 local tag=${1:-""}
61 # Save some values we generated for later use
62 time_stamp=$(date "+$TIMESTAMP_FORMAT")
63 echo "# $time_stamp $tag" >$TOP_DIR/.stackenv
64 for i in $STACK_ENV_VARS; do
65 echo $i=${!i} >>$TOP_DIR/.stackenv
66 done
67}
Dean Troyerdff49a22014-01-30 15:37:40 -060068
Monty Taylor7224eec2015-09-19 11:26:18 -040069# Update/create user clouds.yaml file.
70# clouds.yaml will have
71# - A `devstack` entry for the `demo` user for the `demo` project.
72# - A `devstack-admin` entry for the `admin` user for the `admin` project.
73# write_clouds_yaml
74function write_clouds_yaml {
Monty Tayloree9bb762015-10-19 15:16:18 -040075 # The location is a variable to allow for easier refactoring later to make it
76 # overridable. There is currently no usecase where doing so makes sense, so
77 # it's not currently configurable.
Monty Taylor7224eec2015-09-19 11:26:18 -040078
Monty Tayloree9bb762015-10-19 15:16:18 -040079 CLOUDS_YAML=/etc/openstack/clouds.yaml
80
81 sudo mkdir -p $(dirname $CLOUDS_YAML)
Lenny Verkhovsky313ddae2015-10-20 11:26:34 +030082 sudo chown -R $STACK_USER /etc/openstack
Lenny Verkhovsky313ddae2015-10-20 11:26:34 +030083
Monty Tayloree9bb762015-10-19 15:16:18 -040084 CA_CERT_ARG=''
85 if [ -f "$SSL_BUNDLE_FILE" ]; then
86 CA_CERT_ARG="--os-cacert $SSL_BUNDLE_FILE"
87 fi
Lance Bragstad9c813212021-03-11 16:29:31 +000088 # devstack: user with the member role on demo project
Davanum Srinivas51ecf0a2017-01-05 16:11:17 -050089 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
Monty Tayloree9bb762015-10-19 15:16:18 -040090 --file $CLOUDS_YAML \
91 --os-cloud devstack \
92 --os-region-name $REGION_NAME \
Monty Tayloree9bb762015-10-19 15:16:18 -040093 $CA_CERT_ARG \
Monty Taylore43f60b2017-04-27 18:27:36 -050094 --os-auth-url $KEYSTONE_SERVICE_URI \
Monty Tayloree9bb762015-10-19 15:16:18 -040095 --os-username demo \
96 --os-password $ADMIN_PASSWORD \
97 --os-project-name demo
Sean Daguec67d22e2016-02-02 05:51:14 -050098
Lance Bragstad9c813212021-03-11 16:29:31 +000099 # devstack-admin: user with the admin role on the admin project
Davanum Srinivas51ecf0a2017-01-05 16:11:17 -0500100 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
Monty Tayloree9bb762015-10-19 15:16:18 -0400101 --file $CLOUDS_YAML \
102 --os-cloud devstack-admin \
103 --os-region-name $REGION_NAME \
Monty Tayloree9bb762015-10-19 15:16:18 -0400104 $CA_CERT_ARG \
Monty Taylore43f60b2017-04-27 18:27:36 -0500105 --os-auth-url $KEYSTONE_SERVICE_URI \
Monty Tayloree9bb762015-10-19 15:16:18 -0400106 --os-username admin \
107 --os-password $ADMIN_PASSWORD \
108 --os-project-name admin
Monty Taylor74379df2016-01-21 10:20:08 -0500109
Lance Bragstad9c813212021-03-11 16:29:31 +0000110 # devstack-alt: user with the member role on alt_demo project
111 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
112 --file $CLOUDS_YAML \
113 --os-cloud devstack-alt \
114 --os-region-name $REGION_NAME \
115 $CA_CERT_ARG \
116 --os-auth-url $KEYSTONE_SERVICE_URI \
117 --os-username alt_demo \
118 --os-password $ADMIN_PASSWORD \
119 --os-project-name alt_demo
120
121 # devstack-alt-member: user with the member role on alt_demo project
122 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
123 --file $CLOUDS_YAML \
124 --os-cloud devstack-alt-member \
125 --os-region-name $REGION_NAME \
126 $CA_CERT_ARG \
127 --os-auth-url $KEYSTONE_SERVICE_URI \
128 --os-username alt_demo_member \
129 --os-password $ADMIN_PASSWORD \
130 --os-project-name alt_demo
131
132 # devstack-alt-reader: user with the reader role on alt_demo project
133 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
134 --file $CLOUDS_YAML \
135 --os-cloud devstack-alt-reader \
136 --os-region-name $REGION_NAME \
137 $CA_CERT_ARG \
138 --os-auth-url $KEYSTONE_SERVICE_URI \
139 --os-username alt_demo_reader \
140 --os-password $ADMIN_PASSWORD \
141 --os-project-name alt_demo
142
143 # devstack-reader: user with the reader role on demo project
144 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
145 --file $CLOUDS_YAML \
146 --os-cloud devstack-reader \
147 --os-region-name $REGION_NAME \
148 $CA_CERT_ARG \
149 --os-auth-url $KEYSTONE_SERVICE_URI \
150 --os-username demo_reader \
151 --os-password $ADMIN_PASSWORD \
152 --os-project-name demo
153
154 # devstack-system-admin: user with the admin role on the system
Monty Taylor56905822019-01-08 15:29:16 +0000155 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
156 --file $CLOUDS_YAML \
157 --os-cloud devstack-system-admin \
158 --os-region-name $REGION_NAME \
159 $CA_CERT_ARG \
160 --os-auth-url $KEYSTONE_SERVICE_URI \
161 --os-username admin \
162 --os-password $ADMIN_PASSWORD \
163 --os-system-scope all
164
Lance Bragstad9c813212021-03-11 16:29:31 +0000165 # devstack-system-member: user with the member role on the system
Lance Bragstad021ae0b2021-03-11 15:47:50 +0000166 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
167 --file $CLOUDS_YAML \
168 --os-cloud devstack-system-member \
169 --os-region-name $REGION_NAME \
170 $CA_CERT_ARG \
171 --os-auth-url $KEYSTONE_SERVICE_URI \
172 --os-username system_member \
173 --os-password $ADMIN_PASSWORD \
174 --os-system-scope all
175
Lance Bragstad9c813212021-03-11 16:29:31 +0000176 # devstack-system-reader: user with the reader role on the system
Lance Bragstad021ae0b2021-03-11 15:47:50 +0000177 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
178 --file $CLOUDS_YAML \
179 --os-cloud devstack-system-reader \
180 --os-region-name $REGION_NAME \
181 $CA_CERT_ARG \
182 --os-auth-url $KEYSTONE_SERVICE_URI \
183 --os-username system_reader \
184 --os-password $ADMIN_PASSWORD \
185 --os-system-scope all
186
Maciej Józefczyk7a0fa4f2020-03-05 16:55:50 +0100187 cat >> $CLOUDS_YAML <<EOF
188functional:
189 image_name: $DEFAULT_IMAGE_NAME
190EOF
191
Monty Taylor74379df2016-01-21 10:20:08 -0500192 # CLean up any old clouds.yaml files we had laying around
ZhiQiang Fan0b4a0092016-04-12 20:26:33 +0800193 rm -f $(eval echo ~"$STACK_USER")/.config/openstack/clouds.yaml
Monty Taylor7224eec2015-09-19 11:26:18 -0400194}
195
Ian Wienande82bac02015-08-25 14:29:08 +1000196# trueorfalse <True|False> <VAR>
197#
198# Normalize config-value provided in variable VAR to either "True" or
199# "False". If VAR is unset (i.e. $VAR evaluates as empty), the value
200# of the second argument will be used as the default value.
201#
202# Accepts as False: 0 no No NO false False FALSE
203# Accepts as True: 1 yes Yes YES true True TRUE
204#
205# usage:
206# VAL=$(trueorfalse False VAL)
Ian Wienandaee18c72014-02-21 15:35:08 +1100207function trueorfalse {
Ian Wienand433a9b12015-10-07 13:29:31 +1100208 local xtrace
209 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -0500210 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600211
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900212 local default=$1
Ian Wienande82bac02015-08-25 14:29:08 +1000213
214 if [ -z $2 ]; then
215 die $LINENO "variable to normalize required"
216 fi
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900217 local testval=${!2:-}
218
219 case "$testval" in
220 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
221 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
222 * ) echo "$default" ;;
223 esac
224
Sean Dague45917cc2014-02-24 16:09:14 -0500225 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600226}
227
Attila Fazekas1bd79592015-02-24 14:06:56 +0100228function isset {
229 [[ -v "$1" ]]
230}
Dean Troyerdff49a22014-01-30 15:37:40 -0600231
Dean Troyer68162342015-05-13 15:41:03 -0500232
Dean Troyerdff49a22014-01-30 15:37:40 -0600233# Control Functions
234# =================
235
236# Prints backtrace info
237# filename:lineno:function
238# backtrace level
239function backtrace {
240 local level=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100241 local deep
242 deep=$((${#BASH_SOURCE[@]} - 1))
Dean Troyerdff49a22014-01-30 15:37:40 -0600243 echo "[Call Trace]"
244 while [ $level -le $deep ]; do
245 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
246 deep=$((deep - 1))
247 done
248}
249
250# Prints line number and "message" then exits
251# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100252function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600253 local exitcode=$?
254 set +o xtrace
255 local line=$1; shift
256 if [ $exitcode == 0 ]; then
257 exitcode=1
258 fi
259 backtrace 2
260 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600261 # Give buffers a second to flush
262 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600263 exit $exitcode
264}
265
266# Checks an environment variable is not set or has length 0 OR if the
267# exit code is non-zero and prints "message" and exits
268# NOTE: env-var is the variable name without a '$'
269# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100270function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600271 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100272 local xtrace
273 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600274 set +o xtrace
275 local line=$1; shift
276 local evar=$1; shift
277 if ! is_set $evar || [ $exitcode != 0 ]; then
278 die $line "$*"
279 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500280 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600281}
282
Sean Dague1de9e332015-10-07 08:46:13 -0400283function deprecated {
284 local text=$1
285 DEPRECATED_TEXT+="\n$text"
YAMAMOTO Takashi8b1bbd62016-12-01 22:29:12 +0900286 echo "WARNING: $text" >&2
Sean Dague1de9e332015-10-07 08:46:13 -0400287}
288
Dean Troyerdff49a22014-01-30 15:37:40 -0600289# Prints line number and "message" in error format
290# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100291function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600292 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100293 local xtrace
294 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600295 set +o xtrace
296 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
Ian Wienande8a6a022018-10-08 15:20:34 +1100297 echo "$msg" 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600298 if [[ -n ${LOGDIR} ]]; then
Ian Wienande8a6a022018-10-08 15:20:34 +1100299 echo "$msg" >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600300 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500301 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600302 return $exitcode
303}
304
305# Checks an environment variable is not set or has length 0 OR if the
306# exit code is non-zero and prints "message"
307# NOTE: env-var is the variable name without a '$'
308# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100309function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600310 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100311 local xtrace
312 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600313 set +o xtrace
314 local line=$1; shift
315 local evar=$1; shift
316 if ! is_set $evar || [ $exitcode != 0 ]; then
317 err $line "$*"
318 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500319 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600320 return $exitcode
321}
322
323# Exit after outputting a message about the distribution not being supported.
324# exit_distro_not_supported [optional-string-telling-what-is-missing]
325function exit_distro_not_supported {
326 if [[ -z "$DISTRO" ]]; then
327 GetDistro
328 fi
329
330 if [ $# -gt 0 ]; then
331 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
332 else
333 die $LINENO "Support for $DISTRO is incomplete."
334 fi
335}
336
337# Test if the named environment variable is set and not zero length
338# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100339function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600340 local var=\$"$1"
341 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
342}
343
344# Prints line number and "message" in warning format
345# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100346function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600347 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100348 local xtrace
349 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600350 set +o xtrace
351 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Ian Wienande8a6a022018-10-08 15:20:34 +1100352 echo "$msg"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500353 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600354 return $exitcode
355}
356
357
358# Distro Functions
359# ================
360
361# Determine OS Vendor, Release and Update
Ian Wienand7710e7f2014-08-27 16:15:32 +1000362
363#
364# NOTE : For portability, you almost certainly do not want to use
365# these variables directly! The "is_*" functions defined below this
366# bundle up compatible platforms under larger umbrellas that we have
367# determinted are compatible enough (e.g. is_ubuntu covers Ubuntu &
368# Debian, is_fedora covers RPM-based distros). Higher-level functions
369# such as "install_package" further abstract things in better ways.
370#
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500371# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
Matt Riedemannff10ac32017-02-13 12:44:24 -0500372# ``os_RELEASE`` - major release: ``16.04`` (Ubuntu), ``23`` (Fedora)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500373# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
Matt Riedemannff10ac32017-02-13 12:44:24 -0500374# ``os_CODENAME`` - vendor's codename for release: ``xenial``
Ian Wienand7710e7f2014-08-27 16:15:32 +1000375
Sean Dagueafef8bf2017-03-06 14:07:23 -0500376declare -g os_VENDOR os_RELEASE os_PACKAGE os_CODENAME
Ian Wienand7710e7f2014-08-27 16:15:32 +1000377
378# Make a *best effort* attempt to install lsb_release packages for the
379# user if not available. Note can't use generic install_package*
380# because they depend on this!
381function _ensure_lsb_release {
Thomas Bechtolddab39012016-03-03 12:11:15 +0100382 if [[ -x $(command -v lsb_release 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000383 return
384 fi
385
Thomas Bechtolddab39012016-03-03 12:11:15 +0100386 if [[ -x $(command -v apt-get 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000387 sudo apt-get install -y lsb-release
Thomas Bechtolddab39012016-03-03 12:11:15 +0100388 elif [[ -x $(command -v zypper 2>/dev/null) ]]; then
Dirk Mueller5ef8a122017-09-25 13:08:51 +0200389 sudo zypper -n install lsb-release
Thomas Bechtolddab39012016-03-03 12:11:15 +0100390 elif [[ -x $(command -v dnf 2>/dev/null) ]]; then
Kevin Zhao7880ba62021-03-31 04:58:28 +0000391 sudo dnf install -y redhat-lsb-core || sudo dnf install -y openeuler-lsb
Ian Wienand7710e7f2014-08-27 16:15:32 +1000392 else
393 die $LINENO "Unable to find or auto-install lsb_release"
394 fi
395}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500396
Dean Troyerdff49a22014-01-30 15:37:40 -0600397# GetOSVersion
Ian Wienand7710e7f2014-08-27 16:15:32 +1000398# Set the following variables:
399# - os_RELEASE
400# - os_CODENAME
401# - os_VENDOR
402# - os_PACKAGE
Ian Wienandaee18c72014-02-21 15:35:08 +1100403function GetOSVersion {
Alfredo Moralejo5ea4c3c2021-11-16 15:13:03 +0100404 # CentOS Stream 9 does not provide lsb_release
405 source /etc/os-release
406 if [[ "${ID}${VERSION}" == "centos9" ]]; then
407 os_RELEASE=${VERSION_ID}
408 os_CODENAME="n/a"
409 os_VENDOR=$(echo $NAME | tr -d '[:space:]')
410 else
411 _ensure_lsb_release
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500412
Alfredo Moralejo5ea4c3c2021-11-16 15:13:03 +0100413 os_RELEASE=$(lsb_release -r -s)
414 os_CODENAME=$(lsb_release -c -s)
415 os_VENDOR=$(lsb_release -i -s)
416 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600417
Ian Wienand7710e7f2014-08-27 16:15:32 +1000418 if [[ $os_VENDOR =~ (Debian|Ubuntu|LinuxMint) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600419 os_PACKAGE="deb"
Ian Wienand7710e7f2014-08-27 16:15:32 +1000420 else
421 os_PACKAGE="rpm"
Dean Troyerdff49a22014-01-30 15:37:40 -0600422 fi
Ian Wienand7710e7f2014-08-27 16:15:32 +1000423
424 typeset -xr os_VENDOR
425 typeset -xr os_RELEASE
426 typeset -xr os_PACKAGE
427 typeset -xr os_CODENAME
Dean Troyerdff49a22014-01-30 15:37:40 -0600428}
429
430# Translate the OS version values into common nomenclature
431# Sets global ``DISTRO`` from the ``os_*`` values
Sean Dagueafef8bf2017-03-06 14:07:23 -0500432declare -g DISTRO
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500433
Ian Wienandaee18c72014-02-21 15:35:08 +1100434function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600435 GetOSVersion
ptoohill196006652016-02-15 16:07:50 -0600436 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) || \
437 "$os_VENDOR" =~ (LinuxMint) ]]; then
438 # 'Everyone' refers to Ubuntu / Debian / Mint releases by
Ian Wienand7710e7f2014-08-27 16:15:32 +1000439 # the code name adjective
Dean Troyerdff49a22014-01-30 15:37:40 -0600440 DISTRO=$os_CODENAME
441 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
442 # For Fedora, just use 'f' and the release
443 DISTRO="f$os_RELEASE"
Adam Spiersb6f04ca2019-01-23 18:55:16 +0000444 elif is_opensuse; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600445 DISTRO="opensuse-$os_RELEASE"
Dirk Mueller4404f682018-03-02 00:37:58 +0100446 # Tumbleweed uses "n/a" as a codename, and the release is a datestring
Dirk Mueller297a50a2018-06-20 11:08:54 +0200447 # like 20180218, so not very useful. Leap however uses a release
448 # with a "dot", so for example 15.0
449 [ "$os_CODENAME" = "n/a" -a "$os_RELEASE" = "${os_RELEASE/\./}" ] && \
450 DISTRO="opensuse-tumbleweed"
Adam Spiersb6f04ca2019-01-23 18:55:16 +0000451 elif is_suse_linux_enterprise; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000452 # just use major release
453 DISTRO="sle${os_RELEASE%.*}"
454 elif [[ "$os_VENDOR" =~ (Red.*Hat) || \
anju Tiwari6c639c92014-07-15 18:11:54 +0530455 "$os_VENDOR" =~ (CentOS) || \
zenkuro6f4eafb2021-07-15 19:24:28 +0300456 "$os_VENDOR" =~ (AlmaLinux) || \
Mike Trimm1da4e792016-04-27 11:40:19 -0500457 "$os_VENDOR" =~ (Scientific) || \
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300458 "$os_VENDOR" =~ (OracleServer) || \
Evgeny Antyshev718512c2016-03-03 14:47:58 +0000459 "$os_VENDOR" =~ (Virtuozzo) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600460 # Drop the . release as we assume it's compatible
Ian Wienand7710e7f2014-08-27 16:15:32 +1000461 # XXX re-evaluate when we get RHEL10
Dean Troyerdff49a22014-01-30 15:37:40 -0600462 DISTRO="rhel${os_RELEASE::1}"
Kevin Zhao7880ba62021-03-31 04:58:28 +0000463 elif [[ "$os_VENDOR" =~ (openEuler) ]]; then
464 # The DISTRO here is `openEuler-20.03`. While, actually only openEuler
465 # 20.03 LTS SP2 is fully tested. Other SP version maybe have bugs.
466 DISTRO="openEuler-$os_RELEASE"
Dean Troyerdff49a22014-01-30 15:37:40 -0600467 else
Ian Wienanded92e432016-02-16 10:56:56 +1100468 # We can't make a good choice here. Setting a sensible DISTRO
469 # is part of the problem, but not the major issue -- we really
470 # only use DISTRO in the code as a fine-filter.
471 #
472 # The bigger problem is categorising the system into one of
473 # our two big categories as Ubuntu/Debian-ish or
474 # Fedora/CentOS-ish.
475 #
476 # The setting of os_PACKAGE above is only set to "deb" based
477 # on a hard-coded list of vendor names ... thus we will
478 # default to thinking unknown distros are RPM based
479 # (ie. is_ubuntu does not match). But the platform will then
480 # also not match in is_fedora, because that also has a list of
481 # names.
482 #
483 # So, if you are reading this, getting your distro supported
484 # is really about making sure it matches correctly in these
485 # functions. Then you can choose a sensible way to construct
486 # DISTRO based on your distros release approach.
487 die $LINENO "Unable to determine DISTRO, can not continue."
Dean Troyerdff49a22014-01-30 15:37:40 -0600488 fi
Ian Wienand7710e7f2014-08-27 16:15:32 +1000489 typeset -xr DISTRO
Dean Troyerdff49a22014-01-30 15:37:40 -0600490}
491
492# Utility function for checking machine architecture
493# is_arch arch-type
494function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500495 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600496}
497
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700498# Determine if current distribution is an Oracle distribution
499# is_oraclelinux
500function is_oraclelinux {
501 if [[ -z "$os_VENDOR" ]]; then
502 GetOSVersion
503 fi
504
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300505 [ "$os_VENDOR" = "OracleServer" ]
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700506}
507
508
Dean Troyerdff49a22014-01-30 15:37:40 -0600509# Determine if current distribution is a Fedora-based distribution
510# (Fedora, RHEL, CentOS, etc).
511# is_fedora
512function is_fedora {
513 if [[ -z "$os_VENDOR" ]]; then
514 GetOSVersion
515 fi
516
anju Tiwari6c639c92014-07-15 18:11:54 +0530517 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Kevin Zhao7880ba62021-03-31 04:58:28 +0000518 [ "$os_VENDOR" = "openEuler" ] || \
tengqm78d37392016-03-15 23:08:00 -0400519 [ "$os_VENDOR" = "RedHatEnterpriseServer" ] || \
Carlos Goncalves587e0a32020-08-01 21:47:55 +0200520 [ "$os_VENDOR" = "RedHatEnterprise" ] || \
Sean Mooneye7c017b2020-12-09 23:53:12 +0000521 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "CentOSStream" ] || \
zenkuro6f4eafb2021-07-15 19:24:28 +0300522 [ "$os_VENDOR" = "AlmaLinux" ] || \
Sean Mooneye7c017b2020-12-09 23:53:12 +0000523 [ "$os_VENDOR" = "OracleServer" ] || [ "$os_VENDOR" = "Virtuozzo" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600524}
525
526
527# Determine if current distribution is a SUSE-based distribution
528# (openSUSE, SLE).
529# is_suse
530function is_suse {
Adam Spiersb6f04ca2019-01-23 18:55:16 +0000531 is_opensuse || is_suse_linux_enterprise
532}
533
534
535# Determine if current distribution is an openSUSE distribution
536# is_opensuse
537function is_opensuse {
Dean Troyerdff49a22014-01-30 15:37:40 -0600538 if [[ -z "$os_VENDOR" ]]; then
539 GetOSVersion
540 fi
541
Adam Spiersb6f04ca2019-01-23 18:55:16 +0000542 [[ "$os_VENDOR" =~ (openSUSE) ]]
543}
544
545
546# Determine if current distribution is a SUSE Linux Enterprise (SLE)
547# distribution
548# is_suse_linux_enterprise
549function is_suse_linux_enterprise {
550 if [[ -z "$os_VENDOR" ]]; then
551 GetOSVersion
552 fi
553
554 [[ "$os_VENDOR" =~ (^SUSE) ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600555}
556
557
558# Determine if current distribution is an Ubuntu-based distribution
559# It will also detect non-Ubuntu but Debian-based distros
560# is_ubuntu
561function is_ubuntu {
562 if [[ -z "$os_PACKAGE" ]]; then
563 GetOSVersion
564 fi
565 [ "$os_PACKAGE" = "deb" ]
566}
567
Kevin Zhao7880ba62021-03-31 04:58:28 +0000568function is_openeuler {
569 if [[ -z "$os_PACKAGE" ]]; then
570 GetOSVersion
571 fi
572 [ "$os_VENDOR" = "openEuler" ]
573}
Dean Troyerdff49a22014-01-30 15:37:40 -0600574# Git Functions
575# =============
576
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600577# Returns openstack release name for a given branch name
578# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100579function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600580 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700581 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600582 echo ${branch#*/}
583 else
584 echo "master"
585 fi
586}
587
Dean Troyerdff49a22014-01-30 15:37:40 -0600588# git clone only if directory doesn't exist already. Since ``DEST`` might not
589# be owned by the installation user, we create the directory and change the
590# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500591# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
592# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600593# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500594# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600595# git_clone remote dest-dir branch
596function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500597 local git_remote=$1
598 local git_dest=$2
599 local git_ref=$3
Ian Wienandada886d2015-10-07 14:06:26 +1100600 local orig_dir
601 orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200602 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500603
Sean Dague53753292014-12-04 19:38:15 -0500604 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800605 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200606 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
607 fi
608
Dean Troyerdff49a22014-01-30 15:37:40 -0600609 if [[ "$OFFLINE" = "True" ]]; then
610 echo "Running in offline mode, clones already exist"
611 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500612 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600613 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400614 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600615 return
616 fi
617
Dean Troyer50cda692014-07-25 11:57:20 -0500618 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600619 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500620 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700621 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
622 echo "The $git_dest project was not found; if this is a gate job, add"
James E. Blair35a0c572017-09-10 15:37:56 -0700623 echo "the project to 'required-projects' in the job definition."
Ghanshyam Mann325792d2021-10-15 15:55:54 -0500624 die $LINENO "ERROR_ON_CLONE is set to True so cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700625 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200626 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600627 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500628 cd $git_dest
629 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600630 else
631 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500632 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700633 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
634 echo "The $git_dest project was not found; if this is a gate job, add"
635 echo "the project to the \$PROJECTS variable in the job definition."
Ghanshyam Mann325792d2021-10-15 15:55:54 -0500636 die $LINENO "ERROR_ON_CLONE is set to True so cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700637 fi
Jakub Wachowski90742fc2016-11-18 14:28:47 +0100638 # '--branch' can also take tags
639 git_timed clone $git_clone_flags $git_remote $git_dest --branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600640 elif [[ "$RECLONE" = "True" ]]; then
641 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500642 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600643 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500644 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100645 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600646 # remove the existing ignored files (like pyc) as they cause breakage
647 # (due to the py files having older timestamps than our pyc, so python
648 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500649 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600650
Dean Troyer50cda692014-07-25 11:57:20 -0500651 # handle git_ref accordingly to type (tag, branch)
652 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
653 git_update_tag $git_ref
654 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
655 git_update_branch $git_ref
656 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
657 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600658 else
Dean Troyer50cda692014-07-25 11:57:20 -0500659 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600660 fi
661
662 fi
663 fi
664
665 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500666 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600667 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400668 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600669}
670
Sean Daguecc524062014-10-01 09:06:43 -0400671# A variation on git clone that lets us specify a project by it's
672# actual name, like oslo.config. This is exceptionally useful in the
673# library installation case
674function git_clone_by_name {
675 local name=$1
676 local repo=${GITREPO[$name]}
677 local dir=${GITDIR[$name]}
678 local branch=${GITBRANCH[$name]}
679 git_clone $repo $dir $branch
680}
681
682
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100683# git can sometimes get itself infinitely stuck with transient network
684# errors or other issues with the remote end. This wraps git in a
685# timeout/retry loop and is intended to watch over non-local git
686# processes that might hang. GIT_TIMEOUT, if set, is passed directly
687# to timeout(1); otherwise the default value of 0 maintains the status
688# quo of waiting forever.
689# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100690function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100691 local count=0
692 local timeout=0
693
694 if [[ -n "${GIT_TIMEOUT}" ]]; then
695 timeout=${GIT_TIMEOUT}
696 fi
697
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900698 time_start "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100699 until timeout -s SIGINT ${timeout} git "$@"; do
700 # 124 is timeout(1)'s special return code when it reached the
701 # timeout; otherwise assume fatal failure
702 if [[ $? -ne 124 ]]; then
703 die $LINENO "git call failed: [git $@]"
704 fi
705
706 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400707 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100708 if [ $count -eq 3 ]; then
709 die $LINENO "Maximum of 3 git retries reached"
710 fi
711 sleep 5
712 done
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900713 time_stop "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100714}
715
Dean Troyerdff49a22014-01-30 15:37:40 -0600716# git update using reference as a branch.
717# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100718function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500719 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600720
Dean Troyer50cda692014-07-25 11:57:20 -0500721 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600722 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500723 git branch -D $git_branch || true
724 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600725}
726
727# git update using reference as a branch.
728# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100729function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500730 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600731
Dean Troyer50cda692014-07-25 11:57:20 -0500732 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600733}
734
735# git update using reference as a tag. Be careful editing source at that repo
736# as working copy will be in a detached mode
737# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100738function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500739 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600740
Dean Troyer50cda692014-07-25 11:57:20 -0500741 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600742 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500743 git_timed fetch origin tag $git_tag
744 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600745}
746
747
748# OpenStack Functions
749# ===================
750
751# Get the default value for HOST_IP
752# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100753function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600754 local fixed_range=$1
755 local floating_range=$2
756 local host_ip_iface=$3
757 local host_ip=$4
Brian Haley180f5eb2015-06-16 13:14:31 -0400758 local af=$5
Dean Troyerdff49a22014-01-30 15:37:40 -0600759
Dean Troyerdff49a22014-01-30 15:37:40 -0600760 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
761 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
762 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100763 # Find the interface used for the default route
Brian Haley180f5eb2015-06-16 13:14:31 -0400764 host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
Ian Wienandada886d2015-10-07 14:06:26 +1100765 local host_ips
766 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 -0500767 local ip
768 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600769 # Attempt to filter out IP addresses that are part of the fixed and
770 # floating range. Note that this method only works if the ``netaddr``
771 # python library is installed. If it is not installed, an error
772 # will be printed and the first IP from the interface will be used.
773 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
774 # address.
Brian Haley180f5eb2015-06-16 13:14:31 -0400775 if [[ "$af" == "inet6" ]]; then
776 host_ip=$ip
777 break;
778 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500779 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
780 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600781 break;
782 fi
783 done
784 fi
785 echo $host_ip
786}
787
Attila Fazekasf71b5002014-05-28 09:52:22 +0200788# Generates hex string from ``size`` byte of pseudo random data
789# generate_hex_string size
790function generate_hex_string {
791 local size=$1
792 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
793}
794
Dean Troyerdff49a22014-01-30 15:37:40 -0600795# Grab a numbered field from python prettytable output
796# Fields are numbered starting with 1
797# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
798# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100799function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500800 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600801 while read data; do
802 if [ "$1" -lt 0 ]; then
803 field="(\$(NF$1))"
804 else
805 field="\$$(($1 + 1))"
806 fi
807 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
808 done
809}
810
yuntongjinf26deea2015-02-28 10:50:34 +0800811# install default policy
812# copy over a default policy.json and policy.d for projects
813function install_default_policy {
814 local project=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100815 local project_uc
816 project_uc=$(echo $1|tr a-z A-Z)
yuntongjinf26deea2015-02-28 10:50:34 +0800817 local conf_dir="${project_uc}_CONF_DIR"
818 # eval conf dir to get the variable
819 conf_dir="${!conf_dir}"
820 local project_dir="${project_uc}_DIR"
821 # eval project dir to get the variable
822 project_dir="${!project_dir}"
823 local sample_conf_dir="${project_dir}/etc/${project}"
824 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
825
826 # first copy any policy.json
827 cp -p $sample_conf_dir/policy.json $conf_dir
828 # then optionally copy over policy.d
829 if [[ -d $sample_policy_dir ]]; then
830 cp -r $sample_policy_dir $conf_dir/policy.d
831 fi
832}
833
Dean Troyerdff49a22014-01-30 15:37:40 -0600834# Add a policy to a policy.json file
835# Do nothing if the policy already exists
836# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100837function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600838 local policy_file=$1
839 local policy_name=$2
840 local policy_perm=$3
841
842 if grep -q ${policy_name} ${policy_file}; then
843 echo "Policy ${policy_name} already exists in ${policy_file}"
844 return
845 fi
846
847 # Add a terminating comma to policy lines without one
848 # Remove the closing '}' and all lines following to the end-of-file
Ian Wienandada886d2015-10-07 14:06:26 +1100849 local tmpfile
850 tmpfile=$(mktemp)
Dean Troyerdff49a22014-01-30 15:37:40 -0600851 uniq ${policy_file} | sed -e '
852 s/]$/],/
853 /^[}]/,$d
854 ' > ${tmpfile}
855
856 # Append policy and closing brace
857 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
858 echo "}" >>${tmpfile}
859
860 mv ${tmpfile} ${policy_file}
861}
862
Alistair Coles24779f62014-10-15 18:57:59 +0100863# Gets or creates a domain
864# Usage: get_or_create_domain <name> <description>
865function get_or_create_domain {
Ian Wienand11d276c2015-07-02 09:34:34 +1000866 local domain_id
Alistair Coles24779f62014-10-15 18:57:59 +0100867 # Gets domain id
Ian Wienand11d276c2015-07-02 09:34:34 +1000868 domain_id=$(
Alistair Coles24779f62014-10-15 18:57:59 +0100869 # Gets domain id
Steve Martinelli050a0d52015-09-06 22:03:54 +0000870 openstack domain show $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100871 -f value -c id 2>/dev/null ||
872 # Creates new domain
Steve Martinelli050a0d52015-09-06 22:03:54 +0000873 openstack domain create $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100874 --description "$2" \
875 -f value -c id
876 )
877 echo $domain_id
878}
879
Steve Martinellib74e01c2014-12-18 01:35:35 -0500880# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000881# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500882function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500883 local desc="${3:-}"
Ian Wienand11d276c2015-07-02 09:34:34 +1000884 local group_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500885 # Gets group id
Ian Wienand11d276c2015-07-02 09:34:34 +1000886 group_id=$(
Steve Martinellib74e01c2014-12-18 01:35:35 -0500887 # Creates new group with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000888 openstack group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000889 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500890 -f value -c id
891 )
892 echo $group_id
893}
894
Bartosz Górski0abde392014-02-28 14:15:19 +0100895# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000896# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100897function get_or_create_user {
Ian Wienand11d276c2015-07-02 09:34:34 +1000898 local user_id
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000899 if [[ ! -z "$4" ]]; then
900 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200901 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500902 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200903 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100904 # Gets user id
Ian Wienand11d276c2015-07-02 09:34:34 +1000905 user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500906 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000907 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100908 $1 \
909 --password "$2" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000910 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500911 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500912 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100913 -f value -c id
914 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500915 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100916}
917
918# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000919# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100920function get_or_create_project {
Ian Wienand11d276c2015-07-02 09:34:34 +1000921 local project_id
922 project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500923 # Creates new project with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000924 openstack project create $1 \
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000925 --domain=$2 \
926 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100927 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500928 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100929}
930
931# Gets or creates role
932# Usage: get_or_create_role <name>
933function get_or_create_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000934 local role_id
935 role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500936 # Creates role with --or-show
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000937 openstack role create $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000938 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100939 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500940 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100941}
942
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600943# Returns the domain parts of a function call if present
944# Usage: _get_domain_args [<user_domain> <project_domain>]
945function _get_domain_args {
946 local domain
947 domain=""
948
949 if [[ -n "$1" ]]; then
950 domain="$domain --user-domain $1"
951 fi
952 if [[ -n "$2" ]]; then
953 domain="$domain --project-domain $2"
954 fi
955
956 echo $domain
957}
958
Jamie Lennox9b215db2015-02-10 18:19:57 +1100959# Gets or adds user role to project
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600960# Usage: get_or_add_user_project_role <role> <user> <project> [<user_domain> <project_domain>]
Jamie Lennox9b215db2015-02-10 18:19:57 +1100961function get_or_add_user_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000962 local user_role_id
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600963
964 domain_args=$(_get_domain_args $4 $5)
965
Bartosz Górski0abde392014-02-28 14:15:19 +0100966 # Gets user role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700967 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700968 --role $1 \
Steve Martinelli5541a612015-01-19 15:58:49 -0500969 --user $2 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000970 --project $3 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600971 $domain_args \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700972 | grep '^|\s[a-f0-9]\+' | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500973 if [[ -z "$user_role_id" ]]; then
Roxana Gherle50821be2015-09-22 10:52:46 -0700974 # Adds role to user and get it
975 openstack role add $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100976 --user $2 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600977 --project $3 \
978 $domain_args
Mike Perezc271b3e2016-10-03 16:00:33 -0700979 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700980 --role $1 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700981 --user $2 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700982 --project $3 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600983 $domain_args \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700984 | grep '^|\s[a-f0-9]\+' | get_field 1)
Bartosz Górski0abde392014-02-28 14:15:19 +0100985 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500986 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100987}
988
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500989# Gets or adds user role to domain
990# Usage: get_or_add_user_domain_role <role> <user> <domain>
991function get_or_add_user_domain_role {
992 local user_role_id
993 # Gets user role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700994 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700995 --role $1 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500996 --user $2 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500997 --domain $3 \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700998 | grep '^|\s[a-f0-9]\+' | get_field 1)
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500999 if [[ -z "$user_role_id" ]]; then
1000 # Adds role to user and get it
1001 openstack role add $1 \
1002 --user $2 \
1003 --domain $3
Mike Perezc271b3e2016-10-03 16:00:33 -07001004 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -07001005 --role $1 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -05001006 --user $2 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -05001007 --domain $3 \
Clark Boylan3e9b5622017-06-14 15:29:47 -07001008 | grep '^|\s[a-f0-9]\+' | get_field 1)
Steve Martinelli4ce859a2015-12-20 01:27:30 -05001009 fi
1010 echo $user_role_id
1011}
1012
Lance Bragstad021ae0b2021-03-11 15:47:50 +00001013# Gets or adds user role to system
1014# Usage: get_or_add_user_system_role <role> <user> <system> [<user_domain>]
1015function get_or_add_user_system_role {
1016 local user_role_id
1017 local domain_args
1018
1019 domain_args=$(_get_domain_args $4)
1020
1021 # Gets user role id
1022 user_role_id=$(openstack role assignment list \
1023 --role $1 \
1024 --user $2 \
1025 --system $3 \
1026 $domain_args \
1027 -f value -c Role)
1028 if [[ -z "$user_role_id" ]]; then
1029 # Adds role to user and get it
1030 openstack role add $1 \
1031 --user $2 \
1032 --system $3 \
1033 $domain_args
1034 user_role_id=$(openstack role assignment list \
1035 --role $1 \
1036 --user $2 \
1037 --system $3 \
1038 $domain_args \
1039 -f value -c Role)
1040 fi
1041 echo $user_role_id
1042}
1043
Steve Martinelli4599fd12015-03-12 21:30:58 -04001044# Gets or adds group role to project
1045# Usage: get_or_add_group_project_role <role> <group> <project>
1046function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +10001047 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -04001048 # Gets group role id
Mike Perezc271b3e2016-10-03 16:00:33 -07001049 group_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -07001050 --role $1 \
Steve Martinelli4599fd12015-03-12 21:30:58 -04001051 --group $2 \
1052 --project $3 \
Mike Perezc271b3e2016-10-03 16:00:33 -07001053 -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -04001054 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +10001055 # Adds role to group and get it
1056 openstack role add $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +10001057 --group $2 \
1058 --project $3
Mike Perezc271b3e2016-10-03 16:00:33 -07001059 group_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -07001060 --role $1 \
Steve Martinelli4599fd12015-03-12 21:30:58 -04001061 --group $2 \
1062 --project $3 \
Mike Perezc271b3e2016-10-03 16:00:33 -07001063 -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -04001064 fi
1065 echo $group_role_id
1066}
1067
Bartosz Górski0abde392014-02-28 14:15:19 +01001068# Gets or creates service
1069# Usage: get_or_create_service <name> <type> <description>
1070function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +10001071 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +01001072 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +10001073 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +01001074 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +10001075 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +01001076 # Creates new service if not exists
1077 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -05001078 $2 \
1079 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +01001080 --description="$3" \
1081 -f value -c id
1082 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001083 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +01001084}
1085
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001086# Create an endpoint with a specific interface
Matt Riedemannae4578b2016-04-23 01:45:40 +00001087# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
1088function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +10001089 local endpoint_id
Matt Riedemannae4578b2016-04-23 01:45:40 +00001090 endpoint_id=$(openstack endpoint list \
1091 --service $1 \
1092 --interface $2 \
1093 --region $4 \
1094 -c ID -f value)
1095 if [[ -z "$endpoint_id" ]]; then
1096 # Creates new endpoint
1097 endpoint_id=$(openstack endpoint create \
1098 $1 $2 $3 --region $4 -f value -c id)
1099 fi
1100
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001101 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +01001102}
Dean Troyerdff49a22014-01-30 15:37:40 -06001103
Sean Dague7d1ec432016-04-22 09:19:10 -04001104# Gets or creates endpoint
Sean Dague11eb2012017-02-13 16:16:59 -05001105# Usage: get_or_create_endpoint <service> <region> <publicurl> [adminurl] [internalurl]
Matt Riedemannae4578b2016-04-23 01:45:40 +00001106function get_or_create_endpoint {
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001107 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
1108 # creating one endpoint with multiple urls to multiple endpoints each with
1109 # a different interface. To maintain the existing function interface we
Matt Riedemannae4578b2016-04-23 01:45:40 +00001110 # create 3 endpoints and return the id of the public one. In reality
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001111 # returning the public id will not make a lot of difference as there are no
1112 # scenarios currently that use the returned id. Ideally this behaviour
1113 # should be pushed out to the service setups and let them create the
1114 # endpoints they need.
Ian Wienandada886d2015-10-07 14:06:26 +11001115 local public_id
Matt Riedemannae4578b2016-04-23 01:45:40 +00001116 public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
Sean Dague11eb2012017-02-13 16:16:59 -05001117 # only create admin/internal urls if provided content for them
1118 if [[ -n "$4" ]]; then
1119 _get_or_create_endpoint_with_interface $1 admin $4 $2
1120 fi
1121 if [[ -n "$5" ]]; then
1122 _get_or_create_endpoint_with_interface $1 internal $5 $2
1123 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001124 # return the public id to indicate success, and this is the endpoint most likely wanted
1125 echo $public_id
1126}
1127
1128# Get a URL from the identity service
1129# Usage: get_endpoint_url <service> <interface>
1130function get_endpoint_url {
1131 echo $(openstack endpoint list \
1132 --service $1 --interface $2 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001133 -c URL -f value)
1134}
1135
Jim Rollenhagen47367072015-12-10 14:24:00 +00001136# check if we are using ironic with hardware
1137# TODO(jroll) this is a kludge left behind when ripping ironic code
1138# out of tree, as it is used by nova and neutron.
1139# figure out a way to refactor nova/neutron code to eliminate this
1140function is_ironic_hardware {
Lucas Alvares Gomes8a4dea22016-02-18 15:52:22 +00001141 is_service_enabled ironic && [[ "$IRONIC_IS_HARDWARE" == "True" ]] && return 0
Jim Rollenhagen47367072015-12-10 14:24:00 +00001142 return 1
1143}
1144
Julia Kreger6af3cb92021-03-11 11:28:47 -08001145function is_ironic_enforce_scope {
1146 is_service_enabled ironic && [[ "$IRONIC_ENFORCE_SCOPE" == "True" ]] && return 0
1147 return 1
1148}
1149
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001150
Dean Troyerdff49a22014-01-30 15:37:40 -06001151# Package Functions
1152# =================
1153
1154# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +11001155function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001156 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -06001157 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001158
1159 if [[ -z "$base_dir" ]]; then
1160 base_dir=$FILES
1161 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001162 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001163 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -06001164 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001165 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -06001166 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001167 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -06001168 else
1169 exit_distro_not_supported "list of packages"
1170 fi
1171 echo "$pkg_dir"
1172}
1173
Sean Dague88ee8ce2015-12-02 07:47:31 -05001174# Wrapper for ``apt-get update`` to try multiple times on the update
1175# to address bad package mirrors (which happen all the time).
1176function apt_get_update {
1177 # only do this once per run
1178 if [[ "$REPOS_UPDATED" == "True" && "$RETRY_UPDATE" != "True" ]]; then
1179 return
1180 fi
1181
1182 # bail if we are offline
1183 [[ "$OFFLINE" = "True" ]] && return
1184
1185 local sudo="sudo"
1186 [[ "$(id -u)" = "0" ]] && sudo="env"
1187
1188 # time all the apt operations
1189 time_start "apt-get-update"
1190
1191 local proxies="http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} no_proxy=${no_proxy:-} "
1192 local update_cmd="$sudo $proxies apt-get update"
1193 if ! timeout 300 sh -c "while ! $update_cmd; do sleep 30; done"; then
1194 die $LINENO "Failed to update apt repos, we're dead now"
1195 fi
1196
1197 REPOS_UPDATED=True
1198 # stop the clock
1199 time_stop "apt-get-update"
1200}
1201
Dean Troyerdff49a22014-01-30 15:37:40 -06001202# Wrapper for ``apt-get`` to set cache and proxy environment variables
1203# Uses globals ``OFFLINE``, ``*_proxy``
1204# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001205function apt_get {
Federico Ressie208d062015-11-21 11:15:39 +00001206 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +11001207 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001208 set +o xtrace
1209
Dean Troyerdff49a22014-01-30 15:37:40 -06001210 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
1211 local sudo="sudo"
1212 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -05001213
Sean Dague95c33d52015-10-07 11:05:59 -04001214 # time all the apt operations
1215 time_start "apt-get"
1216
Sean Dague45917cc2014-02-24 16:09:14 -05001217 $xtrace
Sean Dague53753292014-12-04 19:38:15 -05001218
Dean Troyerdff49a22014-01-30 15:37:40 -06001219 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -05001220 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
1221 no_proxy=${no_proxy:-} \
John L. Villalovosf568c3a2016-01-07 19:10:50 -08001222 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" < /dev/null
Federico Ressie208d062015-11-21 11:15:39 +00001223 result=$?
Sean Dague95c33d52015-10-07 11:05:59 -04001224
1225 # stop the clock
1226 time_stop "apt-get"
Federico Ressie208d062015-11-21 11:15:39 +00001227 return $result
Dean Troyerdff49a22014-01-30 15:37:40 -06001228}
1229
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001230function _parse_package_files {
1231 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -06001232
Dean Troyerdff49a22014-01-30 15:37:40 -06001233 if [[ -z "$DISTRO" ]]; then
1234 GetDistro
1235 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001236
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001237 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001238 local OIFS line package distros distro
1239 [[ -e $fname ]] || continue
1240
1241 OIFS=$IFS
1242 IFS=$'\n'
1243 for line in $(<${fname}); do
1244 if [[ $line =~ "NOPRIME" ]]; then
1245 continue
1246 fi
1247
Ian Wienand1ff75ff2016-02-19 14:28:37 +11001248 # Assume we want this package; free-form
1249 # comments allowed after a #
1250 package=${line%%#*}
Dean Troyerdff49a22014-01-30 15:37:40 -06001251 inst_pkg=1
1252
1253 # Look for # dist:xxx in comment
1254 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1255 # We are using BASH regexp matching feature.
1256 package=${BASH_REMATCH[1]}
1257 distros=${BASH_REMATCH[2]}
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001258 # In bash ${VAR,,} will lowercase VAR
Dean Troyerdff49a22014-01-30 15:37:40 -06001259 # Look for a match in the distro list
1260 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1261 # If no match then skip this package
1262 inst_pkg=0
1263 fi
1264 fi
1265
David Rabel682e0ab2017-03-17 19:19:00 +01001266 # Look for # not:xxx in comment
1267 if [[ $line =~ (.*)#.*not:([^ ]*) ]]; then
1268 # We are using BASH regexp matching feature.
1269 package=${BASH_REMATCH[1]}
1270 distros=${BASH_REMATCH[2]}
1271 # In bash ${VAR,,} will lowercase VAR
1272 # Look for a match in the distro list
1273 if [[ ${distros,,} =~ ${DISTRO,,} ]]; then
1274 # If match then skip this package
1275 inst_pkg=0
1276 fi
1277 fi
1278
Dean Troyerdff49a22014-01-30 15:37:40 -06001279 if [[ $inst_pkg = 1 ]]; then
1280 echo $package
1281 fi
1282 done
1283 IFS=$OIFS
1284 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001285}
1286
1287# get_packages() collects a list of package names of any type from the
1288# prerequisite files in ``files/{debs|rpms}``. The list is intended
1289# to be passed to a package installer such as apt or yum.
1290#
1291# Only packages required for the services in 1st argument will be
1292# included. Two bits of metadata are recognized in the prerequisite files:
1293#
1294# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1295# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1296# of the package to the distros listed. The distro names are case insensitive.
David Rabel682e0ab2017-03-17 19:19:00 +01001297# - ``# not:DISTRO`` or ``not:DISTRO1,DISTRO2`` limits the selection
1298# of the package to the distros not listed. The distro names are case insensitive.
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001299function get_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001300 local xtrace
1301 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001302 set +o xtrace
1303 local services=$@
Ian Wienandada886d2015-10-07 14:06:26 +11001304 local package_dir
1305 package_dir=$(_get_package_dir)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001306 local file_to_parse=""
1307 local service=""
1308
Ian Wienand7d515b52015-11-09 15:04:32 +11001309 if [ $# -ne 1 ]; then
1310 die $LINENO "get_packages takes a single, comma-separated argument"
1311 fi
1312
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001313 if [[ -z "$package_dir" ]]; then
1314 echo "No package directory supplied"
1315 return 1
1316 fi
1317 for service in ${services//,/ }; do
1318 # Allow individual services to specify dependencies
1319 if [[ -e ${package_dir}/${service} ]]; then
1320 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1321 fi
1322 # NOTE(sdague) n-api needs glance for now because that's where
1323 # glance client is
1324 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001325 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001326 file_to_parse="${file_to_parse} ${package_dir}/nova"
1327 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001328 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001329 file_to_parse="${file_to_parse} ${package_dir}/glance"
1330 fi
Luigi Toscano52e52d82020-08-13 09:55:08 +02001331 if [[ ! $file_to_parse =~ $package_dir/os-brick ]]; then
1332 file_to_parse="${file_to_parse} ${package_dir}/os-brick"
1333 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001334 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001335 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001336 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1337 fi
Luigi Toscano52e52d82020-08-13 09:55:08 +02001338 if [[ ! $file_to_parse =~ $package_dir/os-brick ]]; then
1339 file_to_parse="${file_to_parse} ${package_dir}/os-brick"
1340 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001341 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001342 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001343 file_to_parse="${file_to_parse} ${package_dir}/swift"
1344 fi
1345 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001346 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001347 file_to_parse="${file_to_parse} ${package_dir}/nova"
1348 fi
Luigi Toscano52e52d82020-08-13 09:55:08 +02001349 if [[ ! $file_to_parse =~ $package_dir/os-brick ]]; then
1350 file_to_parse="${file_to_parse} ${package_dir}/os-brick"
1351 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001352 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001353 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001354 file_to_parse="${file_to_parse} ${package_dir}/glance"
1355 fi
1356 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001357 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001358 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1359 fi
Ihar Hrachyshka387aadd2017-09-14 09:25:59 -06001360 elif [[ $service == q-* || $service == neutron-* ]]; then
1361 if [[ ! $file_to_parse =~ $package_dir/neutron-common ]]; then
1362 file_to_parse="${file_to_parse} ${package_dir}/neutron-common"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001363 fi
1364 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001365 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001366 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1367 fi
1368 fi
1369 done
1370 echo "$(_parse_package_files $file_to_parse)"
1371 $xtrace
1372}
1373
1374# get_plugin_packages() collects a list of package names of any type from a
1375# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1376# list is intended to be passed to a package installer such as apt or yum.
1377#
1378# Only packages required for enabled and collected plugins will included.
1379#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001380# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001381# in these prerequisite files, see get_packages() for more info.
1382function get_plugin_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001383 local xtrace
1384 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001385 set +o xtrace
1386 local files_to_parse=""
1387 local package_dir=""
1388 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
Ian Wienand7ae97292016-02-16 14:50:53 +11001389 package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
Dmitry Tantsur5979f472016-01-03 18:08:14 +01001390 files_to_parse+=" $package_dir/$plugin"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001391 done
1392 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001393 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001394}
1395
Ian Wienandfa9aadf2019-01-15 18:31:05 +11001396# Search plugins for a bindep.txt file
1397#
1398# Uses globals ``BINDEP_CMD``, ``GITDIR``, ``DEVSTACK_PLUGINS``
1399#
1400# Note this is only valid after BINDEP_CMD is setup in stack.sh, and
1401# is thus not really intended to be called externally.
1402function _get_plugin_bindep_packages {
1403 local xtrace
1404 xtrace=$(set +o | grep xtrace)
1405 set +o xtrace
1406
1407 local bindep_file
1408 local packages
1409
1410 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1411 bindep_file=${GITDIR[$plugin]}/devstack/files/bindep.txt
1412 if [[ -f ${bindep_file} ]]; then
1413 packages+=$($BINDEP_CMD -b --file ${bindep_file} || true)
1414 fi
1415 done
1416 echo "${packages}"
1417 $xtrace
1418}
1419
Dean Troyerdff49a22014-01-30 15:37:40 -06001420# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001421# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001422# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001423function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001424 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1425 REPOS_UPDATED=${REPOS_UPDATED:-False}
1426 RETRY_UPDATE=${RETRY_UPDATE:-False}
1427
Paul Linchpiner9e179742014-07-13 22:23:00 -07001428 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001429 return 0
1430 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001431
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001432 if is_ubuntu; then
Sean Dague88ee8ce2015-12-02 07:47:31 -05001433 apt_get_update
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001434 fi
1435}
1436
1437function real_install_package {
1438 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001439 apt_get install "$@"
1440 elif is_fedora; then
1441 yum_install "$@"
1442 elif is_suse; then
1443 zypper_install "$@"
1444 else
1445 exit_distro_not_supported "installing packages"
1446 fi
1447}
1448
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001449# Distro-agnostic package installer
1450# install_package package [package ...]
1451function install_package {
1452 update_package_repo
Federico Ressiecc1f412015-12-28 15:14:13 +01001453 if ! real_install_package "$@"; then
1454 RETRY_UPDATE=True update_package_repo && real_install_package "$@"
1455 fi
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001456}
1457
Dean Troyerdff49a22014-01-30 15:37:40 -06001458# Distro-agnostic function to tell if a package is installed
1459# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001460function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001461 if [[ -z "$@" ]]; then
1462 return 1
1463 fi
1464
1465 if [[ -z "$os_PACKAGE" ]]; then
1466 GetOSVersion
1467 fi
1468
1469 if [[ "$os_PACKAGE" = "deb" ]]; then
1470 dpkg -s "$@" > /dev/null 2> /dev/null
1471 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1472 rpm --quiet -q "$@"
1473 else
1474 exit_distro_not_supported "finding if a package is installed"
1475 fi
1476}
1477
1478# Distro-agnostic package uninstaller
1479# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001480function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001481 if is_ubuntu; then
1482 apt_get purge "$@"
1483 elif is_fedora; then
Ian Wienand67fd81a2020-04-30 09:24:04 +10001484 sudo dnf remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001485 elif is_suse; then
Gary W. Smith56b39122016-11-16 22:03:43 -08001486 sudo zypper remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001487 else
1488 exit_distro_not_supported "uninstalling packages"
1489 fi
1490}
1491
Ian Wienand67fd81a2020-04-30 09:24:04 +10001492# Wrapper for ``dnf`` to set proxy environment variables
1493# Uses globals ``OFFLINE``, ``*_proxy``
1494# The name is kept for backwards compatability with external
1495# callers, despite none of our supported platforms using yum
1496# any more.
Dean Troyerdff49a22014-01-30 15:37:40 -06001497# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001498function yum_install {
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001499 local result parse_yum_result
1500
Dean Troyerdff49a22014-01-30 15:37:40 -06001501 [[ "$OFFLINE" = "True" ]] && return
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001502
1503 time_start "yum_install"
Ian Wienand67fd81a2020-04-30 09:24:04 +10001504 sudo_with_proxies dnf install -y "$@"
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001505 time_stop "yum_install"
Dean Troyerdff49a22014-01-30 15:37:40 -06001506}
1507
1508# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001509# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001510# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001511function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001512 [[ "$OFFLINE" = "True" ]] && return
1513 local sudo="sudo"
1514 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001515 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1516 no_proxy="${no_proxy:-}" \
Dirk Mueller297a50a2018-06-20 11:08:54 +02001517 zypper --non-interactive install --auto-agree-with-licenses --no-recommends "$@"
Dean Troyerdff49a22014-01-30 15:37:40 -06001518}
1519
Ian Wienand58243f62018-12-13 14:05:53 +11001520# Run bindep and install packages it outputs
1521#
1522# Usage:
1523# install_bindep <path-to-bindep.txt> [profile,profile]
1524#
1525# Note unlike the bindep command itself, profile(s) specified should
1526# be a single, comma-separated string, no spaces.
1527function install_bindep {
1528 local file=$1
1529 local profiles=${2:-""}
1530 local pkgs
1531
1532 if [[ ! -f $file ]]; then
Sean McGinnisdd3731c2020-07-28 08:51:41 -05001533 warn $LINENO "Can not find bindep file: $file"
1534 return
Ian Wienand58243f62018-12-13 14:05:53 +11001535 fi
1536
1537 # converting here makes it much easier to work with passing
1538 # arguments
1539 profiles=${profiles/,/ /}
1540
1541 # Note bindep returns 1 when packages need to be installed, so we
1542 # have to ignore it's return for "-e"
1543 pkgs=$($DEST/bindep-venv/bin/bindep -b --file $file $profiles || true)
1544
1545 if [[ -n "${pkgs}" ]]; then
1546 install_package ${pkgs}
1547 fi
1548}
1549
Sean Dague5edae542017-03-21 20:50:24 -04001550function write_user_unit_file {
1551 local service=$1
1552 local command="$2"
1553 local group=$3
1554 local user=$4
1555 local extra=""
1556 if [[ -n "$group" ]]; then
1557 extra="Group=$group"
1558 fi
1559 local unitfile="$SYSTEMD_DIR/$service"
1560 mkdir -p $SYSTEMD_DIR
1561
1562 iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
1563 iniset -sudo $unitfile "Service" "User" "$user"
1564 iniset -sudo $unitfile "Service" "ExecStart" "$command"
Matthew Treinish477a9622017-08-04 11:09:26 -04001565 iniset -sudo $unitfile "Service" "KillMode" "process"
Michał Dulko3b815a32017-11-14 16:04:51 +01001566 iniset -sudo $unitfile "Service" "TimeoutStopSec" "300"
Dinesh Bhoref60f2b2017-09-05 14:40:32 +05301567 iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
Sean Dague5edae542017-03-21 20:50:24 -04001568 if [[ -n "$group" ]]; then
1569 iniset -sudo $unitfile "Service" "Group" "$group"
1570 fi
1571 iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
1572
1573 # changes to existing units sometimes need a refresh
1574 $SYSTEMCTL daemon-reload
1575}
1576
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001577function write_uwsgi_user_unit_file {
1578 local service=$1
1579 local command="$2"
1580 local group=$3
1581 local user=$4
1582 local unitfile="$SYSTEMD_DIR/$service"
1583 mkdir -p $SYSTEMD_DIR
1584
1585 iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
Sean Daguef41bf2a2017-05-05 07:50:52 -04001586 iniset -sudo $unitfile "Service" "SyslogIdentifier" "$service"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001587 iniset -sudo $unitfile "Service" "User" "$user"
1588 iniset -sudo $unitfile "Service" "ExecStart" "$command"
Dinesh Bhoref60f2b2017-09-05 14:40:32 +05301589 iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001590 iniset -sudo $unitfile "Service" "Type" "notify"
Matthew Treinish477a9622017-08-04 11:09:26 -04001591 iniset -sudo $unitfile "Service" "KillMode" "process"
gong yong sheng07b3bc22017-06-05 14:02:28 +08001592 iniset -sudo $unitfile "Service" "Restart" "always"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001593 iniset -sudo $unitfile "Service" "NotifyAccess" "all"
1594 iniset -sudo $unitfile "Service" "RestartForceExitStatus" "100"
1595
1596 if [[ -n "$group" ]]; then
1597 iniset -sudo $unitfile "Service" "Group" "$group"
1598 fi
1599 iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
1600
1601 # changes to existing units sometimes need a refresh
1602 $SYSTEMCTL daemon-reload
1603}
1604
Sean Dague148d58c2017-05-04 13:08:25 -04001605function _common_systemd_pitfalls {
1606 local cmd=$1
1607 # do some sanity checks on $cmd to see things we don't expect to work
1608
1609 if [[ "$cmd" =~ "sudo" ]]; then
Paweł Suder4e16c3d2018-09-26 15:52:13 +02001610 read -r -d '' msg << EOF || true # read returns 1 for EOF, but it is ok here
Sean Dague148d58c2017-05-04 13:08:25 -04001611You are trying to use run_process with sudo, this is not going to work under systemd.
1612
Ian Wienande8a6a022018-10-08 15:20:34 +11001613If you need to run a service as a user other than \$STACK_USER call it with:
Sean Dague148d58c2017-05-04 13:08:25 -04001614
1615 run_process \$name \$cmd \$group \$user
1616EOF
Ian Wienande8a6a022018-10-08 15:20:34 +11001617 die $LINENO "$msg"
Sean Dague148d58c2017-05-04 13:08:25 -04001618 fi
1619
1620 if [[ ! "$cmd" =~ ^/ ]]; then
Paweł Suder4e16c3d2018-09-26 15:52:13 +02001621 read -r -d '' msg << EOF || true # read returns 1 for EOF, but it is ok here
Sean Dague148d58c2017-05-04 13:08:25 -04001622The cmd="$cmd" does not start with an absolute path. It will fail to
1623start under systemd.
1624
1625Please update your run_process stanza to have an absolute path.
1626EOF
Ian Wienande8a6a022018-10-08 15:20:34 +11001627 die $LINENO "$msg"
Sean Dague148d58c2017-05-04 13:08:25 -04001628 fi
1629
1630}
1631
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001632# Helper function to build a basic unit file and run it under systemd.
1633function _run_under_systemd {
Sean Dague5edae542017-03-21 20:50:24 -04001634 local service=$1
1635 local command="$2"
1636 local cmd=$command
Sean Dague148d58c2017-05-04 13:08:25 -04001637 # sanity check the command
1638 _common_systemd_pitfalls "$cmd"
1639
Sean Dague5edae542017-03-21 20:50:24 -04001640 local systemd_service="devstack@$service.service"
1641 local group=$3
1642 local user=${4:-$STACK_USER}
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001643 if [[ "$command" =~ "uwsgi" ]] ; then
1644 write_uwsgi_user_unit_file $systemd_service "$cmd" "$group" "$user"
1645 else
1646 write_user_unit_file $systemd_service "$cmd" "$group" "$user"
1647 fi
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001648
1649 $SYSTEMCTL enable $systemd_service
1650 $SYSTEMCTL start $systemd_service
Sean Dague5edae542017-03-21 20:50:24 -04001651}
1652
Dean Troyerdff49a22014-01-30 15:37:40 -06001653# Find out if a process exists by partial name.
1654# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001655function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001656 local name=$1
1657 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001658 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001659 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001660 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001661}
1662
Dean Troyer3159a822014-08-27 14:13:58 -05001663# Run a single service under screen or directly
1664# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001665# If an optional group is provided sg will be used to run the
1666# command as that group.
Sean Dague148d58c2017-05-04 13:08:25 -04001667# run_process service "command-line" [group] [user]
Ian Wienandaee18c72014-02-21 15:35:08 +11001668function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001669 local service=$1
1670 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001671 local group=$3
Sean Dague5edae542017-03-21 20:50:24 -04001672 local user=$4
Brant Knudsonedc11c22015-12-14 15:32:05 -06001673
Sean Dague5edae542017-03-21 20:50:24 -04001674 local name=$service
Dean Troyerdff49a22014-01-30 15:37:40 -06001675
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001676 time_start "run_process"
Dean Troyer3159a822014-08-27 14:13:58 -05001677 if is_service_enabled $service; then
Sean Daguecdba1b32017-08-30 11:11:06 -04001678 _run_under_systemd "$name" "$command" "$group" "$user"
Dean Troyer3159a822014-08-27 14:13:58 -05001679 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001680 time_stop "run_process"
Dean Troyerdff49a22014-01-30 15:37:40 -06001681}
1682
Dean Troyer3159a822014-08-27 14:13:58 -05001683# Stop a service process
1684# If a PID is available use it, kill the whole process group via TERM
1685# If screen is being used kill the screen window; this will catch processes
1686# that did not leave a PID behind
Radoslav Gerganov2b97a812017-10-10 16:51:12 +03001687# Uses globals ``SERVICE_DIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001688# stop_process service
1689function stop_process {
1690 local service=$1
1691
1692 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer3159a822014-08-27 14:13:58 -05001693
1694 if is_service_enabled $service; then
Sean Dague803acff2017-05-01 10:52:38 -04001695 # Only do this for units which appear enabled, this also
1696 # catches units that don't really exist for cases like
1697 # keystone without a failure.
1698 if $SYSTEMCTL is-enabled devstack@$service.service; then
1699 $SYSTEMCTL stop devstack@$service.service
1700 $SYSTEMCTL disable devstack@$service.service
Sean Dague5edae542017-03-21 20:50:24 -04001701 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001702 fi
1703}
1704
Sean Daguecdba1b32017-08-30 11:11:06 -04001705# use systemctl to check service status
Ian Wienandaee18c72014-02-21 15:35:08 +11001706function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001707 local service
Sean Daguecdba1b32017-08-30 11:11:06 -04001708 for service in ${ENABLED_SERVICES//,/ }; do
1709 # because some things got renamed like key => keystone
1710 if $SYSTEMCTL is-enabled devstack@$service.service; then
1711 # no-pager is needed because otherwise status dumps to a
1712 # pager when in interactive mode, which will stop a manual
1713 # devstack run.
1714 $SYSTEMCTL status devstack@$service.service --no-pager
Dean Troyer3159a822014-08-27 14:13:58 -05001715 fi
Sean Daguecdba1b32017-08-30 11:11:06 -04001716 done
Dean Troyer3159a822014-08-27 14:13:58 -05001717}
1718
Dean Troyer3159a822014-08-27 14:13:58 -05001719
Sean Dague2c65e712014-12-18 09:44:56 -05001720# Plugin Functions
1721# =================
1722
1723DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1724
1725# enable_plugin <name> <url> [branch]
1726#
1727# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1728# ``url`` is a git url
1729# ``branch`` is a gitref. If it's not set, defaults to master
1730function enable_plugin {
1731 local name=$1
1732 local url=$2
1733 local branch=${3:-master}
Omer Anson51584862017-08-24 17:47:37 +03001734 if is_plugin_enabled $name; then
John L. Villalovos21d84c22016-11-11 15:26:11 -08001735 die $LINENO "Plugin attempted to be enabled twice: ${name} ${url} ${branch}"
1736 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001737 DEVSTACK_PLUGINS+=",$name"
1738 GITREPO[$name]=$url
1739 GITDIR[$name]=$DEST/$name
1740 GITBRANCH[$name]=$branch
1741}
1742
Omer Anson51584862017-08-24 17:47:37 +03001743# is_plugin_enabled <name>
1744#
1745# Check if the plugin was enabled, e.g. using enable_plugin
1746#
1747# ``name`` The name with which the plugin was enabled
1748function is_plugin_enabled {
1749 local name=$1
1750 if [[ ",${DEVSTACK_PLUGINS}," =~ ",${name}," ]]; then
1751 return 0
1752 fi
1753 return 1
1754}
1755
Sean Dague2c65e712014-12-18 09:44:56 -05001756# fetch_plugins
1757#
1758# clones all plugins
1759function fetch_plugins {
1760 local plugins="${DEVSTACK_PLUGINS}"
1761 local plugin
1762
1763 # short circuit if nothing to do
1764 if [[ -z $plugins ]]; then
1765 return
1766 fi
1767
Dean Troyerdc97cb72015-03-28 08:20:50 -05001768 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001769 for plugin in ${plugins//,/ }; do
1770 git_clone_by_name $plugin
1771 done
1772}
1773
1774# load_plugin_settings
1775#
1776# Load settings from plugins in the order that they were registered
1777function load_plugin_settings {
1778 local plugins="${DEVSTACK_PLUGINS}"
1779 local plugin
1780
1781 # short circuit if nothing to do
1782 if [[ -z $plugins ]]; then
1783 return
1784 fi
1785
1786 echo "Loading plugin settings"
1787 for plugin in ${plugins//,/ }; do
1788 local dir=${GITDIR[$plugin]}
1789 # source any known settings
1790 if [[ -f $dir/devstack/settings ]]; then
1791 source $dir/devstack/settings
1792 fi
1793 done
1794}
1795
Sean Dague6e275e12015-03-26 05:54:28 -04001796# plugin_override_defaults
1797#
1798# Run an extremely early setting phase for plugins that allows default
1799# overriding of services.
1800function plugin_override_defaults {
1801 local plugins="${DEVSTACK_PLUGINS}"
1802 local plugin
1803
1804 # short circuit if nothing to do
1805 if [[ -z $plugins ]]; then
1806 return
1807 fi
1808
1809 echo "Overriding Configuration Defaults"
1810 for plugin in ${plugins//,/ }; do
1811 local dir=${GITDIR[$plugin]}
1812 # source any overrides
1813 if [[ -f $dir/devstack/override-defaults ]]; then
1814 # be really verbose that an override is happening, as it
1815 # may not be obvious if things fail later.
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001816 echo "$plugin has overridden the following defaults"
Sean Dague6e275e12015-03-26 05:54:28 -04001817 cat $dir/devstack/override-defaults
1818 source $dir/devstack/override-defaults
1819 fi
1820 done
1821}
1822
Sean Dague2c65e712014-12-18 09:44:56 -05001823# run_plugins
1824#
1825# Run the devstack/plugin.sh in all the plugin directories. These are
1826# run in registration order.
1827function run_plugins {
1828 local mode=$1
1829 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301830
1831 local plugins="${DEVSTACK_PLUGINS}"
1832 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001833 for plugin in ${plugins//,/ }; do
1834 local dir=${GITDIR[$plugin]}
1835 if [[ -f $dir/devstack/plugin.sh ]]; then
1836 source $dir/devstack/plugin.sh $mode $phase
1837 fi
1838 done
1839}
1840
1841function run_phase {
1842 local mode=$1
1843 local phase=$2
1844 if [[ -d $TOP_DIR/extras.d ]]; then
Dmitry Tantsur64be3212015-10-12 13:10:24 +02001845 local extra_plugin_file_name
1846 for extra_plugin_file_name in $TOP_DIR/extras.d/*.sh; do
Sean Dague41d01102015-12-03 08:12:23 -05001847 # NOTE(sdague): only process extras.d for the 3 explicitly
1848 # white listed elements in tree. We want these to move out
1849 # over time as well, but they are in tree, so we need to
1850 # manage that.
Matt Riedemannc9f63272016-08-30 17:21:30 -04001851 local exceptions="80-tempest.sh"
Ian Wienand5cdee8d2015-10-19 14:17:18 +11001852 local extra
1853 extra=$(basename $extra_plugin_file_name)
Sean Dague1de9e332015-10-07 08:46:13 -04001854 if [[ ! ( $exceptions =~ "$extra" ) ]]; then
Sean Dague41d01102015-12-03 08:12:23 -05001855 warn "use of extras.d is no longer supported"
1856 warn "processing of project $extra is skipped"
1857 else
1858 [[ -r $extra_plugin_file_name ]] && source $extra_plugin_file_name $mode $phase
Sean Dague1de9e332015-10-07 08:46:13 -04001859 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001860 done
1861 fi
1862 # the source phase corresponds to settings loading in plugins
1863 if [[ "$mode" == "source" ]]; then
1864 load_plugin_settings
Chris Dentc6d47012015-10-09 14:57:05 +00001865 verify_disabled_services
Sean Dague6e275e12015-03-26 05:54:28 -04001866 elif [[ "$mode" == "override_defaults" ]]; then
1867 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001868 else
1869 run_plugins $mode $phase
1870 fi
1871}
1872
James E. Blairc5853ac2017-11-21 09:44:42 -08001873# define_plugin <name>
1874#
1875# This function is a no-op. It allows a plugin to define its name So
1876# that other plugins may reference it by name. It should generally be
1877# the last component of the canonical git repo name. E.g.,
1878# openstack/devstack-foo should use "devstack-foo" as the name here.
1879#
1880# This function is currently a noop, but the value may still be used
1881# by external tools (as in plugin_requires) and may be used by
1882# devstack in the future.
1883#
1884# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1885function define_plugin {
1886 :
1887}
1888
1889# plugin_requires <name> <other>
1890#
1891# This function is a no-op. It is currently used by external tools
1892# (such as the devstack module for Ansible) to automatically generate
1893# local.conf files. It is not currently used by devstack itself to
1894# resolve dependencies.
1895#
1896# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1897# ``other`` is the name of another plugin
1898function plugin_requires {
1899 :
1900}
1901
Dean Troyerdff49a22014-01-30 15:37:40 -06001902
1903# Service Functions
1904# =================
1905
1906# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1907# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001908function _cleanup_service_list {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001909 local xtrace
1910 xtrace=$(set +o | grep xtrace)
1911 set +o xtrace
1912
Dean Troyerdff49a22014-01-30 15:37:40 -06001913 echo "$1" | sed -e '
1914 s/,,/,/g;
1915 s/^,//;
1916 s/,$//
1917 '
Ian Wienand8043bfa2015-10-14 14:53:18 +11001918
1919 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001920}
1921
1922# disable_all_services() removes all current services
1923# from ``ENABLED_SERVICES`` to reset the configuration
1924# before a minimal installation
1925# Uses global ``ENABLED_SERVICES``
1926# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001927function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001928 ENABLED_SERVICES=""
1929}
1930
1931# Remove all services starting with '-'. For example, to install all default
1932# services except rabbit (rabbit) set in ``localrc``:
1933# ENABLED_SERVICES+=",-rabbit"
1934# Uses global ``ENABLED_SERVICES``
1935# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001936function disable_negated_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001937 local xtrace
1938 xtrace=$(set +o | grep xtrace)
1939 set +o xtrace
1940
Ian Wienand2796a822015-04-15 08:59:04 +10001941 local to_remove=""
1942 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001943 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001944
1945 # build up list of services that should be removed; i.e. they
1946 # begin with "-"
1947 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001948 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001949 to_remove+=",${service#-}"
1950 else
1951 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001952 fi
1953 done
Ian Wienand2796a822015-04-15 08:59:04 +10001954
1955 # go through the service list. if this service appears in the "to
1956 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001957 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001958
1959 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001960}
1961
Chris Dentc6d47012015-10-09 14:57:05 +00001962# disable_service() prepares the services passed as argument to be
1963# removed from the ``ENABLED_SERVICES`` list, if they are present.
Dean Troyerdff49a22014-01-30 15:37:40 -06001964#
1965# For example:
1966# disable_service rabbit
1967#
Chris Dentc6d47012015-10-09 14:57:05 +00001968# Uses global ``DISABLED_SERVICES``
Dean Troyerdff49a22014-01-30 15:37:40 -06001969# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001970function disable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001971 local xtrace
1972 xtrace=$(set +o | grep xtrace)
1973 set +o xtrace
1974
Chris Dentc6d47012015-10-09 14:57:05 +00001975 local disabled_svcs="${DISABLED_SERVICES}"
1976 local enabled_svcs=",${ENABLED_SERVICES},"
Dean Troyerdff49a22014-01-30 15:37:40 -06001977 local service
1978 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001979 disabled_svcs+=",$service"
Dean Troyerdff49a22014-01-30 15:37:40 -06001980 if is_service_enabled $service; then
Chris Dentc6d47012015-10-09 14:57:05 +00001981 enabled_svcs=${enabled_svcs//,$service,/,}
Dean Troyerdff49a22014-01-30 15:37:40 -06001982 fi
1983 done
Chris Dentc6d47012015-10-09 14:57:05 +00001984 DISABLED_SERVICES=$(_cleanup_service_list "$disabled_svcs")
1985 ENABLED_SERVICES=$(_cleanup_service_list "$enabled_svcs")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001986
1987 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001988}
1989
1990# enable_service() adds the services passed as argument to the
1991# ``ENABLED_SERVICES`` list, if they are not already present.
1992#
1993# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001994# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001995#
1996# This function does not know about the special cases
1997# for nova, glance, and neutron built into is_service_enabled().
1998# Uses global ``ENABLED_SERVICES``
1999# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11002000function enable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11002001 local xtrace
2002 xtrace=$(set +o | grep xtrace)
2003 set +o xtrace
2004
Dean Troyerdff49a22014-01-30 15:37:40 -06002005 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002006 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06002007 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00002008 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
2009 warn $LINENO "Attempt to enable_service ${service} when it has been disabled"
2010 continue
2011 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06002012 if ! is_service_enabled $service; then
2013 tmpsvcs+=",$service"
2014 fi
2015 done
2016 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
2017 disable_negated_services
Ian Wienand8043bfa2015-10-14 14:53:18 +11002018
2019 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002020}
2021
2022# is_service_enabled() checks if the service(s) specified as arguments are
2023# enabled by the user in ``ENABLED_SERVICES``.
2024#
2025# Multiple services specified as arguments are ``OR``'ed together; the test
2026# is a short-circuit boolean, i.e it returns on the first match.
2027#
2028# There are special cases for some 'catch-all' services::
2029# **nova** returns true if any service enabled start with **n-**
2030# **cinder** returns true if any service enabled start with **c-**
Dean Troyerdff49a22014-01-30 15:37:40 -06002031# **glance** returns true if any service enabled start with **g-**
2032# **neutron** returns true if any service enabled start with **q-**
2033# **swift** returns true if any service enabled start with **s-**
2034# **trove** returns true if any service enabled start with **tr-**
2035# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
2036# **s-** services will be enabled. This will be deprecated in the future.
2037#
Dean Troyerdff49a22014-01-30 15:37:40 -06002038# Uses global ``ENABLED_SERVICES``
2039# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11002040function is_service_enabled {
Ian Wienand433a9b12015-10-07 13:29:31 +11002041 local xtrace
2042 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05002043 set +o xtrace
Ian Wienand8043bfa2015-10-14 14:53:18 +11002044
Sean Dague45917cc2014-02-24 16:09:14 -05002045 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002046 local services=$@
2047 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06002048 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05002049 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002050
2051 # Look for top-level 'enabled' function for this service
2052 if type is_${service}_enabled >/dev/null 2>&1; then
2053 # A function exists for this service, use it
Christian Schwede3db0aad2015-09-10 11:15:39 +00002054 is_${service}_enabled && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002055 fi
2056
2057 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
2058 # are implemented
2059
John Kasperski7b3ae532016-04-24 22:56:04 -05002060 [[ ${service} == n-cpu-* && ,${ENABLED_SERVICES} =~ ,"n-cpu" ]] && enabled=0
2061 [[ ${service} == "nova" && ,${ENABLED_SERVICES} =~ ,"n-" ]] && enabled=0
2062 [[ ${service} == "glance" && ,${ENABLED_SERVICES} =~ ,"g-" ]] && enabled=0
2063 [[ ${service} == "neutron" && ,${ENABLED_SERVICES} =~ ,"q-" ]] && enabled=0
2064 [[ ${service} == "trove" && ,${ENABLED_SERVICES} =~ ,"tr-" ]] && enabled=0
2065 [[ ${service} == "swift" && ,${ENABLED_SERVICES} =~ ,"s-" ]] && enabled=0
2066 [[ ${service} == s-* && ,${ENABLED_SERVICES} =~ ,"swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002067 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11002068
Sean Dague45917cc2014-02-24 16:09:14 -05002069 $xtrace
2070 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06002071}
2072
fumihiko kakuma8606c982015-04-13 09:55:06 +09002073# remove specified list from the input string
2074# remove_disabled_services service-list remove-list
2075function remove_disabled_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11002076 local xtrace
2077 xtrace=$(set +o | grep xtrace)
2078 set +o xtrace
2079
fumihiko kakuma8606c982015-04-13 09:55:06 +09002080 local service_list=$1
2081 local remove_list=$2
2082 local service
2083 local enabled=""
2084
2085 for service in ${service_list//,/ }; do
2086 local remove
2087 local add=1
2088 for remove in ${remove_list//,/ }; do
2089 if [[ ${remove} == ${service} ]]; then
2090 add=0
2091 break
2092 fi
2093 done
2094 if [[ $add == 1 ]]; then
2095 enabled="${enabled},$service"
2096 fi
2097 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11002098
2099 $xtrace
2100
fumihiko kakuma8606c982015-04-13 09:55:06 +09002101 _cleanup_service_list "$enabled"
2102}
2103
Dean Troyerdff49a22014-01-30 15:37:40 -06002104# Toggle enable/disable_service for services that must run exclusive of each other
2105# $1 The name of a variable containing a space-separated list of services
2106# $2 The name of a variable in which to store the enabled service's name
2107# $3 The name of the service to enable
2108function use_exclusive_service {
2109 local options=${!1}
2110 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002111 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06002112 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002113 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06002114 for opt in $options;do
2115 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
2116 done
2117 eval "$out=$selection"
2118 return 0
2119}
2120
Chris Dentc6d47012015-10-09 14:57:05 +00002121# Make sure that nothing has manipulated ENABLED_SERVICES in a way
2122# that conflicts with prior calls to disable_service.
2123# Uses global ``ENABLED_SERVICES``
2124function verify_disabled_services {
2125 local service
2126 for service in ${ENABLED_SERVICES//,/ }; do
2127 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
2128 die $LINENO "ENABLED_SERVICES directly modified to overcome 'disable_service ${service}'"
2129 fi
2130 done
2131}
2132
Dean Troyerdff49a22014-01-30 15:37:40 -06002133
Masayuki Igawaf6368d32014-02-20 13:31:26 +09002134# System Functions
2135# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06002136
2137# Only run the command if the target file (the last arg) is not on an
2138# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002139function _safe_permission_operation {
Ian Wienand433a9b12015-10-07 13:29:31 +11002140 local xtrace
2141 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05002142 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002143 local args=( $@ )
2144 local last
2145 local sudo_cmd
2146 local dir_to_check
2147
2148 let last="${#args[*]} - 1"
2149
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002150 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002151 if [ ! -d "$dir_to_check" ]; then
2152 dir_to_check=`dirname "$dir_to_check"`
2153 fi
2154
2155 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002156 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002157 return 0
2158 fi
2159
Ian Wienandbcb2c302020-01-13 16:31:20 +11002160 sudo_cmd="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06002161
Sean Dague45917cc2014-02-24 16:09:14 -05002162 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002163 $sudo_cmd $@
2164}
2165
2166# Exit 0 if address is in network or 1 if address is not in network
2167# ip-range is in CIDR notation: 1.2.3.4/20
2168# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002169function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002170 local ip=$1
2171 local range=$2
2172 local masklen=${range#*/}
Ian Wienandada886d2015-10-07 14:06:26 +11002173 local network
2174 network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2175 local subnet
2176 subnet=$(maskip $ip $(cidr2netmask $masklen))
Dean Troyerdff49a22014-01-30 15:37:40 -06002177 [[ $network == $subnet ]]
2178}
2179
2180# Add a user to a group.
2181# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002182function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002183 local user=$1
2184 local group=$2
2185
Thomas Bechtolda8580852015-05-31 00:04:33 +02002186 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06002187}
2188
2189# Convert CIDR notation to a IPv4 netmask
2190# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002191function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002192 local maskpat="255 255 255 255"
2193 local maskdgt="254 252 248 240 224 192 128"
2194 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2195 echo ${1-0}.${2-0}.${3-0}.${4-0}
2196}
2197
Ian Cordasco69e3c0a2016-09-26 12:53:14 -05002198# Check if this is a valid ipv4 address string
2199function is_ipv4_address {
2200 local address=$1
Jens Harbott7617ac22017-09-19 17:43:48 +00002201 local regex='([0-9]{1,3}\.){3}[0-9]{1,3}'
Ian Cordasco69e3c0a2016-09-26 12:53:14 -05002202 # TODO(clarkb) make this more robust
2203 if [[ "$address" =~ $regex ]] ; then
2204 return 0
2205 else
2206 return 1
2207 fi
2208}
2209
Jens Harbottdc7b4292017-09-19 10:52:32 +00002210# Remove "[]" around urlquoted IPv6 addresses
2211function ipv6_unquote {
2212 echo $1 | tr -d []
2213}
2214
Dean Troyerdff49a22014-01-30 15:37:40 -06002215# Gracefully cp only if source file/dir exists
2216# cp_it source destination
2217function cp_it {
2218 if [ -e $1 ] || [ -d $1 ]; then
2219 cp -pRL $1 $2
2220 fi
2221}
2222
2223# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2224# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2225# ``localrc`` or on the command line if necessary::
2226#
2227# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2228#
2229# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2230
Ian Wienandaee18c72014-02-21 15:35:08 +11002231function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05002232 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002233 export http_proxy=$http_proxy
2234 fi
Sean Dague53753292014-12-04 19:38:15 -05002235 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002236 export https_proxy=$https_proxy
2237 fi
Sean Dague53753292014-12-04 19:38:15 -05002238 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002239 export no_proxy=$no_proxy
2240 fi
2241}
2242
2243# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002244function is_nfs_directory {
Ian Wienandada886d2015-10-07 14:06:26 +11002245 local mount_type
2246 mount_type=`stat -f -L -c %T $1`
Dean Troyerdff49a22014-01-30 15:37:40 -06002247 test "$mount_type" == "nfs"
2248}
2249
2250# Return the network portion of the given IP address using netmask
2251# netmask is in the traditional dotted-quad format
2252# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002253function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002254 local ip=$1
2255 local mask=$2
2256 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
Ian Wienandada886d2015-10-07 14:06:26 +11002257 local subnet
2258 subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
Dean Troyerdff49a22014-01-30 15:37:40 -06002259 echo $subnet
2260}
2261
Michael Turek7938d832016-04-12 14:55:21 -04002262function is_provider_network {
2263 if [ "$Q_USE_PROVIDER_NETWORKING" == "True" ]; then
2264 return 0
2265 fi
2266 return 1
2267}
2268
2269
Ian Wienand6d213df2017-08-22 16:05:16 +10002270# Return just the <major>.<minor> for the given python interpreter
2271function _get_python_version {
2272 local interp=$1
2273 local version
Sean Dague4324f4e2017-09-14 12:59:25 -06002274 # disable erroring out here, otherwise if python 3 doesn't exist we fail hard.
Javier Pena6bd49242017-09-15 15:55:00 +02002275 if [[ -x $(which $interp 2> /dev/null) ]]; then
Sean Dague4324f4e2017-09-14 12:59:25 -06002276 version=$($interp -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
2277 fi
Ian Wienand6d213df2017-08-22 16:05:16 +10002278 echo ${version}
2279}
2280
Chris Dent3a2c86a2015-05-12 13:41:25 +00002281# Return the current python as "python<major>.<minor>"
2282function python_version {
Ian Wienandada886d2015-10-07 14:06:26 +11002283 local python_version
Ian Wienand6d213df2017-08-22 16:05:16 +10002284 python_version=$(_get_python_version python2)
Chris Dent3a2c86a2015-05-12 13:41:25 +00002285 echo "python${python_version}"
2286}
2287
Ian Wienand6d213df2017-08-22 16:05:16 +10002288function python3_version {
2289 local python3_version
2290 python3_version=$(_get_python_version python3)
Doug Hellmann04178582018-06-12 15:37:00 -04002291 echo "python${python3_version}"
Ian Wienand6d213df2017-08-22 16:05:16 +10002292}
2293
2294
Dean Troyerdff49a22014-01-30 15:37:40 -06002295# Service wrapper to restart services
2296# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002297function restart_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002298 if [ -x /bin/systemctl ]; then
2299 sudo /bin/systemctl restart $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002300 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002301 sudo service $1 restart
Dean Troyerdff49a22014-01-30 15:37:40 -06002302 fi
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002303
Dean Troyerdff49a22014-01-30 15:37:40 -06002304}
2305
2306# Only change permissions of a file or directory if it is not on an
2307# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002308function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002309 _safe_permission_operation chmod $@
2310}
2311
2312# Only change ownership of a file or directory if it is not on an NFS
2313# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002314function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002315 _safe_permission_operation chown $@
2316}
2317
2318# Service wrapper to start services
2319# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002320function start_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002321 if [ -x /bin/systemctl ]; then
2322 sudo /bin/systemctl start $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002323 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002324 sudo service $1 start
Dean Troyerdff49a22014-01-30 15:37:40 -06002325 fi
2326}
2327
2328# Service wrapper to stop services
2329# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002330function stop_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002331 if [ -x /bin/systemctl ]; then
2332 sudo /bin/systemctl stop $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002333 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002334 sudo service $1 stop
Dean Troyerdff49a22014-01-30 15:37:40 -06002335 fi
2336}
2337
Attila Fazekasbcaadd62016-11-23 12:43:02 +01002338# Service wrapper to reload services
2339# If the service was not in running state it will start it
Gregory Haynes4b49e402016-08-31 18:19:51 -07002340# reload_service service-name
2341function reload_service {
2342 if [ -x /bin/systemctl ]; then
Attila Fazekasbcaadd62016-11-23 12:43:02 +01002343 sudo /bin/systemctl reload-or-restart $1
Gregory Haynes4b49e402016-08-31 18:19:51 -07002344 else
2345 sudo service $1 reload
2346 fi
2347}
2348
Sean Dague442e4e92015-06-24 13:24:02 -04002349# Test with a finite retry loop.
2350#
2351function test_with_retry {
2352 local testcmd=$1
2353 local failmsg=$2
2354 local until=${3:-10}
2355 local sleep=${4:-0.5}
2356
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002357 time_start "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002358 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2359 die $LINENO "$failmsg"
2360 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002361 time_stop "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002362}
2363
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00002364# Like sudo but forwarding http_proxy https_proxy no_proxy environment vars.
2365# If it is run as superuser then sudo is replaced by env.
2366#
2367function sudo_with_proxies {
2368 local sudo
2369
2370 [[ "$(id -u)" = "0" ]] && sudo="env" || sudo="sudo"
2371
2372 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}"\
2373 no_proxy="${no_proxy:-}" "$@"
2374}
2375
Sean Dague95c33d52015-10-07 11:05:59 -04002376# Timing infrastructure - figure out where large blocks of time are
2377# used in DevStack
2378#
2379# The timing infrastructure for DevStack is about collecting buckets
2380# of time that are spend in some subtask. For instance, that might be
2381# 'apt', 'pip', 'osc', even database migrations. We do this by a pair
2382# of functions: time_start / time_stop.
2383#
2384# These take a single parameter: $name - which specifies the name of
2385# the bucket to be accounted against. time_totals function spits out
2386# the results.
2387#
2388# Resolution is only in whole seconds, so should be used for long
2389# running activities.
2390
Sean Dagueafef8bf2017-03-06 14:07:23 -05002391declare -A -g _TIME_TOTAL
2392declare -A -g _TIME_START
2393declare -r -g _TIME_BEGIN=$(date +%s)
Sean Dague95c33d52015-10-07 11:05:59 -04002394
2395# time_start $name
2396#
2397# starts the clock for a timer by name. Errors if that clock is
2398# already started.
2399function time_start {
2400 local name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002401 local start_time=${_TIME_START[$name]}
Sean Dague95c33d52015-10-07 11:05:59 -04002402 if [[ -n "$start_time" ]]; then
2403 die $LINENO "Trying to start the clock on $name, but it's already been started"
2404 fi
Sean Dague85cf2932017-03-27 15:35:13 -04002405 _TIME_START[$name]=$(date +%s%3N)
Sean Dague95c33d52015-10-07 11:05:59 -04002406}
2407
2408# time_stop $name
2409#
2410# stops the clock for a timer by name, and accumulate that time in the
2411# global counter for that name. Errors if that clock had not
2412# previously been started.
2413function time_stop {
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002414 local name
2415 local end_time
Rob Crittenden38245da2016-05-26 17:55:14 -04002416 local elapsed_time
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002417 local total
2418 local start_time
2419
2420 name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002421 start_time=${_TIME_START[$name]}
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002422
Sean Dague95c33d52015-10-07 11:05:59 -04002423 if [[ -z "$start_time" ]]; then
2424 die $LINENO "Trying to stop the clock on $name, but it was never started"
2425 fi
Sean Dague85cf2932017-03-27 15:35:13 -04002426 end_time=$(date +%s%3N)
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002427 elapsed_time=$(($end_time - $start_time))
Ian Wienand908a3a92016-03-29 14:47:09 +11002428 total=${_TIME_TOTAL[$name]:-0}
Sean Dague95c33d52015-10-07 11:05:59 -04002429 # reset the clock so we can start it in the future
Ian Wienand908a3a92016-03-29 14:47:09 +11002430 _TIME_START[$name]=""
2431 _TIME_TOTAL[$name]=$(($total + $elapsed_time))
Sean Dague95c33d52015-10-07 11:05:59 -04002432}
2433
Sean Dague85cf2932017-03-27 15:35:13 -04002434function oscwrap {
Ian Wienand474f5352019-08-08 09:15:11 +10002435 local xtrace
2436 xtrace=$(set +o | grep xtrace)
2437 set +o xtrace
2438
Sean Dague85cf2932017-03-27 15:35:13 -04002439 local out
2440 local rc
2441 local start
2442 local end
2443 # Cannot use timer_start and timer_stop as we run in subshells
2444 # and those rely on modifying vars in the same process (which cannot
2445 # happen from a subshell.
2446 start=$(date +%s%3N)
2447 out=$(command openstack "$@")
2448 rc=$?
2449 end=$(date +%s%3N)
2450 echo $((end - start)) >> $OSCWRAP_TIMER_FILE
2451
2452 echo "$out"
Ian Wienand474f5352019-08-08 09:15:11 +10002453 $xtrace
Sean Dague85cf2932017-03-27 15:35:13 -04002454 return $rc
2455}
2456
2457function install_oscwrap {
2458 # File to accumulate our timing data
2459 OSCWRAP_TIMER_FILE=$(mktemp)
2460 # Bash by default doesn't expand aliases, allow it for the aliases
2461 # we want to whitelist.
2462 shopt -s expand_aliases
2463 # Remove all aliases that might be expanded to preserve old unexpanded
2464 # behavior
2465 unalias -a
2466 # Add only the alias we want for openstack
2467 alias openstack=oscwrap
2468}
2469
2470function cleanup_oscwrap {
2471 local total=0
Stephen Finucaneffd00472018-01-18 15:12:29 +00002472 total=$(cat $OSCWRAP_TIMER_FILE | $PYTHON -c "import sys; print(sum(int(l) for l in sys.stdin))")
Sean Dague85cf2932017-03-27 15:35:13 -04002473 _TIME_TOTAL["osc"]=$total
2474 rm $OSCWRAP_TIMER_FILE
2475}
2476
Sean Dague95c33d52015-10-07 11:05:59 -04002477# time_totals
Ian Wienand908a3a92016-03-29 14:47:09 +11002478# Print out total time summary
Sean Dague95c33d52015-10-07 11:05:59 -04002479function time_totals {
Ian Wienand908a3a92016-03-29 14:47:09 +11002480 local elapsed_time
2481 local end_time
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002482 local len=20
Ian Wienand908a3a92016-03-29 14:47:09 +11002483 local xtrace
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002484 local unaccounted_time
Ian Wienand908a3a92016-03-29 14:47:09 +11002485
2486 end_time=$(date +%s)
2487 elapsed_time=$(($end_time - $_TIME_BEGIN))
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002488 unaccounted_time=$elapsed_time
Ian Wienand908a3a92016-03-29 14:47:09 +11002489
2490 # pad 1st column this far
2491 for t in ${!_TIME_TOTAL[*]}; do
2492 if [[ ${#t} -gt $len ]]; then
2493 len=${#t}
2494 fi
Sean Dague95c33d52015-10-07 11:05:59 -04002495 done
Ian Wienand908a3a92016-03-29 14:47:09 +11002496
Sean Dague85cf2932017-03-27 15:35:13 -04002497 cleanup_oscwrap
2498
Ian Wienand908a3a92016-03-29 14:47:09 +11002499 xtrace=$(set +o | grep xtrace)
2500 set +o xtrace
2501
2502 echo
2503 echo "========================="
2504 echo "DevStack Component Timing"
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002505 echo " (times are in seconds) "
Ian Wienand908a3a92016-03-29 14:47:09 +11002506 echo "========================="
Ian Wienand908a3a92016-03-29 14:47:09 +11002507 for t in ${!_TIME_TOTAL[*]}; do
2508 local v=${_TIME_TOTAL[$t]}
Sean Dague85cf2932017-03-27 15:35:13 -04002509 # because we're recording in milliseconds
2510 v=$(($v / 1000))
Ian Wienand908a3a92016-03-29 14:47:09 +11002511 printf "%-${len}s %3d\n" "$t" "$v"
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002512 unaccounted_time=$(($unaccounted_time - $v))
Ian Wienand908a3a92016-03-29 14:47:09 +11002513 done
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002514 echo "-------------------------"
2515 printf "%-${len}s %3d\n" "Unaccounted time" "$unaccounted_time"
Ian Wienand908a3a92016-03-29 14:47:09 +11002516 echo "========================="
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002517 printf "%-${len}s %3d\n" "Total runtime" "$elapsed_time"
Ian Wienand908a3a92016-03-29 14:47:09 +11002518
2519 $xtrace
Sean Dague95c33d52015-10-07 11:05:59 -04002520}
Dean Troyerdff49a22014-01-30 15:37:40 -06002521
Sean Mooneyae21b352020-09-01 14:11:45 +00002522function clean_pyc_files {
2523 # Clean up all *.pyc files
2524 if [[ -n "$DEST" ]] && [[ -d "$DEST" ]]; then
2525 sudo find $DEST -name "*.pyc" -delete
2526 fi
2527}
2528
Dean Troyerdff49a22014-01-30 15:37:40 -06002529# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +11002530$_XTRACE_FUNCTIONS_COMMON
Dean Troyerdff49a22014-01-30 15:37:40 -06002531
2532# Local variables:
2533# mode: shell-script
2534# End: