blob: 08e5e7fb358ab86c4d67a839d368a0d5a04f5d9d [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
35XTRACE=$(set +o | grep xtrace)
36set +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 {
76 # 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.
79 CLOUDS_YAML=~/.config/openstack/clouds.yaml
80
81 mkdir -p $(dirname $CLOUDS_YAML)
82
83 CA_CERT_ARG=''
84 if [ -f "$SSL_BUNDLE_FILE" ]; then
85 CA_CERT_ARG="--os-cacert $SSL_BUNDLE_FILE"
86 fi
87 $TOP_DIR/tools/update_clouds_yaml.py \
88 --file $CLOUDS_YAML \
89 --os-cloud devstack \
90 --os-region-name $REGION_NAME \
Steve Martinelli050a0d52015-09-06 22:03:54 +000091 --os-identity-api-version 3 \
Monty Taylor7224eec2015-09-19 11:26:18 -040092 $CA_CERT_ARG \
Steve Martinelli050a0d52015-09-06 22:03:54 +000093 --os-auth-url $KEYSTONE_AUTH_URI \
Monty Taylor7224eec2015-09-19 11:26:18 -040094 --os-username demo \
95 --os-password $ADMIN_PASSWORD \
96 --os-project-name demo
97 $TOP_DIR/tools/update_clouds_yaml.py \
98 --file $CLOUDS_YAML \
99 --os-cloud devstack-admin \
100 --os-region-name $REGION_NAME \
Steve Martinelli050a0d52015-09-06 22:03:54 +0000101 --os-identity-api-version 3 \
Monty Taylor7224eec2015-09-19 11:26:18 -0400102 $CA_CERT_ARG \
Steve Martinelli050a0d52015-09-06 22:03:54 +0000103 --os-auth-url $KEYSTONE_AUTH_URI \
Monty Taylor7224eec2015-09-19 11:26:18 -0400104 --os-username admin \
105 --os-password $ADMIN_PASSWORD \
106 --os-project-name admin
107}
108
Dean Troyerdff49a22014-01-30 15:37:40 -0600109# Normalize config values to True or False
110# Accepts as False: 0 no No NO false False FALSE
111# Accepts as True: 1 yes Yes YES true True TRUE
112# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100113function trueorfalse {
Ian Wienand433a9b12015-10-07 13:29:31 +1100114 local xtrace
115 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -0500116 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600117
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900118 local default=$1
119 local testval=${!2:-}
120
121 case "$testval" in
122 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
123 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
124 * ) echo "$default" ;;
125 esac
126
Sean Dague45917cc2014-02-24 16:09:14 -0500127 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600128}
129
Attila Fazekas1bd79592015-02-24 14:06:56 +0100130function isset {
131 [[ -v "$1" ]]
132}
Dean Troyerdff49a22014-01-30 15:37:40 -0600133
Dean Troyer68162342015-05-13 15:41:03 -0500134
Dean Troyerdff49a22014-01-30 15:37:40 -0600135# Control Functions
136# =================
137
138# Prints backtrace info
139# filename:lineno:function
140# backtrace level
141function backtrace {
142 local level=$1
143 local deep=$((${#BASH_SOURCE[@]} - 1))
144 echo "[Call Trace]"
145 while [ $level -le $deep ]; do
146 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
147 deep=$((deep - 1))
148 done
149}
150
151# Prints line number and "message" then exits
152# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100153function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600154 local exitcode=$?
155 set +o xtrace
156 local line=$1; shift
157 if [ $exitcode == 0 ]; then
158 exitcode=1
159 fi
160 backtrace 2
161 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600162 # Give buffers a second to flush
163 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600164 exit $exitcode
165}
166
167# Checks an environment variable is not set or has length 0 OR if the
168# exit code is non-zero and prints "message" and exits
169# NOTE: env-var is the variable name without a '$'
170# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100171function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600172 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100173 local xtrace
174 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600175 set +o xtrace
176 local line=$1; shift
177 local evar=$1; shift
178 if ! is_set $evar || [ $exitcode != 0 ]; then
179 die $line "$*"
180 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500181 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600182}
183
Sean Dague1de9e332015-10-07 08:46:13 -0400184function deprecated {
185 local text=$1
186 DEPRECATED_TEXT+="\n$text"
187 echo "WARNING: $text"
188}
189
Dean Troyerdff49a22014-01-30 15:37:40 -0600190# Prints line number and "message" in error format
191# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100192function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600193 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100194 local xtrace
195 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600196 set +o xtrace
197 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
198 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600199 if [[ -n ${LOGDIR} ]]; then
200 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600201 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500202 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600203 return $exitcode
204}
205
206# Checks an environment variable is not set or has length 0 OR if the
207# exit code is non-zero and prints "message"
208# NOTE: env-var is the variable name without a '$'
209# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100210function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600211 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100212 local xtrace
213 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600214 set +o xtrace
215 local line=$1; shift
216 local evar=$1; shift
217 if ! is_set $evar || [ $exitcode != 0 ]; then
218 err $line "$*"
219 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500220 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600221 return $exitcode
222}
223
224# Exit after outputting a message about the distribution not being supported.
225# exit_distro_not_supported [optional-string-telling-what-is-missing]
226function exit_distro_not_supported {
227 if [[ -z "$DISTRO" ]]; then
228 GetDistro
229 fi
230
231 if [ $# -gt 0 ]; then
232 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
233 else
234 die $LINENO "Support for $DISTRO is incomplete."
235 fi
236}
237
238# Test if the named environment variable is set and not zero length
239# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100240function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600241 local var=\$"$1"
242 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
243}
244
245# Prints line number and "message" in warning format
246# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100247function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600248 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100249 local xtrace
250 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600251 set +o xtrace
252 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Sean Daguee4af9292015-04-28 08:57:57 -0400253 echo $msg
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500254 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600255 return $exitcode
256}
257
258
259# Distro Functions
260# ================
261
262# Determine OS Vendor, Release and Update
263# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
264# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500265# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
266# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
267# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
268# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
269# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500270os_VENDOR=""
271os_RELEASE=""
272os_UPDATE=""
273os_PACKAGE=""
274os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500275
Dean Troyerdff49a22014-01-30 15:37:40 -0600276# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100277function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500278
Dean Troyerdff49a22014-01-30 15:37:40 -0600279 # Figure out which vendor we are
280 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
281 # OS/X
282 os_VENDOR=`sw_vers -productName`
283 os_RELEASE=`sw_vers -productVersion`
284 os_UPDATE=${os_RELEASE##*.}
285 os_RELEASE=${os_RELEASE%.*}
286 os_PACKAGE=""
287 if [[ "$os_RELEASE" =~ "10.7" ]]; then
288 os_CODENAME="lion"
289 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
290 os_CODENAME="snow leopard"
291 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
292 os_CODENAME="leopard"
293 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
294 os_CODENAME="tiger"
295 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
296 os_CODENAME="panther"
297 else
298 os_CODENAME=""
299 fi
300 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
301 os_VENDOR=$(lsb_release -i -s)
302 os_RELEASE=$(lsb_release -r -s)
303 os_UPDATE=""
304 os_PACKAGE="rpm"
305 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
306 os_PACKAGE="deb"
307 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
308 lsb_release -d -s | grep -q openSUSE
309 if [[ $? -eq 0 ]]; then
310 os_VENDOR="openSUSE"
311 fi
312 elif [[ $os_VENDOR == "openSUSE project" ]]; then
313 os_VENDOR="openSUSE"
314 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
315 os_VENDOR="Red Hat"
316 fi
317 os_CODENAME=$(lsb_release -c -s)
318 elif [[ -r /etc/redhat-release ]]; then
319 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
320 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
321 # CentOS release 5.5 (Final)
322 # CentOS Linux release 6.0 (Final)
323 # Fedora release 16 (Verne)
324 # XenServer release 6.2.0-70446c (xenenterprise)
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700325 # Oracle Linux release 7
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300326 # CloudLinux release 7.1
Dean Troyerdff49a22014-01-30 15:37:40 -0600327 os_CODENAME=""
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300328 for r in "Red Hat" CentOS Fedora XenServer CloudLinux; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600329 os_VENDOR=$r
330 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
331 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
332 os_CODENAME=${ver#*|}
333 os_RELEASE=${ver%|*}
334 os_UPDATE=${os_RELEASE##*.}
335 os_RELEASE=${os_RELEASE%.*}
336 break
337 fi
338 os_VENDOR=""
339 done
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700340 if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
341 os_VENDOR=OracleLinux
342 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600343 os_PACKAGE="rpm"
344 elif [[ -r /etc/SuSE-release ]]; then
345 for r in openSUSE "SUSE Linux"; do
346 if [[ "$r" = "SUSE Linux" ]]; then
347 os_VENDOR="SUSE LINUX"
348 else
349 os_VENDOR=$r
350 fi
351
352 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
353 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
354 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
355 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
356 break
357 fi
358 os_VENDOR=""
359 done
360 os_PACKAGE="rpm"
361 # If lsb_release is not installed, we should be able to detect Debian OS
362 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
363 os_VENDOR="Debian"
364 os_PACKAGE="deb"
365 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
366 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
367 fi
368 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
369}
370
371# Translate the OS version values into common nomenclature
372# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500373declare DISTRO
374
Ian Wienandaee18c72014-02-21 15:35:08 +1100375function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600376 GetOSVersion
377 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
378 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
379 DISTRO=$os_CODENAME
380 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
381 # For Fedora, just use 'f' and the release
382 DISTRO="f$os_RELEASE"
383 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
384 DISTRO="opensuse-$os_RELEASE"
385 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
386 # For SLE, also use the service pack
387 if [[ -z "$os_UPDATE" ]]; then
388 DISTRO="sle${os_RELEASE}"
389 else
390 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
391 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530392 elif [[ "$os_VENDOR" =~ (Red Hat) || \
393 "$os_VENDOR" =~ (CentOS) || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700394 "$os_VENDOR" =~ (OracleLinux) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600395 # Drop the . release as we assume it's compatible
396 DISTRO="rhel${os_RELEASE::1}"
397 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
398 DISTRO="xs$os_RELEASE"
399 else
400 # Catch-all for now is Vendor + Release + Update
401 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
402 fi
403 export DISTRO
404}
405
406# Utility function for checking machine architecture
407# is_arch arch-type
408function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500409 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600410}
411
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700412# Determine if current distribution is an Oracle distribution
413# is_oraclelinux
414function is_oraclelinux {
415 if [[ -z "$os_VENDOR" ]]; then
416 GetOSVersion
417 fi
418
419 [ "$os_VENDOR" = "OracleLinux" ]
420}
421
422
Dean Troyerdff49a22014-01-30 15:37:40 -0600423# Determine if current distribution is a Fedora-based distribution
424# (Fedora, RHEL, CentOS, etc).
425# is_fedora
426function is_fedora {
427 if [[ -z "$os_VENDOR" ]]; then
428 GetOSVersion
429 fi
430
anju Tiwari6c639c92014-07-15 18:11:54 +0530431 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300432 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ] || \
433 [ "$os_VENDOR" = "CloudLinux" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600434}
435
436
437# Determine if current distribution is a SUSE-based distribution
438# (openSUSE, SLE).
439# is_suse
440function is_suse {
441 if [[ -z "$os_VENDOR" ]]; then
442 GetOSVersion
443 fi
444
445 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
446}
447
448
449# Determine if current distribution is an Ubuntu-based distribution
450# It will also detect non-Ubuntu but Debian-based distros
451# is_ubuntu
452function is_ubuntu {
453 if [[ -z "$os_PACKAGE" ]]; then
454 GetOSVersion
455 fi
456 [ "$os_PACKAGE" = "deb" ]
457}
458
459
460# Git Functions
461# =============
462
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600463# Returns openstack release name for a given branch name
464# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100465function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600466 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700467 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600468 echo ${branch#*/}
469 else
470 echo "master"
471 fi
472}
473
Dean Troyerdff49a22014-01-30 15:37:40 -0600474# git clone only if directory doesn't exist already. Since ``DEST`` might not
475# be owned by the installation user, we create the directory and change the
476# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500477# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
478# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600479# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500480# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600481# git_clone remote dest-dir branch
482function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500483 local git_remote=$1
484 local git_dest=$2
485 local git_ref=$3
486 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200487 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500488
Sean Dague53753292014-12-04 19:38:15 -0500489 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800490 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200491 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
492 fi
493
Dean Troyerdff49a22014-01-30 15:37:40 -0600494 if [[ "$OFFLINE" = "True" ]]; then
495 echo "Running in offline mode, clones already exist"
496 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500497 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600498 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400499 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600500 return
501 fi
502
Dean Troyer50cda692014-07-25 11:57:20 -0500503 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600504 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500505 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700506 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
507 echo "The $git_dest project was not found; if this is a gate job, add"
508 echo "the project to the \$PROJECTS variable in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600509 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700510 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200511 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600512 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500513 cd $git_dest
514 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600515 else
516 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500517 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700518 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
519 echo "The $git_dest project was not found; if this is a gate job, add"
520 echo "the project to the \$PROJECTS variable in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600521 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700522 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200523 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500524 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600525 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500526 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600527 elif [[ "$RECLONE" = "True" ]]; then
528 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500529 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600530 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500531 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100532 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600533 # remove the existing ignored files (like pyc) as they cause breakage
534 # (due to the py files having older timestamps than our pyc, so python
535 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500536 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600537
Dean Troyer50cda692014-07-25 11:57:20 -0500538 # handle git_ref accordingly to type (tag, branch)
539 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
540 git_update_tag $git_ref
541 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
542 git_update_branch $git_ref
543 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
544 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600545 else
Dean Troyer50cda692014-07-25 11:57:20 -0500546 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600547 fi
548
549 fi
550 fi
551
552 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500553 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600554 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400555 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600556}
557
Sean Daguecc524062014-10-01 09:06:43 -0400558# A variation on git clone that lets us specify a project by it's
559# actual name, like oslo.config. This is exceptionally useful in the
560# library installation case
561function git_clone_by_name {
562 local name=$1
563 local repo=${GITREPO[$name]}
564 local dir=${GITDIR[$name]}
565 local branch=${GITBRANCH[$name]}
566 git_clone $repo $dir $branch
567}
568
569
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100570# git can sometimes get itself infinitely stuck with transient network
571# errors or other issues with the remote end. This wraps git in a
572# timeout/retry loop and is intended to watch over non-local git
573# processes that might hang. GIT_TIMEOUT, if set, is passed directly
574# to timeout(1); otherwise the default value of 0 maintains the status
575# quo of waiting forever.
576# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100577function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100578 local count=0
579 local timeout=0
580
581 if [[ -n "${GIT_TIMEOUT}" ]]; then
582 timeout=${GIT_TIMEOUT}
583 fi
584
585 until timeout -s SIGINT ${timeout} git "$@"; do
586 # 124 is timeout(1)'s special return code when it reached the
587 # timeout; otherwise assume fatal failure
588 if [[ $? -ne 124 ]]; then
589 die $LINENO "git call failed: [git $@]"
590 fi
591
592 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400593 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100594 if [ $count -eq 3 ]; then
595 die $LINENO "Maximum of 3 git retries reached"
596 fi
597 sleep 5
598 done
599}
600
Dean Troyerdff49a22014-01-30 15:37:40 -0600601# git update using reference as a branch.
602# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100603function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500604 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600605
Dean Troyer50cda692014-07-25 11:57:20 -0500606 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600607 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500608 git branch -D $git_branch || true
609 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600610}
611
612# git update using reference as a branch.
613# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100614function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500615 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600616
Dean Troyer50cda692014-07-25 11:57:20 -0500617 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600618}
619
620# git update using reference as a tag. Be careful editing source at that repo
621# as working copy will be in a detached mode
622# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100623function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500624 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600625
Dean Troyer50cda692014-07-25 11:57:20 -0500626 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600627 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500628 git_timed fetch origin tag $git_tag
629 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600630}
631
632
633# OpenStack Functions
634# ===================
635
636# Get the default value for HOST_IP
637# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100638function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600639 local fixed_range=$1
640 local floating_range=$2
641 local host_ip_iface=$3
642 local host_ip=$4
Brian Haley180f5eb2015-06-16 13:14:31 -0400643 local af=$5
Dean Troyerdff49a22014-01-30 15:37:40 -0600644
Dean Troyerdff49a22014-01-30 15:37:40 -0600645 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
646 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
647 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100648 # Find the interface used for the default route
Brian Haley180f5eb2015-06-16 13:14:31 -0400649 host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
Sean M. Collinsd20435b2015-08-25 19:24:51 -0400650 local 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 -0500651 local ip
652 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600653 # Attempt to filter out IP addresses that are part of the fixed and
654 # floating range. Note that this method only works if the ``netaddr``
655 # python library is installed. If it is not installed, an error
656 # will be printed and the first IP from the interface will be used.
657 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
658 # address.
Brian Haley180f5eb2015-06-16 13:14:31 -0400659 if [[ "$af" == "inet6" ]]; then
660 host_ip=$ip
661 break;
662 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500663 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
664 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600665 break;
666 fi
667 done
668 fi
669 echo $host_ip
670}
671
Attila Fazekasf71b5002014-05-28 09:52:22 +0200672# Generates hex string from ``size`` byte of pseudo random data
673# generate_hex_string size
674function generate_hex_string {
675 local size=$1
676 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
677}
678
Dean Troyerdff49a22014-01-30 15:37:40 -0600679# Grab a numbered field from python prettytable output
680# Fields are numbered starting with 1
681# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
682# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100683function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500684 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600685 while read data; do
686 if [ "$1" -lt 0 ]; then
687 field="(\$(NF$1))"
688 else
689 field="\$$(($1 + 1))"
690 fi
691 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
692 done
693}
694
yuntongjinf26deea2015-02-28 10:50:34 +0800695# install default policy
696# copy over a default policy.json and policy.d for projects
697function install_default_policy {
698 local project=$1
699 local project_uc=$(echo $1|tr a-z A-Z)
700 local conf_dir="${project_uc}_CONF_DIR"
701 # eval conf dir to get the variable
702 conf_dir="${!conf_dir}"
703 local project_dir="${project_uc}_DIR"
704 # eval project dir to get the variable
705 project_dir="${!project_dir}"
706 local sample_conf_dir="${project_dir}/etc/${project}"
707 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
708
709 # first copy any policy.json
710 cp -p $sample_conf_dir/policy.json $conf_dir
711 # then optionally copy over policy.d
712 if [[ -d $sample_policy_dir ]]; then
713 cp -r $sample_policy_dir $conf_dir/policy.d
714 fi
715}
716
Dean Troyerdff49a22014-01-30 15:37:40 -0600717# Add a policy to a policy.json file
718# Do nothing if the policy already exists
719# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100720function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600721 local policy_file=$1
722 local policy_name=$2
723 local policy_perm=$3
724
725 if grep -q ${policy_name} ${policy_file}; then
726 echo "Policy ${policy_name} already exists in ${policy_file}"
727 return
728 fi
729
730 # Add a terminating comma to policy lines without one
731 # Remove the closing '}' and all lines following to the end-of-file
732 local tmpfile=$(mktemp)
733 uniq ${policy_file} | sed -e '
734 s/]$/],/
735 /^[}]/,$d
736 ' > ${tmpfile}
737
738 # Append policy and closing brace
739 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
740 echo "}" >>${tmpfile}
741
742 mv ${tmpfile} ${policy_file}
743}
744
Alistair Coles24779f62014-10-15 18:57:59 +0100745# Gets or creates a domain
746# Usage: get_or_create_domain <name> <description>
747function get_or_create_domain {
Ian Wienand11d276c2015-07-02 09:34:34 +1000748 local domain_id
Alistair Coles24779f62014-10-15 18:57:59 +0100749 # Gets domain id
Ian Wienand11d276c2015-07-02 09:34:34 +1000750 domain_id=$(
Alistair Coles24779f62014-10-15 18:57:59 +0100751 # Gets domain id
Steve Martinelli050a0d52015-09-06 22:03:54 +0000752 openstack domain show $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100753 -f value -c id 2>/dev/null ||
754 # Creates new domain
Steve Martinelli050a0d52015-09-06 22:03:54 +0000755 openstack domain create $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100756 --description "$2" \
757 -f value -c id
758 )
759 echo $domain_id
760}
761
Steve Martinellib74e01c2014-12-18 01:35:35 -0500762# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000763# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500764function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500765 local desc="${3:-}"
Ian Wienand11d276c2015-07-02 09:34:34 +1000766 local group_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500767 # Gets group id
Ian Wienand11d276c2015-07-02 09:34:34 +1000768 group_id=$(
Steve Martinellib74e01c2014-12-18 01:35:35 -0500769 # Creates new group with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000770 openstack group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000771 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500772 -f value -c id
773 )
774 echo $group_id
775}
776
Bartosz Górski0abde392014-02-28 14:15:19 +0100777# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000778# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100779function get_or_create_user {
Ian Wienand11d276c2015-07-02 09:34:34 +1000780 local user_id
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000781 if [[ ! -z "$4" ]]; then
782 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200783 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500784 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200785 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100786 # Gets user id
Ian Wienand11d276c2015-07-02 09:34:34 +1000787 user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500788 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000789 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100790 $1 \
791 --password "$2" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000792 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500793 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500794 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100795 -f value -c id
796 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500797 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100798}
799
800# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000801# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100802function get_or_create_project {
Ian Wienand11d276c2015-07-02 09:34:34 +1000803 local project_id
804 project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500805 # Creates new project with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000806 openstack project create $1 \
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000807 --domain=$2 \
808 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100809 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500810 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100811}
812
813# Gets or creates role
814# Usage: get_or_create_role <name>
815function get_or_create_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000816 local role_id
817 role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500818 # Creates role with --or-show
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000819 openstack role create $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000820 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100821 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500822 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100823}
824
Jamie Lennox9b215db2015-02-10 18:19:57 +1100825# Gets or adds user role to project
826# Usage: get_or_add_user_project_role <role> <user> <project>
827function get_or_add_user_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000828 local user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100829 # Gets user role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000830 user_role_id=$(openstack role list \
Steve Martinelli5541a612015-01-19 15:58:49 -0500831 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100832 --column "ID" \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000833 --project $3 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100834 --column "Name" \
835 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500836 if [[ -z "$user_role_id" ]]; then
Roxana Gherle50821be2015-09-22 10:52:46 -0700837 # Adds role to user and get it
838 openstack role add $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100839 --user $2 \
Steve Martinelli050a0d52015-09-06 22:03:54 +0000840 --project $3
Roxana Gherle50821be2015-09-22 10:52:46 -0700841 user_role_id=$(openstack role list \
842 --user $2 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700843 --column "ID" \
844 --project $3 \
845 --column "Name" \
846 | grep " $1 " | get_field 1)
Bartosz Górski0abde392014-02-28 14:15:19 +0100847 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500848 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100849}
850
Steve Martinelli4599fd12015-03-12 21:30:58 -0400851# Gets or adds group role to project
852# Usage: get_or_add_group_project_role <role> <group> <project>
853function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000854 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -0400855 # Gets group role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000856 group_role_id=$(openstack role list \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400857 --group $2 \
858 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000859 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400860 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000861 # Adds role to group and get it
862 openstack role add $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000863 --group $2 \
864 --project $3
865 group_role_id=$(openstack role list \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400866 --group $2 \
867 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000868 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400869 fi
870 echo $group_role_id
871}
872
Bartosz Górski0abde392014-02-28 14:15:19 +0100873# Gets or creates service
874# Usage: get_or_create_service <name> <type> <description>
875function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +1000876 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100877 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +1000878 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100879 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +1000880 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100881 # Creates new service if not exists
882 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500883 $2 \
884 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100885 --description="$3" \
886 -f value -c id
887 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500888 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100889}
890
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000891# Create an endpoint with a specific interface
892# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
893function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +1000894 local endpoint_id
Daniel Gonzalez1991e752015-08-11 19:34:22 +0200895 # TODO(dgonzalez): The check of the region name, as done in the grep
896 # statement below, exists only because keystone does currently
897 # not allow filtering the region name when listing endpoints. If keystone
898 # gets support for this, the check for the region name can be removed.
899 # Related bug in keystone: https://bugs.launchpad.net/keystone/+bug/1482772
Ian Wienand11d276c2015-07-02 09:34:34 +1000900 endpoint_id=$(openstack endpoint list \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000901 --service $1 \
902 --interface $2 \
903 --region $4 \
Daniel Gonzalez1991e752015-08-11 19:34:22 +0200904 -c ID -c Region -f value | grep $4 | cut -f 1 -d " ")
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500905 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100906 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500907 endpoint_id=$(openstack endpoint create \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000908 $1 $2 $3 --region $4 -f value -c id)
Bartosz Górski0abde392014-02-28 14:15:19 +0100909 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000910
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500911 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100912}
Dean Troyerdff49a22014-01-30 15:37:40 -0600913
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000914# Gets or creates endpoint
915# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
916function get_or_create_endpoint {
917 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
918 # creating one endpoint with multiple urls to multiple endpoints each with
919 # a different interface. To maintain the existing function interface we
920 # create 3 endpoints and return the id of the public one. In reality
921 # returning the public id will not make a lot of difference as there are no
922 # scenarios currently that use the returned id. Ideally this behaviour
923 # should be pushed out to the service setups and let them create the
924 # endpoints they need.
925 local public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
926 _get_or_create_endpoint_with_interface $1 admin $4 $2
927 _get_or_create_endpoint_with_interface $1 internal $5 $2
928
929 # return the public id to indicate success, and this is the endpoint most likely wanted
930 echo $public_id
931}
932
933# Get a URL from the identity service
934# Usage: get_endpoint_url <service> <interface>
935function get_endpoint_url {
936 echo $(openstack endpoint list \
937 --service $1 --interface $2 \
938 --os-url $KEYSTONE_SERVICE_URI_V3 \
939 --os-identity-api-version=3 \
940 -c URL -f value)
941}
942
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500943
Dean Troyerdff49a22014-01-30 15:37:40 -0600944# Package Functions
945# =================
946
947# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100948function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800949 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600950 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800951
952 if [[ -z "$base_dir" ]]; then
953 base_dir=$FILES
954 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600955 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800956 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600957 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800958 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600959 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800960 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600961 else
962 exit_distro_not_supported "list of packages"
963 fi
964 echo "$pkg_dir"
965}
966
967# Wrapper for ``apt-get`` to set cache and proxy environment variables
968# Uses globals ``OFFLINE``, ``*_proxy``
969# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100970function apt_get {
Ian Wienand433a9b12015-10-07 13:29:31 +1100971 local xtrace
972 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -0500973 set +o xtrace
974
Dean Troyerdff49a22014-01-30 15:37:40 -0600975 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
976 local sudo="sudo"
977 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500978
979 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500980
Dean Troyerdff49a22014-01-30 15:37:40 -0600981 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500982 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
983 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600984 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
985}
986
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800987function _parse_package_files {
988 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600989
Dean Troyerdff49a22014-01-30 15:37:40 -0600990 if [[ -z "$DISTRO" ]]; then
991 GetDistro
992 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600993
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800994 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600995 local OIFS line package distros distro
996 [[ -e $fname ]] || continue
997
998 OIFS=$IFS
999 IFS=$'\n'
1000 for line in $(<${fname}); do
1001 if [[ $line =~ "NOPRIME" ]]; then
1002 continue
1003 fi
1004
1005 # Assume we want this package
1006 package=${line%#*}
1007 inst_pkg=1
1008
1009 # Look for # dist:xxx in comment
1010 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1011 # We are using BASH regexp matching feature.
1012 package=${BASH_REMATCH[1]}
1013 distros=${BASH_REMATCH[2]}
1014 # In bash ${VAR,,} will lowecase VAR
1015 # Look for a match in the distro list
1016 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1017 # If no match then skip this package
1018 inst_pkg=0
1019 fi
1020 fi
1021
Dean Troyerdff49a22014-01-30 15:37:40 -06001022 if [[ $inst_pkg = 1 ]]; then
1023 echo $package
1024 fi
1025 done
1026 IFS=$OIFS
1027 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001028}
1029
1030# get_packages() collects a list of package names of any type from the
1031# prerequisite files in ``files/{debs|rpms}``. The list is intended
1032# to be passed to a package installer such as apt or yum.
1033#
1034# Only packages required for the services in 1st argument will be
1035# included. Two bits of metadata are recognized in the prerequisite files:
1036#
1037# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1038# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1039# of the package to the distros listed. The distro names are case insensitive.
1040function get_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001041 local xtrace
1042 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001043 set +o xtrace
1044 local services=$@
1045 local package_dir=$(_get_package_dir)
1046 local file_to_parse=""
1047 local service=""
1048
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001049 if [[ -z "$package_dir" ]]; then
1050 echo "No package directory supplied"
1051 return 1
1052 fi
1053 for service in ${services//,/ }; do
1054 # Allow individual services to specify dependencies
1055 if [[ -e ${package_dir}/${service} ]]; then
1056 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1057 fi
1058 # NOTE(sdague) n-api needs glance for now because that's where
1059 # glance client is
1060 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001061 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001062 file_to_parse="${file_to_parse} ${package_dir}/nova"
1063 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001064 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001065 file_to_parse="${file_to_parse} ${package_dir}/glance"
1066 fi
1067 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001068 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001069 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1070 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001071 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001072 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001073 file_to_parse="${file_to_parse} ${package_dir}/swift"
1074 fi
1075 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001076 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001077 file_to_parse="${file_to_parse} ${package_dir}/nova"
1078 fi
1079 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001080 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001081 file_to_parse="${file_to_parse} ${package_dir}/glance"
1082 fi
1083 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001084 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001085 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1086 fi
1087 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001088 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001089 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1090 fi
1091 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001092 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001093 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1094 fi
1095 fi
1096 done
1097 echo "$(_parse_package_files $file_to_parse)"
1098 $xtrace
1099}
1100
1101# get_plugin_packages() collects a list of package names of any type from a
1102# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1103# list is intended to be passed to a package installer such as apt or yum.
1104#
1105# Only packages required for enabled and collected plugins will included.
1106#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001107# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001108# in these prerequisite files, see get_packages() for more info.
1109function get_plugin_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001110 local xtrace
1111 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001112 set +o xtrace
1113 local files_to_parse=""
1114 local package_dir=""
1115 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1116 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1117 files_to_parse+="$package_dir/$plugin"
1118 done
1119 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001120 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001121}
1122
1123# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001124# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001125# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001126function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001127 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1128 REPOS_UPDATED=${REPOS_UPDATED:-False}
1129 RETRY_UPDATE=${RETRY_UPDATE:-False}
1130
Paul Linchpiner9e179742014-07-13 22:23:00 -07001131 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001132 return 0
1133 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001134
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001135 if is_ubuntu; then
Ian Wienand433a9b12015-10-07 13:29:31 +11001136 local xtrace
1137 xtrace=$(set +o | grep xtrace)
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001138 set +o xtrace
1139 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1140 # if there are transient errors pulling the updates, that's fine.
1141 # It may be secondary repositories that we don't really care about.
1142 apt_get update || /bin/true
1143 REPOS_UPDATED=True
1144 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001145 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001146 fi
1147}
1148
1149function real_install_package {
1150 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001151 apt_get install "$@"
1152 elif is_fedora; then
1153 yum_install "$@"
1154 elif is_suse; then
1155 zypper_install "$@"
1156 else
1157 exit_distro_not_supported "installing packages"
1158 fi
1159}
1160
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001161# Distro-agnostic package installer
1162# install_package package [package ...]
1163function install_package {
1164 update_package_repo
1165 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1166}
1167
Dean Troyerdff49a22014-01-30 15:37:40 -06001168# Distro-agnostic function to tell if a package is installed
1169# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001170function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001171 if [[ -z "$@" ]]; then
1172 return 1
1173 fi
1174
1175 if [[ -z "$os_PACKAGE" ]]; then
1176 GetOSVersion
1177 fi
1178
1179 if [[ "$os_PACKAGE" = "deb" ]]; then
1180 dpkg -s "$@" > /dev/null 2> /dev/null
1181 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1182 rpm --quiet -q "$@"
1183 else
1184 exit_distro_not_supported "finding if a package is installed"
1185 fi
1186}
1187
1188# Distro-agnostic package uninstaller
1189# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001190function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001191 if is_ubuntu; then
1192 apt_get purge "$@"
1193 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001194 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001195 elif is_suse; then
1196 sudo zypper rm "$@"
1197 else
1198 exit_distro_not_supported "uninstalling packages"
1199 fi
1200}
1201
1202# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001203# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001204# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001205function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001206 [[ "$OFFLINE" = "True" ]] && return
1207 local sudo="sudo"
1208 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001209
1210 # The manual check for missing packages is because yum -y assumes
1211 # missing packages are OK. See
1212 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001213 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1214 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001215 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001216 awk '
1217 BEGIN { fail=0 }
1218 /No package/ { fail=1 }
1219 { print }
1220 END { exit fail }' || \
1221 die $LINENO "Missing packages detected"
1222
1223 # also ensure we catch a yum failure
1224 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001225 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001226 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001227}
1228
1229# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001230# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001231# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001232function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001233 [[ "$OFFLINE" = "True" ]] && return
1234 local sudo="sudo"
1235 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001236 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1237 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001238 zypper --non-interactive install --auto-agree-with-licenses "$@"
1239}
1240
1241
1242# Process Functions
1243# =================
1244
1245# _run_process() is designed to be backgrounded by run_process() to simulate a
1246# fork. It includes the dirty work of closing extra filehandles and preparing log
1247# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001248# from the service name.
1249# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001250# If an optional group is provided sg will be used to set the group of
1251# the command.
1252# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001253function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001254 # disable tracing through the exec redirects, it's just confusing in the logs.
1255 xtrace=$(set +o | grep xtrace)
1256 set +o xtrace
1257
Dean Troyerdff49a22014-01-30 15:37:40 -06001258 local service=$1
1259 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001260 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001261
1262 # Undo logging redirections and close the extra descriptors
1263 exec 1>&3
1264 exec 2>&3
1265 exec 3>&-
1266 exec 6>&-
1267
Dean Troyerdde41d02014-12-09 17:47:57 -06001268 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1269 if [[ -n ${LOGDIR} ]]; then
1270 exec 1>&"$real_logfile" 2>&1
1271 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1272 if [[ -n ${SCREEN_LOGDIR} ]]; then
1273 # Drop the backward-compat symlink
1274 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1275 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001276
1277 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1278 export PYTHONUNBUFFERED=1
1279 fi
1280
Sean Dague6e137ab2015-04-29 08:22:24 -04001281 # reenable xtrace before we do *real* work
1282 $xtrace
1283
Dean Troyer3159a822014-08-27 14:13:58 -05001284 # Run under ``setsid`` to force the process to become a session and group leader.
1285 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001286 if [[ -n "$group" ]]; then
1287 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1288 else
1289 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1290 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001291
1292 # Just silently exit this process
1293 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001294}
1295
1296# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1297# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001298# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001299# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001300function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001301 SCREEN_NAME=${SCREEN_NAME:-stack}
1302 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1303
1304 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1305 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1306 fi
1307
1308 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1309}
1310
1311# Find out if a process exists by partial name.
1312# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001313function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001314 local name=$1
1315 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001316 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001317 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001318 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001319}
1320
Dean Troyer3159a822014-08-27 14:13:58 -05001321# Run a single service under screen or directly
1322# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001323# If an optional group is provided sg will be used to run the
1324# command as that group.
1325# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001326function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001327 local service=$1
1328 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001329 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001330
Dean Troyer3159a822014-08-27 14:13:58 -05001331 if is_service_enabled $service; then
1332 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001333 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001334 else
1335 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001336 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001337 fi
1338 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001339}
1340
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001341# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001342# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001343# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001344# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001345# Run a command in a shell in a screen window, if an optional group
1346# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001347function screen_process {
1348 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001349 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001350 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001351
Sean Dagueea22a4f2014-06-27 15:21:41 -04001352 SCREEN_NAME=${SCREEN_NAME:-stack}
1353 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001354 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001355
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001356 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001357
Dean Troyerdde41d02014-12-09 17:47:57 -06001358 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1359 echo "LOGDIR: $LOGDIR"
1360 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1361 echo "log: $real_logfile"
1362 if [[ -n ${LOGDIR} ]]; then
1363 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001364 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001365 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1366 if [[ -n ${SCREEN_LOGDIR} ]]; then
1367 # Drop the backward-compat symlink
1368 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1369 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001370 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001371
1372 # sleep to allow bash to be ready to be send the command - we are
1373 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001374 # bash isn't running by the time we send the command, nothing
1375 # happens. This sleep was added originally to handle gate runs
1376 # where we needed this to be at least 3 seconds to pass
1377 # consistently on slow clouds. Now this is configurable so that we
1378 # can determine a reasonable value for the local case which should
1379 # be much smaller.
1380 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001381
1382 NL=`echo -ne '\015'`
1383 # This fun command does the following:
1384 # - the passed server command is backgrounded
1385 # - the pid of the background process is saved in the usual place
1386 # - the server process is brought back to the foreground
1387 # - if the server process exits prematurely the fg command errors
1388 # and a message is written to stdout and the process failure file
1389 #
1390 # The pid saved can be used in stop_process() as a process group
1391 # id to kill off all child processes
1392 if [[ -n "$group" ]]; then
1393 command="sg $group '$command'"
1394 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001395
1396 # Append the process to the screen rc file
1397 screen_rc "$name" "$command"
1398
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001399 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 -06001400}
1401
1402# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001403# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001404# screen_rc service "command-line"
1405function screen_rc {
1406 SCREEN_NAME=${SCREEN_NAME:-stack}
1407 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1408 if [[ ! -e $SCREENRC ]]; then
1409 # Name the screen session
1410 echo "sessionname $SCREEN_NAME" > $SCREENRC
1411 # Set a reasonable statusbar
1412 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1413 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1414 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1415 echo "screen -t shell bash" >> $SCREENRC
1416 fi
1417 # If this service doesn't already exist in the screenrc file
1418 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1419 NL=`echo -ne '\015'`
1420 echo "screen -t $1 bash" >> $SCREENRC
1421 echo "stuff \"$2$NL\"" >> $SCREENRC
1422
Dean Troyerdde41d02014-12-09 17:47:57 -06001423 if [[ -n ${LOGDIR} ]]; then
1424 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001425 echo "log on" >>$SCREENRC
1426 fi
1427 fi
1428}
1429
1430# Stop a service in screen
1431# If a PID is available use it, kill the whole process group via TERM
1432# If screen is being used kill the screen window; this will catch processes
1433# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001434# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001435# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001436function screen_stop_service {
1437 local service=$1
1438
Dean Troyerdff49a22014-01-30 15:37:40 -06001439 SCREEN_NAME=${SCREEN_NAME:-stack}
1440 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001441 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001442
Dean Troyer3159a822014-08-27 14:13:58 -05001443 if is_service_enabled $service; then
1444 # Clean up the screen window
Attila Fazekasf750a6f2015-07-01 12:17:35 +02001445 screen -S $SCREEN_NAME -p $service -X kill || true
Dean Troyer3159a822014-08-27 14:13:58 -05001446 fi
1447}
1448
1449# Stop a service process
1450# If a PID is available use it, kill the whole process group via TERM
1451# If screen is being used kill the screen window; this will catch processes
1452# that did not leave a PID behind
1453# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1454# stop_process service
1455function stop_process {
1456 local service=$1
1457
1458 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001459 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001460
1461 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001462 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001463 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1464 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
Dan Smithce7246a2015-04-23 09:41:06 -07001465 # oslo.service tends to stop actually shutting down
1466 # reliably in between releases because someone believes it
1467 # is dying too early due to some inflight work they
1468 # have. This is a tension. It happens often enough we're
1469 # going to just account for it in devstack and assume it
1470 # doesn't work.
1471 #
1472 # Set OSLO_SERVICE_WORKS=True to skip this block
1473 if [[ -z "$OSLO_SERVICE_WORKS" ]]; then
1474 # TODO(danms): Remove this double-kill when we have
1475 # this fixed in all services:
1476 # https://bugs.launchpad.net/oslo-incubator/+bug/1446583
1477 sleep 1
1478 # /bin/true becakse pkill on a non existant process returns an error
1479 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid) || /bin/true
1480 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001481 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001482 fi
1483 if [[ "$USE_SCREEN" = "True" ]]; then
1484 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001485 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001486 fi
1487 fi
1488}
1489
1490# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001491# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001492# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001493function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001494 local service
1495 local failures
1496 SCREEN_NAME=${SCREEN_NAME:-stack}
1497 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1498
1499
1500 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1501 echo "No service status directory found"
1502 return
1503 fi
1504
Wei Jiangange3340f12015-09-21 17:52:14 +08001505 # Check if there is any failure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001506 # make this -o errexit safe
1507 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001508
1509 for service in $failures; do
1510 service=`basename $service`
1511 service=${service%.failure}
1512 echo "Error: Service $service is not running"
1513 done
1514
1515 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001516 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001517 fi
1518}
1519
Chris Dent2f27a0e2014-09-09 13:46:02 +01001520# Tail a log file in a screen if USE_SCREEN is true.
1521function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001522 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001523 local logfile=$2
1524
Sean Dague53753292014-12-04 19:38:15 -05001525 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001526 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001527 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001528 fi
1529}
1530
Dean Troyerdff49a22014-01-30 15:37:40 -06001531
Dean Troyer3159a822014-08-27 14:13:58 -05001532# Deprecated Functions
1533# --------------------
1534
1535# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1536# fork. It includes the dirty work of closing extra filehandles and preparing log
1537# files to produce the same logs as screen_it(). The log filename is derived
1538# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1539# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1540# _old_run_process service "command-line"
1541function _old_run_process {
1542 local service=$1
1543 local command="$2"
1544
1545 # Undo logging redirections and close the extra descriptors
1546 exec 1>&3
1547 exec 2>&3
1548 exec 3>&-
1549 exec 6>&-
1550
1551 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001552 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1553 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001554
1555 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1556 export PYTHONUNBUFFERED=1
1557 fi
1558
1559 exec /bin/bash -c "$command"
1560 die "$service exec failure: $command"
1561}
1562
1563# old_run_process() launches a child process that closes all file descriptors and
1564# then exec's the passed in command. This is meant to duplicate the semantics
1565# of screen_it() without screen. PIDs are written to
1566# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1567# old_run_process service "command-line"
1568function old_run_process {
1569 local service=$1
1570 local command="$2"
1571
1572 # Spawn the child process
1573 _old_run_process "$service" "$command" &
1574 echo $!
1575}
1576
1577# Compatibility for existing start_XXXX() functions
1578# Uses global ``USE_SCREEN``
1579# screen_it service "command-line"
1580function screen_it {
1581 if is_service_enabled $1; then
1582 # Append the service to the screen rc file
1583 screen_rc "$1" "$2"
1584
1585 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001586 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001587 else
1588 # Spawn directly without screen
1589 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1590 fi
1591 fi
1592}
1593
1594# Compatibility for existing stop_XXXX() functions
1595# Stop a service in screen
1596# If a PID is available use it, kill the whole process group via TERM
1597# If screen is being used kill the screen window; this will catch processes
1598# that did not leave a PID behind
1599# screen_stop service
1600function screen_stop {
1601 # Clean up the screen window
1602 stop_process $1
1603}
1604
1605
Sean Dague2c65e712014-12-18 09:44:56 -05001606# Plugin Functions
1607# =================
1608
1609DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1610
1611# enable_plugin <name> <url> [branch]
1612#
1613# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1614# ``url`` is a git url
1615# ``branch`` is a gitref. If it's not set, defaults to master
1616function enable_plugin {
1617 local name=$1
1618 local url=$2
1619 local branch=${3:-master}
1620 DEVSTACK_PLUGINS+=",$name"
1621 GITREPO[$name]=$url
1622 GITDIR[$name]=$DEST/$name
1623 GITBRANCH[$name]=$branch
1624}
1625
1626# fetch_plugins
1627#
1628# clones all plugins
1629function fetch_plugins {
1630 local plugins="${DEVSTACK_PLUGINS}"
1631 local plugin
1632
1633 # short circuit if nothing to do
1634 if [[ -z $plugins ]]; then
1635 return
1636 fi
1637
Dean Troyerdc97cb72015-03-28 08:20:50 -05001638 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001639 for plugin in ${plugins//,/ }; do
1640 git_clone_by_name $plugin
1641 done
1642}
1643
1644# load_plugin_settings
1645#
1646# Load settings from plugins in the order that they were registered
1647function load_plugin_settings {
1648 local plugins="${DEVSTACK_PLUGINS}"
1649 local plugin
1650
1651 # short circuit if nothing to do
1652 if [[ -z $plugins ]]; then
1653 return
1654 fi
1655
1656 echo "Loading plugin settings"
1657 for plugin in ${plugins//,/ }; do
1658 local dir=${GITDIR[$plugin]}
1659 # source any known settings
1660 if [[ -f $dir/devstack/settings ]]; then
1661 source $dir/devstack/settings
1662 fi
1663 done
1664}
1665
Sean Dague6e275e12015-03-26 05:54:28 -04001666# plugin_override_defaults
1667#
1668# Run an extremely early setting phase for plugins that allows default
1669# overriding of services.
1670function plugin_override_defaults {
1671 local plugins="${DEVSTACK_PLUGINS}"
1672 local plugin
1673
1674 # short circuit if nothing to do
1675 if [[ -z $plugins ]]; then
1676 return
1677 fi
1678
1679 echo "Overriding Configuration Defaults"
1680 for plugin in ${plugins//,/ }; do
1681 local dir=${GITDIR[$plugin]}
1682 # source any overrides
1683 if [[ -f $dir/devstack/override-defaults ]]; then
1684 # be really verbose that an override is happening, as it
1685 # may not be obvious if things fail later.
1686 echo "$plugin has overriden the following defaults"
1687 cat $dir/devstack/override-defaults
1688 source $dir/devstack/override-defaults
1689 fi
1690 done
1691}
1692
Sean Dague2c65e712014-12-18 09:44:56 -05001693# run_plugins
1694#
1695# Run the devstack/plugin.sh in all the plugin directories. These are
1696# run in registration order.
1697function run_plugins {
1698 local mode=$1
1699 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301700
1701 local plugins="${DEVSTACK_PLUGINS}"
1702 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001703 for plugin in ${plugins//,/ }; do
1704 local dir=${GITDIR[$plugin]}
1705 if [[ -f $dir/devstack/plugin.sh ]]; then
1706 source $dir/devstack/plugin.sh $mode $phase
1707 fi
1708 done
1709}
1710
1711function run_phase {
1712 local mode=$1
1713 local phase=$2
1714 if [[ -d $TOP_DIR/extras.d ]]; then
1715 for i in $TOP_DIR/extras.d/*.sh; do
1716 [[ -r $i ]] && source $i $mode $phase
Sean Dague1de9e332015-10-07 08:46:13 -04001717 # NOTE(sdague): generate a big warning about using
1718 # extras.d in an unsupported way which will let us track
1719 # unsupported usage in the gate.
1720 local exceptions="50-ironic.sh 60-ceph.sh 80-tempest.sh"
1721 local extra=$(basename $i)
1722 if [[ ! ( $exceptions =~ "$extra" ) ]]; then
1723 deprecated "extras.d support is being removed in Mitaka-1"
1724 deprecated "jobs for project $extra will break after that point"
1725 deprecated "please move project to a supported devstack plugin model"
1726 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001727 done
1728 fi
1729 # the source phase corresponds to settings loading in plugins
1730 if [[ "$mode" == "source" ]]; then
1731 load_plugin_settings
Chris Dentc6d47012015-10-09 14:57:05 +00001732 verify_disabled_services
Sean Dague6e275e12015-03-26 05:54:28 -04001733 elif [[ "$mode" == "override_defaults" ]]; then
1734 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001735 else
1736 run_plugins $mode $phase
1737 fi
1738}
1739
Dean Troyerdff49a22014-01-30 15:37:40 -06001740
1741# Service Functions
1742# =================
1743
1744# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1745# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001746function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001747 echo "$1" | sed -e '
1748 s/,,/,/g;
1749 s/^,//;
1750 s/,$//
1751 '
1752}
1753
1754# disable_all_services() removes all current services
1755# from ``ENABLED_SERVICES`` to reset the configuration
1756# before a minimal installation
1757# Uses global ``ENABLED_SERVICES``
1758# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001759function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001760 ENABLED_SERVICES=""
1761}
1762
1763# Remove all services starting with '-'. For example, to install all default
1764# services except rabbit (rabbit) set in ``localrc``:
1765# ENABLED_SERVICES+=",-rabbit"
1766# Uses global ``ENABLED_SERVICES``
1767# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001768function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001769 local to_remove=""
1770 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001771 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001772
1773 # build up list of services that should be removed; i.e. they
1774 # begin with "-"
1775 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001776 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001777 to_remove+=",${service#-}"
1778 else
1779 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001780 fi
1781 done
Ian Wienand2796a822015-04-15 08:59:04 +10001782
1783 # go through the service list. if this service appears in the "to
1784 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001785 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Dean Troyerdff49a22014-01-30 15:37:40 -06001786}
1787
Chris Dentc6d47012015-10-09 14:57:05 +00001788# disable_service() prepares the services passed as argument to be
1789# removed from the ``ENABLED_SERVICES`` list, if they are present.
Dean Troyerdff49a22014-01-30 15:37:40 -06001790#
1791# For example:
1792# disable_service rabbit
1793#
Chris Dentc6d47012015-10-09 14:57:05 +00001794# Uses global ``DISABLED_SERVICES``
Dean Troyerdff49a22014-01-30 15:37:40 -06001795# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001796function disable_service {
Chris Dentc6d47012015-10-09 14:57:05 +00001797 local disabled_svcs="${DISABLED_SERVICES}"
1798 local enabled_svcs=",${ENABLED_SERVICES},"
Dean Troyerdff49a22014-01-30 15:37:40 -06001799 local service
1800 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001801 disabled_svcs+=",$service"
Dean Troyerdff49a22014-01-30 15:37:40 -06001802 if is_service_enabled $service; then
Chris Dentc6d47012015-10-09 14:57:05 +00001803 enabled_svcs=${enabled_svcs//,$service,/,}
Dean Troyerdff49a22014-01-30 15:37:40 -06001804 fi
1805 done
Chris Dentc6d47012015-10-09 14:57:05 +00001806 DISABLED_SERVICES=$(_cleanup_service_list "$disabled_svcs")
1807 ENABLED_SERVICES=$(_cleanup_service_list "$enabled_svcs")
Dean Troyerdff49a22014-01-30 15:37:40 -06001808}
1809
1810# enable_service() adds the services passed as argument to the
1811# ``ENABLED_SERVICES`` list, if they are not already present.
1812#
1813# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001814# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001815#
1816# This function does not know about the special cases
1817# for nova, glance, and neutron built into is_service_enabled().
1818# Uses global ``ENABLED_SERVICES``
1819# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001820function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001821 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001822 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001823 for service in $@; do
Chris Dentc6d47012015-10-09 14:57:05 +00001824 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
1825 warn $LINENO "Attempt to enable_service ${service} when it has been disabled"
1826 continue
1827 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001828 if ! is_service_enabled $service; then
1829 tmpsvcs+=",$service"
1830 fi
1831 done
1832 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1833 disable_negated_services
1834}
1835
1836# is_service_enabled() checks if the service(s) specified as arguments are
1837# enabled by the user in ``ENABLED_SERVICES``.
1838#
1839# Multiple services specified as arguments are ``OR``'ed together; the test
1840# is a short-circuit boolean, i.e it returns on the first match.
1841#
1842# There are special cases for some 'catch-all' services::
1843# **nova** returns true if any service enabled start with **n-**
1844# **cinder** returns true if any service enabled start with **c-**
Dean Troyerdff49a22014-01-30 15:37:40 -06001845# **glance** returns true if any service enabled start with **g-**
1846# **neutron** returns true if any service enabled start with **q-**
1847# **swift** returns true if any service enabled start with **s-**
1848# **trove** returns true if any service enabled start with **tr-**
1849# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1850# **s-** services will be enabled. This will be deprecated in the future.
1851#
1852# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1853# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1854# as enabled in this case.
1855#
1856# Uses global ``ENABLED_SERVICES``
1857# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001858function is_service_enabled {
Ian Wienand433a9b12015-10-07 13:29:31 +11001859 local xtrace
1860 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001861 set +o xtrace
1862 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001863 local services=$@
1864 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001865 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001866 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001867
1868 # Look for top-level 'enabled' function for this service
1869 if type is_${service}_enabled >/dev/null 2>&1; then
1870 # A function exists for this service, use it
Christian Schwede3db0aad2015-09-10 11:15:39 +00001871 is_${service}_enabled && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001872 fi
1873
1874 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1875 # are implemented
1876
Sean Dague45917cc2014-02-24 16:09:14 -05001877 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001878 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001879 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001880 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1881 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1882 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1883 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1884 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1885 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001886 done
Sean Dague45917cc2014-02-24 16:09:14 -05001887 $xtrace
1888 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001889}
1890
fumihiko kakuma8606c982015-04-13 09:55:06 +09001891# remove specified list from the input string
1892# remove_disabled_services service-list remove-list
1893function remove_disabled_services {
1894 local service_list=$1
1895 local remove_list=$2
1896 local service
1897 local enabled=""
1898
1899 for service in ${service_list//,/ }; do
1900 local remove
1901 local add=1
1902 for remove in ${remove_list//,/ }; do
1903 if [[ ${remove} == ${service} ]]; then
1904 add=0
1905 break
1906 fi
1907 done
1908 if [[ $add == 1 ]]; then
1909 enabled="${enabled},$service"
1910 fi
1911 done
1912 _cleanup_service_list "$enabled"
1913}
1914
Dean Troyerdff49a22014-01-30 15:37:40 -06001915# Toggle enable/disable_service for services that must run exclusive of each other
1916# $1 The name of a variable containing a space-separated list of services
1917# $2 The name of a variable in which to store the enabled service's name
1918# $3 The name of the service to enable
1919function use_exclusive_service {
1920 local options=${!1}
1921 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001922 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001923 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001924 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001925 for opt in $options;do
1926 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1927 done
1928 eval "$out=$selection"
1929 return 0
1930}
1931
Chris Dentc6d47012015-10-09 14:57:05 +00001932# Make sure that nothing has manipulated ENABLED_SERVICES in a way
1933# that conflicts with prior calls to disable_service.
1934# Uses global ``ENABLED_SERVICES``
1935function verify_disabled_services {
1936 local service
1937 for service in ${ENABLED_SERVICES//,/ }; do
1938 if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
1939 die $LINENO "ENABLED_SERVICES directly modified to overcome 'disable_service ${service}'"
1940 fi
1941 done
1942}
1943
Dean Troyerdff49a22014-01-30 15:37:40 -06001944
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001945# System Functions
1946# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001947
1948# Only run the command if the target file (the last arg) is not on an
1949# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001950function _safe_permission_operation {
Ian Wienand433a9b12015-10-07 13:29:31 +11001951 local xtrace
1952 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001953 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001954 local args=( $@ )
1955 local last
1956 local sudo_cmd
1957 local dir_to_check
1958
1959 let last="${#args[*]} - 1"
1960
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001961 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001962 if [ ! -d "$dir_to_check" ]; then
1963 dir_to_check=`dirname "$dir_to_check"`
1964 fi
1965
1966 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001967 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001968 return 0
1969 fi
1970
1971 if [[ $TRACK_DEPENDS = True ]]; then
1972 sudo_cmd="env"
1973 else
1974 sudo_cmd="sudo"
1975 fi
1976
Sean Dague45917cc2014-02-24 16:09:14 -05001977 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001978 $sudo_cmd $@
1979}
1980
1981# Exit 0 if address is in network or 1 if address is not in network
1982# ip-range is in CIDR notation: 1.2.3.4/20
1983# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001984function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001985 local ip=$1
1986 local range=$2
1987 local masklen=${range#*/}
1988 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1989 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1990 [[ $network == $subnet ]]
1991}
1992
1993# Add a user to a group.
1994# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001995function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001996 local user=$1
1997 local group=$2
1998
Thomas Bechtolda8580852015-05-31 00:04:33 +02001999 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06002000}
2001
2002# Convert CIDR notation to a IPv4 netmask
2003# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002004function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002005 local maskpat="255 255 255 255"
2006 local maskdgt="254 252 248 240 224 192 128"
2007 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2008 echo ${1-0}.${2-0}.${3-0}.${4-0}
2009}
2010
2011# Gracefully cp only if source file/dir exists
2012# cp_it source destination
2013function cp_it {
2014 if [ -e $1 ] || [ -d $1 ]; then
2015 cp -pRL $1 $2
2016 fi
2017}
2018
2019# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2020# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2021# ``localrc`` or on the command line if necessary::
2022#
2023# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2024#
2025# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2026
Ian Wienandaee18c72014-02-21 15:35:08 +11002027function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05002028 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002029 export http_proxy=$http_proxy
2030 fi
Sean Dague53753292014-12-04 19:38:15 -05002031 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002032 export https_proxy=$https_proxy
2033 fi
Sean Dague53753292014-12-04 19:38:15 -05002034 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002035 export no_proxy=$no_proxy
2036 fi
2037}
2038
2039# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002040function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06002041 local mount_type=`stat -f -L -c %T $1`
2042 test "$mount_type" == "nfs"
2043}
2044
2045# Return the network portion of the given IP address using netmask
2046# netmask is in the traditional dotted-quad format
2047# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002048function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002049 local ip=$1
2050 local mask=$2
2051 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2052 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2053 echo $subnet
2054}
2055
Chris Dent3a2c86a2015-05-12 13:41:25 +00002056# Return the current python as "python<major>.<minor>"
2057function python_version {
2058 local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
2059 echo "python${python_version}"
2060}
2061
Dean Troyerdff49a22014-01-30 15:37:40 -06002062# Service wrapper to restart services
2063# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002064function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002065 if is_ubuntu; then
2066 sudo /usr/sbin/service $1 restart
2067 else
2068 sudo /sbin/service $1 restart
2069 fi
2070}
2071
2072# Only change permissions of a file or directory if it is not on an
2073# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002074function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002075 _safe_permission_operation chmod $@
2076}
2077
2078# Only change ownership of a file or directory if it is not on an NFS
2079# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002080function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002081 _safe_permission_operation chown $@
2082}
2083
2084# Service wrapper to start services
2085# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002086function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002087 if is_ubuntu; then
2088 sudo /usr/sbin/service $1 start
2089 else
2090 sudo /sbin/service $1 start
2091 fi
2092}
2093
2094# Service wrapper to stop services
2095# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002096function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002097 if is_ubuntu; then
2098 sudo /usr/sbin/service $1 stop
2099 else
2100 sudo /sbin/service $1 stop
2101 fi
2102}
2103
Sean Dague442e4e92015-06-24 13:24:02 -04002104# Test with a finite retry loop.
2105#
2106function test_with_retry {
2107 local testcmd=$1
2108 local failmsg=$2
2109 local until=${3:-10}
2110 local sleep=${4:-0.5}
2111
2112 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2113 die $LINENO "$failmsg"
2114 fi
2115}
2116
Dean Troyerdff49a22014-01-30 15:37:40 -06002117
2118# Restore xtrace
2119$XTRACE
2120
2121# Local variables:
2122# mode: shell-script
2123# End: