blob: 1a0b5d6d1c0817c3cf6522803cf0b3783d6faf8a [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
18# - Python Functions
19# - Service Functions
Masayuki Igawaf6368d32014-02-20 13:31:26 +090020# - System Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060021#
22# The following variables are assumed to be defined by certain functions:
23#
Jamie Lennox51f0de52014-10-20 16:32:34 +020024# - ``GIT_DEPTH``
Dean Troyerdff49a22014-01-30 15:37:40 -060025# - ``ENABLED_SERVICES``
26# - ``ERROR_ON_CLONE``
27# - ``FILES``
28# - ``OFFLINE``
29# - ``PIP_DOWNLOAD_CACHE``
Dean Troyerdff49a22014-01-30 15:37:40 -060030# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090031# - ``REQUIREMENTS_DIR``
32# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060033# - ``TRACK_DEPENDS``
Masayuki Igawad20f6322014-02-28 09:22:37 +090034# - ``UNDO_REQUIREMENTS``
Dean Troyerdff49a22014-01-30 15:37:40 -060035# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050036#
Dean Troyerdff49a22014-01-30 15:37:40 -060037
38# Save trace setting
39XTRACE=$(set +o | grep xtrace)
40set +o xtrace
41
Sean Daguecc524062014-10-01 09:06:43 -040042# Global Config Variables
43declare -A GITREPO
44declare -A GITBRANCH
45declare -A GITDIR
46
Dean Troyerdff49a22014-01-30 15:37:40 -060047# Config Functions
48# ================
49
50# Append a new option in an ini file without replacing the old value
51# iniadd config-file section option value1 value2 value3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +110052function iniadd {
Sean Dague45917cc2014-02-24 16:09:14 -050053 local xtrace=$(set +o | grep xtrace)
54 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060055 local file=$1
56 local section=$2
57 local option=$3
58 shift 3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050059
Dean Troyerdff49a22014-01-30 15:37:40 -060060 local values="$(iniget_multiline $file $section $option) $@"
61 iniset_multiline $file $section $option $values
Sean Dague45917cc2014-02-24 16:09:14 -050062 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060063}
64
65# Comment an option in an INI file
66# inicomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110067function inicomment {
Sean Dague45917cc2014-02-24 16:09:14 -050068 local xtrace=$(set +o | grep xtrace)
69 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060070 local file=$1
71 local section=$2
72 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050073
Dean Troyerdff49a22014-01-30 15:37:40 -060074 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -050075 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060076}
77
78# Get an option from an INI file
79# iniget config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110080function iniget {
Sean Dague45917cc2014-02-24 16:09:14 -050081 local xtrace=$(set +o | grep xtrace)
82 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060083 local file=$1
84 local section=$2
85 local option=$3
86 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050087
Dean Troyerdff49a22014-01-30 15:37:40 -060088 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
89 echo ${line#*=}
Sean Dague45917cc2014-02-24 16:09:14 -050090 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060091}
92
93# Get a multiple line option from an INI file
94# iniget_multiline config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110095function iniget_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -050096 local xtrace=$(set +o | grep xtrace)
97 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060098 local file=$1
99 local section=$2
100 local option=$3
101 local values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500102
Dean Troyerdff49a22014-01-30 15:37:40 -0600103 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
104 echo ${values}
Sean Dague45917cc2014-02-24 16:09:14 -0500105 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600106}
107
108# Determinate is the given option present in the INI file
109# ini_has_option config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100110function ini_has_option {
Sean Dague45917cc2014-02-24 16:09:14 -0500111 local xtrace=$(set +o | grep xtrace)
112 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600113 local file=$1
114 local section=$2
115 local option=$3
116 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500117
Dean Troyerdff49a22014-01-30 15:37:40 -0600118 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Sean Dague45917cc2014-02-24 16:09:14 -0500119 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600120 [ -n "$line" ]
121}
122
Robert Li751ad1a2014-10-15 21:40:53 -0400123# Add another config line for a multi-line option.
124# It's normally called after iniset of the same option and assumes
125# that the section already exists.
126#
127# Note that iniset_multiline requires all the 'lines' to be supplied
128# in the argument list. Doing that will cause incorrect configuration
129# if spaces are used in the config values.
130#
131# iniadd_literal config-file section option value
132function iniadd_literal {
133 local xtrace=$(set +o | grep xtrace)
134 set +o xtrace
135 local file=$1
136 local section=$2
137 local option=$3
138 local value=$4
139
140 [[ -z $section || -z $option ]] && return
141
142 # Add it
143 sed -i -e "/^\[$section\]/ a\\
144$option = $value
145" "$file"
146
147 $xtrace
148}
149
Dean Troyerdff49a22014-01-30 15:37:40 -0600150# Set an option in an INI file
151# iniset config-file section option value
Ian Wienandaee18c72014-02-21 15:35:08 +1100152function iniset {
Sean Dague45917cc2014-02-24 16:09:14 -0500153 local xtrace=$(set +o | grep xtrace)
154 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600155 local file=$1
156 local section=$2
157 local option=$3
158 local value=$4
159
160 [[ -z $section || -z $option ]] && return
161
162 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
163 # Add section at the end
164 echo -e "\n[$section]" >>"$file"
165 fi
166 if ! ini_has_option "$file" "$section" "$option"; then
167 # Add it
168 sed -i -e "/^\[$section\]/ a\\
169$option = $value
170" "$file"
171 else
172 local sep=$(echo -ne "\x01")
173 # Replace it
174 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
175 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500176 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600177}
178
179# Set a multiple line option in an INI file
180# iniset_multiline config-file section option value1 value2 valu3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +1100181function iniset_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -0500182 local xtrace=$(set +o | grep xtrace)
183 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600184 local file=$1
185 local section=$2
186 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500187
Dean Troyerdff49a22014-01-30 15:37:40 -0600188 shift 3
189 local values
190 for v in $@; do
191 # The later sed command inserts each new value in the line next to
192 # the section identifier, which causes the values to be inserted in
193 # the reverse order. Do a reverse here to keep the original order.
194 values="$v ${values}"
195 done
196 if ! grep -q "^\[$section\]" "$file"; then
197 # Add section at the end
198 echo -e "\n[$section]" >>"$file"
199 else
200 # Remove old values
201 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
202 fi
203 # Add new ones
204 for v in $values; do
205 sed -i -e "/^\[$section\]/ a\\
206$option = $v
207" "$file"
208 done
Sean Dague45917cc2014-02-24 16:09:14 -0500209 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600210}
211
212# Uncomment an option in an INI file
213# iniuncomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100214function iniuncomment {
Sean Dague45917cc2014-02-24 16:09:14 -0500215 local xtrace=$(set +o | grep xtrace)
216 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600217 local file=$1
218 local section=$2
219 local option=$3
220 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500221 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600222}
223
224# Normalize config values to True or False
225# Accepts as False: 0 no No NO false False FALSE
226# Accepts as True: 1 yes Yes YES true True TRUE
227# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100228function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500229 local xtrace=$(set +o | grep xtrace)
230 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600231 local default=$1
232 local testval=$2
233
234 [[ -z "$testval" ]] && { echo "$default"; return; }
235 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
236 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
237 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500238 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600239}
240
241
242# Control Functions
243# =================
244
245# Prints backtrace info
246# filename:lineno:function
247# backtrace level
248function backtrace {
249 local level=$1
250 local deep=$((${#BASH_SOURCE[@]} - 1))
251 echo "[Call Trace]"
252 while [ $level -le $deep ]; do
253 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
254 deep=$((deep - 1))
255 done
256}
257
258# Prints line number and "message" then exits
259# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100260function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600261 local exitcode=$?
262 set +o xtrace
263 local line=$1; shift
264 if [ $exitcode == 0 ]; then
265 exitcode=1
266 fi
267 backtrace 2
268 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600269 # Give buffers a second to flush
270 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600271 exit $exitcode
272}
273
274# Checks an environment variable is not set or has length 0 OR if the
275# exit code is non-zero and prints "message" and exits
276# NOTE: env-var is the variable name without a '$'
277# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100278function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600279 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500280 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600281 set +o xtrace
282 local line=$1; shift
283 local evar=$1; shift
284 if ! is_set $evar || [ $exitcode != 0 ]; then
285 die $line "$*"
286 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500287 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600288}
289
290# Prints line number and "message" in error format
291# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100292function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600293 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500294 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600295 set +o xtrace
296 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
297 echo $msg 1>&2;
298 if [[ -n ${SCREEN_LOGDIR} ]]; then
299 echo $msg >> "${SCREEN_LOGDIR}/error.log"
300 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500301 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600302 return $exitcode
303}
304
305# Checks an environment variable is not set or has length 0 OR if the
306# exit code is non-zero and prints "message"
307# NOTE: env-var is the variable name without a '$'
308# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100309function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600310 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500311 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600312 set +o xtrace
313 local line=$1; shift
314 local evar=$1; shift
315 if ! is_set $evar || [ $exitcode != 0 ]; then
316 err $line "$*"
317 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500318 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600319 return $exitcode
320}
321
322# Exit after outputting a message about the distribution not being supported.
323# exit_distro_not_supported [optional-string-telling-what-is-missing]
324function exit_distro_not_supported {
325 if [[ -z "$DISTRO" ]]; then
326 GetDistro
327 fi
328
329 if [ $# -gt 0 ]; then
330 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
331 else
332 die $LINENO "Support for $DISTRO is incomplete."
333 fi
334}
335
336# Test if the named environment variable is set and not zero length
337# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100338function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600339 local var=\$"$1"
340 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
341}
342
343# Prints line number and "message" in warning format
344# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100345function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600346 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500347 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600348 set +o xtrace
349 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
350 echo $msg 1>&2;
351 if [[ -n ${SCREEN_LOGDIR} ]]; then
352 echo $msg >> "${SCREEN_LOGDIR}/error.log"
353 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500354 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600355 return $exitcode
356}
357
358
359# Distro Functions
360# ================
361
362# Determine OS Vendor, Release and Update
363# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
364# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500365# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
366# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
367# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
368# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
369# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
370declare os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
371
Dean Troyerdff49a22014-01-30 15:37:40 -0600372# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100373function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500374
Dean Troyerdff49a22014-01-30 15:37:40 -0600375 # Figure out which vendor we are
376 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
377 # OS/X
378 os_VENDOR=`sw_vers -productName`
379 os_RELEASE=`sw_vers -productVersion`
380 os_UPDATE=${os_RELEASE##*.}
381 os_RELEASE=${os_RELEASE%.*}
382 os_PACKAGE=""
383 if [[ "$os_RELEASE" =~ "10.7" ]]; then
384 os_CODENAME="lion"
385 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
386 os_CODENAME="snow leopard"
387 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
388 os_CODENAME="leopard"
389 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
390 os_CODENAME="tiger"
391 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
392 os_CODENAME="panther"
393 else
394 os_CODENAME=""
395 fi
396 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
397 os_VENDOR=$(lsb_release -i -s)
398 os_RELEASE=$(lsb_release -r -s)
399 os_UPDATE=""
400 os_PACKAGE="rpm"
401 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
402 os_PACKAGE="deb"
403 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
404 lsb_release -d -s | grep -q openSUSE
405 if [[ $? -eq 0 ]]; then
406 os_VENDOR="openSUSE"
407 fi
408 elif [[ $os_VENDOR == "openSUSE project" ]]; then
409 os_VENDOR="openSUSE"
410 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
411 os_VENDOR="Red Hat"
412 fi
413 os_CODENAME=$(lsb_release -c -s)
414 elif [[ -r /etc/redhat-release ]]; then
415 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
416 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
417 # CentOS release 5.5 (Final)
418 # CentOS Linux release 6.0 (Final)
419 # Fedora release 16 (Verne)
420 # XenServer release 6.2.0-70446c (xenenterprise)
421 os_CODENAME=""
422 for r in "Red Hat" CentOS Fedora XenServer; do
423 os_VENDOR=$r
424 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
425 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
426 os_CODENAME=${ver#*|}
427 os_RELEASE=${ver%|*}
428 os_UPDATE=${os_RELEASE##*.}
429 os_RELEASE=${os_RELEASE%.*}
430 break
431 fi
432 os_VENDOR=""
433 done
434 os_PACKAGE="rpm"
435 elif [[ -r /etc/SuSE-release ]]; then
436 for r in openSUSE "SUSE Linux"; do
437 if [[ "$r" = "SUSE Linux" ]]; then
438 os_VENDOR="SUSE LINUX"
439 else
440 os_VENDOR=$r
441 fi
442
443 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
444 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
445 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
446 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
447 break
448 fi
449 os_VENDOR=""
450 done
451 os_PACKAGE="rpm"
452 # If lsb_release is not installed, we should be able to detect Debian OS
453 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
454 os_VENDOR="Debian"
455 os_PACKAGE="deb"
456 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
457 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
458 fi
459 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
460}
461
462# Translate the OS version values into common nomenclature
463# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500464declare DISTRO
465
Ian Wienandaee18c72014-02-21 15:35:08 +1100466function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600467 GetOSVersion
468 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
469 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
470 DISTRO=$os_CODENAME
471 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
472 # For Fedora, just use 'f' and the release
473 DISTRO="f$os_RELEASE"
474 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
475 DISTRO="opensuse-$os_RELEASE"
476 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
477 # For SLE, also use the service pack
478 if [[ -z "$os_UPDATE" ]]; then
479 DISTRO="sle${os_RELEASE}"
480 else
481 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
482 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530483 elif [[ "$os_VENDOR" =~ (Red Hat) || \
484 "$os_VENDOR" =~ (CentOS) || \
485 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600486 # Drop the . release as we assume it's compatible
487 DISTRO="rhel${os_RELEASE::1}"
488 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
489 DISTRO="xs$os_RELEASE"
490 else
491 # Catch-all for now is Vendor + Release + Update
492 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
493 fi
494 export DISTRO
495}
496
497# Utility function for checking machine architecture
498# is_arch arch-type
499function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500500 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600501}
502
Ian Wienandbdc90c52014-08-04 15:44:58 +1000503# Quick check for a rackspace host; n.b. rackspace provided images
504# have these Xen tools installed but a custom image may not.
505function is_rackspace {
506 [ -f /usr/bin/xenstore-ls ] && \
507 sudo /usr/bin/xenstore-ls vm-data | grep -q "Rackspace"
508}
509
Dean Troyerdff49a22014-01-30 15:37:40 -0600510# Determine if current distribution is a Fedora-based distribution
511# (Fedora, RHEL, CentOS, etc).
512# is_fedora
513function is_fedora {
514 if [[ -z "$os_VENDOR" ]]; then
515 GetOSVersion
516 fi
517
anju Tiwari6c639c92014-07-15 18:11:54 +0530518 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
519 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600520}
521
522
523# Determine if current distribution is a SUSE-based distribution
524# (openSUSE, SLE).
525# is_suse
526function is_suse {
527 if [[ -z "$os_VENDOR" ]]; then
528 GetOSVersion
529 fi
530
531 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
532}
533
534
535# Determine if current distribution is an Ubuntu-based distribution
536# It will also detect non-Ubuntu but Debian-based distros
537# is_ubuntu
538function is_ubuntu {
539 if [[ -z "$os_PACKAGE" ]]; then
540 GetOSVersion
541 fi
542 [ "$os_PACKAGE" = "deb" ]
543}
544
545
546# Git Functions
547# =============
548
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600549# Returns openstack release name for a given branch name
550# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100551function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600552 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700553 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600554 echo ${branch#*/}
555 else
556 echo "master"
557 fi
558}
559
Dean Troyerdff49a22014-01-30 15:37:40 -0600560# git clone only if directory doesn't exist already. Since ``DEST`` might not
561# be owned by the installation user, we create the directory and change the
562# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500563# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
564# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600565# does not exist (default is False, meaning the repo will be cloned).
Jamie Lennox51f0de52014-10-20 16:32:34 +0200566# Set global ``GIT_DEPTH=<number>`` to limit the history depth of the git clone
567# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``, ``GIT_DEPTH``
Dean Troyerdff49a22014-01-30 15:37:40 -0600568# git_clone remote dest-dir branch
569function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500570 local git_remote=$1
571 local git_dest=$2
572 local git_ref=$3
573 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200574 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500575
Dean Troyerdff49a22014-01-30 15:37:40 -0600576 RECLONE=$(trueorfalse False $RECLONE)
577
YAMAMOTO Takashi292b2a72014-10-31 13:48:58 +0900578 if [[ -n "${GIT_DEPTH}" ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200579 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
580 fi
581
Dean Troyerdff49a22014-01-30 15:37:40 -0600582 if [[ "$OFFLINE" = "True" ]]; then
583 echo "Running in offline mode, clones already exist"
584 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500585 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600586 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400587 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600588 return
589 fi
590
Dean Troyer50cda692014-07-25 11:57:20 -0500591 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600592 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500593 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600594 [[ "$ERROR_ON_CLONE" = "True" ]] && \
595 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200596 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600597 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500598 cd $git_dest
599 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600600 else
601 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500602 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600603 [[ "$ERROR_ON_CLONE" = "True" ]] && \
604 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200605 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500606 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600607 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500608 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600609 elif [[ "$RECLONE" = "True" ]]; then
610 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500611 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600612 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500613 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100614 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600615 # remove the existing ignored files (like pyc) as they cause breakage
616 # (due to the py files having older timestamps than our pyc, so python
617 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500618 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600619
Dean Troyer50cda692014-07-25 11:57:20 -0500620 # handle git_ref accordingly to type (tag, branch)
621 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
622 git_update_tag $git_ref
623 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
624 git_update_branch $git_ref
625 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
626 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600627 else
Dean Troyer50cda692014-07-25 11:57:20 -0500628 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600629 fi
630
631 fi
632 fi
633
634 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500635 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600636 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400637 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600638}
639
Sean Daguecc524062014-10-01 09:06:43 -0400640# A variation on git clone that lets us specify a project by it's
641# actual name, like oslo.config. This is exceptionally useful in the
642# library installation case
643function git_clone_by_name {
644 local name=$1
645 local repo=${GITREPO[$name]}
646 local dir=${GITDIR[$name]}
647 local branch=${GITBRANCH[$name]}
648 git_clone $repo $dir $branch
649}
650
651
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100652# git can sometimes get itself infinitely stuck with transient network
653# errors or other issues with the remote end. This wraps git in a
654# timeout/retry loop and is intended to watch over non-local git
655# processes that might hang. GIT_TIMEOUT, if set, is passed directly
656# to timeout(1); otherwise the default value of 0 maintains the status
657# quo of waiting forever.
658# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100659function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100660 local count=0
661 local timeout=0
662
663 if [[ -n "${GIT_TIMEOUT}" ]]; then
664 timeout=${GIT_TIMEOUT}
665 fi
666
667 until timeout -s SIGINT ${timeout} git "$@"; do
668 # 124 is timeout(1)'s special return code when it reached the
669 # timeout; otherwise assume fatal failure
670 if [[ $? -ne 124 ]]; then
671 die $LINENO "git call failed: [git $@]"
672 fi
673
674 count=$(($count + 1))
675 warn "timeout ${count} for git call: [git $@]"
676 if [ $count -eq 3 ]; then
677 die $LINENO "Maximum of 3 git retries reached"
678 fi
679 sleep 5
680 done
681}
682
Dean Troyerdff49a22014-01-30 15:37:40 -0600683# git update using reference as a branch.
684# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100685function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500686 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600687
Dean Troyer50cda692014-07-25 11:57:20 -0500688 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600689 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500690 git branch -D $git_branch || true
691 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600692}
693
694# git update using reference as a branch.
695# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100696function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500697 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600698
Dean Troyer50cda692014-07-25 11:57:20 -0500699 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600700}
701
702# git update using reference as a tag. Be careful editing source at that repo
703# as working copy will be in a detached mode
704# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100705function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500706 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600707
Dean Troyer50cda692014-07-25 11:57:20 -0500708 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600709 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500710 git_timed fetch origin tag $git_tag
711 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600712}
713
714
715# OpenStack Functions
716# ===================
717
718# Get the default value for HOST_IP
719# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100720function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600721 local fixed_range=$1
722 local floating_range=$2
723 local host_ip_iface=$3
724 local host_ip=$4
725
726 # Find the interface used for the default route
727 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
728 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
729 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
730 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500731 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
732 local ip
733 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600734 # Attempt to filter out IP addresses that are part of the fixed and
735 # floating range. Note that this method only works if the ``netaddr``
736 # python library is installed. If it is not installed, an error
737 # will be printed and the first IP from the interface will be used.
738 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
739 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500740 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
741 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600742 break;
743 fi
744 done
745 fi
746 echo $host_ip
747}
748
Attila Fazekasf71b5002014-05-28 09:52:22 +0200749# Generates hex string from ``size`` byte of pseudo random data
750# generate_hex_string size
751function generate_hex_string {
752 local size=$1
753 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
754}
755
Dean Troyerdff49a22014-01-30 15:37:40 -0600756# Grab a numbered field from python prettytable output
757# Fields are numbered starting with 1
758# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
759# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100760function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500761 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600762 while read data; do
763 if [ "$1" -lt 0 ]; then
764 field="(\$(NF$1))"
765 else
766 field="\$$(($1 + 1))"
767 fi
768 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
769 done
770}
771
772# Add a policy to a policy.json file
773# Do nothing if the policy already exists
774# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100775function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600776 local policy_file=$1
777 local policy_name=$2
778 local policy_perm=$3
779
780 if grep -q ${policy_name} ${policy_file}; then
781 echo "Policy ${policy_name} already exists in ${policy_file}"
782 return
783 fi
784
785 # Add a terminating comma to policy lines without one
786 # Remove the closing '}' and all lines following to the end-of-file
787 local tmpfile=$(mktemp)
788 uniq ${policy_file} | sed -e '
789 s/]$/],/
790 /^[}]/,$d
791 ' > ${tmpfile}
792
793 # Append policy and closing brace
794 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
795 echo "}" >>${tmpfile}
796
797 mv ${tmpfile} ${policy_file}
798}
799
Alistair Coles24779f62014-10-15 18:57:59 +0100800# Gets or creates a domain
801# Usage: get_or_create_domain <name> <description>
802function get_or_create_domain {
803 local os_url="$KEYSTONE_SERVICE_URI/v3"
804 # Gets domain id
805 local domain_id=$(
806 # Gets domain id
807 openstack --os-token=$OS_TOKEN --os-url=$os_url \
808 --os-identity-api-version=3 domain show $1 \
809 -f value -c id 2>/dev/null ||
810 # Creates new domain
811 openstack --os-token=$OS_TOKEN --os-url=$os_url \
812 --os-identity-api-version=3 domain create $1 \
813 --description "$2" \
814 -f value -c id
815 )
816 echo $domain_id
817}
818
Bartosz Górski0abde392014-02-28 14:15:19 +0100819# Gets or creates user
Alistair Coles24779f62014-10-15 18:57:59 +0100820# Usage: get_or_create_user <username> <password> <project> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100821function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200822 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500823 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200824 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500825 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200826 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100827 local os_cmd="openstack"
828 local domain=""
829 if [[ ! -z "$5" ]]; then
830 domain="--domain=$5"
831 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
832 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100833 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500834 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500835 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100836 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100837 $1 \
838 --password "$2" \
839 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500840 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100841 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500842 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100843 -f value -c id
844 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500845 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100846}
847
848# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100849# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100850function get_or_create_project {
851 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100852 local os_cmd="openstack"
853 local domain=""
854 if [[ ! -z "$2" ]]; then
855 domain="--domain=$2"
856 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
857 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500858 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500859 # Creates new project with --or-show
860 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100861 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500862 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100863}
864
865# Gets or creates role
866# Usage: get_or_create_role <name>
867function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500868 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500869 # Creates role with --or-show
870 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100871 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500872 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100873}
874
875# Gets or adds user role
876# Usage: get_or_add_user_role <role> <user> <project>
877function get_or_add_user_role {
878 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500879 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100880 $2 \
881 --project $3 \
882 --column "ID" \
883 --column "Name" \
884 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500885 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100886 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500887 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100888 $1 \
889 --user $2 \
890 --project $3 \
891 | grep " id " | get_field 2)
892 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500893 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100894}
895
896# Gets or creates service
897# Usage: get_or_create_service <name> <type> <description>
898function get_or_create_service {
899 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500900 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100901 # Gets service id
902 openstack service show $1 -f value -c id 2>/dev/null ||
903 # Creates new service if not exists
904 openstack service create \
905 $1 \
906 --type=$2 \
907 --description="$3" \
908 -f value -c id
909 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500910 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100911}
912
913# Gets or creates endpoint
914# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
915function get_or_create_endpoint {
916 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500917 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100918 --column "ID" \
919 --column "Region" \
920 --column "Service Name" \
921 | grep " $2 " \
922 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500923 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100924 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500925 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100926 $1 \
927 --region $2 \
928 --publicurl $3 \
929 --adminurl $4 \
930 --internalurl $5 \
931 | grep " id " | get_field 2)
932 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500933 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100934}
Dean Troyerdff49a22014-01-30 15:37:40 -0600935
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500936
Dean Troyerdff49a22014-01-30 15:37:40 -0600937# Package Functions
938# =================
939
940# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100941function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600942 local pkg_dir
943 if is_ubuntu; then
Monty Taylor81a016d2014-11-15 17:18:13 -0300944 pkg_dir=$FILES/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600945 elif is_fedora; then
946 pkg_dir=$FILES/rpms
947 elif is_suse; then
948 pkg_dir=$FILES/rpms-suse
949 else
950 exit_distro_not_supported "list of packages"
951 fi
952 echo "$pkg_dir"
953}
954
955# Wrapper for ``apt-get`` to set cache and proxy environment variables
956# Uses globals ``OFFLINE``, ``*_proxy``
957# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100958function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500959 local xtrace=$(set +o | grep xtrace)
960 set +o xtrace
961
Dean Troyerdff49a22014-01-30 15:37:40 -0600962 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
963 local sudo="sudo"
964 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500965
966 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600967 $sudo DEBIAN_FRONTEND=noninteractive \
968 http_proxy=$http_proxy https_proxy=$https_proxy \
969 no_proxy=$no_proxy \
970 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
971}
972
973# get_packages() collects a list of package names of any type from the
Monty Taylor81a016d2014-11-15 17:18:13 -0300974# prerequisite files in ``files/{debs|rpms}``. The list is intended
Dean Troyerdff49a22014-01-30 15:37:40 -0600975# to be passed to a package installer such as apt or yum.
976#
977# Only packages required for the services in 1st argument will be
978# included. Two bits of metadata are recognized in the prerequisite files:
979#
980# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
981# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
982# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100983function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500984 local xtrace=$(set +o | grep xtrace)
985 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600986 local services=$@
987 local package_dir=$(_get_package_dir)
988 local file_to_parse
989 local service
990
Flavio Percoco5a91c352014-10-31 18:48:00 +0100991 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
992
Dean Troyerdff49a22014-01-30 15:37:40 -0600993 if [[ -z "$package_dir" ]]; then
994 echo "No package directory supplied"
995 return 1
996 fi
997 if [[ -z "$DISTRO" ]]; then
998 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -0500999 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -06001000 fi
1001 for service in ${services//,/ }; do
1002 # Allow individual services to specify dependencies
1003 if [[ -e ${package_dir}/${service} ]]; then
1004 file_to_parse="${file_to_parse} $service"
1005 fi
1006 # NOTE(sdague) n-api needs glance for now because that's where
1007 # glance client is
1008 if [[ $service == n-api ]]; then
1009 if [[ ! $file_to_parse =~ nova ]]; then
1010 file_to_parse="${file_to_parse} nova"
1011 fi
1012 if [[ ! $file_to_parse =~ glance ]]; then
1013 file_to_parse="${file_to_parse} glance"
1014 fi
1015 elif [[ $service == c-* ]]; then
1016 if [[ ! $file_to_parse =~ cinder ]]; then
1017 file_to_parse="${file_to_parse} cinder"
1018 fi
1019 elif [[ $service == ceilometer-* ]]; then
1020 if [[ ! $file_to_parse =~ ceilometer ]]; then
1021 file_to_parse="${file_to_parse} ceilometer"
1022 fi
1023 elif [[ $service == s-* ]]; then
1024 if [[ ! $file_to_parse =~ swift ]]; then
1025 file_to_parse="${file_to_parse} swift"
1026 fi
1027 elif [[ $service == n-* ]]; then
1028 if [[ ! $file_to_parse =~ nova ]]; then
1029 file_to_parse="${file_to_parse} nova"
1030 fi
1031 elif [[ $service == g-* ]]; then
1032 if [[ ! $file_to_parse =~ glance ]]; then
1033 file_to_parse="${file_to_parse} glance"
1034 fi
1035 elif [[ $service == key* ]]; then
1036 if [[ ! $file_to_parse =~ keystone ]]; then
1037 file_to_parse="${file_to_parse} keystone"
1038 fi
1039 elif [[ $service == q-* ]]; then
1040 if [[ ! $file_to_parse =~ neutron ]]; then
1041 file_to_parse="${file_to_parse} neutron"
1042 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001043 elif [[ $service == ir-* ]]; then
1044 if [[ ! $file_to_parse =~ ironic ]]; then
1045 file_to_parse="${file_to_parse} ironic"
1046 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001047 fi
1048 done
1049
1050 for file in ${file_to_parse}; do
1051 local fname=${package_dir}/${file}
1052 local OIFS line package distros distro
1053 [[ -e $fname ]] || continue
1054
1055 OIFS=$IFS
1056 IFS=$'\n'
1057 for line in $(<${fname}); do
1058 if [[ $line =~ "NOPRIME" ]]; then
1059 continue
1060 fi
1061
1062 # Assume we want this package
1063 package=${line%#*}
1064 inst_pkg=1
1065
1066 # Look for # dist:xxx in comment
1067 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1068 # We are using BASH regexp matching feature.
1069 package=${BASH_REMATCH[1]}
1070 distros=${BASH_REMATCH[2]}
1071 # In bash ${VAR,,} will lowecase VAR
1072 # Look for a match in the distro list
1073 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1074 # If no match then skip this package
1075 inst_pkg=0
1076 fi
1077 fi
1078
1079 # Look for # testonly in comment
1080 if [[ $line =~ (.*)#.*testonly.* ]]; then
1081 package=${BASH_REMATCH[1]}
1082 # Are we installing test packages? (test for the default value)
1083 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1084 # If not installing test packages the skip this package
1085 inst_pkg=0
1086 fi
1087 fi
1088
1089 if [[ $inst_pkg = 1 ]]; then
1090 echo $package
1091 fi
1092 done
1093 IFS=$OIFS
1094 done
Sean Dague45917cc2014-02-24 16:09:14 -05001095 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001096}
1097
1098# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001099# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001100# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001101function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001102 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001103 return 0
1104 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001105
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001106 if is_ubuntu; then
1107 local xtrace=$(set +o | grep xtrace)
1108 set +o xtrace
1109 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1110 # if there are transient errors pulling the updates, that's fine.
1111 # It may be secondary repositories that we don't really care about.
1112 apt_get update || /bin/true
1113 REPOS_UPDATED=True
1114 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001115 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001116 fi
1117}
1118
1119function real_install_package {
1120 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001121 apt_get install "$@"
1122 elif is_fedora; then
1123 yum_install "$@"
1124 elif is_suse; then
1125 zypper_install "$@"
1126 else
1127 exit_distro_not_supported "installing packages"
1128 fi
1129}
1130
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001131# Distro-agnostic package installer
1132# install_package package [package ...]
1133function install_package {
1134 update_package_repo
1135 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1136}
1137
Dean Troyerdff49a22014-01-30 15:37:40 -06001138# Distro-agnostic function to tell if a package is installed
1139# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001140function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001141 if [[ -z "$@" ]]; then
1142 return 1
1143 fi
1144
1145 if [[ -z "$os_PACKAGE" ]]; then
1146 GetOSVersion
1147 fi
1148
1149 if [[ "$os_PACKAGE" = "deb" ]]; then
1150 dpkg -s "$@" > /dev/null 2> /dev/null
1151 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1152 rpm --quiet -q "$@"
1153 else
1154 exit_distro_not_supported "finding if a package is installed"
1155 fi
1156}
1157
1158# Distro-agnostic package uninstaller
1159# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001160function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001161 if is_ubuntu; then
1162 apt_get purge "$@"
1163 elif is_fedora; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001164 sudo $YUM remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001165 elif is_suse; then
1166 sudo zypper rm "$@"
1167 else
1168 exit_distro_not_supported "uninstalling packages"
1169 fi
1170}
1171
1172# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001173# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001174# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001175function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001176 [[ "$OFFLINE" = "True" ]] && return
1177 local sudo="sudo"
1178 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001179
1180 # The manual check for missing packages is because yum -y assumes
1181 # missing packages are OK. See
1182 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001183 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1184 no_proxy=$no_proxy \
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001185 $YUM install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001186 awk '
1187 BEGIN { fail=0 }
1188 /No package/ { fail=1 }
1189 { print }
1190 END { exit fail }' || \
1191 die $LINENO "Missing packages detected"
1192
1193 # also ensure we catch a yum failure
1194 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001195 die $LINENO "$YUM install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001196 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001197}
1198
1199# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001200# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001201# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001202function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001203 [[ "$OFFLINE" = "True" ]] && return
1204 local sudo="sudo"
1205 [[ "$(id -u)" = "0" ]] && sudo="env"
1206 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1207 zypper --non-interactive install --auto-agree-with-licenses "$@"
1208}
1209
1210
1211# Process Functions
1212# =================
1213
1214# _run_process() is designed to be backgrounded by run_process() to simulate a
1215# fork. It includes the dirty work of closing extra filehandles and preparing log
1216# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001217# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001218# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001219# If an optional group is provided sg will be used to set the group of
1220# the command.
1221# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001222function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001223 local service=$1
1224 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001225 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001226
1227 # Undo logging redirections and close the extra descriptors
1228 exec 1>&3
1229 exec 2>&3
1230 exec 3>&-
1231 exec 6>&-
1232
1233 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001234 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1235 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001236
1237 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1238 export PYTHONUNBUFFERED=1
1239 fi
1240
Dean Troyer3159a822014-08-27 14:13:58 -05001241 # Run under ``setsid`` to force the process to become a session and group leader.
1242 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001243 if [[ -n "$group" ]]; then
1244 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1245 else
1246 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1247 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001248
1249 # Just silently exit this process
1250 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001251}
1252
1253# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1254# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001255# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001256# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001257function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001258 SCREEN_NAME=${SCREEN_NAME:-stack}
1259 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1260
1261 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1262 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1263 fi
1264
1265 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1266}
1267
1268# Find out if a process exists by partial name.
1269# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001270function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001271 local name=$1
1272 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001273 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001274 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001275 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001276}
1277
Dean Troyer3159a822014-08-27 14:13:58 -05001278# Run a single service under screen or directly
1279# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001280# If an optional group is provided sg will be used to run the
1281# command as that group.
1282# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001283function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001284 local service=$1
1285 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001286 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001287
Dean Troyer3159a822014-08-27 14:13:58 -05001288 if is_service_enabled $service; then
1289 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001290 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001291 else
1292 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001293 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001294 fi
1295 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001296}
1297
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001298# Helper to launch a process in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001299# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1300# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001301# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001302# Run a command in a shell in a screen window, if an optional group
1303# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001304function screen_process {
1305 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001306 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001307 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001308
Sean Dagueea22a4f2014-06-27 15:21:41 -04001309 SCREEN_NAME=${SCREEN_NAME:-stack}
1310 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1311 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001312
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001313 # Append the process to the screen rc file
1314 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001315
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001316 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001317
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001318 if [[ -n ${SCREEN_LOGDIR} ]]; then
1319 screen -S $SCREEN_NAME -p $name -X logfile ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log
1320 screen -S $SCREEN_NAME -p $name -X log on
1321 ln -sf ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${name}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001322 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001323
1324 # sleep to allow bash to be ready to be send the command - we are
1325 # creating a new window in screen and then sends characters, so if
1326 # bash isn't running by the time we send the command, nothing happens
1327 sleep 3
1328
1329 NL=`echo -ne '\015'`
1330 # This fun command does the following:
1331 # - the passed server command is backgrounded
1332 # - the pid of the background process is saved in the usual place
1333 # - the server process is brought back to the foreground
1334 # - if the server process exits prematurely the fg command errors
1335 # and a message is written to stdout and the process failure file
1336 #
1337 # The pid saved can be used in stop_process() as a process group
1338 # id to kill off all child processes
1339 if [[ -n "$group" ]]; then
1340 command="sg $group '$command'"
1341 fi
1342 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 -06001343}
1344
1345# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001346# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001347# screen_rc service "command-line"
1348function screen_rc {
1349 SCREEN_NAME=${SCREEN_NAME:-stack}
1350 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1351 if [[ ! -e $SCREENRC ]]; then
1352 # Name the screen session
1353 echo "sessionname $SCREEN_NAME" > $SCREENRC
1354 # Set a reasonable statusbar
1355 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1356 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1357 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1358 echo "screen -t shell bash" >> $SCREENRC
1359 fi
1360 # If this service doesn't already exist in the screenrc file
1361 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1362 NL=`echo -ne '\015'`
1363 echo "screen -t $1 bash" >> $SCREENRC
1364 echo "stuff \"$2$NL\"" >> $SCREENRC
1365
1366 if [[ -n ${SCREEN_LOGDIR} ]]; then
1367 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1368 echo "log on" >>$SCREENRC
1369 fi
1370 fi
1371}
1372
1373# Stop a service in screen
1374# If a PID is available use it, kill the whole process group via TERM
1375# If screen is being used kill the screen window; this will catch processes
1376# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001377# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001378# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001379function screen_stop_service {
1380 local service=$1
1381
Dean Troyerdff49a22014-01-30 15:37:40 -06001382 SCREEN_NAME=${SCREEN_NAME:-stack}
1383 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1384 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1385
Dean Troyer3159a822014-08-27 14:13:58 -05001386 if is_service_enabled $service; then
1387 # Clean up the screen window
1388 screen -S $SCREEN_NAME -p $service -X kill
1389 fi
1390}
1391
1392# Stop a service process
1393# If a PID is available use it, kill the whole process group via TERM
1394# If screen is being used kill the screen window; this will catch processes
1395# that did not leave a PID behind
1396# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1397# stop_process service
1398function stop_process {
1399 local service=$1
1400
1401 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1402 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1403
1404 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001405 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001406 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1407 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1408 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001409 fi
1410 if [[ "$USE_SCREEN" = "True" ]]; then
1411 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001412 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001413 fi
1414 fi
1415}
1416
1417# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001418# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001419# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001420function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001421 local service
1422 local failures
1423 SCREEN_NAME=${SCREEN_NAME:-stack}
1424 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1425
1426
1427 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1428 echo "No service status directory found"
1429 return
1430 fi
1431
1432 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001433 # make this -o errexit safe
1434 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001435
1436 for service in $failures; do
1437 service=`basename $service`
1438 service=${service%.failure}
1439 echo "Error: Service $service is not running"
1440 done
1441
1442 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001443 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001444 fi
1445}
1446
Chris Dent2f27a0e2014-09-09 13:46:02 +01001447# Tail a log file in a screen if USE_SCREEN is true.
1448function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001449 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001450 local logfile=$2
1451
1452 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1453 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001454 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001455 fi
1456}
1457
Dean Troyerdff49a22014-01-30 15:37:40 -06001458
Dean Troyer3159a822014-08-27 14:13:58 -05001459# Deprecated Functions
1460# --------------------
1461
1462# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1463# fork. It includes the dirty work of closing extra filehandles and preparing log
1464# files to produce the same logs as screen_it(). The log filename is derived
1465# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1466# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1467# _old_run_process service "command-line"
1468function _old_run_process {
1469 local service=$1
1470 local command="$2"
1471
1472 # Undo logging redirections and close the extra descriptors
1473 exec 1>&3
1474 exec 2>&3
1475 exec 3>&-
1476 exec 6>&-
1477
1478 if [[ -n ${SCREEN_LOGDIR} ]]; then
1479 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1480 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1481
1482 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1483 export PYTHONUNBUFFERED=1
1484 fi
1485
1486 exec /bin/bash -c "$command"
1487 die "$service exec failure: $command"
1488}
1489
1490# old_run_process() launches a child process that closes all file descriptors and
1491# then exec's the passed in command. This is meant to duplicate the semantics
1492# of screen_it() without screen. PIDs are written to
1493# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1494# old_run_process service "command-line"
1495function old_run_process {
1496 local service=$1
1497 local command="$2"
1498
1499 # Spawn the child process
1500 _old_run_process "$service" "$command" &
1501 echo $!
1502}
1503
1504# Compatibility for existing start_XXXX() functions
1505# Uses global ``USE_SCREEN``
1506# screen_it service "command-line"
1507function screen_it {
1508 if is_service_enabled $1; then
1509 # Append the service to the screen rc file
1510 screen_rc "$1" "$2"
1511
1512 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001513 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001514 else
1515 # Spawn directly without screen
1516 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1517 fi
1518 fi
1519}
1520
1521# Compatibility for existing stop_XXXX() functions
1522# Stop a service in screen
1523# If a PID is available use it, kill the whole process group via TERM
1524# If screen is being used kill the screen window; this will catch processes
1525# that did not leave a PID behind
1526# screen_stop service
1527function screen_stop {
1528 # Clean up the screen window
1529 stop_process $1
1530}
1531
1532
Dean Troyerdff49a22014-01-30 15:37:40 -06001533# Python Functions
1534# ================
1535
1536# Get the path to the pip command.
1537# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001538function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001539 which pip || which pip-python
1540
1541 if [ $? -ne 0 ]; then
1542 die $LINENO "Unable to find pip; cannot continue"
1543 fi
1544}
1545
1546# Get the path to the direcotry where python executables are installed.
1547# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001548function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001549 if is_fedora || is_suse; then
1550 echo "/usr/bin"
1551 else
1552 echo "/usr/local/bin"
1553 fi
1554}
1555
1556# Wrapper for ``pip install`` to set cache and proxy environment variables
Attila Fazekasaf81d672014-11-10 09:04:54 +01001557# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``,
Dean Troyerdff49a22014-01-30 15:37:40 -06001558# ``TRACK_DEPENDS``, ``*_proxy``
1559# pip_install package [package ...]
1560function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001561 local xtrace=$(set +o | grep xtrace)
1562 set +o xtrace
1563 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1564 $xtrace
1565 return
1566 fi
1567
Dean Troyerdff49a22014-01-30 15:37:40 -06001568 if [[ -z "$os_PACKAGE" ]]; then
1569 GetOSVersion
1570 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001571 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1572 # TRACK_DEPENDS=True installation creates a circular dependency when
1573 # we attempt to install virtualenv into a virualenv, so we must global
1574 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001575 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001576 local cmd_pip=$DEST/.venv/bin/pip
1577 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001578 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001579 local cmd_pip=$(get_pip_command)
1580 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001581 fi
1582
Sean Dague45917cc2014-02-24 16:09:14 -05001583 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001584 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001585 http_proxy=$http_proxy \
1586 https_proxy=$https_proxy \
1587 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001588 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001589 $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001590
Flavio Percoco5a91c352014-10-31 18:48:00 +01001591 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001592 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1593 local test_req="$@/test-requirements.txt"
1594 if [[ -e "$test_req" ]]; then
1595 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1596 http_proxy=$http_proxy \
1597 https_proxy=$https_proxy \
1598 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001599 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001600 -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001601 fi
1602 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001603}
1604
Sean Daguecc524062014-10-01 09:06:43 -04001605# should we use this library from their git repo, or should we let it
1606# get pulled in via pip dependencies.
1607function use_library_from_git {
1608 local name=$1
1609 local enabled=1
1610 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1611 return $enabled
1612}
1613
1614# setup a library by name. If we are trying to use the library from
1615# git, we'll do a git based install, otherwise we'll punt and the
1616# library should be installed by a requirements pull from another
1617# project.
1618function setup_lib {
1619 local name=$1
1620 local dir=${GITDIR[$name]}
1621 setup_install $dir
1622}
1623
Sean Daguee08ab102014-11-13 17:09:28 -05001624# setup a library by name in editiable mode. If we are trying to use
1625# the library from git, we'll do a git based install, otherwise we'll
1626# punt and the library should be installed by a requirements pull from
1627# another project.
1628#
1629# use this for non namespaced libraries
1630function setup_dev_lib {
1631 local name=$1
1632 local dir=${GITDIR[$name]}
1633 setup_develop $dir
1634}
Sean Daguecc524062014-10-01 09:06:43 -04001635
Sean Dague099e5e32014-03-31 10:35:43 -04001636# this should be used if you want to install globally, all libraries should
1637# use this, especially *oslo* ones
1638function setup_install {
1639 local project_dir=$1
1640 setup_package_with_req_sync $project_dir
1641}
1642
1643# this should be used for projects which run services, like all services
1644function setup_develop {
1645 local project_dir=$1
1646 setup_package_with_req_sync $project_dir -e
1647}
1648
Sean Daguedef15342014-10-27 12:26:04 -04001649# determine if a project as specified by directory is in
1650# projects.txt. This will not be an exact match because we throw away
1651# the namespacing when we clone, but it should be good enough in all
1652# practical ways.
1653function is_in_projects_txt {
1654 local project_dir=$1
1655 local project_name=$(basename $project_dir)
1656 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1657}
1658
Dean Troyeraf616d92014-02-17 12:57:55 -06001659# ``pip install -e`` the package, which processes the dependencies
1660# using pip before running `setup.py develop`
1661#
1662# Updates the dependencies in project_dir from the
1663# openstack/requirements global list before installing anything.
1664#
1665# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1666# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001667function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001668 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001669 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001670
Dean Troyeraf616d92014-02-17 12:57:55 -06001671 # Don't update repo if local changes exist
1672 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001673 # ``errexit`` requires us to trap the exit code when the repo is changed
1674 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001675
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001676 if [[ $update_requirements != "changed" ]]; then
Sean Daguedef15342014-10-27 12:26:04 -04001677 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1678 if is_in_projects_txt $project_dir; then
1679 (cd $REQUIREMENTS_DIR; \
1680 python update.py $project_dir)
1681 else
1682 # soft update projects not found in requirements project.txt
1683 (cd $REQUIREMENTS_DIR; \
1684 python update.py -s $project_dir)
1685 fi
1686 else
1687 (cd $REQUIREMENTS_DIR; \
1688 python update.py $project_dir)
1689 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001690 fi
1691
Sean Dague099e5e32014-03-31 10:35:43 -04001692 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001693
1694 # We've just gone and possibly modified the user's source tree in an
1695 # automated way, which is considered bad form if it's a development
1696 # tree because we've screwed up their next git checkin. So undo it.
1697 #
1698 # However... there are some circumstances, like running in the gate
1699 # where we really really want the overridden version to stick. So provide
1700 # a variable that tells us whether or not we should UNDO the requirements
1701 # changes (this will be set to False in the OpenStack ci gate)
1702 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001703 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001704 (cd $project_dir && git reset --hard)
1705 fi
1706 fi
1707}
1708
1709# ``pip install -e`` the package, which processes the dependencies
1710# using pip before running `setup.py develop`
1711# Uses globals ``STACK_USER``
1712# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001713function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001714 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001715 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001716
Sean Dague099e5e32014-03-31 10:35:43 -04001717 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001718 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001719 if [[ "$flags" == "-e" ]]; then
1720 safe_chown -R $STACK_USER $1/*.egg-info
1721 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001722}
1723
Sean Dague2c65e712014-12-18 09:44:56 -05001724# Plugin Functions
1725# =================
1726
1727DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1728
1729# enable_plugin <name> <url> [branch]
1730#
1731# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1732# ``url`` is a git url
1733# ``branch`` is a gitref. If it's not set, defaults to master
1734function enable_plugin {
1735 local name=$1
1736 local url=$2
1737 local branch=${3:-master}
1738 DEVSTACK_PLUGINS+=",$name"
1739 GITREPO[$name]=$url
1740 GITDIR[$name]=$DEST/$name
1741 GITBRANCH[$name]=$branch
1742}
1743
1744# fetch_plugins
1745#
1746# clones all plugins
1747function fetch_plugins {
1748 local plugins="${DEVSTACK_PLUGINS}"
1749 local plugin
1750
1751 # short circuit if nothing to do
1752 if [[ -z $plugins ]]; then
1753 return
1754 fi
1755
1756 echo "Fetching devstack plugins"
1757 for plugin in ${plugins//,/ }; do
1758 git_clone_by_name $plugin
1759 done
1760}
1761
1762# load_plugin_settings
1763#
1764# Load settings from plugins in the order that they were registered
1765function load_plugin_settings {
1766 local plugins="${DEVSTACK_PLUGINS}"
1767 local plugin
1768
1769 # short circuit if nothing to do
1770 if [[ -z $plugins ]]; then
1771 return
1772 fi
1773
1774 echo "Loading plugin settings"
1775 for plugin in ${plugins//,/ }; do
1776 local dir=${GITDIR[$plugin]}
1777 # source any known settings
1778 if [[ -f $dir/devstack/settings ]]; then
1779 source $dir/devstack/settings
1780 fi
1781 done
1782}
1783
1784# run_plugins
1785#
1786# Run the devstack/plugin.sh in all the plugin directories. These are
1787# run in registration order.
1788function run_plugins {
1789 local mode=$1
1790 local phase=$2
1791 for plugin in ${plugins//,/ }; do
1792 local dir=${GITDIR[$plugin]}
1793 if [[ -f $dir/devstack/plugin.sh ]]; then
1794 source $dir/devstack/plugin.sh $mode $phase
1795 fi
1796 done
1797}
1798
1799function run_phase {
1800 local mode=$1
1801 local phase=$2
1802 if [[ -d $TOP_DIR/extras.d ]]; then
1803 for i in $TOP_DIR/extras.d/*.sh; do
1804 [[ -r $i ]] && source $i $mode $phase
1805 done
1806 fi
1807 # the source phase corresponds to settings loading in plugins
1808 if [[ "$mode" == "source" ]]; then
1809 load_plugin_settings
1810 else
1811 run_plugins $mode $phase
1812 fi
1813}
1814
Dean Troyerdff49a22014-01-30 15:37:40 -06001815
1816# Service Functions
1817# =================
1818
1819# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1820# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001821function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001822 echo "$1" | sed -e '
1823 s/,,/,/g;
1824 s/^,//;
1825 s/,$//
1826 '
1827}
1828
1829# disable_all_services() removes all current services
1830# from ``ENABLED_SERVICES`` to reset the configuration
1831# before a minimal installation
1832# Uses global ``ENABLED_SERVICES``
1833# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001834function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001835 ENABLED_SERVICES=""
1836}
1837
1838# Remove all services starting with '-'. For example, to install all default
1839# services except rabbit (rabbit) set in ``localrc``:
1840# ENABLED_SERVICES+=",-rabbit"
1841# Uses global ``ENABLED_SERVICES``
1842# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001843function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001844 local tmpsvcs="${ENABLED_SERVICES}"
1845 local service
1846 for service in ${tmpsvcs//,/ }; do
1847 if [[ ${service} == -* ]]; then
1848 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1849 fi
1850 done
1851 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1852}
1853
1854# disable_service() removes the services passed as argument to the
1855# ``ENABLED_SERVICES`` list, if they are present.
1856#
1857# For example:
1858# disable_service rabbit
1859#
1860# This function does not know about the special cases
1861# for nova, glance, and neutron built into is_service_enabled().
1862# Uses global ``ENABLED_SERVICES``
1863# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001864function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001865 local tmpsvcs=",${ENABLED_SERVICES},"
1866 local service
1867 for service in $@; do
1868 if is_service_enabled $service; then
1869 tmpsvcs=${tmpsvcs//,$service,/,}
1870 fi
1871 done
1872 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1873}
1874
1875# enable_service() adds the services passed as argument to the
1876# ``ENABLED_SERVICES`` list, if they are not already present.
1877#
1878# For example:
1879# enable_service qpid
1880#
1881# This function does not know about the special cases
1882# for nova, glance, and neutron built into is_service_enabled().
1883# Uses global ``ENABLED_SERVICES``
1884# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001885function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001886 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001887 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001888 for service in $@; do
1889 if ! is_service_enabled $service; then
1890 tmpsvcs+=",$service"
1891 fi
1892 done
1893 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1894 disable_negated_services
1895}
1896
1897# is_service_enabled() checks if the service(s) specified as arguments are
1898# enabled by the user in ``ENABLED_SERVICES``.
1899#
1900# Multiple services specified as arguments are ``OR``'ed together; the test
1901# is a short-circuit boolean, i.e it returns on the first match.
1902#
1903# There are special cases for some 'catch-all' services::
1904# **nova** returns true if any service enabled start with **n-**
1905# **cinder** returns true if any service enabled start with **c-**
1906# **ceilometer** returns true if any service enabled start with **ceilometer**
1907# **glance** returns true if any service enabled start with **g-**
1908# **neutron** returns true if any service enabled start with **q-**
1909# **swift** returns true if any service enabled start with **s-**
1910# **trove** returns true if any service enabled start with **tr-**
1911# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1912# **s-** services will be enabled. This will be deprecated in the future.
1913#
1914# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1915# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1916# as enabled in this case.
1917#
1918# Uses global ``ENABLED_SERVICES``
1919# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001920function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001921 local xtrace=$(set +o | grep xtrace)
1922 set +o xtrace
1923 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001924 local services=$@
1925 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001926 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001927 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001928
1929 # Look for top-level 'enabled' function for this service
1930 if type is_${service}_enabled >/dev/null 2>&1; then
1931 # A function exists for this service, use it
1932 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001933 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001934 fi
1935
1936 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1937 # are implemented
1938
Sean Dague45917cc2014-02-24 16:09:14 -05001939 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001940 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001941 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1942 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1943 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1944 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1945 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1946 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1947 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1948 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1949 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001950 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001951 done
Sean Dague45917cc2014-02-24 16:09:14 -05001952 $xtrace
1953 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001954}
1955
1956# Toggle enable/disable_service for services that must run exclusive of each other
1957# $1 The name of a variable containing a space-separated list of services
1958# $2 The name of a variable in which to store the enabled service's name
1959# $3 The name of the service to enable
1960function use_exclusive_service {
1961 local options=${!1}
1962 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001963 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001964 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001965 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001966 for opt in $options;do
1967 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1968 done
1969 eval "$out=$selection"
1970 return 0
1971}
1972
1973
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001974# System Functions
1975# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001976
1977# Only run the command if the target file (the last arg) is not on an
1978# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001979function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001980 local xtrace=$(set +o | grep xtrace)
1981 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001982 local args=( $@ )
1983 local last
1984 local sudo_cmd
1985 local dir_to_check
1986
1987 let last="${#args[*]} - 1"
1988
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001989 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001990 if [ ! -d "$dir_to_check" ]; then
1991 dir_to_check=`dirname "$dir_to_check"`
1992 fi
1993
1994 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001995 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001996 return 0
1997 fi
1998
1999 if [[ $TRACK_DEPENDS = True ]]; then
2000 sudo_cmd="env"
2001 else
2002 sudo_cmd="sudo"
2003 fi
2004
Sean Dague45917cc2014-02-24 16:09:14 -05002005 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002006 $sudo_cmd $@
2007}
2008
2009# Exit 0 if address is in network or 1 if address is not in network
2010# ip-range is in CIDR notation: 1.2.3.4/20
2011# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002012function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002013 local ip=$1
2014 local range=$2
2015 local masklen=${range#*/}
2016 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2017 local subnet=$(maskip $ip $(cidr2netmask $masklen))
2018 [[ $network == $subnet ]]
2019}
2020
2021# Add a user to a group.
2022# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002023function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002024 local user=$1
2025 local group=$2
2026
2027 if [[ -z "$os_VENDOR" ]]; then
2028 GetOSVersion
2029 fi
2030
2031 # SLE11 and openSUSE 12.2 don't have the usual usermod
2032 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
2033 sudo usermod -a -G "$group" "$user"
2034 else
2035 sudo usermod -A "$group" "$user"
2036 fi
2037}
2038
2039# Convert CIDR notation to a IPv4 netmask
2040# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002041function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002042 local maskpat="255 255 255 255"
2043 local maskdgt="254 252 248 240 224 192 128"
2044 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2045 echo ${1-0}.${2-0}.${3-0}.${4-0}
2046}
2047
2048# Gracefully cp only if source file/dir exists
2049# cp_it source destination
2050function cp_it {
2051 if [ -e $1 ] || [ -d $1 ]; then
2052 cp -pRL $1 $2
2053 fi
2054}
2055
2056# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2057# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2058# ``localrc`` or on the command line if necessary::
2059#
2060# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2061#
2062# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2063
Ian Wienandaee18c72014-02-21 15:35:08 +11002064function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06002065 if [[ -n "$http_proxy" ]]; then
2066 export http_proxy=$http_proxy
2067 fi
2068 if [[ -n "$https_proxy" ]]; then
2069 export https_proxy=$https_proxy
2070 fi
2071 if [[ -n "$no_proxy" ]]; then
2072 export no_proxy=$no_proxy
2073 fi
2074}
2075
2076# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002077function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06002078 local mount_type=`stat -f -L -c %T $1`
2079 test "$mount_type" == "nfs"
2080}
2081
2082# Return the network portion of the given IP address using netmask
2083# netmask is in the traditional dotted-quad format
2084# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002085function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002086 local ip=$1
2087 local mask=$2
2088 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2089 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2090 echo $subnet
2091}
2092
2093# Service wrapper to restart services
2094# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002095function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002096 if is_ubuntu; then
2097 sudo /usr/sbin/service $1 restart
2098 else
2099 sudo /sbin/service $1 restart
2100 fi
2101}
2102
2103# Only change permissions of a file or directory if it is not on an
2104# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002105function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002106 _safe_permission_operation chmod $@
2107}
2108
2109# Only change ownership of a file or directory if it is not on an NFS
2110# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002111function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002112 _safe_permission_operation chown $@
2113}
2114
2115# Service wrapper to start services
2116# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002117function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002118 if is_ubuntu; then
2119 sudo /usr/sbin/service $1 start
2120 else
2121 sudo /sbin/service $1 start
2122 fi
2123}
2124
2125# Service wrapper to stop services
2126# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002127function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002128 if is_ubuntu; then
2129 sudo /usr/sbin/service $1 stop
2130 else
2131 sudo /sbin/service $1 stop
2132 fi
2133}
2134
2135
2136# Restore xtrace
2137$XTRACE
2138
2139# Local variables:
2140# mode: shell-script
2141# End: