blob: b6350cebd7438e2e155b563a8b14b08177cbf94e [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
40declare -r _DEVSTACK_FUNCTIONS_COMMON=1
41
Sean Daguecc524062014-10-01 09:06:43 -040042# Global Config Variables
43declare -A GITREPO
44declare -A GITBRANCH
45declare -A GITDIR
46
Sean Dague53753292014-12-04 19:38:15 -050047TRACK_DEPENDS=${TRACK_DEPENDS:-False}
48
Dean Troyer68162342015-05-13 15:41:03 -050049# Save these variables to .stackenv
50STACK_ENV_VARS="BASE_SQL_CONN DATA_DIR DEST ENABLED_SERVICES HOST_IP \
51 KEYSTONE_AUTH_PROTOCOL KEYSTONE_AUTH_URI KEYSTONE_SERVICE_URI \
Brian Haley180f5eb2015-06-16 13:14:31 -040052 LOGFILE OS_CACERT SERVICE_HOST SERVICE_PROTOCOL STACK_USER TLS_IP \
Brian Haley5d04db22015-06-16 13:14:31 -040053 HOST_IPV6 SERVICE_IP_VERSION"
Dean Troyer68162342015-05-13 15:41:03 -050054
55
56# Saves significant environment variables to .stackenv for later use
57# Refers to a lot of globals, only TOP_DIR and STACK_ENV_VARS are required to
58# function, the rest are simply saved and do not cause problems if they are undefined.
59# save_stackenv [tag]
60function save_stackenv {
61 local tag=${1:-""}
62 # Save some values we generated for later use
63 time_stamp=$(date "+$TIMESTAMP_FORMAT")
64 echo "# $time_stamp $tag" >$TOP_DIR/.stackenv
65 for i in $STACK_ENV_VARS; do
66 echo $i=${!i} >>$TOP_DIR/.stackenv
67 done
68}
Dean Troyerdff49a22014-01-30 15:37:40 -060069
Monty Taylor7224eec2015-09-19 11:26:18 -040070# Update/create user clouds.yaml file.
71# clouds.yaml will have
72# - A `devstack` entry for the `demo` user for the `demo` project.
73# - A `devstack-admin` entry for the `admin` user for the `admin` project.
74# write_clouds_yaml
75function write_clouds_yaml {
Monty Tayloree9bb762015-10-19 15:16:18 -040076 # The location is a variable to allow for easier refactoring later to make it
77 # overridable. There is currently no usecase where doing so makes sense, so
78 # it's not currently configurable.
Monty Taylor7224eec2015-09-19 11:26:18 -040079
Monty Tayloree9bb762015-10-19 15:16:18 -040080 CLOUDS_YAML=/etc/openstack/clouds.yaml
81
82 sudo mkdir -p $(dirname $CLOUDS_YAML)
Lenny Verkhovsky313ddae2015-10-20 11:26:34 +030083 sudo chown -R $STACK_USER /etc/openstack
Lenny Verkhovsky313ddae2015-10-20 11:26:34 +030084
Monty Tayloree9bb762015-10-19 15:16:18 -040085 CA_CERT_ARG=''
86 if [ -f "$SSL_BUNDLE_FILE" ]; then
87 CA_CERT_ARG="--os-cacert $SSL_BUNDLE_FILE"
88 fi
89 $TOP_DIR/tools/update_clouds_yaml.py \
90 --file $CLOUDS_YAML \
91 --os-cloud devstack \
92 --os-region-name $REGION_NAME \
93 --os-identity-api-version 3 \
94 $CA_CERT_ARG \
95 --os-auth-url $KEYSTONE_AUTH_URI \
96 --os-username demo \
97 --os-password $ADMIN_PASSWORD \
98 --os-project-name demo
99 $TOP_DIR/tools/update_clouds_yaml.py \
100 --file $CLOUDS_YAML \
101 --os-cloud devstack-admin \
102 --os-region-name $REGION_NAME \
103 --os-identity-api-version 3 \
104 $CA_CERT_ARG \
105 --os-auth-url $KEYSTONE_AUTH_URI \
106 --os-username admin \
107 --os-password $ADMIN_PASSWORD \
108 --os-project-name admin
Monty Taylor7224eec2015-09-19 11:26:18 -0400109}
110
Ian Wienande82bac02015-08-25 14:29:08 +1000111# trueorfalse <True|False> <VAR>
112#
113# Normalize config-value provided in variable VAR to either "True" or
114# "False". If VAR is unset (i.e. $VAR evaluates as empty), the value
115# of the second argument will be used as the default value.
116#
117# Accepts as False: 0 no No NO false False FALSE
118# Accepts as True: 1 yes Yes YES true True TRUE
119#
120# usage:
121# VAL=$(trueorfalse False VAL)
Ian Wienandaee18c72014-02-21 15:35:08 +1100122function trueorfalse {
Ian Wienand433a9b12015-10-07 13:29:31 +1100123 local xtrace
124 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -0500125 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600126
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900127 local default=$1
Ian Wienande82bac02015-08-25 14:29:08 +1000128
129 if [ -z $2 ]; then
130 die $LINENO "variable to normalize required"
131 fi
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900132 local testval=${!2:-}
133
134 case "$testval" in
135 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
136 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
137 * ) echo "$default" ;;
138 esac
139
Sean Dague45917cc2014-02-24 16:09:14 -0500140 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600141}
142
Attila Fazekas1bd79592015-02-24 14:06:56 +0100143function isset {
144 [[ -v "$1" ]]
145}
Dean Troyerdff49a22014-01-30 15:37:40 -0600146
Dean Troyer68162342015-05-13 15:41:03 -0500147
Dean Troyerdff49a22014-01-30 15:37:40 -0600148# Control Functions
149# =================
150
151# Prints backtrace info
152# filename:lineno:function
153# backtrace level
154function backtrace {
155 local level=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100156 local deep
157 deep=$((${#BASH_SOURCE[@]} - 1))
Dean Troyerdff49a22014-01-30 15:37:40 -0600158 echo "[Call Trace]"
159 while [ $level -le $deep ]; do
160 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
161 deep=$((deep - 1))
162 done
163}
164
165# Prints line number and "message" then exits
166# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100167function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600168 local exitcode=$?
169 set +o xtrace
170 local line=$1; shift
171 if [ $exitcode == 0 ]; then
172 exitcode=1
173 fi
174 backtrace 2
175 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600176 # Give buffers a second to flush
177 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600178 exit $exitcode
179}
180
181# Checks an environment variable is not set or has length 0 OR if the
182# exit code is non-zero and prints "message" and exits
183# NOTE: env-var is the variable name without a '$'
184# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100185function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600186 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100187 local xtrace
188 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600189 set +o xtrace
190 local line=$1; shift
191 local evar=$1; shift
192 if ! is_set $evar || [ $exitcode != 0 ]; then
193 die $line "$*"
194 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500195 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600196}
197
Sean Dague1de9e332015-10-07 08:46:13 -0400198function deprecated {
199 local text=$1
200 DEPRECATED_TEXT+="\n$text"
201 echo "WARNING: $text"
202}
203
Dean Troyerdff49a22014-01-30 15:37:40 -0600204# Prints line number and "message" in error format
205# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100206function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600207 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100208 local xtrace
209 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600210 set +o xtrace
211 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
212 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600213 if [[ -n ${LOGDIR} ]]; then
214 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600215 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500216 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600217 return $exitcode
218}
219
220# Checks an environment variable is not set or has length 0 OR if the
221# exit code is non-zero and prints "message"
222# NOTE: env-var is the variable name without a '$'
223# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100224function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600225 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100226 local xtrace
227 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600228 set +o xtrace
229 local line=$1; shift
230 local evar=$1; shift
231 if ! is_set $evar || [ $exitcode != 0 ]; then
232 err $line "$*"
233 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500234 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600235 return $exitcode
236}
237
238# Exit after outputting a message about the distribution not being supported.
239# exit_distro_not_supported [optional-string-telling-what-is-missing]
240function exit_distro_not_supported {
241 if [[ -z "$DISTRO" ]]; then
242 GetDistro
243 fi
244
245 if [ $# -gt 0 ]; then
246 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
247 else
248 die $LINENO "Support for $DISTRO is incomplete."
249 fi
250}
251
252# Test if the named environment variable is set and not zero length
253# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100254function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600255 local var=\$"$1"
256 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
257}
258
259# Prints line number and "message" in warning format
260# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100261function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600262 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100263 local xtrace
264 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600265 set +o xtrace
266 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Sean Daguee4af9292015-04-28 08:57:57 -0400267 echo $msg
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500268 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600269 return $exitcode
270}
271
272
273# Distro Functions
274# ================
275
276# Determine OS Vendor, Release and Update
277# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
278# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500279# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
280# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
281# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
282# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
283# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500284os_VENDOR=""
285os_RELEASE=""
286os_UPDATE=""
287os_PACKAGE=""
288os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500289
Dean Troyerdff49a22014-01-30 15:37:40 -0600290# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100291function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500292
Dean Troyerdff49a22014-01-30 15:37:40 -0600293 # Figure out which vendor we are
294 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
295 # OS/X
296 os_VENDOR=`sw_vers -productName`
297 os_RELEASE=`sw_vers -productVersion`
298 os_UPDATE=${os_RELEASE##*.}
299 os_RELEASE=${os_RELEASE%.*}
300 os_PACKAGE=""
301 if [[ "$os_RELEASE" =~ "10.7" ]]; then
302 os_CODENAME="lion"
303 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
304 os_CODENAME="snow leopard"
305 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
306 os_CODENAME="leopard"
307 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
308 os_CODENAME="tiger"
309 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
310 os_CODENAME="panther"
311 else
312 os_CODENAME=""
313 fi
314 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
315 os_VENDOR=$(lsb_release -i -s)
316 os_RELEASE=$(lsb_release -r -s)
317 os_UPDATE=""
318 os_PACKAGE="rpm"
319 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
320 os_PACKAGE="deb"
321 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
322 lsb_release -d -s | grep -q openSUSE
323 if [[ $? -eq 0 ]]; then
324 os_VENDOR="openSUSE"
325 fi
326 elif [[ $os_VENDOR == "openSUSE project" ]]; then
327 os_VENDOR="openSUSE"
328 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
329 os_VENDOR="Red Hat"
330 fi
331 os_CODENAME=$(lsb_release -c -s)
332 elif [[ -r /etc/redhat-release ]]; then
333 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
334 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
335 # CentOS release 5.5 (Final)
336 # CentOS Linux release 6.0 (Final)
337 # Fedora release 16 (Verne)
338 # XenServer release 6.2.0-70446c (xenenterprise)
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700339 # Oracle Linux release 7
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300340 # CloudLinux release 7.1
Dean Troyerdff49a22014-01-30 15:37:40 -0600341 os_CODENAME=""
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300342 for r in "Red Hat" CentOS Fedora XenServer CloudLinux; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600343 os_VENDOR=$r
344 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
345 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
346 os_CODENAME=${ver#*|}
347 os_RELEASE=${ver%|*}
348 os_UPDATE=${os_RELEASE##*.}
349 os_RELEASE=${os_RELEASE%.*}
350 break
351 fi
352 os_VENDOR=""
353 done
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700354 if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
355 os_VENDOR=OracleLinux
356 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600357 os_PACKAGE="rpm"
358 elif [[ -r /etc/SuSE-release ]]; then
359 for r in openSUSE "SUSE Linux"; do
360 if [[ "$r" = "SUSE Linux" ]]; then
361 os_VENDOR="SUSE LINUX"
362 else
363 os_VENDOR=$r
364 fi
365
366 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
367 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
368 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
369 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
370 break
371 fi
372 os_VENDOR=""
373 done
374 os_PACKAGE="rpm"
375 # If lsb_release is not installed, we should be able to detect Debian OS
376 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
377 os_VENDOR="Debian"
378 os_PACKAGE="deb"
379 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
380 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
381 fi
382 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
383}
384
385# Translate the OS version values into common nomenclature
386# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500387declare DISTRO
388
Ian Wienandaee18c72014-02-21 15:35:08 +1100389function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600390 GetOSVersion
391 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
392 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
393 DISTRO=$os_CODENAME
394 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
395 # For Fedora, just use 'f' and the release
396 DISTRO="f$os_RELEASE"
397 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
398 DISTRO="opensuse-$os_RELEASE"
399 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
400 # For SLE, also use the service pack
401 if [[ -z "$os_UPDATE" ]]; then
402 DISTRO="sle${os_RELEASE}"
403 else
404 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
405 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530406 elif [[ "$os_VENDOR" =~ (Red Hat) || \
407 "$os_VENDOR" =~ (CentOS) || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700408 "$os_VENDOR" =~ (OracleLinux) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600409 # Drop the . release as we assume it's compatible
410 DISTRO="rhel${os_RELEASE::1}"
411 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
412 DISTRO="xs$os_RELEASE"
Alexander Schmidta5ea08b2015-12-02 15:30:44 +0000413 elif [[ "$os_VENDOR" =~ (kvmibm) ]]; then
414 DISTRO="${os_VENDOR}${os_RELEASE::1}"
Dean Troyerdff49a22014-01-30 15:37:40 -0600415 else
416 # Catch-all for now is Vendor + Release + Update
417 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
418 fi
419 export DISTRO
420}
421
422# Utility function for checking machine architecture
423# is_arch arch-type
424function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500425 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600426}
427
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700428# Determine if current distribution is an Oracle distribution
429# is_oraclelinux
430function is_oraclelinux {
431 if [[ -z "$os_VENDOR" ]]; then
432 GetOSVersion
433 fi
434
435 [ "$os_VENDOR" = "OracleLinux" ]
436}
437
438
Dean Troyerdff49a22014-01-30 15:37:40 -0600439# Determine if current distribution is a Fedora-based distribution
440# (Fedora, RHEL, CentOS, etc).
441# is_fedora
442function is_fedora {
443 if [[ -z "$os_VENDOR" ]]; then
444 GetOSVersion
445 fi
446
anju Tiwari6c639c92014-07-15 18:11:54 +0530447 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300448 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ] || \
Alexander Schmidta5ea08b2015-12-02 15:30:44 +0000449 [ "$os_VENDOR" = "CloudLinux" ] || [ "$os_VENDOR" = "kvmibm" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600450}
451
452
453# Determine if current distribution is a SUSE-based distribution
454# (openSUSE, SLE).
455# is_suse
456function is_suse {
457 if [[ -z "$os_VENDOR" ]]; then
458 GetOSVersion
459 fi
460
461 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
462}
463
464
465# Determine if current distribution is an Ubuntu-based distribution
466# It will also detect non-Ubuntu but Debian-based distros
467# is_ubuntu
468function is_ubuntu {
469 if [[ -z "$os_PACKAGE" ]]; then
470 GetOSVersion
471 fi
472 [ "$os_PACKAGE" = "deb" ]
473}
474
475
476# Git Functions
477# =============
478
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600479# Returns openstack release name for a given branch name
480# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100481function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600482 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700483 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600484 echo ${branch#*/}
485 else
486 echo "master"
487 fi
488}
489
Dean Troyerdff49a22014-01-30 15:37:40 -0600490# git clone only if directory doesn't exist already. Since ``DEST`` might not
491# be owned by the installation user, we create the directory and change the
492# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500493# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
494# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600495# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500496# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600497# git_clone remote dest-dir branch
498function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500499 local git_remote=$1
500 local git_dest=$2
501 local git_ref=$3
Ian Wienandada886d2015-10-07 14:06:26 +1100502 local orig_dir
503 orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200504 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500505
Sean Dague53753292014-12-04 19:38:15 -0500506 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800507 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200508 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
509 fi
510
Dean Troyerdff49a22014-01-30 15:37:40 -0600511 if [[ "$OFFLINE" = "True" ]]; then
512 echo "Running in offline mode, clones already exist"
513 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500514 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600515 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400516 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600517 return
518 fi
519
Dean Troyer50cda692014-07-25 11:57:20 -0500520 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600521 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500522 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700523 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
524 echo "The $git_dest project was not found; if this is a gate job, add"
525 echo "the project to the \$PROJECTS variable in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600526 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700527 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200528 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600529 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500530 cd $git_dest
531 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600532 else
533 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500534 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700535 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
536 echo "The $git_dest project was not found; if this is a gate job, add"
537 echo "the project to the \$PROJECTS variable in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600538 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700539 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200540 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500541 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600542 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500543 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600544 elif [[ "$RECLONE" = "True" ]]; then
545 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500546 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600547 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500548 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100549 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600550 # remove the existing ignored files (like pyc) as they cause breakage
551 # (due to the py files having older timestamps than our pyc, so python
552 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500553 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600554
Dean Troyer50cda692014-07-25 11:57:20 -0500555 # handle git_ref accordingly to type (tag, branch)
556 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
557 git_update_tag $git_ref
558 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
559 git_update_branch $git_ref
560 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
561 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600562 else
Dean Troyer50cda692014-07-25 11:57:20 -0500563 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600564 fi
565
566 fi
567 fi
568
569 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500570 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600571 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400572 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600573}
574
Sean Daguecc524062014-10-01 09:06:43 -0400575# A variation on git clone that lets us specify a project by it's
576# actual name, like oslo.config. This is exceptionally useful in the
577# library installation case
578function git_clone_by_name {
579 local name=$1
580 local repo=${GITREPO[$name]}
581 local dir=${GITDIR[$name]}
582 local branch=${GITBRANCH[$name]}
583 git_clone $repo $dir $branch
584}
585
586
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100587# git can sometimes get itself infinitely stuck with transient network
588# errors or other issues with the remote end. This wraps git in a
589# timeout/retry loop and is intended to watch over non-local git
590# processes that might hang. GIT_TIMEOUT, if set, is passed directly
591# to timeout(1); otherwise the default value of 0 maintains the status
592# quo of waiting forever.
593# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100594function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100595 local count=0
596 local timeout=0
597
598 if [[ -n "${GIT_TIMEOUT}" ]]; then
599 timeout=${GIT_TIMEOUT}
600 fi
601
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900602 time_start "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100603 until timeout -s SIGINT ${timeout} git "$@"; do
604 # 124 is timeout(1)'s special return code when it reached the
605 # timeout; otherwise assume fatal failure
606 if [[ $? -ne 124 ]]; then
607 die $LINENO "git call failed: [git $@]"
608 fi
609
610 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400611 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100612 if [ $count -eq 3 ]; then
613 die $LINENO "Maximum of 3 git retries reached"
614 fi
615 sleep 5
616 done
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900617 time_stop "git_timed"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100618}
619
Dean Troyerdff49a22014-01-30 15:37:40 -0600620# git update using reference as a branch.
621# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100622function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500623 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600624
Dean Troyer50cda692014-07-25 11:57:20 -0500625 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600626 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500627 git branch -D $git_branch || true
628 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600629}
630
631# git update using reference as a branch.
632# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100633function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500634 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600635
Dean Troyer50cda692014-07-25 11:57:20 -0500636 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600637}
638
639# git update using reference as a tag. Be careful editing source at that repo
640# as working copy will be in a detached mode
641# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100642function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500643 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600644
Dean Troyer50cda692014-07-25 11:57:20 -0500645 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600646 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500647 git_timed fetch origin tag $git_tag
648 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600649}
650
651
652# OpenStack Functions
653# ===================
654
655# Get the default value for HOST_IP
656# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100657function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600658 local fixed_range=$1
659 local floating_range=$2
660 local host_ip_iface=$3
661 local host_ip=$4
Brian Haley180f5eb2015-06-16 13:14:31 -0400662 local af=$5
Dean Troyerdff49a22014-01-30 15:37:40 -0600663
Dean Troyerdff49a22014-01-30 15:37:40 -0600664 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
665 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
666 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100667 # Find the interface used for the default route
Brian Haley180f5eb2015-06-16 13:14:31 -0400668 host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
Ian Wienandada886d2015-10-07 14:06:26 +1100669 local host_ips
670 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 -0500671 local ip
672 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600673 # Attempt to filter out IP addresses that are part of the fixed and
674 # floating range. Note that this method only works if the ``netaddr``
675 # python library is installed. If it is not installed, an error
676 # will be printed and the first IP from the interface will be used.
677 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
678 # address.
Brian Haley180f5eb2015-06-16 13:14:31 -0400679 if [[ "$af" == "inet6" ]]; then
680 host_ip=$ip
681 break;
682 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500683 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
684 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600685 break;
686 fi
687 done
688 fi
689 echo $host_ip
690}
691
Attila Fazekasf71b5002014-05-28 09:52:22 +0200692# Generates hex string from ``size`` byte of pseudo random data
693# generate_hex_string size
694function generate_hex_string {
695 local size=$1
696 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
697}
698
Dean Troyerdff49a22014-01-30 15:37:40 -0600699# Grab a numbered field from python prettytable output
700# Fields are numbered starting with 1
701# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
702# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100703function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500704 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600705 while read data; do
706 if [ "$1" -lt 0 ]; then
707 field="(\$(NF$1))"
708 else
709 field="\$$(($1 + 1))"
710 fi
711 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
712 done
713}
714
yuntongjinf26deea2015-02-28 10:50:34 +0800715# install default policy
716# copy over a default policy.json and policy.d for projects
717function install_default_policy {
718 local project=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100719 local project_uc
720 project_uc=$(echo $1|tr a-z A-Z)
yuntongjinf26deea2015-02-28 10:50:34 +0800721 local conf_dir="${project_uc}_CONF_DIR"
722 # eval conf dir to get the variable
723 conf_dir="${!conf_dir}"
724 local project_dir="${project_uc}_DIR"
725 # eval project dir to get the variable
726 project_dir="${!project_dir}"
727 local sample_conf_dir="${project_dir}/etc/${project}"
728 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
729
730 # first copy any policy.json
731 cp -p $sample_conf_dir/policy.json $conf_dir
732 # then optionally copy over policy.d
733 if [[ -d $sample_policy_dir ]]; then
734 cp -r $sample_policy_dir $conf_dir/policy.d
735 fi
736}
737
Dean Troyerdff49a22014-01-30 15:37:40 -0600738# Add a policy to a policy.json file
739# Do nothing if the policy already exists
740# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100741function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600742 local policy_file=$1
743 local policy_name=$2
744 local policy_perm=$3
745
746 if grep -q ${policy_name} ${policy_file}; then
747 echo "Policy ${policy_name} already exists in ${policy_file}"
748 return
749 fi
750
751 # Add a terminating comma to policy lines without one
752 # Remove the closing '}' and all lines following to the end-of-file
Ian Wienandada886d2015-10-07 14:06:26 +1100753 local tmpfile
754 tmpfile=$(mktemp)
Dean Troyerdff49a22014-01-30 15:37:40 -0600755 uniq ${policy_file} | sed -e '
756 s/]$/],/
757 /^[}]/,$d
758 ' > ${tmpfile}
759
760 # Append policy and closing brace
761 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
762 echo "}" >>${tmpfile}
763
764 mv ${tmpfile} ${policy_file}
765}
766
Alistair Coles24779f62014-10-15 18:57:59 +0100767# Gets or creates a domain
768# Usage: get_or_create_domain <name> <description>
769function get_or_create_domain {
Ian Wienand11d276c2015-07-02 09:34:34 +1000770 local domain_id
Alistair Coles24779f62014-10-15 18:57:59 +0100771 # Gets domain id
Ian Wienand11d276c2015-07-02 09:34:34 +1000772 domain_id=$(
Alistair Coles24779f62014-10-15 18:57:59 +0100773 # Gets domain id
Steve Martinelli050a0d52015-09-06 22:03:54 +0000774 openstack domain show $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100775 -f value -c id 2>/dev/null ||
776 # Creates new domain
Steve Martinelli050a0d52015-09-06 22:03:54 +0000777 openstack domain create $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100778 --description "$2" \
779 -f value -c id
780 )
781 echo $domain_id
782}
783
Steve Martinellib74e01c2014-12-18 01:35:35 -0500784# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000785# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500786function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500787 local desc="${3:-}"
Ian Wienand11d276c2015-07-02 09:34:34 +1000788 local group_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500789 # Gets group id
Ian Wienand11d276c2015-07-02 09:34:34 +1000790 group_id=$(
Steve Martinellib74e01c2014-12-18 01:35:35 -0500791 # Creates new group with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000792 openstack group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000793 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500794 -f value -c id
795 )
796 echo $group_id
797}
798
Bartosz Górski0abde392014-02-28 14:15:19 +0100799# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000800# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100801function get_or_create_user {
Ian Wienand11d276c2015-07-02 09:34:34 +1000802 local user_id
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000803 if [[ ! -z "$4" ]]; then
804 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200805 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500806 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200807 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100808 # Gets user id
Ian Wienand11d276c2015-07-02 09:34:34 +1000809 user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500810 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000811 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100812 $1 \
813 --password "$2" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000814 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500815 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500816 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100817 -f value -c id
818 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500819 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100820}
821
822# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000823# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100824function get_or_create_project {
Ian Wienand11d276c2015-07-02 09:34:34 +1000825 local project_id
826 project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500827 # Creates new project with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000828 openstack project create $1 \
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000829 --domain=$2 \
830 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100831 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500832 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100833}
834
835# Gets or creates role
836# Usage: get_or_create_role <name>
837function get_or_create_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000838 local role_id
839 role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500840 # Creates role with --or-show
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000841 openstack role create $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000842 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100843 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500844 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100845}
846
Jamie Lennox9b215db2015-02-10 18:19:57 +1100847# Gets or adds user role to project
848# Usage: get_or_add_user_project_role <role> <user> <project>
849function get_or_add_user_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000850 local user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100851 # Gets user role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000852 user_role_id=$(openstack role list \
Steve Martinelli5541a612015-01-19 15:58:49 -0500853 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100854 --column "ID" \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000855 --project $3 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100856 --column "Name" \
857 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500858 if [[ -z "$user_role_id" ]]; then
Roxana Gherle50821be2015-09-22 10:52:46 -0700859 # Adds role to user and get it
860 openstack role add $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100861 --user $2 \
Steve Martinelli050a0d52015-09-06 22:03:54 +0000862 --project $3
Roxana Gherle50821be2015-09-22 10:52:46 -0700863 user_role_id=$(openstack role list \
864 --user $2 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700865 --column "ID" \
866 --project $3 \
867 --column "Name" \
868 | grep " $1 " | get_field 1)
Bartosz Górski0abde392014-02-28 14:15:19 +0100869 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500870 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100871}
872
Steve Martinelli4ce859a2015-12-20 01:27:30 -0500873# Gets or adds user role to domain
874# Usage: get_or_add_user_domain_role <role> <user> <domain>
875function get_or_add_user_domain_role {
876 local user_role_id
877 # Gets user role id
878 user_role_id=$(openstack role list \
879 --user $2 \
880 --column "ID" \
881 --domain $3 \
882 --column "Name" \
883 | grep " $1 " | get_field 1)
884 if [[ -z "$user_role_id" ]]; then
885 # Adds role to user and get it
886 openstack role add $1 \
887 --user $2 \
888 --domain $3
889 user_role_id=$(openstack role list \
890 --user $2 \
891 --column "ID" \
892 --domain $3 \
893 --column "Name" \
894 | grep " $1 " | get_field 1)
895 fi
896 echo $user_role_id
897}
898
Roxana Gherle59c63772015-09-09 18:22:31 -0700899# Gets or adds user role to domain
900# Usage: get_or_add_user_domain_role <role> <user> <domain>
901function get_or_add_user_domain_role {
902 local user_role_id
903 # Gets user role id
904 user_role_id=$(openstack role list \
905 --user $2 \
906 --os-url=$KEYSTONE_SERVICE_URI_V3 \
907 --os-identity-api-version=3 \
908 --column "ID" \
909 --domain $3 \
910 --column "Name" \
911 | grep " $1 " | get_field 1)
912 if [[ -z "$user_role_id" ]]; then
913 # Adds role to user and get it
914 openstack role add $1 \
915 --user $2 \
916 --domain $3 \
917 --os-url=$KEYSTONE_SERVICE_URI_V3 \
918 --os-identity-api-version=3
919 user_role_id=$(openstack role list \
920 --user $2 \
921 --os-url=$KEYSTONE_SERVICE_URI_V3 \
922 --os-identity-api-version=3 \
923 --column "ID" \
924 --domain $3 \
925 --column "Name" \
926 | grep " $1 " | get_field 1)
927 fi
928 echo $user_role_id
929}
930
Steve Martinelli4599fd12015-03-12 21:30:58 -0400931# Gets or adds group role to project
932# Usage: get_or_add_group_project_role <role> <group> <project>
933function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000934 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -0400935 # Gets group role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000936 group_role_id=$(openstack role list \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400937 --group $2 \
938 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000939 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400940 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000941 # Adds role to group and get it
942 openstack role add $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000943 --group $2 \
944 --project $3
945 group_role_id=$(openstack role list \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400946 --group $2 \
947 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000948 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400949 fi
950 echo $group_role_id
951}
952
Bartosz Górski0abde392014-02-28 14:15:19 +0100953# Gets or creates service
954# Usage: get_or_create_service <name> <type> <description>
955function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +1000956 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100957 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +1000958 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100959 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +1000960 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100961 # Creates new service if not exists
962 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500963 $2 \
964 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100965 --description="$3" \
966 -f value -c id
967 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500968 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100969}
970
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000971# Create an endpoint with a specific interface
972# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
973function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +1000974 local endpoint_id
975 endpoint_id=$(openstack endpoint list \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000976 --service $1 \
977 --interface $2 \
978 --region $4 \
Daniel Gonzalez0ec80802015-10-30 14:25:41 +0100979 -c ID -f value)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500980 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100981 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500982 endpoint_id=$(openstack endpoint create \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000983 $1 $2 $3 --region $4 -f value -c id)
Bartosz Górski0abde392014-02-28 14:15:19 +0100984 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000985
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500986 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100987}
Dean Troyerdff49a22014-01-30 15:37:40 -0600988
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000989# Gets or creates endpoint
990# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
991function get_or_create_endpoint {
992 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
993 # creating one endpoint with multiple urls to multiple endpoints each with
994 # a different interface. To maintain the existing function interface we
995 # create 3 endpoints and return the id of the public one. In reality
996 # returning the public id will not make a lot of difference as there are no
997 # scenarios currently that use the returned id. Ideally this behaviour
998 # should be pushed out to the service setups and let them create the
999 # endpoints they need.
Ian Wienandada886d2015-10-07 14:06:26 +11001000 local public_id
1001 public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001002 _get_or_create_endpoint_with_interface $1 admin $4 $2
1003 _get_or_create_endpoint_with_interface $1 internal $5 $2
1004
1005 # return the public id to indicate success, and this is the endpoint most likely wanted
1006 echo $public_id
1007}
1008
1009# Get a URL from the identity service
1010# Usage: get_endpoint_url <service> <interface>
1011function get_endpoint_url {
1012 echo $(openstack endpoint list \
1013 --service $1 --interface $2 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +00001014 -c URL -f value)
1015}
1016
Jim Rollenhagen47367072015-12-10 14:24:00 +00001017# check if we are using ironic with hardware
1018# TODO(jroll) this is a kludge left behind when ripping ironic code
1019# out of tree, as it is used by nova and neutron.
1020# figure out a way to refactor nova/neutron code to eliminate this
1021function is_ironic_hardware {
1022 is_service_enabled ironic && [[ -n "${IRONIC_DEPLOY_DRIVER##*_ssh}" ]] && return 0
1023 return 1
1024}
1025
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001026
Dean Troyerdff49a22014-01-30 15:37:40 -06001027# Package Functions
1028# =================
1029
1030# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +11001031function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001032 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -06001033 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001034
1035 if [[ -z "$base_dir" ]]; then
1036 base_dir=$FILES
1037 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001038 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001039 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -06001040 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001041 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -06001042 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001043 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -06001044 else
1045 exit_distro_not_supported "list of packages"
1046 fi
1047 echo "$pkg_dir"
1048}
1049
Sean Dague88ee8ce2015-12-02 07:47:31 -05001050# Wrapper for ``apt-get update`` to try multiple times on the update
1051# to address bad package mirrors (which happen all the time).
1052function apt_get_update {
1053 # only do this once per run
1054 if [[ "$REPOS_UPDATED" == "True" && "$RETRY_UPDATE" != "True" ]]; then
1055 return
1056 fi
1057
1058 # bail if we are offline
1059 [[ "$OFFLINE" = "True" ]] && return
1060
1061 local sudo="sudo"
1062 [[ "$(id -u)" = "0" ]] && sudo="env"
1063
1064 # time all the apt operations
1065 time_start "apt-get-update"
1066
1067 local proxies="http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} no_proxy=${no_proxy:-} "
1068 local update_cmd="$sudo $proxies apt-get update"
1069 if ! timeout 300 sh -c "while ! $update_cmd; do sleep 30; done"; then
1070 die $LINENO "Failed to update apt repos, we're dead now"
1071 fi
1072
1073 REPOS_UPDATED=True
1074 # stop the clock
1075 time_stop "apt-get-update"
1076}
1077
Dean Troyerdff49a22014-01-30 15:37:40 -06001078# Wrapper for ``apt-get`` to set cache and proxy environment variables
1079# Uses globals ``OFFLINE``, ``*_proxy``
1080# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001081function apt_get {
Federico Ressie208d062015-11-21 11:15:39 +00001082 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +11001083 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001084 set +o xtrace
1085
Dean Troyerdff49a22014-01-30 15:37:40 -06001086 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
1087 local sudo="sudo"
1088 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -05001089
Sean Dague95c33d52015-10-07 11:05:59 -04001090 # time all the apt operations
1091 time_start "apt-get"
1092
Sean Dague45917cc2014-02-24 16:09:14 -05001093 $xtrace
Sean Dague53753292014-12-04 19:38:15 -05001094
Dean Troyerdff49a22014-01-30 15:37:40 -06001095 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -05001096 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
1097 no_proxy=${no_proxy:-} \
John L. Villalovosf568c3a2016-01-07 19:10:50 -08001098 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" < /dev/null
Federico Ressie208d062015-11-21 11:15:39 +00001099 result=$?
Sean Dague95c33d52015-10-07 11:05:59 -04001100
1101 # stop the clock
1102 time_stop "apt-get"
Federico Ressie208d062015-11-21 11:15:39 +00001103 return $result
Dean Troyerdff49a22014-01-30 15:37:40 -06001104}
1105
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001106function _parse_package_files {
1107 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -06001108
Dean Troyerdff49a22014-01-30 15:37:40 -06001109 if [[ -z "$DISTRO" ]]; then
1110 GetDistro
1111 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001112
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001113 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001114 local OIFS line package distros distro
1115 [[ -e $fname ]] || continue
1116
1117 OIFS=$IFS
1118 IFS=$'\n'
1119 for line in $(<${fname}); do
1120 if [[ $line =~ "NOPRIME" ]]; then
1121 continue
1122 fi
1123
1124 # Assume we want this package
1125 package=${line%#*}
1126 inst_pkg=1
1127
1128 # Look for # dist:xxx in comment
1129 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1130 # We are using BASH regexp matching feature.
1131 package=${BASH_REMATCH[1]}
1132 distros=${BASH_REMATCH[2]}
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001133 # In bash ${VAR,,} will lowercase VAR
Dean Troyerdff49a22014-01-30 15:37:40 -06001134 # Look for a match in the distro list
1135 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1136 # If no 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.
1159function get_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001160 local xtrace
1161 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001162 set +o xtrace
1163 local services=$@
Ian Wienandada886d2015-10-07 14:06:26 +11001164 local package_dir
1165 package_dir=$(_get_package_dir)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001166 local file_to_parse=""
1167 local service=""
1168
Ian Wienand7d515b52015-11-09 15:04:32 +11001169 if [ $# -ne 1 ]; then
1170 die $LINENO "get_packages takes a single, comma-separated argument"
1171 fi
1172
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001173 if [[ -z "$package_dir" ]]; then
1174 echo "No package directory supplied"
1175 return 1
1176 fi
1177 for service in ${services//,/ }; do
1178 # Allow individual services to specify dependencies
1179 if [[ -e ${package_dir}/${service} ]]; then
1180 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1181 fi
1182 # NOTE(sdague) n-api needs glance for now because that's where
1183 # glance client is
1184 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001185 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001186 file_to_parse="${file_to_parse} ${package_dir}/nova"
1187 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001188 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001189 file_to_parse="${file_to_parse} ${package_dir}/glance"
1190 fi
1191 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001192 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001193 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1194 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001195 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001196 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001197 file_to_parse="${file_to_parse} ${package_dir}/swift"
1198 fi
1199 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001200 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001201 file_to_parse="${file_to_parse} ${package_dir}/nova"
1202 fi
1203 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001204 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001205 file_to_parse="${file_to_parse} ${package_dir}/glance"
1206 fi
1207 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001208 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001209 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1210 fi
1211 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001212 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001213 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1214 fi
1215 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001216 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001217 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1218 fi
1219 fi
1220 done
1221 echo "$(_parse_package_files $file_to_parse)"
1222 $xtrace
1223}
1224
1225# get_plugin_packages() collects a list of package names of any type from a
1226# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1227# list is intended to be passed to a package installer such as apt or yum.
1228#
1229# Only packages required for enabled and collected plugins will included.
1230#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001231# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001232# in these prerequisite files, see get_packages() for more info.
1233function get_plugin_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001234 local xtrace
1235 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001236 set +o xtrace
1237 local files_to_parse=""
1238 local package_dir=""
1239 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1240 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
Dmitry Tantsur5979f472016-01-03 18:08:14 +01001241 files_to_parse+=" $package_dir/$plugin"
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001242 done
1243 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001244 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001245}
1246
1247# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001248# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001249# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001250function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001251 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1252 REPOS_UPDATED=${REPOS_UPDATED:-False}
1253 RETRY_UPDATE=${RETRY_UPDATE:-False}
1254
Paul Linchpiner9e179742014-07-13 22:23:00 -07001255 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001256 return 0
1257 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001258
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001259 if is_ubuntu; then
Sean Dague88ee8ce2015-12-02 07:47:31 -05001260 apt_get_update
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001261 fi
1262}
1263
1264function real_install_package {
1265 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001266 apt_get install "$@"
1267 elif is_fedora; then
1268 yum_install "$@"
1269 elif is_suse; then
1270 zypper_install "$@"
1271 else
1272 exit_distro_not_supported "installing packages"
1273 fi
1274}
1275
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001276# Distro-agnostic package installer
1277# install_package package [package ...]
1278function install_package {
1279 update_package_repo
Federico Ressiecc1f412015-12-28 15:14:13 +01001280 if ! real_install_package "$@"; then
1281 RETRY_UPDATE=True update_package_repo && real_install_package "$@"
1282 fi
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001283}
1284
Dean Troyerdff49a22014-01-30 15:37:40 -06001285# Distro-agnostic function to tell if a package is installed
1286# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001287function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001288 if [[ -z "$@" ]]; then
1289 return 1
1290 fi
1291
1292 if [[ -z "$os_PACKAGE" ]]; then
1293 GetOSVersion
1294 fi
1295
1296 if [[ "$os_PACKAGE" = "deb" ]]; then
1297 dpkg -s "$@" > /dev/null 2> /dev/null
1298 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1299 rpm --quiet -q "$@"
1300 else
1301 exit_distro_not_supported "finding if a package is installed"
1302 fi
1303}
1304
1305# Distro-agnostic package uninstaller
1306# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001307function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001308 if is_ubuntu; then
1309 apt_get purge "$@"
1310 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001311 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001312 elif is_suse; then
1313 sudo zypper rm "$@"
1314 else
1315 exit_distro_not_supported "uninstalling packages"
1316 fi
1317}
1318
1319# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001320# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001321# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001322function yum_install {
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001323 local result parse_yum_result
1324
Dean Troyerdff49a22014-01-30 15:37:40 -06001325 [[ "$OFFLINE" = "True" ]] && return
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001326
1327 time_start "yum_install"
1328
1329 # Warning: this would not work if yum output message
1330 # have been translated to another language
1331 parse_yum_result='\
1332 BEGIN { result=0 }\
1333 /^YUM_FAILED/ { exit $2 }\
1334 /^No package/ { result=1 }\
1335 //{ print }\
1336 END { exit result }'
Ian Wienandb27f16d2014-02-28 14:29:02 +11001337
1338 # The manual check for missing packages is because yum -y assumes
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001339 # missing packages are OK.
1340 # See https://bugzilla.redhat.com/show_bug.cgi?id=965567
1341 (sudo_with_proxies "${YUM:-yum}" install -y "$@" 2>&1 || echo YUM_FAILED $?) \
1342 | awk "$parse_yum_result"
1343 result=$?
Ian Wienandb27f16d2014-02-28 14:29:02 +11001344
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001345 if [ "$result" != 0 ]; then
1346 echo $LINENO "${YUM:-yum}" install failure: $result
Ian Wienandb27f16d2014-02-28 14:29:02 +11001347 fi
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00001348
1349 time_stop "yum_install"
1350
1351 return "$result"
Dean Troyerdff49a22014-01-30 15:37:40 -06001352}
1353
1354# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001355# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001356# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001357function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001358 [[ "$OFFLINE" = "True" ]] && return
1359 local sudo="sudo"
1360 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001361 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1362 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001363 zypper --non-interactive install --auto-agree-with-licenses "$@"
1364}
1365
1366
1367# Process Functions
1368# =================
1369
1370# _run_process() is designed to be backgrounded by run_process() to simulate a
1371# fork. It includes the dirty work of closing extra filehandles and preparing log
1372# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001373# from the service name.
1374# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001375# If an optional group is provided sg will be used to set the group of
1376# the command.
1377# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001378function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001379 # disable tracing through the exec redirects, it's just confusing in the logs.
1380 xtrace=$(set +o | grep xtrace)
1381 set +o xtrace
1382
Dean Troyerdff49a22014-01-30 15:37:40 -06001383 local service=$1
1384 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001385 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001386
1387 # Undo logging redirections and close the extra descriptors
1388 exec 1>&3
1389 exec 2>&3
1390 exec 3>&-
1391 exec 6>&-
1392
Philipp Marekaf0801d2015-12-09 13:51:56 +01001393 local logfile="${service}.log.${CURRENT_LOG_TIME}"
1394 local real_logfile="${LOGDIR}/${logfile}"
Dean Troyerdde41d02014-12-09 17:47:57 -06001395 if [[ -n ${LOGDIR} ]]; then
1396 exec 1>&"$real_logfile" 2>&1
Philipp Marekaf0801d2015-12-09 13:51:56 +01001397 bash -c "cd '$LOGDIR' && ln -sf '$logfile' ${service}.log"
Dean Troyerdde41d02014-12-09 17:47:57 -06001398 if [[ -n ${SCREEN_LOGDIR} ]]; then
1399 # Drop the backward-compat symlink
1400 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1401 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001402
1403 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1404 export PYTHONUNBUFFERED=1
1405 fi
1406
Sean Dague6e137ab2015-04-29 08:22:24 -04001407 # reenable xtrace before we do *real* work
1408 $xtrace
1409
Dean Troyer3159a822014-08-27 14:13:58 -05001410 # Run under ``setsid`` to force the process to become a session and group leader.
1411 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001412 if [[ -n "$group" ]]; then
1413 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1414 else
1415 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1416 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001417
1418 # Just silently exit this process
1419 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001420}
1421
1422# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1423# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001424# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001425# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001426function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001427 SCREEN_NAME=${SCREEN_NAME:-stack}
1428 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1429
1430 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1431 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1432 fi
1433
1434 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1435}
1436
1437# Find out if a process exists by partial name.
1438# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001439function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001440 local name=$1
1441 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001442 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001443 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001444 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001445}
1446
Dean Troyer3159a822014-08-27 14:13:58 -05001447# Run a single service under screen or directly
1448# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001449# If an optional group is provided sg will be used to run the
1450# command as that group.
Tony Breeds93292902015-11-09 15:45:04 +11001451# Uses globals ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001452# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001453function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001454 local service=$1
1455 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001456 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001457
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001458 time_start "run_process"
Dean Troyer3159a822014-08-27 14:13:58 -05001459 if is_service_enabled $service; then
1460 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001461 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001462 else
1463 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001464 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001465 fi
1466 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09001467 time_stop "run_process"
Dean Troyerdff49a22014-01-30 15:37:40 -06001468}
1469
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001470# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001471# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Tony Breeds93292902015-11-09 15:45:04 +11001472# ``SERVICE_DIR``, ``SCREEN_IS_LOGGING``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001473# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001474# Run a command in a shell in a screen window, if an optional group
1475# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001476function screen_process {
1477 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001478 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001479 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001480
Sean Dagueea22a4f2014-06-27 15:21:41 -04001481 SCREEN_NAME=${SCREEN_NAME:-stack}
1482 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyerdff49a22014-01-30 15:37:40 -06001483
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001484 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001485
Philipp Marekaf0801d2015-12-09 13:51:56 +01001486 local logfile="${name}.log.${CURRENT_LOG_TIME}"
1487 local real_logfile="${LOGDIR}/${logfile}"
Dean Troyerdde41d02014-12-09 17:47:57 -06001488 echo "LOGDIR: $LOGDIR"
1489 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1490 echo "log: $real_logfile"
1491 if [[ -n ${LOGDIR} ]]; then
Tony Breeds536b8c12015-11-09 10:05:37 +11001492 if [[ "$SCREEN_IS_LOGGING" == "True" ]]; then
1493 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
1494 screen -S $SCREEN_NAME -p $name -X log on
1495 fi
1496 # If logging isn't active then avoid a broken symlink
1497 touch "$real_logfile"
Philipp Marekaf0801d2015-12-09 13:51:56 +01001498 bash -c "cd '$LOGDIR' && ln -sf '$logfile' ${name}.log"
Dean Troyerdde41d02014-12-09 17:47:57 -06001499 if [[ -n ${SCREEN_LOGDIR} ]]; then
1500 # Drop the backward-compat symlink
1501 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1502 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001503 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001504
1505 # sleep to allow bash to be ready to be send the command - we are
1506 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001507 # bash isn't running by the time we send the command, nothing
1508 # happens. This sleep was added originally to handle gate runs
1509 # where we needed this to be at least 3 seconds to pass
1510 # consistently on slow clouds. Now this is configurable so that we
1511 # can determine a reasonable value for the local case which should
1512 # be much smaller.
1513 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001514
1515 NL=`echo -ne '\015'`
1516 # This fun command does the following:
1517 # - the passed server command is backgrounded
1518 # - the pid of the background process is saved in the usual place
1519 # - the server process is brought back to the foreground
1520 # - if the server process exits prematurely the fg command errors
1521 # and a message is written to stdout and the process failure file
1522 #
1523 # The pid saved can be used in stop_process() as a process group
1524 # id to kill off all child processes
1525 if [[ -n "$group" ]]; then
1526 command="sg $group '$command'"
1527 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001528
1529 # Append the process to the screen rc file
1530 screen_rc "$name" "$command"
1531
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001532 screen -S $SCREEN_NAME -p $name -X stuff "$command & echo \$! >$SERVICE_DIR/$SCREEN_NAME/${name}.pid; fg || echo \"$name failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/${name}.failure\"$NL"
Dean Troyerdff49a22014-01-30 15:37:40 -06001533}
1534
1535# Screen rc file builder
Tony Breeds536b8c12015-11-09 10:05:37 +11001536# Uses globals ``SCREEN_NAME``, ``SCREENRC``, ``SCREEN_IS_LOGGING``
Dean Troyerdff49a22014-01-30 15:37:40 -06001537# screen_rc service "command-line"
1538function screen_rc {
1539 SCREEN_NAME=${SCREEN_NAME:-stack}
1540 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1541 if [[ ! -e $SCREENRC ]]; then
1542 # Name the screen session
1543 echo "sessionname $SCREEN_NAME" > $SCREENRC
1544 # Set a reasonable statusbar
1545 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1546 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1547 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1548 echo "screen -t shell bash" >> $SCREENRC
1549 fi
1550 # If this service doesn't already exist in the screenrc file
1551 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1552 NL=`echo -ne '\015'`
1553 echo "screen -t $1 bash" >> $SCREENRC
1554 echo "stuff \"$2$NL\"" >> $SCREENRC
1555
Tony Breeds536b8c12015-11-09 10:05:37 +11001556 if [[ -n ${LOGDIR} ]] && [[ "$SCREEN_IS_LOGGING" == "True" ]]; then
Dean Troyerdde41d02014-12-09 17:47:57 -06001557 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001558 echo "log on" >>$SCREENRC
1559 fi
1560 fi
1561}
1562
1563# Stop a service in screen
1564# If a PID is available use it, kill the whole process group via TERM
1565# If screen is being used kill the screen window; this will catch processes
1566# that did not leave a PID behind
Tony Breeds93292902015-11-09 15:45:04 +11001567# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001568# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001569function screen_stop_service {
1570 local service=$1
1571
Dean Troyerdff49a22014-01-30 15:37:40 -06001572 SCREEN_NAME=${SCREEN_NAME:-stack}
1573 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyerdff49a22014-01-30 15:37:40 -06001574
Dean Troyer3159a822014-08-27 14:13:58 -05001575 if is_service_enabled $service; then
1576 # Clean up the screen window
Attila Fazekasf750a6f2015-07-01 12:17:35 +02001577 screen -S $SCREEN_NAME -p $service -X kill || true
Dean Troyer3159a822014-08-27 14:13:58 -05001578 fi
1579}
1580
1581# Stop a service process
1582# If a PID is available use it, kill the whole process group via TERM
1583# If screen is being used kill the screen window; this will catch processes
1584# that did not leave a PID behind
1585# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1586# stop_process service
1587function stop_process {
1588 local service=$1
1589
1590 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer3159a822014-08-27 14:13:58 -05001591
1592 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001593 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001594 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1595 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
Dan Smithce7246a2015-04-23 09:41:06 -07001596 # oslo.service tends to stop actually shutting down
1597 # reliably in between releases because someone believes it
1598 # is dying too early due to some inflight work they
1599 # have. This is a tension. It happens often enough we're
1600 # going to just account for it in devstack and assume it
1601 # doesn't work.
1602 #
1603 # Set OSLO_SERVICE_WORKS=True to skip this block
1604 if [[ -z "$OSLO_SERVICE_WORKS" ]]; then
1605 # TODO(danms): Remove this double-kill when we have
1606 # this fixed in all services:
1607 # https://bugs.launchpad.net/oslo-incubator/+bug/1446583
1608 sleep 1
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001609 # /bin/true because pkill on a non existent process returns an error
Dan Smithce7246a2015-04-23 09:41:06 -07001610 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid) || /bin/true
1611 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001612 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001613 fi
1614 if [[ "$USE_SCREEN" = "True" ]]; then
1615 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001616 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001617 fi
1618 fi
1619}
1620
1621# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001622# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001623# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001624function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001625 local service
1626 local failures
1627 SCREEN_NAME=${SCREEN_NAME:-stack}
1628 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1629
1630
1631 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1632 echo "No service status directory found"
1633 return
1634 fi
1635
Wei Jiangange3340f12015-09-21 17:52:14 +08001636 # Check if there is any failure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001637 # make this -o errexit safe
1638 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001639
1640 for service in $failures; do
1641 service=`basename $service`
1642 service=${service%.failure}
1643 echo "Error: Service $service is not running"
1644 done
1645
1646 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001647 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001648 fi
1649}
1650
Chris Dent2f27a0e2014-09-09 13:46:02 +01001651# Tail a log file in a screen if USE_SCREEN is true.
Tony Breeds93292902015-11-09 15:45:04 +11001652# Uses globals ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001653function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001654 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001655 local logfile=$2
1656
Chris Dent2f27a0e2014-09-09 13:46:02 +01001657 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001658 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001659 fi
1660}
1661
Dean Troyerdff49a22014-01-30 15:37:40 -06001662
Dean Troyer3159a822014-08-27 14:13:58 -05001663# Deprecated Functions
1664# --------------------
1665
1666# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1667# fork. It includes the dirty work of closing extra filehandles and preparing log
1668# files to produce the same logs as screen_it(). The log filename is derived
1669# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1670# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1671# _old_run_process service "command-line"
1672function _old_run_process {
1673 local service=$1
1674 local command="$2"
1675
1676 # Undo logging redirections and close the extra descriptors
1677 exec 1>&3
1678 exec 2>&3
1679 exec 3>&-
1680 exec 6>&-
1681
1682 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001683 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1684 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001685
1686 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1687 export PYTHONUNBUFFERED=1
1688 fi
1689
1690 exec /bin/bash -c "$command"
1691 die "$service exec failure: $command"
1692}
1693
1694# old_run_process() launches a child process that closes all file descriptors and
1695# then exec's the passed in command. This is meant to duplicate the semantics
1696# of screen_it() without screen. PIDs are written to
1697# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1698# old_run_process service "command-line"
1699function old_run_process {
1700 local service=$1
1701 local command="$2"
1702
1703 # Spawn the child process
1704 _old_run_process "$service" "$command" &
1705 echo $!
1706}
1707
1708# Compatibility for existing start_XXXX() functions
1709# Uses global ``USE_SCREEN``
1710# screen_it service "command-line"
1711function screen_it {
1712 if is_service_enabled $1; then
1713 # Append the service to the screen rc file
1714 screen_rc "$1" "$2"
1715
1716 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001717 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001718 else
1719 # Spawn directly without screen
1720 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1721 fi
1722 fi
1723}
1724
1725# Compatibility for existing stop_XXXX() functions
1726# Stop a service in screen
1727# If a PID is available use it, kill the whole process group via TERM
1728# If screen is being used kill the screen window; this will catch processes
1729# that did not leave a PID behind
1730# screen_stop service
1731function screen_stop {
1732 # Clean up the screen window
1733 stop_process $1
1734}
1735
1736
Sean Dague2c65e712014-12-18 09:44:56 -05001737# Plugin Functions
1738# =================
1739
1740DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1741
1742# enable_plugin <name> <url> [branch]
1743#
1744# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1745# ``url`` is a git url
1746# ``branch`` is a gitref. If it's not set, defaults to master
1747function enable_plugin {
1748 local name=$1
1749 local url=$2
1750 local branch=${3:-master}
1751 DEVSTACK_PLUGINS+=",$name"
1752 GITREPO[$name]=$url
1753 GITDIR[$name]=$DEST/$name
1754 GITBRANCH[$name]=$branch
1755}
1756
1757# fetch_plugins
1758#
1759# clones all plugins
1760function fetch_plugins {
1761 local plugins="${DEVSTACK_PLUGINS}"
1762 local plugin
1763
1764 # short circuit if nothing to do
1765 if [[ -z $plugins ]]; then
1766 return
1767 fi
1768
Dean Troyerdc97cb72015-03-28 08:20:50 -05001769 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001770 for plugin in ${plugins//,/ }; do
1771 git_clone_by_name $plugin
1772 done
1773}
1774
1775# load_plugin_settings
1776#
1777# Load settings from plugins in the order that they were registered
1778function load_plugin_settings {
1779 local plugins="${DEVSTACK_PLUGINS}"
1780 local plugin
1781
1782 # short circuit if nothing to do
1783 if [[ -z $plugins ]]; then
1784 return
1785 fi
1786
1787 echo "Loading plugin settings"
1788 for plugin in ${plugins//,/ }; do
1789 local dir=${GITDIR[$plugin]}
1790 # source any known settings
1791 if [[ -f $dir/devstack/settings ]]; then
1792 source $dir/devstack/settings
1793 fi
1794 done
1795}
1796
Sean Dague6e275e12015-03-26 05:54:28 -04001797# plugin_override_defaults
1798#
1799# Run an extremely early setting phase for plugins that allows default
1800# overriding of services.
1801function plugin_override_defaults {
1802 local plugins="${DEVSTACK_PLUGINS}"
1803 local plugin
1804
1805 # short circuit if nothing to do
1806 if [[ -z $plugins ]]; then
1807 return
1808 fi
1809
1810 echo "Overriding Configuration Defaults"
1811 for plugin in ${plugins//,/ }; do
1812 local dir=${GITDIR[$plugin]}
1813 # source any overrides
1814 if [[ -f $dir/devstack/override-defaults ]]; then
1815 # be really verbose that an override is happening, as it
1816 # may not be obvious if things fail later.
Atsushi SAKAI33c9a672015-11-12 19:50:00 +09001817 echo "$plugin has overridden the following defaults"
Sean Dague6e275e12015-03-26 05:54:28 -04001818 cat $dir/devstack/override-defaults
1819 source $dir/devstack/override-defaults
1820 fi
1821 done
1822}
1823
Sean Dague2c65e712014-12-18 09:44:56 -05001824# run_plugins
1825#
1826# Run the devstack/plugin.sh in all the plugin directories. These are
1827# run in registration order.
1828function run_plugins {
1829 local mode=$1
1830 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301831
1832 local plugins="${DEVSTACK_PLUGINS}"
1833 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001834 for plugin in ${plugins//,/ }; do
1835 local dir=${GITDIR[$plugin]}
1836 if [[ -f $dir/devstack/plugin.sh ]]; then
1837 source $dir/devstack/plugin.sh $mode $phase
1838 fi
1839 done
1840}
1841
1842function run_phase {
1843 local mode=$1
1844 local phase=$2
1845 if [[ -d $TOP_DIR/extras.d ]]; then
Dmitry Tantsur64be3212015-10-12 13:10:24 +02001846 local extra_plugin_file_name
1847 for extra_plugin_file_name in $TOP_DIR/extras.d/*.sh; do
Sean Dague41d01102015-12-03 08:12:23 -05001848 # NOTE(sdague): only process extras.d for the 3 explicitly
1849 # white listed elements in tree. We want these to move out
1850 # over time as well, but they are in tree, so we need to
1851 # manage that.
Jim Rollenhagen47367072015-12-10 14:24:00 +00001852 local exceptions="60-ceph.sh 80-tempest.sh"
Ian Wienand5cdee8d2015-10-19 14:17:18 +11001853 local extra
1854 extra=$(basename $extra_plugin_file_name)
Sean Dague1de9e332015-10-07 08:46:13 -04001855 if [[ ! ( $exceptions =~ "$extra" ) ]]; then
Sean Dague41d01102015-12-03 08:12:23 -05001856 warn "use of extras.d is no longer supported"
1857 warn "processing of project $extra is skipped"
1858 else
1859 [[ -r $extra_plugin_file_name ]] && source $extra_plugin_file_name $mode $phase
Sean Dague1de9e332015-10-07 08:46:13 -04001860 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001861 done
1862 fi
1863 # the source phase corresponds to settings loading in plugins
1864 if [[ "$mode" == "source" ]]; then
1865 load_plugin_settings
Chris Dentc6d47012015-10-09 14:57:05 +00001866 verify_disabled_services
Sean Dague6e275e12015-03-26 05:54:28 -04001867 elif [[ "$mode" == "override_defaults" ]]; then
1868 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001869 else
1870 run_plugins $mode $phase
1871 fi
1872}
1873
Dean Troyerdff49a22014-01-30 15:37:40 -06001874
1875# Service Functions
1876# =================
1877
1878# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1879# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001880function _cleanup_service_list {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001881 local xtrace
1882 xtrace=$(set +o | grep xtrace)
1883 set +o xtrace
1884
Dean Troyerdff49a22014-01-30 15:37:40 -06001885 echo "$1" | sed -e '
1886 s/,,/,/g;
1887 s/^,//;
1888 s/,$//
1889 '
Ian Wienand8043bfa2015-10-14 14:53:18 +11001890
1891 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001892}
1893
1894# disable_all_services() removes all current services
1895# from ``ENABLED_SERVICES`` to reset the configuration
1896# before a minimal installation
1897# Uses global ``ENABLED_SERVICES``
1898# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001899function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001900 ENABLED_SERVICES=""
1901}
1902
1903# Remove all services starting with '-'. For example, to install all default
1904# services except rabbit (rabbit) set in ``localrc``:
1905# ENABLED_SERVICES+=",-rabbit"
1906# Uses global ``ENABLED_SERVICES``
1907# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001908function disable_negated_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001909 local xtrace
1910 xtrace=$(set +o | grep xtrace)
1911 set +o xtrace
1912
Ian Wienand2796a822015-04-15 08:59:04 +10001913 local to_remove=""
1914 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001915 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001916
1917 # build up list of services that should be removed; i.e. they
1918 # begin with "-"
1919 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001920 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001921 to_remove+=",${service#-}"
1922 else
1923 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001924 fi
1925 done
Ian Wienand2796a822015-04-15 08:59:04 +10001926
1927 # go through the service list. if this service appears in the "to
1928 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001929 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001930
1931 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001932}
1933
Chris Dentc6d47012015-10-09 14:57:05 +00001934# disable_service() prepares the services passed as argument to be
1935# removed from the ``ENABLED_SERVICES`` list, if they are present.
Dean Troyerdff49a22014-01-30 15:37:40 -06001936#
1937# For example:
1938# disable_service rabbit
1939#
Chris Dentc6d47012015-10-09 14:57:05 +00001940# Uses global ``DISABLED_SERVICES``
Dean Troyerdff49a22014-01-30 15:37:40 -06001941# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001942function disable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001943 local xtrace
1944 xtrace=$(set +o | grep xtrace)
1945 set +o xtrace
1946
Chris Dentc6d47012015-10-09 14:57:05 +00001947 local disabled_svcs="${DISABLED_SERVICES}"
1948 local enabled_svcs=",${ENABLED_SERVICES},"
Dean Troyerdff49a22014-01-30 15:37:40 -06001949 local service
1950 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001951 disabled_svcs+=",$service"
Dean Troyerdff49a22014-01-30 15:37:40 -06001952 if is_service_enabled $service; then
Chris Dentc6d47012015-10-09 14:57:05 +00001953 enabled_svcs=${enabled_svcs//,$service,/,}
Dean Troyerdff49a22014-01-30 15:37:40 -06001954 fi
1955 done
Chris Dentc6d47012015-10-09 14:57:05 +00001956 DISABLED_SERVICES=$(_cleanup_service_list "$disabled_svcs")
1957 ENABLED_SERVICES=$(_cleanup_service_list "$enabled_svcs")
Ian Wienand8043bfa2015-10-14 14:53:18 +11001958
1959 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001960}
1961
1962# enable_service() adds the services passed as argument to the
1963# ``ENABLED_SERVICES`` list, if they are not already present.
1964#
1965# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001966# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001967#
1968# This function does not know about the special cases
1969# for nova, glance, and neutron built into is_service_enabled().
1970# Uses global ``ENABLED_SERVICES``
1971# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001972function enable_service {
Ian Wienand8043bfa2015-10-14 14:53:18 +11001973 local xtrace
1974 xtrace=$(set +o | grep xtrace)
1975 set +o xtrace
1976
Dean Troyerdff49a22014-01-30 15:37:40 -06001977 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001978 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001979 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001980 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
1981 warn $LINENO "Attempt to enable_service ${service} when it has been disabled"
1982 continue
1983 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001984 if ! is_service_enabled $service; then
1985 tmpsvcs+=",$service"
1986 fi
1987 done
1988 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1989 disable_negated_services
Ian Wienand8043bfa2015-10-14 14:53:18 +11001990
1991 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001992}
1993
1994# is_service_enabled() checks if the service(s) specified as arguments are
1995# enabled by the user in ``ENABLED_SERVICES``.
1996#
1997# Multiple services specified as arguments are ``OR``'ed together; the test
1998# is a short-circuit boolean, i.e it returns on the first match.
1999#
2000# There are special cases for some 'catch-all' services::
2001# **nova** returns true if any service enabled start with **n-**
2002# **cinder** returns true if any service enabled start with **c-**
Dean Troyerdff49a22014-01-30 15:37:40 -06002003# **glance** returns true if any service enabled start with **g-**
2004# **neutron** returns true if any service enabled start with **q-**
2005# **swift** returns true if any service enabled start with **s-**
2006# **trove** returns true if any service enabled start with **tr-**
2007# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
2008# **s-** services will be enabled. This will be deprecated in the future.
2009#
2010# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
2011# We also need to make sure to treat **n-cell-region** and **n-cell-child**
2012# as enabled in this case.
2013#
2014# Uses global ``ENABLED_SERVICES``
2015# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11002016function is_service_enabled {
Ian Wienand433a9b12015-10-07 13:29:31 +11002017 local xtrace
2018 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05002019 set +o xtrace
Ian Wienand8043bfa2015-10-14 14:53:18 +11002020
Sean Dague45917cc2014-02-24 16:09:14 -05002021 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002022 local services=$@
2023 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06002024 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05002025 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002026
2027 # Look for top-level 'enabled' function for this service
2028 if type is_${service}_enabled >/dev/null 2>&1; then
2029 # A function exists for this service, use it
Christian Schwede3db0aad2015-09-10 11:15:39 +00002030 is_${service}_enabled && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002031 fi
2032
2033 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
2034 # are implemented
2035
Sean Dague45917cc2014-02-24 16:09:14 -05002036 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01002037 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05002038 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05002039 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05002040 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
2041 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
2042 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
2043 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06002044 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11002045
Sean Dague45917cc2014-02-24 16:09:14 -05002046 $xtrace
2047 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06002048}
2049
fumihiko kakuma8606c982015-04-13 09:55:06 +09002050# remove specified list from the input string
2051# remove_disabled_services service-list remove-list
2052function remove_disabled_services {
Ian Wienand8043bfa2015-10-14 14:53:18 +11002053 local xtrace
2054 xtrace=$(set +o | grep xtrace)
2055 set +o xtrace
2056
fumihiko kakuma8606c982015-04-13 09:55:06 +09002057 local service_list=$1
2058 local remove_list=$2
2059 local service
2060 local enabled=""
2061
2062 for service in ${service_list//,/ }; do
2063 local remove
2064 local add=1
2065 for remove in ${remove_list//,/ }; do
2066 if [[ ${remove} == ${service} ]]; then
2067 add=0
2068 break
2069 fi
2070 done
2071 if [[ $add == 1 ]]; then
2072 enabled="${enabled},$service"
2073 fi
2074 done
Ian Wienand8043bfa2015-10-14 14:53:18 +11002075
2076 $xtrace
2077
fumihiko kakuma8606c982015-04-13 09:55:06 +09002078 _cleanup_service_list "$enabled"
2079}
2080
Dean Troyerdff49a22014-01-30 15:37:40 -06002081# Toggle enable/disable_service for services that must run exclusive of each other
2082# $1 The name of a variable containing a space-separated list of services
2083# $2 The name of a variable in which to store the enabled service's name
2084# $3 The name of the service to enable
2085function use_exclusive_service {
2086 local options=${!1}
2087 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002088 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06002089 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002090 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06002091 for opt in $options;do
2092 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
2093 done
2094 eval "$out=$selection"
2095 return 0
2096}
2097
Chris Dentc6d47012015-10-09 14:57:05 +00002098# Make sure that nothing has manipulated ENABLED_SERVICES in a way
2099# that conflicts with prior calls to disable_service.
2100# Uses global ``ENABLED_SERVICES``
2101function verify_disabled_services {
2102 local service
2103 for service in ${ENABLED_SERVICES//,/ }; do
2104 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
2105 die $LINENO "ENABLED_SERVICES directly modified to overcome 'disable_service ${service}'"
2106 fi
2107 done
2108}
2109
Dean Troyerdff49a22014-01-30 15:37:40 -06002110
Masayuki Igawaf6368d32014-02-20 13:31:26 +09002111# System Functions
2112# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06002113
2114# Only run the command if the target file (the last arg) is not on an
2115# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002116function _safe_permission_operation {
Ian Wienand433a9b12015-10-07 13:29:31 +11002117 local xtrace
2118 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05002119 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002120 local args=( $@ )
2121 local last
2122 local sudo_cmd
2123 local dir_to_check
2124
2125 let last="${#args[*]} - 1"
2126
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002127 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002128 if [ ! -d "$dir_to_check" ]; then
2129 dir_to_check=`dirname "$dir_to_check"`
2130 fi
2131
2132 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002133 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002134 return 0
2135 fi
2136
2137 if [[ $TRACK_DEPENDS = True ]]; then
2138 sudo_cmd="env"
2139 else
2140 sudo_cmd="sudo"
2141 fi
2142
Sean Dague45917cc2014-02-24 16:09:14 -05002143 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002144 $sudo_cmd $@
2145}
2146
2147# Exit 0 if address is in network or 1 if address is not in network
2148# ip-range is in CIDR notation: 1.2.3.4/20
2149# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002150function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002151 local ip=$1
2152 local range=$2
2153 local masklen=${range#*/}
Ian Wienandada886d2015-10-07 14:06:26 +11002154 local network
2155 network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2156 local subnet
2157 subnet=$(maskip $ip $(cidr2netmask $masklen))
Dean Troyerdff49a22014-01-30 15:37:40 -06002158 [[ $network == $subnet ]]
2159}
2160
2161# Add a user to a group.
2162# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002163function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002164 local user=$1
2165 local group=$2
2166
Thomas Bechtolda8580852015-05-31 00:04:33 +02002167 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06002168}
2169
2170# Convert CIDR notation to a IPv4 netmask
2171# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002172function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002173 local maskpat="255 255 255 255"
2174 local maskdgt="254 252 248 240 224 192 128"
2175 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2176 echo ${1-0}.${2-0}.${3-0}.${4-0}
2177}
2178
2179# Gracefully cp only if source file/dir exists
2180# cp_it source destination
2181function cp_it {
2182 if [ -e $1 ] || [ -d $1 ]; then
2183 cp -pRL $1 $2
2184 fi
2185}
2186
2187# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2188# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2189# ``localrc`` or on the command line if necessary::
2190#
2191# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2192#
2193# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2194
Ian Wienandaee18c72014-02-21 15:35:08 +11002195function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05002196 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002197 export http_proxy=$http_proxy
2198 fi
Sean Dague53753292014-12-04 19:38:15 -05002199 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002200 export https_proxy=$https_proxy
2201 fi
Sean Dague53753292014-12-04 19:38:15 -05002202 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002203 export no_proxy=$no_proxy
2204 fi
2205}
2206
2207# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002208function is_nfs_directory {
Ian Wienandada886d2015-10-07 14:06:26 +11002209 local mount_type
2210 mount_type=`stat -f -L -c %T $1`
Dean Troyerdff49a22014-01-30 15:37:40 -06002211 test "$mount_type" == "nfs"
2212}
2213
2214# Return the network portion of the given IP address using netmask
2215# netmask is in the traditional dotted-quad format
2216# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002217function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002218 local ip=$1
2219 local mask=$2
2220 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
Ian Wienandada886d2015-10-07 14:06:26 +11002221 local subnet
2222 subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
Dean Troyerdff49a22014-01-30 15:37:40 -06002223 echo $subnet
2224}
2225
Chris Dent3a2c86a2015-05-12 13:41:25 +00002226# Return the current python as "python<major>.<minor>"
2227function python_version {
Ian Wienandada886d2015-10-07 14:06:26 +11002228 local python_version
2229 python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
Chris Dent3a2c86a2015-05-12 13:41:25 +00002230 echo "python${python_version}"
2231}
2232
Dean Troyerdff49a22014-01-30 15:37:40 -06002233# Service wrapper to restart services
2234# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002235function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002236 if is_ubuntu; then
2237 sudo /usr/sbin/service $1 restart
2238 else
2239 sudo /sbin/service $1 restart
2240 fi
2241}
2242
2243# Only change permissions of a file or directory if it is not on an
2244# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002245function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002246 _safe_permission_operation chmod $@
2247}
2248
2249# Only change ownership of a file or directory if it is not on an NFS
2250# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002251function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002252 _safe_permission_operation chown $@
2253}
2254
2255# Service wrapper to start services
2256# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002257function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002258 if is_ubuntu; then
2259 sudo /usr/sbin/service $1 start
2260 else
2261 sudo /sbin/service $1 start
2262 fi
2263}
2264
2265# Service wrapper to stop services
2266# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002267function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002268 if is_ubuntu; then
2269 sudo /usr/sbin/service $1 stop
2270 else
2271 sudo /sbin/service $1 stop
2272 fi
2273}
2274
Sean Dague442e4e92015-06-24 13:24:02 -04002275# Test with a finite retry loop.
2276#
2277function test_with_retry {
2278 local testcmd=$1
2279 local failmsg=$2
2280 local until=${3:-10}
2281 local sleep=${4:-0.5}
2282
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002283 time_start "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002284 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2285 die $LINENO "$failmsg"
2286 fi
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +09002287 time_stop "test_with_retry"
Sean Dague442e4e92015-06-24 13:24:02 -04002288}
2289
Federico Ressi5dcbf7a2016-01-13 12:17:32 +00002290# Like sudo but forwarding http_proxy https_proxy no_proxy environment vars.
2291# If it is run as superuser then sudo is replaced by env.
2292#
2293function sudo_with_proxies {
2294 local sudo
2295
2296 [[ "$(id -u)" = "0" ]] && sudo="env" || sudo="sudo"
2297
2298 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}"\
2299 no_proxy="${no_proxy:-}" "$@"
2300}
2301
Sean Dague95c33d52015-10-07 11:05:59 -04002302# Timing infrastructure - figure out where large blocks of time are
2303# used in DevStack
2304#
2305# The timing infrastructure for DevStack is about collecting buckets
2306# of time that are spend in some subtask. For instance, that might be
2307# 'apt', 'pip', 'osc', even database migrations. We do this by a pair
2308# of functions: time_start / time_stop.
2309#
2310# These take a single parameter: $name - which specifies the name of
2311# the bucket to be accounted against. time_totals function spits out
2312# the results.
2313#
2314# Resolution is only in whole seconds, so should be used for long
2315# running activities.
2316
2317declare -A TOTAL_TIME
2318declare -A START_TIME
2319
2320# time_start $name
2321#
2322# starts the clock for a timer by name. Errors if that clock is
2323# already started.
2324function time_start {
2325 local name=$1
2326 local start_time=${START_TIME[$name]}
2327 if [[ -n "$start_time" ]]; then
2328 die $LINENO "Trying to start the clock on $name, but it's already been started"
2329 fi
2330 START_TIME[$name]=$(date +%s)
2331}
2332
2333# time_stop $name
2334#
2335# stops the clock for a timer by name, and accumulate that time in the
2336# global counter for that name. Errors if that clock had not
2337# previously been started.
2338function time_stop {
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002339 local name
2340 local end_time
2341 local elpased_time
2342 local total
2343 local start_time
2344
2345 name=$1
2346 start_time=${START_TIME[$name]}
2347
Sean Dague95c33d52015-10-07 11:05:59 -04002348 if [[ -z "$start_time" ]]; then
2349 die $LINENO "Trying to stop the clock on $name, but it was never started"
2350 fi
Ian Wienand5cdee8d2015-10-19 14:17:18 +11002351 end_time=$(date +%s)
2352 elapsed_time=$(($end_time - $start_time))
2353 total=${TOTAL_TIME[$name]:-0}
Sean Dague95c33d52015-10-07 11:05:59 -04002354 # reset the clock so we can start it in the future
2355 START_TIME[$name]=""
2356 TOTAL_TIME[$name]=$(($total + $elapsed_time))
2357}
2358
2359# time_totals
2360#
2361# prints out total time
2362function time_totals {
2363 echo
2364 echo "========================"
2365 echo "DevStack Components Timed"
2366 echo "========================"
2367 echo
2368 for t in ${!TOTAL_TIME[*]}; do
2369 local v=${TOTAL_TIME[$t]}
2370 echo "$t - $v secs"
2371 done
2372}
Dean Troyerdff49a22014-01-30 15:37:40 -06002373
2374# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +11002375$_XTRACE_FUNCTIONS_COMMON
Dean Troyerdff49a22014-01-30 15:37:40 -06002376
2377# Local variables:
2378# mode: shell-script
2379# End: