blob: 459aedd34c7a64a8df5440fd9df80046efe48f31 [file] [log] [blame]
Dean Troyer7f9aa712012-01-31 12:11:56 -06001# functions - Common functions used by DevStack components
Dean Troyer13dc5cc2012-03-27 14:50:45 -05002#
Dean Troyer4a43b7b2012-08-28 17:43:40 -05003# The following variables are assumed to be defined by certain functions:
Dean Troyer4a43b7b2012-08-28 17:43:40 -05004# ``ENABLED_SERVICES``
5# ``EROR_ON_CLONE``
6# ``FILES``
7# ``GLANCE_HOSTPORT``
8# ``OFFLINE``
9# ``PIP_DOWNLOAD_CACHE``
Maru Newby3a87edd2012-10-25 23:01:06 +000010# ``PIP_USE_MIRRORS``
Dean Troyer4a43b7b2012-08-28 17:43:40 -050011# ``RECLONE``
12# ``TRACK_DEPENDS``
13# ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer13dc5cc2012-03-27 14:50:45 -050014
Dean Troyer7f9aa712012-01-31 12:11:56 -060015
Dean Troyer27e32692012-03-16 16:16:56 -050016# Save trace setting
17XTRACE=$(set +o | grep xtrace)
18set +o xtrace
19
Dean Troyer7f9aa712012-01-31 12:11:56 -060020
Dean Troyer4a43b7b2012-08-28 17:43:40 -050021# Exit 0 if address is in network or 1 if address is not in
22# network or netaddr library is not installed.
23# address_in_net ip-address ip-range
Vishvananda Ishayac9ad14b2012-07-03 20:29:01 +000024function address_in_net() {
25 python -c "
26import netaddr
27import sys
28sys.exit(netaddr.IPAddress('$1') not in netaddr.IPNetwork('$2'))
29"
30}
31
32
Dean Troyer4a43b7b2012-08-28 17:43:40 -050033# Wrapper for ``apt-get`` to set cache and proxy environment variables
34# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -050035# apt_get operation package [package ...]
Dean Troyer7f9aa712012-01-31 12:11:56 -060036function apt_get() {
Dean Troyerd0b21e22012-03-07 14:52:25 -060037 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer7f9aa712012-01-31 12:11:56 -060038 local sudo="sudo"
39 [[ "$(id -u)" = "0" ]] && sudo="env"
40 $sudo DEBIAN_FRONTEND=noninteractive \
41 http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +090042 no_proxy=$no_proxy \
Dean Troyer7f9aa712012-01-31 12:11:56 -060043 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
44}
45
46
47# Gracefully cp only if source file/dir exists
48# cp_it source destination
49function cp_it {
50 if [ -e $1 ] || [ -d $1 ]; then
51 cp -pRL $1 $2
52 fi
53}
54
55
Dean Troyer27e32692012-03-16 16:16:56 -050056# Prints "message" and exits
57# die "message"
58function die() {
Dean Troyer489bd2a2012-03-02 10:44:29 -060059 local exitcode=$?
Nachi Ueno07115eb2013-02-26 12:38:18 -080060 if [ $exitcode == 0 ]; then
61 exitcode=1
62 fi
Dean Troyer27e32692012-03-16 16:16:56 -050063 set +o xtrace
Nachi Ueno07115eb2013-02-26 12:38:18 -080064 local msg="[ERROR] $0:$1 $2"
65 echo $msg 1>&2;
66 if [[ -n ${SCREEN_LOGDIR} ]]; then
67 echo $msg >> "${SCREEN_LOGDIR}/error.log"
68 fi
Dean Troyer27e32692012-03-16 16:16:56 -050069 exit $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -060070}
71
72
73# Checks an environment variable is not set or has length 0 OR if the
74# exit code is non-zero and prints "message" and exits
75# NOTE: env-var is the variable name without a '$'
76# die_if_not_set env-var "message"
77function die_if_not_set() {
Dean Troyer27e32692012-03-16 16:16:56 -050078 (
79 local exitcode=$?
80 set +o xtrace
Nachi Ueno07115eb2013-02-26 12:38:18 -080081 local evar=$2; shift
Dean Troyer27e32692012-03-16 16:16:56 -050082 if ! is_set $evar || [ $exitcode != 0 ]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -080083 die $@
Dean Troyer27e32692012-03-16 16:16:56 -050084 fi
85 )
Dean Troyer489bd2a2012-03-02 10:44:29 -060086}
87
88
Dean Troyer48352ee2012-12-12 12:50:38 -060089# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
90# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
91# ``localrc`` or on the command line if necessary::
92#
93# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
94#
95# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
96
97function export_proxy_variables() {
98 if [[ -n "$http_proxy" ]]; then
99 export http_proxy=$http_proxy
100 fi
101 if [[ -n "$https_proxy" ]]; then
102 export https_proxy=$https_proxy
103 fi
104 if [[ -n "$no_proxy" ]]; then
105 export no_proxy=$no_proxy
106 fi
107}
108
109
Dean Troyer489bd2a2012-03-02 10:44:29 -0600110# Grab a numbered field from python prettytable output
111# Fields are numbered starting with 1
112# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
113# get_field field-number
114function get_field() {
115 while read data; do
116 if [ "$1" -lt 0 ]; then
117 field="(\$(NF$1))"
118 else
119 field="\$$(($1 + 1))"
120 fi
121 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
122 done
123}
124
125
Dean Troyer7e270512012-06-14 15:23:24 -0500126# get_packages() collects a list of package names of any type from the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500127# prerequisite files in ``files/{apts|rpms}``. The list is intended
128# to be passed to a package installer such as apt or yum.
Dean Troyer7e270512012-06-14 15:23:24 -0500129#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500130# Only packages required for the services in ``ENABLED_SERVICES`` will be
Dean Troyer7e270512012-06-14 15:23:24 -0500131# included. Two bits of metadata are recognized in the prerequisite files:
132# - ``# NOPRIME`` defers installation to be performed later in stack.sh
133# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
134# of the package to the distros listed. The distro names are case insensitive.
135#
Vincent Untz855c5872012-10-04 13:36:46 +0200136# Uses globals ``ENABLED_SERVICES``
Dean Troyer7e270512012-06-14 15:23:24 -0500137# get_packages dir
138function get_packages() {
139 local package_dir=$1
140 local file_to_parse
141 local service
142
143 if [[ -z "$package_dir" ]]; then
144 echo "No package directory supplied"
145 return 1
146 fi
147 if [[ -z "$DISTRO" ]]; then
Vincent Untz855c5872012-10-04 13:36:46 +0200148 GetDistro
Dean Troyer7e270512012-06-14 15:23:24 -0500149 fi
150 for service in general ${ENABLED_SERVICES//,/ }; do
151 # Allow individual services to specify dependencies
152 if [[ -e ${package_dir}/${service} ]]; then
153 file_to_parse="${file_to_parse} $service"
154 fi
155 # NOTE(sdague) n-api needs glance for now because that's where
156 # glance client is
157 if [[ $service == n-api ]]; then
158 if [[ ! $file_to_parse =~ nova ]]; then
159 file_to_parse="${file_to_parse} nova"
160 fi
161 if [[ ! $file_to_parse =~ glance ]]; then
162 file_to_parse="${file_to_parse} glance"
163 fi
164 elif [[ $service == c-* ]]; then
165 if [[ ! $file_to_parse =~ cinder ]]; then
166 file_to_parse="${file_to_parse} cinder"
167 fi
John H. Tran93361642012-07-26 11:22:05 -0700168 elif [[ $service == ceilometer-* ]]; then
169 if [[ ! $file_to_parse =~ ceilometer ]]; then
170 file_to_parse="${file_to_parse} ceilometer"
171 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500172 elif [[ $service == n-* ]]; then
173 if [[ ! $file_to_parse =~ nova ]]; then
174 file_to_parse="${file_to_parse} nova"
175 fi
176 elif [[ $service == g-* ]]; then
177 if [[ ! $file_to_parse =~ glance ]]; then
178 file_to_parse="${file_to_parse} glance"
179 fi
180 elif [[ $service == key* ]]; then
181 if [[ ! $file_to_parse =~ keystone ]]; then
182 file_to_parse="${file_to_parse} keystone"
183 fi
Robert Collins0a9954f2012-11-20 11:34:25 +1300184 elif [[ $service == q-* ]]; then
185 if [[ ! $file_to_parse =~ quantum ]]; then
186 file_to_parse="${file_to_parse} quantum"
187 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500188 fi
189 done
190
191 for file in ${file_to_parse}; do
192 local fname=${package_dir}/${file}
193 local OIFS line package distros distro
194 [[ -e $fname ]] || continue
195
196 OIFS=$IFS
197 IFS=$'\n'
198 for line in $(<${fname}); do
199 if [[ $line =~ "NOPRIME" ]]; then
200 continue
201 fi
202
203 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
204 # We are using BASH regexp matching feature.
205 package=${BASH_REMATCH[1]}
206 distros=${BASH_REMATCH[2]}
207 # In bash ${VAR,,} will lowecase VAR
208 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
209 continue
210 fi
211
212 echo ${line%#*}
213 done
214 IFS=$OIFS
215 done
216}
217
218
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500219# Determine OS Vendor, Release and Update
220# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
221# Returns results in global variables:
222# os_VENDOR - vendor name
223# os_RELEASE - release
224# os_UPDATE - update
225# os_PACKAGE - package type
226# os_CODENAME - vendor's codename for release
227# GetOSVersion
228GetOSVersion() {
229 # Figure out which vendor we are
230 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
231 # OS/X
232 os_VENDOR=`sw_vers -productName`
233 os_RELEASE=`sw_vers -productVersion`
234 os_UPDATE=${os_RELEASE##*.}
235 os_RELEASE=${os_RELEASE%.*}
236 os_PACKAGE=""
237 if [[ "$os_RELEASE" =~ "10.7" ]]; then
238 os_CODENAME="lion"
239 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
240 os_CODENAME="snow leopard"
241 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
242 os_CODENAME="leopard"
243 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
244 os_CODENAME="tiger"
245 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
246 os_CODENAME="panther"
247 else
248 os_CODENAME=""
249 fi
250 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
251 os_VENDOR=$(lsb_release -i -s)
252 os_RELEASE=$(lsb_release -r -s)
253 os_UPDATE=""
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100254 os_PACKAGE="rpm"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500255 if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
256 os_PACKAGE="deb"
Vincent Untz856a11e2012-11-21 16:04:12 +0100257 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
258 lsb_release -d -s | grep -q openSUSE
259 if [[ $? -eq 0 ]]; then
260 os_VENDOR="openSUSE"
261 fi
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100262 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
263 os_VENDOR="Red Hat"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500264 fi
265 os_CODENAME=$(lsb_release -c -s)
266 elif [[ -r /etc/redhat-release ]]; then
267 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
268 # CentOS release 5.5 (Final)
269 # CentOS Linux release 6.0 (Final)
270 # Fedora release 16 (Verne)
271 os_CODENAME=""
272 for r in "Red Hat" CentOS Fedora; do
273 os_VENDOR=$r
274 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
275 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
276 os_CODENAME=${ver#*|}
277 os_RELEASE=${ver%|*}
278 os_UPDATE=${os_RELEASE##*.}
279 os_RELEASE=${os_RELEASE%.*}
280 break
281 fi
282 os_VENDOR=""
283 done
284 os_PACKAGE="rpm"
Vincent Untz856a11e2012-11-21 16:04:12 +0100285 elif [[ -r /etc/SuSE-release ]]; then
286 for r in openSUSE "SUSE Linux"; do
287 if [[ "$r" = "SUSE Linux" ]]; then
288 os_VENDOR="SUSE LINUX"
289 else
290 os_VENDOR=$r
291 fi
292
293 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
294 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
295 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
296 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
297 break
298 fi
299 os_VENDOR=""
300 done
301 os_PACKAGE="rpm"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500302 fi
303 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
304}
305
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300306# git update using reference as a branch.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500307# git_update_branch ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300308function git_update_branch() {
309
310 GIT_BRANCH=$1
311
312 git checkout -f origin/$GIT_BRANCH
313 # a local branch might not exist
314 git branch -D $GIT_BRANCH || true
315 git checkout -b $GIT_BRANCH
316}
317
318
319# git update using reference as a tag. Be careful editing source at that repo
320# as working copy will be in a detached mode
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500321# git_update_tag ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300322function git_update_tag() {
323
324 GIT_TAG=$1
325
326 git tag -d $GIT_TAG
327 # fetching given tag only
328 git fetch origin tag $GIT_TAG
329 git checkout -f $GIT_TAG
330}
331
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500332
Andrew Laskif900bd72012-09-05 17:23:14 -0400333# git update using reference as a branch.
334# git_update_remote_branch ref
335function git_update_remote_branch() {
336
337 GIT_BRANCH=$1
338
339 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
340}
341
342
Dean Troyera9e0a482012-07-09 14:07:23 -0500343# Translate the OS version values into common nomenclature
344# Sets ``DISTRO`` from the ``os_*`` values
345function GetDistro() {
346 GetOSVersion
347 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
348 # 'Everyone' refers to Ubuntu releases by the code name adjective
349 DISTRO=$os_CODENAME
350 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
351 # For Fedora, just use 'f' and the release
352 DISTRO="f$os_RELEASE"
Vincent Untz856a11e2012-11-21 16:04:12 +0100353 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
354 DISTRO="opensuse-$os_RELEASE"
355 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
356 # For SLE, also use the service pack
357 if [[ -z "$os_UPDATE" ]]; then
358 DISTRO="sle${os_RELEASE}"
359 else
360 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
361 fi
Dean Troyera9e0a482012-07-09 14:07:23 -0500362 else
363 # Catch-all for now is Vendor + Release + Update
364 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
365 fi
366 export DISTRO
367}
368
369
Vincent Untzc18b9652012-12-04 12:36:34 +0100370# Determine if current distribution is an Ubuntu-based distribution.
371# It will also detect non-Ubuntu but Debian-based distros; this is not an issue
372# since Debian and Ubuntu should be compatible.
373# is_ubuntu
374function is_ubuntu {
375 if [[ -z "$os_PACKAGE" ]]; then
376 GetOSVersion
377 fi
378
379 [ "$os_PACKAGE" = "deb" ]
380}
381
382
Vincent Untz00011c02012-12-06 09:56:32 +0100383# Determine if current distribution is a Fedora-based distribution
384# (Fedora, RHEL, CentOS).
385# is_fedora
386function is_fedora {
387 if [[ -z "$os_VENDOR" ]]; then
388 GetOSVersion
389 fi
390
391 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ]
392}
393
394
Vincent Untz856a11e2012-11-21 16:04:12 +0100395# Determine if current distribution is a SUSE-based distribution
396# (openSUSE, SLE).
397# is_suse
398function is_suse {
399 if [[ -z "$os_VENDOR" ]]; then
400 GetOSVersion
401 fi
402
Steve Baker1a7bbd22012-12-03 17:04:02 +1300403 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
Vincent Untz856a11e2012-11-21 16:04:12 +0100404}
405
406
Vincent Untz00011c02012-12-06 09:56:32 +0100407# Exit after outputting a message about the distribution not being supported.
408# exit_distro_not_supported [optional-string-telling-what-is-missing]
409function exit_distro_not_supported {
410 if [[ -z "$DISTRO" ]]; then
411 GetDistro
412 fi
413
414 if [ $# -gt 0 ]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800415 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
Vincent Untz00011c02012-12-06 09:56:32 +0100416 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800417 die $LINENO "Support for $DISTRO is incomplete."
Vincent Untz00011c02012-12-06 09:56:32 +0100418 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100419}
420
421
Dean Troyer7f9aa712012-01-31 12:11:56 -0600422# git clone only if directory doesn't exist already. Since ``DEST`` might not
423# be owned by the installation user, we create the directory and change the
424# ownership to the proper user.
425# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700426# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
427# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500428# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600429# git_clone remote dest-dir branch
430function git_clone {
431 [[ "$OFFLINE" = "True" ]] && return
432
433 GIT_REMOTE=$1
434 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300435 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600436
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300437 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600438 # If our branch name is a gerrit style refs/changes/...
439 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700440 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600441 git clone $GIT_REMOTE $GIT_DEST
442 fi
443 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300444 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600445 else
446 # do a full clone only if the directory doesn't exist
447 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700448 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600449 git clone $GIT_REMOTE $GIT_DEST
450 cd $GIT_DEST
451 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300452 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600453 elif [[ "$RECLONE" == "yes" ]]; then
454 # if it does exist then simulate what clone does if asked to RECLONE
455 cd $GIT_DEST
456 # set the url to pull from and fetch
457 git remote set-url origin $GIT_REMOTE
458 git fetch origin
459 # remove the existing ignored files (like pyc) as they cause breakage
460 # (due to the py files having older timestamps than our pyc, so python
461 # thinks the pyc files are correct using them)
462 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300463
464 # handle GIT_REF accordingly to type (tag, branch)
465 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
466 git_update_tag $GIT_REF
467 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
468 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400469 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
470 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300471 else
472 echo $GIT_REF is neither branch nor tag
473 exit 1
474 fi
475
Dean Troyer7f9aa712012-01-31 12:11:56 -0600476 fi
477 fi
478}
479
480
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500481# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200482# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500483function inicomment() {
484 local file=$1
485 local section=$2
486 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100487 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500488}
489
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200490# Uncomment an option in an INI file
491# iniuncomment config-file section option
492function iniuncomment() {
493 local file=$1
494 local section=$2
495 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100496 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200497}
498
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500499
500# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500501# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500502function iniget() {
503 local file=$1
504 local section=$2
505 local option=$3
506 local line
Attila Fazekas588eb412012-12-20 10:57:16 +0100507 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500508 echo ${line#*=}
509}
510
Attila Fazekas588eb412012-12-20 10:57:16 +0100511# Determinate is the given option present in the INI file
512# ini_has_option config-file section option
513function ini_has_option() {
514 local file=$1
515 local section=$2
516 local option=$3
517 local line
518 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
519 [ -n "$line" ]
520}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500521
522# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500523# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500524function iniset() {
525 local file=$1
526 local section=$2
527 local option=$3
528 local value=$4
Attila Fazekas588eb412012-12-20 10:57:16 +0100529 if ! grep -q "^\[$section\]" "$file"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500530 # Add section at the end
Attila Fazekas588eb412012-12-20 10:57:16 +0100531 echo -e "\n[$section]" >>"$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500532 fi
Attila Fazekas588eb412012-12-20 10:57:16 +0100533 if ! ini_has_option "$file" "$section" "$option"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500534 # Add it
Attila Fazekas588eb412012-12-20 10:57:16 +0100535 sed -i -e "/^\[$section\]/ a\\
Dean Troyer09e636e2012-03-19 16:31:12 -0500536$option = $value
Attila Fazekas588eb412012-12-20 10:57:16 +0100537" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500538 else
539 # Replace it
Attila Fazekas588eb412012-12-20 10:57:16 +0100540 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500541 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500542}
543
544
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000545# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500546# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000547#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500548# Multiple services specified as arguments are ``OR``'ed together; the test
549# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000550#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500551# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000552# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500553# **cinder** returns true if any service enabled start with **c-**
554# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000555# **glance** returns true if any service enabled start with **g-**
556# **quantum** returns true if any service enabled start with **q-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500557#
558# Uses global ``ENABLED_SERVICES``
559# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000560function is_service_enabled() {
561 services=$@
562 for service in ${services}; do
563 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
564 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500565 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700566 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000567 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
568 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
569 done
570 return 1
571}
572
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500573
574# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
575# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400576function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500577 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400578 s/,,/,/g;
579 s/^,//;
580 s/,$//
581 '
582}
583
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500584
Doug Hellmannf04178f2012-07-05 17:10:03 -0400585# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500586# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400587#
588# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800589# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400590#
591# This function does not know about the special cases
592# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500593# Uses global ``ENABLED_SERVICES``
594# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400595function enable_service() {
596 local tmpsvcs="${ENABLED_SERVICES}"
597 for service in $@; do
598 if ! is_service_enabled $service; then
599 tmpsvcs+=",$service"
600 fi
601 done
602 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
603 disable_negated_services
604}
605
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500606
Doug Hellmannf04178f2012-07-05 17:10:03 -0400607# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500608# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400609#
610# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800611# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400612#
613# This function does not know about the special cases
614# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500615# Uses global ``ENABLED_SERVICES``
616# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400617function disable_service() {
618 local tmpsvcs=",${ENABLED_SERVICES},"
619 local service
620 for service in $@; do
621 if is_service_enabled $service; then
622 tmpsvcs=${tmpsvcs//,$service,/,}
623 fi
624 done
625 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
626}
627
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500628
Doug Hellmannf04178f2012-07-05 17:10:03 -0400629# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500630# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400631# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500632# Uses global ``ENABLED_SERVICES``
633# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400634function disable_all_services() {
635 ENABLED_SERVICES=""
636}
637
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500638
639# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800640# services except rabbit (rabbit) set in ``localrc``:
641# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500642# Uses global ``ENABLED_SERVICES``
643# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400644function disable_negated_services() {
645 local tmpsvcs="${ENABLED_SERVICES}"
646 local service
647 for service in ${tmpsvcs//,/ }; do
648 if [[ ${service} == -* ]]; then
649 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
650 fi
651 done
652 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
653}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600654
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500655
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500656# Distro-agnostic package installer
657# install_package package [package ...]
658function install_package() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100659 if is_ubuntu; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200660 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
661 NO_UPDATE_REPOS=True
662
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500663 apt_get install "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100664 elif is_fedora; then
665 yum_install "$@"
666 elif is_suse; then
667 zypper_install "$@"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500668 else
Vincent Untz00011c02012-12-06 09:56:32 +0100669 exit_distro_not_supported "installing packages"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500670 fi
671}
672
673
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200674# Distro-agnostic function to tell if a package is installed
675# is_package_installed package [package ...]
676function is_package_installed() {
677 if [[ -z "$@" ]]; then
678 return 1
679 fi
680
681 if [[ -z "$os_PACKAGE" ]]; then
682 GetOSVersion
683 fi
Vincent Untzc18b9652012-12-04 12:36:34 +0100684
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200685 if [[ "$os_PACKAGE" = "deb" ]]; then
686 dpkg -l "$@" > /dev/null
Vincent Untz00011c02012-12-06 09:56:32 +0100687 elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200688 rpm --quiet -q "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100689 else
690 exit_distro_not_supported "finding if a package is installed"
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200691 fi
692}
693
694
Dean Troyer489bd2a2012-03-02 10:44:29 -0600695# Test if the named environment variable is set and not zero length
696# is_set env-var
697function is_set() {
698 local var=\$"$1"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100699 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 -0600700}
701
702
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500703# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +0000704# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
705# ``TRACK_DEPENDS``, ``*_proxy`
Dean Troyer7f9aa712012-01-31 12:11:56 -0600706# pip_install package [package ...]
707function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600708 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500709 if [[ -z "$os_PACKAGE" ]]; then
710 GetOSVersion
711 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500712 if [[ $TRACK_DEPENDS = True ]] ; then
713 source $DEST/.venv/bin/activate
714 CMD_PIP=$DEST/.venv/bin/pip
715 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500716 else
Monty Taylor47f02062012-07-26 11:09:24 -0500717 SUDO_PIP="sudo"
Vincent Untz8ec27222012-11-29 09:25:31 +0100718 CMD_PIP=$(get_pip_command)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500719 fi
Maru Newby3a87edd2012-10-25 23:01:06 +0000720 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
721 PIP_MIRROR_OPT="--use-mirrors"
722 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500723 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600724 HTTP_PROXY=$http_proxy \
725 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900726 NO_PROXY=$no_proxy \
Maru Newby3a87edd2012-10-25 23:01:06 +0000727 $CMD_PIP install $PIP_MIRROR_OPT $@
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500728}
729
730
731# Service wrapper to restart services
732# restart_service service-name
733function restart_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100734 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600735 sudo /usr/sbin/service $1 restart
736 else
737 sudo /sbin/service $1 restart
738 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500739}
740
741
Dean Troyer15733352012-09-06 11:51:30 -0500742# Helper to launch a service in a named screen
743# screen_it service "command-line"
744function screen_it {
Dean Troyer15733352012-09-06 11:51:30 -0500745 SCREEN_NAME=${SCREEN_NAME:-stack}
jiajun xua9414242012-12-06 16:30:57 +0800746 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Vishvananda Ishaya58e21342013-02-11 16:48:12 -0800747 SCREEN_DEV=`trueorfalse True $SCREEN_DEV`
jiajun xua9414242012-12-06 16:30:57 +0800748
Dean Troyer15733352012-09-06 11:51:30 -0500749 if is_service_enabled $1; then
750 # Append the service to the screen rc file
751 screen_rc "$1" "$2"
752
753 screen -S $SCREEN_NAME -X screen -t $1
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +0000754
755 if [[ -n ${SCREEN_LOGDIR} ]]; then
756 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
757 screen -S $SCREEN_NAME -p $1 -X log on
758 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
759 fi
760
Vishvananda Ishaya58e21342013-02-11 16:48:12 -0800761 if [[ "$SCREEN_DEV" = "True" ]]; then
762 # sleep to allow bash to be ready to be send the command - we are
763 # creating a new window in screen and then sends characters, so if
764 # bash isn't running by the time we send the command, nothing happens
765 sleep 1.5
Dean Troyer15733352012-09-06 11:51:30 -0500766
Vishvananda Ishaya58e21342013-02-11 16:48:12 -0800767 NL=`echo -ne '\015'`
768 screen -S $SCREEN_NAME -p $1 -X stuff "$2 || touch \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
769 else
770 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 -0500771 fi
Dean Troyer15733352012-09-06 11:51:30 -0500772 fi
773}
774
775
776# Screen rc file builder
777# screen_rc service "command-line"
778function screen_rc {
779 SCREEN_NAME=${SCREEN_NAME:-stack}
780 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
781 if [[ ! -e $SCREENRC ]]; then
782 # Name the screen session
783 echo "sessionname $SCREEN_NAME" > $SCREENRC
784 # Set a reasonable statusbar
785 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
786 echo "screen -t shell bash" >> $SCREENRC
787 fi
788 # If this service doesn't already exist in the screenrc file
789 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
790 NL=`echo -ne '\015'`
791 echo "screen -t $1 bash" >> $SCREENRC
792 echo "stuff \"$2$NL\"" >> $SCREENRC
793 fi
794}
795
jiajun xua9414242012-12-06 16:30:57 +0800796# Helper to remove the *.failure files under $SERVICE_DIR/$SCREEN_NAME
797# This is used for service_check when all the screen_it are called finished
798# init_service_check
799function init_service_check() {
800 SCREEN_NAME=${SCREEN_NAME:-stack}
801 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
802
803 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
804 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
805 fi
806
807 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
808}
809
810# Helper to get the status of each running service
811# service_check
812function service_check() {
813 local service
814 local failures
815 SCREEN_NAME=${SCREEN_NAME:-stack}
816 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
817
818
819 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
820 echo "No service status directory found"
821 return
822 fi
823
824 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
825 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null`
826
827 for service in $failures; do
828 service=`basename $service`
829 service=${service::-8}
830 echo "Error: Service $service is not running"
831 done
832
833 if [ -n "$failures" ]; then
834 echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
835 fi
836}
Dean Troyer15733352012-09-06 11:51:30 -0500837
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500838# ``pip install`` the dependencies of the package before ``setup.py develop``
839# so pip and not distutils processes the dependency chain
840# Uses globals ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500841# setup_develop directory
842function setup_develop() {
Monty Taylor47f02062012-07-26 11:09:24 -0500843 if [[ $TRACK_DEPENDS = True ]] ; then
844 SUDO_CMD="env"
845 else
846 SUDO_CMD="sudo"
847 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500848 (cd $1; \
849 python setup.py egg_info; \
850 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
851 depend_links=$(echo $raw_links | xargs); \
Dean Troyer1a3c9fe2012-09-29 17:25:02 -0500852 require_file=$([ ! -r *-info/requires.txt ] || echo "-r *-info/requires.txt"); \
853 pip_install $require_file $depend_links; \
Monty Taylor47f02062012-07-26 11:09:24 -0500854 $SUDO_CMD \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500855 HTTP_PROXY=$http_proxy \
856 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900857 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500858 python setup.py develop \
859 )
860}
861
862
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500863# Service wrapper to start services
864# start_service service-name
865function start_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100866 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600867 sudo /usr/sbin/service $1 start
868 else
869 sudo /sbin/service $1 start
870 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500871}
872
873
874# Service wrapper to stop services
875# stop_service service-name
876function stop_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100877 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600878 sudo /usr/sbin/service $1 stop
879 else
880 sudo /sbin/service $1 stop
881 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600882}
883
884
885# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500886# Accepts as False: 0 no false False FALSE
887# Accepts as True: 1 yes true True TRUE
888# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -0600889function trueorfalse() {
890 local default=$1
891 local testval=$2
892
893 [[ -z "$testval" ]] && { echo "$default"; return; }
894 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
895 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
896 echo "$default"
897}
Dean Troyer27e32692012-03-16 16:16:56 -0500898
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500899
Dean Troyerca0e3d02012-04-13 15:58:37 -0500900# Retrieve an image from a URL and upload into Glance
901# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500902# ``FILES`` must be set to the cache dir
903# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -0500904# upload_image image-url glance-token
905function upload_image() {
906 local image_url=$1
907 local token=$2
908
909 # Create a directory for the downloaded image tarballs.
910 mkdir -p $FILES/images
911
912 # Downloads the image (uec ami+aki style), then extracts it.
913 IMAGE_FNAME=`basename "$image_url"`
914 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
915 wget -c $image_url -O $FILES/$IMAGE_FNAME
916 if [[ $? -ne 0 ]]; then
917 echo "Not found: $image_url"
918 return
919 fi
920 fi
921
922 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
923 if [[ "$image_url" =~ 'openvz' ]]; then
924 IMAGE="$FILES/${IMAGE_FNAME}"
925 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
926 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}"
927 return
928 fi
929
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500930 # XenServer-ovf-format images are provided as .vhd.tgz as well
931 # and should not be decompressed prior to loading
932 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
933 IMAGE="$FILES/${IMAGE_FNAME}"
934 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
935 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}"
936 return
937 fi
938
Dean Troyerca0e3d02012-04-13 15:58:37 -0500939 KERNEL=""
940 RAMDISK=""
941 DISK_FORMAT=""
942 CONTAINER_FORMAT=""
943 UNPACK=""
944 case "$IMAGE_FNAME" in
945 *.tar.gz|*.tgz)
946 # Extract ami and aki files
947 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
948 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
949 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
950 xdir="$FILES/images/$IMAGE_NAME"
951 rm -Rf "$xdir";
952 mkdir "$xdir"
953 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
954 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
955 [ -f "$f" ] && echo "$f" && break; done; true)
956 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
957 [ -f "$f" ] && echo "$f" && break; done; true)
958 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
959 [ -f "$f" ] && echo "$f" && break; done; true)
960 if [[ -z "$IMAGE_NAME" ]]; then
961 IMAGE_NAME=$(basename "$IMAGE" ".img")
962 fi
963 ;;
964 *.img)
965 IMAGE="$FILES/$IMAGE_FNAME";
966 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -0500967 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
968 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
969 DISK_FORMAT=$format
970 else
971 DISK_FORMAT=raw
972 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -0500973 CONTAINER_FORMAT=bare
974 ;;
975 *.img.gz)
976 IMAGE="$FILES/${IMAGE_FNAME}"
977 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
978 DISK_FORMAT=raw
979 CONTAINER_FORMAT=bare
980 UNPACK=zcat
981 ;;
982 *.qcow2)
983 IMAGE="$FILES/${IMAGE_FNAME}"
984 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
985 DISK_FORMAT=qcow2
986 CONTAINER_FORMAT=bare
987 ;;
988 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
989 esac
990
991 if [ "$CONTAINER_FORMAT" = "bare" ]; then
992 if [ "$UNPACK" = "zcat" ]; then
993 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}")
994 else
995 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}"
996 fi
997 else
998 # Use glance client to add the kernel the root filesystem.
999 # We parse the results of the first upload to get the glance ID of the
1000 # kernel for use when uploading the root filesystem.
1001 KERNEL_ID=""; RAMDISK_ID="";
1002 if [ -n "$KERNEL" ]; then
1003 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)
1004 fi
1005 if [ -n "$RAMDISK" ]; then
1006 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)
1007 fi
1008 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}"
1009 fi
1010}
1011
Dean Troyerc1b486a2012-11-05 14:26:09 -06001012# Set the database backend to use
1013# When called from stackrc/localrc DATABASE_BACKENDS has not been
1014# initialized yet, just save the configuration selection and call back later
1015# to validate it.
1016# $1 The name of the database backend to use (mysql, postgresql, ...)
1017function use_database {
1018 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -06001019 # No backends registered means this is likely called from ``localrc``
1020 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -06001021 DATABASE_TYPE=$1
Attila Fazekas251d3b52012-12-16 15:05:44 +01001022 else
Dean Troyerafc29fe2013-02-07 15:56:24 -06001023 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +01001024 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -06001025 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -06001026}
1027
Terry Wilson428af5a2012-11-01 16:12:39 -04001028# Toggle enable/disable_service for services that must run exclusive of each other
1029# $1 The name of a variable containing a space-separated list of services
1030# $2 The name of a variable in which to store the enabled service's name
1031# $3 The name of the service to enable
1032function use_exclusive_service {
1033 local options=${!1}
1034 local selection=$3
1035 out=$2
1036 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1037 for opt in $options;do
1038 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1039 done
1040 eval "$out=$selection"
1041 return 0
1042}
Dean Troyerca0e3d02012-04-13 15:58:37 -05001043
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001044# Wait for an HTTP server to start answering requests
1045# wait_for_service timeout url
1046function wait_for_service() {
1047 local timeout=$1
1048 local url=$2
1049 timeout $timeout sh -c "while ! http_proxy= https_proxy= curl -s $url >/dev/null; do sleep 1; done"
1050}
1051
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001052# Wrapper for ``yum`` to set proxy environment variables
1053# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001054# yum_install package [package ...]
1055function yum_install() {
1056 [[ "$OFFLINE" = "True" ]] && return
1057 local sudo="sudo"
1058 [[ "$(id -u)" = "0" ]] && sudo="env"
1059 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001060 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001061 yum install -y "$@"
1062}
1063
Nachi Uenofda946e2012-10-24 17:26:02 -07001064# ping check
1065# Uses globals ``ENABLED_SERVICES``
1066function ping_check() {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001067 if is_service_enabled quantum; then
1068 _ping_check_quantum "$1" $2 $3 $4
1069 return
1070 fi
1071 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -07001072}
1073
1074# ping check for nova
1075# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
1076function _ping_check_novanet() {
1077 local from_net=$1
1078 local ip=$2
1079 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001080 local expected=${4:-"True"}
1081 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001082 MULTI_HOST=`trueorfalse False $MULTI_HOST`
1083 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
1084 sleep $boot_timeout
1085 return
1086 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001087 if [[ "$expected" = "True" ]]; then
1088 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
1089 else
1090 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
1091 fi
1092 if ! timeout $boot_timeout sh -c "$check_command"; then
1093 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -08001094 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001095 else
Nachi Ueno07115eb2013-02-26 12:38:18 -08001096 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001097 fi
Nachi Uenofda946e2012-10-24 17:26:02 -07001098 exit 1
1099 fi
1100}
1101
1102# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001103
Nachi Uenofda946e2012-10-24 17:26:02 -07001104function ssh_check() {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001105 if is_service_enabled quantum; then
1106 _ssh_check_quantum "$1" $2 $3 $4 $5
1107 return
1108 fi
1109 _ssh_check_novanet "$1" $2 $3 $4 $5
1110}
1111
1112function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -07001113 local NET_NAME=$1
1114 local KEY_FILE=$2
1115 local FLOATING_IP=$3
1116 local DEFAULT_INSTANCE_USER=$4
1117 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -06001118 local probe_cmd=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001119 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP echo success ; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -08001120 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -07001121 fi
1122}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001123
Vincent Untz856a11e2012-11-21 16:04:12 +01001124
1125# zypper wrapper to set arguments correctly
1126# zypper_install package [package ...]
1127function zypper_install() {
1128 [[ "$OFFLINE" = "True" ]] && return
1129 local sudo="sudo"
1130 [[ "$(id -u)" = "0" ]] && sudo="env"
1131 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1132 zypper --non-interactive install --auto-agree-with-licenses "$@"
1133}
1134
1135
1136# Add a user to a group.
1137# add_user_to_group user group
1138function add_user_to_group() {
1139 local user=$1
1140 local group=$2
1141
1142 if [[ -z "$os_VENDOR" ]]; then
1143 GetOSVersion
1144 fi
1145
1146 # SLE11 and openSUSE 12.2 don't have the usual usermod
1147 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1148 sudo usermod -a -G "$group" "$user"
1149 else
1150 sudo usermod -A "$group" "$user"
1151 fi
1152}
1153
1154
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001155# Get the path to the direcotry where python executables are installed.
1156# get_python_exec_prefix
1157function get_python_exec_prefix() {
1158 if is_fedora; then
1159 echo "/usr/bin"
1160 else
1161 echo "/usr/local/bin"
1162 fi
1163}
1164
Vincent Untz856a11e2012-11-21 16:04:12 +01001165# Get the location of the $module-rootwrap executables, where module is cinder
1166# or nova.
1167# get_rootwrap_location module
1168function get_rootwrap_location() {
1169 local module=$1
1170
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001171 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +01001172}
1173
Vincent Untz8ec27222012-11-29 09:25:31 +01001174# Get the path to the pip command.
1175# get_pip_command
1176function get_pip_command() {
Vincent Untz00011c02012-12-06 09:56:32 +01001177 if is_fedora; then
Nikhil Manchanda35138ed2013-01-03 17:49:58 -08001178 which pip-python
Vincent Untz00011c02012-12-06 09:56:32 +01001179 else
Nikhil Manchanda35138ed2013-01-03 17:49:58 -08001180 which pip
Vincent Untz8ec27222012-11-29 09:25:31 +01001181 fi
1182}
Vincent Untz856a11e2012-11-21 16:04:12 +01001183
Dean Troyer27e32692012-03-16 16:16:56 -05001184# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +00001185$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001186
1187
1188# Local variables:
1189# -*- mode: Shell-script -*-
Andrew Laskif900bd72012-09-05 17:23:14 -04001190# End: