blob: c38a77243e2cbf96b560dba387ec3e5b2c001b3e [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
Roxana Gherle59c63772015-09-09 18:22:31 -0700806# Gets or adds user role to domain
807# Usage: get_or_add_user_domain_role <role> <user> <domain>
808function get_or_add_user_domain_role {
809 local user_role_id
810 # Gets user role id
811 user_role_id=$(openstack role list \
812 --user $2 \
813 --os-url=$KEYSTONE_SERVICE_URI_V3 \
814 --os-identity-api-version=3 \
815 --column "ID" \
816 --domain $3 \
817 --column "Name" \
818 | grep " $1 " | get_field 1)
819 if [[ -z "$user_role_id" ]]; then
820 # Adds role to user and get it
821 openstack role add $1 \
822 --user $2 \
823 --domain $3 \
824 --os-url=$KEYSTONE_SERVICE_URI_V3 \
825 --os-identity-api-version=3
826 user_role_id=$(openstack role list \
827 --user $2 \
828 --os-url=$KEYSTONE_SERVICE_URI_V3 \
829 --os-identity-api-version=3 \
830 --column "ID" \
831 --domain $3 \
832 --column "Name" \
833 | grep " $1 " | get_field 1)
834 fi
835 echo $user_role_id
836}
837
Steve Martinelli4599fd12015-03-12 21:30:58 -0400838# Gets or adds group role to project
839# Usage: get_or_add_group_project_role <role> <group> <project>
840function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000841 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -0400842 # Gets group role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000843 group_role_id=$(openstack role list \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100844 --os-url=$KEYSTONE_SERVICE_URI_V3 \
845 --os-identity-api-version=3 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400846 --group $2 \
847 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000848 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400849 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000850 # Adds role to group and get it
851 openstack role add $1 \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100852 --os-url=$KEYSTONE_SERVICE_URI_V3 \
853 --os-identity-api-version=3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000854 --group $2 \
855 --project $3
856 group_role_id=$(openstack role list \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100857 --os-url=$KEYSTONE_SERVICE_URI_V3 \
858 --os-identity-api-version=3 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400859 --group $2 \
860 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000861 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400862 fi
863 echo $group_role_id
864}
865
Bartosz Górski0abde392014-02-28 14:15:19 +0100866# Gets or creates service
867# Usage: get_or_create_service <name> <type> <description>
868function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +1000869 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100870 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +1000871 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100872 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +1000873 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100874 # Creates new service if not exists
875 openstack service create \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100876 --os-url $KEYSTONE_SERVICE_URI_V3 \
877 --os-identity-api-version=3 \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500878 $2 \
879 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100880 --description="$3" \
881 -f value -c id
882 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500883 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100884}
885
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000886# Create an endpoint with a specific interface
887# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
888function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +1000889 local endpoint_id
Daniel Gonzalez1991e752015-08-11 19:34:22 +0200890 # TODO(dgonzalez): The check of the region name, as done in the grep
891 # statement below, exists only because keystone does currently
892 # not allow filtering the region name when listing endpoints. If keystone
893 # gets support for this, the check for the region name can be removed.
894 # Related bug in keystone: https://bugs.launchpad.net/keystone/+bug/1482772
Ian Wienand11d276c2015-07-02 09:34:34 +1000895 endpoint_id=$(openstack endpoint list \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100896 --os-url $KEYSTONE_SERVICE_URI_V3 \
897 --os-identity-api-version=3 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000898 --service $1 \
899 --interface $2 \
900 --region $4 \
Daniel Gonzalez1991e752015-08-11 19:34:22 +0200901 -c ID -c Region -f value | grep $4 | cut -f 1 -d " ")
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500902 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100903 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500904 endpoint_id=$(openstack endpoint create \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100905 --os-url $KEYSTONE_SERVICE_URI_V3 \
906 --os-identity-api-version=3 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000907 $1 $2 $3 --region $4 -f value -c id)
Bartosz Górski0abde392014-02-28 14:15:19 +0100908 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000909
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500910 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100911}
Dean Troyerdff49a22014-01-30 15:37:40 -0600912
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000913# Gets or creates endpoint
914# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
915function get_or_create_endpoint {
916 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
917 # creating one endpoint with multiple urls to multiple endpoints each with
918 # a different interface. To maintain the existing function interface we
919 # create 3 endpoints and return the id of the public one. In reality
920 # returning the public id will not make a lot of difference as there are no
921 # scenarios currently that use the returned id. Ideally this behaviour
922 # should be pushed out to the service setups and let them create the
923 # endpoints they need.
924 local public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
925 _get_or_create_endpoint_with_interface $1 admin $4 $2
926 _get_or_create_endpoint_with_interface $1 internal $5 $2
927
928 # return the public id to indicate success, and this is the endpoint most likely wanted
929 echo $public_id
930}
931
932# Get a URL from the identity service
933# Usage: get_endpoint_url <service> <interface>
934function get_endpoint_url {
935 echo $(openstack endpoint list \
936 --service $1 --interface $2 \
937 --os-url $KEYSTONE_SERVICE_URI_V3 \
938 --os-identity-api-version=3 \
939 -c URL -f value)
940}
941
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500942
Dean Troyerdff49a22014-01-30 15:37:40 -0600943# Package Functions
944# =================
945
946# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100947function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800948 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600949 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800950
951 if [[ -z "$base_dir" ]]; then
952 base_dir=$FILES
953 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600954 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800955 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600956 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800957 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600958 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800959 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600960 else
961 exit_distro_not_supported "list of packages"
962 fi
963 echo "$pkg_dir"
964}
965
966# Wrapper for ``apt-get`` to set cache and proxy environment variables
967# Uses globals ``OFFLINE``, ``*_proxy``
968# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100969function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500970 local xtrace=$(set +o | grep xtrace)
971 set +o xtrace
972
Dean Troyerdff49a22014-01-30 15:37:40 -0600973 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
974 local sudo="sudo"
975 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500976
977 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500978
Dean Troyerdff49a22014-01-30 15:37:40 -0600979 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500980 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
981 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600982 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
983}
984
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800985function _parse_package_files {
986 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600987
Dean Troyerdff49a22014-01-30 15:37:40 -0600988 if [[ -z "$DISTRO" ]]; then
989 GetDistro
990 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600991
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800992 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600993 local OIFS line package distros distro
994 [[ -e $fname ]] || continue
995
996 OIFS=$IFS
997 IFS=$'\n'
998 for line in $(<${fname}); do
999 if [[ $line =~ "NOPRIME" ]]; then
1000 continue
1001 fi
1002
1003 # Assume we want this package
1004 package=${line%#*}
1005 inst_pkg=1
1006
1007 # Look for # dist:xxx in comment
1008 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1009 # We are using BASH regexp matching feature.
1010 package=${BASH_REMATCH[1]}
1011 distros=${BASH_REMATCH[2]}
1012 # In bash ${VAR,,} will lowecase VAR
1013 # Look for a match in the distro list
1014 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1015 # If no match then skip this package
1016 inst_pkg=0
1017 fi
1018 fi
1019
Dean Troyerdff49a22014-01-30 15:37:40 -06001020 if [[ $inst_pkg = 1 ]]; then
1021 echo $package
1022 fi
1023 done
1024 IFS=$OIFS
1025 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001026}
1027
1028# get_packages() collects a list of package names of any type from the
1029# prerequisite files in ``files/{debs|rpms}``. The list is intended
1030# to be passed to a package installer such as apt or yum.
1031#
1032# Only packages required for the services in 1st argument will be
1033# included. Two bits of metadata are recognized in the prerequisite files:
1034#
1035# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1036# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1037# of the package to the distros listed. The distro names are case insensitive.
1038function get_packages {
1039 local xtrace=$(set +o | grep xtrace)
1040 set +o xtrace
1041 local services=$@
1042 local package_dir=$(_get_package_dir)
1043 local file_to_parse=""
1044 local service=""
1045
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001046 if [[ -z "$package_dir" ]]; then
1047 echo "No package directory supplied"
1048 return 1
1049 fi
1050 for service in ${services//,/ }; do
1051 # Allow individual services to specify dependencies
1052 if [[ -e ${package_dir}/${service} ]]; then
1053 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1054 fi
1055 # NOTE(sdague) n-api needs glance for now because that's where
1056 # glance client is
1057 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001058 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001059 file_to_parse="${file_to_parse} ${package_dir}/nova"
1060 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001061 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001062 file_to_parse="${file_to_parse} ${package_dir}/glance"
1063 fi
1064 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001065 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001066 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1067 fi
1068 elif [[ $service == ceilometer-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001069 if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001070 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
1071 fi
1072 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001073 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001074 file_to_parse="${file_to_parse} ${package_dir}/swift"
1075 fi
1076 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001077 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001078 file_to_parse="${file_to_parse} ${package_dir}/nova"
1079 fi
1080 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001081 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001082 file_to_parse="${file_to_parse} ${package_dir}/glance"
1083 fi
1084 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001085 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001086 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1087 fi
1088 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001089 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001090 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1091 fi
1092 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001093 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001094 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1095 fi
1096 fi
1097 done
1098 echo "$(_parse_package_files $file_to_parse)"
1099 $xtrace
1100}
1101
1102# get_plugin_packages() collects a list of package names of any type from a
1103# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1104# list is intended to be passed to a package installer such as apt or yum.
1105#
1106# Only packages required for enabled and collected plugins will included.
1107#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001108# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001109# in these prerequisite files, see get_packages() for more info.
1110function get_plugin_packages {
1111 local xtrace=$(set +o | grep xtrace)
1112 set +o xtrace
1113 local files_to_parse=""
1114 local package_dir=""
1115 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1116 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1117 files_to_parse+="$package_dir/$plugin"
1118 done
1119 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001120 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001121}
1122
1123# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001124# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001125# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001126function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001127 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1128 REPOS_UPDATED=${REPOS_UPDATED:-False}
1129 RETRY_UPDATE=${RETRY_UPDATE:-False}
1130
Paul Linchpiner9e179742014-07-13 22:23:00 -07001131 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001132 return 0
1133 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001134
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001135 if is_ubuntu; then
1136 local xtrace=$(set +o | grep xtrace)
1137 set +o xtrace
1138 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1139 # if there are transient errors pulling the updates, that's fine.
1140 # It may be secondary repositories that we don't really care about.
1141 apt_get update || /bin/true
1142 REPOS_UPDATED=True
1143 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001144 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001145 fi
1146}
1147
1148function real_install_package {
1149 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001150 apt_get install "$@"
1151 elif is_fedora; then
1152 yum_install "$@"
1153 elif is_suse; then
1154 zypper_install "$@"
1155 else
1156 exit_distro_not_supported "installing packages"
1157 fi
1158}
1159
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001160# Distro-agnostic package installer
1161# install_package package [package ...]
1162function install_package {
1163 update_package_repo
1164 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1165}
1166
Dean Troyerdff49a22014-01-30 15:37:40 -06001167# Distro-agnostic function to tell if a package is installed
1168# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001169function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001170 if [[ -z "$@" ]]; then
1171 return 1
1172 fi
1173
1174 if [[ -z "$os_PACKAGE" ]]; then
1175 GetOSVersion
1176 fi
1177
1178 if [[ "$os_PACKAGE" = "deb" ]]; then
1179 dpkg -s "$@" > /dev/null 2> /dev/null
1180 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1181 rpm --quiet -q "$@"
1182 else
1183 exit_distro_not_supported "finding if a package is installed"
1184 fi
1185}
1186
1187# Distro-agnostic package uninstaller
1188# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001189function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001190 if is_ubuntu; then
1191 apt_get purge "$@"
1192 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001193 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001194 elif is_suse; then
1195 sudo zypper rm "$@"
1196 else
1197 exit_distro_not_supported "uninstalling packages"
1198 fi
1199}
1200
1201# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001202# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001203# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001204function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001205 [[ "$OFFLINE" = "True" ]] && return
1206 local sudo="sudo"
1207 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001208
1209 # The manual check for missing packages is because yum -y assumes
1210 # missing packages are OK. See
1211 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001212 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1213 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001214 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001215 awk '
1216 BEGIN { fail=0 }
1217 /No package/ { fail=1 }
1218 { print }
1219 END { exit fail }' || \
1220 die $LINENO "Missing packages detected"
1221
1222 # also ensure we catch a yum failure
1223 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001224 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001225 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001226}
1227
1228# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001229# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001230# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001231function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001232 [[ "$OFFLINE" = "True" ]] && return
1233 local sudo="sudo"
1234 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001235 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1236 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001237 zypper --non-interactive install --auto-agree-with-licenses "$@"
1238}
1239
1240
1241# Process Functions
1242# =================
1243
1244# _run_process() is designed to be backgrounded by run_process() to simulate a
1245# fork. It includes the dirty work of closing extra filehandles and preparing log
1246# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001247# from the service name.
1248# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001249# If an optional group is provided sg will be used to set the group of
1250# the command.
1251# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001252function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001253 # disable tracing through the exec redirects, it's just confusing in the logs.
1254 xtrace=$(set +o | grep xtrace)
1255 set +o xtrace
1256
Dean Troyerdff49a22014-01-30 15:37:40 -06001257 local service=$1
1258 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001259 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001260
1261 # Undo logging redirections and close the extra descriptors
1262 exec 1>&3
1263 exec 2>&3
1264 exec 3>&-
1265 exec 6>&-
1266
Dean Troyerdde41d02014-12-09 17:47:57 -06001267 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1268 if [[ -n ${LOGDIR} ]]; then
1269 exec 1>&"$real_logfile" 2>&1
1270 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1271 if [[ -n ${SCREEN_LOGDIR} ]]; then
1272 # Drop the backward-compat symlink
1273 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1274 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001275
1276 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1277 export PYTHONUNBUFFERED=1
1278 fi
1279
Sean Dague6e137ab2015-04-29 08:22:24 -04001280 # reenable xtrace before we do *real* work
1281 $xtrace
1282
Dean Troyer3159a822014-08-27 14:13:58 -05001283 # Run under ``setsid`` to force the process to become a session and group leader.
1284 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001285 if [[ -n "$group" ]]; then
1286 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1287 else
1288 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1289 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001290
1291 # Just silently exit this process
1292 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001293}
1294
1295# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1296# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001297# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001298# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001299function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001300 SCREEN_NAME=${SCREEN_NAME:-stack}
1301 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1302
1303 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1304 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1305 fi
1306
1307 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1308}
1309
1310# Find out if a process exists by partial name.
1311# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001312function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001313 local name=$1
1314 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001315 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001316 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001317 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001318}
1319
Dean Troyer3159a822014-08-27 14:13:58 -05001320# Run a single service under screen or directly
1321# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001322# If an optional group is provided sg will be used to run the
1323# command as that group.
1324# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001325function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001326 local service=$1
1327 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001328 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001329
Dean Troyer3159a822014-08-27 14:13:58 -05001330 if is_service_enabled $service; then
1331 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001332 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001333 else
1334 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001335 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001336 fi
1337 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001338}
1339
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001340# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001341# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001342# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001343# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001344# Run a command in a shell in a screen window, if an optional group
1345# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001346function screen_process {
1347 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001348 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001349 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001350
Sean Dagueea22a4f2014-06-27 15:21:41 -04001351 SCREEN_NAME=${SCREEN_NAME:-stack}
1352 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001353 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001354
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001355 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001356
Dean Troyerdde41d02014-12-09 17:47:57 -06001357 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1358 echo "LOGDIR: $LOGDIR"
1359 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1360 echo "log: $real_logfile"
1361 if [[ -n ${LOGDIR} ]]; then
1362 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001363 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001364 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1365 if [[ -n ${SCREEN_LOGDIR} ]]; then
1366 # Drop the backward-compat symlink
1367 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1368 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001369 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001370
1371 # sleep to allow bash to be ready to be send the command - we are
1372 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001373 # bash isn't running by the time we send the command, nothing
1374 # happens. This sleep was added originally to handle gate runs
1375 # where we needed this to be at least 3 seconds to pass
1376 # consistently on slow clouds. Now this is configurable so that we
1377 # can determine a reasonable value for the local case which should
1378 # be much smaller.
1379 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001380
1381 NL=`echo -ne '\015'`
1382 # This fun command does the following:
1383 # - the passed server command is backgrounded
1384 # - the pid of the background process is saved in the usual place
1385 # - the server process is brought back to the foreground
1386 # - if the server process exits prematurely the fg command errors
1387 # and a message is written to stdout and the process failure file
1388 #
1389 # The pid saved can be used in stop_process() as a process group
1390 # id to kill off all child processes
1391 if [[ -n "$group" ]]; then
1392 command="sg $group '$command'"
1393 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001394
1395 # Append the process to the screen rc file
1396 screen_rc "$name" "$command"
1397
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001398 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 -06001399}
1400
1401# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001402# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001403# screen_rc service "command-line"
1404function screen_rc {
1405 SCREEN_NAME=${SCREEN_NAME:-stack}
1406 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1407 if [[ ! -e $SCREENRC ]]; then
1408 # Name the screen session
1409 echo "sessionname $SCREEN_NAME" > $SCREENRC
1410 # Set a reasonable statusbar
1411 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1412 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1413 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1414 echo "screen -t shell bash" >> $SCREENRC
1415 fi
1416 # If this service doesn't already exist in the screenrc file
1417 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1418 NL=`echo -ne '\015'`
1419 echo "screen -t $1 bash" >> $SCREENRC
1420 echo "stuff \"$2$NL\"" >> $SCREENRC
1421
Dean Troyerdde41d02014-12-09 17:47:57 -06001422 if [[ -n ${LOGDIR} ]]; then
1423 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001424 echo "log on" >>$SCREENRC
1425 fi
1426 fi
1427}
1428
1429# Stop a service in screen
1430# If a PID is available use it, kill the whole process group via TERM
1431# If screen is being used kill the screen window; this will catch processes
1432# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001433# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001434# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001435function screen_stop_service {
1436 local service=$1
1437
Dean Troyerdff49a22014-01-30 15:37:40 -06001438 SCREEN_NAME=${SCREEN_NAME:-stack}
1439 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001440 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001441
Dean Troyer3159a822014-08-27 14:13:58 -05001442 if is_service_enabled $service; then
1443 # Clean up the screen window
Attila Fazekasf750a6f2015-07-01 12:17:35 +02001444 screen -S $SCREEN_NAME -p $service -X kill || true
Dean Troyer3159a822014-08-27 14:13:58 -05001445 fi
1446}
1447
1448# Stop a service process
1449# If a PID is available use it, kill the whole process group via TERM
1450# If screen is being used kill the screen window; this will catch processes
1451# that did not leave a PID behind
1452# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1453# stop_process service
1454function stop_process {
1455 local service=$1
1456
1457 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001458 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001459
1460 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001461 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001462 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1463 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1464 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001465 fi
1466 if [[ "$USE_SCREEN" = "True" ]]; then
1467 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001468 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001469 fi
1470 fi
1471}
1472
1473# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001474# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001475# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001476function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001477 local service
1478 local failures
1479 SCREEN_NAME=${SCREEN_NAME:-stack}
1480 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1481
1482
1483 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1484 echo "No service status directory found"
1485 return
1486 fi
1487
1488 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001489 # make this -o errexit safe
1490 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001491
1492 for service in $failures; do
1493 service=`basename $service`
1494 service=${service%.failure}
1495 echo "Error: Service $service is not running"
1496 done
1497
1498 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001499 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001500 fi
1501}
1502
Chris Dent2f27a0e2014-09-09 13:46:02 +01001503# Tail a log file in a screen if USE_SCREEN is true.
1504function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001505 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001506 local logfile=$2
1507
Sean Dague53753292014-12-04 19:38:15 -05001508 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001509 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001510 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001511 fi
1512}
1513
Dean Troyerdff49a22014-01-30 15:37:40 -06001514
Dean Troyer3159a822014-08-27 14:13:58 -05001515# Deprecated Functions
1516# --------------------
1517
1518# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1519# fork. It includes the dirty work of closing extra filehandles and preparing log
1520# files to produce the same logs as screen_it(). The log filename is derived
1521# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1522# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1523# _old_run_process service "command-line"
1524function _old_run_process {
1525 local service=$1
1526 local command="$2"
1527
1528 # Undo logging redirections and close the extra descriptors
1529 exec 1>&3
1530 exec 2>&3
1531 exec 3>&-
1532 exec 6>&-
1533
1534 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001535 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1536 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001537
1538 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1539 export PYTHONUNBUFFERED=1
1540 fi
1541
1542 exec /bin/bash -c "$command"
1543 die "$service exec failure: $command"
1544}
1545
1546# old_run_process() launches a child process that closes all file descriptors and
1547# then exec's the passed in command. This is meant to duplicate the semantics
1548# of screen_it() without screen. PIDs are written to
1549# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1550# old_run_process service "command-line"
1551function old_run_process {
1552 local service=$1
1553 local command="$2"
1554
1555 # Spawn the child process
1556 _old_run_process "$service" "$command" &
1557 echo $!
1558}
1559
1560# Compatibility for existing start_XXXX() functions
1561# Uses global ``USE_SCREEN``
1562# screen_it service "command-line"
1563function screen_it {
1564 if is_service_enabled $1; then
1565 # Append the service to the screen rc file
1566 screen_rc "$1" "$2"
1567
1568 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001569 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001570 else
1571 # Spawn directly without screen
1572 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1573 fi
1574 fi
1575}
1576
1577# Compatibility for existing stop_XXXX() functions
1578# Stop a service in screen
1579# If a PID is available use it, kill the whole process group via TERM
1580# If screen is being used kill the screen window; this will catch processes
1581# that did not leave a PID behind
1582# screen_stop service
1583function screen_stop {
1584 # Clean up the screen window
1585 stop_process $1
1586}
1587
1588
Sean Dague2c65e712014-12-18 09:44:56 -05001589# Plugin Functions
1590# =================
1591
1592DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1593
1594# enable_plugin <name> <url> [branch]
1595#
1596# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1597# ``url`` is a git url
1598# ``branch`` is a gitref. If it's not set, defaults to master
1599function enable_plugin {
1600 local name=$1
1601 local url=$2
1602 local branch=${3:-master}
1603 DEVSTACK_PLUGINS+=",$name"
1604 GITREPO[$name]=$url
1605 GITDIR[$name]=$DEST/$name
1606 GITBRANCH[$name]=$branch
1607}
1608
1609# fetch_plugins
1610#
1611# clones all plugins
1612function fetch_plugins {
1613 local plugins="${DEVSTACK_PLUGINS}"
1614 local plugin
1615
1616 # short circuit if nothing to do
1617 if [[ -z $plugins ]]; then
1618 return
1619 fi
1620
Dean Troyerdc97cb72015-03-28 08:20:50 -05001621 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001622 for plugin in ${plugins//,/ }; do
1623 git_clone_by_name $plugin
1624 done
1625}
1626
1627# load_plugin_settings
1628#
1629# Load settings from plugins in the order that they were registered
1630function load_plugin_settings {
1631 local plugins="${DEVSTACK_PLUGINS}"
1632 local plugin
1633
1634 # short circuit if nothing to do
1635 if [[ -z $plugins ]]; then
1636 return
1637 fi
1638
1639 echo "Loading plugin settings"
1640 for plugin in ${plugins//,/ }; do
1641 local dir=${GITDIR[$plugin]}
1642 # source any known settings
1643 if [[ -f $dir/devstack/settings ]]; then
1644 source $dir/devstack/settings
1645 fi
1646 done
1647}
1648
Sean Dague6e275e12015-03-26 05:54:28 -04001649# plugin_override_defaults
1650#
1651# Run an extremely early setting phase for plugins that allows default
1652# overriding of services.
1653function plugin_override_defaults {
1654 local plugins="${DEVSTACK_PLUGINS}"
1655 local plugin
1656
1657 # short circuit if nothing to do
1658 if [[ -z $plugins ]]; then
1659 return
1660 fi
1661
1662 echo "Overriding Configuration Defaults"
1663 for plugin in ${plugins//,/ }; do
1664 local dir=${GITDIR[$plugin]}
1665 # source any overrides
1666 if [[ -f $dir/devstack/override-defaults ]]; then
1667 # be really verbose that an override is happening, as it
1668 # may not be obvious if things fail later.
1669 echo "$plugin has overriden the following defaults"
1670 cat $dir/devstack/override-defaults
1671 source $dir/devstack/override-defaults
1672 fi
1673 done
1674}
1675
Sean Dague2c65e712014-12-18 09:44:56 -05001676# run_plugins
1677#
1678# Run the devstack/plugin.sh in all the plugin directories. These are
1679# run in registration order.
1680function run_plugins {
1681 local mode=$1
1682 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301683
1684 local plugins="${DEVSTACK_PLUGINS}"
1685 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001686 for plugin in ${plugins//,/ }; do
1687 local dir=${GITDIR[$plugin]}
1688 if [[ -f $dir/devstack/plugin.sh ]]; then
1689 source $dir/devstack/plugin.sh $mode $phase
1690 fi
1691 done
1692}
1693
1694function run_phase {
1695 local mode=$1
1696 local phase=$2
1697 if [[ -d $TOP_DIR/extras.d ]]; then
1698 for i in $TOP_DIR/extras.d/*.sh; do
1699 [[ -r $i ]] && source $i $mode $phase
1700 done
1701 fi
1702 # the source phase corresponds to settings loading in plugins
1703 if [[ "$mode" == "source" ]]; then
1704 load_plugin_settings
Sean Dague6e275e12015-03-26 05:54:28 -04001705 elif [[ "$mode" == "override_defaults" ]]; then
1706 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001707 else
1708 run_plugins $mode $phase
1709 fi
1710}
1711
Dean Troyerdff49a22014-01-30 15:37:40 -06001712
1713# Service Functions
1714# =================
1715
1716# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1717# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001718function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001719 echo "$1" | sed -e '
1720 s/,,/,/g;
1721 s/^,//;
1722 s/,$//
1723 '
1724}
1725
1726# disable_all_services() removes all current services
1727# from ``ENABLED_SERVICES`` to reset the configuration
1728# before a minimal installation
1729# Uses global ``ENABLED_SERVICES``
1730# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001731function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001732 ENABLED_SERVICES=""
1733}
1734
1735# Remove all services starting with '-'. For example, to install all default
1736# services except rabbit (rabbit) set in ``localrc``:
1737# ENABLED_SERVICES+=",-rabbit"
1738# Uses global ``ENABLED_SERVICES``
1739# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001740function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001741 local to_remove=""
1742 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001743 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001744
1745 # build up list of services that should be removed; i.e. they
1746 # begin with "-"
1747 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001748 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001749 to_remove+=",${service#-}"
1750 else
1751 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001752 fi
1753 done
Ian Wienand2796a822015-04-15 08:59:04 +10001754
1755 # go through the service list. if this service appears in the "to
1756 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001757 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Dean Troyerdff49a22014-01-30 15:37:40 -06001758}
1759
1760# disable_service() removes the services passed as argument to the
1761# ``ENABLED_SERVICES`` list, if they are present.
1762#
1763# For example:
1764# disable_service rabbit
1765#
1766# This function does not know about the special cases
1767# for nova, glance, and neutron built into is_service_enabled().
1768# Uses global ``ENABLED_SERVICES``
1769# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001770function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001771 local tmpsvcs=",${ENABLED_SERVICES},"
1772 local service
1773 for service in $@; do
1774 if is_service_enabled $service; then
1775 tmpsvcs=${tmpsvcs//,$service,/,}
1776 fi
1777 done
1778 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1779}
1780
1781# enable_service() adds the services passed as argument to the
1782# ``ENABLED_SERVICES`` list, if they are not already present.
1783#
1784# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001785# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001786#
1787# This function does not know about the special cases
1788# for nova, glance, and neutron built into is_service_enabled().
1789# Uses global ``ENABLED_SERVICES``
1790# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001791function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001792 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001793 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001794 for service in $@; do
1795 if ! is_service_enabled $service; then
1796 tmpsvcs+=",$service"
1797 fi
1798 done
1799 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1800 disable_negated_services
1801}
1802
1803# is_service_enabled() checks if the service(s) specified as arguments are
1804# enabled by the user in ``ENABLED_SERVICES``.
1805#
1806# Multiple services specified as arguments are ``OR``'ed together; the test
1807# is a short-circuit boolean, i.e it returns on the first match.
1808#
1809# There are special cases for some 'catch-all' services::
1810# **nova** returns true if any service enabled start with **n-**
1811# **cinder** returns true if any service enabled start with **c-**
1812# **ceilometer** returns true if any service enabled start with **ceilometer**
1813# **glance** returns true if any service enabled start with **g-**
1814# **neutron** returns true if any service enabled start with **q-**
1815# **swift** returns true if any service enabled start with **s-**
1816# **trove** returns true if any service enabled start with **tr-**
1817# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1818# **s-** services will be enabled. This will be deprecated in the future.
1819#
1820# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1821# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1822# as enabled in this case.
1823#
1824# Uses global ``ENABLED_SERVICES``
1825# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001826function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001827 local xtrace=$(set +o | grep xtrace)
1828 set +o xtrace
1829 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001830 local services=$@
1831 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001832 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001833 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001834
1835 # Look for top-level 'enabled' function for this service
1836 if type is_${service}_enabled >/dev/null 2>&1; then
1837 # A function exists for this service, use it
1838 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001839 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001840 fi
1841
1842 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1843 # are implemented
1844
Sean Dague45917cc2014-02-24 16:09:14 -05001845 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001846 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001847 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001848 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1849 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1850 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1851 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1852 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1853 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1854 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001855 done
Sean Dague45917cc2014-02-24 16:09:14 -05001856 $xtrace
1857 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001858}
1859
fumihiko kakuma8606c982015-04-13 09:55:06 +09001860# remove specified list from the input string
1861# remove_disabled_services service-list remove-list
1862function remove_disabled_services {
1863 local service_list=$1
1864 local remove_list=$2
1865 local service
1866 local enabled=""
1867
1868 for service in ${service_list//,/ }; do
1869 local remove
1870 local add=1
1871 for remove in ${remove_list//,/ }; do
1872 if [[ ${remove} == ${service} ]]; then
1873 add=0
1874 break
1875 fi
1876 done
1877 if [[ $add == 1 ]]; then
1878 enabled="${enabled},$service"
1879 fi
1880 done
1881 _cleanup_service_list "$enabled"
1882}
1883
Dean Troyerdff49a22014-01-30 15:37:40 -06001884# Toggle enable/disable_service for services that must run exclusive of each other
1885# $1 The name of a variable containing a space-separated list of services
1886# $2 The name of a variable in which to store the enabled service's name
1887# $3 The name of the service to enable
1888function use_exclusive_service {
1889 local options=${!1}
1890 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001891 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001892 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001893 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001894 for opt in $options;do
1895 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1896 done
1897 eval "$out=$selection"
1898 return 0
1899}
1900
1901
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001902# System Functions
1903# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001904
1905# Only run the command if the target file (the last arg) is not on an
1906# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001907function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001908 local xtrace=$(set +o | grep xtrace)
1909 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001910 local args=( $@ )
1911 local last
1912 local sudo_cmd
1913 local dir_to_check
1914
1915 let last="${#args[*]} - 1"
1916
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001917 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001918 if [ ! -d "$dir_to_check" ]; then
1919 dir_to_check=`dirname "$dir_to_check"`
1920 fi
1921
1922 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001923 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001924 return 0
1925 fi
1926
1927 if [[ $TRACK_DEPENDS = True ]]; then
1928 sudo_cmd="env"
1929 else
1930 sudo_cmd="sudo"
1931 fi
1932
Sean Dague45917cc2014-02-24 16:09:14 -05001933 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001934 $sudo_cmd $@
1935}
1936
1937# Exit 0 if address is in network or 1 if address is not in network
1938# ip-range is in CIDR notation: 1.2.3.4/20
1939# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001940function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001941 local ip=$1
1942 local range=$2
1943 local masklen=${range#*/}
1944 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1945 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1946 [[ $network == $subnet ]]
1947}
1948
1949# Add a user to a group.
1950# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001951function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001952 local user=$1
1953 local group=$2
1954
Thomas Bechtolda8580852015-05-31 00:04:33 +02001955 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06001956}
1957
1958# Convert CIDR notation to a IPv4 netmask
1959# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001960function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001961 local maskpat="255 255 255 255"
1962 local maskdgt="254 252 248 240 224 192 128"
1963 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1964 echo ${1-0}.${2-0}.${3-0}.${4-0}
1965}
1966
1967# Gracefully cp only if source file/dir exists
1968# cp_it source destination
1969function cp_it {
1970 if [ -e $1 ] || [ -d $1 ]; then
1971 cp -pRL $1 $2
1972 fi
1973}
1974
1975# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1976# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1977# ``localrc`` or on the command line if necessary::
1978#
1979# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1980#
1981# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1982
Ian Wienandaee18c72014-02-21 15:35:08 +11001983function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001984 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001985 export http_proxy=$http_proxy
1986 fi
Sean Dague53753292014-12-04 19:38:15 -05001987 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001988 export https_proxy=$https_proxy
1989 fi
Sean Dague53753292014-12-04 19:38:15 -05001990 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001991 export no_proxy=$no_proxy
1992 fi
1993}
1994
1995# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001996function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001997 local mount_type=`stat -f -L -c %T $1`
1998 test "$mount_type" == "nfs"
1999}
2000
2001# Return the network portion of the given IP address using netmask
2002# netmask is in the traditional dotted-quad format
2003# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002004function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002005 local ip=$1
2006 local mask=$2
2007 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2008 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2009 echo $subnet
2010}
2011
Chris Dent3a2c86a2015-05-12 13:41:25 +00002012# Return the current python as "python<major>.<minor>"
2013function python_version {
2014 local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
2015 echo "python${python_version}"
2016}
2017
Dean Troyerdff49a22014-01-30 15:37:40 -06002018# Service wrapper to restart services
2019# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002020function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002021 if is_ubuntu; then
2022 sudo /usr/sbin/service $1 restart
2023 else
2024 sudo /sbin/service $1 restart
2025 fi
2026}
2027
2028# Only change permissions of a file or directory if it is not on an
2029# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002030function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002031 _safe_permission_operation chmod $@
2032}
2033
2034# Only change ownership of a file or directory if it is not on an NFS
2035# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002036function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002037 _safe_permission_operation chown $@
2038}
2039
2040# Service wrapper to start services
2041# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002042function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002043 if is_ubuntu; then
2044 sudo /usr/sbin/service $1 start
2045 else
2046 sudo /sbin/service $1 start
2047 fi
2048}
2049
2050# Service wrapper to stop services
2051# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002052function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002053 if is_ubuntu; then
2054 sudo /usr/sbin/service $1 stop
2055 else
2056 sudo /sbin/service $1 stop
2057 fi
2058}
2059
Sean Dague442e4e92015-06-24 13:24:02 -04002060# Test with a finite retry loop.
2061#
2062function test_with_retry {
2063 local testcmd=$1
2064 local failmsg=$2
2065 local until=${3:-10}
2066 local sleep=${4:-0.5}
2067
2068 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2069 die $LINENO "$failmsg"
2070 fi
2071}
2072
Dean Troyerdff49a22014-01-30 15:37:40 -06002073
2074# Restore xtrace
2075$XTRACE
2076
2077# Local variables:
2078# mode: shell-script
2079# End: