blob: fa7c805823744769f081e6e49ef4cfb852ead9e0 [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:
4# ``DISTRO``
5# ``ENABLED_SERVICES``
6# ``EROR_ON_CLONE``
7# ``FILES``
8# ``GLANCE_HOSTPORT``
9# ``OFFLINE``
10# ``PIP_DOWNLOAD_CACHE``
11# ``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#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500110# Uses globals ``DISTRO``, ``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
122 echo "No distro set in DISTRO"
123 return 1
124 fi
125 for service in general ${ENABLED_SERVICES//,/ }; do
126 # Allow individual services to specify dependencies
127 if [[ -e ${package_dir}/${service} ]]; then
128 file_to_parse="${file_to_parse} $service"
129 fi
130 # NOTE(sdague) n-api needs glance for now because that's where
131 # glance client is
132 if [[ $service == n-api ]]; then
133 if [[ ! $file_to_parse =~ nova ]]; then
134 file_to_parse="${file_to_parse} nova"
135 fi
136 if [[ ! $file_to_parse =~ glance ]]; then
137 file_to_parse="${file_to_parse} glance"
138 fi
139 elif [[ $service == c-* ]]; then
140 if [[ ! $file_to_parse =~ cinder ]]; then
141 file_to_parse="${file_to_parse} cinder"
142 fi
John H. Tran93361642012-07-26 11:22:05 -0700143 elif [[ $service == ceilometer-* ]]; then
144 if [[ ! $file_to_parse =~ ceilometer ]]; then
145 file_to_parse="${file_to_parse} ceilometer"
146 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500147 elif [[ $service == n-* ]]; then
148 if [[ ! $file_to_parse =~ nova ]]; then
149 file_to_parse="${file_to_parse} nova"
150 fi
151 elif [[ $service == g-* ]]; then
152 if [[ ! $file_to_parse =~ glance ]]; then
153 file_to_parse="${file_to_parse} glance"
154 fi
155 elif [[ $service == key* ]]; then
156 if [[ ! $file_to_parse =~ keystone ]]; then
157 file_to_parse="${file_to_parse} keystone"
158 fi
159 fi
160 done
161
162 for file in ${file_to_parse}; do
163 local fname=${package_dir}/${file}
164 local OIFS line package distros distro
165 [[ -e $fname ]] || continue
166
167 OIFS=$IFS
168 IFS=$'\n'
169 for line in $(<${fname}); do
170 if [[ $line =~ "NOPRIME" ]]; then
171 continue
172 fi
173
174 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
175 # We are using BASH regexp matching feature.
176 package=${BASH_REMATCH[1]}
177 distros=${BASH_REMATCH[2]}
178 # In bash ${VAR,,} will lowecase VAR
179 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
180 continue
181 fi
182
183 echo ${line%#*}
184 done
185 IFS=$OIFS
186 done
187}
188
189
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500190# Determine OS Vendor, Release and Update
191# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
192# Returns results in global variables:
193# os_VENDOR - vendor name
194# os_RELEASE - release
195# os_UPDATE - update
196# os_PACKAGE - package type
197# os_CODENAME - vendor's codename for release
198# GetOSVersion
199GetOSVersion() {
200 # Figure out which vendor we are
201 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
202 # OS/X
203 os_VENDOR=`sw_vers -productName`
204 os_RELEASE=`sw_vers -productVersion`
205 os_UPDATE=${os_RELEASE##*.}
206 os_RELEASE=${os_RELEASE%.*}
207 os_PACKAGE=""
208 if [[ "$os_RELEASE" =~ "10.7" ]]; then
209 os_CODENAME="lion"
210 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
211 os_CODENAME="snow leopard"
212 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
213 os_CODENAME="leopard"
214 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
215 os_CODENAME="tiger"
216 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
217 os_CODENAME="panther"
218 else
219 os_CODENAME=""
220 fi
221 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
222 os_VENDOR=$(lsb_release -i -s)
223 os_RELEASE=$(lsb_release -r -s)
224 os_UPDATE=""
225 if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
226 os_PACKAGE="deb"
227 else
228 os_PACKAGE="rpm"
229 fi
230 os_CODENAME=$(lsb_release -c -s)
231 elif [[ -r /etc/redhat-release ]]; then
232 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
233 # CentOS release 5.5 (Final)
234 # CentOS Linux release 6.0 (Final)
235 # Fedora release 16 (Verne)
236 os_CODENAME=""
237 for r in "Red Hat" CentOS Fedora; do
238 os_VENDOR=$r
239 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
240 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
241 os_CODENAME=${ver#*|}
242 os_RELEASE=${ver%|*}
243 os_UPDATE=${os_RELEASE##*.}
244 os_RELEASE=${os_RELEASE%.*}
245 break
246 fi
247 os_VENDOR=""
248 done
249 os_PACKAGE="rpm"
250 fi
251 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
252}
253
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300254# git update using reference as a branch.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500255# git_update_branch ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300256function git_update_branch() {
257
258 GIT_BRANCH=$1
259
260 git checkout -f origin/$GIT_BRANCH
261 # a local branch might not exist
262 git branch -D $GIT_BRANCH || true
263 git checkout -b $GIT_BRANCH
264}
265
266
267# git update using reference as a tag. Be careful editing source at that repo
268# as working copy will be in a detached mode
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500269# git_update_tag ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300270function git_update_tag() {
271
272 GIT_TAG=$1
273
274 git tag -d $GIT_TAG
275 # fetching given tag only
276 git fetch origin tag $GIT_TAG
277 git checkout -f $GIT_TAG
278}
279
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500280
Dean Troyera9e0a482012-07-09 14:07:23 -0500281# Translate the OS version values into common nomenclature
282# Sets ``DISTRO`` from the ``os_*`` values
283function GetDistro() {
284 GetOSVersion
285 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
286 # 'Everyone' refers to Ubuntu releases by the code name adjective
287 DISTRO=$os_CODENAME
288 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
289 # For Fedora, just use 'f' and the release
290 DISTRO="f$os_RELEASE"
291 else
292 # Catch-all for now is Vendor + Release + Update
293 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
294 fi
295 export DISTRO
296}
297
298
Dean Troyer7f9aa712012-01-31 12:11:56 -0600299# git clone only if directory doesn't exist already. Since ``DEST`` might not
300# be owned by the installation user, we create the directory and change the
301# ownership to the proper user.
302# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700303# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
304# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500305# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600306# git_clone remote dest-dir branch
307function git_clone {
308 [[ "$OFFLINE" = "True" ]] && return
309
310 GIT_REMOTE=$1
311 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300312 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600313
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300314 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600315 # If our branch name is a gerrit style refs/changes/...
316 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700317 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600318 git clone $GIT_REMOTE $GIT_DEST
319 fi
320 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300321 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600322 else
323 # do a full clone only if the directory doesn't exist
324 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700325 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600326 git clone $GIT_REMOTE $GIT_DEST
327 cd $GIT_DEST
328 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300329 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600330 elif [[ "$RECLONE" == "yes" ]]; then
331 # if it does exist then simulate what clone does if asked to RECLONE
332 cd $GIT_DEST
333 # set the url to pull from and fetch
334 git remote set-url origin $GIT_REMOTE
335 git fetch origin
336 # remove the existing ignored files (like pyc) as they cause breakage
337 # (due to the py files having older timestamps than our pyc, so python
338 # thinks the pyc files are correct using them)
339 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300340
341 # handle GIT_REF accordingly to type (tag, branch)
342 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
343 git_update_tag $GIT_REF
344 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
345 git_update_branch $GIT_REF
346 else
347 echo $GIT_REF is neither branch nor tag
348 exit 1
349 fi
350
Dean Troyer7f9aa712012-01-31 12:11:56 -0600351 fi
352 fi
353}
354
355
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500356# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200357# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500358function inicomment() {
359 local file=$1
360 local section=$2
361 local option=$3
362 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
363}
364
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200365# Uncomment an option in an INI file
366# iniuncomment config-file section option
367function iniuncomment() {
368 local file=$1
369 local section=$2
370 local option=$3
371 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
372}
373
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500374
375# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500376# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500377function iniget() {
378 local file=$1
379 local section=$2
380 local option=$3
381 local line
382 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
383 echo ${line#*=}
384}
385
386
387# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500388# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500389function iniset() {
390 local file=$1
391 local section=$2
392 local option=$3
393 local value=$4
Dean Troyer09e636e2012-03-19 16:31:12 -0500394 if ! grep -q "^\[$section\]" $file; then
395 # Add section at the end
396 echo -e "\n[$section]" >>$file
397 fi
398 if [[ -z "$(iniget $file $section $option)" ]]; then
399 # Add it
400 sed -i -e "/^\[$section\]/ a\\
401$option = $value
402" $file
403 else
404 # Replace it
405 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
406 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500407}
408
409
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000410# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500411# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000412#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500413# Multiple services specified as arguments are ``OR``'ed together; the test
414# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000415#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500416# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000417# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500418# **cinder** returns true if any service enabled start with **c-**
419# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000420# **glance** returns true if any service enabled start with **g-**
421# **quantum** returns true if any service enabled start with **q-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500422#
423# Uses global ``ENABLED_SERVICES``
424# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000425function is_service_enabled() {
426 services=$@
427 for service in ${services}; do
428 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
429 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500430 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700431 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000432 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
433 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
434 done
435 return 1
436}
437
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500438
439# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
440# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400441function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500442 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400443 s/,,/,/g;
444 s/^,//;
445 s/,$//
446 '
447}
448
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500449
Doug Hellmannf04178f2012-07-05 17:10:03 -0400450# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500451# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400452#
453# For example:
Doug Hellmannf04178f2012-07-05 17:10:03 -0400454# enable_service n-vol
455#
456# This function does not know about the special cases
457# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500458# Uses global ``ENABLED_SERVICES``
459# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400460function enable_service() {
461 local tmpsvcs="${ENABLED_SERVICES}"
462 for service in $@; do
463 if ! is_service_enabled $service; then
464 tmpsvcs+=",$service"
465 fi
466 done
467 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
468 disable_negated_services
469}
470
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500471
Doug Hellmannf04178f2012-07-05 17:10:03 -0400472# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500473# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400474#
475# For example:
Doug Hellmannf04178f2012-07-05 17:10:03 -0400476# disable_service n-vol
477#
478# This function does not know about the special cases
479# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500480# Uses global ``ENABLED_SERVICES``
481# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400482function disable_service() {
483 local tmpsvcs=",${ENABLED_SERVICES},"
484 local service
485 for service in $@; do
486 if is_service_enabled $service; then
487 tmpsvcs=${tmpsvcs//,$service,/,}
488 fi
489 done
490 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
491}
492
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500493
Doug Hellmannf04178f2012-07-05 17:10:03 -0400494# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500495# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400496# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500497# Uses global ``ENABLED_SERVICES``
498# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400499function disable_all_services() {
500 ENABLED_SERVICES=""
501}
502
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500503
504# Remove all services starting with '-'. For example, to install all default
505# services except nova-volume (n-vol) set in ``localrc``:
Doug Hellmannf04178f2012-07-05 17:10:03 -0400506# ENABLED_SERVICES+=",-n-vol"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500507# Uses global ``ENABLED_SERVICES``
508# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400509function disable_negated_services() {
510 local tmpsvcs="${ENABLED_SERVICES}"
511 local service
512 for service in ${tmpsvcs//,/ }; do
513 if [[ ${service} == -* ]]; then
514 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
515 fi
516 done
517 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
518}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600519
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500520
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500521# Distro-agnostic package installer
522# install_package package [package ...]
523function install_package() {
524 if [[ -z "$os_PACKAGE" ]]; then
525 GetOSVersion
526 fi
527 if [[ "$os_PACKAGE" = "deb" ]]; then
528 apt_get install "$@"
529 else
530 yum_install "$@"
531 fi
532}
533
534
Dean Troyer489bd2a2012-03-02 10:44:29 -0600535# Test if the named environment variable is set and not zero length
536# is_set env-var
537function is_set() {
538 local var=\$"$1"
539 if eval "[ -z $var ]"; then
540 return 1
541 fi
542 return 0
543}
544
545
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500546# Wrapper for ``pip install`` to set cache and proxy environment variables
547# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyer7f9aa712012-01-31 12:11:56 -0600548# pip_install package [package ...]
549function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600550 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500551 if [[ -z "$os_PACKAGE" ]]; then
552 GetOSVersion
553 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500554 if [[ $TRACK_DEPENDS = True ]] ; then
555 source $DEST/.venv/bin/activate
556 CMD_PIP=$DEST/.venv/bin/pip
557 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500558 else
Monty Taylor47f02062012-07-26 11:09:24 -0500559 SUDO_PIP="sudo"
560 if [[ "$os_PACKAGE" = "deb" ]]; then
561 CMD_PIP=/usr/bin/pip
562 else
563 CMD_PIP=/usr/bin/pip-python
564 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500565 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500566 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600567 HTTP_PROXY=$http_proxy \
568 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900569 NO_PROXY=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500570 $CMD_PIP install --use-mirrors $@
571}
572
573
574# Service wrapper to restart services
575# restart_service service-name
576function restart_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600577 if [[ -z "$os_PACKAGE" ]]; then
578 GetOSVersion
579 fi
580 if [[ "$os_PACKAGE" = "deb" ]]; then
581 sudo /usr/sbin/service $1 restart
582 else
583 sudo /sbin/service $1 restart
584 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500585}
586
587
Dean Troyer15733352012-09-06 11:51:30 -0500588# Helper to launch a service in a named screen
589# screen_it service "command-line"
590function screen_it {
591 NL=`echo -ne '\015'`
592 SCREEN_NAME=${SCREEN_NAME:-stack}
593 if is_service_enabled $1; then
594 # Append the service to the screen rc file
595 screen_rc "$1" "$2"
596
597 screen -S $SCREEN_NAME -X screen -t $1
598 # sleep to allow bash to be ready to be send the command - we are
599 # creating a new window in screen and then sends characters, so if
600 # bash isn't running by the time we send the command, nothing happens
601 sleep 1.5
602
603 if [[ -n ${SCREEN_LOGDIR} ]]; then
604 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
605 screen -S $SCREEN_NAME -p $1 -X log on
606 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
607 fi
608 screen -S $SCREEN_NAME -p $1 -X stuff "$2$NL"
609 fi
610}
611
612
613# Screen rc file builder
614# screen_rc service "command-line"
615function screen_rc {
616 SCREEN_NAME=${SCREEN_NAME:-stack}
617 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
618 if [[ ! -e $SCREENRC ]]; then
619 # Name the screen session
620 echo "sessionname $SCREEN_NAME" > $SCREENRC
621 # Set a reasonable statusbar
622 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
623 echo "screen -t shell bash" >> $SCREENRC
624 fi
625 # If this service doesn't already exist in the screenrc file
626 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
627 NL=`echo -ne '\015'`
628 echo "screen -t $1 bash" >> $SCREENRC
629 echo "stuff \"$2$NL\"" >> $SCREENRC
630 fi
631}
632
633
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500634# ``pip install`` the dependencies of the package before ``setup.py develop``
635# so pip and not distutils processes the dependency chain
636# Uses globals ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500637# setup_develop directory
638function setup_develop() {
Monty Taylor47f02062012-07-26 11:09:24 -0500639 if [[ $TRACK_DEPENDS = True ]] ; then
640 SUDO_CMD="env"
641 else
642 SUDO_CMD="sudo"
643 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500644 (cd $1; \
645 python setup.py egg_info; \
646 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
647 depend_links=$(echo $raw_links | xargs); \
648 pip_install -r *-info/requires.txt $depend_links; \
Monty Taylor47f02062012-07-26 11:09:24 -0500649 $SUDO_CMD \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500650 HTTP_PROXY=$http_proxy \
651 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900652 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500653 python setup.py develop \
654 )
655}
656
657
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500658# Service wrapper to start services
659# start_service service-name
660function start_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600661 if [[ -z "$os_PACKAGE" ]]; then
662 GetOSVersion
663 fi
664 if [[ "$os_PACKAGE" = "deb" ]]; then
665 sudo /usr/sbin/service $1 start
666 else
667 sudo /sbin/service $1 start
668 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500669}
670
671
672# Service wrapper to stop services
673# stop_service service-name
674function stop_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600675 if [[ -z "$os_PACKAGE" ]]; then
676 GetOSVersion
677 fi
678 if [[ "$os_PACKAGE" = "deb" ]]; then
679 sudo /usr/sbin/service $1 stop
680 else
681 sudo /sbin/service $1 stop
682 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600683}
684
685
686# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500687# Accepts as False: 0 no false False FALSE
688# Accepts as True: 1 yes true True TRUE
689# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -0600690function trueorfalse() {
691 local default=$1
692 local testval=$2
693
694 [[ -z "$testval" ]] && { echo "$default"; return; }
695 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
696 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
697 echo "$default"
698}
Dean Troyer27e32692012-03-16 16:16:56 -0500699
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500700
Dean Troyerca0e3d02012-04-13 15:58:37 -0500701# Retrieve an image from a URL and upload into Glance
702# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500703# ``FILES`` must be set to the cache dir
704# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -0500705# upload_image image-url glance-token
706function upload_image() {
707 local image_url=$1
708 local token=$2
709
710 # Create a directory for the downloaded image tarballs.
711 mkdir -p $FILES/images
712
713 # Downloads the image (uec ami+aki style), then extracts it.
714 IMAGE_FNAME=`basename "$image_url"`
715 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
716 wget -c $image_url -O $FILES/$IMAGE_FNAME
717 if [[ $? -ne 0 ]]; then
718 echo "Not found: $image_url"
719 return
720 fi
721 fi
722
723 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
724 if [[ "$image_url" =~ 'openvz' ]]; then
725 IMAGE="$FILES/${IMAGE_FNAME}"
726 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
727 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}"
728 return
729 fi
730
731 KERNEL=""
732 RAMDISK=""
733 DISK_FORMAT=""
734 CONTAINER_FORMAT=""
735 UNPACK=""
736 case "$IMAGE_FNAME" in
737 *.tar.gz|*.tgz)
738 # Extract ami and aki files
739 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
740 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
741 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
742 xdir="$FILES/images/$IMAGE_NAME"
743 rm -Rf "$xdir";
744 mkdir "$xdir"
745 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
746 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
747 [ -f "$f" ] && echo "$f" && break; done; true)
748 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
749 [ -f "$f" ] && echo "$f" && break; done; true)
750 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
751 [ -f "$f" ] && echo "$f" && break; done; true)
752 if [[ -z "$IMAGE_NAME" ]]; then
753 IMAGE_NAME=$(basename "$IMAGE" ".img")
754 fi
755 ;;
756 *.img)
757 IMAGE="$FILES/$IMAGE_FNAME";
758 IMAGE_NAME=$(basename "$IMAGE" ".img")
759 DISK_FORMAT=raw
760 CONTAINER_FORMAT=bare
761 ;;
762 *.img.gz)
763 IMAGE="$FILES/${IMAGE_FNAME}"
764 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
765 DISK_FORMAT=raw
766 CONTAINER_FORMAT=bare
767 UNPACK=zcat
768 ;;
769 *.qcow2)
770 IMAGE="$FILES/${IMAGE_FNAME}"
771 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
772 DISK_FORMAT=qcow2
773 CONTAINER_FORMAT=bare
774 ;;
775 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
776 esac
777
778 if [ "$CONTAINER_FORMAT" = "bare" ]; then
779 if [ "$UNPACK" = "zcat" ]; then
780 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}")
781 else
782 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}"
783 fi
784 else
785 # Use glance client to add the kernel the root filesystem.
786 # We parse the results of the first upload to get the glance ID of the
787 # kernel for use when uploading the root filesystem.
788 KERNEL_ID=""; RAMDISK_ID="";
789 if [ -n "$KERNEL" ]; then
790 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)
791 fi
792 if [ -n "$RAMDISK" ]; then
793 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)
794 fi
795 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}"
796 fi
797}
798
799
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500800# Wrapper for ``yum`` to set proxy environment variables
801# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500802# yum_install package [package ...]
803function yum_install() {
804 [[ "$OFFLINE" = "True" ]] && return
805 local sudo="sudo"
806 [[ "$(id -u)" = "0" ]] && sudo="env"
807 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900808 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500809 yum install -y "$@"
810}
811
812
Dean Troyer27e32692012-03-16 16:16:56 -0500813# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000814$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500815
816
817# Local variables:
818# -*- mode: Shell-script -*-
819# End: