blob: 56fa64a990f74ce9b4d129ba975c436274f389ea [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 local default=$1
Sean Dague53753292014-12-04 19:38:15 -050055 local literal=$2
Dean Troyera1b82cc2015-01-30 13:54:40 -060056 local testval=${!literal:-}
Dean Troyerdff49a22014-01-30 15:37:40 -060057
58 [[ -z "$testval" ]] && { echo "$default"; return; }
59 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
60 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
61 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -050062 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060063}
64
Attila Fazekas1bd79592015-02-24 14:06:56 +010065function isset {
66 [[ -v "$1" ]]
67}
Dean Troyerdff49a22014-01-30 15:37:40 -060068
69# Control Functions
70# =================
71
72# Prints backtrace info
73# filename:lineno:function
74# backtrace level
75function backtrace {
76 local level=$1
77 local deep=$((${#BASH_SOURCE[@]} - 1))
78 echo "[Call Trace]"
79 while [ $level -le $deep ]; do
80 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
81 deep=$((deep - 1))
82 done
83}
84
85# Prints line number and "message" then exits
86# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +110087function die {
Dean Troyerdff49a22014-01-30 15:37:40 -060088 local exitcode=$?
89 set +o xtrace
90 local line=$1; shift
91 if [ $exitcode == 0 ]; then
92 exitcode=1
93 fi
94 backtrace 2
95 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -060096 # Give buffers a second to flush
97 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -060098 exit $exitcode
99}
100
101# Checks an environment variable is not set or has length 0 OR if the
102# exit code is non-zero and prints "message" and exits
103# NOTE: env-var is the variable name without a '$'
104# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100105function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600106 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500107 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600108 set +o xtrace
109 local line=$1; shift
110 local evar=$1; shift
111 if ! is_set $evar || [ $exitcode != 0 ]; then
112 die $line "$*"
113 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500114 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600115}
116
117# Prints line number and "message" in error format
118# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100119function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600120 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500121 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600122 set +o xtrace
123 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
124 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600125 if [[ -n ${LOGDIR} ]]; then
126 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600127 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500128 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600129 return $exitcode
130}
131
132# Checks an environment variable is not set or has length 0 OR if the
133# exit code is non-zero and prints "message"
134# NOTE: env-var is the variable name without a '$'
135# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100136function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600137 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500138 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600139 set +o xtrace
140 local line=$1; shift
141 local evar=$1; shift
142 if ! is_set $evar || [ $exitcode != 0 ]; then
143 err $line "$*"
144 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500145 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600146 return $exitcode
147}
148
149# Exit after outputting a message about the distribution not being supported.
150# exit_distro_not_supported [optional-string-telling-what-is-missing]
151function exit_distro_not_supported {
152 if [[ -z "$DISTRO" ]]; then
153 GetDistro
154 fi
155
156 if [ $# -gt 0 ]; then
157 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
158 else
159 die $LINENO "Support for $DISTRO is incomplete."
160 fi
161}
162
163# Test if the named environment variable is set and not zero length
164# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100165function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600166 local var=\$"$1"
167 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
168}
169
170# Prints line number and "message" in warning format
171# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100172function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600173 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500174 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600175 set +o xtrace
176 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
177 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600178 if [[ -n ${LOGDIR} ]]; then
179 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600180 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500181 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600182 return $exitcode
183}
184
185
186# Distro Functions
187# ================
188
189# Determine OS Vendor, Release and Update
190# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
191# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500192# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
193# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
194# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
195# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
196# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500197os_VENDOR=""
198os_RELEASE=""
199os_UPDATE=""
200os_PACKAGE=""
201os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500202
Dean Troyerdff49a22014-01-30 15:37:40 -0600203# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100204function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500205
Dean Troyerdff49a22014-01-30 15:37:40 -0600206 # Figure out which vendor we are
207 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
208 # OS/X
209 os_VENDOR=`sw_vers -productName`
210 os_RELEASE=`sw_vers -productVersion`
211 os_UPDATE=${os_RELEASE##*.}
212 os_RELEASE=${os_RELEASE%.*}
213 os_PACKAGE=""
214 if [[ "$os_RELEASE" =~ "10.7" ]]; then
215 os_CODENAME="lion"
216 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
217 os_CODENAME="snow leopard"
218 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
219 os_CODENAME="leopard"
220 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
221 os_CODENAME="tiger"
222 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
223 os_CODENAME="panther"
224 else
225 os_CODENAME=""
226 fi
227 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
228 os_VENDOR=$(lsb_release -i -s)
229 os_RELEASE=$(lsb_release -r -s)
230 os_UPDATE=""
231 os_PACKAGE="rpm"
232 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
233 os_PACKAGE="deb"
234 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
235 lsb_release -d -s | grep -q openSUSE
236 if [[ $? -eq 0 ]]; then
237 os_VENDOR="openSUSE"
238 fi
239 elif [[ $os_VENDOR == "openSUSE project" ]]; then
240 os_VENDOR="openSUSE"
241 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
242 os_VENDOR="Red Hat"
243 fi
244 os_CODENAME=$(lsb_release -c -s)
245 elif [[ -r /etc/redhat-release ]]; then
246 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
247 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
248 # CentOS release 5.5 (Final)
249 # CentOS Linux release 6.0 (Final)
250 # Fedora release 16 (Verne)
251 # XenServer release 6.2.0-70446c (xenenterprise)
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700252 # Oracle Linux release 7
Dean Troyerdff49a22014-01-30 15:37:40 -0600253 os_CODENAME=""
254 for r in "Red Hat" CentOS Fedora XenServer; do
255 os_VENDOR=$r
256 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
257 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
258 os_CODENAME=${ver#*|}
259 os_RELEASE=${ver%|*}
260 os_UPDATE=${os_RELEASE##*.}
261 os_RELEASE=${os_RELEASE%.*}
262 break
263 fi
264 os_VENDOR=""
265 done
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700266 if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
267 os_VENDOR=OracleLinux
268 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600269 os_PACKAGE="rpm"
270 elif [[ -r /etc/SuSE-release ]]; then
271 for r in openSUSE "SUSE Linux"; do
272 if [[ "$r" = "SUSE Linux" ]]; then
273 os_VENDOR="SUSE LINUX"
274 else
275 os_VENDOR=$r
276 fi
277
278 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
279 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
280 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
281 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
282 break
283 fi
284 os_VENDOR=""
285 done
286 os_PACKAGE="rpm"
287 # If lsb_release is not installed, we should be able to detect Debian OS
288 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
289 os_VENDOR="Debian"
290 os_PACKAGE="deb"
291 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
292 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
293 fi
294 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
295}
296
297# Translate the OS version values into common nomenclature
298# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500299declare DISTRO
300
Ian Wienandaee18c72014-02-21 15:35:08 +1100301function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600302 GetOSVersion
303 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
304 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
305 DISTRO=$os_CODENAME
306 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
307 # For Fedora, just use 'f' and the release
308 DISTRO="f$os_RELEASE"
309 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
310 DISTRO="opensuse-$os_RELEASE"
311 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
312 # For SLE, also use the service pack
313 if [[ -z "$os_UPDATE" ]]; then
314 DISTRO="sle${os_RELEASE}"
315 else
316 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
317 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530318 elif [[ "$os_VENDOR" =~ (Red Hat) || \
319 "$os_VENDOR" =~ (CentOS) || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700320 "$os_VENDOR" =~ (OracleLinux) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600321 # Drop the . release as we assume it's compatible
322 DISTRO="rhel${os_RELEASE::1}"
323 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
324 DISTRO="xs$os_RELEASE"
325 else
326 # Catch-all for now is Vendor + Release + Update
327 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
328 fi
329 export DISTRO
330}
331
332# Utility function for checking machine architecture
333# is_arch arch-type
334function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500335 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600336}
337
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700338# Determine if current distribution is an Oracle distribution
339# is_oraclelinux
340function is_oraclelinux {
341 if [[ -z "$os_VENDOR" ]]; then
342 GetOSVersion
343 fi
344
345 [ "$os_VENDOR" = "OracleLinux" ]
346}
347
348
Dean Troyerdff49a22014-01-30 15:37:40 -0600349# Determine if current distribution is a Fedora-based distribution
350# (Fedora, RHEL, CentOS, etc).
351# is_fedora
352function is_fedora {
353 if [[ -z "$os_VENDOR" ]]; then
354 GetOSVersion
355 fi
356
anju Tiwari6c639c92014-07-15 18:11:54 +0530357 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700358 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600359}
360
361
362# Determine if current distribution is a SUSE-based distribution
363# (openSUSE, SLE).
364# is_suse
365function is_suse {
366 if [[ -z "$os_VENDOR" ]]; then
367 GetOSVersion
368 fi
369
370 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
371}
372
373
374# Determine if current distribution is an Ubuntu-based distribution
375# It will also detect non-Ubuntu but Debian-based distros
376# is_ubuntu
377function is_ubuntu {
378 if [[ -z "$os_PACKAGE" ]]; then
379 GetOSVersion
380 fi
381 [ "$os_PACKAGE" = "deb" ]
382}
383
384
385# Git Functions
386# =============
387
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600388# Returns openstack release name for a given branch name
389# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100390function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600391 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700392 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600393 echo ${branch#*/}
394 else
395 echo "master"
396 fi
397}
398
Dean Troyerdff49a22014-01-30 15:37:40 -0600399# git clone only if directory doesn't exist already. Since ``DEST`` might not
400# be owned by the installation user, we create the directory and change the
401# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500402# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
403# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600404# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500405# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600406# git_clone remote dest-dir branch
407function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500408 local git_remote=$1
409 local git_dest=$2
410 local git_ref=$3
411 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200412 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500413
Sean Dague53753292014-12-04 19:38:15 -0500414 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800415 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200416 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
417 fi
418
Dean Troyerdff49a22014-01-30 15:37:40 -0600419 if [[ "$OFFLINE" = "True" ]]; then
420 echo "Running in offline mode, clones already exist"
421 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500422 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600423 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400424 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600425 return
426 fi
427
Dean Troyer50cda692014-07-25 11:57:20 -0500428 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600429 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500430 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600431 [[ "$ERROR_ON_CLONE" = "True" ]] && \
432 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200433 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600434 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500435 cd $git_dest
436 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600437 else
438 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500439 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600440 [[ "$ERROR_ON_CLONE" = "True" ]] && \
441 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200442 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500443 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600444 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500445 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600446 elif [[ "$RECLONE" = "True" ]]; then
447 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500448 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600449 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500450 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100451 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600452 # remove the existing ignored files (like pyc) as they cause breakage
453 # (due to the py files having older timestamps than our pyc, so python
454 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500455 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600456
Dean Troyer50cda692014-07-25 11:57:20 -0500457 # handle git_ref accordingly to type (tag, branch)
458 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
459 git_update_tag $git_ref
460 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
461 git_update_branch $git_ref
462 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
463 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600464 else
Dean Troyer50cda692014-07-25 11:57:20 -0500465 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600466 fi
467
468 fi
469 fi
470
471 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500472 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600473 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400474 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600475}
476
Sean Daguecc524062014-10-01 09:06:43 -0400477# A variation on git clone that lets us specify a project by it's
478# actual name, like oslo.config. This is exceptionally useful in the
479# library installation case
480function git_clone_by_name {
481 local name=$1
482 local repo=${GITREPO[$name]}
483 local dir=${GITDIR[$name]}
484 local branch=${GITBRANCH[$name]}
485 git_clone $repo $dir $branch
486}
487
488
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100489# git can sometimes get itself infinitely stuck with transient network
490# errors or other issues with the remote end. This wraps git in a
491# timeout/retry loop and is intended to watch over non-local git
492# processes that might hang. GIT_TIMEOUT, if set, is passed directly
493# to timeout(1); otherwise the default value of 0 maintains the status
494# quo of waiting forever.
495# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100496function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100497 local count=0
498 local timeout=0
499
500 if [[ -n "${GIT_TIMEOUT}" ]]; then
501 timeout=${GIT_TIMEOUT}
502 fi
503
504 until timeout -s SIGINT ${timeout} git "$@"; do
505 # 124 is timeout(1)'s special return code when it reached the
506 # timeout; otherwise assume fatal failure
507 if [[ $? -ne 124 ]]; then
508 die $LINENO "git call failed: [git $@]"
509 fi
510
511 count=$(($count + 1))
512 warn "timeout ${count} for git call: [git $@]"
513 if [ $count -eq 3 ]; then
514 die $LINENO "Maximum of 3 git retries reached"
515 fi
516 sleep 5
517 done
518}
519
Dean Troyerdff49a22014-01-30 15:37:40 -0600520# git update using reference as a branch.
521# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100522function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500523 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600524
Dean Troyer50cda692014-07-25 11:57:20 -0500525 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600526 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500527 git branch -D $git_branch || true
528 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600529}
530
531# git update using reference as a branch.
532# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100533function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500534 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600535
Dean Troyer50cda692014-07-25 11:57:20 -0500536 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600537}
538
539# git update using reference as a tag. Be careful editing source at that repo
540# as working copy will be in a detached mode
541# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100542function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500543 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600544
Dean Troyer50cda692014-07-25 11:57:20 -0500545 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600546 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500547 git_timed fetch origin tag $git_tag
548 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600549}
550
551
552# OpenStack Functions
553# ===================
554
555# Get the default value for HOST_IP
556# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100557function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600558 local fixed_range=$1
559 local floating_range=$2
560 local host_ip_iface=$3
561 local host_ip=$4
562
Dean Troyerdff49a22014-01-30 15:37:40 -0600563 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
564 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
565 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100566 # Find the interface used for the default route
567 host_ip_iface=${host_ip_iface:-$(ip route | awk '/default/ {print $5}' | head -1)}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500568 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
569 local ip
570 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600571 # Attempt to filter out IP addresses that are part of the fixed and
572 # floating range. Note that this method only works if the ``netaddr``
573 # python library is installed. If it is not installed, an error
574 # will be printed and the first IP from the interface will be used.
575 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
576 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500577 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
578 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600579 break;
580 fi
581 done
582 fi
583 echo $host_ip
584}
585
Attila Fazekasf71b5002014-05-28 09:52:22 +0200586# Generates hex string from ``size`` byte of pseudo random data
587# generate_hex_string size
588function generate_hex_string {
589 local size=$1
590 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
591}
592
Dean Troyerdff49a22014-01-30 15:37:40 -0600593# Grab a numbered field from python prettytable output
594# Fields are numbered starting with 1
595# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
596# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100597function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500598 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600599 while read data; do
600 if [ "$1" -lt 0 ]; then
601 field="(\$(NF$1))"
602 else
603 field="\$$(($1 + 1))"
604 fi
605 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
606 done
607}
608
yuntongjinf26deea2015-02-28 10:50:34 +0800609# install default policy
610# copy over a default policy.json and policy.d for projects
611function install_default_policy {
612 local project=$1
613 local project_uc=$(echo $1|tr a-z A-Z)
614 local conf_dir="${project_uc}_CONF_DIR"
615 # eval conf dir to get the variable
616 conf_dir="${!conf_dir}"
617 local project_dir="${project_uc}_DIR"
618 # eval project dir to get the variable
619 project_dir="${!project_dir}"
620 local sample_conf_dir="${project_dir}/etc/${project}"
621 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
622
623 # first copy any policy.json
624 cp -p $sample_conf_dir/policy.json $conf_dir
625 # then optionally copy over policy.d
626 if [[ -d $sample_policy_dir ]]; then
627 cp -r $sample_policy_dir $conf_dir/policy.d
628 fi
629}
630
Dean Troyerdff49a22014-01-30 15:37:40 -0600631# Add a policy to a policy.json file
632# Do nothing if the policy already exists
633# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100634function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600635 local policy_file=$1
636 local policy_name=$2
637 local policy_perm=$3
638
639 if grep -q ${policy_name} ${policy_file}; then
640 echo "Policy ${policy_name} already exists in ${policy_file}"
641 return
642 fi
643
644 # Add a terminating comma to policy lines without one
645 # Remove the closing '}' and all lines following to the end-of-file
646 local tmpfile=$(mktemp)
647 uniq ${policy_file} | sed -e '
648 s/]$/],/
649 /^[}]/,$d
650 ' > ${tmpfile}
651
652 # Append policy and closing brace
653 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
654 echo "}" >>${tmpfile}
655
656 mv ${tmpfile} ${policy_file}
657}
658
Alistair Coles24779f62014-10-15 18:57:59 +0100659# Gets or creates a domain
660# Usage: get_or_create_domain <name> <description>
661function get_or_create_domain {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500662 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100663 # Gets domain id
664 local domain_id=$(
665 # Gets domain id
666 openstack --os-token=$OS_TOKEN --os-url=$os_url \
667 --os-identity-api-version=3 domain show $1 \
668 -f value -c id 2>/dev/null ||
669 # Creates new domain
670 openstack --os-token=$OS_TOKEN --os-url=$os_url \
671 --os-identity-api-version=3 domain create $1 \
672 --description "$2" \
673 -f value -c id
674 )
675 echo $domain_id
676}
677
Steve Martinellib74e01c2014-12-18 01:35:35 -0500678# Gets or creates group
679# Usage: get_or_create_group <groupname> [<domain> <description>]
680function get_or_create_group {
681 local domain=${2:+--domain ${2}}
682 local desc="${3:-}"
683 local os_url="$KEYSTONE_SERVICE_URI_V3"
684 # Gets group id
685 local group_id=$(
686 # Creates new group with --or-show
687 openstack --os-token=$OS_TOKEN --os-url=$os_url \
688 --os-identity-api-version=3 group create $1 \
689 $domain --description "$desc" --or-show \
690 -f value -c id
691 )
692 echo $group_id
693}
694
Bartosz Górski0abde392014-02-28 14:15:19 +0100695# Gets or creates user
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000696# Usage: get_or_create_user <username> <password> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100697function get_or_create_user {
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000698 if [[ ! -z "$3" ]]; then
699 local email="--email=$3"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200700 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500701 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200702 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100703 local os_cmd="openstack"
704 local domain=""
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000705 if [[ ! -z "$4" ]]; then
706 domain="--domain=$4"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500707 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100708 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100709 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500710 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500711 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100712 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100713 $1 \
714 --password "$2" \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500715 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100716 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500717 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100718 -f value -c id
719 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500720 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100721}
722
723# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100724# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100725function get_or_create_project {
726 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100727 local os_cmd="openstack"
728 local domain=""
729 if [[ ! -z "$2" ]]; then
730 domain="--domain=$2"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500731 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100732 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500733 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500734 # Creates new project with --or-show
735 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100736 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500737 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100738}
739
740# Gets or creates role
741# Usage: get_or_create_role <name>
742function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500743 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500744 # Creates role with --or-show
745 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100746 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500747 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100748}
749
Jamie Lennox9b215db2015-02-10 18:19:57 +1100750# Gets or adds user role to project
751# Usage: get_or_add_user_project_role <role> <user> <project>
752function get_or_add_user_project_role {
Bartosz Górski0abde392014-02-28 14:15:19 +0100753 # Gets user role id
Steve Martinelli5541a612015-01-19 15:58:49 -0500754 local user_role_id=$(openstack role list \
755 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100756 --project $3 \
757 --column "ID" \
758 --column "Name" \
759 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500760 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100761 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500762 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100763 $1 \
764 --user $2 \
765 --project $3 \
766 | grep " id " | get_field 2)
767 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500768 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100769}
770
771# Gets or creates service
772# Usage: get_or_create_service <name> <type> <description>
773function get_or_create_service {
774 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500775 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100776 # Gets service id
777 openstack service show $1 -f value -c id 2>/dev/null ||
778 # Creates new service if not exists
779 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500780 $2 \
781 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100782 --description="$3" \
783 -f value -c id
784 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500785 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100786}
787
788# Gets or creates endpoint
789# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
790function get_or_create_endpoint {
791 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500792 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100793 --column "ID" \
794 --column "Region" \
795 --column "Service Name" \
796 | grep " $2 " \
797 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500798 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100799 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500800 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100801 $1 \
802 --region $2 \
803 --publicurl $3 \
804 --adminurl $4 \
805 --internalurl $5 \
806 | grep " id " | get_field 2)
807 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500808 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100809}
Dean Troyerdff49a22014-01-30 15:37:40 -0600810
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500811
Dean Troyerdff49a22014-01-30 15:37:40 -0600812# Package Functions
813# =================
814
815# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100816function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800817 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600818 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800819
820 if [[ -z "$base_dir" ]]; then
821 base_dir=$FILES
822 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600823 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800824 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600825 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800826 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600827 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800828 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600829 else
830 exit_distro_not_supported "list of packages"
831 fi
832 echo "$pkg_dir"
833}
834
835# Wrapper for ``apt-get`` to set cache and proxy environment variables
836# Uses globals ``OFFLINE``, ``*_proxy``
837# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100838function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500839 local xtrace=$(set +o | grep xtrace)
840 set +o xtrace
841
Dean Troyerdff49a22014-01-30 15:37:40 -0600842 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
843 local sudo="sudo"
844 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500845
846 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500847
Dean Troyerdff49a22014-01-30 15:37:40 -0600848 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500849 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
850 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600851 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
852}
853
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800854function _parse_package_files {
855 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600856
Dean Troyerdff49a22014-01-30 15:37:40 -0600857 if [[ -z "$DISTRO" ]]; then
858 GetDistro
859 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600860
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800861 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600862 local OIFS line package distros distro
863 [[ -e $fname ]] || continue
864
865 OIFS=$IFS
866 IFS=$'\n'
867 for line in $(<${fname}); do
868 if [[ $line =~ "NOPRIME" ]]; then
869 continue
870 fi
871
872 # Assume we want this package
873 package=${line%#*}
874 inst_pkg=1
875
876 # Look for # dist:xxx in comment
877 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
878 # We are using BASH regexp matching feature.
879 package=${BASH_REMATCH[1]}
880 distros=${BASH_REMATCH[2]}
881 # In bash ${VAR,,} will lowecase VAR
882 # Look for a match in the distro list
883 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
884 # If no match then skip this package
885 inst_pkg=0
886 fi
887 fi
888
Dean Troyerdff49a22014-01-30 15:37:40 -0600889 if [[ $inst_pkg = 1 ]]; then
890 echo $package
891 fi
892 done
893 IFS=$OIFS
894 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800895}
896
897# get_packages() collects a list of package names of any type from the
898# prerequisite files in ``files/{debs|rpms}``. The list is intended
899# to be passed to a package installer such as apt or yum.
900#
901# Only packages required for the services in 1st argument will be
902# included. Two bits of metadata are recognized in the prerequisite files:
903#
904# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
905# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
906# of the package to the distros listed. The distro names are case insensitive.
907function get_packages {
908 local xtrace=$(set +o | grep xtrace)
909 set +o xtrace
910 local services=$@
911 local package_dir=$(_get_package_dir)
912 local file_to_parse=""
913 local service=""
914
915 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
916
917 if [[ -z "$package_dir" ]]; then
918 echo "No package directory supplied"
919 return 1
920 fi
921 for service in ${services//,/ }; do
922 # Allow individual services to specify dependencies
923 if [[ -e ${package_dir}/${service} ]]; then
924 file_to_parse="${file_to_parse} ${package_dir}/${service}"
925 fi
926 # NOTE(sdague) n-api needs glance for now because that's where
927 # glance client is
928 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700929 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800930 file_to_parse="${file_to_parse} ${package_dir}/nova"
931 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700932 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800933 file_to_parse="${file_to_parse} ${package_dir}/glance"
934 fi
935 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700936 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800937 file_to_parse="${file_to_parse} ${package_dir}/cinder"
938 fi
939 elif [[ $service == ceilometer-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700940 if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800941 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
942 fi
943 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700944 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800945 file_to_parse="${file_to_parse} ${package_dir}/swift"
946 fi
947 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700948 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800949 file_to_parse="${file_to_parse} ${package_dir}/nova"
950 fi
951 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700952 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800953 file_to_parse="${file_to_parse} ${package_dir}/glance"
954 fi
955 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700956 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800957 file_to_parse="${file_to_parse} ${package_dir}/keystone"
958 fi
959 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700960 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800961 file_to_parse="${file_to_parse} ${package_dir}/neutron"
962 fi
963 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700964 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800965 file_to_parse="${file_to_parse} ${package_dir}/ironic"
966 fi
967 fi
968 done
969 echo "$(_parse_package_files $file_to_parse)"
970 $xtrace
971}
972
973# get_plugin_packages() collects a list of package names of any type from a
974# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
975# list is intended to be passed to a package installer such as apt or yum.
976#
977# Only packages required for enabled and collected plugins will included.
978#
979# The same metadata used in the main devstack prerequisite files may be used
980# in these prerequisite files, see get_packages() for more info.
981function get_plugin_packages {
982 local xtrace=$(set +o | grep xtrace)
983 set +o xtrace
984 local files_to_parse=""
985 local package_dir=""
986 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
987 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
988 files_to_parse+="$package_dir/$plugin"
989 done
990 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -0500991 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600992}
993
994# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500995# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600996# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400997function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -0500998 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
999 REPOS_UPDATED=${REPOS_UPDATED:-False}
1000 RETRY_UPDATE=${RETRY_UPDATE:-False}
1001
Paul Linchpiner9e179742014-07-13 22:23:00 -07001002 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001003 return 0
1004 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001005
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001006 if is_ubuntu; then
1007 local xtrace=$(set +o | grep xtrace)
1008 set +o xtrace
1009 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1010 # if there are transient errors pulling the updates, that's fine.
1011 # It may be secondary repositories that we don't really care about.
1012 apt_get update || /bin/true
1013 REPOS_UPDATED=True
1014 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001015 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001016 fi
1017}
1018
1019function real_install_package {
1020 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001021 apt_get install "$@"
1022 elif is_fedora; then
1023 yum_install "$@"
1024 elif is_suse; then
1025 zypper_install "$@"
1026 else
1027 exit_distro_not_supported "installing packages"
1028 fi
1029}
1030
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001031# Distro-agnostic package installer
1032# install_package package [package ...]
1033function install_package {
1034 update_package_repo
1035 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1036}
1037
Dean Troyerdff49a22014-01-30 15:37:40 -06001038# Distro-agnostic function to tell if a package is installed
1039# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001040function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001041 if [[ -z "$@" ]]; then
1042 return 1
1043 fi
1044
1045 if [[ -z "$os_PACKAGE" ]]; then
1046 GetOSVersion
1047 fi
1048
1049 if [[ "$os_PACKAGE" = "deb" ]]; then
1050 dpkg -s "$@" > /dev/null 2> /dev/null
1051 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1052 rpm --quiet -q "$@"
1053 else
1054 exit_distro_not_supported "finding if a package is installed"
1055 fi
1056}
1057
1058# Distro-agnostic package uninstaller
1059# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001060function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001061 if is_ubuntu; then
1062 apt_get purge "$@"
1063 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001064 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001065 elif is_suse; then
1066 sudo zypper rm "$@"
1067 else
1068 exit_distro_not_supported "uninstalling packages"
1069 fi
1070}
1071
1072# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001073# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001074# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001075function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001076 [[ "$OFFLINE" = "True" ]] && return
1077 local sudo="sudo"
1078 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001079
1080 # The manual check for missing packages is because yum -y assumes
1081 # missing packages are OK. See
1082 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001083 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1084 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001085 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001086 awk '
1087 BEGIN { fail=0 }
1088 /No package/ { fail=1 }
1089 { print }
1090 END { exit fail }' || \
1091 die $LINENO "Missing packages detected"
1092
1093 # also ensure we catch a yum failure
1094 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001095 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001096 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001097}
1098
1099# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001100# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001101# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001102function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001103 [[ "$OFFLINE" = "True" ]] && return
1104 local sudo="sudo"
1105 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001106 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1107 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001108 zypper --non-interactive install --auto-agree-with-licenses "$@"
1109}
1110
1111
1112# Process Functions
1113# =================
1114
1115# _run_process() is designed to be backgrounded by run_process() to simulate a
1116# fork. It includes the dirty work of closing extra filehandles and preparing log
1117# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001118# from the service name.
1119# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001120# If an optional group is provided sg will be used to set the group of
1121# the command.
1122# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001123function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001124 local service=$1
1125 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001126 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001127
1128 # Undo logging redirections and close the extra descriptors
1129 exec 1>&3
1130 exec 2>&3
1131 exec 3>&-
1132 exec 6>&-
1133
Dean Troyerdde41d02014-12-09 17:47:57 -06001134 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1135 if [[ -n ${LOGDIR} ]]; then
1136 exec 1>&"$real_logfile" 2>&1
1137 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1138 if [[ -n ${SCREEN_LOGDIR} ]]; then
1139 # Drop the backward-compat symlink
1140 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1141 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001142
1143 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1144 export PYTHONUNBUFFERED=1
1145 fi
1146
Dean Troyer3159a822014-08-27 14:13:58 -05001147 # Run under ``setsid`` to force the process to become a session and group leader.
1148 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001149 if [[ -n "$group" ]]; then
1150 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1151 else
1152 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1153 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001154
1155 # Just silently exit this process
1156 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001157}
1158
1159# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1160# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001161# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001162# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001163function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001164 SCREEN_NAME=${SCREEN_NAME:-stack}
1165 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1166
1167 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1168 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1169 fi
1170
1171 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1172}
1173
1174# Find out if a process exists by partial name.
1175# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001176function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001177 local name=$1
1178 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001179 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001180 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001181 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001182}
1183
Dean Troyer3159a822014-08-27 14:13:58 -05001184# Run a single service under screen or directly
1185# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001186# If an optional group is provided sg will be used to run the
1187# command as that group.
1188# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001189function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001190 local service=$1
1191 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001192 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001193
Dean Troyer3159a822014-08-27 14:13:58 -05001194 if is_service_enabled $service; then
1195 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001196 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001197 else
1198 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001199 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001200 fi
1201 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001202}
1203
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001204# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001205# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001206# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001207# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001208# Run a command in a shell in a screen window, if an optional group
1209# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001210function screen_process {
1211 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001212 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001213 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001214
Sean Dagueea22a4f2014-06-27 15:21:41 -04001215 SCREEN_NAME=${SCREEN_NAME:-stack}
1216 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001217 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001218
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001219 # Append the process to the screen rc file
1220 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001221
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001222 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001223
Dean Troyerdde41d02014-12-09 17:47:57 -06001224 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1225 echo "LOGDIR: $LOGDIR"
1226 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1227 echo "log: $real_logfile"
1228 if [[ -n ${LOGDIR} ]]; then
1229 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001230 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001231 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1232 if [[ -n ${SCREEN_LOGDIR} ]]; then
1233 # Drop the backward-compat symlink
1234 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1235 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001236 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001237
1238 # sleep to allow bash to be ready to be send the command - we are
1239 # creating a new window in screen and then sends characters, so if
1240 # bash isn't running by the time we send the command, nothing happens
1241 sleep 3
1242
1243 NL=`echo -ne '\015'`
1244 # This fun command does the following:
1245 # - the passed server command is backgrounded
1246 # - the pid of the background process is saved in the usual place
1247 # - the server process is brought back to the foreground
1248 # - if the server process exits prematurely the fg command errors
1249 # and a message is written to stdout and the process failure file
1250 #
1251 # The pid saved can be used in stop_process() as a process group
1252 # id to kill off all child processes
1253 if [[ -n "$group" ]]; then
1254 command="sg $group '$command'"
1255 fi
1256 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 -06001257}
1258
1259# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001260# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001261# screen_rc service "command-line"
1262function screen_rc {
1263 SCREEN_NAME=${SCREEN_NAME:-stack}
1264 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1265 if [[ ! -e $SCREENRC ]]; then
1266 # Name the screen session
1267 echo "sessionname $SCREEN_NAME" > $SCREENRC
1268 # Set a reasonable statusbar
1269 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1270 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1271 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1272 echo "screen -t shell bash" >> $SCREENRC
1273 fi
1274 # If this service doesn't already exist in the screenrc file
1275 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1276 NL=`echo -ne '\015'`
1277 echo "screen -t $1 bash" >> $SCREENRC
1278 echo "stuff \"$2$NL\"" >> $SCREENRC
1279
Dean Troyerdde41d02014-12-09 17:47:57 -06001280 if [[ -n ${LOGDIR} ]]; then
1281 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001282 echo "log on" >>$SCREENRC
1283 fi
1284 fi
1285}
1286
1287# Stop a service in screen
1288# If a PID is available use it, kill the whole process group via TERM
1289# If screen is being used kill the screen window; this will catch processes
1290# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001291# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001292# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001293function screen_stop_service {
1294 local service=$1
1295
Dean Troyerdff49a22014-01-30 15:37:40 -06001296 SCREEN_NAME=${SCREEN_NAME:-stack}
1297 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001298 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001299
Dean Troyer3159a822014-08-27 14:13:58 -05001300 if is_service_enabled $service; then
1301 # Clean up the screen window
1302 screen -S $SCREEN_NAME -p $service -X kill
1303 fi
1304}
1305
1306# Stop a service process
1307# If a PID is available use it, kill the whole process group via TERM
1308# If screen is being used kill the screen window; this will catch processes
1309# that did not leave a PID behind
1310# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1311# stop_process service
1312function stop_process {
1313 local service=$1
1314
1315 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001316 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001317
1318 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001319 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001320 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1321 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1322 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001323 fi
1324 if [[ "$USE_SCREEN" = "True" ]]; then
1325 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001326 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001327 fi
1328 fi
1329}
1330
1331# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001332# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001333# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001334function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001335 local service
1336 local failures
1337 SCREEN_NAME=${SCREEN_NAME:-stack}
1338 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1339
1340
1341 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1342 echo "No service status directory found"
1343 return
1344 fi
1345
1346 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001347 # make this -o errexit safe
1348 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001349
1350 for service in $failures; do
1351 service=`basename $service`
1352 service=${service%.failure}
1353 echo "Error: Service $service is not running"
1354 done
1355
1356 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001357 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001358 fi
1359}
1360
Chris Dent2f27a0e2014-09-09 13:46:02 +01001361# Tail a log file in a screen if USE_SCREEN is true.
1362function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001363 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001364 local logfile=$2
1365
Sean Dague53753292014-12-04 19:38:15 -05001366 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001367 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001368 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001369 fi
1370}
1371
Dean Troyerdff49a22014-01-30 15:37:40 -06001372
Dean Troyer3159a822014-08-27 14:13:58 -05001373# Deprecated Functions
1374# --------------------
1375
1376# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1377# fork. It includes the dirty work of closing extra filehandles and preparing log
1378# files to produce the same logs as screen_it(). The log filename is derived
1379# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1380# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1381# _old_run_process service "command-line"
1382function _old_run_process {
1383 local service=$1
1384 local command="$2"
1385
1386 # Undo logging redirections and close the extra descriptors
1387 exec 1>&3
1388 exec 2>&3
1389 exec 3>&-
1390 exec 6>&-
1391
1392 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001393 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1394 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001395
1396 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1397 export PYTHONUNBUFFERED=1
1398 fi
1399
1400 exec /bin/bash -c "$command"
1401 die "$service exec failure: $command"
1402}
1403
1404# old_run_process() launches a child process that closes all file descriptors and
1405# then exec's the passed in command. This is meant to duplicate the semantics
1406# of screen_it() without screen. PIDs are written to
1407# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1408# old_run_process service "command-line"
1409function old_run_process {
1410 local service=$1
1411 local command="$2"
1412
1413 # Spawn the child process
1414 _old_run_process "$service" "$command" &
1415 echo $!
1416}
1417
1418# Compatibility for existing start_XXXX() functions
1419# Uses global ``USE_SCREEN``
1420# screen_it service "command-line"
1421function screen_it {
1422 if is_service_enabled $1; then
1423 # Append the service to the screen rc file
1424 screen_rc "$1" "$2"
1425
1426 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001427 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001428 else
1429 # Spawn directly without screen
1430 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1431 fi
1432 fi
1433}
1434
1435# Compatibility for existing stop_XXXX() functions
1436# Stop a service in screen
1437# If a PID is available use it, kill the whole process group via TERM
1438# If screen is being used kill the screen window; this will catch processes
1439# that did not leave a PID behind
1440# screen_stop service
1441function screen_stop {
1442 # Clean up the screen window
1443 stop_process $1
1444}
1445
1446
Sean Dague2c65e712014-12-18 09:44:56 -05001447# Plugin Functions
1448# =================
1449
1450DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1451
1452# enable_plugin <name> <url> [branch]
1453#
1454# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1455# ``url`` is a git url
1456# ``branch`` is a gitref. If it's not set, defaults to master
1457function enable_plugin {
1458 local name=$1
1459 local url=$2
1460 local branch=${3:-master}
1461 DEVSTACK_PLUGINS+=",$name"
1462 GITREPO[$name]=$url
1463 GITDIR[$name]=$DEST/$name
1464 GITBRANCH[$name]=$branch
1465}
1466
1467# fetch_plugins
1468#
1469# clones all plugins
1470function fetch_plugins {
1471 local plugins="${DEVSTACK_PLUGINS}"
1472 local plugin
1473
1474 # short circuit if nothing to do
1475 if [[ -z $plugins ]]; then
1476 return
1477 fi
1478
1479 echo "Fetching devstack plugins"
1480 for plugin in ${plugins//,/ }; do
1481 git_clone_by_name $plugin
1482 done
1483}
1484
1485# load_plugin_settings
1486#
1487# Load settings from plugins in the order that they were registered
1488function load_plugin_settings {
1489 local plugins="${DEVSTACK_PLUGINS}"
1490 local plugin
1491
1492 # short circuit if nothing to do
1493 if [[ -z $plugins ]]; then
1494 return
1495 fi
1496
1497 echo "Loading plugin settings"
1498 for plugin in ${plugins//,/ }; do
1499 local dir=${GITDIR[$plugin]}
1500 # source any known settings
1501 if [[ -f $dir/devstack/settings ]]; then
1502 source $dir/devstack/settings
1503 fi
1504 done
1505}
1506
1507# run_plugins
1508#
1509# Run the devstack/plugin.sh in all the plugin directories. These are
1510# run in registration order.
1511function run_plugins {
1512 local mode=$1
1513 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301514
1515 local plugins="${DEVSTACK_PLUGINS}"
1516 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001517 for plugin in ${plugins//,/ }; do
1518 local dir=${GITDIR[$plugin]}
1519 if [[ -f $dir/devstack/plugin.sh ]]; then
1520 source $dir/devstack/plugin.sh $mode $phase
1521 fi
1522 done
1523}
1524
1525function run_phase {
1526 local mode=$1
1527 local phase=$2
1528 if [[ -d $TOP_DIR/extras.d ]]; then
1529 for i in $TOP_DIR/extras.d/*.sh; do
1530 [[ -r $i ]] && source $i $mode $phase
1531 done
1532 fi
1533 # the source phase corresponds to settings loading in plugins
1534 if [[ "$mode" == "source" ]]; then
1535 load_plugin_settings
1536 else
1537 run_plugins $mode $phase
1538 fi
1539}
1540
Dean Troyerdff49a22014-01-30 15:37:40 -06001541
1542# Service Functions
1543# =================
1544
1545# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1546# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001547function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001548 echo "$1" | sed -e '
1549 s/,,/,/g;
1550 s/^,//;
1551 s/,$//
1552 '
1553}
1554
1555# disable_all_services() removes all current services
1556# from ``ENABLED_SERVICES`` to reset the configuration
1557# before a minimal installation
1558# Uses global ``ENABLED_SERVICES``
1559# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001560function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001561 ENABLED_SERVICES=""
1562}
1563
1564# Remove all services starting with '-'. For example, to install all default
1565# services except rabbit (rabbit) set in ``localrc``:
1566# ENABLED_SERVICES+=",-rabbit"
1567# Uses global ``ENABLED_SERVICES``
1568# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001569function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001570 local tmpsvcs="${ENABLED_SERVICES}"
1571 local service
1572 for service in ${tmpsvcs//,/ }; do
1573 if [[ ${service} == -* ]]; then
1574 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1575 fi
1576 done
1577 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1578}
1579
1580# disable_service() removes the services passed as argument to the
1581# ``ENABLED_SERVICES`` list, if they are present.
1582#
1583# For example:
1584# disable_service rabbit
1585#
1586# This function does not know about the special cases
1587# for nova, glance, and neutron built into is_service_enabled().
1588# Uses global ``ENABLED_SERVICES``
1589# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001590function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001591 local tmpsvcs=",${ENABLED_SERVICES},"
1592 local service
1593 for service in $@; do
1594 if is_service_enabled $service; then
1595 tmpsvcs=${tmpsvcs//,$service,/,}
1596 fi
1597 done
1598 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1599}
1600
1601# enable_service() adds the services passed as argument to the
1602# ``ENABLED_SERVICES`` list, if they are not already present.
1603#
1604# For example:
1605# enable_service qpid
1606#
1607# This function does not know about the special cases
1608# for nova, glance, and neutron built into is_service_enabled().
1609# Uses global ``ENABLED_SERVICES``
1610# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001611function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001612 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001613 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001614 for service in $@; do
1615 if ! is_service_enabled $service; then
1616 tmpsvcs+=",$service"
1617 fi
1618 done
1619 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1620 disable_negated_services
1621}
1622
1623# is_service_enabled() checks if the service(s) specified as arguments are
1624# enabled by the user in ``ENABLED_SERVICES``.
1625#
1626# Multiple services specified as arguments are ``OR``'ed together; the test
1627# is a short-circuit boolean, i.e it returns on the first match.
1628#
1629# There are special cases for some 'catch-all' services::
1630# **nova** returns true if any service enabled start with **n-**
1631# **cinder** returns true if any service enabled start with **c-**
1632# **ceilometer** returns true if any service enabled start with **ceilometer**
1633# **glance** returns true if any service enabled start with **g-**
1634# **neutron** returns true if any service enabled start with **q-**
1635# **swift** returns true if any service enabled start with **s-**
1636# **trove** returns true if any service enabled start with **tr-**
1637# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1638# **s-** services will be enabled. This will be deprecated in the future.
1639#
1640# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1641# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1642# as enabled in this case.
1643#
1644# Uses global ``ENABLED_SERVICES``
1645# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001646function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001647 local xtrace=$(set +o | grep xtrace)
1648 set +o xtrace
1649 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001650 local services=$@
1651 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001652 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001653 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001654
1655 # Look for top-level 'enabled' function for this service
1656 if type is_${service}_enabled >/dev/null 2>&1; then
1657 # A function exists for this service, use it
1658 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001659 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001660 fi
1661
1662 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1663 # are implemented
1664
Sean Dague45917cc2014-02-24 16:09:14 -05001665 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001666 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001667 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1668 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1669 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1670 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1671 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1672 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1673 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1674 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1675 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001676 done
Sean Dague45917cc2014-02-24 16:09:14 -05001677 $xtrace
1678 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001679}
1680
1681# Toggle enable/disable_service for services that must run exclusive of each other
1682# $1 The name of a variable containing a space-separated list of services
1683# $2 The name of a variable in which to store the enabled service's name
1684# $3 The name of the service to enable
1685function use_exclusive_service {
1686 local options=${!1}
1687 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001688 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001689 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001690 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001691 for opt in $options;do
1692 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1693 done
1694 eval "$out=$selection"
1695 return 0
1696}
1697
1698
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001699# System Functions
1700# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001701
1702# Only run the command if the target file (the last arg) is not on an
1703# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001704function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001705 local xtrace=$(set +o | grep xtrace)
1706 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001707 local args=( $@ )
1708 local last
1709 local sudo_cmd
1710 local dir_to_check
1711
1712 let last="${#args[*]} - 1"
1713
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001714 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001715 if [ ! -d "$dir_to_check" ]; then
1716 dir_to_check=`dirname "$dir_to_check"`
1717 fi
1718
1719 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001720 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001721 return 0
1722 fi
1723
1724 if [[ $TRACK_DEPENDS = True ]]; then
1725 sudo_cmd="env"
1726 else
1727 sudo_cmd="sudo"
1728 fi
1729
Sean Dague45917cc2014-02-24 16:09:14 -05001730 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001731 $sudo_cmd $@
1732}
1733
1734# Exit 0 if address is in network or 1 if address is not in network
1735# ip-range is in CIDR notation: 1.2.3.4/20
1736# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001737function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001738 local ip=$1
1739 local range=$2
1740 local masklen=${range#*/}
1741 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1742 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1743 [[ $network == $subnet ]]
1744}
1745
1746# Add a user to a group.
1747# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001748function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001749 local user=$1
1750 local group=$2
1751
1752 if [[ -z "$os_VENDOR" ]]; then
1753 GetOSVersion
1754 fi
1755
1756 # SLE11 and openSUSE 12.2 don't have the usual usermod
1757 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1758 sudo usermod -a -G "$group" "$user"
1759 else
1760 sudo usermod -A "$group" "$user"
1761 fi
1762}
1763
1764# Convert CIDR notation to a IPv4 netmask
1765# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001766function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001767 local maskpat="255 255 255 255"
1768 local maskdgt="254 252 248 240 224 192 128"
1769 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1770 echo ${1-0}.${2-0}.${3-0}.${4-0}
1771}
1772
1773# Gracefully cp only if source file/dir exists
1774# cp_it source destination
1775function cp_it {
1776 if [ -e $1 ] || [ -d $1 ]; then
1777 cp -pRL $1 $2
1778 fi
1779}
1780
1781# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1782# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1783# ``localrc`` or on the command line if necessary::
1784#
1785# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1786#
1787# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1788
Ian Wienandaee18c72014-02-21 15:35:08 +11001789function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001790 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001791 export http_proxy=$http_proxy
1792 fi
Sean Dague53753292014-12-04 19:38:15 -05001793 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001794 export https_proxy=$https_proxy
1795 fi
Sean Dague53753292014-12-04 19:38:15 -05001796 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001797 export no_proxy=$no_proxy
1798 fi
1799}
1800
1801# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001802function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001803 local mount_type=`stat -f -L -c %T $1`
1804 test "$mount_type" == "nfs"
1805}
1806
1807# Return the network portion of the given IP address using netmask
1808# netmask is in the traditional dotted-quad format
1809# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001810function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001811 local ip=$1
1812 local mask=$2
1813 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1814 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1815 echo $subnet
1816}
1817
1818# Service wrapper to restart services
1819# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001820function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001821 if is_ubuntu; then
1822 sudo /usr/sbin/service $1 restart
1823 else
1824 sudo /sbin/service $1 restart
1825 fi
1826}
1827
1828# Only change permissions of a file or directory if it is not on an
1829# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001830function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001831 _safe_permission_operation chmod $@
1832}
1833
1834# Only change ownership of a file or directory if it is not on an NFS
1835# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001836function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001837 _safe_permission_operation chown $@
1838}
1839
1840# Service wrapper to start services
1841# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001842function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001843 if is_ubuntu; then
1844 sudo /usr/sbin/service $1 start
1845 else
1846 sudo /sbin/service $1 start
1847 fi
1848}
1849
1850# Service wrapper to stop services
1851# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001852function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001853 if is_ubuntu; then
1854 sudo /usr/sbin/service $1 stop
1855 else
1856 sudo /sbin/service $1 stop
1857 fi
1858}
1859
1860
1861# Restore xtrace
1862$XTRACE
1863
1864# Local variables:
1865# mode: shell-script
1866# End: