blob: 23d59c7d98cb4a45c6d0aba82336c88e0ed2efa8 [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``
Dean Troyerdff49a22014-01-30 15:37:40 -060029# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090030# - ``REQUIREMENTS_DIR``
31# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060032# - ``TRACK_DEPENDS``
Masayuki Igawad20f6322014-02-28 09:22:37 +090033# - ``UNDO_REQUIREMENTS``
Dean Troyerdff49a22014-01-30 15:37:40 -060034# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050035#
Dean Troyerdff49a22014-01-30 15:37:40 -060036
37# Save trace setting
38XTRACE=$(set +o | grep xtrace)
39set +o xtrace
40
Sean Daguecc524062014-10-01 09:06:43 -040041# Global Config Variables
42declare -A GITREPO
43declare -A GITBRANCH
44declare -A GITDIR
45
Dean Troyerdff49a22014-01-30 15:37:40 -060046# Config Functions
47# ================
48
49# Append a new option in an ini file without replacing the old value
50# iniadd config-file section option value1 value2 value3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +110051function iniadd {
Sean Dague45917cc2014-02-24 16:09:14 -050052 local xtrace=$(set +o | grep xtrace)
53 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060054 local file=$1
55 local section=$2
56 local option=$3
57 shift 3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050058
Dean Troyerdff49a22014-01-30 15:37:40 -060059 local values="$(iniget_multiline $file $section $option) $@"
60 iniset_multiline $file $section $option $values
Sean Dague45917cc2014-02-24 16:09:14 -050061 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060062}
63
64# Comment an option in an INI file
65# inicomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110066function inicomment {
Sean Dague45917cc2014-02-24 16:09:14 -050067 local xtrace=$(set +o | grep xtrace)
68 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060069 local file=$1
70 local section=$2
71 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050072
Dean Troyerdff49a22014-01-30 15:37:40 -060073 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -050074 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060075}
76
77# Get an option from an INI file
78# iniget config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110079function iniget {
Sean Dague45917cc2014-02-24 16:09:14 -050080 local xtrace=$(set +o | grep xtrace)
81 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060082 local file=$1
83 local section=$2
84 local option=$3
85 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -050086
Dean Troyerdff49a22014-01-30 15:37:40 -060087 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
88 echo ${line#*=}
Sean Dague45917cc2014-02-24 16:09:14 -050089 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060090}
91
92# Get a multiple line option from an INI file
93# iniget_multiline config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +110094function iniget_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -050095 local xtrace=$(set +o | grep xtrace)
96 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -060097 local file=$1
98 local section=$2
99 local option=$3
100 local values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500101
Dean Troyerdff49a22014-01-30 15:37:40 -0600102 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
103 echo ${values}
Sean Dague45917cc2014-02-24 16:09:14 -0500104 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600105}
106
107# Determinate is the given option present in the INI file
108# ini_has_option config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100109function ini_has_option {
Sean Dague45917cc2014-02-24 16:09:14 -0500110 local xtrace=$(set +o | grep xtrace)
111 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600112 local file=$1
113 local section=$2
114 local option=$3
115 local line
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500116
Dean Troyerdff49a22014-01-30 15:37:40 -0600117 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Sean Dague45917cc2014-02-24 16:09:14 -0500118 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600119 [ -n "$line" ]
120}
121
Robert Li751ad1a2014-10-15 21:40:53 -0400122# Add another config line for a multi-line option.
123# It's normally called after iniset of the same option and assumes
124# that the section already exists.
125#
126# Note that iniset_multiline requires all the 'lines' to be supplied
127# in the argument list. Doing that will cause incorrect configuration
128# if spaces are used in the config values.
129#
130# iniadd_literal config-file section option value
131function iniadd_literal {
132 local xtrace=$(set +o | grep xtrace)
133 set +o xtrace
134 local file=$1
135 local section=$2
136 local option=$3
137 local value=$4
138
139 [[ -z $section || -z $option ]] && return
140
141 # Add it
142 sed -i -e "/^\[$section\]/ a\\
143$option = $value
144" "$file"
145
146 $xtrace
147}
148
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700149function inidelete {
150 local xtrace=$(set +o | grep xtrace)
151 set +o xtrace
152 local file=$1
153 local section=$2
154 local option=$3
155
156 [[ -z $section || -z $option ]] && return
157
158 # Remove old values
159 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
160
161 $xtrace
162}
163
Dean Troyerdff49a22014-01-30 15:37:40 -0600164# Set an option in an INI file
165# iniset config-file section option value
Ian Wienandaee18c72014-02-21 15:35:08 +1100166function iniset {
Sean Dague45917cc2014-02-24 16:09:14 -0500167 local xtrace=$(set +o | grep xtrace)
168 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600169 local file=$1
170 local section=$2
171 local option=$3
172 local value=$4
173
174 [[ -z $section || -z $option ]] && return
175
176 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
177 # Add section at the end
178 echo -e "\n[$section]" >>"$file"
179 fi
180 if ! ini_has_option "$file" "$section" "$option"; then
181 # Add it
182 sed -i -e "/^\[$section\]/ a\\
183$option = $value
184" "$file"
185 else
186 local sep=$(echo -ne "\x01")
187 # Replace it
188 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
189 fi
Sean Dague45917cc2014-02-24 16:09:14 -0500190 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600191}
192
193# Set a multiple line option in an INI file
194# iniset_multiline config-file section option value1 value2 valu3 ...
Ian Wienandaee18c72014-02-21 15:35:08 +1100195function iniset_multiline {
Sean Dague45917cc2014-02-24 16:09:14 -0500196 local xtrace=$(set +o | grep xtrace)
197 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600198 local file=$1
199 local section=$2
200 local option=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500201
Dean Troyerdff49a22014-01-30 15:37:40 -0600202 shift 3
203 local values
204 for v in $@; do
205 # The later sed command inserts each new value in the line next to
206 # the section identifier, which causes the values to be inserted in
207 # the reverse order. Do a reverse here to keep the original order.
208 values="$v ${values}"
209 done
210 if ! grep -q "^\[$section\]" "$file"; then
211 # Add section at the end
212 echo -e "\n[$section]" >>"$file"
213 else
214 # Remove old values
215 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
216 fi
217 # Add new ones
218 for v in $values; do
219 sed -i -e "/^\[$section\]/ a\\
220$option = $v
221" "$file"
222 done
Sean Dague45917cc2014-02-24 16:09:14 -0500223 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600224}
225
226# Uncomment an option in an INI file
227# iniuncomment config-file section option
Ian Wienandaee18c72014-02-21 15:35:08 +1100228function iniuncomment {
Sean Dague45917cc2014-02-24 16:09:14 -0500229 local xtrace=$(set +o | grep xtrace)
230 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600231 local file=$1
232 local section=$2
233 local option=$3
234 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Sean Dague45917cc2014-02-24 16:09:14 -0500235 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600236}
237
238# Normalize config values to True or False
239# Accepts as False: 0 no No NO false False FALSE
240# Accepts as True: 1 yes Yes YES true True TRUE
241# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100242function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500243 local xtrace=$(set +o | grep xtrace)
244 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600245 local default=$1
246 local testval=$2
247
248 [[ -z "$testval" ]] && { echo "$default"; return; }
249 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
250 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
251 echo "$default"
Sean Dague45917cc2014-02-24 16:09:14 -0500252 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600253}
254
255
256# Control Functions
257# =================
258
259# Prints backtrace info
260# filename:lineno:function
261# backtrace level
262function backtrace {
263 local level=$1
264 local deep=$((${#BASH_SOURCE[@]} - 1))
265 echo "[Call Trace]"
266 while [ $level -le $deep ]; do
267 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
268 deep=$((deep - 1))
269 done
270}
271
272# Prints line number and "message" then exits
273# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100274function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600275 local exitcode=$?
276 set +o xtrace
277 local line=$1; shift
278 if [ $exitcode == 0 ]; then
279 exitcode=1
280 fi
281 backtrace 2
282 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600283 # Give buffers a second to flush
284 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600285 exit $exitcode
286}
287
288# Checks an environment variable is not set or has length 0 OR if the
289# exit code is non-zero and prints "message" and exits
290# NOTE: env-var is the variable name without a '$'
291# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100292function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600293 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500294 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600295 set +o xtrace
296 local line=$1; shift
297 local evar=$1; shift
298 if ! is_set $evar || [ $exitcode != 0 ]; then
299 die $line "$*"
300 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500301 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600302}
303
304# Prints line number and "message" in error format
305# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100306function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600307 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500308 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600309 set +o xtrace
310 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
311 echo $msg 1>&2;
312 if [[ -n ${SCREEN_LOGDIR} ]]; then
313 echo $msg >> "${SCREEN_LOGDIR}/error.log"
314 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500315 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600316 return $exitcode
317}
318
319# Checks an environment variable is not set or has length 0 OR if the
320# exit code is non-zero and prints "message"
321# NOTE: env-var is the variable name without a '$'
322# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100323function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600324 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500325 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600326 set +o xtrace
327 local line=$1; shift
328 local evar=$1; shift
329 if ! is_set $evar || [ $exitcode != 0 ]; then
330 err $line "$*"
331 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500332 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600333 return $exitcode
334}
335
336# Exit after outputting a message about the distribution not being supported.
337# exit_distro_not_supported [optional-string-telling-what-is-missing]
338function exit_distro_not_supported {
339 if [[ -z "$DISTRO" ]]; then
340 GetDistro
341 fi
342
343 if [ $# -gt 0 ]; then
344 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
345 else
346 die $LINENO "Support for $DISTRO is incomplete."
347 fi
348}
349
350# Test if the named environment variable is set and not zero length
351# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100352function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600353 local var=\$"$1"
354 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
355}
356
357# Prints line number and "message" in warning format
358# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100359function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600360 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500361 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600362 set +o xtrace
363 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
364 echo $msg 1>&2;
365 if [[ -n ${SCREEN_LOGDIR} ]]; then
366 echo $msg >> "${SCREEN_LOGDIR}/error.log"
367 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500368 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600369 return $exitcode
370}
371
372
373# Distro Functions
374# ================
375
376# Determine OS Vendor, Release and Update
377# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
378# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500379# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
380# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
381# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
382# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
383# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
384declare os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
385
Dean Troyerdff49a22014-01-30 15:37:40 -0600386# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100387function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500388
Dean Troyerdff49a22014-01-30 15:37:40 -0600389 # Figure out which vendor we are
390 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
391 # OS/X
392 os_VENDOR=`sw_vers -productName`
393 os_RELEASE=`sw_vers -productVersion`
394 os_UPDATE=${os_RELEASE##*.}
395 os_RELEASE=${os_RELEASE%.*}
396 os_PACKAGE=""
397 if [[ "$os_RELEASE" =~ "10.7" ]]; then
398 os_CODENAME="lion"
399 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
400 os_CODENAME="snow leopard"
401 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
402 os_CODENAME="leopard"
403 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
404 os_CODENAME="tiger"
405 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
406 os_CODENAME="panther"
407 else
408 os_CODENAME=""
409 fi
410 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
411 os_VENDOR=$(lsb_release -i -s)
412 os_RELEASE=$(lsb_release -r -s)
413 os_UPDATE=""
414 os_PACKAGE="rpm"
415 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
416 os_PACKAGE="deb"
417 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
418 lsb_release -d -s | grep -q openSUSE
419 if [[ $? -eq 0 ]]; then
420 os_VENDOR="openSUSE"
421 fi
422 elif [[ $os_VENDOR == "openSUSE project" ]]; then
423 os_VENDOR="openSUSE"
424 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
425 os_VENDOR="Red Hat"
426 fi
427 os_CODENAME=$(lsb_release -c -s)
428 elif [[ -r /etc/redhat-release ]]; then
429 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
430 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
431 # CentOS release 5.5 (Final)
432 # CentOS Linux release 6.0 (Final)
433 # Fedora release 16 (Verne)
434 # XenServer release 6.2.0-70446c (xenenterprise)
435 os_CODENAME=""
436 for r in "Red Hat" CentOS Fedora XenServer; do
437 os_VENDOR=$r
438 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
439 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
440 os_CODENAME=${ver#*|}
441 os_RELEASE=${ver%|*}
442 os_UPDATE=${os_RELEASE##*.}
443 os_RELEASE=${os_RELEASE%.*}
444 break
445 fi
446 os_VENDOR=""
447 done
448 os_PACKAGE="rpm"
449 elif [[ -r /etc/SuSE-release ]]; then
450 for r in openSUSE "SUSE Linux"; do
451 if [[ "$r" = "SUSE Linux" ]]; then
452 os_VENDOR="SUSE LINUX"
453 else
454 os_VENDOR=$r
455 fi
456
457 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
458 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
459 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
460 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
461 break
462 fi
463 os_VENDOR=""
464 done
465 os_PACKAGE="rpm"
466 # If lsb_release is not installed, we should be able to detect Debian OS
467 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
468 os_VENDOR="Debian"
469 os_PACKAGE="deb"
470 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
471 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
472 fi
473 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
474}
475
476# Translate the OS version values into common nomenclature
477# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500478declare DISTRO
479
Ian Wienandaee18c72014-02-21 15:35:08 +1100480function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600481 GetOSVersion
482 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
483 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
484 DISTRO=$os_CODENAME
485 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
486 # For Fedora, just use 'f' and the release
487 DISTRO="f$os_RELEASE"
488 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
489 DISTRO="opensuse-$os_RELEASE"
490 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
491 # For SLE, also use the service pack
492 if [[ -z "$os_UPDATE" ]]; then
493 DISTRO="sle${os_RELEASE}"
494 else
495 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
496 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530497 elif [[ "$os_VENDOR" =~ (Red Hat) || \
498 "$os_VENDOR" =~ (CentOS) || \
499 "$os_VENDOR" =~ (OracleServer) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600500 # Drop the . release as we assume it's compatible
501 DISTRO="rhel${os_RELEASE::1}"
502 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
503 DISTRO="xs$os_RELEASE"
504 else
505 # Catch-all for now is Vendor + Release + Update
506 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
507 fi
508 export DISTRO
509}
510
511# Utility function for checking machine architecture
512# is_arch arch-type
513function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500514 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600515}
516
Ian Wienandbdc90c52014-08-04 15:44:58 +1000517# Quick check for a rackspace host; n.b. rackspace provided images
518# have these Xen tools installed but a custom image may not.
519function is_rackspace {
520 [ -f /usr/bin/xenstore-ls ] && \
521 sudo /usr/bin/xenstore-ls vm-data | grep -q "Rackspace"
522}
523
Dean Troyerdff49a22014-01-30 15:37:40 -0600524# Determine if current distribution is a Fedora-based distribution
525# (Fedora, RHEL, CentOS, etc).
526# is_fedora
527function is_fedora {
528 if [[ -z "$os_VENDOR" ]]; then
529 GetOSVersion
530 fi
531
anju Tiwari6c639c92014-07-15 18:11:54 +0530532 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
533 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600534}
535
536
537# Determine if current distribution is a SUSE-based distribution
538# (openSUSE, SLE).
539# is_suse
540function is_suse {
541 if [[ -z "$os_VENDOR" ]]; then
542 GetOSVersion
543 fi
544
545 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
546}
547
548
549# Determine if current distribution is an Ubuntu-based distribution
550# It will also detect non-Ubuntu but Debian-based distros
551# is_ubuntu
552function is_ubuntu {
553 if [[ -z "$os_PACKAGE" ]]; then
554 GetOSVersion
555 fi
556 [ "$os_PACKAGE" = "deb" ]
557}
558
559
560# Git Functions
561# =============
562
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600563# Returns openstack release name for a given branch name
564# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100565function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600566 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700567 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600568 echo ${branch#*/}
569 else
570 echo "master"
571 fi
572}
573
Dean Troyerdff49a22014-01-30 15:37:40 -0600574# git clone only if directory doesn't exist already. Since ``DEST`` might not
575# be owned by the installation user, we create the directory and change the
576# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500577# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
578# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600579# does not exist (default is False, meaning the repo will be cloned).
Jamie Lennox51f0de52014-10-20 16:32:34 +0200580# Set global ``GIT_DEPTH=<number>`` to limit the history depth of the git clone
581# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``, ``GIT_DEPTH``
Dean Troyerdff49a22014-01-30 15:37:40 -0600582# git_clone remote dest-dir branch
583function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500584 local git_remote=$1
585 local git_dest=$2
586 local git_ref=$3
587 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200588 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500589
Dean Troyerdff49a22014-01-30 15:37:40 -0600590 RECLONE=$(trueorfalse False $RECLONE)
591
YAMAMOTO Takashi292b2a72014-10-31 13:48:58 +0900592 if [[ -n "${GIT_DEPTH}" ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200593 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
594 fi
595
Dean Troyerdff49a22014-01-30 15:37:40 -0600596 if [[ "$OFFLINE" = "True" ]]; then
597 echo "Running in offline mode, clones already exist"
598 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500599 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600600 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400601 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600602 return
603 fi
604
Dean Troyer50cda692014-07-25 11:57:20 -0500605 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600606 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500607 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600608 [[ "$ERROR_ON_CLONE" = "True" ]] && \
609 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200610 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600611 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500612 cd $git_dest
613 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600614 else
615 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500616 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600617 [[ "$ERROR_ON_CLONE" = "True" ]] && \
618 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200619 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500620 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600621 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500622 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600623 elif [[ "$RECLONE" = "True" ]]; then
624 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500625 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600626 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500627 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100628 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600629 # remove the existing ignored files (like pyc) as they cause breakage
630 # (due to the py files having older timestamps than our pyc, so python
631 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500632 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600633
Dean Troyer50cda692014-07-25 11:57:20 -0500634 # handle git_ref accordingly to type (tag, branch)
635 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
636 git_update_tag $git_ref
637 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
638 git_update_branch $git_ref
639 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
640 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600641 else
Dean Troyer50cda692014-07-25 11:57:20 -0500642 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600643 fi
644
645 fi
646 fi
647
648 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500649 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600650 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400651 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600652}
653
Sean Daguecc524062014-10-01 09:06:43 -0400654# A variation on git clone that lets us specify a project by it's
655# actual name, like oslo.config. This is exceptionally useful in the
656# library installation case
657function git_clone_by_name {
658 local name=$1
659 local repo=${GITREPO[$name]}
660 local dir=${GITDIR[$name]}
661 local branch=${GITBRANCH[$name]}
662 git_clone $repo $dir $branch
663}
664
665
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100666# git can sometimes get itself infinitely stuck with transient network
667# errors or other issues with the remote end. This wraps git in a
668# timeout/retry loop and is intended to watch over non-local git
669# processes that might hang. GIT_TIMEOUT, if set, is passed directly
670# to timeout(1); otherwise the default value of 0 maintains the status
671# quo of waiting forever.
672# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100673function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100674 local count=0
675 local timeout=0
676
677 if [[ -n "${GIT_TIMEOUT}" ]]; then
678 timeout=${GIT_TIMEOUT}
679 fi
680
681 until timeout -s SIGINT ${timeout} git "$@"; do
682 # 124 is timeout(1)'s special return code when it reached the
683 # timeout; otherwise assume fatal failure
684 if [[ $? -ne 124 ]]; then
685 die $LINENO "git call failed: [git $@]"
686 fi
687
688 count=$(($count + 1))
689 warn "timeout ${count} for git call: [git $@]"
690 if [ $count -eq 3 ]; then
691 die $LINENO "Maximum of 3 git retries reached"
692 fi
693 sleep 5
694 done
695}
696
Dean Troyerdff49a22014-01-30 15:37:40 -0600697# git update using reference as a branch.
698# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100699function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500700 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600701
Dean Troyer50cda692014-07-25 11:57:20 -0500702 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600703 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500704 git branch -D $git_branch || true
705 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600706}
707
708# git update using reference as a branch.
709# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100710function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500711 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600712
Dean Troyer50cda692014-07-25 11:57:20 -0500713 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600714}
715
716# git update using reference as a tag. Be careful editing source at that repo
717# as working copy will be in a detached mode
718# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100719function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500720 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600721
Dean Troyer50cda692014-07-25 11:57:20 -0500722 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600723 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500724 git_timed fetch origin tag $git_tag
725 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600726}
727
728
729# OpenStack Functions
730# ===================
731
732# Get the default value for HOST_IP
733# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100734function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600735 local fixed_range=$1
736 local floating_range=$2
737 local host_ip_iface=$3
738 local host_ip=$4
739
740 # Find the interface used for the default route
741 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
742 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
743 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
744 host_ip=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500745 local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}')
746 local ip
747 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600748 # Attempt to filter out IP addresses that are part of the fixed and
749 # floating range. Note that this method only works if the ``netaddr``
750 # python library is installed. If it is not installed, an error
751 # will be printed and the first IP from the interface will be used.
752 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
753 # address.
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500754 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
755 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600756 break;
757 fi
758 done
759 fi
760 echo $host_ip
761}
762
Attila Fazekasf71b5002014-05-28 09:52:22 +0200763# Generates hex string from ``size`` byte of pseudo random data
764# generate_hex_string size
765function generate_hex_string {
766 local size=$1
767 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
768}
769
Dean Troyerdff49a22014-01-30 15:37:40 -0600770# Grab a numbered field from python prettytable output
771# Fields are numbered starting with 1
772# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
773# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100774function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500775 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600776 while read data; do
777 if [ "$1" -lt 0 ]; then
778 field="(\$(NF$1))"
779 else
780 field="\$$(($1 + 1))"
781 fi
782 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
783 done
784}
785
786# Add a policy to a policy.json file
787# Do nothing if the policy already exists
788# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100789function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600790 local policy_file=$1
791 local policy_name=$2
792 local policy_perm=$3
793
794 if grep -q ${policy_name} ${policy_file}; then
795 echo "Policy ${policy_name} already exists in ${policy_file}"
796 return
797 fi
798
799 # Add a terminating comma to policy lines without one
800 # Remove the closing '}' and all lines following to the end-of-file
801 local tmpfile=$(mktemp)
802 uniq ${policy_file} | sed -e '
803 s/]$/],/
804 /^[}]/,$d
805 ' > ${tmpfile}
806
807 # Append policy and closing brace
808 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
809 echo "}" >>${tmpfile}
810
811 mv ${tmpfile} ${policy_file}
812}
813
Alistair Coles24779f62014-10-15 18:57:59 +0100814# Gets or creates a domain
815# Usage: get_or_create_domain <name> <description>
816function get_or_create_domain {
817 local os_url="$KEYSTONE_SERVICE_URI/v3"
818 # Gets domain id
819 local domain_id=$(
820 # Gets domain id
821 openstack --os-token=$OS_TOKEN --os-url=$os_url \
822 --os-identity-api-version=3 domain show $1 \
823 -f value -c id 2>/dev/null ||
824 # Creates new domain
825 openstack --os-token=$OS_TOKEN --os-url=$os_url \
826 --os-identity-api-version=3 domain create $1 \
827 --description "$2" \
828 -f value -c id
829 )
830 echo $domain_id
831}
832
Bartosz Górski0abde392014-02-28 14:15:19 +0100833# Gets or creates user
Alistair Coles24779f62014-10-15 18:57:59 +0100834# Usage: get_or_create_user <username> <password> <project> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100835function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200836 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500837 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200838 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500839 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200840 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100841 local os_cmd="openstack"
842 local domain=""
843 if [[ ! -z "$5" ]]; then
844 domain="--domain=$5"
845 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
846 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100847 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500848 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500849 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100850 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100851 $1 \
852 --password "$2" \
853 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500854 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100855 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500856 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100857 -f value -c id
858 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500859 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100860}
861
862# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100863# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100864function get_or_create_project {
865 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100866 local os_cmd="openstack"
867 local domain=""
868 if [[ ! -z "$2" ]]; then
869 domain="--domain=$2"
870 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI/v3 --os-identity-api-version=3"
871 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500872 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500873 # Creates new project with --or-show
874 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100875 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500876 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100877}
878
879# Gets or creates role
880# Usage: get_or_create_role <name>
881function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500882 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500883 # Creates role with --or-show
884 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100885 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500886 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100887}
888
889# Gets or adds user role
890# Usage: get_or_add_user_role <role> <user> <project>
891function get_or_add_user_role {
892 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500893 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100894 $2 \
895 --project $3 \
896 --column "ID" \
897 --column "Name" \
898 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500899 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100900 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500901 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100902 $1 \
903 --user $2 \
904 --project $3 \
905 | grep " id " | get_field 2)
906 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500907 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100908}
909
910# Gets or creates service
911# Usage: get_or_create_service <name> <type> <description>
912function get_or_create_service {
913 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500914 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100915 # Gets service id
916 openstack service show $1 -f value -c id 2>/dev/null ||
917 # Creates new service if not exists
918 openstack service create \
919 $1 \
920 --type=$2 \
921 --description="$3" \
922 -f value -c id
923 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500924 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100925}
926
927# Gets or creates endpoint
928# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
929function get_or_create_endpoint {
930 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500931 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100932 --column "ID" \
933 --column "Region" \
934 --column "Service Name" \
935 | grep " $2 " \
936 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500937 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100938 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500939 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100940 $1 \
941 --region $2 \
942 --publicurl $3 \
943 --adminurl $4 \
944 --internalurl $5 \
945 | grep " id " | get_field 2)
946 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500947 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100948}
Dean Troyerdff49a22014-01-30 15:37:40 -0600949
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500950
Dean Troyerdff49a22014-01-30 15:37:40 -0600951# Package Functions
952# =================
953
954# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100955function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600956 local pkg_dir
957 if is_ubuntu; then
Monty Taylor81a016d2014-11-15 17:18:13 -0300958 pkg_dir=$FILES/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600959 elif is_fedora; then
960 pkg_dir=$FILES/rpms
961 elif is_suse; then
962 pkg_dir=$FILES/rpms-suse
963 else
964 exit_distro_not_supported "list of packages"
965 fi
966 echo "$pkg_dir"
967}
968
969# Wrapper for ``apt-get`` to set cache and proxy environment variables
970# Uses globals ``OFFLINE``, ``*_proxy``
971# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100972function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500973 local xtrace=$(set +o | grep xtrace)
974 set +o xtrace
975
Dean Troyerdff49a22014-01-30 15:37:40 -0600976 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
977 local sudo="sudo"
978 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500979
980 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600981 $sudo DEBIAN_FRONTEND=noninteractive \
982 http_proxy=$http_proxy https_proxy=$https_proxy \
983 no_proxy=$no_proxy \
984 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
985}
986
987# get_packages() collects a list of package names of any type from the
Monty Taylor81a016d2014-11-15 17:18:13 -0300988# prerequisite files in ``files/{debs|rpms}``. The list is intended
Dean Troyerdff49a22014-01-30 15:37:40 -0600989# to be passed to a package installer such as apt or yum.
990#
991# Only packages required for the services in 1st argument will be
992# included. Two bits of metadata are recognized in the prerequisite files:
993#
994# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
995# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
996# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +1100997function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -0500998 local xtrace=$(set +o | grep xtrace)
999 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001000 local services=$@
1001 local package_dir=$(_get_package_dir)
1002 local file_to_parse
1003 local service
1004
Flavio Percoco5a91c352014-10-31 18:48:00 +01001005 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
1006
Dean Troyerdff49a22014-01-30 15:37:40 -06001007 if [[ -z "$package_dir" ]]; then
1008 echo "No package directory supplied"
1009 return 1
1010 fi
1011 if [[ -z "$DISTRO" ]]; then
1012 GetDistro
1013 fi
1014 for service in ${services//,/ }; do
1015 # Allow individual services to specify dependencies
1016 if [[ -e ${package_dir}/${service} ]]; then
1017 file_to_parse="${file_to_parse} $service"
1018 fi
1019 # NOTE(sdague) n-api needs glance for now because that's where
1020 # glance client is
1021 if [[ $service == n-api ]]; then
1022 if [[ ! $file_to_parse =~ nova ]]; then
1023 file_to_parse="${file_to_parse} nova"
1024 fi
1025 if [[ ! $file_to_parse =~ glance ]]; then
1026 file_to_parse="${file_to_parse} glance"
1027 fi
1028 elif [[ $service == c-* ]]; then
1029 if [[ ! $file_to_parse =~ cinder ]]; then
1030 file_to_parse="${file_to_parse} cinder"
1031 fi
1032 elif [[ $service == ceilometer-* ]]; then
1033 if [[ ! $file_to_parse =~ ceilometer ]]; then
1034 file_to_parse="${file_to_parse} ceilometer"
1035 fi
1036 elif [[ $service == s-* ]]; then
1037 if [[ ! $file_to_parse =~ swift ]]; then
1038 file_to_parse="${file_to_parse} swift"
1039 fi
1040 elif [[ $service == n-* ]]; then
1041 if [[ ! $file_to_parse =~ nova ]]; then
1042 file_to_parse="${file_to_parse} nova"
1043 fi
1044 elif [[ $service == g-* ]]; then
1045 if [[ ! $file_to_parse =~ glance ]]; then
1046 file_to_parse="${file_to_parse} glance"
1047 fi
1048 elif [[ $service == key* ]]; then
1049 if [[ ! $file_to_parse =~ keystone ]]; then
1050 file_to_parse="${file_to_parse} keystone"
1051 fi
1052 elif [[ $service == q-* ]]; then
1053 if [[ ! $file_to_parse =~ neutron ]]; then
1054 file_to_parse="${file_to_parse} neutron"
1055 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001056 elif [[ $service == ir-* ]]; then
1057 if [[ ! $file_to_parse =~ ironic ]]; then
1058 file_to_parse="${file_to_parse} ironic"
1059 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001060 fi
1061 done
1062
1063 for file in ${file_to_parse}; do
1064 local fname=${package_dir}/${file}
1065 local OIFS line package distros distro
1066 [[ -e $fname ]] || continue
1067
1068 OIFS=$IFS
1069 IFS=$'\n'
1070 for line in $(<${fname}); do
1071 if [[ $line =~ "NOPRIME" ]]; then
1072 continue
1073 fi
1074
1075 # Assume we want this package
1076 package=${line%#*}
1077 inst_pkg=1
1078
1079 # Look for # dist:xxx in comment
1080 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1081 # We are using BASH regexp matching feature.
1082 package=${BASH_REMATCH[1]}
1083 distros=${BASH_REMATCH[2]}
1084 # In bash ${VAR,,} will lowecase VAR
1085 # Look for a match in the distro list
1086 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1087 # If no match then skip this package
1088 inst_pkg=0
1089 fi
1090 fi
1091
1092 # Look for # testonly in comment
1093 if [[ $line =~ (.*)#.*testonly.* ]]; then
1094 package=${BASH_REMATCH[1]}
1095 # Are we installing test packages? (test for the default value)
1096 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1097 # If not installing test packages the skip this package
1098 inst_pkg=0
1099 fi
1100 fi
1101
1102 if [[ $inst_pkg = 1 ]]; then
1103 echo $package
1104 fi
1105 done
1106 IFS=$OIFS
1107 done
Sean Dague45917cc2014-02-24 16:09:14 -05001108 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001109}
1110
1111# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001112# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001113# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001114function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001115 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001116 return 0
1117 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001118
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001119 if is_ubuntu; then
1120 local xtrace=$(set +o | grep xtrace)
1121 set +o xtrace
1122 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1123 # if there are transient errors pulling the updates, that's fine.
1124 # It may be secondary repositories that we don't really care about.
1125 apt_get update || /bin/true
1126 REPOS_UPDATED=True
1127 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001128 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001129 fi
1130}
1131
1132function real_install_package {
1133 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001134 apt_get install "$@"
1135 elif is_fedora; then
1136 yum_install "$@"
1137 elif is_suse; then
1138 zypper_install "$@"
1139 else
1140 exit_distro_not_supported "installing packages"
1141 fi
1142}
1143
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001144# Distro-agnostic package installer
1145# install_package package [package ...]
1146function install_package {
1147 update_package_repo
1148 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1149}
1150
Dean Troyerdff49a22014-01-30 15:37:40 -06001151# Distro-agnostic function to tell if a package is installed
1152# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001153function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001154 if [[ -z "$@" ]]; then
1155 return 1
1156 fi
1157
1158 if [[ -z "$os_PACKAGE" ]]; then
1159 GetOSVersion
1160 fi
1161
1162 if [[ "$os_PACKAGE" = "deb" ]]; then
1163 dpkg -s "$@" > /dev/null 2> /dev/null
1164 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1165 rpm --quiet -q "$@"
1166 else
1167 exit_distro_not_supported "finding if a package is installed"
1168 fi
1169}
1170
1171# Distro-agnostic package uninstaller
1172# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001173function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001174 if is_ubuntu; then
1175 apt_get purge "$@"
1176 elif is_fedora; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001177 sudo $YUM remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001178 elif is_suse; then
1179 sudo zypper rm "$@"
1180 else
1181 exit_distro_not_supported "uninstalling packages"
1182 fi
1183}
1184
1185# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001186# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001187# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001188function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001189 [[ "$OFFLINE" = "True" ]] && return
1190 local sudo="sudo"
1191 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001192
1193 # The manual check for missing packages is because yum -y assumes
1194 # missing packages are OK. See
1195 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001196 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1197 no_proxy=$no_proxy \
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001198 $YUM install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001199 awk '
1200 BEGIN { fail=0 }
1201 /No package/ { fail=1 }
1202 { print }
1203 END { exit fail }' || \
1204 die $LINENO "Missing packages detected"
1205
1206 # also ensure we catch a yum failure
1207 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001208 die $LINENO "$YUM install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001209 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001210}
1211
1212# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001213# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001214# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001215function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001216 [[ "$OFFLINE" = "True" ]] && return
1217 local sudo="sudo"
1218 [[ "$(id -u)" = "0" ]] && sudo="env"
1219 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1220 zypper --non-interactive install --auto-agree-with-licenses "$@"
1221}
1222
1223
1224# Process Functions
1225# =================
1226
1227# _run_process() is designed to be backgrounded by run_process() to simulate a
1228# fork. It includes the dirty work of closing extra filehandles and preparing log
1229# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001230# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001231# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001232# If an optional group is provided sg will be used to set the group of
1233# the command.
1234# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001235function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001236 local service=$1
1237 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001238 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001239
1240 # Undo logging redirections and close the extra descriptors
1241 exec 1>&3
1242 exec 2>&3
1243 exec 3>&-
1244 exec 6>&-
1245
1246 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001247 exec 1>&${SCREEN_LOGDIR}/screen-${service}.log.${CURRENT_LOG_TIME} 2>&1
1248 ln -sf ${SCREEN_LOGDIR}/screen-${service}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001249
1250 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1251 export PYTHONUNBUFFERED=1
1252 fi
1253
Dean Troyer3159a822014-08-27 14:13:58 -05001254 # Run under ``setsid`` to force the process to become a session and group leader.
1255 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001256 if [[ -n "$group" ]]; then
1257 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1258 else
1259 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1260 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001261
1262 # Just silently exit this process
1263 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001264}
1265
1266# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1267# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001268# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001269# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001270function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001271 SCREEN_NAME=${SCREEN_NAME:-stack}
1272 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1273
1274 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1275 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1276 fi
1277
1278 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1279}
1280
1281# Find out if a process exists by partial name.
1282# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001283function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001284 local name=$1
1285 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001286 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001287 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001288 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001289}
1290
Dean Troyer3159a822014-08-27 14:13:58 -05001291# Run a single service under screen or directly
1292# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001293# If an optional group is provided sg will be used to run the
1294# command as that group.
1295# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001296function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001297 local service=$1
1298 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001299 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001300
Dean Troyer3159a822014-08-27 14:13:58 -05001301 if is_service_enabled $service; then
1302 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001303 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001304 else
1305 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001306 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001307 fi
1308 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001309}
1310
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001311# Helper to launch a process in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001312# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1313# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001314# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001315# Run a command in a shell in a screen window, if an optional group
1316# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001317function screen_process {
1318 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001319 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001320 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001321
Sean Dagueea22a4f2014-06-27 15:21:41 -04001322 SCREEN_NAME=${SCREEN_NAME:-stack}
1323 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1324 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001325
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001326 # Append the process to the screen rc file
1327 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001328
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001329 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001330
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001331 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001332 screen -S $SCREEN_NAME -p $name -X logfile ${SCREEN_LOGDIR}/screen-${name}.log.${CURRENT_LOG_TIME}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001333 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerad5cc982014-12-10 16:35:32 -06001334 ln -sf ${SCREEN_LOGDIR}/screen-${name}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${name}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001335 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001336
1337 # sleep to allow bash to be ready to be send the command - we are
1338 # creating a new window in screen and then sends characters, so if
1339 # bash isn't running by the time we send the command, nothing happens
1340 sleep 3
1341
1342 NL=`echo -ne '\015'`
1343 # This fun command does the following:
1344 # - the passed server command is backgrounded
1345 # - the pid of the background process is saved in the usual place
1346 # - the server process is brought back to the foreground
1347 # - if the server process exits prematurely the fg command errors
1348 # and a message is written to stdout and the process failure file
1349 #
1350 # The pid saved can be used in stop_process() as a process group
1351 # id to kill off all child processes
1352 if [[ -n "$group" ]]; then
1353 command="sg $group '$command'"
1354 fi
1355 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 -06001356}
1357
1358# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001359# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001360# screen_rc service "command-line"
1361function screen_rc {
1362 SCREEN_NAME=${SCREEN_NAME:-stack}
1363 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1364 if [[ ! -e $SCREENRC ]]; then
1365 # Name the screen session
1366 echo "sessionname $SCREEN_NAME" > $SCREENRC
1367 # Set a reasonable statusbar
1368 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1369 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1370 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1371 echo "screen -t shell bash" >> $SCREENRC
1372 fi
1373 # If this service doesn't already exist in the screenrc file
1374 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1375 NL=`echo -ne '\015'`
1376 echo "screen -t $1 bash" >> $SCREENRC
1377 echo "stuff \"$2$NL\"" >> $SCREENRC
1378
1379 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001380 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001381 echo "log on" >>$SCREENRC
1382 fi
1383 fi
1384}
1385
1386# Stop a service in screen
1387# If a PID is available use it, kill the whole process group via TERM
1388# If screen is being used kill the screen window; this will catch processes
1389# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001390# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001391# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001392function screen_stop_service {
1393 local service=$1
1394
Dean Troyerdff49a22014-01-30 15:37:40 -06001395 SCREEN_NAME=${SCREEN_NAME:-stack}
1396 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1397 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1398
Dean Troyer3159a822014-08-27 14:13:58 -05001399 if is_service_enabled $service; then
1400 # Clean up the screen window
1401 screen -S $SCREEN_NAME -p $service -X kill
1402 fi
1403}
1404
1405# Stop a service process
1406# If a PID is available use it, kill the whole process group via TERM
1407# If screen is being used kill the screen window; this will catch processes
1408# that did not leave a PID behind
1409# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1410# stop_process service
1411function stop_process {
1412 local service=$1
1413
1414 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1415 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1416
1417 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001418 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001419 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1420 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1421 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001422 fi
1423 if [[ "$USE_SCREEN" = "True" ]]; then
1424 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001425 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001426 fi
1427 fi
1428}
1429
1430# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001431# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001432# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001433function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001434 local service
1435 local failures
1436 SCREEN_NAME=${SCREEN_NAME:-stack}
1437 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1438
1439
1440 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1441 echo "No service status directory found"
1442 return
1443 fi
1444
1445 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001446 # make this -o errexit safe
1447 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001448
1449 for service in $failures; do
1450 service=`basename $service`
1451 service=${service%.failure}
1452 echo "Error: Service $service is not running"
1453 done
1454
1455 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001456 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001457 fi
1458}
1459
Chris Dent2f27a0e2014-09-09 13:46:02 +01001460# Tail a log file in a screen if USE_SCREEN is true.
1461function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001462 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001463 local logfile=$2
1464
1465 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1466 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001467 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001468 fi
1469}
1470
Dean Troyerdff49a22014-01-30 15:37:40 -06001471
Dean Troyer3159a822014-08-27 14:13:58 -05001472# Deprecated Functions
1473# --------------------
1474
1475# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1476# fork. It includes the dirty work of closing extra filehandles and preparing log
1477# files to produce the same logs as screen_it(). The log filename is derived
1478# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1479# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1480# _old_run_process service "command-line"
1481function _old_run_process {
1482 local service=$1
1483 local command="$2"
1484
1485 # Undo logging redirections and close the extra descriptors
1486 exec 1>&3
1487 exec 2>&3
1488 exec 3>&-
1489 exec 6>&-
1490
1491 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001492 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1493 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001494
1495 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1496 export PYTHONUNBUFFERED=1
1497 fi
1498
1499 exec /bin/bash -c "$command"
1500 die "$service exec failure: $command"
1501}
1502
1503# old_run_process() launches a child process that closes all file descriptors and
1504# then exec's the passed in command. This is meant to duplicate the semantics
1505# of screen_it() without screen. PIDs are written to
1506# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1507# old_run_process service "command-line"
1508function old_run_process {
1509 local service=$1
1510 local command="$2"
1511
1512 # Spawn the child process
1513 _old_run_process "$service" "$command" &
1514 echo $!
1515}
1516
1517# Compatibility for existing start_XXXX() functions
1518# Uses global ``USE_SCREEN``
1519# screen_it service "command-line"
1520function screen_it {
1521 if is_service_enabled $1; then
1522 # Append the service to the screen rc file
1523 screen_rc "$1" "$2"
1524
1525 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001526 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001527 else
1528 # Spawn directly without screen
1529 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1530 fi
1531 fi
1532}
1533
1534# Compatibility for existing stop_XXXX() functions
1535# Stop a service in screen
1536# If a PID is available use it, kill the whole process group via TERM
1537# If screen is being used kill the screen window; this will catch processes
1538# that did not leave a PID behind
1539# screen_stop service
1540function screen_stop {
1541 # Clean up the screen window
1542 stop_process $1
1543}
1544
1545
Dean Troyerdff49a22014-01-30 15:37:40 -06001546# Python Functions
1547# ================
1548
1549# Get the path to the pip command.
1550# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001551function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001552 which pip || which pip-python
1553
1554 if [ $? -ne 0 ]; then
1555 die $LINENO "Unable to find pip; cannot continue"
1556 fi
1557}
1558
1559# Get the path to the direcotry where python executables are installed.
1560# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001561function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001562 if is_fedora || is_suse; then
1563 echo "/usr/bin"
1564 else
1565 echo "/usr/local/bin"
1566 fi
1567}
1568
1569# Wrapper for ``pip install`` to set cache and proxy environment variables
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001570# Uses globals ``OFFLINE``, ``TRACK_DEPENDS``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001571# pip_install package [package ...]
1572function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001573 local xtrace=$(set +o | grep xtrace)
1574 set +o xtrace
1575 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1576 $xtrace
1577 return
1578 fi
1579
Dean Troyerdff49a22014-01-30 15:37:40 -06001580 if [[ -z "$os_PACKAGE" ]]; then
1581 GetOSVersion
1582 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001583 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1584 # TRACK_DEPENDS=True installation creates a circular dependency when
1585 # we attempt to install virtualenv into a virualenv, so we must global
1586 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001587 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001588 local cmd_pip=$DEST/.venv/bin/pip
1589 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001590 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001591 local cmd_pip=$(get_pip_command)
Jeremy Stanley6ec66bb2014-12-22 17:17:51 +00001592 local sudo_pip="sudo -H"
Dean Troyerdff49a22014-01-30 15:37:40 -06001593 fi
1594
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001595 local pip_version=$(python -c "import pip; \
1596 print(pip.__version__.strip('.')[0])")
1597 if (( pip_version<6 )); then
1598 die $LINENO "Currently installed pip version ${pip_version} does not" \
1599 "meet minimum requirements (>=6)."
1600 fi
1601
Sean Dague45917cc2014-02-24 16:09:14 -05001602 $xtrace
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001603 $sudo_pip \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001604 http_proxy=$http_proxy \
1605 https_proxy=$https_proxy \
1606 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001607 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001608 $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001609
Flavio Percoco5a91c352014-10-31 18:48:00 +01001610 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001611 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1612 local test_req="$@/test-requirements.txt"
1613 if [[ -e "$test_req" ]]; then
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001614 $sudo_pip \
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001615 http_proxy=$http_proxy \
1616 https_proxy=$https_proxy \
1617 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001618 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001619 -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001620 fi
1621 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001622}
1623
Sean Daguecc524062014-10-01 09:06:43 -04001624# should we use this library from their git repo, or should we let it
1625# get pulled in via pip dependencies.
1626function use_library_from_git {
1627 local name=$1
1628 local enabled=1
1629 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1630 return $enabled
1631}
1632
1633# setup a library by name. If we are trying to use the library from
1634# git, we'll do a git based install, otherwise we'll punt and the
1635# library should be installed by a requirements pull from another
1636# project.
1637function setup_lib {
1638 local name=$1
1639 local dir=${GITDIR[$name]}
1640 setup_install $dir
1641}
1642
Sean Daguee08ab102014-11-13 17:09:28 -05001643# setup a library by name in editiable mode. If we are trying to use
1644# the library from git, we'll do a git based install, otherwise we'll
1645# punt and the library should be installed by a requirements pull from
1646# another project.
1647#
1648# use this for non namespaced libraries
1649function setup_dev_lib {
1650 local name=$1
1651 local dir=${GITDIR[$name]}
1652 setup_develop $dir
1653}
Sean Daguecc524062014-10-01 09:06:43 -04001654
Sean Dague099e5e32014-03-31 10:35:43 -04001655# this should be used if you want to install globally, all libraries should
1656# use this, especially *oslo* ones
1657function setup_install {
1658 local project_dir=$1
1659 setup_package_with_req_sync $project_dir
1660}
1661
1662# this should be used for projects which run services, like all services
1663function setup_develop {
1664 local project_dir=$1
1665 setup_package_with_req_sync $project_dir -e
1666}
1667
Sean Daguedef15342014-10-27 12:26:04 -04001668# determine if a project as specified by directory is in
1669# projects.txt. This will not be an exact match because we throw away
1670# the namespacing when we clone, but it should be good enough in all
1671# practical ways.
1672function is_in_projects_txt {
1673 local project_dir=$1
1674 local project_name=$(basename $project_dir)
1675 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1676}
1677
Dean Troyeraf616d92014-02-17 12:57:55 -06001678# ``pip install -e`` the package, which processes the dependencies
1679# using pip before running `setup.py develop`
1680#
1681# Updates the dependencies in project_dir from the
1682# openstack/requirements global list before installing anything.
1683#
1684# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1685# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001686function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001687 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001688 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001689
Dean Troyeraf616d92014-02-17 12:57:55 -06001690 # Don't update repo if local changes exist
1691 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001692 # ``errexit`` requires us to trap the exit code when the repo is changed
1693 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001694
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001695 if [[ $update_requirements != "changed" ]]; then
Sean Daguedef15342014-10-27 12:26:04 -04001696 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1697 if is_in_projects_txt $project_dir; then
1698 (cd $REQUIREMENTS_DIR; \
1699 python update.py $project_dir)
1700 else
1701 # soft update projects not found in requirements project.txt
1702 (cd $REQUIREMENTS_DIR; \
1703 python update.py -s $project_dir)
1704 fi
1705 else
1706 (cd $REQUIREMENTS_DIR; \
1707 python update.py $project_dir)
1708 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001709 fi
1710
Sean Dague099e5e32014-03-31 10:35:43 -04001711 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001712
1713 # We've just gone and possibly modified the user's source tree in an
1714 # automated way, which is considered bad form if it's a development
1715 # tree because we've screwed up their next git checkin. So undo it.
1716 #
1717 # However... there are some circumstances, like running in the gate
1718 # where we really really want the overridden version to stick. So provide
1719 # a variable that tells us whether or not we should UNDO the requirements
1720 # changes (this will be set to False in the OpenStack ci gate)
1721 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001722 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001723 (cd $project_dir && git reset --hard)
1724 fi
1725 fi
1726}
1727
1728# ``pip install -e`` the package, which processes the dependencies
1729# using pip before running `setup.py develop`
1730# Uses globals ``STACK_USER``
1731# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001732function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001733 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001734 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001735
Sean Dague099e5e32014-03-31 10:35:43 -04001736 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001737 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001738 if [[ "$flags" == "-e" ]]; then
1739 safe_chown -R $STACK_USER $1/*.egg-info
1740 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001741}
1742
Sean Dague2c65e712014-12-18 09:44:56 -05001743# Plugin Functions
1744# =================
1745
1746DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1747
1748# enable_plugin <name> <url> [branch]
1749#
1750# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1751# ``url`` is a git url
1752# ``branch`` is a gitref. If it's not set, defaults to master
1753function enable_plugin {
1754 local name=$1
1755 local url=$2
1756 local branch=${3:-master}
1757 DEVSTACK_PLUGINS+=",$name"
1758 GITREPO[$name]=$url
1759 GITDIR[$name]=$DEST/$name
1760 GITBRANCH[$name]=$branch
1761}
1762
1763# fetch_plugins
1764#
1765# clones all plugins
1766function fetch_plugins {
1767 local plugins="${DEVSTACK_PLUGINS}"
1768 local plugin
1769
1770 # short circuit if nothing to do
1771 if [[ -z $plugins ]]; then
1772 return
1773 fi
1774
1775 echo "Fetching devstack plugins"
1776 for plugin in ${plugins//,/ }; do
1777 git_clone_by_name $plugin
1778 done
1779}
1780
1781# load_plugin_settings
1782#
1783# Load settings from plugins in the order that they were registered
1784function load_plugin_settings {
1785 local plugins="${DEVSTACK_PLUGINS}"
1786 local plugin
1787
1788 # short circuit if nothing to do
1789 if [[ -z $plugins ]]; then
1790 return
1791 fi
1792
1793 echo "Loading plugin settings"
1794 for plugin in ${plugins//,/ }; do
1795 local dir=${GITDIR[$plugin]}
1796 # source any known settings
1797 if [[ -f $dir/devstack/settings ]]; then
1798 source $dir/devstack/settings
1799 fi
1800 done
1801}
1802
1803# run_plugins
1804#
1805# Run the devstack/plugin.sh in all the plugin directories. These are
1806# run in registration order.
1807function run_plugins {
1808 local mode=$1
1809 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301810
1811 local plugins="${DEVSTACK_PLUGINS}"
1812 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001813 for plugin in ${plugins//,/ }; do
1814 local dir=${GITDIR[$plugin]}
1815 if [[ -f $dir/devstack/plugin.sh ]]; then
1816 source $dir/devstack/plugin.sh $mode $phase
1817 fi
1818 done
1819}
1820
1821function run_phase {
1822 local mode=$1
1823 local phase=$2
1824 if [[ -d $TOP_DIR/extras.d ]]; then
1825 for i in $TOP_DIR/extras.d/*.sh; do
1826 [[ -r $i ]] && source $i $mode $phase
1827 done
1828 fi
1829 # the source phase corresponds to settings loading in plugins
1830 if [[ "$mode" == "source" ]]; then
1831 load_plugin_settings
1832 else
1833 run_plugins $mode $phase
1834 fi
1835}
1836
Dean Troyerdff49a22014-01-30 15:37:40 -06001837
1838# Service Functions
1839# =================
1840
1841# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1842# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001843function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001844 echo "$1" | sed -e '
1845 s/,,/,/g;
1846 s/^,//;
1847 s/,$//
1848 '
1849}
1850
1851# disable_all_services() removes all current services
1852# from ``ENABLED_SERVICES`` to reset the configuration
1853# before a minimal installation
1854# Uses global ``ENABLED_SERVICES``
1855# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001856function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001857 ENABLED_SERVICES=""
1858}
1859
1860# Remove all services starting with '-'. For example, to install all default
1861# services except rabbit (rabbit) set in ``localrc``:
1862# ENABLED_SERVICES+=",-rabbit"
1863# Uses global ``ENABLED_SERVICES``
1864# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001865function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001866 local tmpsvcs="${ENABLED_SERVICES}"
1867 local service
1868 for service in ${tmpsvcs//,/ }; do
1869 if [[ ${service} == -* ]]; then
1870 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1871 fi
1872 done
1873 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1874}
1875
1876# disable_service() removes the services passed as argument to the
1877# ``ENABLED_SERVICES`` list, if they are present.
1878#
1879# For example:
1880# disable_service rabbit
1881#
1882# This function does not know about the special cases
1883# for nova, glance, and neutron built into is_service_enabled().
1884# Uses global ``ENABLED_SERVICES``
1885# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001886function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001887 local tmpsvcs=",${ENABLED_SERVICES},"
1888 local service
1889 for service in $@; do
1890 if is_service_enabled $service; then
1891 tmpsvcs=${tmpsvcs//,$service,/,}
1892 fi
1893 done
1894 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1895}
1896
1897# enable_service() adds the services passed as argument to the
1898# ``ENABLED_SERVICES`` list, if they are not already present.
1899#
1900# For example:
1901# enable_service qpid
1902#
1903# This function does not know about the special cases
1904# for nova, glance, and neutron built into is_service_enabled().
1905# Uses global ``ENABLED_SERVICES``
1906# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001907function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001908 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001909 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001910 for service in $@; do
1911 if ! is_service_enabled $service; then
1912 tmpsvcs+=",$service"
1913 fi
1914 done
1915 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1916 disable_negated_services
1917}
1918
1919# is_service_enabled() checks if the service(s) specified as arguments are
1920# enabled by the user in ``ENABLED_SERVICES``.
1921#
1922# Multiple services specified as arguments are ``OR``'ed together; the test
1923# is a short-circuit boolean, i.e it returns on the first match.
1924#
1925# There are special cases for some 'catch-all' services::
1926# **nova** returns true if any service enabled start with **n-**
1927# **cinder** returns true if any service enabled start with **c-**
1928# **ceilometer** returns true if any service enabled start with **ceilometer**
1929# **glance** returns true if any service enabled start with **g-**
1930# **neutron** returns true if any service enabled start with **q-**
1931# **swift** returns true if any service enabled start with **s-**
1932# **trove** returns true if any service enabled start with **tr-**
1933# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1934# **s-** services will be enabled. This will be deprecated in the future.
1935#
1936# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1937# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1938# as enabled in this case.
1939#
1940# Uses global ``ENABLED_SERVICES``
1941# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001942function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001943 local xtrace=$(set +o | grep xtrace)
1944 set +o xtrace
1945 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001946 local services=$@
1947 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001948 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001949 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001950
1951 # Look for top-level 'enabled' function for this service
1952 if type is_${service}_enabled >/dev/null 2>&1; then
1953 # A function exists for this service, use it
1954 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001955 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001956 fi
1957
1958 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1959 # are implemented
1960
Sean Dague45917cc2014-02-24 16:09:14 -05001961 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001962 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001963 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1964 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1965 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1966 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1967 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1968 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1969 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1970 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1971 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001972 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001973 done
Sean Dague45917cc2014-02-24 16:09:14 -05001974 $xtrace
1975 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001976}
1977
1978# Toggle enable/disable_service for services that must run exclusive of each other
1979# $1 The name of a variable containing a space-separated list of services
1980# $2 The name of a variable in which to store the enabled service's name
1981# $3 The name of the service to enable
1982function use_exclusive_service {
1983 local options=${!1}
1984 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001985 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001986 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001987 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001988 for opt in $options;do
1989 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1990 done
1991 eval "$out=$selection"
1992 return 0
1993}
1994
1995
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001996# System Functions
1997# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001998
1999# Only run the command if the target file (the last arg) is not on an
2000# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002001function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05002002 local xtrace=$(set +o | grep xtrace)
2003 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002004 local args=( $@ )
2005 local last
2006 local sudo_cmd
2007 local dir_to_check
2008
2009 let last="${#args[*]} - 1"
2010
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002011 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002012 if [ ! -d "$dir_to_check" ]; then
2013 dir_to_check=`dirname "$dir_to_check"`
2014 fi
2015
2016 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002017 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002018 return 0
2019 fi
2020
2021 if [[ $TRACK_DEPENDS = True ]]; then
2022 sudo_cmd="env"
2023 else
2024 sudo_cmd="sudo"
2025 fi
2026
Sean Dague45917cc2014-02-24 16:09:14 -05002027 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002028 $sudo_cmd $@
2029}
2030
2031# Exit 0 if address is in network or 1 if address is not in network
2032# ip-range is in CIDR notation: 1.2.3.4/20
2033# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002034function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002035 local ip=$1
2036 local range=$2
2037 local masklen=${range#*/}
2038 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2039 local subnet=$(maskip $ip $(cidr2netmask $masklen))
2040 [[ $network == $subnet ]]
2041}
2042
2043# Add a user to a group.
2044# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002045function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002046 local user=$1
2047 local group=$2
2048
2049 if [[ -z "$os_VENDOR" ]]; then
2050 GetOSVersion
2051 fi
2052
2053 # SLE11 and openSUSE 12.2 don't have the usual usermod
2054 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
2055 sudo usermod -a -G "$group" "$user"
2056 else
2057 sudo usermod -A "$group" "$user"
2058 fi
2059}
2060
2061# Convert CIDR notation to a IPv4 netmask
2062# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002063function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002064 local maskpat="255 255 255 255"
2065 local maskdgt="254 252 248 240 224 192 128"
2066 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2067 echo ${1-0}.${2-0}.${3-0}.${4-0}
2068}
2069
2070# Gracefully cp only if source file/dir exists
2071# cp_it source destination
2072function cp_it {
2073 if [ -e $1 ] || [ -d $1 ]; then
2074 cp -pRL $1 $2
2075 fi
2076}
2077
2078# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2079# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2080# ``localrc`` or on the command line if necessary::
2081#
2082# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2083#
2084# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2085
Ian Wienandaee18c72014-02-21 15:35:08 +11002086function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06002087 if [[ -n "$http_proxy" ]]; then
2088 export http_proxy=$http_proxy
2089 fi
2090 if [[ -n "$https_proxy" ]]; then
2091 export https_proxy=$https_proxy
2092 fi
2093 if [[ -n "$no_proxy" ]]; then
2094 export no_proxy=$no_proxy
2095 fi
2096}
2097
2098# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002099function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06002100 local mount_type=`stat -f -L -c %T $1`
2101 test "$mount_type" == "nfs"
2102}
2103
2104# Return the network portion of the given IP address using netmask
2105# netmask is in the traditional dotted-quad format
2106# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002107function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002108 local ip=$1
2109 local mask=$2
2110 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2111 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2112 echo $subnet
2113}
2114
2115# Service wrapper to restart services
2116# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002117function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002118 if is_ubuntu; then
2119 sudo /usr/sbin/service $1 restart
2120 else
2121 sudo /sbin/service $1 restart
2122 fi
2123}
2124
2125# Only change permissions of a file or directory if it is not on an
2126# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002127function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002128 _safe_permission_operation chmod $@
2129}
2130
2131# Only change ownership of a file or directory if it is not on an NFS
2132# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002133function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002134 _safe_permission_operation chown $@
2135}
2136
2137# Service wrapper to start services
2138# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002139function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002140 if is_ubuntu; then
2141 sudo /usr/sbin/service $1 start
2142 else
2143 sudo /sbin/service $1 start
2144 fi
2145}
2146
2147# Service wrapper to stop services
2148# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002149function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002150 if is_ubuntu; then
2151 sudo /usr/sbin/service $1 stop
2152 else
2153 sudo /sbin/service $1 stop
2154 fi
2155}
2156
2157
2158# Restore xtrace
2159$XTRACE
2160
2161# Local variables:
2162# mode: shell-script
2163# End: