blob: 473808b1f56beff2afea017b631316ee0ed18607 [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
70# Normalize config values to True or False
71# Accepts as False: 0 no No NO false False FALSE
72# Accepts as True: 1 yes Yes YES true True TRUE
73# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +110074function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -050075 local xtrace=$(set +o | grep xtrace)
76 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060077
Mahito OGURA98f59aa2015-05-11 18:02:34 +090078 local default=$1
79 local testval=${!2:-}
80
81 case "$testval" in
82 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
83 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
84 * ) echo "$default" ;;
85 esac
86
Sean Dague45917cc2014-02-24 16:09:14 -050087 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060088}
89
Attila Fazekas1bd79592015-02-24 14:06:56 +010090function isset {
91 [[ -v "$1" ]]
92}
Dean Troyerdff49a22014-01-30 15:37:40 -060093
Dean Troyer68162342015-05-13 15:41:03 -050094
Dean Troyerdff49a22014-01-30 15:37:40 -060095# Control Functions
96# =================
97
98# Prints backtrace info
99# filename:lineno:function
100# backtrace level
101function backtrace {
102 local level=$1
103 local deep=$((${#BASH_SOURCE[@]} - 1))
104 echo "[Call Trace]"
105 while [ $level -le $deep ]; do
106 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
107 deep=$((deep - 1))
108 done
109}
110
111# Prints line number and "message" then exits
112# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100113function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600114 local exitcode=$?
115 set +o xtrace
116 local line=$1; shift
117 if [ $exitcode == 0 ]; then
118 exitcode=1
119 fi
120 backtrace 2
121 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600122 # Give buffers a second to flush
123 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600124 exit $exitcode
125}
126
127# Checks an environment variable is not set or has length 0 OR if the
128# exit code is non-zero and prints "message" and exits
129# NOTE: env-var is the variable name without a '$'
130# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100131function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600132 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500133 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600134 set +o xtrace
135 local line=$1; shift
136 local evar=$1; shift
137 if ! is_set $evar || [ $exitcode != 0 ]; then
138 die $line "$*"
139 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500140 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600141}
142
143# Prints line number and "message" in error format
144# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100145function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600146 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500147 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600148 set +o xtrace
149 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
150 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600151 if [[ -n ${LOGDIR} ]]; then
152 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600153 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500154 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600155 return $exitcode
156}
157
158# Checks an environment variable is not set or has length 0 OR if the
159# exit code is non-zero and prints "message"
160# NOTE: env-var is the variable name without a '$'
161# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100162function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600163 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500164 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600165 set +o xtrace
166 local line=$1; shift
167 local evar=$1; shift
168 if ! is_set $evar || [ $exitcode != 0 ]; then
169 err $line "$*"
170 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500171 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600172 return $exitcode
173}
174
175# Exit after outputting a message about the distribution not being supported.
176# exit_distro_not_supported [optional-string-telling-what-is-missing]
177function exit_distro_not_supported {
178 if [[ -z "$DISTRO" ]]; then
179 GetDistro
180 fi
181
182 if [ $# -gt 0 ]; then
183 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
184 else
185 die $LINENO "Support for $DISTRO is incomplete."
186 fi
187}
188
189# Test if the named environment variable is set and not zero length
190# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100191function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600192 local var=\$"$1"
193 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
194}
195
196# Prints line number and "message" in warning format
197# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100198function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600199 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500200 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600201 set +o xtrace
202 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Sean Daguee4af9292015-04-28 08:57:57 -0400203 echo $msg
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500204 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600205 return $exitcode
206}
207
208
209# Distro Functions
210# ================
211
212# Determine OS Vendor, Release and Update
213# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
214# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500215# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
216# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
217# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
218# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
219# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500220os_VENDOR=""
221os_RELEASE=""
222os_UPDATE=""
223os_PACKAGE=""
224os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500225
Dean Troyerdff49a22014-01-30 15:37:40 -0600226# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100227function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500228
Dean Troyerdff49a22014-01-30 15:37:40 -0600229 # Figure out which vendor we are
230 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
231 # OS/X
232 os_VENDOR=`sw_vers -productName`
233 os_RELEASE=`sw_vers -productVersion`
234 os_UPDATE=${os_RELEASE##*.}
235 os_RELEASE=${os_RELEASE%.*}
236 os_PACKAGE=""
237 if [[ "$os_RELEASE" =~ "10.7" ]]; then
238 os_CODENAME="lion"
239 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
240 os_CODENAME="snow leopard"
241 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
242 os_CODENAME="leopard"
243 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
244 os_CODENAME="tiger"
245 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
246 os_CODENAME="panther"
247 else
248 os_CODENAME=""
249 fi
250 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
251 os_VENDOR=$(lsb_release -i -s)
252 os_RELEASE=$(lsb_release -r -s)
253 os_UPDATE=""
254 os_PACKAGE="rpm"
255 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
256 os_PACKAGE="deb"
257 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
258 lsb_release -d -s | grep -q openSUSE
259 if [[ $? -eq 0 ]]; then
260 os_VENDOR="openSUSE"
261 fi
262 elif [[ $os_VENDOR == "openSUSE project" ]]; then
263 os_VENDOR="openSUSE"
264 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
265 os_VENDOR="Red Hat"
266 fi
267 os_CODENAME=$(lsb_release -c -s)
268 elif [[ -r /etc/redhat-release ]]; then
269 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
270 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
271 # CentOS release 5.5 (Final)
272 # CentOS Linux release 6.0 (Final)
273 # Fedora release 16 (Verne)
274 # XenServer release 6.2.0-70446c (xenenterprise)
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700275 # Oracle Linux release 7
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300276 # CloudLinux release 7.1
Dean Troyerdff49a22014-01-30 15:37:40 -0600277 os_CODENAME=""
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300278 for r in "Red Hat" CentOS Fedora XenServer CloudLinux; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600279 os_VENDOR=$r
280 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
281 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
282 os_CODENAME=${ver#*|}
283 os_RELEASE=${ver%|*}
284 os_UPDATE=${os_RELEASE##*.}
285 os_RELEASE=${os_RELEASE%.*}
286 break
287 fi
288 os_VENDOR=""
289 done
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700290 if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
291 os_VENDOR=OracleLinux
292 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600293 os_PACKAGE="rpm"
294 elif [[ -r /etc/SuSE-release ]]; then
295 for r in openSUSE "SUSE Linux"; do
296 if [[ "$r" = "SUSE Linux" ]]; then
297 os_VENDOR="SUSE LINUX"
298 else
299 os_VENDOR=$r
300 fi
301
302 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
303 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
304 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
305 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
306 break
307 fi
308 os_VENDOR=""
309 done
310 os_PACKAGE="rpm"
311 # If lsb_release is not installed, we should be able to detect Debian OS
312 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
313 os_VENDOR="Debian"
314 os_PACKAGE="deb"
315 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
316 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
317 fi
318 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
319}
320
321# Translate the OS version values into common nomenclature
322# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500323declare DISTRO
324
Ian Wienandaee18c72014-02-21 15:35:08 +1100325function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600326 GetOSVersion
327 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
328 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
329 DISTRO=$os_CODENAME
330 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
331 # For Fedora, just use 'f' and the release
332 DISTRO="f$os_RELEASE"
333 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
334 DISTRO="opensuse-$os_RELEASE"
335 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
336 # For SLE, also use the service pack
337 if [[ -z "$os_UPDATE" ]]; then
338 DISTRO="sle${os_RELEASE}"
339 else
340 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
341 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530342 elif [[ "$os_VENDOR" =~ (Red Hat) || \
343 "$os_VENDOR" =~ (CentOS) || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700344 "$os_VENDOR" =~ (OracleLinux) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600345 # Drop the . release as we assume it's compatible
346 DISTRO="rhel${os_RELEASE::1}"
347 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
348 DISTRO="xs$os_RELEASE"
349 else
350 # Catch-all for now is Vendor + Release + Update
351 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
352 fi
353 export DISTRO
354}
355
356# Utility function for checking machine architecture
357# is_arch arch-type
358function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500359 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600360}
361
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700362# Determine if current distribution is an Oracle distribution
363# is_oraclelinux
364function is_oraclelinux {
365 if [[ -z "$os_VENDOR" ]]; then
366 GetOSVersion
367 fi
368
369 [ "$os_VENDOR" = "OracleLinux" ]
370}
371
372
Dean Troyerdff49a22014-01-30 15:37:40 -0600373# Determine if current distribution is a Fedora-based distribution
374# (Fedora, RHEL, CentOS, etc).
375# is_fedora
376function is_fedora {
377 if [[ -z "$os_VENDOR" ]]; then
378 GetOSVersion
379 fi
380
anju Tiwari6c639c92014-07-15 18:11:54 +0530381 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300382 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ] || \
383 [ "$os_VENDOR" = "CloudLinux" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600384}
385
386
387# Determine if current distribution is a SUSE-based distribution
388# (openSUSE, SLE).
389# is_suse
390function is_suse {
391 if [[ -z "$os_VENDOR" ]]; then
392 GetOSVersion
393 fi
394
395 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
396}
397
398
399# Determine if current distribution is an Ubuntu-based distribution
400# It will also detect non-Ubuntu but Debian-based distros
401# is_ubuntu
402function is_ubuntu {
403 if [[ -z "$os_PACKAGE" ]]; then
404 GetOSVersion
405 fi
406 [ "$os_PACKAGE" = "deb" ]
407}
408
409
410# Git Functions
411# =============
412
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600413# Returns openstack release name for a given branch name
414# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100415function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600416 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700417 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600418 echo ${branch#*/}
419 else
420 echo "master"
421 fi
422}
423
Dean Troyerdff49a22014-01-30 15:37:40 -0600424# git clone only if directory doesn't exist already. Since ``DEST`` might not
425# be owned by the installation user, we create the directory and change the
426# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500427# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
428# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600429# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500430# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600431# git_clone remote dest-dir branch
432function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500433 local git_remote=$1
434 local git_dest=$2
435 local git_ref=$3
436 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200437 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500438
Sean Dague53753292014-12-04 19:38:15 -0500439 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800440 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200441 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
442 fi
443
Dean Troyerdff49a22014-01-30 15:37:40 -0600444 if [[ "$OFFLINE" = "True" ]]; then
445 echo "Running in offline mode, clones already exist"
446 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500447 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600448 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400449 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600450 return
451 fi
452
Dean Troyer50cda692014-07-25 11:57:20 -0500453 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600454 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500455 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600456 [[ "$ERROR_ON_CLONE" = "True" ]] && \
457 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200458 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600459 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500460 cd $git_dest
461 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600462 else
463 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500464 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600465 [[ "$ERROR_ON_CLONE" = "True" ]] && \
466 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200467 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500468 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600469 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500470 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600471 elif [[ "$RECLONE" = "True" ]]; then
472 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500473 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600474 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500475 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100476 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600477 # remove the existing ignored files (like pyc) as they cause breakage
478 # (due to the py files having older timestamps than our pyc, so python
479 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500480 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600481
Dean Troyer50cda692014-07-25 11:57:20 -0500482 # handle git_ref accordingly to type (tag, branch)
483 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
484 git_update_tag $git_ref
485 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
486 git_update_branch $git_ref
487 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
488 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600489 else
Dean Troyer50cda692014-07-25 11:57:20 -0500490 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600491 fi
492
493 fi
494 fi
495
496 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500497 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600498 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400499 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600500}
501
Sean Daguecc524062014-10-01 09:06:43 -0400502# A variation on git clone that lets us specify a project by it's
503# actual name, like oslo.config. This is exceptionally useful in the
504# library installation case
505function git_clone_by_name {
506 local name=$1
507 local repo=${GITREPO[$name]}
508 local dir=${GITDIR[$name]}
509 local branch=${GITBRANCH[$name]}
510 git_clone $repo $dir $branch
511}
512
513
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100514# git can sometimes get itself infinitely stuck with transient network
515# errors or other issues with the remote end. This wraps git in a
516# timeout/retry loop and is intended to watch over non-local git
517# processes that might hang. GIT_TIMEOUT, if set, is passed directly
518# to timeout(1); otherwise the default value of 0 maintains the status
519# quo of waiting forever.
520# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100521function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100522 local count=0
523 local timeout=0
524
525 if [[ -n "${GIT_TIMEOUT}" ]]; then
526 timeout=${GIT_TIMEOUT}
527 fi
528
529 until timeout -s SIGINT ${timeout} git "$@"; do
530 # 124 is timeout(1)'s special return code when it reached the
531 # timeout; otherwise assume fatal failure
532 if [[ $? -ne 124 ]]; then
533 die $LINENO "git call failed: [git $@]"
534 fi
535
536 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400537 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100538 if [ $count -eq 3 ]; then
539 die $LINENO "Maximum of 3 git retries reached"
540 fi
541 sleep 5
542 done
543}
544
Dean Troyerdff49a22014-01-30 15:37:40 -0600545# git update using reference as a branch.
546# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100547function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500548 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600549
Dean Troyer50cda692014-07-25 11:57:20 -0500550 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600551 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500552 git branch -D $git_branch || true
553 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600554}
555
556# git update using reference as a branch.
557# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100558function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500559 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600560
Dean Troyer50cda692014-07-25 11:57:20 -0500561 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600562}
563
564# git update using reference as a tag. Be careful editing source at that repo
565# as working copy will be in a detached mode
566# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100567function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500568 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600569
Dean Troyer50cda692014-07-25 11:57:20 -0500570 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600571 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500572 git_timed fetch origin tag $git_tag
573 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600574}
575
576
577# OpenStack Functions
578# ===================
579
580# Get the default value for HOST_IP
581# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100582function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600583 local fixed_range=$1
584 local floating_range=$2
585 local host_ip_iface=$3
586 local host_ip=$4
Brian Haley180f5eb2015-06-16 13:14:31 -0400587 local af=$5
Dean Troyerdff49a22014-01-30 15:37:40 -0600588
Dean Troyerdff49a22014-01-30 15:37:40 -0600589 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
590 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
591 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100592 # Find the interface used for the default route
Brian Haley180f5eb2015-06-16 13:14:31 -0400593 host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
Sean M. Collinsd20435b2015-08-25 19:24:51 -0400594 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 -0500595 local ip
596 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600597 # Attempt to filter out IP addresses that are part of the fixed and
598 # floating range. Note that this method only works if the ``netaddr``
599 # python library is installed. If it is not installed, an error
600 # will be printed and the first IP from the interface will be used.
601 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
602 # address.
Brian Haley180f5eb2015-06-16 13:14:31 -0400603 if [[ "$af" == "inet6" ]]; then
604 host_ip=$ip
605 break;
606 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500607 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
608 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600609 break;
610 fi
611 done
612 fi
613 echo $host_ip
614}
615
Attila Fazekasf71b5002014-05-28 09:52:22 +0200616# Generates hex string from ``size`` byte of pseudo random data
617# generate_hex_string size
618function generate_hex_string {
619 local size=$1
620 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
621}
622
Dean Troyerdff49a22014-01-30 15:37:40 -0600623# Grab a numbered field from python prettytable output
624# Fields are numbered starting with 1
625# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
626# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100627function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500628 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600629 while read data; do
630 if [ "$1" -lt 0 ]; then
631 field="(\$(NF$1))"
632 else
633 field="\$$(($1 + 1))"
634 fi
635 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
636 done
637}
638
yuntongjinf26deea2015-02-28 10:50:34 +0800639# install default policy
640# copy over a default policy.json and policy.d for projects
641function install_default_policy {
642 local project=$1
643 local project_uc=$(echo $1|tr a-z A-Z)
644 local conf_dir="${project_uc}_CONF_DIR"
645 # eval conf dir to get the variable
646 conf_dir="${!conf_dir}"
647 local project_dir="${project_uc}_DIR"
648 # eval project dir to get the variable
649 project_dir="${!project_dir}"
650 local sample_conf_dir="${project_dir}/etc/${project}"
651 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
652
653 # first copy any policy.json
654 cp -p $sample_conf_dir/policy.json $conf_dir
655 # then optionally copy over policy.d
656 if [[ -d $sample_policy_dir ]]; then
657 cp -r $sample_policy_dir $conf_dir/policy.d
658 fi
659}
660
Dean Troyerdff49a22014-01-30 15:37:40 -0600661# Add a policy to a policy.json file
662# Do nothing if the policy already exists
663# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100664function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600665 local policy_file=$1
666 local policy_name=$2
667 local policy_perm=$3
668
669 if grep -q ${policy_name} ${policy_file}; then
670 echo "Policy ${policy_name} already exists in ${policy_file}"
671 return
672 fi
673
674 # Add a terminating comma to policy lines without one
675 # Remove the closing '}' and all lines following to the end-of-file
676 local tmpfile=$(mktemp)
677 uniq ${policy_file} | sed -e '
678 s/]$/],/
679 /^[}]/,$d
680 ' > ${tmpfile}
681
682 # Append policy and closing brace
683 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
684 echo "}" >>${tmpfile}
685
686 mv ${tmpfile} ${policy_file}
687}
688
Alistair Coles24779f62014-10-15 18:57:59 +0100689# Gets or creates a domain
690# Usage: get_or_create_domain <name> <description>
691function get_or_create_domain {
Ian Wienand11d276c2015-07-02 09:34:34 +1000692 local domain_id
Alistair Coles24779f62014-10-15 18:57:59 +0100693 # Gets domain id
Ian Wienand11d276c2015-07-02 09:34:34 +1000694 domain_id=$(
Alistair Coles24779f62014-10-15 18:57:59 +0100695 # Gets domain id
Jamie Lennox4b115ad2015-05-29 08:36:40 +0000696 openstack domain show $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100697 -f value -c id 2>/dev/null ||
698 # Creates new domain
Jamie Lennox4b115ad2015-05-29 08:36:40 +0000699 openstack domain create $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100700 --description "$2" \
701 -f value -c id
702 )
703 echo $domain_id
704}
705
Steve Martinellib74e01c2014-12-18 01:35:35 -0500706# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000707# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500708function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500709 local desc="${3:-}"
Ian Wienand11d276c2015-07-02 09:34:34 +1000710 local group_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500711 # Gets group id
Ian Wienand11d276c2015-07-02 09:34:34 +1000712 group_id=$(
Steve Martinellib74e01c2014-12-18 01:35:35 -0500713 # Creates new group with --or-show
Jamie Lennox4b115ad2015-05-29 08:36:40 +0000714 openstack group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000715 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500716 -f value -c id
717 )
718 echo $group_id
719}
720
Bartosz Górski0abde392014-02-28 14:15:19 +0100721# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000722# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100723function get_or_create_user {
Ian Wienand11d276c2015-07-02 09:34:34 +1000724 local user_id
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000725 if [[ ! -z "$4" ]]; then
726 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200727 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500728 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200729 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100730 # Gets user id
Ian Wienand11d276c2015-07-02 09:34:34 +1000731 user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500732 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000733 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100734 $1 \
735 --password "$2" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000736 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500737 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500738 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100739 -f value -c id
740 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500741 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100742}
743
744# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000745# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100746function get_or_create_project {
Ian Wienand11d276c2015-07-02 09:34:34 +1000747 local project_id
748 project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500749 # Creates new project with --or-show
Jamie Lennox4b115ad2015-05-29 08:36:40 +0000750 openstack project create $1 \
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000751 --domain=$2 \
752 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100753 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500754 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100755}
756
757# Gets or creates role
758# Usage: get_or_create_role <name>
759function get_or_create_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000760 local role_id
761 role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500762 # Creates role with --or-show
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000763 openstack role create $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000764 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100765 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500766 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100767}
768
Jamie Lennox9b215db2015-02-10 18:19:57 +1100769# Gets or adds user role to project
770# Usage: get_or_add_user_project_role <role> <user> <project>
771function get_or_add_user_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000772 local user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100773 # Gets user role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000774 user_role_id=$(openstack role list \
Steve Martinelli5541a612015-01-19 15:58:49 -0500775 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100776 --column "ID" \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000777 --project $3 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100778 --column "Name" \
779 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500780 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100781 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500782 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100783 $1 \
784 --user $2 \
785 --project $3 \
786 | grep " id " | get_field 2)
787 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500788 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100789}
790
Steve Martinelli4599fd12015-03-12 21:30:58 -0400791# Gets or adds group role to project
792# Usage: get_or_add_group_project_role <role> <group> <project>
793function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000794 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -0400795 # Gets group role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000796 group_role_id=$(openstack role list \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400797 --group $2 \
798 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000799 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400800 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000801 # Adds role to group and get it
802 openstack role add $1 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000803 --group $2 \
804 --project $3
805 group_role_id=$(openstack role list \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400806 --group $2 \
807 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000808 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400809 fi
810 echo $group_role_id
811}
812
Bartosz Górski0abde392014-02-28 14:15:19 +0100813# Gets or creates service
814# Usage: get_or_create_service <name> <type> <description>
815function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +1000816 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100817 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +1000818 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100819 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +1000820 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100821 # Creates new service if not exists
822 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500823 $2 \
824 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100825 --description="$3" \
826 -f value -c id
827 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500828 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100829}
830
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000831# Create an endpoint with a specific interface
832# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
833function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +1000834 local endpoint_id
Daniel Gonzalez1991e752015-08-11 19:34:22 +0200835 # TODO(dgonzalez): The check of the region name, as done in the grep
836 # statement below, exists only because keystone does currently
837 # not allow filtering the region name when listing endpoints. If keystone
838 # gets support for this, the check for the region name can be removed.
839 # Related bug in keystone: https://bugs.launchpad.net/keystone/+bug/1482772
Ian Wienand11d276c2015-07-02 09:34:34 +1000840 endpoint_id=$(openstack endpoint list \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000841 --service $1 \
842 --interface $2 \
843 --region $4 \
Daniel Gonzalez1991e752015-08-11 19:34:22 +0200844 -c ID -c Region -f value | grep $4 | cut -f 1 -d " ")
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500845 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100846 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500847 endpoint_id=$(openstack endpoint create \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000848 $1 $2 $3 --region $4 -f value -c id)
Bartosz Górski0abde392014-02-28 14:15:19 +0100849 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000850
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500851 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100852}
Dean Troyerdff49a22014-01-30 15:37:40 -0600853
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000854# Gets or creates endpoint
855# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
856function get_or_create_endpoint {
857 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
858 # creating one endpoint with multiple urls to multiple endpoints each with
859 # a different interface. To maintain the existing function interface we
860 # create 3 endpoints and return the id of the public one. In reality
861 # returning the public id will not make a lot of difference as there are no
862 # scenarios currently that use the returned id. Ideally this behaviour
863 # should be pushed out to the service setups and let them create the
864 # endpoints they need.
865 local public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
866 _get_or_create_endpoint_with_interface $1 admin $4 $2
867 _get_or_create_endpoint_with_interface $1 internal $5 $2
868
869 # return the public id to indicate success, and this is the endpoint most likely wanted
870 echo $public_id
871}
872
873# Get a URL from the identity service
874# Usage: get_endpoint_url <service> <interface>
875function get_endpoint_url {
876 echo $(openstack endpoint list \
877 --service $1 --interface $2 \
878 --os-url $KEYSTONE_SERVICE_URI_V3 \
879 --os-identity-api-version=3 \
880 -c URL -f value)
881}
882
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500883
Dean Troyerdff49a22014-01-30 15:37:40 -0600884# Package Functions
885# =================
886
887# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100888function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800889 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600890 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800891
892 if [[ -z "$base_dir" ]]; then
893 base_dir=$FILES
894 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600895 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800896 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600897 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800898 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600899 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800900 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600901 else
902 exit_distro_not_supported "list of packages"
903 fi
904 echo "$pkg_dir"
905}
906
907# Wrapper for ``apt-get`` to set cache and proxy environment variables
908# Uses globals ``OFFLINE``, ``*_proxy``
909# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100910function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500911 local xtrace=$(set +o | grep xtrace)
912 set +o xtrace
913
Dean Troyerdff49a22014-01-30 15:37:40 -0600914 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
915 local sudo="sudo"
916 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500917
918 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500919
Dean Troyerdff49a22014-01-30 15:37:40 -0600920 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500921 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
922 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600923 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
924}
925
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800926function _parse_package_files {
927 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600928
Dean Troyerdff49a22014-01-30 15:37:40 -0600929 if [[ -z "$DISTRO" ]]; then
930 GetDistro
931 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600932
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800933 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600934 local OIFS line package distros distro
935 [[ -e $fname ]] || continue
936
937 OIFS=$IFS
938 IFS=$'\n'
939 for line in $(<${fname}); do
940 if [[ $line =~ "NOPRIME" ]]; then
941 continue
942 fi
943
944 # Assume we want this package
945 package=${line%#*}
946 inst_pkg=1
947
948 # Look for # dist:xxx in comment
949 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
950 # We are using BASH regexp matching feature.
951 package=${BASH_REMATCH[1]}
952 distros=${BASH_REMATCH[2]}
953 # In bash ${VAR,,} will lowecase VAR
954 # Look for a match in the distro list
955 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
956 # If no match then skip this package
957 inst_pkg=0
958 fi
959 fi
960
Dean Troyerdff49a22014-01-30 15:37:40 -0600961 if [[ $inst_pkg = 1 ]]; then
962 echo $package
963 fi
964 done
965 IFS=$OIFS
966 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800967}
968
969# get_packages() collects a list of package names of any type from the
970# prerequisite files in ``files/{debs|rpms}``. The list is intended
971# to be passed to a package installer such as apt or yum.
972#
973# Only packages required for the services in 1st argument will be
974# included. Two bits of metadata are recognized in the prerequisite files:
975#
976# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
977# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
978# of the package to the distros listed. The distro names are case insensitive.
979function get_packages {
980 local xtrace=$(set +o | grep xtrace)
981 set +o xtrace
982 local services=$@
983 local package_dir=$(_get_package_dir)
984 local file_to_parse=""
985 local service=""
986
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800987 if [[ -z "$package_dir" ]]; then
988 echo "No package directory supplied"
989 return 1
990 fi
991 for service in ${services//,/ }; do
992 # Allow individual services to specify dependencies
993 if [[ -e ${package_dir}/${service} ]]; then
994 file_to_parse="${file_to_parse} ${package_dir}/${service}"
995 fi
996 # NOTE(sdague) n-api needs glance for now because that's where
997 # glance client is
998 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700999 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001000 file_to_parse="${file_to_parse} ${package_dir}/nova"
1001 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001002 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001003 file_to_parse="${file_to_parse} ${package_dir}/glance"
1004 fi
1005 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001006 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001007 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1008 fi
1009 elif [[ $service == ceilometer-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001010 if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001011 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
1012 fi
1013 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001014 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001015 file_to_parse="${file_to_parse} ${package_dir}/swift"
1016 fi
1017 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001018 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001019 file_to_parse="${file_to_parse} ${package_dir}/nova"
1020 fi
1021 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001022 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001023 file_to_parse="${file_to_parse} ${package_dir}/glance"
1024 fi
1025 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001026 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001027 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1028 fi
1029 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001030 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001031 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1032 fi
1033 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001034 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001035 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1036 fi
1037 fi
1038 done
1039 echo "$(_parse_package_files $file_to_parse)"
1040 $xtrace
1041}
1042
1043# get_plugin_packages() collects a list of package names of any type from a
1044# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1045# list is intended to be passed to a package installer such as apt or yum.
1046#
1047# Only packages required for enabled and collected plugins will included.
1048#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001049# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001050# in these prerequisite files, see get_packages() for more info.
1051function get_plugin_packages {
1052 local xtrace=$(set +o | grep xtrace)
1053 set +o xtrace
1054 local files_to_parse=""
1055 local package_dir=""
1056 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1057 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1058 files_to_parse+="$package_dir/$plugin"
1059 done
1060 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001061 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001062}
1063
1064# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001065# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001066# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001067function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001068 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1069 REPOS_UPDATED=${REPOS_UPDATED:-False}
1070 RETRY_UPDATE=${RETRY_UPDATE:-False}
1071
Paul Linchpiner9e179742014-07-13 22:23:00 -07001072 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001073 return 0
1074 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001075
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001076 if is_ubuntu; then
1077 local xtrace=$(set +o | grep xtrace)
1078 set +o xtrace
1079 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1080 # if there are transient errors pulling the updates, that's fine.
1081 # It may be secondary repositories that we don't really care about.
1082 apt_get update || /bin/true
1083 REPOS_UPDATED=True
1084 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001085 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001086 fi
1087}
1088
1089function real_install_package {
1090 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001091 apt_get install "$@"
1092 elif is_fedora; then
1093 yum_install "$@"
1094 elif is_suse; then
1095 zypper_install "$@"
1096 else
1097 exit_distro_not_supported "installing packages"
1098 fi
1099}
1100
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001101# Distro-agnostic package installer
1102# install_package package [package ...]
1103function install_package {
1104 update_package_repo
1105 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1106}
1107
Dean Troyerdff49a22014-01-30 15:37:40 -06001108# Distro-agnostic function to tell if a package is installed
1109# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001110function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001111 if [[ -z "$@" ]]; then
1112 return 1
1113 fi
1114
1115 if [[ -z "$os_PACKAGE" ]]; then
1116 GetOSVersion
1117 fi
1118
1119 if [[ "$os_PACKAGE" = "deb" ]]; then
1120 dpkg -s "$@" > /dev/null 2> /dev/null
1121 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1122 rpm --quiet -q "$@"
1123 else
1124 exit_distro_not_supported "finding if a package is installed"
1125 fi
1126}
1127
1128# Distro-agnostic package uninstaller
1129# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001130function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001131 if is_ubuntu; then
1132 apt_get purge "$@"
1133 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001134 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001135 elif is_suse; then
1136 sudo zypper rm "$@"
1137 else
1138 exit_distro_not_supported "uninstalling packages"
1139 fi
1140}
1141
1142# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001143# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001144# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001145function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001146 [[ "$OFFLINE" = "True" ]] && return
1147 local sudo="sudo"
1148 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001149
1150 # The manual check for missing packages is because yum -y assumes
1151 # missing packages are OK. See
1152 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001153 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1154 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001155 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001156 awk '
1157 BEGIN { fail=0 }
1158 /No package/ { fail=1 }
1159 { print }
1160 END { exit fail }' || \
1161 die $LINENO "Missing packages detected"
1162
1163 # also ensure we catch a yum failure
1164 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001165 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001166 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001167}
1168
1169# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001170# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001171# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001172function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001173 [[ "$OFFLINE" = "True" ]] && return
1174 local sudo="sudo"
1175 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001176 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1177 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001178 zypper --non-interactive install --auto-agree-with-licenses "$@"
1179}
1180
1181
1182# Process Functions
1183# =================
1184
1185# _run_process() is designed to be backgrounded by run_process() to simulate a
1186# fork. It includes the dirty work of closing extra filehandles and preparing log
1187# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001188# from the service name.
1189# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001190# If an optional group is provided sg will be used to set the group of
1191# the command.
1192# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001193function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001194 # disable tracing through the exec redirects, it's just confusing in the logs.
1195 xtrace=$(set +o | grep xtrace)
1196 set +o xtrace
1197
Dean Troyerdff49a22014-01-30 15:37:40 -06001198 local service=$1
1199 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001200 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001201
1202 # Undo logging redirections and close the extra descriptors
1203 exec 1>&3
1204 exec 2>&3
1205 exec 3>&-
1206 exec 6>&-
1207
Dean Troyerdde41d02014-12-09 17:47:57 -06001208 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1209 if [[ -n ${LOGDIR} ]]; then
1210 exec 1>&"$real_logfile" 2>&1
1211 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1212 if [[ -n ${SCREEN_LOGDIR} ]]; then
1213 # Drop the backward-compat symlink
1214 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1215 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001216
1217 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1218 export PYTHONUNBUFFERED=1
1219 fi
1220
Sean Dague6e137ab2015-04-29 08:22:24 -04001221 # reenable xtrace before we do *real* work
1222 $xtrace
1223
Dean Troyer3159a822014-08-27 14:13:58 -05001224 # Run under ``setsid`` to force the process to become a session and group leader.
1225 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001226 if [[ -n "$group" ]]; then
1227 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1228 else
1229 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1230 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001231
1232 # Just silently exit this process
1233 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001234}
1235
1236# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1237# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001238# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001239# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001240function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001241 SCREEN_NAME=${SCREEN_NAME:-stack}
1242 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1243
1244 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1245 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1246 fi
1247
1248 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1249}
1250
1251# Find out if a process exists by partial name.
1252# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001253function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001254 local name=$1
1255 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001256 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001257 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001258 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001259}
1260
Dean Troyer3159a822014-08-27 14:13:58 -05001261# Run a single service under screen or directly
1262# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001263# If an optional group is provided sg will be used to run the
1264# command as that group.
1265# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001266function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001267 local service=$1
1268 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001269 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001270
Dean Troyer3159a822014-08-27 14:13:58 -05001271 if is_service_enabled $service; then
1272 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001273 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001274 else
1275 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001276 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001277 fi
1278 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001279}
1280
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001281# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001282# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001283# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001284# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001285# Run a command in a shell in a screen window, if an optional group
1286# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001287function screen_process {
1288 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001289 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001290 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001291
Sean Dagueea22a4f2014-06-27 15:21:41 -04001292 SCREEN_NAME=${SCREEN_NAME:-stack}
1293 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001294 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001295
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001296 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001297
Dean Troyerdde41d02014-12-09 17:47:57 -06001298 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1299 echo "LOGDIR: $LOGDIR"
1300 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1301 echo "log: $real_logfile"
1302 if [[ -n ${LOGDIR} ]]; then
1303 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001304 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001305 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1306 if [[ -n ${SCREEN_LOGDIR} ]]; then
1307 # Drop the backward-compat symlink
1308 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1309 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001310 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001311
1312 # sleep to allow bash to be ready to be send the command - we are
1313 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001314 # bash isn't running by the time we send the command, nothing
1315 # happens. This sleep was added originally to handle gate runs
1316 # where we needed this to be at least 3 seconds to pass
1317 # consistently on slow clouds. Now this is configurable so that we
1318 # can determine a reasonable value for the local case which should
1319 # be much smaller.
1320 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001321
1322 NL=`echo -ne '\015'`
1323 # This fun command does the following:
1324 # - the passed server command is backgrounded
1325 # - the pid of the background process is saved in the usual place
1326 # - the server process is brought back to the foreground
1327 # - if the server process exits prematurely the fg command errors
1328 # and a message is written to stdout and the process failure file
1329 #
1330 # The pid saved can be used in stop_process() as a process group
1331 # id to kill off all child processes
1332 if [[ -n "$group" ]]; then
1333 command="sg $group '$command'"
1334 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001335
1336 # Append the process to the screen rc file
1337 screen_rc "$name" "$command"
1338
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001339 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 -06001340}
1341
1342# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001343# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001344# screen_rc service "command-line"
1345function screen_rc {
1346 SCREEN_NAME=${SCREEN_NAME:-stack}
1347 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1348 if [[ ! -e $SCREENRC ]]; then
1349 # Name the screen session
1350 echo "sessionname $SCREEN_NAME" > $SCREENRC
1351 # Set a reasonable statusbar
1352 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1353 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1354 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1355 echo "screen -t shell bash" >> $SCREENRC
1356 fi
1357 # If this service doesn't already exist in the screenrc file
1358 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1359 NL=`echo -ne '\015'`
1360 echo "screen -t $1 bash" >> $SCREENRC
1361 echo "stuff \"$2$NL\"" >> $SCREENRC
1362
Dean Troyerdde41d02014-12-09 17:47:57 -06001363 if [[ -n ${LOGDIR} ]]; then
1364 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001365 echo "log on" >>$SCREENRC
1366 fi
1367 fi
1368}
1369
1370# Stop a service in screen
1371# If a PID is available use it, kill the whole process group via TERM
1372# If screen is being used kill the screen window; this will catch processes
1373# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001374# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001375# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001376function screen_stop_service {
1377 local service=$1
1378
Dean Troyerdff49a22014-01-30 15:37:40 -06001379 SCREEN_NAME=${SCREEN_NAME:-stack}
1380 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001381 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001382
Dean Troyer3159a822014-08-27 14:13:58 -05001383 if is_service_enabled $service; then
1384 # Clean up the screen window
Attila Fazekasf750a6f2015-07-01 12:17:35 +02001385 screen -S $SCREEN_NAME -p $service -X kill || true
Dean Troyer3159a822014-08-27 14:13:58 -05001386 fi
1387}
1388
1389# Stop a service process
1390# If a PID is available use it, kill the whole process group via TERM
1391# If screen is being used kill the screen window; this will catch processes
1392# that did not leave a PID behind
1393# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1394# stop_process service
1395function stop_process {
1396 local service=$1
1397
1398 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001399 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001400
1401 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001402 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001403 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1404 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1405 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001406 fi
1407 if [[ "$USE_SCREEN" = "True" ]]; then
1408 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001409 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001410 fi
1411 fi
1412}
1413
1414# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001415# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001416# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001417function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001418 local service
1419 local failures
1420 SCREEN_NAME=${SCREEN_NAME:-stack}
1421 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1422
1423
1424 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1425 echo "No service status directory found"
1426 return
1427 fi
1428
1429 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001430 # make this -o errexit safe
1431 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001432
1433 for service in $failures; do
1434 service=`basename $service`
1435 service=${service%.failure}
1436 echo "Error: Service $service is not running"
1437 done
1438
1439 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001440 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001441 fi
1442}
1443
Chris Dent2f27a0e2014-09-09 13:46:02 +01001444# Tail a log file in a screen if USE_SCREEN is true.
1445function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001446 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001447 local logfile=$2
1448
Sean Dague53753292014-12-04 19:38:15 -05001449 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001450 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001451 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001452 fi
1453}
1454
Dean Troyerdff49a22014-01-30 15:37:40 -06001455
Dean Troyer3159a822014-08-27 14:13:58 -05001456# Deprecated Functions
1457# --------------------
1458
1459# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1460# fork. It includes the dirty work of closing extra filehandles and preparing log
1461# files to produce the same logs as screen_it(). The log filename is derived
1462# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1463# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1464# _old_run_process service "command-line"
1465function _old_run_process {
1466 local service=$1
1467 local command="$2"
1468
1469 # Undo logging redirections and close the extra descriptors
1470 exec 1>&3
1471 exec 2>&3
1472 exec 3>&-
1473 exec 6>&-
1474
1475 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001476 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1477 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001478
1479 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1480 export PYTHONUNBUFFERED=1
1481 fi
1482
1483 exec /bin/bash -c "$command"
1484 die "$service exec failure: $command"
1485}
1486
1487# old_run_process() launches a child process that closes all file descriptors and
1488# then exec's the passed in command. This is meant to duplicate the semantics
1489# of screen_it() without screen. PIDs are written to
1490# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1491# old_run_process service "command-line"
1492function old_run_process {
1493 local service=$1
1494 local command="$2"
1495
1496 # Spawn the child process
1497 _old_run_process "$service" "$command" &
1498 echo $!
1499}
1500
1501# Compatibility for existing start_XXXX() functions
1502# Uses global ``USE_SCREEN``
1503# screen_it service "command-line"
1504function screen_it {
1505 if is_service_enabled $1; then
1506 # Append the service to the screen rc file
1507 screen_rc "$1" "$2"
1508
1509 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001510 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001511 else
1512 # Spawn directly without screen
1513 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1514 fi
1515 fi
1516}
1517
1518# Compatibility for existing stop_XXXX() functions
1519# Stop a service in screen
1520# If a PID is available use it, kill the whole process group via TERM
1521# If screen is being used kill the screen window; this will catch processes
1522# that did not leave a PID behind
1523# screen_stop service
1524function screen_stop {
1525 # Clean up the screen window
1526 stop_process $1
1527}
1528
1529
Sean Dague2c65e712014-12-18 09:44:56 -05001530# Plugin Functions
1531# =================
1532
1533DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1534
1535# enable_plugin <name> <url> [branch]
1536#
1537# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1538# ``url`` is a git url
1539# ``branch`` is a gitref. If it's not set, defaults to master
1540function enable_plugin {
1541 local name=$1
1542 local url=$2
1543 local branch=${3:-master}
1544 DEVSTACK_PLUGINS+=",$name"
1545 GITREPO[$name]=$url
1546 GITDIR[$name]=$DEST/$name
1547 GITBRANCH[$name]=$branch
1548}
1549
1550# fetch_plugins
1551#
1552# clones all plugins
1553function fetch_plugins {
1554 local plugins="${DEVSTACK_PLUGINS}"
1555 local plugin
1556
1557 # short circuit if nothing to do
1558 if [[ -z $plugins ]]; then
1559 return
1560 fi
1561
Dean Troyerdc97cb72015-03-28 08:20:50 -05001562 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001563 for plugin in ${plugins//,/ }; do
1564 git_clone_by_name $plugin
1565 done
1566}
1567
1568# load_plugin_settings
1569#
1570# Load settings from plugins in the order that they were registered
1571function load_plugin_settings {
1572 local plugins="${DEVSTACK_PLUGINS}"
1573 local plugin
1574
1575 # short circuit if nothing to do
1576 if [[ -z $plugins ]]; then
1577 return
1578 fi
1579
1580 echo "Loading plugin settings"
1581 for plugin in ${plugins//,/ }; do
1582 local dir=${GITDIR[$plugin]}
1583 # source any known settings
1584 if [[ -f $dir/devstack/settings ]]; then
1585 source $dir/devstack/settings
1586 fi
1587 done
1588}
1589
Sean Dague6e275e12015-03-26 05:54:28 -04001590# plugin_override_defaults
1591#
1592# Run an extremely early setting phase for plugins that allows default
1593# overriding of services.
1594function plugin_override_defaults {
1595 local plugins="${DEVSTACK_PLUGINS}"
1596 local plugin
1597
1598 # short circuit if nothing to do
1599 if [[ -z $plugins ]]; then
1600 return
1601 fi
1602
1603 echo "Overriding Configuration Defaults"
1604 for plugin in ${plugins//,/ }; do
1605 local dir=${GITDIR[$plugin]}
1606 # source any overrides
1607 if [[ -f $dir/devstack/override-defaults ]]; then
1608 # be really verbose that an override is happening, as it
1609 # may not be obvious if things fail later.
1610 echo "$plugin has overriden the following defaults"
1611 cat $dir/devstack/override-defaults
1612 source $dir/devstack/override-defaults
1613 fi
1614 done
1615}
1616
Sean Dague2c65e712014-12-18 09:44:56 -05001617# run_plugins
1618#
1619# Run the devstack/plugin.sh in all the plugin directories. These are
1620# run in registration order.
1621function run_plugins {
1622 local mode=$1
1623 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301624
1625 local plugins="${DEVSTACK_PLUGINS}"
1626 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001627 for plugin in ${plugins//,/ }; do
1628 local dir=${GITDIR[$plugin]}
1629 if [[ -f $dir/devstack/plugin.sh ]]; then
1630 source $dir/devstack/plugin.sh $mode $phase
1631 fi
1632 done
1633}
1634
1635function run_phase {
1636 local mode=$1
1637 local phase=$2
1638 if [[ -d $TOP_DIR/extras.d ]]; then
1639 for i in $TOP_DIR/extras.d/*.sh; do
1640 [[ -r $i ]] && source $i $mode $phase
1641 done
1642 fi
1643 # the source phase corresponds to settings loading in plugins
1644 if [[ "$mode" == "source" ]]; then
1645 load_plugin_settings
Sean Dague6e275e12015-03-26 05:54:28 -04001646 elif [[ "$mode" == "override_defaults" ]]; then
1647 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001648 else
1649 run_plugins $mode $phase
1650 fi
1651}
1652
Dean Troyerdff49a22014-01-30 15:37:40 -06001653
1654# Service Functions
1655# =================
1656
1657# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1658# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001659function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001660 echo "$1" | sed -e '
1661 s/,,/,/g;
1662 s/^,//;
1663 s/,$//
1664 '
1665}
1666
1667# disable_all_services() removes all current services
1668# from ``ENABLED_SERVICES`` to reset the configuration
1669# before a minimal installation
1670# Uses global ``ENABLED_SERVICES``
1671# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001672function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001673 ENABLED_SERVICES=""
1674}
1675
1676# Remove all services starting with '-'. For example, to install all default
1677# services except rabbit (rabbit) set in ``localrc``:
1678# ENABLED_SERVICES+=",-rabbit"
1679# Uses global ``ENABLED_SERVICES``
1680# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001681function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001682 local to_remove=""
1683 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001684 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001685
1686 # build up list of services that should be removed; i.e. they
1687 # begin with "-"
1688 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001689 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001690 to_remove+=",${service#-}"
1691 else
1692 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001693 fi
1694 done
Ian Wienand2796a822015-04-15 08:59:04 +10001695
1696 # go through the service list. if this service appears in the "to
1697 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001698 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Dean Troyerdff49a22014-01-30 15:37:40 -06001699}
1700
1701# disable_service() removes the services passed as argument to the
1702# ``ENABLED_SERVICES`` list, if they are present.
1703#
1704# For example:
1705# disable_service rabbit
1706#
1707# This function does not know about the special cases
1708# for nova, glance, and neutron built into is_service_enabled().
1709# Uses global ``ENABLED_SERVICES``
1710# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001711function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001712 local tmpsvcs=",${ENABLED_SERVICES},"
1713 local service
1714 for service in $@; do
1715 if is_service_enabled $service; then
1716 tmpsvcs=${tmpsvcs//,$service,/,}
1717 fi
1718 done
1719 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1720}
1721
1722# enable_service() adds the services passed as argument to the
1723# ``ENABLED_SERVICES`` list, if they are not already present.
1724#
1725# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001726# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001727#
1728# This function does not know about the special cases
1729# for nova, glance, and neutron built into is_service_enabled().
1730# Uses global ``ENABLED_SERVICES``
1731# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001732function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001733 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001734 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001735 for service in $@; do
1736 if ! is_service_enabled $service; then
1737 tmpsvcs+=",$service"
1738 fi
1739 done
1740 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1741 disable_negated_services
1742}
1743
1744# is_service_enabled() checks if the service(s) specified as arguments are
1745# enabled by the user in ``ENABLED_SERVICES``.
1746#
1747# Multiple services specified as arguments are ``OR``'ed together; the test
1748# is a short-circuit boolean, i.e it returns on the first match.
1749#
1750# There are special cases for some 'catch-all' services::
1751# **nova** returns true if any service enabled start with **n-**
1752# **cinder** returns true if any service enabled start with **c-**
1753# **ceilometer** returns true if any service enabled start with **ceilometer**
1754# **glance** returns true if any service enabled start with **g-**
1755# **neutron** returns true if any service enabled start with **q-**
1756# **swift** returns true if any service enabled start with **s-**
1757# **trove** returns true if any service enabled start with **tr-**
1758# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1759# **s-** services will be enabled. This will be deprecated in the future.
1760#
1761# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1762# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1763# as enabled in this case.
1764#
1765# Uses global ``ENABLED_SERVICES``
1766# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001767function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001768 local xtrace=$(set +o | grep xtrace)
1769 set +o xtrace
1770 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001771 local services=$@
1772 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001773 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001774 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001775
1776 # Look for top-level 'enabled' function for this service
1777 if type is_${service}_enabled >/dev/null 2>&1; then
1778 # A function exists for this service, use it
1779 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001780 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001781 fi
1782
1783 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1784 # are implemented
1785
Sean Dague45917cc2014-02-24 16:09:14 -05001786 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001787 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001788 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001789 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1790 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1791 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1792 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1793 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1794 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1795 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001796 done
Sean Dague45917cc2014-02-24 16:09:14 -05001797 $xtrace
1798 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001799}
1800
fumihiko kakuma8606c982015-04-13 09:55:06 +09001801# remove specified list from the input string
1802# remove_disabled_services service-list remove-list
1803function remove_disabled_services {
1804 local service_list=$1
1805 local remove_list=$2
1806 local service
1807 local enabled=""
1808
1809 for service in ${service_list//,/ }; do
1810 local remove
1811 local add=1
1812 for remove in ${remove_list//,/ }; do
1813 if [[ ${remove} == ${service} ]]; then
1814 add=0
1815 break
1816 fi
1817 done
1818 if [[ $add == 1 ]]; then
1819 enabled="${enabled},$service"
1820 fi
1821 done
1822 _cleanup_service_list "$enabled"
1823}
1824
Dean Troyerdff49a22014-01-30 15:37:40 -06001825# Toggle enable/disable_service for services that must run exclusive of each other
1826# $1 The name of a variable containing a space-separated list of services
1827# $2 The name of a variable in which to store the enabled service's name
1828# $3 The name of the service to enable
1829function use_exclusive_service {
1830 local options=${!1}
1831 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001832 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001833 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001834 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001835 for opt in $options;do
1836 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1837 done
1838 eval "$out=$selection"
1839 return 0
1840}
1841
1842
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001843# System Functions
1844# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001845
1846# Only run the command if the target file (the last arg) is not on an
1847# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001848function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001849 local xtrace=$(set +o | grep xtrace)
1850 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001851 local args=( $@ )
1852 local last
1853 local sudo_cmd
1854 local dir_to_check
1855
1856 let last="${#args[*]} - 1"
1857
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001858 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001859 if [ ! -d "$dir_to_check" ]; then
1860 dir_to_check=`dirname "$dir_to_check"`
1861 fi
1862
1863 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001864 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001865 return 0
1866 fi
1867
1868 if [[ $TRACK_DEPENDS = True ]]; then
1869 sudo_cmd="env"
1870 else
1871 sudo_cmd="sudo"
1872 fi
1873
Sean Dague45917cc2014-02-24 16:09:14 -05001874 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001875 $sudo_cmd $@
1876}
1877
1878# Exit 0 if address is in network or 1 if address is not in network
1879# ip-range is in CIDR notation: 1.2.3.4/20
1880# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001881function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001882 local ip=$1
1883 local range=$2
1884 local masklen=${range#*/}
1885 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1886 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1887 [[ $network == $subnet ]]
1888}
1889
1890# Add a user to a group.
1891# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001892function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001893 local user=$1
1894 local group=$2
1895
Thomas Bechtolda8580852015-05-31 00:04:33 +02001896 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06001897}
1898
1899# Convert CIDR notation to a IPv4 netmask
1900# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001901function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001902 local maskpat="255 255 255 255"
1903 local maskdgt="254 252 248 240 224 192 128"
1904 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1905 echo ${1-0}.${2-0}.${3-0}.${4-0}
1906}
1907
1908# Gracefully cp only if source file/dir exists
1909# cp_it source destination
1910function cp_it {
1911 if [ -e $1 ] || [ -d $1 ]; then
1912 cp -pRL $1 $2
1913 fi
1914}
1915
1916# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1917# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1918# ``localrc`` or on the command line if necessary::
1919#
1920# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1921#
1922# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1923
Ian Wienandaee18c72014-02-21 15:35:08 +11001924function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001925 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001926 export http_proxy=$http_proxy
1927 fi
Sean Dague53753292014-12-04 19:38:15 -05001928 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001929 export https_proxy=$https_proxy
1930 fi
Sean Dague53753292014-12-04 19:38:15 -05001931 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001932 export no_proxy=$no_proxy
1933 fi
1934}
1935
1936# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001937function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001938 local mount_type=`stat -f -L -c %T $1`
1939 test "$mount_type" == "nfs"
1940}
1941
1942# Return the network portion of the given IP address using netmask
1943# netmask is in the traditional dotted-quad format
1944# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001945function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001946 local ip=$1
1947 local mask=$2
1948 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1949 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1950 echo $subnet
1951}
1952
Chris Dent3a2c86a2015-05-12 13:41:25 +00001953# Return the current python as "python<major>.<minor>"
1954function python_version {
1955 local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
1956 echo "python${python_version}"
1957}
1958
Dean Troyerdff49a22014-01-30 15:37:40 -06001959# Service wrapper to restart services
1960# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001961function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001962 if is_ubuntu; then
1963 sudo /usr/sbin/service $1 restart
1964 else
1965 sudo /sbin/service $1 restart
1966 fi
1967}
1968
1969# Only change permissions of a file or directory if it is not on an
1970# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001971function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001972 _safe_permission_operation chmod $@
1973}
1974
1975# Only change ownership of a file or directory if it is not on an NFS
1976# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001977function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001978 _safe_permission_operation chown $@
1979}
1980
1981# Service wrapper to start services
1982# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001983function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001984 if is_ubuntu; then
1985 sudo /usr/sbin/service $1 start
1986 else
1987 sudo /sbin/service $1 start
1988 fi
1989}
1990
1991# Service wrapper to stop services
1992# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001993function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001994 if is_ubuntu; then
1995 sudo /usr/sbin/service $1 stop
1996 else
1997 sudo /sbin/service $1 stop
1998 fi
1999}
2000
Sean Dague442e4e92015-06-24 13:24:02 -04002001# Test with a finite retry loop.
2002#
2003function test_with_retry {
2004 local testcmd=$1
2005 local failmsg=$2
2006 local until=${3:-10}
2007 local sleep=${4:-0.5}
2008
2009 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2010 die $LINENO "$failmsg"
2011 fi
2012}
2013
Dean Troyerdff49a22014-01-30 15:37:40 -06002014
2015# Restore xtrace
2016$XTRACE
2017
2018# Local variables:
2019# mode: shell-script
2020# End: