blob: 55a81c54287f9a670361ee5f3334f2dcef159bdc [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
83# Grab a numbered field from python prettytable output
84# Fields are numbered starting with 1
85# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
86# get_field field-number
87function get_field() {
88 while read data; do
89 if [ "$1" -lt 0 ]; then
90 field="(\$(NF$1))"
91 else
92 field="\$$(($1 + 1))"
93 fi
94 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
95 done
96}
97
98
Dean Troyer7e270512012-06-14 15:23:24 -050099# get_packages() collects a list of package names of any type from the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500100# prerequisite files in ``files/{apts|rpms}``. The list is intended
101# to be passed to a package installer such as apt or yum.
Dean Troyer7e270512012-06-14 15:23:24 -0500102#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500103# Only packages required for the services in ``ENABLED_SERVICES`` will be
Dean Troyer7e270512012-06-14 15:23:24 -0500104# included. Two bits of metadata are recognized in the prerequisite files:
105# - ``# NOPRIME`` defers installation to be performed later in stack.sh
106# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
107# of the package to the distros listed. The distro names are case insensitive.
108#
Vincent Untz855c5872012-10-04 13:36:46 +0200109# Uses globals ``ENABLED_SERVICES``
Dean Troyer7e270512012-06-14 15:23:24 -0500110# get_packages dir
111function get_packages() {
112 local package_dir=$1
113 local file_to_parse
114 local service
115
116 if [[ -z "$package_dir" ]]; then
117 echo "No package directory supplied"
118 return 1
119 fi
120 if [[ -z "$DISTRO" ]]; then
Vincent Untz855c5872012-10-04 13:36:46 +0200121 GetDistro
Dean Troyer7e270512012-06-14 15:23:24 -0500122 fi
123 for service in general ${ENABLED_SERVICES//,/ }; do
124 # Allow individual services to specify dependencies
125 if [[ -e ${package_dir}/${service} ]]; then
126 file_to_parse="${file_to_parse} $service"
127 fi
128 # NOTE(sdague) n-api needs glance for now because that's where
129 # glance client is
130 if [[ $service == n-api ]]; then
131 if [[ ! $file_to_parse =~ nova ]]; then
132 file_to_parse="${file_to_parse} nova"
133 fi
134 if [[ ! $file_to_parse =~ glance ]]; then
135 file_to_parse="${file_to_parse} glance"
136 fi
137 elif [[ $service == c-* ]]; then
138 if [[ ! $file_to_parse =~ cinder ]]; then
139 file_to_parse="${file_to_parse} cinder"
140 fi
John H. Tran93361642012-07-26 11:22:05 -0700141 elif [[ $service == ceilometer-* ]]; then
142 if [[ ! $file_to_parse =~ ceilometer ]]; then
143 file_to_parse="${file_to_parse} ceilometer"
144 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500145 elif [[ $service == n-* ]]; then
146 if [[ ! $file_to_parse =~ nova ]]; then
147 file_to_parse="${file_to_parse} nova"
148 fi
149 elif [[ $service == g-* ]]; then
150 if [[ ! $file_to_parse =~ glance ]]; then
151 file_to_parse="${file_to_parse} glance"
152 fi
153 elif [[ $service == key* ]]; then
154 if [[ ! $file_to_parse =~ keystone ]]; then
155 file_to_parse="${file_to_parse} keystone"
156 fi
Robert Collins0a9954f2012-11-20 11:34:25 +1300157 elif [[ $service == q-* ]]; then
158 if [[ ! $file_to_parse =~ quantum ]]; then
159 file_to_parse="${file_to_parse} quantum"
160 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500161 fi
162 done
163
164 for file in ${file_to_parse}; do
165 local fname=${package_dir}/${file}
166 local OIFS line package distros distro
167 [[ -e $fname ]] || continue
168
169 OIFS=$IFS
170 IFS=$'\n'
171 for line in $(<${fname}); do
172 if [[ $line =~ "NOPRIME" ]]; then
173 continue
174 fi
175
176 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
177 # We are using BASH regexp matching feature.
178 package=${BASH_REMATCH[1]}
179 distros=${BASH_REMATCH[2]}
180 # In bash ${VAR,,} will lowecase VAR
181 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
182 continue
183 fi
184
185 echo ${line%#*}
186 done
187 IFS=$OIFS
188 done
189}
190
191
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500192# Determine OS Vendor, Release and Update
193# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
194# Returns results in global variables:
195# os_VENDOR - vendor name
196# os_RELEASE - release
197# os_UPDATE - update
198# os_PACKAGE - package type
199# os_CODENAME - vendor's codename for release
200# GetOSVersion
201GetOSVersion() {
202 # Figure out which vendor we are
203 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
204 # OS/X
205 os_VENDOR=`sw_vers -productName`
206 os_RELEASE=`sw_vers -productVersion`
207 os_UPDATE=${os_RELEASE##*.}
208 os_RELEASE=${os_RELEASE%.*}
209 os_PACKAGE=""
210 if [[ "$os_RELEASE" =~ "10.7" ]]; then
211 os_CODENAME="lion"
212 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
213 os_CODENAME="snow leopard"
214 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
215 os_CODENAME="leopard"
216 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
217 os_CODENAME="tiger"
218 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
219 os_CODENAME="panther"
220 else
221 os_CODENAME=""
222 fi
223 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
224 os_VENDOR=$(lsb_release -i -s)
225 os_RELEASE=$(lsb_release -r -s)
226 os_UPDATE=""
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100227 os_PACKAGE="rpm"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500228 if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
229 os_PACKAGE="deb"
Vincent Untz856a11e2012-11-21 16:04:12 +0100230 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
231 lsb_release -d -s | grep -q openSUSE
232 if [[ $? -eq 0 ]]; then
233 os_VENDOR="openSUSE"
234 fi
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100235 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
236 os_VENDOR="Red Hat"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500237 fi
238 os_CODENAME=$(lsb_release -c -s)
239 elif [[ -r /etc/redhat-release ]]; then
240 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
241 # CentOS release 5.5 (Final)
242 # CentOS Linux release 6.0 (Final)
243 # Fedora release 16 (Verne)
244 os_CODENAME=""
245 for r in "Red Hat" CentOS Fedora; do
246 os_VENDOR=$r
247 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
248 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
249 os_CODENAME=${ver#*|}
250 os_RELEASE=${ver%|*}
251 os_UPDATE=${os_RELEASE##*.}
252 os_RELEASE=${os_RELEASE%.*}
253 break
254 fi
255 os_VENDOR=""
256 done
257 os_PACKAGE="rpm"
Vincent Untz856a11e2012-11-21 16:04:12 +0100258 elif [[ -r /etc/SuSE-release ]]; then
259 for r in openSUSE "SUSE Linux"; do
260 if [[ "$r" = "SUSE Linux" ]]; then
261 os_VENDOR="SUSE LINUX"
262 else
263 os_VENDOR=$r
264 fi
265
266 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
267 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
268 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
269 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
270 break
271 fi
272 os_VENDOR=""
273 done
274 os_PACKAGE="rpm"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500275 fi
276 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
277}
278
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300279# git update using reference as a branch.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500280# git_update_branch ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300281function git_update_branch() {
282
283 GIT_BRANCH=$1
284
285 git checkout -f origin/$GIT_BRANCH
286 # a local branch might not exist
287 git branch -D $GIT_BRANCH || true
288 git checkout -b $GIT_BRANCH
289}
290
291
292# git update using reference as a tag. Be careful editing source at that repo
293# as working copy will be in a detached mode
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500294# git_update_tag ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300295function git_update_tag() {
296
297 GIT_TAG=$1
298
299 git tag -d $GIT_TAG
300 # fetching given tag only
301 git fetch origin tag $GIT_TAG
302 git checkout -f $GIT_TAG
303}
304
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500305
Andrew Laskif900bd72012-09-05 17:23:14 -0400306# git update using reference as a branch.
307# git_update_remote_branch ref
308function git_update_remote_branch() {
309
310 GIT_BRANCH=$1
311
312 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
313}
314
315
Dean Troyera9e0a482012-07-09 14:07:23 -0500316# Translate the OS version values into common nomenclature
317# Sets ``DISTRO`` from the ``os_*`` values
318function GetDistro() {
319 GetOSVersion
320 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
321 # 'Everyone' refers to Ubuntu releases by the code name adjective
322 DISTRO=$os_CODENAME
323 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
324 # For Fedora, just use 'f' and the release
325 DISTRO="f$os_RELEASE"
Vincent Untz856a11e2012-11-21 16:04:12 +0100326 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
327 DISTRO="opensuse-$os_RELEASE"
328 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
329 # For SLE, also use the service pack
330 if [[ -z "$os_UPDATE" ]]; then
331 DISTRO="sle${os_RELEASE}"
332 else
333 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
334 fi
Dean Troyera9e0a482012-07-09 14:07:23 -0500335 else
336 # Catch-all for now is Vendor + Release + Update
337 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
338 fi
339 export DISTRO
340}
341
342
Vincent Untzc18b9652012-12-04 12:36:34 +0100343# Determine if current distribution is an Ubuntu-based distribution.
344# It will also detect non-Ubuntu but Debian-based distros; this is not an issue
345# since Debian and Ubuntu should be compatible.
346# is_ubuntu
347function is_ubuntu {
348 if [[ -z "$os_PACKAGE" ]]; then
349 GetOSVersion
350 fi
351
352 [ "$os_PACKAGE" = "deb" ]
353}
354
355
Vincent Untz00011c02012-12-06 09:56:32 +0100356# Determine if current distribution is a Fedora-based distribution
357# (Fedora, RHEL, CentOS).
358# is_fedora
359function is_fedora {
360 if [[ -z "$os_VENDOR" ]]; then
361 GetOSVersion
362 fi
363
364 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ]
365}
366
367
Vincent Untz856a11e2012-11-21 16:04:12 +0100368# Determine if current distribution is a SUSE-based distribution
369# (openSUSE, SLE).
370# is_suse
371function is_suse {
372 if [[ -z "$os_VENDOR" ]]; then
373 GetOSVersion
374 fi
375
Steve Baker1a7bbd22012-12-03 17:04:02 +1300376 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
Vincent Untz856a11e2012-11-21 16:04:12 +0100377}
378
379
Vincent Untz00011c02012-12-06 09:56:32 +0100380# Exit after outputting a message about the distribution not being supported.
381# exit_distro_not_supported [optional-string-telling-what-is-missing]
382function exit_distro_not_supported {
383 if [[ -z "$DISTRO" ]]; then
384 GetDistro
385 fi
386
387 if [ $# -gt 0 ]; then
388 echo "Support for $DISTRO is incomplete: no support for $@"
389 else
390 echo "Support for $DISTRO is incomplete."
391 fi
392
393 exit 1
394}
395
396
Dean Troyer7f9aa712012-01-31 12:11:56 -0600397# git clone only if directory doesn't exist already. Since ``DEST`` might not
398# be owned by the installation user, we create the directory and change the
399# ownership to the proper user.
400# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700401# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
402# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500403# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600404# git_clone remote dest-dir branch
405function git_clone {
406 [[ "$OFFLINE" = "True" ]] && return
407
408 GIT_REMOTE=$1
409 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300410 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600411
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300412 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600413 # If our branch name is a gerrit style refs/changes/...
414 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700415 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600416 git clone $GIT_REMOTE $GIT_DEST
417 fi
418 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300419 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600420 else
421 # do a full clone only if the directory doesn't exist
422 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700423 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600424 git clone $GIT_REMOTE $GIT_DEST
425 cd $GIT_DEST
426 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300427 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600428 elif [[ "$RECLONE" == "yes" ]]; then
429 # if it does exist then simulate what clone does if asked to RECLONE
430 cd $GIT_DEST
431 # set the url to pull from and fetch
432 git remote set-url origin $GIT_REMOTE
433 git fetch origin
434 # remove the existing ignored files (like pyc) as they cause breakage
435 # (due to the py files having older timestamps than our pyc, so python
436 # thinks the pyc files are correct using them)
437 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300438
439 # handle GIT_REF accordingly to type (tag, branch)
440 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
441 git_update_tag $GIT_REF
442 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
443 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400444 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
445 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300446 else
447 echo $GIT_REF is neither branch nor tag
448 exit 1
449 fi
450
Dean Troyer7f9aa712012-01-31 12:11:56 -0600451 fi
452 fi
453}
454
455
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500456# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200457# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500458function inicomment() {
459 local file=$1
460 local section=$2
461 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100462 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500463}
464
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200465# Uncomment an option in an INI file
466# iniuncomment config-file section option
467function iniuncomment() {
468 local file=$1
469 local section=$2
470 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100471 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200472}
473
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500474
475# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500476# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500477function iniget() {
478 local file=$1
479 local section=$2
480 local option=$3
481 local line
Attila Fazekas588eb412012-12-20 10:57:16 +0100482 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500483 echo ${line#*=}
484}
485
Attila Fazekas588eb412012-12-20 10:57:16 +0100486# Determinate is the given option present in the INI file
487# ini_has_option config-file section option
488function ini_has_option() {
489 local file=$1
490 local section=$2
491 local option=$3
492 local line
493 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
494 [ -n "$line" ]
495}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500496
497# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500498# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500499function iniset() {
500 local file=$1
501 local section=$2
502 local option=$3
503 local value=$4
Attila Fazekas588eb412012-12-20 10:57:16 +0100504 if ! grep -q "^\[$section\]" "$file"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500505 # Add section at the end
Attila Fazekas588eb412012-12-20 10:57:16 +0100506 echo -e "\n[$section]" >>"$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500507 fi
Attila Fazekas588eb412012-12-20 10:57:16 +0100508 if ! ini_has_option "$file" "$section" "$option"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500509 # Add it
Attila Fazekas588eb412012-12-20 10:57:16 +0100510 sed -i -e "/^\[$section\]/ a\\
Dean Troyer09e636e2012-03-19 16:31:12 -0500511$option = $value
Attila Fazekas588eb412012-12-20 10:57:16 +0100512" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500513 else
514 # Replace it
Attila Fazekas588eb412012-12-20 10:57:16 +0100515 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500516 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500517}
518
519
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000520# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500521# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000522#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500523# Multiple services specified as arguments are ``OR``'ed together; the test
524# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000525#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500526# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000527# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500528# **cinder** returns true if any service enabled start with **c-**
529# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000530# **glance** returns true if any service enabled start with **g-**
531# **quantum** returns true if any service enabled start with **q-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500532#
533# Uses global ``ENABLED_SERVICES``
534# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000535function is_service_enabled() {
536 services=$@
537 for service in ${services}; do
538 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
539 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500540 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700541 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000542 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
543 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
544 done
545 return 1
546}
547
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500548
549# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
550# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400551function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500552 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400553 s/,,/,/g;
554 s/^,//;
555 s/,$//
556 '
557}
558
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500559
Doug Hellmannf04178f2012-07-05 17:10:03 -0400560# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500561# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400562#
563# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800564# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400565#
566# This function does not know about the special cases
567# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500568# Uses global ``ENABLED_SERVICES``
569# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400570function enable_service() {
571 local tmpsvcs="${ENABLED_SERVICES}"
572 for service in $@; do
573 if ! is_service_enabled $service; then
574 tmpsvcs+=",$service"
575 fi
576 done
577 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
578 disable_negated_services
579}
580
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500581
Doug Hellmannf04178f2012-07-05 17:10:03 -0400582# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500583# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400584#
585# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800586# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400587#
588# This function does not know about the special cases
589# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500590# Uses global ``ENABLED_SERVICES``
591# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400592function disable_service() {
593 local tmpsvcs=",${ENABLED_SERVICES},"
594 local service
595 for service in $@; do
596 if is_service_enabled $service; then
597 tmpsvcs=${tmpsvcs//,$service,/,}
598 fi
599 done
600 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
601}
602
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500603
Doug Hellmannf04178f2012-07-05 17:10:03 -0400604# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500605# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400606# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500607# Uses global ``ENABLED_SERVICES``
608# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400609function disable_all_services() {
610 ENABLED_SERVICES=""
611}
612
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500613
614# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800615# services except rabbit (rabbit) set in ``localrc``:
616# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500617# Uses global ``ENABLED_SERVICES``
618# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400619function disable_negated_services() {
620 local tmpsvcs="${ENABLED_SERVICES}"
621 local service
622 for service in ${tmpsvcs//,/ }; do
623 if [[ ${service} == -* ]]; then
624 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
625 fi
626 done
627 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
628}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600629
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500630
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500631# Distro-agnostic package installer
632# install_package package [package ...]
633function install_package() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100634 if is_ubuntu; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200635 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
636 NO_UPDATE_REPOS=True
637
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500638 apt_get install "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100639 elif is_fedora; then
640 yum_install "$@"
641 elif is_suse; then
642 zypper_install "$@"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500643 else
Vincent Untz00011c02012-12-06 09:56:32 +0100644 exit_distro_not_supported "installing packages"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500645 fi
646}
647
648
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200649# Distro-agnostic function to tell if a package is installed
650# is_package_installed package [package ...]
651function is_package_installed() {
652 if [[ -z "$@" ]]; then
653 return 1
654 fi
655
656 if [[ -z "$os_PACKAGE" ]]; then
657 GetOSVersion
658 fi
Vincent Untzc18b9652012-12-04 12:36:34 +0100659
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200660 if [[ "$os_PACKAGE" = "deb" ]]; then
661 dpkg -l "$@" > /dev/null
Vincent Untz00011c02012-12-06 09:56:32 +0100662 elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200663 rpm --quiet -q "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100664 else
665 exit_distro_not_supported "finding if a package is installed"
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200666 fi
667}
668
669
Dean Troyer489bd2a2012-03-02 10:44:29 -0600670# Test if the named environment variable is set and not zero length
671# is_set env-var
672function is_set() {
673 local var=\$"$1"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100674 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 -0600675}
676
677
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500678# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +0000679# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
680# ``TRACK_DEPENDS``, ``*_proxy`
Dean Troyer7f9aa712012-01-31 12:11:56 -0600681# pip_install package [package ...]
682function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600683 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500684 if [[ -z "$os_PACKAGE" ]]; then
685 GetOSVersion
686 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500687 if [[ $TRACK_DEPENDS = True ]] ; then
688 source $DEST/.venv/bin/activate
689 CMD_PIP=$DEST/.venv/bin/pip
690 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500691 else
Monty Taylor47f02062012-07-26 11:09:24 -0500692 SUDO_PIP="sudo"
Vincent Untz8ec27222012-11-29 09:25:31 +0100693 CMD_PIP=$(get_pip_command)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500694 fi
Maru Newby3a87edd2012-10-25 23:01:06 +0000695 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
696 PIP_MIRROR_OPT="--use-mirrors"
697 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500698 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600699 HTTP_PROXY=$http_proxy \
700 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900701 NO_PROXY=$no_proxy \
Maru Newby3a87edd2012-10-25 23:01:06 +0000702 $CMD_PIP install $PIP_MIRROR_OPT $@
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500703}
704
705
706# Service wrapper to restart services
707# restart_service service-name
708function restart_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100709 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600710 sudo /usr/sbin/service $1 restart
711 else
712 sudo /sbin/service $1 restart
713 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500714}
715
716
Dean Troyer15733352012-09-06 11:51:30 -0500717# Helper to launch a service in a named screen
718# screen_it service "command-line"
719function screen_it {
720 NL=`echo -ne '\015'`
721 SCREEN_NAME=${SCREEN_NAME:-stack}
jiajun xua9414242012-12-06 16:30:57 +0800722 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
723
Dean Troyer15733352012-09-06 11:51:30 -0500724 if is_service_enabled $1; then
725 # Append the service to the screen rc file
726 screen_rc "$1" "$2"
727
728 screen -S $SCREEN_NAME -X screen -t $1
729 # sleep to allow bash to be ready to be send the command - we are
730 # creating a new window in screen and then sends characters, so if
731 # bash isn't running by the time we send the command, nothing happens
732 sleep 1.5
733
734 if [[ -n ${SCREEN_LOGDIR} ]]; then
735 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
736 screen -S $SCREEN_NAME -p $1 -X log on
737 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
738 fi
jiajun xua9414242012-12-06 16:30:57 +0800739 screen -S $SCREEN_NAME -p $1 -X stuff "$2 || touch \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
Dean Troyer15733352012-09-06 11:51:30 -0500740 fi
741}
742
743
744# Screen rc file builder
745# screen_rc service "command-line"
746function screen_rc {
747 SCREEN_NAME=${SCREEN_NAME:-stack}
748 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
749 if [[ ! -e $SCREENRC ]]; then
750 # Name the screen session
751 echo "sessionname $SCREEN_NAME" > $SCREENRC
752 # Set a reasonable statusbar
753 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
754 echo "screen -t shell bash" >> $SCREENRC
755 fi
756 # If this service doesn't already exist in the screenrc file
757 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
758 NL=`echo -ne '\015'`
759 echo "screen -t $1 bash" >> $SCREENRC
760 echo "stuff \"$2$NL\"" >> $SCREENRC
761 fi
762}
763
jiajun xua9414242012-12-06 16:30:57 +0800764# Helper to remove the *.failure files under $SERVICE_DIR/$SCREEN_NAME
765# This is used for service_check when all the screen_it are called finished
766# init_service_check
767function init_service_check() {
768 SCREEN_NAME=${SCREEN_NAME:-stack}
769 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
770
771 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
772 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
773 fi
774
775 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
776}
777
778# Helper to get the status of each running service
779# service_check
780function service_check() {
781 local service
782 local failures
783 SCREEN_NAME=${SCREEN_NAME:-stack}
784 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
785
786
787 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
788 echo "No service status directory found"
789 return
790 fi
791
792 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
793 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null`
794
795 for service in $failures; do
796 service=`basename $service`
797 service=${service::-8}
798 echo "Error: Service $service is not running"
799 done
800
801 if [ -n "$failures" ]; then
802 echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
803 fi
804}
Dean Troyer15733352012-09-06 11:51:30 -0500805
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500806# ``pip install`` the dependencies of the package before ``setup.py develop``
807# so pip and not distutils processes the dependency chain
808# Uses globals ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500809# setup_develop directory
810function setup_develop() {
Monty Taylor47f02062012-07-26 11:09:24 -0500811 if [[ $TRACK_DEPENDS = True ]] ; then
812 SUDO_CMD="env"
813 else
814 SUDO_CMD="sudo"
815 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500816 (cd $1; \
817 python setup.py egg_info; \
818 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
819 depend_links=$(echo $raw_links | xargs); \
Dean Troyer1a3c9fe2012-09-29 17:25:02 -0500820 require_file=$([ ! -r *-info/requires.txt ] || echo "-r *-info/requires.txt"); \
821 pip_install $require_file $depend_links; \
Monty Taylor47f02062012-07-26 11:09:24 -0500822 $SUDO_CMD \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500823 HTTP_PROXY=$http_proxy \
824 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900825 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500826 python setup.py develop \
827 )
828}
829
830
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500831# Service wrapper to start services
832# start_service service-name
833function start_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100834 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600835 sudo /usr/sbin/service $1 start
836 else
837 sudo /sbin/service $1 start
838 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500839}
840
841
842# Service wrapper to stop services
843# stop_service service-name
844function stop_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100845 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600846 sudo /usr/sbin/service $1 stop
847 else
848 sudo /sbin/service $1 stop
849 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600850}
851
852
853# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500854# Accepts as False: 0 no false False FALSE
855# Accepts as True: 1 yes true True TRUE
856# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -0600857function trueorfalse() {
858 local default=$1
859 local testval=$2
860
861 [[ -z "$testval" ]] && { echo "$default"; return; }
862 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
863 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
864 echo "$default"
865}
Dean Troyer27e32692012-03-16 16:16:56 -0500866
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500867
Dean Troyerca0e3d02012-04-13 15:58:37 -0500868# Retrieve an image from a URL and upload into Glance
869# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500870# ``FILES`` must be set to the cache dir
871# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -0500872# upload_image image-url glance-token
873function upload_image() {
874 local image_url=$1
875 local token=$2
876
877 # Create a directory for the downloaded image tarballs.
878 mkdir -p $FILES/images
879
880 # Downloads the image (uec ami+aki style), then extracts it.
881 IMAGE_FNAME=`basename "$image_url"`
882 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
883 wget -c $image_url -O $FILES/$IMAGE_FNAME
884 if [[ $? -ne 0 ]]; then
885 echo "Not found: $image_url"
886 return
887 fi
888 fi
889
890 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
891 if [[ "$image_url" =~ 'openvz' ]]; then
892 IMAGE="$FILES/${IMAGE_FNAME}"
893 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
894 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}"
895 return
896 fi
897
898 KERNEL=""
899 RAMDISK=""
900 DISK_FORMAT=""
901 CONTAINER_FORMAT=""
902 UNPACK=""
903 case "$IMAGE_FNAME" in
904 *.tar.gz|*.tgz)
905 # Extract ami and aki files
906 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
907 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
908 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
909 xdir="$FILES/images/$IMAGE_NAME"
910 rm -Rf "$xdir";
911 mkdir "$xdir"
912 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
913 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
914 [ -f "$f" ] && echo "$f" && break; done; true)
915 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
916 [ -f "$f" ] && echo "$f" && break; done; true)
917 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
918 [ -f "$f" ] && echo "$f" && break; done; true)
919 if [[ -z "$IMAGE_NAME" ]]; then
920 IMAGE_NAME=$(basename "$IMAGE" ".img")
921 fi
922 ;;
923 *.img)
924 IMAGE="$FILES/$IMAGE_FNAME";
925 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -0500926 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
927 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
928 DISK_FORMAT=$format
929 else
930 DISK_FORMAT=raw
931 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -0500932 CONTAINER_FORMAT=bare
933 ;;
934 *.img.gz)
935 IMAGE="$FILES/${IMAGE_FNAME}"
936 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
937 DISK_FORMAT=raw
938 CONTAINER_FORMAT=bare
939 UNPACK=zcat
940 ;;
941 *.qcow2)
942 IMAGE="$FILES/${IMAGE_FNAME}"
943 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
944 DISK_FORMAT=qcow2
945 CONTAINER_FORMAT=bare
946 ;;
947 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
948 esac
949
950 if [ "$CONTAINER_FORMAT" = "bare" ]; then
951 if [ "$UNPACK" = "zcat" ]; then
952 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}")
953 else
954 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}"
955 fi
956 else
957 # Use glance client to add the kernel the root filesystem.
958 # We parse the results of the first upload to get the glance ID of the
959 # kernel for use when uploading the root filesystem.
960 KERNEL_ID=""; RAMDISK_ID="";
961 if [ -n "$KERNEL" ]; then
962 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)
963 fi
964 if [ -n "$RAMDISK" ]; then
965 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)
966 fi
967 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}"
968 fi
969}
970
Dean Troyerc1b486a2012-11-05 14:26:09 -0600971# Set the database backend to use
972# When called from stackrc/localrc DATABASE_BACKENDS has not been
973# initialized yet, just save the configuration selection and call back later
974# to validate it.
975# $1 The name of the database backend to use (mysql, postgresql, ...)
976function use_database {
977 if [[ -z "$DATABASE_BACKENDS" ]]; then
978 # The backends haven't initialized yet, just save the selection for now
979 DATABASE_TYPE=$1
Attila Fazekas251d3b52012-12-16 15:05:44 +0100980 else
981 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -0600982 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -0600983}
984
Terry Wilson428af5a2012-11-01 16:12:39 -0400985# Toggle enable/disable_service for services that must run exclusive of each other
986# $1 The name of a variable containing a space-separated list of services
987# $2 The name of a variable in which to store the enabled service's name
988# $3 The name of the service to enable
989function use_exclusive_service {
990 local options=${!1}
991 local selection=$3
992 out=$2
993 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
994 for opt in $options;do
995 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
996 done
997 eval "$out=$selection"
998 return 0
999}
Dean Troyerca0e3d02012-04-13 15:58:37 -05001000
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001001# Wait for an HTTP server to start answering requests
1002# wait_for_service timeout url
1003function wait_for_service() {
1004 local timeout=$1
1005 local url=$2
1006 timeout $timeout sh -c "while ! http_proxy= https_proxy= curl -s $url >/dev/null; do sleep 1; done"
1007}
1008
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001009# Wrapper for ``yum`` to set proxy environment variables
1010# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001011# yum_install package [package ...]
1012function yum_install() {
1013 [[ "$OFFLINE" = "True" ]] && return
1014 local sudo="sudo"
1015 [[ "$(id -u)" = "0" ]] && sudo="env"
1016 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001017 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001018 yum install -y "$@"
1019}
1020
Nachi Uenofda946e2012-10-24 17:26:02 -07001021# ping check
1022# Uses globals ``ENABLED_SERVICES``
1023function ping_check() {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001024 if is_service_enabled quantum; then
1025 _ping_check_quantum "$1" $2 $3 $4
1026 return
1027 fi
1028 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -07001029}
1030
1031# ping check for nova
1032# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
1033function _ping_check_novanet() {
1034 local from_net=$1
1035 local ip=$2
1036 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001037 local expected=${4:-"True"}
1038 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001039 MULTI_HOST=`trueorfalse False $MULTI_HOST`
1040 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
1041 sleep $boot_timeout
1042 return
1043 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001044 if [[ "$expected" = "True" ]]; then
1045 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
1046 else
1047 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
1048 fi
1049 if ! timeout $boot_timeout sh -c "$check_command"; then
1050 if [[ "$expected" = "True" ]]; then
1051 echo "[Fail] Couldn't ping server"
1052 else
1053 echo "[Fail] Could ping server"
1054 fi
Nachi Uenofda946e2012-10-24 17:26:02 -07001055 exit 1
1056 fi
1057}
1058
1059# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001060
Nachi Uenofda946e2012-10-24 17:26:02 -07001061function ssh_check() {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001062 if is_service_enabled quantum; then
1063 _ssh_check_quantum "$1" $2 $3 $4 $5
1064 return
1065 fi
1066 _ssh_check_novanet "$1" $2 $3 $4 $5
1067}
1068
1069function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -07001070 local NET_NAME=$1
1071 local KEY_FILE=$2
1072 local FLOATING_IP=$3
1073 local DEFAULT_INSTANCE_USER=$4
1074 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -06001075 local probe_cmd=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001076 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
1077 echo "server didn't become ssh-able!"
1078 exit 1
1079 fi
1080}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001081
Vincent Untz856a11e2012-11-21 16:04:12 +01001082
1083# zypper wrapper to set arguments correctly
1084# zypper_install package [package ...]
1085function zypper_install() {
1086 [[ "$OFFLINE" = "True" ]] && return
1087 local sudo="sudo"
1088 [[ "$(id -u)" = "0" ]] && sudo="env"
1089 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1090 zypper --non-interactive install --auto-agree-with-licenses "$@"
1091}
1092
1093
1094# Add a user to a group.
1095# add_user_to_group user group
1096function add_user_to_group() {
1097 local user=$1
1098 local group=$2
1099
1100 if [[ -z "$os_VENDOR" ]]; then
1101 GetOSVersion
1102 fi
1103
1104 # SLE11 and openSUSE 12.2 don't have the usual usermod
1105 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1106 sudo usermod -a -G "$group" "$user"
1107 else
1108 sudo usermod -A "$group" "$user"
1109 fi
1110}
1111
1112
1113# Get the location of the $module-rootwrap executables, where module is cinder
1114# or nova.
1115# get_rootwrap_location module
1116function get_rootwrap_location() {
1117 local module=$1
1118
Vincent Untz00011c02012-12-06 09:56:32 +01001119 if is_fedora; then
Vincent Untz856a11e2012-11-21 16:04:12 +01001120 echo "/usr/bin/$module-rootwrap"
Vincent Untz00011c02012-12-06 09:56:32 +01001121 else
1122 echo "/usr/local/bin/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +01001123 fi
1124}
1125
Vincent Untz8ec27222012-11-29 09:25:31 +01001126# Get the path to the pip command.
1127# get_pip_command
1128function get_pip_command() {
Vincent Untz00011c02012-12-06 09:56:32 +01001129 if is_fedora; then
Nikhil Manchanda35138ed2013-01-03 17:49:58 -08001130 which pip-python
Vincent Untz00011c02012-12-06 09:56:32 +01001131 else
Nikhil Manchanda35138ed2013-01-03 17:49:58 -08001132 which pip
Vincent Untz8ec27222012-11-29 09:25:31 +01001133 fi
1134}
Vincent Untz856a11e2012-11-21 16:04:12 +01001135
1136# Check if qpid can be used on the current distro.
1137# qpid_is_supported
1138function qpid_is_supported() {
1139 if [[ -z "$DISTRO" ]]; then
1140 GetDistro
1141 fi
1142
1143 # Qpid was introduced to Ubuntu in precise, disallow it on oneiric; it is
1144 # not in openSUSE either right now.
Russell Bryantc2d2f522012-12-03 10:02:40 -05001145 ( ! ([[ "$DISTRO" = "oneiric" ]] || is_suse) )
Vincent Untz856a11e2012-11-21 16:04:12 +01001146}
1147
Dean Troyer27e32692012-03-16 16:16:56 -05001148# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +00001149$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001150
1151
1152# Local variables:
1153# -*- mode: Shell-script -*-
Andrew Laskif900bd72012-09-05 17:23:14 -04001154# End: