blob: 4219b62e6882353c794b6b8cf164f2c0403fbbfb [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 {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500817 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100818 # 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
Steve Martinellib74e01c2014-12-18 01:35:35 -0500833# Gets or creates group
834# Usage: get_or_create_group <groupname> [<domain> <description>]
835function get_or_create_group {
836 local domain=${2:+--domain ${2}}
837 local desc="${3:-}"
838 local os_url="$KEYSTONE_SERVICE_URI_V3"
839 # Gets group id
840 local group_id=$(
841 # Creates new group with --or-show
842 openstack --os-token=$OS_TOKEN --os-url=$os_url \
843 --os-identity-api-version=3 group create $1 \
844 $domain --description "$desc" --or-show \
845 -f value -c id
846 )
847 echo $group_id
848}
849
Bartosz Górski0abde392014-02-28 14:15:19 +0100850# Gets or creates user
Alistair Coles24779f62014-10-15 18:57:59 +0100851# Usage: get_or_create_user <username> <password> <project> [<email> [<domain>]]
Bartosz Górski0abde392014-02-28 14:15:19 +0100852function get_or_create_user {
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200853 if [[ ! -z "$4" ]]; then
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500854 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200855 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500856 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200857 fi
Alistair Coles24779f62014-10-15 18:57:59 +0100858 local os_cmd="openstack"
859 local domain=""
860 if [[ ! -z "$5" ]]; then
861 domain="--domain=$5"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500862 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100863 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100864 # Gets user id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500865 local user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500866 # Creates new user with --or-show
Alistair Coles24779f62014-10-15 18:57:59 +0100867 $os_cmd user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100868 $1 \
869 --password "$2" \
870 --project $3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500871 $email \
Alistair Coles24779f62014-10-15 18:57:59 +0100872 $domain \
Steve Martinelli245daa22014-11-14 02:17:22 -0500873 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100874 -f value -c id
875 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500876 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100877}
878
879# Gets or creates project
Alistair Coles24779f62014-10-15 18:57:59 +0100880# Usage: get_or_create_project <name> [<domain>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100881function get_or_create_project {
882 # Gets project id
Alistair Coles24779f62014-10-15 18:57:59 +0100883 local os_cmd="openstack"
884 local domain=""
885 if [[ ! -z "$2" ]]; then
886 domain="--domain=$2"
Steve Martinellib74e01c2014-12-18 01:35:35 -0500887 os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
Alistair Coles24779f62014-10-15 18:57:59 +0100888 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500889 local project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500890 # Creates new project with --or-show
891 $os_cmd project create $1 $domain --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100892 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500893 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100894}
895
896# Gets or creates role
897# Usage: get_or_create_role <name>
898function get_or_create_role {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500899 local role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500900 # Creates role with --or-show
901 openstack role create $1 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100902 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500903 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100904}
905
906# Gets or adds user role
907# Usage: get_or_add_user_role <role> <user> <project>
908function get_or_add_user_role {
909 # Gets user role id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500910 local user_role_id=$(openstack user role list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100911 $2 \
912 --project $3 \
913 --column "ID" \
914 --column "Name" \
915 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500916 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100917 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500918 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100919 $1 \
920 --user $2 \
921 --project $3 \
922 | grep " id " | get_field 2)
923 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500924 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100925}
926
927# Gets or creates service
928# Usage: get_or_create_service <name> <type> <description>
929function get_or_create_service {
930 # Gets service id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500931 local service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100932 # Gets service id
933 openstack service show $1 -f value -c id 2>/dev/null ||
934 # Creates new service if not exists
935 openstack service create \
936 $1 \
937 --type=$2 \
938 --description="$3" \
939 -f value -c id
940 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500941 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100942}
943
944# Gets or creates endpoint
945# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
946function get_or_create_endpoint {
947 # Gets endpoint id
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500948 local endpoint_id=$(openstack endpoint list \
Bartosz Górski0abde392014-02-28 14:15:19 +0100949 --column "ID" \
950 --column "Region" \
951 --column "Service Name" \
952 | grep " $2 " \
953 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500954 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100955 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500956 endpoint_id=$(openstack endpoint create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100957 $1 \
958 --region $2 \
959 --publicurl $3 \
960 --adminurl $4 \
961 --internalurl $5 \
962 | grep " id " | get_field 2)
963 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500964 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100965}
Dean Troyerdff49a22014-01-30 15:37:40 -0600966
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500967
Dean Troyerdff49a22014-01-30 15:37:40 -0600968# Package Functions
969# =================
970
971# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100972function _get_package_dir {
Dean Troyerdff49a22014-01-30 15:37:40 -0600973 local pkg_dir
974 if is_ubuntu; then
Monty Taylor81a016d2014-11-15 17:18:13 -0300975 pkg_dir=$FILES/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600976 elif is_fedora; then
977 pkg_dir=$FILES/rpms
978 elif is_suse; then
979 pkg_dir=$FILES/rpms-suse
980 else
981 exit_distro_not_supported "list of packages"
982 fi
983 echo "$pkg_dir"
984}
985
986# Wrapper for ``apt-get`` to set cache and proxy environment variables
987# Uses globals ``OFFLINE``, ``*_proxy``
988# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100989function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500990 local xtrace=$(set +o | grep xtrace)
991 set +o xtrace
992
Dean Troyerdff49a22014-01-30 15:37:40 -0600993 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
994 local sudo="sudo"
995 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500996
997 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600998 $sudo DEBIAN_FRONTEND=noninteractive \
999 http_proxy=$http_proxy https_proxy=$https_proxy \
1000 no_proxy=$no_proxy \
1001 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
1002}
1003
1004# get_packages() collects a list of package names of any type from the
Monty Taylor81a016d2014-11-15 17:18:13 -03001005# prerequisite files in ``files/{debs|rpms}``. The list is intended
Dean Troyerdff49a22014-01-30 15:37:40 -06001006# to be passed to a package installer such as apt or yum.
1007#
1008# Only packages required for the services in 1st argument will be
1009# included. Two bits of metadata are recognized in the prerequisite files:
1010#
1011# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1012# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1013# of the package to the distros listed. The distro names are case insensitive.
Ian Wienandaee18c72014-02-21 15:35:08 +11001014function get_packages {
Sean Dague45917cc2014-02-24 16:09:14 -05001015 local xtrace=$(set +o | grep xtrace)
1016 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001017 local services=$@
1018 local package_dir=$(_get_package_dir)
1019 local file_to_parse
1020 local service
1021
Flavio Percoco5a91c352014-10-31 18:48:00 +01001022 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
1023
Dean Troyerdff49a22014-01-30 15:37:40 -06001024 if [[ -z "$package_dir" ]]; then
1025 echo "No package directory supplied"
1026 return 1
1027 fi
1028 if [[ -z "$DISTRO" ]]; then
1029 GetDistro
1030 fi
1031 for service in ${services//,/ }; do
1032 # Allow individual services to specify dependencies
1033 if [[ -e ${package_dir}/${service} ]]; then
1034 file_to_parse="${file_to_parse} $service"
1035 fi
1036 # NOTE(sdague) n-api needs glance for now because that's where
1037 # glance client is
1038 if [[ $service == n-api ]]; then
1039 if [[ ! $file_to_parse =~ nova ]]; then
1040 file_to_parse="${file_to_parse} nova"
1041 fi
1042 if [[ ! $file_to_parse =~ glance ]]; then
1043 file_to_parse="${file_to_parse} glance"
1044 fi
1045 elif [[ $service == c-* ]]; then
1046 if [[ ! $file_to_parse =~ cinder ]]; then
1047 file_to_parse="${file_to_parse} cinder"
1048 fi
1049 elif [[ $service == ceilometer-* ]]; then
1050 if [[ ! $file_to_parse =~ ceilometer ]]; then
1051 file_to_parse="${file_to_parse} ceilometer"
1052 fi
1053 elif [[ $service == s-* ]]; then
1054 if [[ ! $file_to_parse =~ swift ]]; then
1055 file_to_parse="${file_to_parse} swift"
1056 fi
1057 elif [[ $service == n-* ]]; then
1058 if [[ ! $file_to_parse =~ nova ]]; then
1059 file_to_parse="${file_to_parse} nova"
1060 fi
1061 elif [[ $service == g-* ]]; then
1062 if [[ ! $file_to_parse =~ glance ]]; then
1063 file_to_parse="${file_to_parse} glance"
1064 fi
1065 elif [[ $service == key* ]]; then
1066 if [[ ! $file_to_parse =~ keystone ]]; then
1067 file_to_parse="${file_to_parse} keystone"
1068 fi
1069 elif [[ $service == q-* ]]; then
1070 if [[ ! $file_to_parse =~ neutron ]]; then
1071 file_to_parse="${file_to_parse} neutron"
1072 fi
Adam Gandelman539ec432014-03-18 18:57:43 -07001073 elif [[ $service == ir-* ]]; then
1074 if [[ ! $file_to_parse =~ ironic ]]; then
1075 file_to_parse="${file_to_parse} ironic"
1076 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001077 fi
1078 done
1079
1080 for file in ${file_to_parse}; do
1081 local fname=${package_dir}/${file}
1082 local OIFS line package distros distro
1083 [[ -e $fname ]] || continue
1084
1085 OIFS=$IFS
1086 IFS=$'\n'
1087 for line in $(<${fname}); do
1088 if [[ $line =~ "NOPRIME" ]]; then
1089 continue
1090 fi
1091
1092 # Assume we want this package
1093 package=${line%#*}
1094 inst_pkg=1
1095
1096 # Look for # dist:xxx in comment
1097 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1098 # We are using BASH regexp matching feature.
1099 package=${BASH_REMATCH[1]}
1100 distros=${BASH_REMATCH[2]}
1101 # In bash ${VAR,,} will lowecase VAR
1102 # Look for a match in the distro list
1103 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1104 # If no match then skip this package
1105 inst_pkg=0
1106 fi
1107 fi
1108
1109 # Look for # testonly in comment
1110 if [[ $line =~ (.*)#.*testonly.* ]]; then
1111 package=${BASH_REMATCH[1]}
1112 # Are we installing test packages? (test for the default value)
1113 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
1114 # If not installing test packages the skip this package
1115 inst_pkg=0
1116 fi
1117 fi
1118
1119 if [[ $inst_pkg = 1 ]]; then
1120 echo $package
1121 fi
1122 done
1123 IFS=$OIFS
1124 done
Sean Dague45917cc2014-02-24 16:09:14 -05001125 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001126}
1127
1128# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001129# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001130# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001131function update_package_repo {
Paul Linchpiner9e179742014-07-13 22:23:00 -07001132 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001133 return 0
1134 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001135
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001136 if is_ubuntu; then
1137 local xtrace=$(set +o | grep xtrace)
1138 set +o xtrace
1139 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1140 # if there are transient errors pulling the updates, that's fine.
1141 # It may be secondary repositories that we don't really care about.
1142 apt_get update || /bin/true
1143 REPOS_UPDATED=True
1144 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001145 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001146 fi
1147}
1148
1149function real_install_package {
1150 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001151 apt_get install "$@"
1152 elif is_fedora; then
1153 yum_install "$@"
1154 elif is_suse; then
1155 zypper_install "$@"
1156 else
1157 exit_distro_not_supported "installing packages"
1158 fi
1159}
1160
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001161# Distro-agnostic package installer
1162# install_package package [package ...]
1163function install_package {
1164 update_package_repo
1165 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1166}
1167
Dean Troyerdff49a22014-01-30 15:37:40 -06001168# Distro-agnostic function to tell if a package is installed
1169# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001170function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001171 if [[ -z "$@" ]]; then
1172 return 1
1173 fi
1174
1175 if [[ -z "$os_PACKAGE" ]]; then
1176 GetOSVersion
1177 fi
1178
1179 if [[ "$os_PACKAGE" = "deb" ]]; then
1180 dpkg -s "$@" > /dev/null 2> /dev/null
1181 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1182 rpm --quiet -q "$@"
1183 else
1184 exit_distro_not_supported "finding if a package is installed"
1185 fi
1186}
1187
1188# Distro-agnostic package uninstaller
1189# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001190function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001191 if is_ubuntu; then
1192 apt_get purge "$@"
1193 elif is_fedora; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001194 sudo $YUM remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001195 elif is_suse; then
1196 sudo zypper rm "$@"
1197 else
1198 exit_distro_not_supported "uninstalling packages"
1199 fi
1200}
1201
1202# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001203# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001204# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001205function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001206 [[ "$OFFLINE" = "True" ]] && return
1207 local sudo="sudo"
1208 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001209
1210 # The manual check for missing packages is because yum -y assumes
1211 # missing packages are OK. See
1212 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Dean Troyerdff49a22014-01-30 15:37:40 -06001213 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1214 no_proxy=$no_proxy \
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001215 $YUM install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001216 awk '
1217 BEGIN { fail=0 }
1218 /No package/ { fail=1 }
1219 { print }
1220 END { exit fail }' || \
1221 die $LINENO "Missing packages detected"
1222
1223 # also ensure we catch a yum failure
1224 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001225 die $LINENO "$YUM install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001226 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001227}
1228
1229# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001230# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001231# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001232function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001233 [[ "$OFFLINE" = "True" ]] && return
1234 local sudo="sudo"
1235 [[ "$(id -u)" = "0" ]] && sudo="env"
1236 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1237 zypper --non-interactive install --auto-agree-with-licenses "$@"
1238}
1239
1240
1241# Process Functions
1242# =================
1243
1244# _run_process() is designed to be backgrounded by run_process() to simulate a
1245# fork. It includes the dirty work of closing extra filehandles and preparing log
1246# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001247# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
Dean Troyer3159a822014-08-27 14:13:58 -05001248# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001249# If an optional group is provided sg will be used to set the group of
1250# the command.
1251# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001252function _run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001253 local service=$1
1254 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001255 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001256
1257 # Undo logging redirections and close the extra descriptors
1258 exec 1>&3
1259 exec 2>&3
1260 exec 3>&-
1261 exec 6>&-
1262
1263 if [[ -n ${SCREEN_LOGDIR} ]]; then
Chris Dent2f27a0e2014-09-09 13:46:02 +01001264 exec 1>&${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log 2>&1
1265 ln -sf ${SCREEN_LOGDIR}/screen-${service}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${service}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001266
1267 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1268 export PYTHONUNBUFFERED=1
1269 fi
1270
Dean Troyer3159a822014-08-27 14:13:58 -05001271 # Run under ``setsid`` to force the process to become a session and group leader.
1272 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001273 if [[ -n "$group" ]]; then
1274 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1275 else
1276 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1277 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001278
1279 # Just silently exit this process
1280 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001281}
1282
1283# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1284# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001285# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001286# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001287function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001288 SCREEN_NAME=${SCREEN_NAME:-stack}
1289 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1290
1291 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1292 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1293 fi
1294
1295 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1296}
1297
1298# Find out if a process exists by partial name.
1299# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001300function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001301 local name=$1
1302 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001303 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001304 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001305 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001306}
1307
Dean Troyer3159a822014-08-27 14:13:58 -05001308# Run a single service under screen or directly
1309# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001310# If an optional group is provided sg will be used to run the
1311# command as that group.
1312# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001313function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001314 local service=$1
1315 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001316 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001317
Dean Troyer3159a822014-08-27 14:13:58 -05001318 if is_service_enabled $service; then
1319 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001320 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001321 else
1322 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001323 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001324 fi
1325 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001326}
1327
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001328# Helper to launch a process in a named screen
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001329# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_NAME``, ``SCREEN_LOGDIR``,
1330# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001331# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001332# Run a command in a shell in a screen window, if an optional group
1333# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001334function screen_process {
1335 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001336 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001337 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001338
Sean Dagueea22a4f2014-06-27 15:21:41 -04001339 SCREEN_NAME=${SCREEN_NAME:-stack}
1340 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1341 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001342
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001343 # Append the process to the screen rc file
1344 screen_rc "$name" "$command"
Dean Troyerdff49a22014-01-30 15:37:40 -06001345
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001346 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001347
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001348 if [[ -n ${SCREEN_LOGDIR} ]]; then
1349 screen -S $SCREEN_NAME -p $name -X logfile ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log
1350 screen -S $SCREEN_NAME -p $name -X log on
1351 ln -sf ${SCREEN_LOGDIR}/screen-${name}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${name}.log
Dean Troyerdff49a22014-01-30 15:37:40 -06001352 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001353
1354 # sleep to allow bash to be ready to be send the command - we are
1355 # creating a new window in screen and then sends characters, so if
1356 # bash isn't running by the time we send the command, nothing happens
1357 sleep 3
1358
1359 NL=`echo -ne '\015'`
1360 # This fun command does the following:
1361 # - the passed server command is backgrounded
1362 # - the pid of the background process is saved in the usual place
1363 # - the server process is brought back to the foreground
1364 # - if the server process exits prematurely the fg command errors
1365 # and a message is written to stdout and the process failure file
1366 #
1367 # The pid saved can be used in stop_process() as a process group
1368 # id to kill off all child processes
1369 if [[ -n "$group" ]]; then
1370 command="sg $group '$command'"
1371 fi
1372 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 -06001373}
1374
1375# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001376# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001377# screen_rc service "command-line"
1378function screen_rc {
1379 SCREEN_NAME=${SCREEN_NAME:-stack}
1380 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1381 if [[ ! -e $SCREENRC ]]; then
1382 # Name the screen session
1383 echo "sessionname $SCREEN_NAME" > $SCREENRC
1384 # Set a reasonable statusbar
1385 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1386 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1387 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1388 echo "screen -t shell bash" >> $SCREENRC
1389 fi
1390 # If this service doesn't already exist in the screenrc file
1391 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1392 NL=`echo -ne '\015'`
1393 echo "screen -t $1 bash" >> $SCREENRC
1394 echo "stuff \"$2$NL\"" >> $SCREENRC
1395
1396 if [[ -n ${SCREEN_LOGDIR} ]]; then
1397 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1398 echo "log on" >>$SCREENRC
1399 fi
1400 fi
1401}
1402
1403# Stop a service in screen
1404# If a PID is available use it, kill the whole process group via TERM
1405# If screen is being used kill the screen window; this will catch processes
1406# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001407# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001408# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001409function screen_stop_service {
1410 local service=$1
1411
Dean Troyerdff49a22014-01-30 15:37:40 -06001412 SCREEN_NAME=${SCREEN_NAME:-stack}
1413 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1414 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1415
Dean Troyer3159a822014-08-27 14:13:58 -05001416 if is_service_enabled $service; then
1417 # Clean up the screen window
1418 screen -S $SCREEN_NAME -p $service -X kill
1419 fi
1420}
1421
1422# Stop a service process
1423# If a PID is available use it, kill the whole process group via TERM
1424# If screen is being used kill the screen window; this will catch processes
1425# that did not leave a PID behind
1426# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1427# stop_process service
1428function stop_process {
1429 local service=$1
1430
1431 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1432 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1433
1434 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001435 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001436 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1437 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1438 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001439 fi
1440 if [[ "$USE_SCREEN" = "True" ]]; then
1441 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001442 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001443 fi
1444 fi
1445}
1446
1447# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001448# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001449# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001450function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001451 local service
1452 local failures
1453 SCREEN_NAME=${SCREEN_NAME:-stack}
1454 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1455
1456
1457 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1458 echo "No service status directory found"
1459 return
1460 fi
1461
1462 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001463 # make this -o errexit safe
1464 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001465
1466 for service in $failures; do
1467 service=`basename $service`
1468 service=${service%.failure}
1469 echo "Error: Service $service is not running"
1470 done
1471
1472 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001473 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001474 fi
1475}
1476
Chris Dent2f27a0e2014-09-09 13:46:02 +01001477# Tail a log file in a screen if USE_SCREEN is true.
1478function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001479 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001480 local logfile=$2
1481
1482 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1483 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001484 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001485 fi
1486}
1487
Dean Troyerdff49a22014-01-30 15:37:40 -06001488
Dean Troyer3159a822014-08-27 14:13:58 -05001489# Deprecated Functions
1490# --------------------
1491
1492# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1493# fork. It includes the dirty work of closing extra filehandles and preparing log
1494# files to produce the same logs as screen_it(). The log filename is derived
1495# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1496# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1497# _old_run_process service "command-line"
1498function _old_run_process {
1499 local service=$1
1500 local command="$2"
1501
1502 # Undo logging redirections and close the extra descriptors
1503 exec 1>&3
1504 exec 2>&3
1505 exec 3>&-
1506 exec 6>&-
1507
1508 if [[ -n ${SCREEN_LOGDIR} ]]; then
1509 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1510 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1511
1512 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1513 export PYTHONUNBUFFERED=1
1514 fi
1515
1516 exec /bin/bash -c "$command"
1517 die "$service exec failure: $command"
1518}
1519
1520# old_run_process() launches a child process that closes all file descriptors and
1521# then exec's the passed in command. This is meant to duplicate the semantics
1522# of screen_it() without screen. PIDs are written to
1523# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1524# old_run_process service "command-line"
1525function old_run_process {
1526 local service=$1
1527 local command="$2"
1528
1529 # Spawn the child process
1530 _old_run_process "$service" "$command" &
1531 echo $!
1532}
1533
1534# Compatibility for existing start_XXXX() functions
1535# Uses global ``USE_SCREEN``
1536# screen_it service "command-line"
1537function screen_it {
1538 if is_service_enabled $1; then
1539 # Append the service to the screen rc file
1540 screen_rc "$1" "$2"
1541
1542 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001543 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001544 else
1545 # Spawn directly without screen
1546 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1547 fi
1548 fi
1549}
1550
1551# Compatibility for existing stop_XXXX() functions
1552# Stop a service in screen
1553# If a PID is available use it, kill the whole process group via TERM
1554# If screen is being used kill the screen window; this will catch processes
1555# that did not leave a PID behind
1556# screen_stop service
1557function screen_stop {
1558 # Clean up the screen window
1559 stop_process $1
1560}
1561
1562
Dean Troyerdff49a22014-01-30 15:37:40 -06001563# Python Functions
1564# ================
1565
1566# Get the path to the pip command.
1567# get_pip_command
Ian Wienandaee18c72014-02-21 15:35:08 +11001568function get_pip_command {
Dean Troyerdff49a22014-01-30 15:37:40 -06001569 which pip || which pip-python
1570
1571 if [ $? -ne 0 ]; then
1572 die $LINENO "Unable to find pip; cannot continue"
1573 fi
1574}
1575
1576# Get the path to the direcotry where python executables are installed.
1577# get_python_exec_prefix
Ian Wienandaee18c72014-02-21 15:35:08 +11001578function get_python_exec_prefix {
Dean Troyerdff49a22014-01-30 15:37:40 -06001579 if is_fedora || is_suse; then
1580 echo "/usr/bin"
1581 else
1582 echo "/usr/local/bin"
1583 fi
1584}
1585
1586# Wrapper for ``pip install`` to set cache and proxy environment variables
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001587# Uses globals ``OFFLINE``, ``TRACK_DEPENDS``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001588# pip_install package [package ...]
1589function pip_install {
Sean Dague45917cc2014-02-24 16:09:14 -05001590 local xtrace=$(set +o | grep xtrace)
1591 set +o xtrace
1592 if [[ "$OFFLINE" = "True" || -z "$@" ]]; then
1593 $xtrace
1594 return
1595 fi
1596
Dean Troyerdff49a22014-01-30 15:37:40 -06001597 if [[ -z "$os_PACKAGE" ]]; then
1598 GetOSVersion
1599 fi
Robbie Harwood (frozencemetery)1229a082014-07-31 13:55:06 -04001600 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1601 # TRACK_DEPENDS=True installation creates a circular dependency when
1602 # we attempt to install virtualenv into a virualenv, so we must global
1603 # that installation.
Dean Troyerdff49a22014-01-30 15:37:40 -06001604 source $DEST/.venv/bin/activate
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001605 local cmd_pip=$DEST/.venv/bin/pip
1606 local sudo_pip="env"
Dean Troyerdff49a22014-01-30 15:37:40 -06001607 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001608 local cmd_pip=$(get_pip_command)
Jeremy Stanley6ec66bb2014-12-22 17:17:51 +00001609 local sudo_pip="sudo -H"
Dean Troyerdff49a22014-01-30 15:37:40 -06001610 fi
1611
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001612 local pip_version=$(python -c "import pip; \
1613 print(pip.__version__.strip('.')[0])")
1614 if (( pip_version<6 )); then
1615 die $LINENO "Currently installed pip version ${pip_version} does not" \
1616 "meet minimum requirements (>=6)."
1617 fi
1618
Sean Dague45917cc2014-02-24 16:09:14 -05001619 $xtrace
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001620 $sudo_pip \
Yves-Gwenael Bourhisd79a8ac2014-04-14 14:49:07 +02001621 http_proxy=$http_proxy \
1622 https_proxy=$https_proxy \
1623 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001624 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001625 $@
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001626
Flavio Percoco5a91c352014-10-31 18:48:00 +01001627 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False $INSTALL_TESTONLY_PACKAGES)
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001628 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1629 local test_req="$@/test-requirements.txt"
1630 if [[ -e "$test_req" ]]; then
Radoslaw Smigielski6ce071b2015-01-13 06:29:31 +00001631 $sudo_pip \
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001632 http_proxy=$http_proxy \
1633 https_proxy=$https_proxy \
1634 no_proxy=$no_proxy \
Sean Daguec53e8362014-09-30 22:37:52 -04001635 $cmd_pip install \
Attila Fazekasaf81d672014-11-10 09:04:54 +01001636 -r $test_req
Sean Daguef3f4b0a2014-07-15 12:07:42 +02001637 fi
1638 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001639}
1640
Sean Daguecc524062014-10-01 09:06:43 -04001641# should we use this library from their git repo, or should we let it
1642# get pulled in via pip dependencies.
1643function use_library_from_git {
1644 local name=$1
1645 local enabled=1
1646 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1647 return $enabled
1648}
1649
1650# setup a library by name. If we are trying to use the library from
1651# git, we'll do a git based install, otherwise we'll punt and the
1652# library should be installed by a requirements pull from another
1653# project.
1654function setup_lib {
1655 local name=$1
1656 local dir=${GITDIR[$name]}
1657 setup_install $dir
1658}
1659
Sean Daguee08ab102014-11-13 17:09:28 -05001660# setup a library by name in editiable mode. If we are trying to use
1661# the library from git, we'll do a git based install, otherwise we'll
1662# punt and the library should be installed by a requirements pull from
1663# another project.
1664#
1665# use this for non namespaced libraries
1666function setup_dev_lib {
1667 local name=$1
1668 local dir=${GITDIR[$name]}
1669 setup_develop $dir
1670}
Sean Daguecc524062014-10-01 09:06:43 -04001671
Sean Dague099e5e32014-03-31 10:35:43 -04001672# this should be used if you want to install globally, all libraries should
1673# use this, especially *oslo* ones
1674function setup_install {
1675 local project_dir=$1
1676 setup_package_with_req_sync $project_dir
1677}
1678
1679# this should be used for projects which run services, like all services
1680function setup_develop {
1681 local project_dir=$1
1682 setup_package_with_req_sync $project_dir -e
1683}
1684
Sean Daguedef15342014-10-27 12:26:04 -04001685# determine if a project as specified by directory is in
1686# projects.txt. This will not be an exact match because we throw away
1687# the namespacing when we clone, but it should be good enough in all
1688# practical ways.
1689function is_in_projects_txt {
1690 local project_dir=$1
1691 local project_name=$(basename $project_dir)
1692 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1693}
1694
Dean Troyeraf616d92014-02-17 12:57:55 -06001695# ``pip install -e`` the package, which processes the dependencies
1696# using pip before running `setup.py develop`
1697#
1698# Updates the dependencies in project_dir from the
1699# openstack/requirements global list before installing anything.
1700#
1701# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1702# setup_develop directory
Sean Dague099e5e32014-03-31 10:35:43 -04001703function setup_package_with_req_sync {
Dean Troyeraf616d92014-02-17 12:57:55 -06001704 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001705 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001706
Dean Troyeraf616d92014-02-17 12:57:55 -06001707 # Don't update repo if local changes exist
1708 # Don't use buggy "git diff --quiet"
Dean Troyer83b6c992014-02-27 12:41:28 -06001709 # ``errexit`` requires us to trap the exit code when the repo is changed
1710 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
Dean Troyeraf616d92014-02-17 12:57:55 -06001711
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001712 if [[ $update_requirements != "changed" ]]; then
Sean Daguedef15342014-10-27 12:26:04 -04001713 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1714 if is_in_projects_txt $project_dir; then
1715 (cd $REQUIREMENTS_DIR; \
1716 python update.py $project_dir)
1717 else
1718 # soft update projects not found in requirements project.txt
1719 (cd $REQUIREMENTS_DIR; \
1720 python update.py -s $project_dir)
1721 fi
1722 else
1723 (cd $REQUIREMENTS_DIR; \
1724 python update.py $project_dir)
1725 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001726 fi
1727
Sean Dague099e5e32014-03-31 10:35:43 -04001728 setup_package $project_dir $flags
Dean Troyeraf616d92014-02-17 12:57:55 -06001729
1730 # We've just gone and possibly modified the user's source tree in an
1731 # automated way, which is considered bad form if it's a development
1732 # tree because we've screwed up their next git checkin. So undo it.
1733 #
1734 # However... there are some circumstances, like running in the gate
1735 # where we really really want the overridden version to stick. So provide
1736 # a variable that tells us whether or not we should UNDO the requirements
1737 # changes (this will be set to False in the OpenStack ci gate)
1738 if [ $UNDO_REQUIREMENTS = "True" ]; then
YAMAMOTO Takashi3b1f2e42014-02-24 20:30:07 +09001739 if [[ $update_requirements != "changed" ]]; then
Dean Troyeraf616d92014-02-17 12:57:55 -06001740 (cd $project_dir && git reset --hard)
1741 fi
1742 fi
1743}
1744
1745# ``pip install -e`` the package, which processes the dependencies
1746# using pip before running `setup.py develop`
1747# Uses globals ``STACK_USER``
1748# setup_develop_no_requirements_update directory
Sean Dague099e5e32014-03-31 10:35:43 -04001749function setup_package {
Dean Troyeraf616d92014-02-17 12:57:55 -06001750 local project_dir=$1
Sean Dague099e5e32014-03-31 10:35:43 -04001751 local flags=$2
Dean Troyeraf616d92014-02-17 12:57:55 -06001752
Sean Dague099e5e32014-03-31 10:35:43 -04001753 pip_install $flags $project_dir
Dean Troyeraf616d92014-02-17 12:57:55 -06001754 # ensure that further actions can do things like setup.py sdist
Sean Dague099e5e32014-03-31 10:35:43 -04001755 if [[ "$flags" == "-e" ]]; then
1756 safe_chown -R $STACK_USER $1/*.egg-info
1757 fi
Dean Troyeraf616d92014-02-17 12:57:55 -06001758}
1759
Sean Dague2c65e712014-12-18 09:44:56 -05001760# Plugin Functions
1761# =================
1762
1763DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1764
1765# enable_plugin <name> <url> [branch]
1766#
1767# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1768# ``url`` is a git url
1769# ``branch`` is a gitref. If it's not set, defaults to master
1770function enable_plugin {
1771 local name=$1
1772 local url=$2
1773 local branch=${3:-master}
1774 DEVSTACK_PLUGINS+=",$name"
1775 GITREPO[$name]=$url
1776 GITDIR[$name]=$DEST/$name
1777 GITBRANCH[$name]=$branch
1778}
1779
1780# fetch_plugins
1781#
1782# clones all plugins
1783function fetch_plugins {
1784 local plugins="${DEVSTACK_PLUGINS}"
1785 local plugin
1786
1787 # short circuit if nothing to do
1788 if [[ -z $plugins ]]; then
1789 return
1790 fi
1791
1792 echo "Fetching devstack plugins"
1793 for plugin in ${plugins//,/ }; do
1794 git_clone_by_name $plugin
1795 done
1796}
1797
1798# load_plugin_settings
1799#
1800# Load settings from plugins in the order that they were registered
1801function load_plugin_settings {
1802 local plugins="${DEVSTACK_PLUGINS}"
1803 local plugin
1804
1805 # short circuit if nothing to do
1806 if [[ -z $plugins ]]; then
1807 return
1808 fi
1809
1810 echo "Loading plugin settings"
1811 for plugin in ${plugins//,/ }; do
1812 local dir=${GITDIR[$plugin]}
1813 # source any known settings
1814 if [[ -f $dir/devstack/settings ]]; then
1815 source $dir/devstack/settings
1816 fi
1817 done
1818}
1819
1820# run_plugins
1821#
1822# Run the devstack/plugin.sh in all the plugin directories. These are
1823# run in registration order.
1824function run_plugins {
1825 local mode=$1
1826 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301827
1828 local plugins="${DEVSTACK_PLUGINS}"
1829 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001830 for plugin in ${plugins//,/ }; do
1831 local dir=${GITDIR[$plugin]}
1832 if [[ -f $dir/devstack/plugin.sh ]]; then
1833 source $dir/devstack/plugin.sh $mode $phase
1834 fi
1835 done
1836}
1837
1838function run_phase {
1839 local mode=$1
1840 local phase=$2
1841 if [[ -d $TOP_DIR/extras.d ]]; then
1842 for i in $TOP_DIR/extras.d/*.sh; do
1843 [[ -r $i ]] && source $i $mode $phase
1844 done
1845 fi
1846 # the source phase corresponds to settings loading in plugins
1847 if [[ "$mode" == "source" ]]; then
1848 load_plugin_settings
1849 else
1850 run_plugins $mode $phase
1851 fi
1852}
1853
Dean Troyerdff49a22014-01-30 15:37:40 -06001854
1855# Service Functions
1856# =================
1857
1858# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1859# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001860function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001861 echo "$1" | sed -e '
1862 s/,,/,/g;
1863 s/^,//;
1864 s/,$//
1865 '
1866}
1867
1868# disable_all_services() removes all current services
1869# from ``ENABLED_SERVICES`` to reset the configuration
1870# before a minimal installation
1871# Uses global ``ENABLED_SERVICES``
1872# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001873function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001874 ENABLED_SERVICES=""
1875}
1876
1877# Remove all services starting with '-'. For example, to install all default
1878# services except rabbit (rabbit) set in ``localrc``:
1879# ENABLED_SERVICES+=",-rabbit"
1880# Uses global ``ENABLED_SERVICES``
1881# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001882function disable_negated_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001883 local tmpsvcs="${ENABLED_SERVICES}"
1884 local service
1885 for service in ${tmpsvcs//,/ }; do
1886 if [[ ${service} == -* ]]; then
1887 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1888 fi
1889 done
1890 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1891}
1892
1893# disable_service() removes the services passed as argument to the
1894# ``ENABLED_SERVICES`` list, if they are present.
1895#
1896# For example:
1897# disable_service rabbit
1898#
1899# This function does not know about the special cases
1900# for nova, glance, and neutron built into is_service_enabled().
1901# Uses global ``ENABLED_SERVICES``
1902# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001903function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001904 local tmpsvcs=",${ENABLED_SERVICES},"
1905 local service
1906 for service in $@; do
1907 if is_service_enabled $service; then
1908 tmpsvcs=${tmpsvcs//,$service,/,}
1909 fi
1910 done
1911 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1912}
1913
1914# enable_service() adds the services passed as argument to the
1915# ``ENABLED_SERVICES`` list, if they are not already present.
1916#
1917# For example:
1918# enable_service qpid
1919#
1920# This function does not know about the special cases
1921# for nova, glance, and neutron built into is_service_enabled().
1922# Uses global ``ENABLED_SERVICES``
1923# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001924function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001925 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001926 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001927 for service in $@; do
1928 if ! is_service_enabled $service; then
1929 tmpsvcs+=",$service"
1930 fi
1931 done
1932 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1933 disable_negated_services
1934}
1935
1936# is_service_enabled() checks if the service(s) specified as arguments are
1937# enabled by the user in ``ENABLED_SERVICES``.
1938#
1939# Multiple services specified as arguments are ``OR``'ed together; the test
1940# is a short-circuit boolean, i.e it returns on the first match.
1941#
1942# There are special cases for some 'catch-all' services::
1943# **nova** returns true if any service enabled start with **n-**
1944# **cinder** returns true if any service enabled start with **c-**
1945# **ceilometer** returns true if any service enabled start with **ceilometer**
1946# **glance** returns true if any service enabled start with **g-**
1947# **neutron** returns true if any service enabled start with **q-**
1948# **swift** returns true if any service enabled start with **s-**
1949# **trove** returns true if any service enabled start with **tr-**
1950# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1951# **s-** services will be enabled. This will be deprecated in the future.
1952#
1953# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1954# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1955# as enabled in this case.
1956#
1957# Uses global ``ENABLED_SERVICES``
1958# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001959function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001960 local xtrace=$(set +o | grep xtrace)
1961 set +o xtrace
1962 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001963 local services=$@
1964 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001965 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001966 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001967
1968 # Look for top-level 'enabled' function for this service
1969 if type is_${service}_enabled >/dev/null 2>&1; then
1970 # A function exists for this service, use it
1971 is_${service}_enabled
Sean Dague45917cc2014-02-24 16:09:14 -05001972 enabled=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001973 fi
1974
1975 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1976 # are implemented
1977
Sean Dague45917cc2014-02-24 16:09:14 -05001978 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001979 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001980 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1981 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1982 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1983 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1984 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1985 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1986 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1987 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1988 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Brant Knudson966463c2014-08-21 18:24:42 -05001989 [[ ${service} == key-* && ${ENABLED_SERVICES} =~ "key" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001990 done
Sean Dague45917cc2014-02-24 16:09:14 -05001991 $xtrace
1992 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001993}
1994
1995# Toggle enable/disable_service for services that must run exclusive of each other
1996# $1 The name of a variable containing a space-separated list of services
1997# $2 The name of a variable in which to store the enabled service's name
1998# $3 The name of the service to enable
1999function use_exclusive_service {
2000 local options=${!1}
2001 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002002 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06002003 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002004 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06002005 for opt in $options;do
2006 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
2007 done
2008 eval "$out=$selection"
2009 return 0
2010}
2011
2012
Masayuki Igawaf6368d32014-02-20 13:31:26 +09002013# System Functions
2014# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06002015
2016# Only run the command if the target file (the last arg) is not on an
2017# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002018function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05002019 local xtrace=$(set +o | grep xtrace)
2020 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002021 local args=( $@ )
2022 local last
2023 local sudo_cmd
2024 local dir_to_check
2025
2026 let last="${#args[*]} - 1"
2027
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05002028 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06002029 if [ ! -d "$dir_to_check" ]; then
2030 dir_to_check=`dirname "$dir_to_check"`
2031 fi
2032
2033 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05002034 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002035 return 0
2036 fi
2037
2038 if [[ $TRACK_DEPENDS = True ]]; then
2039 sudo_cmd="env"
2040 else
2041 sudo_cmd="sudo"
2042 fi
2043
Sean Dague45917cc2014-02-24 16:09:14 -05002044 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06002045 $sudo_cmd $@
2046}
2047
2048# Exit 0 if address is in network or 1 if address is not in network
2049# ip-range is in CIDR notation: 1.2.3.4/20
2050# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11002051function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06002052 local ip=$1
2053 local range=$2
2054 local masklen=${range#*/}
2055 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
2056 local subnet=$(maskip $ip $(cidr2netmask $masklen))
2057 [[ $network == $subnet ]]
2058}
2059
2060# Add a user to a group.
2061# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11002062function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06002063 local user=$1
2064 local group=$2
2065
2066 if [[ -z "$os_VENDOR" ]]; then
2067 GetOSVersion
2068 fi
2069
2070 # SLE11 and openSUSE 12.2 don't have the usual usermod
2071 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
2072 sudo usermod -a -G "$group" "$user"
2073 else
2074 sudo usermod -A "$group" "$user"
2075 fi
2076}
2077
2078# Convert CIDR notation to a IPv4 netmask
2079# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11002080function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06002081 local maskpat="255 255 255 255"
2082 local maskdgt="254 252 248 240 224 192 128"
2083 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
2084 echo ${1-0}.${2-0}.${3-0}.${4-0}
2085}
2086
2087# Gracefully cp only if source file/dir exists
2088# cp_it source destination
2089function cp_it {
2090 if [ -e $1 ] || [ -d $1 ]; then
2091 cp -pRL $1 $2
2092 fi
2093}
2094
2095# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
2096# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
2097# ``localrc`` or on the command line if necessary::
2098#
2099# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
2100#
2101# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
2102
Ian Wienandaee18c72014-02-21 15:35:08 +11002103function export_proxy_variables {
Dean Troyerdff49a22014-01-30 15:37:40 -06002104 if [[ -n "$http_proxy" ]]; then
2105 export http_proxy=$http_proxy
2106 fi
2107 if [[ -n "$https_proxy" ]]; then
2108 export https_proxy=$https_proxy
2109 fi
2110 if [[ -n "$no_proxy" ]]; then
2111 export no_proxy=$no_proxy
2112 fi
2113}
2114
2115# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002116function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06002117 local mount_type=`stat -f -L -c %T $1`
2118 test "$mount_type" == "nfs"
2119}
2120
2121# Return the network portion of the given IP address using netmask
2122# netmask is in the traditional dotted-quad format
2123# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002124function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002125 local ip=$1
2126 local mask=$2
2127 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2128 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2129 echo $subnet
2130}
2131
2132# Service wrapper to restart services
2133# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002134function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002135 if is_ubuntu; then
2136 sudo /usr/sbin/service $1 restart
2137 else
2138 sudo /sbin/service $1 restart
2139 fi
2140}
2141
2142# Only change permissions of a file or directory if it is not on an
2143# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002144function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002145 _safe_permission_operation chmod $@
2146}
2147
2148# Only change ownership of a file or directory if it is not on an NFS
2149# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002150function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002151 _safe_permission_operation chown $@
2152}
2153
2154# Service wrapper to start services
2155# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002156function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002157 if is_ubuntu; then
2158 sudo /usr/sbin/service $1 start
2159 else
2160 sudo /sbin/service $1 start
2161 fi
2162}
2163
2164# Service wrapper to stop services
2165# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002166function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002167 if is_ubuntu; then
2168 sudo /usr/sbin/service $1 stop
2169 else
2170 sudo /sbin/service $1 stop
2171 fi
2172}
2173
2174
2175# Restore xtrace
2176$XTRACE
2177
2178# Local variables:
2179# mode: shell-script
2180# End: