blob: 2f5cdbc5db345ed431fab59dc1ead59c2931d48b [file] [log] [blame]
Doug Hellmannf04178f2012-07-05 17:10:03 -04001# -*- mode: Shell-script -*-
Dean Troyer7f9aa712012-01-31 12:11:56 -06002# functions - Common functions used by DevStack components
Dean Troyer13dc5cc2012-03-27 14:50:45 -05003#
4# ENABLED_SERVICES is used by is_service_enabled()
5
Dean Troyer7f9aa712012-01-31 12:11:56 -06006
Dean Troyer27e32692012-03-16 16:16:56 -05007# Save trace setting
8XTRACE=$(set +o | grep xtrace)
9set +o xtrace
10
Dean Troyer7f9aa712012-01-31 12:11:56 -060011
12# apt-get wrapper to set arguments correctly
Dean Troyer13dc5cc2012-03-27 14:50:45 -050013# apt_get operation package [package ...]
Dean Troyer7f9aa712012-01-31 12:11:56 -060014function apt_get() {
Dean Troyerd0b21e22012-03-07 14:52:25 -060015 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer7f9aa712012-01-31 12:11:56 -060016 local sudo="sudo"
17 [[ "$(id -u)" = "0" ]] && sudo="env"
18 $sudo DEBIAN_FRONTEND=noninteractive \
19 http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +090020 no_proxy=$no_proxy \
Dean Troyer7f9aa712012-01-31 12:11:56 -060021 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
22}
23
24
25# Gracefully cp only if source file/dir exists
26# cp_it source destination
27function cp_it {
28 if [ -e $1 ] || [ -d $1 ]; then
29 cp -pRL $1 $2
30 fi
31}
32
33
Dean Troyer27e32692012-03-16 16:16:56 -050034# Prints "message" and exits
35# die "message"
36function die() {
Dean Troyer489bd2a2012-03-02 10:44:29 -060037 local exitcode=$?
Dean Troyer27e32692012-03-16 16:16:56 -050038 set +o xtrace
39 echo $@
40 exit $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -060041}
42
43
44# Checks an environment variable is not set or has length 0 OR if the
45# exit code is non-zero and prints "message" and exits
46# NOTE: env-var is the variable name without a '$'
47# die_if_not_set env-var "message"
48function die_if_not_set() {
Dean Troyer27e32692012-03-16 16:16:56 -050049 (
50 local exitcode=$?
51 set +o xtrace
52 local evar=$1; shift
53 if ! is_set $evar || [ $exitcode != 0 ]; then
54 set +o xtrace
55 echo $@
56 exit -1
57 fi
58 )
Dean Troyer489bd2a2012-03-02 10:44:29 -060059}
60
61
62# Grab a numbered field from python prettytable output
63# Fields are numbered starting with 1
64# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
65# get_field field-number
66function get_field() {
67 while read data; do
68 if [ "$1" -lt 0 ]; then
69 field="(\$(NF$1))"
70 else
71 field="\$$(($1 + 1))"
72 fi
73 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
74 done
75}
76
77
Dean Troyer7e270512012-06-14 15:23:24 -050078# get_packages() collects a list of package names of any type from the
79# prerequisite files in ``files/{apts|pips}``. The list is intended
80# to be passed to a package installer such as apt or pip.
81#
82# Only packages required for the services in ENABLED_SERVICES will be
83# included. Two bits of metadata are recognized in the prerequisite files:
84# - ``# NOPRIME`` defers installation to be performed later in stack.sh
85# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
86# of the package to the distros listed. The distro names are case insensitive.
87#
88# get_packages dir
89function get_packages() {
90 local package_dir=$1
91 local file_to_parse
92 local service
93
94 if [[ -z "$package_dir" ]]; then
95 echo "No package directory supplied"
96 return 1
97 fi
98 if [[ -z "$DISTRO" ]]; then
99 echo "No distro set in DISTRO"
100 return 1
101 fi
102 for service in general ${ENABLED_SERVICES//,/ }; do
103 # Allow individual services to specify dependencies
104 if [[ -e ${package_dir}/${service} ]]; then
105 file_to_parse="${file_to_parse} $service"
106 fi
107 # NOTE(sdague) n-api needs glance for now because that's where
108 # glance client is
109 if [[ $service == n-api ]]; then
110 if [[ ! $file_to_parse =~ nova ]]; then
111 file_to_parse="${file_to_parse} nova"
112 fi
113 if [[ ! $file_to_parse =~ glance ]]; then
114 file_to_parse="${file_to_parse} glance"
115 fi
116 elif [[ $service == c-* ]]; then
117 if [[ ! $file_to_parse =~ cinder ]]; then
118 file_to_parse="${file_to_parse} cinder"
119 fi
120 elif [[ $service == n-* ]]; then
121 if [[ ! $file_to_parse =~ nova ]]; then
122 file_to_parse="${file_to_parse} nova"
123 fi
124 elif [[ $service == g-* ]]; then
125 if [[ ! $file_to_parse =~ glance ]]; then
126 file_to_parse="${file_to_parse} glance"
127 fi
128 elif [[ $service == key* ]]; then
129 if [[ ! $file_to_parse =~ keystone ]]; then
130 file_to_parse="${file_to_parse} keystone"
131 fi
132 fi
133 done
134
135 for file in ${file_to_parse}; do
136 local fname=${package_dir}/${file}
137 local OIFS line package distros distro
138 [[ -e $fname ]] || continue
139
140 OIFS=$IFS
141 IFS=$'\n'
142 for line in $(<${fname}); do
143 if [[ $line =~ "NOPRIME" ]]; then
144 continue
145 fi
146
147 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
148 # We are using BASH regexp matching feature.
149 package=${BASH_REMATCH[1]}
150 distros=${BASH_REMATCH[2]}
151 # In bash ${VAR,,} will lowecase VAR
152 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
153 continue
154 fi
155
156 echo ${line%#*}
157 done
158 IFS=$OIFS
159 done
160}
161
162
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500163# Determine OS Vendor, Release and Update
164# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
165# Returns results in global variables:
166# os_VENDOR - vendor name
167# os_RELEASE - release
168# os_UPDATE - update
169# os_PACKAGE - package type
170# os_CODENAME - vendor's codename for release
171# GetOSVersion
172GetOSVersion() {
173 # Figure out which vendor we are
174 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
175 # OS/X
176 os_VENDOR=`sw_vers -productName`
177 os_RELEASE=`sw_vers -productVersion`
178 os_UPDATE=${os_RELEASE##*.}
179 os_RELEASE=${os_RELEASE%.*}
180 os_PACKAGE=""
181 if [[ "$os_RELEASE" =~ "10.7" ]]; then
182 os_CODENAME="lion"
183 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
184 os_CODENAME="snow leopard"
185 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
186 os_CODENAME="leopard"
187 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
188 os_CODENAME="tiger"
189 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
190 os_CODENAME="panther"
191 else
192 os_CODENAME=""
193 fi
194 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
195 os_VENDOR=$(lsb_release -i -s)
196 os_RELEASE=$(lsb_release -r -s)
197 os_UPDATE=""
198 if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
199 os_PACKAGE="deb"
200 else
201 os_PACKAGE="rpm"
202 fi
203 os_CODENAME=$(lsb_release -c -s)
204 elif [[ -r /etc/redhat-release ]]; then
205 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
206 # CentOS release 5.5 (Final)
207 # CentOS Linux release 6.0 (Final)
208 # Fedora release 16 (Verne)
209 os_CODENAME=""
210 for r in "Red Hat" CentOS Fedora; do
211 os_VENDOR=$r
212 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
213 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
214 os_CODENAME=${ver#*|}
215 os_RELEASE=${ver%|*}
216 os_UPDATE=${os_RELEASE##*.}
217 os_RELEASE=${os_RELEASE%.*}
218 break
219 fi
220 os_VENDOR=""
221 done
222 os_PACKAGE="rpm"
223 fi
224 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
225}
226
227
Dean Troyera9e0a482012-07-09 14:07:23 -0500228# Translate the OS version values into common nomenclature
229# Sets ``DISTRO`` from the ``os_*`` values
230function GetDistro() {
231 GetOSVersion
232 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
233 # 'Everyone' refers to Ubuntu releases by the code name adjective
234 DISTRO=$os_CODENAME
235 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
236 # For Fedora, just use 'f' and the release
237 DISTRO="f$os_RELEASE"
238 else
239 # Catch-all for now is Vendor + Release + Update
240 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
241 fi
242 export DISTRO
243}
244
245
Dean Troyer7f9aa712012-01-31 12:11:56 -0600246# git clone only if directory doesn't exist already. Since ``DEST`` might not
247# be owned by the installation user, we create the directory and change the
248# ownership to the proper user.
249# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700250# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
251# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer7f9aa712012-01-31 12:11:56 -0600252# git_clone remote dest-dir branch
253function git_clone {
254 [[ "$OFFLINE" = "True" ]] && return
255
256 GIT_REMOTE=$1
257 GIT_DEST=$2
258 GIT_BRANCH=$3
259
260 if echo $GIT_BRANCH | egrep -q "^refs"; then
261 # If our branch name is a gerrit style refs/changes/...
262 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700263 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600264 git clone $GIT_REMOTE $GIT_DEST
265 fi
266 cd $GIT_DEST
267 git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD
268 else
269 # do a full clone only if the directory doesn't exist
270 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700271 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600272 git clone $GIT_REMOTE $GIT_DEST
273 cd $GIT_DEST
274 # This checkout syntax works for both branches and tags
275 git checkout $GIT_BRANCH
276 elif [[ "$RECLONE" == "yes" ]]; then
277 # if it does exist then simulate what clone does if asked to RECLONE
278 cd $GIT_DEST
279 # set the url to pull from and fetch
280 git remote set-url origin $GIT_REMOTE
281 git fetch origin
282 # remove the existing ignored files (like pyc) as they cause breakage
283 # (due to the py files having older timestamps than our pyc, so python
284 # thinks the pyc files are correct using them)
285 find $GIT_DEST -name '*.pyc' -delete
286 git checkout -f origin/$GIT_BRANCH
287 # a local branch might not exist
288 git branch -D $GIT_BRANCH || true
289 git checkout -b $GIT_BRANCH
290 fi
291 fi
292}
293
294
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500295# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200296# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500297function inicomment() {
298 local file=$1
299 local section=$2
300 local option=$3
301 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
302}
303
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200304# Uncomment an option in an INI file
305# iniuncomment config-file section option
306function iniuncomment() {
307 local file=$1
308 local section=$2
309 local option=$3
310 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
311}
312
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500313
314# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500315# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500316function iniget() {
317 local file=$1
318 local section=$2
319 local option=$3
320 local line
321 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
322 echo ${line#*=}
323}
324
325
326# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500327# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500328function iniset() {
329 local file=$1
330 local section=$2
331 local option=$3
332 local value=$4
Dean Troyer09e636e2012-03-19 16:31:12 -0500333 if ! grep -q "^\[$section\]" $file; then
334 # Add section at the end
335 echo -e "\n[$section]" >>$file
336 fi
337 if [[ -z "$(iniget $file $section $option)" ]]; then
338 # Add it
339 sed -i -e "/^\[$section\]/ a\\
340$option = $value
341" $file
342 else
343 # Replace it
344 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
345 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500346}
347
348
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000349# is_service_enabled() checks if the service(s) specified as arguments are
350# enabled by the user in **ENABLED_SERVICES**.
351#
352# If there are multiple services specified as arguments the test performs a
353# boolean OR or if any of the services specified on the command line
354# return true.
355#
356# There is a special cases for some 'catch-all' services::
357# **nova** returns true if any service enabled start with **n-**
358# **glance** returns true if any service enabled start with **g-**
359# **quantum** returns true if any service enabled start with **q-**
360function is_service_enabled() {
361 services=$@
362 for service in ${services}; do
363 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
364 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500365 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000366 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
367 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
368 done
369 return 1
370}
371
Doug Hellmannf04178f2012-07-05 17:10:03 -0400372# remove extra commas from the input string (ENABLED_SERVICES)
373function _cleanup_service_list () {
374 echo "$1" | sed -e '
375 s/,,/,/g;
376 s/^,//;
377 s/,$//
378 '
379}
380
381# enable_service() adds the services passed as argument to the
382# **ENABLED_SERVICES** list, if they are not already present.
383#
384# For example:
385#
386# enable_service n-vol
387#
388# This function does not know about the special cases
389# for nova, glance, and quantum built into is_service_enabled().
390function enable_service() {
391 local tmpsvcs="${ENABLED_SERVICES}"
392 for service in $@; do
393 if ! is_service_enabled $service; then
394 tmpsvcs+=",$service"
395 fi
396 done
397 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
398 disable_negated_services
399}
400
401# disable_service() removes the services passed as argument to the
402# **ENABLED_SERVICES** list, if they are present.
403#
404# For example:
405#
406# disable_service n-vol
407#
408# This function does not know about the special cases
409# for nova, glance, and quantum built into is_service_enabled().
410function disable_service() {
411 local tmpsvcs=",${ENABLED_SERVICES},"
412 local service
413 for service in $@; do
414 if is_service_enabled $service; then
415 tmpsvcs=${tmpsvcs//,$service,/,}
416 fi
417 done
418 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
419}
420
421# disable_all_services() removes all current services
422# from **ENABLED_SERVICES** to reset the configuration
423# before a minimal installation
424function disable_all_services() {
425 ENABLED_SERVICES=""
426}
427
428# We are looking for services with a - at the beginning to force
429# excluding those services. For example if you want to install all the default
430# services but not nova-volume (n-vol) you can have this set in your localrc :
431# ENABLED_SERVICES+=",-n-vol"
432function disable_negated_services() {
433 local tmpsvcs="${ENABLED_SERVICES}"
434 local service
435 for service in ${tmpsvcs//,/ }; do
436 if [[ ${service} == -* ]]; then
437 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
438 fi
439 done
440 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
441}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600442
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500443# Distro-agnostic package installer
444# install_package package [package ...]
445function install_package() {
446 if [[ -z "$os_PACKAGE" ]]; then
447 GetOSVersion
448 fi
449 if [[ "$os_PACKAGE" = "deb" ]]; then
450 apt_get install "$@"
451 else
452 yum_install "$@"
453 fi
454}
455
456
Dean Troyer489bd2a2012-03-02 10:44:29 -0600457# Test if the named environment variable is set and not zero length
458# is_set env-var
459function is_set() {
460 local var=\$"$1"
461 if eval "[ -z $var ]"; then
462 return 1
463 fi
464 return 0
465}
466
467
Dean Troyer7f9aa712012-01-31 12:11:56 -0600468# pip install wrapper to set cache and proxy environment variables
469# pip_install package [package ...]
470function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600471 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500472 if [[ -z "$os_PACKAGE" ]]; then
473 GetOSVersion
474 fi
475 if [[ "$os_PACKAGE" = "deb" ]]; then
476 CMD_PIP=/usr/bin/pip
477 else
478 CMD_PIP=/usr/bin/pip-python
479 fi
Doug Hellmann4d5e29d2012-07-13 11:11:50 -0400480 sudo PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600481 HTTP_PROXY=$http_proxy \
482 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900483 NO_PROXY=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500484 $CMD_PIP install --use-mirrors $@
485}
486
487
488# Service wrapper to restart services
489# restart_service service-name
490function restart_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600491 if [[ -z "$os_PACKAGE" ]]; then
492 GetOSVersion
493 fi
494 if [[ "$os_PACKAGE" = "deb" ]]; then
495 sudo /usr/sbin/service $1 restart
496 else
497 sudo /sbin/service $1 restart
498 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500499}
500
501
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500502# pip install the dependencies of the package before we do the setup.py
503# develop, so that pip and not distutils process the dependency chain
504# setup_develop directory
505function setup_develop() {
506 (cd $1; \
507 python setup.py egg_info; \
508 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
509 depend_links=$(echo $raw_links | xargs); \
510 pip_install -r *-info/requires.txt $depend_links; \
511 sudo \
512 HTTP_PROXY=$http_proxy \
513 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900514 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500515 python setup.py develop \
516 )
517}
518
519
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500520# Service wrapper to start services
521# start_service service-name
522function start_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600523 if [[ -z "$os_PACKAGE" ]]; then
524 GetOSVersion
525 fi
526 if [[ "$os_PACKAGE" = "deb" ]]; then
527 sudo /usr/sbin/service $1 start
528 else
529 sudo /sbin/service $1 start
530 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500531}
532
533
534# Service wrapper to stop services
535# stop_service service-name
536function stop_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600537 if [[ -z "$os_PACKAGE" ]]; then
538 GetOSVersion
539 fi
540 if [[ "$os_PACKAGE" = "deb" ]]; then
541 sudo /usr/sbin/service $1 stop
542 else
543 sudo /sbin/service $1 stop
544 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600545}
546
547
548# Normalize config values to True or False
549# VAR=`trueorfalse default-value test-value`
550function trueorfalse() {
551 local default=$1
552 local testval=$2
553
554 [[ -z "$testval" ]] && { echo "$default"; return; }
555 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
556 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
557 echo "$default"
558}
Dean Troyer27e32692012-03-16 16:16:56 -0500559
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500560
561# yum wrapper to set arguments correctly
562# yum_install package [package ...]
563function yum_install() {
564 [[ "$OFFLINE" = "True" ]] && return
565 local sudo="sudo"
566 [[ "$(id -u)" = "0" ]] && sudo="env"
567 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900568 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500569 yum install -y "$@"
570}
571
572
Dean Troyer27e32692012-03-16 16:16:56 -0500573# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000574$XTRACE