blob: f6a525354fcee2b200991d9c7d5f57ab99b993ba [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)}
594 local host_ips=$(LC_ALL=C ip -f $af addr show ${host_ip_iface} | 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
Steve Martinellib74e01c2014-12-18 01:35:35 -0500693 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
697 openstack --os-token=$OS_TOKEN --os-url=$os_url \
698 --os-identity-api-version=3 domain show $1 \
699 -f value -c id 2>/dev/null ||
700 # Creates new domain
701 openstack --os-token=$OS_TOKEN --os-url=$os_url \
702 --os-identity-api-version=3 domain create $1 \
703 --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:-}"
713 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
718 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" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000741 --os-url=$KEYSTONE_SERVICE_URI_V3 \
742 --os-identity-api-version=3 \
743 --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
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000757 openstack --os-url=$KEYSTONE_SERVICE_URI_V3 \
758 --os-identity-api-version=3 \
759 project create $1 \
760 --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 \
773 --os-url=$KEYSTONE_SERVICE_URI_V3 \
774 --os-identity-api-version=3 \
775 --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 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000787 --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 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000799 --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 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000812 --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 \
820 --os-url=$KEYSTONE_SERVICE_URI_V3 \
821 --os-identity-api-version=3 \
822 --group $2 \
823 --project $3
824 group_role_id=$(openstack role list \
825 --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 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000844 --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 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000864 --os-url $KEYSTONE_SERVICE_URI_V3 \
865 --os-identity-api-version=3 \
866 --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 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000873 --os-url $KEYSTONE_SERVICE_URI_V3 \
874 --os-identity-api-version=3 \
875 $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
1036 elif [[ $service == ceilometer-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001037 if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001038 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
1039 fi
1040 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001041 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001042 file_to_parse="${file_to_parse} ${package_dir}/swift"
1043 fi
1044 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001045 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001046 file_to_parse="${file_to_parse} ${package_dir}/nova"
1047 fi
1048 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001049 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001050 file_to_parse="${file_to_parse} ${package_dir}/glance"
1051 fi
1052 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001053 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001054 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1055 fi
1056 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001057 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001058 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1059 fi
1060 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001061 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001062 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1063 fi
1064 fi
1065 done
1066 echo "$(_parse_package_files $file_to_parse)"
1067 $xtrace
1068}
1069
1070# get_plugin_packages() collects a list of package names of any type from a
1071# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1072# list is intended to be passed to a package installer such as apt or yum.
1073#
1074# Only packages required for enabled and collected plugins will included.
1075#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001076# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001077# in these prerequisite files, see get_packages() for more info.
1078function get_plugin_packages {
1079 local xtrace=$(set +o | grep xtrace)
1080 set +o xtrace
1081 local files_to_parse=""
1082 local package_dir=""
1083 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1084 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1085 files_to_parse+="$package_dir/$plugin"
1086 done
1087 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001088 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001089}
1090
1091# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001092# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001093# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001094function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001095 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1096 REPOS_UPDATED=${REPOS_UPDATED:-False}
1097 RETRY_UPDATE=${RETRY_UPDATE:-False}
1098
Paul Linchpiner9e179742014-07-13 22:23:00 -07001099 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001100 return 0
1101 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001102
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001103 if is_ubuntu; then
1104 local xtrace=$(set +o | grep xtrace)
1105 set +o xtrace
1106 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1107 # if there are transient errors pulling the updates, that's fine.
1108 # It may be secondary repositories that we don't really care about.
1109 apt_get update || /bin/true
1110 REPOS_UPDATED=True
1111 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001112 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001113 fi
1114}
1115
1116function real_install_package {
1117 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001118 apt_get install "$@"
1119 elif is_fedora; then
1120 yum_install "$@"
1121 elif is_suse; then
1122 zypper_install "$@"
1123 else
1124 exit_distro_not_supported "installing packages"
1125 fi
1126}
1127
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001128# Distro-agnostic package installer
1129# install_package package [package ...]
1130function install_package {
1131 update_package_repo
1132 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1133}
1134
Dean Troyerdff49a22014-01-30 15:37:40 -06001135# Distro-agnostic function to tell if a package is installed
1136# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001137function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001138 if [[ -z "$@" ]]; then
1139 return 1
1140 fi
1141
1142 if [[ -z "$os_PACKAGE" ]]; then
1143 GetOSVersion
1144 fi
1145
1146 if [[ "$os_PACKAGE" = "deb" ]]; then
1147 dpkg -s "$@" > /dev/null 2> /dev/null
1148 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1149 rpm --quiet -q "$@"
1150 else
1151 exit_distro_not_supported "finding if a package is installed"
1152 fi
1153}
1154
1155# Distro-agnostic package uninstaller
1156# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001157function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001158 if is_ubuntu; then
1159 apt_get purge "$@"
1160 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001161 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001162 elif is_suse; then
1163 sudo zypper rm "$@"
1164 else
1165 exit_distro_not_supported "uninstalling packages"
1166 fi
1167}
1168
1169# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001170# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001171# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001172function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001173 [[ "$OFFLINE" = "True" ]] && return
1174 local sudo="sudo"
1175 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001176
1177 # The manual check for missing packages is because yum -y assumes
1178 # missing packages are OK. See
1179 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001180 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1181 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001182 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001183 awk '
1184 BEGIN { fail=0 }
1185 /No package/ { fail=1 }
1186 { print }
1187 END { exit fail }' || \
1188 die $LINENO "Missing packages detected"
1189
1190 # also ensure we catch a yum failure
1191 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001192 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001193 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001194}
1195
1196# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001197# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001198# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001199function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001200 [[ "$OFFLINE" = "True" ]] && return
1201 local sudo="sudo"
1202 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001203 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1204 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001205 zypper --non-interactive install --auto-agree-with-licenses "$@"
1206}
1207
1208
1209# Process Functions
1210# =================
1211
1212# _run_process() is designed to be backgrounded by run_process() to simulate a
1213# fork. It includes the dirty work of closing extra filehandles and preparing log
1214# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001215# from the service name.
1216# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001217# If an optional group is provided sg will be used to set the group of
1218# the command.
1219# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001220function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001221 # disable tracing through the exec redirects, it's just confusing in the logs.
1222 xtrace=$(set +o | grep xtrace)
1223 set +o xtrace
1224
Dean Troyerdff49a22014-01-30 15:37:40 -06001225 local service=$1
1226 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001227 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001228
1229 # Undo logging redirections and close the extra descriptors
1230 exec 1>&3
1231 exec 2>&3
1232 exec 3>&-
1233 exec 6>&-
1234
Dean Troyerdde41d02014-12-09 17:47:57 -06001235 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1236 if [[ -n ${LOGDIR} ]]; then
1237 exec 1>&"$real_logfile" 2>&1
1238 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1239 if [[ -n ${SCREEN_LOGDIR} ]]; then
1240 # Drop the backward-compat symlink
1241 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1242 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001243
1244 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1245 export PYTHONUNBUFFERED=1
1246 fi
1247
Sean Dague6e137ab2015-04-29 08:22:24 -04001248 # reenable xtrace before we do *real* work
1249 $xtrace
1250
Dean Troyer3159a822014-08-27 14:13:58 -05001251 # Run under ``setsid`` to force the process to become a session and group leader.
1252 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001253 if [[ -n "$group" ]]; then
1254 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1255 else
1256 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1257 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001258
1259 # Just silently exit this process
1260 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001261}
1262
1263# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1264# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001265# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001266# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001267function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001268 SCREEN_NAME=${SCREEN_NAME:-stack}
1269 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1270
1271 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1272 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1273 fi
1274
1275 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1276}
1277
1278# Find out if a process exists by partial name.
1279# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001280function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001281 local name=$1
1282 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001283 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001284 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001285 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001286}
1287
Dean Troyer3159a822014-08-27 14:13:58 -05001288# Run a single service under screen or directly
1289# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001290# If an optional group is provided sg will be used to run the
1291# command as that group.
1292# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001293function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001294 local service=$1
1295 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001296 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001297
Dean Troyer3159a822014-08-27 14:13:58 -05001298 if is_service_enabled $service; then
1299 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001300 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001301 else
1302 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001303 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001304 fi
1305 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001306}
1307
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001308# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001309# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001310# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001311# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001312# Run a command in a shell in a screen window, if an optional group
1313# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001314function screen_process {
1315 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001316 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001317 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001318
Sean Dagueea22a4f2014-06-27 15:21:41 -04001319 SCREEN_NAME=${SCREEN_NAME:-stack}
1320 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001321 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001322
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001323 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001324
Dean Troyerdde41d02014-12-09 17:47:57 -06001325 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1326 echo "LOGDIR: $LOGDIR"
1327 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1328 echo "log: $real_logfile"
1329 if [[ -n ${LOGDIR} ]]; then
1330 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001331 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001332 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1333 if [[ -n ${SCREEN_LOGDIR} ]]; then
1334 # Drop the backward-compat symlink
1335 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1336 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001337 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001338
1339 # sleep to allow bash to be ready to be send the command - we are
1340 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001341 # bash isn't running by the time we send the command, nothing
1342 # happens. This sleep was added originally to handle gate runs
1343 # where we needed this to be at least 3 seconds to pass
1344 # consistently on slow clouds. Now this is configurable so that we
1345 # can determine a reasonable value for the local case which should
1346 # be much smaller.
1347 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001348
1349 NL=`echo -ne '\015'`
1350 # This fun command does the following:
1351 # - the passed server command is backgrounded
1352 # - the pid of the background process is saved in the usual place
1353 # - the server process is brought back to the foreground
1354 # - if the server process exits prematurely the fg command errors
1355 # and a message is written to stdout and the process failure file
1356 #
1357 # The pid saved can be used in stop_process() as a process group
1358 # id to kill off all child processes
1359 if [[ -n "$group" ]]; then
1360 command="sg $group '$command'"
1361 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001362
1363 # Append the process to the screen rc file
1364 screen_rc "$name" "$command"
1365
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001366 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 -06001367}
1368
1369# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001370# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001371# screen_rc service "command-line"
1372function screen_rc {
1373 SCREEN_NAME=${SCREEN_NAME:-stack}
1374 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1375 if [[ ! -e $SCREENRC ]]; then
1376 # Name the screen session
1377 echo "sessionname $SCREEN_NAME" > $SCREENRC
1378 # Set a reasonable statusbar
1379 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1380 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1381 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1382 echo "screen -t shell bash" >> $SCREENRC
1383 fi
1384 # If this service doesn't already exist in the screenrc file
1385 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1386 NL=`echo -ne '\015'`
1387 echo "screen -t $1 bash" >> $SCREENRC
1388 echo "stuff \"$2$NL\"" >> $SCREENRC
1389
Dean Troyerdde41d02014-12-09 17:47:57 -06001390 if [[ -n ${LOGDIR} ]]; then
1391 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001392 echo "log on" >>$SCREENRC
1393 fi
1394 fi
1395}
1396
1397# Stop a service in screen
1398# If a PID is available use it, kill the whole process group via TERM
1399# If screen is being used kill the screen window; this will catch processes
1400# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001401# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001402# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001403function screen_stop_service {
1404 local service=$1
1405
Dean Troyerdff49a22014-01-30 15:37:40 -06001406 SCREEN_NAME=${SCREEN_NAME:-stack}
1407 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001408 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001409
Dean Troyer3159a822014-08-27 14:13:58 -05001410 if is_service_enabled $service; then
1411 # Clean up the screen window
Attila Fazekasf750a6f2015-07-01 12:17:35 +02001412 screen -S $SCREEN_NAME -p $service -X kill || true
Dean Troyer3159a822014-08-27 14:13:58 -05001413 fi
1414}
1415
1416# Stop a service process
1417# If a PID is available use it, kill the whole process group via TERM
1418# If screen is being used kill the screen window; this will catch processes
1419# that did not leave a PID behind
1420# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1421# stop_process service
1422function stop_process {
1423 local service=$1
1424
1425 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001426 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001427
1428 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001429 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001430 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1431 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1432 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001433 fi
1434 if [[ "$USE_SCREEN" = "True" ]]; then
1435 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001436 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001437 fi
1438 fi
1439}
1440
1441# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001442# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001443# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001444function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001445 local service
1446 local failures
1447 SCREEN_NAME=${SCREEN_NAME:-stack}
1448 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1449
1450
1451 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1452 echo "No service status directory found"
1453 return
1454 fi
1455
1456 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001457 # make this -o errexit safe
1458 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001459
1460 for service in $failures; do
1461 service=`basename $service`
1462 service=${service%.failure}
1463 echo "Error: Service $service is not running"
1464 done
1465
1466 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001467 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001468 fi
1469}
1470
Chris Dent2f27a0e2014-09-09 13:46:02 +01001471# Tail a log file in a screen if USE_SCREEN is true.
1472function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001473 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001474 local logfile=$2
1475
Sean Dague53753292014-12-04 19:38:15 -05001476 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001477 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001478 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001479 fi
1480}
1481
Dean Troyerdff49a22014-01-30 15:37:40 -06001482
Dean Troyer3159a822014-08-27 14:13:58 -05001483# Deprecated Functions
1484# --------------------
1485
1486# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1487# fork. It includes the dirty work of closing extra filehandles and preparing log
1488# files to produce the same logs as screen_it(). The log filename is derived
1489# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1490# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1491# _old_run_process service "command-line"
1492function _old_run_process {
1493 local service=$1
1494 local command="$2"
1495
1496 # Undo logging redirections and close the extra descriptors
1497 exec 1>&3
1498 exec 2>&3
1499 exec 3>&-
1500 exec 6>&-
1501
1502 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001503 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1504 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001505
1506 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1507 export PYTHONUNBUFFERED=1
1508 fi
1509
1510 exec /bin/bash -c "$command"
1511 die "$service exec failure: $command"
1512}
1513
1514# old_run_process() launches a child process that closes all file descriptors and
1515# then exec's the passed in command. This is meant to duplicate the semantics
1516# of screen_it() without screen. PIDs are written to
1517# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1518# old_run_process service "command-line"
1519function old_run_process {
1520 local service=$1
1521 local command="$2"
1522
1523 # Spawn the child process
1524 _old_run_process "$service" "$command" &
1525 echo $!
1526}
1527
1528# Compatibility for existing start_XXXX() functions
1529# Uses global ``USE_SCREEN``
1530# screen_it service "command-line"
1531function screen_it {
1532 if is_service_enabled $1; then
1533 # Append the service to the screen rc file
1534 screen_rc "$1" "$2"
1535
1536 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001537 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001538 else
1539 # Spawn directly without screen
1540 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1541 fi
1542 fi
1543}
1544
1545# Compatibility for existing stop_XXXX() functions
1546# Stop a service in screen
1547# If a PID is available use it, kill the whole process group via TERM
1548# If screen is being used kill the screen window; this will catch processes
1549# that did not leave a PID behind
1550# screen_stop service
1551function screen_stop {
1552 # Clean up the screen window
1553 stop_process $1
1554}
1555
1556
Sean Dague2c65e712014-12-18 09:44:56 -05001557# Plugin Functions
1558# =================
1559
1560DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1561
1562# enable_plugin <name> <url> [branch]
1563#
1564# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1565# ``url`` is a git url
1566# ``branch`` is a gitref. If it's not set, defaults to master
1567function enable_plugin {
1568 local name=$1
1569 local url=$2
1570 local branch=${3:-master}
1571 DEVSTACK_PLUGINS+=",$name"
1572 GITREPO[$name]=$url
1573 GITDIR[$name]=$DEST/$name
1574 GITBRANCH[$name]=$branch
1575}
1576
1577# fetch_plugins
1578#
1579# clones all plugins
1580function fetch_plugins {
1581 local plugins="${DEVSTACK_PLUGINS}"
1582 local plugin
1583
1584 # short circuit if nothing to do
1585 if [[ -z $plugins ]]; then
1586 return
1587 fi
1588
Dean Troyerdc97cb72015-03-28 08:20:50 -05001589 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001590 for plugin in ${plugins//,/ }; do
1591 git_clone_by_name $plugin
1592 done
1593}
1594
1595# load_plugin_settings
1596#
1597# Load settings from plugins in the order that they were registered
1598function load_plugin_settings {
1599 local plugins="${DEVSTACK_PLUGINS}"
1600 local plugin
1601
1602 # short circuit if nothing to do
1603 if [[ -z $plugins ]]; then
1604 return
1605 fi
1606
1607 echo "Loading plugin settings"
1608 for plugin in ${plugins//,/ }; do
1609 local dir=${GITDIR[$plugin]}
1610 # source any known settings
1611 if [[ -f $dir/devstack/settings ]]; then
1612 source $dir/devstack/settings
1613 fi
1614 done
1615}
1616
Sean Dague6e275e12015-03-26 05:54:28 -04001617# plugin_override_defaults
1618#
1619# Run an extremely early setting phase for plugins that allows default
1620# overriding of services.
1621function plugin_override_defaults {
1622 local plugins="${DEVSTACK_PLUGINS}"
1623 local plugin
1624
1625 # short circuit if nothing to do
1626 if [[ -z $plugins ]]; then
1627 return
1628 fi
1629
1630 echo "Overriding Configuration Defaults"
1631 for plugin in ${plugins//,/ }; do
1632 local dir=${GITDIR[$plugin]}
1633 # source any overrides
1634 if [[ -f $dir/devstack/override-defaults ]]; then
1635 # be really verbose that an override is happening, as it
1636 # may not be obvious if things fail later.
1637 echo "$plugin has overriden the following defaults"
1638 cat $dir/devstack/override-defaults
1639 source $dir/devstack/override-defaults
1640 fi
1641 done
1642}
1643
Sean Dague2c65e712014-12-18 09:44:56 -05001644# run_plugins
1645#
1646# Run the devstack/plugin.sh in all the plugin directories. These are
1647# run in registration order.
1648function run_plugins {
1649 local mode=$1
1650 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301651
1652 local plugins="${DEVSTACK_PLUGINS}"
1653 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001654 for plugin in ${plugins//,/ }; do
1655 local dir=${GITDIR[$plugin]}
1656 if [[ -f $dir/devstack/plugin.sh ]]; then
1657 source $dir/devstack/plugin.sh $mode $phase
1658 fi
1659 done
1660}
1661
1662function run_phase {
1663 local mode=$1
1664 local phase=$2
1665 if [[ -d $TOP_DIR/extras.d ]]; then
1666 for i in $TOP_DIR/extras.d/*.sh; do
1667 [[ -r $i ]] && source $i $mode $phase
1668 done
1669 fi
1670 # the source phase corresponds to settings loading in plugins
1671 if [[ "$mode" == "source" ]]; then
1672 load_plugin_settings
Sean Dague6e275e12015-03-26 05:54:28 -04001673 elif [[ "$mode" == "override_defaults" ]]; then
1674 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001675 else
1676 run_plugins $mode $phase
1677 fi
1678}
1679
Dean Troyerdff49a22014-01-30 15:37:40 -06001680
1681# Service Functions
1682# =================
1683
1684# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1685# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001686function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001687 echo "$1" | sed -e '
1688 s/,,/,/g;
1689 s/^,//;
1690 s/,$//
1691 '
1692}
1693
1694# disable_all_services() removes all current services
1695# from ``ENABLED_SERVICES`` to reset the configuration
1696# before a minimal installation
1697# Uses global ``ENABLED_SERVICES``
1698# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001699function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001700 ENABLED_SERVICES=""
1701}
1702
1703# Remove all services starting with '-'. For example, to install all default
1704# services except rabbit (rabbit) set in ``localrc``:
1705# ENABLED_SERVICES+=",-rabbit"
1706# Uses global ``ENABLED_SERVICES``
1707# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001708function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001709 local to_remove=""
1710 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001711 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001712
1713 # build up list of services that should be removed; i.e. they
1714 # begin with "-"
1715 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001716 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001717 to_remove+=",${service#-}"
1718 else
1719 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001720 fi
1721 done
Ian Wienand2796a822015-04-15 08:59:04 +10001722
1723 # go through the service list. if this service appears in the "to
1724 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001725 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Dean Troyerdff49a22014-01-30 15:37:40 -06001726}
1727
1728# disable_service() removes the services passed as argument to the
1729# ``ENABLED_SERVICES`` list, if they are present.
1730#
1731# For example:
1732# disable_service rabbit
1733#
1734# This function does not know about the special cases
1735# for nova, glance, and neutron built into is_service_enabled().
1736# Uses global ``ENABLED_SERVICES``
1737# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001738function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001739 local tmpsvcs=",${ENABLED_SERVICES},"
1740 local service
1741 for service in $@; do
1742 if is_service_enabled $service; then
1743 tmpsvcs=${tmpsvcs//,$service,/,}
1744 fi
1745 done
1746 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1747}
1748
1749# enable_service() adds the services passed as argument to the
1750# ``ENABLED_SERVICES`` list, if they are not already present.
1751#
1752# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001753# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001754#
1755# This function does not know about the special cases
1756# for nova, glance, and neutron built into is_service_enabled().
1757# Uses global ``ENABLED_SERVICES``
1758# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001759function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001760 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001761 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001762 for service in $@; do
1763 if ! is_service_enabled $service; then
1764 tmpsvcs+=",$service"
1765 fi
1766 done
1767 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1768 disable_negated_services
1769}
1770
1771# is_service_enabled() checks if the service(s) specified as arguments are
1772# enabled by the user in ``ENABLED_SERVICES``.
1773#
1774# Multiple services specified as arguments are ``OR``'ed together; the test
1775# is a short-circuit boolean, i.e it returns on the first match.
1776#
1777# There are special cases for some 'catch-all' services::
1778# **nova** returns true if any service enabled start with **n-**
1779# **cinder** returns true if any service enabled start with **c-**
1780# **ceilometer** returns true if any service enabled start with **ceilometer**
1781# **glance** returns true if any service enabled start with **g-**
1782# **neutron** returns true if any service enabled start with **q-**
1783# **swift** returns true if any service enabled start with **s-**
1784# **trove** returns true if any service enabled start with **tr-**
1785# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1786# **s-** services will be enabled. This will be deprecated in the future.
1787#
1788# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1789# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1790# as enabled in this case.
1791#
1792# Uses global ``ENABLED_SERVICES``
1793# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001794function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001795 local xtrace=$(set +o | grep xtrace)
1796 set +o xtrace
1797 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001798 local services=$@
1799 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001800 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001801 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001802
1803 # Look for top-level 'enabled' function for this service
1804 if type is_${service}_enabled >/dev/null 2>&1; then
1805 # A function exists for this service, use it
1806 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001807 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001808 fi
1809
1810 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1811 # are implemented
1812
Sean Dague45917cc2014-02-24 16:09:14 -05001813 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001814 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001815 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001816 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1817 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1818 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1819 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1820 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1821 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1822 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001823 done
Sean Dague45917cc2014-02-24 16:09:14 -05001824 $xtrace
1825 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001826}
1827
fumihiko kakuma8606c982015-04-13 09:55:06 +09001828# remove specified list from the input string
1829# remove_disabled_services service-list remove-list
1830function remove_disabled_services {
1831 local service_list=$1
1832 local remove_list=$2
1833 local service
1834 local enabled=""
1835
1836 for service in ${service_list//,/ }; do
1837 local remove
1838 local add=1
1839 for remove in ${remove_list//,/ }; do
1840 if [[ ${remove} == ${service} ]]; then
1841 add=0
1842 break
1843 fi
1844 done
1845 if [[ $add == 1 ]]; then
1846 enabled="${enabled},$service"
1847 fi
1848 done
1849 _cleanup_service_list "$enabled"
1850}
1851
Dean Troyerdff49a22014-01-30 15:37:40 -06001852# Toggle enable/disable_service for services that must run exclusive of each other
1853# $1 The name of a variable containing a space-separated list of services
1854# $2 The name of a variable in which to store the enabled service's name
1855# $3 The name of the service to enable
1856function use_exclusive_service {
1857 local options=${!1}
1858 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001859 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001860 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001861 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001862 for opt in $options;do
1863 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1864 done
1865 eval "$out=$selection"
1866 return 0
1867}
1868
1869
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001870# System Functions
1871# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001872
1873# Only run the command if the target file (the last arg) is not on an
1874# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001875function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001876 local xtrace=$(set +o | grep xtrace)
1877 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001878 local args=( $@ )
1879 local last
1880 local sudo_cmd
1881 local dir_to_check
1882
1883 let last="${#args[*]} - 1"
1884
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001885 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001886 if [ ! -d "$dir_to_check" ]; then
1887 dir_to_check=`dirname "$dir_to_check"`
1888 fi
1889
1890 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001891 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001892 return 0
1893 fi
1894
1895 if [[ $TRACK_DEPENDS = True ]]; then
1896 sudo_cmd="env"
1897 else
1898 sudo_cmd="sudo"
1899 fi
1900
Sean Dague45917cc2014-02-24 16:09:14 -05001901 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001902 $sudo_cmd $@
1903}
1904
1905# Exit 0 if address is in network or 1 if address is not in network
1906# ip-range is in CIDR notation: 1.2.3.4/20
1907# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001908function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001909 local ip=$1
1910 local range=$2
1911 local masklen=${range#*/}
1912 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1913 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1914 [[ $network == $subnet ]]
1915}
1916
1917# Add a user to a group.
1918# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001919function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001920 local user=$1
1921 local group=$2
1922
Thomas Bechtolda8580852015-05-31 00:04:33 +02001923 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06001924}
1925
1926# Convert CIDR notation to a IPv4 netmask
1927# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001928function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001929 local maskpat="255 255 255 255"
1930 local maskdgt="254 252 248 240 224 192 128"
1931 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1932 echo ${1-0}.${2-0}.${3-0}.${4-0}
1933}
1934
1935# Gracefully cp only if source file/dir exists
1936# cp_it source destination
1937function cp_it {
1938 if [ -e $1 ] || [ -d $1 ]; then
1939 cp -pRL $1 $2
1940 fi
1941}
1942
1943# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1944# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1945# ``localrc`` or on the command line if necessary::
1946#
1947# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1948#
1949# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1950
Ian Wienandaee18c72014-02-21 15:35:08 +11001951function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001952 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001953 export http_proxy=$http_proxy
1954 fi
Sean Dague53753292014-12-04 19:38:15 -05001955 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001956 export https_proxy=$https_proxy
1957 fi
Sean Dague53753292014-12-04 19:38:15 -05001958 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001959 export no_proxy=$no_proxy
1960 fi
1961}
1962
1963# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001964function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001965 local mount_type=`stat -f -L -c %T $1`
1966 test "$mount_type" == "nfs"
1967}
1968
1969# Return the network portion of the given IP address using netmask
1970# netmask is in the traditional dotted-quad format
1971# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001972function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001973 local ip=$1
1974 local mask=$2
1975 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1976 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1977 echo $subnet
1978}
1979
Chris Dent3a2c86a2015-05-12 13:41:25 +00001980# Return the current python as "python<major>.<minor>"
1981function python_version {
1982 local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
1983 echo "python${python_version}"
1984}
1985
Dean Troyerdff49a22014-01-30 15:37:40 -06001986# Service wrapper to restart services
1987# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001988function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001989 if is_ubuntu; then
1990 sudo /usr/sbin/service $1 restart
1991 else
1992 sudo /sbin/service $1 restart
1993 fi
1994}
1995
1996# Only change permissions of a file or directory if it is not on an
1997# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001998function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001999 _safe_permission_operation chmod $@
2000}
2001
2002# Only change ownership of a file or directory if it is not on an NFS
2003# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002004function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002005 _safe_permission_operation chown $@
2006}
2007
2008# Service wrapper to start services
2009# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002010function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002011 if is_ubuntu; then
2012 sudo /usr/sbin/service $1 start
2013 else
2014 sudo /sbin/service $1 start
2015 fi
2016}
2017
2018# Service wrapper to stop services
2019# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002020function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002021 if is_ubuntu; then
2022 sudo /usr/sbin/service $1 stop
2023 else
2024 sudo /sbin/service $1 stop
2025 fi
2026}
2027
Sean Dague442e4e92015-06-24 13:24:02 -04002028# Test with a finite retry loop.
2029#
2030function test_with_retry {
2031 local testcmd=$1
2032 local failmsg=$2
2033 local until=${3:-10}
2034 local sleep=${4:-0.5}
2035
2036 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2037 die $LINENO "$failmsg"
2038 fi
2039}
2040
Dean Troyerdff49a22014-01-30 15:37:40 -06002041
2042# Restore xtrace
2043$XTRACE
2044
2045# Local variables:
2046# mode: shell-script
2047# End: