blob: 94ab34763edf52cc1b6a8dc22c5cfa81d6b2ca0e [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
48# Config Functions
49# ================
50
51# Append a new option in an ini file without replacing the old value
52# iniadd config-file section option value1 value2 value3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +110053function iniadd {
Sean Dague45917cc2014-02-24 16:09:14 -050054 local xtrace=$(set +o | grep xtrace)
55 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060056 local file=$1
57 local section=$2
58 local option=$3
59 shift 3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050060
Dean Troyerdff49a22014-01-30 15:37:40 -060061 local values="$(iniget_multiline $file $section $option) $@"
62 iniset_multiline $file $section $option $values
Sean Dague45917cc2014-02-24 16:09:14 -050063 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060064}
65
66# Comment an option in an INI file
67# inicomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110068function inicomment {
Sean Dague45917cc2014-02-24 16:09:14 -050069 local xtrace=$(set +o | grep xtrace)
70 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060071 local file=$1
72 local section=$2
73 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050074
Dean Troyerdff49a22014-01-30 15:37:40 -060075 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -050076 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060077}
78
79# Get an option from an INI file
80# iniget config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110081function iniget {
Sean Dague45917cc2014-02-24 16:09:14 -050082 local xtrace=$(set +o | grep xtrace)
83 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060084 local file=$1
85 local section=$2
86 local option=$3
87 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050088
Dean Troyerdff49a22014-01-30 15:37:40 -060089 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
90 echo ${line#*=}
Sean Dague45917cc2014-02-24 16:09:14 -050091 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060092}
93
94# Get a multiple line option from an INI file
95# iniget_multiline config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110096function iniget_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -050097 local xtrace=$(set +o | grep xtrace)
98 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060099 local file=$1
100 local section=$2
101 local option=$3
102 local values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500103
Dean Troyerdff49a22014-01-30 15:37:40 -0600104 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
105 echo ${values}
Sean Dague45917cc2014-02-24 16:09:14 -0500106 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600107}
108
109# Determinate is the given option present in the INI file
110# ini_has_option config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100111function ini_has_option {
Sean Dague45917cc2014-02-24 16:09:14 -0500112 local xtrace=$(set +o | grep xtrace)
113 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600114 local file=$1
115 local section=$2
116 local option=$3
117 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500118
Dean Troyerdff49a22014-01-30 15:37:40 -0600119 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Sean Dague45917cc2014-02-24 16:09:14 -0500120 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600121 [ -n "$line" ]
122}
123
Robert Li751ad1a2014-10-15 21:40:53 -0400124# Add another config line for a multi-line option.
125# It's normally called after iniset of the same option and assumes
126# that the section already exists.
127#
128# Note that iniset_multiline requires all the 'lines' to be supplied
129# in the argument list. Doing that will cause incorrect configuration
130# if spaces are used in the config values.
131#
132# iniadd_literal config-file section option value
133function iniadd_literal {
134 local xtrace=$(set +o | grep xtrace)
135 set +o xtrace
136 local file=$1
137 local section=$2
138 local option=$3
139 local value=$4
140
141 [[ -z $section || -z $option ]] && return
142
143 # Add it
144 sed -i -e "/^\[$section\]/ a\\
145$option = $value
146" "$file"
147
148 $xtrace
149}
150
Dean Troyerdff49a22014-01-30 15:37:40 -0600151# Set an option in an INI file
152# iniset config-file section option value
Ian Wienandaee18c72014-02-21 15:35:08 +1100153function iniset {
Sean Dague45917cc2014-02-24 16:09:14 -0500154 local xtrace=$(set +o | grep xtrace)
155 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600156 local file=$1
157 local section=$2
158 local option=$3
159 local value=$4
160
161 [[ -z $section || -z $option ]] && return
162
163 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
164 # Add section at the end
165 echo -e "\n[$section]" >>"$file"
166 fi
167 if ! ini_has_option "$file" "$section" "$option"; then
168 # Add it
169 sed -i -e "/^\[$section\]/ a\\
170$option = $value
171" "$file"
172 else
173 local sep=$(echo -ne "\x01")
174 # Replace it
175 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
176 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500177 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600178}
179
180# Set a multiple line option in an INI file
181# iniset_multiline config-file section option value1 value2 valu3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +1100182function iniset_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -0500183 local xtrace=$(set +o | grep xtrace)
184 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600185 local file=$1
186 local section=$2
187 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500188
Dean Troyerdff49a22014-01-30 15:37:40 -0600189 shift 3
190 local values
191 for v in $@; do
192 # The later sed command inserts each new value in the line next to
193 # the section identifier, which causes the values to be inserted in
194 # the reverse order. Do a reverse here to keep the original order.
195 values="$v ${values}"
196 done
197 if ! grep -q "^\[$section\]" "$file"; then
198 # Add section at the end
199 echo -e "\n[$section]" >>"$file"
200 else
201 # Remove old values
202 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
203 fi
204 # Add new ones
205 for v in $values; do
206 sed -i -e "/^\[$section\]/ a\\
207$option = $v
208" "$file"
209 done
Sean Dague45917cc2014-02-24 16:09:14 -0500210 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600211}
212
213# Uncomment an option in an INI file
214# iniuncomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100215function iniuncomment {
Sean Dague45917cc2014-02-24 16:09:14 -0500216 local xtrace=$(set +o | grep xtrace)
217 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600218 local file=$1
219 local section=$2
220 local option=$3
221 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500222 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600223}
224
225# Normalize config values to True or False
226# Accepts as False: 0 no No NO false False FALSE
227# Accepts as True: 1 yes Yes YES true True TRUE
228# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100229function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500230 local xtrace=$(set +o | grep xtrace)
231 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600232 local default=$1
233 local testval=$2
234
235 [[ -z "$testval" ]] && { echo "$default"; return; }
236 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
237 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
238 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500239 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600240}
241
242
243# Control Functions
244# =================
245
246# Prints backtrace info
247# filename:lineno:function
248# backtrace level
249function backtrace {
250 local level=$1
251 local deep=$((${#BASH_SOURCE[@]} - 1))
252 echo "[Call Trace]"
253 while [ $level -le $deep ]; do
254 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
255 deep=$((deep - 1))
256 done
257}
258
259# Prints line number and "message" then exits
260# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100261function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600262 local exitcode=$?
263 set +o xtrace
264 local line=$1; shift
265 if [ $exitcode == 0 ]; then
266 exitcode=1
267 fi
268 backtrace 2
269 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600270 # Give buffers a second to flush
271 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600272 exit $exitcode
273}
274
275# Checks an environment variable is not set or has length 0 OR if the
276# exit code is non-zero and prints "message" and exits
277# NOTE: env-var is the variable name without a '$'
278# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100279function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600280 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500281 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600282 set +o xtrace
283 local line=$1; shift
284 local evar=$1; shift
285 if ! is_set $evar || [ $exitcode != 0 ]; then
286 die $line "$*"
287 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500288 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600289}
290
291# Prints line number and "message" in error format
292# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100293function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600294 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500295 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600296 set +o xtrace
297 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
298 echo $msg 1>&2;
299 if [[ -n ${SCREEN_LOGDIR} ]]; then
300 echo $msg >> "${SCREEN_LOGDIR}/error.log"
301 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500302 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600303 return $exitcode
304}
305
306# Checks an environment variable is not set or has length 0 OR if the
307# exit code is non-zero and prints "message"
308# NOTE: env-var is the variable name without a '$'
309# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100310function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600311 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500312 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600313 set +o xtrace
314 local line=$1; shift
315 local evar=$1; shift
316 if ! is_set $evar || [ $exitcode != 0 ]; then
317 err $line "$*"
318 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500319 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600320 return $exitcode
321}
322
323# Exit after outputting a message about the distribution not being supported.
324# exit_distro_not_supported [optional-string-telling-what-is-missing]
325function exit_distro_not_supported {
326 if [[ -z "$DISTRO" ]]; then
327 GetDistro
328 fi
329
330 if [ $# -gt 0 ]; then
331 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
332 else
333 die $LINENO "Support for $DISTRO is incomplete."
334 fi
335}
336
337# Test if the named environment variable is set and not zero length
338# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100339function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600340 local var=\$"$1"
341 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
342}
343
344# Prints line number and "message" in warning format
345# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100346function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600347 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500348 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600349 set +o xtrace
350 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
351 echo $msg 1>&2;
352 if [[ -n ${SCREEN_LOGDIR} ]]; then
353 echo $msg >> "${SCREEN_LOGDIR}/error.log"
354 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500355 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600356 return $exitcode
357}
358
359
360# Distro Functions
361# ================
362
363# Determine OS Vendor, Release and Update
364# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
365# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500366# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
367# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
368# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
369# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
370# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
371declare os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
372
Dean Troyerdff49a22014-01-30 15:37:40 -0600373# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100374function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500375
Dean Troyerdff49a22014-01-30 15:37:40 -0600376 # Figure out which vendor we are
377 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
378 # OS/X
379 os_VENDOR=`sw_vers -productName`
380 os_RELEASE=`sw_vers -productVersion`
381 os_UPDATE=${os_RELEASE##*.}
382 os_RELEASE=${os_RELEASE%.*}
383 os_PACKAGE=""
384 if [[ "$os_RELEASE" =~ "10.7" ]]; then
385 os_CODENAME="lion"
386 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
387 os_CODENAME="snow leopard"
388 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
389 os_CODENAME="leopard"
390 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
391 os_CODENAME="tiger"
392 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
393 os_CODENAME="panther"
394 else
395 os_CODENAME=""
396 fi
397 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
398 os_VENDOR=$(lsb_release -i -s)
399 os_RELEASE=$(lsb_release -r -s)
400 os_UPDATE=""
401 os_PACKAGE="rpm"
402 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
403 os_PACKAGE="deb"
404 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
405 lsb_release -d -s | grep -q openSUSE
406 if [[ $? -eq 0 ]]; then
407 os_VENDOR="openSUSE"
408 fi
409 elif [[ $os_VENDOR == "openSUSE project" ]]; then
410 os_VENDOR="openSUSE"
411 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
412 os_VENDOR="Red Hat"
413 fi
414 os_CODENAME=$(lsb_release -c -s)
415 elif [[ -r /etc/redhat-release ]]; then
416 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
417 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
418 # CentOS release 5.5 (Final)
419 # CentOS Linux release 6.0 (Final)
420 # Fedora release 16 (Verne)
421 # XenServer release 6.2.0-70446c (xenenterprise)
422 os_CODENAME=""
423 for r in "Red Hat" CentOS Fedora XenServer; do
424 os_VENDOR=$r
425 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
426 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
427 os_CODENAME=${ver#*|}
428 os_RELEASE=${ver%|*}
429 os_UPDATE=${os_RELEASE##*.}
430 os_RELEASE=${os_RELEASE%.*}
431 break
432 fi
433 os_VENDOR=""
434 done
435 os_PACKAGE="rpm"
436 elif [[ -r /etc/SuSE-release ]]; then
437 for r in openSUSE "SUSE Linux"; do
438 if [[ "$r" = "SUSE Linux" ]]; then
439 os_VENDOR="SUSE LINUX"
440 else
441 os_VENDOR=$r
442 fi
443
444 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
445 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
446 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
447 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
448 break
449 fi
450 os_VENDOR=""
451 done
452 os_PACKAGE="rpm"
453 # If lsb_release is not installed, we should be able to detect Debian OS
454 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
455 os_VENDOR="Debian"
456 os_PACKAGE="deb"
457 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
458 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
459 fi
460 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
461}
462
463# Translate the OS version values into common nomenclature
464# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500465declare DISTRO
466
Ian Wienandaee18c72014-02-21 15:35:08 +1100467function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600468 GetOSVersion
469 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
470 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
471 DISTRO=$os_CODENAME
472 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
473 # For Fedora, just use 'f' and the release
474 DISTRO="f$os_RELEASE"
475 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
476 DISTRO="opensuse-$os_RELEASE"
477 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
478 # For SLE, also use the service pack
479 if [[ -z "$os_UPDATE" ]]; then
480 DISTRO="sle${os_RELEASE}"
481 else
482 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
483 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530484 elif [[ "$os_VENDOR" =~ (Red Hat) || \
485 "$os_VENDOR" =~ (CentOS) || \
486 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600487 # Drop the . release as we assume it's compatible
488 DISTRO="rhel${os_RELEASE::1}"
489 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
490 DISTRO="xs$os_RELEASE"
491 else
492 # Catch-all for now is Vendor + Release + Update
493 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
494 fi
495 export DISTRO
496}
497
498# Utility function for checking machine architecture
499# is_arch arch-type
500function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500501 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600502}
503
Ian Wienandbdc90c52014-08-04 15:44:58 +1000504# Quick check for a rackspace host; n.b. rackspace provided images
505# have these Xen tools installed but a custom image may not.
506function is_rackspace {
507 [ -f /usr/bin/xenstore-ls ] && \
508 sudo /usr/bin/xenstore-ls vm-data | grep -q "Rackspace"
509}
510
Dean Troyerdff49a22014-01-30 15:37:40 -0600511# Determine if current distribution is a Fedora-based distribution
512# (Fedora, RHEL, CentOS, etc).
513# is_fedora
514function is_fedora {
515 if [[ -z "$os_VENDOR" ]]; then
516 GetOSVersion
517 fi
518
anju Tiwari6c639c92014-07-15 18:11:54 +0530519 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
520 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600521}
522
523
524# Determine if current distribution is a SUSE-based distribution
525# (openSUSE, SLE).
526# is_suse
527function is_suse {
528 if [[ -z "$os_VENDOR" ]]; then
529 GetOSVersion
530 fi
531
532 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
533}
534
535
536# Determine if current distribution is an Ubuntu-based distribution
537# It will also detect non-Ubuntu but Debian-based distros
538# is_ubuntu
539function is_ubuntu {
540 if [[ -z "$os_PACKAGE" ]]; then
541 GetOSVersion
542 fi
543 [ "$os_PACKAGE" = "deb" ]
544}
545
546
547# Git Functions
548# =============
549
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600550# Returns openstack release name for a given branch name
551# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100552function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600553 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700554 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600555 echo ${branch#*/}
556 else
557 echo "master"
558 fi
559}
560
Dean Troyerdff49a22014-01-30 15:37:40 -0600561# git clone only if directory doesn't exist already. Since ``DEST`` might not
562# be owned by the installation user, we create the directory and change the
563# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500564# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
565# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600566# does not exist (default is False, meaning the repo will be cloned).
Jamie Lennox51f0de52014-10-20 16:32:34 +0200567# Set global ``GIT_DEPTH=<number>`` to limit the history depth of the git clone
568# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``, ``GIT_DEPTH``
Dean Troyerdff49a22014-01-30 15:37:40 -0600569# git_clone remote dest-dir branch
570function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500571 local git_remote=$1
572 local git_dest=$2
573 local git_ref=$3
574 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200575 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500576
Dean Troyerdff49a22014-01-30 15:37:40 -0600577 RECLONE=$(trueorfalse False $RECLONE)
578
YAMAMOTO Takashi292b2a72014-10-31 13:48:58 +0900579 if [[ -n "${GIT_DEPTH}" ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200580 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
581 fi
582
Dean Troyerdff49a22014-01-30 15:37:40 -0600583 if [[ "$OFFLINE" = "True" ]]; then
584 echo "Running in offline mode, clones already exist"
585 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500586 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600587 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400588 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600589 return
590 fi
591
Dean Troyer50cda692014-07-25 11:57:20 -0500592 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600593 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500594 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600595 [[ "$ERROR_ON_CLONE" = "True" ]] && \
596 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200597 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600598 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500599 cd $git_dest
600 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600601 else
602 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500603 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600604 [[ "$ERROR_ON_CLONE" = "True" ]] && \
605 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200606 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500607 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600608 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500609 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600610 elif [[ "$RECLONE" = "True" ]]; then
611 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500612 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600613 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500614 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100615 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600616 # remove the existing ignored files (like pyc) as they cause breakage
617 # (due to the py files having older timestamps than our pyc, so python
618 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500619 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600620
Dean Troyer50cda692014-07-25 11:57:20 -0500621 # handle git_ref accordingly to type (tag, branch)
622 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
623 git_update_tag $git_ref
624 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
625 git_update_branch $git_ref
626 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
627 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600628 else
Dean Troyer50cda692014-07-25 11:57:20 -0500629 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600630 fi
631
632 fi
633 fi
634
635 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500636 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600637 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400638 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600639}
640
Sean Daguecc524062014-10-01 09:06:43 -0400641# A variation on git clone that lets us specify a project by it's
642# actual name, like oslo.config. This is exceptionally useful in the
643# library installation case
644function git_clone_by_name {
645 local name=$1
646 local repo=${GITREPO[$name]}
647 local dir=${GITDIR[$name]}
648 local branch=${GITBRANCH[$name]}
649 git_clone $repo $dir $branch
650}
651
652
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100653# git can sometimes get itself infinitely stuck with transient network
654# errors or other issues with the remote end. This wraps git in a
655# timeout/retry loop and is intended to watch over non-local git
656# processes that might hang. GIT_TIMEOUT, if set, is passed directly
657# to timeout(1); otherwise the default value of 0 maintains the status
658# quo of waiting forever.
659# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100660function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100661 local count=0
662 local timeout=0
663
664 if [[ -n "${GIT_TIMEOUT}" ]]; then
665 timeout=${GIT_TIMEOUT}
666 fi
667
668 until timeout -s SIGINT ${timeout} git "$@"; do
669 # 124 is timeout(1)'s special return code when it reached the
670 # timeout; otherwise assume fatal failure
671 if [[ $? -ne 124 ]]; then
672 die $LINENO "git call failed: [git $@]"
673 fi
674
675 count=$(($count + 1))
676 warn "timeout ${count} for git call: [git $@]"
677 if [ $count -eq 3 ]; then
678 die $LINENO "Maximum of 3 git retries reached"
679 fi
680 sleep 5
681 done
682}
683
Dean Troyerdff49a22014-01-30 15:37:40 -0600684# git update using reference as a branch.
685# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100686function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500687 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600688
Dean Troyer50cda692014-07-25 11:57:20 -0500689 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600690 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500691 git branch -D $git_branch || true
692 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600693}
694
695# git update using reference as a branch.
696# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100697function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500698 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600699
Dean Troyer50cda692014-07-25 11:57:20 -0500700 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600701}
702
703# git update using reference as a tag. Be careful editing source at that repo
704# as working copy will be in a detached mode
705# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100706function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500707 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600708
Dean Troyer50cda692014-07-25 11:57:20 -0500709 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600710 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500711 git_timed fetch origin tag $git_tag
712 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600713}
714
715
716# OpenStack Functions
717# ===================
718
719# Get the default value for HOST_IP
720# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100721function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600722 local fixed_range=$1
723 local floating_range=$2
724 local host_ip_iface=$3
725 local host_ip=$4
726
727 # Find the interface used for the default route
728 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
729 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
730 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
731 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500732 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
733 local ip
734 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600735 # Attempt to filter out IP addresses that are part of the fixed and
736 # floating range. Note that this method only works if the ``netaddr``
737 # python library is installed. If it is not installed, an error
738 # will be printed and the first IP from the interface will be used.
739 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
740 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500741 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
742 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600743 break;
744 fi
745 done
746 fi
747 echo $host_ip
748}
749
Attila Fazekasf71b5002014-05-28 09:52:22 +0200750# Generates hex string from ``size`` byte of pseudo random data
751# generate_hex_string size
752function generate_hex_string {
753 local size=$1
754 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
755}
756
Dean Troyerdff49a22014-01-30 15:37:40 -0600757# Grab a numbered field from python prettytable output
758# Fields are numbered starting with 1
759# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
760# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100761function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500762 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600763 while read data; do
764 if [ "$1" -lt 0 ]; then
765 field="(\$(NF$1))"
766 else
767 field="\$$(($1 + 1))"
768 fi
769 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
770 done
771}
772
773# Add a policy to a policy.json file
774# Do nothing if the policy already exists
775# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100776function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600777 local policy_file=$1
778 local policy_name=$2
779 local policy_perm=$3
780
781 if grep -q ${policy_name} ${policy_file}; then
782 echo "Policy ${policy_name} already exists in ${policy_file}"
783 return
784 fi
785
786 # Add a terminating comma to policy lines without one
787 # Remove the closing '}' and all lines following to the end-of-file
788 local tmpfile=$(mktemp)
789 uniq ${policy_file} | sed -e '
790 s/]$/],/
791 /^[}]/,$d
792 ' > ${tmpfile}
793
794 # Append policy and closing brace
795 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
796 echo "}" >>${tmpfile}
797
798 mv ${tmpfile} ${policy_file}
799}
800
Alistair Coles24779f62014-10-15 18:57:59 +0100801# Gets or creates a domain
802# Usage: get_or_create_domain <name> <description>
803function get_or_create_domain {
804 local os_url="$KEYSTONE_SERVICE_URI/v3"
805 # Gets domain id
806 local domain_id=$(
807 # Gets domain id
808 openstack --os-token=$OS_TOKEN --os-url=$os_url \
809 --os-identity-api-version=3 domain show $1 \
810 -f value -c id 2>/dev/null ||
811 # Creates new domain
812 openstack --os-token=$OS_TOKEN --os-url=$os_url \
813 --os-identity-api-version=3 domain create $1 \
814 --description "$2" \
815 -f value -c id
816 )
817 echo $domain_id
818}
819
Bartosz Górski0abde392014-02-28 14:15:19 +0100820# Gets or creates user
Alistair Coles24779f62014-10-15 18:57:59 +0100821# Usage: get_or_create_user <username> <password> <project> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100822function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200823 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500824 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200825 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500826 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200827 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100828 local os_cmd="openstack"
829 local domain=""
830 if [[ ! -z "$5" ]]; then
831 domain="--domain=$5"
832 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
833 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100834 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500835 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500836 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100837 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100838 $1 \
839 --password "$2" \
840 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500841 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100842 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500843 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100844 -f value -c id
845 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500846 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100847}
848
849# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100850# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100851function get_or_create_project {
852 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100853 local os_cmd="openstack"
854 local domain=""
855 if [[ ! -z "$2" ]]; then
856 domain="--domain=$2"
857 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
858 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500859 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500860 # Creates new project with --or-show
861 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100862 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500863 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100864}
865
866# Gets or creates role
867# Usage: get_or_create_role <name>
868function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500869 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500870 # Creates role with --or-show
871 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100872 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500873 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100874}
875
876# Gets or adds user role
877# Usage: get_or_add_user_role <role> <user> <project>
878function get_or_add_user_role {
879 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500880 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100881 $2 \
882 --project $3 \
883 --column "ID" \
884 --column "Name" \
885 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500886 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100887 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500888 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100889 $1 \
890 --user $2 \
891 --project $3 \
892 | grep " id " | get_field 2)
893 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500894 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100895}
896
897# Gets or creates service
898# Usage: get_or_create_service <name> <type> <description>
899function get_or_create_service {
900 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500901 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100902 # Gets service id
903 openstack service show $1 -f value -c id 2>/dev/null ||
904 # Creates new service if not exists
905 openstack service create \
906 $1 \
907 --type=$2 \
908 --description="$3" \
909 -f value -c id
910 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500911 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100912}
913
914# Gets or creates endpoint
915# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
916function get_or_create_endpoint {
917 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500918 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100919 --column "ID" \
920 --column "Region" \
921 --column "Service Name" \
922 | grep " $2 " \
923 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500924 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100925 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500926 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100927 $1 \
928 --region $2 \
929 --publicurl $3 \
930 --adminurl $4 \
931 --internalurl $5 \
932 | grep " id " | get_field 2)
933 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500934 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100935}
Dean Troyerdff49a22014-01-30 15:37:40 -0600936
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500937
Dean Troyerdff49a22014-01-30 15:37:40 -0600938# Package Functions
939# =================
940
941# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100942function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600943 local pkg_dir
944 if is_ubuntu; then
Monty Taylor81a016d2014-11-15 17:18:13 -0300945 pkg_dir=$FILES/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600946 elif is_fedora; then
947 pkg_dir=$FILES/rpms
948 elif is_suse; then
949 pkg_dir=$FILES/rpms-suse
950 else
951 exit_distro_not_supported "list of packages"
952 fi
953 echo "$pkg_dir"
954}
955
956# Wrapper for ``apt-get`` to set cache and proxy environment variables
957# Uses globals ``OFFLINE``, ``*_proxy``
958# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100959function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500960 local xtrace=$(set +o | grep xtrace)
961 set +o xtrace
962
Dean Troyerdff49a22014-01-30 15:37:40 -0600963 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
964 local sudo="sudo"
965 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500966
967 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600968 $sudo DEBIAN_FRONTEND=noninteractive \
969 http_proxy=$http_proxy https_proxy=$https_proxy \
970 no_proxy=$no_proxy \
971 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
972}
973
974# get_packages() collects a list of package names of any type from the
Monty Taylor81a016d2014-11-15 17:18:13 -0300975# prerequisite files in ``files/{debs|rpms}``. The list is intended
Dean Troyerdff49a22014-01-30 15:37:40 -0600976# to be passed to a package installer such as apt or yum.
977#
978# Only packages required for the services in 1st argument will be
979# included. Two bits of metadata are recognized in the prerequisite files:
980#
981# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
982# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
983# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100984function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500985 local xtrace=$(set +o | grep xtrace)
986 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600987 local services=$@
988 local package_dir=$(_get_package_dir)
989 local file_to_parse
990 local service
991
Flavio Percoco5a91c352014-10-31 18:48:00 +0100992 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
993
Dean Troyerdff49a22014-01-30 15:37:40 -0600994 if [[ -z "$package_dir" ]]; then
995 echo "No package directory supplied"
996 return 1
997 fi
998 if [[ -z "$DISTRO" ]]; then
999 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -05001000 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -06001001 fi
1002 for service in ${services//,/ }; do
1003 # Allow individual services to specify dependencies
1004 if [[ -e ${package_dir}/${service} ]]; then
1005 file_to_parse="${file_to_parse} $service"
1006 fi
1007 # NOTE(sdague) n-api needs glance for now because that's where
1008 # glance client is
1009 if [[ $service == n-api ]]; then
1010 if [[ ! $file_to_parse =~ nova ]]; then
1011 file_to_parse="${file_to_parse} nova"
1012 fi
1013 if [[ ! $file_to_parse =~ glance ]]; then
1014 file_to_parse="${file_to_parse} glance"
1015 fi
1016 elif [[ $service == c-* ]]; then
1017 if [[ ! $file_to_parse =~ cinder ]]; then
1018 file_to_parse="${file_to_parse} cinder"
1019 fi
1020 elif [[ $service == ceilometer-* ]]; then
1021 if [[ ! $file_to_parse =~ ceilometer ]]; then
1022 file_to_parse="${file_to_parse} ceilometer"
1023 fi
1024 elif [[ $service == s-* ]]; then
1025 if [[ ! $file_to_parse =~ swift ]]; then
1026 file_to_parse="${file_to_parse} swift"
1027 fi
1028 elif [[ $service == n-* ]]; then
1029 if [[ ! $file_to_parse =~ nova ]]; then
1030 file_to_parse="${file_to_parse} nova"
1031 fi
1032 elif [[ $service == g-* ]]; then
1033 if [[ ! $file_to_parse =~ glance ]]; then
1034 file_to_parse="${file_to_parse} glance"
1035 fi
1036 elif [[ $service == key* ]]; then
1037 if [[ ! $file_to_parse =~ keystone ]]; then
1038 file_to_parse="${file_to_parse} keystone"
1039 fi
1040 elif [[ $service == q-* ]]; then
1041 if [[ ! $file_to_parse =~ neutron ]]; then
1042 file_to_parse="${file_to_parse} neutron"
1043 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001044 elif [[ $service == ir-* ]]; then
1045 if [[ ! $file_to_parse =~ ironic ]]; then
1046 file_to_parse="${file_to_parse} ironic"
1047 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001048 fi
1049 done
1050
1051 for file in ${file_to_parse}; do
1052 local fname=${package_dir}/${file}
1053 local OIFS line package distros distro
1054 [[ -e $fname ]] || continue
1055
1056 OIFS=$IFS
1057 IFS=$'\n'
1058 for line in $(<${fname}); do
1059 if [[ $line =~ "NOPRIME" ]]; then
1060 continue
1061 fi
1062
1063 # Assume we want this package
1064 package=${line%#*}
1065 inst_pkg=1
1066
1067 # Look for # dist:xxx in comment
1068 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1069 # We are using BASH regexp matching feature.
1070 package=${BASH_REMATCH[1]}
1071 distros=${BASH_REMATCH[2]}
1072 # In bash ${VAR,,} will lowecase VAR
1073 # Look for a match in the distro list
1074 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1075 # If no match then skip this package
1076 inst_pkg=0
1077 fi
1078 fi
1079
1080 # Look for # testonly in comment
1081 if [[ $line =~ (.*)#.*testonly.* ]]; then
1082 package=${BASH_REMATCH[1]}
1083 # Are we installing test packages? (test for the default value)
1084 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1085 # If not installing test packages the skip this package
1086 inst_pkg=0
1087 fi
1088 fi
1089
1090 if [[ $inst_pkg = 1 ]]; then
1091 echo $package
1092 fi
1093 done
1094 IFS=$OIFS
1095 done
Sean Dague45917cc2014-02-24 16:09:14 -05001096 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001097}
1098
1099# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001100# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001101# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001102function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001103 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001104 return 0
1105 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001106
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001107 if is_ubuntu; then
1108 local xtrace=$(set +o | grep xtrace)
1109 set +o xtrace
1110 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1111 # if there are transient errors pulling the updates, that's fine.
1112 # It may be secondary repositories that we don't really care about.
1113 apt_get update || /bin/true
1114 REPOS_UPDATED=True
1115 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001116 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001117 fi
1118}
1119
1120function real_install_package {
1121 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001122 apt_get install "$@"
1123 elif is_fedora; then
1124 yum_install "$@"
1125 elif is_suse; then
1126 zypper_install "$@"
1127 else
1128 exit_distro_not_supported "installing packages"
1129 fi
1130}
1131
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001132# Distro-agnostic package installer
1133# install_package package [package ...]
1134function install_package {
1135 update_package_repo
1136 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1137}
1138
Dean Troyerdff49a22014-01-30 15:37:40 -06001139# Distro-agnostic function to tell if a package is installed
1140# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001141function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001142 if [[ -z "$@" ]]; then
1143 return 1
1144 fi
1145
1146 if [[ -z "$os_PACKAGE" ]]; then
1147 GetOSVersion
1148 fi
1149
1150 if [[ "$os_PACKAGE" = "deb" ]]; then
1151 dpkg -s "$@" > /dev/null 2> /dev/null
1152 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1153 rpm --quiet -q "$@"
1154 else
1155 exit_distro_not_supported "finding if a package is installed"
1156 fi
1157}
1158
1159# Distro-agnostic package uninstaller
1160# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001161function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001162 if is_ubuntu; then
1163 apt_get purge "$@"
1164 elif is_fedora; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001165 sudo $YUM remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001166 elif is_suse; then
1167 sudo zypper rm "$@"
1168 else
1169 exit_distro_not_supported "uninstalling packages"
1170 fi
1171}
1172
1173# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001174# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001175# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001176function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001177 [[ "$OFFLINE" = "True" ]] && return
1178 local sudo="sudo"
1179 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001180
1181 # The manual check for missing packages is because yum -y assumes
1182 # missing packages are OK. See
1183 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001184 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1185 no_proxy=$no_proxy \
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001186 $YUM install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001187 awk '
1188 BEGIN { fail=0 }
1189 /No package/ { fail=1 }
1190 { print }
1191 END { exit fail }' || \
1192 die $LINENO "Missing packages detected"
1193
1194 # also ensure we catch a yum failure
1195 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001196 die $LINENO "$YUM install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001197 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001198}
1199
1200# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001201# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001202# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001203function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001204 [[ "$OFFLINE" = "True" ]] && return
1205 local sudo="sudo"
1206 [[ "$(id -u)" = "0" ]] && sudo="env"
1207 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1208 zypper --non-interactive install --auto-agree-with-licenses "$@"
1209}
1210
1211
1212# Process Functions
1213# =================
1214
1215# _run_process() is designed to be backgrounded by run_process() to simulate a
1216# fork. It includes the dirty work of closing extra filehandles and preparing log
1217# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001218# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001219# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001220# If an optional group is provided sg will be used to set the group of
1221# the command.
1222# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001223function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001224 local service=$1
1225 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001226 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001227
1228 # Undo logging redirections and close the extra descriptors
1229 exec 1>&3
1230 exec 2>&3
1231 exec 3>&-
1232 exec 6>&-
1233
1234 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001235 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1236 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001237
1238 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1239 export PYTHONUNBUFFERED=1
1240 fi
1241
Dean Troyer3159a822014-08-27 14:13:58 -05001242 # Run under ``setsid`` to force the process to become a session and group leader.
1243 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001244 if [[ -n "$group" ]]; then
1245 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1246 else
1247 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1248 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001249
1250 # Just silently exit this process
1251 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001252}
1253
1254# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1255# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001256# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001257# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001258function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001259 SCREEN_NAME=${SCREEN_NAME:-stack}
1260 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1261
1262 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1263 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1264 fi
1265
1266 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1267}
1268
1269# Find out if a process exists by partial name.
1270# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001271function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001272 local name=$1
1273 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001274 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001275 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001276 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001277}
1278
Dean Troyer3159a822014-08-27 14:13:58 -05001279# Run a single service under screen or directly
1280# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001281# If an optional group is provided sg will be used to run the
1282# command as that group.
1283# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001284function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001285 local service=$1
1286 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001287 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001288
Dean Troyer3159a822014-08-27 14:13:58 -05001289 if is_service_enabled $service; then
1290 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001291 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001292 else
1293 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001294 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001295 fi
1296 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001297}
1298
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001299# Helper to launch a process in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001300# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1301# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001302# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001303# Run a command in a shell in a screen window, if an optional group
1304# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001305function screen_process {
1306 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001307 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001308 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001309
Sean Dagueea22a4f2014-06-27 15:21:41 -04001310 SCREEN_NAME=${SCREEN_NAME:-stack}
1311 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1312 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001313
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001314 # Append the process to the screen rc file
1315 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001316
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001317 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001318
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001319 if [[ -n ${SCREEN_LOGDIR} ]]; then
1320 screen -S $SCREEN_NAME -p $name -X logfile ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log
1321 screen -S $SCREEN_NAME -p $name -X log on
1322 ln -sf ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${name}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001323 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001324
1325 # sleep to allow bash to be ready to be send the command - we are
1326 # creating a new window in screen and then sends characters, so if
1327 # bash isn't running by the time we send the command, nothing happens
1328 sleep 3
1329
1330 NL=`echo -ne '\015'`
1331 # This fun command does the following:
1332 # - the passed server command is backgrounded
1333 # - the pid of the background process is saved in the usual place
1334 # - the server process is brought back to the foreground
1335 # - if the server process exits prematurely the fg command errors
1336 # and a message is written to stdout and the process failure file
1337 #
1338 # The pid saved can be used in stop_process() as a process group
1339 # id to kill off all child processes
1340 if [[ -n "$group" ]]; then
1341 command="sg $group '$command'"
1342 fi
1343 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 -06001344}
1345
1346# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001347# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001348# screen_rc service "command-line"
1349function screen_rc {
1350 SCREEN_NAME=${SCREEN_NAME:-stack}
1351 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1352 if [[ ! -e $SCREENRC ]]; then
1353 # Name the screen session
1354 echo "sessionname $SCREEN_NAME" > $SCREENRC
1355 # Set a reasonable statusbar
1356 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1357 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1358 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1359 echo "screen -t shell bash" >> $SCREENRC
1360 fi
1361 # If this service doesn't already exist in the screenrc file
1362 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1363 NL=`echo -ne '\015'`
1364 echo "screen -t $1 bash" >> $SCREENRC
1365 echo "stuff \"$2$NL\"" >> $SCREENRC
1366
1367 if [[ -n ${SCREEN_LOGDIR} ]]; then
1368 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1369 echo "log on" >>$SCREENRC
1370 fi
1371 fi
1372}
1373
1374# Stop a service in screen
1375# If a PID is available use it, kill the whole process group via TERM
1376# If screen is being used kill the screen window; this will catch processes
1377# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001378# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001379# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001380function screen_stop_service {
1381 local service=$1
1382
Dean Troyerdff49a22014-01-30 15:37:40 -06001383 SCREEN_NAME=${SCREEN_NAME:-stack}
1384 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1385 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1386
Dean Troyer3159a822014-08-27 14:13:58 -05001387 if is_service_enabled $service; then
1388 # Clean up the screen window
1389 screen -S $SCREEN_NAME -p $service -X kill
1390 fi
1391}
1392
1393# Stop a service process
1394# If a PID is available use it, kill the whole process group via TERM
1395# If screen is being used kill the screen window; this will catch processes
1396# that did not leave a PID behind
1397# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1398# stop_process service
1399function stop_process {
1400 local service=$1
1401
1402 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1403 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1404
1405 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001406 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001407 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1408 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1409 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001410 fi
1411 if [[ "$USE_SCREEN" = "True" ]]; then
1412 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001413 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001414 fi
1415 fi
1416}
1417
1418# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001419# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001420# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001421function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001422 local service
1423 local failures
1424 SCREEN_NAME=${SCREEN_NAME:-stack}
1425 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1426
1427
1428 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1429 echo "No service status directory found"
1430 return
1431 fi
1432
1433 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001434 # make this -o errexit safe
1435 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001436
1437 for service in $failures; do
1438 service=`basename $service`
1439 service=${service%.failure}
1440 echo "Error: Service $service is not running"
1441 done
1442
1443 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001444 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001445 fi
1446}
1447
Chris Dent2f27a0e2014-09-09 13:46:02 +01001448# Tail a log file in a screen if USE_SCREEN is true.
1449function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001450 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001451 local logfile=$2
1452
1453 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1454 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001455 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001456 fi
1457}
1458
Dean Troyerdff49a22014-01-30 15:37:40 -06001459
Dean Troyer3159a822014-08-27 14:13:58 -05001460# Deprecated Functions
1461# --------------------
1462
1463# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1464# fork. It includes the dirty work of closing extra filehandles and preparing log
1465# files to produce the same logs as screen_it(). The log filename is derived
1466# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1467# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1468# _old_run_process service "command-line"
1469function _old_run_process {
1470 local service=$1
1471 local command="$2"
1472
1473 # Undo logging redirections and close the extra descriptors
1474 exec 1>&3
1475 exec 2>&3
1476 exec 3>&-
1477 exec 6>&-
1478
1479 if [[ -n ${SCREEN_LOGDIR} ]]; then
1480 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1481 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1482
1483 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1484 export PYTHONUNBUFFERED=1
1485 fi
1486
1487 exec /bin/bash -c "$command"
1488 die "$service exec failure: $command"
1489}
1490
1491# old_run_process() launches a child process that closes all file descriptors and
1492# then exec's the passed in command. This is meant to duplicate the semantics
1493# of screen_it() without screen. PIDs are written to
1494# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1495# old_run_process service "command-line"
1496function old_run_process {
1497 local service=$1
1498 local command="$2"
1499
1500 # Spawn the child process
1501 _old_run_process "$service" "$command" &
1502 echo $!
1503}
1504
1505# Compatibility for existing start_XXXX() functions
1506# Uses global ``USE_SCREEN``
1507# screen_it service "command-line"
1508function screen_it {
1509 if is_service_enabled $1; then
1510 # Append the service to the screen rc file
1511 screen_rc "$1" "$2"
1512
1513 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001514 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001515 else
1516 # Spawn directly without screen
1517 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1518 fi
1519 fi
1520}
1521
1522# Compatibility for existing stop_XXXX() functions
1523# Stop a service in screen
1524# If a PID is available use it, kill the whole process group via TERM
1525# If screen is being used kill the screen window; this will catch processes
1526# that did not leave a PID behind
1527# screen_stop service
1528function screen_stop {
1529 # Clean up the screen window
1530 stop_process $1
1531}
1532
1533
Dean Troyerdff49a22014-01-30 15:37:40 -06001534# Python Functions
1535# ================
1536
1537# Get the path to the pip command.
1538# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001539function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001540 which pip || which pip-python
1541
1542 if [ $? -ne 0 ]; then
1543 die $LINENO "Unable to find pip; cannot continue"
1544 fi
1545}
1546
1547# Get the path to the direcotry where python executables are installed.
1548# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001549function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001550 if is_fedora || is_suse; then
1551 echo "/usr/bin"
1552 else
1553 echo "/usr/local/bin"
1554 fi
1555}
1556
1557# Wrapper for ``pip install`` to set cache and proxy environment variables
Attila Fazekasaf81d672014-11-10 09:04:54 +01001558# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``,
Dean Troyerdff49a22014-01-30 15:37:40 -06001559# ``TRACK_DEPENDS``, ``*_proxy``
1560# pip_install package [package ...]
1561function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001562 local xtrace=$(set +o | grep xtrace)
1563 set +o xtrace
1564 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1565 $xtrace
1566 return
1567 fi
1568
Dean Troyerdff49a22014-01-30 15:37:40 -06001569 if [[ -z "$os_PACKAGE" ]]; then
1570 GetOSVersion
1571 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001572 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1573 # TRACK_DEPENDS=True installation creates a circular dependency when
1574 # we attempt to install virtualenv into a virualenv, so we must global
1575 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001576 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001577 local cmd_pip=$DEST/.venv/bin/pip
1578 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001579 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001580 local cmd_pip=$(get_pip_command)
1581 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001582 fi
1583
Sean Dague45917cc2014-02-24 16:09:14 -05001584 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001585 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001586 http_proxy=$http_proxy \
1587 https_proxy=$https_proxy \
1588 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001589 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001590 $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001591
Flavio Percoco5a91c352014-10-31 18:48:00 +01001592 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001593 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1594 local test_req="$@/test-requirements.txt"
1595 if [[ -e "$test_req" ]]; then
1596 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1597 http_proxy=$http_proxy \
1598 https_proxy=$https_proxy \
1599 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001600 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001601 -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001602 fi
1603 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001604}
1605
Sean Daguecc524062014-10-01 09:06:43 -04001606# should we use this library from their git repo, or should we let it
1607# get pulled in via pip dependencies.
1608function use_library_from_git {
1609 local name=$1
1610 local enabled=1
1611 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1612 return $enabled
1613}
1614
1615# setup a library by name. If we are trying to use the library from
1616# git, we'll do a git based install, otherwise we'll punt and the
1617# library should be installed by a requirements pull from another
1618# project.
1619function setup_lib {
1620 local name=$1
1621 local dir=${GITDIR[$name]}
1622 setup_install $dir
1623}
1624
Sean Daguee08ab102014-11-13 17:09:28 -05001625# setup a library by name in editiable mode. If we are trying to use
1626# the library from git, we'll do a git based install, otherwise we'll
1627# punt and the library should be installed by a requirements pull from
1628# another project.
1629#
1630# use this for non namespaced libraries
1631function setup_dev_lib {
1632 local name=$1
1633 local dir=${GITDIR[$name]}
1634 setup_develop $dir
1635}
Sean Daguecc524062014-10-01 09:06:43 -04001636
Sean Dague099e5e32014-03-31 10:35:43 -04001637# this should be used if you want to install globally, all libraries should
1638# use this, especially *oslo* ones
1639function setup_install {
1640 local project_dir=$1
1641 setup_package_with_req_sync $project_dir
1642}
1643
1644# this should be used for projects which run services, like all services
1645function setup_develop {
1646 local project_dir=$1
1647 setup_package_with_req_sync $project_dir -e
1648}
1649
Sean Daguedef15342014-10-27 12:26:04 -04001650# determine if a project as specified by directory is in
1651# projects.txt. This will not be an exact match because we throw away
1652# the namespacing when we clone, but it should be good enough in all
1653# practical ways.
1654function is_in_projects_txt {
1655 local project_dir=$1
1656 local project_name=$(basename $project_dir)
1657 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1658}
1659
Dean Troyeraf616d92014-02-17 12:57:55 -06001660# ``pip install -e`` the package, which processes the dependencies
1661# using pip before running `setup.py develop`
1662#
1663# Updates the dependencies in project_dir from the
1664# openstack/requirements global list before installing anything.
1665#
1666# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1667# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001668function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001669 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001670 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001671
Dean Troyeraf616d92014-02-17 12:57:55 -06001672 # Don't update repo if local changes exist
1673 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001674 # ``errexit`` requires us to trap the exit code when the repo is changed
1675 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001676
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001677 if [[ $update_requirements != "changed" ]]; then
Sean Daguedef15342014-10-27 12:26:04 -04001678 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1679 if is_in_projects_txt $project_dir; then
1680 (cd $REQUIREMENTS_DIR; \
1681 python update.py $project_dir)
1682 else
1683 # soft update projects not found in requirements project.txt
1684 (cd $REQUIREMENTS_DIR; \
1685 python update.py -s $project_dir)
1686 fi
1687 else
1688 (cd $REQUIREMENTS_DIR; \
1689 python update.py $project_dir)
1690 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001691 fi
1692
Sean Dague099e5e32014-03-31 10:35:43 -04001693 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001694
1695 # We've just gone and possibly modified the user's source tree in an
1696 # automated way, which is considered bad form if it's a development
1697 # tree because we've screwed up their next git checkin. So undo it.
1698 #
1699 # However... there are some circumstances, like running in the gate
1700 # where we really really want the overridden version to stick. So provide
1701 # a variable that tells us whether or not we should UNDO the requirements
1702 # changes (this will be set to False in the OpenStack ci gate)
1703 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001704 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001705 (cd $project_dir && git reset --hard)
1706 fi
1707 fi
1708}
1709
1710# ``pip install -e`` the package, which processes the dependencies
1711# using pip before running `setup.py develop`
1712# Uses globals ``STACK_USER``
1713# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001714function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001715 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001716 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001717
Sean Dague099e5e32014-03-31 10:35:43 -04001718 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001719 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001720 if [[ "$flags" == "-e" ]]; then
1721 safe_chown -R $STACK_USER $1/*.egg-info
1722 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001723}
1724
Dean Troyerdff49a22014-01-30 15:37:40 -06001725
1726# Service Functions
1727# =================
1728
1729# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1730# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001731function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001732 echo "$1" | sed -e '
1733 s/,,/,/g;
1734 s/^,//;
1735 s/,$//
1736 '
1737}
1738
1739# disable_all_services() removes all current services
1740# from ``ENABLED_SERVICES`` to reset the configuration
1741# before a minimal installation
1742# Uses global ``ENABLED_SERVICES``
1743# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001744function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001745 ENABLED_SERVICES=""
1746}
1747
1748# Remove all services starting with '-'. For example, to install all default
1749# services except rabbit (rabbit) set in ``localrc``:
1750# ENABLED_SERVICES+=",-rabbit"
1751# Uses global ``ENABLED_SERVICES``
1752# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001753function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001754 local tmpsvcs="${ENABLED_SERVICES}"
1755 local service
1756 for service in ${tmpsvcs//,/ }; do
1757 if [[ ${service} == -* ]]; then
1758 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1759 fi
1760 done
1761 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1762}
1763
1764# disable_service() removes the services passed as argument to the
1765# ``ENABLED_SERVICES`` list, if they are present.
1766#
1767# For example:
1768# disable_service rabbit
1769#
1770# This function does not know about the special cases
1771# for nova, glance, and neutron built into is_service_enabled().
1772# Uses global ``ENABLED_SERVICES``
1773# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001774function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001775 local tmpsvcs=",${ENABLED_SERVICES},"
1776 local service
1777 for service in $@; do
1778 if is_service_enabled $service; then
1779 tmpsvcs=${tmpsvcs//,$service,/,}
1780 fi
1781 done
1782 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1783}
1784
1785# enable_service() adds the services passed as argument to the
1786# ``ENABLED_SERVICES`` list, if they are not already present.
1787#
1788# For example:
1789# enable_service qpid
1790#
1791# This function does not know about the special cases
1792# for nova, glance, and neutron built into is_service_enabled().
1793# Uses global ``ENABLED_SERVICES``
1794# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001795function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001796 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001797 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001798 for service in $@; do
1799 if ! is_service_enabled $service; then
1800 tmpsvcs+=",$service"
1801 fi
1802 done
1803 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1804 disable_negated_services
1805}
1806
1807# is_service_enabled() checks if the service(s) specified as arguments are
1808# enabled by the user in ``ENABLED_SERVICES``.
1809#
1810# Multiple services specified as arguments are ``OR``'ed together; the test
1811# is a short-circuit boolean, i.e it returns on the first match.
1812#
1813# There are special cases for some 'catch-all' services::
1814# **nova** returns true if any service enabled start with **n-**
1815# **cinder** returns true if any service enabled start with **c-**
1816# **ceilometer** returns true if any service enabled start with **ceilometer**
1817# **glance** returns true if any service enabled start with **g-**
1818# **neutron** returns true if any service enabled start with **q-**
1819# **swift** returns true if any service enabled start with **s-**
1820# **trove** returns true if any service enabled start with **tr-**
1821# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1822# **s-** services will be enabled. This will be deprecated in the future.
1823#
1824# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1825# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1826# as enabled in this case.
1827#
1828# Uses global ``ENABLED_SERVICES``
1829# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001830function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001831 local xtrace=$(set +o | grep xtrace)
1832 set +o xtrace
1833 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001834 local services=$@
1835 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001836 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001837 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001838
1839 # Look for top-level 'enabled' function for this service
1840 if type is_${service}_enabled >/dev/null 2>&1; then
1841 # A function exists for this service, use it
1842 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001843 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001844 fi
1845
1846 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1847 # are implemented
1848
Sean Dague45917cc2014-02-24 16:09:14 -05001849 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001850 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001851 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1852 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1853 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1854 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1855 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1856 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1857 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1858 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1859 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001860 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001861 done
Sean Dague45917cc2014-02-24 16:09:14 -05001862 $xtrace
1863 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001864}
1865
1866# Toggle enable/disable_service for services that must run exclusive of each other
1867# $1 The name of a variable containing a space-separated list of services
1868# $2 The name of a variable in which to store the enabled service's name
1869# $3 The name of the service to enable
1870function use_exclusive_service {
1871 local options=${!1}
1872 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001873 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001874 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001875 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001876 for opt in $options;do
1877 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1878 done
1879 eval "$out=$selection"
1880 return 0
1881}
1882
1883
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001884# System Functions
1885# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001886
1887# Only run the command if the target file (the last arg) is not on an
1888# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001889function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001890 local xtrace=$(set +o | grep xtrace)
1891 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001892 local args=( $@ )
1893 local last
1894 local sudo_cmd
1895 local dir_to_check
1896
1897 let last="${#args[*]} - 1"
1898
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001899 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001900 if [ ! -d "$dir_to_check" ]; then
1901 dir_to_check=`dirname "$dir_to_check"`
1902 fi
1903
1904 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001905 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001906 return 0
1907 fi
1908
1909 if [[ $TRACK_DEPENDS = True ]]; then
1910 sudo_cmd="env"
1911 else
1912 sudo_cmd="sudo"
1913 fi
1914
Sean Dague45917cc2014-02-24 16:09:14 -05001915 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001916 $sudo_cmd $@
1917}
1918
1919# Exit 0 if address is in network or 1 if address is not in network
1920# ip-range is in CIDR notation: 1.2.3.4/20
1921# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001922function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001923 local ip=$1
1924 local range=$2
1925 local masklen=${range#*/}
1926 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1927 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1928 [[ $network == $subnet ]]
1929}
1930
1931# Add a user to a group.
1932# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001933function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001934 local user=$1
1935 local group=$2
1936
1937 if [[ -z "$os_VENDOR" ]]; then
1938 GetOSVersion
1939 fi
1940
1941 # SLE11 and openSUSE 12.2 don't have the usual usermod
1942 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1943 sudo usermod -a -G "$group" "$user"
1944 else
1945 sudo usermod -A "$group" "$user"
1946 fi
1947}
1948
1949# Convert CIDR notation to a IPv4 netmask
1950# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001951function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001952 local maskpat="255 255 255 255"
1953 local maskdgt="254 252 248 240 224 192 128"
1954 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1955 echo ${1-0}.${2-0}.${3-0}.${4-0}
1956}
1957
1958# Gracefully cp only if source file/dir exists
1959# cp_it source destination
1960function cp_it {
1961 if [ -e $1 ] || [ -d $1 ]; then
1962 cp -pRL $1 $2
1963 fi
1964}
1965
1966# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1967# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1968# ``localrc`` or on the command line if necessary::
1969#
1970# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1971#
1972# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1973
Ian Wienandaee18c72014-02-21 15:35:08 +11001974function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001975 if [[ -n "$http_proxy" ]]; then
1976 export http_proxy=$http_proxy
1977 fi
1978 if [[ -n "$https_proxy" ]]; then
1979 export https_proxy=$https_proxy
1980 fi
1981 if [[ -n "$no_proxy" ]]; then
1982 export no_proxy=$no_proxy
1983 fi
1984}
1985
1986# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001987function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001988 local mount_type=`stat -f -L -c %T $1`
1989 test "$mount_type" == "nfs"
1990}
1991
1992# Return the network portion of the given IP address using netmask
1993# netmask is in the traditional dotted-quad format
1994# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11001995function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06001996 local ip=$1
1997 local mask=$2
1998 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1999 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2000 echo $subnet
2001}
2002
2003# Service wrapper to restart services
2004# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002005function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002006 if is_ubuntu; then
2007 sudo /usr/sbin/service $1 restart
2008 else
2009 sudo /sbin/service $1 restart
2010 fi
2011}
2012
2013# Only change permissions of a file or directory if it is not on an
2014# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002015function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002016 _safe_permission_operation chmod $@
2017}
2018
2019# Only change ownership of a file or directory if it is not on an NFS
2020# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002021function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002022 _safe_permission_operation chown $@
2023}
2024
2025# Service wrapper to start services
2026# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002027function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002028 if is_ubuntu; then
2029 sudo /usr/sbin/service $1 start
2030 else
2031 sudo /sbin/service $1 start
2032 fi
2033}
2034
2035# Service wrapper to stop services
2036# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002037function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002038 if is_ubuntu; then
2039 sudo /usr/sbin/service $1 stop
2040 else
2041 sudo /sbin/service $1 stop
2042 fi
2043}
2044
2045
2046# Restore xtrace
2047$XTRACE
2048
2049# Local variables:
2050# mode: shell-script
2051# End: