blob: bc6fdc354b474dabf7c5016d655d0e67a71f14d0 [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
Robert Collins0a9954f2012-11-20 11:34:25 +1300158 elif [[ $service == q-* ]]; then
159 if [[ ! $file_to_parse =~ quantum ]]; then
160 file_to_parse="${file_to_parse} quantum"
161 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500162 fi
163 done
164
165 for file in ${file_to_parse}; do
166 local fname=${package_dir}/${file}
167 local OIFS line package distros distro
168 [[ -e $fname ]] || continue
169
170 OIFS=$IFS
171 IFS=$'\n'
172 for line in $(<${fname}); do
173 if [[ $line =~ "NOPRIME" ]]; then
174 continue
175 fi
176
177 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
178 # We are using BASH regexp matching feature.
179 package=${BASH_REMATCH[1]}
180 distros=${BASH_REMATCH[2]}
181 # In bash ${VAR,,} will lowecase VAR
182 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
183 continue
184 fi
185
186 echo ${line%#*}
187 done
188 IFS=$OIFS
189 done
190}
191
192
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500193# Determine OS Vendor, Release and Update
194# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
195# Returns results in global variables:
196# os_VENDOR - vendor name
197# os_RELEASE - release
198# os_UPDATE - update
199# os_PACKAGE - package type
200# os_CODENAME - vendor's codename for release
201# GetOSVersion
202GetOSVersion() {
203 # Figure out which vendor we are
204 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
205 # OS/X
206 os_VENDOR=`sw_vers -productName`
207 os_RELEASE=`sw_vers -productVersion`
208 os_UPDATE=${os_RELEASE##*.}
209 os_RELEASE=${os_RELEASE%.*}
210 os_PACKAGE=""
211 if [[ "$os_RELEASE" =~ "10.7" ]]; then
212 os_CODENAME="lion"
213 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
214 os_CODENAME="snow leopard"
215 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
216 os_CODENAME="leopard"
217 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
218 os_CODENAME="tiger"
219 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
220 os_CODENAME="panther"
221 else
222 os_CODENAME=""
223 fi
224 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
225 os_VENDOR=$(lsb_release -i -s)
226 os_RELEASE=$(lsb_release -r -s)
227 os_UPDATE=""
228 if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
229 os_PACKAGE="deb"
Vincent Untz856a11e2012-11-21 16:04:12 +0100230 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
231 lsb_release -d -s | grep -q openSUSE
232 if [[ $? -eq 0 ]]; then
233 os_VENDOR="openSUSE"
234 fi
235 os_PACKAGE="rpm"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500236 else
237 os_PACKAGE="rpm"
238 fi
239 os_CODENAME=$(lsb_release -c -s)
240 elif [[ -r /etc/redhat-release ]]; then
241 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
242 # CentOS release 5.5 (Final)
243 # CentOS Linux release 6.0 (Final)
244 # Fedora release 16 (Verne)
245 os_CODENAME=""
246 for r in "Red Hat" CentOS Fedora; do
247 os_VENDOR=$r
248 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
249 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
250 os_CODENAME=${ver#*|}
251 os_RELEASE=${ver%|*}
252 os_UPDATE=${os_RELEASE##*.}
253 os_RELEASE=${os_RELEASE%.*}
254 break
255 fi
256 os_VENDOR=""
257 done
258 os_PACKAGE="rpm"
Vincent Untz856a11e2012-11-21 16:04:12 +0100259 elif [[ -r /etc/SuSE-release ]]; then
260 for r in openSUSE "SUSE Linux"; do
261 if [[ "$r" = "SUSE Linux" ]]; then
262 os_VENDOR="SUSE LINUX"
263 else
264 os_VENDOR=$r
265 fi
266
267 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
268 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
269 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
270 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
271 break
272 fi
273 os_VENDOR=""
274 done
275 os_PACKAGE="rpm"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500276 fi
277 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
278}
279
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300280# git update using reference as a branch.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500281# git_update_branch ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300282function git_update_branch() {
283
284 GIT_BRANCH=$1
285
286 git checkout -f origin/$GIT_BRANCH
287 # a local branch might not exist
288 git branch -D $GIT_BRANCH || true
289 git checkout -b $GIT_BRANCH
290}
291
292
293# git update using reference as a tag. Be careful editing source at that repo
294# as working copy will be in a detached mode
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500295# git_update_tag ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300296function git_update_tag() {
297
298 GIT_TAG=$1
299
300 git tag -d $GIT_TAG
301 # fetching given tag only
302 git fetch origin tag $GIT_TAG
303 git checkout -f $GIT_TAG
304}
305
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500306
Andrew Laskif900bd72012-09-05 17:23:14 -0400307# git update using reference as a branch.
308# git_update_remote_branch ref
309function git_update_remote_branch() {
310
311 GIT_BRANCH=$1
312
313 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
314}
315
316
Dean Troyera9e0a482012-07-09 14:07:23 -0500317# Translate the OS version values into common nomenclature
318# Sets ``DISTRO`` from the ``os_*`` values
319function GetDistro() {
320 GetOSVersion
321 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
322 # 'Everyone' refers to Ubuntu releases by the code name adjective
323 DISTRO=$os_CODENAME
324 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
325 # For Fedora, just use 'f' and the release
326 DISTRO="f$os_RELEASE"
Vincent Untz856a11e2012-11-21 16:04:12 +0100327 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
328 DISTRO="opensuse-$os_RELEASE"
329 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
330 # For SLE, also use the service pack
331 if [[ -z "$os_UPDATE" ]]; then
332 DISTRO="sle${os_RELEASE}"
333 else
334 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
335 fi
Dean Troyera9e0a482012-07-09 14:07:23 -0500336 else
337 # Catch-all for now is Vendor + Release + Update
338 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
339 fi
340 export DISTRO
341}
342
343
Vincent Untz856a11e2012-11-21 16:04:12 +0100344# Determine if current distribution is a SUSE-based distribution
345# (openSUSE, SLE).
346# is_suse
347function is_suse {
348 if [[ -z "$os_VENDOR" ]]; then
349 GetOSVersion
350 fi
351
Steve Baker1a7bbd22012-12-03 17:04:02 +1300352 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
Vincent Untz856a11e2012-11-21 16:04:12 +0100353}
354
355
Dean Troyer7f9aa712012-01-31 12:11:56 -0600356# git clone only if directory doesn't exist already. Since ``DEST`` might not
357# be owned by the installation user, we create the directory and change the
358# ownership to the proper user.
359# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700360# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
361# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500362# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600363# git_clone remote dest-dir branch
364function git_clone {
365 [[ "$OFFLINE" = "True" ]] && return
366
367 GIT_REMOTE=$1
368 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300369 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600370
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300371 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600372 # If our branch name is a gerrit style refs/changes/...
373 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700374 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600375 git clone $GIT_REMOTE $GIT_DEST
376 fi
377 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300378 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600379 else
380 # do a full clone only if the directory doesn't exist
381 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700382 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600383 git clone $GIT_REMOTE $GIT_DEST
384 cd $GIT_DEST
385 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300386 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600387 elif [[ "$RECLONE" == "yes" ]]; then
388 # if it does exist then simulate what clone does if asked to RECLONE
389 cd $GIT_DEST
390 # set the url to pull from and fetch
391 git remote set-url origin $GIT_REMOTE
392 git fetch origin
393 # remove the existing ignored files (like pyc) as they cause breakage
394 # (due to the py files having older timestamps than our pyc, so python
395 # thinks the pyc files are correct using them)
396 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300397
398 # handle GIT_REF accordingly to type (tag, branch)
399 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
400 git_update_tag $GIT_REF
401 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
402 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400403 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
404 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300405 else
406 echo $GIT_REF is neither branch nor tag
407 exit 1
408 fi
409
Dean Troyer7f9aa712012-01-31 12:11:56 -0600410 fi
411 fi
412}
413
414
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500415# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200416# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500417function inicomment() {
418 local file=$1
419 local section=$2
420 local option=$3
Dean Troyerafd472c2012-11-28 11:54:45 -0600421 sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500422}
423
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200424# Uncomment an option in an INI file
425# iniuncomment config-file section option
426function iniuncomment() {
427 local file=$1
428 local section=$2
429 local option=$3
Dean Troyerafd472c2012-11-28 11:54:45 -0600430 sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200431}
432
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500433
434# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500435# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500436function iniget() {
437 local file=$1
438 local section=$2
439 local option=$3
440 local line
Dean Troyere8335622012-11-27 17:00:11 -0600441 line=$(sed -ne "/^\[ *$section *\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500442 echo ${line#*=}
443}
444
445
446# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500447# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500448function iniset() {
449 local file=$1
450 local section=$2
451 local option=$3
452 local value=$4
Dean Troyere8335622012-11-27 17:00:11 -0600453 if ! grep -q "^\[ *$section *\]" $file; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500454 # Add section at the end
455 echo -e "\n[$section]" >>$file
456 fi
457 if [[ -z "$(iniget $file $section $option)" ]]; then
458 # Add it
Dean Troyerafd472c2012-11-28 11:54:45 -0600459 sed -i -e "/^\[ *$section *\]/ a\\
Dean Troyer09e636e2012-03-19 16:31:12 -0500460$option = $value
461" $file
462 else
463 # Replace it
Dean Troyerafd472c2012-11-28 11:54:45 -0600464 sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
Dean Troyer09e636e2012-03-19 16:31:12 -0500465 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500466}
467
468
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000469# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500470# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000471#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500472# Multiple services specified as arguments are ``OR``'ed together; the test
473# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000474#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500475# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000476# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500477# **cinder** returns true if any service enabled start with **c-**
478# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000479# **glance** returns true if any service enabled start with **g-**
480# **quantum** returns true if any service enabled start with **q-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500481#
482# Uses global ``ENABLED_SERVICES``
483# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000484function is_service_enabled() {
485 services=$@
486 for service in ${services}; do
487 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
488 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500489 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700490 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000491 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
492 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
493 done
494 return 1
495}
496
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500497
498# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
499# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400500function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500501 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400502 s/,,/,/g;
503 s/^,//;
504 s/,$//
505 '
506}
507
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500508
Doug Hellmannf04178f2012-07-05 17:10:03 -0400509# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500510# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400511#
512# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800513# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400514#
515# This function does not know about the special cases
516# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500517# Uses global ``ENABLED_SERVICES``
518# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400519function enable_service() {
520 local tmpsvcs="${ENABLED_SERVICES}"
521 for service in $@; do
522 if ! is_service_enabled $service; then
523 tmpsvcs+=",$service"
524 fi
525 done
526 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
527 disable_negated_services
528}
529
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500530
Doug Hellmannf04178f2012-07-05 17:10:03 -0400531# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500532# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400533#
534# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800535# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400536#
537# This function does not know about the special cases
538# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500539# Uses global ``ENABLED_SERVICES``
540# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400541function disable_service() {
542 local tmpsvcs=",${ENABLED_SERVICES},"
543 local service
544 for service in $@; do
545 if is_service_enabled $service; then
546 tmpsvcs=${tmpsvcs//,$service,/,}
547 fi
548 done
549 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
550}
551
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500552
Doug Hellmannf04178f2012-07-05 17:10:03 -0400553# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500554# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400555# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500556# Uses global ``ENABLED_SERVICES``
557# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400558function disable_all_services() {
559 ENABLED_SERVICES=""
560}
561
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500562
563# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800564# services except rabbit (rabbit) set in ``localrc``:
565# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500566# Uses global ``ENABLED_SERVICES``
567# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400568function disable_negated_services() {
569 local tmpsvcs="${ENABLED_SERVICES}"
570 local service
571 for service in ${tmpsvcs//,/ }; do
572 if [[ ${service} == -* ]]; then
573 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
574 fi
575 done
576 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
577}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600578
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500579
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500580# Distro-agnostic package installer
581# install_package package [package ...]
582function install_package() {
583 if [[ -z "$os_PACKAGE" ]]; then
584 GetOSVersion
585 fi
Vincent Untzc0482e62012-06-12 11:30:43 +0200586
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500587 if [[ "$os_PACKAGE" = "deb" ]]; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200588 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
589 NO_UPDATE_REPOS=True
590
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500591 apt_get install "$@"
592 else
Vincent Untz856a11e2012-11-21 16:04:12 +0100593 if is_suse; then
594 zypper_install "$@"
595 else
596 yum_install "$@"
597 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500598 fi
599}
600
601
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200602# Distro-agnostic function to tell if a package is installed
603# is_package_installed package [package ...]
604function is_package_installed() {
605 if [[ -z "$@" ]]; then
606 return 1
607 fi
608
609 if [[ -z "$os_PACKAGE" ]]; then
610 GetOSVersion
611 fi
612 if [[ "$os_PACKAGE" = "deb" ]]; then
613 dpkg -l "$@" > /dev/null
614 return $?
615 else
616 rpm --quiet -q "$@"
617 return $?
618 fi
619}
620
621
Dean Troyer489bd2a2012-03-02 10:44:29 -0600622# Test if the named environment variable is set and not zero length
623# is_set env-var
624function is_set() {
625 local var=\$"$1"
Dean Troyere88c0a22012-11-02 16:59:03 -0500626 if eval "[ -z \"$var\" ]"; then
Dean Troyer489bd2a2012-03-02 10:44:29 -0600627 return 1
628 fi
629 return 0
630}
631
632
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500633# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +0000634# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
635# ``TRACK_DEPENDS``, ``*_proxy`
Dean Troyer7f9aa712012-01-31 12:11:56 -0600636# pip_install package [package ...]
637function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600638 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500639 if [[ -z "$os_PACKAGE" ]]; then
640 GetOSVersion
641 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500642 if [[ $TRACK_DEPENDS = True ]] ; then
643 source $DEST/.venv/bin/activate
644 CMD_PIP=$DEST/.venv/bin/pip
645 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500646 else
Monty Taylor47f02062012-07-26 11:09:24 -0500647 SUDO_PIP="sudo"
Vincent Untz8ec27222012-11-29 09:25:31 +0100648 CMD_PIP=$(get_pip_command)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500649 fi
Maru Newby3a87edd2012-10-25 23:01:06 +0000650 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
651 PIP_MIRROR_OPT="--use-mirrors"
652 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500653 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600654 HTTP_PROXY=$http_proxy \
655 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900656 NO_PROXY=$no_proxy \
Maru Newby3a87edd2012-10-25 23:01:06 +0000657 $CMD_PIP install $PIP_MIRROR_OPT $@
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500658}
659
660
661# Service wrapper to restart services
662# restart_service service-name
663function restart_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600664 if [[ -z "$os_PACKAGE" ]]; then
665 GetOSVersion
666 fi
667 if [[ "$os_PACKAGE" = "deb" ]]; then
668 sudo /usr/sbin/service $1 restart
669 else
670 sudo /sbin/service $1 restart
671 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500672}
673
674
Dean Troyer15733352012-09-06 11:51:30 -0500675# Helper to launch a service in a named screen
676# screen_it service "command-line"
677function screen_it {
678 NL=`echo -ne '\015'`
679 SCREEN_NAME=${SCREEN_NAME:-stack}
680 if is_service_enabled $1; then
681 # Append the service to the screen rc file
682 screen_rc "$1" "$2"
683
684 screen -S $SCREEN_NAME -X screen -t $1
685 # sleep to allow bash to be ready to be send the command - we are
686 # creating a new window in screen and then sends characters, so if
687 # bash isn't running by the time we send the command, nothing happens
688 sleep 1.5
689
690 if [[ -n ${SCREEN_LOGDIR} ]]; then
691 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
692 screen -S $SCREEN_NAME -p $1 -X log on
693 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
694 fi
695 screen -S $SCREEN_NAME -p $1 -X stuff "$2$NL"
696 fi
697}
698
699
700# Screen rc file builder
701# screen_rc service "command-line"
702function screen_rc {
703 SCREEN_NAME=${SCREEN_NAME:-stack}
704 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
705 if [[ ! -e $SCREENRC ]]; then
706 # Name the screen session
707 echo "sessionname $SCREEN_NAME" > $SCREENRC
708 # Set a reasonable statusbar
709 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
710 echo "screen -t shell bash" >> $SCREENRC
711 fi
712 # If this service doesn't already exist in the screenrc file
713 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
714 NL=`echo -ne '\015'`
715 echo "screen -t $1 bash" >> $SCREENRC
716 echo "stuff \"$2$NL\"" >> $SCREENRC
717 fi
718}
719
720
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500721# ``pip install`` the dependencies of the package before ``setup.py develop``
722# so pip and not distutils processes the dependency chain
723# Uses globals ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500724# setup_develop directory
725function setup_develop() {
Monty Taylor47f02062012-07-26 11:09:24 -0500726 if [[ $TRACK_DEPENDS = True ]] ; then
727 SUDO_CMD="env"
728 else
729 SUDO_CMD="sudo"
730 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500731 (cd $1; \
732 python setup.py egg_info; \
733 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
734 depend_links=$(echo $raw_links | xargs); \
Dean Troyer1a3c9fe2012-09-29 17:25:02 -0500735 require_file=$([ ! -r *-info/requires.txt ] || echo "-r *-info/requires.txt"); \
736 pip_install $require_file $depend_links; \
Monty Taylor47f02062012-07-26 11:09:24 -0500737 $SUDO_CMD \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500738 HTTP_PROXY=$http_proxy \
739 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900740 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500741 python setup.py develop \
742 )
743}
744
745
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500746# Service wrapper to start services
747# start_service service-name
748function start_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600749 if [[ -z "$os_PACKAGE" ]]; then
750 GetOSVersion
751 fi
752 if [[ "$os_PACKAGE" = "deb" ]]; then
753 sudo /usr/sbin/service $1 start
754 else
755 sudo /sbin/service $1 start
756 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500757}
758
759
760# Service wrapper to stop services
761# stop_service service-name
762function stop_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600763 if [[ -z "$os_PACKAGE" ]]; then
764 GetOSVersion
765 fi
766 if [[ "$os_PACKAGE" = "deb" ]]; then
767 sudo /usr/sbin/service $1 stop
768 else
769 sudo /sbin/service $1 stop
770 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600771}
772
773
774# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500775# Accepts as False: 0 no false False FALSE
776# Accepts as True: 1 yes true True TRUE
777# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -0600778function trueorfalse() {
779 local default=$1
780 local testval=$2
781
782 [[ -z "$testval" ]] && { echo "$default"; return; }
783 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
784 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
785 echo "$default"
786}
Dean Troyer27e32692012-03-16 16:16:56 -0500787
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500788
Dean Troyerca0e3d02012-04-13 15:58:37 -0500789# Retrieve an image from a URL and upload into Glance
790# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500791# ``FILES`` must be set to the cache dir
792# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -0500793# upload_image image-url glance-token
794function upload_image() {
795 local image_url=$1
796 local token=$2
797
798 # Create a directory for the downloaded image tarballs.
799 mkdir -p $FILES/images
800
801 # Downloads the image (uec ami+aki style), then extracts it.
802 IMAGE_FNAME=`basename "$image_url"`
803 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
804 wget -c $image_url -O $FILES/$IMAGE_FNAME
805 if [[ $? -ne 0 ]]; then
806 echo "Not found: $image_url"
807 return
808 fi
809 fi
810
811 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
812 if [[ "$image_url" =~ 'openvz' ]]; then
813 IMAGE="$FILES/${IMAGE_FNAME}"
814 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
815 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}"
816 return
817 fi
818
819 KERNEL=""
820 RAMDISK=""
821 DISK_FORMAT=""
822 CONTAINER_FORMAT=""
823 UNPACK=""
824 case "$IMAGE_FNAME" in
825 *.tar.gz|*.tgz)
826 # Extract ami and aki files
827 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
828 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
829 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
830 xdir="$FILES/images/$IMAGE_NAME"
831 rm -Rf "$xdir";
832 mkdir "$xdir"
833 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
834 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
835 [ -f "$f" ] && echo "$f" && break; done; true)
836 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
837 [ -f "$f" ] && echo "$f" && break; done; true)
838 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
839 [ -f "$f" ] && echo "$f" && break; done; true)
840 if [[ -z "$IMAGE_NAME" ]]; then
841 IMAGE_NAME=$(basename "$IMAGE" ".img")
842 fi
843 ;;
844 *.img)
845 IMAGE="$FILES/$IMAGE_FNAME";
846 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -0500847 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
848 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
849 DISK_FORMAT=$format
850 else
851 DISK_FORMAT=raw
852 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -0500853 CONTAINER_FORMAT=bare
854 ;;
855 *.img.gz)
856 IMAGE="$FILES/${IMAGE_FNAME}"
857 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
858 DISK_FORMAT=raw
859 CONTAINER_FORMAT=bare
860 UNPACK=zcat
861 ;;
862 *.qcow2)
863 IMAGE="$FILES/${IMAGE_FNAME}"
864 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
865 DISK_FORMAT=qcow2
866 CONTAINER_FORMAT=bare
867 ;;
868 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
869 esac
870
871 if [ "$CONTAINER_FORMAT" = "bare" ]; then
872 if [ "$UNPACK" = "zcat" ]; then
873 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}")
874 else
875 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}"
876 fi
877 else
878 # Use glance client to add the kernel the root filesystem.
879 # We parse the results of the first upload to get the glance ID of the
880 # kernel for use when uploading the root filesystem.
881 KERNEL_ID=""; RAMDISK_ID="";
882 if [ -n "$KERNEL" ]; then
883 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)
884 fi
885 if [ -n "$RAMDISK" ]; then
886 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)
887 fi
888 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}"
889 fi
890}
891
Dean Troyerc1b486a2012-11-05 14:26:09 -0600892# Set the database backend to use
893# When called from stackrc/localrc DATABASE_BACKENDS has not been
894# initialized yet, just save the configuration selection and call back later
895# to validate it.
896# $1 The name of the database backend to use (mysql, postgresql, ...)
897function use_database {
898 if [[ -z "$DATABASE_BACKENDS" ]]; then
899 # The backends haven't initialized yet, just save the selection for now
900 DATABASE_TYPE=$1
901 return
902 fi
903 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1 && return 0
904 ret=$?
905 return $ret
906}
907
Terry Wilson428af5a2012-11-01 16:12:39 -0400908# Toggle enable/disable_service for services that must run exclusive of each other
909# $1 The name of a variable containing a space-separated list of services
910# $2 The name of a variable in which to store the enabled service's name
911# $3 The name of the service to enable
912function use_exclusive_service {
913 local options=${!1}
914 local selection=$3
915 out=$2
916 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
917 for opt in $options;do
918 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
919 done
920 eval "$out=$selection"
921 return 0
922}
Dean Troyerca0e3d02012-04-13 15:58:37 -0500923
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500924# Wrapper for ``yum`` to set proxy environment variables
925# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500926# yum_install package [package ...]
927function yum_install() {
928 [[ "$OFFLINE" = "True" ]] && return
929 local sudo="sudo"
930 [[ "$(id -u)" = "0" ]] && sudo="env"
931 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900932 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500933 yum install -y "$@"
934}
935
Nachi Uenofda946e2012-10-24 17:26:02 -0700936# ping check
937# Uses globals ``ENABLED_SERVICES``
938function ping_check() {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700939 if is_service_enabled quantum; then
940 _ping_check_quantum "$1" $2 $3 $4
941 return
942 fi
943 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -0700944}
945
946# ping check for nova
947# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
948function _ping_check_novanet() {
949 local from_net=$1
950 local ip=$2
951 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700952 local expected=${4:-"True"}
953 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -0700954 MULTI_HOST=`trueorfalse False $MULTI_HOST`
955 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
956 sleep $boot_timeout
957 return
958 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700959 if [[ "$expected" = "True" ]]; then
960 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
961 else
962 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
963 fi
964 if ! timeout $boot_timeout sh -c "$check_command"; then
965 if [[ "$expected" = "True" ]]; then
966 echo "[Fail] Couldn't ping server"
967 else
968 echo "[Fail] Could ping server"
969 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700970 exit 1
971 fi
972}
973
974# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700975
Nachi Uenofda946e2012-10-24 17:26:02 -0700976function ssh_check() {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700977 if is_service_enabled quantum; then
978 _ssh_check_quantum "$1" $2 $3 $4 $5
979 return
980 fi
981 _ssh_check_novanet "$1" $2 $3 $4 $5
982}
983
984function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -0700985 local NET_NAME=$1
986 local KEY_FILE=$2
987 local FLOATING_IP=$3
988 local DEFAULT_INSTANCE_USER=$4
989 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600990 local probe_cmd=""
Nachi Uenofda946e2012-10-24 17:26:02 -0700991 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
992 echo "server didn't become ssh-able!"
993 exit 1
994 fi
995}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500996
Vincent Untz856a11e2012-11-21 16:04:12 +0100997
998# zypper wrapper to set arguments correctly
999# zypper_install package [package ...]
1000function zypper_install() {
1001 [[ "$OFFLINE" = "True" ]] && return
1002 local sudo="sudo"
1003 [[ "$(id -u)" = "0" ]] && sudo="env"
1004 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1005 zypper --non-interactive install --auto-agree-with-licenses "$@"
1006}
1007
1008
1009# Add a user to a group.
1010# add_user_to_group user group
1011function add_user_to_group() {
1012 local user=$1
1013 local group=$2
1014
1015 if [[ -z "$os_VENDOR" ]]; then
1016 GetOSVersion
1017 fi
1018
1019 # SLE11 and openSUSE 12.2 don't have the usual usermod
1020 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1021 sudo usermod -a -G "$group" "$user"
1022 else
1023 sudo usermod -A "$group" "$user"
1024 fi
1025}
1026
1027
1028# Get the location of the $module-rootwrap executables, where module is cinder
1029# or nova.
1030# get_rootwrap_location module
1031function get_rootwrap_location() {
1032 local module=$1
1033
1034 if [[ -z "$os_PACKAGE" ]]; then
1035 GetOSVersion
1036 fi
1037
Steve Baker1a7bbd22012-12-03 17:04:02 +13001038 if [[ "$os_PACKAGE" = "deb" ]] || is_suse; then
Vincent Untz856a11e2012-11-21 16:04:12 +01001039 echo "/usr/local/bin/$module-rootwrap"
1040 else
1041 echo "/usr/bin/$module-rootwrap"
1042 fi
1043}
1044
Vincent Untz8ec27222012-11-29 09:25:31 +01001045# Get the path to the pip command.
1046# get_pip_command
1047function get_pip_command() {
1048 if [[ -z "$os_PACKAGE" ]]; then
1049 GetOSVersion
1050 fi
1051
1052 if [[ "$os_PACKAGE" = "deb" ]] || is_suse; then
1053 echo "/usr/bin/pip"
1054 else
1055 echo "/usr/bin/pip-python"
1056 fi
1057}
Vincent Untz856a11e2012-11-21 16:04:12 +01001058
1059# Check if qpid can be used on the current distro.
1060# qpid_is_supported
1061function qpid_is_supported() {
1062 if [[ -z "$DISTRO" ]]; then
1063 GetDistro
1064 fi
1065
1066 # Qpid was introduced to Ubuntu in precise, disallow it on oneiric; it is
1067 # not in openSUSE either right now.
Steve Baker1a7bbd22012-12-03 17:04:02 +13001068 [[ "$DISTRO" = "oneiric" ]] || is_suse
Vincent Untz856a11e2012-11-21 16:04:12 +01001069 return $?
1070}
1071
Dean Troyer27e32692012-03-16 16:16:56 -05001072# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +00001073$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001074
1075
1076# Local variables:
1077# -*- mode: Shell-script -*-
Andrew Laskif900bd72012-09-05 17:23:14 -04001078# End: