blob: 9a59616d1a28bb0a63e22db0ef1eee0811ad9f95 [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=$?
Dean Troyer27e32692012-03-16 16:16:56 -050060 set +o xtrace
61 echo $@
62 exit $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -060063}
64
65
66# Checks an environment variable is not set or has length 0 OR if the
67# exit code is non-zero and prints "message" and exits
68# NOTE: env-var is the variable name without a '$'
69# die_if_not_set env-var "message"
70function die_if_not_set() {
Dean Troyer27e32692012-03-16 16:16:56 -050071 (
72 local exitcode=$?
73 set +o xtrace
74 local evar=$1; shift
75 if ! is_set $evar || [ $exitcode != 0 ]; then
Dean Troyer27e32692012-03-16 16:16:56 -050076 echo $@
77 exit -1
78 fi
79 )
Dean Troyer489bd2a2012-03-02 10:44:29 -060080}
81
82
Dean Troyer48352ee2012-12-12 12:50:38 -060083# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
84# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
85# ``localrc`` or on the command line if necessary::
86#
87# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
88#
89# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
90
91function export_proxy_variables() {
92 if [[ -n "$http_proxy" ]]; then
93 export http_proxy=$http_proxy
94 fi
95 if [[ -n "$https_proxy" ]]; then
96 export https_proxy=$https_proxy
97 fi
98 if [[ -n "$no_proxy" ]]; then
99 export no_proxy=$no_proxy
100 fi
101}
102
103
Dean Troyer489bd2a2012-03-02 10:44:29 -0600104# Grab a numbered field from python prettytable output
105# Fields are numbered starting with 1
106# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
107# get_field field-number
108function get_field() {
109 while read data; do
110 if [ "$1" -lt 0 ]; then
111 field="(\$(NF$1))"
112 else
113 field="\$$(($1 + 1))"
114 fi
115 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
116 done
117}
118
119
Isaku Yamahata8c438092013-02-12 22:30:56 +0900120function _get_package_dir() {
121 local pkg_dir
122 if is_ubuntu; then
123 pkg_dir=$FILES/apts
124 elif is_fedora; then
125 pkg_dir=$FILES/rpms
126 elif is_suse; then
127 pkg_dir=$FILES/rpms-suse
128 else
129 exit_distro_not_supported "list of packages"
130 fi
131 echo "$pkg_dir"
132}
133
Dean Troyer7e270512012-06-14 15:23:24 -0500134# get_packages() collects a list of package names of any type from the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500135# prerequisite files in ``files/{apts|rpms}``. The list is intended
136# to be passed to a package installer such as apt or yum.
Dean Troyer7e270512012-06-14 15:23:24 -0500137#
Isaku Yamahata8c438092013-02-12 22:30:56 +0900138# Only packages required for the services in 1st argument will be
Dean Troyer7e270512012-06-14 15:23:24 -0500139# included. Two bits of metadata are recognized in the prerequisite files:
140# - ``# NOPRIME`` defers installation to be performed later in stack.sh
141# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
142# of the package to the distros listed. The distro names are case insensitive.
Dean Troyer7e270512012-06-14 15:23:24 -0500143function get_packages() {
Isaku Yamahata8c438092013-02-12 22:30:56 +0900144 local services=$1
145 local package_dir=$(_get_package_dir)
Dean Troyer7e270512012-06-14 15:23:24 -0500146 local file_to_parse
147 local service
148
149 if [[ -z "$package_dir" ]]; then
150 echo "No package directory supplied"
151 return 1
152 fi
153 if [[ -z "$DISTRO" ]]; then
Vincent Untz855c5872012-10-04 13:36:46 +0200154 GetDistro
Dean Troyer7e270512012-06-14 15:23:24 -0500155 fi
Isaku Yamahata8c438092013-02-12 22:30:56 +0900156 for service in general ${services//,/ }; do
Dean Troyer7e270512012-06-14 15:23:24 -0500157 # Allow individual services to specify dependencies
158 if [[ -e ${package_dir}/${service} ]]; then
159 file_to_parse="${file_to_parse} $service"
160 fi
161 # NOTE(sdague) n-api needs glance for now because that's where
162 # glance client is
163 if [[ $service == n-api ]]; then
164 if [[ ! $file_to_parse =~ nova ]]; then
165 file_to_parse="${file_to_parse} nova"
166 fi
167 if [[ ! $file_to_parse =~ glance ]]; then
168 file_to_parse="${file_to_parse} glance"
169 fi
170 elif [[ $service == c-* ]]; then
171 if [[ ! $file_to_parse =~ cinder ]]; then
172 file_to_parse="${file_to_parse} cinder"
173 fi
John H. Tran93361642012-07-26 11:22:05 -0700174 elif [[ $service == ceilometer-* ]]; then
175 if [[ ! $file_to_parse =~ ceilometer ]]; then
176 file_to_parse="${file_to_parse} ceilometer"
177 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500178 elif [[ $service == n-* ]]; then
179 if [[ ! $file_to_parse =~ nova ]]; then
180 file_to_parse="${file_to_parse} nova"
181 fi
182 elif [[ $service == g-* ]]; then
183 if [[ ! $file_to_parse =~ glance ]]; then
184 file_to_parse="${file_to_parse} glance"
185 fi
186 elif [[ $service == key* ]]; then
187 if [[ ! $file_to_parse =~ keystone ]]; then
188 file_to_parse="${file_to_parse} keystone"
189 fi
Robert Collins0a9954f2012-11-20 11:34:25 +1300190 elif [[ $service == q-* ]]; then
191 if [[ ! $file_to_parse =~ quantum ]]; then
192 file_to_parse="${file_to_parse} quantum"
193 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500194 fi
195 done
196
197 for file in ${file_to_parse}; do
198 local fname=${package_dir}/${file}
199 local OIFS line package distros distro
200 [[ -e $fname ]] || continue
201
202 OIFS=$IFS
203 IFS=$'\n'
204 for line in $(<${fname}); do
205 if [[ $line =~ "NOPRIME" ]]; then
206 continue
207 fi
208
209 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
210 # We are using BASH regexp matching feature.
211 package=${BASH_REMATCH[1]}
212 distros=${BASH_REMATCH[2]}
213 # In bash ${VAR,,} will lowecase VAR
214 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
215 continue
216 fi
217
218 echo ${line%#*}
219 done
220 IFS=$OIFS
221 done
222}
223
224
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500225# Determine OS Vendor, Release and Update
226# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
227# Returns results in global variables:
228# os_VENDOR - vendor name
229# os_RELEASE - release
230# os_UPDATE - update
231# os_PACKAGE - package type
232# os_CODENAME - vendor's codename for release
233# GetOSVersion
234GetOSVersion() {
235 # Figure out which vendor we are
236 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
237 # OS/X
238 os_VENDOR=`sw_vers -productName`
239 os_RELEASE=`sw_vers -productVersion`
240 os_UPDATE=${os_RELEASE##*.}
241 os_RELEASE=${os_RELEASE%.*}
242 os_PACKAGE=""
243 if [[ "$os_RELEASE" =~ "10.7" ]]; then
244 os_CODENAME="lion"
245 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
246 os_CODENAME="snow leopard"
247 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
248 os_CODENAME="leopard"
249 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
250 os_CODENAME="tiger"
251 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
252 os_CODENAME="panther"
253 else
254 os_CODENAME=""
255 fi
256 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
257 os_VENDOR=$(lsb_release -i -s)
258 os_RELEASE=$(lsb_release -r -s)
259 os_UPDATE=""
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100260 os_PACKAGE="rpm"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500261 if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
262 os_PACKAGE="deb"
Vincent Untz856a11e2012-11-21 16:04:12 +0100263 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
264 lsb_release -d -s | grep -q openSUSE
265 if [[ $? -eq 0 ]]; then
266 os_VENDOR="openSUSE"
267 fi
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100268 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
269 os_VENDOR="Red Hat"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500270 fi
271 os_CODENAME=$(lsb_release -c -s)
272 elif [[ -r /etc/redhat-release ]]; then
273 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
274 # CentOS release 5.5 (Final)
275 # CentOS Linux release 6.0 (Final)
276 # Fedora release 16 (Verne)
277 os_CODENAME=""
278 for r in "Red Hat" CentOS Fedora; do
279 os_VENDOR=$r
280 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
281 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
282 os_CODENAME=${ver#*|}
283 os_RELEASE=${ver%|*}
284 os_UPDATE=${os_RELEASE##*.}
285 os_RELEASE=${os_RELEASE%.*}
286 break
287 fi
288 os_VENDOR=""
289 done
290 os_PACKAGE="rpm"
Vincent Untz856a11e2012-11-21 16:04:12 +0100291 elif [[ -r /etc/SuSE-release ]]; then
292 for r in openSUSE "SUSE Linux"; do
293 if [[ "$r" = "SUSE Linux" ]]; then
294 os_VENDOR="SUSE LINUX"
295 else
296 os_VENDOR=$r
297 fi
298
299 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
300 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
301 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
302 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
303 break
304 fi
305 os_VENDOR=""
306 done
307 os_PACKAGE="rpm"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500308 fi
309 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
310}
311
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300312# git update using reference as a branch.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500313# git_update_branch ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300314function git_update_branch() {
315
316 GIT_BRANCH=$1
317
318 git checkout -f origin/$GIT_BRANCH
319 # a local branch might not exist
320 git branch -D $GIT_BRANCH || true
321 git checkout -b $GIT_BRANCH
322}
323
324
325# git update using reference as a tag. Be careful editing source at that repo
326# as working copy will be in a detached mode
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500327# git_update_tag ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300328function git_update_tag() {
329
330 GIT_TAG=$1
331
332 git tag -d $GIT_TAG
333 # fetching given tag only
334 git fetch origin tag $GIT_TAG
335 git checkout -f $GIT_TAG
336}
337
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500338
Andrew Laskif900bd72012-09-05 17:23:14 -0400339# git update using reference as a branch.
340# git_update_remote_branch ref
341function git_update_remote_branch() {
342
343 GIT_BRANCH=$1
344
345 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
346}
347
348
Dean Troyera9e0a482012-07-09 14:07:23 -0500349# Translate the OS version values into common nomenclature
350# Sets ``DISTRO`` from the ``os_*`` values
351function GetDistro() {
352 GetOSVersion
353 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
354 # 'Everyone' refers to Ubuntu releases by the code name adjective
355 DISTRO=$os_CODENAME
356 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
357 # For Fedora, just use 'f' and the release
358 DISTRO="f$os_RELEASE"
Vincent Untz856a11e2012-11-21 16:04:12 +0100359 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
360 DISTRO="opensuse-$os_RELEASE"
361 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
362 # For SLE, also use the service pack
363 if [[ -z "$os_UPDATE" ]]; then
364 DISTRO="sle${os_RELEASE}"
365 else
366 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
367 fi
Dean Troyera9e0a482012-07-09 14:07:23 -0500368 else
369 # Catch-all for now is Vendor + Release + Update
370 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
371 fi
372 export DISTRO
373}
374
375
Vincent Untzc18b9652012-12-04 12:36:34 +0100376# Determine if current distribution is an Ubuntu-based distribution.
377# It will also detect non-Ubuntu but Debian-based distros; this is not an issue
378# since Debian and Ubuntu should be compatible.
379# is_ubuntu
380function is_ubuntu {
381 if [[ -z "$os_PACKAGE" ]]; then
382 GetOSVersion
383 fi
384
385 [ "$os_PACKAGE" = "deb" ]
386}
387
388
Vincent Untz00011c02012-12-06 09:56:32 +0100389# Determine if current distribution is a Fedora-based distribution
390# (Fedora, RHEL, CentOS).
391# is_fedora
392function is_fedora {
393 if [[ -z "$os_VENDOR" ]]; then
394 GetOSVersion
395 fi
396
397 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ]
398}
399
400
Vincent Untz856a11e2012-11-21 16:04:12 +0100401# Determine if current distribution is a SUSE-based distribution
402# (openSUSE, SLE).
403# is_suse
404function is_suse {
405 if [[ -z "$os_VENDOR" ]]; then
406 GetOSVersion
407 fi
408
Steve Baker1a7bbd22012-12-03 17:04:02 +1300409 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
Vincent Untz856a11e2012-11-21 16:04:12 +0100410}
411
412
Vincent Untz00011c02012-12-06 09:56:32 +0100413# Exit after outputting a message about the distribution not being supported.
414# exit_distro_not_supported [optional-string-telling-what-is-missing]
415function exit_distro_not_supported {
416 if [[ -z "$DISTRO" ]]; then
417 GetDistro
418 fi
419
420 if [ $# -gt 0 ]; then
421 echo "Support for $DISTRO is incomplete: no support for $@"
422 else
423 echo "Support for $DISTRO is incomplete."
424 fi
425
426 exit 1
427}
428
429
Dean Troyer7f9aa712012-01-31 12:11:56 -0600430# git clone only if directory doesn't exist already. Since ``DEST`` might not
431# be owned by the installation user, we create the directory and change the
432# ownership to the proper user.
433# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700434# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
435# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500436# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600437# git_clone remote dest-dir branch
438function git_clone {
439 [[ "$OFFLINE" = "True" ]] && return
440
441 GIT_REMOTE=$1
442 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300443 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600444
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300445 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600446 # If our branch name is a gerrit style refs/changes/...
447 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700448 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600449 git clone $GIT_REMOTE $GIT_DEST
450 fi
451 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300452 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600453 else
454 # do a full clone only if the directory doesn't exist
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 cd $GIT_DEST
459 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300460 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600461 elif [[ "$RECLONE" == "yes" ]]; then
462 # if it does exist then simulate what clone does if asked to RECLONE
463 cd $GIT_DEST
464 # set the url to pull from and fetch
465 git remote set-url origin $GIT_REMOTE
466 git fetch origin
467 # remove the existing ignored files (like pyc) as they cause breakage
468 # (due to the py files having older timestamps than our pyc, so python
469 # thinks the pyc files are correct using them)
470 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300471
472 # handle GIT_REF accordingly to type (tag, branch)
473 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
474 git_update_tag $GIT_REF
475 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
476 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400477 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
478 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300479 else
480 echo $GIT_REF is neither branch nor tag
481 exit 1
482 fi
483
Dean Troyer7f9aa712012-01-31 12:11:56 -0600484 fi
485 fi
486}
487
488
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500489# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200490# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500491function inicomment() {
492 local file=$1
493 local section=$2
494 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100495 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500496}
497
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200498# Uncomment an option in an INI file
499# iniuncomment config-file section option
500function iniuncomment() {
501 local file=$1
502 local section=$2
503 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100504 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200505}
506
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500507
508# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500509# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500510function iniget() {
511 local file=$1
512 local section=$2
513 local option=$3
514 local line
Attila Fazekas588eb412012-12-20 10:57:16 +0100515 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500516 echo ${line#*=}
517}
518
Attila Fazekas588eb412012-12-20 10:57:16 +0100519# Determinate is the given option present in the INI file
520# ini_has_option config-file section option
521function ini_has_option() {
522 local file=$1
523 local section=$2
524 local option=$3
525 local line
526 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
527 [ -n "$line" ]
528}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500529
530# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500531# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500532function iniset() {
533 local file=$1
534 local section=$2
535 local option=$3
536 local value=$4
Attila Fazekas588eb412012-12-20 10:57:16 +0100537 if ! grep -q "^\[$section\]" "$file"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500538 # Add section at the end
Attila Fazekas588eb412012-12-20 10:57:16 +0100539 echo -e "\n[$section]" >>"$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500540 fi
Attila Fazekas588eb412012-12-20 10:57:16 +0100541 if ! ini_has_option "$file" "$section" "$option"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500542 # Add it
Attila Fazekas588eb412012-12-20 10:57:16 +0100543 sed -i -e "/^\[$section\]/ a\\
Dean Troyer09e636e2012-03-19 16:31:12 -0500544$option = $value
Attila Fazekas588eb412012-12-20 10:57:16 +0100545" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500546 else
547 # Replace it
Attila Fazekas588eb412012-12-20 10:57:16 +0100548 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500549 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500550}
551
552
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000553# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500554# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000555#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500556# Multiple services specified as arguments are ``OR``'ed together; the test
557# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000558#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500559# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000560# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500561# **cinder** returns true if any service enabled start with **c-**
562# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000563# **glance** returns true if any service enabled start with **g-**
564# **quantum** returns true if any service enabled start with **q-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500565#
566# Uses global ``ENABLED_SERVICES``
567# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000568function is_service_enabled() {
569 services=$@
570 for service in ${services}; do
571 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
572 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500573 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700574 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000575 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
576 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
577 done
578 return 1
579}
580
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500581
582# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
583# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400584function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500585 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400586 s/,,/,/g;
587 s/^,//;
588 s/,$//
589 '
590}
591
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500592
Doug Hellmannf04178f2012-07-05 17:10:03 -0400593# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500594# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400595#
596# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800597# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400598#
599# This function does not know about the special cases
600# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500601# Uses global ``ENABLED_SERVICES``
602# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400603function enable_service() {
604 local tmpsvcs="${ENABLED_SERVICES}"
605 for service in $@; do
606 if ! is_service_enabled $service; then
607 tmpsvcs+=",$service"
608 fi
609 done
610 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
611 disable_negated_services
612}
613
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500614
Doug Hellmannf04178f2012-07-05 17:10:03 -0400615# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500616# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400617#
618# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800619# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400620#
621# This function does not know about the special cases
622# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500623# Uses global ``ENABLED_SERVICES``
624# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400625function disable_service() {
626 local tmpsvcs=",${ENABLED_SERVICES},"
627 local service
628 for service in $@; do
629 if is_service_enabled $service; then
630 tmpsvcs=${tmpsvcs//,$service,/,}
631 fi
632 done
633 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
634}
635
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500636
Doug Hellmannf04178f2012-07-05 17:10:03 -0400637# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500638# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400639# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500640# Uses global ``ENABLED_SERVICES``
641# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400642function disable_all_services() {
643 ENABLED_SERVICES=""
644}
645
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500646
647# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800648# services except rabbit (rabbit) set in ``localrc``:
649# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500650# Uses global ``ENABLED_SERVICES``
651# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400652function disable_negated_services() {
653 local tmpsvcs="${ENABLED_SERVICES}"
654 local service
655 for service in ${tmpsvcs//,/ }; do
656 if [[ ${service} == -* ]]; then
657 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
658 fi
659 done
660 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
661}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600662
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500663
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500664# Distro-agnostic package installer
665# install_package package [package ...]
666function install_package() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100667 if is_ubuntu; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200668 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
669 NO_UPDATE_REPOS=True
670
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500671 apt_get install "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100672 elif is_fedora; then
673 yum_install "$@"
674 elif is_suse; then
675 zypper_install "$@"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500676 else
Vincent Untz00011c02012-12-06 09:56:32 +0100677 exit_distro_not_supported "installing packages"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500678 fi
679}
680
681
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200682# Distro-agnostic function to tell if a package is installed
683# is_package_installed package [package ...]
684function is_package_installed() {
685 if [[ -z "$@" ]]; then
686 return 1
687 fi
688
689 if [[ -z "$os_PACKAGE" ]]; then
690 GetOSVersion
691 fi
Vincent Untzc18b9652012-12-04 12:36:34 +0100692
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200693 if [[ "$os_PACKAGE" = "deb" ]]; then
694 dpkg -l "$@" > /dev/null
Vincent Untz00011c02012-12-06 09:56:32 +0100695 elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200696 rpm --quiet -q "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100697 else
698 exit_distro_not_supported "finding if a package is installed"
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200699 fi
700}
701
702
Dean Troyer489bd2a2012-03-02 10:44:29 -0600703# Test if the named environment variable is set and not zero length
704# is_set env-var
705function is_set() {
706 local var=\$"$1"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100707 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 -0600708}
709
710
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500711# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +0000712# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
713# ``TRACK_DEPENDS``, ``*_proxy`
Dean Troyer7f9aa712012-01-31 12:11:56 -0600714# pip_install package [package ...]
715function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600716 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500717 if [[ -z "$os_PACKAGE" ]]; then
718 GetOSVersion
719 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500720 if [[ $TRACK_DEPENDS = True ]] ; then
721 source $DEST/.venv/bin/activate
722 CMD_PIP=$DEST/.venv/bin/pip
723 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500724 else
Monty Taylor47f02062012-07-26 11:09:24 -0500725 SUDO_PIP="sudo"
Vincent Untz8ec27222012-11-29 09:25:31 +0100726 CMD_PIP=$(get_pip_command)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500727 fi
Maru Newby3a87edd2012-10-25 23:01:06 +0000728 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
729 PIP_MIRROR_OPT="--use-mirrors"
730 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500731 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600732 HTTP_PROXY=$http_proxy \
733 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900734 NO_PROXY=$no_proxy \
Maru Newby3a87edd2012-10-25 23:01:06 +0000735 $CMD_PIP install $PIP_MIRROR_OPT $@
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500736}
737
738
739# Service wrapper to restart services
740# restart_service service-name
741function restart_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100742 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600743 sudo /usr/sbin/service $1 restart
744 else
745 sudo /sbin/service $1 restart
746 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500747}
748
749
Dean Troyer15733352012-09-06 11:51:30 -0500750# Helper to launch a service in a named screen
751# screen_it service "command-line"
752function screen_it {
Dean Troyer15733352012-09-06 11:51:30 -0500753 SCREEN_NAME=${SCREEN_NAME:-stack}
jiajun xua9414242012-12-06 16:30:57 +0800754 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Vishvananda Ishaya58e21342013-02-11 16:48:12 -0800755 SCREEN_DEV=`trueorfalse True $SCREEN_DEV`
jiajun xua9414242012-12-06 16:30:57 +0800756
Dean Troyer15733352012-09-06 11:51:30 -0500757 if is_service_enabled $1; then
758 # Append the service to the screen rc file
759 screen_rc "$1" "$2"
760
761 screen -S $SCREEN_NAME -X screen -t $1
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +0000762
763 if [[ -n ${SCREEN_LOGDIR} ]]; then
764 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
765 screen -S $SCREEN_NAME -p $1 -X log on
766 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
767 fi
768
Vishvananda Ishaya58e21342013-02-11 16:48:12 -0800769 if [[ "$SCREEN_DEV" = "True" ]]; then
770 # sleep to allow bash to be ready to be send the command - we are
771 # creating a new window in screen and then sends characters, so if
772 # bash isn't running by the time we send the command, nothing happens
773 sleep 1.5
Dean Troyer15733352012-09-06 11:51:30 -0500774
Vishvananda Ishaya58e21342013-02-11 16:48:12 -0800775 NL=`echo -ne '\015'`
776 screen -S $SCREEN_NAME -p $1 -X stuff "$2 || touch \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
777 else
778 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 -0500779 fi
Dean Troyer15733352012-09-06 11:51:30 -0500780 fi
781}
782
783
784# Screen rc file builder
785# screen_rc service "command-line"
786function screen_rc {
787 SCREEN_NAME=${SCREEN_NAME:-stack}
788 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
789 if [[ ! -e $SCREENRC ]]; then
790 # Name the screen session
791 echo "sessionname $SCREEN_NAME" > $SCREENRC
792 # Set a reasonable statusbar
793 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
794 echo "screen -t shell bash" >> $SCREENRC
795 fi
796 # If this service doesn't already exist in the screenrc file
797 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
798 NL=`echo -ne '\015'`
799 echo "screen -t $1 bash" >> $SCREENRC
800 echo "stuff \"$2$NL\"" >> $SCREENRC
801 fi
802}
803
jiajun xua9414242012-12-06 16:30:57 +0800804# Helper to remove the *.failure files under $SERVICE_DIR/$SCREEN_NAME
805# This is used for service_check when all the screen_it are called finished
806# init_service_check
807function init_service_check() {
808 SCREEN_NAME=${SCREEN_NAME:-stack}
809 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
810
811 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
812 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
813 fi
814
815 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
816}
817
818# Helper to get the status of each running service
819# service_check
820function service_check() {
821 local service
822 local failures
823 SCREEN_NAME=${SCREEN_NAME:-stack}
824 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
825
826
827 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
828 echo "No service status directory found"
829 return
830 fi
831
832 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
833 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null`
834
835 for service in $failures; do
836 service=`basename $service`
837 service=${service::-8}
838 echo "Error: Service $service is not running"
839 done
840
841 if [ -n "$failures" ]; then
842 echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
843 fi
844}
Dean Troyer15733352012-09-06 11:51:30 -0500845
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500846# ``pip install`` the dependencies of the package before ``setup.py develop``
847# so pip and not distutils processes the dependency chain
848# Uses globals ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500849# setup_develop directory
850function setup_develop() {
Monty Taylor47f02062012-07-26 11:09:24 -0500851 if [[ $TRACK_DEPENDS = True ]] ; then
852 SUDO_CMD="env"
853 else
854 SUDO_CMD="sudo"
855 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500856 (cd $1; \
857 python setup.py egg_info; \
858 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
859 depend_links=$(echo $raw_links | xargs); \
Dean Troyer1a3c9fe2012-09-29 17:25:02 -0500860 require_file=$([ ! -r *-info/requires.txt ] || echo "-r *-info/requires.txt"); \
861 pip_install $require_file $depend_links; \
Monty Taylor47f02062012-07-26 11:09:24 -0500862 $SUDO_CMD \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500863 HTTP_PROXY=$http_proxy \
864 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900865 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500866 python setup.py develop \
867 )
868}
869
870
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500871# Service wrapper to start services
872# start_service service-name
873function start_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100874 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600875 sudo /usr/sbin/service $1 start
876 else
877 sudo /sbin/service $1 start
878 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500879}
880
881
882# Service wrapper to stop services
883# stop_service service-name
884function stop_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100885 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600886 sudo /usr/sbin/service $1 stop
887 else
888 sudo /sbin/service $1 stop
889 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600890}
891
892
893# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500894# Accepts as False: 0 no false False FALSE
895# Accepts as True: 1 yes true True TRUE
896# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -0600897function trueorfalse() {
898 local default=$1
899 local testval=$2
900
901 [[ -z "$testval" ]] && { echo "$default"; return; }
902 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
903 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
904 echo "$default"
905}
Dean Troyer27e32692012-03-16 16:16:56 -0500906
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500907
Dean Troyerca0e3d02012-04-13 15:58:37 -0500908# Retrieve an image from a URL and upload into Glance
909# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500910# ``FILES`` must be set to the cache dir
911# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -0500912# upload_image image-url glance-token
913function upload_image() {
914 local image_url=$1
915 local token=$2
916
917 # Create a directory for the downloaded image tarballs.
918 mkdir -p $FILES/images
919
920 # Downloads the image (uec ami+aki style), then extracts it.
921 IMAGE_FNAME=`basename "$image_url"`
922 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
923 wget -c $image_url -O $FILES/$IMAGE_FNAME
924 if [[ $? -ne 0 ]]; then
925 echo "Not found: $image_url"
926 return
927 fi
928 fi
929
930 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
931 if [[ "$image_url" =~ 'openvz' ]]; then
932 IMAGE="$FILES/${IMAGE_FNAME}"
933 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
934 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}"
935 return
936 fi
937
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500938 # XenServer-ovf-format images are provided as .vhd.tgz as well
939 # and should not be decompressed prior to loading
940 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
941 IMAGE="$FILES/${IMAGE_FNAME}"
942 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
943 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}"
944 return
945 fi
946
Dean Troyerca0e3d02012-04-13 15:58:37 -0500947 KERNEL=""
948 RAMDISK=""
949 DISK_FORMAT=""
950 CONTAINER_FORMAT=""
951 UNPACK=""
952 case "$IMAGE_FNAME" in
953 *.tar.gz|*.tgz)
954 # Extract ami and aki files
955 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
956 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
957 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
958 xdir="$FILES/images/$IMAGE_NAME"
959 rm -Rf "$xdir";
960 mkdir "$xdir"
961 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
962 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
963 [ -f "$f" ] && echo "$f" && break; done; true)
964 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
965 [ -f "$f" ] && echo "$f" && break; done; true)
966 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
967 [ -f "$f" ] && echo "$f" && break; done; true)
968 if [[ -z "$IMAGE_NAME" ]]; then
969 IMAGE_NAME=$(basename "$IMAGE" ".img")
970 fi
971 ;;
972 *.img)
973 IMAGE="$FILES/$IMAGE_FNAME";
974 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -0500975 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
976 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
977 DISK_FORMAT=$format
978 else
979 DISK_FORMAT=raw
980 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -0500981 CONTAINER_FORMAT=bare
982 ;;
983 *.img.gz)
984 IMAGE="$FILES/${IMAGE_FNAME}"
985 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
986 DISK_FORMAT=raw
987 CONTAINER_FORMAT=bare
988 UNPACK=zcat
989 ;;
990 *.qcow2)
991 IMAGE="$FILES/${IMAGE_FNAME}"
992 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
993 DISK_FORMAT=qcow2
994 CONTAINER_FORMAT=bare
995 ;;
996 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
997 esac
998
999 if [ "$CONTAINER_FORMAT" = "bare" ]; then
1000 if [ "$UNPACK" = "zcat" ]; then
1001 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}")
1002 else
1003 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}"
1004 fi
1005 else
1006 # Use glance client to add the kernel the root filesystem.
1007 # We parse the results of the first upload to get the glance ID of the
1008 # kernel for use when uploading the root filesystem.
1009 KERNEL_ID=""; RAMDISK_ID="";
1010 if [ -n "$KERNEL" ]; then
1011 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)
1012 fi
1013 if [ -n "$RAMDISK" ]; then
1014 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)
1015 fi
1016 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}"
1017 fi
1018}
1019
Dean Troyerc1b486a2012-11-05 14:26:09 -06001020# Set the database backend to use
1021# When called from stackrc/localrc DATABASE_BACKENDS has not been
1022# initialized yet, just save the configuration selection and call back later
1023# to validate it.
1024# $1 The name of the database backend to use (mysql, postgresql, ...)
1025function use_database {
1026 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -06001027 # No backends registered means this is likely called from ``localrc``
1028 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -06001029 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +00001030 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 +01001031 else
Dean Troyerafc29fe2013-02-07 15:56:24 -06001032 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +01001033 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -06001034 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -06001035}
1036
Terry Wilson428af5a2012-11-01 16:12:39 -04001037# Toggle enable/disable_service for services that must run exclusive of each other
1038# $1 The name of a variable containing a space-separated list of services
1039# $2 The name of a variable in which to store the enabled service's name
1040# $3 The name of the service to enable
1041function use_exclusive_service {
1042 local options=${!1}
1043 local selection=$3
1044 out=$2
1045 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1046 for opt in $options;do
1047 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1048 done
1049 eval "$out=$selection"
1050 return 0
1051}
Dean Troyerca0e3d02012-04-13 15:58:37 -05001052
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001053# Wait for an HTTP server to start answering requests
1054# wait_for_service timeout url
1055function wait_for_service() {
1056 local timeout=$1
1057 local url=$2
1058 timeout $timeout sh -c "while ! http_proxy= https_proxy= curl -s $url >/dev/null; do sleep 1; done"
1059}
1060
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001061# Wrapper for ``yum`` to set proxy environment variables
1062# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001063# yum_install package [package ...]
1064function yum_install() {
1065 [[ "$OFFLINE" = "True" ]] && return
1066 local sudo="sudo"
1067 [[ "$(id -u)" = "0" ]] && sudo="env"
1068 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001069 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001070 yum install -y "$@"
1071}
1072
Nachi Uenofda946e2012-10-24 17:26:02 -07001073# ping check
1074# Uses globals ``ENABLED_SERVICES``
1075function ping_check() {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001076 if is_service_enabled quantum; then
1077 _ping_check_quantum "$1" $2 $3 $4
1078 return
1079 fi
1080 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -07001081}
1082
1083# ping check for nova
1084# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
1085function _ping_check_novanet() {
1086 local from_net=$1
1087 local ip=$2
1088 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001089 local expected=${4:-"True"}
1090 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001091 MULTI_HOST=`trueorfalse False $MULTI_HOST`
1092 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
1093 sleep $boot_timeout
1094 return
1095 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001096 if [[ "$expected" = "True" ]]; then
1097 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
1098 else
1099 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
1100 fi
1101 if ! timeout $boot_timeout sh -c "$check_command"; then
1102 if [[ "$expected" = "True" ]]; then
1103 echo "[Fail] Couldn't ping server"
1104 else
1105 echo "[Fail] Could ping server"
1106 fi
Nachi Uenofda946e2012-10-24 17:26:02 -07001107 exit 1
1108 fi
1109}
1110
1111# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001112
Nachi Uenofda946e2012-10-24 17:26:02 -07001113function ssh_check() {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001114 if is_service_enabled quantum; then
1115 _ssh_check_quantum "$1" $2 $3 $4 $5
1116 return
1117 fi
1118 _ssh_check_novanet "$1" $2 $3 $4 $5
1119}
1120
1121function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -07001122 local NET_NAME=$1
1123 local KEY_FILE=$2
1124 local FLOATING_IP=$3
1125 local DEFAULT_INSTANCE_USER=$4
1126 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -06001127 local probe_cmd=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001128 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
1129 echo "server didn't become ssh-able!"
1130 exit 1
1131 fi
1132}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001133
Vincent Untz856a11e2012-11-21 16:04:12 +01001134
1135# zypper wrapper to set arguments correctly
1136# zypper_install package [package ...]
1137function zypper_install() {
1138 [[ "$OFFLINE" = "True" ]] && return
1139 local sudo="sudo"
1140 [[ "$(id -u)" = "0" ]] && sudo="env"
1141 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1142 zypper --non-interactive install --auto-agree-with-licenses "$@"
1143}
1144
1145
1146# Add a user to a group.
1147# add_user_to_group user group
1148function add_user_to_group() {
1149 local user=$1
1150 local group=$2
1151
1152 if [[ -z "$os_VENDOR" ]]; then
1153 GetOSVersion
1154 fi
1155
1156 # SLE11 and openSUSE 12.2 don't have the usual usermod
1157 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1158 sudo usermod -a -G "$group" "$user"
1159 else
1160 sudo usermod -A "$group" "$user"
1161 fi
1162}
1163
1164
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001165# Get the path to the direcotry where python executables are installed.
1166# get_python_exec_prefix
1167function get_python_exec_prefix() {
1168 if is_fedora; then
1169 echo "/usr/bin"
1170 else
1171 echo "/usr/local/bin"
1172 fi
1173}
1174
Vincent Untz856a11e2012-11-21 16:04:12 +01001175# Get the location of the $module-rootwrap executables, where module is cinder
1176# or nova.
1177# get_rootwrap_location module
1178function get_rootwrap_location() {
1179 local module=$1
1180
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001181 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +01001182}
1183
Vincent Untz8ec27222012-11-29 09:25:31 +01001184# Get the path to the pip command.
1185# get_pip_command
1186function get_pip_command() {
Vincent Untz00011c02012-12-06 09:56:32 +01001187 if is_fedora; then
Nikhil Manchanda35138ed2013-01-03 17:49:58 -08001188 which pip-python
Vincent Untz00011c02012-12-06 09:56:32 +01001189 else
Nikhil Manchanda35138ed2013-01-03 17:49:58 -08001190 which pip
Vincent Untz8ec27222012-11-29 09:25:31 +01001191 fi
1192}
Vincent Untz856a11e2012-11-21 16:04:12 +01001193
Dean Troyer27e32692012-03-16 16:16:56 -05001194# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +00001195$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001196
1197
1198# Local variables:
1199# -*- mode: Shell-script -*-
Andrew Laskif900bd72012-09-05 17:23:14 -04001200# End: