blob: 27f3384e88eae896eaa7924b4187224a0a5a6641 [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``
5# ``EROR_ON_CLONE``
6# ``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 Troyer4a43b7b2012-08-28 17:43:40 -050021# Exit 0 if address is in network or 1 if address is not in
22# network or netaddr library is not installed.
23# address_in_net ip-address ip-range
Vishvananda Ishayac9ad14b2012-07-03 20:29:01 +000024function address_in_net() {
25 python -c "
26import netaddr
27import sys
28sys.exit(netaddr.IPAddress('$1') not in netaddr.IPNetwork('$2'))
29"
30}
31
32
Dean Troyer4a43b7b2012-08-28 17:43:40 -050033# Wrapper for ``apt-get`` to set cache and proxy environment variables
34# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -050035# apt_get operation package [package ...]
Dean Troyer7f9aa712012-01-31 12:11:56 -060036function apt_get() {
Dean Troyerd0b21e22012-03-07 14:52:25 -060037 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer7f9aa712012-01-31 12:11:56 -060038 local sudo="sudo"
39 [[ "$(id -u)" = "0" ]] && sudo="env"
40 $sudo DEBIAN_FRONTEND=noninteractive \
41 http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +090042 no_proxy=$no_proxy \
Dean Troyer7f9aa712012-01-31 12:11:56 -060043 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
44}
45
46
47# Gracefully cp only if source file/dir exists
48# cp_it source destination
49function cp_it {
50 if [ -e $1 ] || [ -d $1 ]; then
51 cp -pRL $1 $2
52 fi
53}
54
55
Dean Troyer27e32692012-03-16 16:16:56 -050056# Prints "message" and exits
57# die "message"
58function die() {
Dean Troyer489bd2a2012-03-02 10:44:29 -060059 local exitcode=$?
Nachi Ueno07115eb2013-02-26 12:38:18 -080060 if [ $exitcode == 0 ]; then
61 exitcode=1
62 fi
Dean Troyer27e32692012-03-16 16:16:56 -050063 set +o xtrace
Nachi Ueno07115eb2013-02-26 12:38:18 -080064 local msg="[ERROR] $0:$1 $2"
65 echo $msg 1>&2;
66 if [[ -n ${SCREEN_LOGDIR} ]]; then
67 echo $msg >> "${SCREEN_LOGDIR}/error.log"
68 fi
Dean Troyer27e32692012-03-16 16:16:56 -050069 exit $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -060070}
71
72
73# Checks an environment variable is not set or has length 0 OR if the
74# exit code is non-zero and prints "message" and exits
75# NOTE: env-var is the variable name without a '$'
76# die_if_not_set env-var "message"
77function die_if_not_set() {
Dean Troyer27e32692012-03-16 16:16:56 -050078 (
79 local exitcode=$?
80 set +o xtrace
Nachi Ueno07115eb2013-02-26 12:38:18 -080081 local evar=$2; shift
Dean Troyer27e32692012-03-16 16:16:56 -050082 if ! is_set $evar || [ $exitcode != 0 ]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -080083 die $@
Dean Troyer27e32692012-03-16 16:16:56 -050084 fi
85 )
Dean Troyer489bd2a2012-03-02 10:44:29 -060086}
87
88
Dean Troyer48352ee2012-12-12 12:50:38 -060089# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
90# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
91# ``localrc`` or on the command line if necessary::
92#
93# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
94#
95# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
96
97function export_proxy_variables() {
98 if [[ -n "$http_proxy" ]]; then
99 export http_proxy=$http_proxy
100 fi
101 if [[ -n "$https_proxy" ]]; then
102 export https_proxy=$https_proxy
103 fi
104 if [[ -n "$no_proxy" ]]; then
105 export no_proxy=$no_proxy
106 fi
107}
108
109
Dean Troyer489bd2a2012-03-02 10:44:29 -0600110# Grab a numbered field from python prettytable output
111# Fields are numbered starting with 1
112# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
113# get_field field-number
114function get_field() {
115 while read data; do
116 if [ "$1" -lt 0 ]; then
117 field="(\$(NF$1))"
118 else
119 field="\$$(($1 + 1))"
120 fi
121 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
122 done
123}
124
125
Isaku Yamahata8c438092013-02-12 22:30:56 +0900126function _get_package_dir() {
127 local pkg_dir
128 if is_ubuntu; then
129 pkg_dir=$FILES/apts
130 elif is_fedora; then
131 pkg_dir=$FILES/rpms
132 elif is_suse; then
133 pkg_dir=$FILES/rpms-suse
134 else
135 exit_distro_not_supported "list of packages"
136 fi
137 echo "$pkg_dir"
138}
139
Dean Troyer7e270512012-06-14 15:23:24 -0500140# get_packages() collects a list of package names of any type from the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500141# prerequisite files in ``files/{apts|rpms}``. The list is intended
142# to be passed to a package installer such as apt or yum.
Dean Troyer7e270512012-06-14 15:23:24 -0500143#
Isaku Yamahata8c438092013-02-12 22:30:56 +0900144# Only packages required for the services in 1st argument will be
Dean Troyer7e270512012-06-14 15:23:24 -0500145# included. Two bits of metadata are recognized in the prerequisite files:
146# - ``# NOPRIME`` defers installation to be performed later in stack.sh
147# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
148# of the package to the distros listed. The distro names are case insensitive.
Dean Troyer7e270512012-06-14 15:23:24 -0500149function get_packages() {
Isaku Yamahata8c438092013-02-12 22:30:56 +0900150 local services=$1
151 local package_dir=$(_get_package_dir)
Dean Troyer7e270512012-06-14 15:23:24 -0500152 local file_to_parse
153 local service
154
155 if [[ -z "$package_dir" ]]; then
156 echo "No package directory supplied"
157 return 1
158 fi
159 if [[ -z "$DISTRO" ]]; then
Vincent Untz855c5872012-10-04 13:36:46 +0200160 GetDistro
Dean Troyer7e270512012-06-14 15:23:24 -0500161 fi
Isaku Yamahata8c438092013-02-12 22:30:56 +0900162 for service in general ${services//,/ }; do
Dean Troyer7e270512012-06-14 15:23:24 -0500163 # Allow individual services to specify dependencies
164 if [[ -e ${package_dir}/${service} ]]; then
165 file_to_parse="${file_to_parse} $service"
166 fi
167 # NOTE(sdague) n-api needs glance for now because that's where
168 # glance client is
169 if [[ $service == n-api ]]; then
170 if [[ ! $file_to_parse =~ nova ]]; then
171 file_to_parse="${file_to_parse} nova"
172 fi
173 if [[ ! $file_to_parse =~ glance ]]; then
174 file_to_parse="${file_to_parse} glance"
175 fi
176 elif [[ $service == c-* ]]; then
177 if [[ ! $file_to_parse =~ cinder ]]; then
178 file_to_parse="${file_to_parse} cinder"
179 fi
John H. Tran93361642012-07-26 11:22:05 -0700180 elif [[ $service == ceilometer-* ]]; then
181 if [[ ! $file_to_parse =~ ceilometer ]]; then
182 file_to_parse="${file_to_parse} ceilometer"
183 fi
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100184 elif [[ $service == s-* ]]; then
185 if [[ ! $file_to_parse =~ swift ]]; then
186 file_to_parse="${file_to_parse} swift"
187 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500188 elif [[ $service == n-* ]]; then
189 if [[ ! $file_to_parse =~ nova ]]; then
190 file_to_parse="${file_to_parse} nova"
191 fi
192 elif [[ $service == g-* ]]; then
193 if [[ ! $file_to_parse =~ glance ]]; then
194 file_to_parse="${file_to_parse} glance"
195 fi
196 elif [[ $service == key* ]]; then
197 if [[ ! $file_to_parse =~ keystone ]]; then
198 file_to_parse="${file_to_parse} keystone"
199 fi
Robert Collins0a9954f2012-11-20 11:34:25 +1300200 elif [[ $service == q-* ]]; then
201 if [[ ! $file_to_parse =~ quantum ]]; then
202 file_to_parse="${file_to_parse} quantum"
203 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500204 fi
205 done
206
207 for file in ${file_to_parse}; do
208 local fname=${package_dir}/${file}
209 local OIFS line package distros distro
210 [[ -e $fname ]] || continue
211
212 OIFS=$IFS
213 IFS=$'\n'
214 for line in $(<${fname}); do
215 if [[ $line =~ "NOPRIME" ]]; then
216 continue
217 fi
218
219 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
220 # We are using BASH regexp matching feature.
221 package=${BASH_REMATCH[1]}
222 distros=${BASH_REMATCH[2]}
223 # In bash ${VAR,,} will lowecase VAR
224 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
225 continue
226 fi
227
228 echo ${line%#*}
229 done
230 IFS=$OIFS
231 done
232}
233
234
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500235# Determine OS Vendor, Release and Update
236# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
237# Returns results in global variables:
238# os_VENDOR - vendor name
239# os_RELEASE - release
240# os_UPDATE - update
241# os_PACKAGE - package type
242# os_CODENAME - vendor's codename for release
243# GetOSVersion
244GetOSVersion() {
245 # Figure out which vendor we are
246 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
247 # OS/X
248 os_VENDOR=`sw_vers -productName`
249 os_RELEASE=`sw_vers -productVersion`
250 os_UPDATE=${os_RELEASE##*.}
251 os_RELEASE=${os_RELEASE%.*}
252 os_PACKAGE=""
253 if [[ "$os_RELEASE" =~ "10.7" ]]; then
254 os_CODENAME="lion"
255 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
256 os_CODENAME="snow leopard"
257 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
258 os_CODENAME="leopard"
259 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
260 os_CODENAME="tiger"
261 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
262 os_CODENAME="panther"
263 else
264 os_CODENAME=""
265 fi
266 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
267 os_VENDOR=$(lsb_release -i -s)
268 os_RELEASE=$(lsb_release -r -s)
269 os_UPDATE=""
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100270 os_PACKAGE="rpm"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500271 if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
272 os_PACKAGE="deb"
Vincent Untz856a11e2012-11-21 16:04:12 +0100273 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
274 lsb_release -d -s | grep -q openSUSE
275 if [[ $? -eq 0 ]]; then
276 os_VENDOR="openSUSE"
277 fi
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100278 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
279 os_VENDOR="Red Hat"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500280 fi
281 os_CODENAME=$(lsb_release -c -s)
282 elif [[ -r /etc/redhat-release ]]; then
283 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
284 # CentOS release 5.5 (Final)
285 # CentOS Linux release 6.0 (Final)
286 # Fedora release 16 (Verne)
287 os_CODENAME=""
288 for r in "Red Hat" CentOS Fedora; do
289 os_VENDOR=$r
290 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
291 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
292 os_CODENAME=${ver#*|}
293 os_RELEASE=${ver%|*}
294 os_UPDATE=${os_RELEASE##*.}
295 os_RELEASE=${os_RELEASE%.*}
296 break
297 fi
298 os_VENDOR=""
299 done
300 os_PACKAGE="rpm"
Vincent Untz856a11e2012-11-21 16:04:12 +0100301 elif [[ -r /etc/SuSE-release ]]; then
302 for r in openSUSE "SUSE Linux"; do
303 if [[ "$r" = "SUSE Linux" ]]; then
304 os_VENDOR="SUSE LINUX"
305 else
306 os_VENDOR=$r
307 fi
308
309 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
310 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
311 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
312 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
313 break
314 fi
315 os_VENDOR=""
316 done
317 os_PACKAGE="rpm"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500318 fi
319 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
320}
321
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300322# git update using reference as a branch.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500323# git_update_branch ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300324function git_update_branch() {
325
326 GIT_BRANCH=$1
327
328 git checkout -f origin/$GIT_BRANCH
329 # a local branch might not exist
330 git branch -D $GIT_BRANCH || true
331 git checkout -b $GIT_BRANCH
332}
333
334
335# git update using reference as a tag. Be careful editing source at that repo
336# as working copy will be in a detached mode
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500337# git_update_tag ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300338function git_update_tag() {
339
340 GIT_TAG=$1
341
342 git tag -d $GIT_TAG
343 # fetching given tag only
344 git fetch origin tag $GIT_TAG
345 git checkout -f $GIT_TAG
346}
347
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500348
Andrew Laskif900bd72012-09-05 17:23:14 -0400349# git update using reference as a branch.
350# git_update_remote_branch ref
351function git_update_remote_branch() {
352
353 GIT_BRANCH=$1
354
355 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
356}
357
358
Dean Troyera9e0a482012-07-09 14:07:23 -0500359# Translate the OS version values into common nomenclature
360# Sets ``DISTRO`` from the ``os_*`` values
361function GetDistro() {
362 GetOSVersion
363 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
364 # 'Everyone' refers to Ubuntu releases by the code name adjective
365 DISTRO=$os_CODENAME
366 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
367 # For Fedora, just use 'f' and the release
368 DISTRO="f$os_RELEASE"
Vincent Untz856a11e2012-11-21 16:04:12 +0100369 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
370 DISTRO="opensuse-$os_RELEASE"
371 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
372 # For SLE, also use the service pack
373 if [[ -z "$os_UPDATE" ]]; then
374 DISTRO="sle${os_RELEASE}"
375 else
376 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
377 fi
Dean Troyera9e0a482012-07-09 14:07:23 -0500378 else
379 # Catch-all for now is Vendor + Release + Update
380 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
381 fi
382 export DISTRO
383}
384
385
Vincent Untzc18b9652012-12-04 12:36:34 +0100386# Determine if current distribution is an Ubuntu-based distribution.
387# It will also detect non-Ubuntu but Debian-based distros; this is not an issue
388# since Debian and Ubuntu should be compatible.
389# is_ubuntu
390function is_ubuntu {
391 if [[ -z "$os_PACKAGE" ]]; then
392 GetOSVersion
393 fi
394
395 [ "$os_PACKAGE" = "deb" ]
396}
397
398
Vincent Untz00011c02012-12-06 09:56:32 +0100399# Determine if current distribution is a Fedora-based distribution
400# (Fedora, RHEL, CentOS).
401# is_fedora
402function is_fedora {
403 if [[ -z "$os_VENDOR" ]]; then
404 GetOSVersion
405 fi
406
407 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ]
408}
409
410
Vincent Untz856a11e2012-11-21 16:04:12 +0100411# Determine if current distribution is a SUSE-based distribution
412# (openSUSE, SLE).
413# is_suse
414function is_suse {
415 if [[ -z "$os_VENDOR" ]]; then
416 GetOSVersion
417 fi
418
Steve Baker1a7bbd22012-12-03 17:04:02 +1300419 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
Vincent Untz856a11e2012-11-21 16:04:12 +0100420}
421
422
Vincent Untz00011c02012-12-06 09:56:32 +0100423# Exit after outputting a message about the distribution not being supported.
424# exit_distro_not_supported [optional-string-telling-what-is-missing]
425function exit_distro_not_supported {
426 if [[ -z "$DISTRO" ]]; then
427 GetDistro
428 fi
429
430 if [ $# -gt 0 ]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800431 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
Vincent Untz00011c02012-12-06 09:56:32 +0100432 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800433 die $LINENO "Support for $DISTRO is incomplete."
Vincent Untz00011c02012-12-06 09:56:32 +0100434 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100435}
436
437
Dean Troyer7f9aa712012-01-31 12:11:56 -0600438# git clone only if directory doesn't exist already. Since ``DEST`` might not
439# be owned by the installation user, we create the directory and change the
440# ownership to the proper user.
441# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700442# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
443# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500444# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600445# git_clone remote dest-dir branch
446function git_clone {
447 [[ "$OFFLINE" = "True" ]] && return
448
449 GIT_REMOTE=$1
450 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300451 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600452
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300453 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600454 # If our branch name is a gerrit style refs/changes/...
455 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700456 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600457 git clone $GIT_REMOTE $GIT_DEST
458 fi
459 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300460 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600461 else
462 # do a full clone only if the directory doesn't exist
463 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700464 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600465 git clone $GIT_REMOTE $GIT_DEST
466 cd $GIT_DEST
467 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300468 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600469 elif [[ "$RECLONE" == "yes" ]]; then
470 # if it does exist then simulate what clone does if asked to RECLONE
471 cd $GIT_DEST
472 # set the url to pull from and fetch
473 git remote set-url origin $GIT_REMOTE
474 git fetch origin
475 # remove the existing ignored files (like pyc) as they cause breakage
476 # (due to the py files having older timestamps than our pyc, so python
477 # thinks the pyc files are correct using them)
478 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300479
480 # handle GIT_REF accordingly to type (tag, branch)
481 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
482 git_update_tag $GIT_REF
483 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
484 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400485 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
486 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300487 else
488 echo $GIT_REF is neither branch nor tag
489 exit 1
490 fi
491
Dean Troyer7f9aa712012-01-31 12:11:56 -0600492 fi
493 fi
494}
495
496
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500497# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200498# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500499function inicomment() {
500 local file=$1
501 local section=$2
502 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100503 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500504}
505
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200506# Uncomment an option in an INI file
507# iniuncomment config-file section option
508function iniuncomment() {
509 local file=$1
510 local section=$2
511 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100512 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200513}
514
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500515
516# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500517# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500518function iniget() {
519 local file=$1
520 local section=$2
521 local option=$3
522 local line
Attila Fazekas588eb412012-12-20 10:57:16 +0100523 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500524 echo ${line#*=}
525}
526
Attila Fazekas588eb412012-12-20 10:57:16 +0100527# Determinate is the given option present in the INI file
528# ini_has_option config-file section option
529function ini_has_option() {
530 local file=$1
531 local section=$2
532 local option=$3
533 local line
534 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
535 [ -n "$line" ]
536}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500537
538# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500539# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500540function iniset() {
541 local file=$1
542 local section=$2
543 local option=$3
544 local value=$4
Attila Fazekas588eb412012-12-20 10:57:16 +0100545 if ! grep -q "^\[$section\]" "$file"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500546 # Add section at the end
Attila Fazekas588eb412012-12-20 10:57:16 +0100547 echo -e "\n[$section]" >>"$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500548 fi
Attila Fazekas588eb412012-12-20 10:57:16 +0100549 if ! ini_has_option "$file" "$section" "$option"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500550 # Add it
Attila Fazekas588eb412012-12-20 10:57:16 +0100551 sed -i -e "/^\[$section\]/ a\\
Dean Troyer09e636e2012-03-19 16:31:12 -0500552$option = $value
Attila Fazekas588eb412012-12-20 10:57:16 +0100553" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500554 else
555 # Replace it
Attila Fazekas588eb412012-12-20 10:57:16 +0100556 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500557 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500558}
559
560
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000561# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500562# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000563#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500564# Multiple services specified as arguments are ``OR``'ed together; the test
565# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000566#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500567# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000568# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500569# **cinder** returns true if any service enabled start with **c-**
570# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000571# **glance** returns true if any service enabled start with **g-**
572# **quantum** returns true if any service enabled start with **q-**
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100573# **swift** returns true if any service enabled start with **s-**
574# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
575# **s-** services will be enabled. This will be deprecated in the future.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500576#
577# Uses global ``ENABLED_SERVICES``
578# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000579function is_service_enabled() {
580 services=$@
581 for service in ${services}; do
582 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
583 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500584 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700585 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000586 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
587 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100588 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && return 0
589 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000590 done
591 return 1
592}
593
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500594
595# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
596# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400597function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500598 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400599 s/,,/,/g;
600 s/^,//;
601 s/,$//
602 '
603}
604
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500605
Doug Hellmannf04178f2012-07-05 17:10:03 -0400606# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500607# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400608#
609# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800610# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400611#
612# This function does not know about the special cases
613# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500614# Uses global ``ENABLED_SERVICES``
615# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400616function enable_service() {
617 local tmpsvcs="${ENABLED_SERVICES}"
618 for service in $@; do
619 if ! is_service_enabled $service; then
620 tmpsvcs+=",$service"
621 fi
622 done
623 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
624 disable_negated_services
625}
626
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500627
Doug Hellmannf04178f2012-07-05 17:10:03 -0400628# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500629# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400630#
631# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800632# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400633#
634# This function does not know about the special cases
635# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500636# Uses global ``ENABLED_SERVICES``
637# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400638function disable_service() {
639 local tmpsvcs=",${ENABLED_SERVICES},"
640 local service
641 for service in $@; do
642 if is_service_enabled $service; then
643 tmpsvcs=${tmpsvcs//,$service,/,}
644 fi
645 done
646 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
647}
648
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500649
Doug Hellmannf04178f2012-07-05 17:10:03 -0400650# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500651# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400652# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500653# Uses global ``ENABLED_SERVICES``
654# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400655function disable_all_services() {
656 ENABLED_SERVICES=""
657}
658
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500659
660# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800661# services except rabbit (rabbit) set in ``localrc``:
662# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500663# Uses global ``ENABLED_SERVICES``
664# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400665function disable_negated_services() {
666 local tmpsvcs="${ENABLED_SERVICES}"
667 local service
668 for service in ${tmpsvcs//,/ }; do
669 if [[ ${service} == -* ]]; then
670 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
671 fi
672 done
673 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
674}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600675
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500676
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500677# Distro-agnostic package installer
678# install_package package [package ...]
679function install_package() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100680 if is_ubuntu; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200681 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
682 NO_UPDATE_REPOS=True
683
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500684 apt_get install "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100685 elif is_fedora; then
686 yum_install "$@"
687 elif is_suse; then
688 zypper_install "$@"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500689 else
Vincent Untz00011c02012-12-06 09:56:32 +0100690 exit_distro_not_supported "installing packages"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500691 fi
692}
693
694
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200695# Distro-agnostic function to tell if a package is installed
696# is_package_installed package [package ...]
697function is_package_installed() {
698 if [[ -z "$@" ]]; then
699 return 1
700 fi
701
702 if [[ -z "$os_PACKAGE" ]]; then
703 GetOSVersion
704 fi
Vincent Untzc18b9652012-12-04 12:36:34 +0100705
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200706 if [[ "$os_PACKAGE" = "deb" ]]; then
707 dpkg -l "$@" > /dev/null
Vincent Untz00011c02012-12-06 09:56:32 +0100708 elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200709 rpm --quiet -q "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100710 else
711 exit_distro_not_supported "finding if a package is installed"
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200712 fi
713}
714
715
Dean Troyer489bd2a2012-03-02 10:44:29 -0600716# Test if the named environment variable is set and not zero length
717# is_set env-var
718function is_set() {
719 local var=\$"$1"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100720 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 -0600721}
722
723
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500724# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +0000725# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
726# ``TRACK_DEPENDS``, ``*_proxy`
Dean Troyer7f9aa712012-01-31 12:11:56 -0600727# pip_install package [package ...]
728function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600729 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500730 if [[ -z "$os_PACKAGE" ]]; then
731 GetOSVersion
732 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500733 if [[ $TRACK_DEPENDS = True ]] ; then
734 source $DEST/.venv/bin/activate
735 CMD_PIP=$DEST/.venv/bin/pip
736 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500737 else
Monty Taylor47f02062012-07-26 11:09:24 -0500738 SUDO_PIP="sudo"
Vincent Untz8ec27222012-11-29 09:25:31 +0100739 CMD_PIP=$(get_pip_command)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500740 fi
Maru Newby3a87edd2012-10-25 23:01:06 +0000741 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
742 PIP_MIRROR_OPT="--use-mirrors"
743 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500744 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600745 HTTP_PROXY=$http_proxy \
746 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900747 NO_PROXY=$no_proxy \
Maru Newby3a87edd2012-10-25 23:01:06 +0000748 $CMD_PIP install $PIP_MIRROR_OPT $@
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500749}
750
751
752# Service wrapper to restart services
753# restart_service service-name
754function restart_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100755 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600756 sudo /usr/sbin/service $1 restart
757 else
758 sudo /sbin/service $1 restart
759 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500760}
761
762
Dean Troyer15733352012-09-06 11:51:30 -0500763# Helper to launch a service in a named screen
764# screen_it service "command-line"
765function screen_it {
Dean Troyer15733352012-09-06 11:51:30 -0500766 SCREEN_NAME=${SCREEN_NAME:-stack}
jiajun xua9414242012-12-06 16:30:57 +0800767 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Vishvananda Ishaya58e21342013-02-11 16:48:12 -0800768 SCREEN_DEV=`trueorfalse True $SCREEN_DEV`
jiajun xua9414242012-12-06 16:30:57 +0800769
Dean Troyer15733352012-09-06 11:51:30 -0500770 if is_service_enabled $1; then
771 # Append the service to the screen rc file
772 screen_rc "$1" "$2"
773
774 screen -S $SCREEN_NAME -X screen -t $1
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +0000775
776 if [[ -n ${SCREEN_LOGDIR} ]]; then
777 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
778 screen -S $SCREEN_NAME -p $1 -X log on
779 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
780 fi
781
Vishvananda Ishaya58e21342013-02-11 16:48:12 -0800782 if [[ "$SCREEN_DEV" = "True" ]]; then
783 # sleep to allow bash to be ready to be send the command - we are
784 # creating a new window in screen and then sends characters, so if
785 # bash isn't running by the time we send the command, nothing happens
786 sleep 1.5
Dean Troyer15733352012-09-06 11:51:30 -0500787
Vishvananda Ishaya58e21342013-02-11 16:48:12 -0800788 NL=`echo -ne '\015'`
789 screen -S $SCREEN_NAME -p $1 -X stuff "$2 || touch \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
790 else
791 screen -S $SCREEN_NAME -p $1 -X exec /bin/bash -c "$2 || touch \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\""
Dean Troyer15733352012-09-06 11:51:30 -0500792 fi
Dean Troyer15733352012-09-06 11:51:30 -0500793 fi
794}
795
796
797# Screen rc file builder
798# screen_rc service "command-line"
799function screen_rc {
800 SCREEN_NAME=${SCREEN_NAME:-stack}
801 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
802 if [[ ! -e $SCREENRC ]]; then
803 # Name the screen session
804 echo "sessionname $SCREEN_NAME" > $SCREENRC
805 # Set a reasonable statusbar
806 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
807 echo "screen -t shell bash" >> $SCREENRC
808 fi
809 # If this service doesn't already exist in the screenrc file
810 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
811 NL=`echo -ne '\015'`
812 echo "screen -t $1 bash" >> $SCREENRC
813 echo "stuff \"$2$NL\"" >> $SCREENRC
814 fi
815}
816
jiajun xua9414242012-12-06 16:30:57 +0800817# Helper to remove the *.failure files under $SERVICE_DIR/$SCREEN_NAME
818# This is used for service_check when all the screen_it are called finished
819# init_service_check
820function init_service_check() {
821 SCREEN_NAME=${SCREEN_NAME:-stack}
822 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
823
824 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
825 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
826 fi
827
828 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
829}
830
831# Helper to get the status of each running service
832# service_check
833function service_check() {
834 local service
835 local failures
836 SCREEN_NAME=${SCREEN_NAME:-stack}
837 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
838
839
840 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
841 echo "No service status directory found"
842 return
843 fi
844
845 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
846 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null`
847
848 for service in $failures; do
849 service=`basename $service`
850 service=${service::-8}
851 echo "Error: Service $service is not running"
852 done
853
854 if [ -n "$failures" ]; then
855 echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
856 fi
857}
Dean Troyer15733352012-09-06 11:51:30 -0500858
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500859# ``pip install`` the dependencies of the package before ``setup.py develop``
860# so pip and not distutils processes the dependency chain
861# Uses globals ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500862# setup_develop directory
863function setup_develop() {
Monty Taylor47f02062012-07-26 11:09:24 -0500864 if [[ $TRACK_DEPENDS = True ]] ; then
865 SUDO_CMD="env"
866 else
867 SUDO_CMD="sudo"
868 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500869 (cd $1; \
870 python setup.py egg_info; \
871 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
872 depend_links=$(echo $raw_links | xargs); \
Dean Troyer1a3c9fe2012-09-29 17:25:02 -0500873 require_file=$([ ! -r *-info/requires.txt ] || echo "-r *-info/requires.txt"); \
874 pip_install $require_file $depend_links; \
Monty Taylor47f02062012-07-26 11:09:24 -0500875 $SUDO_CMD \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500876 HTTP_PROXY=$http_proxy \
877 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900878 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500879 python setup.py develop \
880 )
881}
882
883
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500884# Service wrapper to start services
885# start_service service-name
886function start_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100887 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600888 sudo /usr/sbin/service $1 start
889 else
890 sudo /sbin/service $1 start
891 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500892}
893
894
895# Service wrapper to stop services
896# stop_service service-name
897function stop_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100898 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600899 sudo /usr/sbin/service $1 stop
900 else
901 sudo /sbin/service $1 stop
902 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600903}
904
905
906# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500907# Accepts as False: 0 no false False FALSE
908# Accepts as True: 1 yes true True TRUE
909# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -0600910function trueorfalse() {
911 local default=$1
912 local testval=$2
913
914 [[ -z "$testval" ]] && { echo "$default"; return; }
915 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
916 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
917 echo "$default"
918}
Dean Troyer27e32692012-03-16 16:16:56 -0500919
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500920
Dean Troyerca0e3d02012-04-13 15:58:37 -0500921# Retrieve an image from a URL and upload into Glance
922# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500923# ``FILES`` must be set to the cache dir
924# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -0500925# upload_image image-url glance-token
926function upload_image() {
927 local image_url=$1
928 local token=$2
929
930 # Create a directory for the downloaded image tarballs.
931 mkdir -p $FILES/images
932
933 # Downloads the image (uec ami+aki style), then extracts it.
934 IMAGE_FNAME=`basename "$image_url"`
935 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
936 wget -c $image_url -O $FILES/$IMAGE_FNAME
937 if [[ $? -ne 0 ]]; then
938 echo "Not found: $image_url"
939 return
940 fi
941 fi
942
943 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
944 if [[ "$image_url" =~ 'openvz' ]]; then
945 IMAGE="$FILES/${IMAGE_FNAME}"
946 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
947 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}"
948 return
949 fi
950
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500951 # XenServer-ovf-format images are provided as .vhd.tgz as well
952 # and should not be decompressed prior to loading
953 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
954 IMAGE="$FILES/${IMAGE_FNAME}"
955 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
956 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}"
957 return
958 fi
959
Dean Troyerca0e3d02012-04-13 15:58:37 -0500960 KERNEL=""
961 RAMDISK=""
962 DISK_FORMAT=""
963 CONTAINER_FORMAT=""
964 UNPACK=""
965 case "$IMAGE_FNAME" in
966 *.tar.gz|*.tgz)
967 # Extract ami and aki files
968 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
969 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
970 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
971 xdir="$FILES/images/$IMAGE_NAME"
972 rm -Rf "$xdir";
973 mkdir "$xdir"
974 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
975 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
976 [ -f "$f" ] && echo "$f" && break; done; true)
977 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
978 [ -f "$f" ] && echo "$f" && break; done; true)
979 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
980 [ -f "$f" ] && echo "$f" && break; done; true)
981 if [[ -z "$IMAGE_NAME" ]]; then
982 IMAGE_NAME=$(basename "$IMAGE" ".img")
983 fi
984 ;;
985 *.img)
986 IMAGE="$FILES/$IMAGE_FNAME";
987 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -0500988 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
989 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
990 DISK_FORMAT=$format
991 else
992 DISK_FORMAT=raw
993 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -0500994 CONTAINER_FORMAT=bare
995 ;;
996 *.img.gz)
997 IMAGE="$FILES/${IMAGE_FNAME}"
998 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
999 DISK_FORMAT=raw
1000 CONTAINER_FORMAT=bare
1001 UNPACK=zcat
1002 ;;
1003 *.qcow2)
1004 IMAGE="$FILES/${IMAGE_FNAME}"
1005 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
1006 DISK_FORMAT=qcow2
1007 CONTAINER_FORMAT=bare
1008 ;;
1009 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
1010 esac
1011
1012 if [ "$CONTAINER_FORMAT" = "bare" ]; then
1013 if [ "$UNPACK" = "zcat" ]; then
1014 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --public --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < <(zcat --force "${IMAGE}")
1015 else
1016 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --public --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < "${IMAGE}"
1017 fi
1018 else
1019 # Use glance client to add the kernel the root filesystem.
1020 # We parse the results of the first upload to get the glance ID of the
1021 # kernel for use when uploading the root filesystem.
1022 KERNEL_ID=""; RAMDISK_ID="";
1023 if [ -n "$KERNEL" ]; then
1024 KERNEL_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-kernel" --public --container-format aki --disk-format aki < "$KERNEL" | grep ' id ' | get_field 2)
1025 fi
1026 if [ -n "$RAMDISK" ]; then
1027 RAMDISK_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-ramdisk" --public --container-format ari --disk-format ari < "$RAMDISK" | grep ' id ' | get_field 2)
1028 fi
1029 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "${IMAGE_NAME%.img}" --public --container-format ami --disk-format ami ${KERNEL_ID:+--property kernel_id=$KERNEL_ID} ${RAMDISK_ID:+--property ramdisk_id=$RAMDISK_ID} < "${IMAGE}"
1030 fi
1031}
1032
Dean Troyerc1b486a2012-11-05 14:26:09 -06001033# Set the database backend to use
1034# When called from stackrc/localrc DATABASE_BACKENDS has not been
1035# initialized yet, just save the configuration selection and call back later
1036# to validate it.
1037# $1 The name of the database backend to use (mysql, postgresql, ...)
1038function use_database {
1039 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -06001040 # No backends registered means this is likely called from ``localrc``
1041 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -06001042 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +00001043 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 +01001044 else
Dean Troyerafc29fe2013-02-07 15:56:24 -06001045 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +01001046 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -06001047 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -06001048}
1049
Terry Wilson428af5a2012-11-01 16:12:39 -04001050# Toggle enable/disable_service for services that must run exclusive of each other
1051# $1 The name of a variable containing a space-separated list of services
1052# $2 The name of a variable in which to store the enabled service's name
1053# $3 The name of the service to enable
1054function use_exclusive_service {
1055 local options=${!1}
1056 local selection=$3
1057 out=$2
1058 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1059 for opt in $options;do
1060 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1061 done
1062 eval "$out=$selection"
1063 return 0
1064}
Dean Troyerca0e3d02012-04-13 15:58:37 -05001065
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001066# Wait for an HTTP server to start answering requests
1067# wait_for_service timeout url
1068function wait_for_service() {
1069 local timeout=$1
1070 local url=$2
1071 timeout $timeout sh -c "while ! http_proxy= https_proxy= curl -s $url >/dev/null; do sleep 1; done"
1072}
1073
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001074# Wrapper for ``yum`` to set proxy environment variables
1075# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001076# yum_install package [package ...]
1077function yum_install() {
1078 [[ "$OFFLINE" = "True" ]] && return
1079 local sudo="sudo"
1080 [[ "$(id -u)" = "0" ]] && sudo="env"
1081 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001082 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001083 yum install -y "$@"
1084}
1085
Nachi Uenofda946e2012-10-24 17:26:02 -07001086# ping check
1087# Uses globals ``ENABLED_SERVICES``
1088function ping_check() {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001089 if is_service_enabled quantum; then
1090 _ping_check_quantum "$1" $2 $3 $4
1091 return
1092 fi
1093 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -07001094}
1095
1096# ping check for nova
1097# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
1098function _ping_check_novanet() {
1099 local from_net=$1
1100 local ip=$2
1101 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001102 local expected=${4:-"True"}
1103 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001104 MULTI_HOST=`trueorfalse False $MULTI_HOST`
1105 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
1106 sleep $boot_timeout
1107 return
1108 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001109 if [[ "$expected" = "True" ]]; then
1110 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
1111 else
1112 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
1113 fi
1114 if ! timeout $boot_timeout sh -c "$check_command"; then
1115 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -08001116 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001117 else
Nachi Ueno07115eb2013-02-26 12:38:18 -08001118 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001119 fi
Nachi Uenofda946e2012-10-24 17:26:02 -07001120 exit 1
1121 fi
1122}
1123
1124# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001125
Nachi Uenofda946e2012-10-24 17:26:02 -07001126function ssh_check() {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001127 if is_service_enabled quantum; then
1128 _ssh_check_quantum "$1" $2 $3 $4 $5
1129 return
1130 fi
1131 _ssh_check_novanet "$1" $2 $3 $4 $5
1132}
1133
1134function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -07001135 local NET_NAME=$1
1136 local KEY_FILE=$2
1137 local FLOATING_IP=$3
1138 local DEFAULT_INSTANCE_USER=$4
1139 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -06001140 local probe_cmd=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001141 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 -08001142 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -07001143 fi
1144}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001145
Vincent Untz856a11e2012-11-21 16:04:12 +01001146
1147# zypper wrapper to set arguments correctly
1148# zypper_install package [package ...]
1149function zypper_install() {
1150 [[ "$OFFLINE" = "True" ]] && return
1151 local sudo="sudo"
1152 [[ "$(id -u)" = "0" ]] && sudo="env"
1153 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1154 zypper --non-interactive install --auto-agree-with-licenses "$@"
1155}
1156
1157
1158# Add a user to a group.
1159# add_user_to_group user group
1160function add_user_to_group() {
1161 local user=$1
1162 local group=$2
1163
1164 if [[ -z "$os_VENDOR" ]]; then
1165 GetOSVersion
1166 fi
1167
1168 # SLE11 and openSUSE 12.2 don't have the usual usermod
1169 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1170 sudo usermod -a -G "$group" "$user"
1171 else
1172 sudo usermod -A "$group" "$user"
1173 fi
1174}
1175
1176
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001177# Get the path to the direcotry where python executables are installed.
1178# get_python_exec_prefix
1179function get_python_exec_prefix() {
1180 if is_fedora; then
1181 echo "/usr/bin"
1182 else
1183 echo "/usr/local/bin"
1184 fi
1185}
1186
Vincent Untz856a11e2012-11-21 16:04:12 +01001187# Get the location of the $module-rootwrap executables, where module is cinder
1188# or nova.
1189# get_rootwrap_location module
1190function get_rootwrap_location() {
1191 local module=$1
1192
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001193 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +01001194}
1195
Vincent Untz8ec27222012-11-29 09:25:31 +01001196# Get the path to the pip command.
1197# get_pip_command
1198function get_pip_command() {
Vincent Untz00011c02012-12-06 09:56:32 +01001199 if is_fedora; then
Nikhil Manchanda35138ed2013-01-03 17:49:58 -08001200 which pip-python
Vincent Untz00011c02012-12-06 09:56:32 +01001201 else
Nikhil Manchanda35138ed2013-01-03 17:49:58 -08001202 which pip
Vincent Untz8ec27222012-11-29 09:25:31 +01001203 fi
1204}
Vincent Untz856a11e2012-11-21 16:04:12 +01001205
Dean Troyer27e32692012-03-16 16:16:56 -05001206# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +00001207$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001208
1209
1210# Local variables:
1211# -*- mode: Shell-script -*-
Andrew Laskif900bd72012-09-05 17:23:14 -04001212# End: