blob: cbfa130fd1a227f4bf91a4458a02a8b0bf62a6cf [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=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100836 # Gets user id
Alistair Coles24779f62014-10-15 18:57:59 +0100837 $os_cmd user show $1 $domain -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100838 # Creates new user
Alistair Coles24779f62014-10-15 18:57:59 +0100839 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100840 $1 \
841 --password "$2" \
842 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500843 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100844 $domain \
Bartosz Górski0abde392014-02-28 14:15:19 +0100845 -f value -c id
846 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500847 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100848}
849
850# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100851# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100852function get_or_create_project {
853 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100854 local os_cmd="openstack"
855 local domain=""
856 if [[ ! -z "$2" ]]; then
857 domain="--domain=$2"
858 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
859 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500860 local project_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100861 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100862 $os_cmd project show $1 $domain -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100863 # Creates new project if not exists
Alistair Coles24779f62014-10-15 18:57:59 +0100864 $os_cmd project create $1 $domain -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100865 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500866 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100867}
868
869# Gets or creates role
870# Usage: get_or_create_role <name>
871function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500872 local role_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100873 # Gets role id
874 openstack role show $1 -f value -c id 2>/dev/null ||
875 # Creates role if not exists
876 openstack role create $1 -f value -c id
877 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500878 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100879}
880
881# Gets or adds user role
882# Usage: get_or_add_user_role <role> <user> <project>
883function get_or_add_user_role {
884 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500885 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100886 $2 \
887 --project $3 \
888 --column "ID" \
889 --column "Name" \
890 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500891 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100892 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500893 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100894 $1 \
895 --user $2 \
896 --project $3 \
897 | grep " id " | get_field 2)
898 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500899 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100900}
901
902# Gets or creates service
903# Usage: get_or_create_service <name> <type> <description>
904function get_or_create_service {
905 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500906 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100907 # Gets service id
908 openstack service show $1 -f value -c id 2>/dev/null ||
909 # Creates new service if not exists
910 openstack service create \
911 $1 \
912 --type=$2 \
913 --description="$3" \
914 -f value -c id
915 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500916 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100917}
918
919# Gets or creates endpoint
920# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
921function get_or_create_endpoint {
922 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500923 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100924 --column "ID" \
925 --column "Region" \
926 --column "Service Name" \
927 | grep " $2 " \
928 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500929 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100930 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500931 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100932 $1 \
933 --region $2 \
934 --publicurl $3 \
935 --adminurl $4 \
936 --internalurl $5 \
937 | grep " id " | get_field 2)
938 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500939 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100940}
Dean Troyerdff49a22014-01-30 15:37:40 -0600941
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500942
Dean Troyerdff49a22014-01-30 15:37:40 -0600943# Package Functions
944# =================
945
946# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100947function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600948 local pkg_dir
949 if is_ubuntu; then
Monty Taylor81a016d2014-11-15 17:18:13 -0300950 pkg_dir=$FILES/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600951 elif is_fedora; then
952 pkg_dir=$FILES/rpms
953 elif is_suse; then
954 pkg_dir=$FILES/rpms-suse
955 else
956 exit_distro_not_supported "list of packages"
957 fi
958 echo "$pkg_dir"
959}
960
961# Wrapper for ``apt-get`` to set cache and proxy environment variables
962# Uses globals ``OFFLINE``, ``*_proxy``
963# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100964function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500965 local xtrace=$(set +o | grep xtrace)
966 set +o xtrace
967
Dean Troyerdff49a22014-01-30 15:37:40 -0600968 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
969 local sudo="sudo"
970 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500971
972 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600973 $sudo DEBIAN_FRONTEND=noninteractive \
974 http_proxy=$http_proxy https_proxy=$https_proxy \
975 no_proxy=$no_proxy \
976 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
977}
978
979# get_packages() collects a list of package names of any type from the
Monty Taylor81a016d2014-11-15 17:18:13 -0300980# prerequisite files in ``files/{debs|rpms}``. The list is intended
Dean Troyerdff49a22014-01-30 15:37:40 -0600981# to be passed to a package installer such as apt or yum.
982#
983# Only packages required for the services in 1st argument will be
984# included. Two bits of metadata are recognized in the prerequisite files:
985#
986# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
987# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
988# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100989function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500990 local xtrace=$(set +o | grep xtrace)
991 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600992 local services=$@
993 local package_dir=$(_get_package_dir)
994 local file_to_parse
995 local service
996
Flavio Percoco5a91c352014-10-31 18:48:00 +0100997 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
998
Dean Troyerdff49a22014-01-30 15:37:40 -0600999 if [[ -z "$package_dir" ]]; then
1000 echo "No package directory supplied"
1001 return 1
1002 fi
1003 if [[ -z "$DISTRO" ]]; then
1004 GetDistro
Sean Dague45917cc2014-02-24 16:09:14 -05001005 echo "Found Distro $DISTRO"
Dean Troyerdff49a22014-01-30 15:37:40 -06001006 fi
1007 for service in ${services//,/ }; do
1008 # Allow individual services to specify dependencies
1009 if [[ -e ${package_dir}/${service} ]]; then
1010 file_to_parse="${file_to_parse} $service"
1011 fi
1012 # NOTE(sdague) n-api needs glance for now because that's where
1013 # glance client is
1014 if [[ $service == n-api ]]; then
1015 if [[ ! $file_to_parse =~ nova ]]; then
1016 file_to_parse="${file_to_parse} nova"
1017 fi
1018 if [[ ! $file_to_parse =~ glance ]]; then
1019 file_to_parse="${file_to_parse} glance"
1020 fi
1021 elif [[ $service == c-* ]]; then
1022 if [[ ! $file_to_parse =~ cinder ]]; then
1023 file_to_parse="${file_to_parse} cinder"
1024 fi
1025 elif [[ $service == ceilometer-* ]]; then
1026 if [[ ! $file_to_parse =~ ceilometer ]]; then
1027 file_to_parse="${file_to_parse} ceilometer"
1028 fi
1029 elif [[ $service == s-* ]]; then
1030 if [[ ! $file_to_parse =~ swift ]]; then
1031 file_to_parse="${file_to_parse} swift"
1032 fi
1033 elif [[ $service == n-* ]]; then
1034 if [[ ! $file_to_parse =~ nova ]]; then
1035 file_to_parse="${file_to_parse} nova"
1036 fi
1037 elif [[ $service == g-* ]]; then
1038 if [[ ! $file_to_parse =~ glance ]]; then
1039 file_to_parse="${file_to_parse} glance"
1040 fi
1041 elif [[ $service == key* ]]; then
1042 if [[ ! $file_to_parse =~ keystone ]]; then
1043 file_to_parse="${file_to_parse} keystone"
1044 fi
1045 elif [[ $service == q-* ]]; then
1046 if [[ ! $file_to_parse =~ neutron ]]; then
1047 file_to_parse="${file_to_parse} neutron"
1048 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001049 elif [[ $service == ir-* ]]; then
1050 if [[ ! $file_to_parse =~ ironic ]]; then
1051 file_to_parse="${file_to_parse} ironic"
1052 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001053 fi
1054 done
1055
1056 for file in ${file_to_parse}; do
1057 local fname=${package_dir}/${file}
1058 local OIFS line package distros distro
1059 [[ -e $fname ]] || continue
1060
1061 OIFS=$IFS
1062 IFS=$'\n'
1063 for line in $(<${fname}); do
1064 if [[ $line =~ "NOPRIME" ]]; then
1065 continue
1066 fi
1067
1068 # Assume we want this package
1069 package=${line%#*}
1070 inst_pkg=1
1071
1072 # Look for # dist:xxx in comment
1073 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1074 # We are using BASH regexp matching feature.
1075 package=${BASH_REMATCH[1]}
1076 distros=${BASH_REMATCH[2]}
1077 # In bash ${VAR,,} will lowecase VAR
1078 # Look for a match in the distro list
1079 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1080 # If no match then skip this package
1081 inst_pkg=0
1082 fi
1083 fi
1084
1085 # Look for # testonly in comment
1086 if [[ $line =~ (.*)#.*testonly.* ]]; then
1087 package=${BASH_REMATCH[1]}
1088 # Are we installing test packages? (test for the default value)
1089 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1090 # If not installing test packages the skip this package
1091 inst_pkg=0
1092 fi
1093 fi
1094
1095 if [[ $inst_pkg = 1 ]]; then
1096 echo $package
1097 fi
1098 done
1099 IFS=$OIFS
1100 done
Sean Dague45917cc2014-02-24 16:09:14 -05001101 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001102}
1103
1104# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001105# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001106# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001107function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001108 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001109 return 0
1110 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001111
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001112 if is_ubuntu; then
1113 local xtrace=$(set +o | grep xtrace)
1114 set +o xtrace
1115 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1116 # if there are transient errors pulling the updates, that's fine.
1117 # It may be secondary repositories that we don't really care about.
1118 apt_get update || /bin/true
1119 REPOS_UPDATED=True
1120 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001121 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001122 fi
1123}
1124
1125function real_install_package {
1126 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001127 apt_get install "$@"
1128 elif is_fedora; then
1129 yum_install "$@"
1130 elif is_suse; then
1131 zypper_install "$@"
1132 else
1133 exit_distro_not_supported "installing packages"
1134 fi
1135}
1136
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001137# Distro-agnostic package installer
1138# install_package package [package ...]
1139function install_package {
1140 update_package_repo
1141 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1142}
1143
Dean Troyerdff49a22014-01-30 15:37:40 -06001144# Distro-agnostic function to tell if a package is installed
1145# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001146function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001147 if [[ -z "$@" ]]; then
1148 return 1
1149 fi
1150
1151 if [[ -z "$os_PACKAGE" ]]; then
1152 GetOSVersion
1153 fi
1154
1155 if [[ "$os_PACKAGE" = "deb" ]]; then
1156 dpkg -s "$@" > /dev/null 2> /dev/null
1157 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1158 rpm --quiet -q "$@"
1159 else
1160 exit_distro_not_supported "finding if a package is installed"
1161 fi
1162}
1163
1164# Distro-agnostic package uninstaller
1165# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001166function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001167 if is_ubuntu; then
1168 apt_get purge "$@"
1169 elif is_fedora; then
1170 sudo yum remove -y "$@"
1171 elif is_suse; then
1172 sudo zypper rm "$@"
1173 else
1174 exit_distro_not_supported "uninstalling packages"
1175 fi
1176}
1177
1178# Wrapper for ``yum`` to set proxy environment variables
1179# Uses globals ``OFFLINE``, ``*_proxy``
1180# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001181function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001182 [[ "$OFFLINE" = "True" ]] && return
1183 local sudo="sudo"
1184 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001185
1186 # The manual check for missing packages is because yum -y assumes
1187 # missing packages are OK. See
1188 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001189 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1190 no_proxy=$no_proxy \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001191 yum install -y "$@" 2>&1 | \
1192 awk '
1193 BEGIN { fail=0 }
1194 /No package/ { fail=1 }
1195 { print }
1196 END { exit fail }' || \
1197 die $LINENO "Missing packages detected"
1198
1199 # also ensure we catch a yum failure
1200 if [[ ${PIPESTATUS[0]} != 0 ]]; then
1201 die $LINENO "Yum install failure"
1202 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001203}
1204
1205# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001206# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001207# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001208function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001209 [[ "$OFFLINE" = "True" ]] && return
1210 local sudo="sudo"
1211 [[ "$(id -u)" = "0" ]] && sudo="env"
1212 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1213 zypper --non-interactive install --auto-agree-with-licenses "$@"
1214}
1215
1216
1217# Process Functions
1218# =================
1219
1220# _run_process() is designed to be backgrounded by run_process() to simulate a
1221# fork. It includes the dirty work of closing extra filehandles and preparing log
1222# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001223# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001224# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001225# If an optional group is provided sg will be used to set the group of
1226# the command.
1227# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001228function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001229 local service=$1
1230 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001231 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001232
1233 # Undo logging redirections and close the extra descriptors
1234 exec 1>&3
1235 exec 2>&3
1236 exec 3>&-
1237 exec 6>&-
1238
1239 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001240 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1241 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001242
1243 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1244 export PYTHONUNBUFFERED=1
1245 fi
1246
Dean Troyer3159a822014-08-27 14:13:58 -05001247 # Run under ``setsid`` to force the process to become a session and group leader.
1248 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001249 if [[ -n "$group" ]]; then
1250 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1251 else
1252 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1253 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001254
1255 # Just silently exit this process
1256 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001257}
1258
1259# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1260# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001261# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001262# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001263function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001264 SCREEN_NAME=${SCREEN_NAME:-stack}
1265 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1266
1267 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1268 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1269 fi
1270
1271 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1272}
1273
1274# Find out if a process exists by partial name.
1275# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001276function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001277 local name=$1
1278 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001279 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001280 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001281 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001282}
1283
Dean Troyer3159a822014-08-27 14:13:58 -05001284# Run a single service under screen or directly
1285# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001286# If an optional group is provided sg will be used to run the
1287# command as that group.
1288# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001289function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001290 local service=$1
1291 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001292 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001293
Dean Troyer3159a822014-08-27 14:13:58 -05001294 if is_service_enabled $service; then
1295 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001296 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001297 else
1298 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001299 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001300 fi
1301 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001302}
1303
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001304# Helper to launch a process in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001305# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1306# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001307# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001308# Run a command in a shell in a screen window, if an optional group
1309# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001310function screen_process {
1311 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001312 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001313 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001314
Sean Dagueea22a4f2014-06-27 15:21:41 -04001315 SCREEN_NAME=${SCREEN_NAME:-stack}
1316 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1317 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001318
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001319 # Append the process to the screen rc file
1320 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001321
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001322 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001323
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001324 if [[ -n ${SCREEN_LOGDIR} ]]; then
1325 screen -S $SCREEN_NAME -p $name -X logfile ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log
1326 screen -S $SCREEN_NAME -p $name -X log on
1327 ln -sf ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${name}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001328 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001329
1330 # sleep to allow bash to be ready to be send the command - we are
1331 # creating a new window in screen and then sends characters, so if
1332 # bash isn't running by the time we send the command, nothing happens
1333 sleep 3
1334
1335 NL=`echo -ne '\015'`
1336 # This fun command does the following:
1337 # - the passed server command is backgrounded
1338 # - the pid of the background process is saved in the usual place
1339 # - the server process is brought back to the foreground
1340 # - if the server process exits prematurely the fg command errors
1341 # and a message is written to stdout and the process failure file
1342 #
1343 # The pid saved can be used in stop_process() as a process group
1344 # id to kill off all child processes
1345 if [[ -n "$group" ]]; then
1346 command="sg $group '$command'"
1347 fi
1348 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 -06001349}
1350
1351# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001352# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001353# screen_rc service "command-line"
1354function screen_rc {
1355 SCREEN_NAME=${SCREEN_NAME:-stack}
1356 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1357 if [[ ! -e $SCREENRC ]]; then
1358 # Name the screen session
1359 echo "sessionname $SCREEN_NAME" > $SCREENRC
1360 # Set a reasonable statusbar
1361 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1362 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1363 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1364 echo "screen -t shell bash" >> $SCREENRC
1365 fi
1366 # If this service doesn't already exist in the screenrc file
1367 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1368 NL=`echo -ne '\015'`
1369 echo "screen -t $1 bash" >> $SCREENRC
1370 echo "stuff \"$2$NL\"" >> $SCREENRC
1371
1372 if [[ -n ${SCREEN_LOGDIR} ]]; then
1373 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1374 echo "log on" >>$SCREENRC
1375 fi
1376 fi
1377}
1378
1379# Stop a service in screen
1380# If a PID is available use it, kill the whole process group via TERM
1381# If screen is being used kill the screen window; this will catch processes
1382# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001383# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001384# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001385function screen_stop_service {
1386 local service=$1
1387
Dean Troyerdff49a22014-01-30 15:37:40 -06001388 SCREEN_NAME=${SCREEN_NAME:-stack}
1389 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1390 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1391
Dean Troyer3159a822014-08-27 14:13:58 -05001392 if is_service_enabled $service; then
1393 # Clean up the screen window
1394 screen -S $SCREEN_NAME -p $service -X kill
1395 fi
1396}
1397
1398# Stop a service process
1399# If a PID is available use it, kill the whole process group via TERM
1400# If screen is being used kill the screen window; this will catch processes
1401# that did not leave a PID behind
1402# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1403# stop_process service
1404function stop_process {
1405 local service=$1
1406
1407 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1408 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1409
1410 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001411 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001412 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1413 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1414 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001415 fi
1416 if [[ "$USE_SCREEN" = "True" ]]; then
1417 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001418 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001419 fi
1420 fi
1421}
1422
1423# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001424# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001425# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001426function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001427 local service
1428 local failures
1429 SCREEN_NAME=${SCREEN_NAME:-stack}
1430 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1431
1432
1433 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1434 echo "No service status directory found"
1435 return
1436 fi
1437
1438 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001439 # make this -o errexit safe
1440 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001441
1442 for service in $failures; do
1443 service=`basename $service`
1444 service=${service%.failure}
1445 echo "Error: Service $service is not running"
1446 done
1447
1448 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001449 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001450 fi
1451}
1452
Chris Dent2f27a0e2014-09-09 13:46:02 +01001453# Tail a log file in a screen if USE_SCREEN is true.
1454function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001455 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001456 local logfile=$2
1457
1458 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1459 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001460 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001461 fi
1462}
1463
Dean Troyerdff49a22014-01-30 15:37:40 -06001464
Dean Troyer3159a822014-08-27 14:13:58 -05001465# Deprecated Functions
1466# --------------------
1467
1468# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1469# fork. It includes the dirty work of closing extra filehandles and preparing log
1470# files to produce the same logs as screen_it(). The log filename is derived
1471# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1472# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1473# _old_run_process service "command-line"
1474function _old_run_process {
1475 local service=$1
1476 local command="$2"
1477
1478 # Undo logging redirections and close the extra descriptors
1479 exec 1>&3
1480 exec 2>&3
1481 exec 3>&-
1482 exec 6>&-
1483
1484 if [[ -n ${SCREEN_LOGDIR} ]]; then
1485 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1486 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1487
1488 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1489 export PYTHONUNBUFFERED=1
1490 fi
1491
1492 exec /bin/bash -c "$command"
1493 die "$service exec failure: $command"
1494}
1495
1496# old_run_process() launches a child process that closes all file descriptors and
1497# then exec's the passed in command. This is meant to duplicate the semantics
1498# of screen_it() without screen. PIDs are written to
1499# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1500# old_run_process service "command-line"
1501function old_run_process {
1502 local service=$1
1503 local command="$2"
1504
1505 # Spawn the child process
1506 _old_run_process "$service" "$command" &
1507 echo $!
1508}
1509
1510# Compatibility for existing start_XXXX() functions
1511# Uses global ``USE_SCREEN``
1512# screen_it service "command-line"
1513function screen_it {
1514 if is_service_enabled $1; then
1515 # Append the service to the screen rc file
1516 screen_rc "$1" "$2"
1517
1518 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001519 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001520 else
1521 # Spawn directly without screen
1522 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1523 fi
1524 fi
1525}
1526
1527# Compatibility for existing stop_XXXX() functions
1528# Stop a service in screen
1529# If a PID is available use it, kill the whole process group via TERM
1530# If screen is being used kill the screen window; this will catch processes
1531# that did not leave a PID behind
1532# screen_stop service
1533function screen_stop {
1534 # Clean up the screen window
1535 stop_process $1
1536}
1537
1538
Dean Troyerdff49a22014-01-30 15:37:40 -06001539# Python Functions
1540# ================
1541
1542# Get the path to the pip command.
1543# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001544function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001545 which pip || which pip-python
1546
1547 if [ $? -ne 0 ]; then
1548 die $LINENO "Unable to find pip; cannot continue"
1549 fi
1550}
1551
1552# Get the path to the direcotry where python executables are installed.
1553# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001554function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001555 if is_fedora || is_suse; then
1556 echo "/usr/bin"
1557 else
1558 echo "/usr/local/bin"
1559 fi
1560}
1561
1562# Wrapper for ``pip install`` to set cache and proxy environment variables
Attila Fazekasaf81d672014-11-10 09:04:54 +01001563# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``,
Dean Troyerdff49a22014-01-30 15:37:40 -06001564# ``TRACK_DEPENDS``, ``*_proxy``
1565# pip_install package [package ...]
1566function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001567 local xtrace=$(set +o | grep xtrace)
1568 set +o xtrace
1569 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1570 $xtrace
1571 return
1572 fi
1573
Dean Troyerdff49a22014-01-30 15:37:40 -06001574 if [[ -z "$os_PACKAGE" ]]; then
1575 GetOSVersion
1576 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001577 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1578 # TRACK_DEPENDS=True installation creates a circular dependency when
1579 # we attempt to install virtualenv into a virualenv, so we must global
1580 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001581 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001582 local cmd_pip=$DEST/.venv/bin/pip
1583 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001584 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001585 local cmd_pip=$(get_pip_command)
1586 local sudo_pip="sudo"
Dean Troyerdff49a22014-01-30 15:37:40 -06001587 fi
1588
Sean Dague45917cc2014-02-24 16:09:14 -05001589 $xtrace
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001590 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001591 http_proxy=$http_proxy \
1592 https_proxy=$https_proxy \
1593 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001594 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001595 $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001596
Flavio Percoco5a91c352014-10-31 18:48:00 +01001597 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001598 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1599 local test_req="$@/test-requirements.txt"
1600 if [[ -e "$test_req" ]]; then
1601 $sudo_pip PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
1602 http_proxy=$http_proxy \
1603 https_proxy=$https_proxy \
1604 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001605 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001606 -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001607 fi
1608 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001609}
1610
Sean Daguecc524062014-10-01 09:06:43 -04001611# should we use this library from their git repo, or should we let it
1612# get pulled in via pip dependencies.
1613function use_library_from_git {
1614 local name=$1
1615 local enabled=1
1616 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1617 return $enabled
1618}
1619
1620# setup a library by name. If we are trying to use the library from
1621# git, we'll do a git based install, otherwise we'll punt and the
1622# library should be installed by a requirements pull from another
1623# project.
1624function setup_lib {
1625 local name=$1
1626 local dir=${GITDIR[$name]}
1627 setup_install $dir
1628}
1629
Sean Daguee08ab102014-11-13 17:09:28 -05001630# setup a library by name in editiable mode. If we are trying to use
1631# the library from git, we'll do a git based install, otherwise we'll
1632# punt and the library should be installed by a requirements pull from
1633# another project.
1634#
1635# use this for non namespaced libraries
1636function setup_dev_lib {
1637 local name=$1
1638 local dir=${GITDIR[$name]}
1639 setup_develop $dir
1640}
Sean Daguecc524062014-10-01 09:06:43 -04001641
Sean Dague099e5e32014-03-31 10:35:43 -04001642# this should be used if you want to install globally, all libraries should
1643# use this, especially *oslo* ones
1644function setup_install {
1645 local project_dir=$1
1646 setup_package_with_req_sync $project_dir
1647}
1648
1649# this should be used for projects which run services, like all services
1650function setup_develop {
1651 local project_dir=$1
1652 setup_package_with_req_sync $project_dir -e
1653}
1654
Sean Daguedef15342014-10-27 12:26:04 -04001655# determine if a project as specified by directory is in
1656# projects.txt. This will not be an exact match because we throw away
1657# the namespacing when we clone, but it should be good enough in all
1658# practical ways.
1659function is_in_projects_txt {
1660 local project_dir=$1
1661 local project_name=$(basename $project_dir)
1662 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1663}
1664
Dean Troyeraf616d92014-02-17 12:57:55 -06001665# ``pip install -e`` the package, which processes the dependencies
1666# using pip before running `setup.py develop`
1667#
1668# Updates the dependencies in project_dir from the
1669# openstack/requirements global list before installing anything.
1670#
1671# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1672# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001673function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001674 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001675 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001676
Dean Troyeraf616d92014-02-17 12:57:55 -06001677 # Don't update repo if local changes exist
1678 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001679 # ``errexit`` requires us to trap the exit code when the repo is changed
1680 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001681
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001682 if [[ $update_requirements != "changed" ]]; then
Sean Daguedef15342014-10-27 12:26:04 -04001683 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1684 if is_in_projects_txt $project_dir; then
1685 (cd $REQUIREMENTS_DIR; \
1686 python update.py $project_dir)
1687 else
1688 # soft update projects not found in requirements project.txt
1689 (cd $REQUIREMENTS_DIR; \
1690 python update.py -s $project_dir)
1691 fi
1692 else
1693 (cd $REQUIREMENTS_DIR; \
1694 python update.py $project_dir)
1695 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001696 fi
1697
Sean Dague099e5e32014-03-31 10:35:43 -04001698 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001699
1700 # We've just gone and possibly modified the user's source tree in an
1701 # automated way, which is considered bad form if it's a development
1702 # tree because we've screwed up their next git checkin. So undo it.
1703 #
1704 # However... there are some circumstances, like running in the gate
1705 # where we really really want the overridden version to stick. So provide
1706 # a variable that tells us whether or not we should UNDO the requirements
1707 # changes (this will be set to False in the OpenStack ci gate)
1708 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001709 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001710 (cd $project_dir && git reset --hard)
1711 fi
1712 fi
1713}
1714
1715# ``pip install -e`` the package, which processes the dependencies
1716# using pip before running `setup.py develop`
1717# Uses globals ``STACK_USER``
1718# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001719function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001720 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001721 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001722
Sean Dague099e5e32014-03-31 10:35:43 -04001723 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001724 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001725 if [[ "$flags" == "-e" ]]; then
1726 safe_chown -R $STACK_USER $1/*.egg-info
1727 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001728}
1729
Dean Troyerdff49a22014-01-30 15:37:40 -06001730
1731# Service Functions
1732# =================
1733
1734# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1735# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001736function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001737 echo "$1" | sed -e '
1738 s/,,/,/g;
1739 s/^,//;
1740 s/,$//
1741 '
1742}
1743
1744# disable_all_services() removes all current services
1745# from ``ENABLED_SERVICES`` to reset the configuration
1746# before a minimal installation
1747# Uses global ``ENABLED_SERVICES``
1748# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001749function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001750 ENABLED_SERVICES=""
1751}
1752
1753# Remove all services starting with '-'. For example, to install all default
1754# services except rabbit (rabbit) set in ``localrc``:
1755# ENABLED_SERVICES+=",-rabbit"
1756# Uses global ``ENABLED_SERVICES``
1757# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001758function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001759 local tmpsvcs="${ENABLED_SERVICES}"
1760 local service
1761 for service in ${tmpsvcs//,/ }; do
1762 if [[ ${service} == -* ]]; then
1763 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1764 fi
1765 done
1766 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1767}
1768
1769# disable_service() removes the services passed as argument to the
1770# ``ENABLED_SERVICES`` list, if they are present.
1771#
1772# For example:
1773# disable_service rabbit
1774#
1775# This function does not know about the special cases
1776# for nova, glance, and neutron built into is_service_enabled().
1777# Uses global ``ENABLED_SERVICES``
1778# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001779function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001780 local tmpsvcs=",${ENABLED_SERVICES},"
1781 local service
1782 for service in $@; do
1783 if is_service_enabled $service; then
1784 tmpsvcs=${tmpsvcs//,$service,/,}
1785 fi
1786 done
1787 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1788}
1789
1790# enable_service() adds the services passed as argument to the
1791# ``ENABLED_SERVICES`` list, if they are not already present.
1792#
1793# For example:
1794# enable_service qpid
1795#
1796# This function does not know about the special cases
1797# for nova, glance, and neutron built into is_service_enabled().
1798# Uses global ``ENABLED_SERVICES``
1799# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001800function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001801 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001802 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001803 for service in $@; do
1804 if ! is_service_enabled $service; then
1805 tmpsvcs+=",$service"
1806 fi
1807 done
1808 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1809 disable_negated_services
1810}
1811
1812# is_service_enabled() checks if the service(s) specified as arguments are
1813# enabled by the user in ``ENABLED_SERVICES``.
1814#
1815# Multiple services specified as arguments are ``OR``'ed together; the test
1816# is a short-circuit boolean, i.e it returns on the first match.
1817#
1818# There are special cases for some 'catch-all' services::
1819# **nova** returns true if any service enabled start with **n-**
1820# **cinder** returns true if any service enabled start with **c-**
1821# **ceilometer** returns true if any service enabled start with **ceilometer**
1822# **glance** returns true if any service enabled start with **g-**
1823# **neutron** returns true if any service enabled start with **q-**
1824# **swift** returns true if any service enabled start with **s-**
1825# **trove** returns true if any service enabled start with **tr-**
1826# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1827# **s-** services will be enabled. This will be deprecated in the future.
1828#
1829# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1830# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1831# as enabled in this case.
1832#
1833# Uses global ``ENABLED_SERVICES``
1834# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001835function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001836 local xtrace=$(set +o | grep xtrace)
1837 set +o xtrace
1838 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001839 local services=$@
1840 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001841 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001842 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001843
1844 # Look for top-level 'enabled' function for this service
1845 if type is_${service}_enabled >/dev/null 2>&1; then
1846 # A function exists for this service, use it
1847 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001848 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001849 fi
1850
1851 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1852 # are implemented
1853
Sean Dague45917cc2014-02-24 16:09:14 -05001854 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001855 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001856 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1857 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1858 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1859 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1860 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1861 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1862 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1863 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1864 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001865 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001866 done
Sean Dague45917cc2014-02-24 16:09:14 -05001867 $xtrace
1868 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001869}
1870
1871# Toggle enable/disable_service for services that must run exclusive of each other
1872# $1 The name of a variable containing a space-separated list of services
1873# $2 The name of a variable in which to store the enabled service's name
1874# $3 The name of the service to enable
1875function use_exclusive_service {
1876 local options=${!1}
1877 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001878 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001879 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001880 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001881 for opt in $options;do
1882 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1883 done
1884 eval "$out=$selection"
1885 return 0
1886}
1887
1888
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001889# System Functions
1890# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001891
1892# Only run the command if the target file (the last arg) is not on an
1893# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001894function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001895 local xtrace=$(set +o | grep xtrace)
1896 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001897 local args=( $@ )
1898 local last
1899 local sudo_cmd
1900 local dir_to_check
1901
1902 let last="${#args[*]} - 1"
1903
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001904 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001905 if [ ! -d "$dir_to_check" ]; then
1906 dir_to_check=`dirname "$dir_to_check"`
1907 fi
1908
1909 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001910 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001911 return 0
1912 fi
1913
1914 if [[ $TRACK_DEPENDS = True ]]; then
1915 sudo_cmd="env"
1916 else
1917 sudo_cmd="sudo"
1918 fi
1919
Sean Dague45917cc2014-02-24 16:09:14 -05001920 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001921 $sudo_cmd $@
1922}
1923
1924# Exit 0 if address is in network or 1 if address is not in network
1925# ip-range is in CIDR notation: 1.2.3.4/20
1926# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001927function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001928 local ip=$1
1929 local range=$2
1930 local masklen=${range#*/}
1931 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1932 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1933 [[ $network == $subnet ]]
1934}
1935
1936# Add a user to a group.
1937# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001938function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001939 local user=$1
1940 local group=$2
1941
1942 if [[ -z "$os_VENDOR" ]]; then
1943 GetOSVersion
1944 fi
1945
1946 # SLE11 and openSUSE 12.2 don't have the usual usermod
1947 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1948 sudo usermod -a -G "$group" "$user"
1949 else
1950 sudo usermod -A "$group" "$user"
1951 fi
1952}
1953
1954# Convert CIDR notation to a IPv4 netmask
1955# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001956function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001957 local maskpat="255 255 255 255"
1958 local maskdgt="254 252 248 240 224 192 128"
1959 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1960 echo ${1-0}.${2-0}.${3-0}.${4-0}
1961}
1962
1963# Gracefully cp only if source file/dir exists
1964# cp_it source destination
1965function cp_it {
1966 if [ -e $1 ] || [ -d $1 ]; then
1967 cp -pRL $1 $2
1968 fi
1969}
1970
1971# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1972# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1973# ``localrc`` or on the command line if necessary::
1974#
1975# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1976#
1977# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1978
Ian Wienandaee18c72014-02-21 15:35:08 +11001979function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06001980 if [[ -n "$http_proxy" ]]; then
1981 export http_proxy=$http_proxy
1982 fi
1983 if [[ -n "$https_proxy" ]]; then
1984 export https_proxy=$https_proxy
1985 fi
1986 if [[ -n "$no_proxy" ]]; then
1987 export no_proxy=$no_proxy
1988 fi
1989}
1990
1991# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11001992function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06001993 local mount_type=`stat -f -L -c %T $1`
1994 test "$mount_type" == "nfs"
1995}
1996
1997# Return the network portion of the given IP address using netmask
1998# netmask is in the traditional dotted-quad format
1999# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002000function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002001 local ip=$1
2002 local mask=$2
2003 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2004 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2005 echo $subnet
2006}
2007
2008# Service wrapper to restart services
2009# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002010function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002011 if is_ubuntu; then
2012 sudo /usr/sbin/service $1 restart
2013 else
2014 sudo /sbin/service $1 restart
2015 fi
2016}
2017
2018# Only change permissions of a file or directory if it is not on an
2019# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002020function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002021 _safe_permission_operation chmod $@
2022}
2023
2024# Only change ownership of a file or directory if it is not on an NFS
2025# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002026function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002027 _safe_permission_operation chown $@
2028}
2029
2030# Service wrapper to start services
2031# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002032function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002033 if is_ubuntu; then
2034 sudo /usr/sbin/service $1 start
2035 else
2036 sudo /sbin/service $1 start
2037 fi
2038}
2039
2040# Service wrapper to stop services
2041# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002042function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002043 if is_ubuntu; then
2044 sudo /usr/sbin/service $1 stop
2045 else
2046 sudo /sbin/service $1 stop
2047 fi
2048}
2049
2050
2051# Restore xtrace
2052$XTRACE
2053
2054# Local variables:
2055# mode: shell-script
2056# End: