blob: 16664d6ded2ae1d303c89e4d983394deedd6c541 [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
76 set +o xtrace
77 echo $@
78 exit -1
79 fi
80 )
Dean Troyer489bd2a2012-03-02 10:44:29 -060081}
82
83
84# Grab a numbered field from python prettytable output
85# Fields are numbered starting with 1
86# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
87# get_field field-number
88function get_field() {
89 while read data; do
90 if [ "$1" -lt 0 ]; then
91 field="(\$(NF$1))"
92 else
93 field="\$$(($1 + 1))"
94 fi
95 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
96 done
97}
98
99
Dean Troyer7e270512012-06-14 15:23:24 -0500100# get_packages() collects a list of package names of any type from the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500101# prerequisite files in ``files/{apts|rpms}``. The list is intended
102# to be passed to a package installer such as apt or yum.
Dean Troyer7e270512012-06-14 15:23:24 -0500103#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500104# Only packages required for the services in ``ENABLED_SERVICES`` will be
Dean Troyer7e270512012-06-14 15:23:24 -0500105# included. Two bits of metadata are recognized in the prerequisite files:
106# - ``# NOPRIME`` defers installation to be performed later in stack.sh
107# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
108# of the package to the distros listed. The distro names are case insensitive.
109#
Vincent Untz855c5872012-10-04 13:36:46 +0200110# Uses globals ``ENABLED_SERVICES``
Dean Troyer7e270512012-06-14 15:23:24 -0500111# get_packages dir
112function get_packages() {
113 local package_dir=$1
114 local file_to_parse
115 local service
116
117 if [[ -z "$package_dir" ]]; then
118 echo "No package directory supplied"
119 return 1
120 fi
121 if [[ -z "$DISTRO" ]]; then
Vincent Untz855c5872012-10-04 13:36:46 +0200122 GetDistro
Dean Troyer7e270512012-06-14 15:23:24 -0500123 fi
124 for service in general ${ENABLED_SERVICES//,/ }; do
125 # Allow individual services to specify dependencies
126 if [[ -e ${package_dir}/${service} ]]; then
127 file_to_parse="${file_to_parse} $service"
128 fi
129 # NOTE(sdague) n-api needs glance for now because that's where
130 # glance client is
131 if [[ $service == n-api ]]; then
132 if [[ ! $file_to_parse =~ nova ]]; then
133 file_to_parse="${file_to_parse} nova"
134 fi
135 if [[ ! $file_to_parse =~ glance ]]; then
136 file_to_parse="${file_to_parse} glance"
137 fi
138 elif [[ $service == c-* ]]; then
139 if [[ ! $file_to_parse =~ cinder ]]; then
140 file_to_parse="${file_to_parse} cinder"
141 fi
John H. Tran93361642012-07-26 11:22:05 -0700142 elif [[ $service == ceilometer-* ]]; then
143 if [[ ! $file_to_parse =~ ceilometer ]]; then
144 file_to_parse="${file_to_parse} ceilometer"
145 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500146 elif [[ $service == n-* ]]; then
147 if [[ ! $file_to_parse =~ nova ]]; then
148 file_to_parse="${file_to_parse} nova"
149 fi
150 elif [[ $service == g-* ]]; then
151 if [[ ! $file_to_parse =~ glance ]]; then
152 file_to_parse="${file_to_parse} glance"
153 fi
154 elif [[ $service == key* ]]; then
155 if [[ ! $file_to_parse =~ keystone ]]; then
156 file_to_parse="${file_to_parse} keystone"
157 fi
158 fi
159 done
160
161 for file in ${file_to_parse}; do
162 local fname=${package_dir}/${file}
163 local OIFS line package distros distro
164 [[ -e $fname ]] || continue
165
166 OIFS=$IFS
167 IFS=$'\n'
168 for line in $(<${fname}); do
169 if [[ $line =~ "NOPRIME" ]]; then
170 continue
171 fi
172
173 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
174 # We are using BASH regexp matching feature.
175 package=${BASH_REMATCH[1]}
176 distros=${BASH_REMATCH[2]}
177 # In bash ${VAR,,} will lowecase VAR
178 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
179 continue
180 fi
181
182 echo ${line%#*}
183 done
184 IFS=$OIFS
185 done
186}
187
188
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500189# Determine OS Vendor, Release and Update
190# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
191# Returns results in global variables:
192# os_VENDOR - vendor name
193# os_RELEASE - release
194# os_UPDATE - update
195# os_PACKAGE - package type
196# os_CODENAME - vendor's codename for release
197# GetOSVersion
198GetOSVersion() {
199 # Figure out which vendor we are
200 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
201 # OS/X
202 os_VENDOR=`sw_vers -productName`
203 os_RELEASE=`sw_vers -productVersion`
204 os_UPDATE=${os_RELEASE##*.}
205 os_RELEASE=${os_RELEASE%.*}
206 os_PACKAGE=""
207 if [[ "$os_RELEASE" =~ "10.7" ]]; then
208 os_CODENAME="lion"
209 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
210 os_CODENAME="snow leopard"
211 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
212 os_CODENAME="leopard"
213 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
214 os_CODENAME="tiger"
215 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
216 os_CODENAME="panther"
217 else
218 os_CODENAME=""
219 fi
220 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
221 os_VENDOR=$(lsb_release -i -s)
222 os_RELEASE=$(lsb_release -r -s)
223 os_UPDATE=""
224 if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
225 os_PACKAGE="deb"
Vincent Untz856a11e2012-11-21 16:04:12 +0100226 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
227 lsb_release -d -s | grep -q openSUSE
228 if [[ $? -eq 0 ]]; then
229 os_VENDOR="openSUSE"
230 fi
231 os_PACKAGE="rpm"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500232 else
233 os_PACKAGE="rpm"
234 fi
235 os_CODENAME=$(lsb_release -c -s)
236 elif [[ -r /etc/redhat-release ]]; then
237 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
238 # CentOS release 5.5 (Final)
239 # CentOS Linux release 6.0 (Final)
240 # Fedora release 16 (Verne)
241 os_CODENAME=""
242 for r in "Red Hat" CentOS Fedora; do
243 os_VENDOR=$r
244 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
245 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
246 os_CODENAME=${ver#*|}
247 os_RELEASE=${ver%|*}
248 os_UPDATE=${os_RELEASE##*.}
249 os_RELEASE=${os_RELEASE%.*}
250 break
251 fi
252 os_VENDOR=""
253 done
254 os_PACKAGE="rpm"
Vincent Untz856a11e2012-11-21 16:04:12 +0100255 elif [[ -r /etc/SuSE-release ]]; then
256 for r in openSUSE "SUSE Linux"; do
257 if [[ "$r" = "SUSE Linux" ]]; then
258 os_VENDOR="SUSE LINUX"
259 else
260 os_VENDOR=$r
261 fi
262
263 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
264 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
265 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
266 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
267 break
268 fi
269 os_VENDOR=""
270 done
271 os_PACKAGE="rpm"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500272 fi
273 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
274}
275
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300276# git update using reference as a branch.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500277# git_update_branch ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300278function git_update_branch() {
279
280 GIT_BRANCH=$1
281
282 git checkout -f origin/$GIT_BRANCH
283 # a local branch might not exist
284 git branch -D $GIT_BRANCH || true
285 git checkout -b $GIT_BRANCH
286}
287
288
289# git update using reference as a tag. Be careful editing source at that repo
290# as working copy will be in a detached mode
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500291# git_update_tag ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300292function git_update_tag() {
293
294 GIT_TAG=$1
295
296 git tag -d $GIT_TAG
297 # fetching given tag only
298 git fetch origin tag $GIT_TAG
299 git checkout -f $GIT_TAG
300}
301
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500302
Andrew Laskif900bd72012-09-05 17:23:14 -0400303# git update using reference as a branch.
304# git_update_remote_branch ref
305function git_update_remote_branch() {
306
307 GIT_BRANCH=$1
308
309 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
310}
311
312
Dean Troyera9e0a482012-07-09 14:07:23 -0500313# Translate the OS version values into common nomenclature
314# Sets ``DISTRO`` from the ``os_*`` values
315function GetDistro() {
316 GetOSVersion
317 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
318 # 'Everyone' refers to Ubuntu releases by the code name adjective
319 DISTRO=$os_CODENAME
320 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
321 # For Fedora, just use 'f' and the release
322 DISTRO="f$os_RELEASE"
Vincent Untz856a11e2012-11-21 16:04:12 +0100323 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
324 DISTRO="opensuse-$os_RELEASE"
325 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
326 # For SLE, also use the service pack
327 if [[ -z "$os_UPDATE" ]]; then
328 DISTRO="sle${os_RELEASE}"
329 else
330 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
331 fi
Dean Troyera9e0a482012-07-09 14:07:23 -0500332 else
333 # Catch-all for now is Vendor + Release + Update
334 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
335 fi
336 export DISTRO
337}
338
339
Vincent Untz856a11e2012-11-21 16:04:12 +0100340# Determine if current distribution is a SUSE-based distribution
341# (openSUSE, SLE).
342# is_suse
343function is_suse {
344 if [[ -z "$os_VENDOR" ]]; then
345 GetOSVersion
346 fi
347
348 [[ "$os_VENDOR" = "openSUSE" || "$os_VENDOR" = "SUSE LINUX" ]]
349 return $?
350}
351
352
Dean Troyer7f9aa712012-01-31 12:11:56 -0600353# git clone only if directory doesn't exist already. Since ``DEST`` might not
354# be owned by the installation user, we create the directory and change the
355# ownership to the proper user.
356# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700357# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
358# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500359# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600360# git_clone remote dest-dir branch
361function git_clone {
362 [[ "$OFFLINE" = "True" ]] && return
363
364 GIT_REMOTE=$1
365 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300366 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600367
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300368 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600369 # If our branch name is a gerrit style refs/changes/...
370 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700371 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600372 git clone $GIT_REMOTE $GIT_DEST
373 fi
374 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300375 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600376 else
377 # do a full clone only if the directory doesn't exist
378 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700379 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600380 git clone $GIT_REMOTE $GIT_DEST
381 cd $GIT_DEST
382 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300383 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600384 elif [[ "$RECLONE" == "yes" ]]; then
385 # if it does exist then simulate what clone does if asked to RECLONE
386 cd $GIT_DEST
387 # set the url to pull from and fetch
388 git remote set-url origin $GIT_REMOTE
389 git fetch origin
390 # remove the existing ignored files (like pyc) as they cause breakage
391 # (due to the py files having older timestamps than our pyc, so python
392 # thinks the pyc files are correct using them)
393 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300394
395 # handle GIT_REF accordingly to type (tag, branch)
396 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
397 git_update_tag $GIT_REF
398 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
399 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400400 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
401 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300402 else
403 echo $GIT_REF is neither branch nor tag
404 exit 1
405 fi
406
Dean Troyer7f9aa712012-01-31 12:11:56 -0600407 fi
408 fi
409}
410
411
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500412# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200413# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500414function inicomment() {
415 local file=$1
416 local section=$2
417 local option=$3
418 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
419}
420
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200421# Uncomment an option in an INI file
422# iniuncomment config-file section option
423function iniuncomment() {
424 local file=$1
425 local section=$2
426 local option=$3
427 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
428}
429
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500430
431# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500432# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500433function iniget() {
434 local file=$1
435 local section=$2
436 local option=$3
437 local line
438 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
439 echo ${line#*=}
440}
441
442
443# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500444# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500445function iniset() {
446 local file=$1
447 local section=$2
448 local option=$3
449 local value=$4
Dean Troyer09e636e2012-03-19 16:31:12 -0500450 if ! grep -q "^\[$section\]" $file; then
451 # Add section at the end
452 echo -e "\n[$section]" >>$file
453 fi
454 if [[ -z "$(iniget $file $section $option)" ]]; then
455 # Add it
456 sed -i -e "/^\[$section\]/ a\\
457$option = $value
458" $file
459 else
460 # Replace it
461 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
462 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500463}
464
465
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000466# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500467# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000468#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500469# Multiple services specified as arguments are ``OR``'ed together; the test
470# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000471#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500472# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000473# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500474# **cinder** returns true if any service enabled start with **c-**
475# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000476# **glance** returns true if any service enabled start with **g-**
477# **quantum** returns true if any service enabled start with **q-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500478#
479# Uses global ``ENABLED_SERVICES``
480# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000481function is_service_enabled() {
482 services=$@
483 for service in ${services}; do
484 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
485 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500486 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700487 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000488 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
489 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
490 done
491 return 1
492}
493
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500494
495# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
496# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400497function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500498 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400499 s/,,/,/g;
500 s/^,//;
501 s/,$//
502 '
503}
504
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500505
Doug Hellmannf04178f2012-07-05 17:10:03 -0400506# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500507# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400508#
509# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800510# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400511#
512# This function does not know about the special cases
513# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500514# Uses global ``ENABLED_SERVICES``
515# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400516function enable_service() {
517 local tmpsvcs="${ENABLED_SERVICES}"
518 for service in $@; do
519 if ! is_service_enabled $service; then
520 tmpsvcs+=",$service"
521 fi
522 done
523 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
524 disable_negated_services
525}
526
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500527
Doug Hellmannf04178f2012-07-05 17:10:03 -0400528# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500529# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400530#
531# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800532# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400533#
534# This function does not know about the special cases
535# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500536# Uses global ``ENABLED_SERVICES``
537# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400538function disable_service() {
539 local tmpsvcs=",${ENABLED_SERVICES},"
540 local service
541 for service in $@; do
542 if is_service_enabled $service; then
543 tmpsvcs=${tmpsvcs//,$service,/,}
544 fi
545 done
546 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
547}
548
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500549
Doug Hellmannf04178f2012-07-05 17:10:03 -0400550# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500551# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400552# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500553# Uses global ``ENABLED_SERVICES``
554# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400555function disable_all_services() {
556 ENABLED_SERVICES=""
557}
558
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500559
560# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800561# services except rabbit (rabbit) set in ``localrc``:
562# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500563# Uses global ``ENABLED_SERVICES``
564# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400565function disable_negated_services() {
566 local tmpsvcs="${ENABLED_SERVICES}"
567 local service
568 for service in ${tmpsvcs//,/ }; do
569 if [[ ${service} == -* ]]; then
570 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
571 fi
572 done
573 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
574}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600575
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500576
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500577# Distro-agnostic package installer
578# install_package package [package ...]
579function install_package() {
580 if [[ -z "$os_PACKAGE" ]]; then
581 GetOSVersion
582 fi
Vincent Untzc0482e62012-06-12 11:30:43 +0200583
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500584 if [[ "$os_PACKAGE" = "deb" ]]; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200585 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
586 NO_UPDATE_REPOS=True
587
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500588 apt_get install "$@"
589 else
Vincent Untz856a11e2012-11-21 16:04:12 +0100590 if is_suse; then
591 zypper_install "$@"
592 else
593 yum_install "$@"
594 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500595 fi
596}
597
598
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200599# Distro-agnostic function to tell if a package is installed
600# is_package_installed package [package ...]
601function is_package_installed() {
602 if [[ -z "$@" ]]; then
603 return 1
604 fi
605
606 if [[ -z "$os_PACKAGE" ]]; then
607 GetOSVersion
608 fi
609 if [[ "$os_PACKAGE" = "deb" ]]; then
610 dpkg -l "$@" > /dev/null
611 return $?
612 else
613 rpm --quiet -q "$@"
614 return $?
615 fi
616}
617
618
Dean Troyer489bd2a2012-03-02 10:44:29 -0600619# Test if the named environment variable is set and not zero length
620# is_set env-var
621function is_set() {
622 local var=\$"$1"
Dean Troyere88c0a22012-11-02 16:59:03 -0500623 if eval "[ -z \"$var\" ]"; then
Dean Troyer489bd2a2012-03-02 10:44:29 -0600624 return 1
625 fi
626 return 0
627}
628
629
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500630# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +0000631# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
632# ``TRACK_DEPENDS``, ``*_proxy`
Dean Troyer7f9aa712012-01-31 12:11:56 -0600633# pip_install package [package ...]
634function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600635 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500636 if [[ -z "$os_PACKAGE" ]]; then
637 GetOSVersion
638 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500639 if [[ $TRACK_DEPENDS = True ]] ; then
640 source $DEST/.venv/bin/activate
641 CMD_PIP=$DEST/.venv/bin/pip
642 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500643 else
Monty Taylor47f02062012-07-26 11:09:24 -0500644 SUDO_PIP="sudo"
Vincent Untz856a11e2012-11-21 16:04:12 +0100645 if [[ "$os_PACKAGE" = "deb" || is_suse ]]; then
Monty Taylor47f02062012-07-26 11:09:24 -0500646 CMD_PIP=/usr/bin/pip
647 else
648 CMD_PIP=/usr/bin/pip-python
649 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500650 fi
Maru Newby3a87edd2012-10-25 23:01:06 +0000651 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
652 PIP_MIRROR_OPT="--use-mirrors"
653 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500654 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600655 HTTP_PROXY=$http_proxy \
656 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900657 NO_PROXY=$no_proxy \
Maru Newby3a87edd2012-10-25 23:01:06 +0000658 $CMD_PIP install $PIP_MIRROR_OPT $@
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500659}
660
661
662# Service wrapper to restart services
663# restart_service service-name
664function restart_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600665 if [[ -z "$os_PACKAGE" ]]; then
666 GetOSVersion
667 fi
668 if [[ "$os_PACKAGE" = "deb" ]]; then
669 sudo /usr/sbin/service $1 restart
670 else
671 sudo /sbin/service $1 restart
672 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500673}
674
675
Dean Troyer15733352012-09-06 11:51:30 -0500676# Helper to launch a service in a named screen
677# screen_it service "command-line"
678function screen_it {
679 NL=`echo -ne '\015'`
680 SCREEN_NAME=${SCREEN_NAME:-stack}
681 if is_service_enabled $1; then
682 # Append the service to the screen rc file
683 screen_rc "$1" "$2"
684
685 screen -S $SCREEN_NAME -X screen -t $1
686 # sleep to allow bash to be ready to be send the command - we are
687 # creating a new window in screen and then sends characters, so if
688 # bash isn't running by the time we send the command, nothing happens
689 sleep 1.5
690
691 if [[ -n ${SCREEN_LOGDIR} ]]; then
692 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
693 screen -S $SCREEN_NAME -p $1 -X log on
694 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
695 fi
696 screen -S $SCREEN_NAME -p $1 -X stuff "$2$NL"
697 fi
698}
699
700
701# Screen rc file builder
702# screen_rc service "command-line"
703function screen_rc {
704 SCREEN_NAME=${SCREEN_NAME:-stack}
705 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
706 if [[ ! -e $SCREENRC ]]; then
707 # Name the screen session
708 echo "sessionname $SCREEN_NAME" > $SCREENRC
709 # Set a reasonable statusbar
710 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
711 echo "screen -t shell bash" >> $SCREENRC
712 fi
713 # If this service doesn't already exist in the screenrc file
714 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
715 NL=`echo -ne '\015'`
716 echo "screen -t $1 bash" >> $SCREENRC
717 echo "stuff \"$2$NL\"" >> $SCREENRC
718 fi
719}
720
721
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500722# ``pip install`` the dependencies of the package before ``setup.py develop``
723# so pip and not distutils processes the dependency chain
724# Uses globals ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500725# setup_develop directory
726function setup_develop() {
Monty Taylor47f02062012-07-26 11:09:24 -0500727 if [[ $TRACK_DEPENDS = True ]] ; then
728 SUDO_CMD="env"
729 else
730 SUDO_CMD="sudo"
731 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500732 (cd $1; \
733 python setup.py egg_info; \
734 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
735 depend_links=$(echo $raw_links | xargs); \
Dean Troyer1a3c9fe2012-09-29 17:25:02 -0500736 require_file=$([ ! -r *-info/requires.txt ] || echo "-r *-info/requires.txt"); \
737 pip_install $require_file $depend_links; \
Monty Taylor47f02062012-07-26 11:09:24 -0500738 $SUDO_CMD \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500739 HTTP_PROXY=$http_proxy \
740 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900741 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500742 python setup.py develop \
743 )
744}
745
746
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500747# Service wrapper to start services
748# start_service service-name
749function start_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600750 if [[ -z "$os_PACKAGE" ]]; then
751 GetOSVersion
752 fi
753 if [[ "$os_PACKAGE" = "deb" ]]; then
754 sudo /usr/sbin/service $1 start
755 else
756 sudo /sbin/service $1 start
757 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500758}
759
760
761# Service wrapper to stop services
762# stop_service service-name
763function stop_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600764 if [[ -z "$os_PACKAGE" ]]; then
765 GetOSVersion
766 fi
767 if [[ "$os_PACKAGE" = "deb" ]]; then
768 sudo /usr/sbin/service $1 stop
769 else
770 sudo /sbin/service $1 stop
771 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600772}
773
774
775# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500776# Accepts as False: 0 no false False FALSE
777# Accepts as True: 1 yes true True TRUE
778# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -0600779function trueorfalse() {
780 local default=$1
781 local testval=$2
782
783 [[ -z "$testval" ]] && { echo "$default"; return; }
784 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
785 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
786 echo "$default"
787}
Dean Troyer27e32692012-03-16 16:16:56 -0500788
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500789
Dean Troyerca0e3d02012-04-13 15:58:37 -0500790# Retrieve an image from a URL and upload into Glance
791# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500792# ``FILES`` must be set to the cache dir
793# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -0500794# upload_image image-url glance-token
795function upload_image() {
796 local image_url=$1
797 local token=$2
798
799 # Create a directory for the downloaded image tarballs.
800 mkdir -p $FILES/images
801
802 # Downloads the image (uec ami+aki style), then extracts it.
803 IMAGE_FNAME=`basename "$image_url"`
804 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
805 wget -c $image_url -O $FILES/$IMAGE_FNAME
806 if [[ $? -ne 0 ]]; then
807 echo "Not found: $image_url"
808 return
809 fi
810 fi
811
812 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
813 if [[ "$image_url" =~ 'openvz' ]]; then
814 IMAGE="$FILES/${IMAGE_FNAME}"
815 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
816 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}"
817 return
818 fi
819
820 KERNEL=""
821 RAMDISK=""
822 DISK_FORMAT=""
823 CONTAINER_FORMAT=""
824 UNPACK=""
825 case "$IMAGE_FNAME" in
826 *.tar.gz|*.tgz)
827 # Extract ami and aki files
828 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
829 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
830 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
831 xdir="$FILES/images/$IMAGE_NAME"
832 rm -Rf "$xdir";
833 mkdir "$xdir"
834 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
835 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
836 [ -f "$f" ] && echo "$f" && break; done; true)
837 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
838 [ -f "$f" ] && echo "$f" && break; done; true)
839 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
840 [ -f "$f" ] && echo "$f" && break; done; true)
841 if [[ -z "$IMAGE_NAME" ]]; then
842 IMAGE_NAME=$(basename "$IMAGE" ".img")
843 fi
844 ;;
845 *.img)
846 IMAGE="$FILES/$IMAGE_FNAME";
847 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -0500848 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
849 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
850 DISK_FORMAT=$format
851 else
852 DISK_FORMAT=raw
853 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -0500854 CONTAINER_FORMAT=bare
855 ;;
856 *.img.gz)
857 IMAGE="$FILES/${IMAGE_FNAME}"
858 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
859 DISK_FORMAT=raw
860 CONTAINER_FORMAT=bare
861 UNPACK=zcat
862 ;;
863 *.qcow2)
864 IMAGE="$FILES/${IMAGE_FNAME}"
865 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
866 DISK_FORMAT=qcow2
867 CONTAINER_FORMAT=bare
868 ;;
869 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
870 esac
871
872 if [ "$CONTAINER_FORMAT" = "bare" ]; then
873 if [ "$UNPACK" = "zcat" ]; then
874 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}")
875 else
876 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}"
877 fi
878 else
879 # Use glance client to add the kernel the root filesystem.
880 # We parse the results of the first upload to get the glance ID of the
881 # kernel for use when uploading the root filesystem.
882 KERNEL_ID=""; RAMDISK_ID="";
883 if [ -n "$KERNEL" ]; then
884 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)
885 fi
886 if [ -n "$RAMDISK" ]; then
887 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)
888 fi
889 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}"
890 fi
891}
892
Dean Troyerc1b486a2012-11-05 14:26:09 -0600893# Set the database backend to use
894# When called from stackrc/localrc DATABASE_BACKENDS has not been
895# initialized yet, just save the configuration selection and call back later
896# to validate it.
897# $1 The name of the database backend to use (mysql, postgresql, ...)
898function use_database {
899 if [[ -z "$DATABASE_BACKENDS" ]]; then
900 # The backends haven't initialized yet, just save the selection for now
901 DATABASE_TYPE=$1
902 return
903 fi
904 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1 && return 0
905 ret=$?
906 return $ret
907}
908
Terry Wilson428af5a2012-11-01 16:12:39 -0400909# Toggle enable/disable_service for services that must run exclusive of each other
910# $1 The name of a variable containing a space-separated list of services
911# $2 The name of a variable in which to store the enabled service's name
912# $3 The name of the service to enable
913function use_exclusive_service {
914 local options=${!1}
915 local selection=$3
916 out=$2
917 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
918 for opt in $options;do
919 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
920 done
921 eval "$out=$selection"
922 return 0
923}
Dean Troyerca0e3d02012-04-13 15:58:37 -0500924
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500925# Wrapper for ``yum`` to set proxy environment variables
926# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500927# yum_install package [package ...]
928function yum_install() {
929 [[ "$OFFLINE" = "True" ]] && return
930 local sudo="sudo"
931 [[ "$(id -u)" = "0" ]] && sudo="env"
932 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900933 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500934 yum install -y "$@"
935}
936
Nachi Uenofda946e2012-10-24 17:26:02 -0700937# ping check
938# Uses globals ``ENABLED_SERVICES``
939function ping_check() {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700940 if is_service_enabled quantum; then
941 _ping_check_quantum "$1" $2 $3 $4
942 return
943 fi
944 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -0700945}
946
947# ping check for nova
948# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
949function _ping_check_novanet() {
950 local from_net=$1
951 local ip=$2
952 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700953 local expected=${4:-"True"}
954 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -0700955 MULTI_HOST=`trueorfalse False $MULTI_HOST`
956 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
957 sleep $boot_timeout
958 return
959 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700960 if [[ "$expected" = "True" ]]; then
961 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
962 else
963 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
964 fi
965 if ! timeout $boot_timeout sh -c "$check_command"; then
966 if [[ "$expected" = "True" ]]; then
967 echo "[Fail] Couldn't ping server"
968 else
969 echo "[Fail] Could ping server"
970 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700971 exit 1
972 fi
973}
974
975# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700976
Nachi Uenofda946e2012-10-24 17:26:02 -0700977function ssh_check() {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700978 if is_service_enabled quantum; then
979 _ssh_check_quantum "$1" $2 $3 $4 $5
980 return
981 fi
982 _ssh_check_novanet "$1" $2 $3 $4 $5
983}
984
985function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -0700986 local NET_NAME=$1
987 local KEY_FILE=$2
988 local FLOATING_IP=$3
989 local DEFAULT_INSTANCE_USER=$4
990 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600991 local probe_cmd=""
Nachi Uenofda946e2012-10-24 17:26:02 -0700992 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
993 echo "server didn't become ssh-able!"
994 exit 1
995 fi
996}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500997
Vincent Untz856a11e2012-11-21 16:04:12 +0100998
999# zypper wrapper to set arguments correctly
1000# zypper_install package [package ...]
1001function zypper_install() {
1002 [[ "$OFFLINE" = "True" ]] && return
1003 local sudo="sudo"
1004 [[ "$(id -u)" = "0" ]] && sudo="env"
1005 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1006 zypper --non-interactive install --auto-agree-with-licenses "$@"
1007}
1008
1009
1010# Add a user to a group.
1011# add_user_to_group user group
1012function add_user_to_group() {
1013 local user=$1
1014 local group=$2
1015
1016 if [[ -z "$os_VENDOR" ]]; then
1017 GetOSVersion
1018 fi
1019
1020 # SLE11 and openSUSE 12.2 don't have the usual usermod
1021 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1022 sudo usermod -a -G "$group" "$user"
1023 else
1024 sudo usermod -A "$group" "$user"
1025 fi
1026}
1027
1028
1029# Get the location of the $module-rootwrap executables, where module is cinder
1030# or nova.
1031# get_rootwrap_location module
1032function get_rootwrap_location() {
1033 local module=$1
1034
1035 if [[ -z "$os_PACKAGE" ]]; then
1036 GetOSVersion
1037 fi
1038
1039 if [[ "$os_PACKAGE" = "deb" || is_suse ]]; then
1040 echo "/usr/local/bin/$module-rootwrap"
1041 else
1042 echo "/usr/bin/$module-rootwrap"
1043 fi
1044}
1045
1046
1047# Check if qpid can be used on the current distro.
1048# qpid_is_supported
1049function qpid_is_supported() {
1050 if [[ -z "$DISTRO" ]]; then
1051 GetDistro
1052 fi
1053
1054 # Qpid was introduced to Ubuntu in precise, disallow it on oneiric; it is
1055 # not in openSUSE either right now.
1056 [[ "$DISTRO" = "oneiric" || is_suse ]]
1057 return $?
1058}
1059
Dean Troyer27e32692012-03-16 16:16:56 -05001060# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +00001061$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001062
1063
1064# Local variables:
1065# -*- mode: Shell-script -*-
Andrew Laskif900bd72012-09-05 17:23:14 -04001066# End: