blob: 4a7cb98900a588610677473a0f34c87ebfefe9f8 [file] [log] [blame]
Dean Troyer7f9aa712012-01-31 12:11:56 -06001# functions - Common functions used by DevStack components
Dean Troyer13dc5cc2012-03-27 14:50:45 -05002#
Dean Troyer4a43b7b2012-08-28 17:43:40 -05003# The following variables are assumed to be defined by certain functions:
Dean Troyer4a43b7b2012-08-28 17:43:40 -05004# ``ENABLED_SERVICES``
DennyZhangb5e11ff2013-10-14 00:53:37 -05005# ``ERROR_ON_CLONE``
Dean Troyer4a43b7b2012-08-28 17:43:40 -05006# ``FILES``
7# ``GLANCE_HOSTPORT``
8# ``OFFLINE``
9# ``PIP_DOWNLOAD_CACHE``
Maru Newby3a87edd2012-10-25 23:01:06 +000010# ``PIP_USE_MIRRORS``
Dean Troyer4a43b7b2012-08-28 17:43:40 -050011# ``RECLONE``
12# ``TRACK_DEPENDS``
13# ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer13dc5cc2012-03-27 14:50:45 -050014
Dean Troyer7f9aa712012-01-31 12:11:56 -060015
Dean Troyer27e32692012-03-16 16:16:56 -050016# Save trace setting
17XTRACE=$(set +o | grep xtrace)
18set +o xtrace
19
Dean Troyer7f9aa712012-01-31 12:11:56 -060020
Dean Troyerd4f69b22013-07-24 12:24:43 -050021# Convert CIDR notation to a IPv4 netmask
22# cidr2netmask cidr-bits
23function cidr2netmask() {
24 local maskpat="255 255 255 255"
25 local maskdgt="254 252 248 240 224 192 128"
26 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
27 echo ${1-0}.${2-0}.${3-0}.${4-0}
28}
29
30
31# Return the network portion of the given IP address using netmask
32# netmask is in the traditional dotted-quad format
33# maskip ip-address netmask
34function maskip() {
35 local ip=$1
36 local mask=$2
37 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
38 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
39 echo $subnet
40}
41
42
43# Exit 0 if address is in network or 1 if address is not in network
44# ip-range is in CIDR notation: 1.2.3.4/20
Dean Troyer4a43b7b2012-08-28 17:43:40 -050045# address_in_net ip-address ip-range
Vishvananda Ishayac9ad14b2012-07-03 20:29:01 +000046function address_in_net() {
Dean Troyerd4f69b22013-07-24 12:24:43 -050047 local ip=$1
48 local range=$2
49 local masklen=${range#*/}
50 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
51 local subnet=$(maskip $ip $(cidr2netmask $masklen))
52 [[ $network == $subnet ]]
Vishvananda Ishayac9ad14b2012-07-03 20:29:01 +000053}
54
55
Dean Troyer4a43b7b2012-08-28 17:43:40 -050056# Wrapper for ``apt-get`` to set cache and proxy environment variables
Adam Spierscb961592013-10-05 12:11:07 +010057# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyer13dc5cc2012-03-27 14:50:45 -050058# apt_get operation package [package ...]
Dean Troyer7f9aa712012-01-31 12:11:56 -060059function apt_get() {
Dean Troyerd0b21e22012-03-07 14:52:25 -060060 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer7f9aa712012-01-31 12:11:56 -060061 local sudo="sudo"
62 [[ "$(id -u)" = "0" ]] && sudo="env"
63 $sudo DEBIAN_FRONTEND=noninteractive \
64 http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +090065 no_proxy=$no_proxy \
Dean Troyer7f9aa712012-01-31 12:11:56 -060066 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
67}
68
69
70# Gracefully cp only if source file/dir exists
71# cp_it source destination
72function cp_it {
73 if [ -e $1 ] || [ -d $1 ]; then
74 cp -pRL $1 $2
75 fi
76}
77
78
Kui Shi5e28a3e2013-08-02 17:26:28 +080079# Prints backtrace info
80# filename:lineno:function
81function backtrace {
82 local level=$1
83 local deep=$((${#BASH_SOURCE[@]} - 1))
84 echo "[Call Trace]"
85 while [ $level -le $deep ]; do
86 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
87 deep=$((deep - 1))
88 done
89}
90
91
Dean Troyerac93efb2013-03-13 14:30:54 -050092# Prints line number and "message" then exits
93# die $LINENO "message"
Dean Troyer27e32692012-03-16 16:16:56 -050094function die() {
Dean Troyer489bd2a2012-03-02 10:44:29 -060095 local exitcode=$?
Dean Troyer896eb662013-04-05 15:02:01 -050096 set +o xtrace
97 local line=$1; shift
Nachi Ueno07115eb2013-02-26 12:38:18 -080098 if [ $exitcode == 0 ]; then
99 exitcode=1
100 fi
Kui Shi5e28a3e2013-08-02 17:26:28 +0800101 backtrace 2
Dean Troyer896eb662013-04-05 15:02:01 -0500102 err $line "$*"
Dean Troyer27e32692012-03-16 16:16:56 -0500103 exit $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -0600104}
105
106
107# Checks an environment variable is not set or has length 0 OR if the
108# exit code is non-zero and prints "message" and exits
109# NOTE: env-var is the variable name without a '$'
Dean Troyerac93efb2013-03-13 14:30:54 -0500110# die_if_not_set $LINENO env-var "message"
Dean Troyer489bd2a2012-03-02 10:44:29 -0600111function die_if_not_set() {
Dean Troyer896eb662013-04-05 15:02:01 -0500112 local exitcode=$?
113 FXTRACE=$(set +o | grep xtrace)
114 set +o xtrace
115 local line=$1; shift
116 local evar=$1; shift
117 if ! is_set $evar || [ $exitcode != 0 ]; then
118 die $line "$*"
119 fi
120 $FXTRACE
121}
122
123
124# Prints line number and "message" in error format
125# err $LINENO "message"
126function err() {
127 local exitcode=$?
128 errXTRACE=$(set +o | grep xtrace)
129 set +o xtrace
Kui Shi17df0772013-08-02 17:55:41 +0800130 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
Dean Troyer896eb662013-04-05 15:02:01 -0500131 echo $msg 1>&2;
132 if [[ -n ${SCREEN_LOGDIR} ]]; then
133 echo $msg >> "${SCREEN_LOGDIR}/error.log"
134 fi
135 $errXTRACE
136 return $exitcode
137}
138
139
140# Checks an environment variable is not set or has length 0 OR if the
141# exit code is non-zero and prints "message"
142# NOTE: env-var is the variable name without a '$'
143# err_if_not_set $LINENO env-var "message"
144function err_if_not_set() {
145 local exitcode=$?
146 errinsXTRACE=$(set +o | grep xtrace)
147 set +o xtrace
148 local line=$1; shift
149 local evar=$1; shift
150 if ! is_set $evar || [ $exitcode != 0 ]; then
151 err $line "$*"
152 fi
153 $errinsXTRACE
154 return $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -0600155}
156
157
Dean Troyer893e6632013-09-13 15:05:51 -0500158# Prints line number and "message" in warning format
159# warn $LINENO "message"
160function warn() {
161 local exitcode=$?
162 errXTRACE=$(set +o | grep xtrace)
163 set +o xtrace
164 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
165 echo $msg 1>&2;
166 if [[ -n ${SCREEN_LOGDIR} ]]; then
167 echo $msg >> "${SCREEN_LOGDIR}/error.log"
168 fi
169 $errXTRACE
170 return $exitcode
171}
172
173
Dean Troyer48352ee2012-12-12 12:50:38 -0600174# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
175# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
176# ``localrc`` or on the command line if necessary::
177#
178# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
179#
180# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
181
182function export_proxy_variables() {
183 if [[ -n "$http_proxy" ]]; then
184 export http_proxy=$http_proxy
185 fi
186 if [[ -n "$https_proxy" ]]; then
187 export https_proxy=$https_proxy
188 fi
189 if [[ -n "$no_proxy" ]]; then
190 export no_proxy=$no_proxy
191 fi
192}
193
194
Dean Troyer489bd2a2012-03-02 10:44:29 -0600195# Grab a numbered field from python prettytable output
196# Fields are numbered starting with 1
197# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
198# get_field field-number
199function get_field() {
200 while read data; do
201 if [ "$1" -lt 0 ]; then
202 field="(\$(NF$1))"
203 else
204 field="\$$(($1 + 1))"
205 fi
206 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
207 done
208}
209
210
Dean Troyerc892bde2013-03-13 14:06:13 -0500211# Get the default value for HOST_IP
212# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
213function get_default_host_ip() {
214 local fixed_range=$1
215 local floating_range=$2
216 local host_ip_iface=$3
217 local host_ip=$4
218
219 # Find the interface used for the default route
220 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
221 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
222 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
223 host_ip=""
224 host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}'`
225 for IP in $host_ips; do
226 # Attempt to filter out IP addresses that are part of the fixed and
227 # floating range. Note that this method only works if the ``netaddr``
228 # python library is installed. If it is not installed, an error
229 # will be printed and the first IP from the interface will be used.
230 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
231 # address.
232 if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then
233 host_ip=$IP
234 break;
235 fi
236 done
237 fi
238 echo $host_ip
239}
240
241
Isaku Yamahata8c438092013-02-12 22:30:56 +0900242function _get_package_dir() {
243 local pkg_dir
244 if is_ubuntu; then
245 pkg_dir=$FILES/apts
246 elif is_fedora; then
247 pkg_dir=$FILES/rpms
248 elif is_suse; then
249 pkg_dir=$FILES/rpms-suse
250 else
251 exit_distro_not_supported "list of packages"
252 fi
253 echo "$pkg_dir"
254}
255
Dean Troyer1a6d4492013-06-03 16:47:36 -0500256
Dean Troyer7e270512012-06-14 15:23:24 -0500257# get_packages() collects a list of package names of any type from the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500258# prerequisite files in ``files/{apts|rpms}``. The list is intended
259# to be passed to a package installer such as apt or yum.
Dean Troyer7e270512012-06-14 15:23:24 -0500260#
Isaku Yamahata8c438092013-02-12 22:30:56 +0900261# Only packages required for the services in 1st argument will be
Dean Troyer7e270512012-06-14 15:23:24 -0500262# included. Two bits of metadata are recognized in the prerequisite files:
Adam Spierscb961592013-10-05 12:11:07 +0100263#
264# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
Dean Troyer7e270512012-06-14 15:23:24 -0500265# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
266# of the package to the distros listed. The distro names are case insensitive.
Dean Troyer7e270512012-06-14 15:23:24 -0500267function get_packages() {
Dean Troyerca5af862013-10-04 13:33:07 -0500268 local services=$@
Isaku Yamahata8c438092013-02-12 22:30:56 +0900269 local package_dir=$(_get_package_dir)
Dean Troyer7e270512012-06-14 15:23:24 -0500270 local file_to_parse
271 local service
272
273 if [[ -z "$package_dir" ]]; then
274 echo "No package directory supplied"
275 return 1
276 fi
277 if [[ -z "$DISTRO" ]]; then
Vincent Untz855c5872012-10-04 13:36:46 +0200278 GetDistro
Dean Troyer7e270512012-06-14 15:23:24 -0500279 fi
Dean Troyerca5af862013-10-04 13:33:07 -0500280 for service in ${services//,/ }; do
Dean Troyer7e270512012-06-14 15:23:24 -0500281 # Allow individual services to specify dependencies
282 if [[ -e ${package_dir}/${service} ]]; then
283 file_to_parse="${file_to_parse} $service"
284 fi
285 # NOTE(sdague) n-api needs glance for now because that's where
286 # glance client is
287 if [[ $service == n-api ]]; then
288 if [[ ! $file_to_parse =~ nova ]]; then
289 file_to_parse="${file_to_parse} nova"
290 fi
291 if [[ ! $file_to_parse =~ glance ]]; then
292 file_to_parse="${file_to_parse} glance"
293 fi
294 elif [[ $service == c-* ]]; then
295 if [[ ! $file_to_parse =~ cinder ]]; then
296 file_to_parse="${file_to_parse} cinder"
297 fi
John H. Tran93361642012-07-26 11:22:05 -0700298 elif [[ $service == ceilometer-* ]]; then
299 if [[ ! $file_to_parse =~ ceilometer ]]; then
300 file_to_parse="${file_to_parse} ceilometer"
301 fi
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100302 elif [[ $service == s-* ]]; then
303 if [[ ! $file_to_parse =~ swift ]]; then
304 file_to_parse="${file_to_parse} swift"
305 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500306 elif [[ $service == n-* ]]; then
307 if [[ ! $file_to_parse =~ nova ]]; then
308 file_to_parse="${file_to_parse} nova"
309 fi
310 elif [[ $service == g-* ]]; then
311 if [[ ! $file_to_parse =~ glance ]]; then
312 file_to_parse="${file_to_parse} glance"
313 fi
314 elif [[ $service == key* ]]; then
315 if [[ ! $file_to_parse =~ keystone ]]; then
316 file_to_parse="${file_to_parse} keystone"
317 fi
Robert Collins0a9954f2012-11-20 11:34:25 +1300318 elif [[ $service == q-* ]]; then
Mark McClainb05c8762013-07-06 23:29:39 -0400319 if [[ ! $file_to_parse =~ neutron ]]; then
320 file_to_parse="${file_to_parse} neutron"
Robert Collins0a9954f2012-11-20 11:34:25 +1300321 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500322 fi
323 done
324
325 for file in ${file_to_parse}; do
326 local fname=${package_dir}/${file}
327 local OIFS line package distros distro
328 [[ -e $fname ]] || continue
329
330 OIFS=$IFS
331 IFS=$'\n'
332 for line in $(<${fname}); do
333 if [[ $line =~ "NOPRIME" ]]; then
334 continue
335 fi
336
Christian Berendt71d56302013-07-22 11:37:42 +0200337 # Assume we want this package
338 package=${line%#*}
339 inst_pkg=1
340
341 # Look for # dist:xxx in comment
Dean Troyer7e270512012-06-14 15:23:24 -0500342 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
343 # We are using BASH regexp matching feature.
344 package=${BASH_REMATCH[1]}
345 distros=${BASH_REMATCH[2]}
346 # In bash ${VAR,,} will lowecase VAR
Christian Berendt71d56302013-07-22 11:37:42 +0200347 # Look for a match in the distro list
348 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
349 # If no match then skip this package
350 inst_pkg=0
351 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500352 fi
353
Christian Berendt71d56302013-07-22 11:37:42 +0200354 # Look for # testonly in comment
355 if [[ $line =~ (.*)#.*testonly.* ]]; then
356 package=${BASH_REMATCH[1]}
357 # Are we installing test packages? (test for the default value)
358 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
359 # If not installing test packages the skip this package
360 inst_pkg=0
361 fi
362 fi
363
364 if [[ $inst_pkg = 1 ]]; then
365 echo $package
366 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500367 done
368 IFS=$OIFS
369 done
370}
371
372
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500373# Determine OS Vendor, Release and Update
374# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
375# Returns results in global variables:
376# os_VENDOR - vendor name
377# os_RELEASE - release
378# os_UPDATE - update
379# os_PACKAGE - package type
380# os_CODENAME - vendor's codename for release
381# GetOSVersion
382GetOSVersion() {
383 # Figure out which vendor we are
Mehdi Abaakoukaee94122013-09-30 11:48:00 +0000384 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500385 # OS/X
386 os_VENDOR=`sw_vers -productName`
387 os_RELEASE=`sw_vers -productVersion`
388 os_UPDATE=${os_RELEASE##*.}
389 os_RELEASE=${os_RELEASE%.*}
390 os_PACKAGE=""
391 if [[ "$os_RELEASE" =~ "10.7" ]]; then
392 os_CODENAME="lion"
393 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
394 os_CODENAME="snow leopard"
395 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
396 os_CODENAME="leopard"
397 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
398 os_CODENAME="tiger"
399 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
400 os_CODENAME="panther"
401 else
402 os_CODENAME=""
403 fi
404 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
405 os_VENDOR=$(lsb_release -i -s)
406 os_RELEASE=$(lsb_release -r -s)
407 os_UPDATE=""
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100408 os_PACKAGE="rpm"
Derek Morton4a8496e2013-04-08 23:46:08 -0500409 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500410 os_PACKAGE="deb"
Vincent Untz856a11e2012-11-21 16:04:12 +0100411 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
412 lsb_release -d -s | grep -q openSUSE
413 if [[ $? -eq 0 ]]; then
414 os_VENDOR="openSUSE"
415 fi
Vincent Untzcd1fe982013-03-12 18:04:29 +0100416 elif [[ $os_VENDOR == "openSUSE project" ]]; then
417 os_VENDOR="openSUSE"
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100418 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
419 os_VENDOR="Red Hat"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500420 fi
421 os_CODENAME=$(lsb_release -c -s)
422 elif [[ -r /etc/redhat-release ]]; then
423 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
424 # CentOS release 5.5 (Final)
425 # CentOS Linux release 6.0 (Final)
426 # Fedora release 16 (Verne)
Bob Ball46691222013-08-12 17:28:50 +0100427 # XenServer release 6.2.0-70446c (xenenterprise)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500428 os_CODENAME=""
Bob Ball46691222013-08-12 17:28:50 +0100429 for r in "Red Hat" CentOS Fedora XenServer; do
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500430 os_VENDOR=$r
431 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
432 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
433 os_CODENAME=${ver#*|}
434 os_RELEASE=${ver%|*}
435 os_UPDATE=${os_RELEASE##*.}
436 os_RELEASE=${os_RELEASE%.*}
437 break
438 fi
439 os_VENDOR=""
440 done
441 os_PACKAGE="rpm"
Vincent Untz856a11e2012-11-21 16:04:12 +0100442 elif [[ -r /etc/SuSE-release ]]; then
443 for r in openSUSE "SUSE Linux"; do
444 if [[ "$r" = "SUSE Linux" ]]; then
445 os_VENDOR="SUSE LINUX"
446 else
447 os_VENDOR=$r
448 fi
449
450 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
451 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
452 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
453 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
454 break
455 fi
456 os_VENDOR=""
457 done
458 os_PACKAGE="rpm"
Émilien Macchib2ef8902013-05-04 00:48:20 +0200459 # If lsb_release is not installed, we should be able to detect Debian OS
460 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
461 os_VENDOR="Debian"
462 os_PACKAGE="deb"
463 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
464 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500465 fi
466 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
467}
468
Andrew Laskif900bd72012-09-05 17:23:14 -0400469
Dean Troyera9e0a482012-07-09 14:07:23 -0500470# Translate the OS version values into common nomenclature
471# Sets ``DISTRO`` from the ``os_*`` values
472function GetDistro() {
473 GetOSVersion
Émilien Macchib2ef8902013-05-04 00:48:20 +0200474 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
475 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
Dean Troyera9e0a482012-07-09 14:07:23 -0500476 DISTRO=$os_CODENAME
477 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
478 # For Fedora, just use 'f' and the release
479 DISTRO="f$os_RELEASE"
Vincent Untz856a11e2012-11-21 16:04:12 +0100480 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
481 DISTRO="opensuse-$os_RELEASE"
482 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
483 # For SLE, also use the service pack
484 if [[ -z "$os_UPDATE" ]]; then
485 DISTRO="sle${os_RELEASE}"
486 else
487 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
488 fi
Ian Wienandd857f4b2013-03-20 14:51:06 +1100489 elif [[ "$os_VENDOR" =~ (Red Hat) || "$os_VENDOR" =~ (CentOS) ]]; then
490 # Drop the . release as we assume it's compatible
491 DISTRO="rhel${os_RELEASE::1}"
Bob Ball46691222013-08-12 17:28:50 +0100492 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
493 DISTRO="xs$os_RELEASE"
Dean Troyera9e0a482012-07-09 14:07:23 -0500494 else
495 # Catch-all for now is Vendor + Release + Update
496 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
497 fi
498 export DISTRO
499}
500
501
Vincent Untz00011c02012-12-06 09:56:32 +0100502# Determine if current distribution is a Fedora-based distribution
Dean Troyer1a6d4492013-06-03 16:47:36 -0500503# (Fedora, RHEL, CentOS, etc).
Vincent Untz00011c02012-12-06 09:56:32 +0100504# is_fedora
505function is_fedora {
506 if [[ -z "$os_VENDOR" ]]; then
507 GetOSVersion
508 fi
509
510 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ]
511}
512
Dean Troyer1a6d4492013-06-03 16:47:36 -0500513
Vincent Untz856a11e2012-11-21 16:04:12 +0100514# Determine if current distribution is a SUSE-based distribution
515# (openSUSE, SLE).
516# is_suse
517function is_suse {
518 if [[ -z "$os_VENDOR" ]]; then
519 GetOSVersion
520 fi
521
Steve Baker1a7bbd22012-12-03 17:04:02 +1300522 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
Vincent Untz856a11e2012-11-21 16:04:12 +0100523}
524
525
Dean Troyer1a6d4492013-06-03 16:47:36 -0500526# Determine if current distribution is an Ubuntu-based distribution
527# It will also detect non-Ubuntu but Debian-based distros
528# is_ubuntu
529function is_ubuntu {
530 if [[ -z "$os_PACKAGE" ]]; then
531 GetOSVersion
532 fi
533 [ "$os_PACKAGE" = "deb" ]
534}
535
536
Vincent Untz00011c02012-12-06 09:56:32 +0100537# Exit after outputting a message about the distribution not being supported.
538# exit_distro_not_supported [optional-string-telling-what-is-missing]
539function exit_distro_not_supported {
540 if [[ -z "$DISTRO" ]]; then
541 GetDistro
542 fi
543
544 if [ $# -gt 0 ]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800545 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
Vincent Untz00011c02012-12-06 09:56:32 +0100546 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800547 die $LINENO "Support for $DISTRO is incomplete."
Vincent Untz00011c02012-12-06 09:56:32 +0100548 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100549}
550
Daniel Jonesfa868cb2013-06-18 15:28:01 -0500551# Utility function for checking machine architecture
552# is_arch arch-type
553function is_arch {
554 ARCH_TYPE=$1
555
556 [ "($uname -m)" = "$ARCH_TYPE" ]
557}
Vincent Untz00011c02012-12-06 09:56:32 +0100558
Dean Troyer7f9aa712012-01-31 12:11:56 -0600559# git clone only if directory doesn't exist already. Since ``DEST`` might not
560# be owned by the installation user, we create the directory and change the
561# ownership to the proper user.
562# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700563# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
564# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500565# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600566# git_clone remote dest-dir branch
567function git_clone {
Dean Troyer7f9aa712012-01-31 12:11:56 -0600568 GIT_REMOTE=$1
569 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300570 GIT_REF=$3
Sirushti Murugesana8d41e32013-09-25 11:30:31 +0530571 RECLONE=$(trueorfalse False $RECLONE)
Dean Troyer7f9aa712012-01-31 12:11:56 -0600572
Sean Dague835db2f2013-09-23 14:17:06 -0400573 if [[ "$OFFLINE" = "True" ]]; then
574 echo "Running in offline mode, clones already exist"
575 # print out the results so we know what change was used in the logs
576 cd $GIT_DEST
Sean Dague45a21f02013-09-25 10:27:27 -0400577 git show --oneline | head -1
Sean Dague835db2f2013-09-23 14:17:06 -0400578 return
579 fi
580
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300581 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600582 # If our branch name is a gerrit style refs/changes/...
583 if [[ ! -d $GIT_DEST ]]; then
Sean Daguedc30bd32013-10-22 07:30:47 -0400584 [[ "$ERROR_ON_CLONE" = "True" ]] && \
585 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer7f9aa712012-01-31 12:11:56 -0600586 git clone $GIT_REMOTE $GIT_DEST
587 fi
588 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300589 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600590 else
591 # do a full clone only if the directory doesn't exist
592 if [[ ! -d $GIT_DEST ]]; then
Sean Daguedc30bd32013-10-22 07:30:47 -0400593 [[ "$ERROR_ON_CLONE" = "True" ]] && \
594 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer7f9aa712012-01-31 12:11:56 -0600595 git clone $GIT_REMOTE $GIT_DEST
596 cd $GIT_DEST
597 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300598 git checkout $GIT_REF
Sirushti Murugesana8d41e32013-09-25 11:30:31 +0530599 elif [[ "$RECLONE" = "True" ]]; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600600 # if it does exist then simulate what clone does if asked to RECLONE
601 cd $GIT_DEST
602 # set the url to pull from and fetch
603 git remote set-url origin $GIT_REMOTE
604 git fetch origin
605 # remove the existing ignored files (like pyc) as they cause breakage
606 # (due to the py files having older timestamps than our pyc, so python
607 # thinks the pyc files are correct using them)
608 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300609
610 # handle GIT_REF accordingly to type (tag, branch)
611 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
612 git_update_tag $GIT_REF
613 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
614 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400615 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
616 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300617 else
Sean Daguedc30bd32013-10-22 07:30:47 -0400618 die $LINENO "$GIT_REF is neither branch nor tag"
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300619 fi
620
Dean Troyer7f9aa712012-01-31 12:11:56 -0600621 fi
622 fi
Sean Dague835db2f2013-09-23 14:17:06 -0400623
624 # print out the results so we know what change was used in the logs
625 cd $GIT_DEST
Sean Dague45a21f02013-09-25 10:27:27 -0400626 git show --oneline | head -1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600627}
628
629
Dean Troyer1a6d4492013-06-03 16:47:36 -0500630# git update using reference as a branch.
631# git_update_branch ref
632function git_update_branch() {
633
634 GIT_BRANCH=$1
635
636 git checkout -f origin/$GIT_BRANCH
637 # a local branch might not exist
638 git branch -D $GIT_BRANCH || true
639 git checkout -b $GIT_BRANCH
640}
641
642
643# git update using reference as a branch.
644# git_update_remote_branch ref
645function git_update_remote_branch() {
646
647 GIT_BRANCH=$1
648
649 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
650}
651
652
653# git update using reference as a tag. Be careful editing source at that repo
654# as working copy will be in a detached mode
655# git_update_tag ref
656function git_update_tag() {
657
658 GIT_TAG=$1
659
660 git tag -d $GIT_TAG
661 # fetching given tag only
662 git fetch origin tag $GIT_TAG
663 git checkout -f $GIT_TAG
664}
665
666
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500667# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200668# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500669function inicomment() {
670 local file=$1
671 local section=$2
672 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100673 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500674}
675
Dean Troyer896eb662013-04-05 15:02:01 -0500676
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200677# Uncomment an option in an INI file
678# iniuncomment config-file section option
679function iniuncomment() {
680 local file=$1
681 local section=$2
682 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100683 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200684}
685
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500686
687# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500688# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500689function iniget() {
690 local file=$1
691 local section=$2
692 local option=$3
693 local line
Attila Fazekas588eb412012-12-20 10:57:16 +0100694 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500695 echo ${line#*=}
696}
697
Dean Troyer896eb662013-04-05 15:02:01 -0500698
Attila Fazekas588eb412012-12-20 10:57:16 +0100699# Determinate is the given option present in the INI file
700# ini_has_option config-file section option
701function ini_has_option() {
702 local file=$1
703 local section=$2
704 local option=$3
705 local line
706 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
707 [ -n "$line" ]
708}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500709
Dean Troyer896eb662013-04-05 15:02:01 -0500710
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500711# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500712# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500713function iniset() {
714 local file=$1
715 local section=$2
716 local option=$3
717 local value=$4
DennyZhangf43f3a52013-10-11 23:09:47 -0500718
719 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500720 # Add section at the end
Attila Fazekas588eb412012-12-20 10:57:16 +0100721 echo -e "\n[$section]" >>"$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500722 fi
Attila Fazekas588eb412012-12-20 10:57:16 +0100723 if ! ini_has_option "$file" "$section" "$option"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500724 # Add it
Attila Fazekas588eb412012-12-20 10:57:16 +0100725 sed -i -e "/^\[$section\]/ a\\
Dean Troyer09e636e2012-03-19 16:31:12 -0500726$option = $value
Attila Fazekas588eb412012-12-20 10:57:16 +0100727" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500728 else
729 # Replace it
Attila Fazekas588eb412012-12-20 10:57:16 +0100730 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500731 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500732}
733
Dean Troyer896eb662013-04-05 15:02:01 -0500734
Lianhao Lu239f3242013-03-01 15:54:02 +0800735# Get a multiple line option from an INI file
736# iniget_multiline config-file section option
737function iniget_multiline() {
738 local file=$1
739 local section=$2
740 local option=$3
741 local values
742 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
743 echo ${values}
744}
745
Dean Troyer896eb662013-04-05 15:02:01 -0500746
Lianhao Lu239f3242013-03-01 15:54:02 +0800747# Set a multiple line option in an INI file
748# iniset_multiline config-file section option value1 value2 valu3 ...
749function iniset_multiline() {
750 local file=$1
751 local section=$2
752 local option=$3
753 shift 3
754 local values
755 for v in $@; do
756 # The later sed command inserts each new value in the line next to
757 # the section identifier, which causes the values to be inserted in
758 # the reverse order. Do a reverse here to keep the original order.
759 values="$v ${values}"
760 done
761 if ! grep -q "^\[$section\]" "$file"; then
762 # Add section at the end
763 echo -e "\n[$section]" >>"$file"
764 else
765 # Remove old values
766 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
767 fi
768 # Add new ones
769 for v in $values; do
770 sed -i -e "/^\[$section\]/ a\\
771$option = $v
772" "$file"
773 done
774}
775
Dean Troyer896eb662013-04-05 15:02:01 -0500776
Lianhao Lu239f3242013-03-01 15:54:02 +0800777# Append a new option in an ini file without replacing the old value
778# iniadd config-file section option value1 value2 value3 ...
779function iniadd() {
780 local file=$1
781 local section=$2
782 local option=$3
783 shift 3
784 local values="$(iniget_multiline $file $section $option) $@"
785 iniset_multiline $file $section $option $values
786}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500787
Dean Troyer896eb662013-04-05 15:02:01 -0500788# Find out if a process exists by partial name.
789# is_running name
790function is_running() {
791 local name=$1
792 ps auxw | grep -v grep | grep ${name} > /dev/null
793 RC=$?
794 # some times I really hate bash reverse binary logic
795 return $RC
796}
797
798
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000799# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500800# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000801#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500802# Multiple services specified as arguments are ``OR``'ed together; the test
803# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000804#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500805# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000806# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500807# **cinder** returns true if any service enabled start with **c-**
808# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000809# **glance** returns true if any service enabled start with **g-**
Mark McClainb05c8762013-07-06 23:29:39 -0400810# **neutron** returns true if any service enabled start with **q-**
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100811# **swift** returns true if any service enabled start with **s-**
Nikhil Manchanda0cccad42012-12-03 18:15:09 -0700812# **trove** returns true if any service enabled start with **tr-**
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100813# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
814# **s-** services will be enabled. This will be deprecated in the future.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500815#
Chris Behrensc62c2b92013-07-24 03:56:13 -0700816# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
817# We also need to make sure to treat **n-cell-region** and **n-cell-child**
818# as enabled in this case.
819#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500820# Uses global ``ENABLED_SERVICES``
821# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000822function is_service_enabled() {
823 services=$@
824 for service in ${services}; do
825 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
Chris Behrensc62c2b92013-07-24 03:56:13 -0700826 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000827 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500828 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700829 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000830 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
Mark McClainb05c8762013-07-06 23:29:39 -0400831 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
Nikhil Manchanda0cccad42012-12-03 18:15:09 -0700832 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && return 0
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100833 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && return 0
834 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000835 done
836 return 1
837}
838
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500839
840# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
841# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400842function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500843 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400844 s/,,/,/g;
845 s/^,//;
846 s/,$//
847 '
848}
849
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500850
Doug Hellmannf04178f2012-07-05 17:10:03 -0400851# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500852# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400853#
854# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800855# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400856#
857# This function does not know about the special cases
Mark McClainb05c8762013-07-06 23:29:39 -0400858# for nova, glance, and neutron built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500859# Uses global ``ENABLED_SERVICES``
860# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400861function enable_service() {
862 local tmpsvcs="${ENABLED_SERVICES}"
863 for service in $@; do
864 if ! is_service_enabled $service; then
865 tmpsvcs+=",$service"
866 fi
867 done
868 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
869 disable_negated_services
870}
871
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500872
Doug Hellmannf04178f2012-07-05 17:10:03 -0400873# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500874# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400875#
876# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800877# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400878#
879# This function does not know about the special cases
Mark McClainb05c8762013-07-06 23:29:39 -0400880# for nova, glance, and neutron built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500881# Uses global ``ENABLED_SERVICES``
882# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400883function disable_service() {
884 local tmpsvcs=",${ENABLED_SERVICES},"
885 local service
886 for service in $@; do
887 if is_service_enabled $service; then
888 tmpsvcs=${tmpsvcs//,$service,/,}
889 fi
890 done
891 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
892}
893
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500894
Doug Hellmannf04178f2012-07-05 17:10:03 -0400895# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500896# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400897# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500898# Uses global ``ENABLED_SERVICES``
899# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400900function disable_all_services() {
901 ENABLED_SERVICES=""
902}
903
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500904
905# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800906# services except rabbit (rabbit) set in ``localrc``:
907# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500908# Uses global ``ENABLED_SERVICES``
909# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400910function disable_negated_services() {
911 local tmpsvcs="${ENABLED_SERVICES}"
912 local service
913 for service in ${tmpsvcs//,/ }; do
914 if [[ ${service} == -* ]]; then
915 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
916 fi
917 done
918 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
919}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600920
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500921
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500922# Distro-agnostic package installer
923# install_package package [package ...]
924function install_package() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100925 if is_ubuntu; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200926 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
927 NO_UPDATE_REPOS=True
928
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500929 apt_get install "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100930 elif is_fedora; then
931 yum_install "$@"
932 elif is_suse; then
933 zypper_install "$@"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500934 else
Vincent Untz00011c02012-12-06 09:56:32 +0100935 exit_distro_not_supported "installing packages"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500936 fi
937}
938
939
Dean Troyer995eb922013-03-07 16:11:40 -0600940# Distro-agnostic package uninstaller
941# uninstall_package package [package ...]
942function uninstall_package() {
943 if is_ubuntu; then
944 apt_get purge "$@"
945 elif is_fedora; then
Ian Wienand2c678cc2013-03-20 13:00:44 +1100946 sudo yum remove -y "$@"
Dean Troyer995eb922013-03-07 16:11:40 -0600947 elif is_suse; then
Adam Spiers6d8fce72013-10-01 15:59:05 +0100948 sudo zypper rm "$@"
Dean Troyer995eb922013-03-07 16:11:40 -0600949 else
950 exit_distro_not_supported "uninstalling packages"
951 fi
952}
953
954
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200955# Distro-agnostic function to tell if a package is installed
956# is_package_installed package [package ...]
957function is_package_installed() {
958 if [[ -z "$@" ]]; then
959 return 1
960 fi
961
962 if [[ -z "$os_PACKAGE" ]]; then
963 GetOSVersion
964 fi
Vincent Untzc18b9652012-12-04 12:36:34 +0100965
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200966 if [[ "$os_PACKAGE" = "deb" ]]; then
Dean Troyer04762cd2013-08-27 17:06:14 -0500967 dpkg -s "$@" > /dev/null 2> /dev/null
Vincent Untz00011c02012-12-06 09:56:32 +0100968 elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200969 rpm --quiet -q "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100970 else
971 exit_distro_not_supported "finding if a package is installed"
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200972 fi
973}
974
975
Dean Troyer489bd2a2012-03-02 10:44:29 -0600976# Test if the named environment variable is set and not zero length
977# is_set env-var
978function is_set() {
979 local var=\$"$1"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100980 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
Dean Troyer489bd2a2012-03-02 10:44:29 -0600981}
982
983
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500984# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +0000985# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
Adam Spierscb961592013-10-05 12:11:07 +0100986# ``TRACK_DEPENDS``, ``*_proxy``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600987# pip_install package [package ...]
988function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600989 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500990 if [[ -z "$os_PACKAGE" ]]; then
991 GetOSVersion
992 fi
Dean Troyercc6b4432013-04-08 15:38:03 -0500993 if [[ $TRACK_DEPENDS = True ]]; then
Monty Taylor47f02062012-07-26 11:09:24 -0500994 source $DEST/.venv/bin/activate
995 CMD_PIP=$DEST/.venv/bin/pip
996 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500997 else
Monty Taylor47f02062012-07-26 11:09:24 -0500998 SUDO_PIP="sudo"
Vincent Untz8ec27222012-11-29 09:25:31 +0100999 CMD_PIP=$(get_pip_command)
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001000 fi
Ian Wienandd67dd872013-04-11 11:14:36 +10001001
Roman Gorodeckij99405a42013-08-07 09:20:36 -04001002 # Mirror option not needed anymore because pypi has CDN available,
1003 # but it's useful in certain circumstances
1004 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
Maru Newby3a87edd2012-10-25 23:01:06 +00001005 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
1006 PIP_MIRROR_OPT="--use-mirrors"
1007 fi
Ian Wienandd67dd872013-04-11 11:14:36 +10001008
Ian Wienand31dcd3e2013-07-16 13:36:34 +10001009 # pip < 1.4 has a bug where it will use an already existing build
1010 # directory unconditionally. Say an earlier component installs
1011 # foo v1.1; pip will have built foo's source in
1012 # /tmp/$USER-pip-build. Even if a later component specifies foo <
1013 # 1.1, the existing extracted build will be used and cause
1014 # confusing errors. By creating unique build directories we avoid
Adam Spierscb961592013-10-05 12:11:07 +01001015 # this problem. See https://github.com/pypa/pip/issues/709
Ian Wienand31dcd3e2013-07-16 13:36:34 +10001016 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1017
Monty Taylor47f02062012-07-26 11:09:24 -05001018 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -06001019 HTTP_PROXY=$http_proxy \
1020 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001021 NO_PROXY=$no_proxy \
Ian Wienand31dcd3e2013-07-16 13:36:34 +10001022 $CMD_PIP install --build=${pip_build_tmp} \
1023 $PIP_MIRROR_OPT $@ \
1024 && $SUDO_PIP rm -rf ${pip_build_tmp}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001025}
1026
1027
Ian Wienand31dcd3e2013-07-16 13:36:34 +10001028# Cleanup anything from /tmp on unstack
1029# clean_tmp
1030function cleanup_tmp {
1031 local tmp_dir=${TMPDIR:-/tmp}
1032
1033 # see comments in pip_install
1034 sudo rm -rf ${tmp_dir}/pip-build.*
1035}
1036
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001037# Service wrapper to restart services
1038# restart_service service-name
1039function restart_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001040 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001041 sudo /usr/sbin/service $1 restart
1042 else
1043 sudo /sbin/service $1 restart
1044 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001045}
1046
1047
Dean Troyer681f3fd2013-02-27 19:00:39 -06001048# _run_process() is designed to be backgrounded by run_process() to simulate a
1049# fork. It includes the dirty work of closing extra filehandles and preparing log
1050# files to produce the same logs as screen_it(). The log filename is derived
1051# from the service name and global-and-now-misnamed SCREEN_LOGDIR
1052# _run_process service "command-line"
1053function _run_process() {
1054 local service=$1
1055 local command="$2"
1056
1057 # Undo logging redirections and close the extra descriptors
1058 exec 1>&3
1059 exec 2>&3
1060 exec 3>&-
1061 exec 6>&-
1062
1063 if [[ -n ${SCREEN_LOGDIR} ]]; then
1064 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1065 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1066
1067 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1068 export PYTHONUNBUFFERED=1
1069 fi
1070
1071 exec /bin/bash -c "$command"
1072 die "$service exec failure: $command"
1073}
1074
1075
1076# run_process() launches a child process that closes all file descriptors and
1077# then exec's the passed in command. This is meant to duplicate the semantics
1078# of screen_it() without screen. PIDs are written to
1079# $SERVICE_DIR/$SCREEN_NAME/$service.pid
1080# run_process service "command-line"
1081function run_process() {
1082 local service=$1
1083 local command="$2"
1084
1085 # Spawn the child process
1086 _run_process "$service" "$command" &
1087 echo $!
1088}
1089
1090
Dean Troyer15733352012-09-06 11:51:30 -05001091# Helper to launch a service in a named screen
1092# screen_it service "command-line"
1093function screen_it {
Dean Troyer15733352012-09-06 11:51:30 -05001094 SCREEN_NAME=${SCREEN_NAME:-stack}
jiajun xua9414242012-12-06 16:30:57 +08001095 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer681f3fd2013-02-27 19:00:39 -06001096 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
jiajun xua9414242012-12-06 16:30:57 +08001097
Dean Troyer15733352012-09-06 11:51:30 -05001098 if is_service_enabled $1; then
1099 # Append the service to the screen rc file
1100 screen_rc "$1" "$2"
1101
Dean Troyer681f3fd2013-02-27 19:00:39 -06001102 if [[ "$USE_SCREEN" = "True" ]]; then
1103 screen -S $SCREEN_NAME -X screen -t $1
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +00001104
Dean Troyer681f3fd2013-02-27 19:00:39 -06001105 if [[ -n ${SCREEN_LOGDIR} ]]; then
1106 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
1107 screen -S $SCREEN_NAME -p $1 -X log on
1108 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1109 fi
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +00001110
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001111 # sleep to allow bash to be ready to be send the command - we are
1112 # creating a new window in screen and then sends characters, so if
1113 # bash isn't running by the time we send the command, nothing happens
1114 sleep 1.5
Dean Troyer15733352012-09-06 11:51:30 -05001115
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001116 NL=`echo -ne '\015'`
Clark Boylan41815cd2013-08-16 14:57:38 -07001117 screen -S $SCREEN_NAME -p $1 -X stuff "$2 || echo \"$1 failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001118 else
Dean Troyer681f3fd2013-02-27 19:00:39 -06001119 # Spawn directly without screen
1120 run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyer15733352012-09-06 11:51:30 -05001121 fi
Dean Troyer15733352012-09-06 11:51:30 -05001122 fi
1123}
1124
1125
1126# Screen rc file builder
1127# screen_rc service "command-line"
1128function screen_rc {
1129 SCREEN_NAME=${SCREEN_NAME:-stack}
1130 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1131 if [[ ! -e $SCREENRC ]]; then
1132 # Name the screen session
1133 echo "sessionname $SCREEN_NAME" > $SCREENRC
1134 # Set a reasonable statusbar
1135 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
Steven Dake30396572013-06-30 16:11:54 -07001136 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1137 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
Dean Troyer15733352012-09-06 11:51:30 -05001138 echo "screen -t shell bash" >> $SCREENRC
1139 fi
1140 # If this service doesn't already exist in the screenrc file
1141 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1142 NL=`echo -ne '\015'`
1143 echo "screen -t $1 bash" >> $SCREENRC
1144 echo "stuff \"$2$NL\"" >> $SCREENRC
1145 fi
1146}
1147
Dean Troyer1a6d4492013-06-03 16:47:36 -05001148
Adam Spierscb961592013-10-05 12:11:07 +01001149# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1150# This is used for ``service_check`` when all the ``screen_it`` are called finished
jiajun xua9414242012-12-06 16:30:57 +08001151# init_service_check
1152function init_service_check() {
1153 SCREEN_NAME=${SCREEN_NAME:-stack}
1154 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1155
1156 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1157 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1158 fi
1159
1160 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1161}
1162
Dean Troyer1a6d4492013-06-03 16:47:36 -05001163
jiajun xua9414242012-12-06 16:30:57 +08001164# Helper to get the status of each running service
1165# service_check
1166function service_check() {
1167 local service
1168 local failures
1169 SCREEN_NAME=${SCREEN_NAME:-stack}
1170 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1171
1172
1173 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1174 echo "No service status directory found"
1175 return
1176 fi
1177
1178 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
1179 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null`
1180
1181 for service in $failures; do
1182 service=`basename $service`
Bob Ball46287d82013-07-30 09:43:17 +01001183 service=${service%.failure}
jiajun xua9414242012-12-06 16:30:57 +08001184 echo "Error: Service $service is not running"
1185 done
1186
1187 if [ -n "$failures" ]; then
1188 echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
1189 fi
1190}
Dean Troyer15733352012-09-06 11:51:30 -05001191
Doug Hellmanne7002672013-09-05 08:10:07 -04001192# Returns true if the directory is on a filesystem mounted via NFS.
1193function is_nfs_directory() {
1194 local mount_type=`stat -f -L -c %T $1`
1195 test "$mount_type" == "nfs"
1196}
1197
1198# Only run the command if the target file (the last arg) is not on an
1199# NFS filesystem.
1200function _safe_permission_operation() {
1201 local args=( $@ )
1202 local last
1203 local sudo_cmd
1204 local dir_to_check
1205
1206 let last="${#args[*]} - 1"
1207
1208 dir_to_check=${args[$last]}
1209 if [ ! -d "$dir_to_check" ]; then
1210 dir_to_check=`dirname "$dir_to_check"`
1211 fi
1212
1213 if is_nfs_directory "$dir_to_check" ; then
1214 return 0
1215 fi
1216
1217 if [[ $TRACK_DEPENDS = True ]]; then
1218 sudo_cmd="env"
1219 else
1220 sudo_cmd="sudo"
1221 fi
1222
1223 $sudo_cmd $@
1224}
1225
1226# Only change ownership of a file or directory if it is not on an NFS
1227# filesystem.
1228function safe_chown() {
1229 _safe_permission_operation chown $@
1230}
1231
1232# Only change permissions of a file or directory if it is not on an
1233# NFS filesystem.
1234function safe_chmod() {
1235 _safe_permission_operation chmod $@
1236}
Dean Troyer1a6d4492013-06-03 16:47:36 -05001237
Monty Taylor408a4a72013-08-02 15:43:47 -04001238# ``pip install -e`` the package, which processes the dependencies
1239# using pip before running `setup.py develop`
Monty Taylorb5bbaac2013-08-06 10:35:02 -03001240# Uses globals ``STACK_USER``, ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001241# setup_develop directory
1242function setup_develop() {
Sean Dague6c844632013-07-31 06:50:14 -04001243 local project_dir=$1
Sean Dague6c844632013-07-31 06:50:14 -04001244
1245 echo "cd $REQUIREMENTS_DIR; $SUDO_CMD python update.py $project_dir"
1246
Dean Troyer62d1d692013-08-01 17:40:40 -05001247 # Don't update repo if local changes exist
Doug Hellmannc3431bf2013-09-06 15:30:22 -04001248 (cd $project_dir && git diff --quiet)
1249 local update_requirements=$?
1250
1251 if [ $update_requirements -eq 0 ]; then
Dean Troyer62d1d692013-08-01 17:40:40 -05001252 (cd $REQUIREMENTS_DIR; \
1253 $SUDO_CMD python update.py $project_dir)
1254 fi
Sean Dague6c844632013-07-31 06:50:14 -04001255
Monty Taylorb5bbaac2013-08-06 10:35:02 -03001256 pip_install -e $project_dir
1257 # ensure that further actions can do things like setup.py sdist
Doug Hellmanne7002672013-09-05 08:10:07 -04001258 safe_chown -R $STACK_USER $1/*.egg-info
Doug Hellmannc3431bf2013-09-06 15:30:22 -04001259
Sean Daguefd98edb2013-10-24 14:57:59 -04001260 # We've just gone and possibly modified the user's source tree in an
1261 # automated way, which is considered bad form if it's a development
1262 # tree because we've screwed up their next git checkin. So undo it.
1263 #
1264 # However... there are some circumstances, like running in the gate
1265 # where we really really want the overridden version to stick. So provide
1266 # a variable that tells us whether or not we should UNDO the requirements
1267 # changes (this will be set to False in the OpenStack ci gate)
1268 if [ $UNDO_REQUIREMENTS = "True"]; then
1269 if [ $update_requirements -eq 0 ]; then
1270 (cd $project_dir && git reset --hard)
1271 fi
Doug Hellmannc3431bf2013-09-06 15:30:22 -04001272 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001273}
1274
1275
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001276# Service wrapper to start services
1277# start_service service-name
1278function start_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001279 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001280 sudo /usr/sbin/service $1 start
1281 else
1282 sudo /sbin/service $1 start
1283 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001284}
1285
1286
1287# Service wrapper to stop services
1288# stop_service service-name
1289function stop_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001290 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001291 sudo /usr/sbin/service $1 stop
1292 else
1293 sudo /sbin/service $1 stop
1294 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -06001295}
1296
1297
1298# Normalize config values to True or False
Sirushti Murugesana8d41e32013-09-25 11:30:31 +05301299# Accepts as False: 0 no No NO false False FALSE
1300# Accepts as True: 1 yes Yes YES true True TRUE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001301# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -06001302function trueorfalse() {
1303 local default=$1
1304 local testval=$2
1305
1306 [[ -z "$testval" ]] && { echo "$default"; return; }
Sirushti Murugesana8d41e32013-09-25 11:30:31 +05301307 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
1308 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
Dean Troyer7f9aa712012-01-31 12:11:56 -06001309 echo "$default"
1310}
Dean Troyer27e32692012-03-16 16:16:56 -05001311
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001312
Adam Spierscb961592013-10-05 12:11:07 +01001313# Retrieve an image from a URL and upload into Glance.
Dean Troyerca0e3d02012-04-13 15:58:37 -05001314# Uses the following variables:
Adam Spierscb961592013-10-05 12:11:07 +01001315#
1316# - ``FILES`` must be set to the cache dir
1317# - ``GLANCE_HOSTPORT``
1318#
Dean Troyerca0e3d02012-04-13 15:58:37 -05001319# upload_image image-url glance-token
1320function upload_image() {
1321 local image_url=$1
1322 local token=$2
1323
1324 # Create a directory for the downloaded image tarballs.
1325 mkdir -p $FILES/images
1326
1327 # Downloads the image (uec ami+aki style), then extracts it.
1328 IMAGE_FNAME=`basename "$image_url"`
1329 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
1330 wget -c $image_url -O $FILES/$IMAGE_FNAME
1331 if [[ $? -ne 0 ]]; then
1332 echo "Not found: $image_url"
1333 return
1334 fi
1335 fi
1336
1337 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
1338 if [[ "$image_url" =~ 'openvz' ]]; then
1339 IMAGE="$FILES/${IMAGE_FNAME}"
1340 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
1341 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format ami --disk-format ami < "${IMAGE}"
1342 return
1343 fi
1344
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -07001345 # vmdk format images
1346 if [[ "$image_url" =~ '.vmdk' ]]; then
1347 IMAGE="$FILES/${IMAGE_FNAME}"
1348 IMAGE_NAME="${IMAGE_FNAME%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -07001349
1350 # Before we can upload vmdk type images to glance, we need to know it's
1351 # disk type, storage adapter, and networking adapter. These values are
1352 # passed to glance as custom properties. We take these values from the
1353 # vmdk filename, which is expected in the following format:
1354 #
1355 # <name>-<disk type>:<storage adapter>:<network adapter>
1356 #
1357 # If the filename does not follow the above format then the vsphere
1358 # driver will supply default values.
1359 property_string=`echo "$IMAGE_NAME" | grep -oP '(?<=-)(?!.*-).+:.+:.+$'`
1360 if [[ ! -z "$property_string" ]]; then
1361 IFS=':' read -a props <<< "$property_string"
1362 vmdk_disktype="${props[0]}"
1363 vmdk_adapter_type="${props[1]}"
1364 vmdk_net_adapter="${props[2]}"
1365 fi
1366
Ryan Hsu49f44862013-10-03 22:27:03 -07001367 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format bare --disk-format vmdk --property vmware_disktype="$vmdk_disktype" --property vmware_adaptertype="$vmdk_adapter_type" --property hw_vif_model="$vmdk_net_adapter" < "${IMAGE}"
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -07001368 return
1369 fi
1370
Mate Lakatbc2ef922013-08-15 18:06:59 +01001371 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -05001372 # and should not be decompressed prior to loading
1373 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
1374 IMAGE="$FILES/${IMAGE_FNAME}"
1375 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
1376 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format=ovf --disk-format=vhd < "${IMAGE}"
1377 return
1378 fi
1379
Mate Lakatbc2ef922013-08-15 18:06:59 +01001380 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
1381 # and should not be decompressed prior to loading.
1382 # Setting metadata, so PV mode is used.
1383 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
1384 IMAGE="$FILES/${IMAGE_FNAME}"
1385 IMAGE_NAME="${IMAGE_FNAME%.xen-raw.tgz}"
1386 glance \
Sean Dague537d4022013-10-22 07:43:22 -04001387 --os-auth-token $token \
1388 --os-image-url http://$GLANCE_HOSTPORT \
1389 image-create \
Mate Lakatbc2ef922013-08-15 18:06:59 +01001390 --name "$IMAGE_NAME" --is-public=True \
1391 --container-format=tgz --disk-format=raw \
1392 --property vm_mode=xen < "${IMAGE}"
1393 return
1394 fi
1395
Dean Troyerca0e3d02012-04-13 15:58:37 -05001396 KERNEL=""
1397 RAMDISK=""
1398 DISK_FORMAT=""
1399 CONTAINER_FORMAT=""
1400 UNPACK=""
1401 case "$IMAGE_FNAME" in
1402 *.tar.gz|*.tgz)
1403 # Extract ami and aki files
1404 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
1405 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
1406 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
1407 xdir="$FILES/images/$IMAGE_NAME"
1408 rm -Rf "$xdir";
1409 mkdir "$xdir"
1410 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
1411 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -04001412 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001413 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -04001414 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001415 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -04001416 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001417 if [[ -z "$IMAGE_NAME" ]]; then
1418 IMAGE_NAME=$(basename "$IMAGE" ".img")
1419 fi
1420 ;;
1421 *.img)
1422 IMAGE="$FILES/$IMAGE_FNAME";
1423 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -05001424 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
1425 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
1426 DISK_FORMAT=$format
1427 else
1428 DISK_FORMAT=raw
1429 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -05001430 CONTAINER_FORMAT=bare
1431 ;;
1432 *.img.gz)
1433 IMAGE="$FILES/${IMAGE_FNAME}"
1434 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
1435 DISK_FORMAT=raw
1436 CONTAINER_FORMAT=bare
1437 UNPACK=zcat
1438 ;;
1439 *.qcow2)
1440 IMAGE="$FILES/${IMAGE_FNAME}"
1441 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
1442 DISK_FORMAT=qcow2
1443 CONTAINER_FORMAT=bare
1444 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +01001445 *.iso)
1446 IMAGE="$FILES/${IMAGE_FNAME}"
1447 IMAGE_NAME=$(basename "$IMAGE" ".iso")
1448 DISK_FORMAT=iso
1449 CONTAINER_FORMAT=bare
1450 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -05001451 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
1452 esac
1453
1454 if [ "$CONTAINER_FORMAT" = "bare" ]; then
1455 if [ "$UNPACK" = "zcat" ]; then
Christian Berendta7a219a2013-07-30 18:22:32 +02001456 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public True --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < <(zcat --force "${IMAGE}")
Dean Troyerca0e3d02012-04-13 15:58:37 -05001457 else
Christian Berendta7a219a2013-07-30 18:22:32 +02001458 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public True --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < "${IMAGE}"
Dean Troyerca0e3d02012-04-13 15:58:37 -05001459 fi
1460 else
1461 # Use glance client to add the kernel the root filesystem.
1462 # We parse the results of the first upload to get the glance ID of the
1463 # kernel for use when uploading the root filesystem.
1464 KERNEL_ID=""; RAMDISK_ID="";
1465 if [ -n "$KERNEL" ]; then
Christian Berendta7a219a2013-07-30 18:22:32 +02001466 KERNEL_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-kernel" --is-public True --container-format aki --disk-format aki < "$KERNEL" | grep ' id ' | get_field 2)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001467 fi
1468 if [ -n "$RAMDISK" ]; then
Christian Berendta7a219a2013-07-30 18:22:32 +02001469 RAMDISK_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-ramdisk" --is-public True --container-format ari --disk-format ari < "$RAMDISK" | grep ' id ' | get_field 2)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001470 fi
Christian Berendta7a219a2013-07-30 18:22:32 +02001471 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "${IMAGE_NAME%.img}" --is-public True --container-format ami --disk-format ami ${KERNEL_ID:+--property kernel_id=$KERNEL_ID} ${RAMDISK_ID:+--property ramdisk_id=$RAMDISK_ID} < "${IMAGE}"
Dean Troyerca0e3d02012-04-13 15:58:37 -05001472 fi
1473}
1474
Dean Troyer1a6d4492013-06-03 16:47:36 -05001475
Dean Troyerc1b486a2012-11-05 14:26:09 -06001476# Set the database backend to use
1477# When called from stackrc/localrc DATABASE_BACKENDS has not been
1478# initialized yet, just save the configuration selection and call back later
1479# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +01001480#
1481# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -06001482function use_database {
1483 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -06001484 # No backends registered means this is likely called from ``localrc``
1485 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -06001486 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +00001487 DEPRECATED_TEXT="$DEPRECATED_TEXT\nThe database backend needs to be properly set in ENABLED_SERVICES; use_database is deprecated localrc\n"
Attila Fazekas251d3b52012-12-16 15:05:44 +01001488 else
Dean Troyerafc29fe2013-02-07 15:56:24 -06001489 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +01001490 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -06001491 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -06001492}
1493
Dean Troyer1a6d4492013-06-03 16:47:36 -05001494
Terry Wilson428af5a2012-11-01 16:12:39 -04001495# Toggle enable/disable_service for services that must run exclusive of each other
1496# $1 The name of a variable containing a space-separated list of services
1497# $2 The name of a variable in which to store the enabled service's name
1498# $3 The name of the service to enable
1499function use_exclusive_service {
1500 local options=${!1}
1501 local selection=$3
1502 out=$2
1503 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1504 for opt in $options;do
1505 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1506 done
1507 eval "$out=$selection"
1508 return 0
1509}
Dean Troyerca0e3d02012-04-13 15:58:37 -05001510
Dean Troyer1a6d4492013-06-03 16:47:36 -05001511
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001512# Wait for an HTTP server to start answering requests
1513# wait_for_service timeout url
1514function wait_for_service() {
1515 local timeout=$1
1516 local url=$2
JUN JIE NAN0aa85342013-09-13 15:47:09 +08001517 timeout $timeout sh -c "while ! curl --noproxy '*' -s $url >/dev/null; do sleep 1; done"
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001518}
1519
Dean Troyer1a6d4492013-06-03 16:47:36 -05001520
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001521# Wrapper for ``yum`` to set proxy environment variables
Adam Spierscb961592013-10-05 12:11:07 +01001522# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001523# yum_install package [package ...]
1524function yum_install() {
1525 [[ "$OFFLINE" = "True" ]] && return
1526 local sudo="sudo"
1527 [[ "$(id -u)" = "0" ]] && sudo="env"
1528 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001529 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001530 yum install -y "$@"
1531}
1532
Dean Troyer1a6d4492013-06-03 16:47:36 -05001533
1534# zypper wrapper to set arguments correctly
1535# zypper_install package [package ...]
1536function zypper_install() {
1537 [[ "$OFFLINE" = "True" ]] && return
1538 local sudo="sudo"
1539 [[ "$(id -u)" = "0" ]] && sudo="env"
1540 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1541 zypper --non-interactive install --auto-agree-with-licenses "$@"
1542}
1543
1544
Nachi Uenofda946e2012-10-24 17:26:02 -07001545# ping check
1546# Uses globals ``ENABLED_SERVICES``
Dean Troyer1a6d4492013-06-03 16:47:36 -05001547# ping_check from-net ip boot-timeout expected
Nachi Uenofda946e2012-10-24 17:26:02 -07001548function ping_check() {
Mark McClainb05c8762013-07-06 23:29:39 -04001549 if is_service_enabled neutron; then
1550 _ping_check_neutron "$1" $2 $3 $4
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001551 return
1552 fi
1553 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -07001554}
1555
1556# ping check for nova
1557# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
1558function _ping_check_novanet() {
1559 local from_net=$1
1560 local ip=$2
1561 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001562 local expected=${4:-"True"}
1563 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001564 MULTI_HOST=`trueorfalse False $MULTI_HOST`
1565 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
Nachi Uenofda946e2012-10-24 17:26:02 -07001566 return
1567 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001568 if [[ "$expected" = "True" ]]; then
1569 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
1570 else
1571 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
1572 fi
1573 if ! timeout $boot_timeout sh -c "$check_command"; then
1574 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -08001575 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001576 else
Nachi Ueno07115eb2013-02-26 12:38:18 -08001577 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001578 fi
Nachi Uenofda946e2012-10-24 17:26:02 -07001579 fi
1580}
1581
Nachi Ueno6769b162013-08-12 18:18:56 -07001582# Get ip of instance
1583function get_instance_ip(){
1584 local vm_id=$1
1585 local network_name=$2
1586 local nova_result="$(nova show $vm_id)"
1587 local ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
1588 if [[ $ip = "" ]];then
1589 echo "$nova_result"
1590 die $LINENO "[Fail] Coudn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -07001591 fi
1592 echo $ip
1593}
Dean Troyer1a6d4492013-06-03 16:47:36 -05001594
Nachi Uenofda946e2012-10-24 17:26:02 -07001595# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001596
Dean Troyer1a6d4492013-06-03 16:47:36 -05001597# ssh_check net-name key-file floating-ip default-user active-timeout
Nachi Uenofda946e2012-10-24 17:26:02 -07001598function ssh_check() {
Mark McClainb05c8762013-07-06 23:29:39 -04001599 if is_service_enabled neutron; then
1600 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001601 return
1602 fi
1603 _ssh_check_novanet "$1" $2 $3 $4 $5
1604}
1605
1606function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -07001607 local NET_NAME=$1
1608 local KEY_FILE=$2
1609 local FLOATING_IP=$3
1610 local DEFAULT_INSTANCE_USER=$4
1611 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -06001612 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -05001613 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP echo success; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -08001614 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -07001615 fi
1616}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001617
Vincent Untz856a11e2012-11-21 16:04:12 +01001618
Vincent Untz856a11e2012-11-21 16:04:12 +01001619# Add a user to a group.
1620# add_user_to_group user group
1621function add_user_to_group() {
1622 local user=$1
1623 local group=$2
1624
1625 if [[ -z "$os_VENDOR" ]]; then
1626 GetOSVersion
1627 fi
1628
1629 # SLE11 and openSUSE 12.2 don't have the usual usermod
1630 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1631 sudo usermod -a -G "$group" "$user"
1632 else
1633 sudo usermod -A "$group" "$user"
1634 fi
1635}
1636
1637
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001638# Get the path to the direcotry where python executables are installed.
1639# get_python_exec_prefix
1640function get_python_exec_prefix() {
Martin Vidner4f9b33d2013-06-27 13:11:22 +00001641 if is_fedora || is_suse; then
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001642 echo "/usr/bin"
1643 else
1644 echo "/usr/local/bin"
1645 fi
1646}
1647
Dean Troyer1a6d4492013-06-03 16:47:36 -05001648
Vincent Untz856a11e2012-11-21 16:04:12 +01001649# Get the location of the $module-rootwrap executables, where module is cinder
1650# or nova.
1651# get_rootwrap_location module
1652function get_rootwrap_location() {
1653 local module=$1
1654
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001655 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +01001656}
1657
Dean Troyer1a6d4492013-06-03 16:47:36 -05001658
Vincent Untz8ec27222012-11-29 09:25:31 +01001659# Get the path to the pip command.
1660# get_pip_command
1661function get_pip_command() {
Dean Troyerd2cfcaa2013-08-01 14:17:27 -05001662 which pip || which pip-python
Ian Wienand535a8142013-05-15 09:25:27 +10001663
1664 if [ $? -ne 0 ]; then
1665 die $LINENO "Unable to find pip; cannot continue"
1666 fi
Vincent Untz8ec27222012-11-29 09:25:31 +01001667}
Vincent Untz856a11e2012-11-21 16:04:12 +01001668
Dean Troyer1a6d4492013-06-03 16:47:36 -05001669
Ian Wienand0488edd2013-04-11 12:04:36 +10001670# Path permissions sanity check
1671# check_path_perm_sanity path
1672function check_path_perm_sanity() {
1673 # Ensure no element of the path has 0700 permissions, which is very
1674 # likely to cause issues for daemons. Inspired by default 0700
1675 # homedir permissions on RHEL and common practice of making DEST in
1676 # the stack user's homedir.
1677
1678 local real_path=$(readlink -f $1)
1679 local rebuilt_path=""
1680 for i in $(echo ${real_path} | tr "/" " "); do
1681 rebuilt_path=$rebuilt_path"/"$i
1682
1683 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
1684 echo "*** DEST path element"
1685 echo "*** ${rebuilt_path}"
1686 echo "*** appears to have 0700 permissions."
1687 echo "*** This is very likely to cause fatal issues for devstack daemons."
1688
1689 if [[ -n "$SKIP_PATH_SANITY" ]]; then
1690 return
1691 else
1692 echo "*** Set SKIP_PATH_SANITY to skip this check"
1693 die $LINENO "Invalid path permissions"
1694 fi
1695 fi
1696 done
1697}
1698
Dean Troyer1a6d4492013-06-03 16:47:36 -05001699
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001700# This function recursively compares versions, and is not meant to be
1701# called by anything other than vercmp_numbers below. This function does
1702# not work with alphabetic versions.
1703#
1704# _vercmp_r sep ver1 ver2
1705function _vercmp_r {
Sean Dague537d4022013-10-22 07:43:22 -04001706 typeset sep
1707 typeset -a ver1=() ver2=()
1708 sep=$1; shift
1709 ver1=("${@:1:sep}")
1710 ver2=("${@:sep+1}")
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001711
Sean Dague537d4022013-10-22 07:43:22 -04001712 if ((ver1 > ver2)); then
1713 echo 1; return 0
1714 elif ((ver2 > ver1)); then
1715 echo -1; return 0
1716 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001717
Sean Dague537d4022013-10-22 07:43:22 -04001718 if ((sep <= 1)); then
1719 echo 0; return 0
1720 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001721
Sean Dague537d4022013-10-22 07:43:22 -04001722 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001723}
1724
1725
1726# This function compares two versions and is meant to be called by
1727# external callers. Please note the function assumes non-alphabetic
1728# versions. For example, this will work:
1729#
1730# vercmp_numbers 1.10 1.4
1731#
1732# The above will return "1", as 1.10 is greater than 1.4.
1733#
1734# vercmp_numbers 5.2 6.4
1735#
1736# The above will return "-1", as 5.2 is less than 6.4.
1737#
1738# vercmp_numbers 4.0 4.0
1739#
1740# The above will return "0", as the versions are equal.
1741#
1742# vercmp_numbers ver1 ver2
1743vercmp_numbers() {
Sean Dague537d4022013-10-22 07:43:22 -04001744 typeset v1=$1 v2=$2 sep
1745 typeset -a ver1 ver2
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001746
Sean Dague537d4022013-10-22 07:43:22 -04001747 IFS=. read -ra ver1 <<< "$v1"
1748 IFS=. read -ra ver2 <<< "$v2"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001749
Sean Dague537d4022013-10-22 07:43:22 -04001750 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001751}
1752
1753
Dean Troyer533e14d2013-08-30 15:11:22 -05001754# ``policy_add policy_file policy_name policy_permissions``
1755#
1756# Add a policy to a policy.json file
1757# Do nothing if the policy already exists
1758
1759function policy_add() {
1760 local policy_file=$1
1761 local policy_name=$2
1762 local policy_perm=$3
1763
1764 if grep -q ${policy_name} ${policy_file}; then
1765 echo "Policy ${policy_name} already exists in ${policy_file}"
1766 return
1767 fi
1768
1769 # Add a terminating comma to policy lines without one
1770 # Remove the closing '}' and all lines following to the end-of-file
1771 local tmpfile=$(mktemp)
1772 uniq ${policy_file} | sed -e '
1773 s/]$/],/
1774 /^[}]/,$d
1775 ' > ${tmpfile}
1776
1777 # Append policy and closing brace
1778 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
1779 echo "}" >>${tmpfile}
1780
1781 mv ${tmpfile} ${policy_file}
1782}
1783
1784
Salvatore Orlando05ae8332013-08-20 14:51:08 -07001785# This function sets log formatting options for colorizing log
1786# output to stdout. It is meant to be called by lib modules.
1787# The last two parameters are optional and can be used to specify
1788# non-default value for project and user format variables.
1789# Defaults are respectively 'project_name' and 'user_name'
1790#
1791# setup_colorized_logging something.conf SOMESECTION
1792function setup_colorized_logging() {
1793 local conf_file=$1
1794 local conf_section=$2
1795 local project_var=${3:-"project_name"}
1796 local user_var=${4:-"user_name"}
1797 # Add color to logging output
1798 iniset $conf_file $conf_section logging_context_format_string "%(asctime)s.%(msecs)03d %(color)s%(levelname)s %(name)s [%(request_id)s %("$user_var")s %("$project_var")s%(color)s] %(instance)s%(color)s%(message)s"
1799 iniset $conf_file $conf_section logging_default_format_string "%(asctime)s.%(msecs)03d %(color)s%(levelname)s %(name)s [-%(color)s] %(instance)s%(color)s%(message)s"
1800 iniset $conf_file $conf_section logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
1801 iniset $conf_file $conf_section logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
1802}
1803
Dean Troyer27e32692012-03-16 16:16:56 -05001804# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +00001805$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001806
1807
1808# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -04001809# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -04001810# End: