blob: 867bd5227db95fdd0158b719e8a698ca081650df [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
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100693 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100694 # Gets domain id
Ian Wienand11d276c2015-07-02 09:34:34 +1000695 domain_id=$(
Alistair Coles24779f62014-10-15 18:57:59 +0100696 # Gets domain id
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100697 openstack --os-token=$OS_TOKEN --os-url=$os_url \
698 --os-identity-api-version=3 domain show $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100699 -f value -c id 2>/dev/null ||
700 # Creates new domain
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100701 openstack --os-token=$OS_TOKEN --os-url=$os_url \
702 --os-identity-api-version=3 domain create $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100703 --description "$2" \
704 -f value -c id
705 )
706 echo $domain_id
707}
708
Steve Martinellib74e01c2014-12-18 01:35:35 -0500709# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000710# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500711function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500712 local desc="${3:-}"
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100713 local os_url="$KEYSTONE_SERVICE_URI_V3"
Ian Wienand11d276c2015-07-02 09:34:34 +1000714 local group_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500715 # Gets group id
Ian Wienand11d276c2015-07-02 09:34:34 +1000716 group_id=$(
Steve Martinellib74e01c2014-12-18 01:35:35 -0500717 # Creates new group with --or-show
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100718 openstack --os-token=$OS_TOKEN --os-url=$os_url \
719 --os-identity-api-version=3 group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000720 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500721 -f value -c id
722 )
723 echo $group_id
724}
725
Bartosz Górski0abde392014-02-28 14:15:19 +0100726# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000727# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100728function get_or_create_user {
Ian Wienand11d276c2015-07-02 09:34:34 +1000729 local user_id
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000730 if [[ ! -z "$4" ]]; then
731 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200732 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500733 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200734 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100735 # Gets user id
Ian Wienand11d276c2015-07-02 09:34:34 +1000736 user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500737 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000738 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100739 $1 \
740 --password "$2" \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100741 --os-url=$KEYSTONE_SERVICE_URI_V3 \
742 --os-identity-api-version=3 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000743 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500744 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500745 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100746 -f value -c id
747 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500748 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100749}
750
751# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000752# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100753function get_or_create_project {
Ian Wienand11d276c2015-07-02 09:34:34 +1000754 local project_id
755 project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500756 # Creates new project with --or-show
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100757 openstack --os-url=$KEYSTONE_SERVICE_URI_V3 \
758 --os-identity-api-version=3 \
759 project create $1 \
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000760 --domain=$2 \
761 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100762 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500763 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100764}
765
766# Gets or creates role
767# Usage: get_or_create_role <name>
768function get_or_create_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000769 local role_id
770 role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500771 # Creates role with --or-show
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000772 openstack role create $1 \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100773 --os-url=$KEYSTONE_SERVICE_URI_V3 \
774 --os-identity-api-version=3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000775 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100776 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500777 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100778}
779
Jamie Lennox9b215db2015-02-10 18:19:57 +1100780# Gets or adds user role to project
781# Usage: get_or_add_user_project_role <role> <user> <project>
782function get_or_add_user_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000783 local user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100784 # Gets user role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000785 user_role_id=$(openstack role list \
Steve Martinelli5541a612015-01-19 15:58:49 -0500786 --user $2 \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100787 --os-url=$KEYSTONE_SERVICE_URI_V3 \
788 --os-identity-api-version=3 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100789 --column "ID" \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000790 --project $3 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100791 --column "Name" \
792 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500793 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100794 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500795 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100796 $1 \
797 --user $2 \
798 --project $3 \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100799 --os-url=$KEYSTONE_SERVICE_URI_V3 \
800 --os-identity-api-version=3 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100801 | grep " id " | get_field 2)
802 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500803 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100804}
805
Steve Martinelli4599fd12015-03-12 21:30:58 -0400806# Gets or adds group role to project
807# Usage: get_or_add_group_project_role <role> <group> <project>
808function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000809 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -0400810 # Gets group role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000811 group_role_id=$(openstack role list \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100812 --os-url=$KEYSTONE_SERVICE_URI_V3 \
813 --os-identity-api-version=3 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400814 --group $2 \
815 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000816 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400817 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000818 # Adds role to group and get it
819 openstack role add $1 \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100820 --os-url=$KEYSTONE_SERVICE_URI_V3 \
821 --os-identity-api-version=3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000822 --group $2 \
823 --project $3
824 group_role_id=$(openstack role list \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100825 --os-url=$KEYSTONE_SERVICE_URI_V3 \
826 --os-identity-api-version=3 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400827 --group $2 \
828 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000829 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400830 fi
831 echo $group_role_id
832}
833
Bartosz Górski0abde392014-02-28 14:15:19 +0100834# Gets or creates service
835# Usage: get_or_create_service <name> <type> <description>
836function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +1000837 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100838 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +1000839 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100840 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +1000841 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100842 # Creates new service if not exists
843 openstack service create \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100844 --os-url $KEYSTONE_SERVICE_URI_V3 \
845 --os-identity-api-version=3 \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500846 $2 \
847 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100848 --description="$3" \
849 -f value -c id
850 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500851 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100852}
853
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000854# Create an endpoint with a specific interface
855# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
856function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +1000857 local endpoint_id
Daniel Gonzalez1991e752015-08-11 19:34:22 +0200858 # TODO(dgonzalez): The check of the region name, as done in the grep
859 # statement below, exists only because keystone does currently
860 # not allow filtering the region name when listing endpoints. If keystone
861 # gets support for this, the check for the region name can be removed.
862 # Related bug in keystone: https://bugs.launchpad.net/keystone/+bug/1482772
Ian Wienand11d276c2015-07-02 09:34:34 +1000863 endpoint_id=$(openstack endpoint list \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100864 --os-url $KEYSTONE_SERVICE_URI_V3 \
865 --os-identity-api-version=3 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000866 --service $1 \
867 --interface $2 \
868 --region $4 \
Daniel Gonzalez1991e752015-08-11 19:34:22 +0200869 -c ID -c Region -f value | grep $4 | cut -f 1 -d " ")
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500870 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100871 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500872 endpoint_id=$(openstack endpoint create \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100873 --os-url $KEYSTONE_SERVICE_URI_V3 \
874 --os-identity-api-version=3 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000875 $1 $2 $3 --region $4 -f value -c id)
Bartosz Górski0abde392014-02-28 14:15:19 +0100876 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000877
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500878 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100879}
Dean Troyerdff49a22014-01-30 15:37:40 -0600880
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000881# Gets or creates endpoint
882# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
883function get_or_create_endpoint {
884 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
885 # creating one endpoint with multiple urls to multiple endpoints each with
886 # a different interface. To maintain the existing function interface we
887 # create 3 endpoints and return the id of the public one. In reality
888 # returning the public id will not make a lot of difference as there are no
889 # scenarios currently that use the returned id. Ideally this behaviour
890 # should be pushed out to the service setups and let them create the
891 # endpoints they need.
892 local public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
893 _get_or_create_endpoint_with_interface $1 admin $4 $2
894 _get_or_create_endpoint_with_interface $1 internal $5 $2
895
896 # return the public id to indicate success, and this is the endpoint most likely wanted
897 echo $public_id
898}
899
900# Get a URL from the identity service
901# Usage: get_endpoint_url <service> <interface>
902function get_endpoint_url {
903 echo $(openstack endpoint list \
904 --service $1 --interface $2 \
905 --os-url $KEYSTONE_SERVICE_URI_V3 \
906 --os-identity-api-version=3 \
907 -c URL -f value)
908}
909
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500910
Dean Troyerdff49a22014-01-30 15:37:40 -0600911# Package Functions
912# =================
913
914# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100915function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800916 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600917 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800918
919 if [[ -z "$base_dir" ]]; then
920 base_dir=$FILES
921 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600922 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800923 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600924 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800925 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600926 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800927 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600928 else
929 exit_distro_not_supported "list of packages"
930 fi
931 echo "$pkg_dir"
932}
933
934# Wrapper for ``apt-get`` to set cache and proxy environment variables
935# Uses globals ``OFFLINE``, ``*_proxy``
936# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100937function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500938 local xtrace=$(set +o | grep xtrace)
939 set +o xtrace
940
Dean Troyerdff49a22014-01-30 15:37:40 -0600941 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
942 local sudo="sudo"
943 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500944
945 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500946
Dean Troyerdff49a22014-01-30 15:37:40 -0600947 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500948 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
949 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600950 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
951}
952
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800953function _parse_package_files {
954 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600955
Dean Troyerdff49a22014-01-30 15:37:40 -0600956 if [[ -z "$DISTRO" ]]; then
957 GetDistro
958 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600959
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800960 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600961 local OIFS line package distros distro
962 [[ -e $fname ]] || continue
963
964 OIFS=$IFS
965 IFS=$'\n'
966 for line in $(<${fname}); do
967 if [[ $line =~ "NOPRIME" ]]; then
968 continue
969 fi
970
971 # Assume we want this package
972 package=${line%#*}
973 inst_pkg=1
974
975 # Look for # dist:xxx in comment
976 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
977 # We are using BASH regexp matching feature.
978 package=${BASH_REMATCH[1]}
979 distros=${BASH_REMATCH[2]}
980 # In bash ${VAR,,} will lowecase VAR
981 # Look for a match in the distro list
982 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
983 # If no match then skip this package
984 inst_pkg=0
985 fi
986 fi
987
Dean Troyerdff49a22014-01-30 15:37:40 -0600988 if [[ $inst_pkg = 1 ]]; then
989 echo $package
990 fi
991 done
992 IFS=$OIFS
993 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800994}
995
996# get_packages() collects a list of package names of any type from the
997# prerequisite files in ``files/{debs|rpms}``. The list is intended
998# to be passed to a package installer such as apt or yum.
999#
1000# Only packages required for the services in 1st argument will be
1001# included. Two bits of metadata are recognized in the prerequisite files:
1002#
1003# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1004# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1005# of the package to the distros listed. The distro names are case insensitive.
1006function get_packages {
1007 local xtrace=$(set +o | grep xtrace)
1008 set +o xtrace
1009 local services=$@
1010 local package_dir=$(_get_package_dir)
1011 local file_to_parse=""
1012 local service=""
1013
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001014 if [[ -z "$package_dir" ]]; then
1015 echo "No package directory supplied"
1016 return 1
1017 fi
1018 for service in ${services//,/ }; do
1019 # Allow individual services to specify dependencies
1020 if [[ -e ${package_dir}/${service} ]]; then
1021 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1022 fi
1023 # NOTE(sdague) n-api needs glance for now because that's where
1024 # glance client is
1025 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001026 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001027 file_to_parse="${file_to_parse} ${package_dir}/nova"
1028 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001029 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001030 file_to_parse="${file_to_parse} ${package_dir}/glance"
1031 fi
1032 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001033 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001034 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1035 fi
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001036 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001037 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001038 file_to_parse="${file_to_parse} ${package_dir}/swift"
1039 fi
1040 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001041 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001042 file_to_parse="${file_to_parse} ${package_dir}/nova"
1043 fi
1044 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001045 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001046 file_to_parse="${file_to_parse} ${package_dir}/glance"
1047 fi
1048 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001049 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001050 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1051 fi
1052 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001053 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001054 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1055 fi
1056 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001057 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001058 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1059 fi
1060 fi
1061 done
1062 echo "$(_parse_package_files $file_to_parse)"
1063 $xtrace
1064}
1065
1066# get_plugin_packages() collects a list of package names of any type from a
1067# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1068# list is intended to be passed to a package installer such as apt or yum.
1069#
1070# Only packages required for enabled and collected plugins will included.
1071#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001072# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001073# in these prerequisite files, see get_packages() for more info.
1074function get_plugin_packages {
1075 local xtrace=$(set +o | grep xtrace)
1076 set +o xtrace
1077 local files_to_parse=""
1078 local package_dir=""
1079 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1080 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1081 files_to_parse+="$package_dir/$plugin"
1082 done
1083 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001084 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001085}
1086
1087# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001088# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001089# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001090function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001091 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1092 REPOS_UPDATED=${REPOS_UPDATED:-False}
1093 RETRY_UPDATE=${RETRY_UPDATE:-False}
1094
Paul Linchpiner9e179742014-07-13 22:23:00 -07001095 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001096 return 0
1097 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001098
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001099 if is_ubuntu; then
1100 local xtrace=$(set +o | grep xtrace)
1101 set +o xtrace
1102 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1103 # if there are transient errors pulling the updates, that's fine.
1104 # It may be secondary repositories that we don't really care about.
1105 apt_get update || /bin/true
1106 REPOS_UPDATED=True
1107 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001108 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001109 fi
1110}
1111
1112function real_install_package {
1113 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001114 apt_get install "$@"
1115 elif is_fedora; then
1116 yum_install "$@"
1117 elif is_suse; then
1118 zypper_install "$@"
1119 else
1120 exit_distro_not_supported "installing packages"
1121 fi
1122}
1123
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001124# Distro-agnostic package installer
1125# install_package package [package ...]
1126function install_package {
1127 update_package_repo
1128 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1129}
1130
Dean Troyerdff49a22014-01-30 15:37:40 -06001131# Distro-agnostic function to tell if a package is installed
1132# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001133function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001134 if [[ -z "$@" ]]; then
1135 return 1
1136 fi
1137
1138 if [[ -z "$os_PACKAGE" ]]; then
1139 GetOSVersion
1140 fi
1141
1142 if [[ "$os_PACKAGE" = "deb" ]]; then
1143 dpkg -s "$@" > /dev/null 2> /dev/null
1144 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1145 rpm --quiet -q "$@"
1146 else
1147 exit_distro_not_supported "finding if a package is installed"
1148 fi
1149}
1150
1151# Distro-agnostic package uninstaller
1152# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001153function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001154 if is_ubuntu; then
1155 apt_get purge "$@"
1156 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001157 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001158 elif is_suse; then
1159 sudo zypper rm "$@"
1160 else
1161 exit_distro_not_supported "uninstalling packages"
1162 fi
1163}
1164
1165# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001166# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001167# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001168function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001169 [[ "$OFFLINE" = "True" ]] && return
1170 local sudo="sudo"
1171 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001172
1173 # The manual check for missing packages is because yum -y assumes
1174 # missing packages are OK. See
1175 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001176 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1177 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001178 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001179 awk '
1180 BEGIN { fail=0 }
1181 /No package/ { fail=1 }
1182 { print }
1183 END { exit fail }' || \
1184 die $LINENO "Missing packages detected"
1185
1186 # also ensure we catch a yum failure
1187 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001188 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001189 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001190}
1191
1192# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001193# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001194# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001195function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001196 [[ "$OFFLINE" = "True" ]] && return
1197 local sudo="sudo"
1198 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001199 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1200 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001201 zypper --non-interactive install --auto-agree-with-licenses "$@"
1202}
1203
1204
1205# Process Functions
1206# =================
1207
1208# _run_process() is designed to be backgrounded by run_process() to simulate a
1209# fork. It includes the dirty work of closing extra filehandles and preparing log
1210# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001211# from the service name.
1212# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001213# If an optional group is provided sg will be used to set the group of
1214# the command.
1215# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001216function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001217 # disable tracing through the exec redirects, it's just confusing in the logs.
1218 xtrace=$(set +o | grep xtrace)
1219 set +o xtrace
1220
Dean Troyerdff49a22014-01-30 15:37:40 -06001221 local service=$1
1222 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001223 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001224
1225 # Undo logging redirections and close the extra descriptors
1226 exec 1>&3
1227 exec 2>&3
1228 exec 3>&-
1229 exec 6>&-
1230
Dean Troyerdde41d02014-12-09 17:47:57 -06001231 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1232 if [[ -n ${LOGDIR} ]]; then
1233 exec 1>&"$real_logfile" 2>&1
1234 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1235 if [[ -n ${SCREEN_LOGDIR} ]]; then
1236 # Drop the backward-compat symlink
1237 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1238 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001239
1240 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1241 export PYTHONUNBUFFERED=1
1242 fi
1243
Sean Dague6e137ab2015-04-29 08:22:24 -04001244 # reenable xtrace before we do *real* work
1245 $xtrace
1246
Dean Troyer3159a822014-08-27 14:13:58 -05001247 # Run under ``setsid`` to force the process to become a session and group leader.
1248 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001249 if [[ -n "$group" ]]; then
1250 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1251 else
1252 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1253 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001254
1255 # Just silently exit this process
1256 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001257}
1258
1259# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1260# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001261# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001262# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001263function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001264 SCREEN_NAME=${SCREEN_NAME:-stack}
1265 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1266
1267 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1268 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1269 fi
1270
1271 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1272}
1273
1274# Find out if a process exists by partial name.
1275# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001276function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001277 local name=$1
1278 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001279 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001280 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001281 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001282}
1283
Dean Troyer3159a822014-08-27 14:13:58 -05001284# Run a single service under screen or directly
1285# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001286# If an optional group is provided sg will be used to run the
1287# command as that group.
1288# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001289function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001290 local service=$1
1291 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001292 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001293
Dean Troyer3159a822014-08-27 14:13:58 -05001294 if is_service_enabled $service; then
1295 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001296 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001297 else
1298 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001299 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001300 fi
1301 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001302}
1303
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001304# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001305# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001306# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001307# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001308# Run a command in a shell in a screen window, if an optional group
1309# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001310function screen_process {
1311 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001312 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001313 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001314
Sean Dagueea22a4f2014-06-27 15:21:41 -04001315 SCREEN_NAME=${SCREEN_NAME:-stack}
1316 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001317 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001318
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001319 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001320
Dean Troyerdde41d02014-12-09 17:47:57 -06001321 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1322 echo "LOGDIR: $LOGDIR"
1323 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1324 echo "log: $real_logfile"
1325 if [[ -n ${LOGDIR} ]]; then
1326 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001327 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001328 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1329 if [[ -n ${SCREEN_LOGDIR} ]]; then
1330 # Drop the backward-compat symlink
1331 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1332 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001333 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001334
1335 # sleep to allow bash to be ready to be send the command - we are
1336 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001337 # bash isn't running by the time we send the command, nothing
1338 # happens. This sleep was added originally to handle gate runs
1339 # where we needed this to be at least 3 seconds to pass
1340 # consistently on slow clouds. Now this is configurable so that we
1341 # can determine a reasonable value for the local case which should
1342 # be much smaller.
1343 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001344
1345 NL=`echo -ne '\015'`
1346 # This fun command does the following:
1347 # - the passed server command is backgrounded
1348 # - the pid of the background process is saved in the usual place
1349 # - the server process is brought back to the foreground
1350 # - if the server process exits prematurely the fg command errors
1351 # and a message is written to stdout and the process failure file
1352 #
1353 # The pid saved can be used in stop_process() as a process group
1354 # id to kill off all child processes
1355 if [[ -n "$group" ]]; then
1356 command="sg $group '$command'"
1357 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001358
1359 # Append the process to the screen rc file
1360 screen_rc "$name" "$command"
1361
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001362 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 -06001363}
1364
1365# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001366# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001367# screen_rc service "command-line"
1368function screen_rc {
1369 SCREEN_NAME=${SCREEN_NAME:-stack}
1370 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1371 if [[ ! -e $SCREENRC ]]; then
1372 # Name the screen session
1373 echo "sessionname $SCREEN_NAME" > $SCREENRC
1374 # Set a reasonable statusbar
1375 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1376 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1377 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1378 echo "screen -t shell bash" >> $SCREENRC
1379 fi
1380 # If this service doesn't already exist in the screenrc file
1381 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1382 NL=`echo -ne '\015'`
1383 echo "screen -t $1 bash" >> $SCREENRC
1384 echo "stuff \"$2$NL\"" >> $SCREENRC
1385
Dean Troyerdde41d02014-12-09 17:47:57 -06001386 if [[ -n ${LOGDIR} ]]; then
1387 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001388 echo "log on" >>$SCREENRC
1389 fi
1390 fi
1391}
1392
1393# Stop a service in screen
1394# If a PID is available use it, kill the whole process group via TERM
1395# If screen is being used kill the screen window; this will catch processes
1396# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001397# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001398# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001399function screen_stop_service {
1400 local service=$1
1401
Dean Troyerdff49a22014-01-30 15:37:40 -06001402 SCREEN_NAME=${SCREEN_NAME:-stack}
1403 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001404 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001405
Dean Troyer3159a822014-08-27 14:13:58 -05001406 if is_service_enabled $service; then
1407 # Clean up the screen window
Attila Fazekasf750a6f2015-07-01 12:17:35 +02001408 screen -S $SCREEN_NAME -p $service -X kill || true
Dean Troyer3159a822014-08-27 14:13:58 -05001409 fi
1410}
1411
1412# Stop a service process
1413# If a PID is available use it, kill the whole process group via TERM
1414# If screen is being used kill the screen window; this will catch processes
1415# that did not leave a PID behind
1416# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1417# stop_process service
1418function stop_process {
1419 local service=$1
1420
1421 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001422 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001423
1424 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001425 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001426 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1427 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1428 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001429 fi
1430 if [[ "$USE_SCREEN" = "True" ]]; then
1431 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001432 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001433 fi
1434 fi
1435}
1436
1437# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001438# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001439# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001440function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001441 local service
1442 local failures
1443 SCREEN_NAME=${SCREEN_NAME:-stack}
1444 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1445
1446
1447 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1448 echo "No service status directory found"
1449 return
1450 fi
1451
1452 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001453 # make this -o errexit safe
1454 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001455
1456 for service in $failures; do
1457 service=`basename $service`
1458 service=${service%.failure}
1459 echo "Error: Service $service is not running"
1460 done
1461
1462 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001463 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001464 fi
1465}
1466
Chris Dent2f27a0e2014-09-09 13:46:02 +01001467# Tail a log file in a screen if USE_SCREEN is true.
1468function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001469 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001470 local logfile=$2
1471
Sean Dague53753292014-12-04 19:38:15 -05001472 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001473 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001474 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001475 fi
1476}
1477
Dean Troyerdff49a22014-01-30 15:37:40 -06001478
Dean Troyer3159a822014-08-27 14:13:58 -05001479# Deprecated Functions
1480# --------------------
1481
1482# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1483# fork. It includes the dirty work of closing extra filehandles and preparing log
1484# files to produce the same logs as screen_it(). The log filename is derived
1485# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1486# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1487# _old_run_process service "command-line"
1488function _old_run_process {
1489 local service=$1
1490 local command="$2"
1491
1492 # Undo logging redirections and close the extra descriptors
1493 exec 1>&3
1494 exec 2>&3
1495 exec 3>&-
1496 exec 6>&-
1497
1498 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001499 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1500 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001501
1502 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1503 export PYTHONUNBUFFERED=1
1504 fi
1505
1506 exec /bin/bash -c "$command"
1507 die "$service exec failure: $command"
1508}
1509
1510# old_run_process() launches a child process that closes all file descriptors and
1511# then exec's the passed in command. This is meant to duplicate the semantics
1512# of screen_it() without screen. PIDs are written to
1513# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1514# old_run_process service "command-line"
1515function old_run_process {
1516 local service=$1
1517 local command="$2"
1518
1519 # Spawn the child process
1520 _old_run_process "$service" "$command" &
1521 echo $!
1522}
1523
1524# Compatibility for existing start_XXXX() functions
1525# Uses global ``USE_SCREEN``
1526# screen_it service "command-line"
1527function screen_it {
1528 if is_service_enabled $1; then
1529 # Append the service to the screen rc file
1530 screen_rc "$1" "$2"
1531
1532 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001533 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001534 else
1535 # Spawn directly without screen
1536 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1537 fi
1538 fi
1539}
1540
1541# Compatibility for existing stop_XXXX() functions
1542# Stop a service in screen
1543# If a PID is available use it, kill the whole process group via TERM
1544# If screen is being used kill the screen window; this will catch processes
1545# that did not leave a PID behind
1546# screen_stop service
1547function screen_stop {
1548 # Clean up the screen window
1549 stop_process $1
1550}
1551
1552
Sean Dague2c65e712014-12-18 09:44:56 -05001553# Plugin Functions
1554# =================
1555
1556DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1557
1558# enable_plugin <name> <url> [branch]
1559#
1560# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1561# ``url`` is a git url
1562# ``branch`` is a gitref. If it's not set, defaults to master
1563function enable_plugin {
1564 local name=$1
1565 local url=$2
1566 local branch=${3:-master}
1567 DEVSTACK_PLUGINS+=",$name"
1568 GITREPO[$name]=$url
1569 GITDIR[$name]=$DEST/$name
1570 GITBRANCH[$name]=$branch
1571}
1572
1573# fetch_plugins
1574#
1575# clones all plugins
1576function fetch_plugins {
1577 local plugins="${DEVSTACK_PLUGINS}"
1578 local plugin
1579
1580 # short circuit if nothing to do
1581 if [[ -z $plugins ]]; then
1582 return
1583 fi
1584
Dean Troyerdc97cb72015-03-28 08:20:50 -05001585 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001586 for plugin in ${plugins//,/ }; do
1587 git_clone_by_name $plugin
1588 done
1589}
1590
1591# load_plugin_settings
1592#
1593# Load settings from plugins in the order that they were registered
1594function load_plugin_settings {
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 "Loading plugin settings"
1604 for plugin in ${plugins//,/ }; do
1605 local dir=${GITDIR[$plugin]}
1606 # source any known settings
1607 if [[ -f $dir/devstack/settings ]]; then
1608 source $dir/devstack/settings
1609 fi
1610 done
1611}
1612
Sean Dague6e275e12015-03-26 05:54:28 -04001613# plugin_override_defaults
1614#
1615# Run an extremely early setting phase for plugins that allows default
1616# overriding of services.
1617function plugin_override_defaults {
1618 local plugins="${DEVSTACK_PLUGINS}"
1619 local plugin
1620
1621 # short circuit if nothing to do
1622 if [[ -z $plugins ]]; then
1623 return
1624 fi
1625
1626 echo "Overriding Configuration Defaults"
1627 for plugin in ${plugins//,/ }; do
1628 local dir=${GITDIR[$plugin]}
1629 # source any overrides
1630 if [[ -f $dir/devstack/override-defaults ]]; then
1631 # be really verbose that an override is happening, as it
1632 # may not be obvious if things fail later.
1633 echo "$plugin has overriden the following defaults"
1634 cat $dir/devstack/override-defaults
1635 source $dir/devstack/override-defaults
1636 fi
1637 done
1638}
1639
Sean Dague2c65e712014-12-18 09:44:56 -05001640# run_plugins
1641#
1642# Run the devstack/plugin.sh in all the plugin directories. These are
1643# run in registration order.
1644function run_plugins {
1645 local mode=$1
1646 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301647
1648 local plugins="${DEVSTACK_PLUGINS}"
1649 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001650 for plugin in ${plugins//,/ }; do
1651 local dir=${GITDIR[$plugin]}
1652 if [[ -f $dir/devstack/plugin.sh ]]; then
1653 source $dir/devstack/plugin.sh $mode $phase
1654 fi
1655 done
1656}
1657
1658function run_phase {
1659 local mode=$1
1660 local phase=$2
1661 if [[ -d $TOP_DIR/extras.d ]]; then
1662 for i in $TOP_DIR/extras.d/*.sh; do
1663 [[ -r $i ]] && source $i $mode $phase
1664 done
1665 fi
1666 # the source phase corresponds to settings loading in plugins
1667 if [[ "$mode" == "source" ]]; then
1668 load_plugin_settings
Sean Dague6e275e12015-03-26 05:54:28 -04001669 elif [[ "$mode" == "override_defaults" ]]; then
1670 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001671 else
1672 run_plugins $mode $phase
1673 fi
1674}
1675
Dean Troyerdff49a22014-01-30 15:37:40 -06001676
1677# Service Functions
1678# =================
1679
1680# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1681# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001682function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001683 echo "$1" | sed -e '
1684 s/,,/,/g;
1685 s/^,//;
1686 s/,$//
1687 '
1688}
1689
1690# disable_all_services() removes all current services
1691# from ``ENABLED_SERVICES`` to reset the configuration
1692# before a minimal installation
1693# Uses global ``ENABLED_SERVICES``
1694# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001695function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001696 ENABLED_SERVICES=""
1697}
1698
1699# Remove all services starting with '-'. For example, to install all default
1700# services except rabbit (rabbit) set in ``localrc``:
1701# ENABLED_SERVICES+=",-rabbit"
1702# Uses global ``ENABLED_SERVICES``
1703# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001704function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001705 local to_remove=""
1706 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001707 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001708
1709 # build up list of services that should be removed; i.e. they
1710 # begin with "-"
1711 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001712 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001713 to_remove+=",${service#-}"
1714 else
1715 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001716 fi
1717 done
Ian Wienand2796a822015-04-15 08:59:04 +10001718
1719 # go through the service list. if this service appears in the "to
1720 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001721 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Dean Troyerdff49a22014-01-30 15:37:40 -06001722}
1723
1724# disable_service() removes the services passed as argument to the
1725# ``ENABLED_SERVICES`` list, if they are present.
1726#
1727# For example:
1728# disable_service rabbit
1729#
1730# This function does not know about the special cases
1731# for nova, glance, and neutron built into is_service_enabled().
1732# Uses global ``ENABLED_SERVICES``
1733# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001734function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001735 local tmpsvcs=",${ENABLED_SERVICES},"
1736 local service
1737 for service in $@; do
1738 if is_service_enabled $service; then
1739 tmpsvcs=${tmpsvcs//,$service,/,}
1740 fi
1741 done
1742 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1743}
1744
1745# enable_service() adds the services passed as argument to the
1746# ``ENABLED_SERVICES`` list, if they are not already present.
1747#
1748# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001749# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001750#
1751# This function does not know about the special cases
1752# for nova, glance, and neutron built into is_service_enabled().
1753# Uses global ``ENABLED_SERVICES``
1754# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001755function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001756 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001757 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001758 for service in $@; do
1759 if ! is_service_enabled $service; then
1760 tmpsvcs+=",$service"
1761 fi
1762 done
1763 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1764 disable_negated_services
1765}
1766
1767# is_service_enabled() checks if the service(s) specified as arguments are
1768# enabled by the user in ``ENABLED_SERVICES``.
1769#
1770# Multiple services specified as arguments are ``OR``'ed together; the test
1771# is a short-circuit boolean, i.e it returns on the first match.
1772#
1773# There are special cases for some 'catch-all' services::
1774# **nova** returns true if any service enabled start with **n-**
1775# **cinder** returns true if any service enabled start with **c-**
Dean Troyerdff49a22014-01-30 15:37:40 -06001776# **glance** returns true if any service enabled start with **g-**
1777# **neutron** returns true if any service enabled start with **q-**
1778# **swift** returns true if any service enabled start with **s-**
1779# **trove** returns true if any service enabled start with **tr-**
1780# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1781# **s-** services will be enabled. This will be deprecated in the future.
1782#
1783# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1784# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1785# as enabled in this case.
1786#
1787# Uses global ``ENABLED_SERVICES``
1788# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001789function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001790 local xtrace=$(set +o | grep xtrace)
1791 set +o xtrace
1792 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001793 local services=$@
1794 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001795 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001796 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001797
1798 # Look for top-level 'enabled' function for this service
1799 if type is_${service}_enabled >/dev/null 2>&1; then
1800 # A function exists for this service, use it
1801 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001802 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001803 fi
1804
1805 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1806 # are implemented
1807
Sean Dague45917cc2014-02-24 16:09:14 -05001808 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001809 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001810 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001811 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1812 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1813 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1814 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1815 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1816 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001817 done
Sean Dague45917cc2014-02-24 16:09:14 -05001818 $xtrace
1819 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001820}
1821
fumihiko kakuma8606c982015-04-13 09:55:06 +09001822# remove specified list from the input string
1823# remove_disabled_services service-list remove-list
1824function remove_disabled_services {
1825 local service_list=$1
1826 local remove_list=$2
1827 local service
1828 local enabled=""
1829
1830 for service in ${service_list//,/ }; do
1831 local remove
1832 local add=1
1833 for remove in ${remove_list//,/ }; do
1834 if [[ ${remove} == ${service} ]]; then
1835 add=0
1836 break
1837 fi
1838 done
1839 if [[ $add == 1 ]]; then
1840 enabled="${enabled},$service"
1841 fi
1842 done
1843 _cleanup_service_list "$enabled"
1844}
1845
Dean Troyerdff49a22014-01-30 15:37:40 -06001846# Toggle enable/disable_service for services that must run exclusive of each other
1847# $1 The name of a variable containing a space-separated list of services
1848# $2 The name of a variable in which to store the enabled service's name
1849# $3 The name of the service to enable
1850function use_exclusive_service {
1851 local options=${!1}
1852 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001853 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001854 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001855 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001856 for opt in $options;do
1857 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1858 done
1859 eval "$out=$selection"
1860 return 0
1861}
1862
1863
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001864# System Functions
1865# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001866
1867# Only run the command if the target file (the last arg) is not on an
1868# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001869function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001870 local xtrace=$(set +o | grep xtrace)
1871 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001872 local args=( $@ )
1873 local last
1874 local sudo_cmd
1875 local dir_to_check
1876
1877 let last="${#args[*]} - 1"
1878
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001879 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001880 if [ ! -d "$dir_to_check" ]; then
1881 dir_to_check=`dirname "$dir_to_check"`
1882 fi
1883
1884 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001885 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001886 return 0
1887 fi
1888
1889 if [[ $TRACK_DEPENDS = True ]]; then
1890 sudo_cmd="env"
1891 else
1892 sudo_cmd="sudo"
1893 fi
1894
Sean Dague45917cc2014-02-24 16:09:14 -05001895 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001896 $sudo_cmd $@
1897}
1898
1899# Exit 0 if address is in network or 1 if address is not in network
1900# ip-range is in CIDR notation: 1.2.3.4/20
1901# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001902function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001903 local ip=$1
1904 local range=$2
1905 local masklen=${range#*/}
1906 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1907 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1908 [[ $network == $subnet ]]
1909}
1910
1911# Add a user to a group.
1912# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001913function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001914 local user=$1
1915 local group=$2
1916
Thomas Bechtolda8580852015-05-31 00:04:33 +02001917 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06001918}
1919
1920# Convert CIDR notation to a IPv4 netmask
1921# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001922function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001923 local maskpat="255 255 255 255"
1924 local maskdgt="254 252 248 240 224 192 128"
1925 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1926 echo ${1-0}.${2-0}.${3-0}.${4-0}
1927}
1928
1929# Gracefully cp only if source file/dir exists
1930# cp_it source destination
1931function cp_it {
1932 if [ -e $1 ] || [ -d $1 ]; then
1933 cp -pRL $1 $2
1934 fi
1935}
1936
1937# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1938# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1939# ``localrc`` or on the command line if necessary::
1940#
1941# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1942#
1943# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1944
Ian Wienandaee18c72014-02-21 15:35:08 +11001945function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001946 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001947 export http_proxy=$http_proxy
1948 fi
Sean Dague53753292014-12-04 19:38:15 -05001949 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001950 export https_proxy=$https_proxy
1951 fi
Sean Dague53753292014-12-04 19:38:15 -05001952 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001953 export no_proxy=$no_proxy
1954 fi
1955}
1956
1957# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001958function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001959 local mount_type=`stat -f -L -c %T $1`
1960 test "$mount_type" == "nfs"
1961}
1962
1963# Return the network portion of the given IP address using netmask
1964# netmask is in the traditional dotted-quad format
1965# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001966function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001967 local ip=$1
1968 local mask=$2
1969 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1970 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1971 echo $subnet
1972}
1973
Chris Dent3a2c86a2015-05-12 13:41:25 +00001974# Return the current python as "python<major>.<minor>"
1975function python_version {
1976 local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
1977 echo "python${python_version}"
1978}
1979
Dean Troyerdff49a22014-01-30 15:37:40 -06001980# Service wrapper to restart services
1981# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001982function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001983 if is_ubuntu; then
1984 sudo /usr/sbin/service $1 restart
1985 else
1986 sudo /sbin/service $1 restart
1987 fi
1988}
1989
1990# Only change permissions of a file or directory if it is not on an
1991# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001992function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001993 _safe_permission_operation chmod $@
1994}
1995
1996# Only change ownership of a file or directory if it is not on an NFS
1997# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001998function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001999 _safe_permission_operation chown $@
2000}
2001
2002# Service wrapper to start services
2003# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002004function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002005 if is_ubuntu; then
2006 sudo /usr/sbin/service $1 start
2007 else
2008 sudo /sbin/service $1 start
2009 fi
2010}
2011
2012# Service wrapper to stop services
2013# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002014function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002015 if is_ubuntu; then
2016 sudo /usr/sbin/service $1 stop
2017 else
2018 sudo /sbin/service $1 stop
2019 fi
2020}
2021
Sean Dague442e4e92015-06-24 13:24:02 -04002022# Test with a finite retry loop.
2023#
2024function test_with_retry {
2025 local testcmd=$1
2026 local failmsg=$2
2027 local until=${3:-10}
2028 local sleep=${4:-0.5}
2029
2030 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2031 die $LINENO "$failmsg"
2032 fi
2033}
2034
Dean Troyerdff49a22014-01-30 15:37:40 -06002035
2036# Restore xtrace
2037$XTRACE
2038
2039# Local variables:
2040# mode: shell-script
2041# End: