blob: d506b846bdfac6fbed8b73a8a280f3f7c67121da [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
Ian Wienande82bac02015-08-25 14:29:08 +1000109# trueorfalse <True|False> <VAR>
110#
111# Normalize config-value provided in variable VAR to either "True" or
112# "False". If VAR is unset (i.e. $VAR evaluates as empty), the value
113# of the second argument will be used as the default value.
114#
115# Accepts as False: 0 no No NO false False FALSE
116# Accepts as True: 1 yes Yes YES true True TRUE
117#
118# usage:
119# VAL=$(trueorfalse False VAL)
Ian Wienandaee18c72014-02-21 15:35:08 +1100120function trueorfalse {
Ian Wienand433a9b12015-10-07 13:29:31 +1100121 local xtrace
122 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -0500123 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600124
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900125 local default=$1
Ian Wienande82bac02015-08-25 14:29:08 +1000126
127 if [ -z $2 ]; then
128 die $LINENO "variable to normalize required"
129 fi
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900130 local testval=${!2:-}
131
132 case "$testval" in
133 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
134 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
135 * ) echo "$default" ;;
136 esac
137
Sean Dague45917cc2014-02-24 16:09:14 -0500138 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600139}
140
Attila Fazekas1bd79592015-02-24 14:06:56 +0100141function isset {
142 [[ -v "$1" ]]
143}
Dean Troyerdff49a22014-01-30 15:37:40 -0600144
Dean Troyer68162342015-05-13 15:41:03 -0500145
Dean Troyerdff49a22014-01-30 15:37:40 -0600146# Control Functions
147# =================
148
149# Prints backtrace info
150# filename:lineno:function
151# backtrace level
152function backtrace {
153 local level=$1
154 local deep=$((${#BASH_SOURCE[@]} - 1))
155 echo "[Call Trace]"
156 while [ $level -le $deep ]; do
157 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
158 deep=$((deep - 1))
159 done
160}
161
162# Prints line number and "message" then exits
163# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100164function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600165 local exitcode=$?
166 set +o xtrace
167 local line=$1; shift
168 if [ $exitcode == 0 ]; then
169 exitcode=1
170 fi
171 backtrace 2
172 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600173 # Give buffers a second to flush
174 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600175 exit $exitcode
176}
177
178# Checks an environment variable is not set or has length 0 OR if the
179# exit code is non-zero and prints "message" and exits
180# NOTE: env-var is the variable name without a '$'
181# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100182function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600183 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100184 local xtrace
185 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600186 set +o xtrace
187 local line=$1; shift
188 local evar=$1; shift
189 if ! is_set $evar || [ $exitcode != 0 ]; then
190 die $line "$*"
191 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500192 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600193}
194
Sean Dague1de9e332015-10-07 08:46:13 -0400195function deprecated {
196 local text=$1
197 DEPRECATED_TEXT+="\n$text"
198 echo "WARNING: $text"
199}
200
Dean Troyerdff49a22014-01-30 15:37:40 -0600201# Prints line number and "message" in error format
202# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100203function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600204 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100205 local xtrace
206 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600207 set +o xtrace
208 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
209 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600210 if [[ -n ${LOGDIR} ]]; then
211 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600212 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500213 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600214 return $exitcode
215}
216
217# Checks an environment variable is not set or has length 0 OR if the
218# exit code is non-zero and prints "message"
219# NOTE: env-var is the variable name without a '$'
220# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100221function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600222 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100223 local xtrace
224 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600225 set +o xtrace
226 local line=$1; shift
227 local evar=$1; shift
228 if ! is_set $evar || [ $exitcode != 0 ]; then
229 err $line "$*"
230 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500231 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600232 return $exitcode
233}
234
235# Exit after outputting a message about the distribution not being supported.
236# exit_distro_not_supported [optional-string-telling-what-is-missing]
237function exit_distro_not_supported {
238 if [[ -z "$DISTRO" ]]; then
239 GetDistro
240 fi
241
242 if [ $# -gt 0 ]; then
243 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
244 else
245 die $LINENO "Support for $DISTRO is incomplete."
246 fi
247}
248
249# Test if the named environment variable is set and not zero length
250# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100251function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600252 local var=\$"$1"
253 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
254}
255
256# Prints line number and "message" in warning format
257# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100258function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600259 local exitcode=$?
Ian Wienand433a9b12015-10-07 13:29:31 +1100260 local xtrace
261 xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600262 set +o xtrace
263 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Sean Daguee4af9292015-04-28 08:57:57 -0400264 echo $msg
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500265 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600266 return $exitcode
267}
268
269
270# Distro Functions
271# ================
272
273# Determine OS Vendor, Release and Update
274# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
275# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500276# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
277# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
278# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
279# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
280# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500281os_VENDOR=""
282os_RELEASE=""
283os_UPDATE=""
284os_PACKAGE=""
285os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500286
Dean Troyerdff49a22014-01-30 15:37:40 -0600287# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100288function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500289
Dean Troyerdff49a22014-01-30 15:37:40 -0600290 # Figure out which vendor we are
291 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
292 # OS/X
293 os_VENDOR=`sw_vers -productName`
294 os_RELEASE=`sw_vers -productVersion`
295 os_UPDATE=${os_RELEASE##*.}
296 os_RELEASE=${os_RELEASE%.*}
297 os_PACKAGE=""
298 if [[ "$os_RELEASE" =~ "10.7" ]]; then
299 os_CODENAME="lion"
300 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
301 os_CODENAME="snow leopard"
302 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
303 os_CODENAME="leopard"
304 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
305 os_CODENAME="tiger"
306 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
307 os_CODENAME="panther"
308 else
309 os_CODENAME=""
310 fi
311 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
312 os_VENDOR=$(lsb_release -i -s)
313 os_RELEASE=$(lsb_release -r -s)
314 os_UPDATE=""
315 os_PACKAGE="rpm"
316 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
317 os_PACKAGE="deb"
318 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
319 lsb_release -d -s | grep -q openSUSE
320 if [[ $? -eq 0 ]]; then
321 os_VENDOR="openSUSE"
322 fi
323 elif [[ $os_VENDOR == "openSUSE project" ]]; then
324 os_VENDOR="openSUSE"
325 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
326 os_VENDOR="Red Hat"
327 fi
328 os_CODENAME=$(lsb_release -c -s)
329 elif [[ -r /etc/redhat-release ]]; then
330 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
331 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
332 # CentOS release 5.5 (Final)
333 # CentOS Linux release 6.0 (Final)
334 # Fedora release 16 (Verne)
335 # XenServer release 6.2.0-70446c (xenenterprise)
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700336 # Oracle Linux release 7
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300337 # CloudLinux release 7.1
Dean Troyerdff49a22014-01-30 15:37:40 -0600338 os_CODENAME=""
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300339 for r in "Red Hat" CentOS Fedora XenServer CloudLinux; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600340 os_VENDOR=$r
341 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
342 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
343 os_CODENAME=${ver#*|}
344 os_RELEASE=${ver%|*}
345 os_UPDATE=${os_RELEASE##*.}
346 os_RELEASE=${os_RELEASE%.*}
347 break
348 fi
349 os_VENDOR=""
350 done
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700351 if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
352 os_VENDOR=OracleLinux
353 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600354 os_PACKAGE="rpm"
355 elif [[ -r /etc/SuSE-release ]]; then
356 for r in openSUSE "SUSE Linux"; do
357 if [[ "$r" = "SUSE Linux" ]]; then
358 os_VENDOR="SUSE LINUX"
359 else
360 os_VENDOR=$r
361 fi
362
363 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
364 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
365 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
366 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
367 break
368 fi
369 os_VENDOR=""
370 done
371 os_PACKAGE="rpm"
372 # If lsb_release is not installed, we should be able to detect Debian OS
373 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
374 os_VENDOR="Debian"
375 os_PACKAGE="deb"
376 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
377 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
378 fi
379 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
380}
381
382# Translate the OS version values into common nomenclature
383# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500384declare DISTRO
385
Ian Wienandaee18c72014-02-21 15:35:08 +1100386function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600387 GetOSVersion
388 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
389 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
390 DISTRO=$os_CODENAME
391 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
392 # For Fedora, just use 'f' and the release
393 DISTRO="f$os_RELEASE"
394 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
395 DISTRO="opensuse-$os_RELEASE"
396 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
397 # For SLE, also use the service pack
398 if [[ -z "$os_UPDATE" ]]; then
399 DISTRO="sle${os_RELEASE}"
400 else
401 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
402 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530403 elif [[ "$os_VENDOR" =~ (Red Hat) || \
404 "$os_VENDOR" =~ (CentOS) || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700405 "$os_VENDOR" =~ (OracleLinux) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600406 # Drop the . release as we assume it's compatible
407 DISTRO="rhel${os_RELEASE::1}"
408 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
409 DISTRO="xs$os_RELEASE"
410 else
411 # Catch-all for now is Vendor + Release + Update
412 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
413 fi
414 export DISTRO
415}
416
417# Utility function for checking machine architecture
418# is_arch arch-type
419function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500420 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600421}
422
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700423# Determine if current distribution is an Oracle distribution
424# is_oraclelinux
425function is_oraclelinux {
426 if [[ -z "$os_VENDOR" ]]; then
427 GetOSVersion
428 fi
429
430 [ "$os_VENDOR" = "OracleLinux" ]
431}
432
433
Dean Troyerdff49a22014-01-30 15:37:40 -0600434# Determine if current distribution is a Fedora-based distribution
435# (Fedora, RHEL, CentOS, etc).
436# is_fedora
437function is_fedora {
438 if [[ -z "$os_VENDOR" ]]; then
439 GetOSVersion
440 fi
441
anju Tiwari6c639c92014-07-15 18:11:54 +0530442 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300443 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ] || \
444 [ "$os_VENDOR" = "CloudLinux" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600445}
446
447
448# Determine if current distribution is a SUSE-based distribution
449# (openSUSE, SLE).
450# is_suse
451function is_suse {
452 if [[ -z "$os_VENDOR" ]]; then
453 GetOSVersion
454 fi
455
456 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
457}
458
459
460# Determine if current distribution is an Ubuntu-based distribution
461# It will also detect non-Ubuntu but Debian-based distros
462# is_ubuntu
463function is_ubuntu {
464 if [[ -z "$os_PACKAGE" ]]; then
465 GetOSVersion
466 fi
467 [ "$os_PACKAGE" = "deb" ]
468}
469
470
471# Git Functions
472# =============
473
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600474# Returns openstack release name for a given branch name
475# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100476function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600477 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700478 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600479 echo ${branch#*/}
480 else
481 echo "master"
482 fi
483}
484
Dean Troyerdff49a22014-01-30 15:37:40 -0600485# git clone only if directory doesn't exist already. Since ``DEST`` might not
486# be owned by the installation user, we create the directory and change the
487# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500488# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
489# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600490# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500491# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600492# git_clone remote dest-dir branch
493function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500494 local git_remote=$1
495 local git_dest=$2
496 local git_ref=$3
497 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200498 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500499
Sean Dague53753292014-12-04 19:38:15 -0500500 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800501 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200502 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
503 fi
504
Dean Troyerdff49a22014-01-30 15:37:40 -0600505 if [[ "$OFFLINE" = "True" ]]; then
506 echo "Running in offline mode, clones already exist"
507 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500508 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600509 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400510 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600511 return
512 fi
513
Dean Troyer50cda692014-07-25 11:57:20 -0500514 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600515 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500516 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700517 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
518 echo "The $git_dest project was not found; if this is a gate job, add"
519 echo "the project to the \$PROJECTS variable in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600520 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700521 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200522 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600523 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500524 cd $git_dest
525 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600526 else
527 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500528 if [[ ! -d $git_dest ]]; then
James E. Blairebe63d82015-09-24 07:43:50 -0700529 if [[ "$ERROR_ON_CLONE" = "True" ]]; then
530 echo "The $git_dest project was not found; if this is a gate job, add"
531 echo "the project to the \$PROJECTS variable in the job definition."
Dean Troyerdff49a22014-01-30 15:37:40 -0600532 die $LINENO "Cloning not allowed in this configuration"
James E. Blairebe63d82015-09-24 07:43:50 -0700533 fi
Jamie Lennox51f0de52014-10-20 16:32:34 +0200534 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500535 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600536 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500537 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600538 elif [[ "$RECLONE" = "True" ]]; then
539 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500540 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600541 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500542 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100543 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600544 # remove the existing ignored files (like pyc) as they cause breakage
545 # (due to the py files having older timestamps than our pyc, so python
546 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500547 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600548
Dean Troyer50cda692014-07-25 11:57:20 -0500549 # handle git_ref accordingly to type (tag, branch)
550 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
551 git_update_tag $git_ref
552 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
553 git_update_branch $git_ref
554 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
555 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600556 else
Dean Troyer50cda692014-07-25 11:57:20 -0500557 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600558 fi
559
560 fi
561 fi
562
563 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500564 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600565 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400566 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600567}
568
Sean Daguecc524062014-10-01 09:06:43 -0400569# A variation on git clone that lets us specify a project by it's
570# actual name, like oslo.config. This is exceptionally useful in the
571# library installation case
572function git_clone_by_name {
573 local name=$1
574 local repo=${GITREPO[$name]}
575 local dir=${GITDIR[$name]}
576 local branch=${GITBRANCH[$name]}
577 git_clone $repo $dir $branch
578}
579
580
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100581# git can sometimes get itself infinitely stuck with transient network
582# errors or other issues with the remote end. This wraps git in a
583# timeout/retry loop and is intended to watch over non-local git
584# processes that might hang. GIT_TIMEOUT, if set, is passed directly
585# to timeout(1); otherwise the default value of 0 maintains the status
586# quo of waiting forever.
587# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100588function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100589 local count=0
590 local timeout=0
591
592 if [[ -n "${GIT_TIMEOUT}" ]]; then
593 timeout=${GIT_TIMEOUT}
594 fi
595
596 until timeout -s SIGINT ${timeout} git "$@"; do
597 # 124 is timeout(1)'s special return code when it reached the
598 # timeout; otherwise assume fatal failure
599 if [[ $? -ne 124 ]]; then
600 die $LINENO "git call failed: [git $@]"
601 fi
602
603 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400604 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100605 if [ $count -eq 3 ]; then
606 die $LINENO "Maximum of 3 git retries reached"
607 fi
608 sleep 5
609 done
610}
611
Dean Troyerdff49a22014-01-30 15:37:40 -0600612# git update using reference as a branch.
613# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100614function git_update_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 -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600618 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500619 git branch -D $git_branch || true
620 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600621}
622
623# git update using reference as a branch.
624# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100625function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500626 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600627
Dean Troyer50cda692014-07-25 11:57:20 -0500628 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600629}
630
631# git update using reference as a tag. Be careful editing source at that repo
632# as working copy will be in a detached mode
633# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100634function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500635 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600636
Dean Troyer50cda692014-07-25 11:57:20 -0500637 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600638 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500639 git_timed fetch origin tag $git_tag
640 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600641}
642
643
644# OpenStack Functions
645# ===================
646
647# Get the default value for HOST_IP
648# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100649function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600650 local fixed_range=$1
651 local floating_range=$2
652 local host_ip_iface=$3
653 local host_ip=$4
Brian Haley180f5eb2015-06-16 13:14:31 -0400654 local af=$5
Dean Troyerdff49a22014-01-30 15:37:40 -0600655
Dean Troyerdff49a22014-01-30 15:37:40 -0600656 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
657 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
658 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100659 # Find the interface used for the default route
Brian Haley180f5eb2015-06-16 13:14:31 -0400660 host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
Sean M. Collinsd20435b2015-08-25 19:24:51 -0400661 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 -0500662 local ip
663 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600664 # Attempt to filter out IP addresses that are part of the fixed and
665 # floating range. Note that this method only works if the ``netaddr``
666 # python library is installed. If it is not installed, an error
667 # will be printed and the first IP from the interface will be used.
668 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
669 # address.
Brian Haley180f5eb2015-06-16 13:14:31 -0400670 if [[ "$af" == "inet6" ]]; then
671 host_ip=$ip
672 break;
673 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500674 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
675 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600676 break;
677 fi
678 done
679 fi
680 echo $host_ip
681}
682
Attila Fazekasf71b5002014-05-28 09:52:22 +0200683# Generates hex string from ``size`` byte of pseudo random data
684# generate_hex_string size
685function generate_hex_string {
686 local size=$1
687 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
688}
689
Dean Troyerdff49a22014-01-30 15:37:40 -0600690# Grab a numbered field from python prettytable output
691# Fields are numbered starting with 1
692# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
693# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100694function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500695 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600696 while read data; do
697 if [ "$1" -lt 0 ]; then
698 field="(\$(NF$1))"
699 else
700 field="\$$(($1 + 1))"
701 fi
702 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
703 done
704}
705
yuntongjinf26deea2015-02-28 10:50:34 +0800706# install default policy
707# copy over a default policy.json and policy.d for projects
708function install_default_policy {
709 local project=$1
710 local project_uc=$(echo $1|tr a-z A-Z)
711 local conf_dir="${project_uc}_CONF_DIR"
712 # eval conf dir to get the variable
713 conf_dir="${!conf_dir}"
714 local project_dir="${project_uc}_DIR"
715 # eval project dir to get the variable
716 project_dir="${!project_dir}"
717 local sample_conf_dir="${project_dir}/etc/${project}"
718 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
719
720 # first copy any policy.json
721 cp -p $sample_conf_dir/policy.json $conf_dir
722 # then optionally copy over policy.d
723 if [[ -d $sample_policy_dir ]]; then
724 cp -r $sample_policy_dir $conf_dir/policy.d
725 fi
726}
727
Dean Troyerdff49a22014-01-30 15:37:40 -0600728# Add a policy to a policy.json file
729# Do nothing if the policy already exists
730# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100731function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600732 local policy_file=$1
733 local policy_name=$2
734 local policy_perm=$3
735
736 if grep -q ${policy_name} ${policy_file}; then
737 echo "Policy ${policy_name} already exists in ${policy_file}"
738 return
739 fi
740
741 # Add a terminating comma to policy lines without one
742 # Remove the closing '}' and all lines following to the end-of-file
743 local tmpfile=$(mktemp)
744 uniq ${policy_file} | sed -e '
745 s/]$/],/
746 /^[}]/,$d
747 ' > ${tmpfile}
748
749 # Append policy and closing brace
750 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
751 echo "}" >>${tmpfile}
752
753 mv ${tmpfile} ${policy_file}
754}
755
Alistair Coles24779f62014-10-15 18:57:59 +0100756# Gets or creates a domain
757# Usage: get_or_create_domain <name> <description>
758function get_or_create_domain {
Ian Wienand11d276c2015-07-02 09:34:34 +1000759 local domain_id
Alistair Coles24779f62014-10-15 18:57:59 +0100760 # Gets domain id
Ian Wienand11d276c2015-07-02 09:34:34 +1000761 domain_id=$(
Alistair Coles24779f62014-10-15 18:57:59 +0100762 # Gets domain id
Steve Martinelli050a0d52015-09-06 22:03:54 +0000763 openstack domain show $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100764 -f value -c id 2>/dev/null ||
765 # Creates new domain
Steve Martinelli050a0d52015-09-06 22:03:54 +0000766 openstack domain create $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100767 --description "$2" \
768 -f value -c id
769 )
770 echo $domain_id
771}
772
Steve Martinellib74e01c2014-12-18 01:35:35 -0500773# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000774# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500775function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500776 local desc="${3:-}"
Ian Wienand11d276c2015-07-02 09:34:34 +1000777 local group_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500778 # Gets group id
Ian Wienand11d276c2015-07-02 09:34:34 +1000779 group_id=$(
Steve Martinellib74e01c2014-12-18 01:35:35 -0500780 # Creates new group with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000781 openstack group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000782 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500783 -f value -c id
784 )
785 echo $group_id
786}
787
Bartosz Górski0abde392014-02-28 14:15:19 +0100788# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000789# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100790function get_or_create_user {
Ian Wienand11d276c2015-07-02 09:34:34 +1000791 local user_id
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000792 if [[ ! -z "$4" ]]; then
793 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200794 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500795 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200796 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100797 # Gets user id
Ian Wienand11d276c2015-07-02 09:34:34 +1000798 user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500799 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000800 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100801 $1 \
802 --password "$2" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000803 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500804 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500805 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100806 -f value -c id
807 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500808 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100809}
810
811# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000812# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100813function get_or_create_project {
Ian Wienand11d276c2015-07-02 09:34:34 +1000814 local project_id
815 project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500816 # Creates new project with --or-show
Steve Martinelli050a0d52015-09-06 22:03:54 +0000817 openstack project create $1 \
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000818 --domain=$2 \
819 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100820 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500821 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100822}
823
824# Gets or creates role
825# Usage: get_or_create_role <name>
826function get_or_create_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000827 local role_id
828 role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500829 # Creates role with --or-show
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000830 openstack role create $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000831 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100832 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500833 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100834}
835
Jamie Lennox9b215db2015-02-10 18:19:57 +1100836# Gets or adds user role to project
837# Usage: get_or_add_user_project_role <role> <user> <project>
838function get_or_add_user_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000839 local user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100840 # Gets user role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000841 user_role_id=$(openstack role list \
Steve Martinelli5541a612015-01-19 15:58:49 -0500842 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100843 --column "ID" \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000844 --project $3 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100845 --column "Name" \
846 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500847 if [[ -z "$user_role_id" ]]; then
Roxana Gherle50821be2015-09-22 10:52:46 -0700848 # Adds role to user and get it
849 openstack role add $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100850 --user $2 \
Steve Martinelli050a0d52015-09-06 22:03:54 +0000851 --project $3
Roxana Gherle50821be2015-09-22 10:52:46 -0700852 user_role_id=$(openstack role list \
853 --user $2 \
Roxana Gherle50821be2015-09-22 10:52:46 -0700854 --column "ID" \
855 --project $3 \
856 --column "Name" \
857 | grep " $1 " | get_field 1)
Bartosz Górski0abde392014-02-28 14:15:19 +0100858 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500859 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100860}
861
Steve Martinelli4599fd12015-03-12 21:30:58 -0400862# Gets or adds group role to project
863# Usage: get_or_add_group_project_role <role> <group> <project>
864function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000865 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -0400866 # Gets group role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000867 group_role_id=$(openstack role list \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400868 --group $2 \
869 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000870 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400871 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000872 # Adds role to group and get it
873 openstack role add $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000874 --group $2 \
875 --project $3
876 group_role_id=$(openstack role list \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400877 --group $2 \
878 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000879 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400880 fi
881 echo $group_role_id
882}
883
Bartosz Górski0abde392014-02-28 14:15:19 +0100884# Gets or creates service
885# Usage: get_or_create_service <name> <type> <description>
886function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +1000887 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100888 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +1000889 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100890 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +1000891 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100892 # Creates new service if not exists
893 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500894 $2 \
895 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100896 --description="$3" \
897 -f value -c id
898 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500899 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100900}
901
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000902# Create an endpoint with a specific interface
903# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
904function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +1000905 local endpoint_id
Daniel Gonzalez1991e752015-08-11 19:34:22 +0200906 # TODO(dgonzalez): The check of the region name, as done in the grep
907 # statement below, exists only because keystone does currently
908 # not allow filtering the region name when listing endpoints. If keystone
909 # gets support for this, the check for the region name can be removed.
910 # Related bug in keystone: https://bugs.launchpad.net/keystone/+bug/1482772
Ian Wienand11d276c2015-07-02 09:34:34 +1000911 endpoint_id=$(openstack endpoint list \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000912 --service $1 \
913 --interface $2 \
914 --region $4 \
Daniel Gonzalez1991e752015-08-11 19:34:22 +0200915 -c ID -c Region -f value | grep $4 | cut -f 1 -d " ")
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500916 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100917 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500918 endpoint_id=$(openstack endpoint create \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000919 $1 $2 $3 --region $4 -f value -c id)
Bartosz Górski0abde392014-02-28 14:15:19 +0100920 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000921
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500922 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100923}
Dean Troyerdff49a22014-01-30 15:37:40 -0600924
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000925# Gets or creates endpoint
926# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
927function get_or_create_endpoint {
928 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
929 # creating one endpoint with multiple urls to multiple endpoints each with
930 # a different interface. To maintain the existing function interface we
931 # create 3 endpoints and return the id of the public one. In reality
932 # returning the public id will not make a lot of difference as there are no
933 # scenarios currently that use the returned id. Ideally this behaviour
934 # should be pushed out to the service setups and let them create the
935 # endpoints they need.
936 local public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
937 _get_or_create_endpoint_with_interface $1 admin $4 $2
938 _get_or_create_endpoint_with_interface $1 internal $5 $2
939
940 # return the public id to indicate success, and this is the endpoint most likely wanted
941 echo $public_id
942}
943
944# Get a URL from the identity service
945# Usage: get_endpoint_url <service> <interface>
946function get_endpoint_url {
947 echo $(openstack endpoint list \
948 --service $1 --interface $2 \
949 --os-url $KEYSTONE_SERVICE_URI_V3 \
950 --os-identity-api-version=3 \
951 -c URL -f value)
952}
953
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500954
Dean Troyerdff49a22014-01-30 15:37:40 -0600955# Package Functions
956# =================
957
958# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100959function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800960 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600961 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800962
963 if [[ -z "$base_dir" ]]; then
964 base_dir=$FILES
965 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600966 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800967 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600968 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800969 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600970 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800971 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600972 else
973 exit_distro_not_supported "list of packages"
974 fi
975 echo "$pkg_dir"
976}
977
978# Wrapper for ``apt-get`` to set cache and proxy environment variables
979# Uses globals ``OFFLINE``, ``*_proxy``
980# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100981function apt_get {
Ian Wienand433a9b12015-10-07 13:29:31 +1100982 local xtrace
983 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -0500984 set +o xtrace
985
Dean Troyerdff49a22014-01-30 15:37:40 -0600986 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
987 local sudo="sudo"
988 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500989
990 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500991
Dean Troyerdff49a22014-01-30 15:37:40 -0600992 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500993 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
994 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600995 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
996}
997
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800998function _parse_package_files {
999 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -06001000
Dean Troyerdff49a22014-01-30 15:37:40 -06001001 if [[ -z "$DISTRO" ]]; then
1002 GetDistro
1003 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001004
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001005 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001006 local OIFS line package distros distro
1007 [[ -e $fname ]] || continue
1008
1009 OIFS=$IFS
1010 IFS=$'\n'
1011 for line in $(<${fname}); do
1012 if [[ $line =~ "NOPRIME" ]]; then
1013 continue
1014 fi
1015
1016 # Assume we want this package
1017 package=${line%#*}
1018 inst_pkg=1
1019
1020 # Look for # dist:xxx in comment
1021 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1022 # We are using BASH regexp matching feature.
1023 package=${BASH_REMATCH[1]}
1024 distros=${BASH_REMATCH[2]}
1025 # In bash ${VAR,,} will lowecase VAR
1026 # Look for a match in the distro list
1027 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1028 # If no match then skip this package
1029 inst_pkg=0
1030 fi
1031 fi
1032
Dean Troyerdff49a22014-01-30 15:37:40 -06001033 if [[ $inst_pkg = 1 ]]; then
1034 echo $package
1035 fi
1036 done
1037 IFS=$OIFS
1038 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001039}
1040
1041# get_packages() collects a list of package names of any type from the
1042# prerequisite files in ``files/{debs|rpms}``. The list is intended
1043# to be passed to a package installer such as apt or yum.
1044#
1045# Only packages required for the services in 1st argument will be
1046# included. Two bits of metadata are recognized in the prerequisite files:
1047#
1048# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1049# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1050# of the package to the distros listed. The distro names are case insensitive.
1051function get_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001052 local xtrace
1053 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001054 set +o xtrace
1055 local services=$@
1056 local package_dir=$(_get_package_dir)
1057 local file_to_parse=""
1058 local service=""
1059
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001060 if [[ -z "$package_dir" ]]; then
1061 echo "No package directory supplied"
1062 return 1
1063 fi
1064 for service in ${services//,/ }; do
1065 # Allow individual services to specify dependencies
1066 if [[ -e ${package_dir}/${service} ]]; then
1067 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1068 fi
1069 # NOTE(sdague) n-api needs glance for now because that's where
1070 # glance client is
1071 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001072 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001073 file_to_parse="${file_to_parse} ${package_dir}/nova"
1074 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001075 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001076 file_to_parse="${file_to_parse} ${package_dir}/glance"
1077 fi
1078 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001079 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001080 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1081 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001082 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001083 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001084 file_to_parse="${file_to_parse} ${package_dir}/swift"
1085 fi
1086 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001087 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001088 file_to_parse="${file_to_parse} ${package_dir}/nova"
1089 fi
1090 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001091 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001092 file_to_parse="${file_to_parse} ${package_dir}/glance"
1093 fi
1094 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001095 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001096 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1097 fi
1098 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001099 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001100 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1101 fi
1102 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001103 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001104 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1105 fi
1106 fi
1107 done
1108 echo "$(_parse_package_files $file_to_parse)"
1109 $xtrace
1110}
1111
1112# get_plugin_packages() collects a list of package names of any type from a
1113# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1114# list is intended to be passed to a package installer such as apt or yum.
1115#
1116# Only packages required for enabled and collected plugins will included.
1117#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001118# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001119# in these prerequisite files, see get_packages() for more info.
1120function get_plugin_packages {
Ian Wienand433a9b12015-10-07 13:29:31 +11001121 local xtrace
1122 xtrace=$(set +o | grep xtrace)
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001123 set +o xtrace
1124 local files_to_parse=""
1125 local package_dir=""
1126 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1127 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1128 files_to_parse+="$package_dir/$plugin"
1129 done
1130 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001131 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001132}
1133
1134# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001135# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001136# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001137function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001138 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1139 REPOS_UPDATED=${REPOS_UPDATED:-False}
1140 RETRY_UPDATE=${RETRY_UPDATE:-False}
1141
Paul Linchpiner9e179742014-07-13 22:23:00 -07001142 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001143 return 0
1144 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001145
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001146 if is_ubuntu; then
Ian Wienand433a9b12015-10-07 13:29:31 +11001147 local xtrace
1148 xtrace=$(set +o | grep xtrace)
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001149 set +o xtrace
1150 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1151 # if there are transient errors pulling the updates, that's fine.
1152 # It may be secondary repositories that we don't really care about.
1153 apt_get update || /bin/true
1154 REPOS_UPDATED=True
1155 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001156 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001157 fi
1158}
1159
1160function real_install_package {
1161 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001162 apt_get install "$@"
1163 elif is_fedora; then
1164 yum_install "$@"
1165 elif is_suse; then
1166 zypper_install "$@"
1167 else
1168 exit_distro_not_supported "installing packages"
1169 fi
1170}
1171
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001172# Distro-agnostic package installer
1173# install_package package [package ...]
1174function install_package {
1175 update_package_repo
1176 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1177}
1178
Dean Troyerdff49a22014-01-30 15:37:40 -06001179# Distro-agnostic function to tell if a package is installed
1180# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001181function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001182 if [[ -z "$@" ]]; then
1183 return 1
1184 fi
1185
1186 if [[ -z "$os_PACKAGE" ]]; then
1187 GetOSVersion
1188 fi
1189
1190 if [[ "$os_PACKAGE" = "deb" ]]; then
1191 dpkg -s "$@" > /dev/null 2> /dev/null
1192 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1193 rpm --quiet -q "$@"
1194 else
1195 exit_distro_not_supported "finding if a package is installed"
1196 fi
1197}
1198
1199# Distro-agnostic package uninstaller
1200# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001201function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001202 if is_ubuntu; then
1203 apt_get purge "$@"
1204 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001205 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001206 elif is_suse; then
1207 sudo zypper rm "$@"
1208 else
1209 exit_distro_not_supported "uninstalling packages"
1210 fi
1211}
1212
1213# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001214# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001215# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001216function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001217 [[ "$OFFLINE" = "True" ]] && return
1218 local sudo="sudo"
1219 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001220
1221 # The manual check for missing packages is because yum -y assumes
1222 # missing packages are OK. See
1223 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001224 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1225 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001226 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001227 awk '
1228 BEGIN { fail=0 }
1229 /No package/ { fail=1 }
1230 { print }
1231 END { exit fail }' || \
1232 die $LINENO "Missing packages detected"
1233
1234 # also ensure we catch a yum failure
1235 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001236 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001237 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001238}
1239
1240# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001241# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001242# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001243function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001244 [[ "$OFFLINE" = "True" ]] && return
1245 local sudo="sudo"
1246 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001247 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1248 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001249 zypper --non-interactive install --auto-agree-with-licenses "$@"
1250}
1251
1252
1253# Process Functions
1254# =================
1255
1256# _run_process() is designed to be backgrounded by run_process() to simulate a
1257# fork. It includes the dirty work of closing extra filehandles and preparing log
1258# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001259# from the service name.
1260# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001261# If an optional group is provided sg will be used to set the group of
1262# the command.
1263# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001264function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001265 # disable tracing through the exec redirects, it's just confusing in the logs.
1266 xtrace=$(set +o | grep xtrace)
1267 set +o xtrace
1268
Dean Troyerdff49a22014-01-30 15:37:40 -06001269 local service=$1
1270 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001271 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001272
1273 # Undo logging redirections and close the extra descriptors
1274 exec 1>&3
1275 exec 2>&3
1276 exec 3>&-
1277 exec 6>&-
1278
Dean Troyerdde41d02014-12-09 17:47:57 -06001279 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1280 if [[ -n ${LOGDIR} ]]; then
1281 exec 1>&"$real_logfile" 2>&1
1282 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1283 if [[ -n ${SCREEN_LOGDIR} ]]; then
1284 # Drop the backward-compat symlink
1285 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1286 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001287
1288 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1289 export PYTHONUNBUFFERED=1
1290 fi
1291
Sean Dague6e137ab2015-04-29 08:22:24 -04001292 # reenable xtrace before we do *real* work
1293 $xtrace
1294
Dean Troyer3159a822014-08-27 14:13:58 -05001295 # Run under ``setsid`` to force the process to become a session and group leader.
1296 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001297 if [[ -n "$group" ]]; then
1298 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1299 else
1300 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1301 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001302
1303 # Just silently exit this process
1304 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001305}
1306
1307# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1308# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001309# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001310# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001311function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001312 SCREEN_NAME=${SCREEN_NAME:-stack}
1313 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1314
1315 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1316 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1317 fi
1318
1319 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1320}
1321
1322# Find out if a process exists by partial name.
1323# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001324function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001325 local name=$1
1326 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001327 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001328 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001329 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001330}
1331
Dean Troyer3159a822014-08-27 14:13:58 -05001332# Run a single service under screen or directly
1333# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001334# If an optional group is provided sg will be used to run the
1335# command as that group.
1336# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001337function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001338 local service=$1
1339 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001340 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001341
Dean Troyer3159a822014-08-27 14:13:58 -05001342 if is_service_enabled $service; then
1343 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001344 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001345 else
1346 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001347 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001348 fi
1349 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001350}
1351
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001352# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001353# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001354# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001355# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001356# Run a command in a shell in a screen window, if an optional group
1357# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001358function screen_process {
1359 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001360 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001361 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001362
Sean Dagueea22a4f2014-06-27 15:21:41 -04001363 SCREEN_NAME=${SCREEN_NAME:-stack}
1364 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001365 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001366
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001367 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001368
Dean Troyerdde41d02014-12-09 17:47:57 -06001369 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1370 echo "LOGDIR: $LOGDIR"
1371 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1372 echo "log: $real_logfile"
1373 if [[ -n ${LOGDIR} ]]; then
1374 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001375 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001376 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1377 if [[ -n ${SCREEN_LOGDIR} ]]; then
1378 # Drop the backward-compat symlink
1379 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1380 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001381 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001382
1383 # sleep to allow bash to be ready to be send the command - we are
1384 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001385 # bash isn't running by the time we send the command, nothing
1386 # happens. This sleep was added originally to handle gate runs
1387 # where we needed this to be at least 3 seconds to pass
1388 # consistently on slow clouds. Now this is configurable so that we
1389 # can determine a reasonable value for the local case which should
1390 # be much smaller.
1391 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001392
1393 NL=`echo -ne '\015'`
1394 # This fun command does the following:
1395 # - the passed server command is backgrounded
1396 # - the pid of the background process is saved in the usual place
1397 # - the server process is brought back to the foreground
1398 # - if the server process exits prematurely the fg command errors
1399 # and a message is written to stdout and the process failure file
1400 #
1401 # The pid saved can be used in stop_process() as a process group
1402 # id to kill off all child processes
1403 if [[ -n "$group" ]]; then
1404 command="sg $group '$command'"
1405 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001406
1407 # Append the process to the screen rc file
1408 screen_rc "$name" "$command"
1409
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001410 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 -06001411}
1412
1413# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001414# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001415# screen_rc service "command-line"
1416function screen_rc {
1417 SCREEN_NAME=${SCREEN_NAME:-stack}
1418 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1419 if [[ ! -e $SCREENRC ]]; then
1420 # Name the screen session
1421 echo "sessionname $SCREEN_NAME" > $SCREENRC
1422 # Set a reasonable statusbar
1423 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1424 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1425 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1426 echo "screen -t shell bash" >> $SCREENRC
1427 fi
1428 # If this service doesn't already exist in the screenrc file
1429 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1430 NL=`echo -ne '\015'`
1431 echo "screen -t $1 bash" >> $SCREENRC
1432 echo "stuff \"$2$NL\"" >> $SCREENRC
1433
Dean Troyerdde41d02014-12-09 17:47:57 -06001434 if [[ -n ${LOGDIR} ]]; then
1435 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001436 echo "log on" >>$SCREENRC
1437 fi
1438 fi
1439}
1440
1441# Stop a service in screen
1442# If a PID is available use it, kill the whole process group via TERM
1443# If screen is being used kill the screen window; this will catch processes
1444# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001445# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001446# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001447function screen_stop_service {
1448 local service=$1
1449
Dean Troyerdff49a22014-01-30 15:37:40 -06001450 SCREEN_NAME=${SCREEN_NAME:-stack}
1451 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001452 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001453
Dean Troyer3159a822014-08-27 14:13:58 -05001454 if is_service_enabled $service; then
1455 # Clean up the screen window
Attila Fazekasf750a6f2015-07-01 12:17:35 +02001456 screen -S $SCREEN_NAME -p $service -X kill || true
Dean Troyer3159a822014-08-27 14:13:58 -05001457 fi
1458}
1459
1460# Stop a service process
1461# If a PID is available use it, kill the whole process group via TERM
1462# If screen is being used kill the screen window; this will catch processes
1463# that did not leave a PID behind
1464# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1465# stop_process service
1466function stop_process {
1467 local service=$1
1468
1469 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001470 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001471
1472 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001473 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001474 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1475 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
Dan Smithce7246a2015-04-23 09:41:06 -07001476 # oslo.service tends to stop actually shutting down
1477 # reliably in between releases because someone believes it
1478 # is dying too early due to some inflight work they
1479 # have. This is a tension. It happens often enough we're
1480 # going to just account for it in devstack and assume it
1481 # doesn't work.
1482 #
1483 # Set OSLO_SERVICE_WORKS=True to skip this block
1484 if [[ -z "$OSLO_SERVICE_WORKS" ]]; then
1485 # TODO(danms): Remove this double-kill when we have
1486 # this fixed in all services:
1487 # https://bugs.launchpad.net/oslo-incubator/+bug/1446583
1488 sleep 1
1489 # /bin/true becakse pkill on a non existant process returns an error
1490 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid) || /bin/true
1491 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001492 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001493 fi
1494 if [[ "$USE_SCREEN" = "True" ]]; then
1495 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001496 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001497 fi
1498 fi
1499}
1500
1501# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001502# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001503# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001504function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001505 local service
1506 local failures
1507 SCREEN_NAME=${SCREEN_NAME:-stack}
1508 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1509
1510
1511 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1512 echo "No service status directory found"
1513 return
1514 fi
1515
Wei Jiangange3340f12015-09-21 17:52:14 +08001516 # Check if there is any failure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001517 # make this -o errexit safe
1518 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001519
1520 for service in $failures; do
1521 service=`basename $service`
1522 service=${service%.failure}
1523 echo "Error: Service $service is not running"
1524 done
1525
1526 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001527 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001528 fi
1529}
1530
Chris Dent2f27a0e2014-09-09 13:46:02 +01001531# Tail a log file in a screen if USE_SCREEN is true.
1532function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001533 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001534 local logfile=$2
1535
Sean Dague53753292014-12-04 19:38:15 -05001536 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001537 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001538 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001539 fi
1540}
1541
Dean Troyerdff49a22014-01-30 15:37:40 -06001542
Dean Troyer3159a822014-08-27 14:13:58 -05001543# Deprecated Functions
1544# --------------------
1545
1546# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1547# fork. It includes the dirty work of closing extra filehandles and preparing log
1548# files to produce the same logs as screen_it(). The log filename is derived
1549# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1550# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1551# _old_run_process service "command-line"
1552function _old_run_process {
1553 local service=$1
1554 local command="$2"
1555
1556 # Undo logging redirections and close the extra descriptors
1557 exec 1>&3
1558 exec 2>&3
1559 exec 3>&-
1560 exec 6>&-
1561
1562 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001563 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1564 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001565
1566 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1567 export PYTHONUNBUFFERED=1
1568 fi
1569
1570 exec /bin/bash -c "$command"
1571 die "$service exec failure: $command"
1572}
1573
1574# old_run_process() launches a child process that closes all file descriptors and
1575# then exec's the passed in command. This is meant to duplicate the semantics
1576# of screen_it() without screen. PIDs are written to
1577# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1578# old_run_process service "command-line"
1579function old_run_process {
1580 local service=$1
1581 local command="$2"
1582
1583 # Spawn the child process
1584 _old_run_process "$service" "$command" &
1585 echo $!
1586}
1587
1588# Compatibility for existing start_XXXX() functions
1589# Uses global ``USE_SCREEN``
1590# screen_it service "command-line"
1591function screen_it {
1592 if is_service_enabled $1; then
1593 # Append the service to the screen rc file
1594 screen_rc "$1" "$2"
1595
1596 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001597 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001598 else
1599 # Spawn directly without screen
1600 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1601 fi
1602 fi
1603}
1604
1605# Compatibility for existing stop_XXXX() functions
1606# Stop a service in screen
1607# If a PID is available use it, kill the whole process group via TERM
1608# If screen is being used kill the screen window; this will catch processes
1609# that did not leave a PID behind
1610# screen_stop service
1611function screen_stop {
1612 # Clean up the screen window
1613 stop_process $1
1614}
1615
1616
Sean Dague2c65e712014-12-18 09:44:56 -05001617# Plugin Functions
1618# =================
1619
1620DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1621
1622# enable_plugin <name> <url> [branch]
1623#
1624# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1625# ``url`` is a git url
1626# ``branch`` is a gitref. If it's not set, defaults to master
1627function enable_plugin {
1628 local name=$1
1629 local url=$2
1630 local branch=${3:-master}
1631 DEVSTACK_PLUGINS+=",$name"
1632 GITREPO[$name]=$url
1633 GITDIR[$name]=$DEST/$name
1634 GITBRANCH[$name]=$branch
1635}
1636
1637# fetch_plugins
1638#
1639# clones all plugins
1640function fetch_plugins {
1641 local plugins="${DEVSTACK_PLUGINS}"
1642 local plugin
1643
1644 # short circuit if nothing to do
1645 if [[ -z $plugins ]]; then
1646 return
1647 fi
1648
Dean Troyerdc97cb72015-03-28 08:20:50 -05001649 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001650 for plugin in ${plugins//,/ }; do
1651 git_clone_by_name $plugin
1652 done
1653}
1654
1655# load_plugin_settings
1656#
1657# Load settings from plugins in the order that they were registered
1658function load_plugin_settings {
1659 local plugins="${DEVSTACK_PLUGINS}"
1660 local plugin
1661
1662 # short circuit if nothing to do
1663 if [[ -z $plugins ]]; then
1664 return
1665 fi
1666
1667 echo "Loading plugin settings"
1668 for plugin in ${plugins//,/ }; do
1669 local dir=${GITDIR[$plugin]}
1670 # source any known settings
1671 if [[ -f $dir/devstack/settings ]]; then
1672 source $dir/devstack/settings
1673 fi
1674 done
1675}
1676
Sean Dague6e275e12015-03-26 05:54:28 -04001677# plugin_override_defaults
1678#
1679# Run an extremely early setting phase for plugins that allows default
1680# overriding of services.
1681function plugin_override_defaults {
1682 local plugins="${DEVSTACK_PLUGINS}"
1683 local plugin
1684
1685 # short circuit if nothing to do
1686 if [[ -z $plugins ]]; then
1687 return
1688 fi
1689
1690 echo "Overriding Configuration Defaults"
1691 for plugin in ${plugins//,/ }; do
1692 local dir=${GITDIR[$plugin]}
1693 # source any overrides
1694 if [[ -f $dir/devstack/override-defaults ]]; then
1695 # be really verbose that an override is happening, as it
1696 # may not be obvious if things fail later.
1697 echo "$plugin has overriden the following defaults"
1698 cat $dir/devstack/override-defaults
1699 source $dir/devstack/override-defaults
1700 fi
1701 done
1702}
1703
Sean Dague2c65e712014-12-18 09:44:56 -05001704# run_plugins
1705#
1706# Run the devstack/plugin.sh in all the plugin directories. These are
1707# run in registration order.
1708function run_plugins {
1709 local mode=$1
1710 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301711
1712 local plugins="${DEVSTACK_PLUGINS}"
1713 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001714 for plugin in ${plugins//,/ }; do
1715 local dir=${GITDIR[$plugin]}
1716 if [[ -f $dir/devstack/plugin.sh ]]; then
1717 source $dir/devstack/plugin.sh $mode $phase
1718 fi
1719 done
1720}
1721
1722function run_phase {
1723 local mode=$1
1724 local phase=$2
1725 if [[ -d $TOP_DIR/extras.d ]]; then
1726 for i in $TOP_DIR/extras.d/*.sh; do
1727 [[ -r $i ]] && source $i $mode $phase
Sean Dague1de9e332015-10-07 08:46:13 -04001728 # NOTE(sdague): generate a big warning about using
1729 # extras.d in an unsupported way which will let us track
1730 # unsupported usage in the gate.
1731 local exceptions="50-ironic.sh 60-ceph.sh 80-tempest.sh"
1732 local extra=$(basename $i)
1733 if [[ ! ( $exceptions =~ "$extra" ) ]]; then
1734 deprecated "extras.d support is being removed in Mitaka-1"
1735 deprecated "jobs for project $extra will break after that point"
1736 deprecated "please move project to a supported devstack plugin model"
1737 fi
Sean Dague2c65e712014-12-18 09:44:56 -05001738 done
1739 fi
1740 # the source phase corresponds to settings loading in plugins
1741 if [[ "$mode" == "source" ]]; then
1742 load_plugin_settings
Sean Dague6e275e12015-03-26 05:54:28 -04001743 elif [[ "$mode" == "override_defaults" ]]; then
1744 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001745 else
1746 run_plugins $mode $phase
1747 fi
1748}
1749
Dean Troyerdff49a22014-01-30 15:37:40 -06001750
1751# Service Functions
1752# =================
1753
1754# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1755# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001756function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001757 echo "$1" | sed -e '
1758 s/,,/,/g;
1759 s/^,//;
1760 s/,$//
1761 '
1762}
1763
1764# disable_all_services() removes all current services
1765# from ``ENABLED_SERVICES`` to reset the configuration
1766# before a minimal installation
1767# Uses global ``ENABLED_SERVICES``
1768# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001769function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001770 ENABLED_SERVICES=""
1771}
1772
1773# Remove all services starting with '-'. For example, to install all default
1774# services except rabbit (rabbit) set in ``localrc``:
1775# ENABLED_SERVICES+=",-rabbit"
1776# Uses global ``ENABLED_SERVICES``
1777# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001778function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001779 local to_remove=""
1780 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001781 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001782
1783 # build up list of services that should be removed; i.e. they
1784 # begin with "-"
1785 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001786 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001787 to_remove+=",${service#-}"
1788 else
1789 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001790 fi
1791 done
Ian Wienand2796a822015-04-15 08:59:04 +10001792
1793 # go through the service list. if this service appears in the "to
1794 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001795 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Dean Troyerdff49a22014-01-30 15:37:40 -06001796}
1797
1798# disable_service() removes the services passed as argument to the
1799# ``ENABLED_SERVICES`` list, if they are present.
1800#
1801# For example:
1802# disable_service rabbit
1803#
1804# This function does not know about the special cases
1805# for nova, glance, and neutron built into is_service_enabled().
1806# Uses global ``ENABLED_SERVICES``
1807# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001808function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001809 local tmpsvcs=",${ENABLED_SERVICES},"
1810 local service
1811 for service in $@; do
1812 if is_service_enabled $service; then
1813 tmpsvcs=${tmpsvcs//,$service,/,}
1814 fi
1815 done
1816 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1817}
1818
1819# enable_service() adds the services passed as argument to the
1820# ``ENABLED_SERVICES`` list, if they are not already present.
1821#
1822# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001823# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001824#
1825# This function does not know about the special cases
1826# for nova, glance, and neutron built into is_service_enabled().
1827# Uses global ``ENABLED_SERVICES``
1828# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001829function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001830 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001831 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001832 for service in $@; do
1833 if ! is_service_enabled $service; then
1834 tmpsvcs+=",$service"
1835 fi
1836 done
1837 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1838 disable_negated_services
1839}
1840
1841# is_service_enabled() checks if the service(s) specified as arguments are
1842# enabled by the user in ``ENABLED_SERVICES``.
1843#
1844# Multiple services specified as arguments are ``OR``'ed together; the test
1845# is a short-circuit boolean, i.e it returns on the first match.
1846#
1847# There are special cases for some 'catch-all' services::
1848# **nova** returns true if any service enabled start with **n-**
1849# **cinder** returns true if any service enabled start with **c-**
Dean Troyerdff49a22014-01-30 15:37:40 -06001850# **glance** returns true if any service enabled start with **g-**
1851# **neutron** returns true if any service enabled start with **q-**
1852# **swift** returns true if any service enabled start with **s-**
1853# **trove** returns true if any service enabled start with **tr-**
1854# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1855# **s-** services will be enabled. This will be deprecated in the future.
1856#
1857# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1858# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1859# as enabled in this case.
1860#
1861# Uses global ``ENABLED_SERVICES``
1862# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001863function is_service_enabled {
Ian Wienand433a9b12015-10-07 13:29:31 +11001864 local xtrace
1865 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001866 set +o xtrace
1867 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001868 local services=$@
1869 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001870 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001871 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001872
1873 # Look for top-level 'enabled' function for this service
1874 if type is_${service}_enabled >/dev/null 2>&1; then
1875 # A function exists for this service, use it
Christian Schwede3db0aad2015-09-10 11:15:39 +00001876 is_${service}_enabled && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001877 fi
1878
1879 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1880 # are implemented
1881
Sean Dague45917cc2014-02-24 16:09:14 -05001882 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001883 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001884 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001885 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1886 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1887 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1888 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1889 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1890 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001891 done
Sean Dague45917cc2014-02-24 16:09:14 -05001892 $xtrace
1893 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001894}
1895
fumihiko kakuma8606c982015-04-13 09:55:06 +09001896# remove specified list from the input string
1897# remove_disabled_services service-list remove-list
1898function remove_disabled_services {
1899 local service_list=$1
1900 local remove_list=$2
1901 local service
1902 local enabled=""
1903
1904 for service in ${service_list//,/ }; do
1905 local remove
1906 local add=1
1907 for remove in ${remove_list//,/ }; do
1908 if [[ ${remove} == ${service} ]]; then
1909 add=0
1910 break
1911 fi
1912 done
1913 if [[ $add == 1 ]]; then
1914 enabled="${enabled},$service"
1915 fi
1916 done
1917 _cleanup_service_list "$enabled"
1918}
1919
Dean Troyerdff49a22014-01-30 15:37:40 -06001920# Toggle enable/disable_service for services that must run exclusive of each other
1921# $1 The name of a variable containing a space-separated list of services
1922# $2 The name of a variable in which to store the enabled service's name
1923# $3 The name of the service to enable
1924function use_exclusive_service {
1925 local options=${!1}
1926 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001927 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001928 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001929 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001930 for opt in $options;do
1931 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1932 done
1933 eval "$out=$selection"
1934 return 0
1935}
1936
1937
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001938# System Functions
1939# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001940
1941# Only run the command if the target file (the last arg) is not on an
1942# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001943function _safe_permission_operation {
Ian Wienand433a9b12015-10-07 13:29:31 +11001944 local xtrace
1945 xtrace=$(set +o | grep xtrace)
Sean Dague45917cc2014-02-24 16:09:14 -05001946 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001947 local args=( $@ )
1948 local last
1949 local sudo_cmd
1950 local dir_to_check
1951
1952 let last="${#args[*]} - 1"
1953
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001954 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001955 if [ ! -d "$dir_to_check" ]; then
1956 dir_to_check=`dirname "$dir_to_check"`
1957 fi
1958
1959 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001960 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001961 return 0
1962 fi
1963
1964 if [[ $TRACK_DEPENDS = True ]]; then
1965 sudo_cmd="env"
1966 else
1967 sudo_cmd="sudo"
1968 fi
1969
Sean Dague45917cc2014-02-24 16:09:14 -05001970 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001971 $sudo_cmd $@
1972}
1973
1974# Exit 0 if address is in network or 1 if address is not in network
1975# ip-range is in CIDR notation: 1.2.3.4/20
1976# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001977function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001978 local ip=$1
1979 local range=$2
1980 local masklen=${range#*/}
1981 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1982 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1983 [[ $network == $subnet ]]
1984}
1985
1986# Add a user to a group.
1987# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001988function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001989 local user=$1
1990 local group=$2
1991
Thomas Bechtolda8580852015-05-31 00:04:33 +02001992 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06001993}
1994
1995# Convert CIDR notation to a IPv4 netmask
1996# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001997function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001998 local maskpat="255 255 255 255"
1999 local maskdgt="254 252 248 240 224 192 128"
2000 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2001 echo ${1-0}.${2-0}.${3-0}.${4-0}
2002}
2003
2004# Gracefully cp only if source file/dir exists
2005# cp_it source destination
2006function cp_it {
2007 if [ -e $1 ] || [ -d $1 ]; then
2008 cp -pRL $1 $2
2009 fi
2010}
2011
2012# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2013# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2014# ``localrc`` or on the command line if necessary::
2015#
2016# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2017#
2018# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2019
Ian Wienandaee18c72014-02-21 15:35:08 +11002020function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05002021 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002022 export http_proxy=$http_proxy
2023 fi
Sean Dague53753292014-12-04 19:38:15 -05002024 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002025 export https_proxy=$https_proxy
2026 fi
Sean Dague53753292014-12-04 19:38:15 -05002027 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06002028 export no_proxy=$no_proxy
2029 fi
2030}
2031
2032# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002033function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06002034 local mount_type=`stat -f -L -c %T $1`
2035 test "$mount_type" == "nfs"
2036}
2037
2038# Return the network portion of the given IP address using netmask
2039# netmask is in the traditional dotted-quad format
2040# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002041function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002042 local ip=$1
2043 local mask=$2
2044 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2045 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2046 echo $subnet
2047}
2048
Chris Dent3a2c86a2015-05-12 13:41:25 +00002049# Return the current python as "python<major>.<minor>"
2050function python_version {
2051 local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
2052 echo "python${python_version}"
2053}
2054
Dean Troyerdff49a22014-01-30 15:37:40 -06002055# Service wrapper to restart services
2056# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002057function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002058 if is_ubuntu; then
2059 sudo /usr/sbin/service $1 restart
2060 else
2061 sudo /sbin/service $1 restart
2062 fi
2063}
2064
2065# Only change permissions of a file or directory if it is not on an
2066# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002067function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002068 _safe_permission_operation chmod $@
2069}
2070
2071# Only change ownership of a file or directory if it is not on an NFS
2072# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002073function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002074 _safe_permission_operation chown $@
2075}
2076
2077# Service wrapper to start services
2078# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002079function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002080 if is_ubuntu; then
2081 sudo /usr/sbin/service $1 start
2082 else
2083 sudo /sbin/service $1 start
2084 fi
2085}
2086
2087# Service wrapper to stop services
2088# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002089function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002090 if is_ubuntu; then
2091 sudo /usr/sbin/service $1 stop
2092 else
2093 sudo /sbin/service $1 stop
2094 fi
2095}
2096
Sean Dague442e4e92015-06-24 13:24:02 -04002097# Test with a finite retry loop.
2098#
2099function test_with_retry {
2100 local testcmd=$1
2101 local failmsg=$2
2102 local until=${3:-10}
2103 local sleep=${4:-0.5}
2104
2105 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2106 die $LINENO "$failmsg"
2107 fi
2108}
2109
Dean Troyerdff49a22014-01-30 15:37:40 -06002110
2111# Restore xtrace
2112$XTRACE
2113
2114# Local variables:
2115# mode: shell-script
2116# End: