blob: effdc53afba801ded73050a249cc3115cdb02c87 [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:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01004#
5# - ``ENABLED_SERVICES``
6# - ``ERROR_ON_CLONE``
7# - ``FILES``
8# - ``GLANCE_HOSTPORT``
9# - ``OFFLINE``
10# - ``PIP_DOWNLOAD_CACHE``
11# - ``PIP_USE_MIRRORS``
12# - ``RECLONE``
13# - ``TRACK_DEPENDS``
14# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer13dc5cc2012-03-27 14:50:45 -050015
Dean Troyer7f9aa712012-01-31 12:11:56 -060016
Dean Troyer27e32692012-03-16 16:16:56 -050017# Save trace setting
18XTRACE=$(set +o | grep xtrace)
19set +o xtrace
20
Dean Troyer7f9aa712012-01-31 12:11:56 -060021
Dean Troyerd4f69b22013-07-24 12:24:43 -050022# Convert CIDR notation to a IPv4 netmask
23# cidr2netmask cidr-bits
24function cidr2netmask() {
25 local maskpat="255 255 255 255"
26 local maskdgt="254 252 248 240 224 192 128"
27 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
28 echo ${1-0}.${2-0}.${3-0}.${4-0}
29}
30
31
32# Return the network portion of the given IP address using netmask
33# netmask is in the traditional dotted-quad format
34# maskip ip-address netmask
35function maskip() {
36 local ip=$1
37 local mask=$2
38 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
39 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
40 echo $subnet
41}
42
43
44# Exit 0 if address is in network or 1 if address is not in network
45# ip-range is in CIDR notation: 1.2.3.4/20
Dean Troyer4a43b7b2012-08-28 17:43:40 -050046# address_in_net ip-address ip-range
Vishvananda Ishayac9ad14b2012-07-03 20:29:01 +000047function address_in_net() {
Dean Troyerd4f69b22013-07-24 12:24:43 -050048 local ip=$1
49 local range=$2
50 local masklen=${range#*/}
51 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
52 local subnet=$(maskip $ip $(cidr2netmask $masklen))
53 [[ $network == $subnet ]]
Vishvananda Ishayac9ad14b2012-07-03 20:29:01 +000054}
55
56
Dean Troyer4a43b7b2012-08-28 17:43:40 -050057# Wrapper for ``apt-get`` to set cache and proxy environment variables
Adam Spierscb961592013-10-05 12:11:07 +010058# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyer13dc5cc2012-03-27 14:50:45 -050059# apt_get operation package [package ...]
Dean Troyer7f9aa712012-01-31 12:11:56 -060060function apt_get() {
Dean Troyerd0b21e22012-03-07 14:52:25 -060061 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer7f9aa712012-01-31 12:11:56 -060062 local sudo="sudo"
63 [[ "$(id -u)" = "0" ]] && sudo="env"
64 $sudo DEBIAN_FRONTEND=noninteractive \
65 http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +090066 no_proxy=$no_proxy \
Dean Troyer7f9aa712012-01-31 12:11:56 -060067 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
68}
69
70
71# Gracefully cp only if source file/dir exists
72# cp_it source destination
73function cp_it {
74 if [ -e $1 ] || [ -d $1 ]; then
75 cp -pRL $1 $2
76 fi
77}
78
79
Kui Shi5e28a3e2013-08-02 17:26:28 +080080# Prints backtrace info
81# filename:lineno:function
82function backtrace {
83 local level=$1
84 local deep=$((${#BASH_SOURCE[@]} - 1))
85 echo "[Call Trace]"
86 while [ $level -le $deep ]; do
87 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
88 deep=$((deep - 1))
89 done
90}
91
92
Dean Troyerac93efb2013-03-13 14:30:54 -050093# Prints line number and "message" then exits
94# die $LINENO "message"
Dean Troyer27e32692012-03-16 16:16:56 -050095function die() {
Dean Troyer489bd2a2012-03-02 10:44:29 -060096 local exitcode=$?
Dean Troyer896eb662013-04-05 15:02:01 -050097 set +o xtrace
98 local line=$1; shift
Nachi Ueno07115eb2013-02-26 12:38:18 -080099 if [ $exitcode == 0 ]; then
100 exitcode=1
101 fi
Kui Shi5e28a3e2013-08-02 17:26:28 +0800102 backtrace 2
Dean Troyer896eb662013-04-05 15:02:01 -0500103 err $line "$*"
Dean Troyer27e32692012-03-16 16:16:56 -0500104 exit $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -0600105}
106
107
108# Checks an environment variable is not set or has length 0 OR if the
109# exit code is non-zero and prints "message" and exits
110# NOTE: env-var is the variable name without a '$'
Dean Troyerac93efb2013-03-13 14:30:54 -0500111# die_if_not_set $LINENO env-var "message"
Dean Troyer489bd2a2012-03-02 10:44:29 -0600112function die_if_not_set() {
Dean Troyer896eb662013-04-05 15:02:01 -0500113 local exitcode=$?
114 FXTRACE=$(set +o | grep xtrace)
115 set +o xtrace
116 local line=$1; shift
117 local evar=$1; shift
118 if ! is_set $evar || [ $exitcode != 0 ]; then
119 die $line "$*"
120 fi
121 $FXTRACE
122}
123
124
125# Prints line number and "message" in error format
126# err $LINENO "message"
127function err() {
128 local exitcode=$?
129 errXTRACE=$(set +o | grep xtrace)
130 set +o xtrace
Kui Shi17df0772013-08-02 17:55:41 +0800131 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
Dean Troyer896eb662013-04-05 15:02:01 -0500132 echo $msg 1>&2;
133 if [[ -n ${SCREEN_LOGDIR} ]]; then
134 echo $msg >> "${SCREEN_LOGDIR}/error.log"
135 fi
136 $errXTRACE
137 return $exitcode
138}
139
140
141# Checks an environment variable is not set or has length 0 OR if the
142# exit code is non-zero and prints "message"
143# NOTE: env-var is the variable name without a '$'
144# err_if_not_set $LINENO env-var "message"
145function err_if_not_set() {
146 local exitcode=$?
147 errinsXTRACE=$(set +o | grep xtrace)
148 set +o xtrace
149 local line=$1; shift
150 local evar=$1; shift
151 if ! is_set $evar || [ $exitcode != 0 ]; then
152 err $line "$*"
153 fi
154 $errinsXTRACE
155 return $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -0600156}
157
158
Dean Troyer893e6632013-09-13 15:05:51 -0500159# Prints line number and "message" in warning format
160# warn $LINENO "message"
161function warn() {
162 local exitcode=$?
163 errXTRACE=$(set +o | grep xtrace)
164 set +o xtrace
165 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
166 echo $msg 1>&2;
167 if [[ -n ${SCREEN_LOGDIR} ]]; then
168 echo $msg >> "${SCREEN_LOGDIR}/error.log"
169 fi
170 $errXTRACE
171 return $exitcode
172}
173
174
Dean Troyer48352ee2012-12-12 12:50:38 -0600175# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
176# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
177# ``localrc`` or on the command line if necessary::
178#
179# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
180#
181# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
182
183function export_proxy_variables() {
184 if [[ -n "$http_proxy" ]]; then
185 export http_proxy=$http_proxy
186 fi
187 if [[ -n "$https_proxy" ]]; then
188 export https_proxy=$https_proxy
189 fi
190 if [[ -n "$no_proxy" ]]; then
191 export no_proxy=$no_proxy
192 fi
193}
194
195
Dean Troyer489bd2a2012-03-02 10:44:29 -0600196# Grab a numbered field from python prettytable output
197# Fields are numbered starting with 1
198# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
199# get_field field-number
200function get_field() {
201 while read data; do
202 if [ "$1" -lt 0 ]; then
203 field="(\$(NF$1))"
204 else
205 field="\$$(($1 + 1))"
206 fi
207 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
208 done
209}
210
211
Dean Troyerc892bde2013-03-13 14:06:13 -0500212# Get the default value for HOST_IP
213# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
214function get_default_host_ip() {
215 local fixed_range=$1
216 local floating_range=$2
217 local host_ip_iface=$3
218 local host_ip=$4
219
220 # Find the interface used for the default route
221 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
222 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
223 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
224 host_ip=""
225 host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}'`
226 for IP in $host_ips; do
227 # Attempt to filter out IP addresses that are part of the fixed and
228 # floating range. Note that this method only works if the ``netaddr``
229 # python library is installed. If it is not installed, an error
230 # will be printed and the first IP from the interface will be used.
231 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
232 # address.
233 if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then
234 host_ip=$IP
235 break;
236 fi
237 done
238 fi
239 echo $host_ip
240}
241
242
Isaku Yamahata8c438092013-02-12 22:30:56 +0900243function _get_package_dir() {
244 local pkg_dir
245 if is_ubuntu; then
246 pkg_dir=$FILES/apts
247 elif is_fedora; then
248 pkg_dir=$FILES/rpms
249 elif is_suse; then
250 pkg_dir=$FILES/rpms-suse
251 else
252 exit_distro_not_supported "list of packages"
253 fi
254 echo "$pkg_dir"
255}
256
Dean Troyer1a6d4492013-06-03 16:47:36 -0500257
Dean Troyer7e270512012-06-14 15:23:24 -0500258# get_packages() collects a list of package names of any type from the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500259# prerequisite files in ``files/{apts|rpms}``. The list is intended
260# to be passed to a package installer such as apt or yum.
Dean Troyer7e270512012-06-14 15:23:24 -0500261#
Isaku Yamahata8c438092013-02-12 22:30:56 +0900262# Only packages required for the services in 1st argument will be
Dean Troyer7e270512012-06-14 15:23:24 -0500263# included. Two bits of metadata are recognized in the prerequisite files:
Adam Spierscb961592013-10-05 12:11:07 +0100264#
265# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
Dean Troyer7e270512012-06-14 15:23:24 -0500266# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
267# of the package to the distros listed. The distro names are case insensitive.
Dean Troyer7e270512012-06-14 15:23:24 -0500268function get_packages() {
Dean Troyerca5af862013-10-04 13:33:07 -0500269 local services=$@
Isaku Yamahata8c438092013-02-12 22:30:56 +0900270 local package_dir=$(_get_package_dir)
Dean Troyer7e270512012-06-14 15:23:24 -0500271 local file_to_parse
272 local service
273
274 if [[ -z "$package_dir" ]]; then
275 echo "No package directory supplied"
276 return 1
277 fi
278 if [[ -z "$DISTRO" ]]; then
Vincent Untz855c5872012-10-04 13:36:46 +0200279 GetDistro
Dean Troyer7e270512012-06-14 15:23:24 -0500280 fi
Dean Troyerca5af862013-10-04 13:33:07 -0500281 for service in ${services//,/ }; do
Dean Troyer7e270512012-06-14 15:23:24 -0500282 # Allow individual services to specify dependencies
283 if [[ -e ${package_dir}/${service} ]]; then
284 file_to_parse="${file_to_parse} $service"
285 fi
286 # NOTE(sdague) n-api needs glance for now because that's where
287 # glance client is
288 if [[ $service == n-api ]]; then
289 if [[ ! $file_to_parse =~ nova ]]; then
290 file_to_parse="${file_to_parse} nova"
291 fi
292 if [[ ! $file_to_parse =~ glance ]]; then
293 file_to_parse="${file_to_parse} glance"
294 fi
295 elif [[ $service == c-* ]]; then
296 if [[ ! $file_to_parse =~ cinder ]]; then
297 file_to_parse="${file_to_parse} cinder"
298 fi
John H. Tran93361642012-07-26 11:22:05 -0700299 elif [[ $service == ceilometer-* ]]; then
300 if [[ ! $file_to_parse =~ ceilometer ]]; then
301 file_to_parse="${file_to_parse} ceilometer"
302 fi
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100303 elif [[ $service == s-* ]]; then
304 if [[ ! $file_to_parse =~ swift ]]; then
305 file_to_parse="${file_to_parse} swift"
306 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500307 elif [[ $service == n-* ]]; then
308 if [[ ! $file_to_parse =~ nova ]]; then
309 file_to_parse="${file_to_parse} nova"
310 fi
311 elif [[ $service == g-* ]]; then
312 if [[ ! $file_to_parse =~ glance ]]; then
313 file_to_parse="${file_to_parse} glance"
314 fi
315 elif [[ $service == key* ]]; then
316 if [[ ! $file_to_parse =~ keystone ]]; then
317 file_to_parse="${file_to_parse} keystone"
318 fi
Robert Collins0a9954f2012-11-20 11:34:25 +1300319 elif [[ $service == q-* ]]; then
Mark McClainb05c8762013-07-06 23:29:39 -0400320 if [[ ! $file_to_parse =~ neutron ]]; then
321 file_to_parse="${file_to_parse} neutron"
Robert Collins0a9954f2012-11-20 11:34:25 +1300322 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500323 fi
324 done
325
326 for file in ${file_to_parse}; do
327 local fname=${package_dir}/${file}
328 local OIFS line package distros distro
329 [[ -e $fname ]] || continue
330
331 OIFS=$IFS
332 IFS=$'\n'
333 for line in $(<${fname}); do
334 if [[ $line =~ "NOPRIME" ]]; then
335 continue
336 fi
337
Christian Berendt71d56302013-07-22 11:37:42 +0200338 # Assume we want this package
339 package=${line%#*}
340 inst_pkg=1
341
342 # Look for # dist:xxx in comment
Dean Troyer7e270512012-06-14 15:23:24 -0500343 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
344 # We are using BASH regexp matching feature.
345 package=${BASH_REMATCH[1]}
346 distros=${BASH_REMATCH[2]}
347 # In bash ${VAR,,} will lowecase VAR
Christian Berendt71d56302013-07-22 11:37:42 +0200348 # Look for a match in the distro list
349 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
350 # If no match then skip this package
351 inst_pkg=0
352 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500353 fi
354
Christian Berendt71d56302013-07-22 11:37:42 +0200355 # Look for # testonly in comment
356 if [[ $line =~ (.*)#.*testonly.* ]]; then
357 package=${BASH_REMATCH[1]}
358 # Are we installing test packages? (test for the default value)
359 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
360 # If not installing test packages the skip this package
361 inst_pkg=0
362 fi
363 fi
364
365 if [[ $inst_pkg = 1 ]]; then
366 echo $package
367 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500368 done
369 IFS=$OIFS
370 done
371}
372
373
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500374# Determine OS Vendor, Release and Update
375# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
376# Returns results in global variables:
377# os_VENDOR - vendor name
378# os_RELEASE - release
379# os_UPDATE - update
380# os_PACKAGE - package type
381# os_CODENAME - vendor's codename for release
382# GetOSVersion
383GetOSVersion() {
384 # Figure out which vendor we are
Mehdi Abaakoukaee94122013-09-30 11:48:00 +0000385 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500386 # OS/X
387 os_VENDOR=`sw_vers -productName`
388 os_RELEASE=`sw_vers -productVersion`
389 os_UPDATE=${os_RELEASE##*.}
390 os_RELEASE=${os_RELEASE%.*}
391 os_PACKAGE=""
392 if [[ "$os_RELEASE" =~ "10.7" ]]; then
393 os_CODENAME="lion"
394 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
395 os_CODENAME="snow leopard"
396 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
397 os_CODENAME="leopard"
398 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
399 os_CODENAME="tiger"
400 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
401 os_CODENAME="panther"
402 else
403 os_CODENAME=""
404 fi
405 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
406 os_VENDOR=$(lsb_release -i -s)
407 os_RELEASE=$(lsb_release -r -s)
408 os_UPDATE=""
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100409 os_PACKAGE="rpm"
Derek Morton4a8496e2013-04-08 23:46:08 -0500410 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500411 os_PACKAGE="deb"
Vincent Untz856a11e2012-11-21 16:04:12 +0100412 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
413 lsb_release -d -s | grep -q openSUSE
414 if [[ $? -eq 0 ]]; then
415 os_VENDOR="openSUSE"
416 fi
Vincent Untzcd1fe982013-03-12 18:04:29 +0100417 elif [[ $os_VENDOR == "openSUSE project" ]]; then
418 os_VENDOR="openSUSE"
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100419 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
420 os_VENDOR="Red Hat"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500421 fi
422 os_CODENAME=$(lsb_release -c -s)
423 elif [[ -r /etc/redhat-release ]]; then
424 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
425 # CentOS release 5.5 (Final)
426 # CentOS Linux release 6.0 (Final)
427 # Fedora release 16 (Verne)
Bob Ball46691222013-08-12 17:28:50 +0100428 # XenServer release 6.2.0-70446c (xenenterprise)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500429 os_CODENAME=""
Bob Ball46691222013-08-12 17:28:50 +0100430 for r in "Red Hat" CentOS Fedora XenServer; do
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500431 os_VENDOR=$r
432 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
433 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
434 os_CODENAME=${ver#*|}
435 os_RELEASE=${ver%|*}
436 os_UPDATE=${os_RELEASE##*.}
437 os_RELEASE=${os_RELEASE%.*}
438 break
439 fi
440 os_VENDOR=""
441 done
442 os_PACKAGE="rpm"
Vincent Untz856a11e2012-11-21 16:04:12 +0100443 elif [[ -r /etc/SuSE-release ]]; then
444 for r in openSUSE "SUSE Linux"; do
445 if [[ "$r" = "SUSE Linux" ]]; then
446 os_VENDOR="SUSE LINUX"
447 else
448 os_VENDOR=$r
449 fi
450
451 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
452 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
453 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
454 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
455 break
456 fi
457 os_VENDOR=""
458 done
459 os_PACKAGE="rpm"
Émilien Macchib2ef8902013-05-04 00:48:20 +0200460 # If lsb_release is not installed, we should be able to detect Debian OS
461 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
462 os_VENDOR="Debian"
463 os_PACKAGE="deb"
464 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
465 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500466 fi
467 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
468}
469
Andrew Laskif900bd72012-09-05 17:23:14 -0400470
Dean Troyera9e0a482012-07-09 14:07:23 -0500471# Translate the OS version values into common nomenclature
472# Sets ``DISTRO`` from the ``os_*`` values
473function GetDistro() {
474 GetOSVersion
Émilien Macchib2ef8902013-05-04 00:48:20 +0200475 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
476 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
Dean Troyera9e0a482012-07-09 14:07:23 -0500477 DISTRO=$os_CODENAME
478 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
479 # For Fedora, just use 'f' and the release
480 DISTRO="f$os_RELEASE"
Vincent Untz856a11e2012-11-21 16:04:12 +0100481 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
482 DISTRO="opensuse-$os_RELEASE"
483 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
484 # For SLE, also use the service pack
485 if [[ -z "$os_UPDATE" ]]; then
486 DISTRO="sle${os_RELEASE}"
487 else
488 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
489 fi
Ian Wienandd857f4b2013-03-20 14:51:06 +1100490 elif [[ "$os_VENDOR" =~ (Red Hat) || "$os_VENDOR" =~ (CentOS) ]]; then
491 # Drop the . release as we assume it's compatible
492 DISTRO="rhel${os_RELEASE::1}"
Bob Ball46691222013-08-12 17:28:50 +0100493 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
494 DISTRO="xs$os_RELEASE"
Dean Troyera9e0a482012-07-09 14:07:23 -0500495 else
496 # Catch-all for now is Vendor + Release + Update
497 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
498 fi
499 export DISTRO
500}
501
502
Vincent Untz00011c02012-12-06 09:56:32 +0100503# Determine if current distribution is a Fedora-based distribution
Dean Troyer1a6d4492013-06-03 16:47:36 -0500504# (Fedora, RHEL, CentOS, etc).
Vincent Untz00011c02012-12-06 09:56:32 +0100505# is_fedora
506function is_fedora {
507 if [[ -z "$os_VENDOR" ]]; then
508 GetOSVersion
509 fi
510
511 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ]
512}
513
Dean Troyer1a6d4492013-06-03 16:47:36 -0500514
Vincent Untz856a11e2012-11-21 16:04:12 +0100515# Determine if current distribution is a SUSE-based distribution
516# (openSUSE, SLE).
517# is_suse
518function is_suse {
519 if [[ -z "$os_VENDOR" ]]; then
520 GetOSVersion
521 fi
522
Steve Baker1a7bbd22012-12-03 17:04:02 +1300523 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
Vincent Untz856a11e2012-11-21 16:04:12 +0100524}
525
526
Dean Troyer1a6d4492013-06-03 16:47:36 -0500527# Determine if current distribution is an Ubuntu-based distribution
528# It will also detect non-Ubuntu but Debian-based distros
529# is_ubuntu
530function is_ubuntu {
531 if [[ -z "$os_PACKAGE" ]]; then
532 GetOSVersion
533 fi
534 [ "$os_PACKAGE" = "deb" ]
535}
536
537
Vincent Untz00011c02012-12-06 09:56:32 +0100538# Exit after outputting a message about the distribution not being supported.
539# exit_distro_not_supported [optional-string-telling-what-is-missing]
540function exit_distro_not_supported {
541 if [[ -z "$DISTRO" ]]; then
542 GetDistro
543 fi
544
545 if [ $# -gt 0 ]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800546 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
Vincent Untz00011c02012-12-06 09:56:32 +0100547 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800548 die $LINENO "Support for $DISTRO is incomplete."
Vincent Untz00011c02012-12-06 09:56:32 +0100549 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100550}
551
Daniel Jonesfa868cb2013-06-18 15:28:01 -0500552# Utility function for checking machine architecture
553# is_arch arch-type
554function is_arch {
555 ARCH_TYPE=$1
556
557 [ "($uname -m)" = "$ARCH_TYPE" ]
558}
Vincent Untz00011c02012-12-06 09:56:32 +0100559
Chris Buccella610af8c2013-11-05 12:56:34 +0000560# Checks if installed Apache is <= given version
561# $1 = x.y.z (version string of Apache)
562function check_apache_version {
563 local cmd="apachectl"
564 if ! [[ -x $(which apachectl 2>/dev/null) ]]; then
565 cmd="/usr/sbin/apachectl"
566 fi
567
568 local version=$($cmd -v | grep version | grep -Po 'Apache/\K[^ ]*')
569 expr "$version" '>=' $1 > /dev/null
570}
571
Dean Troyer7f9aa712012-01-31 12:11:56 -0600572# git clone only if directory doesn't exist already. Since ``DEST`` might not
573# be owned by the installation user, we create the directory and change the
574# ownership to the proper user.
575# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700576# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
577# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500578# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600579# git_clone remote dest-dir branch
580function git_clone {
Dean Troyer7f9aa712012-01-31 12:11:56 -0600581 GIT_REMOTE=$1
582 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300583 GIT_REF=$3
Sirushti Murugesana8d41e32013-09-25 11:30:31 +0530584 RECLONE=$(trueorfalse False $RECLONE)
Dean Troyer7f9aa712012-01-31 12:11:56 -0600585
Sean Dague835db2f2013-09-23 14:17:06 -0400586 if [[ "$OFFLINE" = "True" ]]; then
587 echo "Running in offline mode, clones already exist"
588 # print out the results so we know what change was used in the logs
589 cd $GIT_DEST
Sean Dague45a21f02013-09-25 10:27:27 -0400590 git show --oneline | head -1
Sean Dague835db2f2013-09-23 14:17:06 -0400591 return
592 fi
593
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300594 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600595 # If our branch name is a gerrit style refs/changes/...
596 if [[ ! -d $GIT_DEST ]]; then
Sean Daguedc30bd32013-10-22 07:30:47 -0400597 [[ "$ERROR_ON_CLONE" = "True" ]] && \
598 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer7f9aa712012-01-31 12:11:56 -0600599 git clone $GIT_REMOTE $GIT_DEST
600 fi
601 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300602 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600603 else
604 # do a full clone only if the directory doesn't exist
605 if [[ ! -d $GIT_DEST ]]; then
Sean Daguedc30bd32013-10-22 07:30:47 -0400606 [[ "$ERROR_ON_CLONE" = "True" ]] && \
607 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer7f9aa712012-01-31 12:11:56 -0600608 git clone $GIT_REMOTE $GIT_DEST
609 cd $GIT_DEST
610 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300611 git checkout $GIT_REF
Sirushti Murugesana8d41e32013-09-25 11:30:31 +0530612 elif [[ "$RECLONE" = "True" ]]; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600613 # if it does exist then simulate what clone does if asked to RECLONE
614 cd $GIT_DEST
615 # set the url to pull from and fetch
616 git remote set-url origin $GIT_REMOTE
617 git fetch origin
618 # remove the existing ignored files (like pyc) as they cause breakage
619 # (due to the py files having older timestamps than our pyc, so python
620 # thinks the pyc files are correct using them)
621 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300622
623 # handle GIT_REF accordingly to type (tag, branch)
624 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
625 git_update_tag $GIT_REF
626 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
627 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400628 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
629 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300630 else
Sean Daguedc30bd32013-10-22 07:30:47 -0400631 die $LINENO "$GIT_REF is neither branch nor tag"
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300632 fi
633
Dean Troyer7f9aa712012-01-31 12:11:56 -0600634 fi
635 fi
Sean Dague835db2f2013-09-23 14:17:06 -0400636
637 # print out the results so we know what change was used in the logs
638 cd $GIT_DEST
Sean Dague45a21f02013-09-25 10:27:27 -0400639 git show --oneline | head -1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600640}
641
642
Dean Troyer1a6d4492013-06-03 16:47:36 -0500643# git update using reference as a branch.
644# git_update_branch ref
645function git_update_branch() {
646
647 GIT_BRANCH=$1
648
649 git checkout -f origin/$GIT_BRANCH
650 # a local branch might not exist
651 git branch -D $GIT_BRANCH || true
652 git checkout -b $GIT_BRANCH
653}
654
655
656# git update using reference as a branch.
657# git_update_remote_branch ref
658function git_update_remote_branch() {
659
660 GIT_BRANCH=$1
661
662 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
663}
664
665
666# git update using reference as a tag. Be careful editing source at that repo
667# as working copy will be in a detached mode
668# git_update_tag ref
669function git_update_tag() {
670
671 GIT_TAG=$1
672
673 git tag -d $GIT_TAG
674 # fetching given tag only
675 git fetch origin tag $GIT_TAG
676 git checkout -f $GIT_TAG
677}
678
679
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500680# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200681# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500682function inicomment() {
683 local file=$1
684 local section=$2
685 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100686 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500687}
688
Dean Troyer896eb662013-04-05 15:02:01 -0500689
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200690# Uncomment an option in an INI file
691# iniuncomment config-file section option
692function iniuncomment() {
693 local file=$1
694 local section=$2
695 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100696 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200697}
698
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500699
700# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500701# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500702function iniget() {
703 local file=$1
704 local section=$2
705 local option=$3
706 local line
Attila Fazekas588eb412012-12-20 10:57:16 +0100707 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500708 echo ${line#*=}
709}
710
Dean Troyer896eb662013-04-05 15:02:01 -0500711
Attila Fazekas588eb412012-12-20 10:57:16 +0100712# Determinate is the given option present in the INI file
713# ini_has_option config-file section option
714function ini_has_option() {
715 local file=$1
716 local section=$2
717 local option=$3
718 local line
719 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
720 [ -n "$line" ]
721}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500722
Dean Troyer896eb662013-04-05 15:02:01 -0500723
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500724# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500725# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500726function iniset() {
727 local file=$1
728 local section=$2
729 local option=$3
730 local value=$4
DennyZhangf43f3a52013-10-11 23:09:47 -0500731
732 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500733 # Add section at the end
Attila Fazekas588eb412012-12-20 10:57:16 +0100734 echo -e "\n[$section]" >>"$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500735 fi
Attila Fazekas588eb412012-12-20 10:57:16 +0100736 if ! ini_has_option "$file" "$section" "$option"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500737 # Add it
Attila Fazekas588eb412012-12-20 10:57:16 +0100738 sed -i -e "/^\[$section\]/ a\\
Dean Troyer09e636e2012-03-19 16:31:12 -0500739$option = $value
Attila Fazekas588eb412012-12-20 10:57:16 +0100740" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500741 else
742 # Replace it
Attila Fazekas588eb412012-12-20 10:57:16 +0100743 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500744 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500745}
746
Dean Troyer896eb662013-04-05 15:02:01 -0500747
Lianhao Lu239f3242013-03-01 15:54:02 +0800748# Get a multiple line option from an INI file
749# iniget_multiline config-file section option
750function iniget_multiline() {
751 local file=$1
752 local section=$2
753 local option=$3
754 local values
755 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
756 echo ${values}
757}
758
Dean Troyer896eb662013-04-05 15:02:01 -0500759
Lianhao Lu239f3242013-03-01 15:54:02 +0800760# Set a multiple line option in an INI file
761# iniset_multiline config-file section option value1 value2 valu3 ...
762function iniset_multiline() {
763 local file=$1
764 local section=$2
765 local option=$3
766 shift 3
767 local values
768 for v in $@; do
769 # The later sed command inserts each new value in the line next to
770 # the section identifier, which causes the values to be inserted in
771 # the reverse order. Do a reverse here to keep the original order.
772 values="$v ${values}"
773 done
774 if ! grep -q "^\[$section\]" "$file"; then
775 # Add section at the end
776 echo -e "\n[$section]" >>"$file"
777 else
778 # Remove old values
779 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
780 fi
781 # Add new ones
782 for v in $values; do
783 sed -i -e "/^\[$section\]/ a\\
784$option = $v
785" "$file"
786 done
787}
788
Dean Troyer896eb662013-04-05 15:02:01 -0500789
Lianhao Lu239f3242013-03-01 15:54:02 +0800790# Append a new option in an ini file without replacing the old value
791# iniadd config-file section option value1 value2 value3 ...
792function iniadd() {
793 local file=$1
794 local section=$2
795 local option=$3
796 shift 3
797 local values="$(iniget_multiline $file $section $option) $@"
798 iniset_multiline $file $section $option $values
799}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500800
Dean Troyer896eb662013-04-05 15:02:01 -0500801# Find out if a process exists by partial name.
802# is_running name
803function is_running() {
804 local name=$1
805 ps auxw | grep -v grep | grep ${name} > /dev/null
806 RC=$?
807 # some times I really hate bash reverse binary logic
808 return $RC
809}
810
811
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000812# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500813# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000814#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500815# Multiple services specified as arguments are ``OR``'ed together; the test
816# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000817#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500818# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000819# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500820# **cinder** returns true if any service enabled start with **c-**
821# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000822# **glance** returns true if any service enabled start with **g-**
Mark McClainb05c8762013-07-06 23:29:39 -0400823# **neutron** returns true if any service enabled start with **q-**
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100824# **swift** returns true if any service enabled start with **s-**
Nikhil Manchanda0cccad42012-12-03 18:15:09 -0700825# **trove** returns true if any service enabled start with **tr-**
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100826# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
827# **s-** services will be enabled. This will be deprecated in the future.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500828#
Chris Behrensc62c2b92013-07-24 03:56:13 -0700829# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
830# We also need to make sure to treat **n-cell-region** and **n-cell-child**
831# as enabled in this case.
832#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500833# Uses global ``ENABLED_SERVICES``
834# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000835function is_service_enabled() {
836 services=$@
837 for service in ${services}; do
838 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
Chris Behrensc62c2b92013-07-24 03:56:13 -0700839 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000840 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500841 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700842 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000843 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
Mark McClainb05c8762013-07-06 23:29:39 -0400844 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
Nikhil Manchanda0cccad42012-12-03 18:15:09 -0700845 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && return 0
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100846 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && return 0
847 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000848 done
849 return 1
850}
851
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500852
853# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
854# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400855function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500856 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400857 s/,,/,/g;
858 s/^,//;
859 s/,$//
860 '
861}
862
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500863
Doug Hellmannf04178f2012-07-05 17:10:03 -0400864# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500865# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400866#
867# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800868# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400869#
870# This function does not know about the special cases
Mark McClainb05c8762013-07-06 23:29:39 -0400871# for nova, glance, and neutron built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500872# Uses global ``ENABLED_SERVICES``
873# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400874function enable_service() {
875 local tmpsvcs="${ENABLED_SERVICES}"
876 for service in $@; do
877 if ! is_service_enabled $service; then
878 tmpsvcs+=",$service"
879 fi
880 done
881 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
882 disable_negated_services
883}
884
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500885
Doug Hellmannf04178f2012-07-05 17:10:03 -0400886# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500887# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400888#
889# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800890# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400891#
892# This function does not know about the special cases
Mark McClainb05c8762013-07-06 23:29:39 -0400893# for nova, glance, and neutron built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500894# Uses global ``ENABLED_SERVICES``
895# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400896function disable_service() {
897 local tmpsvcs=",${ENABLED_SERVICES},"
898 local service
899 for service in $@; do
900 if is_service_enabled $service; then
901 tmpsvcs=${tmpsvcs//,$service,/,}
902 fi
903 done
904 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
905}
906
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500907
Doug Hellmannf04178f2012-07-05 17:10:03 -0400908# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500909# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400910# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500911# Uses global ``ENABLED_SERVICES``
912# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400913function disable_all_services() {
914 ENABLED_SERVICES=""
915}
916
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500917
918# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800919# services except rabbit (rabbit) set in ``localrc``:
920# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500921# Uses global ``ENABLED_SERVICES``
922# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400923function disable_negated_services() {
924 local tmpsvcs="${ENABLED_SERVICES}"
925 local service
926 for service in ${tmpsvcs//,/ }; do
927 if [[ ${service} == -* ]]; then
928 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
929 fi
930 done
931 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
932}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600933
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500934
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500935# Distro-agnostic package installer
936# install_package package [package ...]
937function install_package() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100938 if is_ubuntu; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200939 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
940 NO_UPDATE_REPOS=True
941
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500942 apt_get install "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100943 elif is_fedora; then
944 yum_install "$@"
945 elif is_suse; then
946 zypper_install "$@"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500947 else
Vincent Untz00011c02012-12-06 09:56:32 +0100948 exit_distro_not_supported "installing packages"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500949 fi
950}
951
952
Dean Troyer995eb922013-03-07 16:11:40 -0600953# Distro-agnostic package uninstaller
954# uninstall_package package [package ...]
955function uninstall_package() {
956 if is_ubuntu; then
957 apt_get purge "$@"
958 elif is_fedora; then
Ian Wienand2c678cc2013-03-20 13:00:44 +1100959 sudo yum remove -y "$@"
Dean Troyer995eb922013-03-07 16:11:40 -0600960 elif is_suse; then
Adam Spiers6d8fce72013-10-01 15:59:05 +0100961 sudo zypper rm "$@"
Dean Troyer995eb922013-03-07 16:11:40 -0600962 else
963 exit_distro_not_supported "uninstalling packages"
964 fi
965}
966
967
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200968# Distro-agnostic function to tell if a package is installed
969# is_package_installed package [package ...]
970function is_package_installed() {
971 if [[ -z "$@" ]]; then
972 return 1
973 fi
974
975 if [[ -z "$os_PACKAGE" ]]; then
976 GetOSVersion
977 fi
Vincent Untzc18b9652012-12-04 12:36:34 +0100978
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200979 if [[ "$os_PACKAGE" = "deb" ]]; then
Dean Troyer04762cd2013-08-27 17:06:14 -0500980 dpkg -s "$@" > /dev/null 2> /dev/null
Vincent Untz00011c02012-12-06 09:56:32 +0100981 elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200982 rpm --quiet -q "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100983 else
984 exit_distro_not_supported "finding if a package is installed"
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200985 fi
986}
987
988
Dean Troyer489bd2a2012-03-02 10:44:29 -0600989# Test if the named environment variable is set and not zero length
990# is_set env-var
991function is_set() {
992 local var=\$"$1"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100993 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 -0600994}
995
996
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500997# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +0000998# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
Adam Spierscb961592013-10-05 12:11:07 +0100999# ``TRACK_DEPENDS``, ``*_proxy``
Dean Troyer7f9aa712012-01-31 12:11:56 -06001000# pip_install package [package ...]
1001function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -06001002 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001003 if [[ -z "$os_PACKAGE" ]]; then
1004 GetOSVersion
1005 fi
Dean Troyercc6b4432013-04-08 15:38:03 -05001006 if [[ $TRACK_DEPENDS = True ]]; then
Monty Taylor47f02062012-07-26 11:09:24 -05001007 source $DEST/.venv/bin/activate
1008 CMD_PIP=$DEST/.venv/bin/pip
1009 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001010 else
Monty Taylor47f02062012-07-26 11:09:24 -05001011 SUDO_PIP="sudo"
Vincent Untz8ec27222012-11-29 09:25:31 +01001012 CMD_PIP=$(get_pip_command)
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001013 fi
Ian Wienandd67dd872013-04-11 11:14:36 +10001014
Roman Gorodeckij99405a42013-08-07 09:20:36 -04001015 # Mirror option not needed anymore because pypi has CDN available,
1016 # but it's useful in certain circumstances
1017 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
Maru Newby3a87edd2012-10-25 23:01:06 +00001018 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
1019 PIP_MIRROR_OPT="--use-mirrors"
1020 fi
Ian Wienandd67dd872013-04-11 11:14:36 +10001021
Ian Wienand31dcd3e2013-07-16 13:36:34 +10001022 # pip < 1.4 has a bug where it will use an already existing build
1023 # directory unconditionally. Say an earlier component installs
1024 # foo v1.1; pip will have built foo's source in
1025 # /tmp/$USER-pip-build. Even if a later component specifies foo <
1026 # 1.1, the existing extracted build will be used and cause
1027 # confusing errors. By creating unique build directories we avoid
Adam Spierscb961592013-10-05 12:11:07 +01001028 # this problem. See https://github.com/pypa/pip/issues/709
Ian Wienand31dcd3e2013-07-16 13:36:34 +10001029 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1030
Monty Taylor47f02062012-07-26 11:09:24 -05001031 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -06001032 HTTP_PROXY=$http_proxy \
1033 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001034 NO_PROXY=$no_proxy \
Ian Wienand31dcd3e2013-07-16 13:36:34 +10001035 $CMD_PIP install --build=${pip_build_tmp} \
1036 $PIP_MIRROR_OPT $@ \
1037 && $SUDO_PIP rm -rf ${pip_build_tmp}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001038}
1039
1040
Ian Wienand31dcd3e2013-07-16 13:36:34 +10001041# Cleanup anything from /tmp on unstack
1042# clean_tmp
1043function cleanup_tmp {
1044 local tmp_dir=${TMPDIR:-/tmp}
1045
1046 # see comments in pip_install
1047 sudo rm -rf ${tmp_dir}/pip-build.*
1048}
1049
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001050# Service wrapper to restart services
1051# restart_service service-name
1052function restart_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001053 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001054 sudo /usr/sbin/service $1 restart
1055 else
1056 sudo /sbin/service $1 restart
1057 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001058}
1059
1060
Dean Troyer681f3fd2013-02-27 19:00:39 -06001061# _run_process() is designed to be backgrounded by run_process() to simulate a
1062# fork. It includes the dirty work of closing extra filehandles and preparing log
1063# files to produce the same logs as screen_it(). The log filename is derived
1064# from the service name and global-and-now-misnamed SCREEN_LOGDIR
1065# _run_process service "command-line"
1066function _run_process() {
1067 local service=$1
1068 local command="$2"
1069
1070 # Undo logging redirections and close the extra descriptors
1071 exec 1>&3
1072 exec 2>&3
1073 exec 3>&-
1074 exec 6>&-
1075
1076 if [[ -n ${SCREEN_LOGDIR} ]]; then
1077 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1078 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1079
1080 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1081 export PYTHONUNBUFFERED=1
1082 fi
1083
1084 exec /bin/bash -c "$command"
1085 die "$service exec failure: $command"
1086}
1087
1088
1089# run_process() launches a child process that closes all file descriptors and
1090# then exec's the passed in command. This is meant to duplicate the semantics
1091# of screen_it() without screen. PIDs are written to
1092# $SERVICE_DIR/$SCREEN_NAME/$service.pid
1093# run_process service "command-line"
1094function run_process() {
1095 local service=$1
1096 local command="$2"
1097
1098 # Spawn the child process
1099 _run_process "$service" "$command" &
1100 echo $!
1101}
1102
1103
Dean Troyer15733352012-09-06 11:51:30 -05001104# Helper to launch a service in a named screen
1105# screen_it service "command-line"
1106function screen_it {
Dean Troyer15733352012-09-06 11:51:30 -05001107 SCREEN_NAME=${SCREEN_NAME:-stack}
jiajun xua9414242012-12-06 16:30:57 +08001108 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer681f3fd2013-02-27 19:00:39 -06001109 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
jiajun xua9414242012-12-06 16:30:57 +08001110
Dean Troyer15733352012-09-06 11:51:30 -05001111 if is_service_enabled $1; then
1112 # Append the service to the screen rc file
1113 screen_rc "$1" "$2"
1114
Dean Troyer681f3fd2013-02-27 19:00:39 -06001115 if [[ "$USE_SCREEN" = "True" ]]; then
1116 screen -S $SCREEN_NAME -X screen -t $1
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +00001117
Dean Troyer681f3fd2013-02-27 19:00:39 -06001118 if [[ -n ${SCREEN_LOGDIR} ]]; then
1119 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
1120 screen -S $SCREEN_NAME -p $1 -X log on
1121 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1122 fi
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +00001123
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001124 # sleep to allow bash to be ready to be send the command - we are
1125 # creating a new window in screen and then sends characters, so if
1126 # bash isn't running by the time we send the command, nothing happens
1127 sleep 1.5
Dean Troyer15733352012-09-06 11:51:30 -05001128
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001129 NL=`echo -ne '\015'`
Clark Boylan41815cd2013-08-16 14:57:38 -07001130 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 -08001131 else
Dean Troyer681f3fd2013-02-27 19:00:39 -06001132 # Spawn directly without screen
1133 run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyer15733352012-09-06 11:51:30 -05001134 fi
Dean Troyer15733352012-09-06 11:51:30 -05001135 fi
1136}
1137
1138
1139# Screen rc file builder
1140# screen_rc service "command-line"
1141function screen_rc {
1142 SCREEN_NAME=${SCREEN_NAME:-stack}
1143 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1144 if [[ ! -e $SCREENRC ]]; then
1145 # Name the screen session
1146 echo "sessionname $SCREEN_NAME" > $SCREENRC
1147 # Set a reasonable statusbar
1148 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
Steven Dake30396572013-06-30 16:11:54 -07001149 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1150 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
Dean Troyer15733352012-09-06 11:51:30 -05001151 echo "screen -t shell bash" >> $SCREENRC
1152 fi
1153 # If this service doesn't already exist in the screenrc file
1154 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1155 NL=`echo -ne '\015'`
1156 echo "screen -t $1 bash" >> $SCREENRC
1157 echo "stuff \"$2$NL\"" >> $SCREENRC
1158 fi
1159}
1160
Dean Troyer1a6d4492013-06-03 16:47:36 -05001161
Adam Spierscb961592013-10-05 12:11:07 +01001162# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1163# This is used for ``service_check`` when all the ``screen_it`` are called finished
jiajun xua9414242012-12-06 16:30:57 +08001164# init_service_check
1165function init_service_check() {
1166 SCREEN_NAME=${SCREEN_NAME:-stack}
1167 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1168
1169 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1170 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1171 fi
1172
1173 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1174}
1175
Dean Troyer1a6d4492013-06-03 16:47:36 -05001176
jiajun xua9414242012-12-06 16:30:57 +08001177# Helper to get the status of each running service
1178# service_check
1179function service_check() {
1180 local service
1181 local failures
1182 SCREEN_NAME=${SCREEN_NAME:-stack}
1183 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1184
1185
1186 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1187 echo "No service status directory found"
1188 return
1189 fi
1190
1191 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
1192 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null`
1193
1194 for service in $failures; do
1195 service=`basename $service`
Bob Ball46287d82013-07-30 09:43:17 +01001196 service=${service%.failure}
jiajun xua9414242012-12-06 16:30:57 +08001197 echo "Error: Service $service is not running"
1198 done
1199
1200 if [ -n "$failures" ]; then
1201 echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
1202 fi
1203}
Dean Troyer15733352012-09-06 11:51:30 -05001204
Doug Hellmanne7002672013-09-05 08:10:07 -04001205# Returns true if the directory is on a filesystem mounted via NFS.
1206function is_nfs_directory() {
1207 local mount_type=`stat -f -L -c %T $1`
1208 test "$mount_type" == "nfs"
1209}
1210
1211# Only run the command if the target file (the last arg) is not on an
1212# NFS filesystem.
1213function _safe_permission_operation() {
1214 local args=( $@ )
1215 local last
1216 local sudo_cmd
1217 local dir_to_check
1218
1219 let last="${#args[*]} - 1"
1220
1221 dir_to_check=${args[$last]}
1222 if [ ! -d "$dir_to_check" ]; then
1223 dir_to_check=`dirname "$dir_to_check"`
1224 fi
1225
1226 if is_nfs_directory "$dir_to_check" ; then
1227 return 0
1228 fi
1229
1230 if [[ $TRACK_DEPENDS = True ]]; then
1231 sudo_cmd="env"
1232 else
1233 sudo_cmd="sudo"
1234 fi
1235
1236 $sudo_cmd $@
1237}
1238
1239# Only change ownership of a file or directory if it is not on an NFS
1240# filesystem.
1241function safe_chown() {
1242 _safe_permission_operation chown $@
1243}
1244
1245# Only change permissions of a file or directory if it is not on an
1246# NFS filesystem.
1247function safe_chmod() {
1248 _safe_permission_operation chmod $@
1249}
Dean Troyer1a6d4492013-06-03 16:47:36 -05001250
Monty Taylor408a4a72013-08-02 15:43:47 -04001251# ``pip install -e`` the package, which processes the dependencies
1252# using pip before running `setup.py develop`
Monty Taylorb5bbaac2013-08-06 10:35:02 -03001253# Uses globals ``STACK_USER``, ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001254# setup_develop directory
1255function setup_develop() {
Sean Dague6c844632013-07-31 06:50:14 -04001256 local project_dir=$1
Sean Dague6c844632013-07-31 06:50:14 -04001257
1258 echo "cd $REQUIREMENTS_DIR; $SUDO_CMD python update.py $project_dir"
1259
Dean Troyer62d1d692013-08-01 17:40:40 -05001260 # Don't update repo if local changes exist
Doug Hellmannc3431bf2013-09-06 15:30:22 -04001261 (cd $project_dir && git diff --quiet)
1262 local update_requirements=$?
1263
1264 if [ $update_requirements -eq 0 ]; then
Dean Troyer62d1d692013-08-01 17:40:40 -05001265 (cd $REQUIREMENTS_DIR; \
1266 $SUDO_CMD python update.py $project_dir)
1267 fi
Sean Dague6c844632013-07-31 06:50:14 -04001268
Monty Taylorb5bbaac2013-08-06 10:35:02 -03001269 pip_install -e $project_dir
1270 # ensure that further actions can do things like setup.py sdist
Doug Hellmanne7002672013-09-05 08:10:07 -04001271 safe_chown -R $STACK_USER $1/*.egg-info
Doug Hellmannc3431bf2013-09-06 15:30:22 -04001272
Sean Daguefd98edb2013-10-24 14:57:59 -04001273 # We've just gone and possibly modified the user's source tree in an
1274 # automated way, which is considered bad form if it's a development
1275 # tree because we've screwed up their next git checkin. So undo it.
1276 #
1277 # However... there are some circumstances, like running in the gate
1278 # where we really really want the overridden version to stick. So provide
1279 # a variable that tells us whether or not we should UNDO the requirements
1280 # changes (this will be set to False in the OpenStack ci gate)
DennyZhang89d41ca2013-11-01 15:41:01 -05001281 if [ $UNDO_REQUIREMENTS = "True" ]; then
Sean Daguefd98edb2013-10-24 14:57:59 -04001282 if [ $update_requirements -eq 0 ]; then
1283 (cd $project_dir && git reset --hard)
1284 fi
Doug Hellmannc3431bf2013-09-06 15:30:22 -04001285 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001286}
1287
1288
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001289# Service wrapper to start services
1290# start_service service-name
1291function start_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001292 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001293 sudo /usr/sbin/service $1 start
1294 else
1295 sudo /sbin/service $1 start
1296 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001297}
1298
1299
1300# Service wrapper to stop services
1301# stop_service service-name
1302function stop_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001303 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001304 sudo /usr/sbin/service $1 stop
1305 else
1306 sudo /sbin/service $1 stop
1307 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -06001308}
1309
1310
1311# Normalize config values to True or False
Sirushti Murugesana8d41e32013-09-25 11:30:31 +05301312# Accepts as False: 0 no No NO false False FALSE
1313# Accepts as True: 1 yes Yes YES true True TRUE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001314# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -06001315function trueorfalse() {
1316 local default=$1
1317 local testval=$2
1318
1319 [[ -z "$testval" ]] && { echo "$default"; return; }
Sirushti Murugesana8d41e32013-09-25 11:30:31 +05301320 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
1321 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
Dean Troyer7f9aa712012-01-31 12:11:56 -06001322 echo "$default"
1323}
Dean Troyer27e32692012-03-16 16:16:56 -05001324
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001325
Adam Spierscb961592013-10-05 12:11:07 +01001326# Retrieve an image from a URL and upload into Glance.
Dean Troyerca0e3d02012-04-13 15:58:37 -05001327# Uses the following variables:
Adam Spierscb961592013-10-05 12:11:07 +01001328#
1329# - ``FILES`` must be set to the cache dir
1330# - ``GLANCE_HOSTPORT``
1331#
Dean Troyerca0e3d02012-04-13 15:58:37 -05001332# upload_image image-url glance-token
1333function upload_image() {
1334 local image_url=$1
1335 local token=$2
1336
1337 # Create a directory for the downloaded image tarballs.
1338 mkdir -p $FILES/images
1339
1340 # Downloads the image (uec ami+aki style), then extracts it.
1341 IMAGE_FNAME=`basename "$image_url"`
1342 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
1343 wget -c $image_url -O $FILES/$IMAGE_FNAME
1344 if [[ $? -ne 0 ]]; then
1345 echo "Not found: $image_url"
1346 return
1347 fi
1348 fi
1349
1350 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
1351 if [[ "$image_url" =~ 'openvz' ]]; then
1352 IMAGE="$FILES/${IMAGE_FNAME}"
1353 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
1354 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}"
1355 return
1356 fi
1357
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -07001358 # vmdk format images
1359 if [[ "$image_url" =~ '.vmdk' ]]; then
1360 IMAGE="$FILES/${IMAGE_FNAME}"
1361 IMAGE_NAME="${IMAGE_FNAME%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -07001362
1363 # Before we can upload vmdk type images to glance, we need to know it's
1364 # disk type, storage adapter, and networking adapter. These values are
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -07001365 # passed to glance as custom properties.
1366 # We take these values from the vmdk file if populated. Otherwise, we use
Ryan Hsua6273b92013-09-04 23:51:29 -07001367 # vmdk filename, which is expected in the following format:
1368 #
1369 # <name>-<disk type>:<storage adapter>:<network adapter>
1370 #
1371 # If the filename does not follow the above format then the vsphere
1372 # driver will supply default values.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -07001373
1374 # vmdk adapter type
1375 vmdk_adapter_type="$(head -25 $IMAGE | grep -a -F -m 1 'ddb.adapterType =' $IMAGE)"
1376 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
1377 vmdk_adapter_type="${vmdk_adapter_type%?}"
1378
1379 # vmdk disk type
1380 vmdk_create_type="$(head -25 $IMAGE | grep -a -F -m 1 'createType=' $IMAGE)"
1381 vmdk_create_type="${vmdk_create_type#*\"}"
1382 vmdk_create_type="${vmdk_create_type%?}"
1383 if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then
1384 vmdk_disktype="sparse"
1385 elif [[ "$vmdk_create_type" = "monolithicFlat" ]]; then
1386 die $LINENO "Monolithic flat disks should use a descriptor-data pair." \
1387 "Please provide the disk and not the descriptor."
1388 else
1389 #TODO(alegendre): handle streamOptimized once supported by VMware driver.
1390 vmdk_disktype="preallocated"
1391 fi
Ryan Hsua6273b92013-09-04 23:51:29 -07001392 property_string=`echo "$IMAGE_NAME" | grep -oP '(?<=-)(?!.*-).+:.+:.+$'`
1393 if [[ ! -z "$property_string" ]]; then
1394 IFS=':' read -a props <<< "$property_string"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -07001395 if [[ ! -z "${props[0]}" ]]; then
1396 vmdk_disktype="${props[0]}"
1397 fi
1398 if [[ ! -z "${props[1]}" ]]; then
1399 vmdk_adapter_type="${props[1]}"
1400 fi
Ryan Hsua6273b92013-09-04 23:51:29 -07001401 vmdk_net_adapter="${props[2]}"
1402 fi
1403
Ryan Hsu49f44862013-10-03 22:27:03 -07001404 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 -07001405 return
1406 fi
1407
Mate Lakatbc2ef922013-08-15 18:06:59 +01001408 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -05001409 # and should not be decompressed prior to loading
1410 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
1411 IMAGE="$FILES/${IMAGE_FNAME}"
1412 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
1413 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}"
1414 return
1415 fi
1416
Mate Lakatbc2ef922013-08-15 18:06:59 +01001417 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
1418 # and should not be decompressed prior to loading.
1419 # Setting metadata, so PV mode is used.
1420 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
1421 IMAGE="$FILES/${IMAGE_FNAME}"
1422 IMAGE_NAME="${IMAGE_FNAME%.xen-raw.tgz}"
1423 glance \
Sean Dague537d4022013-10-22 07:43:22 -04001424 --os-auth-token $token \
1425 --os-image-url http://$GLANCE_HOSTPORT \
1426 image-create \
Mate Lakatbc2ef922013-08-15 18:06:59 +01001427 --name "$IMAGE_NAME" --is-public=True \
1428 --container-format=tgz --disk-format=raw \
1429 --property vm_mode=xen < "${IMAGE}"
1430 return
1431 fi
1432
Dean Troyerca0e3d02012-04-13 15:58:37 -05001433 KERNEL=""
1434 RAMDISK=""
1435 DISK_FORMAT=""
1436 CONTAINER_FORMAT=""
1437 UNPACK=""
1438 case "$IMAGE_FNAME" in
1439 *.tar.gz|*.tgz)
1440 # Extract ami and aki files
1441 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
1442 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
1443 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
1444 xdir="$FILES/images/$IMAGE_NAME"
1445 rm -Rf "$xdir";
1446 mkdir "$xdir"
1447 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
1448 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -04001449 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001450 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -04001451 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001452 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -04001453 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001454 if [[ -z "$IMAGE_NAME" ]]; then
1455 IMAGE_NAME=$(basename "$IMAGE" ".img")
1456 fi
1457 ;;
1458 *.img)
1459 IMAGE="$FILES/$IMAGE_FNAME";
1460 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -05001461 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
1462 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
1463 DISK_FORMAT=$format
1464 else
1465 DISK_FORMAT=raw
1466 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -05001467 CONTAINER_FORMAT=bare
1468 ;;
1469 *.img.gz)
1470 IMAGE="$FILES/${IMAGE_FNAME}"
1471 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
1472 DISK_FORMAT=raw
1473 CONTAINER_FORMAT=bare
1474 UNPACK=zcat
1475 ;;
1476 *.qcow2)
1477 IMAGE="$FILES/${IMAGE_FNAME}"
1478 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
1479 DISK_FORMAT=qcow2
1480 CONTAINER_FORMAT=bare
1481 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +01001482 *.iso)
1483 IMAGE="$FILES/${IMAGE_FNAME}"
1484 IMAGE_NAME=$(basename "$IMAGE" ".iso")
1485 DISK_FORMAT=iso
1486 CONTAINER_FORMAT=bare
1487 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -05001488 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
1489 esac
1490
1491 if [ "$CONTAINER_FORMAT" = "bare" ]; then
1492 if [ "$UNPACK" = "zcat" ]; then
Christian Berendta7a219a2013-07-30 18:22:32 +02001493 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 -05001494 else
Christian Berendta7a219a2013-07-30 18:22:32 +02001495 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 -05001496 fi
1497 else
1498 # Use glance client to add the kernel the root filesystem.
1499 # We parse the results of the first upload to get the glance ID of the
1500 # kernel for use when uploading the root filesystem.
1501 KERNEL_ID=""; RAMDISK_ID="";
1502 if [ -n "$KERNEL" ]; then
Christian Berendta7a219a2013-07-30 18:22:32 +02001503 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 -05001504 fi
1505 if [ -n "$RAMDISK" ]; then
Christian Berendta7a219a2013-07-30 18:22:32 +02001506 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 -05001507 fi
Christian Berendta7a219a2013-07-30 18:22:32 +02001508 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 -05001509 fi
1510}
1511
Dean Troyer1a6d4492013-06-03 16:47:36 -05001512
Dean Troyerc1b486a2012-11-05 14:26:09 -06001513# Set the database backend to use
1514# When called from stackrc/localrc DATABASE_BACKENDS has not been
1515# initialized yet, just save the configuration selection and call back later
1516# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +01001517#
1518# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -06001519function use_database {
1520 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -06001521 # No backends registered means this is likely called from ``localrc``
1522 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -06001523 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +00001524 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 +01001525 else
Dean Troyerafc29fe2013-02-07 15:56:24 -06001526 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +01001527 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -06001528 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -06001529}
1530
Dean Troyer1a6d4492013-06-03 16:47:36 -05001531
Terry Wilson428af5a2012-11-01 16:12:39 -04001532# Toggle enable/disable_service for services that must run exclusive of each other
1533# $1 The name of a variable containing a space-separated list of services
1534# $2 The name of a variable in which to store the enabled service's name
1535# $3 The name of the service to enable
1536function use_exclusive_service {
1537 local options=${!1}
1538 local selection=$3
1539 out=$2
1540 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1541 for opt in $options;do
1542 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1543 done
1544 eval "$out=$selection"
1545 return 0
1546}
Dean Troyerca0e3d02012-04-13 15:58:37 -05001547
Dean Troyer1a6d4492013-06-03 16:47:36 -05001548
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001549# Wait for an HTTP server to start answering requests
1550# wait_for_service timeout url
1551function wait_for_service() {
1552 local timeout=$1
1553 local url=$2
JUN JIE NAN0aa85342013-09-13 15:47:09 +08001554 timeout $timeout sh -c "while ! curl --noproxy '*' -s $url >/dev/null; do sleep 1; done"
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001555}
1556
Dean Troyer1a6d4492013-06-03 16:47:36 -05001557
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001558# Wrapper for ``yum`` to set proxy environment variables
Adam Spierscb961592013-10-05 12:11:07 +01001559# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001560# yum_install package [package ...]
1561function yum_install() {
1562 [[ "$OFFLINE" = "True" ]] && return
1563 local sudo="sudo"
1564 [[ "$(id -u)" = "0" ]] && sudo="env"
1565 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001566 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001567 yum install -y "$@"
1568}
1569
Dean Troyer1a6d4492013-06-03 16:47:36 -05001570
1571# zypper wrapper to set arguments correctly
1572# zypper_install package [package ...]
1573function zypper_install() {
1574 [[ "$OFFLINE" = "True" ]] && return
1575 local sudo="sudo"
1576 [[ "$(id -u)" = "0" ]] && sudo="env"
1577 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1578 zypper --non-interactive install --auto-agree-with-licenses "$@"
1579}
1580
1581
Nachi Uenofda946e2012-10-24 17:26:02 -07001582# ping check
1583# Uses globals ``ENABLED_SERVICES``
Dean Troyer1a6d4492013-06-03 16:47:36 -05001584# ping_check from-net ip boot-timeout expected
Nachi Uenofda946e2012-10-24 17:26:02 -07001585function ping_check() {
Mark McClainb05c8762013-07-06 23:29:39 -04001586 if is_service_enabled neutron; then
1587 _ping_check_neutron "$1" $2 $3 $4
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001588 return
1589 fi
1590 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -07001591}
1592
1593# ping check for nova
1594# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
1595function _ping_check_novanet() {
1596 local from_net=$1
1597 local ip=$2
1598 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001599 local expected=${4:-"True"}
1600 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001601 MULTI_HOST=`trueorfalse False $MULTI_HOST`
1602 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
Nachi Uenofda946e2012-10-24 17:26:02 -07001603 return
1604 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001605 if [[ "$expected" = "True" ]]; then
1606 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
1607 else
1608 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
1609 fi
1610 if ! timeout $boot_timeout sh -c "$check_command"; then
1611 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -08001612 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001613 else
Nachi Ueno07115eb2013-02-26 12:38:18 -08001614 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001615 fi
Nachi Uenofda946e2012-10-24 17:26:02 -07001616 fi
1617}
1618
Nachi Ueno6769b162013-08-12 18:18:56 -07001619# Get ip of instance
1620function get_instance_ip(){
1621 local vm_id=$1
1622 local network_name=$2
1623 local nova_result="$(nova show $vm_id)"
1624 local ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
1625 if [[ $ip = "" ]];then
1626 echo "$nova_result"
1627 die $LINENO "[Fail] Coudn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -07001628 fi
1629 echo $ip
1630}
Dean Troyer1a6d4492013-06-03 16:47:36 -05001631
Nachi Uenofda946e2012-10-24 17:26:02 -07001632# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001633
Dean Troyer1a6d4492013-06-03 16:47:36 -05001634# ssh_check net-name key-file floating-ip default-user active-timeout
Nachi Uenofda946e2012-10-24 17:26:02 -07001635function ssh_check() {
Mark McClainb05c8762013-07-06 23:29:39 -04001636 if is_service_enabled neutron; then
1637 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001638 return
1639 fi
1640 _ssh_check_novanet "$1" $2 $3 $4 $5
1641}
1642
1643function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -07001644 local NET_NAME=$1
1645 local KEY_FILE=$2
1646 local FLOATING_IP=$3
1647 local DEFAULT_INSTANCE_USER=$4
1648 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -06001649 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -05001650 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 -08001651 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -07001652 fi
1653}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001654
Vincent Untz856a11e2012-11-21 16:04:12 +01001655
Vincent Untz856a11e2012-11-21 16:04:12 +01001656# Add a user to a group.
1657# add_user_to_group user group
1658function add_user_to_group() {
1659 local user=$1
1660 local group=$2
1661
1662 if [[ -z "$os_VENDOR" ]]; then
1663 GetOSVersion
1664 fi
1665
1666 # SLE11 and openSUSE 12.2 don't have the usual usermod
1667 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1668 sudo usermod -a -G "$group" "$user"
1669 else
1670 sudo usermod -A "$group" "$user"
1671 fi
1672}
1673
1674
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001675# Get the path to the direcotry where python executables are installed.
1676# get_python_exec_prefix
1677function get_python_exec_prefix() {
Martin Vidner4f9b33d2013-06-27 13:11:22 +00001678 if is_fedora || is_suse; then
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001679 echo "/usr/bin"
1680 else
1681 echo "/usr/local/bin"
1682 fi
1683}
1684
Dean Troyer1a6d4492013-06-03 16:47:36 -05001685
Vincent Untz856a11e2012-11-21 16:04:12 +01001686# Get the location of the $module-rootwrap executables, where module is cinder
1687# or nova.
1688# get_rootwrap_location module
1689function get_rootwrap_location() {
1690 local module=$1
1691
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001692 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +01001693}
1694
Dean Troyer1a6d4492013-06-03 16:47:36 -05001695
Vincent Untz8ec27222012-11-29 09:25:31 +01001696# Get the path to the pip command.
1697# get_pip_command
1698function get_pip_command() {
Dean Troyerd2cfcaa2013-08-01 14:17:27 -05001699 which pip || which pip-python
Ian Wienand535a8142013-05-15 09:25:27 +10001700
1701 if [ $? -ne 0 ]; then
1702 die $LINENO "Unable to find pip; cannot continue"
1703 fi
Vincent Untz8ec27222012-11-29 09:25:31 +01001704}
Vincent Untz856a11e2012-11-21 16:04:12 +01001705
Dean Troyer1a6d4492013-06-03 16:47:36 -05001706
Ian Wienand0488edd2013-04-11 12:04:36 +10001707# Path permissions sanity check
1708# check_path_perm_sanity path
1709function check_path_perm_sanity() {
1710 # Ensure no element of the path has 0700 permissions, which is very
1711 # likely to cause issues for daemons. Inspired by default 0700
1712 # homedir permissions on RHEL and common practice of making DEST in
1713 # the stack user's homedir.
1714
1715 local real_path=$(readlink -f $1)
1716 local rebuilt_path=""
1717 for i in $(echo ${real_path} | tr "/" " "); do
1718 rebuilt_path=$rebuilt_path"/"$i
1719
1720 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
1721 echo "*** DEST path element"
1722 echo "*** ${rebuilt_path}"
1723 echo "*** appears to have 0700 permissions."
1724 echo "*** This is very likely to cause fatal issues for devstack daemons."
1725
1726 if [[ -n "$SKIP_PATH_SANITY" ]]; then
1727 return
1728 else
1729 echo "*** Set SKIP_PATH_SANITY to skip this check"
1730 die $LINENO "Invalid path permissions"
1731 fi
1732 fi
1733 done
1734}
1735
Dean Troyer1a6d4492013-06-03 16:47:36 -05001736
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001737# This function recursively compares versions, and is not meant to be
1738# called by anything other than vercmp_numbers below. This function does
1739# not work with alphabetic versions.
1740#
1741# _vercmp_r sep ver1 ver2
1742function _vercmp_r {
Sean Dague537d4022013-10-22 07:43:22 -04001743 typeset sep
1744 typeset -a ver1=() ver2=()
1745 sep=$1; shift
1746 ver1=("${@:1:sep}")
1747 ver2=("${@:sep+1}")
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001748
Sean Dague537d4022013-10-22 07:43:22 -04001749 if ((ver1 > ver2)); then
1750 echo 1; return 0
1751 elif ((ver2 > ver1)); then
1752 echo -1; return 0
1753 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001754
Sean Dague537d4022013-10-22 07:43:22 -04001755 if ((sep <= 1)); then
1756 echo 0; return 0
1757 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001758
Sean Dague537d4022013-10-22 07:43:22 -04001759 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001760}
1761
1762
1763# This function compares two versions and is meant to be called by
1764# external callers. Please note the function assumes non-alphabetic
1765# versions. For example, this will work:
1766#
1767# vercmp_numbers 1.10 1.4
1768#
1769# The above will return "1", as 1.10 is greater than 1.4.
1770#
1771# vercmp_numbers 5.2 6.4
1772#
1773# The above will return "-1", as 5.2 is less than 6.4.
1774#
1775# vercmp_numbers 4.0 4.0
1776#
1777# The above will return "0", as the versions are equal.
1778#
1779# vercmp_numbers ver1 ver2
1780vercmp_numbers() {
Sean Dague537d4022013-10-22 07:43:22 -04001781 typeset v1=$1 v2=$2 sep
1782 typeset -a ver1 ver2
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001783
Sean Dague537d4022013-10-22 07:43:22 -04001784 IFS=. read -ra ver1 <<< "$v1"
1785 IFS=. read -ra ver2 <<< "$v2"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001786
Sean Dague537d4022013-10-22 07:43:22 -04001787 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001788}
1789
1790
Dean Troyer533e14d2013-08-30 15:11:22 -05001791# ``policy_add policy_file policy_name policy_permissions``
1792#
1793# Add a policy to a policy.json file
1794# Do nothing if the policy already exists
1795
1796function policy_add() {
1797 local policy_file=$1
1798 local policy_name=$2
1799 local policy_perm=$3
1800
1801 if grep -q ${policy_name} ${policy_file}; then
1802 echo "Policy ${policy_name} already exists in ${policy_file}"
1803 return
1804 fi
1805
1806 # Add a terminating comma to policy lines without one
1807 # Remove the closing '}' and all lines following to the end-of-file
1808 local tmpfile=$(mktemp)
1809 uniq ${policy_file} | sed -e '
1810 s/]$/],/
1811 /^[}]/,$d
1812 ' > ${tmpfile}
1813
1814 # Append policy and closing brace
1815 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
1816 echo "}" >>${tmpfile}
1817
1818 mv ${tmpfile} ${policy_file}
1819}
1820
1821
Salvatore Orlando05ae8332013-08-20 14:51:08 -07001822# This function sets log formatting options for colorizing log
1823# output to stdout. It is meant to be called by lib modules.
1824# The last two parameters are optional and can be used to specify
1825# non-default value for project and user format variables.
1826# Defaults are respectively 'project_name' and 'user_name'
1827#
1828# setup_colorized_logging something.conf SOMESECTION
1829function setup_colorized_logging() {
1830 local conf_file=$1
1831 local conf_section=$2
1832 local project_var=${3:-"project_name"}
1833 local user_var=${4:-"user_name"}
1834 # Add color to logging output
1835 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"
1836 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"
1837 iniset $conf_file $conf_section logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
1838 iniset $conf_file $conf_section logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
1839}
1840
Dean Troyer27e32692012-03-16 16:16:56 -05001841# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +00001842$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001843
1844
1845# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -04001846# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -04001847# End: