blob: 111d3393729c31f4e3858dc58cffca3208f208c8 [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
Sean Daguec67d22e2016-02-02 05:51:14 -050088 # demo -> devstack
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
99 # alt_demo -> devstack-alt
Davanum Srinivas51ecf0a2017-01-05 16:11:17 -0500100 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
Sean Daguec67d22e2016-02-02 05:51:14 -0500101 --file $CLOUDS_YAML \
Clark Boylane20cb432016-02-05 12:00:18 -0800102 --os-cloud devstack-alt \
Sean Daguec67d22e2016-02-02 05:51:14 -0500103 --os-region-name $REGION_NAME \
Sean Daguec67d22e2016-02-02 05:51:14 -0500104 $CA_CERT_ARG \
Monty Taylore43f60b2017-04-27 18:27:36 -0500105 --os-auth-url $KEYSTONE_SERVICE_URI \
Sean Daguec67d22e2016-02-02 05:51:14 -0500106 --os-username alt_demo \
107 --os-password $ADMIN_PASSWORD \
108 --os-project-name alt_demo
109
110 # admin -> devstack-admin
Davanum Srinivas51ecf0a2017-01-05 16:11:17 -0500111 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
Monty Tayloree9bb762015-10-19 15:16:18 -0400112 --file $CLOUDS_YAML \
113 --os-cloud devstack-admin \
114 --os-region-name $REGION_NAME \
Monty Tayloree9bb762015-10-19 15:16:18 -0400115 $CA_CERT_ARG \
Monty Taylore43f60b2017-04-27 18:27:36 -0500116 --os-auth-url $KEYSTONE_SERVICE_URI \
Monty Tayloree9bb762015-10-19 15:16:18 -0400117 --os-username admin \
118 --os-password $ADMIN_PASSWORD \
119 --os-project-name admin
Monty Taylor74379df2016-01-21 10:20:08 -0500120
Monty Taylor56905822019-01-08 15:29:16 +0000121 # admin with a system-scoped token -> devstack-system
122 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
123 --file $CLOUDS_YAML \
124 --os-cloud devstack-system-admin \
125 --os-region-name $REGION_NAME \
126 $CA_CERT_ARG \
127 --os-auth-url $KEYSTONE_SERVICE_URI \
128 --os-username admin \
129 --os-password $ADMIN_PASSWORD \
130 --os-system-scope all
131
Lance Bragstad021ae0b2021-03-11 15:47:50 +0000132 # system member
133 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
134 --file $CLOUDS_YAML \
135 --os-cloud devstack-system-member \
136 --os-region-name $REGION_NAME \
137 $CA_CERT_ARG \
138 --os-auth-url $KEYSTONE_SERVICE_URI \
139 --os-username system_member \
140 --os-password $ADMIN_PASSWORD \
141 --os-system-scope all
142
143 # system reader
144 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
145 --file $CLOUDS_YAML \
146 --os-cloud devstack-system-reader \
147 --os-region-name $REGION_NAME \
148 $CA_CERT_ARG \
149 --os-auth-url $KEYSTONE_SERVICE_URI \
150 --os-username system_reader \
151 --os-password $ADMIN_PASSWORD \
152 --os-system-scope all
153
Maciej Józefczyk7a0fa4f2020-03-05 16:55:50 +0100154 cat >> $CLOUDS_YAML <<EOF
155functional:
156 image_name: $DEFAULT_IMAGE_NAME
157EOF
158
Monty Taylor74379df2016-01-21 10:20:08 -0500159 # CLean up any old clouds.yaml files we had laying around
ZhiQiang Fan0b4a0092016-04-12 20:26:33 +0800160 rm -f $(eval echo ~"$STACK_USER")/.config/openstack/clouds.yaml
Monty Taylor7224eec2015-09-19 11:26:18 -0400161}
162
Ian Wienande82bac02015-08-25 14:29:08 +1000163# trueorfalse <True|False> <VAR>
164#
165# Normalize config-value provided in variable VAR to either "True" or
166# "False". If VAR is unset (i.e. $VAR evaluates as empty), the value
167# of the second argument will be used as the default value.
168#
169# Accepts as False: 0 no No NO false False FALSE
170# Accepts as True: 1 yes Yes YES true True TRUE
171#
172# usage:
173# VAL=$(trueorfalse False VAL)
Ian Wienandaee18c72014-02-21 15:35:08 +1100174function trueorfalse {
Ian Wienand433a9b12015-10-07 13:29:31 +1100175 local xtrace
176 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -0500177 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600178
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900179 local default=$1
Ian Wienande82bac02015-08-25 14:29:08 +1000180
181 if [ -z $2 ]; then
182 die $LINENO "variable to normalize required"
183 fi
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900184 local testval=${!2:-}
185
186 case "$testval" in
187 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
188 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
189 * ) echo "$default" ;;
190 esac
191
Sean Dague45917cc2014-02-24 16:09:14 -0500192 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600193}
194
Attila Fazekas1bd79592015-02-24 14:06:56 +0100195function isset {
196 [[ -v "$1" ]]
197}
Dean Troyerdff49a22014-01-30 15:37:40 -0600198
Dean Troyer68162342015-05-13 15:41:03 -0500199
Dean Troyerdff49a22014-01-30 15:37:40 -0600200# Control Functions
201# =================
202
203# Prints backtrace info
204# filename:lineno:function
205# backtrace level
206function backtrace {
207 local level=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100208 local deep
209 deep=$((${#BASH_SOURCE[@]} - 1))
Dean Troyerdff49a22014-01-30 15:37:40 -0600210 echo "[Call Trace]"
211 while [ $level -le $deep ]; do
212 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
213 deep=$((deep - 1))
214 done
215}
216
217# Prints line number and "message" then exits
218# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100219function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600220 local exitcode=$?
221 set +o xtrace
222 local line=$1; shift
223 if [ $exitcode == 0 ]; then
224 exitcode=1
225 fi
226 backtrace 2
227 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600228 # Give buffers a second to flush
229 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600230 exit $exitcode
231}
232
233# Checks an environment variable is not set or has length 0 OR if the
234# exit code is non-zero and prints "message" and exits
235# NOTE: env-var is the variable name without a '$'
236# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100237function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600238 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100239 local xtrace
240 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600241 set +o xtrace
242 local line=$1; shift
243 local evar=$1; shift
244 if ! is_set $evar || [ $exitcode != 0 ]; then
245 die $line "$*"
246 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500247 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600248}
249
Sean Dague1de9e332015-10-07 08:46:13 -0400250function deprecated {
251 local text=$1
252 DEPRECATED_TEXT+="\n$text"
YAMAMOTO Takashi8b1bbd62016-12-01 22:29:12 +0900253 echo "WARNING: $text" >&2
Sean Dague1de9e332015-10-07 08:46:13 -0400254}
255
Dean Troyerdff49a22014-01-30 15:37:40 -0600256# Prints line number and "message" in error format
257# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100258function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600259 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100260 local xtrace
261 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600262 set +o xtrace
263 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
Ian Wienande8a6a022018-10-08 15:20:34 +1100264 echo "$msg" 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600265 if [[ -n ${LOGDIR} ]]; then
Ian Wienande8a6a022018-10-08 15:20:34 +1100266 echo "$msg" >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600267 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500268 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600269 return $exitcode
270}
271
272# Checks an environment variable is not set or has length 0 OR if the
273# exit code is non-zero and prints "message"
274# NOTE: env-var is the variable name without a '$'
275# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100276function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600277 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100278 local xtrace
279 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600280 set +o xtrace
281 local line=$1; shift
282 local evar=$1; shift
283 if ! is_set $evar || [ $exitcode != 0 ]; then
284 err $line "$*"
285 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500286 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600287 return $exitcode
288}
289
290# Exit after outputting a message about the distribution not being supported.
291# exit_distro_not_supported [optional-string-telling-what-is-missing]
292function exit_distro_not_supported {
293 if [[ -z "$DISTRO" ]]; then
294 GetDistro
295 fi
296
297 if [ $# -gt 0 ]; then
298 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
299 else
300 die $LINENO "Support for $DISTRO is incomplete."
301 fi
302}
303
304# Test if the named environment variable is set and not zero length
305# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100306function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600307 local var=\$"$1"
308 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
309}
310
311# Prints line number and "message" in warning format
312# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100313function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600314 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100315 local xtrace
316 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600317 set +o xtrace
318 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Ian Wienande8a6a022018-10-08 15:20:34 +1100319 echo "$msg"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500320 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600321 return $exitcode
322}
323
324
325# Distro Functions
326# ================
327
328# Determine OS Vendor, Release and Update
Ian Wienand7710e7f2014-08-27 16:15:32 +1000329
330#
331# NOTE : For portability, you almost certainly do not want to use
332# these variables directly! The "is_*" functions defined below this
333# bundle up compatible platforms under larger umbrellas that we have
334# determinted are compatible enough (e.g. is_ubuntu covers Ubuntu &
335# Debian, is_fedora covers RPM-based distros). Higher-level functions
336# such as "install_package" further abstract things in better ways.
337#
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500338# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
Matt Riedemannff10ac32017-02-13 12:44:24 -0500339# ``os_RELEASE`` - major release: ``16.04`` (Ubuntu), ``23`` (Fedora)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500340# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
Matt Riedemannff10ac32017-02-13 12:44:24 -0500341# ``os_CODENAME`` - vendor's codename for release: ``xenial``
Ian Wienand7710e7f2014-08-27 16:15:32 +1000342
Sean Dagueafef8bf2017-03-06 14:07:23 -0500343declare -g os_VENDOR os_RELEASE os_PACKAGE os_CODENAME
Ian Wienand7710e7f2014-08-27 16:15:32 +1000344
345# Make a *best effort* attempt to install lsb_release packages for the
346# user if not available. Note can't use generic install_package*
347# because they depend on this!
348function _ensure_lsb_release {
Thomas Bechtolddab39012016-03-03 12:11:15 +0100349 if [[ -x $(command -v lsb_release 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000350 return
351 fi
352
Thomas Bechtolddab39012016-03-03 12:11:15 +0100353 if [[ -x $(command -v apt-get 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000354 sudo apt-get install -y lsb-release
Thomas Bechtolddab39012016-03-03 12:11:15 +0100355 elif [[ -x $(command -v zypper 2>/dev/null) ]]; then
Dirk Mueller5ef8a122017-09-25 13:08:51 +0200356 sudo zypper -n install lsb-release
Thomas Bechtolddab39012016-03-03 12:11:15 +0100357 elif [[ -x $(command -v dnf 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000358 sudo dnf install -y redhat-lsb-core
Ian Wienand7710e7f2014-08-27 16:15:32 +1000359 else
360 die $LINENO "Unable to find or auto-install lsb_release"
361 fi
362}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500363
Dean Troyerdff49a22014-01-30 15:37:40 -0600364# GetOSVersion
Ian Wienand7710e7f2014-08-27 16:15:32 +1000365# Set the following variables:
366# - os_RELEASE
367# - os_CODENAME
368# - os_VENDOR
369# - os_PACKAGE
Ian Wienandaee18c72014-02-21 15:35:08 +1100370function GetOSVersion {
Ian Wienand7710e7f2014-08-27 16:15:32 +1000371 # We only support distros that provide a sane lsb_release
372 _ensure_lsb_release
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500373
Ian Wienand7710e7f2014-08-27 16:15:32 +1000374 os_RELEASE=$(lsb_release -r -s)
375 os_CODENAME=$(lsb_release -c -s)
376 os_VENDOR=$(lsb_release -i -s)
Dean Troyerdff49a22014-01-30 15:37:40 -0600377
Ian Wienand7710e7f2014-08-27 16:15:32 +1000378 if [[ $os_VENDOR =~ (Debian|Ubuntu|LinuxMint) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600379 os_PACKAGE="deb"
Ian Wienand7710e7f2014-08-27 16:15:32 +1000380 else
381 os_PACKAGE="rpm"
Dean Troyerdff49a22014-01-30 15:37:40 -0600382 fi
Ian Wienand7710e7f2014-08-27 16:15:32 +1000383
384 typeset -xr os_VENDOR
385 typeset -xr os_RELEASE
386 typeset -xr os_PACKAGE
387 typeset -xr os_CODENAME
Dean Troyerdff49a22014-01-30 15:37:40 -0600388}
389
390# Translate the OS version values into common nomenclature
391# Sets global ``DISTRO`` from the ``os_*`` values
Sean Dagueafef8bf2017-03-06 14:07:23 -0500392declare -g DISTRO
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500393
Ian Wienandaee18c72014-02-21 15:35:08 +1100394function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600395 GetOSVersion
ptoohill196006652016-02-15 16:07:50 -0600396 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) || \
397 "$os_VENDOR" =~ (LinuxMint) ]]; then
398 # 'Everyone' refers to Ubuntu / Debian / Mint releases by
Ian Wienand7710e7f2014-08-27 16:15:32 +1000399 # the code name adjective
Dean Troyerdff49a22014-01-30 15:37:40 -0600400 DISTRO=$os_CODENAME
401 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
402 # For Fedora, just use 'f' and the release
403 DISTRO="f$os_RELEASE"
Adam Spiersb6f04ca2019-01-23 18:55:16 +0000404 elif is_opensuse; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600405 DISTRO="opensuse-$os_RELEASE"
Dirk Mueller4404f682018-03-02 00:37:58 +0100406 # Tumbleweed uses "n/a" as a codename, and the release is a datestring
Dirk Mueller297a50a2018-06-20 11:08:54 +0200407 # like 20180218, so not very useful. Leap however uses a release
408 # with a "dot", so for example 15.0
409 [ "$os_CODENAME" = "n/a" -a "$os_RELEASE" = "${os_RELEASE/\./}" ] && \
410 DISTRO="opensuse-tumbleweed"
Adam Spiersb6f04ca2019-01-23 18:55:16 +0000411 elif is_suse_linux_enterprise; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000412 # just use major release
413 DISTRO="sle${os_RELEASE%.*}"
414 elif [[ "$os_VENDOR" =~ (Red.*Hat) || \
anju Tiwari6c639c92014-07-15 18:11:54 +0530415 "$os_VENDOR" =~ (CentOS) || \
Mike Trimm1da4e792016-04-27 11:40:19 -0500416 "$os_VENDOR" =~ (Scientific) || \
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300417 "$os_VENDOR" =~ (OracleServer) || \
Evgeny Antyshev718512c2016-03-03 14:47:58 +0000418 "$os_VENDOR" =~ (Virtuozzo) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600419 # Drop the . release as we assume it's compatible
Ian Wienand7710e7f2014-08-27 16:15:32 +1000420 # XXX re-evaluate when we get RHEL10
Dean Troyerdff49a22014-01-30 15:37:40 -0600421 DISTRO="rhel${os_RELEASE::1}"
Dean Troyerdff49a22014-01-30 15:37:40 -0600422 else
Ian Wienanded92e432016-02-16 10:56:56 +1100423 # We can't make a good choice here. Setting a sensible DISTRO
424 # is part of the problem, but not the major issue -- we really
425 # only use DISTRO in the code as a fine-filter.
426 #
427 # The bigger problem is categorising the system into one of
428 # our two big categories as Ubuntu/Debian-ish or
429 # Fedora/CentOS-ish.
430 #
431 # The setting of os_PACKAGE above is only set to "deb" based
432 # on a hard-coded list of vendor names ... thus we will
433 # default to thinking unknown distros are RPM based
434 # (ie. is_ubuntu does not match). But the platform will then
435 # also not match in is_fedora, because that also has a list of
436 # names.
437 #
438 # So, if you are reading this, getting your distro supported
439 # is really about making sure it matches correctly in these
440 # functions. Then you can choose a sensible way to construct
441 # DISTRO based on your distros release approach.
442 die $LINENO "Unable to determine DISTRO, can not continue."
Dean Troyerdff49a22014-01-30 15:37:40 -0600443 fi
Ian Wienand7710e7f2014-08-27 16:15:32 +1000444 typeset -xr DISTRO
Dean Troyerdff49a22014-01-30 15:37:40 -0600445}
446
447# Utility function for checking machine architecture
448# is_arch arch-type
449function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500450 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600451}
452
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700453# Determine if current distribution is an Oracle distribution
454# is_oraclelinux
455function is_oraclelinux {
456 if [[ -z "$os_VENDOR" ]]; then
457 GetOSVersion
458 fi
459
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300460 [ "$os_VENDOR" = "OracleServer" ]
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700461}
462
463
Dean Troyerdff49a22014-01-30 15:37:40 -0600464# Determine if current distribution is a Fedora-based distribution
465# (Fedora, RHEL, CentOS, etc).
466# is_fedora
467function is_fedora {
468 if [[ -z "$os_VENDOR" ]]; then
469 GetOSVersion
470 fi
471
anju Tiwari6c639c92014-07-15 18:11:54 +0530472 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
tengqm78d37392016-03-15 23:08:00 -0400473 [ "$os_VENDOR" = "RedHatEnterpriseServer" ] || \
Carlos Goncalves587e0a32020-08-01 21:47:55 +0200474 [ "$os_VENDOR" = "RedHatEnterprise" ] || \
Sean Mooneye7c017b2020-12-09 23:53:12 +0000475 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "CentOSStream" ] || \
476 [ "$os_VENDOR" = "OracleServer" ] || [ "$os_VENDOR" = "Virtuozzo" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600477}
478
479
480# Determine if current distribution is a SUSE-based distribution
481# (openSUSE, SLE).
482# is_suse
483function is_suse {
Adam Spiersb6f04ca2019-01-23 18:55:16 +0000484 is_opensuse || is_suse_linux_enterprise
485}
486
487
488# Determine if current distribution is an openSUSE distribution
489# is_opensuse
490function is_opensuse {
Dean Troyerdff49a22014-01-30 15:37:40 -0600491 if [[ -z "$os_VENDOR" ]]; then
492 GetOSVersion
493 fi
494
Adam Spiersb6f04ca2019-01-23 18:55:16 +0000495 [[ "$os_VENDOR" =~ (openSUSE) ]]
496}
497
498
499# Determine if current distribution is a SUSE Linux Enterprise (SLE)
500# distribution
501# is_suse_linux_enterprise
502function is_suse_linux_enterprise {
503 if [[ -z "$os_VENDOR" ]]; then
504 GetOSVersion
505 fi
506
507 [[ "$os_VENDOR" =~ (^SUSE) ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600508}
509
510
511# Determine if current distribution is an Ubuntu-based distribution
512# It will also detect non-Ubuntu but Debian-based distros
513# is_ubuntu
514function is_ubuntu {
515 if [[ -z "$os_PACKAGE" ]]; then
516 GetOSVersion
517 fi
518 [ "$os_PACKAGE" = "deb" ]
519}
520
521
522# Git Functions
523# =============
524
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600525# Returns openstack release name for a given branch name
526# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100527function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600528 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700529 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600530 echo ${branch#*/}
531 else
532 echo "master"
533 fi
534}
535
Dean Troyerdff49a22014-01-30 15:37:40 -0600536# git clone only if directory doesn't exist already. Since ``DEST`` might not
537# be owned by the installation user, we create the directory and change the
538# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500539# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
540# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600541# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500542# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600543# git_clone remote dest-dir branch
544function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500545 local git_remote=$1
546 local git_dest=$2
547 local git_ref=$3
Ian Wienandada886d2015-10-07 14:06:26 +1100548 local orig_dir
549 orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200550 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500551
Sean Dague53753292014-12-04 19:38:15 -0500552 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800553 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200554 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
555 fi
556
Dean Troyerdff49a22014-01-30 15:37:40 -0600557 if [[ "$OFFLINE" = "True" ]]; then
558 echo "Running in offline mode, clones already exist"
559 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500560 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600561 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400562 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600563 return
564 fi
565
Dean Troyer50cda692014-07-25 11:57:20 -0500566 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600567 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500568 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700569 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
570 echo "The $git_dest project was not found; if this is a gate job, add"
James E. Blair35a0c572017-09-10 15:37:56 -0700571 echo "the project to 'required-projects' in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600572 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700573 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200574 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600575 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500576 cd $git_dest
577 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600578 else
579 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500580 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700581 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
582 echo "The $git_dest project was not found; if this is a gate job, add"
583 echo "the project to the \$PROJECTS variable in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600584 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700585 fi
Jakub Wachowski90742fc2016-11-18 14:28:47 +0100586 # '--branch' can also take tags
587 git_timed clone $git_clone_flags $git_remote $git_dest --branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600588 elif [[ "$RECLONE" = "True" ]]; then
589 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500590 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600591 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500592 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100593 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600594 # remove the existing ignored files (like pyc) as they cause breakage
595 # (due to the py files having older timestamps than our pyc, so python
596 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500597 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600598
Dean Troyer50cda692014-07-25 11:57:20 -0500599 # handle git_ref accordingly to type (tag, branch)
600 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
601 git_update_tag $git_ref
602 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
603 git_update_branch $git_ref
604 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
605 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600606 else
Dean Troyer50cda692014-07-25 11:57:20 -0500607 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600608 fi
609
610 fi
611 fi
612
613 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500614 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600615 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400616 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600617}
618
Sean Daguecc524062014-10-01 09:06:43 -0400619# A variation on git clone that lets us specify a project by it's
620# actual name, like oslo.config. This is exceptionally useful in the
621# library installation case
622function git_clone_by_name {
623 local name=$1
624 local repo=${GITREPO[$name]}
625 local dir=${GITDIR[$name]}
626 local branch=${GITBRANCH[$name]}
627 git_clone $repo $dir $branch
628}
629
630
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100631# git can sometimes get itself infinitely stuck with transient network
632# errors or other issues with the remote end. This wraps git in a
633# timeout/retry loop and is intended to watch over non-local git
634# processes that might hang. GIT_TIMEOUT, if set, is passed directly
635# to timeout(1); otherwise the default value of 0 maintains the status
636# quo of waiting forever.
637# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100638function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100639 local count=0
640 local timeout=0
641
642 if [[ -n "${GIT_TIMEOUT}" ]]; then
643 timeout=${GIT_TIMEOUT}
644 fi
645
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900646 time_start "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100647 until timeout -s SIGINT ${timeout} git "$@"; do
648 # 124 is timeout(1)'s special return code when it reached the
649 # timeout; otherwise assume fatal failure
650 if [[ $? -ne 124 ]]; then
651 die $LINENO "git call failed: [git $@]"
652 fi
653
654 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400655 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100656 if [ $count -eq 3 ]; then
657 die $LINENO "Maximum of 3 git retries reached"
658 fi
659 sleep 5
660 done
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900661 time_stop "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100662}
663
Dean Troyerdff49a22014-01-30 15:37:40 -0600664# git update using reference as a branch.
665# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100666function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500667 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600668
Dean Troyer50cda692014-07-25 11:57:20 -0500669 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600670 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500671 git branch -D $git_branch || true
672 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600673}
674
675# git update using reference as a branch.
676# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100677function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500678 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600679
Dean Troyer50cda692014-07-25 11:57:20 -0500680 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600681}
682
683# git update using reference as a tag. Be careful editing source at that repo
684# as working copy will be in a detached mode
685# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100686function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500687 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600688
Dean Troyer50cda692014-07-25 11:57:20 -0500689 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600690 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500691 git_timed fetch origin tag $git_tag
692 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600693}
694
695
696# OpenStack Functions
697# ===================
698
699# Get the default value for HOST_IP
700# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100701function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600702 local fixed_range=$1
703 local floating_range=$2
704 local host_ip_iface=$3
705 local host_ip=$4
Brian Haley180f5eb2015-06-16 13:14:31 -0400706 local af=$5
Dean Troyerdff49a22014-01-30 15:37:40 -0600707
Dean Troyerdff49a22014-01-30 15:37:40 -0600708 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
709 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
710 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100711 # Find the interface used for the default route
Brian Haley180f5eb2015-06-16 13:14:31 -0400712 host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
Ian Wienandada886d2015-10-07 14:06:26 +1100713 local host_ips
714 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 -0500715 local ip
716 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600717 # Attempt to filter out IP addresses that are part of the fixed and
718 # floating range. Note that this method only works if the ``netaddr``
719 # python library is installed. If it is not installed, an error
720 # will be printed and the first IP from the interface will be used.
721 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
722 # address.
Brian Haley180f5eb2015-06-16 13:14:31 -0400723 if [[ "$af" == "inet6" ]]; then
724 host_ip=$ip
725 break;
726 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500727 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
728 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600729 break;
730 fi
731 done
732 fi
733 echo $host_ip
734}
735
Attila Fazekasf71b5002014-05-28 09:52:22 +0200736# Generates hex string from ``size`` byte of pseudo random data
737# generate_hex_string size
738function generate_hex_string {
739 local size=$1
740 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
741}
742
Dean Troyerdff49a22014-01-30 15:37:40 -0600743# Grab a numbered field from python prettytable output
744# Fields are numbered starting with 1
745# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
746# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100747function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500748 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600749 while read data; do
750 if [ "$1" -lt 0 ]; then
751 field="(\$(NF$1))"
752 else
753 field="\$$(($1 + 1))"
754 fi
755 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
756 done
757}
758
yuntongjinf26deea2015-02-28 10:50:34 +0800759# install default policy
760# copy over a default policy.json and policy.d for projects
761function install_default_policy {
762 local project=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100763 local project_uc
764 project_uc=$(echo $1|tr a-z A-Z)
yuntongjinf26deea2015-02-28 10:50:34 +0800765 local conf_dir="${project_uc}_CONF_DIR"
766 # eval conf dir to get the variable
767 conf_dir="${!conf_dir}"
768 local project_dir="${project_uc}_DIR"
769 # eval project dir to get the variable
770 project_dir="${!project_dir}"
771 local sample_conf_dir="${project_dir}/etc/${project}"
772 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
773
774 # first copy any policy.json
775 cp -p $sample_conf_dir/policy.json $conf_dir
776 # then optionally copy over policy.d
777 if [[ -d $sample_policy_dir ]]; then
778 cp -r $sample_policy_dir $conf_dir/policy.d
779 fi
780}
781
Dean Troyerdff49a22014-01-30 15:37:40 -0600782# Add a policy to a policy.json file
783# Do nothing if the policy already exists
784# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100785function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600786 local policy_file=$1
787 local policy_name=$2
788 local policy_perm=$3
789
790 if grep -q ${policy_name} ${policy_file}; then
791 echo "Policy ${policy_name} already exists in ${policy_file}"
792 return
793 fi
794
795 # Add a terminating comma to policy lines without one
796 # Remove the closing '}' and all lines following to the end-of-file
Ian Wienandada886d2015-10-07 14:06:26 +1100797 local tmpfile
798 tmpfile=$(mktemp)
Dean Troyerdff49a22014-01-30 15:37:40 -0600799 uniq ${policy_file} | sed -e '
800 s/]$/],/
801 /^[}]/,$d
802 ' > ${tmpfile}
803
804 # Append policy and closing brace
805 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
806 echo "}" >>${tmpfile}
807
808 mv ${tmpfile} ${policy_file}
809}
810
Alistair Coles24779f62014-10-15 18:57:59 +0100811# Gets or creates a domain
812# Usage: get_or_create_domain <name> <description>
813function get_or_create_domain {
Ian Wienand11d276c2015-07-02 09:34:34 +1000814 local domain_id
Alistair Coles24779f62014-10-15 18:57:59 +0100815 # Gets domain id
Ian Wienand11d276c2015-07-02 09:34:34 +1000816 domain_id=$(
Alistair Coles24779f62014-10-15 18:57:59 +0100817 # Gets domain id
Steve Martinelli050a0d52015-09-06 22:03:54 +0000818 openstack domain show $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100819 -f value -c id 2>/dev/null ||
820 # Creates new domain
Steve Martinelli050a0d52015-09-06 22:03:54 +0000821 openstack domain create $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100822 --description "$2" \
823 -f value -c id
824 )
825 echo $domain_id
826}
827
Steve Martinellib74e01c2014-12-18 01:35:35 -0500828# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000829# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500830function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500831 local desc="${3:-}"
Ian Wienand11d276c2015-07-02 09:34:34 +1000832 local group_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500833 # Gets group id
Ian Wienand11d276c2015-07-02 09:34:34 +1000834 group_id=$(
Steve Martinellib74e01c2014-12-18 01:35:35 -0500835 # Creates new group with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000836 openstack group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000837 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500838 -f value -c id
839 )
840 echo $group_id
841}
842
Bartosz Górski0abde392014-02-28 14:15:19 +0100843# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000844# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100845function get_or_create_user {
Ian Wienand11d276c2015-07-02 09:34:34 +1000846 local user_id
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000847 if [[ ! -z "$4" ]]; then
848 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200849 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500850 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200851 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100852 # Gets user id
Ian Wienand11d276c2015-07-02 09:34:34 +1000853 user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500854 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000855 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100856 $1 \
857 --password "$2" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000858 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500859 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500860 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100861 -f value -c id
862 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500863 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100864}
865
866# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000867# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100868function get_or_create_project {
Ian Wienand11d276c2015-07-02 09:34:34 +1000869 local project_id
870 project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500871 # Creates new project with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000872 openstack project create $1 \
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000873 --domain=$2 \
874 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100875 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500876 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100877}
878
879# Gets or creates role
880# Usage: get_or_create_role <name>
881function get_or_create_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000882 local role_id
883 role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500884 # Creates role with --or-show
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000885 openstack role create $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000886 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100887 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500888 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100889}
890
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600891# Returns the domain parts of a function call if present
892# Usage: _get_domain_args [<user_domain> <project_domain>]
893function _get_domain_args {
894 local domain
895 domain=""
896
897 if [[ -n "$1" ]]; then
898 domain="$domain --user-domain $1"
899 fi
900 if [[ -n "$2" ]]; then
901 domain="$domain --project-domain $2"
902 fi
903
904 echo $domain
905}
906
Jamie Lennox9b215db2015-02-10 18:19:57 +1100907# Gets or adds user role to project
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600908# Usage: get_or_add_user_project_role <role> <user> <project> [<user_domain> <project_domain>]
Jamie Lennox9b215db2015-02-10 18:19:57 +1100909function get_or_add_user_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000910 local user_role_id
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600911
912 domain_args=$(_get_domain_args $4 $5)
913
Bartosz Górski0abde392014-02-28 14:15:19 +0100914 # Gets user role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700915 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700916 --role $1 \
Steve Martinelli5541a612015-01-19 15:58:49 -0500917 --user $2 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000918 --project $3 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600919 $domain_args \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700920 | grep '^|\s[a-f0-9]\+' | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500921 if [[ -z "$user_role_id" ]]; then
Roxana Gherle50821be2015-09-22 10:52:46 -0700922 # Adds role to user and get it
923 openstack role add $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100924 --user $2 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600925 --project $3 \
926 $domain_args
Mike Perezc271b3e2016-10-03 16:00:33 -0700927 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700928 --role $1 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700929 --user $2 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700930 --project $3 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600931 $domain_args \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700932 | grep '^|\s[a-f0-9]\+' | get_field 1)
Bartosz Górski0abde392014-02-28 14:15:19 +0100933 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500934 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100935}
936
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500937# Gets or adds user role to domain
938# Usage: get_or_add_user_domain_role <role> <user> <domain>
939function get_or_add_user_domain_role {
940 local user_role_id
941 # Gets user role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700942 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700943 --role $1 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500944 --user $2 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500945 --domain $3 \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700946 | grep '^|\s[a-f0-9]\+' | get_field 1)
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500947 if [[ -z "$user_role_id" ]]; then
948 # Adds role to user and get it
949 openstack role add $1 \
950 --user $2 \
951 --domain $3
Mike Perezc271b3e2016-10-03 16:00:33 -0700952 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700953 --role $1 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500954 --user $2 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500955 --domain $3 \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700956 | grep '^|\s[a-f0-9]\+' | get_field 1)
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500957 fi
958 echo $user_role_id
959}
960
Lance Bragstad021ae0b2021-03-11 15:47:50 +0000961# Gets or adds user role to system
962# Usage: get_or_add_user_system_role <role> <user> <system> [<user_domain>]
963function get_or_add_user_system_role {
964 local user_role_id
965 local domain_args
966
967 domain_args=$(_get_domain_args $4)
968
969 # Gets user role id
970 user_role_id=$(openstack role assignment list \
971 --role $1 \
972 --user $2 \
973 --system $3 \
974 $domain_args \
975 -f value -c Role)
976 if [[ -z "$user_role_id" ]]; then
977 # Adds role to user and get it
978 openstack role add $1 \
979 --user $2 \
980 --system $3 \
981 $domain_args
982 user_role_id=$(openstack role assignment list \
983 --role $1 \
984 --user $2 \
985 --system $3 \
986 $domain_args \
987 -f value -c Role)
988 fi
989 echo $user_role_id
990}
991
Steve Martinelli4599fd12015-03-12 21:30:58 -0400992# Gets or adds group role to project
993# Usage: get_or_add_group_project_role <role> <group> <project>
994function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000995 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -0400996 # Gets group role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700997 group_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700998 --role $1 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400999 --group $2 \
1000 --project $3 \
Mike Perezc271b3e2016-10-03 16:00:33 -07001001 -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -04001002 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +10001003 # Adds role to group and get it
1004 openstack role add $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +10001005 --group $2 \
1006 --project $3
Mike Perezc271b3e2016-10-03 16:00:33 -07001007 group_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -07001008 --role $1 \
Steve Martinelli4599fd12015-03-12 21:30:58 -04001009 --group $2 \
1010 --project $3 \
Mike Perezc271b3e2016-10-03 16:00:33 -07001011 -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -04001012 fi
1013 echo $group_role_id
1014}
1015
Bartosz Górski0abde392014-02-28 14:15:19 +01001016# Gets or creates service
1017# Usage: get_or_create_service <name> <type> <description>
1018function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +10001019 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +01001020 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +10001021 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +01001022 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +10001023 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +01001024 # Creates new service if not exists
1025 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -05001026 $2 \
1027 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +01001028 --description="$3" \
1029 -f value -c id
1030 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001031 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +01001032}
1033
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001034# Create an endpoint with a specific interface
Matt Riedemannae4578b2016-04-23 01:45:40 +00001035# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
1036function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +10001037 local endpoint_id
Matt Riedemannae4578b2016-04-23 01:45:40 +00001038 endpoint_id=$(openstack endpoint list \
1039 --service $1 \
1040 --interface $2 \
1041 --region $4 \
1042 -c ID -f value)
1043 if [[ -z "$endpoint_id" ]]; then
1044 # Creates new endpoint
1045 endpoint_id=$(openstack endpoint create \
1046 $1 $2 $3 --region $4 -f value -c id)
1047 fi
1048
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001049 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +01001050}
Dean Troyerdff49a22014-01-30 15:37:40 -06001051
Sean Dague7d1ec432016-04-22 09:19:10 -04001052# Gets or creates endpoint
Sean Dague11eb2012017-02-13 16:16:59 -05001053# Usage: get_or_create_endpoint <service> <region> <publicurl> [adminurl] [internalurl]
Matt Riedemannae4578b2016-04-23 01:45:40 +00001054function get_or_create_endpoint {
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001055 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
1056 # creating one endpoint with multiple urls to multiple endpoints each with
1057 # a different interface. To maintain the existing function interface we
Matt Riedemannae4578b2016-04-23 01:45:40 +00001058 # create 3 endpoints and return the id of the public one. In reality
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001059 # returning the public id will not make a lot of difference as there are no
1060 # scenarios currently that use the returned id. Ideally this behaviour
1061 # should be pushed out to the service setups and let them create the
1062 # endpoints they need.
Ian Wienandada886d2015-10-07 14:06:26 +11001063 local public_id
Matt Riedemannae4578b2016-04-23 01:45:40 +00001064 public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
Sean Dague11eb2012017-02-13 16:16:59 -05001065 # only create admin/internal urls if provided content for them
1066 if [[ -n "$4" ]]; then
1067 _get_or_create_endpoint_with_interface $1 admin $4 $2
1068 fi
1069 if [[ -n "$5" ]]; then
1070 _get_or_create_endpoint_with_interface $1 internal $5 $2
1071 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001072 # return the public id to indicate success, and this is the endpoint most likely wanted
1073 echo $public_id
1074}
1075
1076# Get a URL from the identity service
1077# Usage: get_endpoint_url <service> <interface>
1078function get_endpoint_url {
1079 echo $(openstack endpoint list \
1080 --service $1 --interface $2 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001081 -c URL -f value)
1082}
1083
Jim Rollenhagen47367072015-12-10 14:24:00 +00001084# check if we are using ironic with hardware
1085# TODO(jroll) this is a kludge left behind when ripping ironic code
1086# out of tree, as it is used by nova and neutron.
1087# figure out a way to refactor nova/neutron code to eliminate this
1088function is_ironic_hardware {
Lucas Alvares Gomes8a4dea22016-02-18 15:52:22 +00001089 is_service_enabled ironic && [[ "$IRONIC_IS_HARDWARE" == "True" ]] && return 0
Jim Rollenhagen47367072015-12-10 14:24:00 +00001090 return 1
1091}
1092
Julia Kreger6af3cb92021-03-11 11:28:47 -08001093function is_ironic_enforce_scope {
1094 is_service_enabled ironic && [[ "$IRONIC_ENFORCE_SCOPE" == "True" ]] && return 0
1095 return 1
1096}
1097
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001098
Dean Troyerdff49a22014-01-30 15:37:40 -06001099# Package Functions
1100# =================
1101
1102# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +11001103function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001104 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -06001105 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001106
1107 if [[ -z "$base_dir" ]]; then
1108 base_dir=$FILES
1109 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001110 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001111 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -06001112 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001113 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -06001114 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001115 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -06001116 else
1117 exit_distro_not_supported "list of packages"
1118 fi
1119 echo "$pkg_dir"
1120}
1121
Sean Dague88ee8ce2015-12-02 07:47:31 -05001122# Wrapper for ``apt-get update`` to try multiple times on the update
1123# to address bad package mirrors (which happen all the time).
1124function apt_get_update {
1125 # only do this once per run
1126 if [[ "$REPOS_UPDATED" == "True" && "$RETRY_UPDATE" != "True" ]]; then
1127 return
1128 fi
1129
1130 # bail if we are offline
1131 [[ "$OFFLINE" = "True" ]] && return
1132
1133 local sudo="sudo"
1134 [[ "$(id -u)" = "0" ]] && sudo="env"
1135
1136 # time all the apt operations
1137 time_start "apt-get-update"
1138
1139 local proxies="http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} no_proxy=${no_proxy:-} "
1140 local update_cmd="$sudo $proxies apt-get update"
1141 if ! timeout 300 sh -c "while ! $update_cmd; do sleep 30; done"; then
1142 die $LINENO "Failed to update apt repos, we're dead now"
1143 fi
1144
1145 REPOS_UPDATED=True
1146 # stop the clock
1147 time_stop "apt-get-update"
1148}
1149
Dean Troyerdff49a22014-01-30 15:37:40 -06001150# Wrapper for ``apt-get`` to set cache and proxy environment variables
1151# Uses globals ``OFFLINE``, ``*_proxy``
1152# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001153function apt_get {
Federico Ressie208d062015-11-21 11:15:39 +00001154 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +11001155 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001156 set +o xtrace
1157
Dean Troyerdff49a22014-01-30 15:37:40 -06001158 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
1159 local sudo="sudo"
1160 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -05001161
Sean Dague95c33d52015-10-07 11:05:59 -04001162 # time all the apt operations
1163 time_start "apt-get"
1164
Sean Dague45917cc2014-02-24 16:09:14 -05001165 $xtrace
Sean Dague53753292014-12-04 19:38:15 -05001166
Dean Troyerdff49a22014-01-30 15:37:40 -06001167 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -05001168 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
1169 no_proxy=${no_proxy:-} \
John L. Villalovosf568c3a2016-01-07 19:10:50 -08001170 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" < /dev/null
Federico Ressie208d062015-11-21 11:15:39 +00001171 result=$?
Sean Dague95c33d52015-10-07 11:05:59 -04001172
1173 # stop the clock
1174 time_stop "apt-get"
Federico Ressie208d062015-11-21 11:15:39 +00001175 return $result
Dean Troyerdff49a22014-01-30 15:37:40 -06001176}
1177
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001178function _parse_package_files {
1179 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -06001180
Dean Troyerdff49a22014-01-30 15:37:40 -06001181 if [[ -z "$DISTRO" ]]; then
1182 GetDistro
1183 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001184
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001185 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001186 local OIFS line package distros distro
1187 [[ -e $fname ]] || continue
1188
1189 OIFS=$IFS
1190 IFS=$'\n'
1191 for line in $(<${fname}); do
1192 if [[ $line =~ "NOPRIME" ]]; then
1193 continue
1194 fi
1195
Ian Wienand1ff75ff2016-02-19 14:28:37 +11001196 # Assume we want this package; free-form
1197 # comments allowed after a #
1198 package=${line%%#*}
Dean Troyerdff49a22014-01-30 15:37:40 -06001199 inst_pkg=1
1200
1201 # Look for # dist:xxx in comment
1202 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1203 # We are using BASH regexp matching feature.
1204 package=${BASH_REMATCH[1]}
1205 distros=${BASH_REMATCH[2]}
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001206 # In bash ${VAR,,} will lowercase VAR
Dean Troyerdff49a22014-01-30 15:37:40 -06001207 # Look for a match in the distro list
1208 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1209 # If no match then skip this package
1210 inst_pkg=0
1211 fi
1212 fi
1213
David Rabel682e0ab2017-03-17 19:19:00 +01001214 # Look for # not:xxx in comment
1215 if [[ $line =~ (.*)#.*not:([^ ]*) ]]; then
1216 # We are using BASH regexp matching feature.
1217 package=${BASH_REMATCH[1]}
1218 distros=${BASH_REMATCH[2]}
1219 # In bash ${VAR,,} will lowercase VAR
1220 # Look for a match in the distro list
1221 if [[ ${distros,,} =~ ${DISTRO,,} ]]; then
1222 # If match then skip this package
1223 inst_pkg=0
1224 fi
1225 fi
1226
Dean Troyerdff49a22014-01-30 15:37:40 -06001227 if [[ $inst_pkg = 1 ]]; then
1228 echo $package
1229 fi
1230 done
1231 IFS=$OIFS
1232 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001233}
1234
1235# get_packages() collects a list of package names of any type from the
1236# prerequisite files in ``files/{debs|rpms}``. The list is intended
1237# to be passed to a package installer such as apt or yum.
1238#
1239# Only packages required for the services in 1st argument will be
1240# included. Two bits of metadata are recognized in the prerequisite files:
1241#
1242# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1243# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1244# of the package to the distros listed. The distro names are case insensitive.
David Rabel682e0ab2017-03-17 19:19:00 +01001245# - ``# not:DISTRO`` or ``not:DISTRO1,DISTRO2`` limits the selection
1246# of the package to the distros not listed. The distro names are case insensitive.
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001247function get_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001248 local xtrace
1249 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001250 set +o xtrace
1251 local services=$@
Ian Wienandada886d2015-10-07 14:06:26 +11001252 local package_dir
1253 package_dir=$(_get_package_dir)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001254 local file_to_parse=""
1255 local service=""
1256
Ian Wienand7d515b52015-11-09 15:04:32 +11001257 if [ $# -ne 1 ]; then
1258 die $LINENO "get_packages takes a single, comma-separated argument"
1259 fi
1260
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001261 if [[ -z "$package_dir" ]]; then
1262 echo "No package directory supplied"
1263 return 1
1264 fi
1265 for service in ${services//,/ }; do
1266 # Allow individual services to specify dependencies
1267 if [[ -e ${package_dir}/${service} ]]; then
1268 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1269 fi
1270 # NOTE(sdague) n-api needs glance for now because that's where
1271 # glance client is
1272 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001273 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001274 file_to_parse="${file_to_parse} ${package_dir}/nova"
1275 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001276 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001277 file_to_parse="${file_to_parse} ${package_dir}/glance"
1278 fi
Luigi Toscano52e52d82020-08-13 09:55:08 +02001279 if [[ ! $file_to_parse =~ $package_dir/os-brick ]]; then
1280 file_to_parse="${file_to_parse} ${package_dir}/os-brick"
1281 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001282 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001283 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001284 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1285 fi
Luigi Toscano52e52d82020-08-13 09:55:08 +02001286 if [[ ! $file_to_parse =~ $package_dir/os-brick ]]; then
1287 file_to_parse="${file_to_parse} ${package_dir}/os-brick"
1288 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001289 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001290 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001291 file_to_parse="${file_to_parse} ${package_dir}/swift"
1292 fi
1293 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001294 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001295 file_to_parse="${file_to_parse} ${package_dir}/nova"
1296 fi
Luigi Toscano52e52d82020-08-13 09:55:08 +02001297 if [[ ! $file_to_parse =~ $package_dir/os-brick ]]; then
1298 file_to_parse="${file_to_parse} ${package_dir}/os-brick"
1299 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001300 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001301 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001302 file_to_parse="${file_to_parse} ${package_dir}/glance"
1303 fi
1304 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001305 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001306 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1307 fi
Ihar Hrachyshka387aadd2017-09-14 09:25:59 -06001308 elif [[ $service == q-* || $service == neutron-* ]]; then
1309 if [[ ! $file_to_parse =~ $package_dir/neutron-common ]]; then
1310 file_to_parse="${file_to_parse} ${package_dir}/neutron-common"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001311 fi
1312 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001313 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001314 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1315 fi
1316 fi
1317 done
1318 echo "$(_parse_package_files $file_to_parse)"
1319 $xtrace
1320}
1321
1322# get_plugin_packages() collects a list of package names of any type from a
1323# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1324# list is intended to be passed to a package installer such as apt or yum.
1325#
1326# Only packages required for enabled and collected plugins will included.
1327#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001328# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001329# in these prerequisite files, see get_packages() for more info.
1330function get_plugin_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001331 local xtrace
1332 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001333 set +o xtrace
1334 local files_to_parse=""
1335 local package_dir=""
1336 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
Ian Wienand7ae97292016-02-16 14:50:53 +11001337 package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
Dmitry Tantsur5979f472016-01-03 18:08:14 +01001338 files_to_parse+=" $package_dir/$plugin"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001339 done
1340 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001341 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001342}
1343
Ian Wienandfa9aadf2019-01-15 18:31:05 +11001344# Search plugins for a bindep.txt file
1345#
1346# Uses globals ``BINDEP_CMD``, ``GITDIR``, ``DEVSTACK_PLUGINS``
1347#
1348# Note this is only valid after BINDEP_CMD is setup in stack.sh, and
1349# is thus not really intended to be called externally.
1350function _get_plugin_bindep_packages {
1351 local xtrace
1352 xtrace=$(set +o | grep xtrace)
1353 set +o xtrace
1354
1355 local bindep_file
1356 local packages
1357
1358 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1359 bindep_file=${GITDIR[$plugin]}/devstack/files/bindep.txt
1360 if [[ -f ${bindep_file} ]]; then
1361 packages+=$($BINDEP_CMD -b --file ${bindep_file} || true)
1362 fi
1363 done
1364 echo "${packages}"
1365 $xtrace
1366}
1367
Dean Troyerdff49a22014-01-30 15:37:40 -06001368# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001369# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001370# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001371function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001372 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1373 REPOS_UPDATED=${REPOS_UPDATED:-False}
1374 RETRY_UPDATE=${RETRY_UPDATE:-False}
1375
Paul Linchpiner9e179742014-07-13 22:23:00 -07001376 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001377 return 0
1378 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001379
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001380 if is_ubuntu; then
Sean Dague88ee8ce2015-12-02 07:47:31 -05001381 apt_get_update
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001382 fi
1383}
1384
1385function real_install_package {
1386 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001387 apt_get install "$@"
1388 elif is_fedora; then
1389 yum_install "$@"
1390 elif is_suse; then
1391 zypper_install "$@"
1392 else
1393 exit_distro_not_supported "installing packages"
1394 fi
1395}
1396
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001397# Distro-agnostic package installer
1398# install_package package [package ...]
1399function install_package {
1400 update_package_repo
Federico Ressiecc1f412015-12-28 15:14:13 +01001401 if ! real_install_package "$@"; then
1402 RETRY_UPDATE=True update_package_repo && real_install_package "$@"
1403 fi
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001404}
1405
Dean Troyerdff49a22014-01-30 15:37:40 -06001406# Distro-agnostic function to tell if a package is installed
1407# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001408function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001409 if [[ -z "$@" ]]; then
1410 return 1
1411 fi
1412
1413 if [[ -z "$os_PACKAGE" ]]; then
1414 GetOSVersion
1415 fi
1416
1417 if [[ "$os_PACKAGE" = "deb" ]]; then
1418 dpkg -s "$@" > /dev/null 2> /dev/null
1419 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1420 rpm --quiet -q "$@"
1421 else
1422 exit_distro_not_supported "finding if a package is installed"
1423 fi
1424}
1425
1426# Distro-agnostic package uninstaller
1427# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001428function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001429 if is_ubuntu; then
1430 apt_get purge "$@"
1431 elif is_fedora; then
Ian Wienand67fd81a2020-04-30 09:24:04 +10001432 sudo dnf remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001433 elif is_suse; then
Gary W. Smith56b39122016-11-16 22:03:43 -08001434 sudo zypper remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001435 else
1436 exit_distro_not_supported "uninstalling packages"
1437 fi
1438}
1439
Ian Wienand67fd81a2020-04-30 09:24:04 +10001440# Wrapper for ``dnf`` to set proxy environment variables
1441# Uses globals ``OFFLINE``, ``*_proxy``
1442# The name is kept for backwards compatability with external
1443# callers, despite none of our supported platforms using yum
1444# any more.
Dean Troyerdff49a22014-01-30 15:37:40 -06001445# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001446function yum_install {
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001447 local result parse_yum_result
1448
Dean Troyerdff49a22014-01-30 15:37:40 -06001449 [[ "$OFFLINE" = "True" ]] && return
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001450
1451 time_start "yum_install"
Ian Wienand67fd81a2020-04-30 09:24:04 +10001452 sudo_with_proxies dnf install -y "$@"
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001453 time_stop "yum_install"
Dean Troyerdff49a22014-01-30 15:37:40 -06001454}
1455
1456# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001457# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001458# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001459function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001460 [[ "$OFFLINE" = "True" ]] && return
1461 local sudo="sudo"
1462 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001463 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1464 no_proxy="${no_proxy:-}" \
Dirk Mueller297a50a2018-06-20 11:08:54 +02001465 zypper --non-interactive install --auto-agree-with-licenses --no-recommends "$@"
Dean Troyerdff49a22014-01-30 15:37:40 -06001466}
1467
Ian Wienand58243f62018-12-13 14:05:53 +11001468# Run bindep and install packages it outputs
1469#
1470# Usage:
1471# install_bindep <path-to-bindep.txt> [profile,profile]
1472#
1473# Note unlike the bindep command itself, profile(s) specified should
1474# be a single, comma-separated string, no spaces.
1475function install_bindep {
1476 local file=$1
1477 local profiles=${2:-""}
1478 local pkgs
1479
1480 if [[ ! -f $file ]]; then
Sean McGinnisdd3731c2020-07-28 08:51:41 -05001481 warn $LINENO "Can not find bindep file: $file"
1482 return
Ian Wienand58243f62018-12-13 14:05:53 +11001483 fi
1484
1485 # converting here makes it much easier to work with passing
1486 # arguments
1487 profiles=${profiles/,/ /}
1488
1489 # Note bindep returns 1 when packages need to be installed, so we
1490 # have to ignore it's return for "-e"
1491 pkgs=$($DEST/bindep-venv/bin/bindep -b --file $file $profiles || true)
1492
1493 if [[ -n "${pkgs}" ]]; then
1494 install_package ${pkgs}
1495 fi
1496}
1497
Sean Dague5edae542017-03-21 20:50:24 -04001498function write_user_unit_file {
1499 local service=$1
1500 local command="$2"
1501 local group=$3
1502 local user=$4
1503 local extra=""
1504 if [[ -n "$group" ]]; then
1505 extra="Group=$group"
1506 fi
1507 local unitfile="$SYSTEMD_DIR/$service"
1508 mkdir -p $SYSTEMD_DIR
1509
1510 iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
1511 iniset -sudo $unitfile "Service" "User" "$user"
1512 iniset -sudo $unitfile "Service" "ExecStart" "$command"
Matthew Treinish477a9622017-08-04 11:09:26 -04001513 iniset -sudo $unitfile "Service" "KillMode" "process"
Michał Dulko3b815a32017-11-14 16:04:51 +01001514 iniset -sudo $unitfile "Service" "TimeoutStopSec" "300"
Dinesh Bhoref60f2b2017-09-05 14:40:32 +05301515 iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
Sean Dague5edae542017-03-21 20:50:24 -04001516 if [[ -n "$group" ]]; then
1517 iniset -sudo $unitfile "Service" "Group" "$group"
1518 fi
1519 iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
1520
1521 # changes to existing units sometimes need a refresh
1522 $SYSTEMCTL daemon-reload
1523}
1524
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001525function write_uwsgi_user_unit_file {
1526 local service=$1
1527 local command="$2"
1528 local group=$3
1529 local user=$4
1530 local unitfile="$SYSTEMD_DIR/$service"
1531 mkdir -p $SYSTEMD_DIR
1532
1533 iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
Sean Daguef41bf2a2017-05-05 07:50:52 -04001534 iniset -sudo $unitfile "Service" "SyslogIdentifier" "$service"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001535 iniset -sudo $unitfile "Service" "User" "$user"
1536 iniset -sudo $unitfile "Service" "ExecStart" "$command"
Dinesh Bhoref60f2b2017-09-05 14:40:32 +05301537 iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001538 iniset -sudo $unitfile "Service" "Type" "notify"
Matthew Treinish477a9622017-08-04 11:09:26 -04001539 iniset -sudo $unitfile "Service" "KillMode" "process"
gong yong sheng07b3bc22017-06-05 14:02:28 +08001540 iniset -sudo $unitfile "Service" "Restart" "always"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001541 iniset -sudo $unitfile "Service" "NotifyAccess" "all"
1542 iniset -sudo $unitfile "Service" "RestartForceExitStatus" "100"
1543
1544 if [[ -n "$group" ]]; then
1545 iniset -sudo $unitfile "Service" "Group" "$group"
1546 fi
1547 iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
1548
1549 # changes to existing units sometimes need a refresh
1550 $SYSTEMCTL daemon-reload
1551}
1552
Sean Dague148d58c2017-05-04 13:08:25 -04001553function _common_systemd_pitfalls {
1554 local cmd=$1
1555 # do some sanity checks on $cmd to see things we don't expect to work
1556
1557 if [[ "$cmd" =~ "sudo" ]]; then
Paweł Suder4e16c3d2018-09-26 15:52:13 +02001558 read -r -d '' msg << EOF || true # read returns 1 for EOF, but it is ok here
Sean Dague148d58c2017-05-04 13:08:25 -04001559You are trying to use run_process with sudo, this is not going to work under systemd.
1560
Ian Wienande8a6a022018-10-08 15:20:34 +11001561If you need to run a service as a user other than \$STACK_USER call it with:
Sean Dague148d58c2017-05-04 13:08:25 -04001562
1563 run_process \$name \$cmd \$group \$user
1564EOF
Ian Wienande8a6a022018-10-08 15:20:34 +11001565 die $LINENO "$msg"
Sean Dague148d58c2017-05-04 13:08:25 -04001566 fi
1567
1568 if [[ ! "$cmd" =~ ^/ ]]; then
Paweł Suder4e16c3d2018-09-26 15:52:13 +02001569 read -r -d '' msg << EOF || true # read returns 1 for EOF, but it is ok here
Sean Dague148d58c2017-05-04 13:08:25 -04001570The cmd="$cmd" does not start with an absolute path. It will fail to
1571start under systemd.
1572
1573Please update your run_process stanza to have an absolute path.
1574EOF
Ian Wienande8a6a022018-10-08 15:20:34 +11001575 die $LINENO "$msg"
Sean Dague148d58c2017-05-04 13:08:25 -04001576 fi
1577
1578}
1579
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001580# Helper function to build a basic unit file and run it under systemd.
1581function _run_under_systemd {
Sean Dague5edae542017-03-21 20:50:24 -04001582 local service=$1
1583 local command="$2"
1584 local cmd=$command
Sean Dague148d58c2017-05-04 13:08:25 -04001585 # sanity check the command
1586 _common_systemd_pitfalls "$cmd"
1587
Sean Dague5edae542017-03-21 20:50:24 -04001588 local systemd_service="devstack@$service.service"
1589 local group=$3
1590 local user=${4:-$STACK_USER}
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001591 if [[ "$command" =~ "uwsgi" ]] ; then
1592 write_uwsgi_user_unit_file $systemd_service "$cmd" "$group" "$user"
1593 else
1594 write_user_unit_file $systemd_service "$cmd" "$group" "$user"
1595 fi
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001596
1597 $SYSTEMCTL enable $systemd_service
1598 $SYSTEMCTL start $systemd_service
Sean Dague5edae542017-03-21 20:50:24 -04001599}
1600
Dean Troyerdff49a22014-01-30 15:37:40 -06001601# Find out if a process exists by partial name.
1602# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001603function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001604 local name=$1
1605 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001606 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001607 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001608 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001609}
1610
Dean Troyer3159a822014-08-27 14:13:58 -05001611# Run a single service under screen or directly
1612# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001613# If an optional group is provided sg will be used to run the
1614# command as that group.
Sean Dague148d58c2017-05-04 13:08:25 -04001615# run_process service "command-line" [group] [user]
Ian Wienandaee18c72014-02-21 15:35:08 +11001616function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001617 local service=$1
1618 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001619 local group=$3
Sean Dague5edae542017-03-21 20:50:24 -04001620 local user=$4
Brant Knudsonedc11c22015-12-14 15:32:05 -06001621
Sean Dague5edae542017-03-21 20:50:24 -04001622 local name=$service
Dean Troyerdff49a22014-01-30 15:37:40 -06001623
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001624 time_start "run_process"
Dean Troyer3159a822014-08-27 14:13:58 -05001625 if is_service_enabled $service; then
Sean Daguecdba1b32017-08-30 11:11:06 -04001626 _run_under_systemd "$name" "$command" "$group" "$user"
Dean Troyer3159a822014-08-27 14:13:58 -05001627 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001628 time_stop "run_process"
Dean Troyerdff49a22014-01-30 15:37:40 -06001629}
1630
Dean Troyer3159a822014-08-27 14:13:58 -05001631# Stop a service process
1632# If a PID is available use it, kill the whole process group via TERM
1633# If screen is being used kill the screen window; this will catch processes
1634# that did not leave a PID behind
Radoslav Gerganov2b97a812017-10-10 16:51:12 +03001635# Uses globals ``SERVICE_DIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001636# stop_process service
1637function stop_process {
1638 local service=$1
1639
1640 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer3159a822014-08-27 14:13:58 -05001641
1642 if is_service_enabled $service; then
Sean Dague803acff2017-05-01 10:52:38 -04001643 # Only do this for units which appear enabled, this also
1644 # catches units that don't really exist for cases like
1645 # keystone without a failure.
1646 if $SYSTEMCTL is-enabled devstack@$service.service; then
1647 $SYSTEMCTL stop devstack@$service.service
1648 $SYSTEMCTL disable devstack@$service.service
Sean Dague5edae542017-03-21 20:50:24 -04001649 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001650 fi
1651}
1652
Sean Daguecdba1b32017-08-30 11:11:06 -04001653# use systemctl to check service status
Ian Wienandaee18c72014-02-21 15:35:08 +11001654function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001655 local service
Sean Daguecdba1b32017-08-30 11:11:06 -04001656 for service in ${ENABLED_SERVICES//,/ }; do
1657 # because some things got renamed like key => keystone
1658 if $SYSTEMCTL is-enabled devstack@$service.service; then
1659 # no-pager is needed because otherwise status dumps to a
1660 # pager when in interactive mode, which will stop a manual
1661 # devstack run.
1662 $SYSTEMCTL status devstack@$service.service --no-pager
Dean Troyer3159a822014-08-27 14:13:58 -05001663 fi
Sean Daguecdba1b32017-08-30 11:11:06 -04001664 done
Dean Troyer3159a822014-08-27 14:13:58 -05001665}
1666
Dean Troyer3159a822014-08-27 14:13:58 -05001667
Sean Dague2c65e712014-12-18 09:44:56 -05001668# Plugin Functions
1669# =================
1670
1671DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1672
1673# enable_plugin <name> <url> [branch]
1674#
1675# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1676# ``url`` is a git url
1677# ``branch`` is a gitref. If it's not set, defaults to master
1678function enable_plugin {
1679 local name=$1
1680 local url=$2
1681 local branch=${3:-master}
Omer Anson51584862017-08-24 17:47:37 +03001682 if is_plugin_enabled $name; then
John L. Villalovos21d84c22016-11-11 15:26:11 -08001683 die $LINENO "Plugin attempted to be enabled twice: ${name} ${url} ${branch}"
1684 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001685 DEVSTACK_PLUGINS+=",$name"
1686 GITREPO[$name]=$url
1687 GITDIR[$name]=$DEST/$name
1688 GITBRANCH[$name]=$branch
1689}
1690
Omer Anson51584862017-08-24 17:47:37 +03001691# is_plugin_enabled <name>
1692#
1693# Check if the plugin was enabled, e.g. using enable_plugin
1694#
1695# ``name`` The name with which the plugin was enabled
1696function is_plugin_enabled {
1697 local name=$1
1698 if [[ ",${DEVSTACK_PLUGINS}," =~ ",${name}," ]]; then
1699 return 0
1700 fi
1701 return 1
1702}
1703
Sean Dague2c65e712014-12-18 09:44:56 -05001704# fetch_plugins
1705#
1706# clones all plugins
1707function fetch_plugins {
1708 local plugins="${DEVSTACK_PLUGINS}"
1709 local plugin
1710
1711 # short circuit if nothing to do
1712 if [[ -z $plugins ]]; then
1713 return
1714 fi
1715
Dean Troyerdc97cb72015-03-28 08:20:50 -05001716 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001717 for plugin in ${plugins//,/ }; do
1718 git_clone_by_name $plugin
1719 done
1720}
1721
1722# load_plugin_settings
1723#
1724# Load settings from plugins in the order that they were registered
1725function load_plugin_settings {
1726 local plugins="${DEVSTACK_PLUGINS}"
1727 local plugin
1728
1729 # short circuit if nothing to do
1730 if [[ -z $plugins ]]; then
1731 return
1732 fi
1733
1734 echo "Loading plugin settings"
1735 for plugin in ${plugins//,/ }; do
1736 local dir=${GITDIR[$plugin]}
1737 # source any known settings
1738 if [[ -f $dir/devstack/settings ]]; then
1739 source $dir/devstack/settings
1740 fi
1741 done
1742}
1743
Sean Dague6e275e12015-03-26 05:54:28 -04001744# plugin_override_defaults
1745#
1746# Run an extremely early setting phase for plugins that allows default
1747# overriding of services.
1748function plugin_override_defaults {
1749 local plugins="${DEVSTACK_PLUGINS}"
1750 local plugin
1751
1752 # short circuit if nothing to do
1753 if [[ -z $plugins ]]; then
1754 return
1755 fi
1756
1757 echo "Overriding Configuration Defaults"
1758 for plugin in ${plugins//,/ }; do
1759 local dir=${GITDIR[$plugin]}
1760 # source any overrides
1761 if [[ -f $dir/devstack/override-defaults ]]; then
1762 # be really verbose that an override is happening, as it
1763 # may not be obvious if things fail later.
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001764 echo "$plugin has overridden the following defaults"
Sean Dague6e275e12015-03-26 05:54:28 -04001765 cat $dir/devstack/override-defaults
1766 source $dir/devstack/override-defaults
1767 fi
1768 done
1769}
1770
Sean Dague2c65e712014-12-18 09:44:56 -05001771# run_plugins
1772#
1773# Run the devstack/plugin.sh in all the plugin directories. These are
1774# run in registration order.
1775function run_plugins {
1776 local mode=$1
1777 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301778
1779 local plugins="${DEVSTACK_PLUGINS}"
1780 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001781 for plugin in ${plugins//,/ }; do
1782 local dir=${GITDIR[$plugin]}
1783 if [[ -f $dir/devstack/plugin.sh ]]; then
1784 source $dir/devstack/plugin.sh $mode $phase
1785 fi
1786 done
1787}
1788
1789function run_phase {
1790 local mode=$1
1791 local phase=$2
1792 if [[ -d $TOP_DIR/extras.d ]]; then
Dmitry Tantsur64be3212015-10-12 13:10:24 +02001793 local extra_plugin_file_name
1794 for extra_plugin_file_name in $TOP_DIR/extras.d/*.sh; do
Sean Dague41d01102015-12-03 08:12:23 -05001795 # NOTE(sdague): only process extras.d for the 3 explicitly
1796 # white listed elements in tree. We want these to move out
1797 # over time as well, but they are in tree, so we need to
1798 # manage that.
Matt Riedemannc9f63272016-08-30 17:21:30 -04001799 local exceptions="80-tempest.sh"
Ian Wienand5cdee8d2015-10-19 14:17:18 +11001800 local extra
1801 extra=$(basename $extra_plugin_file_name)
Sean Dague1de9e332015-10-07 08:46:13 -04001802 if [[ ! ( $exceptions =~ "$extra" ) ]]; then
Sean Dague41d01102015-12-03 08:12:23 -05001803 warn "use of extras.d is no longer supported"
1804 warn "processing of project $extra is skipped"
1805 else
1806 [[ -r $extra_plugin_file_name ]] && source $extra_plugin_file_name $mode $phase
Sean Dague1de9e332015-10-07 08:46:13 -04001807 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001808 done
1809 fi
1810 # the source phase corresponds to settings loading in plugins
1811 if [[ "$mode" == "source" ]]; then
1812 load_plugin_settings
Chris Dentc6d47012015-10-09 14:57:05 +00001813 verify_disabled_services
Sean Dague6e275e12015-03-26 05:54:28 -04001814 elif [[ "$mode" == "override_defaults" ]]; then
1815 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001816 else
1817 run_plugins $mode $phase
1818 fi
1819}
1820
James E. Blairc5853ac2017-11-21 09:44:42 -08001821# define_plugin <name>
1822#
1823# This function is a no-op. It allows a plugin to define its name So
1824# that other plugins may reference it by name. It should generally be
1825# the last component of the canonical git repo name. E.g.,
1826# openstack/devstack-foo should use "devstack-foo" as the name here.
1827#
1828# This function is currently a noop, but the value may still be used
1829# by external tools (as in plugin_requires) and may be used by
1830# devstack in the future.
1831#
1832# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1833function define_plugin {
1834 :
1835}
1836
1837# plugin_requires <name> <other>
1838#
1839# This function is a no-op. It is currently used by external tools
1840# (such as the devstack module for Ansible) to automatically generate
1841# local.conf files. It is not currently used by devstack itself to
1842# resolve dependencies.
1843#
1844# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1845# ``other`` is the name of another plugin
1846function plugin_requires {
1847 :
1848}
1849
Dean Troyerdff49a22014-01-30 15:37:40 -06001850
1851# Service Functions
1852# =================
1853
1854# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1855# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001856function _cleanup_service_list {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001857 local xtrace
1858 xtrace=$(set +o | grep xtrace)
1859 set +o xtrace
1860
Dean Troyerdff49a22014-01-30 15:37:40 -06001861 echo "$1" | sed -e '
1862 s/,,/,/g;
1863 s/^,//;
1864 s/,$//
1865 '
Ian Wienand8043bfa2015-10-14 14:53:18 +11001866
1867 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001868}
1869
1870# disable_all_services() removes all current services
1871# from ``ENABLED_SERVICES`` to reset the configuration
1872# before a minimal installation
1873# Uses global ``ENABLED_SERVICES``
1874# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001875function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001876 ENABLED_SERVICES=""
1877}
1878
1879# Remove all services starting with '-'. For example, to install all default
1880# services except rabbit (rabbit) set in ``localrc``:
1881# ENABLED_SERVICES+=",-rabbit"
1882# Uses global ``ENABLED_SERVICES``
1883# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001884function disable_negated_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001885 local xtrace
1886 xtrace=$(set +o | grep xtrace)
1887 set +o xtrace
1888
Ian Wienand2796a822015-04-15 08:59:04 +10001889 local to_remove=""
1890 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001891 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001892
1893 # build up list of services that should be removed; i.e. they
1894 # begin with "-"
1895 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001896 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001897 to_remove+=",${service#-}"
1898 else
1899 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001900 fi
1901 done
Ian Wienand2796a822015-04-15 08:59:04 +10001902
1903 # go through the service list. if this service appears in the "to
1904 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001905 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001906
1907 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001908}
1909
Chris Dentc6d47012015-10-09 14:57:05 +00001910# disable_service() prepares the services passed as argument to be
1911# removed from the ``ENABLED_SERVICES`` list, if they are present.
Dean Troyerdff49a22014-01-30 15:37:40 -06001912#
1913# For example:
1914# disable_service rabbit
1915#
Chris Dentc6d47012015-10-09 14:57:05 +00001916# Uses global ``DISABLED_SERVICES``
Dean Troyerdff49a22014-01-30 15:37:40 -06001917# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001918function disable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001919 local xtrace
1920 xtrace=$(set +o | grep xtrace)
1921 set +o xtrace
1922
Chris Dentc6d47012015-10-09 14:57:05 +00001923 local disabled_svcs="${DISABLED_SERVICES}"
1924 local enabled_svcs=",${ENABLED_SERVICES},"
Dean Troyerdff49a22014-01-30 15:37:40 -06001925 local service
1926 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001927 disabled_svcs+=",$service"
Dean Troyerdff49a22014-01-30 15:37:40 -06001928 if is_service_enabled $service; then
Chris Dentc6d47012015-10-09 14:57:05 +00001929 enabled_svcs=${enabled_svcs//,$service,/,}
Dean Troyerdff49a22014-01-30 15:37:40 -06001930 fi
1931 done
Chris Dentc6d47012015-10-09 14:57:05 +00001932 DISABLED_SERVICES=$(_cleanup_service_list "$disabled_svcs")
1933 ENABLED_SERVICES=$(_cleanup_service_list "$enabled_svcs")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001934
1935 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001936}
1937
1938# enable_service() adds the services passed as argument to the
1939# ``ENABLED_SERVICES`` list, if they are not already present.
1940#
1941# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001942# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001943#
1944# This function does not know about the special cases
1945# for nova, glance, and neutron built into is_service_enabled().
1946# Uses global ``ENABLED_SERVICES``
1947# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001948function enable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001949 local xtrace
1950 xtrace=$(set +o | grep xtrace)
1951 set +o xtrace
1952
Dean Troyerdff49a22014-01-30 15:37:40 -06001953 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001954 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001955 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001956 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
1957 warn $LINENO "Attempt to enable_service ${service} when it has been disabled"
1958 continue
1959 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001960 if ! is_service_enabled $service; then
1961 tmpsvcs+=",$service"
1962 fi
1963 done
1964 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1965 disable_negated_services
Ian Wienand8043bfa2015-10-14 14:53:18 +11001966
1967 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001968}
1969
1970# is_service_enabled() checks if the service(s) specified as arguments are
1971# enabled by the user in ``ENABLED_SERVICES``.
1972#
1973# Multiple services specified as arguments are ``OR``'ed together; the test
1974# is a short-circuit boolean, i.e it returns on the first match.
1975#
1976# There are special cases for some 'catch-all' services::
1977# **nova** returns true if any service enabled start with **n-**
1978# **cinder** returns true if any service enabled start with **c-**
Dean Troyerdff49a22014-01-30 15:37:40 -06001979# **glance** returns true if any service enabled start with **g-**
1980# **neutron** returns true if any service enabled start with **q-**
1981# **swift** returns true if any service enabled start with **s-**
1982# **trove** returns true if any service enabled start with **tr-**
1983# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1984# **s-** services will be enabled. This will be deprecated in the future.
1985#
Dean Troyerdff49a22014-01-30 15:37:40 -06001986# Uses global ``ENABLED_SERVICES``
1987# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001988function is_service_enabled {
Ian Wienand433a9b12015-10-07 13:29:31 +11001989 local xtrace
1990 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001991 set +o xtrace
Ian Wienand8043bfa2015-10-14 14:53:18 +11001992
Sean Dague45917cc2014-02-24 16:09:14 -05001993 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001994 local services=$@
1995 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001996 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001997 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001998
1999 # Look for top-level 'enabled' function for this service
2000 if type is_${service}_enabled >/dev/null 2>&1; then
2001 # A function exists for this service, use it
Christian Schwede3db0aad2015-09-10 11:15:39 +00002002 is_${service}_enabled && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002003 fi
2004
2005 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
2006 # are implemented
2007
John Kasperski7b3ae532016-04-24 22:56:04 -05002008 [[ ${service} == n-cpu-* && ,${ENABLED_SERVICES} =~ ,"n-cpu" ]] && enabled=0
2009 [[ ${service} == "nova" && ,${ENABLED_SERVICES} =~ ,"n-" ]] && enabled=0
2010 [[ ${service} == "glance" && ,${ENABLED_SERVICES} =~ ,"g-" ]] && enabled=0
2011 [[ ${service} == "neutron" && ,${ENABLED_SERVICES} =~ ,"q-" ]] && enabled=0
2012 [[ ${service} == "trove" && ,${ENABLED_SERVICES} =~ ,"tr-" ]] && enabled=0
2013 [[ ${service} == "swift" && ,${ENABLED_SERVICES} =~ ,"s-" ]] && enabled=0
2014 [[ ${service} == s-* && ,${ENABLED_SERVICES} =~ ,"swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002015 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11002016
Sean Dague45917cc2014-02-24 16:09:14 -05002017 $xtrace
2018 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06002019}
2020
fumihiko kakuma8606c982015-04-13 09:55:06 +09002021# remove specified list from the input string
2022# remove_disabled_services service-list remove-list
2023function remove_disabled_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11002024 local xtrace
2025 xtrace=$(set +o | grep xtrace)
2026 set +o xtrace
2027
fumihiko kakuma8606c982015-04-13 09:55:06 +09002028 local service_list=$1
2029 local remove_list=$2
2030 local service
2031 local enabled=""
2032
2033 for service in ${service_list//,/ }; do
2034 local remove
2035 local add=1
2036 for remove in ${remove_list//,/ }; do
2037 if [[ ${remove} == ${service} ]]; then
2038 add=0
2039 break
2040 fi
2041 done
2042 if [[ $add == 1 ]]; then
2043 enabled="${enabled},$service"
2044 fi
2045 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11002046
2047 $xtrace
2048
fumihiko kakuma8606c982015-04-13 09:55:06 +09002049 _cleanup_service_list "$enabled"
2050}
2051
Dean Troyerdff49a22014-01-30 15:37:40 -06002052# Toggle enable/disable_service for services that must run exclusive of each other
2053# $1 The name of a variable containing a space-separated list of services
2054# $2 The name of a variable in which to store the enabled service's name
2055# $3 The name of the service to enable
2056function use_exclusive_service {
2057 local options=${!1}
2058 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002059 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06002060 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002061 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06002062 for opt in $options;do
2063 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
2064 done
2065 eval "$out=$selection"
2066 return 0
2067}
2068
Chris Dentc6d47012015-10-09 14:57:05 +00002069# Make sure that nothing has manipulated ENABLED_SERVICES in a way
2070# that conflicts with prior calls to disable_service.
2071# Uses global ``ENABLED_SERVICES``
2072function verify_disabled_services {
2073 local service
2074 for service in ${ENABLED_SERVICES//,/ }; do
2075 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
2076 die $LINENO "ENABLED_SERVICES directly modified to overcome 'disable_service ${service}'"
2077 fi
2078 done
2079}
2080
Dean Troyerdff49a22014-01-30 15:37:40 -06002081
Masayuki Igawaf6368d32014-02-20 13:31:26 +09002082# System Functions
2083# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06002084
2085# Only run the command if the target file (the last arg) is not on an
2086# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002087function _safe_permission_operation {
Ian Wienand433a9b12015-10-07 13:29:31 +11002088 local xtrace
2089 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05002090 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002091 local args=( $@ )
2092 local last
2093 local sudo_cmd
2094 local dir_to_check
2095
2096 let last="${#args[*]} - 1"
2097
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002098 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002099 if [ ! -d "$dir_to_check" ]; then
2100 dir_to_check=`dirname "$dir_to_check"`
2101 fi
2102
2103 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002104 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002105 return 0
2106 fi
2107
Ian Wienandbcb2c302020-01-13 16:31:20 +11002108 sudo_cmd="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06002109
Sean Dague45917cc2014-02-24 16:09:14 -05002110 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002111 $sudo_cmd $@
2112}
2113
2114# Exit 0 if address is in network or 1 if address is not in network
2115# ip-range is in CIDR notation: 1.2.3.4/20
2116# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002117function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002118 local ip=$1
2119 local range=$2
2120 local masklen=${range#*/}
Ian Wienandada886d2015-10-07 14:06:26 +11002121 local network
2122 network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2123 local subnet
2124 subnet=$(maskip $ip $(cidr2netmask $masklen))
Dean Troyerdff49a22014-01-30 15:37:40 -06002125 [[ $network == $subnet ]]
2126}
2127
2128# Add a user to a group.
2129# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002130function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002131 local user=$1
2132 local group=$2
2133
Thomas Bechtolda8580852015-05-31 00:04:33 +02002134 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06002135}
2136
2137# Convert CIDR notation to a IPv4 netmask
2138# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002139function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002140 local maskpat="255 255 255 255"
2141 local maskdgt="254 252 248 240 224 192 128"
2142 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2143 echo ${1-0}.${2-0}.${3-0}.${4-0}
2144}
2145
Ian Cordasco69e3c0a2016-09-26 12:53:14 -05002146# Check if this is a valid ipv4 address string
2147function is_ipv4_address {
2148 local address=$1
Jens Harbott7617ac22017-09-19 17:43:48 +00002149 local regex='([0-9]{1,3}\.){3}[0-9]{1,3}'
Ian Cordasco69e3c0a2016-09-26 12:53:14 -05002150 # TODO(clarkb) make this more robust
2151 if [[ "$address" =~ $regex ]] ; then
2152 return 0
2153 else
2154 return 1
2155 fi
2156}
2157
Jens Harbottdc7b4292017-09-19 10:52:32 +00002158# Remove "[]" around urlquoted IPv6 addresses
2159function ipv6_unquote {
2160 echo $1 | tr -d []
2161}
2162
Dean Troyerdff49a22014-01-30 15:37:40 -06002163# Gracefully cp only if source file/dir exists
2164# cp_it source destination
2165function cp_it {
2166 if [ -e $1 ] || [ -d $1 ]; then
2167 cp -pRL $1 $2
2168 fi
2169}
2170
2171# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2172# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2173# ``localrc`` or on the command line if necessary::
2174#
2175# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2176#
2177# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2178
Ian Wienandaee18c72014-02-21 15:35:08 +11002179function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05002180 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002181 export http_proxy=$http_proxy
2182 fi
Sean Dague53753292014-12-04 19:38:15 -05002183 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002184 export https_proxy=$https_proxy
2185 fi
Sean Dague53753292014-12-04 19:38:15 -05002186 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002187 export no_proxy=$no_proxy
2188 fi
2189}
2190
2191# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002192function is_nfs_directory {
Ian Wienandada886d2015-10-07 14:06:26 +11002193 local mount_type
2194 mount_type=`stat -f -L -c %T $1`
Dean Troyerdff49a22014-01-30 15:37:40 -06002195 test "$mount_type" == "nfs"
2196}
2197
2198# Return the network portion of the given IP address using netmask
2199# netmask is in the traditional dotted-quad format
2200# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002201function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002202 local ip=$1
2203 local mask=$2
2204 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
Ian Wienandada886d2015-10-07 14:06:26 +11002205 local subnet
2206 subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
Dean Troyerdff49a22014-01-30 15:37:40 -06002207 echo $subnet
2208}
2209
Michael Turek7938d832016-04-12 14:55:21 -04002210function is_provider_network {
2211 if [ "$Q_USE_PROVIDER_NETWORKING" == "True" ]; then
2212 return 0
2213 fi
2214 return 1
2215}
2216
2217
Ian Wienand6d213df2017-08-22 16:05:16 +10002218# Return just the <major>.<minor> for the given python interpreter
2219function _get_python_version {
2220 local interp=$1
2221 local version
Sean Dague4324f4e2017-09-14 12:59:25 -06002222 # disable erroring out here, otherwise if python 3 doesn't exist we fail hard.
Javier Pena6bd49242017-09-15 15:55:00 +02002223 if [[ -x $(which $interp 2> /dev/null) ]]; then
Sean Dague4324f4e2017-09-14 12:59:25 -06002224 version=$($interp -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
2225 fi
Ian Wienand6d213df2017-08-22 16:05:16 +10002226 echo ${version}
2227}
2228
Chris Dent3a2c86a2015-05-12 13:41:25 +00002229# Return the current python as "python<major>.<minor>"
2230function python_version {
Ian Wienandada886d2015-10-07 14:06:26 +11002231 local python_version
Ian Wienand6d213df2017-08-22 16:05:16 +10002232 python_version=$(_get_python_version python2)
Chris Dent3a2c86a2015-05-12 13:41:25 +00002233 echo "python${python_version}"
2234}
2235
Ian Wienand6d213df2017-08-22 16:05:16 +10002236function python3_version {
2237 local python3_version
2238 python3_version=$(_get_python_version python3)
Doug Hellmann04178582018-06-12 15:37:00 -04002239 echo "python${python3_version}"
Ian Wienand6d213df2017-08-22 16:05:16 +10002240}
2241
2242
Dean Troyerdff49a22014-01-30 15:37:40 -06002243# Service wrapper to restart services
2244# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002245function restart_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002246 if [ -x /bin/systemctl ]; then
2247 sudo /bin/systemctl restart $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002248 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002249 sudo service $1 restart
Dean Troyerdff49a22014-01-30 15:37:40 -06002250 fi
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002251
Dean Troyerdff49a22014-01-30 15:37:40 -06002252}
2253
2254# Only change permissions of a file or directory if it is not on an
2255# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002256function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002257 _safe_permission_operation chmod $@
2258}
2259
2260# Only change ownership of a file or directory if it is not on an NFS
2261# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002262function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002263 _safe_permission_operation chown $@
2264}
2265
2266# Service wrapper to start services
2267# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002268function start_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002269 if [ -x /bin/systemctl ]; then
2270 sudo /bin/systemctl start $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002271 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002272 sudo service $1 start
Dean Troyerdff49a22014-01-30 15:37:40 -06002273 fi
2274}
2275
2276# Service wrapper to stop services
2277# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002278function stop_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002279 if [ -x /bin/systemctl ]; then
2280 sudo /bin/systemctl stop $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002281 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002282 sudo service $1 stop
Dean Troyerdff49a22014-01-30 15:37:40 -06002283 fi
2284}
2285
Attila Fazekasbcaadd62016-11-23 12:43:02 +01002286# Service wrapper to reload services
2287# If the service was not in running state it will start it
Gregory Haynes4b49e402016-08-31 18:19:51 -07002288# reload_service service-name
2289function reload_service {
2290 if [ -x /bin/systemctl ]; then
Attila Fazekasbcaadd62016-11-23 12:43:02 +01002291 sudo /bin/systemctl reload-or-restart $1
Gregory Haynes4b49e402016-08-31 18:19:51 -07002292 else
2293 sudo service $1 reload
2294 fi
2295}
2296
Sean Dague442e4e92015-06-24 13:24:02 -04002297# Test with a finite retry loop.
2298#
2299function test_with_retry {
2300 local testcmd=$1
2301 local failmsg=$2
2302 local until=${3:-10}
2303 local sleep=${4:-0.5}
2304
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002305 time_start "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002306 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2307 die $LINENO "$failmsg"
2308 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002309 time_stop "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002310}
2311
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00002312# Like sudo but forwarding http_proxy https_proxy no_proxy environment vars.
2313# If it is run as superuser then sudo is replaced by env.
2314#
2315function sudo_with_proxies {
2316 local sudo
2317
2318 [[ "$(id -u)" = "0" ]] && sudo="env" || sudo="sudo"
2319
2320 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}"\
2321 no_proxy="${no_proxy:-}" "$@"
2322}
2323
Sean Dague95c33d52015-10-07 11:05:59 -04002324# Timing infrastructure - figure out where large blocks of time are
2325# used in DevStack
2326#
2327# The timing infrastructure for DevStack is about collecting buckets
2328# of time that are spend in some subtask. For instance, that might be
2329# 'apt', 'pip', 'osc', even database migrations. We do this by a pair
2330# of functions: time_start / time_stop.
2331#
2332# These take a single parameter: $name - which specifies the name of
2333# the bucket to be accounted against. time_totals function spits out
2334# the results.
2335#
2336# Resolution is only in whole seconds, so should be used for long
2337# running activities.
2338
Sean Dagueafef8bf2017-03-06 14:07:23 -05002339declare -A -g _TIME_TOTAL
2340declare -A -g _TIME_START
2341declare -r -g _TIME_BEGIN=$(date +%s)
Sean Dague95c33d52015-10-07 11:05:59 -04002342
2343# time_start $name
2344#
2345# starts the clock for a timer by name. Errors if that clock is
2346# already started.
2347function time_start {
2348 local name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002349 local start_time=${_TIME_START[$name]}
Sean Dague95c33d52015-10-07 11:05:59 -04002350 if [[ -n "$start_time" ]]; then
2351 die $LINENO "Trying to start the clock on $name, but it's already been started"
2352 fi
Sean Dague85cf2932017-03-27 15:35:13 -04002353 _TIME_START[$name]=$(date +%s%3N)
Sean Dague95c33d52015-10-07 11:05:59 -04002354}
2355
2356# time_stop $name
2357#
2358# stops the clock for a timer by name, and accumulate that time in the
2359# global counter for that name. Errors if that clock had not
2360# previously been started.
2361function time_stop {
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002362 local name
2363 local end_time
Rob Crittenden38245da2016-05-26 17:55:14 -04002364 local elapsed_time
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002365 local total
2366 local start_time
2367
2368 name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002369 start_time=${_TIME_START[$name]}
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002370
Sean Dague95c33d52015-10-07 11:05:59 -04002371 if [[ -z "$start_time" ]]; then
2372 die $LINENO "Trying to stop the clock on $name, but it was never started"
2373 fi
Sean Dague85cf2932017-03-27 15:35:13 -04002374 end_time=$(date +%s%3N)
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002375 elapsed_time=$(($end_time - $start_time))
Ian Wienand908a3a92016-03-29 14:47:09 +11002376 total=${_TIME_TOTAL[$name]:-0}
Sean Dague95c33d52015-10-07 11:05:59 -04002377 # reset the clock so we can start it in the future
Ian Wienand908a3a92016-03-29 14:47:09 +11002378 _TIME_START[$name]=""
2379 _TIME_TOTAL[$name]=$(($total + $elapsed_time))
Sean Dague95c33d52015-10-07 11:05:59 -04002380}
2381
Sean Dague85cf2932017-03-27 15:35:13 -04002382function oscwrap {
Ian Wienand474f5352019-08-08 09:15:11 +10002383 local xtrace
2384 xtrace=$(set +o | grep xtrace)
2385 set +o xtrace
2386
Sean Dague85cf2932017-03-27 15:35:13 -04002387 local out
2388 local rc
2389 local start
2390 local end
2391 # Cannot use timer_start and timer_stop as we run in subshells
2392 # and those rely on modifying vars in the same process (which cannot
2393 # happen from a subshell.
2394 start=$(date +%s%3N)
2395 out=$(command openstack "$@")
2396 rc=$?
2397 end=$(date +%s%3N)
2398 echo $((end - start)) >> $OSCWRAP_TIMER_FILE
2399
2400 echo "$out"
Ian Wienand474f5352019-08-08 09:15:11 +10002401 $xtrace
Sean Dague85cf2932017-03-27 15:35:13 -04002402 return $rc
2403}
2404
2405function install_oscwrap {
2406 # File to accumulate our timing data
2407 OSCWRAP_TIMER_FILE=$(mktemp)
2408 # Bash by default doesn't expand aliases, allow it for the aliases
2409 # we want to whitelist.
2410 shopt -s expand_aliases
2411 # Remove all aliases that might be expanded to preserve old unexpanded
2412 # behavior
2413 unalias -a
2414 # Add only the alias we want for openstack
2415 alias openstack=oscwrap
2416}
2417
2418function cleanup_oscwrap {
2419 local total=0
Stephen Finucaneffd00472018-01-18 15:12:29 +00002420 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 -04002421 _TIME_TOTAL["osc"]=$total
2422 rm $OSCWRAP_TIMER_FILE
2423}
2424
Sean Dague95c33d52015-10-07 11:05:59 -04002425# time_totals
Ian Wienand908a3a92016-03-29 14:47:09 +11002426# Print out total time summary
Sean Dague95c33d52015-10-07 11:05:59 -04002427function time_totals {
Ian Wienand908a3a92016-03-29 14:47:09 +11002428 local elapsed_time
2429 local end_time
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002430 local len=20
Ian Wienand908a3a92016-03-29 14:47:09 +11002431 local xtrace
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002432 local unaccounted_time
Ian Wienand908a3a92016-03-29 14:47:09 +11002433
2434 end_time=$(date +%s)
2435 elapsed_time=$(($end_time - $_TIME_BEGIN))
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002436 unaccounted_time=$elapsed_time
Ian Wienand908a3a92016-03-29 14:47:09 +11002437
2438 # pad 1st column this far
2439 for t in ${!_TIME_TOTAL[*]}; do
2440 if [[ ${#t} -gt $len ]]; then
2441 len=${#t}
2442 fi
Sean Dague95c33d52015-10-07 11:05:59 -04002443 done
Ian Wienand908a3a92016-03-29 14:47:09 +11002444
Sean Dague85cf2932017-03-27 15:35:13 -04002445 cleanup_oscwrap
2446
Ian Wienand908a3a92016-03-29 14:47:09 +11002447 xtrace=$(set +o | grep xtrace)
2448 set +o xtrace
2449
2450 echo
2451 echo "========================="
2452 echo "DevStack Component Timing"
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002453 echo " (times are in seconds) "
Ian Wienand908a3a92016-03-29 14:47:09 +11002454 echo "========================="
Ian Wienand908a3a92016-03-29 14:47:09 +11002455 for t in ${!_TIME_TOTAL[*]}; do
2456 local v=${_TIME_TOTAL[$t]}
Sean Dague85cf2932017-03-27 15:35:13 -04002457 # because we're recording in milliseconds
2458 v=$(($v / 1000))
Ian Wienand908a3a92016-03-29 14:47:09 +11002459 printf "%-${len}s %3d\n" "$t" "$v"
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002460 unaccounted_time=$(($unaccounted_time - $v))
Ian Wienand908a3a92016-03-29 14:47:09 +11002461 done
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002462 echo "-------------------------"
2463 printf "%-${len}s %3d\n" "Unaccounted time" "$unaccounted_time"
Ian Wienand908a3a92016-03-29 14:47:09 +11002464 echo "========================="
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002465 printf "%-${len}s %3d\n" "Total runtime" "$elapsed_time"
Ian Wienand908a3a92016-03-29 14:47:09 +11002466
2467 $xtrace
Sean Dague95c33d52015-10-07 11:05:59 -04002468}
Dean Troyerdff49a22014-01-30 15:37:40 -06002469
Sean Mooneyae21b352020-09-01 14:11:45 +00002470function clean_pyc_files {
2471 # Clean up all *.pyc files
2472 if [[ -n "$DEST" ]] && [[ -d "$DEST" ]]; then
2473 sudo find $DEST -name "*.pyc" -delete
2474 fi
2475}
2476
Dean Troyerdff49a22014-01-30 15:37:40 -06002477# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +11002478$_XTRACE_FUNCTIONS_COMMON
Dean Troyerdff49a22014-01-30 15:37:40 -06002479
2480# Local variables:
2481# mode: shell-script
2482# End: