blob: 0d0df51fad997417f94489e70e3e0df34f25d046 [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``
10# ``RECLONE``
11# ``TRACK_DEPENDS``
12# ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer13dc5cc2012-03-27 14:50:45 -050013
Dean Troyer7f9aa712012-01-31 12:11:56 -060014
Dean Troyer27e32692012-03-16 16:16:56 -050015# Save trace setting
16XTRACE=$(set +o | grep xtrace)
17set +o xtrace
18
Dean Troyer7f9aa712012-01-31 12:11:56 -060019
Dean Troyer4a43b7b2012-08-28 17:43:40 -050020# Exit 0 if address is in network or 1 if address is not in
21# network or netaddr library is not installed.
22# address_in_net ip-address ip-range
Vishvananda Ishayac9ad14b2012-07-03 20:29:01 +000023function address_in_net() {
24 python -c "
25import netaddr
26import sys
27sys.exit(netaddr.IPAddress('$1') not in netaddr.IPNetwork('$2'))
28"
29}
30
31
Dean Troyer4a43b7b2012-08-28 17:43:40 -050032# Wrapper for ``apt-get`` to set cache and proxy environment variables
33# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -050034# apt_get operation package [package ...]
Dean Troyer7f9aa712012-01-31 12:11:56 -060035function apt_get() {
Dean Troyerd0b21e22012-03-07 14:52:25 -060036 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer7f9aa712012-01-31 12:11:56 -060037 local sudo="sudo"
38 [[ "$(id -u)" = "0" ]] && sudo="env"
39 $sudo DEBIAN_FRONTEND=noninteractive \
40 http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +090041 no_proxy=$no_proxy \
Dean Troyer7f9aa712012-01-31 12:11:56 -060042 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
43}
44
45
46# Gracefully cp only if source file/dir exists
47# cp_it source destination
48function cp_it {
49 if [ -e $1 ] || [ -d $1 ]; then
50 cp -pRL $1 $2
51 fi
52}
53
54
Dean Troyer27e32692012-03-16 16:16:56 -050055# Prints "message" and exits
56# die "message"
57function die() {
Dean Troyer489bd2a2012-03-02 10:44:29 -060058 local exitcode=$?
Dean Troyer27e32692012-03-16 16:16:56 -050059 set +o xtrace
60 echo $@
61 exit $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -060062}
63
64
65# Checks an environment variable is not set or has length 0 OR if the
66# exit code is non-zero and prints "message" and exits
67# NOTE: env-var is the variable name without a '$'
68# die_if_not_set env-var "message"
69function die_if_not_set() {
Dean Troyer27e32692012-03-16 16:16:56 -050070 (
71 local exitcode=$?
72 set +o xtrace
73 local evar=$1; shift
74 if ! is_set $evar || [ $exitcode != 0 ]; then
75 set +o xtrace
76 echo $@
77 exit -1
78 fi
79 )
Dean Troyer489bd2a2012-03-02 10:44:29 -060080}
81
82
83# Grab a numbered field from python prettytable output
84# Fields are numbered starting with 1
85# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
86# get_field field-number
87function get_field() {
88 while read data; do
89 if [ "$1" -lt 0 ]; then
90 field="(\$(NF$1))"
91 else
92 field="\$$(($1 + 1))"
93 fi
94 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
95 done
96}
97
98
Dean Troyer7e270512012-06-14 15:23:24 -050099# get_packages() collects a list of package names of any type from the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500100# prerequisite files in ``files/{apts|rpms}``. The list is intended
101# to be passed to a package installer such as apt or yum.
Dean Troyer7e270512012-06-14 15:23:24 -0500102#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500103# Only packages required for the services in ``ENABLED_SERVICES`` will be
Dean Troyer7e270512012-06-14 15:23:24 -0500104# included. Two bits of metadata are recognized in the prerequisite files:
105# - ``# NOPRIME`` defers installation to be performed later in stack.sh
106# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
107# of the package to the distros listed. The distro names are case insensitive.
108#
Vincent Untz855c5872012-10-04 13:36:46 +0200109# Uses globals ``ENABLED_SERVICES``
Dean Troyer7e270512012-06-14 15:23:24 -0500110# get_packages dir
111function get_packages() {
112 local package_dir=$1
113 local file_to_parse
114 local service
115
116 if [[ -z "$package_dir" ]]; then
117 echo "No package directory supplied"
118 return 1
119 fi
120 if [[ -z "$DISTRO" ]]; then
Vincent Untz855c5872012-10-04 13:36:46 +0200121 GetDistro
Dean Troyer7e270512012-06-14 15:23:24 -0500122 fi
123 for service in general ${ENABLED_SERVICES//,/ }; do
124 # Allow individual services to specify dependencies
125 if [[ -e ${package_dir}/${service} ]]; then
126 file_to_parse="${file_to_parse} $service"
127 fi
128 # NOTE(sdague) n-api needs glance for now because that's where
129 # glance client is
130 if [[ $service == n-api ]]; then
131 if [[ ! $file_to_parse =~ nova ]]; then
132 file_to_parse="${file_to_parse} nova"
133 fi
134 if [[ ! $file_to_parse =~ glance ]]; then
135 file_to_parse="${file_to_parse} glance"
136 fi
137 elif [[ $service == c-* ]]; then
138 if [[ ! $file_to_parse =~ cinder ]]; then
139 file_to_parse="${file_to_parse} cinder"
140 fi
John H. Tran93361642012-07-26 11:22:05 -0700141 elif [[ $service == ceilometer-* ]]; then
142 if [[ ! $file_to_parse =~ ceilometer ]]; then
143 file_to_parse="${file_to_parse} ceilometer"
144 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500145 elif [[ $service == n-* ]]; then
146 if [[ ! $file_to_parse =~ nova ]]; then
147 file_to_parse="${file_to_parse} nova"
148 fi
149 elif [[ $service == g-* ]]; then
150 if [[ ! $file_to_parse =~ glance ]]; then
151 file_to_parse="${file_to_parse} glance"
152 fi
153 elif [[ $service == key* ]]; then
154 if [[ ! $file_to_parse =~ keystone ]]; then
155 file_to_parse="${file_to_parse} keystone"
156 fi
157 fi
158 done
159
160 for file in ${file_to_parse}; do
161 local fname=${package_dir}/${file}
162 local OIFS line package distros distro
163 [[ -e $fname ]] || continue
164
165 OIFS=$IFS
166 IFS=$'\n'
167 for line in $(<${fname}); do
168 if [[ $line =~ "NOPRIME" ]]; then
169 continue
170 fi
171
172 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
173 # We are using BASH regexp matching feature.
174 package=${BASH_REMATCH[1]}
175 distros=${BASH_REMATCH[2]}
176 # In bash ${VAR,,} will lowecase VAR
177 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
178 continue
179 fi
180
181 echo ${line%#*}
182 done
183 IFS=$OIFS
184 done
185}
186
187
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500188# Determine OS Vendor, Release and Update
189# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
190# Returns results in global variables:
191# os_VENDOR - vendor name
192# os_RELEASE - release
193# os_UPDATE - update
194# os_PACKAGE - package type
195# os_CODENAME - vendor's codename for release
196# GetOSVersion
197GetOSVersion() {
198 # Figure out which vendor we are
199 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
200 # OS/X
201 os_VENDOR=`sw_vers -productName`
202 os_RELEASE=`sw_vers -productVersion`
203 os_UPDATE=${os_RELEASE##*.}
204 os_RELEASE=${os_RELEASE%.*}
205 os_PACKAGE=""
206 if [[ "$os_RELEASE" =~ "10.7" ]]; then
207 os_CODENAME="lion"
208 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
209 os_CODENAME="snow leopard"
210 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
211 os_CODENAME="leopard"
212 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
213 os_CODENAME="tiger"
214 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
215 os_CODENAME="panther"
216 else
217 os_CODENAME=""
218 fi
219 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
220 os_VENDOR=$(lsb_release -i -s)
221 os_RELEASE=$(lsb_release -r -s)
222 os_UPDATE=""
223 if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
224 os_PACKAGE="deb"
225 else
226 os_PACKAGE="rpm"
227 fi
228 os_CODENAME=$(lsb_release -c -s)
229 elif [[ -r /etc/redhat-release ]]; then
230 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
231 # CentOS release 5.5 (Final)
232 # CentOS Linux release 6.0 (Final)
233 # Fedora release 16 (Verne)
234 os_CODENAME=""
235 for r in "Red Hat" CentOS Fedora; do
236 os_VENDOR=$r
237 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
238 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
239 os_CODENAME=${ver#*|}
240 os_RELEASE=${ver%|*}
241 os_UPDATE=${os_RELEASE##*.}
242 os_RELEASE=${os_RELEASE%.*}
243 break
244 fi
245 os_VENDOR=""
246 done
247 os_PACKAGE="rpm"
248 fi
249 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
250}
251
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300252# git update using reference as a branch.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500253# git_update_branch ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300254function git_update_branch() {
255
256 GIT_BRANCH=$1
257
258 git checkout -f origin/$GIT_BRANCH
259 # a local branch might not exist
260 git branch -D $GIT_BRANCH || true
261 git checkout -b $GIT_BRANCH
262}
263
264
265# git update using reference as a tag. Be careful editing source at that repo
266# as working copy will be in a detached mode
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500267# git_update_tag ref
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300268function git_update_tag() {
269
270 GIT_TAG=$1
271
272 git tag -d $GIT_TAG
273 # fetching given tag only
274 git fetch origin tag $GIT_TAG
275 git checkout -f $GIT_TAG
276}
277
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500278
Andrew Laskif900bd72012-09-05 17:23:14 -0400279# git update using reference as a branch.
280# git_update_remote_branch ref
281function git_update_remote_branch() {
282
283 GIT_BRANCH=$1
284
285 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
286}
287
288
Dean Troyera9e0a482012-07-09 14:07:23 -0500289# Translate the OS version values into common nomenclature
290# Sets ``DISTRO`` from the ``os_*`` values
291function GetDistro() {
292 GetOSVersion
293 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
294 # 'Everyone' refers to Ubuntu releases by the code name adjective
295 DISTRO=$os_CODENAME
296 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
297 # For Fedora, just use 'f' and the release
298 DISTRO="f$os_RELEASE"
299 else
300 # Catch-all for now is Vendor + Release + Update
301 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
302 fi
303 export DISTRO
304}
305
306
Dean Troyer7f9aa712012-01-31 12:11:56 -0600307# git clone only if directory doesn't exist already. Since ``DEST`` might not
308# be owned by the installation user, we create the directory and change the
309# ownership to the proper user.
310# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700311# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
312# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500313# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600314# git_clone remote dest-dir branch
315function git_clone {
316 [[ "$OFFLINE" = "True" ]] && return
317
318 GIT_REMOTE=$1
319 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300320 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600321
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300322 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600323 # If our branch name is a gerrit style refs/changes/...
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 fi
328 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300329 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600330 else
331 # do a full clone only if the directory doesn't exist
332 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700333 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600334 git clone $GIT_REMOTE $GIT_DEST
335 cd $GIT_DEST
336 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300337 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600338 elif [[ "$RECLONE" == "yes" ]]; then
339 # if it does exist then simulate what clone does if asked to RECLONE
340 cd $GIT_DEST
341 # set the url to pull from and fetch
342 git remote set-url origin $GIT_REMOTE
343 git fetch origin
344 # remove the existing ignored files (like pyc) as they cause breakage
345 # (due to the py files having older timestamps than our pyc, so python
346 # thinks the pyc files are correct using them)
347 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300348
349 # handle GIT_REF accordingly to type (tag, branch)
350 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
351 git_update_tag $GIT_REF
352 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
353 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400354 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
355 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300356 else
357 echo $GIT_REF is neither branch nor tag
358 exit 1
359 fi
360
Dean Troyer7f9aa712012-01-31 12:11:56 -0600361 fi
362 fi
363}
364
365
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500366# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200367# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500368function inicomment() {
369 local file=$1
370 local section=$2
371 local option=$3
372 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
373}
374
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200375# Uncomment an option in an INI file
376# iniuncomment config-file section option
377function iniuncomment() {
378 local file=$1
379 local section=$2
380 local option=$3
381 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
382}
383
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500384
385# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500386# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500387function iniget() {
388 local file=$1
389 local section=$2
390 local option=$3
391 local line
392 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
393 echo ${line#*=}
394}
395
396
397# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500398# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500399function iniset() {
400 local file=$1
401 local section=$2
402 local option=$3
403 local value=$4
Dean Troyer09e636e2012-03-19 16:31:12 -0500404 if ! grep -q "^\[$section\]" $file; then
405 # Add section at the end
406 echo -e "\n[$section]" >>$file
407 fi
408 if [[ -z "$(iniget $file $section $option)" ]]; then
409 # Add it
410 sed -i -e "/^\[$section\]/ a\\
411$option = $value
412" $file
413 else
414 # Replace it
415 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
416 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500417}
418
419
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000420# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500421# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000422#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500423# Multiple services specified as arguments are ``OR``'ed together; the test
424# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000425#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500426# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000427# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500428# **cinder** returns true if any service enabled start with **c-**
429# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000430# **glance** returns true if any service enabled start with **g-**
431# **quantum** returns true if any service enabled start with **q-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500432#
433# Uses global ``ENABLED_SERVICES``
434# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000435function is_service_enabled() {
436 services=$@
437 for service in ${services}; do
438 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
439 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500440 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700441 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000442 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
443 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
444 done
445 return 1
446}
447
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500448
449# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
450# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400451function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500452 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400453 s/,,/,/g;
454 s/^,//;
455 s/,$//
456 '
457}
458
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500459
Doug Hellmannf04178f2012-07-05 17:10:03 -0400460# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500461# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400462#
463# For example:
Doug Hellmannf04178f2012-07-05 17:10:03 -0400464# enable_service n-vol
465#
466# This function does not know about the special cases
467# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500468# Uses global ``ENABLED_SERVICES``
469# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400470function enable_service() {
471 local tmpsvcs="${ENABLED_SERVICES}"
472 for service in $@; do
473 if ! is_service_enabled $service; then
474 tmpsvcs+=",$service"
475 fi
476 done
477 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
478 disable_negated_services
479}
480
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500481
Doug Hellmannf04178f2012-07-05 17:10:03 -0400482# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500483# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400484#
485# For example:
Doug Hellmannf04178f2012-07-05 17:10:03 -0400486# disable_service n-vol
487#
488# This function does not know about the special cases
489# for nova, glance, and quantum built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500490# Uses global ``ENABLED_SERVICES``
491# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400492function disable_service() {
493 local tmpsvcs=",${ENABLED_SERVICES},"
494 local service
495 for service in $@; do
496 if is_service_enabled $service; then
497 tmpsvcs=${tmpsvcs//,$service,/,}
498 fi
499 done
500 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
501}
502
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500503
Doug Hellmannf04178f2012-07-05 17:10:03 -0400504# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500505# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400506# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500507# Uses global ``ENABLED_SERVICES``
508# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400509function disable_all_services() {
510 ENABLED_SERVICES=""
511}
512
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500513
514# Remove all services starting with '-'. For example, to install all default
515# services except nova-volume (n-vol) set in ``localrc``:
Doug Hellmannf04178f2012-07-05 17:10:03 -0400516# ENABLED_SERVICES+=",-n-vol"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500517# Uses global ``ENABLED_SERVICES``
518# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400519function disable_negated_services() {
520 local tmpsvcs="${ENABLED_SERVICES}"
521 local service
522 for service in ${tmpsvcs//,/ }; do
523 if [[ ${service} == -* ]]; then
524 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
525 fi
526 done
527 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
528}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600529
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500530
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500531# Distro-agnostic package installer
532# install_package package [package ...]
533function install_package() {
534 if [[ -z "$os_PACKAGE" ]]; then
535 GetOSVersion
536 fi
Vincent Untzc0482e62012-06-12 11:30:43 +0200537
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500538 if [[ "$os_PACKAGE" = "deb" ]]; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200539 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
540 NO_UPDATE_REPOS=True
541
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500542 apt_get install "$@"
543 else
544 yum_install "$@"
545 fi
546}
547
548
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200549# Distro-agnostic function to tell if a package is installed
550# is_package_installed package [package ...]
551function is_package_installed() {
552 if [[ -z "$@" ]]; then
553 return 1
554 fi
555
556 if [[ -z "$os_PACKAGE" ]]; then
557 GetOSVersion
558 fi
559 if [[ "$os_PACKAGE" = "deb" ]]; then
560 dpkg -l "$@" > /dev/null
561 return $?
562 else
563 rpm --quiet -q "$@"
564 return $?
565 fi
566}
567
568
Dean Troyer489bd2a2012-03-02 10:44:29 -0600569# Test if the named environment variable is set and not zero length
570# is_set env-var
571function is_set() {
572 local var=\$"$1"
573 if eval "[ -z $var ]"; then
574 return 1
575 fi
576 return 0
577}
578
579
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500580# Wrapper for ``pip install`` to set cache and proxy environment variables
581# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyer7f9aa712012-01-31 12:11:56 -0600582# pip_install package [package ...]
583function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600584 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500585 if [[ -z "$os_PACKAGE" ]]; then
586 GetOSVersion
587 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500588 if [[ $TRACK_DEPENDS = True ]] ; then
589 source $DEST/.venv/bin/activate
590 CMD_PIP=$DEST/.venv/bin/pip
591 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500592 else
Monty Taylor47f02062012-07-26 11:09:24 -0500593 SUDO_PIP="sudo"
594 if [[ "$os_PACKAGE" = "deb" ]]; then
595 CMD_PIP=/usr/bin/pip
596 else
597 CMD_PIP=/usr/bin/pip-python
598 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500599 fi
Monty Taylor47f02062012-07-26 11:09:24 -0500600 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600601 HTTP_PROXY=$http_proxy \
602 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900603 NO_PROXY=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500604 $CMD_PIP install --use-mirrors $@
605}
606
607
608# Service wrapper to restart services
609# restart_service service-name
610function restart_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600611 if [[ -z "$os_PACKAGE" ]]; then
612 GetOSVersion
613 fi
614 if [[ "$os_PACKAGE" = "deb" ]]; then
615 sudo /usr/sbin/service $1 restart
616 else
617 sudo /sbin/service $1 restart
618 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500619}
620
621
Dean Troyer15733352012-09-06 11:51:30 -0500622# Helper to launch a service in a named screen
623# screen_it service "command-line"
624function screen_it {
625 NL=`echo -ne '\015'`
626 SCREEN_NAME=${SCREEN_NAME:-stack}
627 if is_service_enabled $1; then
628 # Append the service to the screen rc file
629 screen_rc "$1" "$2"
630
631 screen -S $SCREEN_NAME -X screen -t $1
632 # sleep to allow bash to be ready to be send the command - we are
633 # creating a new window in screen and then sends characters, so if
634 # bash isn't running by the time we send the command, nothing happens
635 sleep 1.5
636
637 if [[ -n ${SCREEN_LOGDIR} ]]; then
638 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
639 screen -S $SCREEN_NAME -p $1 -X log on
640 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
641 fi
642 screen -S $SCREEN_NAME -p $1 -X stuff "$2$NL"
643 fi
644}
645
646
647# Screen rc file builder
648# screen_rc service "command-line"
649function screen_rc {
650 SCREEN_NAME=${SCREEN_NAME:-stack}
651 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
652 if [[ ! -e $SCREENRC ]]; then
653 # Name the screen session
654 echo "sessionname $SCREEN_NAME" > $SCREENRC
655 # Set a reasonable statusbar
656 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
657 echo "screen -t shell bash" >> $SCREENRC
658 fi
659 # If this service doesn't already exist in the screenrc file
660 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
661 NL=`echo -ne '\015'`
662 echo "screen -t $1 bash" >> $SCREENRC
663 echo "stuff \"$2$NL\"" >> $SCREENRC
664 fi
665}
666
667
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500668# ``pip install`` the dependencies of the package before ``setup.py develop``
669# so pip and not distutils processes the dependency chain
670# Uses globals ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500671# setup_develop directory
672function setup_develop() {
Monty Taylor47f02062012-07-26 11:09:24 -0500673 if [[ $TRACK_DEPENDS = True ]] ; then
674 SUDO_CMD="env"
675 else
676 SUDO_CMD="sudo"
677 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500678 (cd $1; \
679 python setup.py egg_info; \
680 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
681 depend_links=$(echo $raw_links | xargs); \
Dean Troyer1a3c9fe2012-09-29 17:25:02 -0500682 require_file=$([ ! -r *-info/requires.txt ] || echo "-r *-info/requires.txt"); \
683 pip_install $require_file $depend_links; \
Monty Taylor47f02062012-07-26 11:09:24 -0500684 $SUDO_CMD \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500685 HTTP_PROXY=$http_proxy \
686 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900687 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500688 python setup.py develop \
689 )
690}
691
692
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500693# Service wrapper to start services
694# start_service service-name
695function start_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600696 if [[ -z "$os_PACKAGE" ]]; then
697 GetOSVersion
698 fi
699 if [[ "$os_PACKAGE" = "deb" ]]; then
700 sudo /usr/sbin/service $1 start
701 else
702 sudo /sbin/service $1 start
703 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500704}
705
706
707# Service wrapper to stop services
708# stop_service service-name
709function stop_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600710 if [[ -z "$os_PACKAGE" ]]; then
711 GetOSVersion
712 fi
713 if [[ "$os_PACKAGE" = "deb" ]]; then
714 sudo /usr/sbin/service $1 stop
715 else
716 sudo /sbin/service $1 stop
717 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600718}
719
720
721# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500722# Accepts as False: 0 no false False FALSE
723# Accepts as True: 1 yes true True TRUE
724# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -0600725function trueorfalse() {
726 local default=$1
727 local testval=$2
728
729 [[ -z "$testval" ]] && { echo "$default"; return; }
730 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
731 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
732 echo "$default"
733}
Dean Troyer27e32692012-03-16 16:16:56 -0500734
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500735
Dean Troyerca0e3d02012-04-13 15:58:37 -0500736# Retrieve an image from a URL and upload into Glance
737# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500738# ``FILES`` must be set to the cache dir
739# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -0500740# upload_image image-url glance-token
741function upload_image() {
742 local image_url=$1
743 local token=$2
744
745 # Create a directory for the downloaded image tarballs.
746 mkdir -p $FILES/images
747
748 # Downloads the image (uec ami+aki style), then extracts it.
749 IMAGE_FNAME=`basename "$image_url"`
750 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
751 wget -c $image_url -O $FILES/$IMAGE_FNAME
752 if [[ $? -ne 0 ]]; then
753 echo "Not found: $image_url"
754 return
755 fi
756 fi
757
758 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
759 if [[ "$image_url" =~ 'openvz' ]]; then
760 IMAGE="$FILES/${IMAGE_FNAME}"
761 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
762 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}"
763 return
764 fi
765
766 KERNEL=""
767 RAMDISK=""
768 DISK_FORMAT=""
769 CONTAINER_FORMAT=""
770 UNPACK=""
771 case "$IMAGE_FNAME" in
772 *.tar.gz|*.tgz)
773 # Extract ami and aki files
774 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
775 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
776 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
777 xdir="$FILES/images/$IMAGE_NAME"
778 rm -Rf "$xdir";
779 mkdir "$xdir"
780 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
781 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
782 [ -f "$f" ] && echo "$f" && break; done; true)
783 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
784 [ -f "$f" ] && echo "$f" && break; done; true)
785 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
786 [ -f "$f" ] && echo "$f" && break; done; true)
787 if [[ -z "$IMAGE_NAME" ]]; then
788 IMAGE_NAME=$(basename "$IMAGE" ".img")
789 fi
790 ;;
791 *.img)
792 IMAGE="$FILES/$IMAGE_FNAME";
793 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -0500794 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
795 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
796 DISK_FORMAT=$format
797 else
798 DISK_FORMAT=raw
799 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -0500800 CONTAINER_FORMAT=bare
801 ;;
802 *.img.gz)
803 IMAGE="$FILES/${IMAGE_FNAME}"
804 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
805 DISK_FORMAT=raw
806 CONTAINER_FORMAT=bare
807 UNPACK=zcat
808 ;;
809 *.qcow2)
810 IMAGE="$FILES/${IMAGE_FNAME}"
811 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
812 DISK_FORMAT=qcow2
813 CONTAINER_FORMAT=bare
814 ;;
815 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
816 esac
817
818 if [ "$CONTAINER_FORMAT" = "bare" ]; then
819 if [ "$UNPACK" = "zcat" ]; then
820 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}")
821 else
822 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}"
823 fi
824 else
825 # Use glance client to add the kernel the root filesystem.
826 # We parse the results of the first upload to get the glance ID of the
827 # kernel for use when uploading the root filesystem.
828 KERNEL_ID=""; RAMDISK_ID="";
829 if [ -n "$KERNEL" ]; then
830 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)
831 fi
832 if [ -n "$RAMDISK" ]; then
833 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)
834 fi
835 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}"
836 fi
837}
838
839
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500840# Wrapper for ``yum`` to set proxy environment variables
841# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500842# yum_install package [package ...]
843function yum_install() {
844 [[ "$OFFLINE" = "True" ]] && return
845 local sudo="sudo"
846 [[ "$(id -u)" = "0" ]] && sudo="env"
847 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900848 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500849 yum install -y "$@"
850}
851
852
Dean Troyer27e32692012-03-16 16:16:56 -0500853# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000854$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500855
856
857# Local variables:
858# -*- mode: Shell-script -*-
Andrew Laskif900bd72012-09-05 17:23:14 -0400859# End: