blob: d83cd4c3bbcf0095ddfbee74ab0e6ae0c93b4f49 [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"
Dirk Mueller4404f682018-03-02 00:37:58 +0100376 # Tumbleweed uses "n/a" as a codename, and the release is a datestring
377 # like 20180218, so not very useful.
378 [ "$os_CODENAME" = "n/a" ] && DISTRO="opensuse-tumbleweed"
Dean Troyerdff49a22014-01-30 15:37:40 -0600379 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000380 # just use major release
381 DISTRO="sle${os_RELEASE%.*}"
382 elif [[ "$os_VENDOR" =~ (Red.*Hat) || \
anju Tiwari6c639c92014-07-15 18:11:54 +0530383 "$os_VENDOR" =~ (CentOS) || \
Mike Trimm1da4e792016-04-27 11:40:19 -0500384 "$os_VENDOR" =~ (Scientific) || \
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300385 "$os_VENDOR" =~ (OracleServer) || \
Evgeny Antyshev718512c2016-03-03 14:47:58 +0000386 "$os_VENDOR" =~ (Virtuozzo) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600387 # Drop the . release as we assume it's compatible
Ian Wienand7710e7f2014-08-27 16:15:32 +1000388 # XXX re-evaluate when we get RHEL10
Dean Troyerdff49a22014-01-30 15:37:40 -0600389 DISTRO="rhel${os_RELEASE::1}"
390 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
Ian Wienand7710e7f2014-08-27 16:15:32 +1000391 DISTRO="xs${os_RELEASE%.*}"
Dean Troyerdff49a22014-01-30 15:37:40 -0600392 else
Ian Wienanded92e432016-02-16 10:56:56 +1100393 # We can't make a good choice here. Setting a sensible DISTRO
394 # is part of the problem, but not the major issue -- we really
395 # only use DISTRO in the code as a fine-filter.
396 #
397 # The bigger problem is categorising the system into one of
398 # our two big categories as Ubuntu/Debian-ish or
399 # Fedora/CentOS-ish.
400 #
401 # The setting of os_PACKAGE above is only set to "deb" based
402 # on a hard-coded list of vendor names ... thus we will
403 # default to thinking unknown distros are RPM based
404 # (ie. is_ubuntu does not match). But the platform will then
405 # also not match in is_fedora, because that also has a list of
406 # names.
407 #
408 # So, if you are reading this, getting your distro supported
409 # is really about making sure it matches correctly in these
410 # functions. Then you can choose a sensible way to construct
411 # DISTRO based on your distros release approach.
412 die $LINENO "Unable to determine DISTRO, can not continue."
Dean Troyerdff49a22014-01-30 15:37:40 -0600413 fi
Ian Wienand7710e7f2014-08-27 16:15:32 +1000414 typeset -xr DISTRO
Dean Troyerdff49a22014-01-30 15:37:40 -0600415}
416
417# Utility function for checking machine architecture
418# is_arch arch-type
419function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500420 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600421}
422
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700423# Determine if current distribution is an Oracle distribution
424# is_oraclelinux
425function is_oraclelinux {
426 if [[ -z "$os_VENDOR" ]]; then
427 GetOSVersion
428 fi
429
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300430 [ "$os_VENDOR" = "OracleServer" ]
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700431}
432
433
Dean Troyerdff49a22014-01-30 15:37:40 -0600434# Determine if current distribution is a Fedora-based distribution
435# (Fedora, RHEL, CentOS, etc).
436# is_fedora
437function is_fedora {
438 if [[ -z "$os_VENDOR" ]]; then
439 GetOSVersion
440 fi
441
anju Tiwari6c639c92014-07-15 18:11:54 +0530442 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
tengqm78d37392016-03-15 23:08:00 -0400443 [ "$os_VENDOR" = "RedHatEnterpriseServer" ] || \
Fernando Ribeiro4e6f8ca2016-04-12 23:34:03 -0300444 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ] || \
Markus Zoellerb8335ee2017-09-26 08:32:50 +0200445 [ "$os_VENDOR" = "Virtuozzo" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600446}
447
448
449# Determine if current distribution is a SUSE-based distribution
450# (openSUSE, SLE).
451# is_suse
452function is_suse {
453 if [[ -z "$os_VENDOR" ]]; then
454 GetOSVersion
455 fi
456
Ralf Haferkamp37dddb72016-03-02 15:25:57 +0100457 [[ "$os_VENDOR" =~ (openSUSE) || "$os_VENDOR" == "SUSE LINUX" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600458}
459
460
461# Determine if current distribution is an Ubuntu-based distribution
462# It will also detect non-Ubuntu but Debian-based distros
463# is_ubuntu
464function is_ubuntu {
465 if [[ -z "$os_PACKAGE" ]]; then
466 GetOSVersion
467 fi
468 [ "$os_PACKAGE" = "deb" ]
469}
470
471
472# Git Functions
473# =============
474
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600475# Returns openstack release name for a given branch name
476# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100477function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600478 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700479 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600480 echo ${branch#*/}
481 else
482 echo "master"
483 fi
484}
485
Dean Troyerdff49a22014-01-30 15:37:40 -0600486# git clone only if directory doesn't exist already. Since ``DEST`` might not
487# be owned by the installation user, we create the directory and change the
488# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500489# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
490# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600491# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500492# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600493# git_clone remote dest-dir branch
494function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500495 local git_remote=$1
496 local git_dest=$2
497 local git_ref=$3
Ian Wienandada886d2015-10-07 14:06:26 +1100498 local orig_dir
499 orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200500 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500501
Sean Dague53753292014-12-04 19:38:15 -0500502 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800503 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200504 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
505 fi
506
Dean Troyerdff49a22014-01-30 15:37:40 -0600507 if [[ "$OFFLINE" = "True" ]]; then
508 echo "Running in offline mode, clones already exist"
509 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500510 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600511 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400512 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600513 return
514 fi
515
Dean Troyer50cda692014-07-25 11:57:20 -0500516 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600517 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500518 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700519 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
520 echo "The $git_dest project was not found; if this is a gate job, add"
James E. Blair35a0c572017-09-10 15:37:56 -0700521 echo "the project to 'required-projects' in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600522 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700523 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200524 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600525 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500526 cd $git_dest
527 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600528 else
529 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500530 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700531 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
532 echo "The $git_dest project was not found; if this is a gate job, add"
533 echo "the project to the \$PROJECTS variable in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600534 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700535 fi
Jakub Wachowski90742fc2016-11-18 14:28:47 +0100536 # '--branch' can also take tags
537 git_timed clone $git_clone_flags $git_remote $git_dest --branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600538 elif [[ "$RECLONE" = "True" ]]; then
539 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500540 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600541 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500542 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100543 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600544 # remove the existing ignored files (like pyc) as they cause breakage
545 # (due to the py files having older timestamps than our pyc, so python
546 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500547 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600548
Dean Troyer50cda692014-07-25 11:57:20 -0500549 # handle git_ref accordingly to type (tag, branch)
550 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
551 git_update_tag $git_ref
552 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
553 git_update_branch $git_ref
554 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
555 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600556 else
Dean Troyer50cda692014-07-25 11:57:20 -0500557 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600558 fi
559
560 fi
561 fi
562
563 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500564 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600565 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400566 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600567}
568
Sean Daguecc524062014-10-01 09:06:43 -0400569# A variation on git clone that lets us specify a project by it's
570# actual name, like oslo.config. This is exceptionally useful in the
571# library installation case
572function git_clone_by_name {
573 local name=$1
574 local repo=${GITREPO[$name]}
575 local dir=${GITDIR[$name]}
576 local branch=${GITBRANCH[$name]}
577 git_clone $repo $dir $branch
578}
579
580
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100581# git can sometimes get itself infinitely stuck with transient network
582# errors or other issues with the remote end. This wraps git in a
583# timeout/retry loop and is intended to watch over non-local git
584# processes that might hang. GIT_TIMEOUT, if set, is passed directly
585# to timeout(1); otherwise the default value of 0 maintains the status
586# quo of waiting forever.
587# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100588function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100589 local count=0
590 local timeout=0
591
592 if [[ -n "${GIT_TIMEOUT}" ]]; then
593 timeout=${GIT_TIMEOUT}
594 fi
595
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900596 time_start "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100597 until timeout -s SIGINT ${timeout} git "$@"; do
598 # 124 is timeout(1)'s special return code when it reached the
599 # timeout; otherwise assume fatal failure
600 if [[ $? -ne 124 ]]; then
601 die $LINENO "git call failed: [git $@]"
602 fi
603
604 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400605 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100606 if [ $count -eq 3 ]; then
607 die $LINENO "Maximum of 3 git retries reached"
608 fi
609 sleep 5
610 done
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900611 time_stop "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100612}
613
Dean Troyerdff49a22014-01-30 15:37:40 -0600614# git update using reference as a branch.
615# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100616function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500617 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600618
Dean Troyer50cda692014-07-25 11:57:20 -0500619 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600620 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500621 git branch -D $git_branch || true
622 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600623}
624
625# git update using reference as a branch.
626# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100627function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500628 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600629
Dean Troyer50cda692014-07-25 11:57:20 -0500630 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600631}
632
633# git update using reference as a tag. Be careful editing source at that repo
634# as working copy will be in a detached mode
635# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100636function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500637 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600638
Dean Troyer50cda692014-07-25 11:57:20 -0500639 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600640 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500641 git_timed fetch origin tag $git_tag
642 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600643}
644
645
646# OpenStack Functions
647# ===================
648
649# Get the default value for HOST_IP
650# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100651function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600652 local fixed_range=$1
653 local floating_range=$2
654 local host_ip_iface=$3
655 local host_ip=$4
Brian Haley180f5eb2015-06-16 13:14:31 -0400656 local af=$5
Dean Troyerdff49a22014-01-30 15:37:40 -0600657
Dean Troyerdff49a22014-01-30 15:37:40 -0600658 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
659 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
660 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100661 # Find the interface used for the default route
Brian Haley180f5eb2015-06-16 13:14:31 -0400662 host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
Ian Wienandada886d2015-10-07 14:06:26 +1100663 local host_ips
664 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 -0500665 local ip
666 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600667 # Attempt to filter out IP addresses that are part of the fixed and
668 # floating range. Note that this method only works if the ``netaddr``
669 # python library is installed. If it is not installed, an error
670 # will be printed and the first IP from the interface will be used.
671 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
672 # address.
Brian Haley180f5eb2015-06-16 13:14:31 -0400673 if [[ "$af" == "inet6" ]]; then
674 host_ip=$ip
675 break;
676 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500677 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
678 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600679 break;
680 fi
681 done
682 fi
683 echo $host_ip
684}
685
Attila Fazekasf71b5002014-05-28 09:52:22 +0200686# Generates hex string from ``size`` byte of pseudo random data
687# generate_hex_string size
688function generate_hex_string {
689 local size=$1
690 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
691}
692
Dean Troyerdff49a22014-01-30 15:37:40 -0600693# Grab a numbered field from python prettytable output
694# Fields are numbered starting with 1
695# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
696# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100697function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500698 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600699 while read data; do
700 if [ "$1" -lt 0 ]; then
701 field="(\$(NF$1))"
702 else
703 field="\$$(($1 + 1))"
704 fi
705 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
706 done
707}
708
yuntongjinf26deea2015-02-28 10:50:34 +0800709# install default policy
710# copy over a default policy.json and policy.d for projects
711function install_default_policy {
712 local project=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100713 local project_uc
714 project_uc=$(echo $1|tr a-z A-Z)
yuntongjinf26deea2015-02-28 10:50:34 +0800715 local conf_dir="${project_uc}_CONF_DIR"
716 # eval conf dir to get the variable
717 conf_dir="${!conf_dir}"
718 local project_dir="${project_uc}_DIR"
719 # eval project dir to get the variable
720 project_dir="${!project_dir}"
721 local sample_conf_dir="${project_dir}/etc/${project}"
722 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
723
724 # first copy any policy.json
725 cp -p $sample_conf_dir/policy.json $conf_dir
726 # then optionally copy over policy.d
727 if [[ -d $sample_policy_dir ]]; then
728 cp -r $sample_policy_dir $conf_dir/policy.d
729 fi
730}
731
Dean Troyerdff49a22014-01-30 15:37:40 -0600732# Add a policy to a policy.json file
733# Do nothing if the policy already exists
734# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100735function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600736 local policy_file=$1
737 local policy_name=$2
738 local policy_perm=$3
739
740 if grep -q ${policy_name} ${policy_file}; then
741 echo "Policy ${policy_name} already exists in ${policy_file}"
742 return
743 fi
744
745 # Add a terminating comma to policy lines without one
746 # Remove the closing '}' and all lines following to the end-of-file
Ian Wienandada886d2015-10-07 14:06:26 +1100747 local tmpfile
748 tmpfile=$(mktemp)
Dean Troyerdff49a22014-01-30 15:37:40 -0600749 uniq ${policy_file} | sed -e '
750 s/]$/],/
751 /^[}]/,$d
752 ' > ${tmpfile}
753
754 # Append policy and closing brace
755 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
756 echo "}" >>${tmpfile}
757
758 mv ${tmpfile} ${policy_file}
759}
760
Alistair Coles24779f62014-10-15 18:57:59 +0100761# Gets or creates a domain
762# Usage: get_or_create_domain <name> <description>
763function get_or_create_domain {
Ian Wienand11d276c2015-07-02 09:34:34 +1000764 local domain_id
Alistair Coles24779f62014-10-15 18:57:59 +0100765 # Gets domain id
Ian Wienand11d276c2015-07-02 09:34:34 +1000766 domain_id=$(
Alistair Coles24779f62014-10-15 18:57:59 +0100767 # Gets domain id
Steve Martinelli050a0d52015-09-06 22:03:54 +0000768 openstack domain show $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100769 -f value -c id 2>/dev/null ||
770 # Creates new domain
Steve Martinelli050a0d52015-09-06 22:03:54 +0000771 openstack domain create $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100772 --description "$2" \
773 -f value -c id
774 )
775 echo $domain_id
776}
777
Steve Martinellib74e01c2014-12-18 01:35:35 -0500778# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000779# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500780function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500781 local desc="${3:-}"
Ian Wienand11d276c2015-07-02 09:34:34 +1000782 local group_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500783 # Gets group id
Ian Wienand11d276c2015-07-02 09:34:34 +1000784 group_id=$(
Steve Martinellib74e01c2014-12-18 01:35:35 -0500785 # Creates new group with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000786 openstack group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000787 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500788 -f value -c id
789 )
790 echo $group_id
791}
792
Bartosz Górski0abde392014-02-28 14:15:19 +0100793# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000794# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100795function get_or_create_user {
Ian Wienand11d276c2015-07-02 09:34:34 +1000796 local user_id
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000797 if [[ ! -z "$4" ]]; then
798 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200799 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500800 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200801 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100802 # Gets user id
Ian Wienand11d276c2015-07-02 09:34:34 +1000803 user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500804 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000805 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100806 $1 \
807 --password "$2" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000808 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500809 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500810 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100811 -f value -c id
812 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500813 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100814}
815
816# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000817# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100818function get_or_create_project {
Ian Wienand11d276c2015-07-02 09:34:34 +1000819 local project_id
820 project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500821 # Creates new project with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000822 openstack project create $1 \
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000823 --domain=$2 \
824 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100825 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500826 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100827}
828
829# Gets or creates role
830# Usage: get_or_create_role <name>
831function get_or_create_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000832 local role_id
833 role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500834 # Creates role with --or-show
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000835 openstack role create $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000836 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100837 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500838 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100839}
840
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600841# Returns the domain parts of a function call if present
842# Usage: _get_domain_args [<user_domain> <project_domain>]
843function _get_domain_args {
844 local domain
845 domain=""
846
847 if [[ -n "$1" ]]; then
848 domain="$domain --user-domain $1"
849 fi
850 if [[ -n "$2" ]]; then
851 domain="$domain --project-domain $2"
852 fi
853
854 echo $domain
855}
856
Jamie Lennox9b215db2015-02-10 18:19:57 +1100857# Gets or adds user role to project
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600858# Usage: get_or_add_user_project_role <role> <user> <project> [<user_domain> <project_domain>]
Jamie Lennox9b215db2015-02-10 18:19:57 +1100859function get_or_add_user_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000860 local user_role_id
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600861
862 domain_args=$(_get_domain_args $4 $5)
863
Bartosz Górski0abde392014-02-28 14:15:19 +0100864 # Gets user role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700865 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700866 --role $1 \
Steve Martinelli5541a612015-01-19 15:58:49 -0500867 --user $2 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000868 --project $3 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600869 $domain_args \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700870 | grep '^|\s[a-f0-9]\+' | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500871 if [[ -z "$user_role_id" ]]; then
Roxana Gherle50821be2015-09-22 10:52:46 -0700872 # Adds role to user and get it
873 openstack role add $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100874 --user $2 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600875 --project $3 \
876 $domain_args
Mike Perezc271b3e2016-10-03 16:00:33 -0700877 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700878 --role $1 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700879 --user $2 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700880 --project $3 \
Jamie Lennoxcbcbd8f2016-01-21 16:08:14 -0600881 $domain_args \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700882 | grep '^|\s[a-f0-9]\+' | get_field 1)
Bartosz Górski0abde392014-02-28 14:15:19 +0100883 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500884 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100885}
886
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500887# Gets or adds user role to domain
888# Usage: get_or_add_user_domain_role <role> <user> <domain>
889function get_or_add_user_domain_role {
890 local user_role_id
891 # Gets user role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700892 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700893 --role $1 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500894 --user $2 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500895 --domain $3 \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700896 | grep '^|\s[a-f0-9]\+' | get_field 1)
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500897 if [[ -z "$user_role_id" ]]; then
898 # Adds role to user and get it
899 openstack role add $1 \
900 --user $2 \
901 --domain $3
Mike Perezc271b3e2016-10-03 16:00:33 -0700902 user_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700903 --role $1 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500904 --user $2 \
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500905 --domain $3 \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700906 | grep '^|\s[a-f0-9]\+' | get_field 1)
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500907 fi
908 echo $user_role_id
909}
910
Steve Martinelli4599fd12015-03-12 21:30:58 -0400911# Gets or adds group role to project
912# Usage: get_or_add_group_project_role <role> <group> <project>
913function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000914 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -0400915 # Gets group role id
Mike Perezc271b3e2016-10-03 16:00:33 -0700916 group_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700917 --role $1 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400918 --group $2 \
919 --project $3 \
Mike Perezc271b3e2016-10-03 16:00:33 -0700920 -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400921 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000922 # Adds role to group and get it
923 openstack role add $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000924 --group $2 \
925 --project $3
Mike Perezc271b3e2016-10-03 16:00:33 -0700926 group_role_id=$(openstack role assignment list \
Clark Boylan3e9b5622017-06-14 15:29:47 -0700927 --role $1 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400928 --group $2 \
929 --project $3 \
Mike Perezc271b3e2016-10-03 16:00:33 -0700930 -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400931 fi
932 echo $group_role_id
933}
934
Bartosz Górski0abde392014-02-28 14:15:19 +0100935# Gets or creates service
936# Usage: get_or_create_service <name> <type> <description>
937function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +1000938 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100939 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +1000940 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100941 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +1000942 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100943 # Creates new service if not exists
944 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500945 $2 \
946 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100947 --description="$3" \
948 -f value -c id
949 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500950 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100951}
952
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000953# Create an endpoint with a specific interface
Matt Riedemannae4578b2016-04-23 01:45:40 +0000954# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
955function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +1000956 local endpoint_id
Matt Riedemannae4578b2016-04-23 01:45:40 +0000957 endpoint_id=$(openstack endpoint list \
958 --service $1 \
959 --interface $2 \
960 --region $4 \
961 -c ID -f value)
962 if [[ -z "$endpoint_id" ]]; then
963 # Creates new endpoint
964 endpoint_id=$(openstack endpoint create \
965 $1 $2 $3 --region $4 -f value -c id)
966 fi
967
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500968 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100969}
Dean Troyerdff49a22014-01-30 15:37:40 -0600970
Sean Dague7d1ec432016-04-22 09:19:10 -0400971# Gets or creates endpoint
Sean Dague11eb2012017-02-13 16:16:59 -0500972# Usage: get_or_create_endpoint <service> <region> <publicurl> [adminurl] [internalurl]
Matt Riedemannae4578b2016-04-23 01:45:40 +0000973function get_or_create_endpoint {
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000974 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
975 # creating one endpoint with multiple urls to multiple endpoints each with
976 # a different interface. To maintain the existing function interface we
Matt Riedemannae4578b2016-04-23 01:45:40 +0000977 # create 3 endpoints and return the id of the public one. In reality
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000978 # returning the public id will not make a lot of difference as there are no
979 # scenarios currently that use the returned id. Ideally this behaviour
980 # should be pushed out to the service setups and let them create the
981 # endpoints they need.
Ian Wienandada886d2015-10-07 14:06:26 +1100982 local public_id
Matt Riedemannae4578b2016-04-23 01:45:40 +0000983 public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
Sean Dague11eb2012017-02-13 16:16:59 -0500984 # only create admin/internal urls if provided content for them
985 if [[ -n "$4" ]]; then
986 _get_or_create_endpoint_with_interface $1 admin $4 $2
987 fi
988 if [[ -n "$5" ]]; then
989 _get_or_create_endpoint_with_interface $1 internal $5 $2
990 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000991 # return the public id to indicate success, and this is the endpoint most likely wanted
992 echo $public_id
993}
994
995# Get a URL from the identity service
996# Usage: get_endpoint_url <service> <interface>
997function get_endpoint_url {
998 echo $(openstack endpoint list \
999 --service $1 --interface $2 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001000 -c URL -f value)
1001}
1002
Jim Rollenhagen47367072015-12-10 14:24:00 +00001003# check if we are using ironic with hardware
1004# TODO(jroll) this is a kludge left behind when ripping ironic code
1005# out of tree, as it is used by nova and neutron.
1006# figure out a way to refactor nova/neutron code to eliminate this
1007function is_ironic_hardware {
Lucas Alvares Gomes8a4dea22016-02-18 15:52:22 +00001008 is_service_enabled ironic && [[ "$IRONIC_IS_HARDWARE" == "True" ]] && return 0
Jim Rollenhagen47367072015-12-10 14:24:00 +00001009 return 1
1010}
1011
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001012
Dean Troyerdff49a22014-01-30 15:37:40 -06001013# Package Functions
1014# =================
1015
1016# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +11001017function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001018 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -06001019 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001020
1021 if [[ -z "$base_dir" ]]; then
1022 base_dir=$FILES
1023 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001024 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001025 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -06001026 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001027 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -06001028 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001029 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -06001030 else
1031 exit_distro_not_supported "list of packages"
1032 fi
1033 echo "$pkg_dir"
1034}
1035
Sean Dague88ee8ce2015-12-02 07:47:31 -05001036# Wrapper for ``apt-get update`` to try multiple times on the update
1037# to address bad package mirrors (which happen all the time).
1038function apt_get_update {
1039 # only do this once per run
1040 if [[ "$REPOS_UPDATED" == "True" && "$RETRY_UPDATE" != "True" ]]; then
1041 return
1042 fi
1043
1044 # bail if we are offline
1045 [[ "$OFFLINE" = "True" ]] && return
1046
1047 local sudo="sudo"
1048 [[ "$(id -u)" = "0" ]] && sudo="env"
1049
1050 # time all the apt operations
1051 time_start "apt-get-update"
1052
1053 local proxies="http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} no_proxy=${no_proxy:-} "
1054 local update_cmd="$sudo $proxies apt-get update"
1055 if ! timeout 300 sh -c "while ! $update_cmd; do sleep 30; done"; then
1056 die $LINENO "Failed to update apt repos, we're dead now"
1057 fi
1058
1059 REPOS_UPDATED=True
1060 # stop the clock
1061 time_stop "apt-get-update"
1062}
1063
Dean Troyerdff49a22014-01-30 15:37:40 -06001064# Wrapper for ``apt-get`` to set cache and proxy environment variables
1065# Uses globals ``OFFLINE``, ``*_proxy``
1066# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001067function apt_get {
Federico Ressie208d062015-11-21 11:15:39 +00001068 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +11001069 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001070 set +o xtrace
1071
Dean Troyerdff49a22014-01-30 15:37:40 -06001072 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
1073 local sudo="sudo"
1074 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -05001075
Sean Dague95c33d52015-10-07 11:05:59 -04001076 # time all the apt operations
1077 time_start "apt-get"
1078
Sean Dague45917cc2014-02-24 16:09:14 -05001079 $xtrace
Sean Dague53753292014-12-04 19:38:15 -05001080
Dean Troyerdff49a22014-01-30 15:37:40 -06001081 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -05001082 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
1083 no_proxy=${no_proxy:-} \
John L. Villalovosf568c3a2016-01-07 19:10:50 -08001084 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" < /dev/null
Federico Ressie208d062015-11-21 11:15:39 +00001085 result=$?
Sean Dague95c33d52015-10-07 11:05:59 -04001086
1087 # stop the clock
1088 time_stop "apt-get"
Federico Ressie208d062015-11-21 11:15:39 +00001089 return $result
Dean Troyerdff49a22014-01-30 15:37:40 -06001090}
1091
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001092function _parse_package_files {
1093 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -06001094
Dean Troyerdff49a22014-01-30 15:37:40 -06001095 if [[ -z "$DISTRO" ]]; then
1096 GetDistro
1097 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001098
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001099 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001100 local OIFS line package distros distro
1101 [[ -e $fname ]] || continue
1102
1103 OIFS=$IFS
1104 IFS=$'\n'
1105 for line in $(<${fname}); do
1106 if [[ $line =~ "NOPRIME" ]]; then
1107 continue
1108 fi
1109
Ian Wienand1ff75ff2016-02-19 14:28:37 +11001110 # Assume we want this package; free-form
1111 # comments allowed after a #
1112 package=${line%%#*}
Dean Troyerdff49a22014-01-30 15:37:40 -06001113 inst_pkg=1
1114
1115 # Look for # dist:xxx in comment
1116 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1117 # We are using BASH regexp matching feature.
1118 package=${BASH_REMATCH[1]}
1119 distros=${BASH_REMATCH[2]}
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001120 # In bash ${VAR,,} will lowercase VAR
Dean Troyerdff49a22014-01-30 15:37:40 -06001121 # Look for a match in the distro list
1122 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1123 # If no match then skip this package
1124 inst_pkg=0
1125 fi
1126 fi
1127
David Rabel682e0ab2017-03-17 19:19:00 +01001128 # Look for # not:xxx in comment
1129 if [[ $line =~ (.*)#.*not:([^ ]*) ]]; then
1130 # We are using BASH regexp matching feature.
1131 package=${BASH_REMATCH[1]}
1132 distros=${BASH_REMATCH[2]}
1133 # In bash ${VAR,,} will lowercase VAR
1134 # Look for a match in the distro list
1135 if [[ ${distros,,} =~ ${DISTRO,,} ]]; then
1136 # If match then skip this package
1137 inst_pkg=0
1138 fi
1139 fi
1140
Dean Troyerdff49a22014-01-30 15:37:40 -06001141 if [[ $inst_pkg = 1 ]]; then
1142 echo $package
1143 fi
1144 done
1145 IFS=$OIFS
1146 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001147}
1148
1149# get_packages() collects a list of package names of any type from the
1150# prerequisite files in ``files/{debs|rpms}``. The list is intended
1151# to be passed to a package installer such as apt or yum.
1152#
1153# Only packages required for the services in 1st argument will be
1154# included. Two bits of metadata are recognized in the prerequisite files:
1155#
1156# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1157# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1158# of the package to the distros listed. The distro names are case insensitive.
David Rabel682e0ab2017-03-17 19:19:00 +01001159# - ``# not:DISTRO`` or ``not:DISTRO1,DISTRO2`` limits the selection
1160# of the package to the distros not listed. The distro names are case insensitive.
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001161function get_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001162 local xtrace
1163 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001164 set +o xtrace
1165 local services=$@
Ian Wienandada886d2015-10-07 14:06:26 +11001166 local package_dir
1167 package_dir=$(_get_package_dir)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001168 local file_to_parse=""
1169 local service=""
1170
Ian Wienand7d515b52015-11-09 15:04:32 +11001171 if [ $# -ne 1 ]; then
1172 die $LINENO "get_packages takes a single, comma-separated argument"
1173 fi
1174
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001175 if [[ -z "$package_dir" ]]; then
1176 echo "No package directory supplied"
1177 return 1
1178 fi
1179 for service in ${services//,/ }; do
1180 # Allow individual services to specify dependencies
1181 if [[ -e ${package_dir}/${service} ]]; then
1182 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1183 fi
1184 # NOTE(sdague) n-api needs glance for now because that's where
1185 # glance client is
1186 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001187 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001188 file_to_parse="${file_to_parse} ${package_dir}/nova"
1189 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001190 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001191 file_to_parse="${file_to_parse} ${package_dir}/glance"
1192 fi
1193 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001194 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001195 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1196 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001197 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001198 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001199 file_to_parse="${file_to_parse} ${package_dir}/swift"
1200 fi
1201 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001202 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001203 file_to_parse="${file_to_parse} ${package_dir}/nova"
1204 fi
1205 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001206 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001207 file_to_parse="${file_to_parse} ${package_dir}/glance"
1208 fi
1209 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001210 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001211 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1212 fi
Ihar Hrachyshka387aadd2017-09-14 09:25:59 -06001213 elif [[ $service == q-* || $service == neutron-* ]]; then
1214 if [[ ! $file_to_parse =~ $package_dir/neutron-common ]]; then
1215 file_to_parse="${file_to_parse} ${package_dir}/neutron-common"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001216 fi
1217 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001218 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001219 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1220 fi
1221 fi
1222 done
1223 echo "$(_parse_package_files $file_to_parse)"
1224 $xtrace
1225}
1226
1227# get_plugin_packages() collects a list of package names of any type from a
1228# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1229# list is intended to be passed to a package installer such as apt or yum.
1230#
1231# Only packages required for enabled and collected plugins will included.
1232#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001233# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001234# in these prerequisite files, see get_packages() for more info.
1235function get_plugin_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001236 local xtrace
1237 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001238 set +o xtrace
1239 local files_to_parse=""
1240 local package_dir=""
1241 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
Ian Wienand7ae97292016-02-16 14:50:53 +11001242 package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
Dmitry Tantsur5979f472016-01-03 18:08:14 +01001243 files_to_parse+=" $package_dir/$plugin"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001244 done
1245 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001246 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001247}
1248
1249# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001250# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001251# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001252function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001253 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1254 REPOS_UPDATED=${REPOS_UPDATED:-False}
1255 RETRY_UPDATE=${RETRY_UPDATE:-False}
1256
Paul Linchpiner9e179742014-07-13 22:23:00 -07001257 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001258 return 0
1259 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001260
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001261 if is_ubuntu; then
Sean Dague88ee8ce2015-12-02 07:47:31 -05001262 apt_get_update
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001263 fi
1264}
1265
1266function real_install_package {
1267 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001268 apt_get install "$@"
1269 elif is_fedora; then
1270 yum_install "$@"
1271 elif is_suse; then
1272 zypper_install "$@"
1273 else
1274 exit_distro_not_supported "installing packages"
1275 fi
1276}
1277
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001278# Distro-agnostic package installer
1279# install_package package [package ...]
1280function install_package {
1281 update_package_repo
Federico Ressiecc1f412015-12-28 15:14:13 +01001282 if ! real_install_package "$@"; then
1283 RETRY_UPDATE=True update_package_repo && real_install_package "$@"
1284 fi
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001285}
1286
Dean Troyerdff49a22014-01-30 15:37:40 -06001287# Distro-agnostic function to tell if a package is installed
1288# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001289function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001290 if [[ -z "$@" ]]; then
1291 return 1
1292 fi
1293
1294 if [[ -z "$os_PACKAGE" ]]; then
1295 GetOSVersion
1296 fi
1297
1298 if [[ "$os_PACKAGE" = "deb" ]]; then
1299 dpkg -s "$@" > /dev/null 2> /dev/null
1300 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1301 rpm --quiet -q "$@"
1302 else
1303 exit_distro_not_supported "finding if a package is installed"
1304 fi
1305}
1306
1307# Distro-agnostic package uninstaller
1308# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001309function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001310 if is_ubuntu; then
1311 apt_get purge "$@"
1312 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001313 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001314 elif is_suse; then
Gary W. Smith56b39122016-11-16 22:03:43 -08001315 sudo zypper remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001316 else
1317 exit_distro_not_supported "uninstalling packages"
1318 fi
1319}
1320
1321# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001322# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001323# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001324function yum_install {
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001325 local result parse_yum_result
1326
Dean Troyerdff49a22014-01-30 15:37:40 -06001327 [[ "$OFFLINE" = "True" ]] && return
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001328
1329 time_start "yum_install"
1330
Ian Wienanda4705402016-09-23 15:08:48 +10001331 # This is a bit tricky, because yum -y assumes missing or failed
1332 # packages are OK (see [1]). We want devstack to stop if we are
1333 # installing missing packages.
1334 #
1335 # Thus we manually match on the output (stack.sh runs in a fixed
1336 # locale, so lang shouldn't change).
1337 #
1338 # If yum returns !0, we echo the result as "YUM_FAILED" and return
1339 # that from the awk (we're subverting -e with this trick).
1340 # Otherwise we use awk to look for failure strings and return "2"
1341 # to indicate a terminal failure.
1342 #
1343 # [1] https://bugzilla.redhat.com/show_bug.cgi?id=965567
1344 parse_yum_result=' \
1345 BEGIN { result=0 } \
1346 /^YUM_FAILED/ { result=$2 } \
1347 /^No package/ { result=2 } \
1348 /^Failed:/ { result=2 } \
1349 //{ print } \
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001350 END { exit result }'
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001351 (sudo_with_proxies "${YUM:-yum}" install -y "$@" 2>&1 || echo YUM_FAILED $?) \
Ian Wienand1b1cc8c2016-03-01 13:14:39 +11001352 | awk "$parse_yum_result" && result=$? || result=$?
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001353
1354 time_stop "yum_install"
1355
Ian Wienand1b1cc8c2016-03-01 13:14:39 +11001356 # if we return 1, then the wrapper functions will run an update
1357 # and try installing the package again as a defense against bad
1358 # mirrors. This can hide failures, especially when we have
1359 # packages that are in the "Failed:" section because their rpm
1360 # install scripts failed to run correctly (in this case, the
1361 # package looks installed, so when the retry happens we just think
1362 # the package is OK, and incorrectly continue on).
1363 if [ "$result" == 2 ]; then
1364 die "Detected fatal package install failure"
1365 fi
1366
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001367 return "$result"
Dean Troyerdff49a22014-01-30 15:37:40 -06001368}
1369
1370# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001371# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001372# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001373function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001374 [[ "$OFFLINE" = "True" ]] && return
1375 local sudo="sudo"
1376 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001377 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1378 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001379 zypper --non-interactive install --auto-agree-with-licenses "$@"
1380}
1381
Sean Dague5edae542017-03-21 20:50:24 -04001382function write_user_unit_file {
1383 local service=$1
1384 local command="$2"
1385 local group=$3
1386 local user=$4
1387 local extra=""
1388 if [[ -n "$group" ]]; then
1389 extra="Group=$group"
1390 fi
1391 local unitfile="$SYSTEMD_DIR/$service"
1392 mkdir -p $SYSTEMD_DIR
1393
1394 iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
1395 iniset -sudo $unitfile "Service" "User" "$user"
1396 iniset -sudo $unitfile "Service" "ExecStart" "$command"
Matthew Treinish477a9622017-08-04 11:09:26 -04001397 iniset -sudo $unitfile "Service" "KillMode" "process"
Michał Dulko3b815a32017-11-14 16:04:51 +01001398 iniset -sudo $unitfile "Service" "TimeoutStopSec" "300"
Dinesh Bhoref60f2b2017-09-05 14:40:32 +05301399 iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
Sean Dague5edae542017-03-21 20:50:24 -04001400 if [[ -n "$group" ]]; then
1401 iniset -sudo $unitfile "Service" "Group" "$group"
1402 fi
1403 iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
1404
1405 # changes to existing units sometimes need a refresh
1406 $SYSTEMCTL daemon-reload
1407}
1408
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001409function write_uwsgi_user_unit_file {
1410 local service=$1
1411 local command="$2"
1412 local group=$3
1413 local user=$4
1414 local unitfile="$SYSTEMD_DIR/$service"
1415 mkdir -p $SYSTEMD_DIR
1416
1417 iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
Sean Daguef41bf2a2017-05-05 07:50:52 -04001418 iniset -sudo $unitfile "Service" "SyslogIdentifier" "$service"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001419 iniset -sudo $unitfile "Service" "User" "$user"
1420 iniset -sudo $unitfile "Service" "ExecStart" "$command"
Dinesh Bhoref60f2b2017-09-05 14:40:32 +05301421 iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001422 iniset -sudo $unitfile "Service" "Type" "notify"
Matthew Treinish477a9622017-08-04 11:09:26 -04001423 iniset -sudo $unitfile "Service" "KillMode" "process"
gong yong sheng07b3bc22017-06-05 14:02:28 +08001424 iniset -sudo $unitfile "Service" "Restart" "always"
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001425 iniset -sudo $unitfile "Service" "NotifyAccess" "all"
1426 iniset -sudo $unitfile "Service" "RestartForceExitStatus" "100"
1427
1428 if [[ -n "$group" ]]; then
1429 iniset -sudo $unitfile "Service" "Group" "$group"
1430 fi
1431 iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
1432
1433 # changes to existing units sometimes need a refresh
1434 $SYSTEMCTL daemon-reload
1435}
1436
Sean Dague148d58c2017-05-04 13:08:25 -04001437function _common_systemd_pitfalls {
1438 local cmd=$1
1439 # do some sanity checks on $cmd to see things we don't expect to work
1440
1441 if [[ "$cmd" =~ "sudo" ]]; then
Paweł Suder4e16c3d2018-09-26 15:52:13 +02001442 read -r -d '' msg << EOF || true # read returns 1 for EOF, but it is ok here
Sean Dague148d58c2017-05-04 13:08:25 -04001443You are trying to use run_process with sudo, this is not going to work under systemd.
1444
1445If you need to run a service as a user other than $STACK_USER call it with:
1446
1447 run_process \$name \$cmd \$group \$user
1448EOF
1449 die $LINENO $msg
1450 fi
1451
1452 if [[ ! "$cmd" =~ ^/ ]]; then
Paweł Suder4e16c3d2018-09-26 15:52:13 +02001453 read -r -d '' msg << EOF || true # read returns 1 for EOF, but it is ok here
Sean Dague148d58c2017-05-04 13:08:25 -04001454The cmd="$cmd" does not start with an absolute path. It will fail to
1455start under systemd.
1456
1457Please update your run_process stanza to have an absolute path.
1458EOF
1459 die $LINENO $msg
1460 fi
1461
1462}
1463
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001464# Helper function to build a basic unit file and run it under systemd.
1465function _run_under_systemd {
Sean Dague5edae542017-03-21 20:50:24 -04001466 local service=$1
1467 local command="$2"
1468 local cmd=$command
Sean Dague148d58c2017-05-04 13:08:25 -04001469 # sanity check the command
1470 _common_systemd_pitfalls "$cmd"
1471
Sean Dague5edae542017-03-21 20:50:24 -04001472 local systemd_service="devstack@$service.service"
1473 local group=$3
1474 local user=${4:-$STACK_USER}
Matthew Treinish9c5ffd82017-03-29 16:47:57 -04001475 if [[ "$command" =~ "uwsgi" ]] ; then
1476 write_uwsgi_user_unit_file $systemd_service "$cmd" "$group" "$user"
1477 else
1478 write_user_unit_file $systemd_service "$cmd" "$group" "$user"
1479 fi
YAMAMOTO Takashic087c712017-06-15 12:10:45 +00001480
1481 $SYSTEMCTL enable $systemd_service
1482 $SYSTEMCTL start $systemd_service
Sean Dague5edae542017-03-21 20:50:24 -04001483}
1484
Dean Troyerdff49a22014-01-30 15:37:40 -06001485# Find out if a process exists by partial name.
1486# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001487function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001488 local name=$1
1489 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001490 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001491 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001492 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001493}
1494
Dean Troyer3159a822014-08-27 14:13:58 -05001495# Run a single service under screen or directly
1496# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001497# If an optional group is provided sg will be used to run the
1498# command as that group.
Sean Dague148d58c2017-05-04 13:08:25 -04001499# run_process service "command-line" [group] [user]
Ian Wienandaee18c72014-02-21 15:35:08 +11001500function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001501 local service=$1
1502 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001503 local group=$3
Sean Dague5edae542017-03-21 20:50:24 -04001504 local user=$4
Brant Knudsonedc11c22015-12-14 15:32:05 -06001505
Sean Dague5edae542017-03-21 20:50:24 -04001506 local name=$service
Dean Troyerdff49a22014-01-30 15:37:40 -06001507
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001508 time_start "run_process"
Dean Troyer3159a822014-08-27 14:13:58 -05001509 if is_service_enabled $service; then
Sean Daguecdba1b32017-08-30 11:11:06 -04001510 _run_under_systemd "$name" "$command" "$group" "$user"
Dean Troyer3159a822014-08-27 14:13:58 -05001511 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001512 time_stop "run_process"
Dean Troyerdff49a22014-01-30 15:37:40 -06001513}
1514
Dean Troyer3159a822014-08-27 14:13:58 -05001515# Stop a service process
1516# If a PID is available use it, kill the whole process group via TERM
1517# If screen is being used kill the screen window; this will catch processes
1518# that did not leave a PID behind
Radoslav Gerganov2b97a812017-10-10 16:51:12 +03001519# Uses globals ``SERVICE_DIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001520# stop_process service
1521function stop_process {
1522 local service=$1
1523
1524 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer3159a822014-08-27 14:13:58 -05001525
1526 if is_service_enabled $service; then
Sean Dague803acff2017-05-01 10:52:38 -04001527 # Only do this for units which appear enabled, this also
1528 # catches units that don't really exist for cases like
1529 # keystone without a failure.
1530 if $SYSTEMCTL is-enabled devstack@$service.service; then
1531 $SYSTEMCTL stop devstack@$service.service
1532 $SYSTEMCTL disable devstack@$service.service
Sean Dague5edae542017-03-21 20:50:24 -04001533 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001534 fi
1535}
1536
Sean Daguecdba1b32017-08-30 11:11:06 -04001537# use systemctl to check service status
Ian Wienandaee18c72014-02-21 15:35:08 +11001538function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001539 local service
Sean Daguecdba1b32017-08-30 11:11:06 -04001540 for service in ${ENABLED_SERVICES//,/ }; do
1541 # because some things got renamed like key => keystone
1542 if $SYSTEMCTL is-enabled devstack@$service.service; then
1543 # no-pager is needed because otherwise status dumps to a
1544 # pager when in interactive mode, which will stop a manual
1545 # devstack run.
1546 $SYSTEMCTL status devstack@$service.service --no-pager
Dean Troyer3159a822014-08-27 14:13:58 -05001547 fi
Sean Daguecdba1b32017-08-30 11:11:06 -04001548 done
Dean Troyer3159a822014-08-27 14:13:58 -05001549}
1550
Dean Troyer3159a822014-08-27 14:13:58 -05001551
Sean Daguecdba1b32017-08-30 11:11:06 -04001552function tail_log {
1553 deprecated "With the removal of screen support, tail_log is deprecated and will be removed after Queens"
1554}
Dean Troyer3159a822014-08-27 14:13:58 -05001555
Sean Dague2c65e712014-12-18 09:44:56 -05001556# Plugin Functions
1557# =================
1558
1559DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1560
1561# enable_plugin <name> <url> [branch]
1562#
1563# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1564# ``url`` is a git url
1565# ``branch`` is a gitref. If it's not set, defaults to master
1566function enable_plugin {
1567 local name=$1
1568 local url=$2
1569 local branch=${3:-master}
Omer Anson51584862017-08-24 17:47:37 +03001570 if is_plugin_enabled $name; then
John L. Villalovos21d84c22016-11-11 15:26:11 -08001571 die $LINENO "Plugin attempted to be enabled twice: ${name} ${url} ${branch}"
1572 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001573 DEVSTACK_PLUGINS+=",$name"
1574 GITREPO[$name]=$url
1575 GITDIR[$name]=$DEST/$name
1576 GITBRANCH[$name]=$branch
1577}
1578
Omer Anson51584862017-08-24 17:47:37 +03001579# is_plugin_enabled <name>
1580#
1581# Check if the plugin was enabled, e.g. using enable_plugin
1582#
1583# ``name`` The name with which the plugin was enabled
1584function is_plugin_enabled {
1585 local name=$1
1586 if [[ ",${DEVSTACK_PLUGINS}," =~ ",${name}," ]]; then
1587 return 0
1588 fi
1589 return 1
1590}
1591
Sean Dague2c65e712014-12-18 09:44:56 -05001592# fetch_plugins
1593#
1594# clones all plugins
1595function fetch_plugins {
1596 local plugins="${DEVSTACK_PLUGINS}"
1597 local plugin
1598
1599 # short circuit if nothing to do
1600 if [[ -z $plugins ]]; then
1601 return
1602 fi
1603
Dean Troyerdc97cb72015-03-28 08:20:50 -05001604 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001605 for plugin in ${plugins//,/ }; do
1606 git_clone_by_name $plugin
1607 done
1608}
1609
1610# load_plugin_settings
1611#
1612# Load settings from plugins in the order that they were registered
1613function load_plugin_settings {
1614 local plugins="${DEVSTACK_PLUGINS}"
1615 local plugin
1616
1617 # short circuit if nothing to do
1618 if [[ -z $plugins ]]; then
1619 return
1620 fi
1621
1622 echo "Loading plugin settings"
1623 for plugin in ${plugins//,/ }; do
1624 local dir=${GITDIR[$plugin]}
1625 # source any known settings
1626 if [[ -f $dir/devstack/settings ]]; then
1627 source $dir/devstack/settings
1628 fi
1629 done
1630}
1631
Sean Dague6e275e12015-03-26 05:54:28 -04001632# plugin_override_defaults
1633#
1634# Run an extremely early setting phase for plugins that allows default
1635# overriding of services.
1636function plugin_override_defaults {
1637 local plugins="${DEVSTACK_PLUGINS}"
1638 local plugin
1639
1640 # short circuit if nothing to do
1641 if [[ -z $plugins ]]; then
1642 return
1643 fi
1644
1645 echo "Overriding Configuration Defaults"
1646 for plugin in ${plugins//,/ }; do
1647 local dir=${GITDIR[$plugin]}
1648 # source any overrides
1649 if [[ -f $dir/devstack/override-defaults ]]; then
1650 # be really verbose that an override is happening, as it
1651 # may not be obvious if things fail later.
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001652 echo "$plugin has overridden the following defaults"
Sean Dague6e275e12015-03-26 05:54:28 -04001653 cat $dir/devstack/override-defaults
1654 source $dir/devstack/override-defaults
1655 fi
1656 done
1657}
1658
Sean Dague2c65e712014-12-18 09:44:56 -05001659# run_plugins
1660#
1661# Run the devstack/plugin.sh in all the plugin directories. These are
1662# run in registration order.
1663function run_plugins {
1664 local mode=$1
1665 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301666
1667 local plugins="${DEVSTACK_PLUGINS}"
1668 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001669 for plugin in ${plugins//,/ }; do
1670 local dir=${GITDIR[$plugin]}
1671 if [[ -f $dir/devstack/plugin.sh ]]; then
1672 source $dir/devstack/plugin.sh $mode $phase
1673 fi
1674 done
1675}
1676
1677function run_phase {
1678 local mode=$1
1679 local phase=$2
1680 if [[ -d $TOP_DIR/extras.d ]]; then
Dmitry Tantsur64be3212015-10-12 13:10:24 +02001681 local extra_plugin_file_name
1682 for extra_plugin_file_name in $TOP_DIR/extras.d/*.sh; do
Sean Dague41d01102015-12-03 08:12:23 -05001683 # NOTE(sdague): only process extras.d for the 3 explicitly
1684 # white listed elements in tree. We want these to move out
1685 # over time as well, but they are in tree, so we need to
1686 # manage that.
Matt Riedemannc9f63272016-08-30 17:21:30 -04001687 local exceptions="80-tempest.sh"
Ian Wienand5cdee8d2015-10-19 14:17:18 +11001688 local extra
1689 extra=$(basename $extra_plugin_file_name)
Sean Dague1de9e332015-10-07 08:46:13 -04001690 if [[ ! ( $exceptions =~ "$extra" ) ]]; then
Sean Dague41d01102015-12-03 08:12:23 -05001691 warn "use of extras.d is no longer supported"
1692 warn "processing of project $extra is skipped"
1693 else
1694 [[ -r $extra_plugin_file_name ]] && source $extra_plugin_file_name $mode $phase
Sean Dague1de9e332015-10-07 08:46:13 -04001695 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001696 done
1697 fi
1698 # the source phase corresponds to settings loading in plugins
1699 if [[ "$mode" == "source" ]]; then
1700 load_plugin_settings
Chris Dentc6d47012015-10-09 14:57:05 +00001701 verify_disabled_services
Sean Dague6e275e12015-03-26 05:54:28 -04001702 elif [[ "$mode" == "override_defaults" ]]; then
1703 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001704 else
1705 run_plugins $mode $phase
1706 fi
1707}
1708
James E. Blairc5853ac2017-11-21 09:44:42 -08001709# define_plugin <name>
1710#
1711# This function is a no-op. It allows a plugin to define its name So
1712# that other plugins may reference it by name. It should generally be
1713# the last component of the canonical git repo name. E.g.,
1714# openstack/devstack-foo should use "devstack-foo" as the name here.
1715#
1716# This function is currently a noop, but the value may still be used
1717# by external tools (as in plugin_requires) and may be used by
1718# devstack in the future.
1719#
1720# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1721function define_plugin {
1722 :
1723}
1724
1725# plugin_requires <name> <other>
1726#
1727# This function is a no-op. It is currently used by external tools
1728# (such as the devstack module for Ansible) to automatically generate
1729# local.conf files. It is not currently used by devstack itself to
1730# resolve dependencies.
1731#
1732# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1733# ``other`` is the name of another plugin
1734function plugin_requires {
1735 :
1736}
1737
Dean Troyerdff49a22014-01-30 15:37:40 -06001738
1739# Service Functions
1740# =================
1741
1742# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1743# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001744function _cleanup_service_list {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001745 local xtrace
1746 xtrace=$(set +o | grep xtrace)
1747 set +o xtrace
1748
Dean Troyerdff49a22014-01-30 15:37:40 -06001749 echo "$1" | sed -e '
1750 s/,,/,/g;
1751 s/^,//;
1752 s/,$//
1753 '
Ian Wienand8043bfa2015-10-14 14:53:18 +11001754
1755 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001756}
1757
1758# disable_all_services() removes all current services
1759# from ``ENABLED_SERVICES`` to reset the configuration
1760# before a minimal installation
1761# Uses global ``ENABLED_SERVICES``
1762# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001763function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001764 ENABLED_SERVICES=""
1765}
1766
1767# Remove all services starting with '-'. For example, to install all default
1768# services except rabbit (rabbit) set in ``localrc``:
1769# ENABLED_SERVICES+=",-rabbit"
1770# Uses global ``ENABLED_SERVICES``
1771# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001772function disable_negated_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001773 local xtrace
1774 xtrace=$(set +o | grep xtrace)
1775 set +o xtrace
1776
Ian Wienand2796a822015-04-15 08:59:04 +10001777 local to_remove=""
1778 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001779 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001780
1781 # build up list of services that should be removed; i.e. they
1782 # begin with "-"
1783 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001784 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001785 to_remove+=",${service#-}"
1786 else
1787 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001788 fi
1789 done
Ian Wienand2796a822015-04-15 08:59:04 +10001790
1791 # go through the service list. if this service appears in the "to
1792 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001793 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001794
1795 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001796}
1797
Chris Dentc6d47012015-10-09 14:57:05 +00001798# disable_service() prepares the services passed as argument to be
1799# removed from the ``ENABLED_SERVICES`` list, if they are present.
Dean Troyerdff49a22014-01-30 15:37:40 -06001800#
1801# For example:
1802# disable_service rabbit
1803#
Chris Dentc6d47012015-10-09 14:57:05 +00001804# Uses global ``DISABLED_SERVICES``
Dean Troyerdff49a22014-01-30 15:37:40 -06001805# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001806function disable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001807 local xtrace
1808 xtrace=$(set +o | grep xtrace)
1809 set +o xtrace
1810
Chris Dentc6d47012015-10-09 14:57:05 +00001811 local disabled_svcs="${DISABLED_SERVICES}"
1812 local enabled_svcs=",${ENABLED_SERVICES},"
Dean Troyerdff49a22014-01-30 15:37:40 -06001813 local service
1814 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001815 disabled_svcs+=",$service"
Dean Troyerdff49a22014-01-30 15:37:40 -06001816 if is_service_enabled $service; then
Chris Dentc6d47012015-10-09 14:57:05 +00001817 enabled_svcs=${enabled_svcs//,$service,/,}
Dean Troyerdff49a22014-01-30 15:37:40 -06001818 fi
1819 done
Chris Dentc6d47012015-10-09 14:57:05 +00001820 DISABLED_SERVICES=$(_cleanup_service_list "$disabled_svcs")
1821 ENABLED_SERVICES=$(_cleanup_service_list "$enabled_svcs")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001822
1823 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001824}
1825
1826# enable_service() adds the services passed as argument to the
1827# ``ENABLED_SERVICES`` list, if they are not already present.
1828#
1829# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001830# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001831#
1832# This function does not know about the special cases
1833# for nova, glance, and neutron built into is_service_enabled().
1834# Uses global ``ENABLED_SERVICES``
1835# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001836function enable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001837 local xtrace
1838 xtrace=$(set +o | grep xtrace)
1839 set +o xtrace
1840
Dean Troyerdff49a22014-01-30 15:37:40 -06001841 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001842 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001843 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001844 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
1845 warn $LINENO "Attempt to enable_service ${service} when it has been disabled"
1846 continue
1847 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001848 if ! is_service_enabled $service; then
1849 tmpsvcs+=",$service"
1850 fi
1851 done
1852 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1853 disable_negated_services
Ian Wienand8043bfa2015-10-14 14:53:18 +11001854
1855 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001856}
1857
1858# is_service_enabled() checks if the service(s) specified as arguments are
1859# enabled by the user in ``ENABLED_SERVICES``.
1860#
1861# Multiple services specified as arguments are ``OR``'ed together; the test
1862# is a short-circuit boolean, i.e it returns on the first match.
1863#
1864# There are special cases for some 'catch-all' services::
1865# **nova** returns true if any service enabled start with **n-**
1866# **cinder** returns true if any service enabled start with **c-**
Dean Troyerdff49a22014-01-30 15:37:40 -06001867# **glance** returns true if any service enabled start with **g-**
1868# **neutron** returns true if any service enabled start with **q-**
1869# **swift** returns true if any service enabled start with **s-**
1870# **trove** returns true if any service enabled start with **tr-**
1871# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1872# **s-** services will be enabled. This will be deprecated in the future.
1873#
1874# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1875# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1876# as enabled in this case.
1877#
1878# Uses global ``ENABLED_SERVICES``
1879# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001880function is_service_enabled {
Ian Wienand433a9b12015-10-07 13:29:31 +11001881 local xtrace
1882 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001883 set +o xtrace
Ian Wienand8043bfa2015-10-14 14:53:18 +11001884
Sean Dague45917cc2014-02-24 16:09:14 -05001885 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001886 local services=$@
1887 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001888 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001889 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001890
1891 # Look for top-level 'enabled' function for this service
1892 if type is_${service}_enabled >/dev/null 2>&1; then
1893 # A function exists for this service, use it
Christian Schwede3db0aad2015-09-10 11:15:39 +00001894 is_${service}_enabled && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001895 fi
1896
1897 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1898 # are implemented
1899
John Kasperski7b3ae532016-04-24 22:56:04 -05001900 [[ ${service} == n-cell-* && ,${ENABLED_SERVICES} =~ ,"n-cell" ]] && enabled=0
1901 [[ ${service} == n-cpu-* && ,${ENABLED_SERVICES} =~ ,"n-cpu" ]] && enabled=0
1902 [[ ${service} == "nova" && ,${ENABLED_SERVICES} =~ ,"n-" ]] && enabled=0
1903 [[ ${service} == "glance" && ,${ENABLED_SERVICES} =~ ,"g-" ]] && enabled=0
1904 [[ ${service} == "neutron" && ,${ENABLED_SERVICES} =~ ,"q-" ]] && enabled=0
1905 [[ ${service} == "trove" && ,${ENABLED_SERVICES} =~ ,"tr-" ]] && enabled=0
1906 [[ ${service} == "swift" && ,${ENABLED_SERVICES} =~ ,"s-" ]] && enabled=0
1907 [[ ${service} == s-* && ,${ENABLED_SERVICES} =~ ,"swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001908 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11001909
Sean Dague45917cc2014-02-24 16:09:14 -05001910 $xtrace
1911 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001912}
1913
fumihiko kakuma8606c982015-04-13 09:55:06 +09001914# remove specified list from the input string
1915# remove_disabled_services service-list remove-list
1916function remove_disabled_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001917 local xtrace
1918 xtrace=$(set +o | grep xtrace)
1919 set +o xtrace
1920
fumihiko kakuma8606c982015-04-13 09:55:06 +09001921 local service_list=$1
1922 local remove_list=$2
1923 local service
1924 local enabled=""
1925
1926 for service in ${service_list//,/ }; do
1927 local remove
1928 local add=1
1929 for remove in ${remove_list//,/ }; do
1930 if [[ ${remove} == ${service} ]]; then
1931 add=0
1932 break
1933 fi
1934 done
1935 if [[ $add == 1 ]]; then
1936 enabled="${enabled},$service"
1937 fi
1938 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11001939
1940 $xtrace
1941
fumihiko kakuma8606c982015-04-13 09:55:06 +09001942 _cleanup_service_list "$enabled"
1943}
1944
Dean Troyerdff49a22014-01-30 15:37:40 -06001945# Toggle enable/disable_service for services that must run exclusive of each other
1946# $1 The name of a variable containing a space-separated list of services
1947# $2 The name of a variable in which to store the enabled service's name
1948# $3 The name of the service to enable
1949function use_exclusive_service {
1950 local options=${!1}
1951 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001952 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001953 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001954 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001955 for opt in $options;do
1956 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1957 done
1958 eval "$out=$selection"
1959 return 0
1960}
1961
Chris Dentc6d47012015-10-09 14:57:05 +00001962# Make sure that nothing has manipulated ENABLED_SERVICES in a way
1963# that conflicts with prior calls to disable_service.
1964# Uses global ``ENABLED_SERVICES``
1965function verify_disabled_services {
1966 local service
1967 for service in ${ENABLED_SERVICES//,/ }; do
1968 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
1969 die $LINENO "ENABLED_SERVICES directly modified to overcome 'disable_service ${service}'"
1970 fi
1971 done
1972}
1973
Dean Troyerdff49a22014-01-30 15:37:40 -06001974
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001975# System Functions
1976# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001977
1978# Only run the command if the target file (the last arg) is not on an
1979# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001980function _safe_permission_operation {
Ian Wienand433a9b12015-10-07 13:29:31 +11001981 local xtrace
1982 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001983 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001984 local args=( $@ )
1985 local last
1986 local sudo_cmd
1987 local dir_to_check
1988
1989 let last="${#args[*]} - 1"
1990
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001991 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001992 if [ ! -d "$dir_to_check" ]; then
1993 dir_to_check=`dirname "$dir_to_check"`
1994 fi
1995
1996 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001997 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001998 return 0
1999 fi
2000
2001 if [[ $TRACK_DEPENDS = True ]]; then
2002 sudo_cmd="env"
2003 else
2004 sudo_cmd="sudo"
2005 fi
2006
Sean Dague45917cc2014-02-24 16:09:14 -05002007 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002008 $sudo_cmd $@
2009}
2010
2011# Exit 0 if address is in network or 1 if address is not in network
2012# ip-range is in CIDR notation: 1.2.3.4/20
2013# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002014function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002015 local ip=$1
2016 local range=$2
2017 local masklen=${range#*/}
Ian Wienandada886d2015-10-07 14:06:26 +11002018 local network
2019 network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2020 local subnet
2021 subnet=$(maskip $ip $(cidr2netmask $masklen))
Dean Troyerdff49a22014-01-30 15:37:40 -06002022 [[ $network == $subnet ]]
2023}
2024
2025# Add a user to a group.
2026# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002027function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002028 local user=$1
2029 local group=$2
2030
Thomas Bechtolda8580852015-05-31 00:04:33 +02002031 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06002032}
2033
2034# Convert CIDR notation to a IPv4 netmask
2035# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002036function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002037 local maskpat="255 255 255 255"
2038 local maskdgt="254 252 248 240 224 192 128"
2039 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2040 echo ${1-0}.${2-0}.${3-0}.${4-0}
2041}
2042
Ian Cordasco69e3c0a2016-09-26 12:53:14 -05002043# Check if this is a valid ipv4 address string
2044function is_ipv4_address {
2045 local address=$1
Jens Harbott7617ac22017-09-19 17:43:48 +00002046 local regex='([0-9]{1,3}\.){3}[0-9]{1,3}'
Ian Cordasco69e3c0a2016-09-26 12:53:14 -05002047 # TODO(clarkb) make this more robust
2048 if [[ "$address" =~ $regex ]] ; then
2049 return 0
2050 else
2051 return 1
2052 fi
2053}
2054
Jens Harbottdc7b4292017-09-19 10:52:32 +00002055# Remove "[]" around urlquoted IPv6 addresses
2056function ipv6_unquote {
2057 echo $1 | tr -d []
2058}
2059
Dean Troyerdff49a22014-01-30 15:37:40 -06002060# Gracefully cp only if source file/dir exists
2061# cp_it source destination
2062function cp_it {
2063 if [ -e $1 ] || [ -d $1 ]; then
2064 cp -pRL $1 $2
2065 fi
2066}
2067
2068# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2069# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2070# ``localrc`` or on the command line if necessary::
2071#
2072# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2073#
2074# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2075
Ian Wienandaee18c72014-02-21 15:35:08 +11002076function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05002077 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002078 export http_proxy=$http_proxy
2079 fi
Sean Dague53753292014-12-04 19:38:15 -05002080 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002081 export https_proxy=$https_proxy
2082 fi
Sean Dague53753292014-12-04 19:38:15 -05002083 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002084 export no_proxy=$no_proxy
2085 fi
2086}
2087
2088# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002089function is_nfs_directory {
Ian Wienandada886d2015-10-07 14:06:26 +11002090 local mount_type
2091 mount_type=`stat -f -L -c %T $1`
Dean Troyerdff49a22014-01-30 15:37:40 -06002092 test "$mount_type" == "nfs"
2093}
2094
2095# Return the network portion of the given IP address using netmask
2096# netmask is in the traditional dotted-quad format
2097# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002098function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002099 local ip=$1
2100 local mask=$2
2101 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
Ian Wienandada886d2015-10-07 14:06:26 +11002102 local subnet
2103 subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
Dean Troyerdff49a22014-01-30 15:37:40 -06002104 echo $subnet
2105}
2106
Michael Turek7938d832016-04-12 14:55:21 -04002107function is_provider_network {
2108 if [ "$Q_USE_PROVIDER_NETWORKING" == "True" ]; then
2109 return 0
2110 fi
2111 return 1
2112}
2113
2114
Ian Wienand6d213df2017-08-22 16:05:16 +10002115# Return just the <major>.<minor> for the given python interpreter
2116function _get_python_version {
2117 local interp=$1
2118 local version
Sean Dague4324f4e2017-09-14 12:59:25 -06002119 # disable erroring out here, otherwise if python 3 doesn't exist we fail hard.
Javier Pena6bd49242017-09-15 15:55:00 +02002120 if [[ -x $(which $interp 2> /dev/null) ]]; then
Sean Dague4324f4e2017-09-14 12:59:25 -06002121 version=$($interp -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
2122 fi
Ian Wienand6d213df2017-08-22 16:05:16 +10002123 echo ${version}
2124}
2125
Chris Dent3a2c86a2015-05-12 13:41:25 +00002126# Return the current python as "python<major>.<minor>"
2127function python_version {
Ian Wienandada886d2015-10-07 14:06:26 +11002128 local python_version
Ian Wienand6d213df2017-08-22 16:05:16 +10002129 python_version=$(_get_python_version python2)
Chris Dent3a2c86a2015-05-12 13:41:25 +00002130 echo "python${python_version}"
2131}
2132
Ian Wienand6d213df2017-08-22 16:05:16 +10002133function python3_version {
2134 local python3_version
2135 python3_version=$(_get_python_version python3)
Doug Hellmann04178582018-06-12 15:37:00 -04002136 echo "python${python3_version}"
Ian Wienand6d213df2017-08-22 16:05:16 +10002137}
2138
2139
Dean Troyerdff49a22014-01-30 15:37:40 -06002140# Service wrapper to restart services
2141# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002142function restart_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002143 if [ -x /bin/systemctl ]; then
2144 sudo /bin/systemctl restart $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002145 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002146 sudo service $1 restart
Dean Troyerdff49a22014-01-30 15:37:40 -06002147 fi
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002148
Dean Troyerdff49a22014-01-30 15:37:40 -06002149}
2150
2151# Only change permissions of a file or directory if it is not on an
2152# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002153function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002154 _safe_permission_operation chmod $@
2155}
2156
2157# Only change ownership of a file or directory if it is not on an NFS
2158# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002159function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002160 _safe_permission_operation chown $@
2161}
2162
2163# Service wrapper to start services
2164# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002165function start_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002166 if [ -x /bin/systemctl ]; then
2167 sudo /bin/systemctl start $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002168 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002169 sudo service $1 start
Dean Troyerdff49a22014-01-30 15:37:40 -06002170 fi
2171}
2172
2173# Service wrapper to stop services
2174# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002175function stop_service {
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002176 if [ -x /bin/systemctl ]; then
2177 sudo /bin/systemctl stop $1
Dean Troyerdff49a22014-01-30 15:37:40 -06002178 else
Kashyap Chamarthy247e4462016-05-20 13:34:41 +02002179 sudo service $1 stop
Dean Troyerdff49a22014-01-30 15:37:40 -06002180 fi
2181}
2182
Attila Fazekasbcaadd62016-11-23 12:43:02 +01002183# Service wrapper to reload services
2184# If the service was not in running state it will start it
Gregory Haynes4b49e402016-08-31 18:19:51 -07002185# reload_service service-name
2186function reload_service {
2187 if [ -x /bin/systemctl ]; then
Attila Fazekasbcaadd62016-11-23 12:43:02 +01002188 sudo /bin/systemctl reload-or-restart $1
Gregory Haynes4b49e402016-08-31 18:19:51 -07002189 else
2190 sudo service $1 reload
2191 fi
2192}
2193
Sean Dague442e4e92015-06-24 13:24:02 -04002194# Test with a finite retry loop.
2195#
2196function test_with_retry {
2197 local testcmd=$1
2198 local failmsg=$2
2199 local until=${3:-10}
2200 local sleep=${4:-0.5}
2201
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002202 time_start "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002203 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2204 die $LINENO "$failmsg"
2205 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002206 time_stop "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002207}
2208
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00002209# Like sudo but forwarding http_proxy https_proxy no_proxy environment vars.
2210# If it is run as superuser then sudo is replaced by env.
2211#
2212function sudo_with_proxies {
2213 local sudo
2214
2215 [[ "$(id -u)" = "0" ]] && sudo="env" || sudo="sudo"
2216
2217 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}"\
2218 no_proxy="${no_proxy:-}" "$@"
2219}
2220
Sean Dague95c33d52015-10-07 11:05:59 -04002221# Timing infrastructure - figure out where large blocks of time are
2222# used in DevStack
2223#
2224# The timing infrastructure for DevStack is about collecting buckets
2225# of time that are spend in some subtask. For instance, that might be
2226# 'apt', 'pip', 'osc', even database migrations. We do this by a pair
2227# of functions: time_start / time_stop.
2228#
2229# These take a single parameter: $name - which specifies the name of
2230# the bucket to be accounted against. time_totals function spits out
2231# the results.
2232#
2233# Resolution is only in whole seconds, so should be used for long
2234# running activities.
2235
Sean Dagueafef8bf2017-03-06 14:07:23 -05002236declare -A -g _TIME_TOTAL
2237declare -A -g _TIME_START
2238declare -r -g _TIME_BEGIN=$(date +%s)
Sean Dague95c33d52015-10-07 11:05:59 -04002239
2240# time_start $name
2241#
2242# starts the clock for a timer by name. Errors if that clock is
2243# already started.
2244function time_start {
2245 local name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002246 local start_time=${_TIME_START[$name]}
Sean Dague95c33d52015-10-07 11:05:59 -04002247 if [[ -n "$start_time" ]]; then
2248 die $LINENO "Trying to start the clock on $name, but it's already been started"
2249 fi
Sean Dague85cf2932017-03-27 15:35:13 -04002250 _TIME_START[$name]=$(date +%s%3N)
Sean Dague95c33d52015-10-07 11:05:59 -04002251}
2252
2253# time_stop $name
2254#
2255# stops the clock for a timer by name, and accumulate that time in the
2256# global counter for that name. Errors if that clock had not
2257# previously been started.
2258function time_stop {
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002259 local name
2260 local end_time
Rob Crittenden38245da2016-05-26 17:55:14 -04002261 local elapsed_time
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002262 local total
2263 local start_time
2264
2265 name=$1
Ian Wienand908a3a92016-03-29 14:47:09 +11002266 start_time=${_TIME_START[$name]}
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002267
Sean Dague95c33d52015-10-07 11:05:59 -04002268 if [[ -z "$start_time" ]]; then
2269 die $LINENO "Trying to stop the clock on $name, but it was never started"
2270 fi
Sean Dague85cf2932017-03-27 15:35:13 -04002271 end_time=$(date +%s%3N)
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002272 elapsed_time=$(($end_time - $start_time))
Ian Wienand908a3a92016-03-29 14:47:09 +11002273 total=${_TIME_TOTAL[$name]:-0}
Sean Dague95c33d52015-10-07 11:05:59 -04002274 # reset the clock so we can start it in the future
Ian Wienand908a3a92016-03-29 14:47:09 +11002275 _TIME_START[$name]=""
2276 _TIME_TOTAL[$name]=$(($total + $elapsed_time))
Sean Dague95c33d52015-10-07 11:05:59 -04002277}
2278
Sean Dague85cf2932017-03-27 15:35:13 -04002279function oscwrap {
2280 local out
2281 local rc
2282 local start
2283 local end
2284 # Cannot use timer_start and timer_stop as we run in subshells
2285 # and those rely on modifying vars in the same process (which cannot
2286 # happen from a subshell.
2287 start=$(date +%s%3N)
2288 out=$(command openstack "$@")
2289 rc=$?
2290 end=$(date +%s%3N)
2291 echo $((end - start)) >> $OSCWRAP_TIMER_FILE
2292
2293 echo "$out"
2294 return $rc
2295}
2296
2297function install_oscwrap {
2298 # File to accumulate our timing data
2299 OSCWRAP_TIMER_FILE=$(mktemp)
2300 # Bash by default doesn't expand aliases, allow it for the aliases
2301 # we want to whitelist.
2302 shopt -s expand_aliases
2303 # Remove all aliases that might be expanded to preserve old unexpanded
2304 # behavior
2305 unalias -a
2306 # Add only the alias we want for openstack
2307 alias openstack=oscwrap
2308}
2309
2310function cleanup_oscwrap {
2311 local total=0
Stephen Finucaneffd00472018-01-18 15:12:29 +00002312 total=$(cat $OSCWRAP_TIMER_FILE | $PYTHON -c "import sys; print(sum(int(l) for l in sys.stdin))")
Sean Dague85cf2932017-03-27 15:35:13 -04002313 _TIME_TOTAL["osc"]=$total
2314 rm $OSCWRAP_TIMER_FILE
2315}
2316
Sean Dague95c33d52015-10-07 11:05:59 -04002317# time_totals
Ian Wienand908a3a92016-03-29 14:47:09 +11002318# Print out total time summary
Sean Dague95c33d52015-10-07 11:05:59 -04002319function time_totals {
Ian Wienand908a3a92016-03-29 14:47:09 +11002320 local elapsed_time
2321 local end_time
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002322 local len=20
Ian Wienand908a3a92016-03-29 14:47:09 +11002323 local xtrace
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002324 local unaccounted_time
Ian Wienand908a3a92016-03-29 14:47:09 +11002325
2326 end_time=$(date +%s)
2327 elapsed_time=$(($end_time - $_TIME_BEGIN))
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002328 unaccounted_time=$elapsed_time
Ian Wienand908a3a92016-03-29 14:47:09 +11002329
2330 # pad 1st column this far
2331 for t in ${!_TIME_TOTAL[*]}; do
2332 if [[ ${#t} -gt $len ]]; then
2333 len=${#t}
2334 fi
Sean Dague95c33d52015-10-07 11:05:59 -04002335 done
Ian Wienand908a3a92016-03-29 14:47:09 +11002336
Sean Dague85cf2932017-03-27 15:35:13 -04002337 cleanup_oscwrap
2338
Ian Wienand908a3a92016-03-29 14:47:09 +11002339 xtrace=$(set +o | grep xtrace)
2340 set +o xtrace
2341
2342 echo
2343 echo "========================="
2344 echo "DevStack Component Timing"
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002345 echo " (times are in seconds) "
Ian Wienand908a3a92016-03-29 14:47:09 +11002346 echo "========================="
Ian Wienand908a3a92016-03-29 14:47:09 +11002347 for t in ${!_TIME_TOTAL[*]}; do
2348 local v=${_TIME_TOTAL[$t]}
Sean Dague85cf2932017-03-27 15:35:13 -04002349 # because we're recording in milliseconds
2350 v=$(($v / 1000))
Ian Wienand908a3a92016-03-29 14:47:09 +11002351 printf "%-${len}s %3d\n" "$t" "$v"
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002352 unaccounted_time=$(($unaccounted_time - $v))
Ian Wienand908a3a92016-03-29 14:47:09 +11002353 done
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002354 echo "-------------------------"
2355 printf "%-${len}s %3d\n" "Unaccounted time" "$unaccounted_time"
Ian Wienand908a3a92016-03-29 14:47:09 +11002356 echo "========================="
John L. Villalovos5ad4e582017-09-07 15:33:57 -07002357 printf "%-${len}s %3d\n" "Total runtime" "$elapsed_time"
Ian Wienand908a3a92016-03-29 14:47:09 +11002358
2359 $xtrace
Sean Dague95c33d52015-10-07 11:05:59 -04002360}
Dean Troyerdff49a22014-01-30 15:37:40 -06002361
2362# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +11002363$_XTRACE_FUNCTIONS_COMMON
Dean Troyerdff49a22014-01-30 15:37:40 -06002364
2365# Local variables:
2366# mode: shell-script
2367# End: