blob: 641eca836281706ec667b038754bc31b68f03105 [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``
Masayuki Igawad20f6322014-02-28 09:22:37 +090031# - ``UNDO_REQUIREMENTS``
Dean Troyerdff49a22014-01-30 15:37:40 -060032# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050033#
Dean Troyerdff49a22014-01-30 15:37:40 -060034
35# Save trace setting
36XTRACE=$(set +o | grep xtrace)
37set +o xtrace
38
Sean Daguecc524062014-10-01 09:06:43 -040039# Global Config Variables
40declare -A GITREPO
41declare -A GITBRANCH
42declare -A GITDIR
43
Sean Dague53753292014-12-04 19:38:15 -050044TRACK_DEPENDS=${TRACK_DEPENDS:-False}
45
Dean Troyer68162342015-05-13 15:41:03 -050046# Save these variables to .stackenv
47STACK_ENV_VARS="BASE_SQL_CONN DATA_DIR DEST ENABLED_SERVICES HOST_IP \
48 KEYSTONE_AUTH_PROTOCOL KEYSTONE_AUTH_URI KEYSTONE_SERVICE_URI \
Brian Haley180f5eb2015-06-16 13:14:31 -040049 LOGFILE OS_CACERT SERVICE_HOST SERVICE_PROTOCOL STACK_USER TLS_IP \
Brian Haley5d04db22015-06-16 13:14:31 -040050 HOST_IPV6 SERVICE_IP_VERSION"
Dean Troyer68162342015-05-13 15:41:03 -050051
52
53# Saves significant environment variables to .stackenv for later use
54# Refers to a lot of globals, only TOP_DIR and STACK_ENV_VARS are required to
55# function, the rest are simply saved and do not cause problems if they are undefined.
56# save_stackenv [tag]
57function save_stackenv {
58 local tag=${1:-""}
59 # Save some values we generated for later use
60 time_stamp=$(date "+$TIMESTAMP_FORMAT")
61 echo "# $time_stamp $tag" >$TOP_DIR/.stackenv
62 for i in $STACK_ENV_VARS; do
63 echo $i=${!i} >>$TOP_DIR/.stackenv
64 done
65}
Dean Troyerdff49a22014-01-30 15:37:40 -060066
67# Normalize config values to True or False
68# Accepts as False: 0 no No NO false False FALSE
69# Accepts as True: 1 yes Yes YES true True TRUE
70# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +110071function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -050072 local xtrace=$(set +o | grep xtrace)
73 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060074
Mahito OGURA98f59aa2015-05-11 18:02:34 +090075 local default=$1
76 local testval=${!2:-}
77
78 case "$testval" in
79 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
80 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
81 * ) echo "$default" ;;
82 esac
83
Sean Dague45917cc2014-02-24 16:09:14 -050084 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060085}
86
Attila Fazekas1bd79592015-02-24 14:06:56 +010087function isset {
88 [[ -v "$1" ]]
89}
Dean Troyerdff49a22014-01-30 15:37:40 -060090
Dean Troyer68162342015-05-13 15:41:03 -050091
Dean Troyerdff49a22014-01-30 15:37:40 -060092# Control Functions
93# =================
94
95# Prints backtrace info
96# filename:lineno:function
97# backtrace level
98function backtrace {
99 local level=$1
100 local deep=$((${#BASH_SOURCE[@]} - 1))
101 echo "[Call Trace]"
102 while [ $level -le $deep ]; do
103 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
104 deep=$((deep - 1))
105 done
106}
107
108# Prints line number and "message" then exits
109# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100110function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600111 local exitcode=$?
112 set +o xtrace
113 local line=$1; shift
114 if [ $exitcode == 0 ]; then
115 exitcode=1
116 fi
117 backtrace 2
118 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600119 # Give buffers a second to flush
120 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600121 exit $exitcode
122}
123
124# Checks an environment variable is not set or has length 0 OR if the
125# exit code is non-zero and prints "message" and exits
126# NOTE: env-var is the variable name without a '$'
127# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100128function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600129 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500130 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600131 set +o xtrace
132 local line=$1; shift
133 local evar=$1; shift
134 if ! is_set $evar || [ $exitcode != 0 ]; then
135 die $line "$*"
136 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500137 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600138}
139
140# Prints line number and "message" in error format
141# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100142function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600143 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500144 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600145 set +o xtrace
146 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
147 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600148 if [[ -n ${LOGDIR} ]]; then
149 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600150 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500151 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600152 return $exitcode
153}
154
155# Checks an environment variable is not set or has length 0 OR if the
156# exit code is non-zero and prints "message"
157# NOTE: env-var is the variable name without a '$'
158# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100159function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600160 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500161 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600162 set +o xtrace
163 local line=$1; shift
164 local evar=$1; shift
165 if ! is_set $evar || [ $exitcode != 0 ]; then
166 err $line "$*"
167 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500168 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600169 return $exitcode
170}
171
172# Exit after outputting a message about the distribution not being supported.
173# exit_distro_not_supported [optional-string-telling-what-is-missing]
174function exit_distro_not_supported {
175 if [[ -z "$DISTRO" ]]; then
176 GetDistro
177 fi
178
179 if [ $# -gt 0 ]; then
180 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
181 else
182 die $LINENO "Support for $DISTRO is incomplete."
183 fi
184}
185
186# Test if the named environment variable is set and not zero length
187# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100188function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600189 local var=\$"$1"
190 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
191}
192
193# Prints line number and "message" in warning format
194# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100195function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600196 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500197 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600198 set +o xtrace
199 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Sean Daguee4af9292015-04-28 08:57:57 -0400200 echo $msg
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500201 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600202 return $exitcode
203}
204
205
206# Distro Functions
207# ================
208
209# Determine OS Vendor, Release and Update
210# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
211# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500212# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
213# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
214# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
215# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
216# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500217os_VENDOR=""
218os_RELEASE=""
219os_UPDATE=""
220os_PACKAGE=""
221os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500222
Dean Troyerdff49a22014-01-30 15:37:40 -0600223# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100224function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500225
Dean Troyerdff49a22014-01-30 15:37:40 -0600226 # Figure out which vendor we are
227 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
228 # OS/X
229 os_VENDOR=`sw_vers -productName`
230 os_RELEASE=`sw_vers -productVersion`
231 os_UPDATE=${os_RELEASE##*.}
232 os_RELEASE=${os_RELEASE%.*}
233 os_PACKAGE=""
234 if [[ "$os_RELEASE" =~ "10.7" ]]; then
235 os_CODENAME="lion"
236 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
237 os_CODENAME="snow leopard"
238 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
239 os_CODENAME="leopard"
240 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
241 os_CODENAME="tiger"
242 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
243 os_CODENAME="panther"
244 else
245 os_CODENAME=""
246 fi
247 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
248 os_VENDOR=$(lsb_release -i -s)
249 os_RELEASE=$(lsb_release -r -s)
250 os_UPDATE=""
251 os_PACKAGE="rpm"
252 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
253 os_PACKAGE="deb"
254 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
255 lsb_release -d -s | grep -q openSUSE
256 if [[ $? -eq 0 ]]; then
257 os_VENDOR="openSUSE"
258 fi
259 elif [[ $os_VENDOR == "openSUSE project" ]]; then
260 os_VENDOR="openSUSE"
261 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
262 os_VENDOR="Red Hat"
263 fi
264 os_CODENAME=$(lsb_release -c -s)
265 elif [[ -r /etc/redhat-release ]]; then
266 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
267 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
268 # CentOS release 5.5 (Final)
269 # CentOS Linux release 6.0 (Final)
270 # Fedora release 16 (Verne)
271 # XenServer release 6.2.0-70446c (xenenterprise)
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700272 # Oracle Linux release 7
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300273 # CloudLinux release 7.1
Dean Troyerdff49a22014-01-30 15:37:40 -0600274 os_CODENAME=""
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300275 for r in "Red Hat" CentOS Fedora XenServer CloudLinux; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600276 os_VENDOR=$r
277 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
278 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
279 os_CODENAME=${ver#*|}
280 os_RELEASE=${ver%|*}
281 os_UPDATE=${os_RELEASE##*.}
282 os_RELEASE=${os_RELEASE%.*}
283 break
284 fi
285 os_VENDOR=""
286 done
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700287 if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
288 os_VENDOR=OracleLinux
289 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600290 os_PACKAGE="rpm"
291 elif [[ -r /etc/SuSE-release ]]; then
292 for r in openSUSE "SUSE Linux"; do
293 if [[ "$r" = "SUSE Linux" ]]; then
294 os_VENDOR="SUSE LINUX"
295 else
296 os_VENDOR=$r
297 fi
298
299 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
300 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
301 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
302 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
303 break
304 fi
305 os_VENDOR=""
306 done
307 os_PACKAGE="rpm"
308 # If lsb_release is not installed, we should be able to detect Debian OS
309 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
310 os_VENDOR="Debian"
311 os_PACKAGE="deb"
312 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
313 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
314 fi
315 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
316}
317
318# Translate the OS version values into common nomenclature
319# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500320declare DISTRO
321
Ian Wienandaee18c72014-02-21 15:35:08 +1100322function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600323 GetOSVersion
324 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
325 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
326 DISTRO=$os_CODENAME
327 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
328 # For Fedora, just use 'f' and the release
329 DISTRO="f$os_RELEASE"
330 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
331 DISTRO="opensuse-$os_RELEASE"
332 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
333 # For SLE, also use the service pack
334 if [[ -z "$os_UPDATE" ]]; then
335 DISTRO="sle${os_RELEASE}"
336 else
337 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
338 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530339 elif [[ "$os_VENDOR" =~ (Red Hat) || \
340 "$os_VENDOR" =~ (CentOS) || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700341 "$os_VENDOR" =~ (OracleLinux) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600342 # Drop the . release as we assume it's compatible
343 DISTRO="rhel${os_RELEASE::1}"
344 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
345 DISTRO="xs$os_RELEASE"
346 else
347 # Catch-all for now is Vendor + Release + Update
348 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
349 fi
350 export DISTRO
351}
352
353# Utility function for checking machine architecture
354# is_arch arch-type
355function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500356 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600357}
358
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700359# Determine if current distribution is an Oracle distribution
360# is_oraclelinux
361function is_oraclelinux {
362 if [[ -z "$os_VENDOR" ]]; then
363 GetOSVersion
364 fi
365
366 [ "$os_VENDOR" = "OracleLinux" ]
367}
368
369
Dean Troyerdff49a22014-01-30 15:37:40 -0600370# Determine if current distribution is a Fedora-based distribution
371# (Fedora, RHEL, CentOS, etc).
372# is_fedora
373function is_fedora {
374 if [[ -z "$os_VENDOR" ]]; then
375 GetOSVersion
376 fi
377
anju Tiwari6c639c92014-07-15 18:11:54 +0530378 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300379 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ] || \
380 [ "$os_VENDOR" = "CloudLinux" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600381}
382
383
384# Determine if current distribution is a SUSE-based distribution
385# (openSUSE, SLE).
386# is_suse
387function is_suse {
388 if [[ -z "$os_VENDOR" ]]; then
389 GetOSVersion
390 fi
391
392 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
393}
394
395
396# Determine if current distribution is an Ubuntu-based distribution
397# It will also detect non-Ubuntu but Debian-based distros
398# is_ubuntu
399function is_ubuntu {
400 if [[ -z "$os_PACKAGE" ]]; then
401 GetOSVersion
402 fi
403 [ "$os_PACKAGE" = "deb" ]
404}
405
406
407# Git Functions
408# =============
409
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600410# Returns openstack release name for a given branch name
411# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100412function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600413 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700414 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600415 echo ${branch#*/}
416 else
417 echo "master"
418 fi
419}
420
Dean Troyerdff49a22014-01-30 15:37:40 -0600421# git clone only if directory doesn't exist already. Since ``DEST`` might not
422# be owned by the installation user, we create the directory and change the
423# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500424# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
425# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600426# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500427# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600428# git_clone remote dest-dir branch
429function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500430 local git_remote=$1
431 local git_dest=$2
432 local git_ref=$3
433 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200434 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500435
Sean Dague53753292014-12-04 19:38:15 -0500436 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800437 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200438 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
439 fi
440
Dean Troyerdff49a22014-01-30 15:37:40 -0600441 if [[ "$OFFLINE" = "True" ]]; then
442 echo "Running in offline mode, clones already exist"
443 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500444 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600445 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400446 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600447 return
448 fi
449
Dean Troyer50cda692014-07-25 11:57:20 -0500450 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600451 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500452 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600453 [[ "$ERROR_ON_CLONE" = "True" ]] && \
454 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200455 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600456 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500457 cd $git_dest
458 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600459 else
460 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500461 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600462 [[ "$ERROR_ON_CLONE" = "True" ]] && \
463 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200464 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500465 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600466 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500467 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600468 elif [[ "$RECLONE" = "True" ]]; then
469 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500470 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600471 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500472 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100473 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600474 # remove the existing ignored files (like pyc) as they cause breakage
475 # (due to the py files having older timestamps than our pyc, so python
476 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500477 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600478
Dean Troyer50cda692014-07-25 11:57:20 -0500479 # handle git_ref accordingly to type (tag, branch)
480 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
481 git_update_tag $git_ref
482 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
483 git_update_branch $git_ref
484 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
485 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600486 else
Dean Troyer50cda692014-07-25 11:57:20 -0500487 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600488 fi
489
490 fi
491 fi
492
493 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500494 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600495 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400496 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600497}
498
Sean Daguecc524062014-10-01 09:06:43 -0400499# A variation on git clone that lets us specify a project by it's
500# actual name, like oslo.config. This is exceptionally useful in the
501# library installation case
502function git_clone_by_name {
503 local name=$1
504 local repo=${GITREPO[$name]}
505 local dir=${GITDIR[$name]}
506 local branch=${GITBRANCH[$name]}
507 git_clone $repo $dir $branch
508}
509
510
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100511# git can sometimes get itself infinitely stuck with transient network
512# errors or other issues with the remote end. This wraps git in a
513# timeout/retry loop and is intended to watch over non-local git
514# processes that might hang. GIT_TIMEOUT, if set, is passed directly
515# to timeout(1); otherwise the default value of 0 maintains the status
516# quo of waiting forever.
517# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100518function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100519 local count=0
520 local timeout=0
521
522 if [[ -n "${GIT_TIMEOUT}" ]]; then
523 timeout=${GIT_TIMEOUT}
524 fi
525
526 until timeout -s SIGINT ${timeout} git "$@"; do
527 # 124 is timeout(1)'s special return code when it reached the
528 # timeout; otherwise assume fatal failure
529 if [[ $? -ne 124 ]]; then
530 die $LINENO "git call failed: [git $@]"
531 fi
532
533 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400534 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100535 if [ $count -eq 3 ]; then
536 die $LINENO "Maximum of 3 git retries reached"
537 fi
538 sleep 5
539 done
540}
541
Dean Troyerdff49a22014-01-30 15:37:40 -0600542# git update using reference as a branch.
543# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100544function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500545 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600546
Dean Troyer50cda692014-07-25 11:57:20 -0500547 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600548 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500549 git branch -D $git_branch || true
550 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600551}
552
553# git update using reference as a branch.
554# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100555function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500556 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600557
Dean Troyer50cda692014-07-25 11:57:20 -0500558 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600559}
560
561# git update using reference as a tag. Be careful editing source at that repo
562# as working copy will be in a detached mode
563# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100564function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500565 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600566
Dean Troyer50cda692014-07-25 11:57:20 -0500567 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600568 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500569 git_timed fetch origin tag $git_tag
570 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600571}
572
573
574# OpenStack Functions
575# ===================
576
577# Get the default value for HOST_IP
578# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100579function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600580 local fixed_range=$1
581 local floating_range=$2
582 local host_ip_iface=$3
583 local host_ip=$4
Brian Haley180f5eb2015-06-16 13:14:31 -0400584 local af=$5
Dean Troyerdff49a22014-01-30 15:37:40 -0600585
Dean Troyerdff49a22014-01-30 15:37:40 -0600586 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
587 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
588 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100589 # Find the interface used for the default route
Brian Haley180f5eb2015-06-16 13:14:31 -0400590 host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
591 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 -0500592 local ip
593 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600594 # Attempt to filter out IP addresses that are part of the fixed and
595 # floating range. Note that this method only works if the ``netaddr``
596 # python library is installed. If it is not installed, an error
597 # will be printed and the first IP from the interface will be used.
598 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
599 # address.
Brian Haley180f5eb2015-06-16 13:14:31 -0400600 if [[ "$af" == "inet6" ]]; then
601 host_ip=$ip
602 break;
603 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500604 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
605 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600606 break;
607 fi
608 done
609 fi
610 echo $host_ip
611}
612
Attila Fazekasf71b5002014-05-28 09:52:22 +0200613# Generates hex string from ``size`` byte of pseudo random data
614# generate_hex_string size
615function generate_hex_string {
616 local size=$1
617 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
618}
619
Dean Troyerdff49a22014-01-30 15:37:40 -0600620# Grab a numbered field from python prettytable output
621# Fields are numbered starting with 1
622# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
623# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100624function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500625 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600626 while read data; do
627 if [ "$1" -lt 0 ]; then
628 field="(\$(NF$1))"
629 else
630 field="\$$(($1 + 1))"
631 fi
632 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
633 done
634}
635
yuntongjinf26deea2015-02-28 10:50:34 +0800636# install default policy
637# copy over a default policy.json and policy.d for projects
638function install_default_policy {
639 local project=$1
640 local project_uc=$(echo $1|tr a-z A-Z)
641 local conf_dir="${project_uc}_CONF_DIR"
642 # eval conf dir to get the variable
643 conf_dir="${!conf_dir}"
644 local project_dir="${project_uc}_DIR"
645 # eval project dir to get the variable
646 project_dir="${!project_dir}"
647 local sample_conf_dir="${project_dir}/etc/${project}"
648 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
649
650 # first copy any policy.json
651 cp -p $sample_conf_dir/policy.json $conf_dir
652 # then optionally copy over policy.d
653 if [[ -d $sample_policy_dir ]]; then
654 cp -r $sample_policy_dir $conf_dir/policy.d
655 fi
656}
657
Dean Troyerdff49a22014-01-30 15:37:40 -0600658# Add a policy to a policy.json file
659# Do nothing if the policy already exists
660# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100661function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600662 local policy_file=$1
663 local policy_name=$2
664 local policy_perm=$3
665
666 if grep -q ${policy_name} ${policy_file}; then
667 echo "Policy ${policy_name} already exists in ${policy_file}"
668 return
669 fi
670
671 # Add a terminating comma to policy lines without one
672 # Remove the closing '}' and all lines following to the end-of-file
673 local tmpfile=$(mktemp)
674 uniq ${policy_file} | sed -e '
675 s/]$/],/
676 /^[}]/,$d
677 ' > ${tmpfile}
678
679 # Append policy and closing brace
680 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
681 echo "}" >>${tmpfile}
682
683 mv ${tmpfile} ${policy_file}
684}
685
Alistair Coles24779f62014-10-15 18:57:59 +0100686# Gets or creates a domain
687# Usage: get_or_create_domain <name> <description>
688function get_or_create_domain {
Ian Wienand11d276c2015-07-02 09:34:34 +1000689 local domain_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500690 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100691 # Gets domain id
Ian Wienand11d276c2015-07-02 09:34:34 +1000692 domain_id=$(
Alistair Coles24779f62014-10-15 18:57:59 +0100693 # Gets domain id
694 openstack --os-token=$OS_TOKEN --os-url=$os_url \
695 --os-identity-api-version=3 domain show $1 \
696 -f value -c id 2>/dev/null ||
697 # Creates new domain
698 openstack --os-token=$OS_TOKEN --os-url=$os_url \
699 --os-identity-api-version=3 domain create $1 \
700 --description "$2" \
701 -f value -c id
702 )
703 echo $domain_id
704}
705
Steve Martinellib74e01c2014-12-18 01:35:35 -0500706# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000707# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500708function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500709 local desc="${3:-}"
710 local os_url="$KEYSTONE_SERVICE_URI_V3"
Ian Wienand11d276c2015-07-02 09:34:34 +1000711 local group_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500712 # Gets group id
Ian Wienand11d276c2015-07-02 09:34:34 +1000713 group_id=$(
Steve Martinellib74e01c2014-12-18 01:35:35 -0500714 # Creates new group with --or-show
715 openstack --os-token=$OS_TOKEN --os-url=$os_url \
716 --os-identity-api-version=3 group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000717 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500718 -f value -c id
719 )
720 echo $group_id
721}
722
Bartosz Górski0abde392014-02-28 14:15:19 +0100723# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000724# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100725function get_or_create_user {
Ian Wienand11d276c2015-07-02 09:34:34 +1000726 local user_id
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000727 if [[ ! -z "$4" ]]; then
728 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200729 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500730 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200731 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100732 # Gets user id
Ian Wienand11d276c2015-07-02 09:34:34 +1000733 user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500734 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000735 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100736 $1 \
737 --password "$2" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000738 --os-url=$KEYSTONE_SERVICE_URI_V3 \
739 --os-identity-api-version=3 \
740 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500741 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500742 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100743 -f value -c id
744 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500745 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100746}
747
748# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000749# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100750function get_or_create_project {
Ian Wienand11d276c2015-07-02 09:34:34 +1000751 local project_id
752 project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500753 # Creates new project with --or-show
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000754 openstack --os-url=$KEYSTONE_SERVICE_URI_V3 \
755 --os-identity-api-version=3 \
756 project create $1 \
757 --domain=$2 \
758 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100759 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500760 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100761}
762
763# Gets or creates role
764# Usage: get_or_create_role <name>
765function get_or_create_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000766 local role_id
767 role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500768 # Creates role with --or-show
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000769 openstack role create $1 \
770 --os-url=$KEYSTONE_SERVICE_URI_V3 \
771 --os-identity-api-version=3 \
772 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100773 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500774 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100775}
776
Jamie Lennox9b215db2015-02-10 18:19:57 +1100777# Gets or adds user role to project
778# Usage: get_or_add_user_project_role <role> <user> <project>
779function get_or_add_user_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000780 local user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100781 # Gets user role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000782 user_role_id=$(openstack role list \
Steve Martinelli5541a612015-01-19 15:58:49 -0500783 --user $2 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000784 --os-url=$KEYSTONE_SERVICE_URI_V3 \
785 --os-identity-api-version=3 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100786 --column "ID" \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000787 --project $3 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100788 --column "Name" \
789 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500790 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100791 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500792 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100793 $1 \
794 --user $2 \
795 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000796 --os-url=$KEYSTONE_SERVICE_URI_V3 \
797 --os-identity-api-version=3 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100798 | grep " id " | get_field 2)
799 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500800 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100801}
802
Steve Martinelli4599fd12015-03-12 21:30:58 -0400803# Gets or adds group role to project
804# Usage: get_or_add_group_project_role <role> <group> <project>
805function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000806 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -0400807 # Gets group role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000808 group_role_id=$(openstack role list \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000809 --os-url=$KEYSTONE_SERVICE_URI_V3 \
810 --os-identity-api-version=3 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400811 --group $2 \
812 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000813 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400814 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000815 # Adds role to group and get it
816 openstack role add $1 \
817 --os-url=$KEYSTONE_SERVICE_URI_V3 \
818 --os-identity-api-version=3 \
819 --group $2 \
820 --project $3
821 group_role_id=$(openstack role list \
822 --os-url=$KEYSTONE_SERVICE_URI_V3 \
823 --os-identity-api-version=3 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400824 --group $2 \
825 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000826 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400827 fi
828 echo $group_role_id
829}
830
Bartosz Górski0abde392014-02-28 14:15:19 +0100831# Gets or creates service
832# Usage: get_or_create_service <name> <type> <description>
833function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +1000834 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100835 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +1000836 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100837 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +1000838 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100839 # Creates new service if not exists
840 openstack service create \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000841 --os-url $KEYSTONE_SERVICE_URI_V3 \
842 --os-identity-api-version=3 \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500843 $2 \
844 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100845 --description="$3" \
846 -f value -c id
847 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500848 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100849}
850
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000851# Create an endpoint with a specific interface
852# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
853function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +1000854 local endpoint_id
Daniel Gonzalez1991e752015-08-11 19:34:22 +0200855 # TODO(dgonzalez): The check of the region name, as done in the grep
856 # statement below, exists only because keystone does currently
857 # not allow filtering the region name when listing endpoints. If keystone
858 # gets support for this, the check for the region name can be removed.
859 # Related bug in keystone: https://bugs.launchpad.net/keystone/+bug/1482772
Ian Wienand11d276c2015-07-02 09:34:34 +1000860 endpoint_id=$(openstack endpoint list \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000861 --os-url $KEYSTONE_SERVICE_URI_V3 \
862 --os-identity-api-version=3 \
863 --service $1 \
864 --interface $2 \
865 --region $4 \
Daniel Gonzalez1991e752015-08-11 19:34:22 +0200866 -c ID -c Region -f value | grep $4 | cut -f 1 -d " ")
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500867 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100868 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500869 endpoint_id=$(openstack endpoint create \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000870 --os-url $KEYSTONE_SERVICE_URI_V3 \
871 --os-identity-api-version=3 \
872 $1 $2 $3 --region $4 -f value -c id)
Bartosz Górski0abde392014-02-28 14:15:19 +0100873 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000874
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500875 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100876}
Dean Troyerdff49a22014-01-30 15:37:40 -0600877
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000878# Gets or creates endpoint
879# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
880function get_or_create_endpoint {
881 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
882 # creating one endpoint with multiple urls to multiple endpoints each with
883 # a different interface. To maintain the existing function interface we
884 # create 3 endpoints and return the id of the public one. In reality
885 # returning the public id will not make a lot of difference as there are no
886 # scenarios currently that use the returned id. Ideally this behaviour
887 # should be pushed out to the service setups and let them create the
888 # endpoints they need.
889 local public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
890 _get_or_create_endpoint_with_interface $1 admin $4 $2
891 _get_or_create_endpoint_with_interface $1 internal $5 $2
892
893 # return the public id to indicate success, and this is the endpoint most likely wanted
894 echo $public_id
895}
896
897# Get a URL from the identity service
898# Usage: get_endpoint_url <service> <interface>
899function get_endpoint_url {
900 echo $(openstack endpoint list \
901 --service $1 --interface $2 \
902 --os-url $KEYSTONE_SERVICE_URI_V3 \
903 --os-identity-api-version=3 \
904 -c URL -f value)
905}
906
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500907
Dean Troyerdff49a22014-01-30 15:37:40 -0600908# Package Functions
909# =================
910
911# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100912function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800913 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600914 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800915
916 if [[ -z "$base_dir" ]]; then
917 base_dir=$FILES
918 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600919 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800920 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600921 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800922 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600923 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800924 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600925 else
926 exit_distro_not_supported "list of packages"
927 fi
928 echo "$pkg_dir"
929}
930
931# Wrapper for ``apt-get`` to set cache and proxy environment variables
932# Uses globals ``OFFLINE``, ``*_proxy``
933# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100934function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500935 local xtrace=$(set +o | grep xtrace)
936 set +o xtrace
937
Dean Troyerdff49a22014-01-30 15:37:40 -0600938 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
939 local sudo="sudo"
940 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500941
942 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500943
Dean Troyerdff49a22014-01-30 15:37:40 -0600944 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500945 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
946 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600947 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
948}
949
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800950function _parse_package_files {
951 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600952
Dean Troyerdff49a22014-01-30 15:37:40 -0600953 if [[ -z "$DISTRO" ]]; then
954 GetDistro
955 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600956
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800957 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600958 local OIFS line package distros distro
959 [[ -e $fname ]] || continue
960
961 OIFS=$IFS
962 IFS=$'\n'
963 for line in $(<${fname}); do
964 if [[ $line =~ "NOPRIME" ]]; then
965 continue
966 fi
967
968 # Assume we want this package
969 package=${line%#*}
970 inst_pkg=1
971
972 # Look for # dist:xxx in comment
973 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
974 # We are using BASH regexp matching feature.
975 package=${BASH_REMATCH[1]}
976 distros=${BASH_REMATCH[2]}
977 # In bash ${VAR,,} will lowecase VAR
978 # Look for a match in the distro list
979 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
980 # If no match then skip this package
981 inst_pkg=0
982 fi
983 fi
984
Dean Troyerdff49a22014-01-30 15:37:40 -0600985 if [[ $inst_pkg = 1 ]]; then
986 echo $package
987 fi
988 done
989 IFS=$OIFS
990 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800991}
992
993# get_packages() collects a list of package names of any type from the
994# prerequisite files in ``files/{debs|rpms}``. The list is intended
995# to be passed to a package installer such as apt or yum.
996#
997# Only packages required for the services in 1st argument will be
998# included. Two bits of metadata are recognized in the prerequisite files:
999#
1000# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1001# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1002# of the package to the distros listed. The distro names are case insensitive.
1003function get_packages {
1004 local xtrace=$(set +o | grep xtrace)
1005 set +o xtrace
1006 local services=$@
1007 local package_dir=$(_get_package_dir)
1008 local file_to_parse=""
1009 local service=""
1010
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001011 if [[ -z "$package_dir" ]]; then
1012 echo "No package directory supplied"
1013 return 1
1014 fi
1015 for service in ${services//,/ }; do
1016 # Allow individual services to specify dependencies
1017 if [[ -e ${package_dir}/${service} ]]; then
1018 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1019 fi
1020 # NOTE(sdague) n-api needs glance for now because that's where
1021 # glance client is
1022 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001023 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001024 file_to_parse="${file_to_parse} ${package_dir}/nova"
1025 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001026 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001027 file_to_parse="${file_to_parse} ${package_dir}/glance"
1028 fi
1029 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001030 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001031 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1032 fi
1033 elif [[ $service == ceilometer-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001034 if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001035 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
1036 fi
1037 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001038 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001039 file_to_parse="${file_to_parse} ${package_dir}/swift"
1040 fi
1041 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001042 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001043 file_to_parse="${file_to_parse} ${package_dir}/nova"
1044 fi
1045 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001046 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001047 file_to_parse="${file_to_parse} ${package_dir}/glance"
1048 fi
1049 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001050 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001051 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1052 fi
1053 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001054 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001055 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1056 fi
1057 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001058 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001059 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1060 fi
1061 fi
1062 done
1063 echo "$(_parse_package_files $file_to_parse)"
1064 $xtrace
1065}
1066
1067# get_plugin_packages() collects a list of package names of any type from a
1068# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1069# list is intended to be passed to a package installer such as apt or yum.
1070#
1071# Only packages required for enabled and collected plugins will included.
1072#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001073# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001074# in these prerequisite files, see get_packages() for more info.
1075function get_plugin_packages {
1076 local xtrace=$(set +o | grep xtrace)
1077 set +o xtrace
1078 local files_to_parse=""
1079 local package_dir=""
1080 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1081 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1082 files_to_parse+="$package_dir/$plugin"
1083 done
1084 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001085 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001086}
1087
1088# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001089# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001090# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001091function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001092 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1093 REPOS_UPDATED=${REPOS_UPDATED:-False}
1094 RETRY_UPDATE=${RETRY_UPDATE:-False}
1095
Paul Linchpiner9e179742014-07-13 22:23:00 -07001096 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001097 return 0
1098 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001099
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001100 if is_ubuntu; then
1101 local xtrace=$(set +o | grep xtrace)
1102 set +o xtrace
1103 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1104 # if there are transient errors pulling the updates, that's fine.
1105 # It may be secondary repositories that we don't really care about.
1106 apt_get update || /bin/true
1107 REPOS_UPDATED=True
1108 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001109 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001110 fi
1111}
1112
1113function real_install_package {
1114 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001115 apt_get install "$@"
1116 elif is_fedora; then
1117 yum_install "$@"
1118 elif is_suse; then
1119 zypper_install "$@"
1120 else
1121 exit_distro_not_supported "installing packages"
1122 fi
1123}
1124
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001125# Distro-agnostic package installer
1126# install_package package [package ...]
1127function install_package {
1128 update_package_repo
1129 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1130}
1131
Dean Troyerdff49a22014-01-30 15:37:40 -06001132# Distro-agnostic function to tell if a package is installed
1133# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001134function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001135 if [[ -z "$@" ]]; then
1136 return 1
1137 fi
1138
1139 if [[ -z "$os_PACKAGE" ]]; then
1140 GetOSVersion
1141 fi
1142
1143 if [[ "$os_PACKAGE" = "deb" ]]; then
1144 dpkg -s "$@" > /dev/null 2> /dev/null
1145 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1146 rpm --quiet -q "$@"
1147 else
1148 exit_distro_not_supported "finding if a package is installed"
1149 fi
1150}
1151
1152# Distro-agnostic package uninstaller
1153# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001154function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001155 if is_ubuntu; then
1156 apt_get purge "$@"
1157 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001158 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001159 elif is_suse; then
1160 sudo zypper rm "$@"
1161 else
1162 exit_distro_not_supported "uninstalling packages"
1163 fi
1164}
1165
1166# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001167# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001168# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001169function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001170 [[ "$OFFLINE" = "True" ]] && return
1171 local sudo="sudo"
1172 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001173
1174 # The manual check for missing packages is because yum -y assumes
1175 # missing packages are OK. See
1176 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001177 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1178 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001179 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001180 awk '
1181 BEGIN { fail=0 }
1182 /No package/ { fail=1 }
1183 { print }
1184 END { exit fail }' || \
1185 die $LINENO "Missing packages detected"
1186
1187 # also ensure we catch a yum failure
1188 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001189 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001190 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001191}
1192
1193# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001194# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001195# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001196function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001197 [[ "$OFFLINE" = "True" ]] && return
1198 local sudo="sudo"
1199 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001200 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1201 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001202 zypper --non-interactive install --auto-agree-with-licenses "$@"
1203}
1204
1205
1206# Process Functions
1207# =================
1208
1209# _run_process() is designed to be backgrounded by run_process() to simulate a
1210# fork. It includes the dirty work of closing extra filehandles and preparing log
1211# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001212# from the service name.
1213# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001214# If an optional group is provided sg will be used to set the group of
1215# the command.
1216# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001217function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001218 # disable tracing through the exec redirects, it's just confusing in the logs.
1219 xtrace=$(set +o | grep xtrace)
1220 set +o xtrace
1221
Dean Troyerdff49a22014-01-30 15:37:40 -06001222 local service=$1
1223 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001224 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001225
1226 # Undo logging redirections and close the extra descriptors
1227 exec 1>&3
1228 exec 2>&3
1229 exec 3>&-
1230 exec 6>&-
1231
Dean Troyerdde41d02014-12-09 17:47:57 -06001232 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1233 if [[ -n ${LOGDIR} ]]; then
1234 exec 1>&"$real_logfile" 2>&1
1235 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1236 if [[ -n ${SCREEN_LOGDIR} ]]; then
1237 # Drop the backward-compat symlink
1238 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1239 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001240
1241 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1242 export PYTHONUNBUFFERED=1
1243 fi
1244
Sean Dague6e137ab2015-04-29 08:22:24 -04001245 # reenable xtrace before we do *real* work
1246 $xtrace
1247
Dean Troyer3159a822014-08-27 14:13:58 -05001248 # Run under ``setsid`` to force the process to become a session and group leader.
1249 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001250 if [[ -n "$group" ]]; then
1251 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1252 else
1253 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1254 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001255
1256 # Just silently exit this process
1257 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001258}
1259
1260# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1261# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001262# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001263# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001264function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001265 SCREEN_NAME=${SCREEN_NAME:-stack}
1266 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1267
1268 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1269 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1270 fi
1271
1272 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1273}
1274
1275# Find out if a process exists by partial name.
1276# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001277function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001278 local name=$1
1279 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001280 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001281 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001282 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001283}
1284
Dean Troyer3159a822014-08-27 14:13:58 -05001285# Run a single service under screen or directly
1286# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001287# If an optional group is provided sg will be used to run the
1288# command as that group.
1289# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001290function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001291 local service=$1
1292 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001293 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001294
Dean Troyer3159a822014-08-27 14:13:58 -05001295 if is_service_enabled $service; then
1296 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001297 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001298 else
1299 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001300 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001301 fi
1302 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001303}
1304
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001305# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001306# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001307# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001308# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001309# Run a command in a shell in a screen window, if an optional group
1310# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001311function screen_process {
1312 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001313 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001314 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001315
Sean Dagueea22a4f2014-06-27 15:21:41 -04001316 SCREEN_NAME=${SCREEN_NAME:-stack}
1317 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001318 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001319
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001320 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001321
Dean Troyerdde41d02014-12-09 17:47:57 -06001322 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1323 echo "LOGDIR: $LOGDIR"
1324 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1325 echo "log: $real_logfile"
1326 if [[ -n ${LOGDIR} ]]; then
1327 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001328 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001329 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1330 if [[ -n ${SCREEN_LOGDIR} ]]; then
1331 # Drop the backward-compat symlink
1332 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1333 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001334 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001335
1336 # sleep to allow bash to be ready to be send the command - we are
1337 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001338 # bash isn't running by the time we send the command, nothing
1339 # happens. This sleep was added originally to handle gate runs
1340 # where we needed this to be at least 3 seconds to pass
1341 # consistently on slow clouds. Now this is configurable so that we
1342 # can determine a reasonable value for the local case which should
1343 # be much smaller.
1344 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001345
1346 NL=`echo -ne '\015'`
1347 # This fun command does the following:
1348 # - the passed server command is backgrounded
1349 # - the pid of the background process is saved in the usual place
1350 # - the server process is brought back to the foreground
1351 # - if the server process exits prematurely the fg command errors
1352 # and a message is written to stdout and the process failure file
1353 #
1354 # The pid saved can be used in stop_process() as a process group
1355 # id to kill off all child processes
1356 if [[ -n "$group" ]]; then
1357 command="sg $group '$command'"
1358 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001359
1360 # Append the process to the screen rc file
1361 screen_rc "$name" "$command"
1362
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001363 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 -06001364}
1365
1366# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001367# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001368# screen_rc service "command-line"
1369function screen_rc {
1370 SCREEN_NAME=${SCREEN_NAME:-stack}
1371 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1372 if [[ ! -e $SCREENRC ]]; then
1373 # Name the screen session
1374 echo "sessionname $SCREEN_NAME" > $SCREENRC
1375 # Set a reasonable statusbar
1376 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1377 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1378 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1379 echo "screen -t shell bash" >> $SCREENRC
1380 fi
1381 # If this service doesn't already exist in the screenrc file
1382 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1383 NL=`echo -ne '\015'`
1384 echo "screen -t $1 bash" >> $SCREENRC
1385 echo "stuff \"$2$NL\"" >> $SCREENRC
1386
Dean Troyerdde41d02014-12-09 17:47:57 -06001387 if [[ -n ${LOGDIR} ]]; then
1388 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001389 echo "log on" >>$SCREENRC
1390 fi
1391 fi
1392}
1393
1394# Stop a service in screen
1395# If a PID is available use it, kill the whole process group via TERM
1396# If screen is being used kill the screen window; this will catch processes
1397# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001398# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001399# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001400function screen_stop_service {
1401 local service=$1
1402
Dean Troyerdff49a22014-01-30 15:37:40 -06001403 SCREEN_NAME=${SCREEN_NAME:-stack}
1404 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001405 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001406
Dean Troyer3159a822014-08-27 14:13:58 -05001407 if is_service_enabled $service; then
1408 # Clean up the screen window
Attila Fazekasf750a6f2015-07-01 12:17:35 +02001409 screen -S $SCREEN_NAME -p $service -X kill || true
Dean Troyer3159a822014-08-27 14:13:58 -05001410 fi
1411}
1412
1413# Stop a service process
1414# If a PID is available use it, kill the whole process group via TERM
1415# If screen is being used kill the screen window; this will catch processes
1416# that did not leave a PID behind
1417# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1418# stop_process service
1419function stop_process {
1420 local service=$1
1421
1422 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001423 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001424
1425 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001426 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001427 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1428 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1429 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001430 fi
1431 if [[ "$USE_SCREEN" = "True" ]]; then
1432 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001433 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001434 fi
1435 fi
1436}
1437
1438# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001439# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001440# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001441function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001442 local service
1443 local failures
1444 SCREEN_NAME=${SCREEN_NAME:-stack}
1445 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1446
1447
1448 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1449 echo "No service status directory found"
1450 return
1451 fi
1452
1453 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001454 # make this -o errexit safe
1455 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001456
1457 for service in $failures; do
1458 service=`basename $service`
1459 service=${service%.failure}
1460 echo "Error: Service $service is not running"
1461 done
1462
1463 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001464 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001465 fi
1466}
1467
Chris Dent2f27a0e2014-09-09 13:46:02 +01001468# Tail a log file in a screen if USE_SCREEN is true.
1469function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001470 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001471 local logfile=$2
1472
Sean Dague53753292014-12-04 19:38:15 -05001473 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001474 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001475 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001476 fi
1477}
1478
Dean Troyerdff49a22014-01-30 15:37:40 -06001479
Dean Troyer3159a822014-08-27 14:13:58 -05001480# Deprecated Functions
1481# --------------------
1482
1483# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1484# fork. It includes the dirty work of closing extra filehandles and preparing log
1485# files to produce the same logs as screen_it(). The log filename is derived
1486# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1487# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1488# _old_run_process service "command-line"
1489function _old_run_process {
1490 local service=$1
1491 local command="$2"
1492
1493 # Undo logging redirections and close the extra descriptors
1494 exec 1>&3
1495 exec 2>&3
1496 exec 3>&-
1497 exec 6>&-
1498
1499 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001500 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1501 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001502
1503 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1504 export PYTHONUNBUFFERED=1
1505 fi
1506
1507 exec /bin/bash -c "$command"
1508 die "$service exec failure: $command"
1509}
1510
1511# old_run_process() launches a child process that closes all file descriptors and
1512# then exec's the passed in command. This is meant to duplicate the semantics
1513# of screen_it() without screen. PIDs are written to
1514# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1515# old_run_process service "command-line"
1516function old_run_process {
1517 local service=$1
1518 local command="$2"
1519
1520 # Spawn the child process
1521 _old_run_process "$service" "$command" &
1522 echo $!
1523}
1524
1525# Compatibility for existing start_XXXX() functions
1526# Uses global ``USE_SCREEN``
1527# screen_it service "command-line"
1528function screen_it {
1529 if is_service_enabled $1; then
1530 # Append the service to the screen rc file
1531 screen_rc "$1" "$2"
1532
1533 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001534 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001535 else
1536 # Spawn directly without screen
1537 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1538 fi
1539 fi
1540}
1541
1542# Compatibility for existing stop_XXXX() functions
1543# Stop a service in screen
1544# If a PID is available use it, kill the whole process group via TERM
1545# If screen is being used kill the screen window; this will catch processes
1546# that did not leave a PID behind
1547# screen_stop service
1548function screen_stop {
1549 # Clean up the screen window
1550 stop_process $1
1551}
1552
1553
Sean Dague2c65e712014-12-18 09:44:56 -05001554# Plugin Functions
1555# =================
1556
1557DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1558
1559# enable_plugin <name> <url> [branch]
1560#
1561# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1562# ``url`` is a git url
1563# ``branch`` is a gitref. If it's not set, defaults to master
1564function enable_plugin {
1565 local name=$1
1566 local url=$2
1567 local branch=${3:-master}
1568 DEVSTACK_PLUGINS+=",$name"
1569 GITREPO[$name]=$url
1570 GITDIR[$name]=$DEST/$name
1571 GITBRANCH[$name]=$branch
1572}
1573
1574# fetch_plugins
1575#
1576# clones all plugins
1577function fetch_plugins {
1578 local plugins="${DEVSTACK_PLUGINS}"
1579 local plugin
1580
1581 # short circuit if nothing to do
1582 if [[ -z $plugins ]]; then
1583 return
1584 fi
1585
Dean Troyerdc97cb72015-03-28 08:20:50 -05001586 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001587 for plugin in ${plugins//,/ }; do
1588 git_clone_by_name $plugin
1589 done
1590}
1591
1592# load_plugin_settings
1593#
1594# Load settings from plugins in the order that they were registered
1595function load_plugin_settings {
1596 local plugins="${DEVSTACK_PLUGINS}"
1597 local plugin
1598
1599 # short circuit if nothing to do
1600 if [[ -z $plugins ]]; then
1601 return
1602 fi
1603
1604 echo "Loading plugin settings"
1605 for plugin in ${plugins//,/ }; do
1606 local dir=${GITDIR[$plugin]}
1607 # source any known settings
1608 if [[ -f $dir/devstack/settings ]]; then
1609 source $dir/devstack/settings
1610 fi
1611 done
1612}
1613
Sean Dague6e275e12015-03-26 05:54:28 -04001614# plugin_override_defaults
1615#
1616# Run an extremely early setting phase for plugins that allows default
1617# overriding of services.
1618function plugin_override_defaults {
1619 local plugins="${DEVSTACK_PLUGINS}"
1620 local plugin
1621
1622 # short circuit if nothing to do
1623 if [[ -z $plugins ]]; then
1624 return
1625 fi
1626
1627 echo "Overriding Configuration Defaults"
1628 for plugin in ${plugins//,/ }; do
1629 local dir=${GITDIR[$plugin]}
1630 # source any overrides
1631 if [[ -f $dir/devstack/override-defaults ]]; then
1632 # be really verbose that an override is happening, as it
1633 # may not be obvious if things fail later.
1634 echo "$plugin has overriden the following defaults"
1635 cat $dir/devstack/override-defaults
1636 source $dir/devstack/override-defaults
1637 fi
1638 done
1639}
1640
Sean Dague2c65e712014-12-18 09:44:56 -05001641# run_plugins
1642#
1643# Run the devstack/plugin.sh in all the plugin directories. These are
1644# run in registration order.
1645function run_plugins {
1646 local mode=$1
1647 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301648
1649 local plugins="${DEVSTACK_PLUGINS}"
1650 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001651 for plugin in ${plugins//,/ }; do
1652 local dir=${GITDIR[$plugin]}
1653 if [[ -f $dir/devstack/plugin.sh ]]; then
1654 source $dir/devstack/plugin.sh $mode $phase
1655 fi
1656 done
1657}
1658
1659function run_phase {
1660 local mode=$1
1661 local phase=$2
1662 if [[ -d $TOP_DIR/extras.d ]]; then
1663 for i in $TOP_DIR/extras.d/*.sh; do
1664 [[ -r $i ]] && source $i $mode $phase
1665 done
1666 fi
1667 # the source phase corresponds to settings loading in plugins
1668 if [[ "$mode" == "source" ]]; then
1669 load_plugin_settings
Sean Dague6e275e12015-03-26 05:54:28 -04001670 elif [[ "$mode" == "override_defaults" ]]; then
1671 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001672 else
1673 run_plugins $mode $phase
1674 fi
1675}
1676
Dean Troyerdff49a22014-01-30 15:37:40 -06001677
1678# Service Functions
1679# =================
1680
1681# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1682# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001683function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001684 echo "$1" | sed -e '
1685 s/,,/,/g;
1686 s/^,//;
1687 s/,$//
1688 '
1689}
1690
1691# disable_all_services() removes all current services
1692# from ``ENABLED_SERVICES`` to reset the configuration
1693# before a minimal installation
1694# Uses global ``ENABLED_SERVICES``
1695# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001696function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001697 ENABLED_SERVICES=""
1698}
1699
1700# Remove all services starting with '-'. For example, to install all default
1701# services except rabbit (rabbit) set in ``localrc``:
1702# ENABLED_SERVICES+=",-rabbit"
1703# Uses global ``ENABLED_SERVICES``
1704# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001705function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001706 local to_remove=""
1707 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001708 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001709
1710 # build up list of services that should be removed; i.e. they
1711 # begin with "-"
1712 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001713 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001714 to_remove+=",${service#-}"
1715 else
1716 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001717 fi
1718 done
Ian Wienand2796a822015-04-15 08:59:04 +10001719
1720 # go through the service list. if this service appears in the "to
1721 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001722 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Dean Troyerdff49a22014-01-30 15:37:40 -06001723}
1724
1725# disable_service() removes the services passed as argument to the
1726# ``ENABLED_SERVICES`` list, if they are present.
1727#
1728# For example:
1729# disable_service rabbit
1730#
1731# This function does not know about the special cases
1732# for nova, glance, and neutron built into is_service_enabled().
1733# Uses global ``ENABLED_SERVICES``
1734# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001735function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001736 local tmpsvcs=",${ENABLED_SERVICES},"
1737 local service
1738 for service in $@; do
1739 if is_service_enabled $service; then
1740 tmpsvcs=${tmpsvcs//,$service,/,}
1741 fi
1742 done
1743 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1744}
1745
1746# enable_service() adds the services passed as argument to the
1747# ``ENABLED_SERVICES`` list, if they are not already present.
1748#
1749# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001750# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001751#
1752# This function does not know about the special cases
1753# for nova, glance, and neutron built into is_service_enabled().
1754# Uses global ``ENABLED_SERVICES``
1755# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001756function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001757 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001758 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001759 for service in $@; do
1760 if ! is_service_enabled $service; then
1761 tmpsvcs+=",$service"
1762 fi
1763 done
1764 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1765 disable_negated_services
1766}
1767
1768# is_service_enabled() checks if the service(s) specified as arguments are
1769# enabled by the user in ``ENABLED_SERVICES``.
1770#
1771# Multiple services specified as arguments are ``OR``'ed together; the test
1772# is a short-circuit boolean, i.e it returns on the first match.
1773#
1774# There are special cases for some 'catch-all' services::
1775# **nova** returns true if any service enabled start with **n-**
1776# **cinder** returns true if any service enabled start with **c-**
1777# **ceilometer** returns true if any service enabled start with **ceilometer**
1778# **glance** returns true if any service enabled start with **g-**
1779# **neutron** returns true if any service enabled start with **q-**
1780# **swift** returns true if any service enabled start with **s-**
1781# **trove** returns true if any service enabled start with **tr-**
1782# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1783# **s-** services will be enabled. This will be deprecated in the future.
1784#
1785# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1786# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1787# as enabled in this case.
1788#
1789# Uses global ``ENABLED_SERVICES``
1790# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001791function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001792 local xtrace=$(set +o | grep xtrace)
1793 set +o xtrace
1794 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001795 local services=$@
1796 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001797 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001798 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001799
1800 # Look for top-level 'enabled' function for this service
1801 if type is_${service}_enabled >/dev/null 2>&1; then
1802 # A function exists for this service, use it
1803 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001804 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001805 fi
1806
1807 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1808 # are implemented
1809
Sean Dague45917cc2014-02-24 16:09:14 -05001810 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001811 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001812 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001813 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1814 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1815 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1816 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1817 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1818 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1819 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001820 done
Sean Dague45917cc2014-02-24 16:09:14 -05001821 $xtrace
1822 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001823}
1824
fumihiko kakuma8606c982015-04-13 09:55:06 +09001825# remove specified list from the input string
1826# remove_disabled_services service-list remove-list
1827function remove_disabled_services {
1828 local service_list=$1
1829 local remove_list=$2
1830 local service
1831 local enabled=""
1832
1833 for service in ${service_list//,/ }; do
1834 local remove
1835 local add=1
1836 for remove in ${remove_list//,/ }; do
1837 if [[ ${remove} == ${service} ]]; then
1838 add=0
1839 break
1840 fi
1841 done
1842 if [[ $add == 1 ]]; then
1843 enabled="${enabled},$service"
1844 fi
1845 done
1846 _cleanup_service_list "$enabled"
1847}
1848
Dean Troyerdff49a22014-01-30 15:37:40 -06001849# Toggle enable/disable_service for services that must run exclusive of each other
1850# $1 The name of a variable containing a space-separated list of services
1851# $2 The name of a variable in which to store the enabled service's name
1852# $3 The name of the service to enable
1853function use_exclusive_service {
1854 local options=${!1}
1855 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001856 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001857 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001858 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001859 for opt in $options;do
1860 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1861 done
1862 eval "$out=$selection"
1863 return 0
1864}
1865
1866
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001867# System Functions
1868# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001869
1870# Only run the command if the target file (the last arg) is not on an
1871# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001872function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001873 local xtrace=$(set +o | grep xtrace)
1874 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001875 local args=( $@ )
1876 local last
1877 local sudo_cmd
1878 local dir_to_check
1879
1880 let last="${#args[*]} - 1"
1881
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001882 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001883 if [ ! -d "$dir_to_check" ]; then
1884 dir_to_check=`dirname "$dir_to_check"`
1885 fi
1886
1887 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001888 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001889 return 0
1890 fi
1891
1892 if [[ $TRACK_DEPENDS = True ]]; then
1893 sudo_cmd="env"
1894 else
1895 sudo_cmd="sudo"
1896 fi
1897
Sean Dague45917cc2014-02-24 16:09:14 -05001898 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001899 $sudo_cmd $@
1900}
1901
1902# Exit 0 if address is in network or 1 if address is not in network
1903# ip-range is in CIDR notation: 1.2.3.4/20
1904# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001905function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001906 local ip=$1
1907 local range=$2
1908 local masklen=${range#*/}
1909 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1910 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1911 [[ $network == $subnet ]]
1912}
1913
1914# Add a user to a group.
1915# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001916function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001917 local user=$1
1918 local group=$2
1919
Thomas Bechtolda8580852015-05-31 00:04:33 +02001920 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06001921}
1922
1923# Convert CIDR notation to a IPv4 netmask
1924# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001925function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001926 local maskpat="255 255 255 255"
1927 local maskdgt="254 252 248 240 224 192 128"
1928 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1929 echo ${1-0}.${2-0}.${3-0}.${4-0}
1930}
1931
1932# Gracefully cp only if source file/dir exists
1933# cp_it source destination
1934function cp_it {
1935 if [ -e $1 ] || [ -d $1 ]; then
1936 cp -pRL $1 $2
1937 fi
1938}
1939
1940# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1941# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1942# ``localrc`` or on the command line if necessary::
1943#
1944# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1945#
1946# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1947
Ian Wienandaee18c72014-02-21 15:35:08 +11001948function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001949 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001950 export http_proxy=$http_proxy
1951 fi
Sean Dague53753292014-12-04 19:38:15 -05001952 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001953 export https_proxy=$https_proxy
1954 fi
Sean Dague53753292014-12-04 19:38:15 -05001955 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001956 export no_proxy=$no_proxy
1957 fi
1958}
1959
1960# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001961function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001962 local mount_type=`stat -f -L -c %T $1`
1963 test "$mount_type" == "nfs"
1964}
1965
1966# Return the network portion of the given IP address using netmask
1967# netmask is in the traditional dotted-quad format
1968# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001969function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001970 local ip=$1
1971 local mask=$2
1972 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1973 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1974 echo $subnet
1975}
1976
Chris Dent3a2c86a2015-05-12 13:41:25 +00001977# Return the current python as "python<major>.<minor>"
1978function python_version {
1979 local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
1980 echo "python${python_version}"
1981}
1982
Dean Troyerdff49a22014-01-30 15:37:40 -06001983# Service wrapper to restart services
1984# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001985function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001986 if is_ubuntu; then
1987 sudo /usr/sbin/service $1 restart
1988 else
1989 sudo /sbin/service $1 restart
1990 fi
1991}
1992
1993# Only change permissions of a file or directory if it is not on an
1994# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001995function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001996 _safe_permission_operation chmod $@
1997}
1998
1999# Only change ownership of a file or directory if it is not on an NFS
2000# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002001function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002002 _safe_permission_operation chown $@
2003}
2004
2005# Service wrapper to start services
2006# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002007function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002008 if is_ubuntu; then
2009 sudo /usr/sbin/service $1 start
2010 else
2011 sudo /sbin/service $1 start
2012 fi
2013}
2014
2015# Service wrapper to stop services
2016# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002017function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002018 if is_ubuntu; then
2019 sudo /usr/sbin/service $1 stop
2020 else
2021 sudo /sbin/service $1 stop
2022 fi
2023}
2024
Sean Dague442e4e92015-06-24 13:24:02 -04002025# Test with a finite retry loop.
2026#
2027function test_with_retry {
2028 local testcmd=$1
2029 local failmsg=$2
2030 local until=${3:-10}
2031 local sleep=${4:-0.5}
2032
2033 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2034 die $LINENO "$failmsg"
2035 fi
2036}
2037
Dean Troyerdff49a22014-01-30 15:37:40 -06002038
2039# Restore xtrace
2040$XTRACE
2041
2042# Local variables:
2043# mode: shell-script
2044# End: