blob: 33245fbc3d68806b2d5a1944595cda0a0f88c159 [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 Troyerdff49a22014-01-30 15:37:40 -060046
47# Normalize config values to True or False
48# Accepts as False: 0 no No NO false False FALSE
49# Accepts as True: 1 yes Yes YES true True TRUE
50# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +110051function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -050052 local xtrace=$(set +o | grep xtrace)
53 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060054
Mahito OGURA98f59aa2015-05-11 18:02:34 +090055 local default=$1
56 local testval=${!2:-}
57
58 case "$testval" in
59 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
60 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
61 * ) echo "$default" ;;
62 esac
63
Sean Dague45917cc2014-02-24 16:09:14 -050064 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060065}
66
Attila Fazekas1bd79592015-02-24 14:06:56 +010067function isset {
68 [[ -v "$1" ]]
69}
Dean Troyerdff49a22014-01-30 15:37:40 -060070
71# Control Functions
72# =================
73
74# Prints backtrace info
75# filename:lineno:function
76# backtrace level
77function backtrace {
78 local level=$1
79 local deep=$((${#BASH_SOURCE[@]} - 1))
80 echo "[Call Trace]"
81 while [ $level -le $deep ]; do
82 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
83 deep=$((deep - 1))
84 done
85}
86
87# Prints line number and "message" then exits
88# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +110089function die {
Dean Troyerdff49a22014-01-30 15:37:40 -060090 local exitcode=$?
91 set +o xtrace
92 local line=$1; shift
93 if [ $exitcode == 0 ]; then
94 exitcode=1
95 fi
96 backtrace 2
97 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -060098 # Give buffers a second to flush
99 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600100 exit $exitcode
101}
102
103# Checks an environment variable is not set or has length 0 OR if the
104# exit code is non-zero and prints "message" and exits
105# NOTE: env-var is the variable name without a '$'
106# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100107function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600108 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500109 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600110 set +o xtrace
111 local line=$1; shift
112 local evar=$1; shift
113 if ! is_set $evar || [ $exitcode != 0 ]; then
114 die $line "$*"
115 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500116 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600117}
118
119# Prints line number and "message" in error format
120# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100121function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600122 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500123 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600124 set +o xtrace
125 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
126 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600127 if [[ -n ${LOGDIR} ]]; then
128 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600129 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500130 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600131 return $exitcode
132}
133
134# Checks an environment variable is not set or has length 0 OR if the
135# exit code is non-zero and prints "message"
136# NOTE: env-var is the variable name without a '$'
137# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100138function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600139 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500140 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600141 set +o xtrace
142 local line=$1; shift
143 local evar=$1; shift
144 if ! is_set $evar || [ $exitcode != 0 ]; then
145 err $line "$*"
146 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500147 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600148 return $exitcode
149}
150
151# Exit after outputting a message about the distribution not being supported.
152# exit_distro_not_supported [optional-string-telling-what-is-missing]
153function exit_distro_not_supported {
154 if [[ -z "$DISTRO" ]]; then
155 GetDistro
156 fi
157
158 if [ $# -gt 0 ]; then
159 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
160 else
161 die $LINENO "Support for $DISTRO is incomplete."
162 fi
163}
164
165# Test if the named environment variable is set and not zero length
166# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100167function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600168 local var=\$"$1"
169 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
170}
171
172# Prints line number and "message" in warning format
173# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100174function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600175 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500176 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600177 set +o xtrace
178 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Sean Daguee4af9292015-04-28 08:57:57 -0400179 echo $msg
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500180 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600181 return $exitcode
182}
183
184
185# Distro Functions
186# ================
187
188# Determine OS Vendor, Release and Update
189# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
190# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500191# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
192# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
193# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
194# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
195# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500196os_VENDOR=""
197os_RELEASE=""
198os_UPDATE=""
199os_PACKAGE=""
200os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500201
Dean Troyerdff49a22014-01-30 15:37:40 -0600202# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100203function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500204
Dean Troyerdff49a22014-01-30 15:37:40 -0600205 # Figure out which vendor we are
206 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
207 # OS/X
208 os_VENDOR=`sw_vers -productName`
209 os_RELEASE=`sw_vers -productVersion`
210 os_UPDATE=${os_RELEASE##*.}
211 os_RELEASE=${os_RELEASE%.*}
212 os_PACKAGE=""
213 if [[ "$os_RELEASE" =~ "10.7" ]]; then
214 os_CODENAME="lion"
215 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
216 os_CODENAME="snow leopard"
217 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
218 os_CODENAME="leopard"
219 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
220 os_CODENAME="tiger"
221 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
222 os_CODENAME="panther"
223 else
224 os_CODENAME=""
225 fi
226 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
227 os_VENDOR=$(lsb_release -i -s)
228 os_RELEASE=$(lsb_release -r -s)
229 os_UPDATE=""
230 os_PACKAGE="rpm"
231 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
232 os_PACKAGE="deb"
233 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
234 lsb_release -d -s | grep -q openSUSE
235 if [[ $? -eq 0 ]]; then
236 os_VENDOR="openSUSE"
237 fi
238 elif [[ $os_VENDOR == "openSUSE project" ]]; then
239 os_VENDOR="openSUSE"
240 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
241 os_VENDOR="Red Hat"
242 fi
243 os_CODENAME=$(lsb_release -c -s)
244 elif [[ -r /etc/redhat-release ]]; then
245 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
246 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
247 # CentOS release 5.5 (Final)
248 # CentOS Linux release 6.0 (Final)
249 # Fedora release 16 (Verne)
250 # XenServer release 6.2.0-70446c (xenenterprise)
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700251 # Oracle Linux release 7
Dean Troyerdff49a22014-01-30 15:37:40 -0600252 os_CODENAME=""
253 for r in "Red Hat" CentOS Fedora XenServer; do
254 os_VENDOR=$r
255 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
256 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
257 os_CODENAME=${ver#*|}
258 os_RELEASE=${ver%|*}
259 os_UPDATE=${os_RELEASE##*.}
260 os_RELEASE=${os_RELEASE%.*}
261 break
262 fi
263 os_VENDOR=""
264 done
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700265 if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
266 os_VENDOR=OracleLinux
267 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600268 os_PACKAGE="rpm"
269 elif [[ -r /etc/SuSE-release ]]; then
270 for r in openSUSE "SUSE Linux"; do
271 if [[ "$r" = "SUSE Linux" ]]; then
272 os_VENDOR="SUSE LINUX"
273 else
274 os_VENDOR=$r
275 fi
276
277 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
278 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
279 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
280 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
281 break
282 fi
283 os_VENDOR=""
284 done
285 os_PACKAGE="rpm"
286 # If lsb_release is not installed, we should be able to detect Debian OS
287 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
288 os_VENDOR="Debian"
289 os_PACKAGE="deb"
290 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
291 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
292 fi
293 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
294}
295
296# Translate the OS version values into common nomenclature
297# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500298declare DISTRO
299
Ian Wienandaee18c72014-02-21 15:35:08 +1100300function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600301 GetOSVersion
302 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
303 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
304 DISTRO=$os_CODENAME
305 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
306 # For Fedora, just use 'f' and the release
307 DISTRO="f$os_RELEASE"
308 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
309 DISTRO="opensuse-$os_RELEASE"
310 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
311 # For SLE, also use the service pack
312 if [[ -z "$os_UPDATE" ]]; then
313 DISTRO="sle${os_RELEASE}"
314 else
315 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
316 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530317 elif [[ "$os_VENDOR" =~ (Red Hat) || \
318 "$os_VENDOR" =~ (CentOS) || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700319 "$os_VENDOR" =~ (OracleLinux) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600320 # Drop the . release as we assume it's compatible
321 DISTRO="rhel${os_RELEASE::1}"
322 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
323 DISTRO="xs$os_RELEASE"
324 else
325 # Catch-all for now is Vendor + Release + Update
326 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
327 fi
328 export DISTRO
329}
330
331# Utility function for checking machine architecture
332# is_arch arch-type
333function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500334 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600335}
336
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700337# Determine if current distribution is an Oracle distribution
338# is_oraclelinux
339function is_oraclelinux {
340 if [[ -z "$os_VENDOR" ]]; then
341 GetOSVersion
342 fi
343
344 [ "$os_VENDOR" = "OracleLinux" ]
345}
346
347
Dean Troyerdff49a22014-01-30 15:37:40 -0600348# Determine if current distribution is a Fedora-based distribution
349# (Fedora, RHEL, CentOS, etc).
350# is_fedora
351function is_fedora {
352 if [[ -z "$os_VENDOR" ]]; then
353 GetOSVersion
354 fi
355
anju Tiwari6c639c92014-07-15 18:11:54 +0530356 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700357 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600358}
359
360
361# Determine if current distribution is a SUSE-based distribution
362# (openSUSE, SLE).
363# is_suse
364function is_suse {
365 if [[ -z "$os_VENDOR" ]]; then
366 GetOSVersion
367 fi
368
369 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
370}
371
372
373# Determine if current distribution is an Ubuntu-based distribution
374# It will also detect non-Ubuntu but Debian-based distros
375# is_ubuntu
376function is_ubuntu {
377 if [[ -z "$os_PACKAGE" ]]; then
378 GetOSVersion
379 fi
380 [ "$os_PACKAGE" = "deb" ]
381}
382
383
384# Git Functions
385# =============
386
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600387# Returns openstack release name for a given branch name
388# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100389function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600390 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700391 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600392 echo ${branch#*/}
393 else
394 echo "master"
395 fi
396}
397
Dean Troyerdff49a22014-01-30 15:37:40 -0600398# git clone only if directory doesn't exist already. Since ``DEST`` might not
399# be owned by the installation user, we create the directory and change the
400# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500401# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
402# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600403# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500404# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600405# git_clone remote dest-dir branch
406function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500407 local git_remote=$1
408 local git_dest=$2
409 local git_ref=$3
410 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200411 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500412
Sean Dague53753292014-12-04 19:38:15 -0500413 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800414 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200415 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
416 fi
417
Dean Troyerdff49a22014-01-30 15:37:40 -0600418 if [[ "$OFFLINE" = "True" ]]; then
419 echo "Running in offline mode, clones already exist"
420 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500421 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600422 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400423 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600424 return
425 fi
426
Dean Troyer50cda692014-07-25 11:57:20 -0500427 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600428 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500429 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600430 [[ "$ERROR_ON_CLONE" = "True" ]] && \
431 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200432 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600433 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500434 cd $git_dest
435 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600436 else
437 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500438 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600439 [[ "$ERROR_ON_CLONE" = "True" ]] && \
440 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200441 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500442 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600443 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500444 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600445 elif [[ "$RECLONE" = "True" ]]; then
446 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500447 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600448 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500449 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100450 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600451 # remove the existing ignored files (like pyc) as they cause breakage
452 # (due to the py files having older timestamps than our pyc, so python
453 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500454 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600455
Dean Troyer50cda692014-07-25 11:57:20 -0500456 # handle git_ref accordingly to type (tag, branch)
457 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
458 git_update_tag $git_ref
459 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
460 git_update_branch $git_ref
461 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
462 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600463 else
Dean Troyer50cda692014-07-25 11:57:20 -0500464 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600465 fi
466
467 fi
468 fi
469
470 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500471 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600472 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400473 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600474}
475
Sean Daguecc524062014-10-01 09:06:43 -0400476# A variation on git clone that lets us specify a project by it's
477# actual name, like oslo.config. This is exceptionally useful in the
478# library installation case
479function git_clone_by_name {
480 local name=$1
481 local repo=${GITREPO[$name]}
482 local dir=${GITDIR[$name]}
483 local branch=${GITBRANCH[$name]}
484 git_clone $repo $dir $branch
485}
486
487
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100488# git can sometimes get itself infinitely stuck with transient network
489# errors or other issues with the remote end. This wraps git in a
490# timeout/retry loop and is intended to watch over non-local git
491# processes that might hang. GIT_TIMEOUT, if set, is passed directly
492# to timeout(1); otherwise the default value of 0 maintains the status
493# quo of waiting forever.
494# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100495function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100496 local count=0
497 local timeout=0
498
499 if [[ -n "${GIT_TIMEOUT}" ]]; then
500 timeout=${GIT_TIMEOUT}
501 fi
502
503 until timeout -s SIGINT ${timeout} git "$@"; do
504 # 124 is timeout(1)'s special return code when it reached the
505 # timeout; otherwise assume fatal failure
506 if [[ $? -ne 124 ]]; then
507 die $LINENO "git call failed: [git $@]"
508 fi
509
510 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400511 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100512 if [ $count -eq 3 ]; then
513 die $LINENO "Maximum of 3 git retries reached"
514 fi
515 sleep 5
516 done
517}
518
Dean Troyerdff49a22014-01-30 15:37:40 -0600519# git update using reference as a branch.
520# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100521function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500522 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600523
Dean Troyer50cda692014-07-25 11:57:20 -0500524 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600525 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500526 git branch -D $git_branch || true
527 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600528}
529
530# git update using reference as a branch.
531# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100532function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500533 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600534
Dean Troyer50cda692014-07-25 11:57:20 -0500535 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600536}
537
538# git update using reference as a tag. Be careful editing source at that repo
539# as working copy will be in a detached mode
540# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100541function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500542 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600543
Dean Troyer50cda692014-07-25 11:57:20 -0500544 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600545 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500546 git_timed fetch origin tag $git_tag
547 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600548}
549
550
551# OpenStack Functions
552# ===================
553
554# Get the default value for HOST_IP
555# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100556function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600557 local fixed_range=$1
558 local floating_range=$2
559 local host_ip_iface=$3
560 local host_ip=$4
561
Dean Troyerdff49a22014-01-30 15:37:40 -0600562 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
563 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
564 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100565 # Find the interface used for the default route
566 host_ip_iface=${host_ip_iface:-$(ip route | awk '/default/ {print $5}' | head -1)}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500567 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
568 local ip
569 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600570 # Attempt to filter out IP addresses that are part of the fixed and
571 # floating range. Note that this method only works if the ``netaddr``
572 # python library is installed. If it is not installed, an error
573 # will be printed and the first IP from the interface will be used.
574 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
575 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500576 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
577 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600578 break;
579 fi
580 done
581 fi
582 echo $host_ip
583}
584
Attila Fazekasf71b5002014-05-28 09:52:22 +0200585# Generates hex string from ``size`` byte of pseudo random data
586# generate_hex_string size
587function generate_hex_string {
588 local size=$1
589 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
590}
591
Dean Troyerdff49a22014-01-30 15:37:40 -0600592# Grab a numbered field from python prettytable output
593# Fields are numbered starting with 1
594# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
595# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100596function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500597 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600598 while read data; do
599 if [ "$1" -lt 0 ]; then
600 field="(\$(NF$1))"
601 else
602 field="\$$(($1 + 1))"
603 fi
604 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
605 done
606}
607
yuntongjinf26deea2015-02-28 10:50:34 +0800608# install default policy
609# copy over a default policy.json and policy.d for projects
610function install_default_policy {
611 local project=$1
612 local project_uc=$(echo $1|tr a-z A-Z)
613 local conf_dir="${project_uc}_CONF_DIR"
614 # eval conf dir to get the variable
615 conf_dir="${!conf_dir}"
616 local project_dir="${project_uc}_DIR"
617 # eval project dir to get the variable
618 project_dir="${!project_dir}"
619 local sample_conf_dir="${project_dir}/etc/${project}"
620 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
621
622 # first copy any policy.json
623 cp -p $sample_conf_dir/policy.json $conf_dir
624 # then optionally copy over policy.d
625 if [[ -d $sample_policy_dir ]]; then
626 cp -r $sample_policy_dir $conf_dir/policy.d
627 fi
628}
629
Dean Troyerdff49a22014-01-30 15:37:40 -0600630# Add a policy to a policy.json file
631# Do nothing if the policy already exists
632# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100633function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600634 local policy_file=$1
635 local policy_name=$2
636 local policy_perm=$3
637
638 if grep -q ${policy_name} ${policy_file}; then
639 echo "Policy ${policy_name} already exists in ${policy_file}"
640 return
641 fi
642
643 # Add a terminating comma to policy lines without one
644 # Remove the closing '}' and all lines following to the end-of-file
645 local tmpfile=$(mktemp)
646 uniq ${policy_file} | sed -e '
647 s/]$/],/
648 /^[}]/,$d
649 ' > ${tmpfile}
650
651 # Append policy and closing brace
652 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
653 echo "}" >>${tmpfile}
654
655 mv ${tmpfile} ${policy_file}
656}
657
Alistair Coles24779f62014-10-15 18:57:59 +0100658# Gets or creates a domain
659# Usage: get_or_create_domain <name> <description>
660function get_or_create_domain {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500661 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100662 # Gets domain id
663 local domain_id=$(
664 # Gets domain id
665 openstack --os-token=$OS_TOKEN --os-url=$os_url \
666 --os-identity-api-version=3 domain show $1 \
667 -f value -c id 2>/dev/null ||
668 # Creates new domain
669 openstack --os-token=$OS_TOKEN --os-url=$os_url \
670 --os-identity-api-version=3 domain create $1 \
671 --description "$2" \
672 -f value -c id
673 )
674 echo $domain_id
675}
676
Steve Martinellib74e01c2014-12-18 01:35:35 -0500677# Gets or creates group
678# Usage: get_or_create_group <groupname> [<domain> <description>]
679function get_or_create_group {
680 local domain=${2:+--domain ${2}}
681 local desc="${3:-}"
682 local os_url="$KEYSTONE_SERVICE_URI_V3"
683 # Gets group id
684 local group_id=$(
685 # Creates new group with --or-show
686 openstack --os-token=$OS_TOKEN --os-url=$os_url \
687 --os-identity-api-version=3 group create $1 \
688 $domain --description "$desc" --or-show \
689 -f value -c id
690 )
691 echo $group_id
692}
693
Bartosz Górski0abde392014-02-28 14:15:19 +0100694# Gets or creates user
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000695# Usage: get_or_create_user <username> <password> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100696function get_or_create_user {
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000697 if [[ ! -z "$3" ]]; then
698 local email="--email=$3"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200699 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500700 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200701 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100702 local os_cmd="openstack"
703 local domain=""
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000704 if [[ ! -z "$4" ]]; then
705 domain="--domain=$4"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500706 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100707 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100708 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500709 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500710 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100711 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100712 $1 \
713 --password "$2" \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500714 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100715 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500716 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100717 -f value -c id
718 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500719 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100720}
721
722# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000723# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100724function get_or_create_project {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500725 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500726 # Creates new project with --or-show
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000727 openstack --os-url=$KEYSTONE_SERVICE_URI_V3 \
728 --os-identity-api-version=3 \
729 project create $1 \
730 --domain=$2 \
731 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100732 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500733 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100734}
735
736# Gets or creates role
737# Usage: get_or_create_role <name>
738function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500739 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500740 # Creates role with --or-show
741 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100742 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500743 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100744}
745
Jamie Lennox9b215db2015-02-10 18:19:57 +1100746# Gets or adds user role to project
747# Usage: get_or_add_user_project_role <role> <user> <project>
748function get_or_add_user_project_role {
Bartosz Górski0abde392014-02-28 14:15:19 +0100749 # Gets user role id
Steve Martinelli5541a612015-01-19 15:58:49 -0500750 local user_role_id=$(openstack role list \
751 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100752 --project $3 \
753 --column "ID" \
754 --column "Name" \
755 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500756 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100757 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500758 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100759 $1 \
760 --user $2 \
761 --project $3 \
762 | grep " id " | get_field 2)
763 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500764 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100765}
766
Steve Martinelli4599fd12015-03-12 21:30:58 -0400767# Gets or adds group role to project
768# Usage: get_or_add_group_project_role <role> <group> <project>
769function get_or_add_group_project_role {
770 # Gets group role id
771 local group_role_id=$(openstack role list \
772 --group $2 \
773 --project $3 \
774 --column "ID" \
775 --column "Name" \
776 | grep " $1 " | get_field 1)
777 if [[ -z "$group_role_id" ]]; then
778 # Adds role to group
779 group_role_id=$(openstack role add \
780 $1 \
781 --group $2 \
782 --project $3 \
783 | grep " id " | get_field 2)
784 fi
785 echo $group_role_id
786}
787
Bartosz Górski0abde392014-02-28 14:15:19 +0100788# Gets or creates service
789# Usage: get_or_create_service <name> <type> <description>
790function get_or_create_service {
791 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500792 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100793 # Gets service id
794 openstack service show $1 -f value -c id 2>/dev/null ||
795 # Creates new service if not exists
796 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500797 $2 \
798 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100799 --description="$3" \
800 -f value -c id
801 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500802 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100803}
804
805# Gets or creates endpoint
806# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
807function get_or_create_endpoint {
808 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500809 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100810 --column "ID" \
811 --column "Region" \
812 --column "Service Name" \
813 | grep " $2 " \
814 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500815 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100816 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500817 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100818 $1 \
819 --region $2 \
820 --publicurl $3 \
821 --adminurl $4 \
822 --internalurl $5 \
823 | grep " id " | get_field 2)
824 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500825 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100826}
Dean Troyerdff49a22014-01-30 15:37:40 -0600827
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500828
Dean Troyerdff49a22014-01-30 15:37:40 -0600829# Package Functions
830# =================
831
832# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100833function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800834 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600835 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800836
837 if [[ -z "$base_dir" ]]; then
838 base_dir=$FILES
839 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600840 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800841 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600842 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800843 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600844 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800845 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600846 else
847 exit_distro_not_supported "list of packages"
848 fi
849 echo "$pkg_dir"
850}
851
852# Wrapper for ``apt-get`` to set cache and proxy environment variables
853# Uses globals ``OFFLINE``, ``*_proxy``
854# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100855function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500856 local xtrace=$(set +o | grep xtrace)
857 set +o xtrace
858
Dean Troyerdff49a22014-01-30 15:37:40 -0600859 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
860 local sudo="sudo"
861 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500862
863 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500864
Dean Troyerdff49a22014-01-30 15:37:40 -0600865 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500866 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
867 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600868 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
869}
870
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800871function _parse_package_files {
872 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600873
Dean Troyerdff49a22014-01-30 15:37:40 -0600874 if [[ -z "$DISTRO" ]]; then
875 GetDistro
876 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600877
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800878 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600879 local OIFS line package distros distro
880 [[ -e $fname ]] || continue
881
882 OIFS=$IFS
883 IFS=$'\n'
884 for line in $(<${fname}); do
885 if [[ $line =~ "NOPRIME" ]]; then
886 continue
887 fi
888
889 # Assume we want this package
890 package=${line%#*}
891 inst_pkg=1
892
893 # Look for # dist:xxx in comment
894 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
895 # We are using BASH regexp matching feature.
896 package=${BASH_REMATCH[1]}
897 distros=${BASH_REMATCH[2]}
898 # In bash ${VAR,,} will lowecase VAR
899 # Look for a match in the distro list
900 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
901 # If no match then skip this package
902 inst_pkg=0
903 fi
904 fi
905
Dean Troyerdff49a22014-01-30 15:37:40 -0600906 if [[ $inst_pkg = 1 ]]; then
907 echo $package
908 fi
909 done
910 IFS=$OIFS
911 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800912}
913
914# get_packages() collects a list of package names of any type from the
915# prerequisite files in ``files/{debs|rpms}``. The list is intended
916# to be passed to a package installer such as apt or yum.
917#
918# Only packages required for the services in 1st argument will be
919# included. Two bits of metadata are recognized in the prerequisite files:
920#
921# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
922# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
923# of the package to the distros listed. The distro names are case insensitive.
924function get_packages {
925 local xtrace=$(set +o | grep xtrace)
926 set +o xtrace
927 local services=$@
928 local package_dir=$(_get_package_dir)
929 local file_to_parse=""
930 local service=""
931
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800932 if [[ -z "$package_dir" ]]; then
933 echo "No package directory supplied"
934 return 1
935 fi
936 for service in ${services//,/ }; do
937 # Allow individual services to specify dependencies
938 if [[ -e ${package_dir}/${service} ]]; then
939 file_to_parse="${file_to_parse} ${package_dir}/${service}"
940 fi
941 # NOTE(sdague) n-api needs glance for now because that's where
942 # glance client is
943 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700944 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800945 file_to_parse="${file_to_parse} ${package_dir}/nova"
946 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700947 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800948 file_to_parse="${file_to_parse} ${package_dir}/glance"
949 fi
950 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700951 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800952 file_to_parse="${file_to_parse} ${package_dir}/cinder"
953 fi
954 elif [[ $service == ceilometer-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700955 if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800956 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
957 fi
958 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700959 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800960 file_to_parse="${file_to_parse} ${package_dir}/swift"
961 fi
962 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700963 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800964 file_to_parse="${file_to_parse} ${package_dir}/nova"
965 fi
966 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700967 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800968 file_to_parse="${file_to_parse} ${package_dir}/glance"
969 fi
970 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700971 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800972 file_to_parse="${file_to_parse} ${package_dir}/keystone"
973 fi
974 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700975 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800976 file_to_parse="${file_to_parse} ${package_dir}/neutron"
977 fi
978 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700979 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800980 file_to_parse="${file_to_parse} ${package_dir}/ironic"
981 fi
982 fi
983 done
984 echo "$(_parse_package_files $file_to_parse)"
985 $xtrace
986}
987
988# get_plugin_packages() collects a list of package names of any type from a
989# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
990# list is intended to be passed to a package installer such as apt or yum.
991#
992# Only packages required for enabled and collected plugins will included.
993#
Dean Troyerdc97cb72015-03-28 08:20:50 -0500994# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800995# in these prerequisite files, see get_packages() for more info.
996function get_plugin_packages {
997 local xtrace=$(set +o | grep xtrace)
998 set +o xtrace
999 local files_to_parse=""
1000 local package_dir=""
1001 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1002 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1003 files_to_parse+="$package_dir/$plugin"
1004 done
1005 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001006 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001007}
1008
1009# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001010# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001011# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001012function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001013 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1014 REPOS_UPDATED=${REPOS_UPDATED:-False}
1015 RETRY_UPDATE=${RETRY_UPDATE:-False}
1016
Paul Linchpiner9e179742014-07-13 22:23:00 -07001017 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001018 return 0
1019 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001020
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001021 if is_ubuntu; then
1022 local xtrace=$(set +o | grep xtrace)
1023 set +o xtrace
1024 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1025 # if there are transient errors pulling the updates, that's fine.
1026 # It may be secondary repositories that we don't really care about.
1027 apt_get update || /bin/true
1028 REPOS_UPDATED=True
1029 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001030 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001031 fi
1032}
1033
1034function real_install_package {
1035 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001036 apt_get install "$@"
1037 elif is_fedora; then
1038 yum_install "$@"
1039 elif is_suse; then
1040 zypper_install "$@"
1041 else
1042 exit_distro_not_supported "installing packages"
1043 fi
1044}
1045
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001046# Distro-agnostic package installer
1047# install_package package [package ...]
1048function install_package {
1049 update_package_repo
1050 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1051}
1052
Dean Troyerdff49a22014-01-30 15:37:40 -06001053# Distro-agnostic function to tell if a package is installed
1054# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001055function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001056 if [[ -z "$@" ]]; then
1057 return 1
1058 fi
1059
1060 if [[ -z "$os_PACKAGE" ]]; then
1061 GetOSVersion
1062 fi
1063
1064 if [[ "$os_PACKAGE" = "deb" ]]; then
1065 dpkg -s "$@" > /dev/null 2> /dev/null
1066 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1067 rpm --quiet -q "$@"
1068 else
1069 exit_distro_not_supported "finding if a package is installed"
1070 fi
1071}
1072
1073# Distro-agnostic package uninstaller
1074# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001075function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001076 if is_ubuntu; then
1077 apt_get purge "$@"
1078 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001079 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001080 elif is_suse; then
1081 sudo zypper rm "$@"
1082 else
1083 exit_distro_not_supported "uninstalling packages"
1084 fi
1085}
1086
1087# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001088# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001089# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001090function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001091 [[ "$OFFLINE" = "True" ]] && return
1092 local sudo="sudo"
1093 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001094
1095 # The manual check for missing packages is because yum -y assumes
1096 # missing packages are OK. See
1097 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001098 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1099 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001100 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001101 awk '
1102 BEGIN { fail=0 }
1103 /No package/ { fail=1 }
1104 { print }
1105 END { exit fail }' || \
1106 die $LINENO "Missing packages detected"
1107
1108 # also ensure we catch a yum failure
1109 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001110 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001111 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001112}
1113
1114# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001115# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001116# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001117function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001118 [[ "$OFFLINE" = "True" ]] && return
1119 local sudo="sudo"
1120 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001121 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1122 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001123 zypper --non-interactive install --auto-agree-with-licenses "$@"
1124}
1125
1126
1127# Process Functions
1128# =================
1129
1130# _run_process() is designed to be backgrounded by run_process() to simulate a
1131# fork. It includes the dirty work of closing extra filehandles and preparing log
1132# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001133# from the service name.
1134# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001135# If an optional group is provided sg will be used to set the group of
1136# the command.
1137# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001138function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001139 # disable tracing through the exec redirects, it's just confusing in the logs.
1140 xtrace=$(set +o | grep xtrace)
1141 set +o xtrace
1142
Dean Troyerdff49a22014-01-30 15:37:40 -06001143 local service=$1
1144 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001145 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001146
1147 # Undo logging redirections and close the extra descriptors
1148 exec 1>&3
1149 exec 2>&3
1150 exec 3>&-
1151 exec 6>&-
1152
Dean Troyerdde41d02014-12-09 17:47:57 -06001153 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1154 if [[ -n ${LOGDIR} ]]; then
1155 exec 1>&"$real_logfile" 2>&1
1156 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1157 if [[ -n ${SCREEN_LOGDIR} ]]; then
1158 # Drop the backward-compat symlink
1159 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1160 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001161
1162 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1163 export PYTHONUNBUFFERED=1
1164 fi
1165
Sean Dague6e137ab2015-04-29 08:22:24 -04001166 # reenable xtrace before we do *real* work
1167 $xtrace
1168
Dean Troyer3159a822014-08-27 14:13:58 -05001169 # Run under ``setsid`` to force the process to become a session and group leader.
1170 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001171 if [[ -n "$group" ]]; then
1172 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1173 else
1174 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1175 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001176
1177 # Just silently exit this process
1178 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001179}
1180
1181# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1182# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001183# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001184# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001185function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001186 SCREEN_NAME=${SCREEN_NAME:-stack}
1187 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1188
1189 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1190 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1191 fi
1192
1193 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1194}
1195
1196# Find out if a process exists by partial name.
1197# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001198function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001199 local name=$1
1200 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001201 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001202 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001203 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001204}
1205
Dean Troyer3159a822014-08-27 14:13:58 -05001206# Run a single service under screen or directly
1207# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001208# If an optional group is provided sg will be used to run the
1209# command as that group.
1210# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001211function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001212 local service=$1
1213 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001214 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001215
Dean Troyer3159a822014-08-27 14:13:58 -05001216 if is_service_enabled $service; then
1217 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001218 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001219 else
1220 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001221 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001222 fi
1223 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001224}
1225
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001226# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001227# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001228# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001229# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001230# Run a command in a shell in a screen window, if an optional group
1231# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001232function screen_process {
1233 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001234 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001235 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001236
Sean Dagueea22a4f2014-06-27 15:21:41 -04001237 SCREEN_NAME=${SCREEN_NAME:-stack}
1238 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001239 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001240
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001241 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001242
Dean Troyerdde41d02014-12-09 17:47:57 -06001243 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1244 echo "LOGDIR: $LOGDIR"
1245 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1246 echo "log: $real_logfile"
1247 if [[ -n ${LOGDIR} ]]; then
1248 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001249 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001250 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1251 if [[ -n ${SCREEN_LOGDIR} ]]; then
1252 # Drop the backward-compat symlink
1253 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1254 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001255 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001256
1257 # sleep to allow bash to be ready to be send the command - we are
1258 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001259 # bash isn't running by the time we send the command, nothing
1260 # happens. This sleep was added originally to handle gate runs
1261 # where we needed this to be at least 3 seconds to pass
1262 # consistently on slow clouds. Now this is configurable so that we
1263 # can determine a reasonable value for the local case which should
1264 # be much smaller.
1265 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001266
1267 NL=`echo -ne '\015'`
1268 # This fun command does the following:
1269 # - the passed server command is backgrounded
1270 # - the pid of the background process is saved in the usual place
1271 # - the server process is brought back to the foreground
1272 # - if the server process exits prematurely the fg command errors
1273 # and a message is written to stdout and the process failure file
1274 #
1275 # The pid saved can be used in stop_process() as a process group
1276 # id to kill off all child processes
1277 if [[ -n "$group" ]]; then
1278 command="sg $group '$command'"
1279 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001280
1281 # Append the process to the screen rc file
1282 screen_rc "$name" "$command"
1283
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001284 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 -06001285}
1286
1287# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001288# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001289# screen_rc service "command-line"
1290function screen_rc {
1291 SCREEN_NAME=${SCREEN_NAME:-stack}
1292 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1293 if [[ ! -e $SCREENRC ]]; then
1294 # Name the screen session
1295 echo "sessionname $SCREEN_NAME" > $SCREENRC
1296 # Set a reasonable statusbar
1297 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1298 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1299 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1300 echo "screen -t shell bash" >> $SCREENRC
1301 fi
1302 # If this service doesn't already exist in the screenrc file
1303 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1304 NL=`echo -ne '\015'`
1305 echo "screen -t $1 bash" >> $SCREENRC
1306 echo "stuff \"$2$NL\"" >> $SCREENRC
1307
Dean Troyerdde41d02014-12-09 17:47:57 -06001308 if [[ -n ${LOGDIR} ]]; then
1309 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001310 echo "log on" >>$SCREENRC
1311 fi
1312 fi
1313}
1314
1315# Stop a service in screen
1316# If a PID is available use it, kill the whole process group via TERM
1317# If screen is being used kill the screen window; this will catch processes
1318# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001319# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001320# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001321function screen_stop_service {
1322 local service=$1
1323
Dean Troyerdff49a22014-01-30 15:37:40 -06001324 SCREEN_NAME=${SCREEN_NAME:-stack}
1325 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001326 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001327
Dean Troyer3159a822014-08-27 14:13:58 -05001328 if is_service_enabled $service; then
1329 # Clean up the screen window
1330 screen -S $SCREEN_NAME -p $service -X kill
1331 fi
1332}
1333
1334# Stop a service process
1335# If a PID is available use it, kill the whole process group via TERM
1336# If screen is being used kill the screen window; this will catch processes
1337# that did not leave a PID behind
1338# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1339# stop_process service
1340function stop_process {
1341 local service=$1
1342
1343 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001344 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001345
1346 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001347 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001348 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1349 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1350 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001351 fi
1352 if [[ "$USE_SCREEN" = "True" ]]; then
1353 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001354 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001355 fi
1356 fi
1357}
1358
1359# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001360# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001361# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001362function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001363 local service
1364 local failures
1365 SCREEN_NAME=${SCREEN_NAME:-stack}
1366 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1367
1368
1369 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1370 echo "No service status directory found"
1371 return
1372 fi
1373
1374 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001375 # make this -o errexit safe
1376 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001377
1378 for service in $failures; do
1379 service=`basename $service`
1380 service=${service%.failure}
1381 echo "Error: Service $service is not running"
1382 done
1383
1384 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001385 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001386 fi
1387}
1388
Chris Dent2f27a0e2014-09-09 13:46:02 +01001389# Tail a log file in a screen if USE_SCREEN is true.
1390function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001391 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001392 local logfile=$2
1393
Sean Dague53753292014-12-04 19:38:15 -05001394 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001395 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001396 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001397 fi
1398}
1399
Dean Troyerdff49a22014-01-30 15:37:40 -06001400
Dean Troyer3159a822014-08-27 14:13:58 -05001401# Deprecated Functions
1402# --------------------
1403
1404# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1405# fork. It includes the dirty work of closing extra filehandles and preparing log
1406# files to produce the same logs as screen_it(). The log filename is derived
1407# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1408# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1409# _old_run_process service "command-line"
1410function _old_run_process {
1411 local service=$1
1412 local command="$2"
1413
1414 # Undo logging redirections and close the extra descriptors
1415 exec 1>&3
1416 exec 2>&3
1417 exec 3>&-
1418 exec 6>&-
1419
1420 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001421 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1422 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001423
1424 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1425 export PYTHONUNBUFFERED=1
1426 fi
1427
1428 exec /bin/bash -c "$command"
1429 die "$service exec failure: $command"
1430}
1431
1432# old_run_process() launches a child process that closes all file descriptors and
1433# then exec's the passed in command. This is meant to duplicate the semantics
1434# of screen_it() without screen. PIDs are written to
1435# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1436# old_run_process service "command-line"
1437function old_run_process {
1438 local service=$1
1439 local command="$2"
1440
1441 # Spawn the child process
1442 _old_run_process "$service" "$command" &
1443 echo $!
1444}
1445
1446# Compatibility for existing start_XXXX() functions
1447# Uses global ``USE_SCREEN``
1448# screen_it service "command-line"
1449function screen_it {
1450 if is_service_enabled $1; then
1451 # Append the service to the screen rc file
1452 screen_rc "$1" "$2"
1453
1454 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001455 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001456 else
1457 # Spawn directly without screen
1458 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1459 fi
1460 fi
1461}
1462
1463# Compatibility for existing stop_XXXX() functions
1464# Stop a service in screen
1465# If a PID is available use it, kill the whole process group via TERM
1466# If screen is being used kill the screen window; this will catch processes
1467# that did not leave a PID behind
1468# screen_stop service
1469function screen_stop {
1470 # Clean up the screen window
1471 stop_process $1
1472}
1473
1474
Sean Dague2c65e712014-12-18 09:44:56 -05001475# Plugin Functions
1476# =================
1477
1478DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1479
1480# enable_plugin <name> <url> [branch]
1481#
1482# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1483# ``url`` is a git url
1484# ``branch`` is a gitref. If it's not set, defaults to master
1485function enable_plugin {
1486 local name=$1
1487 local url=$2
1488 local branch=${3:-master}
1489 DEVSTACK_PLUGINS+=",$name"
1490 GITREPO[$name]=$url
1491 GITDIR[$name]=$DEST/$name
1492 GITBRANCH[$name]=$branch
1493}
1494
1495# fetch_plugins
1496#
1497# clones all plugins
1498function fetch_plugins {
1499 local plugins="${DEVSTACK_PLUGINS}"
1500 local plugin
1501
1502 # short circuit if nothing to do
1503 if [[ -z $plugins ]]; then
1504 return
1505 fi
1506
Dean Troyerdc97cb72015-03-28 08:20:50 -05001507 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001508 for plugin in ${plugins//,/ }; do
1509 git_clone_by_name $plugin
1510 done
1511}
1512
1513# load_plugin_settings
1514#
1515# Load settings from plugins in the order that they were registered
1516function load_plugin_settings {
1517 local plugins="${DEVSTACK_PLUGINS}"
1518 local plugin
1519
1520 # short circuit if nothing to do
1521 if [[ -z $plugins ]]; then
1522 return
1523 fi
1524
1525 echo "Loading plugin settings"
1526 for plugin in ${plugins//,/ }; do
1527 local dir=${GITDIR[$plugin]}
1528 # source any known settings
1529 if [[ -f $dir/devstack/settings ]]; then
1530 source $dir/devstack/settings
1531 fi
1532 done
1533}
1534
Sean Dague6e275e12015-03-26 05:54:28 -04001535# plugin_override_defaults
1536#
1537# Run an extremely early setting phase for plugins that allows default
1538# overriding of services.
1539function plugin_override_defaults {
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 "Overriding Configuration Defaults"
1549 for plugin in ${plugins//,/ }; do
1550 local dir=${GITDIR[$plugin]}
1551 # source any overrides
1552 if [[ -f $dir/devstack/override-defaults ]]; then
1553 # be really verbose that an override is happening, as it
1554 # may not be obvious if things fail later.
1555 echo "$plugin has overriden the following defaults"
1556 cat $dir/devstack/override-defaults
1557 source $dir/devstack/override-defaults
1558 fi
1559 done
1560}
1561
Sean Dague2c65e712014-12-18 09:44:56 -05001562# run_plugins
1563#
1564# Run the devstack/plugin.sh in all the plugin directories. These are
1565# run in registration order.
1566function run_plugins {
1567 local mode=$1
1568 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301569
1570 local plugins="${DEVSTACK_PLUGINS}"
1571 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001572 for plugin in ${plugins//,/ }; do
1573 local dir=${GITDIR[$plugin]}
1574 if [[ -f $dir/devstack/plugin.sh ]]; then
1575 source $dir/devstack/plugin.sh $mode $phase
1576 fi
1577 done
1578}
1579
1580function run_phase {
1581 local mode=$1
1582 local phase=$2
1583 if [[ -d $TOP_DIR/extras.d ]]; then
1584 for i in $TOP_DIR/extras.d/*.sh; do
1585 [[ -r $i ]] && source $i $mode $phase
1586 done
1587 fi
1588 # the source phase corresponds to settings loading in plugins
1589 if [[ "$mode" == "source" ]]; then
1590 load_plugin_settings
Sean Dague6e275e12015-03-26 05:54:28 -04001591 elif [[ "$mode" == "override_defaults" ]]; then
1592 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001593 else
1594 run_plugins $mode $phase
1595 fi
1596}
1597
Dean Troyerdff49a22014-01-30 15:37:40 -06001598
1599# Service Functions
1600# =================
1601
1602# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1603# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001604function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001605 echo "$1" | sed -e '
1606 s/,,/,/g;
1607 s/^,//;
1608 s/,$//
1609 '
1610}
1611
1612# disable_all_services() removes all current services
1613# from ``ENABLED_SERVICES`` to reset the configuration
1614# before a minimal installation
1615# Uses global ``ENABLED_SERVICES``
1616# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001617function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001618 ENABLED_SERVICES=""
1619}
1620
1621# Remove all services starting with '-'. For example, to install all default
1622# services except rabbit (rabbit) set in ``localrc``:
1623# ENABLED_SERVICES+=",-rabbit"
1624# Uses global ``ENABLED_SERVICES``
1625# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001626function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001627 local to_remove=""
1628 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001629 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001630
1631 # build up list of services that should be removed; i.e. they
1632 # begin with "-"
1633 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001634 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001635 to_remove+=",${service#-}"
1636 else
1637 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001638 fi
1639 done
Ian Wienand2796a822015-04-15 08:59:04 +10001640
1641 # go through the service list. if this service appears in the "to
1642 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001643 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Dean Troyerdff49a22014-01-30 15:37:40 -06001644}
1645
1646# disable_service() removes the services passed as argument to the
1647# ``ENABLED_SERVICES`` list, if they are present.
1648#
1649# For example:
1650# disable_service rabbit
1651#
1652# This function does not know about the special cases
1653# for nova, glance, and neutron built into is_service_enabled().
1654# Uses global ``ENABLED_SERVICES``
1655# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001656function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001657 local tmpsvcs=",${ENABLED_SERVICES},"
1658 local service
1659 for service in $@; do
1660 if is_service_enabled $service; then
1661 tmpsvcs=${tmpsvcs//,$service,/,}
1662 fi
1663 done
1664 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1665}
1666
1667# enable_service() adds the services passed as argument to the
1668# ``ENABLED_SERVICES`` list, if they are not already present.
1669#
1670# For example:
1671# enable_service qpid
1672#
1673# This function does not know about the special cases
1674# for nova, glance, and neutron built into is_service_enabled().
1675# Uses global ``ENABLED_SERVICES``
1676# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001677function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001678 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001679 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001680 for service in $@; do
1681 if ! is_service_enabled $service; then
1682 tmpsvcs+=",$service"
1683 fi
1684 done
1685 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1686 disable_negated_services
1687}
1688
1689# is_service_enabled() checks if the service(s) specified as arguments are
1690# enabled by the user in ``ENABLED_SERVICES``.
1691#
1692# Multiple services specified as arguments are ``OR``'ed together; the test
1693# is a short-circuit boolean, i.e it returns on the first match.
1694#
1695# There are special cases for some 'catch-all' services::
1696# **nova** returns true if any service enabled start with **n-**
1697# **cinder** returns true if any service enabled start with **c-**
1698# **ceilometer** returns true if any service enabled start with **ceilometer**
1699# **glance** returns true if any service enabled start with **g-**
1700# **neutron** returns true if any service enabled start with **q-**
1701# **swift** returns true if any service enabled start with **s-**
1702# **trove** returns true if any service enabled start with **tr-**
1703# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1704# **s-** services will be enabled. This will be deprecated in the future.
1705#
1706# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1707# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1708# as enabled in this case.
1709#
1710# Uses global ``ENABLED_SERVICES``
1711# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001712function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001713 local xtrace=$(set +o | grep xtrace)
1714 set +o xtrace
1715 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001716 local services=$@
1717 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001718 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001719 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001720
1721 # Look for top-level 'enabled' function for this service
1722 if type is_${service}_enabled >/dev/null 2>&1; then
1723 # A function exists for this service, use it
1724 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001725 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001726 fi
1727
1728 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1729 # are implemented
1730
Sean Dague45917cc2014-02-24 16:09:14 -05001731 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001732 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001733 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1734 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1735 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1736 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1737 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1738 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1739 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1740 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1741 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001742 done
Sean Dague45917cc2014-02-24 16:09:14 -05001743 $xtrace
1744 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001745}
1746
fumihiko kakuma8606c982015-04-13 09:55:06 +09001747# remove specified list from the input string
1748# remove_disabled_services service-list remove-list
1749function remove_disabled_services {
1750 local service_list=$1
1751 local remove_list=$2
1752 local service
1753 local enabled=""
1754
1755 for service in ${service_list//,/ }; do
1756 local remove
1757 local add=1
1758 for remove in ${remove_list//,/ }; do
1759 if [[ ${remove} == ${service} ]]; then
1760 add=0
1761 break
1762 fi
1763 done
1764 if [[ $add == 1 ]]; then
1765 enabled="${enabled},$service"
1766 fi
1767 done
1768 _cleanup_service_list "$enabled"
1769}
1770
Dean Troyerdff49a22014-01-30 15:37:40 -06001771# Toggle enable/disable_service for services that must run exclusive of each other
1772# $1 The name of a variable containing a space-separated list of services
1773# $2 The name of a variable in which to store the enabled service's name
1774# $3 The name of the service to enable
1775function use_exclusive_service {
1776 local options=${!1}
1777 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001778 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001779 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001780 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001781 for opt in $options;do
1782 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1783 done
1784 eval "$out=$selection"
1785 return 0
1786}
1787
1788
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001789# System Functions
1790# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001791
1792# Only run the command if the target file (the last arg) is not on an
1793# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001794function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001795 local xtrace=$(set +o | grep xtrace)
1796 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001797 local args=( $@ )
1798 local last
1799 local sudo_cmd
1800 local dir_to_check
1801
1802 let last="${#args[*]} - 1"
1803
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001804 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001805 if [ ! -d "$dir_to_check" ]; then
1806 dir_to_check=`dirname "$dir_to_check"`
1807 fi
1808
1809 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001810 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001811 return 0
1812 fi
1813
1814 if [[ $TRACK_DEPENDS = True ]]; then
1815 sudo_cmd="env"
1816 else
1817 sudo_cmd="sudo"
1818 fi
1819
Sean Dague45917cc2014-02-24 16:09:14 -05001820 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001821 $sudo_cmd $@
1822}
1823
1824# Exit 0 if address is in network or 1 if address is not in network
1825# ip-range is in CIDR notation: 1.2.3.4/20
1826# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001827function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001828 local ip=$1
1829 local range=$2
1830 local masklen=${range#*/}
1831 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1832 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1833 [[ $network == $subnet ]]
1834}
1835
1836# Add a user to a group.
1837# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001838function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001839 local user=$1
1840 local group=$2
1841
Thomas Bechtolda8580852015-05-31 00:04:33 +02001842 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06001843}
1844
1845# Convert CIDR notation to a IPv4 netmask
1846# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001847function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001848 local maskpat="255 255 255 255"
1849 local maskdgt="254 252 248 240 224 192 128"
1850 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1851 echo ${1-0}.${2-0}.${3-0}.${4-0}
1852}
1853
1854# Gracefully cp only if source file/dir exists
1855# cp_it source destination
1856function cp_it {
1857 if [ -e $1 ] || [ -d $1 ]; then
1858 cp -pRL $1 $2
1859 fi
1860}
1861
1862# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1863# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1864# ``localrc`` or on the command line if necessary::
1865#
1866# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1867#
1868# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1869
Ian Wienandaee18c72014-02-21 15:35:08 +11001870function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001871 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001872 export http_proxy=$http_proxy
1873 fi
Sean Dague53753292014-12-04 19:38:15 -05001874 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001875 export https_proxy=$https_proxy
1876 fi
Sean Dague53753292014-12-04 19:38:15 -05001877 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001878 export no_proxy=$no_proxy
1879 fi
1880}
1881
1882# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001883function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001884 local mount_type=`stat -f -L -c %T $1`
1885 test "$mount_type" == "nfs"
1886}
1887
1888# Return the network portion of the given IP address using netmask
1889# netmask is in the traditional dotted-quad format
1890# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001891function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001892 local ip=$1
1893 local mask=$2
1894 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1895 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1896 echo $subnet
1897}
1898
Chris Dent3a2c86a2015-05-12 13:41:25 +00001899# Return the current python as "python<major>.<minor>"
1900function python_version {
1901 local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
1902 echo "python${python_version}"
1903}
1904
Dean Troyerdff49a22014-01-30 15:37:40 -06001905# Service wrapper to restart services
1906# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001907function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001908 if is_ubuntu; then
1909 sudo /usr/sbin/service $1 restart
1910 else
1911 sudo /sbin/service $1 restart
1912 fi
1913}
1914
1915# Only change permissions of a file or directory if it is not on an
1916# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001917function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001918 _safe_permission_operation chmod $@
1919}
1920
1921# Only change ownership of a file or directory if it is not on an NFS
1922# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001923function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001924 _safe_permission_operation chown $@
1925}
1926
1927# Service wrapper to start services
1928# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001929function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001930 if is_ubuntu; then
1931 sudo /usr/sbin/service $1 start
1932 else
1933 sudo /sbin/service $1 start
1934 fi
1935}
1936
1937# Service wrapper to stop services
1938# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001939function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001940 if is_ubuntu; then
1941 sudo /usr/sbin/service $1 stop
1942 else
1943 sudo /sbin/service $1 stop
1944 fi
1945}
1946
1947
1948# Restore xtrace
1949$XTRACE
1950
1951# Local variables:
1952# mode: shell-script
1953# End: