blob: 664cfa0cbea73f2c4fd93877e2041db26c4f3745 [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
Andrew Laskif900bd72012-09-05 17:23:14 -0400281# git update using reference as a branch.
282# git_update_remote_branch ref
283function git_update_remote_branch() {
284
285 GIT_BRANCH=$1
286
287 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
288}
289
290
Dean Troyera9e0a482012-07-09 14:07:23 -0500291# Translate the OS version values into common nomenclature
292# Sets ``DISTRO`` from the ``os_*`` values
293function GetDistro() {
294 GetOSVersion
295 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
296 # 'Everyone' refers to Ubuntu releases by the code name adjective
297 DISTRO=$os_CODENAME
298 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
299 # For Fedora, just use 'f' and the release
300 DISTRO="f$os_RELEASE"
301 else
302 # Catch-all for now is Vendor + Release + Update
303 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
304 fi
305 export DISTRO
306}
307
308
Dean Troyer7f9aa712012-01-31 12:11:56 -0600309# git clone only if directory doesn't exist already. Since ``DEST`` might not
310# be owned by the installation user, we create the directory and change the
311# ownership to the proper user.
312# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700313# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
314# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500315# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600316# git_clone remote dest-dir branch
317function git_clone {
318 [[ "$OFFLINE" = "True" ]] && return
319
320 GIT_REMOTE=$1
321 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300322 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600323
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300324 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600325 # If our branch name is a gerrit style refs/changes/...
326 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700327 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600328 git clone $GIT_REMOTE $GIT_DEST
329 fi
330 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300331 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600332 else
333 # do a full clone only if the directory doesn't exist
334 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700335 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600336 git clone $GIT_REMOTE $GIT_DEST
337 cd $GIT_DEST
338 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300339 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600340 elif [[ "$RECLONE" == "yes" ]]; then
341 # if it does exist then simulate what clone does if asked to RECLONE
342 cd $GIT_DEST
343 # set the url to pull from and fetch
344 git remote set-url origin $GIT_REMOTE
345 git fetch origin
346 # remove the existing ignored files (like pyc) as they cause breakage
347 # (due to the py files having older timestamps than our pyc, so python
348 # thinks the pyc files are correct using them)
349 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300350
351 # handle GIT_REF accordingly to type (tag, branch)
352 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
353 git_update_tag $GIT_REF
354 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
355 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400356 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
357 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300358 else
359 echo $GIT_REF is neither branch nor tag
360 exit 1
361 fi
362
Dean Troyer7f9aa712012-01-31 12:11:56 -0600363 fi
364 fi
365}
366
367
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500368# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200369# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500370function inicomment() {
371 local file=$1
372 local section=$2
373 local option=$3
374 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
375}
376
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200377# Uncomment an option in an INI file
378# iniuncomment config-file section option
379function iniuncomment() {
380 local file=$1
381 local section=$2
382 local option=$3
383 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
384}
385
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500386
387# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500388# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500389function iniget() {
390 local file=$1
391 local section=$2
392 local option=$3
393 local line
394 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
395 echo ${line#*=}
396}
397
398
399# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500400# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500401function iniset() {
402 local file=$1
403 local section=$2
404 local option=$3
405 local value=$4
Dean Troyer09e636e2012-03-19 16:31:12 -0500406 if ! grep -q "^\[$section\]" $file; then
407 # Add section at the end
408 echo -e "\n[$section]" >>$file
409 fi
410 if [[ -z "$(iniget $file $section $option)" ]]; then
411 # Add it
412 sed -i -e "/^\[$section\]/ a\\
413$option = $value
414" $file
415 else
416 # Replace it
417 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
418 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500419}
420
421
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000422# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500423# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000424#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500425# Multiple services specified as arguments are ``OR``'ed together; the test
426# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000427#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500428# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000429# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500430# **cinder** returns true if any service enabled start with **c-**
431# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000432# **glance** returns true if any service enabled start with **g-**
433# **quantum** returns true if any service enabled start with **q-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500434#
435# Uses global ``ENABLED_SERVICES``
436# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000437function is_service_enabled() {
438 services=$@
439 for service in ${services}; do
440 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
441 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500442 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700443 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000444 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
445 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
446 done
447 return 1
448}
449
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500450
451# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
452# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400453function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500454 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400455 s/,,/,/g;
456 s/^,//;
457 s/,$//
458 '
459}
460
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500461
Doug Hellmannf04178f2012-07-05 17:10:03 -0400462# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500463# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400464#
465# For example:
Doug Hellmannf04178f2012-07-05 17:10:03 -0400466# enable_service n-vol
467#
468# This function does not know about the special cases
469# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500470# Uses global ``ENABLED_SERVICES``
471# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400472function enable_service() {
473 local tmpsvcs="${ENABLED_SERVICES}"
474 for service in $@; do
475 if ! is_service_enabled $service; then
476 tmpsvcs+=",$service"
477 fi
478 done
479 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
480 disable_negated_services
481}
482
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500483
Doug Hellmannf04178f2012-07-05 17:10:03 -0400484# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500485# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400486#
487# For example:
Doug Hellmannf04178f2012-07-05 17:10:03 -0400488# disable_service n-vol
489#
490# This function does not know about the special cases
491# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500492# Uses global ``ENABLED_SERVICES``
493# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400494function disable_service() {
495 local tmpsvcs=",${ENABLED_SERVICES},"
496 local service
497 for service in $@; do
498 if is_service_enabled $service; then
499 tmpsvcs=${tmpsvcs//,$service,/,}
500 fi
501 done
502 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
503}
504
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500505
Doug Hellmannf04178f2012-07-05 17:10:03 -0400506# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500507# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400508# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500509# Uses global ``ENABLED_SERVICES``
510# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400511function disable_all_services() {
512 ENABLED_SERVICES=""
513}
514
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500515
516# Remove all services starting with '-'. For example, to install all default
517# services except nova-volume (n-vol) set in ``localrc``:
Doug Hellmannf04178f2012-07-05 17:10:03 -0400518# ENABLED_SERVICES+=",-n-vol"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500519# Uses global ``ENABLED_SERVICES``
520# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400521function disable_negated_services() {
522 local tmpsvcs="${ENABLED_SERVICES}"
523 local service
524 for service in ${tmpsvcs//,/ }; do
525 if [[ ${service} == -* ]]; then
526 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
527 fi
528 done
529 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
530}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600531
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500532
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500533# Distro-agnostic package installer
534# install_package package [package ...]
535function install_package() {
536 if [[ -z "$os_PACKAGE" ]]; then
537 GetOSVersion
538 fi
Vincent Untzc0482e62012-06-12 11:30:43 +0200539
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500540 if [[ "$os_PACKAGE" = "deb" ]]; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200541 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
542 NO_UPDATE_REPOS=True
543
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500544 apt_get install "$@"
545 else
546 yum_install "$@"
547 fi
548}
549
550
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200551# Distro-agnostic function to tell if a package is installed
552# is_package_installed package [package ...]
553function is_package_installed() {
554 if [[ -z "$@" ]]; then
555 return 1
556 fi
557
558 if [[ -z "$os_PACKAGE" ]]; then
559 GetOSVersion
560 fi
561 if [[ "$os_PACKAGE" = "deb" ]]; then
562 dpkg -l "$@" > /dev/null
563 return $?
564 else
565 rpm --quiet -q "$@"
566 return $?
567 fi
568}
569
570
Dean Troyer489bd2a2012-03-02 10:44:29 -0600571# Test if the named environment variable is set and not zero length
572# is_set env-var
573function is_set() {
574 local var=\$"$1"
575 if eval "[ -z $var ]"; then
576 return 1
577 fi
578 return 0
579}
580
581
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500582# Wrapper for ``pip install`` to set cache and proxy environment variables
583# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``TRACK_DEPENDES``, ``*_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
Monty Taylor47f02062012-07-26 11:09:24 -0500602 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600603 HTTP_PROXY=$http_proxy \
604 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900605 NO_PROXY=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500606 $CMD_PIP install --use-mirrors $@
607}
608
609
610# Service wrapper to restart services
611# restart_service service-name
612function restart_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600613 if [[ -z "$os_PACKAGE" ]]; then
614 GetOSVersion
615 fi
616 if [[ "$os_PACKAGE" = "deb" ]]; then
617 sudo /usr/sbin/service $1 restart
618 else
619 sudo /sbin/service $1 restart
620 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500621}
622
623
Dean Troyer15733352012-09-06 11:51:30 -0500624# Helper to launch a service in a named screen
625# screen_it service "command-line"
626function screen_it {
627 NL=`echo -ne '\015'`
628 SCREEN_NAME=${SCREEN_NAME:-stack}
629 if is_service_enabled $1; then
630 # Append the service to the screen rc file
631 screen_rc "$1" "$2"
632
633 screen -S $SCREEN_NAME -X screen -t $1
634 # sleep to allow bash to be ready to be send the command - we are
635 # creating a new window in screen and then sends characters, so if
636 # bash isn't running by the time we send the command, nothing happens
637 sleep 1.5
638
639 if [[ -n ${SCREEN_LOGDIR} ]]; then
640 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
641 screen -S $SCREEN_NAME -p $1 -X log on
642 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
643 fi
644 screen -S $SCREEN_NAME -p $1 -X stuff "$2$NL"
645 fi
646}
647
648
649# Screen rc file builder
650# screen_rc service "command-line"
651function screen_rc {
652 SCREEN_NAME=${SCREEN_NAME:-stack}
653 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
654 if [[ ! -e $SCREENRC ]]; then
655 # Name the screen session
656 echo "sessionname $SCREEN_NAME" > $SCREENRC
657 # Set a reasonable statusbar
658 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
659 echo "screen -t shell bash" >> $SCREENRC
660 fi
661 # If this service doesn't already exist in the screenrc file
662 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
663 NL=`echo -ne '\015'`
664 echo "screen -t $1 bash" >> $SCREENRC
665 echo "stuff \"$2$NL\"" >> $SCREENRC
666 fi
667}
668
669
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500670# ``pip install`` the dependencies of the package before ``setup.py develop``
671# so pip and not distutils processes the dependency chain
672# Uses globals ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500673# setup_develop directory
674function setup_develop() {
Monty Taylor47f02062012-07-26 11:09:24 -0500675 if [[ $TRACK_DEPENDS = True ]] ; then
676 SUDO_CMD="env"
677 else
678 SUDO_CMD="sudo"
679 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500680 (cd $1; \
681 python setup.py egg_info; \
682 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
683 depend_links=$(echo $raw_links | xargs); \
684 pip_install -r *-info/requires.txt $depend_links; \
Monty Taylor47f02062012-07-26 11:09:24 -0500685 $SUDO_CMD \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500686 HTTP_PROXY=$http_proxy \
687 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900688 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500689 python setup.py develop \
690 )
691}
692
693
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500694# Service wrapper to start services
695# start_service service-name
696function start_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600697 if [[ -z "$os_PACKAGE" ]]; then
698 GetOSVersion
699 fi
700 if [[ "$os_PACKAGE" = "deb" ]]; then
701 sudo /usr/sbin/service $1 start
702 else
703 sudo /sbin/service $1 start
704 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500705}
706
707
708# Service wrapper to stop services
709# stop_service service-name
710function stop_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600711 if [[ -z "$os_PACKAGE" ]]; then
712 GetOSVersion
713 fi
714 if [[ "$os_PACKAGE" = "deb" ]]; then
715 sudo /usr/sbin/service $1 stop
716 else
717 sudo /sbin/service $1 stop
718 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600719}
720
721
722# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500723# Accepts as False: 0 no false False FALSE
724# Accepts as True: 1 yes true True TRUE
725# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -0600726function trueorfalse() {
727 local default=$1
728 local testval=$2
729
730 [[ -z "$testval" ]] && { echo "$default"; return; }
731 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
732 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
733 echo "$default"
734}
Dean Troyer27e32692012-03-16 16:16:56 -0500735
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500736
Dean Troyerca0e3d02012-04-13 15:58:37 -0500737# Retrieve an image from a URL and upload into Glance
738# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500739# ``FILES`` must be set to the cache dir
740# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -0500741# upload_image image-url glance-token
742function upload_image() {
743 local image_url=$1
744 local token=$2
745
746 # Create a directory for the downloaded image tarballs.
747 mkdir -p $FILES/images
748
749 # Downloads the image (uec ami+aki style), then extracts it.
750 IMAGE_FNAME=`basename "$image_url"`
751 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
752 wget -c $image_url -O $FILES/$IMAGE_FNAME
753 if [[ $? -ne 0 ]]; then
754 echo "Not found: $image_url"
755 return
756 fi
757 fi
758
759 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
760 if [[ "$image_url" =~ 'openvz' ]]; then
761 IMAGE="$FILES/${IMAGE_FNAME}"
762 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
763 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}"
764 return
765 fi
766
767 KERNEL=""
768 RAMDISK=""
769 DISK_FORMAT=""
770 CONTAINER_FORMAT=""
771 UNPACK=""
772 case "$IMAGE_FNAME" in
773 *.tar.gz|*.tgz)
774 # Extract ami and aki files
775 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
776 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
777 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
778 xdir="$FILES/images/$IMAGE_NAME"
779 rm -Rf "$xdir";
780 mkdir "$xdir"
781 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
782 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
783 [ -f "$f" ] && echo "$f" && break; done; true)
784 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
785 [ -f "$f" ] && echo "$f" && break; done; true)
786 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
787 [ -f "$f" ] && echo "$f" && break; done; true)
788 if [[ -z "$IMAGE_NAME" ]]; then
789 IMAGE_NAME=$(basename "$IMAGE" ".img")
790 fi
791 ;;
792 *.img)
793 IMAGE="$FILES/$IMAGE_FNAME";
794 IMAGE_NAME=$(basename "$IMAGE" ".img")
795 DISK_FORMAT=raw
796 CONTAINER_FORMAT=bare
797 ;;
798 *.img.gz)
799 IMAGE="$FILES/${IMAGE_FNAME}"
800 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
801 DISK_FORMAT=raw
802 CONTAINER_FORMAT=bare
803 UNPACK=zcat
804 ;;
805 *.qcow2)
806 IMAGE="$FILES/${IMAGE_FNAME}"
807 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
808 DISK_FORMAT=qcow2
809 CONTAINER_FORMAT=bare
810 ;;
811 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
812 esac
813
814 if [ "$CONTAINER_FORMAT" = "bare" ]; then
815 if [ "$UNPACK" = "zcat" ]; then
816 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}")
817 else
818 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}"
819 fi
820 else
821 # Use glance client to add the kernel the root filesystem.
822 # We parse the results of the first upload to get the glance ID of the
823 # kernel for use when uploading the root filesystem.
824 KERNEL_ID=""; RAMDISK_ID="";
825 if [ -n "$KERNEL" ]; then
826 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)
827 fi
828 if [ -n "$RAMDISK" ]; then
829 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)
830 fi
831 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}"
832 fi
833}
834
835
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500836# Wrapper for ``yum`` to set proxy environment variables
837# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500838# yum_install package [package ...]
839function yum_install() {
840 [[ "$OFFLINE" = "True" ]] && return
841 local sudo="sudo"
842 [[ "$(id -u)" = "0" ]] && sudo="env"
843 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900844 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500845 yum install -y "$@"
846}
847
848
Dean Troyer27e32692012-03-16 16:16:56 -0500849# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000850$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500851
852
853# Local variables:
854# -*- mode: Shell-script -*-
Andrew Laskif900bd72012-09-05 17:23:14 -0400855# End: