blob: a22d8b767ac1318ea5dd17d3ff19c12bebbe921b [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 \
20 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
21}
22
23
24# Gracefully cp only if source file/dir exists
25# cp_it source destination
26function cp_it {
27 if [ -e $1 ] || [ -d $1 ]; then
28 cp -pRL $1 $2
29 fi
30}
31
32
Dean Troyer27e32692012-03-16 16:16:56 -050033# Prints "message" and exits
34# die "message"
35function die() {
Dean Troyer489bd2a2012-03-02 10:44:29 -060036 local exitcode=$?
Dean Troyer27e32692012-03-16 16:16:56 -050037 set +o xtrace
38 echo $@
39 exit $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -060040}
41
42
43# Checks an environment variable is not set or has length 0 OR if the
44# exit code is non-zero and prints "message" and exits
45# NOTE: env-var is the variable name without a '$'
46# die_if_not_set env-var "message"
47function die_if_not_set() {
Dean Troyer27e32692012-03-16 16:16:56 -050048 (
49 local exitcode=$?
50 set +o xtrace
51 local evar=$1; shift
52 if ! is_set $evar || [ $exitcode != 0 ]; then
53 set +o xtrace
54 echo $@
55 exit -1
56 fi
57 )
Dean Troyer489bd2a2012-03-02 10:44:29 -060058}
59
60
61# Grab a numbered field from python prettytable output
62# Fields are numbered starting with 1
63# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
64# get_field field-number
65function get_field() {
66 while read data; do
67 if [ "$1" -lt 0 ]; then
68 field="(\$(NF$1))"
69 else
70 field="\$$(($1 + 1))"
71 fi
72 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
73 done
74}
75
76
Dean Troyer7e270512012-06-14 15:23:24 -050077# get_packages() collects a list of package names of any type from the
78# prerequisite files in ``files/{apts|pips}``. The list is intended
79# to be passed to a package installer such as apt or pip.
80#
81# Only packages required for the services in ENABLED_SERVICES will be
82# included. Two bits of metadata are recognized in the prerequisite files:
83# - ``# NOPRIME`` defers installation to be performed later in stack.sh
84# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
85# of the package to the distros listed. The distro names are case insensitive.
86#
87# get_packages dir
88function get_packages() {
89 local package_dir=$1
90 local file_to_parse
91 local service
92
93 if [[ -z "$package_dir" ]]; then
94 echo "No package directory supplied"
95 return 1
96 fi
97 if [[ -z "$DISTRO" ]]; then
98 echo "No distro set in DISTRO"
99 return 1
100 fi
101 for service in general ${ENABLED_SERVICES//,/ }; do
102 # Allow individual services to specify dependencies
103 if [[ -e ${package_dir}/${service} ]]; then
104 file_to_parse="${file_to_parse} $service"
105 fi
106 # NOTE(sdague) n-api needs glance for now because that's where
107 # glance client is
108 if [[ $service == n-api ]]; then
109 if [[ ! $file_to_parse =~ nova ]]; then
110 file_to_parse="${file_to_parse} nova"
111 fi
112 if [[ ! $file_to_parse =~ glance ]]; then
113 file_to_parse="${file_to_parse} glance"
114 fi
115 elif [[ $service == c-* ]]; then
116 if [[ ! $file_to_parse =~ cinder ]]; then
117 file_to_parse="${file_to_parse} cinder"
118 fi
119 elif [[ $service == n-* ]]; then
120 if [[ ! $file_to_parse =~ nova ]]; then
121 file_to_parse="${file_to_parse} nova"
122 fi
123 elif [[ $service == g-* ]]; then
124 if [[ ! $file_to_parse =~ glance ]]; then
125 file_to_parse="${file_to_parse} glance"
126 fi
127 elif [[ $service == key* ]]; then
128 if [[ ! $file_to_parse =~ keystone ]]; then
129 file_to_parse="${file_to_parse} keystone"
130 fi
131 fi
132 done
133
134 for file in ${file_to_parse}; do
135 local fname=${package_dir}/${file}
136 local OIFS line package distros distro
137 [[ -e $fname ]] || continue
138
139 OIFS=$IFS
140 IFS=$'\n'
141 for line in $(<${fname}); do
142 if [[ $line =~ "NOPRIME" ]]; then
143 continue
144 fi
145
146 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
147 # We are using BASH regexp matching feature.
148 package=${BASH_REMATCH[1]}
149 distros=${BASH_REMATCH[2]}
150 # In bash ${VAR,,} will lowecase VAR
151 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
152 continue
153 fi
154
155 echo ${line%#*}
156 done
157 IFS=$OIFS
158 done
159}
160
161
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500162# Determine OS Vendor, Release and Update
163# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
164# Returns results in global variables:
165# os_VENDOR - vendor name
166# os_RELEASE - release
167# os_UPDATE - update
168# os_PACKAGE - package type
169# os_CODENAME - vendor's codename for release
170# GetOSVersion
171GetOSVersion() {
172 # Figure out which vendor we are
173 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
174 # OS/X
175 os_VENDOR=`sw_vers -productName`
176 os_RELEASE=`sw_vers -productVersion`
177 os_UPDATE=${os_RELEASE##*.}
178 os_RELEASE=${os_RELEASE%.*}
179 os_PACKAGE=""
180 if [[ "$os_RELEASE" =~ "10.7" ]]; then
181 os_CODENAME="lion"
182 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
183 os_CODENAME="snow leopard"
184 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
185 os_CODENAME="leopard"
186 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
187 os_CODENAME="tiger"
188 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
189 os_CODENAME="panther"
190 else
191 os_CODENAME=""
192 fi
193 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
194 os_VENDOR=$(lsb_release -i -s)
195 os_RELEASE=$(lsb_release -r -s)
196 os_UPDATE=""
197 if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
198 os_PACKAGE="deb"
199 else
200 os_PACKAGE="rpm"
201 fi
202 os_CODENAME=$(lsb_release -c -s)
203 elif [[ -r /etc/redhat-release ]]; then
204 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
205 # CentOS release 5.5 (Final)
206 # CentOS Linux release 6.0 (Final)
207 # Fedora release 16 (Verne)
208 os_CODENAME=""
209 for r in "Red Hat" CentOS Fedora; do
210 os_VENDOR=$r
211 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
212 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
213 os_CODENAME=${ver#*|}
214 os_RELEASE=${ver%|*}
215 os_UPDATE=${os_RELEASE##*.}
216 os_RELEASE=${os_RELEASE%.*}
217 break
218 fi
219 os_VENDOR=""
220 done
221 os_PACKAGE="rpm"
222 fi
223 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
224}
225
226
Dean Troyer7f9aa712012-01-31 12:11:56 -0600227# git clone only if directory doesn't exist already. Since ``DEST`` might not
228# be owned by the installation user, we create the directory and change the
229# ownership to the proper user.
230# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700231# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
232# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer7f9aa712012-01-31 12:11:56 -0600233# git_clone remote dest-dir branch
234function git_clone {
235 [[ "$OFFLINE" = "True" ]] && return
236
237 GIT_REMOTE=$1
238 GIT_DEST=$2
239 GIT_BRANCH=$3
240
241 if echo $GIT_BRANCH | egrep -q "^refs"; then
242 # If our branch name is a gerrit style refs/changes/...
243 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700244 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600245 git clone $GIT_REMOTE $GIT_DEST
246 fi
247 cd $GIT_DEST
248 git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD
249 else
250 # do a full clone only if the directory doesn't exist
251 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700252 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600253 git clone $GIT_REMOTE $GIT_DEST
254 cd $GIT_DEST
255 # This checkout syntax works for both branches and tags
256 git checkout $GIT_BRANCH
257 elif [[ "$RECLONE" == "yes" ]]; then
258 # if it does exist then simulate what clone does if asked to RECLONE
259 cd $GIT_DEST
260 # set the url to pull from and fetch
261 git remote set-url origin $GIT_REMOTE
262 git fetch origin
263 # remove the existing ignored files (like pyc) as they cause breakage
264 # (due to the py files having older timestamps than our pyc, so python
265 # thinks the pyc files are correct using them)
266 find $GIT_DEST -name '*.pyc' -delete
267 git checkout -f origin/$GIT_BRANCH
268 # a local branch might not exist
269 git branch -D $GIT_BRANCH || true
270 git checkout -b $GIT_BRANCH
271 fi
272 fi
273}
274
275
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500276# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200277# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500278function inicomment() {
279 local file=$1
280 local section=$2
281 local option=$3
282 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
283}
284
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200285# Uncomment an option in an INI file
286# iniuncomment config-file section option
287function iniuncomment() {
288 local file=$1
289 local section=$2
290 local option=$3
291 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
292}
293
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500294
295# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500296# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500297function iniget() {
298 local file=$1
299 local section=$2
300 local option=$3
301 local line
302 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
303 echo ${line#*=}
304}
305
306
307# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500308# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500309function iniset() {
310 local file=$1
311 local section=$2
312 local option=$3
313 local value=$4
Dean Troyer09e636e2012-03-19 16:31:12 -0500314 if ! grep -q "^\[$section\]" $file; then
315 # Add section at the end
316 echo -e "\n[$section]" >>$file
317 fi
318 if [[ -z "$(iniget $file $section $option)" ]]; then
319 # Add it
320 sed -i -e "/^\[$section\]/ a\\
321$option = $value
322" $file
323 else
324 # Replace it
325 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
326 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500327}
328
329
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000330# is_service_enabled() checks if the service(s) specified as arguments are
331# enabled by the user in **ENABLED_SERVICES**.
332#
333# If there are multiple services specified as arguments the test performs a
334# boolean OR or if any of the services specified on the command line
335# return true.
336#
337# There is a special cases for some 'catch-all' services::
338# **nova** returns true if any service enabled start with **n-**
339# **glance** returns true if any service enabled start with **g-**
340# **quantum** returns true if any service enabled start with **q-**
341function is_service_enabled() {
342 services=$@
343 for service in ${services}; do
344 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
345 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500346 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000347 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
348 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
349 done
350 return 1
351}
352
Doug Hellmannf04178f2012-07-05 17:10:03 -0400353# remove extra commas from the input string (ENABLED_SERVICES)
354function _cleanup_service_list () {
355 echo "$1" | sed -e '
356 s/,,/,/g;
357 s/^,//;
358 s/,$//
359 '
360}
361
362# enable_service() adds the services passed as argument to the
363# **ENABLED_SERVICES** list, if they are not already present.
364#
365# For example:
366#
367# enable_service n-vol
368#
369# This function does not know about the special cases
370# for nova, glance, and quantum built into is_service_enabled().
371function enable_service() {
372 local tmpsvcs="${ENABLED_SERVICES}"
373 for service in $@; do
374 if ! is_service_enabled $service; then
375 tmpsvcs+=",$service"
376 fi
377 done
378 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
379 disable_negated_services
380}
381
382# disable_service() removes the services passed as argument to the
383# **ENABLED_SERVICES** list, if they are present.
384#
385# For example:
386#
387# disable_service n-vol
388#
389# This function does not know about the special cases
390# for nova, glance, and quantum built into is_service_enabled().
391function disable_service() {
392 local tmpsvcs=",${ENABLED_SERVICES},"
393 local service
394 for service in $@; do
395 if is_service_enabled $service; then
396 tmpsvcs=${tmpsvcs//,$service,/,}
397 fi
398 done
399 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
400}
401
402# disable_all_services() removes all current services
403# from **ENABLED_SERVICES** to reset the configuration
404# before a minimal installation
405function disable_all_services() {
406 ENABLED_SERVICES=""
407}
408
409# We are looking for services with a - at the beginning to force
410# excluding those services. For example if you want to install all the default
411# services but not nova-volume (n-vol) you can have this set in your localrc :
412# ENABLED_SERVICES+=",-n-vol"
413function disable_negated_services() {
414 local tmpsvcs="${ENABLED_SERVICES}"
415 local service
416 for service in ${tmpsvcs//,/ }; do
417 if [[ ${service} == -* ]]; then
418 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
419 fi
420 done
421 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
422}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600423
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500424# Distro-agnostic package installer
425# install_package package [package ...]
426function install_package() {
427 if [[ -z "$os_PACKAGE" ]]; then
428 GetOSVersion
429 fi
430 if [[ "$os_PACKAGE" = "deb" ]]; then
431 apt_get install "$@"
432 else
433 yum_install "$@"
434 fi
435}
436
437
Dean Troyer489bd2a2012-03-02 10:44:29 -0600438# Test if the named environment variable is set and not zero length
439# is_set env-var
440function is_set() {
441 local var=\$"$1"
442 if eval "[ -z $var ]"; then
443 return 1
444 fi
445 return 0
446}
447
448
Dean Troyer7f9aa712012-01-31 12:11:56 -0600449# pip install wrapper to set cache and proxy environment variables
450# pip_install package [package ...]
451function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600452 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500453 if [[ -z "$os_PACKAGE" ]]; then
454 GetOSVersion
455 fi
456 if [[ "$os_PACKAGE" = "deb" ]]; then
457 CMD_PIP=/usr/bin/pip
458 else
459 CMD_PIP=/usr/bin/pip-python
460 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600461 sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \
462 HTTP_PROXY=$http_proxy \
463 HTTPS_PROXY=$https_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500464 $CMD_PIP install --use-mirrors $@
465}
466
467
468# Service wrapper to restart services
469# restart_service service-name
470function restart_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600471 if [[ -z "$os_PACKAGE" ]]; then
472 GetOSVersion
473 fi
474 if [[ "$os_PACKAGE" = "deb" ]]; then
475 sudo /usr/sbin/service $1 restart
476 else
477 sudo /sbin/service $1 restart
478 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500479}
480
481
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500482# pip install the dependencies of the package before we do the setup.py
483# develop, so that pip and not distutils process the dependency chain
484# setup_develop directory
485function setup_develop() {
486 (cd $1; \
487 python setup.py egg_info; \
488 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
489 depend_links=$(echo $raw_links | xargs); \
490 pip_install -r *-info/requires.txt $depend_links; \
491 sudo \
492 HTTP_PROXY=$http_proxy \
493 HTTPS_PROXY=$https_proxy \
494 python setup.py develop \
495 )
496}
497
498
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500499# Service wrapper to start services
500# start_service service-name
501function start_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600502 if [[ -z "$os_PACKAGE" ]]; then
503 GetOSVersion
504 fi
505 if [[ "$os_PACKAGE" = "deb" ]]; then
506 sudo /usr/sbin/service $1 start
507 else
508 sudo /sbin/service $1 start
509 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500510}
511
512
513# Service wrapper to stop services
514# stop_service service-name
515function stop_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600516 if [[ -z "$os_PACKAGE" ]]; then
517 GetOSVersion
518 fi
519 if [[ "$os_PACKAGE" = "deb" ]]; then
520 sudo /usr/sbin/service $1 stop
521 else
522 sudo /sbin/service $1 stop
523 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600524}
525
526
527# Normalize config values to True or False
528# VAR=`trueorfalse default-value test-value`
529function trueorfalse() {
530 local default=$1
531 local testval=$2
532
533 [[ -z "$testval" ]] && { echo "$default"; return; }
534 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
535 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
536 echo "$default"
537}
Dean Troyer27e32692012-03-16 16:16:56 -0500538
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500539
540# yum wrapper to set arguments correctly
541# yum_install package [package ...]
542function yum_install() {
543 [[ "$OFFLINE" = "True" ]] && return
544 local sudo="sudo"
545 [[ "$(id -u)" = "0" ]] && sudo="env"
546 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
547 yum install -y "$@"
548}
549
550
Dean Troyer27e32692012-03-16 16:16:56 -0500551# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000552$XTRACE