blob: 061a9356f594aaed40c195d7d18568aaaa292660 [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 \
49 LOGFILE OS_CACERT SERVICE_HOST SERVICE_PROTOCOL STACK_USER TLS_IP"
50
51
52# Saves significant environment variables to .stackenv for later use
53# Refers to a lot of globals, only TOP_DIR and STACK_ENV_VARS are required to
54# function, the rest are simply saved and do not cause problems if they are undefined.
55# save_stackenv [tag]
56function save_stackenv {
57 local tag=${1:-""}
58 # Save some values we generated for later use
59 time_stamp=$(date "+$TIMESTAMP_FORMAT")
60 echo "# $time_stamp $tag" >$TOP_DIR/.stackenv
61 for i in $STACK_ENV_VARS; do
62 echo $i=${!i} >>$TOP_DIR/.stackenv
63 done
64}
Dean Troyerdff49a22014-01-30 15:37:40 -060065
66# Normalize config values to True or False
67# Accepts as False: 0 no No NO false False FALSE
68# Accepts as True: 1 yes Yes YES true True TRUE
69# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +110070function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -050071 local xtrace=$(set +o | grep xtrace)
72 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060073
Mahito OGURA98f59aa2015-05-11 18:02:34 +090074 local default=$1
75 local testval=${!2:-}
76
77 case "$testval" in
78 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
79 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
80 * ) echo "$default" ;;
81 esac
82
Sean Dague45917cc2014-02-24 16:09:14 -050083 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060084}
85
Attila Fazekas1bd79592015-02-24 14:06:56 +010086function isset {
87 [[ -v "$1" ]]
88}
Dean Troyerdff49a22014-01-30 15:37:40 -060089
Dean Troyer68162342015-05-13 15:41:03 -050090
Dean Troyerdff49a22014-01-30 15:37:40 -060091# Control Functions
92# =================
93
94# Prints backtrace info
95# filename:lineno:function
96# backtrace level
97function backtrace {
98 local level=$1
99 local deep=$((${#BASH_SOURCE[@]} - 1))
100 echo "[Call Trace]"
101 while [ $level -le $deep ]; do
102 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
103 deep=$((deep - 1))
104 done
105}
106
107# Prints line number and "message" then exits
108# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100109function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600110 local exitcode=$?
111 set +o xtrace
112 local line=$1; shift
113 if [ $exitcode == 0 ]; then
114 exitcode=1
115 fi
116 backtrace 2
117 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600118 # Give buffers a second to flush
119 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600120 exit $exitcode
121}
122
123# Checks an environment variable is not set or has length 0 OR if the
124# exit code is non-zero and prints "message" and exits
125# NOTE: env-var is the variable name without a '$'
126# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100127function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600128 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500129 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600130 set +o xtrace
131 local line=$1; shift
132 local evar=$1; shift
133 if ! is_set $evar || [ $exitcode != 0 ]; then
134 die $line "$*"
135 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500136 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600137}
138
139# Prints line number and "message" in error format
140# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100141function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600142 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500143 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600144 set +o xtrace
145 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
146 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600147 if [[ -n ${LOGDIR} ]]; then
148 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600149 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500150 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600151 return $exitcode
152}
153
154# Checks an environment variable is not set or has length 0 OR if the
155# exit code is non-zero and prints "message"
156# NOTE: env-var is the variable name without a '$'
157# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100158function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600159 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500160 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600161 set +o xtrace
162 local line=$1; shift
163 local evar=$1; shift
164 if ! is_set $evar || [ $exitcode != 0 ]; then
165 err $line "$*"
166 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500167 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600168 return $exitcode
169}
170
171# Exit after outputting a message about the distribution not being supported.
172# exit_distro_not_supported [optional-string-telling-what-is-missing]
173function exit_distro_not_supported {
174 if [[ -z "$DISTRO" ]]; then
175 GetDistro
176 fi
177
178 if [ $# -gt 0 ]; then
179 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
180 else
181 die $LINENO "Support for $DISTRO is incomplete."
182 fi
183}
184
185# Test if the named environment variable is set and not zero length
186# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100187function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600188 local var=\$"$1"
189 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
190}
191
192# Prints line number and "message" in warning format
193# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100194function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600195 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500196 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600197 set +o xtrace
198 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Sean Daguee4af9292015-04-28 08:57:57 -0400199 echo $msg
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500200 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600201 return $exitcode
202}
203
204
205# Distro Functions
206# ================
207
208# Determine OS Vendor, Release and Update
209# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
210# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500211# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
212# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
213# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
214# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
215# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500216os_VENDOR=""
217os_RELEASE=""
218os_UPDATE=""
219os_PACKAGE=""
220os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500221
Dean Troyerdff49a22014-01-30 15:37:40 -0600222# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100223function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500224
Dean Troyerdff49a22014-01-30 15:37:40 -0600225 # Figure out which vendor we are
226 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
227 # OS/X
228 os_VENDOR=`sw_vers -productName`
229 os_RELEASE=`sw_vers -productVersion`
230 os_UPDATE=${os_RELEASE##*.}
231 os_RELEASE=${os_RELEASE%.*}
232 os_PACKAGE=""
233 if [[ "$os_RELEASE" =~ "10.7" ]]; then
234 os_CODENAME="lion"
235 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
236 os_CODENAME="snow leopard"
237 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
238 os_CODENAME="leopard"
239 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
240 os_CODENAME="tiger"
241 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
242 os_CODENAME="panther"
243 else
244 os_CODENAME=""
245 fi
246 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
247 os_VENDOR=$(lsb_release -i -s)
248 os_RELEASE=$(lsb_release -r -s)
249 os_UPDATE=""
250 os_PACKAGE="rpm"
251 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
252 os_PACKAGE="deb"
253 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
254 lsb_release -d -s | grep -q openSUSE
255 if [[ $? -eq 0 ]]; then
256 os_VENDOR="openSUSE"
257 fi
258 elif [[ $os_VENDOR == "openSUSE project" ]]; then
259 os_VENDOR="openSUSE"
260 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
261 os_VENDOR="Red Hat"
262 fi
263 os_CODENAME=$(lsb_release -c -s)
264 elif [[ -r /etc/redhat-release ]]; then
265 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
266 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
267 # CentOS release 5.5 (Final)
268 # CentOS Linux release 6.0 (Final)
269 # Fedora release 16 (Verne)
270 # XenServer release 6.2.0-70446c (xenenterprise)
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700271 # Oracle Linux release 7
Dean Troyerdff49a22014-01-30 15:37:40 -0600272 os_CODENAME=""
273 for r in "Red Hat" CentOS Fedora XenServer; do
274 os_VENDOR=$r
275 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
276 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
277 os_CODENAME=${ver#*|}
278 os_RELEASE=${ver%|*}
279 os_UPDATE=${os_RELEASE##*.}
280 os_RELEASE=${os_RELEASE%.*}
281 break
282 fi
283 os_VENDOR=""
284 done
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700285 if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
286 os_VENDOR=OracleLinux
287 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600288 os_PACKAGE="rpm"
289 elif [[ -r /etc/SuSE-release ]]; then
290 for r in openSUSE "SUSE Linux"; do
291 if [[ "$r" = "SUSE Linux" ]]; then
292 os_VENDOR="SUSE LINUX"
293 else
294 os_VENDOR=$r
295 fi
296
297 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
298 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
299 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
300 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
301 break
302 fi
303 os_VENDOR=""
304 done
305 os_PACKAGE="rpm"
306 # If lsb_release is not installed, we should be able to detect Debian OS
307 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
308 os_VENDOR="Debian"
309 os_PACKAGE="deb"
310 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
311 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
312 fi
313 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
314}
315
316# Translate the OS version values into common nomenclature
317# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500318declare DISTRO
319
Ian Wienandaee18c72014-02-21 15:35:08 +1100320function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600321 GetOSVersion
322 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
323 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
324 DISTRO=$os_CODENAME
325 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
326 # For Fedora, just use 'f' and the release
327 DISTRO="f$os_RELEASE"
328 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
329 DISTRO="opensuse-$os_RELEASE"
330 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
331 # For SLE, also use the service pack
332 if [[ -z "$os_UPDATE" ]]; then
333 DISTRO="sle${os_RELEASE}"
334 else
335 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
336 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530337 elif [[ "$os_VENDOR" =~ (Red Hat) || \
338 "$os_VENDOR" =~ (CentOS) || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700339 "$os_VENDOR" =~ (OracleLinux) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600340 # Drop the . release as we assume it's compatible
341 DISTRO="rhel${os_RELEASE::1}"
342 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
343 DISTRO="xs$os_RELEASE"
344 else
345 # Catch-all for now is Vendor + Release + Update
346 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
347 fi
348 export DISTRO
349}
350
351# Utility function for checking machine architecture
352# is_arch arch-type
353function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500354 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600355}
356
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700357# Determine if current distribution is an Oracle distribution
358# is_oraclelinux
359function is_oraclelinux {
360 if [[ -z "$os_VENDOR" ]]; then
361 GetOSVersion
362 fi
363
364 [ "$os_VENDOR" = "OracleLinux" ]
365}
366
367
Dean Troyerdff49a22014-01-30 15:37:40 -0600368# Determine if current distribution is a Fedora-based distribution
369# (Fedora, RHEL, CentOS, etc).
370# is_fedora
371function is_fedora {
372 if [[ -z "$os_VENDOR" ]]; then
373 GetOSVersion
374 fi
375
anju Tiwari6c639c92014-07-15 18:11:54 +0530376 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700377 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600378}
379
380
381# Determine if current distribution is a SUSE-based distribution
382# (openSUSE, SLE).
383# is_suse
384function is_suse {
385 if [[ -z "$os_VENDOR" ]]; then
386 GetOSVersion
387 fi
388
389 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
390}
391
392
393# Determine if current distribution is an Ubuntu-based distribution
394# It will also detect non-Ubuntu but Debian-based distros
395# is_ubuntu
396function is_ubuntu {
397 if [[ -z "$os_PACKAGE" ]]; then
398 GetOSVersion
399 fi
400 [ "$os_PACKAGE" = "deb" ]
401}
402
403
404# Git Functions
405# =============
406
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600407# Returns openstack release name for a given branch name
408# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100409function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600410 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700411 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600412 echo ${branch#*/}
413 else
414 echo "master"
415 fi
416}
417
Dean Troyerdff49a22014-01-30 15:37:40 -0600418# git clone only if directory doesn't exist already. Since ``DEST`` might not
419# be owned by the installation user, we create the directory and change the
420# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500421# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
422# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600423# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500424# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600425# git_clone remote dest-dir branch
426function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500427 local git_remote=$1
428 local git_dest=$2
429 local git_ref=$3
430 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200431 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500432
Sean Dague53753292014-12-04 19:38:15 -0500433 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800434 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200435 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
436 fi
437
Dean Troyerdff49a22014-01-30 15:37:40 -0600438 if [[ "$OFFLINE" = "True" ]]; then
439 echo "Running in offline mode, clones already exist"
440 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500441 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600442 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400443 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600444 return
445 fi
446
Dean Troyer50cda692014-07-25 11:57:20 -0500447 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600448 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500449 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600450 [[ "$ERROR_ON_CLONE" = "True" ]] && \
451 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200452 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600453 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500454 cd $git_dest
455 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600456 else
457 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500458 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600459 [[ "$ERROR_ON_CLONE" = "True" ]] && \
460 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200461 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500462 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600463 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500464 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600465 elif [[ "$RECLONE" = "True" ]]; then
466 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500467 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600468 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500469 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100470 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600471 # remove the existing ignored files (like pyc) as they cause breakage
472 # (due to the py files having older timestamps than our pyc, so python
473 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500474 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600475
Dean Troyer50cda692014-07-25 11:57:20 -0500476 # handle git_ref accordingly to type (tag, branch)
477 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
478 git_update_tag $git_ref
479 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
480 git_update_branch $git_ref
481 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
482 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600483 else
Dean Troyer50cda692014-07-25 11:57:20 -0500484 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600485 fi
486
487 fi
488 fi
489
490 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500491 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600492 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400493 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600494}
495
Sean Daguecc524062014-10-01 09:06:43 -0400496# A variation on git clone that lets us specify a project by it's
497# actual name, like oslo.config. This is exceptionally useful in the
498# library installation case
499function git_clone_by_name {
500 local name=$1
501 local repo=${GITREPO[$name]}
502 local dir=${GITDIR[$name]}
503 local branch=${GITBRANCH[$name]}
504 git_clone $repo $dir $branch
505}
506
507
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100508# git can sometimes get itself infinitely stuck with transient network
509# errors or other issues with the remote end. This wraps git in a
510# timeout/retry loop and is intended to watch over non-local git
511# processes that might hang. GIT_TIMEOUT, if set, is passed directly
512# to timeout(1); otherwise the default value of 0 maintains the status
513# quo of waiting forever.
514# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100515function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100516 local count=0
517 local timeout=0
518
519 if [[ -n "${GIT_TIMEOUT}" ]]; then
520 timeout=${GIT_TIMEOUT}
521 fi
522
523 until timeout -s SIGINT ${timeout} git "$@"; do
524 # 124 is timeout(1)'s special return code when it reached the
525 # timeout; otherwise assume fatal failure
526 if [[ $? -ne 124 ]]; then
527 die $LINENO "git call failed: [git $@]"
528 fi
529
530 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400531 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100532 if [ $count -eq 3 ]; then
533 die $LINENO "Maximum of 3 git retries reached"
534 fi
535 sleep 5
536 done
537}
538
Dean Troyerdff49a22014-01-30 15:37:40 -0600539# git update using reference as a branch.
540# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100541function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500542 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600543
Dean Troyer50cda692014-07-25 11:57:20 -0500544 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600545 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500546 git branch -D $git_branch || true
547 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600548}
549
550# git update using reference as a branch.
551# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100552function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500553 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600554
Dean Troyer50cda692014-07-25 11:57:20 -0500555 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600556}
557
558# git update using reference as a tag. Be careful editing source at that repo
559# as working copy will be in a detached mode
560# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100561function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500562 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600563
Dean Troyer50cda692014-07-25 11:57:20 -0500564 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600565 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500566 git_timed fetch origin tag $git_tag
567 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600568}
569
570
571# OpenStack Functions
572# ===================
573
574# Get the default value for HOST_IP
575# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100576function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600577 local fixed_range=$1
578 local floating_range=$2
579 local host_ip_iface=$3
580 local host_ip=$4
581
Dean Troyerdff49a22014-01-30 15:37:40 -0600582 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
583 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
584 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100585 # Find the interface used for the default route
586 host_ip_iface=${host_ip_iface:-$(ip route | awk '/default/ {print $5}' | head -1)}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500587 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
588 local ip
589 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600590 # Attempt to filter out IP addresses that are part of the fixed and
591 # floating range. Note that this method only works if the ``netaddr``
592 # python library is installed. If it is not installed, an error
593 # will be printed and the first IP from the interface will be used.
594 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
595 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500596 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
597 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600598 break;
599 fi
600 done
601 fi
602 echo $host_ip
603}
604
Attila Fazekasf71b5002014-05-28 09:52:22 +0200605# Generates hex string from ``size`` byte of pseudo random data
606# generate_hex_string size
607function generate_hex_string {
608 local size=$1
609 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
610}
611
Dean Troyerdff49a22014-01-30 15:37:40 -0600612# Grab a numbered field from python prettytable output
613# Fields are numbered starting with 1
614# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
615# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100616function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500617 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600618 while read data; do
619 if [ "$1" -lt 0 ]; then
620 field="(\$(NF$1))"
621 else
622 field="\$$(($1 + 1))"
623 fi
624 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
625 done
626}
627
yuntongjinf26deea2015-02-28 10:50:34 +0800628# install default policy
629# copy over a default policy.json and policy.d for projects
630function install_default_policy {
631 local project=$1
632 local project_uc=$(echo $1|tr a-z A-Z)
633 local conf_dir="${project_uc}_CONF_DIR"
634 # eval conf dir to get the variable
635 conf_dir="${!conf_dir}"
636 local project_dir="${project_uc}_DIR"
637 # eval project dir to get the variable
638 project_dir="${!project_dir}"
639 local sample_conf_dir="${project_dir}/etc/${project}"
640 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
641
642 # first copy any policy.json
643 cp -p $sample_conf_dir/policy.json $conf_dir
644 # then optionally copy over policy.d
645 if [[ -d $sample_policy_dir ]]; then
646 cp -r $sample_policy_dir $conf_dir/policy.d
647 fi
648}
649
Dean Troyerdff49a22014-01-30 15:37:40 -0600650# Add a policy to a policy.json file
651# Do nothing if the policy already exists
652# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100653function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600654 local policy_file=$1
655 local policy_name=$2
656 local policy_perm=$3
657
658 if grep -q ${policy_name} ${policy_file}; then
659 echo "Policy ${policy_name} already exists in ${policy_file}"
660 return
661 fi
662
663 # Add a terminating comma to policy lines without one
664 # Remove the closing '}' and all lines following to the end-of-file
665 local tmpfile=$(mktemp)
666 uniq ${policy_file} | sed -e '
667 s/]$/],/
668 /^[}]/,$d
669 ' > ${tmpfile}
670
671 # Append policy and closing brace
672 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
673 echo "}" >>${tmpfile}
674
675 mv ${tmpfile} ${policy_file}
676}
677
Alistair Coles24779f62014-10-15 18:57:59 +0100678# Gets or creates a domain
679# Usage: get_or_create_domain <name> <description>
680function get_or_create_domain {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500681 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100682 # Gets domain id
683 local domain_id=$(
684 # Gets domain id
685 openstack --os-token=$OS_TOKEN --os-url=$os_url \
686 --os-identity-api-version=3 domain show $1 \
687 -f value -c id 2>/dev/null ||
688 # Creates new domain
689 openstack --os-token=$OS_TOKEN --os-url=$os_url \
690 --os-identity-api-version=3 domain create $1 \
691 --description "$2" \
692 -f value -c id
693 )
694 echo $domain_id
695}
696
Steve Martinellib74e01c2014-12-18 01:35:35 -0500697# Gets or creates group
698# Usage: get_or_create_group <groupname> [<domain> <description>]
699function get_or_create_group {
700 local domain=${2:+--domain ${2}}
701 local desc="${3:-}"
702 local os_url="$KEYSTONE_SERVICE_URI_V3"
703 # Gets group id
704 local group_id=$(
705 # Creates new group with --or-show
706 openstack --os-token=$OS_TOKEN --os-url=$os_url \
707 --os-identity-api-version=3 group create $1 \
708 $domain --description "$desc" --or-show \
709 -f value -c id
710 )
711 echo $group_id
712}
713
Bartosz Górski0abde392014-02-28 14:15:19 +0100714# Gets or creates user
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000715# Usage: get_or_create_user <username> <password> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100716function get_or_create_user {
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000717 if [[ ! -z "$3" ]]; then
718 local email="--email=$3"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200719 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500720 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200721 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100722 local os_cmd="openstack"
723 local domain=""
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000724 if [[ ! -z "$4" ]]; then
725 domain="--domain=$4"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500726 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100727 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100728 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500729 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500730 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100731 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100732 $1 \
733 --password "$2" \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500734 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100735 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500736 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100737 -f value -c id
738 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500739 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100740}
741
742# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100743# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100744function get_or_create_project {
745 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100746 local os_cmd="openstack"
747 local domain=""
748 if [[ ! -z "$2" ]]; then
749 domain="--domain=$2"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500750 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100751 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500752 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500753 # Creates new project with --or-show
754 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100755 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500756 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100757}
758
759# Gets or creates role
760# Usage: get_or_create_role <name>
761function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500762 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500763 # Creates role with --or-show
764 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100765 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500766 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100767}
768
Jamie Lennox9b215db2015-02-10 18:19:57 +1100769# Gets or adds user role to project
770# Usage: get_or_add_user_project_role <role> <user> <project>
771function get_or_add_user_project_role {
Bartosz Górski0abde392014-02-28 14:15:19 +0100772 # Gets user role id
Steve Martinelli5541a612015-01-19 15:58:49 -0500773 local user_role_id=$(openstack role list \
774 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100775 --project $3 \
776 --column "ID" \
777 --column "Name" \
778 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500779 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100780 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500781 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100782 $1 \
783 --user $2 \
784 --project $3 \
785 | grep " id " | get_field 2)
786 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500787 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100788}
789
Steve Martinelli4599fd12015-03-12 21:30:58 -0400790# Gets or adds group role to project
791# Usage: get_or_add_group_project_role <role> <group> <project>
792function get_or_add_group_project_role {
793 # Gets group role id
794 local group_role_id=$(openstack role list \
795 --group $2 \
796 --project $3 \
797 --column "ID" \
798 --column "Name" \
799 | grep " $1 " | get_field 1)
800 if [[ -z "$group_role_id" ]]; then
801 # Adds role to group
802 group_role_id=$(openstack role add \
803 $1 \
804 --group $2 \
805 --project $3 \
806 | grep " id " | get_field 2)
807 fi
808 echo $group_role_id
809}
810
Bartosz Górski0abde392014-02-28 14:15:19 +0100811# Gets or creates service
812# Usage: get_or_create_service <name> <type> <description>
813function get_or_create_service {
814 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500815 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100816 # Gets service id
817 openstack service show $1 -f value -c id 2>/dev/null ||
818 # Creates new service if not exists
819 openstack service create \
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
828# Gets or creates endpoint
829# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
830function get_or_create_endpoint {
831 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500832 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100833 --column "ID" \
834 --column "Region" \
835 --column "Service Name" \
836 | grep " $2 " \
837 | grep " $1 " | get_field 1)
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 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100841 $1 \
842 --region $2 \
843 --publicurl $3 \
844 --adminurl $4 \
845 --internalurl $5 \
846 | grep " id " | get_field 2)
847 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500848 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100849}
Dean Troyerdff49a22014-01-30 15:37:40 -0600850
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500851
Dean Troyerdff49a22014-01-30 15:37:40 -0600852# Package Functions
853# =================
854
855# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100856function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800857 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600858 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800859
860 if [[ -z "$base_dir" ]]; then
861 base_dir=$FILES
862 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600863 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800864 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600865 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800866 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600867 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800868 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600869 else
870 exit_distro_not_supported "list of packages"
871 fi
872 echo "$pkg_dir"
873}
874
875# Wrapper for ``apt-get`` to set cache and proxy environment variables
876# Uses globals ``OFFLINE``, ``*_proxy``
877# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100878function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500879 local xtrace=$(set +o | grep xtrace)
880 set +o xtrace
881
Dean Troyerdff49a22014-01-30 15:37:40 -0600882 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
883 local sudo="sudo"
884 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500885
886 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500887
Dean Troyerdff49a22014-01-30 15:37:40 -0600888 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500889 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
890 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600891 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
892}
893
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800894function _parse_package_files {
895 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600896
Dean Troyerdff49a22014-01-30 15:37:40 -0600897 if [[ -z "$DISTRO" ]]; then
898 GetDistro
899 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600900
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800901 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600902 local OIFS line package distros distro
903 [[ -e $fname ]] || continue
904
905 OIFS=$IFS
906 IFS=$'\n'
907 for line in $(<${fname}); do
908 if [[ $line =~ "NOPRIME" ]]; then
909 continue
910 fi
911
912 # Assume we want this package
913 package=${line%#*}
914 inst_pkg=1
915
916 # Look for # dist:xxx in comment
917 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
918 # We are using BASH regexp matching feature.
919 package=${BASH_REMATCH[1]}
920 distros=${BASH_REMATCH[2]}
921 # In bash ${VAR,,} will lowecase VAR
922 # Look for a match in the distro list
923 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
924 # If no match then skip this package
925 inst_pkg=0
926 fi
927 fi
928
Dean Troyerdff49a22014-01-30 15:37:40 -0600929 if [[ $inst_pkg = 1 ]]; then
930 echo $package
931 fi
932 done
933 IFS=$OIFS
934 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800935}
936
937# get_packages() collects a list of package names of any type from the
938# prerequisite files in ``files/{debs|rpms}``. The list is intended
939# to be passed to a package installer such as apt or yum.
940#
941# Only packages required for the services in 1st argument will be
942# included. Two bits of metadata are recognized in the prerequisite files:
943#
944# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
945# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
946# of the package to the distros listed. The distro names are case insensitive.
947function get_packages {
948 local xtrace=$(set +o | grep xtrace)
949 set +o xtrace
950 local services=$@
951 local package_dir=$(_get_package_dir)
952 local file_to_parse=""
953 local service=""
954
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800955 if [[ -z "$package_dir" ]]; then
956 echo "No package directory supplied"
957 return 1
958 fi
959 for service in ${services//,/ }; do
960 # Allow individual services to specify dependencies
961 if [[ -e ${package_dir}/${service} ]]; then
962 file_to_parse="${file_to_parse} ${package_dir}/${service}"
963 fi
964 # NOTE(sdague) n-api needs glance for now because that's where
965 # glance client is
966 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700967 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800968 file_to_parse="${file_to_parse} ${package_dir}/nova"
969 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700970 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800971 file_to_parse="${file_to_parse} ${package_dir}/glance"
972 fi
973 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700974 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800975 file_to_parse="${file_to_parse} ${package_dir}/cinder"
976 fi
977 elif [[ $service == ceilometer-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700978 if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800979 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
980 fi
981 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700982 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800983 file_to_parse="${file_to_parse} ${package_dir}/swift"
984 fi
985 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700986 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800987 file_to_parse="${file_to_parse} ${package_dir}/nova"
988 fi
989 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700990 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800991 file_to_parse="${file_to_parse} ${package_dir}/glance"
992 fi
993 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700994 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800995 file_to_parse="${file_to_parse} ${package_dir}/keystone"
996 fi
997 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700998 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800999 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1000 fi
1001 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001002 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001003 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1004 fi
1005 fi
1006 done
1007 echo "$(_parse_package_files $file_to_parse)"
1008 $xtrace
1009}
1010
1011# get_plugin_packages() collects a list of package names of any type from a
1012# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1013# list is intended to be passed to a package installer such as apt or yum.
1014#
1015# Only packages required for enabled and collected plugins will included.
1016#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001017# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001018# in these prerequisite files, see get_packages() for more info.
1019function get_plugin_packages {
1020 local xtrace=$(set +o | grep xtrace)
1021 set +o xtrace
1022 local files_to_parse=""
1023 local package_dir=""
1024 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1025 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1026 files_to_parse+="$package_dir/$plugin"
1027 done
1028 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001029 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001030}
1031
1032# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001033# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001034# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001035function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001036 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1037 REPOS_UPDATED=${REPOS_UPDATED:-False}
1038 RETRY_UPDATE=${RETRY_UPDATE:-False}
1039
Paul Linchpiner9e179742014-07-13 22:23:00 -07001040 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001041 return 0
1042 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001043
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001044 if is_ubuntu; then
1045 local xtrace=$(set +o | grep xtrace)
1046 set +o xtrace
1047 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1048 # if there are transient errors pulling the updates, that's fine.
1049 # It may be secondary repositories that we don't really care about.
1050 apt_get update || /bin/true
1051 REPOS_UPDATED=True
1052 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001053 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001054 fi
1055}
1056
1057function real_install_package {
1058 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001059 apt_get install "$@"
1060 elif is_fedora; then
1061 yum_install "$@"
1062 elif is_suse; then
1063 zypper_install "$@"
1064 else
1065 exit_distro_not_supported "installing packages"
1066 fi
1067}
1068
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001069# Distro-agnostic package installer
1070# install_package package [package ...]
1071function install_package {
1072 update_package_repo
1073 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1074}
1075
Dean Troyerdff49a22014-01-30 15:37:40 -06001076# Distro-agnostic function to tell if a package is installed
1077# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001078function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001079 if [[ -z "$@" ]]; then
1080 return 1
1081 fi
1082
1083 if [[ -z "$os_PACKAGE" ]]; then
1084 GetOSVersion
1085 fi
1086
1087 if [[ "$os_PACKAGE" = "deb" ]]; then
1088 dpkg -s "$@" > /dev/null 2> /dev/null
1089 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1090 rpm --quiet -q "$@"
1091 else
1092 exit_distro_not_supported "finding if a package is installed"
1093 fi
1094}
1095
1096# Distro-agnostic package uninstaller
1097# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001098function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001099 if is_ubuntu; then
1100 apt_get purge "$@"
1101 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001102 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001103 elif is_suse; then
1104 sudo zypper rm "$@"
1105 else
1106 exit_distro_not_supported "uninstalling packages"
1107 fi
1108}
1109
1110# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001111# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001112# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001113function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001114 [[ "$OFFLINE" = "True" ]] && return
1115 local sudo="sudo"
1116 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001117
1118 # The manual check for missing packages is because yum -y assumes
1119 # missing packages are OK. See
1120 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001121 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1122 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001123 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001124 awk '
1125 BEGIN { fail=0 }
1126 /No package/ { fail=1 }
1127 { print }
1128 END { exit fail }' || \
1129 die $LINENO "Missing packages detected"
1130
1131 # also ensure we catch a yum failure
1132 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001133 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001134 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001135}
1136
1137# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001138# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001139# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001140function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001141 [[ "$OFFLINE" = "True" ]] && return
1142 local sudo="sudo"
1143 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001144 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1145 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001146 zypper --non-interactive install --auto-agree-with-licenses "$@"
1147}
1148
1149
1150# Process Functions
1151# =================
1152
1153# _run_process() is designed to be backgrounded by run_process() to simulate a
1154# fork. It includes the dirty work of closing extra filehandles and preparing log
1155# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001156# from the service name.
1157# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001158# If an optional group is provided sg will be used to set the group of
1159# the command.
1160# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001161function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001162 # disable tracing through the exec redirects, it's just confusing in the logs.
1163 xtrace=$(set +o | grep xtrace)
1164 set +o xtrace
1165
Dean Troyerdff49a22014-01-30 15:37:40 -06001166 local service=$1
1167 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001168 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001169
1170 # Undo logging redirections and close the extra descriptors
1171 exec 1>&3
1172 exec 2>&3
1173 exec 3>&-
1174 exec 6>&-
1175
Dean Troyerdde41d02014-12-09 17:47:57 -06001176 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1177 if [[ -n ${LOGDIR} ]]; then
1178 exec 1>&"$real_logfile" 2>&1
1179 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1180 if [[ -n ${SCREEN_LOGDIR} ]]; then
1181 # Drop the backward-compat symlink
1182 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1183 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001184
1185 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1186 export PYTHONUNBUFFERED=1
1187 fi
1188
Sean Dague6e137ab2015-04-29 08:22:24 -04001189 # reenable xtrace before we do *real* work
1190 $xtrace
1191
Dean Troyer3159a822014-08-27 14:13:58 -05001192 # Run under ``setsid`` to force the process to become a session and group leader.
1193 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001194 if [[ -n "$group" ]]; then
1195 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1196 else
1197 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1198 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001199
1200 # Just silently exit this process
1201 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001202}
1203
1204# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1205# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001206# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001207# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001208function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001209 SCREEN_NAME=${SCREEN_NAME:-stack}
1210 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1211
1212 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1213 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1214 fi
1215
1216 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1217}
1218
1219# Find out if a process exists by partial name.
1220# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001221function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001222 local name=$1
1223 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001224 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001225 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001226 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001227}
1228
Dean Troyer3159a822014-08-27 14:13:58 -05001229# Run a single service under screen or directly
1230# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001231# If an optional group is provided sg will be used to run the
1232# command as that group.
1233# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001234function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001235 local service=$1
1236 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001237 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001238
Dean Troyer3159a822014-08-27 14:13:58 -05001239 if is_service_enabled $service; then
1240 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001241 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001242 else
1243 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001244 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001245 fi
1246 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001247}
1248
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001249# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001250# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001251# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001252# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001253# Run a command in a shell in a screen window, if an optional group
1254# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001255function screen_process {
1256 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001257 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001258 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001259
Sean Dagueea22a4f2014-06-27 15:21:41 -04001260 SCREEN_NAME=${SCREEN_NAME:-stack}
1261 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001262 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001263
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001264 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001265
Dean Troyerdde41d02014-12-09 17:47:57 -06001266 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1267 echo "LOGDIR: $LOGDIR"
1268 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1269 echo "log: $real_logfile"
1270 if [[ -n ${LOGDIR} ]]; then
1271 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001272 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001273 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1274 if [[ -n ${SCREEN_LOGDIR} ]]; then
1275 # Drop the backward-compat symlink
1276 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1277 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001278 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001279
1280 # sleep to allow bash to be ready to be send the command - we are
1281 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001282 # bash isn't running by the time we send the command, nothing
1283 # happens. This sleep was added originally to handle gate runs
1284 # where we needed this to be at least 3 seconds to pass
1285 # consistently on slow clouds. Now this is configurable so that we
1286 # can determine a reasonable value for the local case which should
1287 # be much smaller.
1288 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001289
1290 NL=`echo -ne '\015'`
1291 # This fun command does the following:
1292 # - the passed server command is backgrounded
1293 # - the pid of the background process is saved in the usual place
1294 # - the server process is brought back to the foreground
1295 # - if the server process exits prematurely the fg command errors
1296 # and a message is written to stdout and the process failure file
1297 #
1298 # The pid saved can be used in stop_process() as a process group
1299 # id to kill off all child processes
1300 if [[ -n "$group" ]]; then
1301 command="sg $group '$command'"
1302 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001303
1304 # Append the process to the screen rc file
1305 screen_rc "$name" "$command"
1306
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001307 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 -06001308}
1309
1310# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001311# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001312# screen_rc service "command-line"
1313function screen_rc {
1314 SCREEN_NAME=${SCREEN_NAME:-stack}
1315 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1316 if [[ ! -e $SCREENRC ]]; then
1317 # Name the screen session
1318 echo "sessionname $SCREEN_NAME" > $SCREENRC
1319 # Set a reasonable statusbar
1320 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1321 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1322 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1323 echo "screen -t shell bash" >> $SCREENRC
1324 fi
1325 # If this service doesn't already exist in the screenrc file
1326 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1327 NL=`echo -ne '\015'`
1328 echo "screen -t $1 bash" >> $SCREENRC
1329 echo "stuff \"$2$NL\"" >> $SCREENRC
1330
Dean Troyerdde41d02014-12-09 17:47:57 -06001331 if [[ -n ${LOGDIR} ]]; then
1332 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001333 echo "log on" >>$SCREENRC
1334 fi
1335 fi
1336}
1337
1338# Stop a service in screen
1339# If a PID is available use it, kill the whole process group via TERM
1340# If screen is being used kill the screen window; this will catch processes
1341# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001342# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001343# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001344function screen_stop_service {
1345 local service=$1
1346
Dean Troyerdff49a22014-01-30 15:37:40 -06001347 SCREEN_NAME=${SCREEN_NAME:-stack}
1348 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001349 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001350
Dean Troyer3159a822014-08-27 14:13:58 -05001351 if is_service_enabled $service; then
1352 # Clean up the screen window
1353 screen -S $SCREEN_NAME -p $service -X kill
1354 fi
1355}
1356
1357# Stop a service process
1358# If a PID is available use it, kill the whole process group via TERM
1359# If screen is being used kill the screen window; this will catch processes
1360# that did not leave a PID behind
1361# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1362# stop_process service
1363function stop_process {
1364 local service=$1
1365
1366 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001367 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001368
1369 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001370 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001371 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1372 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1373 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001374 fi
1375 if [[ "$USE_SCREEN" = "True" ]]; then
1376 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001377 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001378 fi
1379 fi
1380}
1381
1382# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001383# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001384# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001385function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001386 local service
1387 local failures
1388 SCREEN_NAME=${SCREEN_NAME:-stack}
1389 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1390
1391
1392 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1393 echo "No service status directory found"
1394 return
1395 fi
1396
1397 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001398 # make this -o errexit safe
1399 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001400
1401 for service in $failures; do
1402 service=`basename $service`
1403 service=${service%.failure}
1404 echo "Error: Service $service is not running"
1405 done
1406
1407 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001408 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001409 fi
1410}
1411
Chris Dent2f27a0e2014-09-09 13:46:02 +01001412# Tail a log file in a screen if USE_SCREEN is true.
1413function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001414 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001415 local logfile=$2
1416
Sean Dague53753292014-12-04 19:38:15 -05001417 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001418 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001419 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001420 fi
1421}
1422
Dean Troyerdff49a22014-01-30 15:37:40 -06001423
Dean Troyer3159a822014-08-27 14:13:58 -05001424# Deprecated Functions
1425# --------------------
1426
1427# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1428# fork. It includes the dirty work of closing extra filehandles and preparing log
1429# files to produce the same logs as screen_it(). The log filename is derived
1430# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1431# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1432# _old_run_process service "command-line"
1433function _old_run_process {
1434 local service=$1
1435 local command="$2"
1436
1437 # Undo logging redirections and close the extra descriptors
1438 exec 1>&3
1439 exec 2>&3
1440 exec 3>&-
1441 exec 6>&-
1442
1443 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001444 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1445 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001446
1447 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1448 export PYTHONUNBUFFERED=1
1449 fi
1450
1451 exec /bin/bash -c "$command"
1452 die "$service exec failure: $command"
1453}
1454
1455# old_run_process() launches a child process that closes all file descriptors and
1456# then exec's the passed in command. This is meant to duplicate the semantics
1457# of screen_it() without screen. PIDs are written to
1458# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1459# old_run_process service "command-line"
1460function old_run_process {
1461 local service=$1
1462 local command="$2"
1463
1464 # Spawn the child process
1465 _old_run_process "$service" "$command" &
1466 echo $!
1467}
1468
1469# Compatibility for existing start_XXXX() functions
1470# Uses global ``USE_SCREEN``
1471# screen_it service "command-line"
1472function screen_it {
1473 if is_service_enabled $1; then
1474 # Append the service to the screen rc file
1475 screen_rc "$1" "$2"
1476
1477 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001478 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001479 else
1480 # Spawn directly without screen
1481 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1482 fi
1483 fi
1484}
1485
1486# Compatibility for existing stop_XXXX() functions
1487# Stop a service in screen
1488# If a PID is available use it, kill the whole process group via TERM
1489# If screen is being used kill the screen window; this will catch processes
1490# that did not leave a PID behind
1491# screen_stop service
1492function screen_stop {
1493 # Clean up the screen window
1494 stop_process $1
1495}
1496
1497
Sean Dague2c65e712014-12-18 09:44:56 -05001498# Plugin Functions
1499# =================
1500
1501DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1502
1503# enable_plugin <name> <url> [branch]
1504#
1505# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1506# ``url`` is a git url
1507# ``branch`` is a gitref. If it's not set, defaults to master
1508function enable_plugin {
1509 local name=$1
1510 local url=$2
1511 local branch=${3:-master}
1512 DEVSTACK_PLUGINS+=",$name"
1513 GITREPO[$name]=$url
1514 GITDIR[$name]=$DEST/$name
1515 GITBRANCH[$name]=$branch
1516}
1517
1518# fetch_plugins
1519#
1520# clones all plugins
1521function fetch_plugins {
1522 local plugins="${DEVSTACK_PLUGINS}"
1523 local plugin
1524
1525 # short circuit if nothing to do
1526 if [[ -z $plugins ]]; then
1527 return
1528 fi
1529
Dean Troyerdc97cb72015-03-28 08:20:50 -05001530 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001531 for plugin in ${plugins//,/ }; do
1532 git_clone_by_name $plugin
1533 done
1534}
1535
1536# load_plugin_settings
1537#
1538# Load settings from plugins in the order that they were registered
1539function load_plugin_settings {
1540 local plugins="${DEVSTACK_PLUGINS}"
1541 local plugin
1542
1543 # short circuit if nothing to do
1544 if [[ -z $plugins ]]; then
1545 return
1546 fi
1547
1548 echo "Loading plugin settings"
1549 for plugin in ${plugins//,/ }; do
1550 local dir=${GITDIR[$plugin]}
1551 # source any known settings
1552 if [[ -f $dir/devstack/settings ]]; then
1553 source $dir/devstack/settings
1554 fi
1555 done
1556}
1557
Sean Dague6e275e12015-03-26 05:54:28 -04001558# plugin_override_defaults
1559#
1560# Run an extremely early setting phase for plugins that allows default
1561# overriding of services.
1562function plugin_override_defaults {
1563 local plugins="${DEVSTACK_PLUGINS}"
1564 local plugin
1565
1566 # short circuit if nothing to do
1567 if [[ -z $plugins ]]; then
1568 return
1569 fi
1570
1571 echo "Overriding Configuration Defaults"
1572 for plugin in ${plugins//,/ }; do
1573 local dir=${GITDIR[$plugin]}
1574 # source any overrides
1575 if [[ -f $dir/devstack/override-defaults ]]; then
1576 # be really verbose that an override is happening, as it
1577 # may not be obvious if things fail later.
1578 echo "$plugin has overriden the following defaults"
1579 cat $dir/devstack/override-defaults
1580 source $dir/devstack/override-defaults
1581 fi
1582 done
1583}
1584
Sean Dague2c65e712014-12-18 09:44:56 -05001585# run_plugins
1586#
1587# Run the devstack/plugin.sh in all the plugin directories. These are
1588# run in registration order.
1589function run_plugins {
1590 local mode=$1
1591 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301592
1593 local plugins="${DEVSTACK_PLUGINS}"
1594 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001595 for plugin in ${plugins//,/ }; do
1596 local dir=${GITDIR[$plugin]}
1597 if [[ -f $dir/devstack/plugin.sh ]]; then
1598 source $dir/devstack/plugin.sh $mode $phase
1599 fi
1600 done
1601}
1602
1603function run_phase {
1604 local mode=$1
1605 local phase=$2
1606 if [[ -d $TOP_DIR/extras.d ]]; then
1607 for i in $TOP_DIR/extras.d/*.sh; do
1608 [[ -r $i ]] && source $i $mode $phase
1609 done
1610 fi
1611 # the source phase corresponds to settings loading in plugins
1612 if [[ "$mode" == "source" ]]; then
1613 load_plugin_settings
Sean Dague6e275e12015-03-26 05:54:28 -04001614 elif [[ "$mode" == "override_defaults" ]]; then
1615 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001616 else
1617 run_plugins $mode $phase
1618 fi
1619}
1620
Dean Troyerdff49a22014-01-30 15:37:40 -06001621
1622# Service Functions
1623# =================
1624
1625# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1626# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001627function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001628 echo "$1" | sed -e '
1629 s/,,/,/g;
1630 s/^,//;
1631 s/,$//
1632 '
1633}
1634
1635# disable_all_services() removes all current services
1636# from ``ENABLED_SERVICES`` to reset the configuration
1637# before a minimal installation
1638# Uses global ``ENABLED_SERVICES``
1639# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001640function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001641 ENABLED_SERVICES=""
1642}
1643
1644# Remove all services starting with '-'. For example, to install all default
1645# services except rabbit (rabbit) set in ``localrc``:
1646# ENABLED_SERVICES+=",-rabbit"
1647# Uses global ``ENABLED_SERVICES``
1648# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001649function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001650 local to_remove=""
1651 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001652 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001653
1654 # build up list of services that should be removed; i.e. they
1655 # begin with "-"
1656 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001657 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001658 to_remove+=",${service#-}"
1659 else
1660 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001661 fi
1662 done
Ian Wienand2796a822015-04-15 08:59:04 +10001663
1664 # go through the service list. if this service appears in the "to
1665 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001666 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Dean Troyerdff49a22014-01-30 15:37:40 -06001667}
1668
1669# disable_service() removes the services passed as argument to the
1670# ``ENABLED_SERVICES`` list, if they are present.
1671#
1672# For example:
1673# disable_service rabbit
1674#
1675# This function does not know about the special cases
1676# for nova, glance, and neutron built into is_service_enabled().
1677# Uses global ``ENABLED_SERVICES``
1678# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001679function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001680 local tmpsvcs=",${ENABLED_SERVICES},"
1681 local service
1682 for service in $@; do
1683 if is_service_enabled $service; then
1684 tmpsvcs=${tmpsvcs//,$service,/,}
1685 fi
1686 done
1687 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1688}
1689
1690# enable_service() adds the services passed as argument to the
1691# ``ENABLED_SERVICES`` list, if they are not already present.
1692#
1693# For example:
1694# enable_service qpid
1695#
1696# This function does not know about the special cases
1697# for nova, glance, and neutron built into is_service_enabled().
1698# Uses global ``ENABLED_SERVICES``
1699# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001700function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001701 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001702 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001703 for service in $@; do
1704 if ! is_service_enabled $service; then
1705 tmpsvcs+=",$service"
1706 fi
1707 done
1708 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1709 disable_negated_services
1710}
1711
1712# is_service_enabled() checks if the service(s) specified as arguments are
1713# enabled by the user in ``ENABLED_SERVICES``.
1714#
1715# Multiple services specified as arguments are ``OR``'ed together; the test
1716# is a short-circuit boolean, i.e it returns on the first match.
1717#
1718# There are special cases for some 'catch-all' services::
1719# **nova** returns true if any service enabled start with **n-**
1720# **cinder** returns true if any service enabled start with **c-**
1721# **ceilometer** returns true if any service enabled start with **ceilometer**
1722# **glance** returns true if any service enabled start with **g-**
1723# **neutron** returns true if any service enabled start with **q-**
1724# **swift** returns true if any service enabled start with **s-**
1725# **trove** returns true if any service enabled start with **tr-**
1726# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1727# **s-** services will be enabled. This will be deprecated in the future.
1728#
1729# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1730# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1731# as enabled in this case.
1732#
1733# Uses global ``ENABLED_SERVICES``
1734# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001735function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001736 local xtrace=$(set +o | grep xtrace)
1737 set +o xtrace
1738 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001739 local services=$@
1740 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001741 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001742 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001743
1744 # Look for top-level 'enabled' function for this service
1745 if type is_${service}_enabled >/dev/null 2>&1; then
1746 # A function exists for this service, use it
1747 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001748 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001749 fi
1750
1751 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1752 # are implemented
1753
Sean Dague45917cc2014-02-24 16:09:14 -05001754 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001755 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001756 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1757 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1758 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1759 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1760 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1761 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1762 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1763 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1764 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001765 done
Sean Dague45917cc2014-02-24 16:09:14 -05001766 $xtrace
1767 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001768}
1769
fumihiko kakuma8606c982015-04-13 09:55:06 +09001770# remove specified list from the input string
1771# remove_disabled_services service-list remove-list
1772function remove_disabled_services {
1773 local service_list=$1
1774 local remove_list=$2
1775 local service
1776 local enabled=""
1777
1778 for service in ${service_list//,/ }; do
1779 local remove
1780 local add=1
1781 for remove in ${remove_list//,/ }; do
1782 if [[ ${remove} == ${service} ]]; then
1783 add=0
1784 break
1785 fi
1786 done
1787 if [[ $add == 1 ]]; then
1788 enabled="${enabled},$service"
1789 fi
1790 done
1791 _cleanup_service_list "$enabled"
1792}
1793
Dean Troyerdff49a22014-01-30 15:37:40 -06001794# Toggle enable/disable_service for services that must run exclusive of each other
1795# $1 The name of a variable containing a space-separated list of services
1796# $2 The name of a variable in which to store the enabled service's name
1797# $3 The name of the service to enable
1798function use_exclusive_service {
1799 local options=${!1}
1800 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001801 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001802 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001803 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001804 for opt in $options;do
1805 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1806 done
1807 eval "$out=$selection"
1808 return 0
1809}
1810
1811
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001812# System Functions
1813# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001814
1815# Only run the command if the target file (the last arg) is not on an
1816# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001817function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001818 local xtrace=$(set +o | grep xtrace)
1819 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001820 local args=( $@ )
1821 local last
1822 local sudo_cmd
1823 local dir_to_check
1824
1825 let last="${#args[*]} - 1"
1826
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001827 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001828 if [ ! -d "$dir_to_check" ]; then
1829 dir_to_check=`dirname "$dir_to_check"`
1830 fi
1831
1832 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001833 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001834 return 0
1835 fi
1836
1837 if [[ $TRACK_DEPENDS = True ]]; then
1838 sudo_cmd="env"
1839 else
1840 sudo_cmd="sudo"
1841 fi
1842
Sean Dague45917cc2014-02-24 16:09:14 -05001843 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001844 $sudo_cmd $@
1845}
1846
1847# Exit 0 if address is in network or 1 if address is not in network
1848# ip-range is in CIDR notation: 1.2.3.4/20
1849# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001850function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001851 local ip=$1
1852 local range=$2
1853 local masklen=${range#*/}
1854 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1855 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1856 [[ $network == $subnet ]]
1857}
1858
1859# Add a user to a group.
1860# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001861function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001862 local user=$1
1863 local group=$2
1864
Thomas Bechtolda8580852015-05-31 00:04:33 +02001865 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06001866}
1867
1868# Convert CIDR notation to a IPv4 netmask
1869# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001870function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001871 local maskpat="255 255 255 255"
1872 local maskdgt="254 252 248 240 224 192 128"
1873 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1874 echo ${1-0}.${2-0}.${3-0}.${4-0}
1875}
1876
1877# Gracefully cp only if source file/dir exists
1878# cp_it source destination
1879function cp_it {
1880 if [ -e $1 ] || [ -d $1 ]; then
1881 cp -pRL $1 $2
1882 fi
1883}
1884
1885# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1886# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1887# ``localrc`` or on the command line if necessary::
1888#
1889# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1890#
1891# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1892
Ian Wienandaee18c72014-02-21 15:35:08 +11001893function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001894 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001895 export http_proxy=$http_proxy
1896 fi
Sean Dague53753292014-12-04 19:38:15 -05001897 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001898 export https_proxy=$https_proxy
1899 fi
Sean Dague53753292014-12-04 19:38:15 -05001900 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001901 export no_proxy=$no_proxy
1902 fi
1903}
1904
1905# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001906function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001907 local mount_type=`stat -f -L -c %T $1`
1908 test "$mount_type" == "nfs"
1909}
1910
1911# Return the network portion of the given IP address using netmask
1912# netmask is in the traditional dotted-quad format
1913# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001914function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001915 local ip=$1
1916 local mask=$2
1917 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1918 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1919 echo $subnet
1920}
1921
Chris Dent3a2c86a2015-05-12 13:41:25 +00001922# Return the current python as "python<major>.<minor>"
1923function python_version {
1924 local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
1925 echo "python${python_version}"
1926}
1927
Dean Troyerdff49a22014-01-30 15:37:40 -06001928# Service wrapper to restart services
1929# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001930function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001931 if is_ubuntu; then
1932 sudo /usr/sbin/service $1 restart
1933 else
1934 sudo /sbin/service $1 restart
1935 fi
1936}
1937
1938# Only change permissions of a file or directory if it is not on an
1939# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001940function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001941 _safe_permission_operation chmod $@
1942}
1943
1944# Only change ownership of a file or directory if it is not on an NFS
1945# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001946function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001947 _safe_permission_operation chown $@
1948}
1949
1950# Service wrapper to start services
1951# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001952function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001953 if is_ubuntu; then
1954 sudo /usr/sbin/service $1 start
1955 else
1956 sudo /sbin/service $1 start
1957 fi
1958}
1959
1960# Service wrapper to stop services
1961# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001962function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001963 if is_ubuntu; then
1964 sudo /usr/sbin/service $1 stop
1965 else
1966 sudo /sbin/service $1 stop
1967 fi
1968}
1969
Sean Dague442e4e92015-06-24 13:24:02 -04001970# Test with a finite retry loop.
1971#
1972function test_with_retry {
1973 local testcmd=$1
1974 local failmsg=$2
1975 local until=${3:-10}
1976 local sleep=${4:-0.5}
1977
1978 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
1979 die $LINENO "$failmsg"
1980 fi
1981}
1982
Dean Troyerdff49a22014-01-30 15:37:40 -06001983
1984# Restore xtrace
1985$XTRACE
1986
1987# Local variables:
1988# mode: shell-script
1989# End: