blob: 996827f29285caad343db875fe68cdfb9e31b674 [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 {
Ian Wienand7710e7f2014-08-27 16:15:32 +1000404 # We only support distros that provide a sane lsb_release
405 _ensure_lsb_release
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500406
Ian Wienand7710e7f2014-08-27 16:15:32 +1000407 os_RELEASE=$(lsb_release -r -s)
408 os_CODENAME=$(lsb_release -c -s)
409 os_VENDOR=$(lsb_release -i -s)
Dean Troyerdff49a22014-01-30 15:37:40 -0600410
Ian Wienand7710e7f2014-08-27 16:15:32 +1000411 if [[ $os_VENDOR =~ (Debian|Ubuntu|LinuxMint) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600412 os_PACKAGE="deb"
Ian Wienand7710e7f2014-08-27 16:15:32 +1000413 else
414 os_PACKAGE="rpm"
Dean Troyerdff49a22014-01-30 15:37:40 -0600415 fi
Ian Wienand7710e7f2014-08-27 16:15:32 +1000416
417 typeset -xr os_VENDOR
418 typeset -xr os_RELEASE
419 typeset -xr os_PACKAGE
420 typeset -xr os_CODENAME
Dean Troyerdff49a22014-01-30 15:37:40 -0600421}
422
423# Translate the OS version values into common nomenclature
424# Sets global ``DISTRO`` from the ``os_*`` values
Sean Dagueafef8bf2017-03-06 14:07:23 -0500425declare -g DISTRO
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500426
Ian Wienandaee18c72014-02-21 15:35:08 +1100427function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600428 GetOSVersion
ptoohill196006652016-02-15 16:07:50 -0600429 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) || \
430 "$os_VENDOR" =~ (LinuxMint) ]]; then
431 # 'Everyone' refers to Ubuntu / Debian / Mint releases by
Ian Wienand7710e7f2014-08-27 16:15:32 +1000432 # the code name adjective
Dean Troyerdff49a22014-01-30 15:37:40 -0600433 DISTRO=$os_CODENAME
434 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
435 # For Fedora, just use 'f' and the release
436 DISTRO="f$os_RELEASE"
Adam Spiersb6f04ca2019-01-23 18:55:16 +0000437 elif is_opensuse; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600438 DISTRO="opensuse-$os_RELEASE"
Dirk Mueller4404f682018-03-02 00:37:58 +0100439 # Tumbleweed uses "n/a" as a codename, and the release is a datestring
Dirk Mueller297a50a2018-06-20 11:08:54 +0200440 # like 20180218, so not very useful. Leap however uses a release
441 # with a "dot", so for example 15.0
442 [ "$os_CODENAME" = "n/a" -a "$os_RELEASE" = "${os_RELEASE/\./}" ] && \
443 DISTRO="opensuse-tumbleweed"
Adam Spiersb6f04ca2019-01-23 18:55:16 +0000444 elif is_suse_linux_enterprise; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000445 # just use major release
446 DISTRO="sle${os_RELEASE%.*}"
447 elif [[ "$os_VENDOR" =~ (Red.*Hat) || \
anju Tiwari6c639c92014-07-15 18:11:54 +0530448 "$os_VENDOR" =~ (CentOS) || \
Mike Trimm1da4e792016-04-27 11:40:19 -0500449 "$os_VENDOR" =~ (Scientific) || \
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300450 "$os_VENDOR" =~ (OracleServer) || \
Evgeny Antyshev718512c2016-03-03 14:47:58 +0000451 "$os_VENDOR" =~ (Virtuozzo) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600452 # Drop the . release as we assume it's compatible
Ian Wienand7710e7f2014-08-27 16:15:32 +1000453 # XXX re-evaluate when we get RHEL10
Dean Troyerdff49a22014-01-30 15:37:40 -0600454 DISTRO="rhel${os_RELEASE::1}"
Dean Troyerdff49a22014-01-30 15:37:40 -0600455 else
Ian Wienanded92e432016-02-16 10:56:56 +1100456 # We can't make a good choice here. Setting a sensible DISTRO
457 # is part of the problem, but not the major issue -- we really
458 # only use DISTRO in the code as a fine-filter.
459 #
460 # The bigger problem is categorising the system into one of
461 # our two big categories as Ubuntu/Debian-ish or
462 # Fedora/CentOS-ish.
463 #
464 # The setting of os_PACKAGE above is only set to "deb" based
465 # on a hard-coded list of vendor names ... thus we will
466 # default to thinking unknown distros are RPM based
467 # (ie. is_ubuntu does not match). But the platform will then
468 # also not match in is_fedora, because that also has a list of
469 # names.
470 #
471 # So, if you are reading this, getting your distro supported
472 # is really about making sure it matches correctly in these
473 # functions. Then you can choose a sensible way to construct
474 # DISTRO based on your distros release approach.
475 die $LINENO "Unable to determine DISTRO, can not continue."
Dean Troyerdff49a22014-01-30 15:37:40 -0600476 fi
Ian Wienand7710e7f2014-08-27 16:15:32 +1000477 typeset -xr DISTRO
Dean Troyerdff49a22014-01-30 15:37:40 -0600478}
479
480# Utility function for checking machine architecture
481# is_arch arch-type
482function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500483 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600484}
485
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700486# Determine if current distribution is an Oracle distribution
487# is_oraclelinux
488function is_oraclelinux {
489 if [[ -z "$os_VENDOR" ]]; then
490 GetOSVersion
491 fi
492
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300493 [ "$os_VENDOR" = "OracleServer" ]
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700494}
495
496
Dean Troyerdff49a22014-01-30 15:37:40 -0600497# Determine if current distribution is a Fedora-based distribution
498# (Fedora, RHEL, CentOS, etc).
499# is_fedora
500function is_fedora {
501 if [[ -z "$os_VENDOR" ]]; then
502 GetOSVersion
503 fi
504
anju Tiwari6c639c92014-07-15 18:11:54 +0530505 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
tengqm78d37392016-03-15 23:08:00 -0400506 [ "$os_VENDOR" = "RedHatEnterpriseServer" ] || \
Carlos Goncalves587e0a32020-08-01 21:47:55 +0200507 [ "$os_VENDOR" = "RedHatEnterprise" ] || \
Sean Mooneye7c017b2020-12-09 23:53:12 +0000508 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "CentOSStream" ] || \
509 [ "$os_VENDOR" = "OracleServer" ] || [ "$os_VENDOR" = "Virtuozzo" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600510}
511
512
513# Determine if current distribution is a SUSE-based distribution
514# (openSUSE, SLE).
515# is_suse
516function is_suse {
Adam Spiersb6f04ca2019-01-23 18:55:16 +0000517 is_opensuse || is_suse_linux_enterprise
518}
519
520
521# Determine if current distribution is an openSUSE distribution
522# is_opensuse
523function is_opensuse {
Dean Troyerdff49a22014-01-30 15:37:40 -0600524 if [[ -z "$os_VENDOR" ]]; then
525 GetOSVersion
526 fi
527
Adam Spiersb6f04ca2019-01-23 18:55:16 +0000528 [[ "$os_VENDOR" =~ (openSUSE) ]]
529}
530
531
532# Determine if current distribution is a SUSE Linux Enterprise (SLE)
533# distribution
534# is_suse_linux_enterprise
535function is_suse_linux_enterprise {
536 if [[ -z "$os_VENDOR" ]]; then
537 GetOSVersion
538 fi
539
540 [[ "$os_VENDOR" =~ (^SUSE) ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600541}
542
543
544# Determine if current distribution is an Ubuntu-based distribution
545# It will also detect non-Ubuntu but Debian-based distros
546# is_ubuntu
547function is_ubuntu {
548 if [[ -z "$os_PACKAGE" ]]; then
549 GetOSVersion
550 fi
551 [ "$os_PACKAGE" = "deb" ]
552}
553
554
555# Git Functions
556# =============
557
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600558# Returns openstack release name for a given branch name
559# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100560function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600561 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700562 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600563 echo ${branch#*/}
564 else
565 echo "master"
566 fi
567}
568
Dean Troyerdff49a22014-01-30 15:37:40 -0600569# git clone only if directory doesn't exist already. Since ``DEST`` might not
570# be owned by the installation user, we create the directory and change the
571# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500572# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
573# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600574# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500575# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600576# git_clone remote dest-dir branch
577function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500578 local git_remote=$1
579 local git_dest=$2
580 local git_ref=$3
Ian Wienandada886d2015-10-07 14:06:26 +1100581 local orig_dir
582 orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200583 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500584
Sean Dague53753292014-12-04 19:38:15 -0500585 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800586 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200587 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
588 fi
589
Dean Troyerdff49a22014-01-30 15:37:40 -0600590 if [[ "$OFFLINE" = "True" ]]; then
591 echo "Running in offline mode, clones already exist"
592 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500593 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600594 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400595 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600596 return
597 fi
598
Dean Troyer50cda692014-07-25 11:57:20 -0500599 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600600 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500601 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700602 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
603 echo "The $git_dest project was not found; if this is a gate job, add"
James E. Blair35a0c572017-09-10 15:37:56 -0700604 echo "the project to 'required-projects' in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600605 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700606 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200607 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600608 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500609 cd $git_dest
610 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600611 else
612 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500613 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700614 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
615 echo "The $git_dest project was not found; if this is a gate job, add"
616 echo "the project to the \$PROJECTS variable in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600617 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700618 fi
Jakub Wachowski90742fc2016-11-18 14:28:47 +0100619 # '--branch' can also take tags
620 git_timed clone $git_clone_flags $git_remote $git_dest --branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600621 elif [[ "$RECLONE" = "True" ]]; then
622 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500623 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600624 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500625 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100626 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600627 # remove the existing ignored files (like pyc) as they cause breakage
628 # (due to the py files having older timestamps than our pyc, so python
629 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500630 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600631
Dean Troyer50cda692014-07-25 11:57:20 -0500632 # handle git_ref accordingly to type (tag, branch)
633 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
634 git_update_tag $git_ref
635 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
636 git_update_branch $git_ref
637 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
638 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600639 else
Dean Troyer50cda692014-07-25 11:57:20 -0500640 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600641 fi
642
643 fi
644 fi
645
646 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500647 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600648 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400649 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600650}
651
Sean Daguecc524062014-10-01 09:06:43 -0400652# A variation on git clone that lets us specify a project by it's
653# actual name, like oslo.config. This is exceptionally useful in the
654# library installation case
655function git_clone_by_name {
656 local name=$1
657 local repo=${GITREPO[$name]}
658 local dir=${GITDIR[$name]}
659 local branch=${GITBRANCH[$name]}
660 git_clone $repo $dir $branch
661}
662
663
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100664# git can sometimes get itself infinitely stuck with transient network
665# errors or other issues with the remote end. This wraps git in a
666# timeout/retry loop and is intended to watch over non-local git
667# processes that might hang. GIT_TIMEOUT, if set, is passed directly
668# to timeout(1); otherwise the default value of 0 maintains the status
669# quo of waiting forever.
670# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100671function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100672 local count=0
673 local timeout=0
674
675 if [[ -n "${GIT_TIMEOUT}" ]]; then
676 timeout=${GIT_TIMEOUT}
677 fi
678
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900679 time_start "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100680 until timeout -s SIGINT ${timeout} git "$@"; do
681 # 124 is timeout(1)'s special return code when it reached the
682 # timeout; otherwise assume fatal failure
683 if [[ $? -ne 124 ]]; then
684 die $LINENO "git call failed: [git $@]"
685 fi
686
687 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400688 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100689 if [ $count -eq 3 ]; then
690 die $LINENO "Maximum of 3 git retries reached"
691 fi
692 sleep 5
693 done
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900694 time_stop "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100695}
696
Dean Troyerdff49a22014-01-30 15:37:40 -0600697# git update using reference as a branch.
698# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100699function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500700 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600701
Dean Troyer50cda692014-07-25 11:57:20 -0500702 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600703 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500704 git branch -D $git_branch || true
705 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600706}
707
708# git update using reference as a branch.
709# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100710function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500711 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600712
Dean Troyer50cda692014-07-25 11:57:20 -0500713 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600714}
715
716# git update using reference as a tag. Be careful editing source at that repo
717# as working copy will be in a detached mode
718# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100719function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500720 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600721
Dean Troyer50cda692014-07-25 11:57:20 -0500722 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600723 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500724 git_timed fetch origin tag $git_tag
725 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600726}
727
728
729# OpenStack Functions
730# ===================
731
732# Get the default value for HOST_IP
733# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100734function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600735 local fixed_range=$1
736 local floating_range=$2
737 local host_ip_iface=$3
738 local host_ip=$4
Brian Haley180f5eb2015-06-16 13:14:31 -0400739 local af=$5
Dean Troyerdff49a22014-01-30 15:37:40 -0600740
Dean Troyerdff49a22014-01-30 15:37:40 -0600741 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
742 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
743 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100744 # Find the interface used for the default route
Brian Haley180f5eb2015-06-16 13:14:31 -0400745 host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
Ian Wienandada886d2015-10-07 14:06:26 +1100746 local host_ips
747 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 -0500748 local ip
749 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600750 # Attempt to filter out IP addresses that are part of the fixed and
751 # floating range. Note that this method only works if the ``netaddr``
752 # python library is installed. If it is not installed, an error
753 # will be printed and the first IP from the interface will be used.
754 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
755 # address.
Brian Haley180f5eb2015-06-16 13:14:31 -0400756 if [[ "$af" == "inet6" ]]; then
757 host_ip=$ip
758 break;
759 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500760 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
761 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600762 break;
763 fi
764 done
765 fi
766 echo $host_ip
767}
768
Attila Fazekasf71b5002014-05-28 09:52:22 +0200769# Generates hex string from ``size`` byte of pseudo random data
770# generate_hex_string size
771function generate_hex_string {
772 local size=$1
773 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
774}
775
Dean Troyerdff49a22014-01-30 15:37:40 -0600776# Grab a numbered field from python prettytable output
777# Fields are numbered starting with 1
778# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
779# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100780function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500781 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600782 while read data; do
783 if [ "$1" -lt 0 ]; then
784 field="(\$(NF$1))"
785 else
786 field="\$$(($1 + 1))"
787 fi
788 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
789 done
790}
791
yuntongjinf26deea2015-02-28 10:50:34 +0800792# install default policy
793# copy over a default policy.json and policy.d for projects
794function install_default_policy {
795 local project=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100796 local project_uc
797 project_uc=$(echo $1|tr a-z A-Z)
yuntongjinf26deea2015-02-28 10:50:34 +0800798 local conf_dir="${project_uc}_CONF_DIR"
799 # eval conf dir to get the variable
800 conf_dir="${!conf_dir}"
801 local project_dir="${project_uc}_DIR"
802 # eval project dir to get the variable
803 project_dir="${!project_dir}"
804 local sample_conf_dir="${project_dir}/etc/${project}"
805 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
806
807 # first copy any policy.json
808 cp -p $sample_conf_dir/policy.json $conf_dir
809 # then optionally copy over policy.d
810 if [[ -d $sample_policy_dir ]]; then
811 cp -r $sample_policy_dir $conf_dir/policy.d
812 fi
813}
814
Dean Troyerdff49a22014-01-30 15:37:40 -0600815# Add a policy to a policy.json file
816# Do nothing if the policy already exists
817# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100818function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600819 local policy_file=$1
820 local policy_name=$2
821 local policy_perm=$3
822
823 if grep -q ${policy_name} ${policy_file}; then
824 echo "Policy ${policy_name} already exists in ${policy_file}"
825 return
826 fi
827
828 # Add a terminating comma to policy lines without one
829 # Remove the closing '}' and all lines following to the end-of-file
Ian Wienandada886d2015-10-07 14:06:26 +1100830 local tmpfile
831 tmpfile=$(mktemp)
Dean Troyerdff49a22014-01-30 15:37:40 -0600832 uniq ${policy_file} | sed -e '
833 s/]$/],/
834 /^[}]/,$d
835 ' > ${tmpfile}
836
837 # Append policy and closing brace
838 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
839 echo "}" >>${tmpfile}
840
841 mv ${tmpfile} ${policy_file}
842}
843
Alistair Coles24779f62014-10-15 18:57:59 +0100844# Gets or creates a domain
845# Usage: get_or_create_domain <name> <description>
846function get_or_create_domain {
Ian Wienand11d276c2015-07-02 09:34:34 +1000847 local domain_id
Alistair Coles24779f62014-10-15 18:57:59 +0100848 # Gets domain id
Ian Wienand11d276c2015-07-02 09:34:34 +1000849 domain_id=$(
Alistair Coles24779f62014-10-15 18:57:59 +0100850 # Gets domain id
Steve Martinelli050a0d52015-09-06 22:03:54 +0000851 openstack domain show $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100852 -f value -c id 2>/dev/null ||
853 # Creates new domain
Steve Martinelli050a0d52015-09-06 22:03:54 +0000854 openstack domain create $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100855 --description "$2" \
856 -f value -c id
857 )
858 echo $domain_id
859}
860
Steve Martinellib74e01c2014-12-18 01:35:35 -0500861# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000862# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500863function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500864 local desc="${3:-}"
Ian Wienand11d276c2015-07-02 09:34:34 +1000865 local group_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500866 # Gets group id
Ian Wienand11d276c2015-07-02 09:34:34 +1000867 group_id=$(
Steve Martinellib74e01c2014-12-18 01:35:35 -0500868 # Creates new group with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000869 openstack group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000870 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500871 -f value -c id
872 )
873 echo $group_id
874}
875
Bartosz Górski0abde392014-02-28 14:15:19 +0100876# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000877# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100878function get_or_create_user {
Ian Wienand11d276c2015-07-02 09:34:34 +1000879 local user_id
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000880 if [[ ! -z "$4" ]]; then
881 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200882 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500883 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200884 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100885 # Gets user id
Ian Wienand11d276c2015-07-02 09:34:34 +1000886 user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500887 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000888 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100889 $1 \
890 --password "$2" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000891 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500892 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500893 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100894 -f value -c id
895 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500896 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100897}
898
899# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000900# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100901function get_or_create_project {
Ian Wienand11d276c2015-07-02 09:34:34 +1000902 local project_id
903 project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500904 # Creates new project with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000905 openstack project create $1 \
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000906 --domain=$2 \
907 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100908 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500909 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100910}
911
912# Gets or creates role
913# Usage: get_or_create_role <name>
914function get_or_create_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000915 local role_id
916 role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500917 # Creates role with --or-show
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000918 openstack role create $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000919 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100920 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500921 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100922}
923
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600924# Returns the domain parts of a function call if present
925# Usage: _get_domain_args [<user_domain> <project_domain>]
926function _get_domain_args {
927 local domain
928 domain=""
929
930 if [[ -n "$1" ]]; then
931 domain="$domain --user-domain $1"
932 fi
933 if [[ -n "$2" ]]; then
934 domain="$domain --project-domain $2"
935 fi
936
937 echo $domain
938}
939
Jamie Lennox9b215db2015-02-10 18:19:57 +1100940# Gets or adds user role to project
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600941# Usage: get_or_add_user_project_role <role> <user> <project> [<user_domain> <project_domain>]
Jamie Lennox9b215db2015-02-10 18:19:57 +1100942function get_or_add_user_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000943 local user_role_id
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600944
945 domain_args=$(_get_domain_args $4 $5)
946
Bartosz Górski0abde392014-02-28 14:15:19 +0100947 # Gets user role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700948 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700949 --role $1 \
Steve Martinelli5541a612015-01-19 15:58:49 -0500950 --user $2 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000951 --project $3 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600952 $domain_args \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700953 | grep '^|\s[a-f0-9]\+' | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500954 if [[ -z "$user_role_id" ]]; then
Roxana Gherle50821be2015-09-22 10:52:46 -0700955 # Adds role to user and get it
956 openstack role add $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100957 --user $2 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600958 --project $3 \
959 $domain_args
Mike Perezc271b3e2016-10-03 16:00:33 -0700960 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700961 --role $1 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700962 --user $2 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700963 --project $3 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600964 $domain_args \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700965 | grep '^|\s[a-f0-9]\+' | get_field 1)
Bartosz Górski0abde392014-02-28 14:15:19 +0100966 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500967 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100968}
969
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500970# Gets or adds user role to domain
971# Usage: get_or_add_user_domain_role <role> <user> <domain>
972function get_or_add_user_domain_role {
973 local user_role_id
974 # Gets user role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700975 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700976 --role $1 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500977 --user $2 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500978 --domain $3 \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700979 | grep '^|\s[a-f0-9]\+' | get_field 1)
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500980 if [[ -z "$user_role_id" ]]; then
981 # Adds role to user and get it
982 openstack role add $1 \
983 --user $2 \
984 --domain $3
Mike Perezc271b3e2016-10-03 16:00:33 -0700985 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700986 --role $1 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500987 --user $2 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500988 --domain $3 \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700989 | grep '^|\s[a-f0-9]\+' | get_field 1)
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500990 fi
991 echo $user_role_id
992}
993
Lance Bragstad021ae0b2021-03-11 15:47:50 +0000994# Gets or adds user role to system
995# Usage: get_or_add_user_system_role <role> <user> <system> [<user_domain>]
996function get_or_add_user_system_role {
997 local user_role_id
998 local domain_args
999
1000 domain_args=$(_get_domain_args $4)
1001
1002 # Gets user role id
1003 user_role_id=$(openstack role assignment list \
1004 --role $1 \
1005 --user $2 \
1006 --system $3 \
1007 $domain_args \
1008 -f value -c Role)
1009 if [[ -z "$user_role_id" ]]; then
1010 # Adds role to user and get it
1011 openstack role add $1 \
1012 --user $2 \
1013 --system $3 \
1014 $domain_args
1015 user_role_id=$(openstack role assignment list \
1016 --role $1 \
1017 --user $2 \
1018 --system $3 \
1019 $domain_args \
1020 -f value -c Role)
1021 fi
1022 echo $user_role_id
1023}
1024
Steve Martinelli4599fd12015-03-12 21:30:58 -04001025# Gets or adds group role to project
1026# Usage: get_or_add_group_project_role <role> <group> <project>
1027function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +10001028 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -04001029 # Gets group role id
Mike Perezc271b3e2016-10-03 16:00:33 -07001030 group_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -07001031 --role $1 \
Steve Martinelli4599fd12015-03-12 21:30:58 -04001032 --group $2 \
1033 --project $3 \
Mike Perezc271b3e2016-10-03 16:00:33 -07001034 -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -04001035 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +10001036 # Adds role to group and get it
1037 openstack role add $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +10001038 --group $2 \
1039 --project $3
Mike Perezc271b3e2016-10-03 16:00:33 -07001040 group_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -07001041 --role $1 \
Steve Martinelli4599fd12015-03-12 21:30:58 -04001042 --group $2 \
1043 --project $3 \
Mike Perezc271b3e2016-10-03 16:00:33 -07001044 -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -04001045 fi
1046 echo $group_role_id
1047}
1048
Bartosz Górski0abde392014-02-28 14:15:19 +01001049# Gets or creates service
1050# Usage: get_or_create_service <name> <type> <description>
1051function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +10001052 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +01001053 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +10001054 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +01001055 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +10001056 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +01001057 # Creates new service if not exists
1058 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -05001059 $2 \
1060 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +01001061 --description="$3" \
1062 -f value -c id
1063 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001064 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +01001065}
1066
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001067# Create an endpoint with a specific interface
Matt Riedemannae4578b2016-04-23 01:45:40 +00001068# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
1069function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +10001070 local endpoint_id
Matt Riedemannae4578b2016-04-23 01:45:40 +00001071 endpoint_id=$(openstack endpoint list \
1072 --service $1 \
1073 --interface $2 \
1074 --region $4 \
1075 -c ID -f value)
1076 if [[ -z "$endpoint_id" ]]; then
1077 # Creates new endpoint
1078 endpoint_id=$(openstack endpoint create \
1079 $1 $2 $3 --region $4 -f value -c id)
1080 fi
1081
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001082 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +01001083}
Dean Troyerdff49a22014-01-30 15:37:40 -06001084
Sean Dague7d1ec432016-04-22 09:19:10 -04001085# Gets or creates endpoint
Sean Dague11eb2012017-02-13 16:16:59 -05001086# Usage: get_or_create_endpoint <service> <region> <publicurl> [adminurl] [internalurl]
Matt Riedemannae4578b2016-04-23 01:45:40 +00001087function get_or_create_endpoint {
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001088 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
1089 # creating one endpoint with multiple urls to multiple endpoints each with
1090 # a different interface. To maintain the existing function interface we
Matt Riedemannae4578b2016-04-23 01:45:40 +00001091 # create 3 endpoints and return the id of the public one. In reality
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001092 # returning the public id will not make a lot of difference as there are no
1093 # scenarios currently that use the returned id. Ideally this behaviour
1094 # should be pushed out to the service setups and let them create the
1095 # endpoints they need.
Ian Wienandada886d2015-10-07 14:06:26 +11001096 local public_id
Matt Riedemannae4578b2016-04-23 01:45:40 +00001097 public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
Sean Dague11eb2012017-02-13 16:16:59 -05001098 # only create admin/internal urls if provided content for them
1099 if [[ -n "$4" ]]; then
1100 _get_or_create_endpoint_with_interface $1 admin $4 $2
1101 fi
1102 if [[ -n "$5" ]]; then
1103 _get_or_create_endpoint_with_interface $1 internal $5 $2
1104 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001105 # return the public id to indicate success, and this is the endpoint most likely wanted
1106 echo $public_id
1107}
1108
1109# Get a URL from the identity service
1110# Usage: get_endpoint_url <service> <interface>
1111function get_endpoint_url {
1112 echo $(openstack endpoint list \
1113 --service $1 --interface $2 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001114 -c URL -f value)
1115}
1116
Jim Rollenhagen47367072015-12-10 14:24:00 +00001117# check if we are using ironic with hardware
1118# TODO(jroll) this is a kludge left behind when ripping ironic code
1119# out of tree, as it is used by nova and neutron.
1120# figure out a way to refactor nova/neutron code to eliminate this
1121function is_ironic_hardware {
Lucas Alvares Gomes8a4dea22016-02-18 15:52:22 +00001122 is_service_enabled ironic && [[ "$IRONIC_IS_HARDWARE" == "True" ]] && return 0
Jim Rollenhagen47367072015-12-10 14:24:00 +00001123 return 1
1124}
1125
Julia Kreger6af3cb92021-03-11 11:28:47 -08001126function is_ironic_enforce_scope {
1127 is_service_enabled ironic && [[ "$IRONIC_ENFORCE_SCOPE" == "True" ]] && return 0
1128 return 1
1129}
1130
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001131
Dean Troyerdff49a22014-01-30 15:37:40 -06001132# Package Functions
1133# =================
1134
1135# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +11001136function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001137 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -06001138 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001139
1140 if [[ -z "$base_dir" ]]; then
1141 base_dir=$FILES
1142 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001143 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001144 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -06001145 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001146 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -06001147 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001148 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -06001149 else
1150 exit_distro_not_supported "list of packages"
1151 fi
1152 echo "$pkg_dir"
1153}
1154
Sean Dague88ee8ce2015-12-02 07:47:31 -05001155# Wrapper for ``apt-get update`` to try multiple times on the update
1156# to address bad package mirrors (which happen all the time).
1157function apt_get_update {
1158 # only do this once per run
1159 if [[ "$REPOS_UPDATED" == "True" && "$RETRY_UPDATE" != "True" ]]; then
1160 return
1161 fi
1162
1163 # bail if we are offline
1164 [[ "$OFFLINE" = "True" ]] && return
1165
1166 local sudo="sudo"
1167 [[ "$(id -u)" = "0" ]] && sudo="env"
1168
1169 # time all the apt operations
1170 time_start "apt-get-update"
1171
1172 local proxies="http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} no_proxy=${no_proxy:-} "
1173 local update_cmd="$sudo $proxies apt-get update"
1174 if ! timeout 300 sh -c "while ! $update_cmd; do sleep 30; done"; then
1175 die $LINENO "Failed to update apt repos, we're dead now"
1176 fi
1177
1178 REPOS_UPDATED=True
1179 # stop the clock
1180 time_stop "apt-get-update"
1181}
1182
Dean Troyerdff49a22014-01-30 15:37:40 -06001183# Wrapper for ``apt-get`` to set cache and proxy environment variables
1184# Uses globals ``OFFLINE``, ``*_proxy``
1185# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001186function apt_get {
Federico Ressie208d062015-11-21 11:15:39 +00001187 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +11001188 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001189 set +o xtrace
1190
Dean Troyerdff49a22014-01-30 15:37:40 -06001191 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
1192 local sudo="sudo"
1193 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -05001194
Sean Dague95c33d52015-10-07 11:05:59 -04001195 # time all the apt operations
1196 time_start "apt-get"
1197
Sean Dague45917cc2014-02-24 16:09:14 -05001198 $xtrace
Sean Dague53753292014-12-04 19:38:15 -05001199
Dean Troyerdff49a22014-01-30 15:37:40 -06001200 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -05001201 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
1202 no_proxy=${no_proxy:-} \
John L. Villalovosf568c3a2016-01-07 19:10:50 -08001203 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" < /dev/null
Federico Ressie208d062015-11-21 11:15:39 +00001204 result=$?
Sean Dague95c33d52015-10-07 11:05:59 -04001205
1206 # stop the clock
1207 time_stop "apt-get"
Federico Ressie208d062015-11-21 11:15:39 +00001208 return $result
Dean Troyerdff49a22014-01-30 15:37:40 -06001209}
1210
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001211function _parse_package_files {
1212 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -06001213
Dean Troyerdff49a22014-01-30 15:37:40 -06001214 if [[ -z "$DISTRO" ]]; then
1215 GetDistro
1216 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001217
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001218 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001219 local OIFS line package distros distro
1220 [[ -e $fname ]] || continue
1221
1222 OIFS=$IFS
1223 IFS=$'\n'
1224 for line in $(<${fname}); do
1225 if [[ $line =~ "NOPRIME" ]]; then
1226 continue
1227 fi
1228
Ian Wienand1ff75ff2016-02-19 14:28:37 +11001229 # Assume we want this package; free-form
1230 # comments allowed after a #
1231 package=${line%%#*}
Dean Troyerdff49a22014-01-30 15:37:40 -06001232 inst_pkg=1
1233
1234 # Look for # dist:xxx in comment
1235 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1236 # We are using BASH regexp matching feature.
1237 package=${BASH_REMATCH[1]}
1238 distros=${BASH_REMATCH[2]}
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001239 # In bash ${VAR,,} will lowercase VAR
Dean Troyerdff49a22014-01-30 15:37:40 -06001240 # Look for a match in the distro list
1241 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1242 # If no match then skip this package
1243 inst_pkg=0
1244 fi
1245 fi
1246
David Rabel682e0ab2017-03-17 19:19:00 +01001247 # Look for # not:xxx in comment
1248 if [[ $line =~ (.*)#.*not:([^ ]*) ]]; then
1249 # We are using BASH regexp matching feature.
1250 package=${BASH_REMATCH[1]}
1251 distros=${BASH_REMATCH[2]}
1252 # In bash ${VAR,,} will lowercase VAR
1253 # Look for a match in the distro list
1254 if [[ ${distros,,} =~ ${DISTRO,,} ]]; then
1255 # If match then skip this package
1256 inst_pkg=0
1257 fi
1258 fi
1259
Dean Troyerdff49a22014-01-30 15:37:40 -06001260 if [[ $inst_pkg = 1 ]]; then
1261 echo $package
1262 fi
1263 done
1264 IFS=$OIFS
1265 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001266}
1267
1268# get_packages() collects a list of package names of any type from the
1269# prerequisite files in ``files/{debs|rpms}``. The list is intended
1270# to be passed to a package installer such as apt or yum.
1271#
1272# Only packages required for the services in 1st argument will be
1273# included. Two bits of metadata are recognized in the prerequisite files:
1274#
1275# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1276# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1277# of the package to the distros listed. The distro names are case insensitive.
David Rabel682e0ab2017-03-17 19:19:00 +01001278# - ``# not:DISTRO`` or ``not:DISTRO1,DISTRO2`` limits the selection
1279# of the package to the distros not listed. The distro names are case insensitive.
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001280function get_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001281 local xtrace
1282 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001283 set +o xtrace
1284 local services=$@
Ian Wienandada886d2015-10-07 14:06:26 +11001285 local package_dir
1286 package_dir=$(_get_package_dir)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001287 local file_to_parse=""
1288 local service=""
1289
Ian Wienand7d515b52015-11-09 15:04:32 +11001290 if [ $# -ne 1 ]; then
1291 die $LINENO "get_packages takes a single, comma-separated argument"
1292 fi
1293
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001294 if [[ -z "$package_dir" ]]; then
1295 echo "No package directory supplied"
1296 return 1
1297 fi
1298 for service in ${services//,/ }; do
1299 # Allow individual services to specify dependencies
1300 if [[ -e ${package_dir}/${service} ]]; then
1301 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1302 fi
1303 # NOTE(sdague) n-api needs glance for now because that's where
1304 # glance client is
1305 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001306 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001307 file_to_parse="${file_to_parse} ${package_dir}/nova"
1308 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001309 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001310 file_to_parse="${file_to_parse} ${package_dir}/glance"
1311 fi
Luigi Toscano52e52d82020-08-13 09:55:08 +02001312 if [[ ! $file_to_parse =~ $package_dir/os-brick ]]; then
1313 file_to_parse="${file_to_parse} ${package_dir}/os-brick"
1314 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001315 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001316 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001317 file_to_parse="${file_to_parse} ${package_dir}/cinder"
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 == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001323 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001324 file_to_parse="${file_to_parse} ${package_dir}/swift"
1325 fi
1326 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001327 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001328 file_to_parse="${file_to_parse} ${package_dir}/nova"
1329 fi
Luigi Toscano52e52d82020-08-13 09:55:08 +02001330 if [[ ! $file_to_parse =~ $package_dir/os-brick ]]; then
1331 file_to_parse="${file_to_parse} ${package_dir}/os-brick"
1332 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001333 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001334 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001335 file_to_parse="${file_to_parse} ${package_dir}/glance"
1336 fi
1337 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001338 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001339 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1340 fi
Ihar Hrachyshka387aadd2017-09-14 09:25:59 -06001341 elif [[ $service == q-* || $service == neutron-* ]]; then
1342 if [[ ! $file_to_parse =~ $package_dir/neutron-common ]]; then
1343 file_to_parse="${file_to_parse} ${package_dir}/neutron-common"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001344 fi
1345 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001346 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001347 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1348 fi
1349 fi
1350 done
1351 echo "$(_parse_package_files $file_to_parse)"
1352 $xtrace
1353}
1354
1355# get_plugin_packages() collects a list of package names of any type from a
1356# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1357# list is intended to be passed to a package installer such as apt or yum.
1358#
1359# Only packages required for enabled and collected plugins will included.
1360#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001361# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001362# in these prerequisite files, see get_packages() for more info.
1363function get_plugin_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001364 local xtrace
1365 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001366 set +o xtrace
1367 local files_to_parse=""
1368 local package_dir=""
1369 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
Ian Wienand7ae97292016-02-16 14:50:53 +11001370 package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
Dmitry Tantsur5979f472016-01-03 18:08:14 +01001371 files_to_parse+=" $package_dir/$plugin"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001372 done
1373 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001374 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001375}
1376
Ian Wienandfa9aadf2019-01-15 18:31:05 +11001377# Search plugins for a bindep.txt file
1378#
1379# Uses globals ``BINDEP_CMD``, ``GITDIR``, ``DEVSTACK_PLUGINS``
1380#
1381# Note this is only valid after BINDEP_CMD is setup in stack.sh, and
1382# is thus not really intended to be called externally.
1383function _get_plugin_bindep_packages {
1384 local xtrace
1385 xtrace=$(set +o | grep xtrace)
1386 set +o xtrace
1387
1388 local bindep_file
1389 local packages
1390
1391 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1392 bindep_file=${GITDIR[$plugin]}/devstack/files/bindep.txt
1393 if [[ -f ${bindep_file} ]]; then
1394 packages+=$($BINDEP_CMD -b --file ${bindep_file} || true)
1395 fi
1396 done
1397 echo "${packages}"
1398 $xtrace
1399}
1400
Dean Troyerdff49a22014-01-30 15:37:40 -06001401# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001402# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001403# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001404function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001405 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1406 REPOS_UPDATED=${REPOS_UPDATED:-False}
1407 RETRY_UPDATE=${RETRY_UPDATE:-False}
1408
Paul Linchpiner9e179742014-07-13 22:23:00 -07001409 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001410 return 0
1411 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001412
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001413 if is_ubuntu; then
Sean Dague88ee8ce2015-12-02 07:47:31 -05001414 apt_get_update
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001415 fi
1416}
1417
1418function real_install_package {
1419 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001420 apt_get install "$@"
1421 elif is_fedora; then
1422 yum_install "$@"
1423 elif is_suse; then
1424 zypper_install "$@"
1425 else
1426 exit_distro_not_supported "installing packages"
1427 fi
1428}
1429
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001430# Distro-agnostic package installer
1431# install_package package [package ...]
1432function install_package {
1433 update_package_repo
Federico Ressiecc1f412015-12-28 15:14:13 +01001434 if ! real_install_package "$@"; then
1435 RETRY_UPDATE=True update_package_repo && real_install_package "$@"
1436 fi
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001437}
1438
Dean Troyerdff49a22014-01-30 15:37:40 -06001439# Distro-agnostic function to tell if a package is installed
1440# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001441function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001442 if [[ -z "$@" ]]; then
1443 return 1
1444 fi
1445
1446 if [[ -z "$os_PACKAGE" ]]; then
1447 GetOSVersion
1448 fi
1449
1450 if [[ "$os_PACKAGE" = "deb" ]]; then
1451 dpkg -s "$@" > /dev/null 2> /dev/null
1452 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1453 rpm --quiet -q "$@"
1454 else
1455 exit_distro_not_supported "finding if a package is installed"
1456 fi
1457}
1458
1459# Distro-agnostic package uninstaller
1460# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001461function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001462 if is_ubuntu; then
1463 apt_get purge "$@"
1464 elif is_fedora; then
Ian Wienand67fd81a2020-04-30 09:24:04 +10001465 sudo dnf remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001466 elif is_suse; then
Gary W. Smith56b39122016-11-16 22:03:43 -08001467 sudo zypper remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001468 else
1469 exit_distro_not_supported "uninstalling packages"
1470 fi
1471}
1472
Ian Wienand67fd81a2020-04-30 09:24:04 +10001473# Wrapper for ``dnf`` to set proxy environment variables
1474# Uses globals ``OFFLINE``, ``*_proxy``
1475# The name is kept for backwards compatability with external
1476# callers, despite none of our supported platforms using yum
1477# any more.
Dean Troyerdff49a22014-01-30 15:37:40 -06001478# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001479function yum_install {
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001480 local result parse_yum_result
1481
Dean Troyerdff49a22014-01-30 15:37:40 -06001482 [[ "$OFFLINE" = "True" ]] && return
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001483
1484 time_start "yum_install"
Ian Wienand67fd81a2020-04-30 09:24:04 +10001485 sudo_with_proxies dnf install -y "$@"
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001486 time_stop "yum_install"
Dean Troyerdff49a22014-01-30 15:37:40 -06001487}
1488
1489# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001490# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001491# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001492function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001493 [[ "$OFFLINE" = "True" ]] && return
1494 local sudo="sudo"
1495 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001496 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1497 no_proxy="${no_proxy:-}" \
Dirk Mueller297a50a2018-06-20 11:08:54 +02001498 zypper --non-interactive install --auto-agree-with-licenses --no-recommends "$@"
Dean Troyerdff49a22014-01-30 15:37:40 -06001499}
1500
Ian Wienand58243f62018-12-13 14:05:53 +11001501# Run bindep and install packages it outputs
1502#
1503# Usage:
1504# install_bindep <path-to-bindep.txt> [profile,profile]
1505#
1506# Note unlike the bindep command itself, profile(s) specified should
1507# be a single, comma-separated string, no spaces.
1508function install_bindep {
1509 local file=$1
1510 local profiles=${2:-""}
1511 local pkgs
1512
1513 if [[ ! -f $file ]]; then
Sean McGinnisdd3731c2020-07-28 08:51:41 -05001514 warn $LINENO "Can not find bindep file: $file"
1515 return
Ian Wienand58243f62018-12-13 14:05:53 +11001516 fi
1517
1518 # converting here makes it much easier to work with passing
1519 # arguments
1520 profiles=${profiles/,/ /}
1521
1522 # Note bindep returns 1 when packages need to be installed, so we
1523 # have to ignore it's return for "-e"
1524 pkgs=$($DEST/bindep-venv/bin/bindep -b --file $file $profiles || true)
1525
1526 if [[ -n "${pkgs}" ]]; then
1527 install_package ${pkgs}
1528 fi
1529}
1530
Sean Dague5edae542017-03-21 20:50:24 -04001531function write_user_unit_file {
1532 local service=$1
1533 local command="$2"
1534 local group=$3
1535 local user=$4
1536 local extra=""
1537 if [[ -n "$group" ]]; then
1538 extra="Group=$group"
1539 fi
1540 local unitfile="$SYSTEMD_DIR/$service"
1541 mkdir -p $SYSTEMD_DIR
1542
1543 iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
1544 iniset -sudo $unitfile "Service" "User" "$user"
1545 iniset -sudo $unitfile "Service" "ExecStart" "$command"
Matthew Treinish477a9622017-08-04 11:09:26 -04001546 iniset -sudo $unitfile "Service" "KillMode" "process"
Michał Dulko3b815a32017-11-14 16:04:51 +01001547 iniset -sudo $unitfile "Service" "TimeoutStopSec" "300"
Dinesh Bhoref60f2b2017-09-05 14:40:32 +05301548 iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
Sean Dague5edae542017-03-21 20:50:24 -04001549 if [[ -n "$group" ]]; then
1550 iniset -sudo $unitfile "Service" "Group" "$group"
1551 fi
1552 iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
1553
1554 # changes to existing units sometimes need a refresh
1555 $SYSTEMCTL daemon-reload
1556}
1557
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001558function write_uwsgi_user_unit_file {
1559 local service=$1
1560 local command="$2"
1561 local group=$3
1562 local user=$4
1563 local unitfile="$SYSTEMD_DIR/$service"
1564 mkdir -p $SYSTEMD_DIR
1565
1566 iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
Sean Daguef41bf2a2017-05-05 07:50:52 -04001567 iniset -sudo $unitfile "Service" "SyslogIdentifier" "$service"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001568 iniset -sudo $unitfile "Service" "User" "$user"
1569 iniset -sudo $unitfile "Service" "ExecStart" "$command"
Dinesh Bhoref60f2b2017-09-05 14:40:32 +05301570 iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001571 iniset -sudo $unitfile "Service" "Type" "notify"
Matthew Treinish477a9622017-08-04 11:09:26 -04001572 iniset -sudo $unitfile "Service" "KillMode" "process"
gong yong sheng07b3bc22017-06-05 14:02:28 +08001573 iniset -sudo $unitfile "Service" "Restart" "always"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001574 iniset -sudo $unitfile "Service" "NotifyAccess" "all"
1575 iniset -sudo $unitfile "Service" "RestartForceExitStatus" "100"
1576
1577 if [[ -n "$group" ]]; then
1578 iniset -sudo $unitfile "Service" "Group" "$group"
1579 fi
1580 iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
1581
1582 # changes to existing units sometimes need a refresh
1583 $SYSTEMCTL daemon-reload
1584}
1585
Sean Dague148d58c2017-05-04 13:08:25 -04001586function _common_systemd_pitfalls {
1587 local cmd=$1
1588 # do some sanity checks on $cmd to see things we don't expect to work
1589
1590 if [[ "$cmd" =~ "sudo" ]]; then
Paweł Suder4e16c3d2018-09-26 15:52:13 +02001591 read -r -d '' msg << EOF || true # read returns 1 for EOF, but it is ok here
Sean Dague148d58c2017-05-04 13:08:25 -04001592You are trying to use run_process with sudo, this is not going to work under systemd.
1593
Ian Wienande8a6a022018-10-08 15:20:34 +11001594If you need to run a service as a user other than \$STACK_USER call it with:
Sean Dague148d58c2017-05-04 13:08:25 -04001595
1596 run_process \$name \$cmd \$group \$user
1597EOF
Ian Wienande8a6a022018-10-08 15:20:34 +11001598 die $LINENO "$msg"
Sean Dague148d58c2017-05-04 13:08:25 -04001599 fi
1600
1601 if [[ ! "$cmd" =~ ^/ ]]; then
Paweł Suder4e16c3d2018-09-26 15:52:13 +02001602 read -r -d '' msg << EOF || true # read returns 1 for EOF, but it is ok here
Sean Dague148d58c2017-05-04 13:08:25 -04001603The cmd="$cmd" does not start with an absolute path. It will fail to
1604start under systemd.
1605
1606Please update your run_process stanza to have an absolute path.
1607EOF
Ian Wienande8a6a022018-10-08 15:20:34 +11001608 die $LINENO "$msg"
Sean Dague148d58c2017-05-04 13:08:25 -04001609 fi
1610
1611}
1612
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001613# Helper function to build a basic unit file and run it under systemd.
1614function _run_under_systemd {
Sean Dague5edae542017-03-21 20:50:24 -04001615 local service=$1
1616 local command="$2"
1617 local cmd=$command
Sean Dague148d58c2017-05-04 13:08:25 -04001618 # sanity check the command
1619 _common_systemd_pitfalls "$cmd"
1620
Sean Dague5edae542017-03-21 20:50:24 -04001621 local systemd_service="devstack@$service.service"
1622 local group=$3
1623 local user=${4:-$STACK_USER}
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001624 if [[ "$command" =~ "uwsgi" ]] ; then
1625 write_uwsgi_user_unit_file $systemd_service "$cmd" "$group" "$user"
1626 else
1627 write_user_unit_file $systemd_service "$cmd" "$group" "$user"
1628 fi
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001629
1630 $SYSTEMCTL enable $systemd_service
1631 $SYSTEMCTL start $systemd_service
Sean Dague5edae542017-03-21 20:50:24 -04001632}
1633
Dean Troyerdff49a22014-01-30 15:37:40 -06001634# Find out if a process exists by partial name.
1635# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001636function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001637 local name=$1
1638 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001639 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001640 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001641 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001642}
1643
Dean Troyer3159a822014-08-27 14:13:58 -05001644# Run a single service under screen or directly
1645# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001646# If an optional group is provided sg will be used to run the
1647# command as that group.
Sean Dague148d58c2017-05-04 13:08:25 -04001648# run_process service "command-line" [group] [user]
Ian Wienandaee18c72014-02-21 15:35:08 +11001649function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001650 local service=$1
1651 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001652 local group=$3
Sean Dague5edae542017-03-21 20:50:24 -04001653 local user=$4
Brant Knudsonedc11c22015-12-14 15:32:05 -06001654
Sean Dague5edae542017-03-21 20:50:24 -04001655 local name=$service
Dean Troyerdff49a22014-01-30 15:37:40 -06001656
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001657 time_start "run_process"
Dean Troyer3159a822014-08-27 14:13:58 -05001658 if is_service_enabled $service; then
Sean Daguecdba1b32017-08-30 11:11:06 -04001659 _run_under_systemd "$name" "$command" "$group" "$user"
Dean Troyer3159a822014-08-27 14:13:58 -05001660 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001661 time_stop "run_process"
Dean Troyerdff49a22014-01-30 15:37:40 -06001662}
1663
Dean Troyer3159a822014-08-27 14:13:58 -05001664# Stop a service process
1665# If a PID is available use it, kill the whole process group via TERM
1666# If screen is being used kill the screen window; this will catch processes
1667# that did not leave a PID behind
Radoslav Gerganov2b97a812017-10-10 16:51:12 +03001668# Uses globals ``SERVICE_DIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001669# stop_process service
1670function stop_process {
1671 local service=$1
1672
1673 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer3159a822014-08-27 14:13:58 -05001674
1675 if is_service_enabled $service; then
Sean Dague803acff2017-05-01 10:52:38 -04001676 # Only do this for units which appear enabled, this also
1677 # catches units that don't really exist for cases like
1678 # keystone without a failure.
1679 if $SYSTEMCTL is-enabled devstack@$service.service; then
1680 $SYSTEMCTL stop devstack@$service.service
1681 $SYSTEMCTL disable devstack@$service.service
Sean Dague5edae542017-03-21 20:50:24 -04001682 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001683 fi
1684}
1685
Sean Daguecdba1b32017-08-30 11:11:06 -04001686# use systemctl to check service status
Ian Wienandaee18c72014-02-21 15:35:08 +11001687function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001688 local service
Sean Daguecdba1b32017-08-30 11:11:06 -04001689 for service in ${ENABLED_SERVICES//,/ }; do
1690 # because some things got renamed like key => keystone
1691 if $SYSTEMCTL is-enabled devstack@$service.service; then
1692 # no-pager is needed because otherwise status dumps to a
1693 # pager when in interactive mode, which will stop a manual
1694 # devstack run.
1695 $SYSTEMCTL status devstack@$service.service --no-pager
Dean Troyer3159a822014-08-27 14:13:58 -05001696 fi
Sean Daguecdba1b32017-08-30 11:11:06 -04001697 done
Dean Troyer3159a822014-08-27 14:13:58 -05001698}
1699
Dean Troyer3159a822014-08-27 14:13:58 -05001700
Sean Dague2c65e712014-12-18 09:44:56 -05001701# Plugin Functions
1702# =================
1703
1704DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1705
1706# enable_plugin <name> <url> [branch]
1707#
1708# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1709# ``url`` is a git url
1710# ``branch`` is a gitref. If it's not set, defaults to master
1711function enable_plugin {
1712 local name=$1
1713 local url=$2
1714 local branch=${3:-master}
Omer Anson51584862017-08-24 17:47:37 +03001715 if is_plugin_enabled $name; then
John L. Villalovos21d84c22016-11-11 15:26:11 -08001716 die $LINENO "Plugin attempted to be enabled twice: ${name} ${url} ${branch}"
1717 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001718 DEVSTACK_PLUGINS+=",$name"
1719 GITREPO[$name]=$url
1720 GITDIR[$name]=$DEST/$name
1721 GITBRANCH[$name]=$branch
1722}
1723
Omer Anson51584862017-08-24 17:47:37 +03001724# is_plugin_enabled <name>
1725#
1726# Check if the plugin was enabled, e.g. using enable_plugin
1727#
1728# ``name`` The name with which the plugin was enabled
1729function is_plugin_enabled {
1730 local name=$1
1731 if [[ ",${DEVSTACK_PLUGINS}," =~ ",${name}," ]]; then
1732 return 0
1733 fi
1734 return 1
1735}
1736
Sean Dague2c65e712014-12-18 09:44:56 -05001737# fetch_plugins
1738#
1739# clones all plugins
1740function fetch_plugins {
1741 local plugins="${DEVSTACK_PLUGINS}"
1742 local plugin
1743
1744 # short circuit if nothing to do
1745 if [[ -z $plugins ]]; then
1746 return
1747 fi
1748
Dean Troyerdc97cb72015-03-28 08:20:50 -05001749 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001750 for plugin in ${plugins//,/ }; do
1751 git_clone_by_name $plugin
1752 done
1753}
1754
1755# load_plugin_settings
1756#
1757# Load settings from plugins in the order that they were registered
1758function load_plugin_settings {
1759 local plugins="${DEVSTACK_PLUGINS}"
1760 local plugin
1761
1762 # short circuit if nothing to do
1763 if [[ -z $plugins ]]; then
1764 return
1765 fi
1766
1767 echo "Loading plugin settings"
1768 for plugin in ${plugins//,/ }; do
1769 local dir=${GITDIR[$plugin]}
1770 # source any known settings
1771 if [[ -f $dir/devstack/settings ]]; then
1772 source $dir/devstack/settings
1773 fi
1774 done
1775}
1776
Sean Dague6e275e12015-03-26 05:54:28 -04001777# plugin_override_defaults
1778#
1779# Run an extremely early setting phase for plugins that allows default
1780# overriding of services.
1781function plugin_override_defaults {
1782 local plugins="${DEVSTACK_PLUGINS}"
1783 local plugin
1784
1785 # short circuit if nothing to do
1786 if [[ -z $plugins ]]; then
1787 return
1788 fi
1789
1790 echo "Overriding Configuration Defaults"
1791 for plugin in ${plugins//,/ }; do
1792 local dir=${GITDIR[$plugin]}
1793 # source any overrides
1794 if [[ -f $dir/devstack/override-defaults ]]; then
1795 # be really verbose that an override is happening, as it
1796 # may not be obvious if things fail later.
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001797 echo "$plugin has overridden the following defaults"
Sean Dague6e275e12015-03-26 05:54:28 -04001798 cat $dir/devstack/override-defaults
1799 source $dir/devstack/override-defaults
1800 fi
1801 done
1802}
1803
Sean Dague2c65e712014-12-18 09:44:56 -05001804# run_plugins
1805#
1806# Run the devstack/plugin.sh in all the plugin directories. These are
1807# run in registration order.
1808function run_plugins {
1809 local mode=$1
1810 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301811
1812 local plugins="${DEVSTACK_PLUGINS}"
1813 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001814 for plugin in ${plugins//,/ }; do
1815 local dir=${GITDIR[$plugin]}
1816 if [[ -f $dir/devstack/plugin.sh ]]; then
1817 source $dir/devstack/plugin.sh $mode $phase
1818 fi
1819 done
1820}
1821
1822function run_phase {
1823 local mode=$1
1824 local phase=$2
1825 if [[ -d $TOP_DIR/extras.d ]]; then
Dmitry Tantsur64be3212015-10-12 13:10:24 +02001826 local extra_plugin_file_name
1827 for extra_plugin_file_name in $TOP_DIR/extras.d/*.sh; do
Sean Dague41d01102015-12-03 08:12:23 -05001828 # NOTE(sdague): only process extras.d for the 3 explicitly
1829 # white listed elements in tree. We want these to move out
1830 # over time as well, but they are in tree, so we need to
1831 # manage that.
Matt Riedemannc9f63272016-08-30 17:21:30 -04001832 local exceptions="80-tempest.sh"
Ian Wienand5cdee8d2015-10-19 14:17:18 +11001833 local extra
1834 extra=$(basename $extra_plugin_file_name)
Sean Dague1de9e332015-10-07 08:46:13 -04001835 if [[ ! ( $exceptions =~ "$extra" ) ]]; then
Sean Dague41d01102015-12-03 08:12:23 -05001836 warn "use of extras.d is no longer supported"
1837 warn "processing of project $extra is skipped"
1838 else
1839 [[ -r $extra_plugin_file_name ]] && source $extra_plugin_file_name $mode $phase
Sean Dague1de9e332015-10-07 08:46:13 -04001840 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001841 done
1842 fi
1843 # the source phase corresponds to settings loading in plugins
1844 if [[ "$mode" == "source" ]]; then
1845 load_plugin_settings
Chris Dentc6d47012015-10-09 14:57:05 +00001846 verify_disabled_services
Sean Dague6e275e12015-03-26 05:54:28 -04001847 elif [[ "$mode" == "override_defaults" ]]; then
1848 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001849 else
1850 run_plugins $mode $phase
1851 fi
1852}
1853
James E. Blairc5853ac2017-11-21 09:44:42 -08001854# define_plugin <name>
1855#
1856# This function is a no-op. It allows a plugin to define its name So
1857# that other plugins may reference it by name. It should generally be
1858# the last component of the canonical git repo name. E.g.,
1859# openstack/devstack-foo should use "devstack-foo" as the name here.
1860#
1861# This function is currently a noop, but the value may still be used
1862# by external tools (as in plugin_requires) and may be used by
1863# devstack in the future.
1864#
1865# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1866function define_plugin {
1867 :
1868}
1869
1870# plugin_requires <name> <other>
1871#
1872# This function is a no-op. It is currently used by external tools
1873# (such as the devstack module for Ansible) to automatically generate
1874# local.conf files. It is not currently used by devstack itself to
1875# resolve dependencies.
1876#
1877# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1878# ``other`` is the name of another plugin
1879function plugin_requires {
1880 :
1881}
1882
Dean Troyerdff49a22014-01-30 15:37:40 -06001883
1884# Service Functions
1885# =================
1886
1887# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1888# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001889function _cleanup_service_list {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001890 local xtrace
1891 xtrace=$(set +o | grep xtrace)
1892 set +o xtrace
1893
Dean Troyerdff49a22014-01-30 15:37:40 -06001894 echo "$1" | sed -e '
1895 s/,,/,/g;
1896 s/^,//;
1897 s/,$//
1898 '
Ian Wienand8043bfa2015-10-14 14:53:18 +11001899
1900 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001901}
1902
1903# disable_all_services() removes all current services
1904# from ``ENABLED_SERVICES`` to reset the configuration
1905# before a minimal installation
1906# Uses global ``ENABLED_SERVICES``
1907# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001908function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001909 ENABLED_SERVICES=""
1910}
1911
1912# Remove all services starting with '-'. For example, to install all default
1913# services except rabbit (rabbit) set in ``localrc``:
1914# ENABLED_SERVICES+=",-rabbit"
1915# Uses global ``ENABLED_SERVICES``
1916# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001917function disable_negated_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001918 local xtrace
1919 xtrace=$(set +o | grep xtrace)
1920 set +o xtrace
1921
Ian Wienand2796a822015-04-15 08:59:04 +10001922 local to_remove=""
1923 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001924 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001925
1926 # build up list of services that should be removed; i.e. they
1927 # begin with "-"
1928 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001929 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001930 to_remove+=",${service#-}"
1931 else
1932 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001933 fi
1934 done
Ian Wienand2796a822015-04-15 08:59:04 +10001935
1936 # go through the service list. if this service appears in the "to
1937 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001938 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001939
1940 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001941}
1942
Chris Dentc6d47012015-10-09 14:57:05 +00001943# disable_service() prepares the services passed as argument to be
1944# removed from the ``ENABLED_SERVICES`` list, if they are present.
Dean Troyerdff49a22014-01-30 15:37:40 -06001945#
1946# For example:
1947# disable_service rabbit
1948#
Chris Dentc6d47012015-10-09 14:57:05 +00001949# Uses global ``DISABLED_SERVICES``
Dean Troyerdff49a22014-01-30 15:37:40 -06001950# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001951function disable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001952 local xtrace
1953 xtrace=$(set +o | grep xtrace)
1954 set +o xtrace
1955
Chris Dentc6d47012015-10-09 14:57:05 +00001956 local disabled_svcs="${DISABLED_SERVICES}"
1957 local enabled_svcs=",${ENABLED_SERVICES},"
Dean Troyerdff49a22014-01-30 15:37:40 -06001958 local service
1959 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001960 disabled_svcs+=",$service"
Dean Troyerdff49a22014-01-30 15:37:40 -06001961 if is_service_enabled $service; then
Chris Dentc6d47012015-10-09 14:57:05 +00001962 enabled_svcs=${enabled_svcs//,$service,/,}
Dean Troyerdff49a22014-01-30 15:37:40 -06001963 fi
1964 done
Chris Dentc6d47012015-10-09 14:57:05 +00001965 DISABLED_SERVICES=$(_cleanup_service_list "$disabled_svcs")
1966 ENABLED_SERVICES=$(_cleanup_service_list "$enabled_svcs")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001967
1968 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001969}
1970
1971# enable_service() adds the services passed as argument to the
1972# ``ENABLED_SERVICES`` list, if they are not already present.
1973#
1974# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001975# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001976#
1977# This function does not know about the special cases
1978# for nova, glance, and neutron built into is_service_enabled().
1979# Uses global ``ENABLED_SERVICES``
1980# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001981function enable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001982 local xtrace
1983 xtrace=$(set +o | grep xtrace)
1984 set +o xtrace
1985
Dean Troyerdff49a22014-01-30 15:37:40 -06001986 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001987 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001988 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001989 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
1990 warn $LINENO "Attempt to enable_service ${service} when it has been disabled"
1991 continue
1992 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001993 if ! is_service_enabled $service; then
1994 tmpsvcs+=",$service"
1995 fi
1996 done
1997 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1998 disable_negated_services
Ian Wienand8043bfa2015-10-14 14:53:18 +11001999
2000 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002001}
2002
2003# is_service_enabled() checks if the service(s) specified as arguments are
2004# enabled by the user in ``ENABLED_SERVICES``.
2005#
2006# Multiple services specified as arguments are ``OR``'ed together; the test
2007# is a short-circuit boolean, i.e it returns on the first match.
2008#
2009# There are special cases for some 'catch-all' services::
2010# **nova** returns true if any service enabled start with **n-**
2011# **cinder** returns true if any service enabled start with **c-**
Dean Troyerdff49a22014-01-30 15:37:40 -06002012# **glance** returns true if any service enabled start with **g-**
2013# **neutron** returns true if any service enabled start with **q-**
2014# **swift** returns true if any service enabled start with **s-**
2015# **trove** returns true if any service enabled start with **tr-**
2016# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
2017# **s-** services will be enabled. This will be deprecated in the future.
2018#
Dean Troyerdff49a22014-01-30 15:37:40 -06002019# Uses global ``ENABLED_SERVICES``
2020# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11002021function is_service_enabled {
Ian Wienand433a9b12015-10-07 13:29:31 +11002022 local xtrace
2023 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05002024 set +o xtrace
Ian Wienand8043bfa2015-10-14 14:53:18 +11002025
Sean Dague45917cc2014-02-24 16:09:14 -05002026 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002027 local services=$@
2028 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06002029 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05002030 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002031
2032 # Look for top-level 'enabled' function for this service
2033 if type is_${service}_enabled >/dev/null 2>&1; then
2034 # A function exists for this service, use it
Christian Schwede3db0aad2015-09-10 11:15:39 +00002035 is_${service}_enabled && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002036 fi
2037
2038 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
2039 # are implemented
2040
John Kasperski7b3ae532016-04-24 22:56:04 -05002041 [[ ${service} == n-cpu-* && ,${ENABLED_SERVICES} =~ ,"n-cpu" ]] && enabled=0
2042 [[ ${service} == "nova" && ,${ENABLED_SERVICES} =~ ,"n-" ]] && enabled=0
2043 [[ ${service} == "glance" && ,${ENABLED_SERVICES} =~ ,"g-" ]] && enabled=0
2044 [[ ${service} == "neutron" && ,${ENABLED_SERVICES} =~ ,"q-" ]] && enabled=0
2045 [[ ${service} == "trove" && ,${ENABLED_SERVICES} =~ ,"tr-" ]] && enabled=0
2046 [[ ${service} == "swift" && ,${ENABLED_SERVICES} =~ ,"s-" ]] && enabled=0
2047 [[ ${service} == s-* && ,${ENABLED_SERVICES} =~ ,"swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002048 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11002049
Sean Dague45917cc2014-02-24 16:09:14 -05002050 $xtrace
2051 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06002052}
2053
fumihiko kakuma8606c982015-04-13 09:55:06 +09002054# remove specified list from the input string
2055# remove_disabled_services service-list remove-list
2056function remove_disabled_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11002057 local xtrace
2058 xtrace=$(set +o | grep xtrace)
2059 set +o xtrace
2060
fumihiko kakuma8606c982015-04-13 09:55:06 +09002061 local service_list=$1
2062 local remove_list=$2
2063 local service
2064 local enabled=""
2065
2066 for service in ${service_list//,/ }; do
2067 local remove
2068 local add=1
2069 for remove in ${remove_list//,/ }; do
2070 if [[ ${remove} == ${service} ]]; then
2071 add=0
2072 break
2073 fi
2074 done
2075 if [[ $add == 1 ]]; then
2076 enabled="${enabled},$service"
2077 fi
2078 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11002079
2080 $xtrace
2081
fumihiko kakuma8606c982015-04-13 09:55:06 +09002082 _cleanup_service_list "$enabled"
2083}
2084
Dean Troyerdff49a22014-01-30 15:37:40 -06002085# Toggle enable/disable_service for services that must run exclusive of each other
2086# $1 The name of a variable containing a space-separated list of services
2087# $2 The name of a variable in which to store the enabled service's name
2088# $3 The name of the service to enable
2089function use_exclusive_service {
2090 local options=${!1}
2091 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002092 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06002093 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002094 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06002095 for opt in $options;do
2096 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
2097 done
2098 eval "$out=$selection"
2099 return 0
2100}
2101
Chris Dentc6d47012015-10-09 14:57:05 +00002102# Make sure that nothing has manipulated ENABLED_SERVICES in a way
2103# that conflicts with prior calls to disable_service.
2104# Uses global ``ENABLED_SERVICES``
2105function verify_disabled_services {
2106 local service
2107 for service in ${ENABLED_SERVICES//,/ }; do
2108 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
2109 die $LINENO "ENABLED_SERVICES directly modified to overcome 'disable_service ${service}'"
2110 fi
2111 done
2112}
2113
Dean Troyerdff49a22014-01-30 15:37:40 -06002114
Masayuki Igawaf6368d32014-02-20 13:31:26 +09002115# System Functions
2116# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06002117
2118# Only run the command if the target file (the last arg) is not on an
2119# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002120function _safe_permission_operation {
Ian Wienand433a9b12015-10-07 13:29:31 +11002121 local xtrace
2122 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05002123 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002124 local args=( $@ )
2125 local last
2126 local sudo_cmd
2127 local dir_to_check
2128
2129 let last="${#args[*]} - 1"
2130
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002131 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002132 if [ ! -d "$dir_to_check" ]; then
2133 dir_to_check=`dirname "$dir_to_check"`
2134 fi
2135
2136 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002137 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002138 return 0
2139 fi
2140
Ian Wienandbcb2c302020-01-13 16:31:20 +11002141 sudo_cmd="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06002142
Sean Dague45917cc2014-02-24 16:09:14 -05002143 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002144 $sudo_cmd $@
2145}
2146
2147# Exit 0 if address is in network or 1 if address is not in network
2148# ip-range is in CIDR notation: 1.2.3.4/20
2149# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002150function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002151 local ip=$1
2152 local range=$2
2153 local masklen=${range#*/}
Ian Wienandada886d2015-10-07 14:06:26 +11002154 local network
2155 network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2156 local subnet
2157 subnet=$(maskip $ip $(cidr2netmask $masklen))
Dean Troyerdff49a22014-01-30 15:37:40 -06002158 [[ $network == $subnet ]]
2159}
2160
2161# Add a user to a group.
2162# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002163function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002164 local user=$1
2165 local group=$2
2166
Thomas Bechtolda8580852015-05-31 00:04:33 +02002167 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06002168}
2169
2170# Convert CIDR notation to a IPv4 netmask
2171# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002172function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002173 local maskpat="255 255 255 255"
2174 local maskdgt="254 252 248 240 224 192 128"
2175 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2176 echo ${1-0}.${2-0}.${3-0}.${4-0}
2177}
2178
Ian Cordasco69e3c0a2016-09-26 12:53:14 -05002179# Check if this is a valid ipv4 address string
2180function is_ipv4_address {
2181 local address=$1
Jens Harbott7617ac22017-09-19 17:43:48 +00002182 local regex='([0-9]{1,3}\.){3}[0-9]{1,3}'
Ian Cordasco69e3c0a2016-09-26 12:53:14 -05002183 # TODO(clarkb) make this more robust
2184 if [[ "$address" =~ $regex ]] ; then
2185 return 0
2186 else
2187 return 1
2188 fi
2189}
2190
Jens Harbottdc7b4292017-09-19 10:52:32 +00002191# Remove "[]" around urlquoted IPv6 addresses
2192function ipv6_unquote {
2193 echo $1 | tr -d []
2194}
2195
Dean Troyerdff49a22014-01-30 15:37:40 -06002196# Gracefully cp only if source file/dir exists
2197# cp_it source destination
2198function cp_it {
2199 if [ -e $1 ] || [ -d $1 ]; then
2200 cp -pRL $1 $2
2201 fi
2202}
2203
2204# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2205# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2206# ``localrc`` or on the command line if necessary::
2207#
2208# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2209#
2210# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2211
Ian Wienandaee18c72014-02-21 15:35:08 +11002212function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05002213 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002214 export http_proxy=$http_proxy
2215 fi
Sean Dague53753292014-12-04 19:38:15 -05002216 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002217 export https_proxy=$https_proxy
2218 fi
Sean Dague53753292014-12-04 19:38:15 -05002219 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002220 export no_proxy=$no_proxy
2221 fi
2222}
2223
2224# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002225function is_nfs_directory {
Ian Wienandada886d2015-10-07 14:06:26 +11002226 local mount_type
2227 mount_type=`stat -f -L -c %T $1`
Dean Troyerdff49a22014-01-30 15:37:40 -06002228 test "$mount_type" == "nfs"
2229}
2230
2231# Return the network portion of the given IP address using netmask
2232# netmask is in the traditional dotted-quad format
2233# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002234function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002235 local ip=$1
2236 local mask=$2
2237 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
Ian Wienandada886d2015-10-07 14:06:26 +11002238 local subnet
2239 subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
Dean Troyerdff49a22014-01-30 15:37:40 -06002240 echo $subnet
2241}
2242
Michael Turek7938d832016-04-12 14:55:21 -04002243function is_provider_network {
2244 if [ "$Q_USE_PROVIDER_NETWORKING" == "True" ]; then
2245 return 0
2246 fi
2247 return 1
2248}
2249
2250
Ian Wienand6d213df2017-08-22 16:05:16 +10002251# Return just the <major>.<minor> for the given python interpreter
2252function _get_python_version {
2253 local interp=$1
2254 local version
Sean Dague4324f4e2017-09-14 12:59:25 -06002255 # disable erroring out here, otherwise if python 3 doesn't exist we fail hard.
Javier Pena6bd49242017-09-15 15:55:00 +02002256 if [[ -x $(which $interp 2> /dev/null) ]]; then
Sean Dague4324f4e2017-09-14 12:59:25 -06002257 version=$($interp -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
2258 fi
Ian Wienand6d213df2017-08-22 16:05:16 +10002259 echo ${version}
2260}
2261
Chris Dent3a2c86a2015-05-12 13:41:25 +00002262# Return the current python as "python<major>.<minor>"
2263function python_version {
Ian Wienandada886d2015-10-07 14:06:26 +11002264 local python_version
Ian Wienand6d213df2017-08-22 16:05:16 +10002265 python_version=$(_get_python_version python2)
Chris Dent3a2c86a2015-05-12 13:41:25 +00002266 echo "python${python_version}"
2267}
2268
Ian Wienand6d213df2017-08-22 16:05:16 +10002269function python3_version {
2270 local python3_version
2271 python3_version=$(_get_python_version python3)
Doug Hellmann04178582018-06-12 15:37:00 -04002272 echo "python${python3_version}"
Ian Wienand6d213df2017-08-22 16:05:16 +10002273}
2274
2275
Dean Troyerdff49a22014-01-30 15:37:40 -06002276# Service wrapper to restart services
2277# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002278function restart_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002279 if [ -x /bin/systemctl ]; then
2280 sudo /bin/systemctl restart $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002281 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002282 sudo service $1 restart
Dean Troyerdff49a22014-01-30 15:37:40 -06002283 fi
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002284
Dean Troyerdff49a22014-01-30 15:37:40 -06002285}
2286
2287# Only change permissions of a file or directory if it is not on an
2288# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002289function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002290 _safe_permission_operation chmod $@
2291}
2292
2293# Only change ownership of a file or directory if it is not on an NFS
2294# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002295function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002296 _safe_permission_operation chown $@
2297}
2298
2299# Service wrapper to start services
2300# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002301function start_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002302 if [ -x /bin/systemctl ]; then
2303 sudo /bin/systemctl start $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002304 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002305 sudo service $1 start
Dean Troyerdff49a22014-01-30 15:37:40 -06002306 fi
2307}
2308
2309# Service wrapper to stop services
2310# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002311function stop_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002312 if [ -x /bin/systemctl ]; then
2313 sudo /bin/systemctl stop $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002314 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002315 sudo service $1 stop
Dean Troyerdff49a22014-01-30 15:37:40 -06002316 fi
2317}
2318
Attila Fazekasbcaadd62016-11-23 12:43:02 +01002319# Service wrapper to reload services
2320# If the service was not in running state it will start it
Gregory Haynes4b49e402016-08-31 18:19:51 -07002321# reload_service service-name
2322function reload_service {
2323 if [ -x /bin/systemctl ]; then
Attila Fazekasbcaadd62016-11-23 12:43:02 +01002324 sudo /bin/systemctl reload-or-restart $1
Gregory Haynes4b49e402016-08-31 18:19:51 -07002325 else
2326 sudo service $1 reload
2327 fi
2328}
2329
Sean Dague442e4e92015-06-24 13:24:02 -04002330# Test with a finite retry loop.
2331#
2332function test_with_retry {
2333 local testcmd=$1
2334 local failmsg=$2
2335 local until=${3:-10}
2336 local sleep=${4:-0.5}
2337
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002338 time_start "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002339 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2340 die $LINENO "$failmsg"
2341 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002342 time_stop "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002343}
2344
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00002345# Like sudo but forwarding http_proxy https_proxy no_proxy environment vars.
2346# If it is run as superuser then sudo is replaced by env.
2347#
2348function sudo_with_proxies {
2349 local sudo
2350
2351 [[ "$(id -u)" = "0" ]] && sudo="env" || sudo="sudo"
2352
2353 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}"\
2354 no_proxy="${no_proxy:-}" "$@"
2355}
2356
Sean Dague95c33d52015-10-07 11:05:59 -04002357# Timing infrastructure - figure out where large blocks of time are
2358# used in DevStack
2359#
2360# The timing infrastructure for DevStack is about collecting buckets
2361# of time that are spend in some subtask. For instance, that might be
2362# 'apt', 'pip', 'osc', even database migrations. We do this by a pair
2363# of functions: time_start / time_stop.
2364#
2365# These take a single parameter: $name - which specifies the name of
2366# the bucket to be accounted against. time_totals function spits out
2367# the results.
2368#
2369# Resolution is only in whole seconds, so should be used for long
2370# running activities.
2371
Sean Dagueafef8bf2017-03-06 14:07:23 -05002372declare -A -g _TIME_TOTAL
2373declare -A -g _TIME_START
2374declare -r -g _TIME_BEGIN=$(date +%s)
Sean Dague95c33d52015-10-07 11:05:59 -04002375
2376# time_start $name
2377#
2378# starts the clock for a timer by name. Errors if that clock is
2379# already started.
2380function time_start {
2381 local name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002382 local start_time=${_TIME_START[$name]}
Sean Dague95c33d52015-10-07 11:05:59 -04002383 if [[ -n "$start_time" ]]; then
2384 die $LINENO "Trying to start the clock on $name, but it's already been started"
2385 fi
Sean Dague85cf2932017-03-27 15:35:13 -04002386 _TIME_START[$name]=$(date +%s%3N)
Sean Dague95c33d52015-10-07 11:05:59 -04002387}
2388
2389# time_stop $name
2390#
2391# stops the clock for a timer by name, and accumulate that time in the
2392# global counter for that name. Errors if that clock had not
2393# previously been started.
2394function time_stop {
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002395 local name
2396 local end_time
Rob Crittenden38245da2016-05-26 17:55:14 -04002397 local elapsed_time
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002398 local total
2399 local start_time
2400
2401 name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002402 start_time=${_TIME_START[$name]}
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002403
Sean Dague95c33d52015-10-07 11:05:59 -04002404 if [[ -z "$start_time" ]]; then
2405 die $LINENO "Trying to stop the clock on $name, but it was never started"
2406 fi
Sean Dague85cf2932017-03-27 15:35:13 -04002407 end_time=$(date +%s%3N)
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002408 elapsed_time=$(($end_time - $start_time))
Ian Wienand908a3a92016-03-29 14:47:09 +11002409 total=${_TIME_TOTAL[$name]:-0}
Sean Dague95c33d52015-10-07 11:05:59 -04002410 # reset the clock so we can start it in the future
Ian Wienand908a3a92016-03-29 14:47:09 +11002411 _TIME_START[$name]=""
2412 _TIME_TOTAL[$name]=$(($total + $elapsed_time))
Sean Dague95c33d52015-10-07 11:05:59 -04002413}
2414
Sean Dague85cf2932017-03-27 15:35:13 -04002415function oscwrap {
Ian Wienand474f5352019-08-08 09:15:11 +10002416 local xtrace
2417 xtrace=$(set +o | grep xtrace)
2418 set +o xtrace
2419
Sean Dague85cf2932017-03-27 15:35:13 -04002420 local out
2421 local rc
2422 local start
2423 local end
2424 # Cannot use timer_start and timer_stop as we run in subshells
2425 # and those rely on modifying vars in the same process (which cannot
2426 # happen from a subshell.
2427 start=$(date +%s%3N)
2428 out=$(command openstack "$@")
2429 rc=$?
2430 end=$(date +%s%3N)
2431 echo $((end - start)) >> $OSCWRAP_TIMER_FILE
2432
2433 echo "$out"
Ian Wienand474f5352019-08-08 09:15:11 +10002434 $xtrace
Sean Dague85cf2932017-03-27 15:35:13 -04002435 return $rc
2436}
2437
2438function install_oscwrap {
2439 # File to accumulate our timing data
2440 OSCWRAP_TIMER_FILE=$(mktemp)
2441 # Bash by default doesn't expand aliases, allow it for the aliases
2442 # we want to whitelist.
2443 shopt -s expand_aliases
2444 # Remove all aliases that might be expanded to preserve old unexpanded
2445 # behavior
2446 unalias -a
2447 # Add only the alias we want for openstack
2448 alias openstack=oscwrap
2449}
2450
2451function cleanup_oscwrap {
2452 local total=0
Stephen Finucaneffd00472018-01-18 15:12:29 +00002453 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 -04002454 _TIME_TOTAL["osc"]=$total
2455 rm $OSCWRAP_TIMER_FILE
2456}
2457
Sean Dague95c33d52015-10-07 11:05:59 -04002458# time_totals
Ian Wienand908a3a92016-03-29 14:47:09 +11002459# Print out total time summary
Sean Dague95c33d52015-10-07 11:05:59 -04002460function time_totals {
Ian Wienand908a3a92016-03-29 14:47:09 +11002461 local elapsed_time
2462 local end_time
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002463 local len=20
Ian Wienand908a3a92016-03-29 14:47:09 +11002464 local xtrace
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002465 local unaccounted_time
Ian Wienand908a3a92016-03-29 14:47:09 +11002466
2467 end_time=$(date +%s)
2468 elapsed_time=$(($end_time - $_TIME_BEGIN))
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002469 unaccounted_time=$elapsed_time
Ian Wienand908a3a92016-03-29 14:47:09 +11002470
2471 # pad 1st column this far
2472 for t in ${!_TIME_TOTAL[*]}; do
2473 if [[ ${#t} -gt $len ]]; then
2474 len=${#t}
2475 fi
Sean Dague95c33d52015-10-07 11:05:59 -04002476 done
Ian Wienand908a3a92016-03-29 14:47:09 +11002477
Sean Dague85cf2932017-03-27 15:35:13 -04002478 cleanup_oscwrap
2479
Ian Wienand908a3a92016-03-29 14:47:09 +11002480 xtrace=$(set +o | grep xtrace)
2481 set +o xtrace
2482
2483 echo
2484 echo "========================="
2485 echo "DevStack Component Timing"
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002486 echo " (times are in seconds) "
Ian Wienand908a3a92016-03-29 14:47:09 +11002487 echo "========================="
Ian Wienand908a3a92016-03-29 14:47:09 +11002488 for t in ${!_TIME_TOTAL[*]}; do
2489 local v=${_TIME_TOTAL[$t]}
Sean Dague85cf2932017-03-27 15:35:13 -04002490 # because we're recording in milliseconds
2491 v=$(($v / 1000))
Ian Wienand908a3a92016-03-29 14:47:09 +11002492 printf "%-${len}s %3d\n" "$t" "$v"
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002493 unaccounted_time=$(($unaccounted_time - $v))
Ian Wienand908a3a92016-03-29 14:47:09 +11002494 done
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002495 echo "-------------------------"
2496 printf "%-${len}s %3d\n" "Unaccounted time" "$unaccounted_time"
Ian Wienand908a3a92016-03-29 14:47:09 +11002497 echo "========================="
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002498 printf "%-${len}s %3d\n" "Total runtime" "$elapsed_time"
Ian Wienand908a3a92016-03-29 14:47:09 +11002499
2500 $xtrace
Sean Dague95c33d52015-10-07 11:05:59 -04002501}
Dean Troyerdff49a22014-01-30 15:37:40 -06002502
Sean Mooneyae21b352020-09-01 14:11:45 +00002503function clean_pyc_files {
2504 # Clean up all *.pyc files
2505 if [[ -n "$DEST" ]] && [[ -d "$DEST" ]]; then
2506 sudo find $DEST -name "*.pyc" -delete
2507 fi
2508}
2509
Dean Troyerdff49a22014-01-30 15:37:40 -06002510# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +11002511$_XTRACE_FUNCTIONS_COMMON
Dean Troyerdff49a22014-01-30 15:37:40 -06002512
2513# Local variables:
2514# mode: shell-script
2515# End: