blob: 90b3b784dc98f8228d989f66acf8f1fa78a5e73a [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"
226 else
227 os_PACKAGE="rpm"
228 fi
229 os_CODENAME=$(lsb_release -c -s)
230 elif [[ -r /etc/redhat-release ]]; then
231 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
232 # CentOS release 5.5 (Final)
233 # CentOS Linux release 6.0 (Final)
234 # Fedora release 16 (Verne)
235 os_CODENAME=""
236 for r in "Red Hat" CentOS Fedora; do
237 os_VENDOR=$r
238 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
239 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
240 os_CODENAME=${ver#*|}
241 os_RELEASE=${ver%|*}
242 os_UPDATE=${os_RELEASE##*.}
243 os_RELEASE=${os_RELEASE%.*}
244 break
245 fi
246 os_VENDOR=""
247 done
248 os_PACKAGE="rpm"
249 fi
250 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
251}
252
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300253# git update using reference as a branch.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500254# git_update_branch ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300255function git_update_branch() {
256
257 GIT_BRANCH=$1
258
259 git checkout -f origin/$GIT_BRANCH
260 # a local branch might not exist
261 git branch -D $GIT_BRANCH || true
262 git checkout -b $GIT_BRANCH
263}
264
265
266# git update using reference as a tag. Be careful editing source at that repo
267# as working copy will be in a detached mode
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500268# git_update_tag ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300269function git_update_tag() {
270
271 GIT_TAG=$1
272
273 git tag -d $GIT_TAG
274 # fetching given tag only
275 git fetch origin tag $GIT_TAG
276 git checkout -f $GIT_TAG
277}
278
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500279
Andrew Laskif900bd72012-09-05 17:23:14 -0400280# git update using reference as a branch.
281# git_update_remote_branch ref
282function git_update_remote_branch() {
283
284 GIT_BRANCH=$1
285
286 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
287}
288
289
Dean Troyera9e0a482012-07-09 14:07:23 -0500290# Translate the OS version values into common nomenclature
291# Sets ``DISTRO`` from the ``os_*`` values
292function GetDistro() {
293 GetOSVersion
294 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
295 # 'Everyone' refers to Ubuntu releases by the code name adjective
296 DISTRO=$os_CODENAME
297 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
298 # For Fedora, just use 'f' and the release
299 DISTRO="f$os_RELEASE"
300 else
301 # Catch-all for now is Vendor + Release + Update
302 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
303 fi
304 export DISTRO
305}
306
307
Dean Troyer7f9aa712012-01-31 12:11:56 -0600308# git clone only if directory doesn't exist already. Since ``DEST`` might not
309# be owned by the installation user, we create the directory and change the
310# ownership to the proper user.
311# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700312# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
313# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500314# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600315# git_clone remote dest-dir branch
316function git_clone {
317 [[ "$OFFLINE" = "True" ]] && return
318
319 GIT_REMOTE=$1
320 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300321 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600322
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300323 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600324 # If our branch name is a gerrit style refs/changes/...
325 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700326 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600327 git clone $GIT_REMOTE $GIT_DEST
328 fi
329 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300330 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600331 else
332 # do a full clone only if the directory doesn't exist
333 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700334 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600335 git clone $GIT_REMOTE $GIT_DEST
336 cd $GIT_DEST
337 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300338 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600339 elif [[ "$RECLONE" == "yes" ]]; then
340 # if it does exist then simulate what clone does if asked to RECLONE
341 cd $GIT_DEST
342 # set the url to pull from and fetch
343 git remote set-url origin $GIT_REMOTE
344 git fetch origin
345 # remove the existing ignored files (like pyc) as they cause breakage
346 # (due to the py files having older timestamps than our pyc, so python
347 # thinks the pyc files are correct using them)
348 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300349
350 # handle GIT_REF accordingly to type (tag, branch)
351 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
352 git_update_tag $GIT_REF
353 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
354 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400355 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
356 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300357 else
358 echo $GIT_REF is neither branch nor tag
359 exit 1
360 fi
361
Dean Troyer7f9aa712012-01-31 12:11:56 -0600362 fi
363 fi
364}
365
366
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500367# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200368# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500369function inicomment() {
370 local file=$1
371 local section=$2
372 local option=$3
373 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
374}
375
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200376# Uncomment an option in an INI file
377# iniuncomment config-file section option
378function iniuncomment() {
379 local file=$1
380 local section=$2
381 local option=$3
382 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
383}
384
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500385
386# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500387# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500388function iniget() {
389 local file=$1
390 local section=$2
391 local option=$3
392 local line
393 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
394 echo ${line#*=}
395}
396
397
398# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500399# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500400function iniset() {
401 local file=$1
402 local section=$2
403 local option=$3
404 local value=$4
Dean Troyer09e636e2012-03-19 16:31:12 -0500405 if ! grep -q "^\[$section\]" $file; then
406 # Add section at the end
407 echo -e "\n[$section]" >>$file
408 fi
409 if [[ -z "$(iniget $file $section $option)" ]]; then
410 # Add it
411 sed -i -e "/^\[$section\]/ a\\
412$option = $value
413" $file
414 else
415 # Replace it
416 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
417 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500418}
419
420
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000421# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500422# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000423#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500424# Multiple services specified as arguments are ``OR``'ed together; the test
425# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000426#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500427# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000428# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500429# **cinder** returns true if any service enabled start with **c-**
430# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000431# **glance** returns true if any service enabled start with **g-**
432# **quantum** returns true if any service enabled start with **q-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500433#
434# Uses global ``ENABLED_SERVICES``
435# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000436function is_service_enabled() {
437 services=$@
438 for service in ${services}; do
439 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
440 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500441 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700442 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000443 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
444 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
445 done
446 return 1
447}
448
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500449
450# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
451# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400452function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500453 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400454 s/,,/,/g;
455 s/^,//;
456 s/,$//
457 '
458}
459
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500460
Doug Hellmannf04178f2012-07-05 17:10:03 -0400461# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500462# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400463#
464# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800465# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400466#
467# This function does not know about the special cases
468# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500469# Uses global ``ENABLED_SERVICES``
470# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400471function enable_service() {
472 local tmpsvcs="${ENABLED_SERVICES}"
473 for service in $@; do
474 if ! is_service_enabled $service; then
475 tmpsvcs+=",$service"
476 fi
477 done
478 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
479 disable_negated_services
480}
481
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500482
Doug Hellmannf04178f2012-07-05 17:10:03 -0400483# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500484# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400485#
486# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800487# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400488#
489# This function does not know about the special cases
490# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500491# Uses global ``ENABLED_SERVICES``
492# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400493function disable_service() {
494 local tmpsvcs=",${ENABLED_SERVICES},"
495 local service
496 for service in $@; do
497 if is_service_enabled $service; then
498 tmpsvcs=${tmpsvcs//,$service,/,}
499 fi
500 done
501 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
502}
503
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500504
Doug Hellmannf04178f2012-07-05 17:10:03 -0400505# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500506# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400507# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500508# Uses global ``ENABLED_SERVICES``
509# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400510function disable_all_services() {
511 ENABLED_SERVICES=""
512}
513
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500514
515# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800516# services except rabbit (rabbit) set in ``localrc``:
517# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500518# Uses global ``ENABLED_SERVICES``
519# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400520function disable_negated_services() {
521 local tmpsvcs="${ENABLED_SERVICES}"
522 local service
523 for service in ${tmpsvcs//,/ }; do
524 if [[ ${service} == -* ]]; then
525 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
526 fi
527 done
528 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
529}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600530
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500531
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500532# Distro-agnostic package installer
533# install_package package [package ...]
534function install_package() {
535 if [[ -z "$os_PACKAGE" ]]; then
536 GetOSVersion
537 fi
Vincent Untzc0482e62012-06-12 11:30:43 +0200538
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500539 if [[ "$os_PACKAGE" = "deb" ]]; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200540 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
541 NO_UPDATE_REPOS=True
542
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500543 apt_get install "$@"
544 else
545 yum_install "$@"
546 fi
547}
548
549
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200550# Distro-agnostic function to tell if a package is installed
551# is_package_installed package [package ...]
552function is_package_installed() {
553 if [[ -z "$@" ]]; then
554 return 1
555 fi
556
557 if [[ -z "$os_PACKAGE" ]]; then
558 GetOSVersion
559 fi
560 if [[ "$os_PACKAGE" = "deb" ]]; then
561 dpkg -l "$@" > /dev/null
562 return $?
563 else
564 rpm --quiet -q "$@"
565 return $?
566 fi
567}
568
569
Dean Troyer489bd2a2012-03-02 10:44:29 -0600570# Test if the named environment variable is set and not zero length
571# is_set env-var
572function is_set() {
573 local var=\$"$1"
Dean Troyere88c0a22012-11-02 16:59:03 -0500574 if eval "[ -z \"$var\" ]"; then
Dean Troyer489bd2a2012-03-02 10:44:29 -0600575 return 1
576 fi
577 return 0
578}
579
580
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500581# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +0000582# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
583# ``TRACK_DEPENDS``, ``*_proxy`
Dean Troyer7f9aa712012-01-31 12:11:56 -0600584# pip_install package [package ...]
585function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600586 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500587 if [[ -z "$os_PACKAGE" ]]; then
588 GetOSVersion
589 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500590 if [[ $TRACK_DEPENDS = True ]] ; then
591 source $DEST/.venv/bin/activate
592 CMD_PIP=$DEST/.venv/bin/pip
593 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500594 else
Monty Taylor47f02062012-07-26 11:09:24 -0500595 SUDO_PIP="sudo"
596 if [[ "$os_PACKAGE" = "deb" ]]; then
597 CMD_PIP=/usr/bin/pip
598 else
599 CMD_PIP=/usr/bin/pip-python
600 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500601 fi
Maru Newby3a87edd2012-10-25 23:01:06 +0000602 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
603 PIP_MIRROR_OPT="--use-mirrors"
604 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500605 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600606 HTTP_PROXY=$http_proxy \
607 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900608 NO_PROXY=$no_proxy \
Maru Newby3a87edd2012-10-25 23:01:06 +0000609 $CMD_PIP install $PIP_MIRROR_OPT $@
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500610}
611
612
613# Service wrapper to restart services
614# restart_service service-name
615function restart_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600616 if [[ -z "$os_PACKAGE" ]]; then
617 GetOSVersion
618 fi
619 if [[ "$os_PACKAGE" = "deb" ]]; then
620 sudo /usr/sbin/service $1 restart
621 else
622 sudo /sbin/service $1 restart
623 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500624}
625
626
Dean Troyer15733352012-09-06 11:51:30 -0500627# Helper to launch a service in a named screen
628# screen_it service "command-line"
629function screen_it {
630 NL=`echo -ne '\015'`
631 SCREEN_NAME=${SCREEN_NAME:-stack}
632 if is_service_enabled $1; then
633 # Append the service to the screen rc file
634 screen_rc "$1" "$2"
635
636 screen -S $SCREEN_NAME -X screen -t $1
637 # sleep to allow bash to be ready to be send the command - we are
638 # creating a new window in screen and then sends characters, so if
639 # bash isn't running by the time we send the command, nothing happens
640 sleep 1.5
641
642 if [[ -n ${SCREEN_LOGDIR} ]]; then
643 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
644 screen -S $SCREEN_NAME -p $1 -X log on
645 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
646 fi
647 screen -S $SCREEN_NAME -p $1 -X stuff "$2$NL"
648 fi
649}
650
651
652# Screen rc file builder
653# screen_rc service "command-line"
654function screen_rc {
655 SCREEN_NAME=${SCREEN_NAME:-stack}
656 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
657 if [[ ! -e $SCREENRC ]]; then
658 # Name the screen session
659 echo "sessionname $SCREEN_NAME" > $SCREENRC
660 # Set a reasonable statusbar
661 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
662 echo "screen -t shell bash" >> $SCREENRC
663 fi
664 # If this service doesn't already exist in the screenrc file
665 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
666 NL=`echo -ne '\015'`
667 echo "screen -t $1 bash" >> $SCREENRC
668 echo "stuff \"$2$NL\"" >> $SCREENRC
669 fi
670}
671
672
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500673# ``pip install`` the dependencies of the package before ``setup.py develop``
674# so pip and not distutils processes the dependency chain
675# Uses globals ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500676# setup_develop directory
677function setup_develop() {
Monty Taylor47f02062012-07-26 11:09:24 -0500678 if [[ $TRACK_DEPENDS = True ]] ; then
679 SUDO_CMD="env"
680 else
681 SUDO_CMD="sudo"
682 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500683 (cd $1; \
684 python setup.py egg_info; \
685 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
686 depend_links=$(echo $raw_links | xargs); \
Dean Troyer1a3c9fe2012-09-29 17:25:02 -0500687 require_file=$([ ! -r *-info/requires.txt ] || echo "-r *-info/requires.txt"); \
688 pip_install $require_file $depend_links; \
Monty Taylor47f02062012-07-26 11:09:24 -0500689 $SUDO_CMD \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500690 HTTP_PROXY=$http_proxy \
691 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900692 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500693 python setup.py develop \
694 )
695}
696
697
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500698# Service wrapper to start services
699# start_service service-name
700function start_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600701 if [[ -z "$os_PACKAGE" ]]; then
702 GetOSVersion
703 fi
704 if [[ "$os_PACKAGE" = "deb" ]]; then
705 sudo /usr/sbin/service $1 start
706 else
707 sudo /sbin/service $1 start
708 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500709}
710
711
712# Service wrapper to stop services
713# stop_service service-name
714function stop_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600715 if [[ -z "$os_PACKAGE" ]]; then
716 GetOSVersion
717 fi
718 if [[ "$os_PACKAGE" = "deb" ]]; then
719 sudo /usr/sbin/service $1 stop
720 else
721 sudo /sbin/service $1 stop
722 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600723}
724
725
726# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500727# Accepts as False: 0 no false False FALSE
728# Accepts as True: 1 yes true True TRUE
729# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -0600730function trueorfalse() {
731 local default=$1
732 local testval=$2
733
734 [[ -z "$testval" ]] && { echo "$default"; return; }
735 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
736 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
737 echo "$default"
738}
Dean Troyer27e32692012-03-16 16:16:56 -0500739
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500740
Dean Troyerca0e3d02012-04-13 15:58:37 -0500741# Retrieve an image from a URL and upload into Glance
742# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500743# ``FILES`` must be set to the cache dir
744# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -0500745# upload_image image-url glance-token
746function upload_image() {
747 local image_url=$1
748 local token=$2
749
750 # Create a directory for the downloaded image tarballs.
751 mkdir -p $FILES/images
752
753 # Downloads the image (uec ami+aki style), then extracts it.
754 IMAGE_FNAME=`basename "$image_url"`
755 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
756 wget -c $image_url -O $FILES/$IMAGE_FNAME
757 if [[ $? -ne 0 ]]; then
758 echo "Not found: $image_url"
759 return
760 fi
761 fi
762
763 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
764 if [[ "$image_url" =~ 'openvz' ]]; then
765 IMAGE="$FILES/${IMAGE_FNAME}"
766 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
767 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}"
768 return
769 fi
770
771 KERNEL=""
772 RAMDISK=""
773 DISK_FORMAT=""
774 CONTAINER_FORMAT=""
775 UNPACK=""
776 case "$IMAGE_FNAME" in
777 *.tar.gz|*.tgz)
778 # Extract ami and aki files
779 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
780 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
781 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
782 xdir="$FILES/images/$IMAGE_NAME"
783 rm -Rf "$xdir";
784 mkdir "$xdir"
785 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
786 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
787 [ -f "$f" ] && echo "$f" && break; done; true)
788 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
789 [ -f "$f" ] && echo "$f" && break; done; true)
790 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
791 [ -f "$f" ] && echo "$f" && break; done; true)
792 if [[ -z "$IMAGE_NAME" ]]; then
793 IMAGE_NAME=$(basename "$IMAGE" ".img")
794 fi
795 ;;
796 *.img)
797 IMAGE="$FILES/$IMAGE_FNAME";
798 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -0500799 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
800 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
801 DISK_FORMAT=$format
802 else
803 DISK_FORMAT=raw
804 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -0500805 CONTAINER_FORMAT=bare
806 ;;
807 *.img.gz)
808 IMAGE="$FILES/${IMAGE_FNAME}"
809 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
810 DISK_FORMAT=raw
811 CONTAINER_FORMAT=bare
812 UNPACK=zcat
813 ;;
814 *.qcow2)
815 IMAGE="$FILES/${IMAGE_FNAME}"
816 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
817 DISK_FORMAT=qcow2
818 CONTAINER_FORMAT=bare
819 ;;
820 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
821 esac
822
823 if [ "$CONTAINER_FORMAT" = "bare" ]; then
824 if [ "$UNPACK" = "zcat" ]; then
825 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}")
826 else
827 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}"
828 fi
829 else
830 # Use glance client to add the kernel the root filesystem.
831 # We parse the results of the first upload to get the glance ID of the
832 # kernel for use when uploading the root filesystem.
833 KERNEL_ID=""; RAMDISK_ID="";
834 if [ -n "$KERNEL" ]; then
835 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)
836 fi
837 if [ -n "$RAMDISK" ]; then
838 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)
839 fi
840 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}"
841 fi
842}
843
Terry Wilson428af5a2012-11-01 16:12:39 -0400844# Toggle enable/disable_service for services that must run exclusive of each other
845# $1 The name of a variable containing a space-separated list of services
846# $2 The name of a variable in which to store the enabled service's name
847# $3 The name of the service to enable
848function use_exclusive_service {
849 local options=${!1}
850 local selection=$3
851 out=$2
852 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
853 for opt in $options;do
854 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
855 done
856 eval "$out=$selection"
857 return 0
858}
Dean Troyerca0e3d02012-04-13 15:58:37 -0500859
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500860# Wrapper for ``yum`` to set proxy environment variables
861# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500862# yum_install package [package ...]
863function yum_install() {
864 [[ "$OFFLINE" = "True" ]] && return
865 local sudo="sudo"
866 [[ "$(id -u)" = "0" ]] && sudo="env"
867 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900868 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500869 yum install -y "$@"
870}
871
Nachi Uenofda946e2012-10-24 17:26:02 -0700872# ping check
873# Uses globals ``ENABLED_SERVICES``
874function ping_check() {
875 _ping_check_novanet "$1" $2 $3
876}
877
878# ping check for nova
879# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
880function _ping_check_novanet() {
881 local from_net=$1
882 local ip=$2
883 local boot_timeout=$3
884 MULTI_HOST=`trueorfalse False $MULTI_HOST`
885 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
886 sleep $boot_timeout
887 return
888 fi
889 if ! timeout $boot_timeout sh -c "while ! ping -c1 -w1 $ip; do sleep 1; done"; then
890 echo "Couldn't ping server"
891 exit 1
892 fi
893}
894
895# ssh check
896function ssh_check() {
897 local NET_NAME=$1
898 local KEY_FILE=$2
899 local FLOATING_IP=$3
900 local DEFAULT_INSTANCE_USER=$4
901 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600902 local probe_cmd=""
Nachi Uenofda946e2012-10-24 17:26:02 -0700903 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
904 echo "server didn't become ssh-able!"
905 exit 1
906 fi
907}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500908
Dean Troyer27e32692012-03-16 16:16:56 -0500909# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000910$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500911
912
913# Local variables:
914# -*- mode: Shell-script -*-
Andrew Laskif900bd72012-09-05 17:23:14 -0400915# End: