blob: 22ba168e552b6491457e4ac5e39cadbb87f50713 [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
Dean Troyer7e270512012-06-14 15:23:24 -0500120# get_packages() collects a list of package names of any type from the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500121# prerequisite files in ``files/{apts|rpms}``. The list is intended
122# to be passed to a package installer such as apt or yum.
Dean Troyer7e270512012-06-14 15:23:24 -0500123#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500124# Only packages required for the services in ``ENABLED_SERVICES`` will be
Dean Troyer7e270512012-06-14 15:23:24 -0500125# included. Two bits of metadata are recognized in the prerequisite files:
126# - ``# NOPRIME`` defers installation to be performed later in stack.sh
127# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
128# of the package to the distros listed. The distro names are case insensitive.
129#
Vincent Untz855c5872012-10-04 13:36:46 +0200130# Uses globals ``ENABLED_SERVICES``
Dean Troyer7e270512012-06-14 15:23:24 -0500131# get_packages dir
132function get_packages() {
133 local package_dir=$1
134 local file_to_parse
135 local service
136
137 if [[ -z "$package_dir" ]]; then
138 echo "No package directory supplied"
139 return 1
140 fi
141 if [[ -z "$DISTRO" ]]; then
Vincent Untz855c5872012-10-04 13:36:46 +0200142 GetDistro
Dean Troyer7e270512012-06-14 15:23:24 -0500143 fi
144 for service in general ${ENABLED_SERVICES//,/ }; do
145 # Allow individual services to specify dependencies
146 if [[ -e ${package_dir}/${service} ]]; then
147 file_to_parse="${file_to_parse} $service"
148 fi
149 # NOTE(sdague) n-api needs glance for now because that's where
150 # glance client is
151 if [[ $service == n-api ]]; then
152 if [[ ! $file_to_parse =~ nova ]]; then
153 file_to_parse="${file_to_parse} nova"
154 fi
155 if [[ ! $file_to_parse =~ glance ]]; then
156 file_to_parse="${file_to_parse} glance"
157 fi
158 elif [[ $service == c-* ]]; then
159 if [[ ! $file_to_parse =~ cinder ]]; then
160 file_to_parse="${file_to_parse} cinder"
161 fi
John H. Tran93361642012-07-26 11:22:05 -0700162 elif [[ $service == ceilometer-* ]]; then
163 if [[ ! $file_to_parse =~ ceilometer ]]; then
164 file_to_parse="${file_to_parse} ceilometer"
165 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500166 elif [[ $service == n-* ]]; then
167 if [[ ! $file_to_parse =~ nova ]]; then
168 file_to_parse="${file_to_parse} nova"
169 fi
170 elif [[ $service == g-* ]]; then
171 if [[ ! $file_to_parse =~ glance ]]; then
172 file_to_parse="${file_to_parse} glance"
173 fi
174 elif [[ $service == key* ]]; then
175 if [[ ! $file_to_parse =~ keystone ]]; then
176 file_to_parse="${file_to_parse} keystone"
177 fi
Robert Collins0a9954f2012-11-20 11:34:25 +1300178 elif [[ $service == q-* ]]; then
179 if [[ ! $file_to_parse =~ quantum ]]; then
180 file_to_parse="${file_to_parse} quantum"
181 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500182 fi
183 done
184
185 for file in ${file_to_parse}; do
186 local fname=${package_dir}/${file}
187 local OIFS line package distros distro
188 [[ -e $fname ]] || continue
189
190 OIFS=$IFS
191 IFS=$'\n'
192 for line in $(<${fname}); do
193 if [[ $line =~ "NOPRIME" ]]; then
194 continue
195 fi
196
197 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
198 # We are using BASH regexp matching feature.
199 package=${BASH_REMATCH[1]}
200 distros=${BASH_REMATCH[2]}
201 # In bash ${VAR,,} will lowecase VAR
202 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
203 continue
204 fi
205
206 echo ${line%#*}
207 done
208 IFS=$OIFS
209 done
210}
211
212
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500213# Determine OS Vendor, Release and Update
214# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
215# Returns results in global variables:
216# os_VENDOR - vendor name
217# os_RELEASE - release
218# os_UPDATE - update
219# os_PACKAGE - package type
220# os_CODENAME - vendor's codename for release
221# GetOSVersion
222GetOSVersion() {
223 # Figure out which vendor we are
224 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
225 # OS/X
226 os_VENDOR=`sw_vers -productName`
227 os_RELEASE=`sw_vers -productVersion`
228 os_UPDATE=${os_RELEASE##*.}
229 os_RELEASE=${os_RELEASE%.*}
230 os_PACKAGE=""
231 if [[ "$os_RELEASE" =~ "10.7" ]]; then
232 os_CODENAME="lion"
233 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
234 os_CODENAME="snow leopard"
235 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
236 os_CODENAME="leopard"
237 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
238 os_CODENAME="tiger"
239 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
240 os_CODENAME="panther"
241 else
242 os_CODENAME=""
243 fi
244 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
245 os_VENDOR=$(lsb_release -i -s)
246 os_RELEASE=$(lsb_release -r -s)
247 os_UPDATE=""
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100248 os_PACKAGE="rpm"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500249 if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
250 os_PACKAGE="deb"
Vincent Untz856a11e2012-11-21 16:04:12 +0100251 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
252 lsb_release -d -s | grep -q openSUSE
253 if [[ $? -eq 0 ]]; then
254 os_VENDOR="openSUSE"
255 fi
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100256 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
257 os_VENDOR="Red Hat"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500258 fi
259 os_CODENAME=$(lsb_release -c -s)
260 elif [[ -r /etc/redhat-release ]]; then
261 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
262 # CentOS release 5.5 (Final)
263 # CentOS Linux release 6.0 (Final)
264 # Fedora release 16 (Verne)
265 os_CODENAME=""
266 for r in "Red Hat" CentOS Fedora; do
267 os_VENDOR=$r
268 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
269 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
270 os_CODENAME=${ver#*|}
271 os_RELEASE=${ver%|*}
272 os_UPDATE=${os_RELEASE##*.}
273 os_RELEASE=${os_RELEASE%.*}
274 break
275 fi
276 os_VENDOR=""
277 done
278 os_PACKAGE="rpm"
Vincent Untz856a11e2012-11-21 16:04:12 +0100279 elif [[ -r /etc/SuSE-release ]]; then
280 for r in openSUSE "SUSE Linux"; do
281 if [[ "$r" = "SUSE Linux" ]]; then
282 os_VENDOR="SUSE LINUX"
283 else
284 os_VENDOR=$r
285 fi
286
287 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
288 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
289 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
290 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
291 break
292 fi
293 os_VENDOR=""
294 done
295 os_PACKAGE="rpm"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500296 fi
297 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
298}
299
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300300# git update using reference as a branch.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500301# git_update_branch ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300302function git_update_branch() {
303
304 GIT_BRANCH=$1
305
306 git checkout -f origin/$GIT_BRANCH
307 # a local branch might not exist
308 git branch -D $GIT_BRANCH || true
309 git checkout -b $GIT_BRANCH
310}
311
312
313# git update using reference as a tag. Be careful editing source at that repo
314# as working copy will be in a detached mode
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500315# git_update_tag ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300316function git_update_tag() {
317
318 GIT_TAG=$1
319
320 git tag -d $GIT_TAG
321 # fetching given tag only
322 git fetch origin tag $GIT_TAG
323 git checkout -f $GIT_TAG
324}
325
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500326
Andrew Laskif900bd72012-09-05 17:23:14 -0400327# git update using reference as a branch.
328# git_update_remote_branch ref
329function git_update_remote_branch() {
330
331 GIT_BRANCH=$1
332
333 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
334}
335
336
Dean Troyera9e0a482012-07-09 14:07:23 -0500337# Translate the OS version values into common nomenclature
338# Sets ``DISTRO`` from the ``os_*`` values
339function GetDistro() {
340 GetOSVersion
341 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
342 # 'Everyone' refers to Ubuntu releases by the code name adjective
343 DISTRO=$os_CODENAME
344 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
345 # For Fedora, just use 'f' and the release
346 DISTRO="f$os_RELEASE"
Vincent Untz856a11e2012-11-21 16:04:12 +0100347 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
348 DISTRO="opensuse-$os_RELEASE"
349 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
350 # For SLE, also use the service pack
351 if [[ -z "$os_UPDATE" ]]; then
352 DISTRO="sle${os_RELEASE}"
353 else
354 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
355 fi
Dean Troyera9e0a482012-07-09 14:07:23 -0500356 else
357 # Catch-all for now is Vendor + Release + Update
358 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
359 fi
360 export DISTRO
361}
362
363
Vincent Untzc18b9652012-12-04 12:36:34 +0100364# Determine if current distribution is an Ubuntu-based distribution.
365# It will also detect non-Ubuntu but Debian-based distros; this is not an issue
366# since Debian and Ubuntu should be compatible.
367# is_ubuntu
368function is_ubuntu {
369 if [[ -z "$os_PACKAGE" ]]; then
370 GetOSVersion
371 fi
372
373 [ "$os_PACKAGE" = "deb" ]
374}
375
376
Vincent Untz00011c02012-12-06 09:56:32 +0100377# Determine if current distribution is a Fedora-based distribution
378# (Fedora, RHEL, CentOS).
379# is_fedora
380function is_fedora {
381 if [[ -z "$os_VENDOR" ]]; then
382 GetOSVersion
383 fi
384
385 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ]
386}
387
388
Vincent Untz856a11e2012-11-21 16:04:12 +0100389# Determine if current distribution is a SUSE-based distribution
390# (openSUSE, SLE).
391# is_suse
392function is_suse {
393 if [[ -z "$os_VENDOR" ]]; then
394 GetOSVersion
395 fi
396
Steve Baker1a7bbd22012-12-03 17:04:02 +1300397 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
Vincent Untz856a11e2012-11-21 16:04:12 +0100398}
399
400
Vincent Untz00011c02012-12-06 09:56:32 +0100401# Exit after outputting a message about the distribution not being supported.
402# exit_distro_not_supported [optional-string-telling-what-is-missing]
403function exit_distro_not_supported {
404 if [[ -z "$DISTRO" ]]; then
405 GetDistro
406 fi
407
408 if [ $# -gt 0 ]; then
409 echo "Support for $DISTRO is incomplete: no support for $@"
410 else
411 echo "Support for $DISTRO is incomplete."
412 fi
413
414 exit 1
415}
416
417
Dean Troyer7f9aa712012-01-31 12:11:56 -0600418# git clone only if directory doesn't exist already. Since ``DEST`` might not
419# be owned by the installation user, we create the directory and change the
420# ownership to the proper user.
421# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700422# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
423# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500424# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600425# git_clone remote dest-dir branch
426function git_clone {
427 [[ "$OFFLINE" = "True" ]] && return
428
429 GIT_REMOTE=$1
430 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300431 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600432
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300433 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600434 # If our branch name is a gerrit style refs/changes/...
435 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700436 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600437 git clone $GIT_REMOTE $GIT_DEST
438 fi
439 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300440 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600441 else
442 # do a full clone only if the directory doesn't exist
443 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700444 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600445 git clone $GIT_REMOTE $GIT_DEST
446 cd $GIT_DEST
447 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300448 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600449 elif [[ "$RECLONE" == "yes" ]]; then
450 # if it does exist then simulate what clone does if asked to RECLONE
451 cd $GIT_DEST
452 # set the url to pull from and fetch
453 git remote set-url origin $GIT_REMOTE
454 git fetch origin
455 # remove the existing ignored files (like pyc) as they cause breakage
456 # (due to the py files having older timestamps than our pyc, so python
457 # thinks the pyc files are correct using them)
458 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300459
460 # handle GIT_REF accordingly to type (tag, branch)
461 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
462 git_update_tag $GIT_REF
463 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
464 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400465 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
466 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300467 else
468 echo $GIT_REF is neither branch nor tag
469 exit 1
470 fi
471
Dean Troyer7f9aa712012-01-31 12:11:56 -0600472 fi
473 fi
474}
475
476
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500477# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200478# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500479function inicomment() {
480 local file=$1
481 local section=$2
482 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100483 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500484}
485
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200486# Uncomment an option in an INI file
487# iniuncomment config-file section option
488function iniuncomment() {
489 local file=$1
490 local section=$2
491 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100492 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200493}
494
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500495
496# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500497# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500498function iniget() {
499 local file=$1
500 local section=$2
501 local option=$3
502 local line
Attila Fazekas588eb412012-12-20 10:57:16 +0100503 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500504 echo ${line#*=}
505}
506
Attila Fazekas588eb412012-12-20 10:57:16 +0100507# Determinate is the given option present in the INI file
508# ini_has_option config-file section option
509function ini_has_option() {
510 local file=$1
511 local section=$2
512 local option=$3
513 local line
514 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
515 [ -n "$line" ]
516}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500517
518# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500519# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500520function iniset() {
521 local file=$1
522 local section=$2
523 local option=$3
524 local value=$4
Attila Fazekas588eb412012-12-20 10:57:16 +0100525 if ! grep -q "^\[$section\]" "$file"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500526 # Add section at the end
Attila Fazekas588eb412012-12-20 10:57:16 +0100527 echo -e "\n[$section]" >>"$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500528 fi
Attila Fazekas588eb412012-12-20 10:57:16 +0100529 if ! ini_has_option "$file" "$section" "$option"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500530 # Add it
Attila Fazekas588eb412012-12-20 10:57:16 +0100531 sed -i -e "/^\[$section\]/ a\\
Dean Troyer09e636e2012-03-19 16:31:12 -0500532$option = $value
Attila Fazekas588eb412012-12-20 10:57:16 +0100533" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500534 else
535 # Replace it
Attila Fazekas588eb412012-12-20 10:57:16 +0100536 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500537 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500538}
539
540
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000541# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500542# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000543#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500544# Multiple services specified as arguments are ``OR``'ed together; the test
545# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000546#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500547# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000548# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500549# **cinder** returns true if any service enabled start with **c-**
550# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000551# **glance** returns true if any service enabled start with **g-**
552# **quantum** returns true if any service enabled start with **q-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500553#
554# Uses global ``ENABLED_SERVICES``
555# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000556function is_service_enabled() {
557 services=$@
558 for service in ${services}; do
559 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
560 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500561 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700562 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000563 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
564 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
565 done
566 return 1
567}
568
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500569
570# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
571# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400572function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500573 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400574 s/,,/,/g;
575 s/^,//;
576 s/,$//
577 '
578}
579
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500580
Doug Hellmannf04178f2012-07-05 17:10:03 -0400581# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500582# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400583#
584# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800585# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400586#
587# This function does not know about the special cases
588# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500589# Uses global ``ENABLED_SERVICES``
590# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400591function enable_service() {
592 local tmpsvcs="${ENABLED_SERVICES}"
593 for service in $@; do
594 if ! is_service_enabled $service; then
595 tmpsvcs+=",$service"
596 fi
597 done
598 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
599 disable_negated_services
600}
601
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500602
Doug Hellmannf04178f2012-07-05 17:10:03 -0400603# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500604# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400605#
606# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800607# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400608#
609# This function does not know about the special cases
610# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500611# Uses global ``ENABLED_SERVICES``
612# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400613function disable_service() {
614 local tmpsvcs=",${ENABLED_SERVICES},"
615 local service
616 for service in $@; do
617 if is_service_enabled $service; then
618 tmpsvcs=${tmpsvcs//,$service,/,}
619 fi
620 done
621 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
622}
623
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500624
Doug Hellmannf04178f2012-07-05 17:10:03 -0400625# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500626# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400627# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500628# Uses global ``ENABLED_SERVICES``
629# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400630function disable_all_services() {
631 ENABLED_SERVICES=""
632}
633
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500634
635# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800636# services except rabbit (rabbit) set in ``localrc``:
637# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500638# Uses global ``ENABLED_SERVICES``
639# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400640function disable_negated_services() {
641 local tmpsvcs="${ENABLED_SERVICES}"
642 local service
643 for service in ${tmpsvcs//,/ }; do
644 if [[ ${service} == -* ]]; then
645 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
646 fi
647 done
648 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
649}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600650
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500651
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500652# Distro-agnostic package installer
653# install_package package [package ...]
654function install_package() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100655 if is_ubuntu; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200656 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
657 NO_UPDATE_REPOS=True
658
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500659 apt_get install "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100660 elif is_fedora; then
661 yum_install "$@"
662 elif is_suse; then
663 zypper_install "$@"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500664 else
Vincent Untz00011c02012-12-06 09:56:32 +0100665 exit_distro_not_supported "installing packages"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500666 fi
667}
668
669
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200670# Distro-agnostic function to tell if a package is installed
671# is_package_installed package [package ...]
672function is_package_installed() {
673 if [[ -z "$@" ]]; then
674 return 1
675 fi
676
677 if [[ -z "$os_PACKAGE" ]]; then
678 GetOSVersion
679 fi
Vincent Untzc18b9652012-12-04 12:36:34 +0100680
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200681 if [[ "$os_PACKAGE" = "deb" ]]; then
682 dpkg -l "$@" > /dev/null
Vincent Untz00011c02012-12-06 09:56:32 +0100683 elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200684 rpm --quiet -q "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100685 else
686 exit_distro_not_supported "finding if a package is installed"
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200687 fi
688}
689
690
Dean Troyer489bd2a2012-03-02 10:44:29 -0600691# Test if the named environment variable is set and not zero length
692# is_set env-var
693function is_set() {
694 local var=\$"$1"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100695 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 -0600696}
697
698
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500699# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +0000700# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
701# ``TRACK_DEPENDS``, ``*_proxy`
Dean Troyer7f9aa712012-01-31 12:11:56 -0600702# pip_install package [package ...]
703function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600704 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500705 if [[ -z "$os_PACKAGE" ]]; then
706 GetOSVersion
707 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500708 if [[ $TRACK_DEPENDS = True ]] ; then
709 source $DEST/.venv/bin/activate
710 CMD_PIP=$DEST/.venv/bin/pip
711 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500712 else
Monty Taylor47f02062012-07-26 11:09:24 -0500713 SUDO_PIP="sudo"
Vincent Untz8ec27222012-11-29 09:25:31 +0100714 CMD_PIP=$(get_pip_command)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500715 fi
Maru Newby3a87edd2012-10-25 23:01:06 +0000716 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
717 PIP_MIRROR_OPT="--use-mirrors"
718 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500719 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600720 HTTP_PROXY=$http_proxy \
721 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900722 NO_PROXY=$no_proxy \
Maru Newby3a87edd2012-10-25 23:01:06 +0000723 $CMD_PIP install $PIP_MIRROR_OPT $@
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500724}
725
726
727# Service wrapper to restart services
728# restart_service service-name
729function restart_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100730 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600731 sudo /usr/sbin/service $1 restart
732 else
733 sudo /sbin/service $1 restart
734 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500735}
736
737
Dean Troyer15733352012-09-06 11:51:30 -0500738# Helper to launch a service in a named screen
739# screen_it service "command-line"
740function screen_it {
Dean Troyer15733352012-09-06 11:51:30 -0500741 SCREEN_NAME=${SCREEN_NAME:-stack}
jiajun xua9414242012-12-06 16:30:57 +0800742 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Vishvananda Ishaya58e21342013-02-11 16:48:12 -0800743 SCREEN_DEV=`trueorfalse True $SCREEN_DEV`
jiajun xua9414242012-12-06 16:30:57 +0800744
Dean Troyer15733352012-09-06 11:51:30 -0500745 if is_service_enabled $1; then
746 # Append the service to the screen rc file
747 screen_rc "$1" "$2"
748
749 screen -S $SCREEN_NAME -X screen -t $1
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +0000750
751 if [[ -n ${SCREEN_LOGDIR} ]]; then
752 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
753 screen -S $SCREEN_NAME -p $1 -X log on
754 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
755 fi
756
Vishvananda Ishaya58e21342013-02-11 16:48:12 -0800757 if [[ "$SCREEN_DEV" = "True" ]]; then
758 # sleep to allow bash to be ready to be send the command - we are
759 # creating a new window in screen and then sends characters, so if
760 # bash isn't running by the time we send the command, nothing happens
761 sleep 1.5
Dean Troyer15733352012-09-06 11:51:30 -0500762
Vishvananda Ishaya58e21342013-02-11 16:48:12 -0800763 NL=`echo -ne '\015'`
764 screen -S $SCREEN_NAME -p $1 -X stuff "$2 || touch \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
765 else
766 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 -0500767 fi
Dean Troyer15733352012-09-06 11:51:30 -0500768 fi
769}
770
771
772# Screen rc file builder
773# screen_rc service "command-line"
774function screen_rc {
775 SCREEN_NAME=${SCREEN_NAME:-stack}
776 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
777 if [[ ! -e $SCREENRC ]]; then
778 # Name the screen session
779 echo "sessionname $SCREEN_NAME" > $SCREENRC
780 # Set a reasonable statusbar
781 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
782 echo "screen -t shell bash" >> $SCREENRC
783 fi
784 # If this service doesn't already exist in the screenrc file
785 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
786 NL=`echo -ne '\015'`
787 echo "screen -t $1 bash" >> $SCREENRC
788 echo "stuff \"$2$NL\"" >> $SCREENRC
789 fi
790}
791
jiajun xua9414242012-12-06 16:30:57 +0800792# Helper to remove the *.failure files under $SERVICE_DIR/$SCREEN_NAME
793# This is used for service_check when all the screen_it are called finished
794# init_service_check
795function init_service_check() {
796 SCREEN_NAME=${SCREEN_NAME:-stack}
797 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
798
799 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
800 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
801 fi
802
803 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
804}
805
806# Helper to get the status of each running service
807# service_check
808function service_check() {
809 local service
810 local failures
811 SCREEN_NAME=${SCREEN_NAME:-stack}
812 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
813
814
815 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
816 echo "No service status directory found"
817 return
818 fi
819
820 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
821 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null`
822
823 for service in $failures; do
824 service=`basename $service`
825 service=${service::-8}
826 echo "Error: Service $service is not running"
827 done
828
829 if [ -n "$failures" ]; then
830 echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
831 fi
832}
Dean Troyer15733352012-09-06 11:51:30 -0500833
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500834# ``pip install`` the dependencies of the package before ``setup.py develop``
835# so pip and not distutils processes the dependency chain
836# Uses globals ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500837# setup_develop directory
838function setup_develop() {
Monty Taylor47f02062012-07-26 11:09:24 -0500839 if [[ $TRACK_DEPENDS = True ]] ; then
840 SUDO_CMD="env"
841 else
842 SUDO_CMD="sudo"
843 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500844 (cd $1; \
845 python setup.py egg_info; \
846 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
847 depend_links=$(echo $raw_links | xargs); \
Dean Troyer1a3c9fe2012-09-29 17:25:02 -0500848 require_file=$([ ! -r *-info/requires.txt ] || echo "-r *-info/requires.txt"); \
849 pip_install $require_file $depend_links; \
Monty Taylor47f02062012-07-26 11:09:24 -0500850 $SUDO_CMD \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500851 HTTP_PROXY=$http_proxy \
852 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900853 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500854 python setup.py develop \
855 )
856}
857
858
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500859# Service wrapper to start services
860# start_service service-name
861function start_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100862 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600863 sudo /usr/sbin/service $1 start
864 else
865 sudo /sbin/service $1 start
866 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500867}
868
869
870# Service wrapper to stop services
871# stop_service service-name
872function stop_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100873 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600874 sudo /usr/sbin/service $1 stop
875 else
876 sudo /sbin/service $1 stop
877 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600878}
879
880
881# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500882# Accepts as False: 0 no false False FALSE
883# Accepts as True: 1 yes true True TRUE
884# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -0600885function trueorfalse() {
886 local default=$1
887 local testval=$2
888
889 [[ -z "$testval" ]] && { echo "$default"; return; }
890 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
891 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
892 echo "$default"
893}
Dean Troyer27e32692012-03-16 16:16:56 -0500894
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500895
Dean Troyerca0e3d02012-04-13 15:58:37 -0500896# Retrieve an image from a URL and upload into Glance
897# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500898# ``FILES`` must be set to the cache dir
899# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -0500900# upload_image image-url glance-token
901function upload_image() {
902 local image_url=$1
903 local token=$2
904
905 # Create a directory for the downloaded image tarballs.
906 mkdir -p $FILES/images
907
908 # Downloads the image (uec ami+aki style), then extracts it.
909 IMAGE_FNAME=`basename "$image_url"`
910 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
911 wget -c $image_url -O $FILES/$IMAGE_FNAME
912 if [[ $? -ne 0 ]]; then
913 echo "Not found: $image_url"
914 return
915 fi
916 fi
917
918 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
919 if [[ "$image_url" =~ 'openvz' ]]; then
920 IMAGE="$FILES/${IMAGE_FNAME}"
921 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
922 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}"
923 return
924 fi
925
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500926 # XenServer-ovf-format images are provided as .vhd.tgz as well
927 # and should not be decompressed prior to loading
928 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
929 IMAGE="$FILES/${IMAGE_FNAME}"
930 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
931 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}"
932 return
933 fi
934
Dean Troyerca0e3d02012-04-13 15:58:37 -0500935 KERNEL=""
936 RAMDISK=""
937 DISK_FORMAT=""
938 CONTAINER_FORMAT=""
939 UNPACK=""
940 case "$IMAGE_FNAME" in
941 *.tar.gz|*.tgz)
942 # Extract ami and aki files
943 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
944 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
945 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
946 xdir="$FILES/images/$IMAGE_NAME"
947 rm -Rf "$xdir";
948 mkdir "$xdir"
949 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
950 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
951 [ -f "$f" ] && echo "$f" && break; done; true)
952 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
953 [ -f "$f" ] && echo "$f" && break; done; true)
954 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
955 [ -f "$f" ] && echo "$f" && break; done; true)
956 if [[ -z "$IMAGE_NAME" ]]; then
957 IMAGE_NAME=$(basename "$IMAGE" ".img")
958 fi
959 ;;
960 *.img)
961 IMAGE="$FILES/$IMAGE_FNAME";
962 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -0500963 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
964 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
965 DISK_FORMAT=$format
966 else
967 DISK_FORMAT=raw
968 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -0500969 CONTAINER_FORMAT=bare
970 ;;
971 *.img.gz)
972 IMAGE="$FILES/${IMAGE_FNAME}"
973 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
974 DISK_FORMAT=raw
975 CONTAINER_FORMAT=bare
976 UNPACK=zcat
977 ;;
978 *.qcow2)
979 IMAGE="$FILES/${IMAGE_FNAME}"
980 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
981 DISK_FORMAT=qcow2
982 CONTAINER_FORMAT=bare
983 ;;
984 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
985 esac
986
987 if [ "$CONTAINER_FORMAT" = "bare" ]; then
988 if [ "$UNPACK" = "zcat" ]; then
989 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}")
990 else
991 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}"
992 fi
993 else
994 # Use glance client to add the kernel the root filesystem.
995 # We parse the results of the first upload to get the glance ID of the
996 # kernel for use when uploading the root filesystem.
997 KERNEL_ID=""; RAMDISK_ID="";
998 if [ -n "$KERNEL" ]; then
999 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)
1000 fi
1001 if [ -n "$RAMDISK" ]; then
1002 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)
1003 fi
1004 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}"
1005 fi
1006}
1007
Dean Troyerc1b486a2012-11-05 14:26:09 -06001008# Set the database backend to use
1009# When called from stackrc/localrc DATABASE_BACKENDS has not been
1010# initialized yet, just save the configuration selection and call back later
1011# to validate it.
1012# $1 The name of the database backend to use (mysql, postgresql, ...)
1013function use_database {
1014 if [[ -z "$DATABASE_BACKENDS" ]]; then
1015 # The backends haven't initialized yet, just save the selection for now
1016 DATABASE_TYPE=$1
Attila Fazekas251d3b52012-12-16 15:05:44 +01001017 else
1018 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -06001019 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -06001020}
1021
Terry Wilson428af5a2012-11-01 16:12:39 -04001022# Toggle enable/disable_service for services that must run exclusive of each other
1023# $1 The name of a variable containing a space-separated list of services
1024# $2 The name of a variable in which to store the enabled service's name
1025# $3 The name of the service to enable
1026function use_exclusive_service {
1027 local options=${!1}
1028 local selection=$3
1029 out=$2
1030 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1031 for opt in $options;do
1032 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1033 done
1034 eval "$out=$selection"
1035 return 0
1036}
Dean Troyerca0e3d02012-04-13 15:58:37 -05001037
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001038# Wait for an HTTP server to start answering requests
1039# wait_for_service timeout url
1040function wait_for_service() {
1041 local timeout=$1
1042 local url=$2
1043 timeout $timeout sh -c "while ! http_proxy= https_proxy= curl -s $url >/dev/null; do sleep 1; done"
1044}
1045
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001046# Wrapper for ``yum`` to set proxy environment variables
1047# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001048# yum_install package [package ...]
1049function yum_install() {
1050 [[ "$OFFLINE" = "True" ]] && return
1051 local sudo="sudo"
1052 [[ "$(id -u)" = "0" ]] && sudo="env"
1053 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001054 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001055 yum install -y "$@"
1056}
1057
Nachi Uenofda946e2012-10-24 17:26:02 -07001058# ping check
1059# Uses globals ``ENABLED_SERVICES``
1060function ping_check() {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001061 if is_service_enabled quantum; then
1062 _ping_check_quantum "$1" $2 $3 $4
1063 return
1064 fi
1065 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -07001066}
1067
1068# ping check for nova
1069# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
1070function _ping_check_novanet() {
1071 local from_net=$1
1072 local ip=$2
1073 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001074 local expected=${4:-"True"}
1075 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001076 MULTI_HOST=`trueorfalse False $MULTI_HOST`
1077 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
1078 sleep $boot_timeout
1079 return
1080 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001081 if [[ "$expected" = "True" ]]; then
1082 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
1083 else
1084 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
1085 fi
1086 if ! timeout $boot_timeout sh -c "$check_command"; then
1087 if [[ "$expected" = "True" ]]; then
1088 echo "[Fail] Couldn't ping server"
1089 else
1090 echo "[Fail] Could ping server"
1091 fi
Nachi Uenofda946e2012-10-24 17:26:02 -07001092 exit 1
1093 fi
1094}
1095
1096# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001097
Nachi Uenofda946e2012-10-24 17:26:02 -07001098function ssh_check() {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001099 if is_service_enabled quantum; then
1100 _ssh_check_quantum "$1" $2 $3 $4 $5
1101 return
1102 fi
1103 _ssh_check_novanet "$1" $2 $3 $4 $5
1104}
1105
1106function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -07001107 local NET_NAME=$1
1108 local KEY_FILE=$2
1109 local FLOATING_IP=$3
1110 local DEFAULT_INSTANCE_USER=$4
1111 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -06001112 local probe_cmd=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001113 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
1114 echo "server didn't become ssh-able!"
1115 exit 1
1116 fi
1117}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001118
Vincent Untz856a11e2012-11-21 16:04:12 +01001119
1120# zypper wrapper to set arguments correctly
1121# zypper_install package [package ...]
1122function zypper_install() {
1123 [[ "$OFFLINE" = "True" ]] && return
1124 local sudo="sudo"
1125 [[ "$(id -u)" = "0" ]] && sudo="env"
1126 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1127 zypper --non-interactive install --auto-agree-with-licenses "$@"
1128}
1129
1130
1131# Add a user to a group.
1132# add_user_to_group user group
1133function add_user_to_group() {
1134 local user=$1
1135 local group=$2
1136
1137 if [[ -z "$os_VENDOR" ]]; then
1138 GetOSVersion
1139 fi
1140
1141 # SLE11 and openSUSE 12.2 don't have the usual usermod
1142 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1143 sudo usermod -a -G "$group" "$user"
1144 else
1145 sudo usermod -A "$group" "$user"
1146 fi
1147}
1148
1149
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001150# Get the path to the direcotry where python executables are installed.
1151# get_python_exec_prefix
1152function get_python_exec_prefix() {
1153 if is_fedora; then
1154 echo "/usr/bin"
1155 else
1156 echo "/usr/local/bin"
1157 fi
1158}
1159
Vincent Untz856a11e2012-11-21 16:04:12 +01001160# Get the location of the $module-rootwrap executables, where module is cinder
1161# or nova.
1162# get_rootwrap_location module
1163function get_rootwrap_location() {
1164 local module=$1
1165
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001166 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +01001167}
1168
Vincent Untz8ec27222012-11-29 09:25:31 +01001169# Get the path to the pip command.
1170# get_pip_command
1171function get_pip_command() {
Vincent Untz00011c02012-12-06 09:56:32 +01001172 if is_fedora; then
Nikhil Manchanda35138ed2013-01-03 17:49:58 -08001173 which pip-python
Vincent Untz00011c02012-12-06 09:56:32 +01001174 else
Nikhil Manchanda35138ed2013-01-03 17:49:58 -08001175 which pip
Vincent Untz8ec27222012-11-29 09:25:31 +01001176 fi
1177}
Vincent Untz856a11e2012-11-21 16:04:12 +01001178
Dean Troyer27e32692012-03-16 16:16:56 -05001179# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +00001180$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001181
1182
1183# Local variables:
1184# -*- mode: Shell-script -*-
Andrew Laskif900bd72012-09-05 17:23:14 -04001185# End: