blob: df295a3395812cb4e56b93c695353823a1656350 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Dean Troyerdff49a22014-01-30 15:37:40 -06003# functions-common - Common functions used by DevStack components
4#
5# The canonical copy of this file is maintained in the DevStack repo.
6# All modifications should be made there and then sync'ed to other repos
7# as required.
8#
9# This file is sorted alphabetically within the function groups.
10#
11# - Config Functions
12# - Control Functions
13# - Distro Functions
14# - Git Functions
15# - OpenStack Functions
16# - Package Functions
17# - Process Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060018# - Service Functions
Masayuki Igawaf6368d32014-02-20 13:31:26 +090019# - System Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060020#
21# The following variables are assumed to be defined by certain functions:
22#
23# - ``ENABLED_SERVICES``
24# - ``ERROR_ON_CLONE``
25# - ``FILES``
26# - ``OFFLINE``
Dean Troyerdff49a22014-01-30 15:37:40 -060027# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090028# - ``REQUIREMENTS_DIR``
29# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060030# - ``TRACK_DEPENDS``
31# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050032#
Dean Troyerdff49a22014-01-30 15:37:40 -060033
34# Save trace setting
Ian Wienand523f4882015-10-13 11:03:03 +110035_XTRACE_FUNCTIONS_COMMON=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -060036set +o xtrace
37
Ian Wienand4ffb4542015-06-30 11:00:32 +100038# ensure we don't re-source this in the same environment
39[[ -z "$_DEVSTACK_FUNCTIONS_COMMON" ]] || return 0
Sean Dagueafef8bf2017-03-06 14:07:23 -050040declare -r -g _DEVSTACK_FUNCTIONS_COMMON=1
Ian Wienand4ffb4542015-06-30 11:00:32 +100041
Sean Daguecc524062014-10-01 09:06:43 -040042# Global Config Variables
Sean Dagueafef8bf2017-03-06 14:07:23 -050043declare -A -g GITREPO
44declare -A -g GITBRANCH
45declare -A -g GITDIR
Sean Daguecc524062014-10-01 09:06:43 -040046
Sean Dague53753292014-12-04 19:38:15 -050047TRACK_DEPENDS=${TRACK_DEPENDS:-False}
Dinesh Bhoref60f2b2017-09-05 14:40:32 +053048KILL_PATH="$(which kill)"
Sean Dague53753292014-12-04 19:38:15 -050049
Dean Troyer68162342015-05-13 15:41:03 -050050# Save these variables to .stackenv
51STACK_ENV_VARS="BASE_SQL_CONN DATA_DIR DEST ENABLED_SERVICES HOST_IP \
Sean Dague0effa1a2017-04-18 15:16:37 -040052 KEYSTONE_AUTH_URI KEYSTONE_SERVICE_URI \
53 LOGFILE OS_CACERT SERVICE_HOST STACK_USER TLS_IP \
Brian Haley5d04db22015-06-16 13:14:31 -040054 HOST_IPV6 SERVICE_IP_VERSION"
Dean Troyer68162342015-05-13 15:41:03 -050055
56
57# Saves significant environment variables to .stackenv for later use
58# Refers to a lot of globals, only TOP_DIR and STACK_ENV_VARS are required to
59# function, the rest are simply saved and do not cause problems if they are undefined.
60# save_stackenv [tag]
61function save_stackenv {
62 local tag=${1:-""}
63 # Save some values we generated for later use
64 time_stamp=$(date "+$TIMESTAMP_FORMAT")
65 echo "# $time_stamp $tag" >$TOP_DIR/.stackenv
66 for i in $STACK_ENV_VARS; do
67 echo $i=${!i} >>$TOP_DIR/.stackenv
68 done
69}
Dean Troyerdff49a22014-01-30 15:37:40 -060070
Monty Taylor7224eec2015-09-19 11:26:18 -040071# Update/create user clouds.yaml file.
72# clouds.yaml will have
73# - A `devstack` entry for the `demo` user for the `demo` project.
74# - A `devstack-admin` entry for the `admin` user for the `admin` project.
75# write_clouds_yaml
76function write_clouds_yaml {
Monty Tayloree9bb762015-10-19 15:16:18 -040077 # The location is a variable to allow for easier refactoring later to make it
78 # overridable. There is currently no usecase where doing so makes sense, so
79 # it's not currently configurable.
Monty Taylor7224eec2015-09-19 11:26:18 -040080
Monty Tayloree9bb762015-10-19 15:16:18 -040081 CLOUDS_YAML=/etc/openstack/clouds.yaml
82
83 sudo mkdir -p $(dirname $CLOUDS_YAML)
Lenny Verkhovsky313ddae2015-10-20 11:26:34 +030084 sudo chown -R $STACK_USER /etc/openstack
Lenny Verkhovsky313ddae2015-10-20 11:26:34 +030085
Monty Tayloree9bb762015-10-19 15:16:18 -040086 CA_CERT_ARG=''
87 if [ -f "$SSL_BUNDLE_FILE" ]; then
88 CA_CERT_ARG="--os-cacert $SSL_BUNDLE_FILE"
89 fi
Sean Daguec67d22e2016-02-02 05:51:14 -050090 # demo -> devstack
Davanum Srinivas51ecf0a2017-01-05 16:11:17 -050091 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
Monty Tayloree9bb762015-10-19 15:16:18 -040092 --file $CLOUDS_YAML \
93 --os-cloud devstack \
94 --os-region-name $REGION_NAME \
95 --os-identity-api-version 3 \
96 $CA_CERT_ARG \
Monty Taylore43f60b2017-04-27 18:27:36 -050097 --os-auth-url $KEYSTONE_SERVICE_URI \
Monty Tayloree9bb762015-10-19 15:16:18 -040098 --os-username demo \
99 --os-password $ADMIN_PASSWORD \
100 --os-project-name demo
Sean Daguec67d22e2016-02-02 05:51:14 -0500101
102 # alt_demo -> devstack-alt
Davanum Srinivas51ecf0a2017-01-05 16:11:17 -0500103 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
Sean Daguec67d22e2016-02-02 05:51:14 -0500104 --file $CLOUDS_YAML \
Clark Boylane20cb432016-02-05 12:00:18 -0800105 --os-cloud devstack-alt \
Sean Daguec67d22e2016-02-02 05:51:14 -0500106 --os-region-name $REGION_NAME \
107 --os-identity-api-version 3 \
108 $CA_CERT_ARG \
Monty Taylore43f60b2017-04-27 18:27:36 -0500109 --os-auth-url $KEYSTONE_SERVICE_URI \
Sean Daguec67d22e2016-02-02 05:51:14 -0500110 --os-username alt_demo \
111 --os-password $ADMIN_PASSWORD \
112 --os-project-name alt_demo
113
114 # admin -> devstack-admin
Davanum Srinivas51ecf0a2017-01-05 16:11:17 -0500115 $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
Monty Tayloree9bb762015-10-19 15:16:18 -0400116 --file $CLOUDS_YAML \
117 --os-cloud devstack-admin \
118 --os-region-name $REGION_NAME \
119 --os-identity-api-version 3 \
120 $CA_CERT_ARG \
Monty Taylore43f60b2017-04-27 18:27:36 -0500121 --os-auth-url $KEYSTONE_SERVICE_URI \
Monty Tayloree9bb762015-10-19 15:16:18 -0400122 --os-username admin \
123 --os-password $ADMIN_PASSWORD \
124 --os-project-name admin
Monty Taylor74379df2016-01-21 10:20:08 -0500125
126 # CLean up any old clouds.yaml files we had laying around
ZhiQiang Fan0b4a0092016-04-12 20:26:33 +0800127 rm -f $(eval echo ~"$STACK_USER")/.config/openstack/clouds.yaml
Monty Taylor7224eec2015-09-19 11:26:18 -0400128}
129
Ian Wienande82bac02015-08-25 14:29:08 +1000130# trueorfalse <True|False> <VAR>
131#
132# Normalize config-value provided in variable VAR to either "True" or
133# "False". If VAR is unset (i.e. $VAR evaluates as empty), the value
134# of the second argument will be used as the default value.
135#
136# Accepts as False: 0 no No NO false False FALSE
137# Accepts as True: 1 yes Yes YES true True TRUE
138#
139# usage:
140# VAL=$(trueorfalse False VAL)
Ian Wienandaee18c72014-02-21 15:35:08 +1100141function trueorfalse {
Ian Wienand433a9b12015-10-07 13:29:31 +1100142 local xtrace
143 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -0500144 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600145
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900146 local default=$1
Ian Wienande82bac02015-08-25 14:29:08 +1000147
148 if [ -z $2 ]; then
149 die $LINENO "variable to normalize required"
150 fi
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900151 local testval=${!2:-}
152
153 case "$testval" in
154 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
155 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
156 * ) echo "$default" ;;
157 esac
158
Sean Dague45917cc2014-02-24 16:09:14 -0500159 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600160}
161
Attila Fazekas1bd79592015-02-24 14:06:56 +0100162function isset {
163 [[ -v "$1" ]]
164}
Dean Troyerdff49a22014-01-30 15:37:40 -0600165
Dean Troyer68162342015-05-13 15:41:03 -0500166
Dean Troyerdff49a22014-01-30 15:37:40 -0600167# Control Functions
168# =================
169
170# Prints backtrace info
171# filename:lineno:function
172# backtrace level
173function backtrace {
174 local level=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100175 local deep
176 deep=$((${#BASH_SOURCE[@]} - 1))
Dean Troyerdff49a22014-01-30 15:37:40 -0600177 echo "[Call Trace]"
178 while [ $level -le $deep ]; do
179 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
180 deep=$((deep - 1))
181 done
182}
183
184# Prints line number and "message" then exits
185# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100186function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600187 local exitcode=$?
188 set +o xtrace
189 local line=$1; shift
190 if [ $exitcode == 0 ]; then
191 exitcode=1
192 fi
193 backtrace 2
194 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600195 # Give buffers a second to flush
196 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600197 exit $exitcode
198}
199
200# Checks an environment variable is not set or has length 0 OR if the
201# exit code is non-zero and prints "message" and exits
202# NOTE: env-var is the variable name without a '$'
203# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100204function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600205 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100206 local xtrace
207 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600208 set +o xtrace
209 local line=$1; shift
210 local evar=$1; shift
211 if ! is_set $evar || [ $exitcode != 0 ]; then
212 die $line "$*"
213 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500214 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600215}
216
Sean Dague1de9e332015-10-07 08:46:13 -0400217function deprecated {
218 local text=$1
219 DEPRECATED_TEXT+="\n$text"
YAMAMOTO Takashi8b1bbd62016-12-01 22:29:12 +0900220 echo "WARNING: $text" >&2
Sean Dague1de9e332015-10-07 08:46:13 -0400221}
222
Dean Troyerdff49a22014-01-30 15:37:40 -0600223# Prints line number and "message" in error format
224# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100225function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600226 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100227 local xtrace
228 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600229 set +o xtrace
230 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
231 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600232 if [[ -n ${LOGDIR} ]]; then
233 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600234 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500235 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600236 return $exitcode
237}
238
239# Checks an environment variable is not set or has length 0 OR if the
240# exit code is non-zero and prints "message"
241# NOTE: env-var is the variable name without a '$'
242# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100243function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600244 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100245 local xtrace
246 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600247 set +o xtrace
248 local line=$1; shift
249 local evar=$1; shift
250 if ! is_set $evar || [ $exitcode != 0 ]; then
251 err $line "$*"
252 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500253 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600254 return $exitcode
255}
256
257# Exit after outputting a message about the distribution not being supported.
258# exit_distro_not_supported [optional-string-telling-what-is-missing]
259function exit_distro_not_supported {
260 if [[ -z "$DISTRO" ]]; then
261 GetDistro
262 fi
263
264 if [ $# -gt 0 ]; then
265 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
266 else
267 die $LINENO "Support for $DISTRO is incomplete."
268 fi
269}
270
271# Test if the named environment variable is set and not zero length
272# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100273function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600274 local var=\$"$1"
275 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
276}
277
278# Prints line number and "message" in warning format
279# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100280function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600281 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100282 local xtrace
283 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600284 set +o xtrace
285 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Sean Daguee4af9292015-04-28 08:57:57 -0400286 echo $msg
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500287 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600288 return $exitcode
289}
290
291
292# Distro Functions
293# ================
294
295# Determine OS Vendor, Release and Update
Ian Wienand7710e7f2014-08-27 16:15:32 +1000296
297#
298# NOTE : For portability, you almost certainly do not want to use
299# these variables directly! The "is_*" functions defined below this
300# bundle up compatible platforms under larger umbrellas that we have
301# determinted are compatible enough (e.g. is_ubuntu covers Ubuntu &
302# Debian, is_fedora covers RPM-based distros). Higher-level functions
303# such as "install_package" further abstract things in better ways.
304#
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500305# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
Matt Riedemannff10ac32017-02-13 12:44:24 -0500306# ``os_RELEASE`` - major release: ``16.04`` (Ubuntu), ``23`` (Fedora)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500307# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
Matt Riedemannff10ac32017-02-13 12:44:24 -0500308# ``os_CODENAME`` - vendor's codename for release: ``xenial``
Ian Wienand7710e7f2014-08-27 16:15:32 +1000309
Sean Dagueafef8bf2017-03-06 14:07:23 -0500310declare -g os_VENDOR os_RELEASE os_PACKAGE os_CODENAME
Ian Wienand7710e7f2014-08-27 16:15:32 +1000311
312# Make a *best effort* attempt to install lsb_release packages for the
313# user if not available. Note can't use generic install_package*
314# because they depend on this!
315function _ensure_lsb_release {
Thomas Bechtolddab39012016-03-03 12:11:15 +0100316 if [[ -x $(command -v lsb_release 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000317 return
318 fi
319
Thomas Bechtolddab39012016-03-03 12:11:15 +0100320 if [[ -x $(command -v apt-get 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000321 sudo apt-get install -y lsb-release
Thomas Bechtolddab39012016-03-03 12:11:15 +0100322 elif [[ -x $(command -v zypper 2>/dev/null) ]]; then
Dirk Mueller5ef8a122017-09-25 13:08:51 +0200323 sudo zypper -n install lsb-release
Thomas Bechtolddab39012016-03-03 12:11:15 +0100324 elif [[ -x $(command -v dnf 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000325 sudo dnf install -y redhat-lsb-core
Thomas Bechtolddab39012016-03-03 12:11:15 +0100326 elif [[ -x $(command -v yum 2>/dev/null) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000327 # all rh patforms (fedora, centos, rhel) have this pkg
328 sudo yum install -y redhat-lsb-core
329 else
330 die $LINENO "Unable to find or auto-install lsb_release"
331 fi
332}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500333
Dean Troyerdff49a22014-01-30 15:37:40 -0600334# GetOSVersion
Ian Wienand7710e7f2014-08-27 16:15:32 +1000335# Set the following variables:
336# - os_RELEASE
337# - os_CODENAME
338# - os_VENDOR
339# - os_PACKAGE
Ian Wienandaee18c72014-02-21 15:35:08 +1100340function GetOSVersion {
Ian Wienand7710e7f2014-08-27 16:15:32 +1000341 # We only support distros that provide a sane lsb_release
342 _ensure_lsb_release
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500343
Ian Wienand7710e7f2014-08-27 16:15:32 +1000344 os_RELEASE=$(lsb_release -r -s)
345 os_CODENAME=$(lsb_release -c -s)
346 os_VENDOR=$(lsb_release -i -s)
Dean Troyerdff49a22014-01-30 15:37:40 -0600347
Ian Wienand7710e7f2014-08-27 16:15:32 +1000348 if [[ $os_VENDOR =~ (Debian|Ubuntu|LinuxMint) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600349 os_PACKAGE="deb"
Ian Wienand7710e7f2014-08-27 16:15:32 +1000350 else
351 os_PACKAGE="rpm"
Dean Troyerdff49a22014-01-30 15:37:40 -0600352 fi
Ian Wienand7710e7f2014-08-27 16:15:32 +1000353
354 typeset -xr os_VENDOR
355 typeset -xr os_RELEASE
356 typeset -xr os_PACKAGE
357 typeset -xr os_CODENAME
Dean Troyerdff49a22014-01-30 15:37:40 -0600358}
359
360# Translate the OS version values into common nomenclature
361# Sets global ``DISTRO`` from the ``os_*`` values
Sean Dagueafef8bf2017-03-06 14:07:23 -0500362declare -g DISTRO
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500363
Ian Wienandaee18c72014-02-21 15:35:08 +1100364function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600365 GetOSVersion
ptoohill196006652016-02-15 16:07:50 -0600366 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) || \
367 "$os_VENDOR" =~ (LinuxMint) ]]; then
368 # 'Everyone' refers to Ubuntu / Debian / Mint releases by
Ian Wienand7710e7f2014-08-27 16:15:32 +1000369 # the code name adjective
Dean Troyerdff49a22014-01-30 15:37:40 -0600370 DISTRO=$os_CODENAME
371 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
372 # For Fedora, just use 'f' and the release
373 DISTRO="f$os_RELEASE"
374 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
375 DISTRO="opensuse-$os_RELEASE"
376 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000377 # just use major release
378 DISTRO="sle${os_RELEASE%.*}"
379 elif [[ "$os_VENDOR" =~ (Red.*Hat) || \
anju Tiwari6c639c92014-07-15 18:11:54 +0530380 "$os_VENDOR" =~ (CentOS) || \
Mike Trimm1da4e792016-04-27 11:40:19 -0500381 "$os_VENDOR" =~ (Scientific) || \
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300382 "$os_VENDOR" =~ (OracleServer) || \
Evgeny Antyshev718512c2016-03-03 14:47:58 +0000383 "$os_VENDOR" =~ (Virtuozzo) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600384 # Drop the . release as we assume it's compatible
Ian Wienand7710e7f2014-08-27 16:15:32 +1000385 # XXX re-evaluate when we get RHEL10
Dean Troyerdff49a22014-01-30 15:37:40 -0600386 DISTRO="rhel${os_RELEASE::1}"
387 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000388 DISTRO="xs${os_RELEASE%.*}"
Dean Troyerdff49a22014-01-30 15:37:40 -0600389 else
Ian Wienanded92e432016-02-16 10:56:56 +1100390 # We can't make a good choice here. Setting a sensible DISTRO
391 # is part of the problem, but not the major issue -- we really
392 # only use DISTRO in the code as a fine-filter.
393 #
394 # The bigger problem is categorising the system into one of
395 # our two big categories as Ubuntu/Debian-ish or
396 # Fedora/CentOS-ish.
397 #
398 # The setting of os_PACKAGE above is only set to "deb" based
399 # on a hard-coded list of vendor names ... thus we will
400 # default to thinking unknown distros are RPM based
401 # (ie. is_ubuntu does not match). But the platform will then
402 # also not match in is_fedora, because that also has a list of
403 # names.
404 #
405 # So, if you are reading this, getting your distro supported
406 # is really about making sure it matches correctly in these
407 # functions. Then you can choose a sensible way to construct
408 # DISTRO based on your distros release approach.
409 die $LINENO "Unable to determine DISTRO, can not continue."
Dean Troyerdff49a22014-01-30 15:37:40 -0600410 fi
Ian Wienand7710e7f2014-08-27 16:15:32 +1000411 typeset -xr DISTRO
Dean Troyerdff49a22014-01-30 15:37:40 -0600412}
413
414# Utility function for checking machine architecture
415# is_arch arch-type
416function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500417 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600418}
419
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700420# Determine if current distribution is an Oracle distribution
421# is_oraclelinux
422function is_oraclelinux {
423 if [[ -z "$os_VENDOR" ]]; then
424 GetOSVersion
425 fi
426
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300427 [ "$os_VENDOR" = "OracleServer" ]
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700428}
429
430
Dean Troyerdff49a22014-01-30 15:37:40 -0600431# Determine if current distribution is a Fedora-based distribution
432# (Fedora, RHEL, CentOS, etc).
433# is_fedora
434function is_fedora {
435 if [[ -z "$os_VENDOR" ]]; then
436 GetOSVersion
437 fi
438
anju Tiwari6c639c92014-07-15 18:11:54 +0530439 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
tengqm78d37392016-03-15 23:08:00 -0400440 [ "$os_VENDOR" = "RedHatEnterpriseServer" ] || \
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300441 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ] || \
Markus Zoellerb8335ee2017-09-26 08:32:50 +0200442 [ "$os_VENDOR" = "Virtuozzo" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600443}
444
445
446# Determine if current distribution is a SUSE-based distribution
447# (openSUSE, SLE).
448# is_suse
449function is_suse {
450 if [[ -z "$os_VENDOR" ]]; then
451 GetOSVersion
452 fi
453
Ralf Haferkamp37dddb72016-03-02 15:25:57 +0100454 [[ "$os_VENDOR" =~ (openSUSE) || "$os_VENDOR" == "SUSE LINUX" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600455}
456
457
458# Determine if current distribution is an Ubuntu-based distribution
459# It will also detect non-Ubuntu but Debian-based distros
460# is_ubuntu
461function is_ubuntu {
462 if [[ -z "$os_PACKAGE" ]]; then
463 GetOSVersion
464 fi
465 [ "$os_PACKAGE" = "deb" ]
466}
467
468
469# Git Functions
470# =============
471
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600472# Returns openstack release name for a given branch name
473# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100474function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600475 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700476 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600477 echo ${branch#*/}
478 else
479 echo "master"
480 fi
481}
482
Dean Troyerdff49a22014-01-30 15:37:40 -0600483# git clone only if directory doesn't exist already. Since ``DEST`` might not
484# be owned by the installation user, we create the directory and change the
485# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500486# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
487# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600488# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500489# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600490# git_clone remote dest-dir branch
491function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500492 local git_remote=$1
493 local git_dest=$2
494 local git_ref=$3
Ian Wienandada886d2015-10-07 14:06:26 +1100495 local orig_dir
496 orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200497 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500498
Sean Dague53753292014-12-04 19:38:15 -0500499 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800500 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200501 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
502 fi
503
Dean Troyerdff49a22014-01-30 15:37:40 -0600504 if [[ "$OFFLINE" = "True" ]]; then
505 echo "Running in offline mode, clones already exist"
506 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500507 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600508 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400509 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600510 return
511 fi
512
Dean Troyer50cda692014-07-25 11:57:20 -0500513 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600514 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500515 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700516 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
517 echo "The $git_dest project was not found; if this is a gate job, add"
James E. Blair35a0c572017-09-10 15:37:56 -0700518 echo "the project to 'required-projects' in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600519 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700520 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200521 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600522 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500523 cd $git_dest
524 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600525 else
526 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500527 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700528 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
529 echo "The $git_dest project was not found; if this is a gate job, add"
530 echo "the project to the \$PROJECTS variable in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600531 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700532 fi
Jakub Wachowski90742fc2016-11-18 14:28:47 +0100533 # '--branch' can also take tags
534 git_timed clone $git_clone_flags $git_remote $git_dest --branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600535 elif [[ "$RECLONE" = "True" ]]; then
536 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500537 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600538 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500539 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100540 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600541 # remove the existing ignored files (like pyc) as they cause breakage
542 # (due to the py files having older timestamps than our pyc, so python
543 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500544 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600545
Dean Troyer50cda692014-07-25 11:57:20 -0500546 # handle git_ref accordingly to type (tag, branch)
547 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
548 git_update_tag $git_ref
549 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
550 git_update_branch $git_ref
551 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
552 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600553 else
Dean Troyer50cda692014-07-25 11:57:20 -0500554 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600555 fi
556
557 fi
558 fi
559
560 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500561 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600562 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400563 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600564}
565
Sean Daguecc524062014-10-01 09:06:43 -0400566# A variation on git clone that lets us specify a project by it's
567# actual name, like oslo.config. This is exceptionally useful in the
568# library installation case
569function git_clone_by_name {
570 local name=$1
571 local repo=${GITREPO[$name]}
572 local dir=${GITDIR[$name]}
573 local branch=${GITBRANCH[$name]}
574 git_clone $repo $dir $branch
575}
576
577
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100578# git can sometimes get itself infinitely stuck with transient network
579# errors or other issues with the remote end. This wraps git in a
580# timeout/retry loop and is intended to watch over non-local git
581# processes that might hang. GIT_TIMEOUT, if set, is passed directly
582# to timeout(1); otherwise the default value of 0 maintains the status
583# quo of waiting forever.
584# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100585function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100586 local count=0
587 local timeout=0
588
589 if [[ -n "${GIT_TIMEOUT}" ]]; then
590 timeout=${GIT_TIMEOUT}
591 fi
592
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900593 time_start "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100594 until timeout -s SIGINT ${timeout} git "$@"; do
595 # 124 is timeout(1)'s special return code when it reached the
596 # timeout; otherwise assume fatal failure
597 if [[ $? -ne 124 ]]; then
598 die $LINENO "git call failed: [git $@]"
599 fi
600
601 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400602 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100603 if [ $count -eq 3 ]; then
604 die $LINENO "Maximum of 3 git retries reached"
605 fi
606 sleep 5
607 done
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900608 time_stop "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100609}
610
Dean Troyerdff49a22014-01-30 15:37:40 -0600611# git update using reference as a branch.
612# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100613function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500614 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600615
Dean Troyer50cda692014-07-25 11:57:20 -0500616 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600617 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500618 git branch -D $git_branch || true
619 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600620}
621
622# git update using reference as a branch.
623# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100624function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500625 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600626
Dean Troyer50cda692014-07-25 11:57:20 -0500627 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600628}
629
630# git update using reference as a tag. Be careful editing source at that repo
631# as working copy will be in a detached mode
632# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100633function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500634 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600635
Dean Troyer50cda692014-07-25 11:57:20 -0500636 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600637 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500638 git_timed fetch origin tag $git_tag
639 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600640}
641
642
643# OpenStack Functions
644# ===================
645
646# Get the default value for HOST_IP
647# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100648function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600649 local fixed_range=$1
650 local floating_range=$2
651 local host_ip_iface=$3
652 local host_ip=$4
Brian Haley180f5eb2015-06-16 13:14:31 -0400653 local af=$5
Dean Troyerdff49a22014-01-30 15:37:40 -0600654
Dean Troyerdff49a22014-01-30 15:37:40 -0600655 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
656 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
657 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100658 # Find the interface used for the default route
Brian Haley180f5eb2015-06-16 13:14:31 -0400659 host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
Ian Wienandada886d2015-10-07 14:06:26 +1100660 local host_ips
661 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 -0500662 local ip
663 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600664 # Attempt to filter out IP addresses that are part of the fixed and
665 # floating range. Note that this method only works if the ``netaddr``
666 # python library is installed. If it is not installed, an error
667 # will be printed and the first IP from the interface will be used.
668 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
669 # address.
Brian Haley180f5eb2015-06-16 13:14:31 -0400670 if [[ "$af" == "inet6" ]]; then
671 host_ip=$ip
672 break;
673 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500674 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
675 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600676 break;
677 fi
678 done
679 fi
680 echo $host_ip
681}
682
Attila Fazekasf71b5002014-05-28 09:52:22 +0200683# Generates hex string from ``size`` byte of pseudo random data
684# generate_hex_string size
685function generate_hex_string {
686 local size=$1
687 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
688}
689
Dean Troyerdff49a22014-01-30 15:37:40 -0600690# Grab a numbered field from python prettytable output
691# Fields are numbered starting with 1
692# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
693# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100694function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500695 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600696 while read data; do
697 if [ "$1" -lt 0 ]; then
698 field="(\$(NF$1))"
699 else
700 field="\$$(($1 + 1))"
701 fi
702 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
703 done
704}
705
yuntongjinf26deea2015-02-28 10:50:34 +0800706# install default policy
707# copy over a default policy.json and policy.d for projects
708function install_default_policy {
709 local project=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100710 local project_uc
711 project_uc=$(echo $1|tr a-z A-Z)
yuntongjinf26deea2015-02-28 10:50:34 +0800712 local conf_dir="${project_uc}_CONF_DIR"
713 # eval conf dir to get the variable
714 conf_dir="${!conf_dir}"
715 local project_dir="${project_uc}_DIR"
716 # eval project dir to get the variable
717 project_dir="${!project_dir}"
718 local sample_conf_dir="${project_dir}/etc/${project}"
719 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
720
721 # first copy any policy.json
722 cp -p $sample_conf_dir/policy.json $conf_dir
723 # then optionally copy over policy.d
724 if [[ -d $sample_policy_dir ]]; then
725 cp -r $sample_policy_dir $conf_dir/policy.d
726 fi
727}
728
Dean Troyerdff49a22014-01-30 15:37:40 -0600729# Add a policy to a policy.json file
730# Do nothing if the policy already exists
731# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100732function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600733 local policy_file=$1
734 local policy_name=$2
735 local policy_perm=$3
736
737 if grep -q ${policy_name} ${policy_file}; then
738 echo "Policy ${policy_name} already exists in ${policy_file}"
739 return
740 fi
741
742 # Add a terminating comma to policy lines without one
743 # Remove the closing '}' and all lines following to the end-of-file
Ian Wienandada886d2015-10-07 14:06:26 +1100744 local tmpfile
745 tmpfile=$(mktemp)
Dean Troyerdff49a22014-01-30 15:37:40 -0600746 uniq ${policy_file} | sed -e '
747 s/]$/],/
748 /^[}]/,$d
749 ' > ${tmpfile}
750
751 # Append policy and closing brace
752 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
753 echo "}" >>${tmpfile}
754
755 mv ${tmpfile} ${policy_file}
756}
757
Alistair Coles24779f62014-10-15 18:57:59 +0100758# Gets or creates a domain
759# Usage: get_or_create_domain <name> <description>
760function get_or_create_domain {
Ian Wienand11d276c2015-07-02 09:34:34 +1000761 local domain_id
Alistair Coles24779f62014-10-15 18:57:59 +0100762 # Gets domain id
Ian Wienand11d276c2015-07-02 09:34:34 +1000763 domain_id=$(
Alistair Coles24779f62014-10-15 18:57:59 +0100764 # Gets domain id
Steve Martinelli050a0d52015-09-06 22:03:54 +0000765 openstack domain show $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100766 -f value -c id 2>/dev/null ||
767 # Creates new domain
Steve Martinelli050a0d52015-09-06 22:03:54 +0000768 openstack domain create $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100769 --description "$2" \
770 -f value -c id
771 )
772 echo $domain_id
773}
774
Steve Martinellib74e01c2014-12-18 01:35:35 -0500775# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000776# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500777function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500778 local desc="${3:-}"
Ian Wienand11d276c2015-07-02 09:34:34 +1000779 local group_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500780 # Gets group id
Ian Wienand11d276c2015-07-02 09:34:34 +1000781 group_id=$(
Steve Martinellib74e01c2014-12-18 01:35:35 -0500782 # Creates new group with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000783 openstack group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000784 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500785 -f value -c id
786 )
787 echo $group_id
788}
789
Bartosz Górski0abde392014-02-28 14:15:19 +0100790# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000791# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100792function get_or_create_user {
Ian Wienand11d276c2015-07-02 09:34:34 +1000793 local user_id
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000794 if [[ ! -z "$4" ]]; then
795 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200796 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500797 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200798 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100799 # Gets user id
Ian Wienand11d276c2015-07-02 09:34:34 +1000800 user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500801 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000802 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100803 $1 \
804 --password "$2" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000805 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500806 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500807 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100808 -f value -c id
809 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500810 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100811}
812
813# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000814# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100815function get_or_create_project {
Ian Wienand11d276c2015-07-02 09:34:34 +1000816 local project_id
817 project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500818 # Creates new project with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000819 openstack project create $1 \
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000820 --domain=$2 \
821 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100822 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500823 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100824}
825
826# Gets or creates role
827# Usage: get_or_create_role <name>
828function get_or_create_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000829 local role_id
830 role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500831 # Creates role with --or-show
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000832 openstack role create $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000833 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100834 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500835 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100836}
837
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600838# Returns the domain parts of a function call if present
839# Usage: _get_domain_args [<user_domain> <project_domain>]
840function _get_domain_args {
841 local domain
842 domain=""
843
844 if [[ -n "$1" ]]; then
845 domain="$domain --user-domain $1"
846 fi
847 if [[ -n "$2" ]]; then
848 domain="$domain --project-domain $2"
849 fi
850
851 echo $domain
852}
853
Jamie Lennox9b215db2015-02-10 18:19:57 +1100854# Gets or adds user role to project
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600855# Usage: get_or_add_user_project_role <role> <user> <project> [<user_domain> <project_domain>]
Jamie Lennox9b215db2015-02-10 18:19:57 +1100856function get_or_add_user_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000857 local user_role_id
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600858
859 domain_args=$(_get_domain_args $4 $5)
860
Bartosz Górski0abde392014-02-28 14:15:19 +0100861 # Gets user role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700862 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700863 --role $1 \
Steve Martinelli5541a612015-01-19 15:58:49 -0500864 --user $2 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000865 --project $3 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600866 $domain_args \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700867 | grep '^|\s[a-f0-9]\+' | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500868 if [[ -z "$user_role_id" ]]; then
Roxana Gherle50821be2015-09-22 10:52:46 -0700869 # Adds role to user and get it
870 openstack role add $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100871 --user $2 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600872 --project $3 \
873 $domain_args
Mike Perezc271b3e2016-10-03 16:00:33 -0700874 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700875 --role $1 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700876 --user $2 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700877 --project $3 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600878 $domain_args \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700879 | grep '^|\s[a-f0-9]\+' | get_field 1)
Bartosz Górski0abde392014-02-28 14:15:19 +0100880 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500881 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100882}
883
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500884# Gets or adds user role to domain
885# Usage: get_or_add_user_domain_role <role> <user> <domain>
886function get_or_add_user_domain_role {
887 local user_role_id
888 # Gets user role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700889 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700890 --role $1 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500891 --user $2 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500892 --domain $3 \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700893 | grep '^|\s[a-f0-9]\+' | get_field 1)
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500894 if [[ -z "$user_role_id" ]]; then
895 # Adds role to user and get it
896 openstack role add $1 \
897 --user $2 \
898 --domain $3
Mike Perezc271b3e2016-10-03 16:00:33 -0700899 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700900 --role $1 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500901 --user $2 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500902 --domain $3 \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700903 | grep '^|\s[a-f0-9]\+' | get_field 1)
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500904 fi
905 echo $user_role_id
906}
907
Steve Martinelli4599fd12015-03-12 21:30:58 -0400908# Gets or adds group role to project
909# Usage: get_or_add_group_project_role <role> <group> <project>
910function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000911 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -0400912 # Gets group role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700913 group_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700914 --role $1 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400915 --group $2 \
916 --project $3 \
Mike Perezc271b3e2016-10-03 16:00:33 -0700917 -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400918 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000919 # Adds role to group and get it
920 openstack role add $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000921 --group $2 \
922 --project $3
Mike Perezc271b3e2016-10-03 16:00:33 -0700923 group_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700924 --role $1 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400925 --group $2 \
926 --project $3 \
Mike Perezc271b3e2016-10-03 16:00:33 -0700927 -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400928 fi
929 echo $group_role_id
930}
931
Bartosz Górski0abde392014-02-28 14:15:19 +0100932# Gets or creates service
933# Usage: get_or_create_service <name> <type> <description>
934function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +1000935 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100936 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +1000937 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100938 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +1000939 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100940 # Creates new service if not exists
941 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500942 $2 \
943 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100944 --description="$3" \
945 -f value -c id
946 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500947 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100948}
949
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000950# Create an endpoint with a specific interface
Matt Riedemannae4578b2016-04-23 01:45:40 +0000951# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
952function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +1000953 local endpoint_id
Matt Riedemannae4578b2016-04-23 01:45:40 +0000954 endpoint_id=$(openstack endpoint list \
955 --service $1 \
956 --interface $2 \
957 --region $4 \
958 -c ID -f value)
959 if [[ -z "$endpoint_id" ]]; then
960 # Creates new endpoint
961 endpoint_id=$(openstack endpoint create \
962 $1 $2 $3 --region $4 -f value -c id)
963 fi
964
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500965 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100966}
Dean Troyerdff49a22014-01-30 15:37:40 -0600967
Sean Dague7d1ec432016-04-22 09:19:10 -0400968# Gets or creates endpoint
Sean Dague11eb2012017-02-13 16:16:59 -0500969# Usage: get_or_create_endpoint <service> <region> <publicurl> [adminurl] [internalurl]
Matt Riedemannae4578b2016-04-23 01:45:40 +0000970function get_or_create_endpoint {
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000971 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
972 # creating one endpoint with multiple urls to multiple endpoints each with
973 # a different interface. To maintain the existing function interface we
Matt Riedemannae4578b2016-04-23 01:45:40 +0000974 # create 3 endpoints and return the id of the public one. In reality
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000975 # returning the public id will not make a lot of difference as there are no
976 # scenarios currently that use the returned id. Ideally this behaviour
977 # should be pushed out to the service setups and let them create the
978 # endpoints they need.
Ian Wienandada886d2015-10-07 14:06:26 +1100979 local public_id
Matt Riedemannae4578b2016-04-23 01:45:40 +0000980 public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
Sean Dague11eb2012017-02-13 16:16:59 -0500981 # only create admin/internal urls if provided content for them
982 if [[ -n "$4" ]]; then
983 _get_or_create_endpoint_with_interface $1 admin $4 $2
984 fi
985 if [[ -n "$5" ]]; then
986 _get_or_create_endpoint_with_interface $1 internal $5 $2
987 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000988 # return the public id to indicate success, and this is the endpoint most likely wanted
989 echo $public_id
990}
991
992# Get a URL from the identity service
993# Usage: get_endpoint_url <service> <interface>
994function get_endpoint_url {
995 echo $(openstack endpoint list \
996 --service $1 --interface $2 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000997 -c URL -f value)
998}
999
Jim Rollenhagen47367072015-12-10 14:24:00 +00001000# check if we are using ironic with hardware
1001# TODO(jroll) this is a kludge left behind when ripping ironic code
1002# out of tree, as it is used by nova and neutron.
1003# figure out a way to refactor nova/neutron code to eliminate this
1004function is_ironic_hardware {
Lucas Alvares Gomes8a4dea22016-02-18 15:52:22 +00001005 is_service_enabled ironic && [[ "$IRONIC_IS_HARDWARE" == "True" ]] && return 0
Jim Rollenhagen47367072015-12-10 14:24:00 +00001006 return 1
1007}
1008
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001009
Dean Troyerdff49a22014-01-30 15:37:40 -06001010# Package Functions
1011# =================
1012
1013# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +11001014function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001015 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -06001016 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001017
1018 if [[ -z "$base_dir" ]]; then
1019 base_dir=$FILES
1020 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001021 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001022 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -06001023 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001024 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -06001025 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001026 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -06001027 else
1028 exit_distro_not_supported "list of packages"
1029 fi
1030 echo "$pkg_dir"
1031}
1032
Sean Dague88ee8ce2015-12-02 07:47:31 -05001033# Wrapper for ``apt-get update`` to try multiple times on the update
1034# to address bad package mirrors (which happen all the time).
1035function apt_get_update {
1036 # only do this once per run
1037 if [[ "$REPOS_UPDATED" == "True" && "$RETRY_UPDATE" != "True" ]]; then
1038 return
1039 fi
1040
1041 # bail if we are offline
1042 [[ "$OFFLINE" = "True" ]] && return
1043
1044 local sudo="sudo"
1045 [[ "$(id -u)" = "0" ]] && sudo="env"
1046
1047 # time all the apt operations
1048 time_start "apt-get-update"
1049
1050 local proxies="http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} no_proxy=${no_proxy:-} "
1051 local update_cmd="$sudo $proxies apt-get update"
1052 if ! timeout 300 sh -c "while ! $update_cmd; do sleep 30; done"; then
1053 die $LINENO "Failed to update apt repos, we're dead now"
1054 fi
1055
1056 REPOS_UPDATED=True
1057 # stop the clock
1058 time_stop "apt-get-update"
1059}
1060
Dean Troyerdff49a22014-01-30 15:37:40 -06001061# Wrapper for ``apt-get`` to set cache and proxy environment variables
1062# Uses globals ``OFFLINE``, ``*_proxy``
1063# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001064function apt_get {
Federico Ressie208d062015-11-21 11:15:39 +00001065 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +11001066 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001067 set +o xtrace
1068
Dean Troyerdff49a22014-01-30 15:37:40 -06001069 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
1070 local sudo="sudo"
1071 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -05001072
Sean Dague95c33d52015-10-07 11:05:59 -04001073 # time all the apt operations
1074 time_start "apt-get"
1075
Sean Dague45917cc2014-02-24 16:09:14 -05001076 $xtrace
Sean Dague53753292014-12-04 19:38:15 -05001077
Dean Troyerdff49a22014-01-30 15:37:40 -06001078 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -05001079 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
1080 no_proxy=${no_proxy:-} \
John L. Villalovosf568c3a2016-01-07 19:10:50 -08001081 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" < /dev/null
Federico Ressie208d062015-11-21 11:15:39 +00001082 result=$?
Sean Dague95c33d52015-10-07 11:05:59 -04001083
1084 # stop the clock
1085 time_stop "apt-get"
Federico Ressie208d062015-11-21 11:15:39 +00001086 return $result
Dean Troyerdff49a22014-01-30 15:37:40 -06001087}
1088
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001089function _parse_package_files {
1090 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -06001091
Dean Troyerdff49a22014-01-30 15:37:40 -06001092 if [[ -z "$DISTRO" ]]; then
1093 GetDistro
1094 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001095
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001096 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001097 local OIFS line package distros distro
1098 [[ -e $fname ]] || continue
1099
1100 OIFS=$IFS
1101 IFS=$'\n'
1102 for line in $(<${fname}); do
1103 if [[ $line =~ "NOPRIME" ]]; then
1104 continue
1105 fi
1106
Ian Wienand1ff75ff2016-02-19 14:28:37 +11001107 # Assume we want this package; free-form
1108 # comments allowed after a #
1109 package=${line%%#*}
Dean Troyerdff49a22014-01-30 15:37:40 -06001110 inst_pkg=1
1111
1112 # Look for # dist:xxx in comment
1113 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1114 # We are using BASH regexp matching feature.
1115 package=${BASH_REMATCH[1]}
1116 distros=${BASH_REMATCH[2]}
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001117 # In bash ${VAR,,} will lowercase VAR
Dean Troyerdff49a22014-01-30 15:37:40 -06001118 # Look for a match in the distro list
1119 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1120 # If no match then skip this package
1121 inst_pkg=0
1122 fi
1123 fi
1124
David Rabel682e0ab2017-03-17 19:19:00 +01001125 # Look for # not:xxx in comment
1126 if [[ $line =~ (.*)#.*not:([^ ]*) ]]; then
1127 # We are using BASH regexp matching feature.
1128 package=${BASH_REMATCH[1]}
1129 distros=${BASH_REMATCH[2]}
1130 # In bash ${VAR,,} will lowercase VAR
1131 # Look for a match in the distro list
1132 if [[ ${distros,,} =~ ${DISTRO,,} ]]; then
1133 # If match then skip this package
1134 inst_pkg=0
1135 fi
1136 fi
1137
Dean Troyerdff49a22014-01-30 15:37:40 -06001138 if [[ $inst_pkg = 1 ]]; then
1139 echo $package
1140 fi
1141 done
1142 IFS=$OIFS
1143 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001144}
1145
1146# get_packages() collects a list of package names of any type from the
1147# prerequisite files in ``files/{debs|rpms}``. The list is intended
1148# to be passed to a package installer such as apt or yum.
1149#
1150# Only packages required for the services in 1st argument will be
1151# included. Two bits of metadata are recognized in the prerequisite files:
1152#
1153# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1154# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1155# of the package to the distros listed. The distro names are case insensitive.
David Rabel682e0ab2017-03-17 19:19:00 +01001156# - ``# not:DISTRO`` or ``not:DISTRO1,DISTRO2`` limits the selection
1157# of the package to the distros not listed. The distro names are case insensitive.
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001158function get_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001159 local xtrace
1160 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001161 set +o xtrace
1162 local services=$@
Ian Wienandada886d2015-10-07 14:06:26 +11001163 local package_dir
1164 package_dir=$(_get_package_dir)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001165 local file_to_parse=""
1166 local service=""
1167
Ian Wienand7d515b52015-11-09 15:04:32 +11001168 if [ $# -ne 1 ]; then
1169 die $LINENO "get_packages takes a single, comma-separated argument"
1170 fi
1171
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001172 if [[ -z "$package_dir" ]]; then
1173 echo "No package directory supplied"
1174 return 1
1175 fi
1176 for service in ${services//,/ }; do
1177 # Allow individual services to specify dependencies
1178 if [[ -e ${package_dir}/${service} ]]; then
1179 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1180 fi
1181 # NOTE(sdague) n-api needs glance for now because that's where
1182 # glance client is
1183 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001184 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001185 file_to_parse="${file_to_parse} ${package_dir}/nova"
1186 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001187 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001188 file_to_parse="${file_to_parse} ${package_dir}/glance"
1189 fi
1190 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001191 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001192 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1193 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001194 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001195 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001196 file_to_parse="${file_to_parse} ${package_dir}/swift"
1197 fi
1198 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001199 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001200 file_to_parse="${file_to_parse} ${package_dir}/nova"
1201 fi
1202 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001203 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001204 file_to_parse="${file_to_parse} ${package_dir}/glance"
1205 fi
1206 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001207 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001208 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1209 fi
Ihar Hrachyshka387aadd2017-09-14 09:25:59 -06001210 elif [[ $service == q-* || $service == neutron-* ]]; then
1211 if [[ ! $file_to_parse =~ $package_dir/neutron-common ]]; then
1212 file_to_parse="${file_to_parse} ${package_dir}/neutron-common"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001213 fi
1214 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001215 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001216 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1217 fi
1218 fi
1219 done
1220 echo "$(_parse_package_files $file_to_parse)"
1221 $xtrace
1222}
1223
1224# get_plugin_packages() collects a list of package names of any type from a
1225# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1226# list is intended to be passed to a package installer such as apt or yum.
1227#
1228# Only packages required for enabled and collected plugins will included.
1229#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001230# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001231# in these prerequisite files, see get_packages() for more info.
1232function get_plugin_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001233 local xtrace
1234 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001235 set +o xtrace
1236 local files_to_parse=""
1237 local package_dir=""
1238 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
Ian Wienand7ae97292016-02-16 14:50:53 +11001239 package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
Dmitry Tantsur5979f472016-01-03 18:08:14 +01001240 files_to_parse+=" $package_dir/$plugin"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001241 done
1242 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001243 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001244}
1245
1246# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001247# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001248# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001249function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001250 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1251 REPOS_UPDATED=${REPOS_UPDATED:-False}
1252 RETRY_UPDATE=${RETRY_UPDATE:-False}
1253
Paul Linchpiner9e179742014-07-13 22:23:00 -07001254 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001255 return 0
1256 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001257
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001258 if is_ubuntu; then
Sean Dague88ee8ce2015-12-02 07:47:31 -05001259 apt_get_update
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001260 fi
1261}
1262
1263function real_install_package {
1264 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001265 apt_get install "$@"
1266 elif is_fedora; then
1267 yum_install "$@"
1268 elif is_suse; then
1269 zypper_install "$@"
1270 else
1271 exit_distro_not_supported "installing packages"
1272 fi
1273}
1274
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001275# Distro-agnostic package installer
1276# install_package package [package ...]
1277function install_package {
1278 update_package_repo
Federico Ressiecc1f412015-12-28 15:14:13 +01001279 if ! real_install_package "$@"; then
1280 RETRY_UPDATE=True update_package_repo && real_install_package "$@"
1281 fi
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001282}
1283
Dean Troyerdff49a22014-01-30 15:37:40 -06001284# Distro-agnostic function to tell if a package is installed
1285# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001286function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001287 if [[ -z "$@" ]]; then
1288 return 1
1289 fi
1290
1291 if [[ -z "$os_PACKAGE" ]]; then
1292 GetOSVersion
1293 fi
1294
1295 if [[ "$os_PACKAGE" = "deb" ]]; then
1296 dpkg -s "$@" > /dev/null 2> /dev/null
1297 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1298 rpm --quiet -q "$@"
1299 else
1300 exit_distro_not_supported "finding if a package is installed"
1301 fi
1302}
1303
1304# Distro-agnostic package uninstaller
1305# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001306function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001307 if is_ubuntu; then
1308 apt_get purge "$@"
1309 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001310 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001311 elif is_suse; then
Gary W. Smith56b39122016-11-16 22:03:43 -08001312 sudo zypper remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001313 else
1314 exit_distro_not_supported "uninstalling packages"
1315 fi
1316}
1317
1318# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001319# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001320# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001321function yum_install {
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001322 local result parse_yum_result
1323
Dean Troyerdff49a22014-01-30 15:37:40 -06001324 [[ "$OFFLINE" = "True" ]] && return
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001325
1326 time_start "yum_install"
1327
Ian Wienanda4705402016-09-23 15:08:48 +10001328 # This is a bit tricky, because yum -y assumes missing or failed
1329 # packages are OK (see [1]). We want devstack to stop if we are
1330 # installing missing packages.
1331 #
1332 # Thus we manually match on the output (stack.sh runs in a fixed
1333 # locale, so lang shouldn't change).
1334 #
1335 # If yum returns !0, we echo the result as "YUM_FAILED" and return
1336 # that from the awk (we're subverting -e with this trick).
1337 # Otherwise we use awk to look for failure strings and return "2"
1338 # to indicate a terminal failure.
1339 #
1340 # [1] https://bugzilla.redhat.com/show_bug.cgi?id=965567
1341 parse_yum_result=' \
1342 BEGIN { result=0 } \
1343 /^YUM_FAILED/ { result=$2 } \
1344 /^No package/ { result=2 } \
1345 /^Failed:/ { result=2 } \
1346 //{ print } \
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001347 END { exit result }'
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001348 (sudo_with_proxies "${YUM:-yum}" install -y "$@" 2>&1 || echo YUM_FAILED $?) \
Ian Wienand1b1cc8c2016-03-01 13:14:39 +11001349 | awk "$parse_yum_result" && result=$? || result=$?
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001350
1351 time_stop "yum_install"
1352
Ian Wienand1b1cc8c2016-03-01 13:14:39 +11001353 # if we return 1, then the wrapper functions will run an update
1354 # and try installing the package again as a defense against bad
1355 # mirrors. This can hide failures, especially when we have
1356 # packages that are in the "Failed:" section because their rpm
1357 # install scripts failed to run correctly (in this case, the
1358 # package looks installed, so when the retry happens we just think
1359 # the package is OK, and incorrectly continue on).
1360 if [ "$result" == 2 ]; then
1361 die "Detected fatal package install failure"
1362 fi
1363
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001364 return "$result"
Dean Troyerdff49a22014-01-30 15:37:40 -06001365}
1366
1367# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001368# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001369# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001370function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001371 [[ "$OFFLINE" = "True" ]] && return
1372 local sudo="sudo"
1373 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001374 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1375 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001376 zypper --non-interactive install --auto-agree-with-licenses "$@"
1377}
1378
Sean Dague5edae542017-03-21 20:50:24 -04001379function write_user_unit_file {
1380 local service=$1
1381 local command="$2"
1382 local group=$3
1383 local user=$4
1384 local extra=""
1385 if [[ -n "$group" ]]; then
1386 extra="Group=$group"
1387 fi
1388 local unitfile="$SYSTEMD_DIR/$service"
1389 mkdir -p $SYSTEMD_DIR
1390
1391 iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
1392 iniset -sudo $unitfile "Service" "User" "$user"
1393 iniset -sudo $unitfile "Service" "ExecStart" "$command"
Matthew Treinish477a9622017-08-04 11:09:26 -04001394 iniset -sudo $unitfile "Service" "KillMode" "process"
Michał Dulko3b815a32017-11-14 16:04:51 +01001395 iniset -sudo $unitfile "Service" "TimeoutStopSec" "300"
Dinesh Bhoref60f2b2017-09-05 14:40:32 +05301396 iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
Sean Dague5edae542017-03-21 20:50:24 -04001397 if [[ -n "$group" ]]; then
1398 iniset -sudo $unitfile "Service" "Group" "$group"
1399 fi
1400 iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
1401
1402 # changes to existing units sometimes need a refresh
1403 $SYSTEMCTL daemon-reload
1404}
1405
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001406function write_uwsgi_user_unit_file {
1407 local service=$1
1408 local command="$2"
1409 local group=$3
1410 local user=$4
1411 local unitfile="$SYSTEMD_DIR/$service"
1412 mkdir -p $SYSTEMD_DIR
1413
1414 iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
Sean Daguef41bf2a2017-05-05 07:50:52 -04001415 iniset -sudo $unitfile "Service" "SyslogIdentifier" "$service"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001416 iniset -sudo $unitfile "Service" "User" "$user"
1417 iniset -sudo $unitfile "Service" "ExecStart" "$command"
Dinesh Bhoref60f2b2017-09-05 14:40:32 +05301418 iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001419 iniset -sudo $unitfile "Service" "Type" "notify"
Matthew Treinish477a9622017-08-04 11:09:26 -04001420 iniset -sudo $unitfile "Service" "KillMode" "process"
gong yong sheng07b3bc22017-06-05 14:02:28 +08001421 iniset -sudo $unitfile "Service" "Restart" "always"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001422 iniset -sudo $unitfile "Service" "NotifyAccess" "all"
1423 iniset -sudo $unitfile "Service" "RestartForceExitStatus" "100"
1424
1425 if [[ -n "$group" ]]; then
1426 iniset -sudo $unitfile "Service" "Group" "$group"
1427 fi
1428 iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
1429
1430 # changes to existing units sometimes need a refresh
1431 $SYSTEMCTL daemon-reload
1432}
1433
Sean Dague148d58c2017-05-04 13:08:25 -04001434function _common_systemd_pitfalls {
1435 local cmd=$1
1436 # do some sanity checks on $cmd to see things we don't expect to work
1437
1438 if [[ "$cmd" =~ "sudo" ]]; then
1439 local msg=<<EOF
1440You are trying to use run_process with sudo, this is not going to work under systemd.
1441
1442If you need to run a service as a user other than $STACK_USER call it with:
1443
1444 run_process \$name \$cmd \$group \$user
1445EOF
1446 die $LINENO $msg
1447 fi
1448
1449 if [[ ! "$cmd" =~ ^/ ]]; then
1450 local msg=<<EOF
1451The cmd="$cmd" does not start with an absolute path. It will fail to
1452start under systemd.
1453
1454Please update your run_process stanza to have an absolute path.
1455EOF
1456 die $LINENO $msg
1457 fi
1458
1459}
1460
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001461# Helper function to build a basic unit file and run it under systemd.
1462function _run_under_systemd {
Sean Dague5edae542017-03-21 20:50:24 -04001463 local service=$1
1464 local command="$2"
1465 local cmd=$command
Sean Dague148d58c2017-05-04 13:08:25 -04001466 # sanity check the command
1467 _common_systemd_pitfalls "$cmd"
1468
Sean Dague5edae542017-03-21 20:50:24 -04001469 local systemd_service="devstack@$service.service"
1470 local group=$3
1471 local user=${4:-$STACK_USER}
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001472 if [[ "$command" =~ "uwsgi" ]] ; then
1473 write_uwsgi_user_unit_file $systemd_service "$cmd" "$group" "$user"
1474 else
1475 write_user_unit_file $systemd_service "$cmd" "$group" "$user"
1476 fi
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001477
1478 $SYSTEMCTL enable $systemd_service
1479 $SYSTEMCTL start $systemd_service
Sean Dague5edae542017-03-21 20:50:24 -04001480}
1481
Dean Troyerdff49a22014-01-30 15:37:40 -06001482# Find out if a process exists by partial name.
1483# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001484function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001485 local name=$1
1486 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001487 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001488 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001489 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001490}
1491
Dean Troyer3159a822014-08-27 14:13:58 -05001492# Run a single service under screen or directly
1493# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001494# If an optional group is provided sg will be used to run the
1495# command as that group.
Sean Dague148d58c2017-05-04 13:08:25 -04001496# run_process service "command-line" [group] [user]
Ian Wienandaee18c72014-02-21 15:35:08 +11001497function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001498 local service=$1
1499 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001500 local group=$3
Sean Dague5edae542017-03-21 20:50:24 -04001501 local user=$4
Brant Knudsonedc11c22015-12-14 15:32:05 -06001502
Sean Dague5edae542017-03-21 20:50:24 -04001503 local name=$service
Dean Troyerdff49a22014-01-30 15:37:40 -06001504
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001505 time_start "run_process"
Dean Troyer3159a822014-08-27 14:13:58 -05001506 if is_service_enabled $service; then
Sean Daguecdba1b32017-08-30 11:11:06 -04001507 _run_under_systemd "$name" "$command" "$group" "$user"
Dean Troyer3159a822014-08-27 14:13:58 -05001508 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001509 time_stop "run_process"
Dean Troyerdff49a22014-01-30 15:37:40 -06001510}
1511
Dean Troyer3159a822014-08-27 14:13:58 -05001512# Stop a service process
1513# If a PID is available use it, kill the whole process group via TERM
1514# If screen is being used kill the screen window; this will catch processes
1515# that did not leave a PID behind
Radoslav Gerganov2b97a812017-10-10 16:51:12 +03001516# Uses globals ``SERVICE_DIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001517# stop_process service
1518function stop_process {
1519 local service=$1
1520
1521 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer3159a822014-08-27 14:13:58 -05001522
1523 if is_service_enabled $service; then
Sean Dague803acff2017-05-01 10:52:38 -04001524 # Only do this for units which appear enabled, this also
1525 # catches units that don't really exist for cases like
1526 # keystone without a failure.
1527 if $SYSTEMCTL is-enabled devstack@$service.service; then
1528 $SYSTEMCTL stop devstack@$service.service
1529 $SYSTEMCTL disable devstack@$service.service
Sean Dague5edae542017-03-21 20:50:24 -04001530 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001531 fi
1532}
1533
Sean Daguecdba1b32017-08-30 11:11:06 -04001534# use systemctl to check service status
Ian Wienandaee18c72014-02-21 15:35:08 +11001535function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001536 local service
Sean Daguecdba1b32017-08-30 11:11:06 -04001537 for service in ${ENABLED_SERVICES//,/ }; do
1538 # because some things got renamed like key => keystone
1539 if $SYSTEMCTL is-enabled devstack@$service.service; then
1540 # no-pager is needed because otherwise status dumps to a
1541 # pager when in interactive mode, which will stop a manual
1542 # devstack run.
1543 $SYSTEMCTL status devstack@$service.service --no-pager
Dean Troyer3159a822014-08-27 14:13:58 -05001544 fi
Sean Daguecdba1b32017-08-30 11:11:06 -04001545 done
Dean Troyer3159a822014-08-27 14:13:58 -05001546}
1547
Dean Troyer3159a822014-08-27 14:13:58 -05001548
Sean Daguecdba1b32017-08-30 11:11:06 -04001549function tail_log {
1550 deprecated "With the removal of screen support, tail_log is deprecated and will be removed after Queens"
1551}
Dean Troyer3159a822014-08-27 14:13:58 -05001552
Sean Dague2c65e712014-12-18 09:44:56 -05001553# Plugin Functions
1554# =================
1555
1556DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1557
1558# enable_plugin <name> <url> [branch]
1559#
1560# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1561# ``url`` is a git url
1562# ``branch`` is a gitref. If it's not set, defaults to master
1563function enable_plugin {
1564 local name=$1
1565 local url=$2
1566 local branch=${3:-master}
Omer Anson51584862017-08-24 17:47:37 +03001567 if is_plugin_enabled $name; then
John L. Villalovos21d84c22016-11-11 15:26:11 -08001568 die $LINENO "Plugin attempted to be enabled twice: ${name} ${url} ${branch}"
1569 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001570 DEVSTACK_PLUGINS+=",$name"
1571 GITREPO[$name]=$url
1572 GITDIR[$name]=$DEST/$name
1573 GITBRANCH[$name]=$branch
1574}
1575
Omer Anson51584862017-08-24 17:47:37 +03001576# is_plugin_enabled <name>
1577#
1578# Check if the plugin was enabled, e.g. using enable_plugin
1579#
1580# ``name`` The name with which the plugin was enabled
1581function is_plugin_enabled {
1582 local name=$1
1583 if [[ ",${DEVSTACK_PLUGINS}," =~ ",${name}," ]]; then
1584 return 0
1585 fi
1586 return 1
1587}
1588
Sean Dague2c65e712014-12-18 09:44:56 -05001589# fetch_plugins
1590#
1591# clones all plugins
1592function fetch_plugins {
1593 local plugins="${DEVSTACK_PLUGINS}"
1594 local plugin
1595
1596 # short circuit if nothing to do
1597 if [[ -z $plugins ]]; then
1598 return
1599 fi
1600
Dean Troyerdc97cb72015-03-28 08:20:50 -05001601 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001602 for plugin in ${plugins//,/ }; do
1603 git_clone_by_name $plugin
1604 done
1605}
1606
1607# load_plugin_settings
1608#
1609# Load settings from plugins in the order that they were registered
1610function load_plugin_settings {
1611 local plugins="${DEVSTACK_PLUGINS}"
1612 local plugin
1613
1614 # short circuit if nothing to do
1615 if [[ -z $plugins ]]; then
1616 return
1617 fi
1618
1619 echo "Loading plugin settings"
1620 for plugin in ${plugins//,/ }; do
1621 local dir=${GITDIR[$plugin]}
1622 # source any known settings
1623 if [[ -f $dir/devstack/settings ]]; then
1624 source $dir/devstack/settings
1625 fi
1626 done
1627}
1628
Sean Dague6e275e12015-03-26 05:54:28 -04001629# plugin_override_defaults
1630#
1631# Run an extremely early setting phase for plugins that allows default
1632# overriding of services.
1633function plugin_override_defaults {
1634 local plugins="${DEVSTACK_PLUGINS}"
1635 local plugin
1636
1637 # short circuit if nothing to do
1638 if [[ -z $plugins ]]; then
1639 return
1640 fi
1641
1642 echo "Overriding Configuration Defaults"
1643 for plugin in ${plugins//,/ }; do
1644 local dir=${GITDIR[$plugin]}
1645 # source any overrides
1646 if [[ -f $dir/devstack/override-defaults ]]; then
1647 # be really verbose that an override is happening, as it
1648 # may not be obvious if things fail later.
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001649 echo "$plugin has overridden the following defaults"
Sean Dague6e275e12015-03-26 05:54:28 -04001650 cat $dir/devstack/override-defaults
1651 source $dir/devstack/override-defaults
1652 fi
1653 done
1654}
1655
Sean Dague2c65e712014-12-18 09:44:56 -05001656# run_plugins
1657#
1658# Run the devstack/plugin.sh in all the plugin directories. These are
1659# run in registration order.
1660function run_plugins {
1661 local mode=$1
1662 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301663
1664 local plugins="${DEVSTACK_PLUGINS}"
1665 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001666 for plugin in ${plugins//,/ }; do
1667 local dir=${GITDIR[$plugin]}
1668 if [[ -f $dir/devstack/plugin.sh ]]; then
1669 source $dir/devstack/plugin.sh $mode $phase
1670 fi
1671 done
1672}
1673
1674function run_phase {
1675 local mode=$1
1676 local phase=$2
1677 if [[ -d $TOP_DIR/extras.d ]]; then
Dmitry Tantsur64be3212015-10-12 13:10:24 +02001678 local extra_plugin_file_name
1679 for extra_plugin_file_name in $TOP_DIR/extras.d/*.sh; do
Sean Dague41d01102015-12-03 08:12:23 -05001680 # NOTE(sdague): only process extras.d for the 3 explicitly
1681 # white listed elements in tree. We want these to move out
1682 # over time as well, but they are in tree, so we need to
1683 # manage that.
Matt Riedemannc9f63272016-08-30 17:21:30 -04001684 local exceptions="80-tempest.sh"
Ian Wienand5cdee8d2015-10-19 14:17:18 +11001685 local extra
1686 extra=$(basename $extra_plugin_file_name)
Sean Dague1de9e332015-10-07 08:46:13 -04001687 if [[ ! ( $exceptions =~ "$extra" ) ]]; then
Sean Dague41d01102015-12-03 08:12:23 -05001688 warn "use of extras.d is no longer supported"
1689 warn "processing of project $extra is skipped"
1690 else
1691 [[ -r $extra_plugin_file_name ]] && source $extra_plugin_file_name $mode $phase
Sean Dague1de9e332015-10-07 08:46:13 -04001692 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001693 done
1694 fi
1695 # the source phase corresponds to settings loading in plugins
1696 if [[ "$mode" == "source" ]]; then
1697 load_plugin_settings
Chris Dentc6d47012015-10-09 14:57:05 +00001698 verify_disabled_services
Sean Dague6e275e12015-03-26 05:54:28 -04001699 elif [[ "$mode" == "override_defaults" ]]; then
1700 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001701 else
1702 run_plugins $mode $phase
1703 fi
1704}
1705
James E. Blairc5853ac2017-11-21 09:44:42 -08001706# define_plugin <name>
1707#
1708# This function is a no-op. It allows a plugin to define its name So
1709# that other plugins may reference it by name. It should generally be
1710# the last component of the canonical git repo name. E.g.,
1711# openstack/devstack-foo should use "devstack-foo" as the name here.
1712#
1713# This function is currently a noop, but the value may still be used
1714# by external tools (as in plugin_requires) and may be used by
1715# devstack in the future.
1716#
1717# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1718function define_plugin {
1719 :
1720}
1721
1722# plugin_requires <name> <other>
1723#
1724# This function is a no-op. It is currently used by external tools
1725# (such as the devstack module for Ansible) to automatically generate
1726# local.conf files. It is not currently used by devstack itself to
1727# resolve dependencies.
1728#
1729# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1730# ``other`` is the name of another plugin
1731function plugin_requires {
1732 :
1733}
1734
Dean Troyerdff49a22014-01-30 15:37:40 -06001735
1736# Service Functions
1737# =================
1738
1739# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1740# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001741function _cleanup_service_list {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001742 local xtrace
1743 xtrace=$(set +o | grep xtrace)
1744 set +o xtrace
1745
Dean Troyerdff49a22014-01-30 15:37:40 -06001746 echo "$1" | sed -e '
1747 s/,,/,/g;
1748 s/^,//;
1749 s/,$//
1750 '
Ian Wienand8043bfa2015-10-14 14:53:18 +11001751
1752 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001753}
1754
1755# disable_all_services() removes all current services
1756# from ``ENABLED_SERVICES`` to reset the configuration
1757# before a minimal installation
1758# Uses global ``ENABLED_SERVICES``
1759# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001760function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001761 ENABLED_SERVICES=""
1762}
1763
1764# Remove all services starting with '-'. For example, to install all default
1765# services except rabbit (rabbit) set in ``localrc``:
1766# ENABLED_SERVICES+=",-rabbit"
1767# Uses global ``ENABLED_SERVICES``
1768# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001769function disable_negated_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001770 local xtrace
1771 xtrace=$(set +o | grep xtrace)
1772 set +o xtrace
1773
Ian Wienand2796a822015-04-15 08:59:04 +10001774 local to_remove=""
1775 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001776 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001777
1778 # build up list of services that should be removed; i.e. they
1779 # begin with "-"
1780 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001781 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001782 to_remove+=",${service#-}"
1783 else
1784 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001785 fi
1786 done
Ian Wienand2796a822015-04-15 08:59:04 +10001787
1788 # go through the service list. if this service appears in the "to
1789 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001790 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001791
1792 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001793}
1794
Chris Dentc6d47012015-10-09 14:57:05 +00001795# disable_service() prepares the services passed as argument to be
1796# removed from the ``ENABLED_SERVICES`` list, if they are present.
Dean Troyerdff49a22014-01-30 15:37:40 -06001797#
1798# For example:
1799# disable_service rabbit
1800#
Chris Dentc6d47012015-10-09 14:57:05 +00001801# Uses global ``DISABLED_SERVICES``
Dean Troyerdff49a22014-01-30 15:37:40 -06001802# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001803function disable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001804 local xtrace
1805 xtrace=$(set +o | grep xtrace)
1806 set +o xtrace
1807
Chris Dentc6d47012015-10-09 14:57:05 +00001808 local disabled_svcs="${DISABLED_SERVICES}"
1809 local enabled_svcs=",${ENABLED_SERVICES},"
Dean Troyerdff49a22014-01-30 15:37:40 -06001810 local service
1811 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001812 disabled_svcs+=",$service"
Dean Troyerdff49a22014-01-30 15:37:40 -06001813 if is_service_enabled $service; then
Chris Dentc6d47012015-10-09 14:57:05 +00001814 enabled_svcs=${enabled_svcs//,$service,/,}
Dean Troyerdff49a22014-01-30 15:37:40 -06001815 fi
1816 done
Chris Dentc6d47012015-10-09 14:57:05 +00001817 DISABLED_SERVICES=$(_cleanup_service_list "$disabled_svcs")
1818 ENABLED_SERVICES=$(_cleanup_service_list "$enabled_svcs")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001819
1820 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001821}
1822
1823# enable_service() adds the services passed as argument to the
1824# ``ENABLED_SERVICES`` list, if they are not already present.
1825#
1826# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001827# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001828#
1829# This function does not know about the special cases
1830# for nova, glance, and neutron built into is_service_enabled().
1831# Uses global ``ENABLED_SERVICES``
1832# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001833function enable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001834 local xtrace
1835 xtrace=$(set +o | grep xtrace)
1836 set +o xtrace
1837
Dean Troyerdff49a22014-01-30 15:37:40 -06001838 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001839 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001840 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001841 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
1842 warn $LINENO "Attempt to enable_service ${service} when it has been disabled"
1843 continue
1844 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001845 if ! is_service_enabled $service; then
1846 tmpsvcs+=",$service"
1847 fi
1848 done
1849 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1850 disable_negated_services
Ian Wienand8043bfa2015-10-14 14:53:18 +11001851
1852 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001853}
1854
1855# is_service_enabled() checks if the service(s) specified as arguments are
1856# enabled by the user in ``ENABLED_SERVICES``.
1857#
1858# Multiple services specified as arguments are ``OR``'ed together; the test
1859# is a short-circuit boolean, i.e it returns on the first match.
1860#
1861# There are special cases for some 'catch-all' services::
1862# **nova** returns true if any service enabled start with **n-**
1863# **cinder** returns true if any service enabled start with **c-**
Dean Troyerdff49a22014-01-30 15:37:40 -06001864# **glance** returns true if any service enabled start with **g-**
1865# **neutron** returns true if any service enabled start with **q-**
1866# **swift** returns true if any service enabled start with **s-**
1867# **trove** returns true if any service enabled start with **tr-**
1868# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1869# **s-** services will be enabled. This will be deprecated in the future.
1870#
1871# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1872# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1873# as enabled in this case.
1874#
1875# Uses global ``ENABLED_SERVICES``
1876# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001877function is_service_enabled {
Ian Wienand433a9b12015-10-07 13:29:31 +11001878 local xtrace
1879 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001880 set +o xtrace
Ian Wienand8043bfa2015-10-14 14:53:18 +11001881
Sean Dague45917cc2014-02-24 16:09:14 -05001882 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001883 local services=$@
1884 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001885 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001886 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001887
1888 # Look for top-level 'enabled' function for this service
1889 if type is_${service}_enabled >/dev/null 2>&1; then
1890 # A function exists for this service, use it
Christian Schwede3db0aad2015-09-10 11:15:39 +00001891 is_${service}_enabled && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001892 fi
1893
1894 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1895 # are implemented
1896
John Kasperski7b3ae532016-04-24 22:56:04 -05001897 [[ ${service} == n-cell-* && ,${ENABLED_SERVICES} =~ ,"n-cell" ]] && enabled=0
1898 [[ ${service} == n-cpu-* && ,${ENABLED_SERVICES} =~ ,"n-cpu" ]] && enabled=0
1899 [[ ${service} == "nova" && ,${ENABLED_SERVICES} =~ ,"n-" ]] && enabled=0
1900 [[ ${service} == "glance" && ,${ENABLED_SERVICES} =~ ,"g-" ]] && enabled=0
1901 [[ ${service} == "neutron" && ,${ENABLED_SERVICES} =~ ,"q-" ]] && enabled=0
1902 [[ ${service} == "trove" && ,${ENABLED_SERVICES} =~ ,"tr-" ]] && enabled=0
1903 [[ ${service} == "swift" && ,${ENABLED_SERVICES} =~ ,"s-" ]] && enabled=0
1904 [[ ${service} == s-* && ,${ENABLED_SERVICES} =~ ,"swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001905 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11001906
Sean Dague45917cc2014-02-24 16:09:14 -05001907 $xtrace
1908 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001909}
1910
fumihiko kakuma8606c982015-04-13 09:55:06 +09001911# remove specified list from the input string
1912# remove_disabled_services service-list remove-list
1913function remove_disabled_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001914 local xtrace
1915 xtrace=$(set +o | grep xtrace)
1916 set +o xtrace
1917
fumihiko kakuma8606c982015-04-13 09:55:06 +09001918 local service_list=$1
1919 local remove_list=$2
1920 local service
1921 local enabled=""
1922
1923 for service in ${service_list//,/ }; do
1924 local remove
1925 local add=1
1926 for remove in ${remove_list//,/ }; do
1927 if [[ ${remove} == ${service} ]]; then
1928 add=0
1929 break
1930 fi
1931 done
1932 if [[ $add == 1 ]]; then
1933 enabled="${enabled},$service"
1934 fi
1935 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11001936
1937 $xtrace
1938
fumihiko kakuma8606c982015-04-13 09:55:06 +09001939 _cleanup_service_list "$enabled"
1940}
1941
Dean Troyerdff49a22014-01-30 15:37:40 -06001942# Toggle enable/disable_service for services that must run exclusive of each other
1943# $1 The name of a variable containing a space-separated list of services
1944# $2 The name of a variable in which to store the enabled service's name
1945# $3 The name of the service to enable
1946function use_exclusive_service {
1947 local options=${!1}
1948 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001949 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001950 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001951 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001952 for opt in $options;do
1953 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1954 done
1955 eval "$out=$selection"
1956 return 0
1957}
1958
Chris Dentc6d47012015-10-09 14:57:05 +00001959# Make sure that nothing has manipulated ENABLED_SERVICES in a way
1960# that conflicts with prior calls to disable_service.
1961# Uses global ``ENABLED_SERVICES``
1962function verify_disabled_services {
1963 local service
1964 for service in ${ENABLED_SERVICES//,/ }; do
1965 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
1966 die $LINENO "ENABLED_SERVICES directly modified to overcome 'disable_service ${service}'"
1967 fi
1968 done
1969}
1970
Dean Troyerdff49a22014-01-30 15:37:40 -06001971
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001972# System Functions
1973# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001974
1975# Only run the command if the target file (the last arg) is not on an
1976# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001977function _safe_permission_operation {
Ian Wienand433a9b12015-10-07 13:29:31 +11001978 local xtrace
1979 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001980 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001981 local args=( $@ )
1982 local last
1983 local sudo_cmd
1984 local dir_to_check
1985
1986 let last="${#args[*]} - 1"
1987
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001988 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001989 if [ ! -d "$dir_to_check" ]; then
1990 dir_to_check=`dirname "$dir_to_check"`
1991 fi
1992
1993 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001994 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001995 return 0
1996 fi
1997
1998 if [[ $TRACK_DEPENDS = True ]]; then
1999 sudo_cmd="env"
2000 else
2001 sudo_cmd="sudo"
2002 fi
2003
Sean Dague45917cc2014-02-24 16:09:14 -05002004 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002005 $sudo_cmd $@
2006}
2007
2008# Exit 0 if address is in network or 1 if address is not in network
2009# ip-range is in CIDR notation: 1.2.3.4/20
2010# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002011function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002012 local ip=$1
2013 local range=$2
2014 local masklen=${range#*/}
Ian Wienandada886d2015-10-07 14:06:26 +11002015 local network
2016 network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2017 local subnet
2018 subnet=$(maskip $ip $(cidr2netmask $masklen))
Dean Troyerdff49a22014-01-30 15:37:40 -06002019 [[ $network == $subnet ]]
2020}
2021
2022# Add a user to a group.
2023# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002024function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002025 local user=$1
2026 local group=$2
2027
Thomas Bechtolda8580852015-05-31 00:04:33 +02002028 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06002029}
2030
2031# Convert CIDR notation to a IPv4 netmask
2032# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002033function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002034 local maskpat="255 255 255 255"
2035 local maskdgt="254 252 248 240 224 192 128"
2036 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2037 echo ${1-0}.${2-0}.${3-0}.${4-0}
2038}
2039
Ian Cordasco69e3c0a2016-09-26 12:53:14 -05002040# Check if this is a valid ipv4 address string
2041function is_ipv4_address {
2042 local address=$1
Jens Harbott7617ac22017-09-19 17:43:48 +00002043 local regex='([0-9]{1,3}\.){3}[0-9]{1,3}'
Ian Cordasco69e3c0a2016-09-26 12:53:14 -05002044 # TODO(clarkb) make this more robust
2045 if [[ "$address" =~ $regex ]] ; then
2046 return 0
2047 else
2048 return 1
2049 fi
2050}
2051
Dean Troyerdff49a22014-01-30 15:37:40 -06002052# Gracefully cp only if source file/dir exists
2053# cp_it source destination
2054function cp_it {
2055 if [ -e $1 ] || [ -d $1 ]; then
2056 cp -pRL $1 $2
2057 fi
2058}
2059
2060# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2061# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2062# ``localrc`` or on the command line if necessary::
2063#
2064# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2065#
2066# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2067
Ian Wienandaee18c72014-02-21 15:35:08 +11002068function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05002069 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002070 export http_proxy=$http_proxy
2071 fi
Sean Dague53753292014-12-04 19:38:15 -05002072 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002073 export https_proxy=$https_proxy
2074 fi
Sean Dague53753292014-12-04 19:38:15 -05002075 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002076 export no_proxy=$no_proxy
2077 fi
2078}
2079
2080# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002081function is_nfs_directory {
Ian Wienandada886d2015-10-07 14:06:26 +11002082 local mount_type
2083 mount_type=`stat -f -L -c %T $1`
Dean Troyerdff49a22014-01-30 15:37:40 -06002084 test "$mount_type" == "nfs"
2085}
2086
2087# Return the network portion of the given IP address using netmask
2088# netmask is in the traditional dotted-quad format
2089# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002090function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002091 local ip=$1
2092 local mask=$2
2093 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
Ian Wienandada886d2015-10-07 14:06:26 +11002094 local subnet
2095 subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
Dean Troyerdff49a22014-01-30 15:37:40 -06002096 echo $subnet
2097}
2098
Michael Turek7938d832016-04-12 14:55:21 -04002099function is_provider_network {
2100 if [ "$Q_USE_PROVIDER_NETWORKING" == "True" ]; then
2101 return 0
2102 fi
2103 return 1
2104}
2105
2106
Ian Wienand6d213df2017-08-22 16:05:16 +10002107# Return just the <major>.<minor> for the given python interpreter
2108function _get_python_version {
2109 local interp=$1
2110 local version
Sean Dague4324f4e2017-09-14 12:59:25 -06002111 # disable erroring out here, otherwise if python 3 doesn't exist we fail hard.
Javier Pena6bd49242017-09-15 15:55:00 +02002112 if [[ -x $(which $interp 2> /dev/null) ]]; then
Sean Dague4324f4e2017-09-14 12:59:25 -06002113 version=$($interp -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
2114 fi
Ian Wienand6d213df2017-08-22 16:05:16 +10002115 echo ${version}
2116}
2117
Chris Dent3a2c86a2015-05-12 13:41:25 +00002118# Return the current python as "python<major>.<minor>"
2119function python_version {
Ian Wienandada886d2015-10-07 14:06:26 +11002120 local python_version
Ian Wienand6d213df2017-08-22 16:05:16 +10002121 python_version=$(_get_python_version python2)
Chris Dent3a2c86a2015-05-12 13:41:25 +00002122 echo "python${python_version}"
2123}
2124
Ian Wienand6d213df2017-08-22 16:05:16 +10002125function python3_version {
2126 local python3_version
2127 python3_version=$(_get_python_version python3)
2128 echo "python${python_version}"
2129}
2130
2131
Dean Troyerdff49a22014-01-30 15:37:40 -06002132# Service wrapper to restart services
2133# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002134function restart_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002135 if [ -x /bin/systemctl ]; then
2136 sudo /bin/systemctl restart $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002137 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002138 sudo service $1 restart
Dean Troyerdff49a22014-01-30 15:37:40 -06002139 fi
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002140
Dean Troyerdff49a22014-01-30 15:37:40 -06002141}
2142
2143# Only change permissions of a file or directory if it is not on an
2144# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002145function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002146 _safe_permission_operation chmod $@
2147}
2148
2149# Only change ownership of a file or directory if it is not on an NFS
2150# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002151function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002152 _safe_permission_operation chown $@
2153}
2154
2155# Service wrapper to start services
2156# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002157function start_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002158 if [ -x /bin/systemctl ]; then
2159 sudo /bin/systemctl start $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002160 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002161 sudo service $1 start
Dean Troyerdff49a22014-01-30 15:37:40 -06002162 fi
2163}
2164
2165# Service wrapper to stop services
2166# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002167function stop_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002168 if [ -x /bin/systemctl ]; then
2169 sudo /bin/systemctl stop $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002170 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002171 sudo service $1 stop
Dean Troyerdff49a22014-01-30 15:37:40 -06002172 fi
2173}
2174
Attila Fazekasbcaadd62016-11-23 12:43:02 +01002175# Service wrapper to reload services
2176# If the service was not in running state it will start it
Gregory Haynes4b49e402016-08-31 18:19:51 -07002177# reload_service service-name
2178function reload_service {
2179 if [ -x /bin/systemctl ]; then
Attila Fazekasbcaadd62016-11-23 12:43:02 +01002180 sudo /bin/systemctl reload-or-restart $1
Gregory Haynes4b49e402016-08-31 18:19:51 -07002181 else
2182 sudo service $1 reload
2183 fi
2184}
2185
Sean Dague442e4e92015-06-24 13:24:02 -04002186# Test with a finite retry loop.
2187#
2188function test_with_retry {
2189 local testcmd=$1
2190 local failmsg=$2
2191 local until=${3:-10}
2192 local sleep=${4:-0.5}
2193
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002194 time_start "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002195 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2196 die $LINENO "$failmsg"
2197 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002198 time_stop "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002199}
2200
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00002201# Like sudo but forwarding http_proxy https_proxy no_proxy environment vars.
2202# If it is run as superuser then sudo is replaced by env.
2203#
2204function sudo_with_proxies {
2205 local sudo
2206
2207 [[ "$(id -u)" = "0" ]] && sudo="env" || sudo="sudo"
2208
2209 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}"\
2210 no_proxy="${no_proxy:-}" "$@"
2211}
2212
Sean Dague95c33d52015-10-07 11:05:59 -04002213# Timing infrastructure - figure out where large blocks of time are
2214# used in DevStack
2215#
2216# The timing infrastructure for DevStack is about collecting buckets
2217# of time that are spend in some subtask. For instance, that might be
2218# 'apt', 'pip', 'osc', even database migrations. We do this by a pair
2219# of functions: time_start / time_stop.
2220#
2221# These take a single parameter: $name - which specifies the name of
2222# the bucket to be accounted against. time_totals function spits out
2223# the results.
2224#
2225# Resolution is only in whole seconds, so should be used for long
2226# running activities.
2227
Sean Dagueafef8bf2017-03-06 14:07:23 -05002228declare -A -g _TIME_TOTAL
2229declare -A -g _TIME_START
2230declare -r -g _TIME_BEGIN=$(date +%s)
Sean Dague95c33d52015-10-07 11:05:59 -04002231
2232# time_start $name
2233#
2234# starts the clock for a timer by name. Errors if that clock is
2235# already started.
2236function time_start {
2237 local name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002238 local start_time=${_TIME_START[$name]}
Sean Dague95c33d52015-10-07 11:05:59 -04002239 if [[ -n "$start_time" ]]; then
2240 die $LINENO "Trying to start the clock on $name, but it's already been started"
2241 fi
Sean Dague85cf2932017-03-27 15:35:13 -04002242 _TIME_START[$name]=$(date +%s%3N)
Sean Dague95c33d52015-10-07 11:05:59 -04002243}
2244
2245# time_stop $name
2246#
2247# stops the clock for a timer by name, and accumulate that time in the
2248# global counter for that name. Errors if that clock had not
2249# previously been started.
2250function time_stop {
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002251 local name
2252 local end_time
Rob Crittenden38245da2016-05-26 17:55:14 -04002253 local elapsed_time
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002254 local total
2255 local start_time
2256
2257 name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002258 start_time=${_TIME_START[$name]}
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002259
Sean Dague95c33d52015-10-07 11:05:59 -04002260 if [[ -z "$start_time" ]]; then
2261 die $LINENO "Trying to stop the clock on $name, but it was never started"
2262 fi
Sean Dague85cf2932017-03-27 15:35:13 -04002263 end_time=$(date +%s%3N)
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002264 elapsed_time=$(($end_time - $start_time))
Ian Wienand908a3a92016-03-29 14:47:09 +11002265 total=${_TIME_TOTAL[$name]:-0}
Sean Dague95c33d52015-10-07 11:05:59 -04002266 # reset the clock so we can start it in the future
Ian Wienand908a3a92016-03-29 14:47:09 +11002267 _TIME_START[$name]=""
2268 _TIME_TOTAL[$name]=$(($total + $elapsed_time))
Sean Dague95c33d52015-10-07 11:05:59 -04002269}
2270
Sean Dague85cf2932017-03-27 15:35:13 -04002271function oscwrap {
2272 local out
2273 local rc
2274 local start
2275 local end
2276 # Cannot use timer_start and timer_stop as we run in subshells
2277 # and those rely on modifying vars in the same process (which cannot
2278 # happen from a subshell.
2279 start=$(date +%s%3N)
2280 out=$(command openstack "$@")
2281 rc=$?
2282 end=$(date +%s%3N)
2283 echo $((end - start)) >> $OSCWRAP_TIMER_FILE
2284
2285 echo "$out"
2286 return $rc
2287}
2288
2289function install_oscwrap {
2290 # File to accumulate our timing data
2291 OSCWRAP_TIMER_FILE=$(mktemp)
2292 # Bash by default doesn't expand aliases, allow it for the aliases
2293 # we want to whitelist.
2294 shopt -s expand_aliases
2295 # Remove all aliases that might be expanded to preserve old unexpanded
2296 # behavior
2297 unalias -a
2298 # Add only the alias we want for openstack
2299 alias openstack=oscwrap
2300}
2301
2302function cleanup_oscwrap {
2303 local total=0
2304 if python3_enabled ; then
2305 local python=python3
2306 else
2307 local python=python
2308 fi
2309 total=$(cat $OSCWRAP_TIMER_FILE | $python -c "import sys; print(sum(int(l) for l in sys.stdin))")
2310 _TIME_TOTAL["osc"]=$total
2311 rm $OSCWRAP_TIMER_FILE
2312}
2313
Sean Dague95c33d52015-10-07 11:05:59 -04002314# time_totals
Ian Wienand908a3a92016-03-29 14:47:09 +11002315# Print out total time summary
Sean Dague95c33d52015-10-07 11:05:59 -04002316function time_totals {
Ian Wienand908a3a92016-03-29 14:47:09 +11002317 local elapsed_time
2318 local end_time
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002319 local len=20
Ian Wienand908a3a92016-03-29 14:47:09 +11002320 local xtrace
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002321 local unaccounted_time
Ian Wienand908a3a92016-03-29 14:47:09 +11002322
2323 end_time=$(date +%s)
2324 elapsed_time=$(($end_time - $_TIME_BEGIN))
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002325 unaccounted_time=$elapsed_time
Ian Wienand908a3a92016-03-29 14:47:09 +11002326
2327 # pad 1st column this far
2328 for t in ${!_TIME_TOTAL[*]}; do
2329 if [[ ${#t} -gt $len ]]; then
2330 len=${#t}
2331 fi
Sean Dague95c33d52015-10-07 11:05:59 -04002332 done
Ian Wienand908a3a92016-03-29 14:47:09 +11002333
Sean Dague85cf2932017-03-27 15:35:13 -04002334 cleanup_oscwrap
2335
Ian Wienand908a3a92016-03-29 14:47:09 +11002336 xtrace=$(set +o | grep xtrace)
2337 set +o xtrace
2338
2339 echo
2340 echo "========================="
2341 echo "DevStack Component Timing"
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002342 echo " (times are in seconds) "
Ian Wienand908a3a92016-03-29 14:47:09 +11002343 echo "========================="
Ian Wienand908a3a92016-03-29 14:47:09 +11002344 for t in ${!_TIME_TOTAL[*]}; do
2345 local v=${_TIME_TOTAL[$t]}
Sean Dague85cf2932017-03-27 15:35:13 -04002346 # because we're recording in milliseconds
2347 v=$(($v / 1000))
Ian Wienand908a3a92016-03-29 14:47:09 +11002348 printf "%-${len}s %3d\n" "$t" "$v"
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002349 unaccounted_time=$(($unaccounted_time - $v))
Ian Wienand908a3a92016-03-29 14:47:09 +11002350 done
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002351 echo "-------------------------"
2352 printf "%-${len}s %3d\n" "Unaccounted time" "$unaccounted_time"
Ian Wienand908a3a92016-03-29 14:47:09 +11002353 echo "========================="
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002354 printf "%-${len}s %3d\n" "Total runtime" "$elapsed_time"
Ian Wienand908a3a92016-03-29 14:47:09 +11002355
2356 $xtrace
Sean Dague95c33d52015-10-07 11:05:59 -04002357}
Dean Troyerdff49a22014-01-30 15:37:40 -06002358
2359# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +11002360$_XTRACE_FUNCTIONS_COMMON
Dean Troyerdff49a22014-01-30 15:37:40 -06002361
2362# Local variables:
2363# mode: shell-script
2364# End: