blob: 585126477377095bbb42a5f7a6e8756d6326ef4f [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
Dean Troyerdff49a22014-01-30 15:37:40 -0600273 os_CODENAME=""
274 for r in "Red Hat" CentOS Fedora XenServer; do
275 os_VENDOR=$r
276 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
277 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
278 os_CODENAME=${ver#*|}
279 os_RELEASE=${ver%|*}
280 os_UPDATE=${os_RELEASE##*.}
281 os_RELEASE=${os_RELEASE%.*}
282 break
283 fi
284 os_VENDOR=""
285 done
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700286 if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
287 os_VENDOR=OracleLinux
288 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600289 os_PACKAGE="rpm"
290 elif [[ -r /etc/SuSE-release ]]; then
291 for r in openSUSE "SUSE Linux"; do
292 if [[ "$r" = "SUSE Linux" ]]; then
293 os_VENDOR="SUSE LINUX"
294 else
295 os_VENDOR=$r
296 fi
297
298 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
299 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
300 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
301 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
302 break
303 fi
304 os_VENDOR=""
305 done
306 os_PACKAGE="rpm"
307 # If lsb_release is not installed, we should be able to detect Debian OS
308 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
309 os_VENDOR="Debian"
310 os_PACKAGE="deb"
311 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
312 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
313 fi
314 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
315}
316
317# Translate the OS version values into common nomenclature
318# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500319declare DISTRO
320
Ian Wienandaee18c72014-02-21 15:35:08 +1100321function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600322 GetOSVersion
323 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
324 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
325 DISTRO=$os_CODENAME
326 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
327 # For Fedora, just use 'f' and the release
328 DISTRO="f$os_RELEASE"
329 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
330 DISTRO="opensuse-$os_RELEASE"
331 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
332 # For SLE, also use the service pack
333 if [[ -z "$os_UPDATE" ]]; then
334 DISTRO="sle${os_RELEASE}"
335 else
336 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
337 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530338 elif [[ "$os_VENDOR" =~ (Red Hat) || \
339 "$os_VENDOR" =~ (CentOS) || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700340 "$os_VENDOR" =~ (OracleLinux) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600341 # Drop the . release as we assume it's compatible
342 DISTRO="rhel${os_RELEASE::1}"
343 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
344 DISTRO="xs$os_RELEASE"
345 else
346 # Catch-all for now is Vendor + Release + Update
347 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
348 fi
349 export DISTRO
350}
351
352# Utility function for checking machine architecture
353# is_arch arch-type
354function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500355 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600356}
357
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700358# Determine if current distribution is an Oracle distribution
359# is_oraclelinux
360function is_oraclelinux {
361 if [[ -z "$os_VENDOR" ]]; then
362 GetOSVersion
363 fi
364
365 [ "$os_VENDOR" = "OracleLinux" ]
366}
367
368
Dean Troyerdff49a22014-01-30 15:37:40 -0600369# Determine if current distribution is a Fedora-based distribution
370# (Fedora, RHEL, CentOS, etc).
371# is_fedora
372function is_fedora {
373 if [[ -z "$os_VENDOR" ]]; then
374 GetOSVersion
375 fi
376
anju Tiwari6c639c92014-07-15 18:11:54 +0530377 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700378 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600379}
380
381
382# Determine if current distribution is a SUSE-based distribution
383# (openSUSE, SLE).
384# is_suse
385function is_suse {
386 if [[ -z "$os_VENDOR" ]]; then
387 GetOSVersion
388 fi
389
390 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
391}
392
393
394# Determine if current distribution is an Ubuntu-based distribution
395# It will also detect non-Ubuntu but Debian-based distros
396# is_ubuntu
397function is_ubuntu {
398 if [[ -z "$os_PACKAGE" ]]; then
399 GetOSVersion
400 fi
401 [ "$os_PACKAGE" = "deb" ]
402}
403
404
405# Git Functions
406# =============
407
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600408# Returns openstack release name for a given branch name
409# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100410function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600411 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700412 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600413 echo ${branch#*/}
414 else
415 echo "master"
416 fi
417}
418
Dean Troyerdff49a22014-01-30 15:37:40 -0600419# git clone only if directory doesn't exist already. Since ``DEST`` might not
420# be owned by the installation user, we create the directory and change the
421# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500422# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
423# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600424# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500425# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600426# git_clone remote dest-dir branch
427function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500428 local git_remote=$1
429 local git_dest=$2
430 local git_ref=$3
431 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200432 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500433
Sean Dague53753292014-12-04 19:38:15 -0500434 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800435 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200436 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
437 fi
438
Dean Troyerdff49a22014-01-30 15:37:40 -0600439 if [[ "$OFFLINE" = "True" ]]; then
440 echo "Running in offline mode, clones already exist"
441 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500442 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600443 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400444 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600445 return
446 fi
447
Dean Troyer50cda692014-07-25 11:57:20 -0500448 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600449 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500450 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600451 [[ "$ERROR_ON_CLONE" = "True" ]] && \
452 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200453 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600454 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500455 cd $git_dest
456 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600457 else
458 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500459 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600460 [[ "$ERROR_ON_CLONE" = "True" ]] && \
461 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200462 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500463 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600464 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500465 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600466 elif [[ "$RECLONE" = "True" ]]; then
467 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500468 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600469 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500470 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100471 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600472 # remove the existing ignored files (like pyc) as they cause breakage
473 # (due to the py files having older timestamps than our pyc, so python
474 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500475 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600476
Dean Troyer50cda692014-07-25 11:57:20 -0500477 # handle git_ref accordingly to type (tag, branch)
478 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
479 git_update_tag $git_ref
480 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
481 git_update_branch $git_ref
482 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
483 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600484 else
Dean Troyer50cda692014-07-25 11:57:20 -0500485 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600486 fi
487
488 fi
489 fi
490
491 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500492 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600493 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400494 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600495}
496
Sean Daguecc524062014-10-01 09:06:43 -0400497# A variation on git clone that lets us specify a project by it's
498# actual name, like oslo.config. This is exceptionally useful in the
499# library installation case
500function git_clone_by_name {
501 local name=$1
502 local repo=${GITREPO[$name]}
503 local dir=${GITDIR[$name]}
504 local branch=${GITBRANCH[$name]}
505 git_clone $repo $dir $branch
506}
507
508
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100509# git can sometimes get itself infinitely stuck with transient network
510# errors or other issues with the remote end. This wraps git in a
511# timeout/retry loop and is intended to watch over non-local git
512# processes that might hang. GIT_TIMEOUT, if set, is passed directly
513# to timeout(1); otherwise the default value of 0 maintains the status
514# quo of waiting forever.
515# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100516function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100517 local count=0
518 local timeout=0
519
520 if [[ -n "${GIT_TIMEOUT}" ]]; then
521 timeout=${GIT_TIMEOUT}
522 fi
523
524 until timeout -s SIGINT ${timeout} git "$@"; do
525 # 124 is timeout(1)'s special return code when it reached the
526 # timeout; otherwise assume fatal failure
527 if [[ $? -ne 124 ]]; then
528 die $LINENO "git call failed: [git $@]"
529 fi
530
531 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400532 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100533 if [ $count -eq 3 ]; then
534 die $LINENO "Maximum of 3 git retries reached"
535 fi
536 sleep 5
537 done
538}
539
Dean Troyerdff49a22014-01-30 15:37:40 -0600540# git update using reference as a branch.
541# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100542function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500543 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600544
Dean Troyer50cda692014-07-25 11:57:20 -0500545 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600546 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500547 git branch -D $git_branch || true
548 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600549}
550
551# git update using reference as a branch.
552# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100553function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500554 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600555
Dean Troyer50cda692014-07-25 11:57:20 -0500556 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600557}
558
559# git update using reference as a tag. Be careful editing source at that repo
560# as working copy will be in a detached mode
561# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100562function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500563 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600564
Dean Troyer50cda692014-07-25 11:57:20 -0500565 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600566 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500567 git_timed fetch origin tag $git_tag
568 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600569}
570
571
572# OpenStack Functions
573# ===================
574
575# Get the default value for HOST_IP
576# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100577function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600578 local fixed_range=$1
579 local floating_range=$2
580 local host_ip_iface=$3
581 local host_ip=$4
Brian Haley180f5eb2015-06-16 13:14:31 -0400582 local af=$5
Dean Troyerdff49a22014-01-30 15:37:40 -0600583
Dean Troyerdff49a22014-01-30 15:37:40 -0600584 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
585 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
586 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100587 # Find the interface used for the default route
Brian Haley180f5eb2015-06-16 13:14:31 -0400588 host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
589 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 -0500590 local ip
591 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600592 # Attempt to filter out IP addresses that are part of the fixed and
593 # floating range. Note that this method only works if the ``netaddr``
594 # python library is installed. If it is not installed, an error
595 # will be printed and the first IP from the interface will be used.
596 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
597 # address.
Brian Haley180f5eb2015-06-16 13:14:31 -0400598 if [[ "$af" == "inet6" ]]; then
599 host_ip=$ip
600 break;
601 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500602 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
603 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600604 break;
605 fi
606 done
607 fi
608 echo $host_ip
609}
610
Attila Fazekasf71b5002014-05-28 09:52:22 +0200611# Generates hex string from ``size`` byte of pseudo random data
612# generate_hex_string size
613function generate_hex_string {
614 local size=$1
615 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
616}
617
Dean Troyerdff49a22014-01-30 15:37:40 -0600618# Grab a numbered field from python prettytable output
619# Fields are numbered starting with 1
620# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
621# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100622function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500623 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600624 while read data; do
625 if [ "$1" -lt 0 ]; then
626 field="(\$(NF$1))"
627 else
628 field="\$$(($1 + 1))"
629 fi
630 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
631 done
632}
633
yuntongjinf26deea2015-02-28 10:50:34 +0800634# install default policy
635# copy over a default policy.json and policy.d for projects
636function install_default_policy {
637 local project=$1
638 local project_uc=$(echo $1|tr a-z A-Z)
639 local conf_dir="${project_uc}_CONF_DIR"
640 # eval conf dir to get the variable
641 conf_dir="${!conf_dir}"
642 local project_dir="${project_uc}_DIR"
643 # eval project dir to get the variable
644 project_dir="${!project_dir}"
645 local sample_conf_dir="${project_dir}/etc/${project}"
646 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
647
648 # first copy any policy.json
649 cp -p $sample_conf_dir/policy.json $conf_dir
650 # then optionally copy over policy.d
651 if [[ -d $sample_policy_dir ]]; then
652 cp -r $sample_policy_dir $conf_dir/policy.d
653 fi
654}
655
Dean Troyerdff49a22014-01-30 15:37:40 -0600656# Add a policy to a policy.json file
657# Do nothing if the policy already exists
658# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100659function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600660 local policy_file=$1
661 local policy_name=$2
662 local policy_perm=$3
663
664 if grep -q ${policy_name} ${policy_file}; then
665 echo "Policy ${policy_name} already exists in ${policy_file}"
666 return
667 fi
668
669 # Add a terminating comma to policy lines without one
670 # Remove the closing '}' and all lines following to the end-of-file
671 local tmpfile=$(mktemp)
672 uniq ${policy_file} | sed -e '
673 s/]$/],/
674 /^[}]/,$d
675 ' > ${tmpfile}
676
677 # Append policy and closing brace
678 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
679 echo "}" >>${tmpfile}
680
681 mv ${tmpfile} ${policy_file}
682}
683
Alistair Coles24779f62014-10-15 18:57:59 +0100684# Gets or creates a domain
685# Usage: get_or_create_domain <name> <description>
686function get_or_create_domain {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500687 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100688 # Gets domain id
689 local domain_id=$(
690 # Gets domain id
691 openstack --os-token=$OS_TOKEN --os-url=$os_url \
692 --os-identity-api-version=3 domain show $1 \
693 -f value -c id 2>/dev/null ||
694 # Creates new domain
695 openstack --os-token=$OS_TOKEN --os-url=$os_url \
696 --os-identity-api-version=3 domain create $1 \
697 --description "$2" \
698 -f value -c id
699 )
700 echo $domain_id
701}
702
Steve Martinellib74e01c2014-12-18 01:35:35 -0500703# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000704# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500705function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500706 local desc="${3:-}"
707 local os_url="$KEYSTONE_SERVICE_URI_V3"
708 # Gets group id
709 local group_id=$(
710 # Creates new group with --or-show
711 openstack --os-token=$OS_TOKEN --os-url=$os_url \
712 --os-identity-api-version=3 group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000713 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500714 -f value -c id
715 )
716 echo $group_id
717}
718
Bartosz Górski0abde392014-02-28 14:15:19 +0100719# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000720# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100721function get_or_create_user {
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000722 if [[ ! -z "$4" ]]; then
723 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200724 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500725 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200726 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100727 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500728 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500729 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000730 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100731 $1 \
732 --password "$2" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000733 --os-url=$KEYSTONE_SERVICE_URI_V3 \
734 --os-identity-api-version=3 \
735 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500736 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500737 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100738 -f value -c id
739 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500740 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100741}
742
743# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000744# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100745function get_or_create_project {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500746 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500747 # Creates new project with --or-show
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000748 openstack --os-url=$KEYSTONE_SERVICE_URI_V3 \
749 --os-identity-api-version=3 \
750 project create $1 \
751 --domain=$2 \
752 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100753 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500754 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100755}
756
757# Gets or creates role
758# Usage: get_or_create_role <name>
759function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500760 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500761 # Creates role with --or-show
762 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100763 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500764 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100765}
766
Jamie Lennox9b215db2015-02-10 18:19:57 +1100767# Gets or adds user role to project
768# Usage: get_or_add_user_project_role <role> <user> <project>
769function get_or_add_user_project_role {
Bartosz Górski0abde392014-02-28 14:15:19 +0100770 # Gets user role id
Steve Martinelli5541a612015-01-19 15:58:49 -0500771 local user_role_id=$(openstack role list \
772 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100773 --project $3 \
774 --column "ID" \
775 --column "Name" \
776 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500777 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100778 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500779 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100780 $1 \
781 --user $2 \
782 --project $3 \
783 | grep " id " | get_field 2)
784 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500785 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100786}
787
Steve Martinelli4599fd12015-03-12 21:30:58 -0400788# Gets or adds group role to project
789# Usage: get_or_add_group_project_role <role> <group> <project>
790function get_or_add_group_project_role {
791 # Gets group role id
792 local group_role_id=$(openstack role list \
793 --group $2 \
794 --project $3 \
795 --column "ID" \
796 --column "Name" \
797 | grep " $1 " | get_field 1)
798 if [[ -z "$group_role_id" ]]; then
799 # Adds role to group
800 group_role_id=$(openstack role add \
801 $1 \
802 --group $2 \
803 --project $3 \
804 | grep " id " | get_field 2)
805 fi
806 echo $group_role_id
807}
808
Bartosz Górski0abde392014-02-28 14:15:19 +0100809# Gets or creates service
810# Usage: get_or_create_service <name> <type> <description>
811function get_or_create_service {
812 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500813 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100814 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +1000815 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100816 # Creates new service if not exists
817 openstack service create \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000818 --os-url $KEYSTONE_SERVICE_URI_V3 \
819 --os-identity-api-version=3 \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500820 $2 \
821 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100822 --description="$3" \
823 -f value -c id
824 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500825 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100826}
827
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000828# Create an endpoint with a specific interface
829# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
830function _get_or_create_endpoint_with_interface {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500831 local endpoint_id=$(openstack endpoint list \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000832 --os-url $KEYSTONE_SERVICE_URI_V3 \
833 --os-identity-api-version=3 \
834 --service $1 \
835 --interface $2 \
836 --region $4 \
837 -c ID -f value)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500838 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100839 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500840 endpoint_id=$(openstack endpoint create \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000841 --os-url $KEYSTONE_SERVICE_URI_V3 \
842 --os-identity-api-version=3 \
843 $1 $2 $3 --region $4 -f value -c id)
Bartosz Górski0abde392014-02-28 14:15:19 +0100844 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000845
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500846 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100847}
Dean Troyerdff49a22014-01-30 15:37:40 -0600848
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000849# Gets or creates endpoint
850# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
851function get_or_create_endpoint {
852 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
853 # creating one endpoint with multiple urls to multiple endpoints each with
854 # a different interface. To maintain the existing function interface we
855 # create 3 endpoints and return the id of the public one. In reality
856 # returning the public id will not make a lot of difference as there are no
857 # scenarios currently that use the returned id. Ideally this behaviour
858 # should be pushed out to the service setups and let them create the
859 # endpoints they need.
860 local public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
861 _get_or_create_endpoint_with_interface $1 admin $4 $2
862 _get_or_create_endpoint_with_interface $1 internal $5 $2
863
864 # return the public id to indicate success, and this is the endpoint most likely wanted
865 echo $public_id
866}
867
868# Get a URL from the identity service
869# Usage: get_endpoint_url <service> <interface>
870function get_endpoint_url {
871 echo $(openstack endpoint list \
872 --service $1 --interface $2 \
873 --os-url $KEYSTONE_SERVICE_URI_V3 \
874 --os-identity-api-version=3 \
875 -c URL -f value)
876}
877
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500878
Dean Troyerdff49a22014-01-30 15:37:40 -0600879# Package Functions
880# =================
881
882# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100883function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800884 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600885 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800886
887 if [[ -z "$base_dir" ]]; then
888 base_dir=$FILES
889 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600890 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800891 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600892 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800893 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600894 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800895 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600896 else
897 exit_distro_not_supported "list of packages"
898 fi
899 echo "$pkg_dir"
900}
901
902# Wrapper for ``apt-get`` to set cache and proxy environment variables
903# Uses globals ``OFFLINE``, ``*_proxy``
904# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100905function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500906 local xtrace=$(set +o | grep xtrace)
907 set +o xtrace
908
Dean Troyerdff49a22014-01-30 15:37:40 -0600909 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
910 local sudo="sudo"
911 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500912
913 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500914
Dean Troyerdff49a22014-01-30 15:37:40 -0600915 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500916 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
917 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600918 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
919}
920
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800921function _parse_package_files {
922 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600923
Dean Troyerdff49a22014-01-30 15:37:40 -0600924 if [[ -z "$DISTRO" ]]; then
925 GetDistro
926 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600927
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800928 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600929 local OIFS line package distros distro
930 [[ -e $fname ]] || continue
931
932 OIFS=$IFS
933 IFS=$'\n'
934 for line in $(<${fname}); do
935 if [[ $line =~ "NOPRIME" ]]; then
936 continue
937 fi
938
939 # Assume we want this package
940 package=${line%#*}
941 inst_pkg=1
942
943 # Look for # dist:xxx in comment
944 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
945 # We are using BASH regexp matching feature.
946 package=${BASH_REMATCH[1]}
947 distros=${BASH_REMATCH[2]}
948 # In bash ${VAR,,} will lowecase VAR
949 # Look for a match in the distro list
950 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
951 # If no match then skip this package
952 inst_pkg=0
953 fi
954 fi
955
Dean Troyerdff49a22014-01-30 15:37:40 -0600956 if [[ $inst_pkg = 1 ]]; then
957 echo $package
958 fi
959 done
960 IFS=$OIFS
961 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800962}
963
964# get_packages() collects a list of package names of any type from the
965# prerequisite files in ``files/{debs|rpms}``. The list is intended
966# to be passed to a package installer such as apt or yum.
967#
968# Only packages required for the services in 1st argument will be
969# included. Two bits of metadata are recognized in the prerequisite files:
970#
971# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
972# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
973# of the package to the distros listed. The distro names are case insensitive.
974function get_packages {
975 local xtrace=$(set +o | grep xtrace)
976 set +o xtrace
977 local services=$@
978 local package_dir=$(_get_package_dir)
979 local file_to_parse=""
980 local service=""
981
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800982 if [[ -z "$package_dir" ]]; then
983 echo "No package directory supplied"
984 return 1
985 fi
986 for service in ${services//,/ }; do
987 # Allow individual services to specify dependencies
988 if [[ -e ${package_dir}/${service} ]]; then
989 file_to_parse="${file_to_parse} ${package_dir}/${service}"
990 fi
991 # NOTE(sdague) n-api needs glance for now because that's where
992 # glance client is
993 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700994 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800995 file_to_parse="${file_to_parse} ${package_dir}/nova"
996 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700997 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800998 file_to_parse="${file_to_parse} ${package_dir}/glance"
999 fi
1000 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001001 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001002 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1003 fi
1004 elif [[ $service == ceilometer-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001005 if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001006 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
1007 fi
1008 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001009 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001010 file_to_parse="${file_to_parse} ${package_dir}/swift"
1011 fi
1012 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001013 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001014 file_to_parse="${file_to_parse} ${package_dir}/nova"
1015 fi
1016 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001017 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001018 file_to_parse="${file_to_parse} ${package_dir}/glance"
1019 fi
1020 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001021 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001022 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1023 fi
1024 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001025 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001026 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1027 fi
1028 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001029 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001030 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1031 fi
1032 fi
1033 done
1034 echo "$(_parse_package_files $file_to_parse)"
1035 $xtrace
1036}
1037
1038# get_plugin_packages() collects a list of package names of any type from a
1039# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1040# list is intended to be passed to a package installer such as apt or yum.
1041#
1042# Only packages required for enabled and collected plugins will included.
1043#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001044# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001045# in these prerequisite files, see get_packages() for more info.
1046function get_plugin_packages {
1047 local xtrace=$(set +o | grep xtrace)
1048 set +o xtrace
1049 local files_to_parse=""
1050 local package_dir=""
1051 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1052 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1053 files_to_parse+="$package_dir/$plugin"
1054 done
1055 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001056 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001057}
1058
1059# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001060# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001061# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001062function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001063 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1064 REPOS_UPDATED=${REPOS_UPDATED:-False}
1065 RETRY_UPDATE=${RETRY_UPDATE:-False}
1066
Paul Linchpiner9e179742014-07-13 22:23:00 -07001067 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001068 return 0
1069 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001070
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001071 if is_ubuntu; then
1072 local xtrace=$(set +o | grep xtrace)
1073 set +o xtrace
1074 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1075 # if there are transient errors pulling the updates, that's fine.
1076 # It may be secondary repositories that we don't really care about.
1077 apt_get update || /bin/true
1078 REPOS_UPDATED=True
1079 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001080 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001081 fi
1082}
1083
1084function real_install_package {
1085 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001086 apt_get install "$@"
1087 elif is_fedora; then
1088 yum_install "$@"
1089 elif is_suse; then
1090 zypper_install "$@"
1091 else
1092 exit_distro_not_supported "installing packages"
1093 fi
1094}
1095
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001096# Distro-agnostic package installer
1097# install_package package [package ...]
1098function install_package {
1099 update_package_repo
1100 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1101}
1102
Dean Troyerdff49a22014-01-30 15:37:40 -06001103# Distro-agnostic function to tell if a package is installed
1104# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001105function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001106 if [[ -z "$@" ]]; then
1107 return 1
1108 fi
1109
1110 if [[ -z "$os_PACKAGE" ]]; then
1111 GetOSVersion
1112 fi
1113
1114 if [[ "$os_PACKAGE" = "deb" ]]; then
1115 dpkg -s "$@" > /dev/null 2> /dev/null
1116 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1117 rpm --quiet -q "$@"
1118 else
1119 exit_distro_not_supported "finding if a package is installed"
1120 fi
1121}
1122
1123# Distro-agnostic package uninstaller
1124# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001125function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001126 if is_ubuntu; then
1127 apt_get purge "$@"
1128 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001129 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001130 elif is_suse; then
1131 sudo zypper rm "$@"
1132 else
1133 exit_distro_not_supported "uninstalling packages"
1134 fi
1135}
1136
1137# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001138# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001139# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001140function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001141 [[ "$OFFLINE" = "True" ]] && return
1142 local sudo="sudo"
1143 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001144
1145 # The manual check for missing packages is because yum -y assumes
1146 # missing packages are OK. See
1147 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001148 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1149 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001150 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001151 awk '
1152 BEGIN { fail=0 }
1153 /No package/ { fail=1 }
1154 { print }
1155 END { exit fail }' || \
1156 die $LINENO "Missing packages detected"
1157
1158 # also ensure we catch a yum failure
1159 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001160 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001161 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001162}
1163
1164# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001165# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001166# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001167function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001168 [[ "$OFFLINE" = "True" ]] && return
1169 local sudo="sudo"
1170 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001171 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1172 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001173 zypper --non-interactive install --auto-agree-with-licenses "$@"
1174}
1175
1176
1177# Process Functions
1178# =================
1179
1180# _run_process() is designed to be backgrounded by run_process() to simulate a
1181# fork. It includes the dirty work of closing extra filehandles and preparing log
1182# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001183# from the service name.
1184# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001185# If an optional group is provided sg will be used to set the group of
1186# the command.
1187# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001188function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001189 # disable tracing through the exec redirects, it's just confusing in the logs.
1190 xtrace=$(set +o | grep xtrace)
1191 set +o xtrace
1192
Dean Troyerdff49a22014-01-30 15:37:40 -06001193 local service=$1
1194 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001195 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001196
1197 # Undo logging redirections and close the extra descriptors
1198 exec 1>&3
1199 exec 2>&3
1200 exec 3>&-
1201 exec 6>&-
1202
Dean Troyerdde41d02014-12-09 17:47:57 -06001203 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1204 if [[ -n ${LOGDIR} ]]; then
1205 exec 1>&"$real_logfile" 2>&1
1206 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1207 if [[ -n ${SCREEN_LOGDIR} ]]; then
1208 # Drop the backward-compat symlink
1209 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1210 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001211
1212 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1213 export PYTHONUNBUFFERED=1
1214 fi
1215
Sean Dague6e137ab2015-04-29 08:22:24 -04001216 # reenable xtrace before we do *real* work
1217 $xtrace
1218
Dean Troyer3159a822014-08-27 14:13:58 -05001219 # Run under ``setsid`` to force the process to become a session and group leader.
1220 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001221 if [[ -n "$group" ]]; then
1222 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1223 else
1224 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1225 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001226
1227 # Just silently exit this process
1228 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001229}
1230
1231# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1232# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001233# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001234# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001235function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001236 SCREEN_NAME=${SCREEN_NAME:-stack}
1237 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1238
1239 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1240 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1241 fi
1242
1243 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1244}
1245
1246# Find out if a process exists by partial name.
1247# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001248function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001249 local name=$1
1250 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001251 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001252 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001253 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001254}
1255
Dean Troyer3159a822014-08-27 14:13:58 -05001256# Run a single service under screen or directly
1257# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001258# If an optional group is provided sg will be used to run the
1259# command as that group.
1260# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001261function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001262 local service=$1
1263 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001264 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001265
Dean Troyer3159a822014-08-27 14:13:58 -05001266 if is_service_enabled $service; then
1267 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001268 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001269 else
1270 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001271 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001272 fi
1273 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001274}
1275
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001276# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001277# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001278# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001279# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001280# Run a command in a shell in a screen window, if an optional group
1281# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001282function screen_process {
1283 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001284 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001285 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001286
Sean Dagueea22a4f2014-06-27 15:21:41 -04001287 SCREEN_NAME=${SCREEN_NAME:-stack}
1288 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001289 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001290
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001291 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001292
Dean Troyerdde41d02014-12-09 17:47:57 -06001293 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1294 echo "LOGDIR: $LOGDIR"
1295 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1296 echo "log: $real_logfile"
1297 if [[ -n ${LOGDIR} ]]; then
1298 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001299 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001300 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1301 if [[ -n ${SCREEN_LOGDIR} ]]; then
1302 # Drop the backward-compat symlink
1303 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1304 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001305 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001306
1307 # sleep to allow bash to be ready to be send the command - we are
1308 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001309 # bash isn't running by the time we send the command, nothing
1310 # happens. This sleep was added originally to handle gate runs
1311 # where we needed this to be at least 3 seconds to pass
1312 # consistently on slow clouds. Now this is configurable so that we
1313 # can determine a reasonable value for the local case which should
1314 # be much smaller.
1315 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001316
1317 NL=`echo -ne '\015'`
1318 # This fun command does the following:
1319 # - the passed server command is backgrounded
1320 # - the pid of the background process is saved in the usual place
1321 # - the server process is brought back to the foreground
1322 # - if the server process exits prematurely the fg command errors
1323 # and a message is written to stdout and the process failure file
1324 #
1325 # The pid saved can be used in stop_process() as a process group
1326 # id to kill off all child processes
1327 if [[ -n "$group" ]]; then
1328 command="sg $group '$command'"
1329 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001330
1331 # Append the process to the screen rc file
1332 screen_rc "$name" "$command"
1333
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001334 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 -06001335}
1336
1337# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001338# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001339# screen_rc service "command-line"
1340function screen_rc {
1341 SCREEN_NAME=${SCREEN_NAME:-stack}
1342 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1343 if [[ ! -e $SCREENRC ]]; then
1344 # Name the screen session
1345 echo "sessionname $SCREEN_NAME" > $SCREENRC
1346 # Set a reasonable statusbar
1347 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1348 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1349 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1350 echo "screen -t shell bash" >> $SCREENRC
1351 fi
1352 # If this service doesn't already exist in the screenrc file
1353 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1354 NL=`echo -ne '\015'`
1355 echo "screen -t $1 bash" >> $SCREENRC
1356 echo "stuff \"$2$NL\"" >> $SCREENRC
1357
Dean Troyerdde41d02014-12-09 17:47:57 -06001358 if [[ -n ${LOGDIR} ]]; then
1359 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001360 echo "log on" >>$SCREENRC
1361 fi
1362 fi
1363}
1364
1365# Stop a service in screen
1366# If a PID is available use it, kill the whole process group via TERM
1367# If screen is being used kill the screen window; this will catch processes
1368# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001369# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001370# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001371function screen_stop_service {
1372 local service=$1
1373
Dean Troyerdff49a22014-01-30 15:37:40 -06001374 SCREEN_NAME=${SCREEN_NAME:-stack}
1375 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001376 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001377
Dean Troyer3159a822014-08-27 14:13:58 -05001378 if is_service_enabled $service; then
1379 # Clean up the screen window
Attila Fazekasf750a6f2015-07-01 12:17:35 +02001380 screen -S $SCREEN_NAME -p $service -X kill || true
Dean Troyer3159a822014-08-27 14:13:58 -05001381 fi
1382}
1383
1384# Stop a service process
1385# If a PID is available use it, kill the whole process group via TERM
1386# If screen is being used kill the screen window; this will catch processes
1387# that did not leave a PID behind
1388# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1389# stop_process service
1390function stop_process {
1391 local service=$1
1392
1393 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001394 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001395
1396 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001397 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001398 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1399 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1400 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001401 fi
1402 if [[ "$USE_SCREEN" = "True" ]]; then
1403 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001404 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001405 fi
1406 fi
1407}
1408
1409# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001410# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001411# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001412function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001413 local service
1414 local failures
1415 SCREEN_NAME=${SCREEN_NAME:-stack}
1416 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1417
1418
1419 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1420 echo "No service status directory found"
1421 return
1422 fi
1423
1424 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001425 # make this -o errexit safe
1426 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001427
1428 for service in $failures; do
1429 service=`basename $service`
1430 service=${service%.failure}
1431 echo "Error: Service $service is not running"
1432 done
1433
1434 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001435 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001436 fi
1437}
1438
Chris Dent2f27a0e2014-09-09 13:46:02 +01001439# Tail a log file in a screen if USE_SCREEN is true.
1440function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001441 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001442 local logfile=$2
1443
Sean Dague53753292014-12-04 19:38:15 -05001444 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001445 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001446 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001447 fi
1448}
1449
Dean Troyerdff49a22014-01-30 15:37:40 -06001450
Dean Troyer3159a822014-08-27 14:13:58 -05001451# Deprecated Functions
1452# --------------------
1453
1454# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1455# fork. It includes the dirty work of closing extra filehandles and preparing log
1456# files to produce the same logs as screen_it(). The log filename is derived
1457# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1458# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1459# _old_run_process service "command-line"
1460function _old_run_process {
1461 local service=$1
1462 local command="$2"
1463
1464 # Undo logging redirections and close the extra descriptors
1465 exec 1>&3
1466 exec 2>&3
1467 exec 3>&-
1468 exec 6>&-
1469
1470 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001471 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1472 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001473
1474 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1475 export PYTHONUNBUFFERED=1
1476 fi
1477
1478 exec /bin/bash -c "$command"
1479 die "$service exec failure: $command"
1480}
1481
1482# old_run_process() launches a child process that closes all file descriptors and
1483# then exec's the passed in command. This is meant to duplicate the semantics
1484# of screen_it() without screen. PIDs are written to
1485# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1486# old_run_process service "command-line"
1487function old_run_process {
1488 local service=$1
1489 local command="$2"
1490
1491 # Spawn the child process
1492 _old_run_process "$service" "$command" &
1493 echo $!
1494}
1495
1496# Compatibility for existing start_XXXX() functions
1497# Uses global ``USE_SCREEN``
1498# screen_it service "command-line"
1499function screen_it {
1500 if is_service_enabled $1; then
1501 # Append the service to the screen rc file
1502 screen_rc "$1" "$2"
1503
1504 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001505 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001506 else
1507 # Spawn directly without screen
1508 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1509 fi
1510 fi
1511}
1512
1513# Compatibility for existing stop_XXXX() functions
1514# Stop a service in screen
1515# If a PID is available use it, kill the whole process group via TERM
1516# If screen is being used kill the screen window; this will catch processes
1517# that did not leave a PID behind
1518# screen_stop service
1519function screen_stop {
1520 # Clean up the screen window
1521 stop_process $1
1522}
1523
1524
Sean Dague2c65e712014-12-18 09:44:56 -05001525# Plugin Functions
1526# =================
1527
1528DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1529
1530# enable_plugin <name> <url> [branch]
1531#
1532# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1533# ``url`` is a git url
1534# ``branch`` is a gitref. If it's not set, defaults to master
1535function enable_plugin {
1536 local name=$1
1537 local url=$2
1538 local branch=${3:-master}
1539 DEVSTACK_PLUGINS+=",$name"
1540 GITREPO[$name]=$url
1541 GITDIR[$name]=$DEST/$name
1542 GITBRANCH[$name]=$branch
1543}
1544
1545# fetch_plugins
1546#
1547# clones all plugins
1548function fetch_plugins {
1549 local plugins="${DEVSTACK_PLUGINS}"
1550 local plugin
1551
1552 # short circuit if nothing to do
1553 if [[ -z $plugins ]]; then
1554 return
1555 fi
1556
Dean Troyerdc97cb72015-03-28 08:20:50 -05001557 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001558 for plugin in ${plugins//,/ }; do
1559 git_clone_by_name $plugin
1560 done
1561}
1562
1563# load_plugin_settings
1564#
1565# Load settings from plugins in the order that they were registered
1566function load_plugin_settings {
1567 local plugins="${DEVSTACK_PLUGINS}"
1568 local plugin
1569
1570 # short circuit if nothing to do
1571 if [[ -z $plugins ]]; then
1572 return
1573 fi
1574
1575 echo "Loading plugin settings"
1576 for plugin in ${plugins//,/ }; do
1577 local dir=${GITDIR[$plugin]}
1578 # source any known settings
1579 if [[ -f $dir/devstack/settings ]]; then
1580 source $dir/devstack/settings
1581 fi
1582 done
1583}
1584
Sean Dague6e275e12015-03-26 05:54:28 -04001585# plugin_override_defaults
1586#
1587# Run an extremely early setting phase for plugins that allows default
1588# overriding of services.
1589function plugin_override_defaults {
1590 local plugins="${DEVSTACK_PLUGINS}"
1591 local plugin
1592
1593 # short circuit if nothing to do
1594 if [[ -z $plugins ]]; then
1595 return
1596 fi
1597
1598 echo "Overriding Configuration Defaults"
1599 for plugin in ${plugins//,/ }; do
1600 local dir=${GITDIR[$plugin]}
1601 # source any overrides
1602 if [[ -f $dir/devstack/override-defaults ]]; then
1603 # be really verbose that an override is happening, as it
1604 # may not be obvious if things fail later.
1605 echo "$plugin has overriden the following defaults"
1606 cat $dir/devstack/override-defaults
1607 source $dir/devstack/override-defaults
1608 fi
1609 done
1610}
1611
Sean Dague2c65e712014-12-18 09:44:56 -05001612# run_plugins
1613#
1614# Run the devstack/plugin.sh in all the plugin directories. These are
1615# run in registration order.
1616function run_plugins {
1617 local mode=$1
1618 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301619
1620 local plugins="${DEVSTACK_PLUGINS}"
1621 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001622 for plugin in ${plugins//,/ }; do
1623 local dir=${GITDIR[$plugin]}
1624 if [[ -f $dir/devstack/plugin.sh ]]; then
1625 source $dir/devstack/plugin.sh $mode $phase
1626 fi
1627 done
1628}
1629
1630function run_phase {
1631 local mode=$1
1632 local phase=$2
1633 if [[ -d $TOP_DIR/extras.d ]]; then
1634 for i in $TOP_DIR/extras.d/*.sh; do
1635 [[ -r $i ]] && source $i $mode $phase
1636 done
1637 fi
1638 # the source phase corresponds to settings loading in plugins
1639 if [[ "$mode" == "source" ]]; then
1640 load_plugin_settings
Sean Dague6e275e12015-03-26 05:54:28 -04001641 elif [[ "$mode" == "override_defaults" ]]; then
1642 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001643 else
1644 run_plugins $mode $phase
1645 fi
1646}
1647
Dean Troyerdff49a22014-01-30 15:37:40 -06001648
1649# Service Functions
1650# =================
1651
1652# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1653# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001654function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001655 echo "$1" | sed -e '
1656 s/,,/,/g;
1657 s/^,//;
1658 s/,$//
1659 '
1660}
1661
1662# disable_all_services() removes all current services
1663# from ``ENABLED_SERVICES`` to reset the configuration
1664# before a minimal installation
1665# Uses global ``ENABLED_SERVICES``
1666# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001667function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001668 ENABLED_SERVICES=""
1669}
1670
1671# Remove all services starting with '-'. For example, to install all default
1672# services except rabbit (rabbit) set in ``localrc``:
1673# ENABLED_SERVICES+=",-rabbit"
1674# Uses global ``ENABLED_SERVICES``
1675# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001676function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001677 local to_remove=""
1678 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001679 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001680
1681 # build up list of services that should be removed; i.e. they
1682 # begin with "-"
1683 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001684 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001685 to_remove+=",${service#-}"
1686 else
1687 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001688 fi
1689 done
Ian Wienand2796a822015-04-15 08:59:04 +10001690
1691 # go through the service list. if this service appears in the "to
1692 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001693 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Dean Troyerdff49a22014-01-30 15:37:40 -06001694}
1695
1696# disable_service() removes the services passed as argument to the
1697# ``ENABLED_SERVICES`` list, if they are present.
1698#
1699# For example:
1700# disable_service rabbit
1701#
1702# This function does not know about the special cases
1703# for nova, glance, and neutron built into is_service_enabled().
1704# Uses global ``ENABLED_SERVICES``
1705# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001706function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001707 local tmpsvcs=",${ENABLED_SERVICES},"
1708 local service
1709 for service in $@; do
1710 if is_service_enabled $service; then
1711 tmpsvcs=${tmpsvcs//,$service,/,}
1712 fi
1713 done
1714 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1715}
1716
1717# enable_service() adds the services passed as argument to the
1718# ``ENABLED_SERVICES`` list, if they are not already present.
1719#
1720# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001721# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001722#
1723# This function does not know about the special cases
1724# for nova, glance, and neutron built into is_service_enabled().
1725# Uses global ``ENABLED_SERVICES``
1726# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001727function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001728 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001729 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001730 for service in $@; do
1731 if ! is_service_enabled $service; then
1732 tmpsvcs+=",$service"
1733 fi
1734 done
1735 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1736 disable_negated_services
1737}
1738
1739# is_service_enabled() checks if the service(s) specified as arguments are
1740# enabled by the user in ``ENABLED_SERVICES``.
1741#
1742# Multiple services specified as arguments are ``OR``'ed together; the test
1743# is a short-circuit boolean, i.e it returns on the first match.
1744#
1745# There are special cases for some 'catch-all' services::
1746# **nova** returns true if any service enabled start with **n-**
1747# **cinder** returns true if any service enabled start with **c-**
1748# **ceilometer** returns true if any service enabled start with **ceilometer**
1749# **glance** returns true if any service enabled start with **g-**
1750# **neutron** returns true if any service enabled start with **q-**
1751# **swift** returns true if any service enabled start with **s-**
1752# **trove** returns true if any service enabled start with **tr-**
1753# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1754# **s-** services will be enabled. This will be deprecated in the future.
1755#
1756# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1757# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1758# as enabled in this case.
1759#
1760# Uses global ``ENABLED_SERVICES``
1761# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001762function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001763 local xtrace=$(set +o | grep xtrace)
1764 set +o xtrace
1765 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001766 local services=$@
1767 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001768 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001769 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001770
1771 # Look for top-level 'enabled' function for this service
1772 if type is_${service}_enabled >/dev/null 2>&1; then
1773 # A function exists for this service, use it
1774 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001775 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001776 fi
1777
1778 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1779 # are implemented
1780
Sean Dague45917cc2014-02-24 16:09:14 -05001781 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001782 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001783 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001784 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1785 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1786 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1787 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1788 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1789 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1790 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001791 done
Sean Dague45917cc2014-02-24 16:09:14 -05001792 $xtrace
1793 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001794}
1795
fumihiko kakuma8606c982015-04-13 09:55:06 +09001796# remove specified list from the input string
1797# remove_disabled_services service-list remove-list
1798function remove_disabled_services {
1799 local service_list=$1
1800 local remove_list=$2
1801 local service
1802 local enabled=""
1803
1804 for service in ${service_list//,/ }; do
1805 local remove
1806 local add=1
1807 for remove in ${remove_list//,/ }; do
1808 if [[ ${remove} == ${service} ]]; then
1809 add=0
1810 break
1811 fi
1812 done
1813 if [[ $add == 1 ]]; then
1814 enabled="${enabled},$service"
1815 fi
1816 done
1817 _cleanup_service_list "$enabled"
1818}
1819
Dean Troyerdff49a22014-01-30 15:37:40 -06001820# Toggle enable/disable_service for services that must run exclusive of each other
1821# $1 The name of a variable containing a space-separated list of services
1822# $2 The name of a variable in which to store the enabled service's name
1823# $3 The name of the service to enable
1824function use_exclusive_service {
1825 local options=${!1}
1826 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001827 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001828 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001829 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001830 for opt in $options;do
1831 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1832 done
1833 eval "$out=$selection"
1834 return 0
1835}
1836
1837
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001838# System Functions
1839# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001840
1841# Only run the command if the target file (the last arg) is not on an
1842# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001843function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001844 local xtrace=$(set +o | grep xtrace)
1845 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001846 local args=( $@ )
1847 local last
1848 local sudo_cmd
1849 local dir_to_check
1850
1851 let last="${#args[*]} - 1"
1852
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001853 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001854 if [ ! -d "$dir_to_check" ]; then
1855 dir_to_check=`dirname "$dir_to_check"`
1856 fi
1857
1858 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001859 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001860 return 0
1861 fi
1862
1863 if [[ $TRACK_DEPENDS = True ]]; then
1864 sudo_cmd="env"
1865 else
1866 sudo_cmd="sudo"
1867 fi
1868
Sean Dague45917cc2014-02-24 16:09:14 -05001869 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001870 $sudo_cmd $@
1871}
1872
1873# Exit 0 if address is in network or 1 if address is not in network
1874# ip-range is in CIDR notation: 1.2.3.4/20
1875# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001876function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001877 local ip=$1
1878 local range=$2
1879 local masklen=${range#*/}
1880 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1881 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1882 [[ $network == $subnet ]]
1883}
1884
1885# Add a user to a group.
1886# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001887function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001888 local user=$1
1889 local group=$2
1890
Thomas Bechtolda8580852015-05-31 00:04:33 +02001891 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06001892}
1893
1894# Convert CIDR notation to a IPv4 netmask
1895# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001896function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001897 local maskpat="255 255 255 255"
1898 local maskdgt="254 252 248 240 224 192 128"
1899 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1900 echo ${1-0}.${2-0}.${3-0}.${4-0}
1901}
1902
1903# Gracefully cp only if source file/dir exists
1904# cp_it source destination
1905function cp_it {
1906 if [ -e $1 ] || [ -d $1 ]; then
1907 cp -pRL $1 $2
1908 fi
1909}
1910
1911# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1912# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1913# ``localrc`` or on the command line if necessary::
1914#
1915# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1916#
1917# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1918
Ian Wienandaee18c72014-02-21 15:35:08 +11001919function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001920 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001921 export http_proxy=$http_proxy
1922 fi
Sean Dague53753292014-12-04 19:38:15 -05001923 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001924 export https_proxy=$https_proxy
1925 fi
Sean Dague53753292014-12-04 19:38:15 -05001926 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001927 export no_proxy=$no_proxy
1928 fi
1929}
1930
1931# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001932function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001933 local mount_type=`stat -f -L -c %T $1`
1934 test "$mount_type" == "nfs"
1935}
1936
1937# Return the network portion of the given IP address using netmask
1938# netmask is in the traditional dotted-quad format
1939# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001940function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001941 local ip=$1
1942 local mask=$2
1943 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1944 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1945 echo $subnet
1946}
1947
Chris Dent3a2c86a2015-05-12 13:41:25 +00001948# Return the current python as "python<major>.<minor>"
1949function python_version {
1950 local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
1951 echo "python${python_version}"
1952}
1953
Dean Troyerdff49a22014-01-30 15:37:40 -06001954# Service wrapper to restart services
1955# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001956function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001957 if is_ubuntu; then
1958 sudo /usr/sbin/service $1 restart
1959 else
1960 sudo /sbin/service $1 restart
1961 fi
1962}
1963
1964# Only change permissions of a file or directory if it is not on an
1965# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001966function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001967 _safe_permission_operation chmod $@
1968}
1969
1970# Only change ownership of a file or directory if it is not on an NFS
1971# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001972function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001973 _safe_permission_operation chown $@
1974}
1975
1976# Service wrapper to start services
1977# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001978function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001979 if is_ubuntu; then
1980 sudo /usr/sbin/service $1 start
1981 else
1982 sudo /sbin/service $1 start
1983 fi
1984}
1985
1986# Service wrapper to stop services
1987# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001988function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001989 if is_ubuntu; then
1990 sudo /usr/sbin/service $1 stop
1991 else
1992 sudo /sbin/service $1 stop
1993 fi
1994}
1995
Sean Dague442e4e92015-06-24 13:24:02 -04001996# Test with a finite retry loop.
1997#
1998function test_with_retry {
1999 local testcmd=$1
2000 local failmsg=$2
2001 local until=${3:-10}
2002 local sleep=${4:-0.5}
2003
2004 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2005 die $LINENO "$failmsg"
2006 fi
2007}
2008
Dean Troyerdff49a22014-01-30 15:37:40 -06002009
2010# Restore xtrace
2011$XTRACE
2012
2013# Local variables:
2014# mode: shell-script
2015# End: