blob: 6d9a0fa6b08efdabedf8c638cf9d48eab3578f98 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Dean Troyerdff49a22014-01-30 15:37:40 -06003# functions-common - Common functions used by DevStack components
4#
5# The canonical copy of this file is maintained in the DevStack repo.
6# All modifications should be made there and then sync'ed to other repos
7# as required.
8#
9# This file is sorted alphabetically within the function groups.
10#
11# - Config Functions
12# - Control Functions
13# - Distro Functions
14# - Git Functions
15# - OpenStack Functions
16# - Package Functions
17# - Process Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060018# - Service Functions
Masayuki Igawaf6368d32014-02-20 13:31:26 +090019# - System Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060020#
21# The following variables are assumed to be defined by certain functions:
22#
23# - ``ENABLED_SERVICES``
24# - ``ERROR_ON_CLONE``
25# - ``FILES``
26# - ``OFFLINE``
Dean Troyerdff49a22014-01-30 15:37:40 -060027# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090028# - ``REQUIREMENTS_DIR``
29# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060030# - ``TRACK_DEPENDS``
Masayuki Igawad20f6322014-02-28 09:22:37 +090031# - ``UNDO_REQUIREMENTS``
Dean Troyerdff49a22014-01-30 15:37:40 -060032# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050033#
Dean Troyerdff49a22014-01-30 15:37:40 -060034
35# Save trace setting
36XTRACE=$(set +o | grep xtrace)
37set +o xtrace
38
Sean Daguecc524062014-10-01 09:06:43 -040039# Global Config Variables
40declare -A GITREPO
41declare -A GITBRANCH
42declare -A GITDIR
43
Sean Dague53753292014-12-04 19:38:15 -050044TRACK_DEPENDS=${TRACK_DEPENDS:-False}
45
Dean Troyerdff49a22014-01-30 15:37:40 -060046
47# Normalize config values to True or False
48# Accepts as False: 0 no No NO false False FALSE
49# Accepts as True: 1 yes Yes YES true True TRUE
50# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +110051function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -050052 local xtrace=$(set +o | grep xtrace)
53 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060054
Mahito OGURA98f59aa2015-05-11 18:02:34 +090055 local default=$1
56 local testval=${!2:-}
57
58 case "$testval" in
59 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
60 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
61 * ) echo "$default" ;;
62 esac
63
Sean Dague45917cc2014-02-24 16:09:14 -050064 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060065}
66
Attila Fazekas1bd79592015-02-24 14:06:56 +010067function isset {
68 [[ -v "$1" ]]
69}
Dean Troyerdff49a22014-01-30 15:37:40 -060070
71# Control Functions
72# =================
73
74# Prints backtrace info
75# filename:lineno:function
76# backtrace level
77function backtrace {
78 local level=$1
79 local deep=$((${#BASH_SOURCE[@]} - 1))
80 echo "[Call Trace]"
81 while [ $level -le $deep ]; do
82 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
83 deep=$((deep - 1))
84 done
85}
86
87# Prints line number and "message" then exits
88# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +110089function die {
Dean Troyerdff49a22014-01-30 15:37:40 -060090 local exitcode=$?
91 set +o xtrace
92 local line=$1; shift
93 if [ $exitcode == 0 ]; then
94 exitcode=1
95 fi
96 backtrace 2
97 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -060098 # Give buffers a second to flush
99 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600100 exit $exitcode
101}
102
103# Checks an environment variable is not set or has length 0 OR if the
104# exit code is non-zero and prints "message" and exits
105# NOTE: env-var is the variable name without a '$'
106# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100107function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600108 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500109 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600110 set +o xtrace
111 local line=$1; shift
112 local evar=$1; shift
113 if ! is_set $evar || [ $exitcode != 0 ]; then
114 die $line "$*"
115 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500116 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600117}
118
119# Prints line number and "message" in error format
120# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100121function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600122 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500123 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600124 set +o xtrace
125 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
126 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600127 if [[ -n ${LOGDIR} ]]; then
128 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600129 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500130 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600131 return $exitcode
132}
133
134# Checks an environment variable is not set or has length 0 OR if the
135# exit code is non-zero and prints "message"
136# NOTE: env-var is the variable name without a '$'
137# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100138function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600139 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500140 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600141 set +o xtrace
142 local line=$1; shift
143 local evar=$1; shift
144 if ! is_set $evar || [ $exitcode != 0 ]; then
145 err $line "$*"
146 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500147 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600148 return $exitcode
149}
150
151# Exit after outputting a message about the distribution not being supported.
152# exit_distro_not_supported [optional-string-telling-what-is-missing]
153function exit_distro_not_supported {
154 if [[ -z "$DISTRO" ]]; then
155 GetDistro
156 fi
157
158 if [ $# -gt 0 ]; then
159 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
160 else
161 die $LINENO "Support for $DISTRO is incomplete."
162 fi
163}
164
165# Test if the named environment variable is set and not zero length
166# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100167function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600168 local var=\$"$1"
169 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
170}
171
172# Prints line number and "message" in warning format
173# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100174function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600175 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500176 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600177 set +o xtrace
178 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Sean Daguee4af9292015-04-28 08:57:57 -0400179 echo $msg
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500180 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600181 return $exitcode
182}
183
184
185# Distro Functions
186# ================
187
188# Determine OS Vendor, Release and Update
189# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
190# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500191# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
192# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
193# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
194# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
195# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500196os_VENDOR=""
197os_RELEASE=""
198os_UPDATE=""
199os_PACKAGE=""
200os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500201
Dean Troyerdff49a22014-01-30 15:37:40 -0600202# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100203function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500204
Dean Troyerdff49a22014-01-30 15:37:40 -0600205 # Figure out which vendor we are
206 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
207 # OS/X
208 os_VENDOR=`sw_vers -productName`
209 os_RELEASE=`sw_vers -productVersion`
210 os_UPDATE=${os_RELEASE##*.}
211 os_RELEASE=${os_RELEASE%.*}
212 os_PACKAGE=""
213 if [[ "$os_RELEASE" =~ "10.7" ]]; then
214 os_CODENAME="lion"
215 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
216 os_CODENAME="snow leopard"
217 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
218 os_CODENAME="leopard"
219 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
220 os_CODENAME="tiger"
221 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
222 os_CODENAME="panther"
223 else
224 os_CODENAME=""
225 fi
226 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
227 os_VENDOR=$(lsb_release -i -s)
228 os_RELEASE=$(lsb_release -r -s)
229 os_UPDATE=""
230 os_PACKAGE="rpm"
231 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
232 os_PACKAGE="deb"
233 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
234 lsb_release -d -s | grep -q openSUSE
235 if [[ $? -eq 0 ]]; then
236 os_VENDOR="openSUSE"
237 fi
238 elif [[ $os_VENDOR == "openSUSE project" ]]; then
239 os_VENDOR="openSUSE"
240 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
241 os_VENDOR="Red Hat"
242 fi
243 os_CODENAME=$(lsb_release -c -s)
244 elif [[ -r /etc/redhat-release ]]; then
245 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
246 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
247 # CentOS release 5.5 (Final)
248 # CentOS Linux release 6.0 (Final)
249 # Fedora release 16 (Verne)
250 # XenServer release 6.2.0-70446c (xenenterprise)
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700251 # Oracle Linux release 7
Dean Troyerdff49a22014-01-30 15:37:40 -0600252 os_CODENAME=""
253 for r in "Red Hat" CentOS Fedora XenServer; do
254 os_VENDOR=$r
255 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
256 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
257 os_CODENAME=${ver#*|}
258 os_RELEASE=${ver%|*}
259 os_UPDATE=${os_RELEASE##*.}
260 os_RELEASE=${os_RELEASE%.*}
261 break
262 fi
263 os_VENDOR=""
264 done
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700265 if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
266 os_VENDOR=OracleLinux
267 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600268 os_PACKAGE="rpm"
269 elif [[ -r /etc/SuSE-release ]]; then
270 for r in openSUSE "SUSE Linux"; do
271 if [[ "$r" = "SUSE Linux" ]]; then
272 os_VENDOR="SUSE LINUX"
273 else
274 os_VENDOR=$r
275 fi
276
277 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
278 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
279 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
280 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
281 break
282 fi
283 os_VENDOR=""
284 done
285 os_PACKAGE="rpm"
286 # If lsb_release is not installed, we should be able to detect Debian OS
287 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
288 os_VENDOR="Debian"
289 os_PACKAGE="deb"
290 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
291 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
292 fi
293 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
294}
295
296# Translate the OS version values into common nomenclature
297# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500298declare DISTRO
299
Ian Wienandaee18c72014-02-21 15:35:08 +1100300function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600301 GetOSVersion
302 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
303 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
304 DISTRO=$os_CODENAME
305 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
306 # For Fedora, just use 'f' and the release
307 DISTRO="f$os_RELEASE"
308 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
309 DISTRO="opensuse-$os_RELEASE"
310 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
311 # For SLE, also use the service pack
312 if [[ -z "$os_UPDATE" ]]; then
313 DISTRO="sle${os_RELEASE}"
314 else
315 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
316 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530317 elif [[ "$os_VENDOR" =~ (Red Hat) || \
318 "$os_VENDOR" =~ (CentOS) || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700319 "$os_VENDOR" =~ (OracleLinux) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600320 # Drop the . release as we assume it's compatible
321 DISTRO="rhel${os_RELEASE::1}"
322 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
323 DISTRO="xs$os_RELEASE"
324 else
325 # Catch-all for now is Vendor + Release + Update
326 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
327 fi
328 export DISTRO
329}
330
331# Utility function for checking machine architecture
332# is_arch arch-type
333function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500334 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600335}
336
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700337# Determine if current distribution is an Oracle distribution
338# is_oraclelinux
339function is_oraclelinux {
340 if [[ -z "$os_VENDOR" ]]; then
341 GetOSVersion
342 fi
343
344 [ "$os_VENDOR" = "OracleLinux" ]
345}
346
347
Dean Troyerdff49a22014-01-30 15:37:40 -0600348# Determine if current distribution is a Fedora-based distribution
349# (Fedora, RHEL, CentOS, etc).
350# is_fedora
351function is_fedora {
352 if [[ -z "$os_VENDOR" ]]; then
353 GetOSVersion
354 fi
355
anju Tiwari6c639c92014-07-15 18:11:54 +0530356 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700357 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600358}
359
360
361# Determine if current distribution is a SUSE-based distribution
362# (openSUSE, SLE).
363# is_suse
364function is_suse {
365 if [[ -z "$os_VENDOR" ]]; then
366 GetOSVersion
367 fi
368
369 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
370}
371
372
373# Determine if current distribution is an Ubuntu-based distribution
374# It will also detect non-Ubuntu but Debian-based distros
375# is_ubuntu
376function is_ubuntu {
377 if [[ -z "$os_PACKAGE" ]]; then
378 GetOSVersion
379 fi
380 [ "$os_PACKAGE" = "deb" ]
381}
382
383
384# Git Functions
385# =============
386
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600387# Returns openstack release name for a given branch name
388# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100389function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600390 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700391 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600392 echo ${branch#*/}
393 else
394 echo "master"
395 fi
396}
397
Dean Troyerdff49a22014-01-30 15:37:40 -0600398# git clone only if directory doesn't exist already. Since ``DEST`` might not
399# be owned by the installation user, we create the directory and change the
400# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500401# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
402# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600403# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500404# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600405# git_clone remote dest-dir branch
406function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500407 local git_remote=$1
408 local git_dest=$2
409 local git_ref=$3
410 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200411 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500412
Sean Dague53753292014-12-04 19:38:15 -0500413 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800414 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200415 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
416 fi
417
Dean Troyerdff49a22014-01-30 15:37:40 -0600418 if [[ "$OFFLINE" = "True" ]]; then
419 echo "Running in offline mode, clones already exist"
420 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500421 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600422 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400423 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600424 return
425 fi
426
Dean Troyer50cda692014-07-25 11:57:20 -0500427 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600428 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500429 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600430 [[ "$ERROR_ON_CLONE" = "True" ]] && \
431 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200432 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600433 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500434 cd $git_dest
435 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600436 else
437 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500438 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600439 [[ "$ERROR_ON_CLONE" = "True" ]] && \
440 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200441 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500442 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600443 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500444 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600445 elif [[ "$RECLONE" = "True" ]]; then
446 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500447 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600448 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500449 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100450 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600451 # remove the existing ignored files (like pyc) as they cause breakage
452 # (due to the py files having older timestamps than our pyc, so python
453 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500454 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600455
Dean Troyer50cda692014-07-25 11:57:20 -0500456 # handle git_ref accordingly to type (tag, branch)
457 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
458 git_update_tag $git_ref
459 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
460 git_update_branch $git_ref
461 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
462 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600463 else
Dean Troyer50cda692014-07-25 11:57:20 -0500464 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600465 fi
466
467 fi
468 fi
469
470 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500471 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600472 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400473 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600474}
475
Sean Daguecc524062014-10-01 09:06:43 -0400476# A variation on git clone that lets us specify a project by it's
477# actual name, like oslo.config. This is exceptionally useful in the
478# library installation case
479function git_clone_by_name {
480 local name=$1
481 local repo=${GITREPO[$name]}
482 local dir=${GITDIR[$name]}
483 local branch=${GITBRANCH[$name]}
484 git_clone $repo $dir $branch
485}
486
487
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100488# git can sometimes get itself infinitely stuck with transient network
489# errors or other issues with the remote end. This wraps git in a
490# timeout/retry loop and is intended to watch over non-local git
491# processes that might hang. GIT_TIMEOUT, if set, is passed directly
492# to timeout(1); otherwise the default value of 0 maintains the status
493# quo of waiting forever.
494# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100495function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100496 local count=0
497 local timeout=0
498
499 if [[ -n "${GIT_TIMEOUT}" ]]; then
500 timeout=${GIT_TIMEOUT}
501 fi
502
503 until timeout -s SIGINT ${timeout} git "$@"; do
504 # 124 is timeout(1)'s special return code when it reached the
505 # timeout; otherwise assume fatal failure
506 if [[ $? -ne 124 ]]; then
507 die $LINENO "git call failed: [git $@]"
508 fi
509
510 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400511 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100512 if [ $count -eq 3 ]; then
513 die $LINENO "Maximum of 3 git retries reached"
514 fi
515 sleep 5
516 done
517}
518
Dean Troyerdff49a22014-01-30 15:37:40 -0600519# git update using reference as a branch.
520# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100521function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500522 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600523
Dean Troyer50cda692014-07-25 11:57:20 -0500524 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600525 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500526 git branch -D $git_branch || true
527 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600528}
529
530# git update using reference as a branch.
531# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100532function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500533 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600534
Dean Troyer50cda692014-07-25 11:57:20 -0500535 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600536}
537
538# git update using reference as a tag. Be careful editing source at that repo
539# as working copy will be in a detached mode
540# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100541function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500542 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600543
Dean Troyer50cda692014-07-25 11:57:20 -0500544 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600545 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500546 git_timed fetch origin tag $git_tag
547 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600548}
549
550
551# OpenStack Functions
552# ===================
553
554# Get the default value for HOST_IP
555# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100556function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600557 local fixed_range=$1
558 local floating_range=$2
559 local host_ip_iface=$3
560 local host_ip=$4
561
Dean Troyerdff49a22014-01-30 15:37:40 -0600562 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
563 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
564 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100565 # Find the interface used for the default route
566 host_ip_iface=${host_ip_iface:-$(ip route | awk '/default/ {print $5}' | head -1)}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500567 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
568 local ip
569 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600570 # Attempt to filter out IP addresses that are part of the fixed and
571 # floating range. Note that this method only works if the ``netaddr``
572 # python library is installed. If it is not installed, an error
573 # will be printed and the first IP from the interface will be used.
574 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
575 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500576 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
577 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600578 break;
579 fi
580 done
581 fi
582 echo $host_ip
583}
584
Attila Fazekasf71b5002014-05-28 09:52:22 +0200585# Generates hex string from ``size`` byte of pseudo random data
586# generate_hex_string size
587function generate_hex_string {
588 local size=$1
589 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
590}
591
Dean Troyerdff49a22014-01-30 15:37:40 -0600592# Grab a numbered field from python prettytable output
593# Fields are numbered starting with 1
594# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
595# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100596function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500597 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600598 while read data; do
599 if [ "$1" -lt 0 ]; then
600 field="(\$(NF$1))"
601 else
602 field="\$$(($1 + 1))"
603 fi
604 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
605 done
606}
607
yuntongjinf26deea2015-02-28 10:50:34 +0800608# install default policy
609# copy over a default policy.json and policy.d for projects
610function install_default_policy {
611 local project=$1
612 local project_uc=$(echo $1|tr a-z A-Z)
613 local conf_dir="${project_uc}_CONF_DIR"
614 # eval conf dir to get the variable
615 conf_dir="${!conf_dir}"
616 local project_dir="${project_uc}_DIR"
617 # eval project dir to get the variable
618 project_dir="${!project_dir}"
619 local sample_conf_dir="${project_dir}/etc/${project}"
620 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
621
622 # first copy any policy.json
623 cp -p $sample_conf_dir/policy.json $conf_dir
624 # then optionally copy over policy.d
625 if [[ -d $sample_policy_dir ]]; then
626 cp -r $sample_policy_dir $conf_dir/policy.d
627 fi
628}
629
Dean Troyerdff49a22014-01-30 15:37:40 -0600630# Add a policy to a policy.json file
631# Do nothing if the policy already exists
632# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100633function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600634 local policy_file=$1
635 local policy_name=$2
636 local policy_perm=$3
637
638 if grep -q ${policy_name} ${policy_file}; then
639 echo "Policy ${policy_name} already exists in ${policy_file}"
640 return
641 fi
642
643 # Add a terminating comma to policy lines without one
644 # Remove the closing '}' and all lines following to the end-of-file
645 local tmpfile=$(mktemp)
646 uniq ${policy_file} | sed -e '
647 s/]$/],/
648 /^[}]/,$d
649 ' > ${tmpfile}
650
651 # Append policy and closing brace
652 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
653 echo "}" >>${tmpfile}
654
655 mv ${tmpfile} ${policy_file}
656}
657
Alistair Coles24779f62014-10-15 18:57:59 +0100658# Gets or creates a domain
659# Usage: get_or_create_domain <name> <description>
660function get_or_create_domain {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500661 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100662 # Gets domain id
663 local domain_id=$(
664 # Gets domain id
665 openstack --os-token=$OS_TOKEN --os-url=$os_url \
666 --os-identity-api-version=3 domain show $1 \
667 -f value -c id 2>/dev/null ||
668 # Creates new domain
669 openstack --os-token=$OS_TOKEN --os-url=$os_url \
670 --os-identity-api-version=3 domain create $1 \
671 --description "$2" \
672 -f value -c id
673 )
674 echo $domain_id
675}
676
Steve Martinellib74e01c2014-12-18 01:35:35 -0500677# Gets or creates group
678# Usage: get_or_create_group <groupname> [<domain> <description>]
679function get_or_create_group {
680 local domain=${2:+--domain ${2}}
681 local desc="${3:-}"
682 local os_url="$KEYSTONE_SERVICE_URI_V3"
683 # Gets group id
684 local group_id=$(
685 # Creates new group with --or-show
686 openstack --os-token=$OS_TOKEN --os-url=$os_url \
687 --os-identity-api-version=3 group create $1 \
688 $domain --description "$desc" --or-show \
689 -f value -c id
690 )
691 echo $group_id
692}
693
Bartosz Górski0abde392014-02-28 14:15:19 +0100694# Gets or creates user
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000695# Usage: get_or_create_user <username> <password> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100696function get_or_create_user {
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000697 if [[ ! -z "$3" ]]; then
698 local email="--email=$3"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200699 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500700 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200701 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100702 local os_cmd="openstack"
703 local domain=""
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000704 if [[ ! -z "$4" ]]; then
705 domain="--domain=$4"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500706 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100707 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100708 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500709 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500710 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100711 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100712 $1 \
713 --password "$2" \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500714 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100715 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500716 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100717 -f value -c id
718 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500719 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100720}
721
722# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100723# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100724function get_or_create_project {
725 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100726 local os_cmd="openstack"
727 local domain=""
728 if [[ ! -z "$2" ]]; then
729 domain="--domain=$2"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500730 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100731 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500732 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500733 # Creates new project with --or-show
734 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100735 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500736 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100737}
738
739# Gets or creates role
740# Usage: get_or_create_role <name>
741function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500742 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500743 # Creates role with --or-show
744 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100745 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500746 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100747}
748
Jamie Lennox9b215db2015-02-10 18:19:57 +1100749# Gets or adds user role to project
750# Usage: get_or_add_user_project_role <role> <user> <project>
751function get_or_add_user_project_role {
Bartosz Górski0abde392014-02-28 14:15:19 +0100752 # Gets user role id
Steve Martinelli5541a612015-01-19 15:58:49 -0500753 local user_role_id=$(openstack role list \
754 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100755 --project $3 \
756 --column "ID" \
757 --column "Name" \
758 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500759 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100760 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500761 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100762 $1 \
763 --user $2 \
764 --project $3 \
765 | grep " id " | get_field 2)
766 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500767 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100768}
769
Steve Martinelli4599fd12015-03-12 21:30:58 -0400770# Gets or adds group role to project
771# Usage: get_or_add_group_project_role <role> <group> <project>
772function get_or_add_group_project_role {
773 # Gets group role id
774 local group_role_id=$(openstack role list \
775 --group $2 \
776 --project $3 \
777 --column "ID" \
778 --column "Name" \
779 | grep " $1 " | get_field 1)
780 if [[ -z "$group_role_id" ]]; then
781 # Adds role to group
782 group_role_id=$(openstack role add \
783 $1 \
784 --group $2 \
785 --project $3 \
786 | grep " id " | get_field 2)
787 fi
788 echo $group_role_id
789}
790
Bartosz Górski0abde392014-02-28 14:15:19 +0100791# Gets or creates service
792# Usage: get_or_create_service <name> <type> <description>
793function get_or_create_service {
794 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500795 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100796 # Gets service id
797 openstack service show $1 -f value -c id 2>/dev/null ||
798 # Creates new service if not exists
799 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500800 $2 \
801 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100802 --description="$3" \
803 -f value -c id
804 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500805 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100806}
807
808# Gets or creates endpoint
809# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
810function get_or_create_endpoint {
811 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500812 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100813 --column "ID" \
814 --column "Region" \
815 --column "Service Name" \
816 | grep " $2 " \
817 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500818 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100819 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500820 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100821 $1 \
822 --region $2 \
823 --publicurl $3 \
824 --adminurl $4 \
825 --internalurl $5 \
826 | grep " id " | get_field 2)
827 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500828 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100829}
Dean Troyerdff49a22014-01-30 15:37:40 -0600830
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500831
Dean Troyerdff49a22014-01-30 15:37:40 -0600832# Package Functions
833# =================
834
835# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100836function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800837 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600838 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800839
840 if [[ -z "$base_dir" ]]; then
841 base_dir=$FILES
842 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600843 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800844 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600845 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800846 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600847 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800848 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600849 else
850 exit_distro_not_supported "list of packages"
851 fi
852 echo "$pkg_dir"
853}
854
855# Wrapper for ``apt-get`` to set cache and proxy environment variables
856# Uses globals ``OFFLINE``, ``*_proxy``
857# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100858function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500859 local xtrace=$(set +o | grep xtrace)
860 set +o xtrace
861
Dean Troyerdff49a22014-01-30 15:37:40 -0600862 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
863 local sudo="sudo"
864 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500865
866 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500867
Dean Troyerdff49a22014-01-30 15:37:40 -0600868 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500869 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
870 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600871 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
872}
873
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800874function _parse_package_files {
875 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600876
Dean Troyerdff49a22014-01-30 15:37:40 -0600877 if [[ -z "$DISTRO" ]]; then
878 GetDistro
879 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600880
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800881 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600882 local OIFS line package distros distro
883 [[ -e $fname ]] || continue
884
885 OIFS=$IFS
886 IFS=$'\n'
887 for line in $(<${fname}); do
888 if [[ $line =~ "NOPRIME" ]]; then
889 continue
890 fi
891
892 # Assume we want this package
893 package=${line%#*}
894 inst_pkg=1
895
896 # Look for # dist:xxx in comment
897 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
898 # We are using BASH regexp matching feature.
899 package=${BASH_REMATCH[1]}
900 distros=${BASH_REMATCH[2]}
901 # In bash ${VAR,,} will lowecase VAR
902 # Look for a match in the distro list
903 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
904 # If no match then skip this package
905 inst_pkg=0
906 fi
907 fi
908
Dean Troyerdff49a22014-01-30 15:37:40 -0600909 if [[ $inst_pkg = 1 ]]; then
910 echo $package
911 fi
912 done
913 IFS=$OIFS
914 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800915}
916
917# get_packages() collects a list of package names of any type from the
918# prerequisite files in ``files/{debs|rpms}``. The list is intended
919# to be passed to a package installer such as apt or yum.
920#
921# Only packages required for the services in 1st argument will be
922# included. Two bits of metadata are recognized in the prerequisite files:
923#
924# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
925# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
926# of the package to the distros listed. The distro names are case insensitive.
927function get_packages {
928 local xtrace=$(set +o | grep xtrace)
929 set +o xtrace
930 local services=$@
931 local package_dir=$(_get_package_dir)
932 local file_to_parse=""
933 local service=""
934
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800935 if [[ -z "$package_dir" ]]; then
936 echo "No package directory supplied"
937 return 1
938 fi
939 for service in ${services//,/ }; do
940 # Allow individual services to specify dependencies
941 if [[ -e ${package_dir}/${service} ]]; then
942 file_to_parse="${file_to_parse} ${package_dir}/${service}"
943 fi
944 # NOTE(sdague) n-api needs glance for now because that's where
945 # glance client is
946 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700947 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800948 file_to_parse="${file_to_parse} ${package_dir}/nova"
949 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700950 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800951 file_to_parse="${file_to_parse} ${package_dir}/glance"
952 fi
953 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700954 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800955 file_to_parse="${file_to_parse} ${package_dir}/cinder"
956 fi
957 elif [[ $service == ceilometer-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700958 if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800959 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
960 fi
961 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700962 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800963 file_to_parse="${file_to_parse} ${package_dir}/swift"
964 fi
965 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700966 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800967 file_to_parse="${file_to_parse} ${package_dir}/nova"
968 fi
969 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700970 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800971 file_to_parse="${file_to_parse} ${package_dir}/glance"
972 fi
973 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700974 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800975 file_to_parse="${file_to_parse} ${package_dir}/keystone"
976 fi
977 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700978 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800979 file_to_parse="${file_to_parse} ${package_dir}/neutron"
980 fi
981 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700982 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800983 file_to_parse="${file_to_parse} ${package_dir}/ironic"
984 fi
985 fi
986 done
987 echo "$(_parse_package_files $file_to_parse)"
988 $xtrace
989}
990
991# get_plugin_packages() collects a list of package names of any type from a
992# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
993# list is intended to be passed to a package installer such as apt or yum.
994#
995# Only packages required for enabled and collected plugins will included.
996#
Dean Troyerdc97cb72015-03-28 08:20:50 -0500997# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800998# in these prerequisite files, see get_packages() for more info.
999function get_plugin_packages {
1000 local xtrace=$(set +o | grep xtrace)
1001 set +o xtrace
1002 local files_to_parse=""
1003 local package_dir=""
1004 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1005 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1006 files_to_parse+="$package_dir/$plugin"
1007 done
1008 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001009 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001010}
1011
1012# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001013# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001014# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001015function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001016 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1017 REPOS_UPDATED=${REPOS_UPDATED:-False}
1018 RETRY_UPDATE=${RETRY_UPDATE:-False}
1019
Paul Linchpiner9e179742014-07-13 22:23:00 -07001020 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001021 return 0
1022 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001023
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001024 if is_ubuntu; then
1025 local xtrace=$(set +o | grep xtrace)
1026 set +o xtrace
1027 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1028 # if there are transient errors pulling the updates, that's fine.
1029 # It may be secondary repositories that we don't really care about.
1030 apt_get update || /bin/true
1031 REPOS_UPDATED=True
1032 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001033 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001034 fi
1035}
1036
1037function real_install_package {
1038 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001039 apt_get install "$@"
1040 elif is_fedora; then
1041 yum_install "$@"
1042 elif is_suse; then
1043 zypper_install "$@"
1044 else
1045 exit_distro_not_supported "installing packages"
1046 fi
1047}
1048
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001049# Distro-agnostic package installer
1050# install_package package [package ...]
1051function install_package {
1052 update_package_repo
1053 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1054}
1055
Dean Troyerdff49a22014-01-30 15:37:40 -06001056# Distro-agnostic function to tell if a package is installed
1057# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001058function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001059 if [[ -z "$@" ]]; then
1060 return 1
1061 fi
1062
1063 if [[ -z "$os_PACKAGE" ]]; then
1064 GetOSVersion
1065 fi
1066
1067 if [[ "$os_PACKAGE" = "deb" ]]; then
1068 dpkg -s "$@" > /dev/null 2> /dev/null
1069 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1070 rpm --quiet -q "$@"
1071 else
1072 exit_distro_not_supported "finding if a package is installed"
1073 fi
1074}
1075
1076# Distro-agnostic package uninstaller
1077# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001078function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001079 if is_ubuntu; then
1080 apt_get purge "$@"
1081 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001082 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001083 elif is_suse; then
1084 sudo zypper rm "$@"
1085 else
1086 exit_distro_not_supported "uninstalling packages"
1087 fi
1088}
1089
1090# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001091# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001092# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001093function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001094 [[ "$OFFLINE" = "True" ]] && return
1095 local sudo="sudo"
1096 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001097
1098 # The manual check for missing packages is because yum -y assumes
1099 # missing packages are OK. See
1100 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001101 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1102 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001103 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001104 awk '
1105 BEGIN { fail=0 }
1106 /No package/ { fail=1 }
1107 { print }
1108 END { exit fail }' || \
1109 die $LINENO "Missing packages detected"
1110
1111 # also ensure we catch a yum failure
1112 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001113 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001114 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001115}
1116
1117# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001118# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001119# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001120function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001121 [[ "$OFFLINE" = "True" ]] && return
1122 local sudo="sudo"
1123 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001124 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1125 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001126 zypper --non-interactive install --auto-agree-with-licenses "$@"
1127}
1128
1129
1130# Process Functions
1131# =================
1132
1133# _run_process() is designed to be backgrounded by run_process() to simulate a
1134# fork. It includes the dirty work of closing extra filehandles and preparing log
1135# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001136# from the service name.
1137# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001138# If an optional group is provided sg will be used to set the group of
1139# the command.
1140# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001141function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001142 # disable tracing through the exec redirects, it's just confusing in the logs.
1143 xtrace=$(set +o | grep xtrace)
1144 set +o xtrace
1145
Dean Troyerdff49a22014-01-30 15:37:40 -06001146 local service=$1
1147 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001148 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001149
1150 # Undo logging redirections and close the extra descriptors
1151 exec 1>&3
1152 exec 2>&3
1153 exec 3>&-
1154 exec 6>&-
1155
Dean Troyerdde41d02014-12-09 17:47:57 -06001156 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1157 if [[ -n ${LOGDIR} ]]; then
1158 exec 1>&"$real_logfile" 2>&1
1159 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1160 if [[ -n ${SCREEN_LOGDIR} ]]; then
1161 # Drop the backward-compat symlink
1162 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1163 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001164
1165 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1166 export PYTHONUNBUFFERED=1
1167 fi
1168
Sean Dague6e137ab2015-04-29 08:22:24 -04001169 # reenable xtrace before we do *real* work
1170 $xtrace
1171
Dean Troyer3159a822014-08-27 14:13:58 -05001172 # Run under ``setsid`` to force the process to become a session and group leader.
1173 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001174 if [[ -n "$group" ]]; then
1175 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1176 else
1177 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1178 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001179
1180 # Just silently exit this process
1181 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001182}
1183
1184# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1185# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001186# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001187# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001188function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001189 SCREEN_NAME=${SCREEN_NAME:-stack}
1190 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1191
1192 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1193 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1194 fi
1195
1196 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1197}
1198
1199# Find out if a process exists by partial name.
1200# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001201function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001202 local name=$1
1203 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001204 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001205 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001206 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001207}
1208
Dean Troyer3159a822014-08-27 14:13:58 -05001209# Run a single service under screen or directly
1210# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001211# If an optional group is provided sg will be used to run the
1212# command as that group.
1213# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001214function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001215 local service=$1
1216 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001217 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001218
Dean Troyer3159a822014-08-27 14:13:58 -05001219 if is_service_enabled $service; then
1220 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001221 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001222 else
1223 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001224 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001225 fi
1226 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001227}
1228
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001229# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001230# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001231# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001232# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001233# Run a command in a shell in a screen window, if an optional group
1234# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001235function screen_process {
1236 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001237 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001238 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001239
Sean Dagueea22a4f2014-06-27 15:21:41 -04001240 SCREEN_NAME=${SCREEN_NAME:-stack}
1241 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001242 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001243
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001244 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001245
Dean Troyerdde41d02014-12-09 17:47:57 -06001246 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1247 echo "LOGDIR: $LOGDIR"
1248 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1249 echo "log: $real_logfile"
1250 if [[ -n ${LOGDIR} ]]; then
1251 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001252 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001253 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1254 if [[ -n ${SCREEN_LOGDIR} ]]; then
1255 # Drop the backward-compat symlink
1256 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1257 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001258 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001259
1260 # sleep to allow bash to be ready to be send the command - we are
1261 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001262 # bash isn't running by the time we send the command, nothing
1263 # happens. This sleep was added originally to handle gate runs
1264 # where we needed this to be at least 3 seconds to pass
1265 # consistently on slow clouds. Now this is configurable so that we
1266 # can determine a reasonable value for the local case which should
1267 # be much smaller.
1268 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001269
1270 NL=`echo -ne '\015'`
1271 # This fun command does the following:
1272 # - the passed server command is backgrounded
1273 # - the pid of the background process is saved in the usual place
1274 # - the server process is brought back to the foreground
1275 # - if the server process exits prematurely the fg command errors
1276 # and a message is written to stdout and the process failure file
1277 #
1278 # The pid saved can be used in stop_process() as a process group
1279 # id to kill off all child processes
1280 if [[ -n "$group" ]]; then
1281 command="sg $group '$command'"
1282 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001283
1284 # Append the process to the screen rc file
1285 screen_rc "$name" "$command"
1286
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001287 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 -06001288}
1289
1290# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001291# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001292# screen_rc service "command-line"
1293function screen_rc {
1294 SCREEN_NAME=${SCREEN_NAME:-stack}
1295 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1296 if [[ ! -e $SCREENRC ]]; then
1297 # Name the screen session
1298 echo "sessionname $SCREEN_NAME" > $SCREENRC
1299 # Set a reasonable statusbar
1300 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1301 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1302 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1303 echo "screen -t shell bash" >> $SCREENRC
1304 fi
1305 # If this service doesn't already exist in the screenrc file
1306 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1307 NL=`echo -ne '\015'`
1308 echo "screen -t $1 bash" >> $SCREENRC
1309 echo "stuff \"$2$NL\"" >> $SCREENRC
1310
Dean Troyerdde41d02014-12-09 17:47:57 -06001311 if [[ -n ${LOGDIR} ]]; then
1312 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001313 echo "log on" >>$SCREENRC
1314 fi
1315 fi
1316}
1317
1318# Stop a service in screen
1319# If a PID is available use it, kill the whole process group via TERM
1320# If screen is being used kill the screen window; this will catch processes
1321# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001322# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001323# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001324function screen_stop_service {
1325 local service=$1
1326
Dean Troyerdff49a22014-01-30 15:37:40 -06001327 SCREEN_NAME=${SCREEN_NAME:-stack}
1328 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001329 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001330
Dean Troyer3159a822014-08-27 14:13:58 -05001331 if is_service_enabled $service; then
1332 # Clean up the screen window
1333 screen -S $SCREEN_NAME -p $service -X kill
1334 fi
1335}
1336
1337# Stop a service process
1338# If a PID is available use it, kill the whole process group via TERM
1339# If screen is being used kill the screen window; this will catch processes
1340# that did not leave a PID behind
1341# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1342# stop_process service
1343function stop_process {
1344 local service=$1
1345
1346 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001347 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001348
1349 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001350 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001351 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1352 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1353 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001354 fi
1355 if [[ "$USE_SCREEN" = "True" ]]; then
1356 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001357 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001358 fi
1359 fi
1360}
1361
1362# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001363# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001364# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001365function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001366 local service
1367 local failures
1368 SCREEN_NAME=${SCREEN_NAME:-stack}
1369 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1370
1371
1372 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1373 echo "No service status directory found"
1374 return
1375 fi
1376
1377 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001378 # make this -o errexit safe
1379 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001380
1381 for service in $failures; do
1382 service=`basename $service`
1383 service=${service%.failure}
1384 echo "Error: Service $service is not running"
1385 done
1386
1387 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001388 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001389 fi
1390}
1391
Chris Dent2f27a0e2014-09-09 13:46:02 +01001392# Tail a log file in a screen if USE_SCREEN is true.
1393function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001394 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001395 local logfile=$2
1396
Sean Dague53753292014-12-04 19:38:15 -05001397 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001398 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001399 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001400 fi
1401}
1402
Dean Troyerdff49a22014-01-30 15:37:40 -06001403
Dean Troyer3159a822014-08-27 14:13:58 -05001404# Deprecated Functions
1405# --------------------
1406
1407# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1408# fork. It includes the dirty work of closing extra filehandles and preparing log
1409# files to produce the same logs as screen_it(). The log filename is derived
1410# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1411# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1412# _old_run_process service "command-line"
1413function _old_run_process {
1414 local service=$1
1415 local command="$2"
1416
1417 # Undo logging redirections and close the extra descriptors
1418 exec 1>&3
1419 exec 2>&3
1420 exec 3>&-
1421 exec 6>&-
1422
1423 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001424 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1425 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001426
1427 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1428 export PYTHONUNBUFFERED=1
1429 fi
1430
1431 exec /bin/bash -c "$command"
1432 die "$service exec failure: $command"
1433}
1434
1435# old_run_process() launches a child process that closes all file descriptors and
1436# then exec's the passed in command. This is meant to duplicate the semantics
1437# of screen_it() without screen. PIDs are written to
1438# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1439# old_run_process service "command-line"
1440function old_run_process {
1441 local service=$1
1442 local command="$2"
1443
1444 # Spawn the child process
1445 _old_run_process "$service" "$command" &
1446 echo $!
1447}
1448
1449# Compatibility for existing start_XXXX() functions
1450# Uses global ``USE_SCREEN``
1451# screen_it service "command-line"
1452function screen_it {
1453 if is_service_enabled $1; then
1454 # Append the service to the screen rc file
1455 screen_rc "$1" "$2"
1456
1457 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001458 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001459 else
1460 # Spawn directly without screen
1461 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1462 fi
1463 fi
1464}
1465
1466# Compatibility for existing stop_XXXX() functions
1467# Stop a service in screen
1468# If a PID is available use it, kill the whole process group via TERM
1469# If screen is being used kill the screen window; this will catch processes
1470# that did not leave a PID behind
1471# screen_stop service
1472function screen_stop {
1473 # Clean up the screen window
1474 stop_process $1
1475}
1476
1477
Sean Dague2c65e712014-12-18 09:44:56 -05001478# Plugin Functions
1479# =================
1480
1481DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1482
1483# enable_plugin <name> <url> [branch]
1484#
1485# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1486# ``url`` is a git url
1487# ``branch`` is a gitref. If it's not set, defaults to master
1488function enable_plugin {
1489 local name=$1
1490 local url=$2
1491 local branch=${3:-master}
1492 DEVSTACK_PLUGINS+=",$name"
1493 GITREPO[$name]=$url
1494 GITDIR[$name]=$DEST/$name
1495 GITBRANCH[$name]=$branch
1496}
1497
1498# fetch_plugins
1499#
1500# clones all plugins
1501function fetch_plugins {
1502 local plugins="${DEVSTACK_PLUGINS}"
1503 local plugin
1504
1505 # short circuit if nothing to do
1506 if [[ -z $plugins ]]; then
1507 return
1508 fi
1509
Dean Troyerdc97cb72015-03-28 08:20:50 -05001510 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001511 for plugin in ${plugins//,/ }; do
1512 git_clone_by_name $plugin
1513 done
1514}
1515
1516# load_plugin_settings
1517#
1518# Load settings from plugins in the order that they were registered
1519function load_plugin_settings {
1520 local plugins="${DEVSTACK_PLUGINS}"
1521 local plugin
1522
1523 # short circuit if nothing to do
1524 if [[ -z $plugins ]]; then
1525 return
1526 fi
1527
1528 echo "Loading plugin settings"
1529 for plugin in ${plugins//,/ }; do
1530 local dir=${GITDIR[$plugin]}
1531 # source any known settings
1532 if [[ -f $dir/devstack/settings ]]; then
1533 source $dir/devstack/settings
1534 fi
1535 done
1536}
1537
Sean Dague6e275e12015-03-26 05:54:28 -04001538# plugin_override_defaults
1539#
1540# Run an extremely early setting phase for plugins that allows default
1541# overriding of services.
1542function plugin_override_defaults {
1543 local plugins="${DEVSTACK_PLUGINS}"
1544 local plugin
1545
1546 # short circuit if nothing to do
1547 if [[ -z $plugins ]]; then
1548 return
1549 fi
1550
1551 echo "Overriding Configuration Defaults"
1552 for plugin in ${plugins//,/ }; do
1553 local dir=${GITDIR[$plugin]}
1554 # source any overrides
1555 if [[ -f $dir/devstack/override-defaults ]]; then
1556 # be really verbose that an override is happening, as it
1557 # may not be obvious if things fail later.
1558 echo "$plugin has overriden the following defaults"
1559 cat $dir/devstack/override-defaults
1560 source $dir/devstack/override-defaults
1561 fi
1562 done
1563}
1564
Sean Dague2c65e712014-12-18 09:44:56 -05001565# run_plugins
1566#
1567# Run the devstack/plugin.sh in all the plugin directories. These are
1568# run in registration order.
1569function run_plugins {
1570 local mode=$1
1571 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301572
1573 local plugins="${DEVSTACK_PLUGINS}"
1574 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001575 for plugin in ${plugins//,/ }; do
1576 local dir=${GITDIR[$plugin]}
1577 if [[ -f $dir/devstack/plugin.sh ]]; then
1578 source $dir/devstack/plugin.sh $mode $phase
1579 fi
1580 done
1581}
1582
1583function run_phase {
1584 local mode=$1
1585 local phase=$2
1586 if [[ -d $TOP_DIR/extras.d ]]; then
1587 for i in $TOP_DIR/extras.d/*.sh; do
1588 [[ -r $i ]] && source $i $mode $phase
1589 done
1590 fi
1591 # the source phase corresponds to settings loading in plugins
1592 if [[ "$mode" == "source" ]]; then
1593 load_plugin_settings
Sean Dague6e275e12015-03-26 05:54:28 -04001594 elif [[ "$mode" == "override_defaults" ]]; then
1595 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001596 else
1597 run_plugins $mode $phase
1598 fi
1599}
1600
Dean Troyerdff49a22014-01-30 15:37:40 -06001601
1602# Service Functions
1603# =================
1604
1605# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1606# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001607function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001608 echo "$1" | sed -e '
1609 s/,,/,/g;
1610 s/^,//;
1611 s/,$//
1612 '
1613}
1614
1615# disable_all_services() removes all current services
1616# from ``ENABLED_SERVICES`` to reset the configuration
1617# before a minimal installation
1618# Uses global ``ENABLED_SERVICES``
1619# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001620function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001621 ENABLED_SERVICES=""
1622}
1623
1624# Remove all services starting with '-'. For example, to install all default
1625# services except rabbit (rabbit) set in ``localrc``:
1626# ENABLED_SERVICES+=",-rabbit"
1627# Uses global ``ENABLED_SERVICES``
1628# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001629function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001630 local to_remove=""
1631 local remaining=""
1632 local enabled=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001633 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001634
1635 # build up list of services that should be removed; i.e. they
1636 # begin with "-"
1637 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001638 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001639 to_remove+=",${service#-}"
1640 else
1641 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001642 fi
1643 done
Ian Wienand2796a822015-04-15 08:59:04 +10001644
1645 # go through the service list. if this service appears in the "to
1646 # be removed" list, drop it
1647 for service in ${remaining//,/ }; do
1648 local remove
1649 local add=1
1650 for remove in ${to_remove//,/ }; do
1651 if [[ ${remove} == ${service} ]]; then
1652 add=0
1653 break
1654 fi
1655 done
1656 if [[ $add == 1 ]]; then
1657 enabled="${enabled},$service"
1658 fi
1659 done
1660
1661 ENABLED_SERVICES=$(_cleanup_service_list "$enabled")
Dean Troyerdff49a22014-01-30 15:37:40 -06001662}
1663
1664# disable_service() removes the services passed as argument to the
1665# ``ENABLED_SERVICES`` list, if they are present.
1666#
1667# For example:
1668# disable_service rabbit
1669#
1670# This function does not know about the special cases
1671# for nova, glance, and neutron built into is_service_enabled().
1672# Uses global ``ENABLED_SERVICES``
1673# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001674function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001675 local tmpsvcs=",${ENABLED_SERVICES},"
1676 local service
1677 for service in $@; do
1678 if is_service_enabled $service; then
1679 tmpsvcs=${tmpsvcs//,$service,/,}
1680 fi
1681 done
1682 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1683}
1684
1685# enable_service() adds the services passed as argument to the
1686# ``ENABLED_SERVICES`` list, if they are not already present.
1687#
1688# For example:
1689# enable_service qpid
1690#
1691# This function does not know about the special cases
1692# for nova, glance, and neutron built into is_service_enabled().
1693# Uses global ``ENABLED_SERVICES``
1694# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001695function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001696 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001697 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001698 for service in $@; do
1699 if ! is_service_enabled $service; then
1700 tmpsvcs+=",$service"
1701 fi
1702 done
1703 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1704 disable_negated_services
1705}
1706
1707# is_service_enabled() checks if the service(s) specified as arguments are
1708# enabled by the user in ``ENABLED_SERVICES``.
1709#
1710# Multiple services specified as arguments are ``OR``'ed together; the test
1711# is a short-circuit boolean, i.e it returns on the first match.
1712#
1713# There are special cases for some 'catch-all' services::
1714# **nova** returns true if any service enabled start with **n-**
1715# **cinder** returns true if any service enabled start with **c-**
1716# **ceilometer** returns true if any service enabled start with **ceilometer**
1717# **glance** returns true if any service enabled start with **g-**
1718# **neutron** returns true if any service enabled start with **q-**
1719# **swift** returns true if any service enabled start with **s-**
1720# **trove** returns true if any service enabled start with **tr-**
1721# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1722# **s-** services will be enabled. This will be deprecated in the future.
1723#
1724# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1725# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1726# as enabled in this case.
1727#
1728# Uses global ``ENABLED_SERVICES``
1729# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001730function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001731 local xtrace=$(set +o | grep xtrace)
1732 set +o xtrace
1733 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001734 local services=$@
1735 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001736 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001737 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001738
1739 # Look for top-level 'enabled' function for this service
1740 if type is_${service}_enabled >/dev/null 2>&1; then
1741 # A function exists for this service, use it
1742 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001743 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001744 fi
1745
1746 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1747 # are implemented
1748
Sean Dague45917cc2014-02-24 16:09:14 -05001749 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001750 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001751 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1752 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1753 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1754 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1755 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1756 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1757 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1758 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1759 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001760 done
Sean Dague45917cc2014-02-24 16:09:14 -05001761 $xtrace
1762 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001763}
1764
1765# Toggle enable/disable_service for services that must run exclusive of each other
1766# $1 The name of a variable containing a space-separated list of services
1767# $2 The name of a variable in which to store the enabled service's name
1768# $3 The name of the service to enable
1769function use_exclusive_service {
1770 local options=${!1}
1771 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001772 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001773 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001774 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001775 for opt in $options;do
1776 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1777 done
1778 eval "$out=$selection"
1779 return 0
1780}
1781
1782
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001783# System Functions
1784# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001785
1786# Only run the command if the target file (the last arg) is not on an
1787# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001788function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001789 local xtrace=$(set +o | grep xtrace)
1790 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001791 local args=( $@ )
1792 local last
1793 local sudo_cmd
1794 local dir_to_check
1795
1796 let last="${#args[*]} - 1"
1797
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001798 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001799 if [ ! -d "$dir_to_check" ]; then
1800 dir_to_check=`dirname "$dir_to_check"`
1801 fi
1802
1803 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001804 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001805 return 0
1806 fi
1807
1808 if [[ $TRACK_DEPENDS = True ]]; then
1809 sudo_cmd="env"
1810 else
1811 sudo_cmd="sudo"
1812 fi
1813
Sean Dague45917cc2014-02-24 16:09:14 -05001814 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001815 $sudo_cmd $@
1816}
1817
1818# Exit 0 if address is in network or 1 if address is not in network
1819# ip-range is in CIDR notation: 1.2.3.4/20
1820# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001821function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001822 local ip=$1
1823 local range=$2
1824 local masklen=${range#*/}
1825 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1826 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1827 [[ $network == $subnet ]]
1828}
1829
1830# Add a user to a group.
1831# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001832function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001833 local user=$1
1834 local group=$2
1835
1836 if [[ -z "$os_VENDOR" ]]; then
1837 GetOSVersion
1838 fi
1839
1840 # SLE11 and openSUSE 12.2 don't have the usual usermod
1841 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1842 sudo usermod -a -G "$group" "$user"
1843 else
1844 sudo usermod -A "$group" "$user"
1845 fi
1846}
1847
1848# Convert CIDR notation to a IPv4 netmask
1849# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001850function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001851 local maskpat="255 255 255 255"
1852 local maskdgt="254 252 248 240 224 192 128"
1853 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1854 echo ${1-0}.${2-0}.${3-0}.${4-0}
1855}
1856
1857# Gracefully cp only if source file/dir exists
1858# cp_it source destination
1859function cp_it {
1860 if [ -e $1 ] || [ -d $1 ]; then
1861 cp -pRL $1 $2
1862 fi
1863}
1864
1865# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1866# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1867# ``localrc`` or on the command line if necessary::
1868#
1869# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1870#
1871# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1872
Ian Wienandaee18c72014-02-21 15:35:08 +11001873function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001874 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001875 export http_proxy=$http_proxy
1876 fi
Sean Dague53753292014-12-04 19:38:15 -05001877 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001878 export https_proxy=$https_proxy
1879 fi
Sean Dague53753292014-12-04 19:38:15 -05001880 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001881 export no_proxy=$no_proxy
1882 fi
1883}
1884
1885# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001886function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001887 local mount_type=`stat -f -L -c %T $1`
1888 test "$mount_type" == "nfs"
1889}
1890
1891# Return the network portion of the given IP address using netmask
1892# netmask is in the traditional dotted-quad format
1893# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001894function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001895 local ip=$1
1896 local mask=$2
1897 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1898 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1899 echo $subnet
1900}
1901
1902# Service wrapper to restart services
1903# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001904function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001905 if is_ubuntu; then
1906 sudo /usr/sbin/service $1 restart
1907 else
1908 sudo /sbin/service $1 restart
1909 fi
1910}
1911
1912# Only change permissions of a file or directory if it is not on an
1913# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001914function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001915 _safe_permission_operation chmod $@
1916}
1917
1918# Only change ownership of a file or directory if it is not on an NFS
1919# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001920function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001921 _safe_permission_operation chown $@
1922}
1923
1924# Service wrapper to start services
1925# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001926function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001927 if is_ubuntu; then
1928 sudo /usr/sbin/service $1 start
1929 else
1930 sudo /sbin/service $1 start
1931 fi
1932}
1933
1934# Service wrapper to stop services
1935# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001936function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001937 if is_ubuntu; then
1938 sudo /usr/sbin/service $1 stop
1939 else
1940 sudo /sbin/service $1 stop
1941 fi
1942}
1943
1944
1945# Restore xtrace
1946$XTRACE
1947
1948# Local variables:
1949# mode: shell-script
1950# End: