blob: 5f22bf0c6ded373011a9ad1cec17eceab056531e [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
Ian Wienand4ffb4542015-06-30 11:00:32 +100039# ensure we don't re-source this in the same environment
40[[ -z "$_DEVSTACK_FUNCTIONS_COMMON" ]] || return 0
41declare -r _DEVSTACK_FUNCTIONS_COMMON=1
42
Sean Daguecc524062014-10-01 09:06:43 -040043# Global Config Variables
44declare -A GITREPO
45declare -A GITBRANCH
46declare -A GITDIR
47
Sean Dague53753292014-12-04 19:38:15 -050048TRACK_DEPENDS=${TRACK_DEPENDS:-False}
49
Dean Troyer68162342015-05-13 15:41:03 -050050# Save these variables to .stackenv
51STACK_ENV_VARS="BASE_SQL_CONN DATA_DIR DEST ENABLED_SERVICES HOST_IP \
52 KEYSTONE_AUTH_PROTOCOL KEYSTONE_AUTH_URI KEYSTONE_SERVICE_URI \
53 LOGFILE OS_CACERT SERVICE_HOST SERVICE_PROTOCOL STACK_USER TLS_IP"
54
55
56# Saves significant environment variables to .stackenv for later use
57# Refers to a lot of globals, only TOP_DIR and STACK_ENV_VARS are required to
58# function, the rest are simply saved and do not cause problems if they are undefined.
59# save_stackenv [tag]
60function save_stackenv {
61 local tag=${1:-""}
62 # Save some values we generated for later use
63 time_stamp=$(date "+$TIMESTAMP_FORMAT")
64 echo "# $time_stamp $tag" >$TOP_DIR/.stackenv
65 for i in $STACK_ENV_VARS; do
66 echo $i=${!i} >>$TOP_DIR/.stackenv
67 done
68}
Dean Troyerdff49a22014-01-30 15:37:40 -060069
70# Normalize config values to True or False
71# Accepts as False: 0 no No NO false False FALSE
72# Accepts as True: 1 yes Yes YES true True TRUE
73# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +110074function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -050075 local xtrace=$(set +o | grep xtrace)
76 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060077
Mahito OGURA98f59aa2015-05-11 18:02:34 +090078 local default=$1
79 local testval=${!2:-}
80
81 case "$testval" in
82 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
83 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
84 * ) echo "$default" ;;
85 esac
86
Sean Dague45917cc2014-02-24 16:09:14 -050087 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060088}
89
Attila Fazekas1bd79592015-02-24 14:06:56 +010090function isset {
91 [[ -v "$1" ]]
92}
Dean Troyerdff49a22014-01-30 15:37:40 -060093
Dean Troyer68162342015-05-13 15:41:03 -050094
Dean Troyerdff49a22014-01-30 15:37:40 -060095# Control Functions
96# =================
97
98# Prints backtrace info
99# filename:lineno:function
100# backtrace level
101function backtrace {
102 local level=$1
103 local deep=$((${#BASH_SOURCE[@]} - 1))
104 echo "[Call Trace]"
105 while [ $level -le $deep ]; do
106 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
107 deep=$((deep - 1))
108 done
109}
110
111# Prints line number and "message" then exits
112# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100113function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600114 local exitcode=$?
115 set +o xtrace
116 local line=$1; shift
117 if [ $exitcode == 0 ]; then
118 exitcode=1
119 fi
120 backtrace 2
121 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600122 # Give buffers a second to flush
123 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600124 exit $exitcode
125}
126
127# Checks an environment variable is not set or has length 0 OR if the
128# exit code is non-zero and prints "message" and exits
129# NOTE: env-var is the variable name without a '$'
130# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100131function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600132 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500133 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600134 set +o xtrace
135 local line=$1; shift
136 local evar=$1; shift
137 if ! is_set $evar || [ $exitcode != 0 ]; then
138 die $line "$*"
139 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500140 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600141}
142
143# Prints line number and "message" in error format
144# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100145function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600146 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500147 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600148 set +o xtrace
149 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
150 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600151 if [[ -n ${LOGDIR} ]]; then
152 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600153 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500154 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600155 return $exitcode
156}
157
158# Checks an environment variable is not set or has length 0 OR if the
159# exit code is non-zero and prints "message"
160# NOTE: env-var is the variable name without a '$'
161# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100162function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600163 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500164 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600165 set +o xtrace
166 local line=$1; shift
167 local evar=$1; shift
168 if ! is_set $evar || [ $exitcode != 0 ]; then
169 err $line "$*"
170 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500171 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600172 return $exitcode
173}
174
175# Exit after outputting a message about the distribution not being supported.
176# exit_distro_not_supported [optional-string-telling-what-is-missing]
177function exit_distro_not_supported {
178 if [[ -z "$DISTRO" ]]; then
179 GetDistro
180 fi
181
182 if [ $# -gt 0 ]; then
183 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
184 else
185 die $LINENO "Support for $DISTRO is incomplete."
186 fi
187}
188
189# Test if the named environment variable is set and not zero length
190# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100191function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600192 local var=\$"$1"
193 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
194}
195
196# Prints line number and "message" in warning format
197# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100198function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600199 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500200 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600201 set +o xtrace
202 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Sean Daguee4af9292015-04-28 08:57:57 -0400203 echo $msg
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500204 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600205 return $exitcode
206}
207
208
209# Distro Functions
210# ================
211
212# Determine OS Vendor, Release and Update
213# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
214# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500215# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
216# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
217# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
218# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
219# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500220os_VENDOR=""
221os_RELEASE=""
222os_UPDATE=""
223os_PACKAGE=""
224os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500225
Dean Troyerdff49a22014-01-30 15:37:40 -0600226# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100227function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500228
Dean Troyerdff49a22014-01-30 15:37:40 -0600229 # Figure out which vendor we are
230 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
231 # OS/X
232 os_VENDOR=`sw_vers -productName`
233 os_RELEASE=`sw_vers -productVersion`
234 os_UPDATE=${os_RELEASE##*.}
235 os_RELEASE=${os_RELEASE%.*}
236 os_PACKAGE=""
237 if [[ "$os_RELEASE" =~ "10.7" ]]; then
238 os_CODENAME="lion"
239 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
240 os_CODENAME="snow leopard"
241 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
242 os_CODENAME="leopard"
243 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
244 os_CODENAME="tiger"
245 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
246 os_CODENAME="panther"
247 else
248 os_CODENAME=""
249 fi
250 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
251 os_VENDOR=$(lsb_release -i -s)
252 os_RELEASE=$(lsb_release -r -s)
253 os_UPDATE=""
254 os_PACKAGE="rpm"
255 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
256 os_PACKAGE="deb"
257 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
258 lsb_release -d -s | grep -q openSUSE
259 if [[ $? -eq 0 ]]; then
260 os_VENDOR="openSUSE"
261 fi
262 elif [[ $os_VENDOR == "openSUSE project" ]]; then
263 os_VENDOR="openSUSE"
264 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
265 os_VENDOR="Red Hat"
266 fi
267 os_CODENAME=$(lsb_release -c -s)
268 elif [[ -r /etc/redhat-release ]]; then
269 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
270 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
271 # CentOS release 5.5 (Final)
272 # CentOS Linux release 6.0 (Final)
273 # Fedora release 16 (Verne)
274 # XenServer release 6.2.0-70446c (xenenterprise)
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700275 # Oracle Linux release 7
Dean Troyerdff49a22014-01-30 15:37:40 -0600276 os_CODENAME=""
277 for r in "Red Hat" CentOS Fedora XenServer; do
278 os_VENDOR=$r
279 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
280 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
281 os_CODENAME=${ver#*|}
282 os_RELEASE=${ver%|*}
283 os_UPDATE=${os_RELEASE##*.}
284 os_RELEASE=${os_RELEASE%.*}
285 break
286 fi
287 os_VENDOR=""
288 done
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700289 if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
290 os_VENDOR=OracleLinux
291 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600292 os_PACKAGE="rpm"
293 elif [[ -r /etc/SuSE-release ]]; then
294 for r in openSUSE "SUSE Linux"; do
295 if [[ "$r" = "SUSE Linux" ]]; then
296 os_VENDOR="SUSE LINUX"
297 else
298 os_VENDOR=$r
299 fi
300
301 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
302 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
303 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
304 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
305 break
306 fi
307 os_VENDOR=""
308 done
309 os_PACKAGE="rpm"
310 # If lsb_release is not installed, we should be able to detect Debian OS
311 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
312 os_VENDOR="Debian"
313 os_PACKAGE="deb"
314 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
315 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
316 fi
317 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
318}
319
320# Translate the OS version values into common nomenclature
321# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500322declare DISTRO
323
Ian Wienandaee18c72014-02-21 15:35:08 +1100324function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600325 GetOSVersion
326 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
327 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
328 DISTRO=$os_CODENAME
329 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
330 # For Fedora, just use 'f' and the release
331 DISTRO="f$os_RELEASE"
332 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
333 DISTRO="opensuse-$os_RELEASE"
334 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
335 # For SLE, also use the service pack
336 if [[ -z "$os_UPDATE" ]]; then
337 DISTRO="sle${os_RELEASE}"
338 else
339 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
340 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530341 elif [[ "$os_VENDOR" =~ (Red Hat) || \
342 "$os_VENDOR" =~ (CentOS) || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700343 "$os_VENDOR" =~ (OracleLinux) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600344 # Drop the . release as we assume it's compatible
345 DISTRO="rhel${os_RELEASE::1}"
346 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
347 DISTRO="xs$os_RELEASE"
348 else
349 # Catch-all for now is Vendor + Release + Update
350 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
351 fi
352 export DISTRO
353}
354
355# Utility function for checking machine architecture
356# is_arch arch-type
357function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500358 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600359}
360
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700361# Determine if current distribution is an Oracle distribution
362# is_oraclelinux
363function is_oraclelinux {
364 if [[ -z "$os_VENDOR" ]]; then
365 GetOSVersion
366 fi
367
368 [ "$os_VENDOR" = "OracleLinux" ]
369}
370
371
Dean Troyerdff49a22014-01-30 15:37:40 -0600372# Determine if current distribution is a Fedora-based distribution
373# (Fedora, RHEL, CentOS, etc).
374# is_fedora
375function is_fedora {
376 if [[ -z "$os_VENDOR" ]]; then
377 GetOSVersion
378 fi
379
anju Tiwari6c639c92014-07-15 18:11:54 +0530380 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700381 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600382}
383
384
385# Determine if current distribution is a SUSE-based distribution
386# (openSUSE, SLE).
387# is_suse
388function is_suse {
389 if [[ -z "$os_VENDOR" ]]; then
390 GetOSVersion
391 fi
392
393 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
394}
395
396
397# Determine if current distribution is an Ubuntu-based distribution
398# It will also detect non-Ubuntu but Debian-based distros
399# is_ubuntu
400function is_ubuntu {
401 if [[ -z "$os_PACKAGE" ]]; then
402 GetOSVersion
403 fi
404 [ "$os_PACKAGE" = "deb" ]
405}
406
407
408# Git Functions
409# =============
410
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600411# Returns openstack release name for a given branch name
412# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100413function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600414 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700415 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600416 echo ${branch#*/}
417 else
418 echo "master"
419 fi
420}
421
Dean Troyerdff49a22014-01-30 15:37:40 -0600422# git clone only if directory doesn't exist already. Since ``DEST`` might not
423# be owned by the installation user, we create the directory and change the
424# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500425# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
426# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600427# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500428# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600429# git_clone remote dest-dir branch
430function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500431 local git_remote=$1
432 local git_dest=$2
433 local git_ref=$3
434 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200435 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500436
Sean Dague53753292014-12-04 19:38:15 -0500437 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800438 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200439 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
440 fi
441
Dean Troyerdff49a22014-01-30 15:37:40 -0600442 if [[ "$OFFLINE" = "True" ]]; then
443 echo "Running in offline mode, clones already exist"
444 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500445 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600446 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400447 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600448 return
449 fi
450
Dean Troyer50cda692014-07-25 11:57:20 -0500451 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600452 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500453 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600454 [[ "$ERROR_ON_CLONE" = "True" ]] && \
455 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200456 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600457 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500458 cd $git_dest
459 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600460 else
461 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500462 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600463 [[ "$ERROR_ON_CLONE" = "True" ]] && \
464 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200465 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500466 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600467 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500468 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600469 elif [[ "$RECLONE" = "True" ]]; then
470 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500471 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600472 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500473 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100474 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600475 # remove the existing ignored files (like pyc) as they cause breakage
476 # (due to the py files having older timestamps than our pyc, so python
477 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500478 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600479
Dean Troyer50cda692014-07-25 11:57:20 -0500480 # handle git_ref accordingly to type (tag, branch)
481 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
482 git_update_tag $git_ref
483 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
484 git_update_branch $git_ref
485 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
486 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600487 else
Dean Troyer50cda692014-07-25 11:57:20 -0500488 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600489 fi
490
491 fi
492 fi
493
494 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500495 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600496 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400497 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600498}
499
Sean Daguecc524062014-10-01 09:06:43 -0400500# A variation on git clone that lets us specify a project by it's
501# actual name, like oslo.config. This is exceptionally useful in the
502# library installation case
503function git_clone_by_name {
504 local name=$1
505 local repo=${GITREPO[$name]}
506 local dir=${GITDIR[$name]}
507 local branch=${GITBRANCH[$name]}
508 git_clone $repo $dir $branch
509}
510
511
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100512# git can sometimes get itself infinitely stuck with transient network
513# errors or other issues with the remote end. This wraps git in a
514# timeout/retry loop and is intended to watch over non-local git
515# processes that might hang. GIT_TIMEOUT, if set, is passed directly
516# to timeout(1); otherwise the default value of 0 maintains the status
517# quo of waiting forever.
518# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100519function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100520 local count=0
521 local timeout=0
522
523 if [[ -n "${GIT_TIMEOUT}" ]]; then
524 timeout=${GIT_TIMEOUT}
525 fi
526
527 until timeout -s SIGINT ${timeout} git "$@"; do
528 # 124 is timeout(1)'s special return code when it reached the
529 # timeout; otherwise assume fatal failure
530 if [[ $? -ne 124 ]]; then
531 die $LINENO "git call failed: [git $@]"
532 fi
533
534 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400535 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100536 if [ $count -eq 3 ]; then
537 die $LINENO "Maximum of 3 git retries reached"
538 fi
539 sleep 5
540 done
541}
542
Dean Troyerdff49a22014-01-30 15:37:40 -0600543# git update using reference as a branch.
544# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100545function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500546 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600547
Dean Troyer50cda692014-07-25 11:57:20 -0500548 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600549 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500550 git branch -D $git_branch || true
551 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600552}
553
554# git update using reference as a branch.
555# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100556function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500557 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600558
Dean Troyer50cda692014-07-25 11:57:20 -0500559 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600560}
561
562# git update using reference as a tag. Be careful editing source at that repo
563# as working copy will be in a detached mode
564# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100565function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500566 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600567
Dean Troyer50cda692014-07-25 11:57:20 -0500568 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600569 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500570 git_timed fetch origin tag $git_tag
571 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600572}
573
574
575# OpenStack Functions
576# ===================
577
578# Get the default value for HOST_IP
579# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100580function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600581 local fixed_range=$1
582 local floating_range=$2
583 local host_ip_iface=$3
584 local host_ip=$4
585
Dean Troyerdff49a22014-01-30 15:37:40 -0600586 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
587 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
588 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100589 # Find the interface used for the default route
590 host_ip_iface=${host_ip_iface:-$(ip route | awk '/default/ {print $5}' | head -1)}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500591 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
592 local ip
593 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600594 # Attempt to filter out IP addresses that are part of the fixed and
595 # floating range. Note that this method only works if the ``netaddr``
596 # python library is installed. If it is not installed, an error
597 # will be printed and the first IP from the interface will be used.
598 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
599 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500600 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
601 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600602 break;
603 fi
604 done
605 fi
606 echo $host_ip
607}
608
Attila Fazekasf71b5002014-05-28 09:52:22 +0200609# Generates hex string from ``size`` byte of pseudo random data
610# generate_hex_string size
611function generate_hex_string {
612 local size=$1
613 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
614}
615
Dean Troyerdff49a22014-01-30 15:37:40 -0600616# Grab a numbered field from python prettytable output
617# Fields are numbered starting with 1
618# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
619# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100620function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500621 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600622 while read data; do
623 if [ "$1" -lt 0 ]; then
624 field="(\$(NF$1))"
625 else
626 field="\$$(($1 + 1))"
627 fi
628 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
629 done
630}
631
yuntongjinf26deea2015-02-28 10:50:34 +0800632# install default policy
633# copy over a default policy.json and policy.d for projects
634function install_default_policy {
635 local project=$1
636 local project_uc=$(echo $1|tr a-z A-Z)
637 local conf_dir="${project_uc}_CONF_DIR"
638 # eval conf dir to get the variable
639 conf_dir="${!conf_dir}"
640 local project_dir="${project_uc}_DIR"
641 # eval project dir to get the variable
642 project_dir="${!project_dir}"
643 local sample_conf_dir="${project_dir}/etc/${project}"
644 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
645
646 # first copy any policy.json
647 cp -p $sample_conf_dir/policy.json $conf_dir
648 # then optionally copy over policy.d
649 if [[ -d $sample_policy_dir ]]; then
650 cp -r $sample_policy_dir $conf_dir/policy.d
651 fi
652}
653
Dean Troyerdff49a22014-01-30 15:37:40 -0600654# Add a policy to a policy.json file
655# Do nothing if the policy already exists
656# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100657function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600658 local policy_file=$1
659 local policy_name=$2
660 local policy_perm=$3
661
662 if grep -q ${policy_name} ${policy_file}; then
663 echo "Policy ${policy_name} already exists in ${policy_file}"
664 return
665 fi
666
667 # Add a terminating comma to policy lines without one
668 # Remove the closing '}' and all lines following to the end-of-file
669 local tmpfile=$(mktemp)
670 uniq ${policy_file} | sed -e '
671 s/]$/],/
672 /^[}]/,$d
673 ' > ${tmpfile}
674
675 # Append policy and closing brace
676 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
677 echo "}" >>${tmpfile}
678
679 mv ${tmpfile} ${policy_file}
680}
681
Alistair Coles24779f62014-10-15 18:57:59 +0100682# Gets or creates a domain
683# Usage: get_or_create_domain <name> <description>
684function get_or_create_domain {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500685 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100686 # Gets domain id
687 local domain_id=$(
688 # Gets domain id
689 openstack --os-token=$OS_TOKEN --os-url=$os_url \
690 --os-identity-api-version=3 domain show $1 \
691 -f value -c id 2>/dev/null ||
692 # Creates new domain
693 openstack --os-token=$OS_TOKEN --os-url=$os_url \
694 --os-identity-api-version=3 domain create $1 \
695 --description "$2" \
696 -f value -c id
697 )
698 echo $domain_id
699}
700
Steve Martinellib74e01c2014-12-18 01:35:35 -0500701# Gets or creates group
702# Usage: get_or_create_group <groupname> [<domain> <description>]
703function get_or_create_group {
704 local domain=${2:+--domain ${2}}
705 local desc="${3:-}"
706 local os_url="$KEYSTONE_SERVICE_URI_V3"
707 # Gets group id
708 local group_id=$(
709 # Creates new group with --or-show
710 openstack --os-token=$OS_TOKEN --os-url=$os_url \
711 --os-identity-api-version=3 group create $1 \
712 $domain --description "$desc" --or-show \
713 -f value -c id
714 )
715 echo $group_id
716}
717
Bartosz Górski0abde392014-02-28 14:15:19 +0100718# Gets or creates user
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000719# Usage: get_or_create_user <username> <password> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100720function get_or_create_user {
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000721 if [[ ! -z "$3" ]]; then
722 local email="--email=$3"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200723 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500724 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200725 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100726 local os_cmd="openstack"
727 local domain=""
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000728 if [[ ! -z "$4" ]]; then
729 domain="--domain=$4"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500730 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100731 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100732 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500733 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500734 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100735 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100736 $1 \
737 --password "$2" \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500738 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100739 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500740 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100741 -f value -c id
742 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500743 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100744}
745
746# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100747# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100748function get_or_create_project {
749 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100750 local os_cmd="openstack"
751 local domain=""
752 if [[ ! -z "$2" ]]; then
753 domain="--domain=$2"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500754 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100755 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500756 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500757 # Creates new project with --or-show
758 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100759 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500760 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100761}
762
763# Gets or creates role
764# Usage: get_or_create_role <name>
765function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500766 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500767 # Creates role with --or-show
768 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100769 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500770 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100771}
772
Jamie Lennox9b215db2015-02-10 18:19:57 +1100773# Gets or adds user role to project
774# Usage: get_or_add_user_project_role <role> <user> <project>
775function get_or_add_user_project_role {
Bartosz Górski0abde392014-02-28 14:15:19 +0100776 # Gets user role id
Steve Martinelli5541a612015-01-19 15:58:49 -0500777 local user_role_id=$(openstack role list \
778 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100779 --project $3 \
780 --column "ID" \
781 --column "Name" \
782 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500783 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100784 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500785 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100786 $1 \
787 --user $2 \
788 --project $3 \
789 | grep " id " | get_field 2)
790 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500791 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100792}
793
Steve Martinelli4599fd12015-03-12 21:30:58 -0400794# Gets or adds group role to project
795# Usage: get_or_add_group_project_role <role> <group> <project>
796function get_or_add_group_project_role {
797 # Gets group role id
798 local group_role_id=$(openstack role list \
799 --group $2 \
800 --project $3 \
801 --column "ID" \
802 --column "Name" \
803 | grep " $1 " | get_field 1)
804 if [[ -z "$group_role_id" ]]; then
805 # Adds role to group
806 group_role_id=$(openstack role add \
807 $1 \
808 --group $2 \
809 --project $3 \
810 | grep " id " | get_field 2)
811 fi
812 echo $group_role_id
813}
814
Bartosz Górski0abde392014-02-28 14:15:19 +0100815# Gets or creates service
816# Usage: get_or_create_service <name> <type> <description>
817function get_or_create_service {
818 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500819 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100820 # Gets service id
821 openstack service show $1 -f value -c id 2>/dev/null ||
822 # Creates new service if not exists
823 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500824 $2 \
825 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100826 --description="$3" \
827 -f value -c id
828 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500829 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100830}
831
832# Gets or creates endpoint
833# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
834function get_or_create_endpoint {
835 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500836 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100837 --column "ID" \
838 --column "Region" \
839 --column "Service Name" \
840 | grep " $2 " \
841 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500842 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100843 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500844 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100845 $1 \
846 --region $2 \
847 --publicurl $3 \
848 --adminurl $4 \
849 --internalurl $5 \
850 | grep " id " | get_field 2)
851 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500852 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100853}
Dean Troyerdff49a22014-01-30 15:37:40 -0600854
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500855
Dean Troyerdff49a22014-01-30 15:37:40 -0600856# Package Functions
857# =================
858
859# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100860function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800861 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600862 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800863
864 if [[ -z "$base_dir" ]]; then
865 base_dir=$FILES
866 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600867 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800868 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600869 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800870 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600871 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800872 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600873 else
874 exit_distro_not_supported "list of packages"
875 fi
876 echo "$pkg_dir"
877}
878
879# Wrapper for ``apt-get`` to set cache and proxy environment variables
880# Uses globals ``OFFLINE``, ``*_proxy``
881# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100882function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500883 local xtrace=$(set +o | grep xtrace)
884 set +o xtrace
885
Dean Troyerdff49a22014-01-30 15:37:40 -0600886 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
887 local sudo="sudo"
888 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500889
890 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500891
Dean Troyerdff49a22014-01-30 15:37:40 -0600892 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500893 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
894 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600895 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
896}
897
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800898function _parse_package_files {
899 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600900
Dean Troyerdff49a22014-01-30 15:37:40 -0600901 if [[ -z "$DISTRO" ]]; then
902 GetDistro
903 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600904
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800905 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600906 local OIFS line package distros distro
907 [[ -e $fname ]] || continue
908
909 OIFS=$IFS
910 IFS=$'\n'
911 for line in $(<${fname}); do
912 if [[ $line =~ "NOPRIME" ]]; then
913 continue
914 fi
915
916 # Assume we want this package
917 package=${line%#*}
918 inst_pkg=1
919
920 # Look for # dist:xxx in comment
921 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
922 # We are using BASH regexp matching feature.
923 package=${BASH_REMATCH[1]}
924 distros=${BASH_REMATCH[2]}
925 # In bash ${VAR,,} will lowecase VAR
926 # Look for a match in the distro list
927 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
928 # If no match then skip this package
929 inst_pkg=0
930 fi
931 fi
932
Dean Troyerdff49a22014-01-30 15:37:40 -0600933 if [[ $inst_pkg = 1 ]]; then
934 echo $package
935 fi
936 done
937 IFS=$OIFS
938 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800939}
940
941# get_packages() collects a list of package names of any type from the
942# prerequisite files in ``files/{debs|rpms}``. The list is intended
943# to be passed to a package installer such as apt or yum.
944#
945# Only packages required for the services in 1st argument will be
946# included. Two bits of metadata are recognized in the prerequisite files:
947#
948# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
949# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
950# of the package to the distros listed. The distro names are case insensitive.
951function get_packages {
952 local xtrace=$(set +o | grep xtrace)
953 set +o xtrace
954 local services=$@
955 local package_dir=$(_get_package_dir)
956 local file_to_parse=""
957 local service=""
958
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800959 if [[ -z "$package_dir" ]]; then
960 echo "No package directory supplied"
961 return 1
962 fi
963 for service in ${services//,/ }; do
964 # Allow individual services to specify dependencies
965 if [[ -e ${package_dir}/${service} ]]; then
966 file_to_parse="${file_to_parse} ${package_dir}/${service}"
967 fi
968 # NOTE(sdague) n-api needs glance for now because that's where
969 # glance client is
970 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700971 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800972 file_to_parse="${file_to_parse} ${package_dir}/nova"
973 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700974 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800975 file_to_parse="${file_to_parse} ${package_dir}/glance"
976 fi
977 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700978 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800979 file_to_parse="${file_to_parse} ${package_dir}/cinder"
980 fi
981 elif [[ $service == ceilometer-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700982 if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800983 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
984 fi
985 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700986 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800987 file_to_parse="${file_to_parse} ${package_dir}/swift"
988 fi
989 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700990 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800991 file_to_parse="${file_to_parse} ${package_dir}/nova"
992 fi
993 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700994 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800995 file_to_parse="${file_to_parse} ${package_dir}/glance"
996 fi
997 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700998 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800999 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1000 fi
1001 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001002 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001003 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1004 fi
1005 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001006 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001007 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1008 fi
1009 fi
1010 done
1011 echo "$(_parse_package_files $file_to_parse)"
1012 $xtrace
1013}
1014
1015# get_plugin_packages() collects a list of package names of any type from a
1016# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1017# list is intended to be passed to a package installer such as apt or yum.
1018#
1019# Only packages required for enabled and collected plugins will included.
1020#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001021# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001022# in these prerequisite files, see get_packages() for more info.
1023function get_plugin_packages {
1024 local xtrace=$(set +o | grep xtrace)
1025 set +o xtrace
1026 local files_to_parse=""
1027 local package_dir=""
1028 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1029 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1030 files_to_parse+="$package_dir/$plugin"
1031 done
1032 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001033 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001034}
1035
1036# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001037# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001038# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001039function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001040 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1041 REPOS_UPDATED=${REPOS_UPDATED:-False}
1042 RETRY_UPDATE=${RETRY_UPDATE:-False}
1043
Paul Linchpiner9e179742014-07-13 22:23:00 -07001044 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001045 return 0
1046 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001047
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001048 if is_ubuntu; then
1049 local xtrace=$(set +o | grep xtrace)
1050 set +o xtrace
1051 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1052 # if there are transient errors pulling the updates, that's fine.
1053 # It may be secondary repositories that we don't really care about.
1054 apt_get update || /bin/true
1055 REPOS_UPDATED=True
1056 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001057 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001058 fi
1059}
1060
1061function real_install_package {
1062 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001063 apt_get install "$@"
1064 elif is_fedora; then
1065 yum_install "$@"
1066 elif is_suse; then
1067 zypper_install "$@"
1068 else
1069 exit_distro_not_supported "installing packages"
1070 fi
1071}
1072
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001073# Distro-agnostic package installer
1074# install_package package [package ...]
1075function install_package {
1076 update_package_repo
1077 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1078}
1079
Dean Troyerdff49a22014-01-30 15:37:40 -06001080# Distro-agnostic function to tell if a package is installed
1081# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001082function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001083 if [[ -z "$@" ]]; then
1084 return 1
1085 fi
1086
1087 if [[ -z "$os_PACKAGE" ]]; then
1088 GetOSVersion
1089 fi
1090
1091 if [[ "$os_PACKAGE" = "deb" ]]; then
1092 dpkg -s "$@" > /dev/null 2> /dev/null
1093 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1094 rpm --quiet -q "$@"
1095 else
1096 exit_distro_not_supported "finding if a package is installed"
1097 fi
1098}
1099
1100# Distro-agnostic package uninstaller
1101# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001102function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001103 if is_ubuntu; then
1104 apt_get purge "$@"
1105 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001106 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001107 elif is_suse; then
1108 sudo zypper rm "$@"
1109 else
1110 exit_distro_not_supported "uninstalling packages"
1111 fi
1112}
1113
1114# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001115# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001116# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001117function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001118 [[ "$OFFLINE" = "True" ]] && return
1119 local sudo="sudo"
1120 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001121
1122 # The manual check for missing packages is because yum -y assumes
1123 # missing packages are OK. See
1124 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001125 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1126 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001127 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001128 awk '
1129 BEGIN { fail=0 }
1130 /No package/ { fail=1 }
1131 { print }
1132 END { exit fail }' || \
1133 die $LINENO "Missing packages detected"
1134
1135 # also ensure we catch a yum failure
1136 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001137 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001138 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001139}
1140
1141# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001142# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001143# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001144function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001145 [[ "$OFFLINE" = "True" ]] && return
1146 local sudo="sudo"
1147 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001148 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1149 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001150 zypper --non-interactive install --auto-agree-with-licenses "$@"
1151}
1152
1153
1154# Process Functions
1155# =================
1156
1157# _run_process() is designed to be backgrounded by run_process() to simulate a
1158# fork. It includes the dirty work of closing extra filehandles and preparing log
1159# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001160# from the service name.
1161# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001162# If an optional group is provided sg will be used to set the group of
1163# the command.
1164# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001165function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001166 # disable tracing through the exec redirects, it's just confusing in the logs.
1167 xtrace=$(set +o | grep xtrace)
1168 set +o xtrace
1169
Dean Troyerdff49a22014-01-30 15:37:40 -06001170 local service=$1
1171 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001172 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001173
1174 # Undo logging redirections and close the extra descriptors
1175 exec 1>&3
1176 exec 2>&3
1177 exec 3>&-
1178 exec 6>&-
1179
Dean Troyerdde41d02014-12-09 17:47:57 -06001180 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1181 if [[ -n ${LOGDIR} ]]; then
1182 exec 1>&"$real_logfile" 2>&1
1183 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1184 if [[ -n ${SCREEN_LOGDIR} ]]; then
1185 # Drop the backward-compat symlink
1186 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1187 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001188
1189 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1190 export PYTHONUNBUFFERED=1
1191 fi
1192
Sean Dague6e137ab2015-04-29 08:22:24 -04001193 # reenable xtrace before we do *real* work
1194 $xtrace
1195
Dean Troyer3159a822014-08-27 14:13:58 -05001196 # Run under ``setsid`` to force the process to become a session and group leader.
1197 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001198 if [[ -n "$group" ]]; then
1199 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1200 else
1201 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1202 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001203
1204 # Just silently exit this process
1205 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001206}
1207
1208# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1209# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001210# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001211# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001212function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001213 SCREEN_NAME=${SCREEN_NAME:-stack}
1214 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1215
1216 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1217 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1218 fi
1219
1220 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1221}
1222
1223# Find out if a process exists by partial name.
1224# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001225function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001226 local name=$1
1227 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001228 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001229 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001230 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001231}
1232
Dean Troyer3159a822014-08-27 14:13:58 -05001233# Run a single service under screen or directly
1234# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001235# If an optional group is provided sg will be used to run the
1236# command as that group.
1237# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001238function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001239 local service=$1
1240 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001241 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001242
Dean Troyer3159a822014-08-27 14:13:58 -05001243 if is_service_enabled $service; then
1244 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001245 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001246 else
1247 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001248 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001249 fi
1250 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001251}
1252
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001253# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001254# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001255# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001256# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001257# Run a command in a shell in a screen window, if an optional group
1258# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001259function screen_process {
1260 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001261 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001262 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001263
Sean Dagueea22a4f2014-06-27 15:21:41 -04001264 SCREEN_NAME=${SCREEN_NAME:-stack}
1265 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001266 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001267
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001268 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001269
Dean Troyerdde41d02014-12-09 17:47:57 -06001270 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1271 echo "LOGDIR: $LOGDIR"
1272 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1273 echo "log: $real_logfile"
1274 if [[ -n ${LOGDIR} ]]; then
1275 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001276 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001277 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1278 if [[ -n ${SCREEN_LOGDIR} ]]; then
1279 # Drop the backward-compat symlink
1280 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1281 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001282 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001283
1284 # sleep to allow bash to be ready to be send the command - we are
1285 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001286 # bash isn't running by the time we send the command, nothing
1287 # happens. This sleep was added originally to handle gate runs
1288 # where we needed this to be at least 3 seconds to pass
1289 # consistently on slow clouds. Now this is configurable so that we
1290 # can determine a reasonable value for the local case which should
1291 # be much smaller.
1292 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001293
1294 NL=`echo -ne '\015'`
1295 # This fun command does the following:
1296 # - the passed server command is backgrounded
1297 # - the pid of the background process is saved in the usual place
1298 # - the server process is brought back to the foreground
1299 # - if the server process exits prematurely the fg command errors
1300 # and a message is written to stdout and the process failure file
1301 #
1302 # The pid saved can be used in stop_process() as a process group
1303 # id to kill off all child processes
1304 if [[ -n "$group" ]]; then
1305 command="sg $group '$command'"
1306 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001307
1308 # Append the process to the screen rc file
1309 screen_rc "$name" "$command"
1310
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001311 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 -06001312}
1313
1314# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001315# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001316# screen_rc service "command-line"
1317function screen_rc {
1318 SCREEN_NAME=${SCREEN_NAME:-stack}
1319 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1320 if [[ ! -e $SCREENRC ]]; then
1321 # Name the screen session
1322 echo "sessionname $SCREEN_NAME" > $SCREENRC
1323 # Set a reasonable statusbar
1324 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1325 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1326 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1327 echo "screen -t shell bash" >> $SCREENRC
1328 fi
1329 # If this service doesn't already exist in the screenrc file
1330 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1331 NL=`echo -ne '\015'`
1332 echo "screen -t $1 bash" >> $SCREENRC
1333 echo "stuff \"$2$NL\"" >> $SCREENRC
1334
Dean Troyerdde41d02014-12-09 17:47:57 -06001335 if [[ -n ${LOGDIR} ]]; then
1336 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001337 echo "log on" >>$SCREENRC
1338 fi
1339 fi
1340}
1341
1342# Stop a service in screen
1343# If a PID is available use it, kill the whole process group via TERM
1344# If screen is being used kill the screen window; this will catch processes
1345# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001346# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001347# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001348function screen_stop_service {
1349 local service=$1
1350
Dean Troyerdff49a22014-01-30 15:37:40 -06001351 SCREEN_NAME=${SCREEN_NAME:-stack}
1352 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001353 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001354
Dean Troyer3159a822014-08-27 14:13:58 -05001355 if is_service_enabled $service; then
1356 # Clean up the screen window
1357 screen -S $SCREEN_NAME -p $service -X kill
1358 fi
1359}
1360
1361# Stop a service process
1362# If a PID is available use it, kill the whole process group via TERM
1363# If screen is being used kill the screen window; this will catch processes
1364# that did not leave a PID behind
1365# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1366# stop_process service
1367function stop_process {
1368 local service=$1
1369
1370 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001371 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001372
1373 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001374 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001375 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1376 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1377 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001378 fi
1379 if [[ "$USE_SCREEN" = "True" ]]; then
1380 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001381 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001382 fi
1383 fi
1384}
1385
1386# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001387# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001388# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001389function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001390 local service
1391 local failures
1392 SCREEN_NAME=${SCREEN_NAME:-stack}
1393 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1394
1395
1396 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1397 echo "No service status directory found"
1398 return
1399 fi
1400
1401 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001402 # make this -o errexit safe
1403 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001404
1405 for service in $failures; do
1406 service=`basename $service`
1407 service=${service%.failure}
1408 echo "Error: Service $service is not running"
1409 done
1410
1411 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001412 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001413 fi
1414}
1415
Chris Dent2f27a0e2014-09-09 13:46:02 +01001416# Tail a log file in a screen if USE_SCREEN is true.
1417function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001418 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001419 local logfile=$2
1420
Sean Dague53753292014-12-04 19:38:15 -05001421 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001422 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001423 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001424 fi
1425}
1426
Dean Troyerdff49a22014-01-30 15:37:40 -06001427
Dean Troyer3159a822014-08-27 14:13:58 -05001428# Deprecated Functions
1429# --------------------
1430
1431# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1432# fork. It includes the dirty work of closing extra filehandles and preparing log
1433# files to produce the same logs as screen_it(). The log filename is derived
1434# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1435# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1436# _old_run_process service "command-line"
1437function _old_run_process {
1438 local service=$1
1439 local command="$2"
1440
1441 # Undo logging redirections and close the extra descriptors
1442 exec 1>&3
1443 exec 2>&3
1444 exec 3>&-
1445 exec 6>&-
1446
1447 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001448 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1449 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001450
1451 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1452 export PYTHONUNBUFFERED=1
1453 fi
1454
1455 exec /bin/bash -c "$command"
1456 die "$service exec failure: $command"
1457}
1458
1459# old_run_process() launches a child process that closes all file descriptors and
1460# then exec's the passed in command. This is meant to duplicate the semantics
1461# of screen_it() without screen. PIDs are written to
1462# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1463# old_run_process service "command-line"
1464function old_run_process {
1465 local service=$1
1466 local command="$2"
1467
1468 # Spawn the child process
1469 _old_run_process "$service" "$command" &
1470 echo $!
1471}
1472
1473# Compatibility for existing start_XXXX() functions
1474# Uses global ``USE_SCREEN``
1475# screen_it service "command-line"
1476function screen_it {
1477 if is_service_enabled $1; then
1478 # Append the service to the screen rc file
1479 screen_rc "$1" "$2"
1480
1481 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001482 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001483 else
1484 # Spawn directly without screen
1485 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1486 fi
1487 fi
1488}
1489
1490# Compatibility for existing stop_XXXX() functions
1491# Stop a service in screen
1492# If a PID is available use it, kill the whole process group via TERM
1493# If screen is being used kill the screen window; this will catch processes
1494# that did not leave a PID behind
1495# screen_stop service
1496function screen_stop {
1497 # Clean up the screen window
1498 stop_process $1
1499}
1500
1501
Sean Dague2c65e712014-12-18 09:44:56 -05001502# Plugin Functions
1503# =================
1504
1505DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1506
1507# enable_plugin <name> <url> [branch]
1508#
1509# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1510# ``url`` is a git url
1511# ``branch`` is a gitref. If it's not set, defaults to master
1512function enable_plugin {
1513 local name=$1
1514 local url=$2
1515 local branch=${3:-master}
1516 DEVSTACK_PLUGINS+=",$name"
1517 GITREPO[$name]=$url
1518 GITDIR[$name]=$DEST/$name
1519 GITBRANCH[$name]=$branch
1520}
1521
1522# fetch_plugins
1523#
1524# clones all plugins
1525function fetch_plugins {
1526 local plugins="${DEVSTACK_PLUGINS}"
1527 local plugin
1528
1529 # short circuit if nothing to do
1530 if [[ -z $plugins ]]; then
1531 return
1532 fi
1533
Dean Troyerdc97cb72015-03-28 08:20:50 -05001534 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001535 for plugin in ${plugins//,/ }; do
1536 git_clone_by_name $plugin
1537 done
1538}
1539
1540# load_plugin_settings
1541#
1542# Load settings from plugins in the order that they were registered
1543function load_plugin_settings {
1544 local plugins="${DEVSTACK_PLUGINS}"
1545 local plugin
1546
1547 # short circuit if nothing to do
1548 if [[ -z $plugins ]]; then
1549 return
1550 fi
1551
1552 echo "Loading plugin settings"
1553 for plugin in ${plugins//,/ }; do
1554 local dir=${GITDIR[$plugin]}
1555 # source any known settings
1556 if [[ -f $dir/devstack/settings ]]; then
1557 source $dir/devstack/settings
1558 fi
1559 done
1560}
1561
Sean Dague6e275e12015-03-26 05:54:28 -04001562# plugin_override_defaults
1563#
1564# Run an extremely early setting phase for plugins that allows default
1565# overriding of services.
1566function plugin_override_defaults {
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 "Overriding Configuration Defaults"
1576 for plugin in ${plugins//,/ }; do
1577 local dir=${GITDIR[$plugin]}
1578 # source any overrides
1579 if [[ -f $dir/devstack/override-defaults ]]; then
1580 # be really verbose that an override is happening, as it
1581 # may not be obvious if things fail later.
1582 echo "$plugin has overriden the following defaults"
1583 cat $dir/devstack/override-defaults
1584 source $dir/devstack/override-defaults
1585 fi
1586 done
1587}
1588
Sean Dague2c65e712014-12-18 09:44:56 -05001589# run_plugins
1590#
1591# Run the devstack/plugin.sh in all the plugin directories. These are
1592# run in registration order.
1593function run_plugins {
1594 local mode=$1
1595 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301596
1597 local plugins="${DEVSTACK_PLUGINS}"
1598 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001599 for plugin in ${plugins//,/ }; do
1600 local dir=${GITDIR[$plugin]}
1601 if [[ -f $dir/devstack/plugin.sh ]]; then
1602 source $dir/devstack/plugin.sh $mode $phase
1603 fi
1604 done
1605}
1606
1607function run_phase {
1608 local mode=$1
1609 local phase=$2
1610 if [[ -d $TOP_DIR/extras.d ]]; then
1611 for i in $TOP_DIR/extras.d/*.sh; do
1612 [[ -r $i ]] && source $i $mode $phase
1613 done
1614 fi
1615 # the source phase corresponds to settings loading in plugins
1616 if [[ "$mode" == "source" ]]; then
1617 load_plugin_settings
Sean Dague6e275e12015-03-26 05:54:28 -04001618 elif [[ "$mode" == "override_defaults" ]]; then
1619 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001620 else
1621 run_plugins $mode $phase
1622 fi
1623}
1624
Dean Troyerdff49a22014-01-30 15:37:40 -06001625
1626# Service Functions
1627# =================
1628
1629# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1630# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001631function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001632 echo "$1" | sed -e '
1633 s/,,/,/g;
1634 s/^,//;
1635 s/,$//
1636 '
1637}
1638
1639# disable_all_services() removes all current services
1640# from ``ENABLED_SERVICES`` to reset the configuration
1641# before a minimal installation
1642# Uses global ``ENABLED_SERVICES``
1643# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001644function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001645 ENABLED_SERVICES=""
1646}
1647
1648# Remove all services starting with '-'. For example, to install all default
1649# services except rabbit (rabbit) set in ``localrc``:
1650# ENABLED_SERVICES+=",-rabbit"
1651# Uses global ``ENABLED_SERVICES``
1652# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001653function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001654 local to_remove=""
1655 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001656 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001657
1658 # build up list of services that should be removed; i.e. they
1659 # begin with "-"
1660 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001661 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001662 to_remove+=",${service#-}"
1663 else
1664 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001665 fi
1666 done
Ian Wienand2796a822015-04-15 08:59:04 +10001667
1668 # go through the service list. if this service appears in the "to
1669 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001670 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Dean Troyerdff49a22014-01-30 15:37:40 -06001671}
1672
1673# disable_service() removes the services passed as argument to the
1674# ``ENABLED_SERVICES`` list, if they are present.
1675#
1676# For example:
1677# disable_service rabbit
1678#
1679# This function does not know about the special cases
1680# for nova, glance, and neutron built into is_service_enabled().
1681# Uses global ``ENABLED_SERVICES``
1682# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001683function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001684 local tmpsvcs=",${ENABLED_SERVICES},"
1685 local service
1686 for service in $@; do
1687 if is_service_enabled $service; then
1688 tmpsvcs=${tmpsvcs//,$service,/,}
1689 fi
1690 done
1691 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1692}
1693
1694# enable_service() adds the services passed as argument to the
1695# ``ENABLED_SERVICES`` list, if they are not already present.
1696#
1697# For example:
1698# enable_service qpid
1699#
1700# This function does not know about the special cases
1701# for nova, glance, and neutron built into is_service_enabled().
1702# Uses global ``ENABLED_SERVICES``
1703# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001704function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001705 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001706 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001707 for service in $@; do
1708 if ! is_service_enabled $service; then
1709 tmpsvcs+=",$service"
1710 fi
1711 done
1712 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1713 disable_negated_services
1714}
1715
1716# is_service_enabled() checks if the service(s) specified as arguments are
1717# enabled by the user in ``ENABLED_SERVICES``.
1718#
1719# Multiple services specified as arguments are ``OR``'ed together; the test
1720# is a short-circuit boolean, i.e it returns on the first match.
1721#
1722# There are special cases for some 'catch-all' services::
1723# **nova** returns true if any service enabled start with **n-**
1724# **cinder** returns true if any service enabled start with **c-**
1725# **ceilometer** returns true if any service enabled start with **ceilometer**
1726# **glance** returns true if any service enabled start with **g-**
1727# **neutron** returns true if any service enabled start with **q-**
1728# **swift** returns true if any service enabled start with **s-**
1729# **trove** returns true if any service enabled start with **tr-**
1730# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1731# **s-** services will be enabled. This will be deprecated in the future.
1732#
1733# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1734# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1735# as enabled in this case.
1736#
1737# Uses global ``ENABLED_SERVICES``
1738# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001739function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001740 local xtrace=$(set +o | grep xtrace)
1741 set +o xtrace
1742 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001743 local services=$@
1744 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001745 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001746 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001747
1748 # Look for top-level 'enabled' function for this service
1749 if type is_${service}_enabled >/dev/null 2>&1; then
1750 # A function exists for this service, use it
1751 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001752 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001753 fi
1754
1755 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1756 # are implemented
1757
Sean Dague45917cc2014-02-24 16:09:14 -05001758 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001759 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001760 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1761 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1762 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1763 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1764 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1765 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1766 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1767 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1768 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001769 done
Sean Dague45917cc2014-02-24 16:09:14 -05001770 $xtrace
1771 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001772}
1773
fumihiko kakuma8606c982015-04-13 09:55:06 +09001774# remove specified list from the input string
1775# remove_disabled_services service-list remove-list
1776function remove_disabled_services {
1777 local service_list=$1
1778 local remove_list=$2
1779 local service
1780 local enabled=""
1781
1782 for service in ${service_list//,/ }; do
1783 local remove
1784 local add=1
1785 for remove in ${remove_list//,/ }; do
1786 if [[ ${remove} == ${service} ]]; then
1787 add=0
1788 break
1789 fi
1790 done
1791 if [[ $add == 1 ]]; then
1792 enabled="${enabled},$service"
1793 fi
1794 done
1795 _cleanup_service_list "$enabled"
1796}
1797
Dean Troyerdff49a22014-01-30 15:37:40 -06001798# Toggle enable/disable_service for services that must run exclusive of each other
1799# $1 The name of a variable containing a space-separated list of services
1800# $2 The name of a variable in which to store the enabled service's name
1801# $3 The name of the service to enable
1802function use_exclusive_service {
1803 local options=${!1}
1804 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001805 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001806 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001807 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001808 for opt in $options;do
1809 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1810 done
1811 eval "$out=$selection"
1812 return 0
1813}
1814
1815
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001816# System Functions
1817# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001818
1819# Only run the command if the target file (the last arg) is not on an
1820# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001821function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001822 local xtrace=$(set +o | grep xtrace)
1823 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001824 local args=( $@ )
1825 local last
1826 local sudo_cmd
1827 local dir_to_check
1828
1829 let last="${#args[*]} - 1"
1830
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001831 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001832 if [ ! -d "$dir_to_check" ]; then
1833 dir_to_check=`dirname "$dir_to_check"`
1834 fi
1835
1836 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001837 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001838 return 0
1839 fi
1840
1841 if [[ $TRACK_DEPENDS = True ]]; then
1842 sudo_cmd="env"
1843 else
1844 sudo_cmd="sudo"
1845 fi
1846
Sean Dague45917cc2014-02-24 16:09:14 -05001847 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001848 $sudo_cmd $@
1849}
1850
1851# Exit 0 if address is in network or 1 if address is not in network
1852# ip-range is in CIDR notation: 1.2.3.4/20
1853# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001854function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001855 local ip=$1
1856 local range=$2
1857 local masklen=${range#*/}
1858 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1859 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1860 [[ $network == $subnet ]]
1861}
1862
1863# Add a user to a group.
1864# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001865function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001866 local user=$1
1867 local group=$2
1868
Thomas Bechtolda8580852015-05-31 00:04:33 +02001869 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06001870}
1871
1872# Convert CIDR notation to a IPv4 netmask
1873# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001874function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001875 local maskpat="255 255 255 255"
1876 local maskdgt="254 252 248 240 224 192 128"
1877 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1878 echo ${1-0}.${2-0}.${3-0}.${4-0}
1879}
1880
1881# Gracefully cp only if source file/dir exists
1882# cp_it source destination
1883function cp_it {
1884 if [ -e $1 ] || [ -d $1 ]; then
1885 cp -pRL $1 $2
1886 fi
1887}
1888
1889# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1890# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1891# ``localrc`` or on the command line if necessary::
1892#
1893# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1894#
1895# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1896
Ian Wienandaee18c72014-02-21 15:35:08 +11001897function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001898 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001899 export http_proxy=$http_proxy
1900 fi
Sean Dague53753292014-12-04 19:38:15 -05001901 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001902 export https_proxy=$https_proxy
1903 fi
Sean Dague53753292014-12-04 19:38:15 -05001904 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001905 export no_proxy=$no_proxy
1906 fi
1907}
1908
1909# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001910function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001911 local mount_type=`stat -f -L -c %T $1`
1912 test "$mount_type" == "nfs"
1913}
1914
1915# Return the network portion of the given IP address using netmask
1916# netmask is in the traditional dotted-quad format
1917# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001918function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001919 local ip=$1
1920 local mask=$2
1921 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1922 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1923 echo $subnet
1924}
1925
Chris Dent3a2c86a2015-05-12 13:41:25 +00001926# Return the current python as "python<major>.<minor>"
1927function python_version {
1928 local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
1929 echo "python${python_version}"
1930}
1931
Dean Troyerdff49a22014-01-30 15:37:40 -06001932# Service wrapper to restart services
1933# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001934function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001935 if is_ubuntu; then
1936 sudo /usr/sbin/service $1 restart
1937 else
1938 sudo /sbin/service $1 restart
1939 fi
1940}
1941
1942# Only change permissions of a file or directory if it is not on an
1943# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001944function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001945 _safe_permission_operation chmod $@
1946}
1947
1948# Only change ownership of a file or directory if it is not on an NFS
1949# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001950function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001951 _safe_permission_operation chown $@
1952}
1953
1954# Service wrapper to start services
1955# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001956function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001957 if is_ubuntu; then
1958 sudo /usr/sbin/service $1 start
1959 else
1960 sudo /sbin/service $1 start
1961 fi
1962}
1963
1964# Service wrapper to stop services
1965# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001966function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001967 if is_ubuntu; then
1968 sudo /usr/sbin/service $1 stop
1969 else
1970 sudo /sbin/service $1 stop
1971 fi
1972}
1973
Sean Dague442e4e92015-06-24 13:24:02 -04001974# Test with a finite retry loop.
1975#
1976function test_with_retry {
1977 local testcmd=$1
1978 local failmsg=$2
1979 local until=${3:-10}
1980 local sleep=${4:-0.5}
1981
1982 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
1983 die $LINENO "$failmsg"
1984 fi
1985}
1986
Dean Troyerdff49a22014-01-30 15:37:40 -06001987
1988# Restore xtrace
1989$XTRACE
1990
1991# Local variables:
1992# mode: shell-script
1993# End: