blob: 48e400dfb1ecc63b36b3c124591161c2931cf318 [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
65
66# Control Functions
67# =================
68
69# Prints backtrace info
70# filename:lineno:function
71# backtrace level
72function backtrace {
73 local level=$1
74 local deep=$((${#BASH_SOURCE[@]} - 1))
75 echo "[Call Trace]"
76 while [ $level -le $deep ]; do
77 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
78 deep=$((deep - 1))
79 done
80}
81
82# Prints line number and "message" then exits
83# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +110084function die {
Dean Troyerdff49a22014-01-30 15:37:40 -060085 local exitcode=$?
86 set +o xtrace
87 local line=$1; shift
88 if [ $exitcode == 0 ]; then
89 exitcode=1
90 fi
91 backtrace 2
92 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -060093 # Give buffers a second to flush
94 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -060095 exit $exitcode
96}
97
98# Checks an environment variable is not set or has length 0 OR if the
99# exit code is non-zero and prints "message" and exits
100# NOTE: env-var is the variable name without a '$'
101# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100102function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600103 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500104 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600105 set +o xtrace
106 local line=$1; shift
107 local evar=$1; shift
108 if ! is_set $evar || [ $exitcode != 0 ]; then
109 die $line "$*"
110 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500111 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600112}
113
114# Prints line number and "message" in error format
115# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100116function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600117 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500118 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600119 set +o xtrace
120 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
121 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600122 if [[ -n ${LOGDIR} ]]; then
123 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600124 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500125 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600126 return $exitcode
127}
128
129# Checks an environment variable is not set or has length 0 OR if the
130# exit code is non-zero and prints "message"
131# NOTE: env-var is the variable name without a '$'
132# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100133function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600134 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500135 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600136 set +o xtrace
137 local line=$1; shift
138 local evar=$1; shift
139 if ! is_set $evar || [ $exitcode != 0 ]; then
140 err $line "$*"
141 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500142 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600143 return $exitcode
144}
145
146# Exit after outputting a message about the distribution not being supported.
147# exit_distro_not_supported [optional-string-telling-what-is-missing]
148function exit_distro_not_supported {
149 if [[ -z "$DISTRO" ]]; then
150 GetDistro
151 fi
152
153 if [ $# -gt 0 ]; then
154 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
155 else
156 die $LINENO "Support for $DISTRO is incomplete."
157 fi
158}
159
160# Test if the named environment variable is set and not zero length
161# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100162function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600163 local var=\$"$1"
164 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
165}
166
167# Prints line number and "message" in warning format
168# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100169function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600170 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500171 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600172 set +o xtrace
173 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
174 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600175 if [[ -n ${LOGDIR} ]]; then
176 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600177 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500178 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600179 return $exitcode
180}
181
182
183# Distro Functions
184# ================
185
186# Determine OS Vendor, Release and Update
187# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
188# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500189# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
190# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
191# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
192# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
193# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500194os_VENDOR=""
195os_RELEASE=""
196os_UPDATE=""
197os_PACKAGE=""
198os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500199
Dean Troyerdff49a22014-01-30 15:37:40 -0600200# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100201function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500202
Dean Troyerdff49a22014-01-30 15:37:40 -0600203 # Figure out which vendor we are
204 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
205 # OS/X
206 os_VENDOR=`sw_vers -productName`
207 os_RELEASE=`sw_vers -productVersion`
208 os_UPDATE=${os_RELEASE##*.}
209 os_RELEASE=${os_RELEASE%.*}
210 os_PACKAGE=""
211 if [[ "$os_RELEASE" =~ "10.7" ]]; then
212 os_CODENAME="lion"
213 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
214 os_CODENAME="snow leopard"
215 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
216 os_CODENAME="leopard"
217 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
218 os_CODENAME="tiger"
219 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
220 os_CODENAME="panther"
221 else
222 os_CODENAME=""
223 fi
224 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
225 os_VENDOR=$(lsb_release -i -s)
226 os_RELEASE=$(lsb_release -r -s)
227 os_UPDATE=""
228 os_PACKAGE="rpm"
229 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
230 os_PACKAGE="deb"
231 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
232 lsb_release -d -s | grep -q openSUSE
233 if [[ $? -eq 0 ]]; then
234 os_VENDOR="openSUSE"
235 fi
236 elif [[ $os_VENDOR == "openSUSE project" ]]; then
237 os_VENDOR="openSUSE"
238 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
239 os_VENDOR="Red Hat"
240 fi
241 os_CODENAME=$(lsb_release -c -s)
242 elif [[ -r /etc/redhat-release ]]; then
243 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
244 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
245 # CentOS release 5.5 (Final)
246 # CentOS Linux release 6.0 (Final)
247 # Fedora release 16 (Verne)
248 # XenServer release 6.2.0-70446c (xenenterprise)
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700249 # Oracle Linux release 7
Dean Troyerdff49a22014-01-30 15:37:40 -0600250 os_CODENAME=""
251 for r in "Red Hat" CentOS Fedora XenServer; do
252 os_VENDOR=$r
253 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
254 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
255 os_CODENAME=${ver#*|}
256 os_RELEASE=${ver%|*}
257 os_UPDATE=${os_RELEASE##*.}
258 os_RELEASE=${os_RELEASE%.*}
259 break
260 fi
261 os_VENDOR=""
262 done
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700263 if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
264 os_VENDOR=OracleLinux
265 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600266 os_PACKAGE="rpm"
267 elif [[ -r /etc/SuSE-release ]]; then
268 for r in openSUSE "SUSE Linux"; do
269 if [[ "$r" = "SUSE Linux" ]]; then
270 os_VENDOR="SUSE LINUX"
271 else
272 os_VENDOR=$r
273 fi
274
275 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
276 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
277 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
278 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
279 break
280 fi
281 os_VENDOR=""
282 done
283 os_PACKAGE="rpm"
284 # If lsb_release is not installed, we should be able to detect Debian OS
285 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
286 os_VENDOR="Debian"
287 os_PACKAGE="deb"
288 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
289 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
290 fi
291 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
292}
293
294# Translate the OS version values into common nomenclature
295# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500296declare DISTRO
297
Ian Wienandaee18c72014-02-21 15:35:08 +1100298function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600299 GetOSVersion
300 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
301 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
302 DISTRO=$os_CODENAME
303 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
304 # For Fedora, just use 'f' and the release
305 DISTRO="f$os_RELEASE"
306 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
307 DISTRO="opensuse-$os_RELEASE"
308 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
309 # For SLE, also use the service pack
310 if [[ -z "$os_UPDATE" ]]; then
311 DISTRO="sle${os_RELEASE}"
312 else
313 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
314 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530315 elif [[ "$os_VENDOR" =~ (Red Hat) || \
316 "$os_VENDOR" =~ (CentOS) || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700317 "$os_VENDOR" =~ (OracleLinux) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600318 # Drop the . release as we assume it's compatible
319 DISTRO="rhel${os_RELEASE::1}"
320 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
321 DISTRO="xs$os_RELEASE"
322 else
323 # Catch-all for now is Vendor + Release + Update
324 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
325 fi
326 export DISTRO
327}
328
329# Utility function for checking machine architecture
330# is_arch arch-type
331function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500332 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600333}
334
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700335# Determine if current distribution is an Oracle distribution
336# is_oraclelinux
337function is_oraclelinux {
338 if [[ -z "$os_VENDOR" ]]; then
339 GetOSVersion
340 fi
341
342 [ "$os_VENDOR" = "OracleLinux" ]
343}
344
345
Dean Troyerdff49a22014-01-30 15:37:40 -0600346# Determine if current distribution is a Fedora-based distribution
347# (Fedora, RHEL, CentOS, etc).
348# is_fedora
349function is_fedora {
350 if [[ -z "$os_VENDOR" ]]; then
351 GetOSVersion
352 fi
353
anju Tiwari6c639c92014-07-15 18:11:54 +0530354 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700355 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600356}
357
358
359# Determine if current distribution is a SUSE-based distribution
360# (openSUSE, SLE).
361# is_suse
362function is_suse {
363 if [[ -z "$os_VENDOR" ]]; then
364 GetOSVersion
365 fi
366
367 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
368}
369
370
371# Determine if current distribution is an Ubuntu-based distribution
372# It will also detect non-Ubuntu but Debian-based distros
373# is_ubuntu
374function is_ubuntu {
375 if [[ -z "$os_PACKAGE" ]]; then
376 GetOSVersion
377 fi
378 [ "$os_PACKAGE" = "deb" ]
379}
380
381
382# Git Functions
383# =============
384
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600385# Returns openstack release name for a given branch name
386# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100387function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600388 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700389 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600390 echo ${branch#*/}
391 else
392 echo "master"
393 fi
394}
395
Dean Troyerdff49a22014-01-30 15:37:40 -0600396# git clone only if directory doesn't exist already. Since ``DEST`` might not
397# be owned by the installation user, we create the directory and change the
398# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500399# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
400# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600401# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500402# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600403# git_clone remote dest-dir branch
404function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500405 local git_remote=$1
406 local git_dest=$2
407 local git_ref=$3
408 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200409 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500410
Sean Dague53753292014-12-04 19:38:15 -0500411 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800412 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200413 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
414 fi
415
Dean Troyerdff49a22014-01-30 15:37:40 -0600416 if [[ "$OFFLINE" = "True" ]]; then
417 echo "Running in offline mode, clones already exist"
418 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500419 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600420 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400421 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600422 return
423 fi
424
Dean Troyer50cda692014-07-25 11:57:20 -0500425 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600426 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500427 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600428 [[ "$ERROR_ON_CLONE" = "True" ]] && \
429 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200430 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600431 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500432 cd $git_dest
433 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600434 else
435 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500436 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600437 [[ "$ERROR_ON_CLONE" = "True" ]] && \
438 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200439 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500440 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600441 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500442 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600443 elif [[ "$RECLONE" = "True" ]]; then
444 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500445 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600446 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500447 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100448 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600449 # remove the existing ignored files (like pyc) as they cause breakage
450 # (due to the py files having older timestamps than our pyc, so python
451 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500452 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600453
Dean Troyer50cda692014-07-25 11:57:20 -0500454 # handle git_ref accordingly to type (tag, branch)
455 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
456 git_update_tag $git_ref
457 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
458 git_update_branch $git_ref
459 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
460 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600461 else
Dean Troyer50cda692014-07-25 11:57:20 -0500462 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600463 fi
464
465 fi
466 fi
467
468 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500469 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600470 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400471 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600472}
473
Sean Daguecc524062014-10-01 09:06:43 -0400474# A variation on git clone that lets us specify a project by it's
475# actual name, like oslo.config. This is exceptionally useful in the
476# library installation case
477function git_clone_by_name {
478 local name=$1
479 local repo=${GITREPO[$name]}
480 local dir=${GITDIR[$name]}
481 local branch=${GITBRANCH[$name]}
482 git_clone $repo $dir $branch
483}
484
485
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100486# git can sometimes get itself infinitely stuck with transient network
487# errors or other issues with the remote end. This wraps git in a
488# timeout/retry loop and is intended to watch over non-local git
489# processes that might hang. GIT_TIMEOUT, if set, is passed directly
490# to timeout(1); otherwise the default value of 0 maintains the status
491# quo of waiting forever.
492# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100493function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100494 local count=0
495 local timeout=0
496
497 if [[ -n "${GIT_TIMEOUT}" ]]; then
498 timeout=${GIT_TIMEOUT}
499 fi
500
501 until timeout -s SIGINT ${timeout} git "$@"; do
502 # 124 is timeout(1)'s special return code when it reached the
503 # timeout; otherwise assume fatal failure
504 if [[ $? -ne 124 ]]; then
505 die $LINENO "git call failed: [git $@]"
506 fi
507
508 count=$(($count + 1))
509 warn "timeout ${count} for git call: [git $@]"
510 if [ $count -eq 3 ]; then
511 die $LINENO "Maximum of 3 git retries reached"
512 fi
513 sleep 5
514 done
515}
516
Dean Troyerdff49a22014-01-30 15:37:40 -0600517# git update using reference as a branch.
518# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100519function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500520 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600521
Dean Troyer50cda692014-07-25 11:57:20 -0500522 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600523 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500524 git branch -D $git_branch || true
525 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600526}
527
528# git update using reference as a branch.
529# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100530function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500531 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600532
Dean Troyer50cda692014-07-25 11:57:20 -0500533 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600534}
535
536# git update using reference as a tag. Be careful editing source at that repo
537# as working copy will be in a detached mode
538# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100539function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500540 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600541
Dean Troyer50cda692014-07-25 11:57:20 -0500542 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600543 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500544 git_timed fetch origin tag $git_tag
545 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600546}
547
548
549# OpenStack Functions
550# ===================
551
552# Get the default value for HOST_IP
553# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100554function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600555 local fixed_range=$1
556 local floating_range=$2
557 local host_ip_iface=$3
558 local host_ip=$4
559
Dean Troyerdff49a22014-01-30 15:37:40 -0600560 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
561 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
562 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100563 # Find the interface used for the default route
564 host_ip_iface=${host_ip_iface:-$(ip route | awk '/default/ {print $5}' | head -1)}
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500565 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
566 local ip
567 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600568 # Attempt to filter out IP addresses that are part of the fixed and
569 # floating range. Note that this method only works if the ``netaddr``
570 # python library is installed. If it is not installed, an error
571 # will be printed and the first IP from the interface will be used.
572 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
573 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500574 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
575 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600576 break;
577 fi
578 done
579 fi
580 echo $host_ip
581}
582
Attila Fazekasf71b5002014-05-28 09:52:22 +0200583# Generates hex string from ``size`` byte of pseudo random data
584# generate_hex_string size
585function generate_hex_string {
586 local size=$1
587 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
588}
589
Dean Troyerdff49a22014-01-30 15:37:40 -0600590# Grab a numbered field from python prettytable output
591# Fields are numbered starting with 1
592# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
593# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100594function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500595 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600596 while read data; do
597 if [ "$1" -lt 0 ]; then
598 field="(\$(NF$1))"
599 else
600 field="\$$(($1 + 1))"
601 fi
602 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
603 done
604}
605
yuntongjinf26deea2015-02-28 10:50:34 +0800606# install default policy
607# copy over a default policy.json and policy.d for projects
608function install_default_policy {
609 local project=$1
610 local project_uc=$(echo $1|tr a-z A-Z)
611 local conf_dir="${project_uc}_CONF_DIR"
612 # eval conf dir to get the variable
613 conf_dir="${!conf_dir}"
614 local project_dir="${project_uc}_DIR"
615 # eval project dir to get the variable
616 project_dir="${!project_dir}"
617 local sample_conf_dir="${project_dir}/etc/${project}"
618 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
619
620 # first copy any policy.json
621 cp -p $sample_conf_dir/policy.json $conf_dir
622 # then optionally copy over policy.d
623 if [[ -d $sample_policy_dir ]]; then
624 cp -r $sample_policy_dir $conf_dir/policy.d
625 fi
626}
627
Dean Troyerdff49a22014-01-30 15:37:40 -0600628# Add a policy to a policy.json file
629# Do nothing if the policy already exists
630# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100631function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600632 local policy_file=$1
633 local policy_name=$2
634 local policy_perm=$3
635
636 if grep -q ${policy_name} ${policy_file}; then
637 echo "Policy ${policy_name} already exists in ${policy_file}"
638 return
639 fi
640
641 # Add a terminating comma to policy lines without one
642 # Remove the closing '}' and all lines following to the end-of-file
643 local tmpfile=$(mktemp)
644 uniq ${policy_file} | sed -e '
645 s/]$/],/
646 /^[}]/,$d
647 ' > ${tmpfile}
648
649 # Append policy and closing brace
650 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
651 echo "}" >>${tmpfile}
652
653 mv ${tmpfile} ${policy_file}
654}
655
Alistair Coles24779f62014-10-15 18:57:59 +0100656# Gets or creates a domain
657# Usage: get_or_create_domain <name> <description>
658function get_or_create_domain {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500659 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100660 # Gets domain id
661 local domain_id=$(
662 # Gets domain id
663 openstack --os-token=$OS_TOKEN --os-url=$os_url \
664 --os-identity-api-version=3 domain show $1 \
665 -f value -c id 2>/dev/null ||
666 # Creates new domain
667 openstack --os-token=$OS_TOKEN --os-url=$os_url \
668 --os-identity-api-version=3 domain create $1 \
669 --description "$2" \
670 -f value -c id
671 )
672 echo $domain_id
673}
674
Steve Martinellib74e01c2014-12-18 01:35:35 -0500675# Gets or creates group
676# Usage: get_or_create_group <groupname> [<domain> <description>]
677function get_or_create_group {
678 local domain=${2:+--domain ${2}}
679 local desc="${3:-}"
680 local os_url="$KEYSTONE_SERVICE_URI_V3"
681 # Gets group id
682 local group_id=$(
683 # Creates new group with --or-show
684 openstack --os-token=$OS_TOKEN --os-url=$os_url \
685 --os-identity-api-version=3 group create $1 \
686 $domain --description "$desc" --or-show \
687 -f value -c id
688 )
689 echo $group_id
690}
691
Bartosz Górski0abde392014-02-28 14:15:19 +0100692# Gets or creates user
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000693# Usage: get_or_create_user <username> <password> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100694function get_or_create_user {
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000695 if [[ ! -z "$3" ]]; then
696 local email="--email=$3"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200697 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500698 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200699 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100700 local os_cmd="openstack"
701 local domain=""
Jamie Lennox18f39bf2015-01-28 13:38:32 +1000702 if [[ ! -z "$4" ]]; then
703 domain="--domain=$4"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500704 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100705 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100706 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500707 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500708 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100709 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100710 $1 \
711 --password "$2" \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500712 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100713 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500714 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100715 -f value -c id
716 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500717 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100718}
719
720# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100721# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100722function get_or_create_project {
723 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100724 local os_cmd="openstack"
725 local domain=""
726 if [[ ! -z "$2" ]]; then
727 domain="--domain=$2"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500728 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100729 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500730 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500731 # Creates new project with --or-show
732 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100733 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500734 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100735}
736
737# Gets or creates role
738# Usage: get_or_create_role <name>
739function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500740 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500741 # Creates role with --or-show
742 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100743 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500744 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100745}
746
Jamie Lennox9b215db2015-02-10 18:19:57 +1100747# Gets or adds user role to project
748# Usage: get_or_add_user_project_role <role> <user> <project>
749function get_or_add_user_project_role {
Bartosz Górski0abde392014-02-28 14:15:19 +0100750 # Gets user role id
Steve Martinelli5541a612015-01-19 15:58:49 -0500751 local user_role_id=$(openstack role list \
752 --user $2 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100753 --project $3 \
754 --column "ID" \
755 --column "Name" \
756 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500757 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100758 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500759 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100760 $1 \
761 --user $2 \
762 --project $3 \
763 | grep " id " | get_field 2)
764 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500765 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100766}
767
768# Gets or creates service
769# Usage: get_or_create_service <name> <type> <description>
770function get_or_create_service {
771 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500772 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100773 # Gets service id
774 openstack service show $1 -f value -c id 2>/dev/null ||
775 # Creates new service if not exists
776 openstack service create \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500777 $2 \
778 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100779 --description="$3" \
780 -f value -c id
781 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500782 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100783}
784
785# Gets or creates endpoint
786# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
787function get_or_create_endpoint {
788 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500789 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100790 --column "ID" \
791 --column "Region" \
792 --column "Service Name" \
793 | grep " $2 " \
794 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500795 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100796 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500797 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100798 $1 \
799 --region $2 \
800 --publicurl $3 \
801 --adminurl $4 \
802 --internalurl $5 \
803 | grep " id " | get_field 2)
804 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500805 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100806}
Dean Troyerdff49a22014-01-30 15:37:40 -0600807
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500808
Dean Troyerdff49a22014-01-30 15:37:40 -0600809# Package Functions
810# =================
811
812# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100813function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800814 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600815 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800816
817 if [[ -z "$base_dir" ]]; then
818 base_dir=$FILES
819 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600820 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800821 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600822 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800823 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600824 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800825 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600826 else
827 exit_distro_not_supported "list of packages"
828 fi
829 echo "$pkg_dir"
830}
831
832# Wrapper for ``apt-get`` to set cache and proxy environment variables
833# Uses globals ``OFFLINE``, ``*_proxy``
834# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100835function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500836 local xtrace=$(set +o | grep xtrace)
837 set +o xtrace
838
Dean Troyerdff49a22014-01-30 15:37:40 -0600839 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
840 local sudo="sudo"
841 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500842
843 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500844
Dean Troyerdff49a22014-01-30 15:37:40 -0600845 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500846 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
847 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600848 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
849}
850
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800851function _parse_package_files {
852 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600853
Dean Troyerdff49a22014-01-30 15:37:40 -0600854 if [[ -z "$DISTRO" ]]; then
855 GetDistro
856 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600857
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800858 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600859 local OIFS line package distros distro
860 [[ -e $fname ]] || continue
861
862 OIFS=$IFS
863 IFS=$'\n'
864 for line in $(<${fname}); do
865 if [[ $line =~ "NOPRIME" ]]; then
866 continue
867 fi
868
869 # Assume we want this package
870 package=${line%#*}
871 inst_pkg=1
872
873 # Look for # dist:xxx in comment
874 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
875 # We are using BASH regexp matching feature.
876 package=${BASH_REMATCH[1]}
877 distros=${BASH_REMATCH[2]}
878 # In bash ${VAR,,} will lowecase VAR
879 # Look for a match in the distro list
880 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
881 # If no match then skip this package
882 inst_pkg=0
883 fi
884 fi
885
Dean Troyerdff49a22014-01-30 15:37:40 -0600886 if [[ $inst_pkg = 1 ]]; then
887 echo $package
888 fi
889 done
890 IFS=$OIFS
891 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800892}
893
894# get_packages() collects a list of package names of any type from the
895# prerequisite files in ``files/{debs|rpms}``. The list is intended
896# to be passed to a package installer such as apt or yum.
897#
898# Only packages required for the services in 1st argument will be
899# included. Two bits of metadata are recognized in the prerequisite files:
900#
901# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
902# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
903# of the package to the distros listed. The distro names are case insensitive.
904function get_packages {
905 local xtrace=$(set +o | grep xtrace)
906 set +o xtrace
907 local services=$@
908 local package_dir=$(_get_package_dir)
909 local file_to_parse=""
910 local service=""
911
912 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
913
914 if [[ -z "$package_dir" ]]; then
915 echo "No package directory supplied"
916 return 1
917 fi
918 for service in ${services//,/ }; do
919 # Allow individual services to specify dependencies
920 if [[ -e ${package_dir}/${service} ]]; then
921 file_to_parse="${file_to_parse} ${package_dir}/${service}"
922 fi
923 # NOTE(sdague) n-api needs glance for now because that's where
924 # glance client is
925 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700926 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800927 file_to_parse="${file_to_parse} ${package_dir}/nova"
928 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700929 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800930 file_to_parse="${file_to_parse} ${package_dir}/glance"
931 fi
932 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700933 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800934 file_to_parse="${file_to_parse} ${package_dir}/cinder"
935 fi
936 elif [[ $service == ceilometer-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700937 if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800938 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
939 fi
940 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700941 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800942 file_to_parse="${file_to_parse} ${package_dir}/swift"
943 fi
944 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700945 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800946 file_to_parse="${file_to_parse} ${package_dir}/nova"
947 fi
948 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700949 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800950 file_to_parse="${file_to_parse} ${package_dir}/glance"
951 fi
952 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700953 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800954 file_to_parse="${file_to_parse} ${package_dir}/keystone"
955 fi
956 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700957 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800958 file_to_parse="${file_to_parse} ${package_dir}/neutron"
959 fi
960 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -0700961 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800962 file_to_parse="${file_to_parse} ${package_dir}/ironic"
963 fi
964 fi
965 done
966 echo "$(_parse_package_files $file_to_parse)"
967 $xtrace
968}
969
970# get_plugin_packages() collects a list of package names of any type from a
971# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
972# list is intended to be passed to a package installer such as apt or yum.
973#
974# Only packages required for enabled and collected plugins will included.
975#
976# The same metadata used in the main devstack prerequisite files may be used
977# in these prerequisite files, see get_packages() for more info.
978function get_plugin_packages {
979 local xtrace=$(set +o | grep xtrace)
980 set +o xtrace
981 local files_to_parse=""
982 local package_dir=""
983 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
984 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
985 files_to_parse+="$package_dir/$plugin"
986 done
987 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -0500988 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600989}
990
991# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500992# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600993# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -0400994function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -0500995 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
996 REPOS_UPDATED=${REPOS_UPDATED:-False}
997 RETRY_UPDATE=${RETRY_UPDATE:-False}
998
Paul Linchpiner9e179742014-07-13 22:23:00 -0700999 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001000 return 0
1001 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001002
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001003 if is_ubuntu; then
1004 local xtrace=$(set +o | grep xtrace)
1005 set +o xtrace
1006 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1007 # if there are transient errors pulling the updates, that's fine.
1008 # It may be secondary repositories that we don't really care about.
1009 apt_get update || /bin/true
1010 REPOS_UPDATED=True
1011 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001012 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001013 fi
1014}
1015
1016function real_install_package {
1017 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001018 apt_get install "$@"
1019 elif is_fedora; then
1020 yum_install "$@"
1021 elif is_suse; then
1022 zypper_install "$@"
1023 else
1024 exit_distro_not_supported "installing packages"
1025 fi
1026}
1027
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001028# Distro-agnostic package installer
1029# install_package package [package ...]
1030function install_package {
1031 update_package_repo
1032 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1033}
1034
Dean Troyerdff49a22014-01-30 15:37:40 -06001035# Distro-agnostic function to tell if a package is installed
1036# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001037function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001038 if [[ -z "$@" ]]; then
1039 return 1
1040 fi
1041
1042 if [[ -z "$os_PACKAGE" ]]; then
1043 GetOSVersion
1044 fi
1045
1046 if [[ "$os_PACKAGE" = "deb" ]]; then
1047 dpkg -s "$@" > /dev/null 2> /dev/null
1048 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1049 rpm --quiet -q "$@"
1050 else
1051 exit_distro_not_supported "finding if a package is installed"
1052 fi
1053}
1054
1055# Distro-agnostic package uninstaller
1056# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001057function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001058 if is_ubuntu; then
1059 apt_get purge "$@"
1060 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001061 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001062 elif is_suse; then
1063 sudo zypper rm "$@"
1064 else
1065 exit_distro_not_supported "uninstalling packages"
1066 fi
1067}
1068
1069# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001070# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001071# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001072function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001073 [[ "$OFFLINE" = "True" ]] && return
1074 local sudo="sudo"
1075 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001076
1077 # The manual check for missing packages is because yum -y assumes
1078 # missing packages are OK. See
1079 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001080 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1081 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001082 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001083 awk '
1084 BEGIN { fail=0 }
1085 /No package/ { fail=1 }
1086 { print }
1087 END { exit fail }' || \
1088 die $LINENO "Missing packages detected"
1089
1090 # also ensure we catch a yum failure
1091 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001092 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001093 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001094}
1095
1096# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001097# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001098# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001099function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001100 [[ "$OFFLINE" = "True" ]] && return
1101 local sudo="sudo"
1102 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001103 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1104 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001105 zypper --non-interactive install --auto-agree-with-licenses "$@"
1106}
1107
1108
1109# Process Functions
1110# =================
1111
1112# _run_process() is designed to be backgrounded by run_process() to simulate a
1113# fork. It includes the dirty work of closing extra filehandles and preparing log
1114# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001115# from the service name.
1116# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001117# If an optional group is provided sg will be used to set the group of
1118# the command.
1119# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001120function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001121 local service=$1
1122 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001123 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001124
1125 # Undo logging redirections and close the extra descriptors
1126 exec 1>&3
1127 exec 2>&3
1128 exec 3>&-
1129 exec 6>&-
1130
Dean Troyerdde41d02014-12-09 17:47:57 -06001131 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1132 if [[ -n ${LOGDIR} ]]; then
1133 exec 1>&"$real_logfile" 2>&1
1134 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1135 if [[ -n ${SCREEN_LOGDIR} ]]; then
1136 # Drop the backward-compat symlink
1137 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1138 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001139
1140 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1141 export PYTHONUNBUFFERED=1
1142 fi
1143
Dean Troyer3159a822014-08-27 14:13:58 -05001144 # Run under ``setsid`` to force the process to become a session and group leader.
1145 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001146 if [[ -n "$group" ]]; then
1147 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1148 else
1149 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1150 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001151
1152 # Just silently exit this process
1153 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001154}
1155
1156# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1157# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001158# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001159# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001160function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001161 SCREEN_NAME=${SCREEN_NAME:-stack}
1162 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1163
1164 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1165 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1166 fi
1167
1168 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1169}
1170
1171# Find out if a process exists by partial name.
1172# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001173function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001174 local name=$1
1175 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001176 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001177 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001178 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001179}
1180
Dean Troyer3159a822014-08-27 14:13:58 -05001181# Run a single service under screen or directly
1182# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001183# If an optional group is provided sg will be used to run the
1184# command as that group.
1185# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001186function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001187 local service=$1
1188 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001189 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001190
Dean Troyer3159a822014-08-27 14:13:58 -05001191 if is_service_enabled $service; then
1192 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001193 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001194 else
1195 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001196 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001197 fi
1198 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001199}
1200
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001201# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001202# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001203# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001204# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001205# Run a command in a shell in a screen window, if an optional group
1206# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001207function screen_process {
1208 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001209 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001210 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001211
Sean Dagueea22a4f2014-06-27 15:21:41 -04001212 SCREEN_NAME=${SCREEN_NAME:-stack}
1213 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001214 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001215
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001216 # Append the process to the screen rc file
1217 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001218
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001219 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001220
Dean Troyerdde41d02014-12-09 17:47:57 -06001221 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1222 echo "LOGDIR: $LOGDIR"
1223 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1224 echo "log: $real_logfile"
1225 if [[ -n ${LOGDIR} ]]; then
1226 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001227 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001228 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1229 if [[ -n ${SCREEN_LOGDIR} ]]; then
1230 # Drop the backward-compat symlink
1231 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1232 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001233 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001234
1235 # sleep to allow bash to be ready to be send the command - we are
1236 # creating a new window in screen and then sends characters, so if
1237 # bash isn't running by the time we send the command, nothing happens
1238 sleep 3
1239
1240 NL=`echo -ne '\015'`
1241 # This fun command does the following:
1242 # - the passed server command is backgrounded
1243 # - the pid of the background process is saved in the usual place
1244 # - the server process is brought back to the foreground
1245 # - if the server process exits prematurely the fg command errors
1246 # and a message is written to stdout and the process failure file
1247 #
1248 # The pid saved can be used in stop_process() as a process group
1249 # id to kill off all child processes
1250 if [[ -n "$group" ]]; then
1251 command="sg $group '$command'"
1252 fi
1253 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 -06001254}
1255
1256# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001257# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001258# screen_rc service "command-line"
1259function screen_rc {
1260 SCREEN_NAME=${SCREEN_NAME:-stack}
1261 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1262 if [[ ! -e $SCREENRC ]]; then
1263 # Name the screen session
1264 echo "sessionname $SCREEN_NAME" > $SCREENRC
1265 # Set a reasonable statusbar
1266 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1267 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1268 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1269 echo "screen -t shell bash" >> $SCREENRC
1270 fi
1271 # If this service doesn't already exist in the screenrc file
1272 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1273 NL=`echo -ne '\015'`
1274 echo "screen -t $1 bash" >> $SCREENRC
1275 echo "stuff \"$2$NL\"" >> $SCREENRC
1276
Dean Troyerdde41d02014-12-09 17:47:57 -06001277 if [[ -n ${LOGDIR} ]]; then
1278 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001279 echo "log on" >>$SCREENRC
1280 fi
1281 fi
1282}
1283
1284# Stop a service in screen
1285# If a PID is available use it, kill the whole process group via TERM
1286# If screen is being used kill the screen window; this will catch processes
1287# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001288# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001289# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001290function screen_stop_service {
1291 local service=$1
1292
Dean Troyerdff49a22014-01-30 15:37:40 -06001293 SCREEN_NAME=${SCREEN_NAME:-stack}
1294 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001295 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001296
Dean Troyer3159a822014-08-27 14:13:58 -05001297 if is_service_enabled $service; then
1298 # Clean up the screen window
1299 screen -S $SCREEN_NAME -p $service -X kill
1300 fi
1301}
1302
1303# Stop a service process
1304# If a PID is available use it, kill the whole process group via TERM
1305# If screen is being used kill the screen window; this will catch processes
1306# that did not leave a PID behind
1307# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1308# stop_process service
1309function stop_process {
1310 local service=$1
1311
1312 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001313 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001314
1315 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001316 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001317 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1318 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1319 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001320 fi
1321 if [[ "$USE_SCREEN" = "True" ]]; then
1322 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001323 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001324 fi
1325 fi
1326}
1327
1328# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001329# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001330# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001331function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001332 local service
1333 local failures
1334 SCREEN_NAME=${SCREEN_NAME:-stack}
1335 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1336
1337
1338 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1339 echo "No service status directory found"
1340 return
1341 fi
1342
1343 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001344 # make this -o errexit safe
1345 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001346
1347 for service in $failures; do
1348 service=`basename $service`
1349 service=${service%.failure}
1350 echo "Error: Service $service is not running"
1351 done
1352
1353 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001354 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001355 fi
1356}
1357
Chris Dent2f27a0e2014-09-09 13:46:02 +01001358# Tail a log file in a screen if USE_SCREEN is true.
1359function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001360 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001361 local logfile=$2
1362
Sean Dague53753292014-12-04 19:38:15 -05001363 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001364 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001365 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001366 fi
1367}
1368
Dean Troyerdff49a22014-01-30 15:37:40 -06001369
Dean Troyer3159a822014-08-27 14:13:58 -05001370# Deprecated Functions
1371# --------------------
1372
1373# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1374# fork. It includes the dirty work of closing extra filehandles and preparing log
1375# files to produce the same logs as screen_it(). The log filename is derived
1376# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1377# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1378# _old_run_process service "command-line"
1379function _old_run_process {
1380 local service=$1
1381 local command="$2"
1382
1383 # Undo logging redirections and close the extra descriptors
1384 exec 1>&3
1385 exec 2>&3
1386 exec 3>&-
1387 exec 6>&-
1388
1389 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001390 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1391 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001392
1393 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1394 export PYTHONUNBUFFERED=1
1395 fi
1396
1397 exec /bin/bash -c "$command"
1398 die "$service exec failure: $command"
1399}
1400
1401# old_run_process() launches a child process that closes all file descriptors and
1402# then exec's the passed in command. This is meant to duplicate the semantics
1403# of screen_it() without screen. PIDs are written to
1404# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1405# old_run_process service "command-line"
1406function old_run_process {
1407 local service=$1
1408 local command="$2"
1409
1410 # Spawn the child process
1411 _old_run_process "$service" "$command" &
1412 echo $!
1413}
1414
1415# Compatibility for existing start_XXXX() functions
1416# Uses global ``USE_SCREEN``
1417# screen_it service "command-line"
1418function screen_it {
1419 if is_service_enabled $1; then
1420 # Append the service to the screen rc file
1421 screen_rc "$1" "$2"
1422
1423 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001424 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001425 else
1426 # Spawn directly without screen
1427 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1428 fi
1429 fi
1430}
1431
1432# Compatibility for existing stop_XXXX() functions
1433# Stop a service in screen
1434# If a PID is available use it, kill the whole process group via TERM
1435# If screen is being used kill the screen window; this will catch processes
1436# that did not leave a PID behind
1437# screen_stop service
1438function screen_stop {
1439 # Clean up the screen window
1440 stop_process $1
1441}
1442
1443
Sean Dague2c65e712014-12-18 09:44:56 -05001444# Plugin Functions
1445# =================
1446
1447DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1448
1449# enable_plugin <name> <url> [branch]
1450#
1451# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1452# ``url`` is a git url
1453# ``branch`` is a gitref. If it's not set, defaults to master
1454function enable_plugin {
1455 local name=$1
1456 local url=$2
1457 local branch=${3:-master}
1458 DEVSTACK_PLUGINS+=",$name"
1459 GITREPO[$name]=$url
1460 GITDIR[$name]=$DEST/$name
1461 GITBRANCH[$name]=$branch
1462}
1463
1464# fetch_plugins
1465#
1466# clones all plugins
1467function fetch_plugins {
1468 local plugins="${DEVSTACK_PLUGINS}"
1469 local plugin
1470
1471 # short circuit if nothing to do
1472 if [[ -z $plugins ]]; then
1473 return
1474 fi
1475
1476 echo "Fetching devstack plugins"
1477 for plugin in ${plugins//,/ }; do
1478 git_clone_by_name $plugin
1479 done
1480}
1481
1482# load_plugin_settings
1483#
1484# Load settings from plugins in the order that they were registered
1485function load_plugin_settings {
1486 local plugins="${DEVSTACK_PLUGINS}"
1487 local plugin
1488
1489 # short circuit if nothing to do
1490 if [[ -z $plugins ]]; then
1491 return
1492 fi
1493
1494 echo "Loading plugin settings"
1495 for plugin in ${plugins//,/ }; do
1496 local dir=${GITDIR[$plugin]}
1497 # source any known settings
1498 if [[ -f $dir/devstack/settings ]]; then
1499 source $dir/devstack/settings
1500 fi
1501 done
1502}
1503
1504# run_plugins
1505#
1506# Run the devstack/plugin.sh in all the plugin directories. These are
1507# run in registration order.
1508function run_plugins {
1509 local mode=$1
1510 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301511
1512 local plugins="${DEVSTACK_PLUGINS}"
1513 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001514 for plugin in ${plugins//,/ }; do
1515 local dir=${GITDIR[$plugin]}
1516 if [[ -f $dir/devstack/plugin.sh ]]; then
1517 source $dir/devstack/plugin.sh $mode $phase
1518 fi
1519 done
1520}
1521
1522function run_phase {
1523 local mode=$1
1524 local phase=$2
1525 if [[ -d $TOP_DIR/extras.d ]]; then
1526 for i in $TOP_DIR/extras.d/*.sh; do
1527 [[ -r $i ]] && source $i $mode $phase
1528 done
1529 fi
1530 # the source phase corresponds to settings loading in plugins
1531 if [[ "$mode" == "source" ]]; then
1532 load_plugin_settings
1533 else
1534 run_plugins $mode $phase
1535 fi
1536}
1537
Dean Troyerdff49a22014-01-30 15:37:40 -06001538
1539# Service Functions
1540# =================
1541
1542# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1543# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001544function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001545 echo "$1" | sed -e '
1546 s/,,/,/g;
1547 s/^,//;
1548 s/,$//
1549 '
1550}
1551
1552# disable_all_services() removes all current services
1553# from ``ENABLED_SERVICES`` to reset the configuration
1554# before a minimal installation
1555# Uses global ``ENABLED_SERVICES``
1556# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001557function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001558 ENABLED_SERVICES=""
1559}
1560
1561# Remove all services starting with '-'. For example, to install all default
1562# services except rabbit (rabbit) set in ``localrc``:
1563# ENABLED_SERVICES+=",-rabbit"
1564# Uses global ``ENABLED_SERVICES``
1565# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001566function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001567 local tmpsvcs="${ENABLED_SERVICES}"
1568 local service
1569 for service in ${tmpsvcs//,/ }; do
1570 if [[ ${service} == -* ]]; then
1571 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1572 fi
1573 done
1574 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1575}
1576
1577# disable_service() removes the services passed as argument to the
1578# ``ENABLED_SERVICES`` list, if they are present.
1579#
1580# For example:
1581# disable_service rabbit
1582#
1583# This function does not know about the special cases
1584# for nova, glance, and neutron built into is_service_enabled().
1585# Uses global ``ENABLED_SERVICES``
1586# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001587function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001588 local tmpsvcs=",${ENABLED_SERVICES},"
1589 local service
1590 for service in $@; do
1591 if is_service_enabled $service; then
1592 tmpsvcs=${tmpsvcs//,$service,/,}
1593 fi
1594 done
1595 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1596}
1597
1598# enable_service() adds the services passed as argument to the
1599# ``ENABLED_SERVICES`` list, if they are not already present.
1600#
1601# For example:
1602# enable_service qpid
1603#
1604# This function does not know about the special cases
1605# for nova, glance, and neutron built into is_service_enabled().
1606# Uses global ``ENABLED_SERVICES``
1607# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001608function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001609 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001610 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001611 for service in $@; do
1612 if ! is_service_enabled $service; then
1613 tmpsvcs+=",$service"
1614 fi
1615 done
1616 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1617 disable_negated_services
1618}
1619
1620# is_service_enabled() checks if the service(s) specified as arguments are
1621# enabled by the user in ``ENABLED_SERVICES``.
1622#
1623# Multiple services specified as arguments are ``OR``'ed together; the test
1624# is a short-circuit boolean, i.e it returns on the first match.
1625#
1626# There are special cases for some 'catch-all' services::
1627# **nova** returns true if any service enabled start with **n-**
1628# **cinder** returns true if any service enabled start with **c-**
1629# **ceilometer** returns true if any service enabled start with **ceilometer**
1630# **glance** returns true if any service enabled start with **g-**
1631# **neutron** returns true if any service enabled start with **q-**
1632# **swift** returns true if any service enabled start with **s-**
1633# **trove** returns true if any service enabled start with **tr-**
1634# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1635# **s-** services will be enabled. This will be deprecated in the future.
1636#
1637# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1638# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1639# as enabled in this case.
1640#
1641# Uses global ``ENABLED_SERVICES``
1642# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001643function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001644 local xtrace=$(set +o | grep xtrace)
1645 set +o xtrace
1646 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001647 local services=$@
1648 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001649 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001650 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001651
1652 # Look for top-level 'enabled' function for this service
1653 if type is_${service}_enabled >/dev/null 2>&1; then
1654 # A function exists for this service, use it
1655 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001656 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001657 fi
1658
1659 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1660 # are implemented
1661
Sean Dague45917cc2014-02-24 16:09:14 -05001662 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001663 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001664 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1665 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1666 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1667 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1668 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1669 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1670 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1671 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1672 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001673 done
Sean Dague45917cc2014-02-24 16:09:14 -05001674 $xtrace
1675 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001676}
1677
1678# Toggle enable/disable_service for services that must run exclusive of each other
1679# $1 The name of a variable containing a space-separated list of services
1680# $2 The name of a variable in which to store the enabled service's name
1681# $3 The name of the service to enable
1682function use_exclusive_service {
1683 local options=${!1}
1684 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001685 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001686 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001687 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001688 for opt in $options;do
1689 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1690 done
1691 eval "$out=$selection"
1692 return 0
1693}
1694
1695
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001696# System Functions
1697# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001698
1699# Only run the command if the target file (the last arg) is not on an
1700# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001701function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001702 local xtrace=$(set +o | grep xtrace)
1703 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001704 local args=( $@ )
1705 local last
1706 local sudo_cmd
1707 local dir_to_check
1708
1709 let last="${#args[*]} - 1"
1710
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001711 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001712 if [ ! -d "$dir_to_check" ]; then
1713 dir_to_check=`dirname "$dir_to_check"`
1714 fi
1715
1716 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001717 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001718 return 0
1719 fi
1720
1721 if [[ $TRACK_DEPENDS = True ]]; then
1722 sudo_cmd="env"
1723 else
1724 sudo_cmd="sudo"
1725 fi
1726
Sean Dague45917cc2014-02-24 16:09:14 -05001727 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001728 $sudo_cmd $@
1729}
1730
1731# Exit 0 if address is in network or 1 if address is not in network
1732# ip-range is in CIDR notation: 1.2.3.4/20
1733# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001734function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001735 local ip=$1
1736 local range=$2
1737 local masklen=${range#*/}
1738 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1739 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1740 [[ $network == $subnet ]]
1741}
1742
1743# Add a user to a group.
1744# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001745function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001746 local user=$1
1747 local group=$2
1748
1749 if [[ -z "$os_VENDOR" ]]; then
1750 GetOSVersion
1751 fi
1752
1753 # SLE11 and openSUSE 12.2 don't have the usual usermod
1754 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1755 sudo usermod -a -G "$group" "$user"
1756 else
1757 sudo usermod -A "$group" "$user"
1758 fi
1759}
1760
1761# Convert CIDR notation to a IPv4 netmask
1762# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001763function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001764 local maskpat="255 255 255 255"
1765 local maskdgt="254 252 248 240 224 192 128"
1766 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1767 echo ${1-0}.${2-0}.${3-0}.${4-0}
1768}
1769
1770# Gracefully cp only if source file/dir exists
1771# cp_it source destination
1772function cp_it {
1773 if [ -e $1 ] || [ -d $1 ]; then
1774 cp -pRL $1 $2
1775 fi
1776}
1777
1778# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1779# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1780# ``localrc`` or on the command line if necessary::
1781#
1782# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1783#
1784# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1785
Ian Wienandaee18c72014-02-21 15:35:08 +11001786function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001787 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001788 export http_proxy=$http_proxy
1789 fi
Sean Dague53753292014-12-04 19:38:15 -05001790 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001791 export https_proxy=$https_proxy
1792 fi
Sean Dague53753292014-12-04 19:38:15 -05001793 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001794 export no_proxy=$no_proxy
1795 fi
1796}
1797
1798# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001799function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001800 local mount_type=`stat -f -L -c %T $1`
1801 test "$mount_type" == "nfs"
1802}
1803
1804# Return the network portion of the given IP address using netmask
1805# netmask is in the traditional dotted-quad format
1806# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001807function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001808 local ip=$1
1809 local mask=$2
1810 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1811 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1812 echo $subnet
1813}
1814
1815# Service wrapper to restart services
1816# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001817function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001818 if is_ubuntu; then
1819 sudo /usr/sbin/service $1 restart
1820 else
1821 sudo /sbin/service $1 restart
1822 fi
1823}
1824
1825# Only change permissions of a file or directory if it is not on an
1826# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001827function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06001828 _safe_permission_operation chmod $@
1829}
1830
1831# Only change ownership of a file or directory if it is not on an NFS
1832# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001833function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06001834 _safe_permission_operation chown $@
1835}
1836
1837# Service wrapper to start services
1838# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001839function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001840 if is_ubuntu; then
1841 sudo /usr/sbin/service $1 start
1842 else
1843 sudo /sbin/service $1 start
1844 fi
1845}
1846
1847# Service wrapper to stop services
1848# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11001849function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001850 if is_ubuntu; then
1851 sudo /usr/sbin/service $1 stop
1852 else
1853 sudo /sbin/service $1 stop
1854 fi
1855}
1856
1857
1858# Restore xtrace
1859$XTRACE
1860
1861# Local variables:
1862# mode: shell-script
1863# End: