blob: 48c0c756375eda774034a9aad712ec49c0dcd2fd [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
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000678# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500679function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500680 local desc="${3:-}"
681 local os_url="$KEYSTONE_SERVICE_URI_V3"
682 # Gets group id
683 local group_id=$(
684 # Creates new group with --or-show
685 openstack --os-token=$OS_TOKEN --os-url=$os_url \
686 --os-identity-api-version=3 group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000687 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500688 -f value -c id
689 )
690 echo $group_id
691}
692
Bartosz Górski0abde392014-02-28 14:15:19 +0100693# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000694# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100695function get_or_create_user {
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000696 if [[ ! -z "$4" ]]; then
697 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200698 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500699 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200700 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100701 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500702 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500703 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000704 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100705 $1 \
706 --password "$2" \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000707 --os-url=$KEYSTONE_SERVICE_URI_V3 \
708 --os-identity-api-version=3 \
709 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500710 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500711 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100712 -f value -c id
713 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500714 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100715}
716
717# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000718# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100719function get_or_create_project {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500720 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500721 # Creates new project with --or-show
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000722 openstack --os-url=$KEYSTONE_SERVICE_URI_V3 \
723 --os-identity-api-version=3 \
724 project create $1 \
725 --domain=$2 \
726 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100727 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500728 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100729}
730
731# Gets or creates role
732# Usage: get_or_create_role <name>
733function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500734 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500735 # Creates role with --or-show
736 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100737 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500738 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100739}
740
Jamie Lennox9b215db2015-02-10 18:19:57 +1100741# Gets or adds user role to project
742# Usage: get_or_add_user_project_role <role> <user> <project>
743function get_or_add_user_project_role {
Bartosz Górski0abde392014-02-28 14:15:19 +0100744 # Gets user role id
Steve Martinelli5541a612015-01-19 15:58:49 -0500745 local user_role_id=$(openstack role list \
746 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100747 --project $3 \
748 --column "ID" \
749 --column "Name" \
750 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500751 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100752 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500753 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100754 $1 \
755 --user $2 \
756 --project $3 \
757 | grep " id " | get_field 2)
758 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500759 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100760}
761
Steve Martinelli4599fd12015-03-12 21:30:58 -0400762# Gets or adds group role to project
763# Usage: get_or_add_group_project_role <role> <group> <project>
764function get_or_add_group_project_role {
765 # Gets group role id
766 local group_role_id=$(openstack role list \
767 --group $2 \
768 --project $3 \
769 --column "ID" \
770 --column "Name" \
771 | grep " $1 " | get_field 1)
772 if [[ -z "$group_role_id" ]]; then
773 # Adds role to group
774 group_role_id=$(openstack role add \
775 $1 \
776 --group $2 \
777 --project $3 \
778 | grep " id " | get_field 2)
779 fi
780 echo $group_role_id
781}
782
Bartosz Górski0abde392014-02-28 14:15:19 +0100783# Gets or creates service
784# Usage: get_or_create_service <name> <type> <description>
785function get_or_create_service {
786 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500787 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100788 # Gets service id
789 openstack service show $1 -f value -c id 2>/dev/null ||
790 # Creates new service if not exists
791 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500792 $2 \
793 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100794 --description="$3" \
795 -f value -c id
796 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500797 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100798}
799
800# Gets or creates endpoint
801# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
802function get_or_create_endpoint {
803 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500804 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100805 --column "ID" \
806 --column "Region" \
807 --column "Service Name" \
808 | grep " $2 " \
809 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500810 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100811 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500812 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100813 $1 \
814 --region $2 \
815 --publicurl $3 \
816 --adminurl $4 \
817 --internalurl $5 \
818 | grep " id " | get_field 2)
819 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500820 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100821}
Dean Troyerdff49a22014-01-30 15:37:40 -0600822
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500823
Dean Troyerdff49a22014-01-30 15:37:40 -0600824# Package Functions
825# =================
826
827# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100828function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800829 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600830 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800831
832 if [[ -z "$base_dir" ]]; then
833 base_dir=$FILES
834 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600835 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800836 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600837 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800838 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600839 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800840 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600841 else
842 exit_distro_not_supported "list of packages"
843 fi
844 echo "$pkg_dir"
845}
846
847# Wrapper for ``apt-get`` to set cache and proxy environment variables
848# Uses globals ``OFFLINE``, ``*_proxy``
849# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100850function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500851 local xtrace=$(set +o | grep xtrace)
852 set +o xtrace
853
Dean Troyerdff49a22014-01-30 15:37:40 -0600854 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
855 local sudo="sudo"
856 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500857
858 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500859
Dean Troyerdff49a22014-01-30 15:37:40 -0600860 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500861 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
862 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600863 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
864}
865
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800866function _parse_package_files {
867 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600868
Dean Troyerdff49a22014-01-30 15:37:40 -0600869 if [[ -z "$DISTRO" ]]; then
870 GetDistro
871 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600872
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800873 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600874 local OIFS line package distros distro
875 [[ -e $fname ]] || continue
876
877 OIFS=$IFS
878 IFS=$'\n'
879 for line in $(<${fname}); do
880 if [[ $line =~ "NOPRIME" ]]; then
881 continue
882 fi
883
884 # Assume we want this package
885 package=${line%#*}
886 inst_pkg=1
887
888 # Look for # dist:xxx in comment
889 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
890 # We are using BASH regexp matching feature.
891 package=${BASH_REMATCH[1]}
892 distros=${BASH_REMATCH[2]}
893 # In bash ${VAR,,} will lowecase VAR
894 # Look for a match in the distro list
895 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
896 # If no match then skip this package
897 inst_pkg=0
898 fi
899 fi
900
Dean Troyerdff49a22014-01-30 15:37:40 -0600901 if [[ $inst_pkg = 1 ]]; then
902 echo $package
903 fi
904 done
905 IFS=$OIFS
906 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800907}
908
909# get_packages() collects a list of package names of any type from the
910# prerequisite files in ``files/{debs|rpms}``. The list is intended
911# to be passed to a package installer such as apt or yum.
912#
913# Only packages required for the services in 1st argument will be
914# included. Two bits of metadata are recognized in the prerequisite files:
915#
916# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
917# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
918# of the package to the distros listed. The distro names are case insensitive.
919function get_packages {
920 local xtrace=$(set +o | grep xtrace)
921 set +o xtrace
922 local services=$@
923 local package_dir=$(_get_package_dir)
924 local file_to_parse=""
925 local service=""
926
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800927 if [[ -z "$package_dir" ]]; then
928 echo "No package directory supplied"
929 return 1
930 fi
931 for service in ${services//,/ }; do
932 # Allow individual services to specify dependencies
933 if [[ -e ${package_dir}/${service} ]]; then
934 file_to_parse="${file_to_parse} ${package_dir}/${service}"
935 fi
936 # NOTE(sdague) n-api needs glance for now because that's where
937 # glance client is
938 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700939 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800940 file_to_parse="${file_to_parse} ${package_dir}/nova"
941 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700942 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800943 file_to_parse="${file_to_parse} ${package_dir}/glance"
944 fi
945 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700946 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800947 file_to_parse="${file_to_parse} ${package_dir}/cinder"
948 fi
949 elif [[ $service == ceilometer-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700950 if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800951 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
952 fi
953 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700954 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800955 file_to_parse="${file_to_parse} ${package_dir}/swift"
956 fi
957 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700958 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800959 file_to_parse="${file_to_parse} ${package_dir}/nova"
960 fi
961 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700962 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800963 file_to_parse="${file_to_parse} ${package_dir}/glance"
964 fi
965 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700966 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800967 file_to_parse="${file_to_parse} ${package_dir}/keystone"
968 fi
969 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700970 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800971 file_to_parse="${file_to_parse} ${package_dir}/neutron"
972 fi
973 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700974 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800975 file_to_parse="${file_to_parse} ${package_dir}/ironic"
976 fi
977 fi
978 done
979 echo "$(_parse_package_files $file_to_parse)"
980 $xtrace
981}
982
983# get_plugin_packages() collects a list of package names of any type from a
984# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
985# list is intended to be passed to a package installer such as apt or yum.
986#
987# Only packages required for enabled and collected plugins will included.
988#
Dean Troyerdc97cb72015-03-28 08:20:50 -0500989# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800990# in these prerequisite files, see get_packages() for more info.
991function get_plugin_packages {
992 local xtrace=$(set +o | grep xtrace)
993 set +o xtrace
994 local files_to_parse=""
995 local package_dir=""
996 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
997 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
998 files_to_parse+="$package_dir/$plugin"
999 done
1000 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001001 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001002}
1003
1004# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001005# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001006# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001007function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001008 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1009 REPOS_UPDATED=${REPOS_UPDATED:-False}
1010 RETRY_UPDATE=${RETRY_UPDATE:-False}
1011
Paul Linchpiner9e179742014-07-13 22:23:00 -07001012 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001013 return 0
1014 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001015
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001016 if is_ubuntu; then
1017 local xtrace=$(set +o | grep xtrace)
1018 set +o xtrace
1019 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1020 # if there are transient errors pulling the updates, that's fine.
1021 # It may be secondary repositories that we don't really care about.
1022 apt_get update || /bin/true
1023 REPOS_UPDATED=True
1024 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001025 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001026 fi
1027}
1028
1029function real_install_package {
1030 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001031 apt_get install "$@"
1032 elif is_fedora; then
1033 yum_install "$@"
1034 elif is_suse; then
1035 zypper_install "$@"
1036 else
1037 exit_distro_not_supported "installing packages"
1038 fi
1039}
1040
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001041# Distro-agnostic package installer
1042# install_package package [package ...]
1043function install_package {
1044 update_package_repo
1045 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1046}
1047
Dean Troyerdff49a22014-01-30 15:37:40 -06001048# Distro-agnostic function to tell if a package is installed
1049# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001050function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001051 if [[ -z "$@" ]]; then
1052 return 1
1053 fi
1054
1055 if [[ -z "$os_PACKAGE" ]]; then
1056 GetOSVersion
1057 fi
1058
1059 if [[ "$os_PACKAGE" = "deb" ]]; then
1060 dpkg -s "$@" > /dev/null 2> /dev/null
1061 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1062 rpm --quiet -q "$@"
1063 else
1064 exit_distro_not_supported "finding if a package is installed"
1065 fi
1066}
1067
1068# Distro-agnostic package uninstaller
1069# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001070function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001071 if is_ubuntu; then
1072 apt_get purge "$@"
1073 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001074 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001075 elif is_suse; then
1076 sudo zypper rm "$@"
1077 else
1078 exit_distro_not_supported "uninstalling packages"
1079 fi
1080}
1081
1082# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001083# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001084# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001085function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001086 [[ "$OFFLINE" = "True" ]] && return
1087 local sudo="sudo"
1088 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001089
1090 # The manual check for missing packages is because yum -y assumes
1091 # missing packages are OK. See
1092 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001093 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1094 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001095 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001096 awk '
1097 BEGIN { fail=0 }
1098 /No package/ { fail=1 }
1099 { print }
1100 END { exit fail }' || \
1101 die $LINENO "Missing packages detected"
1102
1103 # also ensure we catch a yum failure
1104 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001105 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001106 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001107}
1108
1109# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001110# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001111# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001112function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001113 [[ "$OFFLINE" = "True" ]] && return
1114 local sudo="sudo"
1115 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001116 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1117 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001118 zypper --non-interactive install --auto-agree-with-licenses "$@"
1119}
1120
1121
1122# Process Functions
1123# =================
1124
1125# _run_process() is designed to be backgrounded by run_process() to simulate a
1126# fork. It includes the dirty work of closing extra filehandles and preparing log
1127# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001128# from the service name.
1129# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001130# If an optional group is provided sg will be used to set the group of
1131# the command.
1132# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001133function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001134 # disable tracing through the exec redirects, it's just confusing in the logs.
1135 xtrace=$(set +o | grep xtrace)
1136 set +o xtrace
1137
Dean Troyerdff49a22014-01-30 15:37:40 -06001138 local service=$1
1139 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001140 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001141
1142 # Undo logging redirections and close the extra descriptors
1143 exec 1>&3
1144 exec 2>&3
1145 exec 3>&-
1146 exec 6>&-
1147
Dean Troyerdde41d02014-12-09 17:47:57 -06001148 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1149 if [[ -n ${LOGDIR} ]]; then
1150 exec 1>&"$real_logfile" 2>&1
1151 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1152 if [[ -n ${SCREEN_LOGDIR} ]]; then
1153 # Drop the backward-compat symlink
1154 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1155 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001156
1157 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1158 export PYTHONUNBUFFERED=1
1159 fi
1160
Sean Dague6e137ab2015-04-29 08:22:24 -04001161 # reenable xtrace before we do *real* work
1162 $xtrace
1163
Dean Troyer3159a822014-08-27 14:13:58 -05001164 # Run under ``setsid`` to force the process to become a session and group leader.
1165 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001166 if [[ -n "$group" ]]; then
1167 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1168 else
1169 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1170 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001171
1172 # Just silently exit this process
1173 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001174}
1175
1176# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1177# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001178# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001179# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001180function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001181 SCREEN_NAME=${SCREEN_NAME:-stack}
1182 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1183
1184 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1185 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1186 fi
1187
1188 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1189}
1190
1191# Find out if a process exists by partial name.
1192# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001193function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001194 local name=$1
1195 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001196 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001197 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001198 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001199}
1200
Dean Troyer3159a822014-08-27 14:13:58 -05001201# Run a single service under screen or directly
1202# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001203# If an optional group is provided sg will be used to run the
1204# command as that group.
1205# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001206function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001207 local service=$1
1208 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001209 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001210
Dean Troyer3159a822014-08-27 14:13:58 -05001211 if is_service_enabled $service; then
1212 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001213 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001214 else
1215 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001216 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001217 fi
1218 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001219}
1220
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001221# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001222# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001223# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001224# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001225# Run a command in a shell in a screen window, if an optional group
1226# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001227function screen_process {
1228 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001229 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001230 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001231
Sean Dagueea22a4f2014-06-27 15:21:41 -04001232 SCREEN_NAME=${SCREEN_NAME:-stack}
1233 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001234 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001235
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001236 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001237
Dean Troyerdde41d02014-12-09 17:47:57 -06001238 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1239 echo "LOGDIR: $LOGDIR"
1240 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1241 echo "log: $real_logfile"
1242 if [[ -n ${LOGDIR} ]]; then
1243 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001244 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001245 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1246 if [[ -n ${SCREEN_LOGDIR} ]]; then
1247 # Drop the backward-compat symlink
1248 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1249 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001250 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001251
1252 # sleep to allow bash to be ready to be send the command - we are
1253 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001254 # bash isn't running by the time we send the command, nothing
1255 # happens. This sleep was added originally to handle gate runs
1256 # where we needed this to be at least 3 seconds to pass
1257 # consistently on slow clouds. Now this is configurable so that we
1258 # can determine a reasonable value for the local case which should
1259 # be much smaller.
1260 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001261
1262 NL=`echo -ne '\015'`
1263 # This fun command does the following:
1264 # - the passed server command is backgrounded
1265 # - the pid of the background process is saved in the usual place
1266 # - the server process is brought back to the foreground
1267 # - if the server process exits prematurely the fg command errors
1268 # and a message is written to stdout and the process failure file
1269 #
1270 # The pid saved can be used in stop_process() as a process group
1271 # id to kill off all child processes
1272 if [[ -n "$group" ]]; then
1273 command="sg $group '$command'"
1274 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001275
1276 # Append the process to the screen rc file
1277 screen_rc "$name" "$command"
1278
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001279 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 -06001280}
1281
1282# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001283# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001284# screen_rc service "command-line"
1285function screen_rc {
1286 SCREEN_NAME=${SCREEN_NAME:-stack}
1287 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1288 if [[ ! -e $SCREENRC ]]; then
1289 # Name the screen session
1290 echo "sessionname $SCREEN_NAME" > $SCREENRC
1291 # Set a reasonable statusbar
1292 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1293 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1294 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1295 echo "screen -t shell bash" >> $SCREENRC
1296 fi
1297 # If this service doesn't already exist in the screenrc file
1298 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1299 NL=`echo -ne '\015'`
1300 echo "screen -t $1 bash" >> $SCREENRC
1301 echo "stuff \"$2$NL\"" >> $SCREENRC
1302
Dean Troyerdde41d02014-12-09 17:47:57 -06001303 if [[ -n ${LOGDIR} ]]; then
1304 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001305 echo "log on" >>$SCREENRC
1306 fi
1307 fi
1308}
1309
1310# Stop a service in screen
1311# If a PID is available use it, kill the whole process group via TERM
1312# If screen is being used kill the screen window; this will catch processes
1313# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001314# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001315# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001316function screen_stop_service {
1317 local service=$1
1318
Dean Troyerdff49a22014-01-30 15:37:40 -06001319 SCREEN_NAME=${SCREEN_NAME:-stack}
1320 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001321 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001322
Dean Troyer3159a822014-08-27 14:13:58 -05001323 if is_service_enabled $service; then
1324 # Clean up the screen window
1325 screen -S $SCREEN_NAME -p $service -X kill
1326 fi
1327}
1328
1329# Stop a service process
1330# If a PID is available use it, kill the whole process group via TERM
1331# If screen is being used kill the screen window; this will catch processes
1332# that did not leave a PID behind
1333# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1334# stop_process service
1335function stop_process {
1336 local service=$1
1337
1338 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001339 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001340
1341 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001342 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001343 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1344 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1345 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001346 fi
1347 if [[ "$USE_SCREEN" = "True" ]]; then
1348 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001349 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001350 fi
1351 fi
1352}
1353
1354# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001355# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001356# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001357function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001358 local service
1359 local failures
1360 SCREEN_NAME=${SCREEN_NAME:-stack}
1361 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1362
1363
1364 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1365 echo "No service status directory found"
1366 return
1367 fi
1368
1369 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001370 # make this -o errexit safe
1371 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001372
1373 for service in $failures; do
1374 service=`basename $service`
1375 service=${service%.failure}
1376 echo "Error: Service $service is not running"
1377 done
1378
1379 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001380 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001381 fi
1382}
1383
Chris Dent2f27a0e2014-09-09 13:46:02 +01001384# Tail a log file in a screen if USE_SCREEN is true.
1385function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001386 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001387 local logfile=$2
1388
Sean Dague53753292014-12-04 19:38:15 -05001389 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001390 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001391 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001392 fi
1393}
1394
Dean Troyerdff49a22014-01-30 15:37:40 -06001395
Dean Troyer3159a822014-08-27 14:13:58 -05001396# Deprecated Functions
1397# --------------------
1398
1399# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1400# fork. It includes the dirty work of closing extra filehandles and preparing log
1401# files to produce the same logs as screen_it(). The log filename is derived
1402# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1403# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1404# _old_run_process service "command-line"
1405function _old_run_process {
1406 local service=$1
1407 local command="$2"
1408
1409 # Undo logging redirections and close the extra descriptors
1410 exec 1>&3
1411 exec 2>&3
1412 exec 3>&-
1413 exec 6>&-
1414
1415 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001416 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1417 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001418
1419 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1420 export PYTHONUNBUFFERED=1
1421 fi
1422
1423 exec /bin/bash -c "$command"
1424 die "$service exec failure: $command"
1425}
1426
1427# old_run_process() launches a child process that closes all file descriptors and
1428# then exec's the passed in command. This is meant to duplicate the semantics
1429# of screen_it() without screen. PIDs are written to
1430# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1431# old_run_process service "command-line"
1432function old_run_process {
1433 local service=$1
1434 local command="$2"
1435
1436 # Spawn the child process
1437 _old_run_process "$service" "$command" &
1438 echo $!
1439}
1440
1441# Compatibility for existing start_XXXX() functions
1442# Uses global ``USE_SCREEN``
1443# screen_it service "command-line"
1444function screen_it {
1445 if is_service_enabled $1; then
1446 # Append the service to the screen rc file
1447 screen_rc "$1" "$2"
1448
1449 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001450 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001451 else
1452 # Spawn directly without screen
1453 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1454 fi
1455 fi
1456}
1457
1458# Compatibility for existing stop_XXXX() functions
1459# Stop a service in screen
1460# If a PID is available use it, kill the whole process group via TERM
1461# If screen is being used kill the screen window; this will catch processes
1462# that did not leave a PID behind
1463# screen_stop service
1464function screen_stop {
1465 # Clean up the screen window
1466 stop_process $1
1467}
1468
1469
Sean Dague2c65e712014-12-18 09:44:56 -05001470# Plugin Functions
1471# =================
1472
1473DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1474
1475# enable_plugin <name> <url> [branch]
1476#
1477# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1478# ``url`` is a git url
1479# ``branch`` is a gitref. If it's not set, defaults to master
1480function enable_plugin {
1481 local name=$1
1482 local url=$2
1483 local branch=${3:-master}
1484 DEVSTACK_PLUGINS+=",$name"
1485 GITREPO[$name]=$url
1486 GITDIR[$name]=$DEST/$name
1487 GITBRANCH[$name]=$branch
1488}
1489
1490# fetch_plugins
1491#
1492# clones all plugins
1493function fetch_plugins {
1494 local plugins="${DEVSTACK_PLUGINS}"
1495 local plugin
1496
1497 # short circuit if nothing to do
1498 if [[ -z $plugins ]]; then
1499 return
1500 fi
1501
Dean Troyerdc97cb72015-03-28 08:20:50 -05001502 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001503 for plugin in ${plugins//,/ }; do
1504 git_clone_by_name $plugin
1505 done
1506}
1507
1508# load_plugin_settings
1509#
1510# Load settings from plugins in the order that they were registered
1511function load_plugin_settings {
1512 local plugins="${DEVSTACK_PLUGINS}"
1513 local plugin
1514
1515 # short circuit if nothing to do
1516 if [[ -z $plugins ]]; then
1517 return
1518 fi
1519
1520 echo "Loading plugin settings"
1521 for plugin in ${plugins//,/ }; do
1522 local dir=${GITDIR[$plugin]}
1523 # source any known settings
1524 if [[ -f $dir/devstack/settings ]]; then
1525 source $dir/devstack/settings
1526 fi
1527 done
1528}
1529
Sean Dague6e275e12015-03-26 05:54:28 -04001530# plugin_override_defaults
1531#
1532# Run an extremely early setting phase for plugins that allows default
1533# overriding of services.
1534function plugin_override_defaults {
1535 local plugins="${DEVSTACK_PLUGINS}"
1536 local plugin
1537
1538 # short circuit if nothing to do
1539 if [[ -z $plugins ]]; then
1540 return
1541 fi
1542
1543 echo "Overriding Configuration Defaults"
1544 for plugin in ${plugins//,/ }; do
1545 local dir=${GITDIR[$plugin]}
1546 # source any overrides
1547 if [[ -f $dir/devstack/override-defaults ]]; then
1548 # be really verbose that an override is happening, as it
1549 # may not be obvious if things fail later.
1550 echo "$plugin has overriden the following defaults"
1551 cat $dir/devstack/override-defaults
1552 source $dir/devstack/override-defaults
1553 fi
1554 done
1555}
1556
Sean Dague2c65e712014-12-18 09:44:56 -05001557# run_plugins
1558#
1559# Run the devstack/plugin.sh in all the plugin directories. These are
1560# run in registration order.
1561function run_plugins {
1562 local mode=$1
1563 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301564
1565 local plugins="${DEVSTACK_PLUGINS}"
1566 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001567 for plugin in ${plugins//,/ }; do
1568 local dir=${GITDIR[$plugin]}
1569 if [[ -f $dir/devstack/plugin.sh ]]; then
1570 source $dir/devstack/plugin.sh $mode $phase
1571 fi
1572 done
1573}
1574
1575function run_phase {
1576 local mode=$1
1577 local phase=$2
1578 if [[ -d $TOP_DIR/extras.d ]]; then
1579 for i in $TOP_DIR/extras.d/*.sh; do
1580 [[ -r $i ]] && source $i $mode $phase
1581 done
1582 fi
1583 # the source phase corresponds to settings loading in plugins
1584 if [[ "$mode" == "source" ]]; then
1585 load_plugin_settings
Sean Dague6e275e12015-03-26 05:54:28 -04001586 elif [[ "$mode" == "override_defaults" ]]; then
1587 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001588 else
1589 run_plugins $mode $phase
1590 fi
1591}
1592
Dean Troyerdff49a22014-01-30 15:37:40 -06001593
1594# Service Functions
1595# =================
1596
1597# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1598# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001599function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001600 echo "$1" | sed -e '
1601 s/,,/,/g;
1602 s/^,//;
1603 s/,$//
1604 '
1605}
1606
1607# disable_all_services() removes all current services
1608# from ``ENABLED_SERVICES`` to reset the configuration
1609# before a minimal installation
1610# Uses global ``ENABLED_SERVICES``
1611# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001612function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001613 ENABLED_SERVICES=""
1614}
1615
1616# Remove all services starting with '-'. For example, to install all default
1617# services except rabbit (rabbit) set in ``localrc``:
1618# ENABLED_SERVICES+=",-rabbit"
1619# Uses global ``ENABLED_SERVICES``
1620# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001621function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001622 local to_remove=""
1623 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001624 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001625
1626 # build up list of services that should be removed; i.e. they
1627 # begin with "-"
1628 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001629 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001630 to_remove+=",${service#-}"
1631 else
1632 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001633 fi
1634 done
Ian Wienand2796a822015-04-15 08:59:04 +10001635
1636 # go through the service list. if this service appears in the "to
1637 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001638 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Dean Troyerdff49a22014-01-30 15:37:40 -06001639}
1640
1641# disable_service() removes the services passed as argument to the
1642# ``ENABLED_SERVICES`` list, if they are present.
1643#
1644# For example:
1645# disable_service rabbit
1646#
1647# This function does not know about the special cases
1648# for nova, glance, and neutron built into is_service_enabled().
1649# Uses global ``ENABLED_SERVICES``
1650# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001651function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001652 local tmpsvcs=",${ENABLED_SERVICES},"
1653 local service
1654 for service in $@; do
1655 if is_service_enabled $service; then
1656 tmpsvcs=${tmpsvcs//,$service,/,}
1657 fi
1658 done
1659 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1660}
1661
1662# enable_service() adds the services passed as argument to the
1663# ``ENABLED_SERVICES`` list, if they are not already present.
1664#
1665# For example:
1666# enable_service qpid
1667#
1668# This function does not know about the special cases
1669# for nova, glance, and neutron built into is_service_enabled().
1670# Uses global ``ENABLED_SERVICES``
1671# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001672function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001673 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001674 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001675 for service in $@; do
1676 if ! is_service_enabled $service; then
1677 tmpsvcs+=",$service"
1678 fi
1679 done
1680 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1681 disable_negated_services
1682}
1683
1684# is_service_enabled() checks if the service(s) specified as arguments are
1685# enabled by the user in ``ENABLED_SERVICES``.
1686#
1687# Multiple services specified as arguments are ``OR``'ed together; the test
1688# is a short-circuit boolean, i.e it returns on the first match.
1689#
1690# There are special cases for some 'catch-all' services::
1691# **nova** returns true if any service enabled start with **n-**
1692# **cinder** returns true if any service enabled start with **c-**
1693# **ceilometer** returns true if any service enabled start with **ceilometer**
1694# **glance** returns true if any service enabled start with **g-**
1695# **neutron** returns true if any service enabled start with **q-**
1696# **swift** returns true if any service enabled start with **s-**
1697# **trove** returns true if any service enabled start with **tr-**
1698# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1699# **s-** services will be enabled. This will be deprecated in the future.
1700#
1701# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1702# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1703# as enabled in this case.
1704#
1705# Uses global ``ENABLED_SERVICES``
1706# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001707function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001708 local xtrace=$(set +o | grep xtrace)
1709 set +o xtrace
1710 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001711 local services=$@
1712 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001713 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001714 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001715
1716 # Look for top-level 'enabled' function for this service
1717 if type is_${service}_enabled >/dev/null 2>&1; then
1718 # A function exists for this service, use it
1719 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001720 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001721 fi
1722
1723 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1724 # are implemented
1725
Sean Dague45917cc2014-02-24 16:09:14 -05001726 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001727 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001728 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1729 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1730 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1731 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1732 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1733 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1734 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1735 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1736 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001737 done
Sean Dague45917cc2014-02-24 16:09:14 -05001738 $xtrace
1739 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001740}
1741
fumihiko kakuma8606c982015-04-13 09:55:06 +09001742# remove specified list from the input string
1743# remove_disabled_services service-list remove-list
1744function remove_disabled_services {
1745 local service_list=$1
1746 local remove_list=$2
1747 local service
1748 local enabled=""
1749
1750 for service in ${service_list//,/ }; do
1751 local remove
1752 local add=1
1753 for remove in ${remove_list//,/ }; do
1754 if [[ ${remove} == ${service} ]]; then
1755 add=0
1756 break
1757 fi
1758 done
1759 if [[ $add == 1 ]]; then
1760 enabled="${enabled},$service"
1761 fi
1762 done
1763 _cleanup_service_list "$enabled"
1764}
1765
Dean Troyerdff49a22014-01-30 15:37:40 -06001766# Toggle enable/disable_service for services that must run exclusive of each other
1767# $1 The name of a variable containing a space-separated list of services
1768# $2 The name of a variable in which to store the enabled service's name
1769# $3 The name of the service to enable
1770function use_exclusive_service {
1771 local options=${!1}
1772 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001773 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001774 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001775 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001776 for opt in $options;do
1777 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1778 done
1779 eval "$out=$selection"
1780 return 0
1781}
1782
1783
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001784# System Functions
1785# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001786
1787# Only run the command if the target file (the last arg) is not on an
1788# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001789function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001790 local xtrace=$(set +o | grep xtrace)
1791 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001792 local args=( $@ )
1793 local last
1794 local sudo_cmd
1795 local dir_to_check
1796
1797 let last="${#args[*]} - 1"
1798
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001799 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001800 if [ ! -d "$dir_to_check" ]; then
1801 dir_to_check=`dirname "$dir_to_check"`
1802 fi
1803
1804 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001805 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001806 return 0
1807 fi
1808
1809 if [[ $TRACK_DEPENDS = True ]]; then
1810 sudo_cmd="env"
1811 else
1812 sudo_cmd="sudo"
1813 fi
1814
Sean Dague45917cc2014-02-24 16:09:14 -05001815 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001816 $sudo_cmd $@
1817}
1818
1819# Exit 0 if address is in network or 1 if address is not in network
1820# ip-range is in CIDR notation: 1.2.3.4/20
1821# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001822function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001823 local ip=$1
1824 local range=$2
1825 local masklen=${range#*/}
1826 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1827 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1828 [[ $network == $subnet ]]
1829}
1830
1831# Add a user to a group.
1832# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001833function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001834 local user=$1
1835 local group=$2
1836
Thomas Bechtolda8580852015-05-31 00:04:33 +02001837 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06001838}
1839
1840# Convert CIDR notation to a IPv4 netmask
1841# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001842function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001843 local maskpat="255 255 255 255"
1844 local maskdgt="254 252 248 240 224 192 128"
1845 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1846 echo ${1-0}.${2-0}.${3-0}.${4-0}
1847}
1848
1849# Gracefully cp only if source file/dir exists
1850# cp_it source destination
1851function cp_it {
1852 if [ -e $1 ] || [ -d $1 ]; then
1853 cp -pRL $1 $2
1854 fi
1855}
1856
1857# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1858# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1859# ``localrc`` or on the command line if necessary::
1860#
1861# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1862#
1863# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1864
Ian Wienandaee18c72014-02-21 15:35:08 +11001865function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001866 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001867 export http_proxy=$http_proxy
1868 fi
Sean Dague53753292014-12-04 19:38:15 -05001869 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001870 export https_proxy=$https_proxy
1871 fi
Sean Dague53753292014-12-04 19:38:15 -05001872 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001873 export no_proxy=$no_proxy
1874 fi
1875}
1876
1877# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001878function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001879 local mount_type=`stat -f -L -c %T $1`
1880 test "$mount_type" == "nfs"
1881}
1882
1883# Return the network portion of the given IP address using netmask
1884# netmask is in the traditional dotted-quad format
1885# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001886function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001887 local ip=$1
1888 local mask=$2
1889 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1890 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1891 echo $subnet
1892}
1893
Chris Dent3a2c86a2015-05-12 13:41:25 +00001894# Return the current python as "python<major>.<minor>"
1895function python_version {
1896 local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
1897 echo "python${python_version}"
1898}
1899
Dean Troyerdff49a22014-01-30 15:37:40 -06001900# Service wrapper to restart services
1901# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001902function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001903 if is_ubuntu; then
1904 sudo /usr/sbin/service $1 restart
1905 else
1906 sudo /sbin/service $1 restart
1907 fi
1908}
1909
1910# Only change permissions of a file or directory if it is not on an
1911# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001912function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001913 _safe_permission_operation chmod $@
1914}
1915
1916# Only change ownership of a file or directory if it is not on an NFS
1917# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001918function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001919 _safe_permission_operation chown $@
1920}
1921
1922# Service wrapper to start services
1923# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001924function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001925 if is_ubuntu; then
1926 sudo /usr/sbin/service $1 start
1927 else
1928 sudo /sbin/service $1 start
1929 fi
1930}
1931
1932# Service wrapper to stop services
1933# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001934function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001935 if is_ubuntu; then
1936 sudo /usr/sbin/service $1 stop
1937 else
1938 sudo /sbin/service $1 stop
1939 fi
1940}
1941
1942
1943# Restore xtrace
1944$XTRACE
1945
1946# Local variables:
1947# mode: shell-script
1948# End: