blob: 80f43554d02e0873d6cae9dbb84e80f9193237f9 [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
Ian Wienand7710e7f2014-08-27 16:15:32 +1000391 sudo dnf install -y redhat-lsb-core
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) || \
Mike Trimm1da4e792016-04-27 11:40:19 -0500456 "$os_VENDOR" =~ (Scientific) || \
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300457 "$os_VENDOR" =~ (OracleServer) || \
Evgeny Antyshev718512c2016-03-03 14:47:58 +0000458 "$os_VENDOR" =~ (Virtuozzo) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600459 # Drop the . release as we assume it's compatible
Ian Wienand7710e7f2014-08-27 16:15:32 +1000460 # XXX re-evaluate when we get RHEL10
Dean Troyerdff49a22014-01-30 15:37:40 -0600461 DISTRO="rhel${os_RELEASE::1}"
Dean Troyerdff49a22014-01-30 15:37:40 -0600462 else
Ian Wienanded92e432016-02-16 10:56:56 +1100463 # We can't make a good choice here. Setting a sensible DISTRO
464 # is part of the problem, but not the major issue -- we really
465 # only use DISTRO in the code as a fine-filter.
466 #
467 # The bigger problem is categorising the system into one of
468 # our two big categories as Ubuntu/Debian-ish or
469 # Fedora/CentOS-ish.
470 #
471 # The setting of os_PACKAGE above is only set to "deb" based
472 # on a hard-coded list of vendor names ... thus we will
473 # default to thinking unknown distros are RPM based
474 # (ie. is_ubuntu does not match). But the platform will then
475 # also not match in is_fedora, because that also has a list of
476 # names.
477 #
478 # So, if you are reading this, getting your distro supported
479 # is really about making sure it matches correctly in these
480 # functions. Then you can choose a sensible way to construct
481 # DISTRO based on your distros release approach.
482 die $LINENO "Unable to determine DISTRO, can not continue."
Dean Troyerdff49a22014-01-30 15:37:40 -0600483 fi
Ian Wienand7710e7f2014-08-27 16:15:32 +1000484 typeset -xr DISTRO
Dean Troyerdff49a22014-01-30 15:37:40 -0600485}
486
487# Utility function for checking machine architecture
488# is_arch arch-type
489function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500490 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600491}
492
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700493# Determine if current distribution is an Oracle distribution
494# is_oraclelinux
495function is_oraclelinux {
496 if [[ -z "$os_VENDOR" ]]; then
497 GetOSVersion
498 fi
499
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300500 [ "$os_VENDOR" = "OracleServer" ]
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700501}
502
503
Dean Troyerdff49a22014-01-30 15:37:40 -0600504# Determine if current distribution is a Fedora-based distribution
505# (Fedora, RHEL, CentOS, etc).
506# is_fedora
507function is_fedora {
508 if [[ -z "$os_VENDOR" ]]; then
509 GetOSVersion
510 fi
511
anju Tiwari6c639c92014-07-15 18:11:54 +0530512 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
tengqm78d37392016-03-15 23:08:00 -0400513 [ "$os_VENDOR" = "RedHatEnterpriseServer" ] || \
Carlos Goncalves587e0a32020-08-01 21:47:55 +0200514 [ "$os_VENDOR" = "RedHatEnterprise" ] || \
Sean Mooneye7c017b2020-12-09 23:53:12 +0000515 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "CentOSStream" ] || \
516 [ "$os_VENDOR" = "OracleServer" ] || [ "$os_VENDOR" = "Virtuozzo" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600517}
518
519
520# Determine if current distribution is a SUSE-based distribution
521# (openSUSE, SLE).
522# is_suse
523function is_suse {
Adam Spiersb6f04ca2019-01-23 18:55:16 +0000524 is_opensuse || is_suse_linux_enterprise
525}
526
527
528# Determine if current distribution is an openSUSE distribution
529# is_opensuse
530function is_opensuse {
Dean Troyerdff49a22014-01-30 15:37:40 -0600531 if [[ -z "$os_VENDOR" ]]; then
532 GetOSVersion
533 fi
534
Adam Spiersb6f04ca2019-01-23 18:55:16 +0000535 [[ "$os_VENDOR" =~ (openSUSE) ]]
536}
537
538
539# Determine if current distribution is a SUSE Linux Enterprise (SLE)
540# distribution
541# is_suse_linux_enterprise
542function is_suse_linux_enterprise {
543 if [[ -z "$os_VENDOR" ]]; then
544 GetOSVersion
545 fi
546
547 [[ "$os_VENDOR" =~ (^SUSE) ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600548}
549
550
551# Determine if current distribution is an Ubuntu-based distribution
552# It will also detect non-Ubuntu but Debian-based distros
553# is_ubuntu
554function is_ubuntu {
555 if [[ -z "$os_PACKAGE" ]]; then
556 GetOSVersion
557 fi
558 [ "$os_PACKAGE" = "deb" ]
559}
560
561
562# Git Functions
563# =============
564
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600565# Returns openstack release name for a given branch name
566# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100567function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600568 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700569 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600570 echo ${branch#*/}
571 else
572 echo "master"
573 fi
574}
575
Dean Troyerdff49a22014-01-30 15:37:40 -0600576# git clone only if directory doesn't exist already. Since ``DEST`` might not
577# be owned by the installation user, we create the directory and change the
578# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500579# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
580# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600581# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500582# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600583# git_clone remote dest-dir branch
584function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500585 local git_remote=$1
586 local git_dest=$2
587 local git_ref=$3
Ian Wienandada886d2015-10-07 14:06:26 +1100588 local orig_dir
589 orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200590 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500591
Sean Dague53753292014-12-04 19:38:15 -0500592 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800593 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200594 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
595 fi
596
Dean Troyerdff49a22014-01-30 15:37:40 -0600597 if [[ "$OFFLINE" = "True" ]]; then
598 echo "Running in offline mode, clones already exist"
599 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500600 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600601 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400602 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600603 return
604 fi
605
Dean Troyer50cda692014-07-25 11:57:20 -0500606 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600607 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500608 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700609 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
610 echo "The $git_dest project was not found; if this is a gate job, add"
James E. Blair35a0c572017-09-10 15:37:56 -0700611 echo "the project to 'required-projects' in the job definition."
Ghanshyam Mann325792d2021-10-15 15:55:54 -0500612 die $LINENO "ERROR_ON_CLONE is set to True so cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700613 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200614 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600615 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500616 cd $git_dest
617 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600618 else
619 # do a full clone only if the directory doesn't exist
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"
623 echo "the project to the \$PROJECTS variable 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
Jakub Wachowski90742fc2016-11-18 14:28:47 +0100626 # '--branch' can also take tags
627 git_timed clone $git_clone_flags $git_remote $git_dest --branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600628 elif [[ "$RECLONE" = "True" ]]; then
629 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500630 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600631 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500632 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100633 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600634 # remove the existing ignored files (like pyc) as they cause breakage
635 # (due to the py files having older timestamps than our pyc, so python
636 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500637 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600638
Dean Troyer50cda692014-07-25 11:57:20 -0500639 # handle git_ref accordingly to type (tag, branch)
640 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
641 git_update_tag $git_ref
642 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
643 git_update_branch $git_ref
644 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
645 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600646 else
Dean Troyer50cda692014-07-25 11:57:20 -0500647 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600648 fi
649
650 fi
651 fi
652
653 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500654 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600655 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400656 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600657}
658
Sean Daguecc524062014-10-01 09:06:43 -0400659# A variation on git clone that lets us specify a project by it's
660# actual name, like oslo.config. This is exceptionally useful in the
661# library installation case
662function git_clone_by_name {
663 local name=$1
664 local repo=${GITREPO[$name]}
665 local dir=${GITDIR[$name]}
666 local branch=${GITBRANCH[$name]}
667 git_clone $repo $dir $branch
668}
669
670
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100671# git can sometimes get itself infinitely stuck with transient network
672# errors or other issues with the remote end. This wraps git in a
673# timeout/retry loop and is intended to watch over non-local git
674# processes that might hang. GIT_TIMEOUT, if set, is passed directly
675# to timeout(1); otherwise the default value of 0 maintains the status
676# quo of waiting forever.
677# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100678function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100679 local count=0
680 local timeout=0
681
682 if [[ -n "${GIT_TIMEOUT}" ]]; then
683 timeout=${GIT_TIMEOUT}
684 fi
685
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900686 time_start "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100687 until timeout -s SIGINT ${timeout} git "$@"; do
688 # 124 is timeout(1)'s special return code when it reached the
689 # timeout; otherwise assume fatal failure
690 if [[ $? -ne 124 ]]; then
691 die $LINENO "git call failed: [git $@]"
692 fi
693
694 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400695 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100696 if [ $count -eq 3 ]; then
697 die $LINENO "Maximum of 3 git retries reached"
698 fi
699 sleep 5
700 done
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900701 time_stop "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100702}
703
Dean Troyerdff49a22014-01-30 15:37:40 -0600704# git update using reference as a branch.
705# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100706function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500707 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600708
Dean Troyer50cda692014-07-25 11:57:20 -0500709 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600710 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500711 git branch -D $git_branch || true
712 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600713}
714
715# git update using reference as a branch.
716# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100717function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500718 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600719
Dean Troyer50cda692014-07-25 11:57:20 -0500720 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600721}
722
723# git update using reference as a tag. Be careful editing source at that repo
724# as working copy will be in a detached mode
725# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100726function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500727 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600728
Dean Troyer50cda692014-07-25 11:57:20 -0500729 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600730 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500731 git_timed fetch origin tag $git_tag
732 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600733}
734
735
736# OpenStack Functions
737# ===================
738
739# Get the default value for HOST_IP
740# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100741function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600742 local fixed_range=$1
743 local floating_range=$2
744 local host_ip_iface=$3
745 local host_ip=$4
Brian Haley180f5eb2015-06-16 13:14:31 -0400746 local af=$5
Dean Troyerdff49a22014-01-30 15:37:40 -0600747
Dean Troyerdff49a22014-01-30 15:37:40 -0600748 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
749 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
750 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100751 # Find the interface used for the default route
Brian Haley180f5eb2015-06-16 13:14:31 -0400752 host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
Ian Wienandada886d2015-10-07 14:06:26 +1100753 local host_ips
754 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 -0500755 local ip
756 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600757 # Attempt to filter out IP addresses that are part of the fixed and
758 # floating range. Note that this method only works if the ``netaddr``
759 # python library is installed. If it is not installed, an error
760 # will be printed and the first IP from the interface will be used.
761 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
762 # address.
Brian Haley180f5eb2015-06-16 13:14:31 -0400763 if [[ "$af" == "inet6" ]]; then
764 host_ip=$ip
765 break;
766 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500767 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
768 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600769 break;
770 fi
771 done
772 fi
773 echo $host_ip
774}
775
Attila Fazekasf71b5002014-05-28 09:52:22 +0200776# Generates hex string from ``size`` byte of pseudo random data
777# generate_hex_string size
778function generate_hex_string {
779 local size=$1
780 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
781}
782
Dean Troyerdff49a22014-01-30 15:37:40 -0600783# Grab a numbered field from python prettytable output
784# Fields are numbered starting with 1
785# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
786# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100787function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500788 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600789 while read data; do
790 if [ "$1" -lt 0 ]; then
791 field="(\$(NF$1))"
792 else
793 field="\$$(($1 + 1))"
794 fi
795 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
796 done
797}
798
yuntongjinf26deea2015-02-28 10:50:34 +0800799# install default policy
800# copy over a default policy.json and policy.d for projects
801function install_default_policy {
802 local project=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100803 local project_uc
804 project_uc=$(echo $1|tr a-z A-Z)
yuntongjinf26deea2015-02-28 10:50:34 +0800805 local conf_dir="${project_uc}_CONF_DIR"
806 # eval conf dir to get the variable
807 conf_dir="${!conf_dir}"
808 local project_dir="${project_uc}_DIR"
809 # eval project dir to get the variable
810 project_dir="${!project_dir}"
811 local sample_conf_dir="${project_dir}/etc/${project}"
812 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
813
814 # first copy any policy.json
815 cp -p $sample_conf_dir/policy.json $conf_dir
816 # then optionally copy over policy.d
817 if [[ -d $sample_policy_dir ]]; then
818 cp -r $sample_policy_dir $conf_dir/policy.d
819 fi
820}
821
Dean Troyerdff49a22014-01-30 15:37:40 -0600822# Add a policy to a policy.json file
823# Do nothing if the policy already exists
824# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100825function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600826 local policy_file=$1
827 local policy_name=$2
828 local policy_perm=$3
829
830 if grep -q ${policy_name} ${policy_file}; then
831 echo "Policy ${policy_name} already exists in ${policy_file}"
832 return
833 fi
834
835 # Add a terminating comma to policy lines without one
836 # Remove the closing '}' and all lines following to the end-of-file
Ian Wienandada886d2015-10-07 14:06:26 +1100837 local tmpfile
838 tmpfile=$(mktemp)
Dean Troyerdff49a22014-01-30 15:37:40 -0600839 uniq ${policy_file} | sed -e '
840 s/]$/],/
841 /^[}]/,$d
842 ' > ${tmpfile}
843
844 # Append policy and closing brace
845 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
846 echo "}" >>${tmpfile}
847
848 mv ${tmpfile} ${policy_file}
849}
850
Alistair Coles24779f62014-10-15 18:57:59 +0100851# Gets or creates a domain
852# Usage: get_or_create_domain <name> <description>
853function get_or_create_domain {
Ian Wienand11d276c2015-07-02 09:34:34 +1000854 local domain_id
Alistair Coles24779f62014-10-15 18:57:59 +0100855 # Gets domain id
Ian Wienand11d276c2015-07-02 09:34:34 +1000856 domain_id=$(
Alistair Coles24779f62014-10-15 18:57:59 +0100857 # Gets domain id
Steve Martinelli050a0d52015-09-06 22:03:54 +0000858 openstack domain show $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100859 -f value -c id 2>/dev/null ||
860 # Creates new domain
Steve Martinelli050a0d52015-09-06 22:03:54 +0000861 openstack domain create $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100862 --description "$2" \
863 -f value -c id
864 )
865 echo $domain_id
866}
867
Steve Martinellib74e01c2014-12-18 01:35:35 -0500868# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000869# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500870function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500871 local desc="${3:-}"
Ian Wienand11d276c2015-07-02 09:34:34 +1000872 local group_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500873 # Gets group id
Ian Wienand11d276c2015-07-02 09:34:34 +1000874 group_id=$(
Steve Martinellib74e01c2014-12-18 01:35:35 -0500875 # Creates new group with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000876 openstack group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000877 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500878 -f value -c id
879 )
880 echo $group_id
881}
882
Bartosz Górski0abde392014-02-28 14:15:19 +0100883# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000884# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100885function get_or_create_user {
Ian Wienand11d276c2015-07-02 09:34:34 +1000886 local user_id
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000887 if [[ ! -z "$4" ]]; then
888 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200889 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500890 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200891 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100892 # Gets user id
Ian Wienand11d276c2015-07-02 09:34:34 +1000893 user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500894 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000895 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100896 $1 \
897 --password "$2" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000898 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500899 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500900 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100901 -f value -c id
902 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500903 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100904}
905
906# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000907# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100908function get_or_create_project {
Ian Wienand11d276c2015-07-02 09:34:34 +1000909 local project_id
910 project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500911 # Creates new project with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000912 openstack project create $1 \
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000913 --domain=$2 \
914 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100915 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500916 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100917}
918
919# Gets or creates role
920# Usage: get_or_create_role <name>
921function get_or_create_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000922 local role_id
923 role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500924 # Creates role with --or-show
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000925 openstack role create $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000926 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100927 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500928 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100929}
930
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600931# Returns the domain parts of a function call if present
932# Usage: _get_domain_args [<user_domain> <project_domain>]
933function _get_domain_args {
934 local domain
935 domain=""
936
937 if [[ -n "$1" ]]; then
938 domain="$domain --user-domain $1"
939 fi
940 if [[ -n "$2" ]]; then
941 domain="$domain --project-domain $2"
942 fi
943
944 echo $domain
945}
946
Jamie Lennox9b215db2015-02-10 18:19:57 +1100947# Gets or adds user role to project
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600948# Usage: get_or_add_user_project_role <role> <user> <project> [<user_domain> <project_domain>]
Jamie Lennox9b215db2015-02-10 18:19:57 +1100949function get_or_add_user_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000950 local user_role_id
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600951
952 domain_args=$(_get_domain_args $4 $5)
953
Bartosz Górski0abde392014-02-28 14:15:19 +0100954 # Gets user role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700955 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700956 --role $1 \
Steve Martinelli5541a612015-01-19 15:58:49 -0500957 --user $2 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000958 --project $3 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600959 $domain_args \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700960 | grep '^|\s[a-f0-9]\+' | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500961 if [[ -z "$user_role_id" ]]; then
Roxana Gherle50821be2015-09-22 10:52:46 -0700962 # Adds role to user and get it
963 openstack role add $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100964 --user $2 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600965 --project $3 \
966 $domain_args
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 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700969 --user $2 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700970 --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)
Bartosz Górski0abde392014-02-28 14:15:19 +0100973 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500974 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100975}
976
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500977# Gets or adds user role to domain
978# Usage: get_or_add_user_domain_role <role> <user> <domain>
979function get_or_add_user_domain_role {
980 local user_role_id
981 # Gets user role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700982 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700983 --role $1 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500984 --user $2 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500985 --domain $3 \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700986 | grep '^|\s[a-f0-9]\+' | get_field 1)
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500987 if [[ -z "$user_role_id" ]]; then
988 # Adds role to user and get it
989 openstack role add $1 \
990 --user $2 \
991 --domain $3
Mike Perezc271b3e2016-10-03 16:00:33 -0700992 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700993 --role $1 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500994 --user $2 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500995 --domain $3 \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700996 | grep '^|\s[a-f0-9]\+' | get_field 1)
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500997 fi
998 echo $user_role_id
999}
1000
Lance Bragstad021ae0b2021-03-11 15:47:50 +00001001# Gets or adds user role to system
1002# Usage: get_or_add_user_system_role <role> <user> <system> [<user_domain>]
1003function get_or_add_user_system_role {
1004 local user_role_id
1005 local domain_args
1006
1007 domain_args=$(_get_domain_args $4)
1008
1009 # Gets user role id
1010 user_role_id=$(openstack role assignment list \
1011 --role $1 \
1012 --user $2 \
1013 --system $3 \
1014 $domain_args \
1015 -f value -c Role)
1016 if [[ -z "$user_role_id" ]]; then
1017 # Adds role to user and get it
1018 openstack role add $1 \
1019 --user $2 \
1020 --system $3 \
1021 $domain_args
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 fi
1029 echo $user_role_id
1030}
1031
Steve Martinelli4599fd12015-03-12 21:30:58 -04001032# Gets or adds group role to project
1033# Usage: get_or_add_group_project_role <role> <group> <project>
1034function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +10001035 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -04001036 # Gets group role id
Mike Perezc271b3e2016-10-03 16:00:33 -07001037 group_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -07001038 --role $1 \
Steve Martinelli4599fd12015-03-12 21:30:58 -04001039 --group $2 \
1040 --project $3 \
Mike Perezc271b3e2016-10-03 16:00:33 -07001041 -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -04001042 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +10001043 # Adds role to group and get it
1044 openstack role add $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +10001045 --group $2 \
1046 --project $3
Mike Perezc271b3e2016-10-03 16:00:33 -07001047 group_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -07001048 --role $1 \
Steve Martinelli4599fd12015-03-12 21:30:58 -04001049 --group $2 \
1050 --project $3 \
Mike Perezc271b3e2016-10-03 16:00:33 -07001051 -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -04001052 fi
1053 echo $group_role_id
1054}
1055
Bartosz Górski0abde392014-02-28 14:15:19 +01001056# Gets or creates service
1057# Usage: get_or_create_service <name> <type> <description>
1058function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +10001059 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +01001060 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +10001061 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +01001062 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +10001063 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +01001064 # Creates new service if not exists
1065 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -05001066 $2 \
1067 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +01001068 --description="$3" \
1069 -f value -c id
1070 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001071 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +01001072}
1073
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001074# Create an endpoint with a specific interface
Matt Riedemannae4578b2016-04-23 01:45:40 +00001075# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
1076function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +10001077 local endpoint_id
Matt Riedemannae4578b2016-04-23 01:45:40 +00001078 endpoint_id=$(openstack endpoint list \
1079 --service $1 \
1080 --interface $2 \
1081 --region $4 \
1082 -c ID -f value)
1083 if [[ -z "$endpoint_id" ]]; then
1084 # Creates new endpoint
1085 endpoint_id=$(openstack endpoint create \
1086 $1 $2 $3 --region $4 -f value -c id)
1087 fi
1088
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001089 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +01001090}
Dean Troyerdff49a22014-01-30 15:37:40 -06001091
Sean Dague7d1ec432016-04-22 09:19:10 -04001092# Gets or creates endpoint
Sean Dague11eb2012017-02-13 16:16:59 -05001093# Usage: get_or_create_endpoint <service> <region> <publicurl> [adminurl] [internalurl]
Matt Riedemannae4578b2016-04-23 01:45:40 +00001094function get_or_create_endpoint {
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001095 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
1096 # creating one endpoint with multiple urls to multiple endpoints each with
1097 # a different interface. To maintain the existing function interface we
Matt Riedemannae4578b2016-04-23 01:45:40 +00001098 # create 3 endpoints and return the id of the public one. In reality
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001099 # returning the public id will not make a lot of difference as there are no
1100 # scenarios currently that use the returned id. Ideally this behaviour
1101 # should be pushed out to the service setups and let them create the
1102 # endpoints they need.
Ian Wienandada886d2015-10-07 14:06:26 +11001103 local public_id
Matt Riedemannae4578b2016-04-23 01:45:40 +00001104 public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
Sean Dague11eb2012017-02-13 16:16:59 -05001105 # only create admin/internal urls if provided content for them
1106 if [[ -n "$4" ]]; then
1107 _get_or_create_endpoint_with_interface $1 admin $4 $2
1108 fi
1109 if [[ -n "$5" ]]; then
1110 _get_or_create_endpoint_with_interface $1 internal $5 $2
1111 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001112 # return the public id to indicate success, and this is the endpoint most likely wanted
1113 echo $public_id
1114}
1115
1116# Get a URL from the identity service
1117# Usage: get_endpoint_url <service> <interface>
1118function get_endpoint_url {
1119 echo $(openstack endpoint list \
1120 --service $1 --interface $2 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001121 -c URL -f value)
1122}
1123
Jim Rollenhagen47367072015-12-10 14:24:00 +00001124# check if we are using ironic with hardware
1125# TODO(jroll) this is a kludge left behind when ripping ironic code
1126# out of tree, as it is used by nova and neutron.
1127# figure out a way to refactor nova/neutron code to eliminate this
1128function is_ironic_hardware {
Lucas Alvares Gomes8a4dea22016-02-18 15:52:22 +00001129 is_service_enabled ironic && [[ "$IRONIC_IS_HARDWARE" == "True" ]] && return 0
Jim Rollenhagen47367072015-12-10 14:24:00 +00001130 return 1
1131}
1132
Julia Kreger6af3cb92021-03-11 11:28:47 -08001133function is_ironic_enforce_scope {
1134 is_service_enabled ironic && [[ "$IRONIC_ENFORCE_SCOPE" == "True" ]] && return 0
1135 return 1
1136}
1137
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001138
Dean Troyerdff49a22014-01-30 15:37:40 -06001139# Package Functions
1140# =================
1141
1142# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +11001143function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001144 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -06001145 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001146
1147 if [[ -z "$base_dir" ]]; then
1148 base_dir=$FILES
1149 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001150 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001151 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -06001152 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001153 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -06001154 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001155 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -06001156 else
1157 exit_distro_not_supported "list of packages"
1158 fi
1159 echo "$pkg_dir"
1160}
1161
Sean Dague88ee8ce2015-12-02 07:47:31 -05001162# Wrapper for ``apt-get update`` to try multiple times on the update
1163# to address bad package mirrors (which happen all the time).
1164function apt_get_update {
1165 # only do this once per run
1166 if [[ "$REPOS_UPDATED" == "True" && "$RETRY_UPDATE" != "True" ]]; then
1167 return
1168 fi
1169
1170 # bail if we are offline
1171 [[ "$OFFLINE" = "True" ]] && return
1172
1173 local sudo="sudo"
1174 [[ "$(id -u)" = "0" ]] && sudo="env"
1175
1176 # time all the apt operations
1177 time_start "apt-get-update"
1178
1179 local proxies="http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} no_proxy=${no_proxy:-} "
1180 local update_cmd="$sudo $proxies apt-get update"
1181 if ! timeout 300 sh -c "while ! $update_cmd; do sleep 30; done"; then
1182 die $LINENO "Failed to update apt repos, we're dead now"
1183 fi
1184
1185 REPOS_UPDATED=True
1186 # stop the clock
1187 time_stop "apt-get-update"
1188}
1189
Dean Troyerdff49a22014-01-30 15:37:40 -06001190# Wrapper for ``apt-get`` to set cache and proxy environment variables
1191# Uses globals ``OFFLINE``, ``*_proxy``
1192# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001193function apt_get {
Federico Ressie208d062015-11-21 11:15:39 +00001194 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +11001195 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001196 set +o xtrace
1197
Dean Troyerdff49a22014-01-30 15:37:40 -06001198 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
1199 local sudo="sudo"
1200 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -05001201
Sean Dague95c33d52015-10-07 11:05:59 -04001202 # time all the apt operations
1203 time_start "apt-get"
1204
Sean Dague45917cc2014-02-24 16:09:14 -05001205 $xtrace
Sean Dague53753292014-12-04 19:38:15 -05001206
Dean Troyerdff49a22014-01-30 15:37:40 -06001207 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -05001208 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
1209 no_proxy=${no_proxy:-} \
John L. Villalovosf568c3a2016-01-07 19:10:50 -08001210 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" < /dev/null
Federico Ressie208d062015-11-21 11:15:39 +00001211 result=$?
Sean Dague95c33d52015-10-07 11:05:59 -04001212
1213 # stop the clock
1214 time_stop "apt-get"
Federico Ressie208d062015-11-21 11:15:39 +00001215 return $result
Dean Troyerdff49a22014-01-30 15:37:40 -06001216}
1217
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001218function _parse_package_files {
1219 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -06001220
Dean Troyerdff49a22014-01-30 15:37:40 -06001221 if [[ -z "$DISTRO" ]]; then
1222 GetDistro
1223 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001224
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001225 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001226 local OIFS line package distros distro
1227 [[ -e $fname ]] || continue
1228
1229 OIFS=$IFS
1230 IFS=$'\n'
1231 for line in $(<${fname}); do
1232 if [[ $line =~ "NOPRIME" ]]; then
1233 continue
1234 fi
1235
Ian Wienand1ff75ff2016-02-19 14:28:37 +11001236 # Assume we want this package; free-form
1237 # comments allowed after a #
1238 package=${line%%#*}
Dean Troyerdff49a22014-01-30 15:37:40 -06001239 inst_pkg=1
1240
1241 # Look for # dist:xxx in comment
1242 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1243 # We are using BASH regexp matching feature.
1244 package=${BASH_REMATCH[1]}
1245 distros=${BASH_REMATCH[2]}
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001246 # In bash ${VAR,,} will lowercase VAR
Dean Troyerdff49a22014-01-30 15:37:40 -06001247 # Look for a match in the distro list
1248 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1249 # If no match then skip this package
1250 inst_pkg=0
1251 fi
1252 fi
1253
David Rabel682e0ab2017-03-17 19:19:00 +01001254 # Look for # not:xxx in comment
1255 if [[ $line =~ (.*)#.*not:([^ ]*) ]]; then
1256 # We are using BASH regexp matching feature.
1257 package=${BASH_REMATCH[1]}
1258 distros=${BASH_REMATCH[2]}
1259 # In bash ${VAR,,} will lowercase VAR
1260 # Look for a match in the distro list
1261 if [[ ${distros,,} =~ ${DISTRO,,} ]]; then
1262 # If match then skip this package
1263 inst_pkg=0
1264 fi
1265 fi
1266
Dean Troyerdff49a22014-01-30 15:37:40 -06001267 if [[ $inst_pkg = 1 ]]; then
1268 echo $package
1269 fi
1270 done
1271 IFS=$OIFS
1272 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001273}
1274
1275# get_packages() collects a list of package names of any type from the
1276# prerequisite files in ``files/{debs|rpms}``. The list is intended
1277# to be passed to a package installer such as apt or yum.
1278#
1279# Only packages required for the services in 1st argument will be
1280# included. Two bits of metadata are recognized in the prerequisite files:
1281#
1282# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1283# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1284# of the package to the distros listed. The distro names are case insensitive.
David Rabel682e0ab2017-03-17 19:19:00 +01001285# - ``# not:DISTRO`` or ``not:DISTRO1,DISTRO2`` limits the selection
1286# of the package to the distros not listed. The distro names are case insensitive.
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001287function get_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001288 local xtrace
1289 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001290 set +o xtrace
1291 local services=$@
Ian Wienandada886d2015-10-07 14:06:26 +11001292 local package_dir
1293 package_dir=$(_get_package_dir)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001294 local file_to_parse=""
1295 local service=""
1296
Ian Wienand7d515b52015-11-09 15:04:32 +11001297 if [ $# -ne 1 ]; then
1298 die $LINENO "get_packages takes a single, comma-separated argument"
1299 fi
1300
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001301 if [[ -z "$package_dir" ]]; then
1302 echo "No package directory supplied"
1303 return 1
1304 fi
1305 for service in ${services//,/ }; do
1306 # Allow individual services to specify dependencies
1307 if [[ -e ${package_dir}/${service} ]]; then
1308 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1309 fi
1310 # NOTE(sdague) n-api needs glance for now because that's where
1311 # glance client is
1312 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001313 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001314 file_to_parse="${file_to_parse} ${package_dir}/nova"
1315 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001316 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001317 file_to_parse="${file_to_parse} ${package_dir}/glance"
1318 fi
Luigi Toscano52e52d82020-08-13 09:55:08 +02001319 if [[ ! $file_to_parse =~ $package_dir/os-brick ]]; then
1320 file_to_parse="${file_to_parse} ${package_dir}/os-brick"
1321 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001322 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001323 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001324 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1325 fi
Luigi Toscano52e52d82020-08-13 09:55:08 +02001326 if [[ ! $file_to_parse =~ $package_dir/os-brick ]]; then
1327 file_to_parse="${file_to_parse} ${package_dir}/os-brick"
1328 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001329 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001330 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001331 file_to_parse="${file_to_parse} ${package_dir}/swift"
1332 fi
1333 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001334 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001335 file_to_parse="${file_to_parse} ${package_dir}/nova"
1336 fi
Luigi Toscano52e52d82020-08-13 09:55:08 +02001337 if [[ ! $file_to_parse =~ $package_dir/os-brick ]]; then
1338 file_to_parse="${file_to_parse} ${package_dir}/os-brick"
1339 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001340 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001341 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001342 file_to_parse="${file_to_parse} ${package_dir}/glance"
1343 fi
1344 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001345 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001346 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1347 fi
Ihar Hrachyshka387aadd2017-09-14 09:25:59 -06001348 elif [[ $service == q-* || $service == neutron-* ]]; then
1349 if [[ ! $file_to_parse =~ $package_dir/neutron-common ]]; then
1350 file_to_parse="${file_to_parse} ${package_dir}/neutron-common"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001351 fi
1352 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001353 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001354 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1355 fi
1356 fi
1357 done
1358 echo "$(_parse_package_files $file_to_parse)"
1359 $xtrace
1360}
1361
1362# get_plugin_packages() collects a list of package names of any type from a
1363# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1364# list is intended to be passed to a package installer such as apt or yum.
1365#
1366# Only packages required for enabled and collected plugins will included.
1367#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001368# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001369# in these prerequisite files, see get_packages() for more info.
1370function get_plugin_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001371 local xtrace
1372 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001373 set +o xtrace
1374 local files_to_parse=""
1375 local package_dir=""
1376 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
Ian Wienand7ae97292016-02-16 14:50:53 +11001377 package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
Dmitry Tantsur5979f472016-01-03 18:08:14 +01001378 files_to_parse+=" $package_dir/$plugin"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001379 done
1380 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001381 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001382}
1383
Ian Wienandfa9aadf2019-01-15 18:31:05 +11001384# Search plugins for a bindep.txt file
1385#
1386# Uses globals ``BINDEP_CMD``, ``GITDIR``, ``DEVSTACK_PLUGINS``
1387#
1388# Note this is only valid after BINDEP_CMD is setup in stack.sh, and
1389# is thus not really intended to be called externally.
1390function _get_plugin_bindep_packages {
1391 local xtrace
1392 xtrace=$(set +o | grep xtrace)
1393 set +o xtrace
1394
1395 local bindep_file
1396 local packages
1397
1398 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1399 bindep_file=${GITDIR[$plugin]}/devstack/files/bindep.txt
1400 if [[ -f ${bindep_file} ]]; then
1401 packages+=$($BINDEP_CMD -b --file ${bindep_file} || true)
1402 fi
1403 done
1404 echo "${packages}"
1405 $xtrace
1406}
1407
Dean Troyerdff49a22014-01-30 15:37:40 -06001408# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001409# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001410# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001411function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001412 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1413 REPOS_UPDATED=${REPOS_UPDATED:-False}
1414 RETRY_UPDATE=${RETRY_UPDATE:-False}
1415
Paul Linchpiner9e179742014-07-13 22:23:00 -07001416 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001417 return 0
1418 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001419
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001420 if is_ubuntu; then
Sean Dague88ee8ce2015-12-02 07:47:31 -05001421 apt_get_update
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001422 fi
1423}
1424
1425function real_install_package {
1426 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001427 apt_get install "$@"
1428 elif is_fedora; then
1429 yum_install "$@"
1430 elif is_suse; then
1431 zypper_install "$@"
1432 else
1433 exit_distro_not_supported "installing packages"
1434 fi
1435}
1436
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001437# Distro-agnostic package installer
1438# install_package package [package ...]
1439function install_package {
1440 update_package_repo
Federico Ressiecc1f412015-12-28 15:14:13 +01001441 if ! real_install_package "$@"; then
1442 RETRY_UPDATE=True update_package_repo && real_install_package "$@"
1443 fi
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001444}
1445
Dean Troyerdff49a22014-01-30 15:37:40 -06001446# Distro-agnostic function to tell if a package is installed
1447# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001448function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001449 if [[ -z "$@" ]]; then
1450 return 1
1451 fi
1452
1453 if [[ -z "$os_PACKAGE" ]]; then
1454 GetOSVersion
1455 fi
1456
1457 if [[ "$os_PACKAGE" = "deb" ]]; then
1458 dpkg -s "$@" > /dev/null 2> /dev/null
1459 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1460 rpm --quiet -q "$@"
1461 else
1462 exit_distro_not_supported "finding if a package is installed"
1463 fi
1464}
1465
1466# Distro-agnostic package uninstaller
1467# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001468function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001469 if is_ubuntu; then
1470 apt_get purge "$@"
1471 elif is_fedora; then
Ian Wienand67fd81a2020-04-30 09:24:04 +10001472 sudo dnf remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001473 elif is_suse; then
Gary W. Smith56b39122016-11-16 22:03:43 -08001474 sudo zypper remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001475 else
1476 exit_distro_not_supported "uninstalling packages"
1477 fi
1478}
1479
Ian Wienand67fd81a2020-04-30 09:24:04 +10001480# Wrapper for ``dnf`` to set proxy environment variables
1481# Uses globals ``OFFLINE``, ``*_proxy``
1482# The name is kept for backwards compatability with external
1483# callers, despite none of our supported platforms using yum
1484# any more.
Dean Troyerdff49a22014-01-30 15:37:40 -06001485# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001486function yum_install {
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001487 local result parse_yum_result
1488
Dean Troyerdff49a22014-01-30 15:37:40 -06001489 [[ "$OFFLINE" = "True" ]] && return
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001490
1491 time_start "yum_install"
Ian Wienand67fd81a2020-04-30 09:24:04 +10001492 sudo_with_proxies dnf install -y "$@"
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001493 time_stop "yum_install"
Dean Troyerdff49a22014-01-30 15:37:40 -06001494}
1495
1496# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001497# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001498# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001499function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001500 [[ "$OFFLINE" = "True" ]] && return
1501 local sudo="sudo"
1502 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001503 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1504 no_proxy="${no_proxy:-}" \
Dirk Mueller297a50a2018-06-20 11:08:54 +02001505 zypper --non-interactive install --auto-agree-with-licenses --no-recommends "$@"
Dean Troyerdff49a22014-01-30 15:37:40 -06001506}
1507
Ian Wienand58243f62018-12-13 14:05:53 +11001508# Run bindep and install packages it outputs
1509#
1510# Usage:
1511# install_bindep <path-to-bindep.txt> [profile,profile]
1512#
1513# Note unlike the bindep command itself, profile(s) specified should
1514# be a single, comma-separated string, no spaces.
1515function install_bindep {
1516 local file=$1
1517 local profiles=${2:-""}
1518 local pkgs
1519
1520 if [[ ! -f $file ]]; then
Sean McGinnisdd3731c2020-07-28 08:51:41 -05001521 warn $LINENO "Can not find bindep file: $file"
1522 return
Ian Wienand58243f62018-12-13 14:05:53 +11001523 fi
1524
1525 # converting here makes it much easier to work with passing
1526 # arguments
1527 profiles=${profiles/,/ /}
1528
1529 # Note bindep returns 1 when packages need to be installed, so we
1530 # have to ignore it's return for "-e"
1531 pkgs=$($DEST/bindep-venv/bin/bindep -b --file $file $profiles || true)
1532
1533 if [[ -n "${pkgs}" ]]; then
1534 install_package ${pkgs}
1535 fi
1536}
1537
Sean Dague5edae542017-03-21 20:50:24 -04001538function write_user_unit_file {
1539 local service=$1
1540 local command="$2"
1541 local group=$3
1542 local user=$4
1543 local extra=""
1544 if [[ -n "$group" ]]; then
1545 extra="Group=$group"
1546 fi
1547 local unitfile="$SYSTEMD_DIR/$service"
1548 mkdir -p $SYSTEMD_DIR
1549
1550 iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
1551 iniset -sudo $unitfile "Service" "User" "$user"
1552 iniset -sudo $unitfile "Service" "ExecStart" "$command"
Matthew Treinish477a9622017-08-04 11:09:26 -04001553 iniset -sudo $unitfile "Service" "KillMode" "process"
Michał Dulko3b815a32017-11-14 16:04:51 +01001554 iniset -sudo $unitfile "Service" "TimeoutStopSec" "300"
Dinesh Bhoref60f2b2017-09-05 14:40:32 +05301555 iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
Sean Dague5edae542017-03-21 20:50:24 -04001556 if [[ -n "$group" ]]; then
1557 iniset -sudo $unitfile "Service" "Group" "$group"
1558 fi
1559 iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
1560
1561 # changes to existing units sometimes need a refresh
1562 $SYSTEMCTL daemon-reload
1563}
1564
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001565function write_uwsgi_user_unit_file {
1566 local service=$1
1567 local command="$2"
1568 local group=$3
1569 local user=$4
1570 local unitfile="$SYSTEMD_DIR/$service"
1571 mkdir -p $SYSTEMD_DIR
1572
1573 iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
Sean Daguef41bf2a2017-05-05 07:50:52 -04001574 iniset -sudo $unitfile "Service" "SyslogIdentifier" "$service"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001575 iniset -sudo $unitfile "Service" "User" "$user"
1576 iniset -sudo $unitfile "Service" "ExecStart" "$command"
Dinesh Bhoref60f2b2017-09-05 14:40:32 +05301577 iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001578 iniset -sudo $unitfile "Service" "Type" "notify"
Matthew Treinish477a9622017-08-04 11:09:26 -04001579 iniset -sudo $unitfile "Service" "KillMode" "process"
gong yong sheng07b3bc22017-06-05 14:02:28 +08001580 iniset -sudo $unitfile "Service" "Restart" "always"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001581 iniset -sudo $unitfile "Service" "NotifyAccess" "all"
1582 iniset -sudo $unitfile "Service" "RestartForceExitStatus" "100"
1583
1584 if [[ -n "$group" ]]; then
1585 iniset -sudo $unitfile "Service" "Group" "$group"
1586 fi
1587 iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
1588
1589 # changes to existing units sometimes need a refresh
1590 $SYSTEMCTL daemon-reload
1591}
1592
Sean Dague148d58c2017-05-04 13:08:25 -04001593function _common_systemd_pitfalls {
1594 local cmd=$1
1595 # do some sanity checks on $cmd to see things we don't expect to work
1596
1597 if [[ "$cmd" =~ "sudo" ]]; then
Paweł Suder4e16c3d2018-09-26 15:52:13 +02001598 read -r -d '' msg << EOF || true # read returns 1 for EOF, but it is ok here
Sean Dague148d58c2017-05-04 13:08:25 -04001599You are trying to use run_process with sudo, this is not going to work under systemd.
1600
Ian Wienande8a6a022018-10-08 15:20:34 +11001601If you need to run a service as a user other than \$STACK_USER call it with:
Sean Dague148d58c2017-05-04 13:08:25 -04001602
1603 run_process \$name \$cmd \$group \$user
1604EOF
Ian Wienande8a6a022018-10-08 15:20:34 +11001605 die $LINENO "$msg"
Sean Dague148d58c2017-05-04 13:08:25 -04001606 fi
1607
1608 if [[ ! "$cmd" =~ ^/ ]]; then
Paweł Suder4e16c3d2018-09-26 15:52:13 +02001609 read -r -d '' msg << EOF || true # read returns 1 for EOF, but it is ok here
Sean Dague148d58c2017-05-04 13:08:25 -04001610The cmd="$cmd" does not start with an absolute path. It will fail to
1611start under systemd.
1612
1613Please update your run_process stanza to have an absolute path.
1614EOF
Ian Wienande8a6a022018-10-08 15:20:34 +11001615 die $LINENO "$msg"
Sean Dague148d58c2017-05-04 13:08:25 -04001616 fi
1617
1618}
1619
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001620# Helper function to build a basic unit file and run it under systemd.
1621function _run_under_systemd {
Sean Dague5edae542017-03-21 20:50:24 -04001622 local service=$1
1623 local command="$2"
1624 local cmd=$command
Sean Dague148d58c2017-05-04 13:08:25 -04001625 # sanity check the command
1626 _common_systemd_pitfalls "$cmd"
1627
Sean Dague5edae542017-03-21 20:50:24 -04001628 local systemd_service="devstack@$service.service"
1629 local group=$3
1630 local user=${4:-$STACK_USER}
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001631 if [[ "$command" =~ "uwsgi" ]] ; then
1632 write_uwsgi_user_unit_file $systemd_service "$cmd" "$group" "$user"
1633 else
1634 write_user_unit_file $systemd_service "$cmd" "$group" "$user"
1635 fi
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001636
1637 $SYSTEMCTL enable $systemd_service
1638 $SYSTEMCTL start $systemd_service
Sean Dague5edae542017-03-21 20:50:24 -04001639}
1640
Dean Troyerdff49a22014-01-30 15:37:40 -06001641# Find out if a process exists by partial name.
1642# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001643function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001644 local name=$1
1645 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001646 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001647 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001648 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001649}
1650
Dean Troyer3159a822014-08-27 14:13:58 -05001651# Run a single service under screen or directly
1652# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001653# If an optional group is provided sg will be used to run the
1654# command as that group.
Sean Dague148d58c2017-05-04 13:08:25 -04001655# run_process service "command-line" [group] [user]
Ian Wienandaee18c72014-02-21 15:35:08 +11001656function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001657 local service=$1
1658 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001659 local group=$3
Sean Dague5edae542017-03-21 20:50:24 -04001660 local user=$4
Brant Knudsonedc11c22015-12-14 15:32:05 -06001661
Sean Dague5edae542017-03-21 20:50:24 -04001662 local name=$service
Dean Troyerdff49a22014-01-30 15:37:40 -06001663
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001664 time_start "run_process"
Dean Troyer3159a822014-08-27 14:13:58 -05001665 if is_service_enabled $service; then
Sean Daguecdba1b32017-08-30 11:11:06 -04001666 _run_under_systemd "$name" "$command" "$group" "$user"
Dean Troyer3159a822014-08-27 14:13:58 -05001667 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001668 time_stop "run_process"
Dean Troyerdff49a22014-01-30 15:37:40 -06001669}
1670
Dean Troyer3159a822014-08-27 14:13:58 -05001671# Stop a service process
1672# If a PID is available use it, kill the whole process group via TERM
1673# If screen is being used kill the screen window; this will catch processes
1674# that did not leave a PID behind
Radoslav Gerganov2b97a812017-10-10 16:51:12 +03001675# Uses globals ``SERVICE_DIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001676# stop_process service
1677function stop_process {
1678 local service=$1
1679
1680 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer3159a822014-08-27 14:13:58 -05001681
1682 if is_service_enabled $service; then
Sean Dague803acff2017-05-01 10:52:38 -04001683 # Only do this for units which appear enabled, this also
1684 # catches units that don't really exist for cases like
1685 # keystone without a failure.
1686 if $SYSTEMCTL is-enabled devstack@$service.service; then
1687 $SYSTEMCTL stop devstack@$service.service
1688 $SYSTEMCTL disable devstack@$service.service
Sean Dague5edae542017-03-21 20:50:24 -04001689 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001690 fi
1691}
1692
Sean Daguecdba1b32017-08-30 11:11:06 -04001693# use systemctl to check service status
Ian Wienandaee18c72014-02-21 15:35:08 +11001694function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001695 local service
Sean Daguecdba1b32017-08-30 11:11:06 -04001696 for service in ${ENABLED_SERVICES//,/ }; do
1697 # because some things got renamed like key => keystone
1698 if $SYSTEMCTL is-enabled devstack@$service.service; then
1699 # no-pager is needed because otherwise status dumps to a
1700 # pager when in interactive mode, which will stop a manual
1701 # devstack run.
1702 $SYSTEMCTL status devstack@$service.service --no-pager
Dean Troyer3159a822014-08-27 14:13:58 -05001703 fi
Sean Daguecdba1b32017-08-30 11:11:06 -04001704 done
Dean Troyer3159a822014-08-27 14:13:58 -05001705}
1706
Dean Troyer3159a822014-08-27 14:13:58 -05001707
Sean Dague2c65e712014-12-18 09:44:56 -05001708# Plugin Functions
1709# =================
1710
1711DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1712
1713# enable_plugin <name> <url> [branch]
1714#
1715# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1716# ``url`` is a git url
1717# ``branch`` is a gitref. If it's not set, defaults to master
1718function enable_plugin {
1719 local name=$1
1720 local url=$2
1721 local branch=${3:-master}
Omer Anson51584862017-08-24 17:47:37 +03001722 if is_plugin_enabled $name; then
John L. Villalovos21d84c22016-11-11 15:26:11 -08001723 die $LINENO "Plugin attempted to be enabled twice: ${name} ${url} ${branch}"
1724 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001725 DEVSTACK_PLUGINS+=",$name"
1726 GITREPO[$name]=$url
1727 GITDIR[$name]=$DEST/$name
1728 GITBRANCH[$name]=$branch
1729}
1730
Omer Anson51584862017-08-24 17:47:37 +03001731# is_plugin_enabled <name>
1732#
1733# Check if the plugin was enabled, e.g. using enable_plugin
1734#
1735# ``name`` The name with which the plugin was enabled
1736function is_plugin_enabled {
1737 local name=$1
1738 if [[ ",${DEVSTACK_PLUGINS}," =~ ",${name}," ]]; then
1739 return 0
1740 fi
1741 return 1
1742}
1743
Sean Dague2c65e712014-12-18 09:44:56 -05001744# fetch_plugins
1745#
1746# clones all plugins
1747function fetch_plugins {
1748 local plugins="${DEVSTACK_PLUGINS}"
1749 local plugin
1750
1751 # short circuit if nothing to do
1752 if [[ -z $plugins ]]; then
1753 return
1754 fi
1755
Dean Troyerdc97cb72015-03-28 08:20:50 -05001756 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001757 for plugin in ${plugins//,/ }; do
1758 git_clone_by_name $plugin
1759 done
1760}
1761
1762# load_plugin_settings
1763#
1764# Load settings from plugins in the order that they were registered
1765function load_plugin_settings {
1766 local plugins="${DEVSTACK_PLUGINS}"
1767 local plugin
1768
1769 # short circuit if nothing to do
1770 if [[ -z $plugins ]]; then
1771 return
1772 fi
1773
1774 echo "Loading plugin settings"
1775 for plugin in ${plugins//,/ }; do
1776 local dir=${GITDIR[$plugin]}
1777 # source any known settings
1778 if [[ -f $dir/devstack/settings ]]; then
1779 source $dir/devstack/settings
1780 fi
1781 done
1782}
1783
Sean Dague6e275e12015-03-26 05:54:28 -04001784# plugin_override_defaults
1785#
1786# Run an extremely early setting phase for plugins that allows default
1787# overriding of services.
1788function plugin_override_defaults {
1789 local plugins="${DEVSTACK_PLUGINS}"
1790 local plugin
1791
1792 # short circuit if nothing to do
1793 if [[ -z $plugins ]]; then
1794 return
1795 fi
1796
1797 echo "Overriding Configuration Defaults"
1798 for plugin in ${plugins//,/ }; do
1799 local dir=${GITDIR[$plugin]}
1800 # source any overrides
1801 if [[ -f $dir/devstack/override-defaults ]]; then
1802 # be really verbose that an override is happening, as it
1803 # may not be obvious if things fail later.
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001804 echo "$plugin has overridden the following defaults"
Sean Dague6e275e12015-03-26 05:54:28 -04001805 cat $dir/devstack/override-defaults
1806 source $dir/devstack/override-defaults
1807 fi
1808 done
1809}
1810
Sean Dague2c65e712014-12-18 09:44:56 -05001811# run_plugins
1812#
1813# Run the devstack/plugin.sh in all the plugin directories. These are
1814# run in registration order.
1815function run_plugins {
1816 local mode=$1
1817 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301818
1819 local plugins="${DEVSTACK_PLUGINS}"
1820 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001821 for plugin in ${plugins//,/ }; do
1822 local dir=${GITDIR[$plugin]}
1823 if [[ -f $dir/devstack/plugin.sh ]]; then
1824 source $dir/devstack/plugin.sh $mode $phase
1825 fi
1826 done
1827}
1828
1829function run_phase {
1830 local mode=$1
1831 local phase=$2
1832 if [[ -d $TOP_DIR/extras.d ]]; then
Dmitry Tantsur64be3212015-10-12 13:10:24 +02001833 local extra_plugin_file_name
1834 for extra_plugin_file_name in $TOP_DIR/extras.d/*.sh; do
Sean Dague41d01102015-12-03 08:12:23 -05001835 # NOTE(sdague): only process extras.d for the 3 explicitly
1836 # white listed elements in tree. We want these to move out
1837 # over time as well, but they are in tree, so we need to
1838 # manage that.
Matt Riedemannc9f63272016-08-30 17:21:30 -04001839 local exceptions="80-tempest.sh"
Ian Wienand5cdee8d2015-10-19 14:17:18 +11001840 local extra
1841 extra=$(basename $extra_plugin_file_name)
Sean Dague1de9e332015-10-07 08:46:13 -04001842 if [[ ! ( $exceptions =~ "$extra" ) ]]; then
Sean Dague41d01102015-12-03 08:12:23 -05001843 warn "use of extras.d is no longer supported"
1844 warn "processing of project $extra is skipped"
1845 else
1846 [[ -r $extra_plugin_file_name ]] && source $extra_plugin_file_name $mode $phase
Sean Dague1de9e332015-10-07 08:46:13 -04001847 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001848 done
1849 fi
1850 # the source phase corresponds to settings loading in plugins
1851 if [[ "$mode" == "source" ]]; then
1852 load_plugin_settings
Chris Dentc6d47012015-10-09 14:57:05 +00001853 verify_disabled_services
Sean Dague6e275e12015-03-26 05:54:28 -04001854 elif [[ "$mode" == "override_defaults" ]]; then
1855 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001856 else
1857 run_plugins $mode $phase
1858 fi
1859}
1860
James E. Blairc5853ac2017-11-21 09:44:42 -08001861# define_plugin <name>
1862#
1863# This function is a no-op. It allows a plugin to define its name So
1864# that other plugins may reference it by name. It should generally be
1865# the last component of the canonical git repo name. E.g.,
1866# openstack/devstack-foo should use "devstack-foo" as the name here.
1867#
1868# This function is currently a noop, but the value may still be used
1869# by external tools (as in plugin_requires) and may be used by
1870# devstack in the future.
1871#
1872# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1873function define_plugin {
1874 :
1875}
1876
1877# plugin_requires <name> <other>
1878#
1879# This function is a no-op. It is currently used by external tools
1880# (such as the devstack module for Ansible) to automatically generate
1881# local.conf files. It is not currently used by devstack itself to
1882# resolve dependencies.
1883#
1884# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1885# ``other`` is the name of another plugin
1886function plugin_requires {
1887 :
1888}
1889
Dean Troyerdff49a22014-01-30 15:37:40 -06001890
1891# Service Functions
1892# =================
1893
1894# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1895# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001896function _cleanup_service_list {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001897 local xtrace
1898 xtrace=$(set +o | grep xtrace)
1899 set +o xtrace
1900
Dean Troyerdff49a22014-01-30 15:37:40 -06001901 echo "$1" | sed -e '
1902 s/,,/,/g;
1903 s/^,//;
1904 s/,$//
1905 '
Ian Wienand8043bfa2015-10-14 14:53:18 +11001906
1907 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001908}
1909
1910# disable_all_services() removes all current services
1911# from ``ENABLED_SERVICES`` to reset the configuration
1912# before a minimal installation
1913# Uses global ``ENABLED_SERVICES``
1914# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001915function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001916 ENABLED_SERVICES=""
1917}
1918
1919# Remove all services starting with '-'. For example, to install all default
1920# services except rabbit (rabbit) set in ``localrc``:
1921# ENABLED_SERVICES+=",-rabbit"
1922# Uses global ``ENABLED_SERVICES``
1923# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001924function disable_negated_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001925 local xtrace
1926 xtrace=$(set +o | grep xtrace)
1927 set +o xtrace
1928
Ian Wienand2796a822015-04-15 08:59:04 +10001929 local to_remove=""
1930 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001931 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001932
1933 # build up list of services that should be removed; i.e. they
1934 # begin with "-"
1935 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001936 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001937 to_remove+=",${service#-}"
1938 else
1939 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001940 fi
1941 done
Ian Wienand2796a822015-04-15 08:59:04 +10001942
1943 # go through the service list. if this service appears in the "to
1944 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001945 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001946
1947 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001948}
1949
Chris Dentc6d47012015-10-09 14:57:05 +00001950# disable_service() prepares the services passed as argument to be
1951# removed from the ``ENABLED_SERVICES`` list, if they are present.
Dean Troyerdff49a22014-01-30 15:37:40 -06001952#
1953# For example:
1954# disable_service rabbit
1955#
Chris Dentc6d47012015-10-09 14:57:05 +00001956# Uses global ``DISABLED_SERVICES``
Dean Troyerdff49a22014-01-30 15:37:40 -06001957# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001958function disable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001959 local xtrace
1960 xtrace=$(set +o | grep xtrace)
1961 set +o xtrace
1962
Chris Dentc6d47012015-10-09 14:57:05 +00001963 local disabled_svcs="${DISABLED_SERVICES}"
1964 local enabled_svcs=",${ENABLED_SERVICES},"
Dean Troyerdff49a22014-01-30 15:37:40 -06001965 local service
1966 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001967 disabled_svcs+=",$service"
Dean Troyerdff49a22014-01-30 15:37:40 -06001968 if is_service_enabled $service; then
Chris Dentc6d47012015-10-09 14:57:05 +00001969 enabled_svcs=${enabled_svcs//,$service,/,}
Dean Troyerdff49a22014-01-30 15:37:40 -06001970 fi
1971 done
Chris Dentc6d47012015-10-09 14:57:05 +00001972 DISABLED_SERVICES=$(_cleanup_service_list "$disabled_svcs")
1973 ENABLED_SERVICES=$(_cleanup_service_list "$enabled_svcs")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001974
1975 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001976}
1977
1978# enable_service() adds the services passed as argument to the
1979# ``ENABLED_SERVICES`` list, if they are not already present.
1980#
1981# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001982# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001983#
1984# This function does not know about the special cases
1985# for nova, glance, and neutron built into is_service_enabled().
1986# Uses global ``ENABLED_SERVICES``
1987# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001988function enable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001989 local xtrace
1990 xtrace=$(set +o | grep xtrace)
1991 set +o xtrace
1992
Dean Troyerdff49a22014-01-30 15:37:40 -06001993 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001994 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001995 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001996 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
1997 warn $LINENO "Attempt to enable_service ${service} when it has been disabled"
1998 continue
1999 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06002000 if ! is_service_enabled $service; then
2001 tmpsvcs+=",$service"
2002 fi
2003 done
2004 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
2005 disable_negated_services
Ian Wienand8043bfa2015-10-14 14:53:18 +11002006
2007 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002008}
2009
2010# is_service_enabled() checks if the service(s) specified as arguments are
2011# enabled by the user in ``ENABLED_SERVICES``.
2012#
2013# Multiple services specified as arguments are ``OR``'ed together; the test
2014# is a short-circuit boolean, i.e it returns on the first match.
2015#
2016# There are special cases for some 'catch-all' services::
2017# **nova** returns true if any service enabled start with **n-**
2018# **cinder** returns true if any service enabled start with **c-**
Dean Troyerdff49a22014-01-30 15:37:40 -06002019# **glance** returns true if any service enabled start with **g-**
2020# **neutron** returns true if any service enabled start with **q-**
2021# **swift** returns true if any service enabled start with **s-**
2022# **trove** returns true if any service enabled start with **tr-**
2023# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
2024# **s-** services will be enabled. This will be deprecated in the future.
2025#
Dean Troyerdff49a22014-01-30 15:37:40 -06002026# Uses global ``ENABLED_SERVICES``
2027# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11002028function is_service_enabled {
Ian Wienand433a9b12015-10-07 13:29:31 +11002029 local xtrace
2030 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05002031 set +o xtrace
Ian Wienand8043bfa2015-10-14 14:53:18 +11002032
Sean Dague45917cc2014-02-24 16:09:14 -05002033 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002034 local services=$@
2035 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06002036 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05002037 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002038
2039 # Look for top-level 'enabled' function for this service
2040 if type is_${service}_enabled >/dev/null 2>&1; then
2041 # A function exists for this service, use it
Christian Schwede3db0aad2015-09-10 11:15:39 +00002042 is_${service}_enabled && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002043 fi
2044
2045 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
2046 # are implemented
2047
John Kasperski7b3ae532016-04-24 22:56:04 -05002048 [[ ${service} == n-cpu-* && ,${ENABLED_SERVICES} =~ ,"n-cpu" ]] && enabled=0
2049 [[ ${service} == "nova" && ,${ENABLED_SERVICES} =~ ,"n-" ]] && enabled=0
2050 [[ ${service} == "glance" && ,${ENABLED_SERVICES} =~ ,"g-" ]] && enabled=0
2051 [[ ${service} == "neutron" && ,${ENABLED_SERVICES} =~ ,"q-" ]] && enabled=0
2052 [[ ${service} == "trove" && ,${ENABLED_SERVICES} =~ ,"tr-" ]] && enabled=0
2053 [[ ${service} == "swift" && ,${ENABLED_SERVICES} =~ ,"s-" ]] && enabled=0
2054 [[ ${service} == s-* && ,${ENABLED_SERVICES} =~ ,"swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002055 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11002056
Sean Dague45917cc2014-02-24 16:09:14 -05002057 $xtrace
2058 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06002059}
2060
fumihiko kakuma8606c982015-04-13 09:55:06 +09002061# remove specified list from the input string
2062# remove_disabled_services service-list remove-list
2063function remove_disabled_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11002064 local xtrace
2065 xtrace=$(set +o | grep xtrace)
2066 set +o xtrace
2067
fumihiko kakuma8606c982015-04-13 09:55:06 +09002068 local service_list=$1
2069 local remove_list=$2
2070 local service
2071 local enabled=""
2072
2073 for service in ${service_list//,/ }; do
2074 local remove
2075 local add=1
2076 for remove in ${remove_list//,/ }; do
2077 if [[ ${remove} == ${service} ]]; then
2078 add=0
2079 break
2080 fi
2081 done
2082 if [[ $add == 1 ]]; then
2083 enabled="${enabled},$service"
2084 fi
2085 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11002086
2087 $xtrace
2088
fumihiko kakuma8606c982015-04-13 09:55:06 +09002089 _cleanup_service_list "$enabled"
2090}
2091
Dean Troyerdff49a22014-01-30 15:37:40 -06002092# Toggle enable/disable_service for services that must run exclusive of each other
2093# $1 The name of a variable containing a space-separated list of services
2094# $2 The name of a variable in which to store the enabled service's name
2095# $3 The name of the service to enable
2096function use_exclusive_service {
2097 local options=${!1}
2098 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002099 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06002100 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002101 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06002102 for opt in $options;do
2103 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
2104 done
2105 eval "$out=$selection"
2106 return 0
2107}
2108
Chris Dentc6d47012015-10-09 14:57:05 +00002109# Make sure that nothing has manipulated ENABLED_SERVICES in a way
2110# that conflicts with prior calls to disable_service.
2111# Uses global ``ENABLED_SERVICES``
2112function verify_disabled_services {
2113 local service
2114 for service in ${ENABLED_SERVICES//,/ }; do
2115 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
2116 die $LINENO "ENABLED_SERVICES directly modified to overcome 'disable_service ${service}'"
2117 fi
2118 done
2119}
2120
Dean Troyerdff49a22014-01-30 15:37:40 -06002121
Masayuki Igawaf6368d32014-02-20 13:31:26 +09002122# System Functions
2123# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06002124
2125# Only run the command if the target file (the last arg) is not on an
2126# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002127function _safe_permission_operation {
Ian Wienand433a9b12015-10-07 13:29:31 +11002128 local xtrace
2129 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05002130 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002131 local args=( $@ )
2132 local last
2133 local sudo_cmd
2134 local dir_to_check
2135
2136 let last="${#args[*]} - 1"
2137
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002138 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002139 if [ ! -d "$dir_to_check" ]; then
2140 dir_to_check=`dirname "$dir_to_check"`
2141 fi
2142
2143 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002144 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002145 return 0
2146 fi
2147
Ian Wienandbcb2c302020-01-13 16:31:20 +11002148 sudo_cmd="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06002149
Sean Dague45917cc2014-02-24 16:09:14 -05002150 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002151 $sudo_cmd $@
2152}
2153
2154# Exit 0 if address is in network or 1 if address is not in network
2155# ip-range is in CIDR notation: 1.2.3.4/20
2156# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002157function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002158 local ip=$1
2159 local range=$2
2160 local masklen=${range#*/}
Ian Wienandada886d2015-10-07 14:06:26 +11002161 local network
2162 network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2163 local subnet
2164 subnet=$(maskip $ip $(cidr2netmask $masklen))
Dean Troyerdff49a22014-01-30 15:37:40 -06002165 [[ $network == $subnet ]]
2166}
2167
2168# Add a user to a group.
2169# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002170function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002171 local user=$1
2172 local group=$2
2173
Thomas Bechtolda8580852015-05-31 00:04:33 +02002174 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06002175}
2176
2177# Convert CIDR notation to a IPv4 netmask
2178# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002179function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002180 local maskpat="255 255 255 255"
2181 local maskdgt="254 252 248 240 224 192 128"
2182 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2183 echo ${1-0}.${2-0}.${3-0}.${4-0}
2184}
2185
Ian Cordasco69e3c0a2016-09-26 12:53:14 -05002186# Check if this is a valid ipv4 address string
2187function is_ipv4_address {
2188 local address=$1
Jens Harbott7617ac22017-09-19 17:43:48 +00002189 local regex='([0-9]{1,3}\.){3}[0-9]{1,3}'
Ian Cordasco69e3c0a2016-09-26 12:53:14 -05002190 # TODO(clarkb) make this more robust
2191 if [[ "$address" =~ $regex ]] ; then
2192 return 0
2193 else
2194 return 1
2195 fi
2196}
2197
Jens Harbottdc7b4292017-09-19 10:52:32 +00002198# Remove "[]" around urlquoted IPv6 addresses
2199function ipv6_unquote {
2200 echo $1 | tr -d []
2201}
2202
Dean Troyerdff49a22014-01-30 15:37:40 -06002203# Gracefully cp only if source file/dir exists
2204# cp_it source destination
2205function cp_it {
2206 if [ -e $1 ] || [ -d $1 ]; then
2207 cp -pRL $1 $2
2208 fi
2209}
2210
2211# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2212# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2213# ``localrc`` or on the command line if necessary::
2214#
2215# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2216#
2217# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2218
Ian Wienandaee18c72014-02-21 15:35:08 +11002219function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05002220 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002221 export http_proxy=$http_proxy
2222 fi
Sean Dague53753292014-12-04 19:38:15 -05002223 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002224 export https_proxy=$https_proxy
2225 fi
Sean Dague53753292014-12-04 19:38:15 -05002226 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002227 export no_proxy=$no_proxy
2228 fi
2229}
2230
2231# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002232function is_nfs_directory {
Ian Wienandada886d2015-10-07 14:06:26 +11002233 local mount_type
2234 mount_type=`stat -f -L -c %T $1`
Dean Troyerdff49a22014-01-30 15:37:40 -06002235 test "$mount_type" == "nfs"
2236}
2237
2238# Return the network portion of the given IP address using netmask
2239# netmask is in the traditional dotted-quad format
2240# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002241function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002242 local ip=$1
2243 local mask=$2
2244 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
Ian Wienandada886d2015-10-07 14:06:26 +11002245 local subnet
2246 subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
Dean Troyerdff49a22014-01-30 15:37:40 -06002247 echo $subnet
2248}
2249
Michael Turek7938d832016-04-12 14:55:21 -04002250function is_provider_network {
2251 if [ "$Q_USE_PROVIDER_NETWORKING" == "True" ]; then
2252 return 0
2253 fi
2254 return 1
2255}
2256
2257
Ian Wienand6d213df2017-08-22 16:05:16 +10002258# Return just the <major>.<minor> for the given python interpreter
2259function _get_python_version {
2260 local interp=$1
2261 local version
Sean Dague4324f4e2017-09-14 12:59:25 -06002262 # disable erroring out here, otherwise if python 3 doesn't exist we fail hard.
Javier Pena6bd49242017-09-15 15:55:00 +02002263 if [[ -x $(which $interp 2> /dev/null) ]]; then
Sean Dague4324f4e2017-09-14 12:59:25 -06002264 version=$($interp -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
2265 fi
Ian Wienand6d213df2017-08-22 16:05:16 +10002266 echo ${version}
2267}
2268
Chris Dent3a2c86a2015-05-12 13:41:25 +00002269# Return the current python as "python<major>.<minor>"
2270function python_version {
Ian Wienandada886d2015-10-07 14:06:26 +11002271 local python_version
Ian Wienand6d213df2017-08-22 16:05:16 +10002272 python_version=$(_get_python_version python2)
Chris Dent3a2c86a2015-05-12 13:41:25 +00002273 echo "python${python_version}"
2274}
2275
Ian Wienand6d213df2017-08-22 16:05:16 +10002276function python3_version {
2277 local python3_version
2278 python3_version=$(_get_python_version python3)
Doug Hellmann04178582018-06-12 15:37:00 -04002279 echo "python${python3_version}"
Ian Wienand6d213df2017-08-22 16:05:16 +10002280}
2281
2282
Dean Troyerdff49a22014-01-30 15:37:40 -06002283# Service wrapper to restart services
2284# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002285function restart_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002286 if [ -x /bin/systemctl ]; then
2287 sudo /bin/systemctl restart $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002288 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002289 sudo service $1 restart
Dean Troyerdff49a22014-01-30 15:37:40 -06002290 fi
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002291
Dean Troyerdff49a22014-01-30 15:37:40 -06002292}
2293
2294# Only change permissions of a file or directory if it is not on an
2295# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002296function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002297 _safe_permission_operation chmod $@
2298}
2299
2300# Only change ownership of a file or directory if it is not on an NFS
2301# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002302function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002303 _safe_permission_operation chown $@
2304}
2305
2306# Service wrapper to start services
2307# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002308function start_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002309 if [ -x /bin/systemctl ]; then
2310 sudo /bin/systemctl start $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002311 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002312 sudo service $1 start
Dean Troyerdff49a22014-01-30 15:37:40 -06002313 fi
2314}
2315
2316# Service wrapper to stop services
2317# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002318function stop_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002319 if [ -x /bin/systemctl ]; then
2320 sudo /bin/systemctl stop $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002321 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002322 sudo service $1 stop
Dean Troyerdff49a22014-01-30 15:37:40 -06002323 fi
2324}
2325
Attila Fazekasbcaadd62016-11-23 12:43:02 +01002326# Service wrapper to reload services
2327# If the service was not in running state it will start it
Gregory Haynes4b49e402016-08-31 18:19:51 -07002328# reload_service service-name
2329function reload_service {
2330 if [ -x /bin/systemctl ]; then
Attila Fazekasbcaadd62016-11-23 12:43:02 +01002331 sudo /bin/systemctl reload-or-restart $1
Gregory Haynes4b49e402016-08-31 18:19:51 -07002332 else
2333 sudo service $1 reload
2334 fi
2335}
2336
Sean Dague442e4e92015-06-24 13:24:02 -04002337# Test with a finite retry loop.
2338#
2339function test_with_retry {
2340 local testcmd=$1
2341 local failmsg=$2
2342 local until=${3:-10}
2343 local sleep=${4:-0.5}
2344
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002345 time_start "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002346 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2347 die $LINENO "$failmsg"
2348 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002349 time_stop "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002350}
2351
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00002352# Like sudo but forwarding http_proxy https_proxy no_proxy environment vars.
2353# If it is run as superuser then sudo is replaced by env.
2354#
2355function sudo_with_proxies {
2356 local sudo
2357
2358 [[ "$(id -u)" = "0" ]] && sudo="env" || sudo="sudo"
2359
2360 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}"\
2361 no_proxy="${no_proxy:-}" "$@"
2362}
2363
Sean Dague95c33d52015-10-07 11:05:59 -04002364# Timing infrastructure - figure out where large blocks of time are
2365# used in DevStack
2366#
2367# The timing infrastructure for DevStack is about collecting buckets
2368# of time that are spend in some subtask. For instance, that might be
2369# 'apt', 'pip', 'osc', even database migrations. We do this by a pair
2370# of functions: time_start / time_stop.
2371#
2372# These take a single parameter: $name - which specifies the name of
2373# the bucket to be accounted against. time_totals function spits out
2374# the results.
2375#
2376# Resolution is only in whole seconds, so should be used for long
2377# running activities.
2378
Sean Dagueafef8bf2017-03-06 14:07:23 -05002379declare -A -g _TIME_TOTAL
2380declare -A -g _TIME_START
2381declare -r -g _TIME_BEGIN=$(date +%s)
Sean Dague95c33d52015-10-07 11:05:59 -04002382
2383# time_start $name
2384#
2385# starts the clock for a timer by name. Errors if that clock is
2386# already started.
2387function time_start {
2388 local name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002389 local start_time=${_TIME_START[$name]}
Sean Dague95c33d52015-10-07 11:05:59 -04002390 if [[ -n "$start_time" ]]; then
2391 die $LINENO "Trying to start the clock on $name, but it's already been started"
2392 fi
Sean Dague85cf2932017-03-27 15:35:13 -04002393 _TIME_START[$name]=$(date +%s%3N)
Sean Dague95c33d52015-10-07 11:05:59 -04002394}
2395
2396# time_stop $name
2397#
2398# stops the clock for a timer by name, and accumulate that time in the
2399# global counter for that name. Errors if that clock had not
2400# previously been started.
2401function time_stop {
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002402 local name
2403 local end_time
Rob Crittenden38245da2016-05-26 17:55:14 -04002404 local elapsed_time
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002405 local total
2406 local start_time
2407
2408 name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002409 start_time=${_TIME_START[$name]}
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002410
Sean Dague95c33d52015-10-07 11:05:59 -04002411 if [[ -z "$start_time" ]]; then
2412 die $LINENO "Trying to stop the clock on $name, but it was never started"
2413 fi
Sean Dague85cf2932017-03-27 15:35:13 -04002414 end_time=$(date +%s%3N)
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002415 elapsed_time=$(($end_time - $start_time))
Ian Wienand908a3a92016-03-29 14:47:09 +11002416 total=${_TIME_TOTAL[$name]:-0}
Sean Dague95c33d52015-10-07 11:05:59 -04002417 # reset the clock so we can start it in the future
Ian Wienand908a3a92016-03-29 14:47:09 +11002418 _TIME_START[$name]=""
2419 _TIME_TOTAL[$name]=$(($total + $elapsed_time))
Sean Dague95c33d52015-10-07 11:05:59 -04002420}
2421
Sean Dague85cf2932017-03-27 15:35:13 -04002422function oscwrap {
Ian Wienand474f5352019-08-08 09:15:11 +10002423 local xtrace
2424 xtrace=$(set +o | grep xtrace)
2425 set +o xtrace
2426
Sean Dague85cf2932017-03-27 15:35:13 -04002427 local out
2428 local rc
2429 local start
2430 local end
2431 # Cannot use timer_start and timer_stop as we run in subshells
2432 # and those rely on modifying vars in the same process (which cannot
2433 # happen from a subshell.
2434 start=$(date +%s%3N)
2435 out=$(command openstack "$@")
2436 rc=$?
2437 end=$(date +%s%3N)
2438 echo $((end - start)) >> $OSCWRAP_TIMER_FILE
2439
2440 echo "$out"
Ian Wienand474f5352019-08-08 09:15:11 +10002441 $xtrace
Sean Dague85cf2932017-03-27 15:35:13 -04002442 return $rc
2443}
2444
2445function install_oscwrap {
2446 # File to accumulate our timing data
2447 OSCWRAP_TIMER_FILE=$(mktemp)
2448 # Bash by default doesn't expand aliases, allow it for the aliases
2449 # we want to whitelist.
2450 shopt -s expand_aliases
2451 # Remove all aliases that might be expanded to preserve old unexpanded
2452 # behavior
2453 unalias -a
2454 # Add only the alias we want for openstack
2455 alias openstack=oscwrap
2456}
2457
2458function cleanup_oscwrap {
2459 local total=0
Stephen Finucaneffd00472018-01-18 15:12:29 +00002460 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 -04002461 _TIME_TOTAL["osc"]=$total
2462 rm $OSCWRAP_TIMER_FILE
2463}
2464
Sean Dague95c33d52015-10-07 11:05:59 -04002465# time_totals
Ian Wienand908a3a92016-03-29 14:47:09 +11002466# Print out total time summary
Sean Dague95c33d52015-10-07 11:05:59 -04002467function time_totals {
Ian Wienand908a3a92016-03-29 14:47:09 +11002468 local elapsed_time
2469 local end_time
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002470 local len=20
Ian Wienand908a3a92016-03-29 14:47:09 +11002471 local xtrace
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002472 local unaccounted_time
Ian Wienand908a3a92016-03-29 14:47:09 +11002473
2474 end_time=$(date +%s)
2475 elapsed_time=$(($end_time - $_TIME_BEGIN))
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002476 unaccounted_time=$elapsed_time
Ian Wienand908a3a92016-03-29 14:47:09 +11002477
2478 # pad 1st column this far
2479 for t in ${!_TIME_TOTAL[*]}; do
2480 if [[ ${#t} -gt $len ]]; then
2481 len=${#t}
2482 fi
Sean Dague95c33d52015-10-07 11:05:59 -04002483 done
Ian Wienand908a3a92016-03-29 14:47:09 +11002484
Sean Dague85cf2932017-03-27 15:35:13 -04002485 cleanup_oscwrap
2486
Ian Wienand908a3a92016-03-29 14:47:09 +11002487 xtrace=$(set +o | grep xtrace)
2488 set +o xtrace
2489
2490 echo
2491 echo "========================="
2492 echo "DevStack Component Timing"
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002493 echo " (times are in seconds) "
Ian Wienand908a3a92016-03-29 14:47:09 +11002494 echo "========================="
Ian Wienand908a3a92016-03-29 14:47:09 +11002495 for t in ${!_TIME_TOTAL[*]}; do
2496 local v=${_TIME_TOTAL[$t]}
Sean Dague85cf2932017-03-27 15:35:13 -04002497 # because we're recording in milliseconds
2498 v=$(($v / 1000))
Ian Wienand908a3a92016-03-29 14:47:09 +11002499 printf "%-${len}s %3d\n" "$t" "$v"
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002500 unaccounted_time=$(($unaccounted_time - $v))
Ian Wienand908a3a92016-03-29 14:47:09 +11002501 done
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002502 echo "-------------------------"
2503 printf "%-${len}s %3d\n" "Unaccounted time" "$unaccounted_time"
Ian Wienand908a3a92016-03-29 14:47:09 +11002504 echo "========================="
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002505 printf "%-${len}s %3d\n" "Total runtime" "$elapsed_time"
Ian Wienand908a3a92016-03-29 14:47:09 +11002506
2507 $xtrace
Sean Dague95c33d52015-10-07 11:05:59 -04002508}
Dean Troyerdff49a22014-01-30 15:37:40 -06002509
Sean Mooneyae21b352020-09-01 14:11:45 +00002510function clean_pyc_files {
2511 # Clean up all *.pyc files
2512 if [[ -n "$DEST" ]] && [[ -d "$DEST" ]]; then
2513 sudo find $DEST -name "*.pyc" -delete
2514 fi
2515}
2516
Dean Troyerdff49a22014-01-30 15:37:40 -06002517# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +11002518$_XTRACE_FUNCTIONS_COMMON
Dean Troyerdff49a22014-01-30 15:37:40 -06002519
2520# Local variables:
2521# mode: shell-script
2522# End: