blob: 46a6f8a469011dd83bc64e6ed7c9018515ff021b [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 Troyera9e0a482012-07-09 14:07:23 -0500227# Translate the OS version values into common nomenclature
228# Sets ``DISTRO`` from the ``os_*`` values
229function GetDistro() {
230 GetOSVersion
231 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
232 # 'Everyone' refers to Ubuntu releases by the code name adjective
233 DISTRO=$os_CODENAME
234 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
235 # For Fedora, just use 'f' and the release
236 DISTRO="f$os_RELEASE"
237 else
238 # Catch-all for now is Vendor + Release + Update
239 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
240 fi
241 export DISTRO
242}
243
244
Dean Troyer7f9aa712012-01-31 12:11:56 -0600245# git clone only if directory doesn't exist already. Since ``DEST`` might not
246# be owned by the installation user, we create the directory and change the
247# ownership to the proper user.
248# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700249# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
250# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer7f9aa712012-01-31 12:11:56 -0600251# git_clone remote dest-dir branch
252function git_clone {
253 [[ "$OFFLINE" = "True" ]] && return
254
255 GIT_REMOTE=$1
256 GIT_DEST=$2
257 GIT_BRANCH=$3
258
259 if echo $GIT_BRANCH | egrep -q "^refs"; then
260 # If our branch name is a gerrit style refs/changes/...
261 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700262 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600263 git clone $GIT_REMOTE $GIT_DEST
264 fi
265 cd $GIT_DEST
266 git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD
267 else
268 # do a full clone only if the directory doesn't exist
269 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700270 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600271 git clone $GIT_REMOTE $GIT_DEST
272 cd $GIT_DEST
273 # This checkout syntax works for both branches and tags
274 git checkout $GIT_BRANCH
275 elif [[ "$RECLONE" == "yes" ]]; then
276 # if it does exist then simulate what clone does if asked to RECLONE
277 cd $GIT_DEST
278 # set the url to pull from and fetch
279 git remote set-url origin $GIT_REMOTE
280 git fetch origin
281 # remove the existing ignored files (like pyc) as they cause breakage
282 # (due to the py files having older timestamps than our pyc, so python
283 # thinks the pyc files are correct using them)
284 find $GIT_DEST -name '*.pyc' -delete
285 git checkout -f origin/$GIT_BRANCH
286 # a local branch might not exist
287 git branch -D $GIT_BRANCH || true
288 git checkout -b $GIT_BRANCH
289 fi
290 fi
291}
292
293
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500294# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200295# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500296function inicomment() {
297 local file=$1
298 local section=$2
299 local option=$3
300 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
301}
302
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200303# Uncomment an option in an INI file
304# iniuncomment config-file section option
305function iniuncomment() {
306 local file=$1
307 local section=$2
308 local option=$3
309 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
310}
311
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500312
313# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500314# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500315function iniget() {
316 local file=$1
317 local section=$2
318 local option=$3
319 local line
320 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
321 echo ${line#*=}
322}
323
324
325# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500326# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500327function iniset() {
328 local file=$1
329 local section=$2
330 local option=$3
331 local value=$4
Dean Troyer09e636e2012-03-19 16:31:12 -0500332 if ! grep -q "^\[$section\]" $file; then
333 # Add section at the end
334 echo -e "\n[$section]" >>$file
335 fi
336 if [[ -z "$(iniget $file $section $option)" ]]; then
337 # Add it
338 sed -i -e "/^\[$section\]/ a\\
339$option = $value
340" $file
341 else
342 # Replace it
343 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
344 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500345}
346
347
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000348# is_service_enabled() checks if the service(s) specified as arguments are
349# enabled by the user in **ENABLED_SERVICES**.
350#
351# If there are multiple services specified as arguments the test performs a
352# boolean OR or if any of the services specified on the command line
353# return true.
354#
355# There is a special cases for some 'catch-all' services::
356# **nova** returns true if any service enabled start with **n-**
357# **glance** returns true if any service enabled start with **g-**
358# **quantum** returns true if any service enabled start with **q-**
359function is_service_enabled() {
360 services=$@
361 for service in ${services}; do
362 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
363 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500364 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000365 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
366 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
367 done
368 return 1
369}
370
Doug Hellmannf04178f2012-07-05 17:10:03 -0400371# remove extra commas from the input string (ENABLED_SERVICES)
372function _cleanup_service_list () {
373 echo "$1" | sed -e '
374 s/,,/,/g;
375 s/^,//;
376 s/,$//
377 '
378}
379
380# enable_service() adds the services passed as argument to the
381# **ENABLED_SERVICES** list, if they are not already present.
382#
383# For example:
384#
385# enable_service n-vol
386#
387# This function does not know about the special cases
388# for nova, glance, and quantum built into is_service_enabled().
389function enable_service() {
390 local tmpsvcs="${ENABLED_SERVICES}"
391 for service in $@; do
392 if ! is_service_enabled $service; then
393 tmpsvcs+=",$service"
394 fi
395 done
396 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
397 disable_negated_services
398}
399
400# disable_service() removes the services passed as argument to the
401# **ENABLED_SERVICES** list, if they are present.
402#
403# For example:
404#
405# disable_service n-vol
406#
407# This function does not know about the special cases
408# for nova, glance, and quantum built into is_service_enabled().
409function disable_service() {
410 local tmpsvcs=",${ENABLED_SERVICES},"
411 local service
412 for service in $@; do
413 if is_service_enabled $service; then
414 tmpsvcs=${tmpsvcs//,$service,/,}
415 fi
416 done
417 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
418}
419
420# disable_all_services() removes all current services
421# from **ENABLED_SERVICES** to reset the configuration
422# before a minimal installation
423function disable_all_services() {
424 ENABLED_SERVICES=""
425}
426
427# We are looking for services with a - at the beginning to force
428# excluding those services. For example if you want to install all the default
429# services but not nova-volume (n-vol) you can have this set in your localrc :
430# ENABLED_SERVICES+=",-n-vol"
431function disable_negated_services() {
432 local tmpsvcs="${ENABLED_SERVICES}"
433 local service
434 for service in ${tmpsvcs//,/ }; do
435 if [[ ${service} == -* ]]; then
436 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
437 fi
438 done
439 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
440}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600441
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500442# Distro-agnostic package installer
443# install_package package [package ...]
444function install_package() {
445 if [[ -z "$os_PACKAGE" ]]; then
446 GetOSVersion
447 fi
448 if [[ "$os_PACKAGE" = "deb" ]]; then
449 apt_get install "$@"
450 else
451 yum_install "$@"
452 fi
453}
454
455
Dean Troyer489bd2a2012-03-02 10:44:29 -0600456# Test if the named environment variable is set and not zero length
457# is_set env-var
458function is_set() {
459 local var=\$"$1"
460 if eval "[ -z $var ]"; then
461 return 1
462 fi
463 return 0
464}
465
466
Dean Troyer7f9aa712012-01-31 12:11:56 -0600467# pip install wrapper to set cache and proxy environment variables
468# pip_install package [package ...]
469function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600470 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500471 if [[ -z "$os_PACKAGE" ]]; then
472 GetOSVersion
473 fi
474 if [[ "$os_PACKAGE" = "deb" ]]; then
475 CMD_PIP=/usr/bin/pip
476 else
477 CMD_PIP=/usr/bin/pip-python
478 fi
Doug Hellmann4d5e29d2012-07-13 11:11:50 -0400479 sudo PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600480 HTTP_PROXY=$http_proxy \
481 HTTPS_PROXY=$https_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500482 $CMD_PIP install --use-mirrors $@
483}
484
485
486# Service wrapper to restart services
487# restart_service service-name
488function restart_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600489 if [[ -z "$os_PACKAGE" ]]; then
490 GetOSVersion
491 fi
492 if [[ "$os_PACKAGE" = "deb" ]]; then
493 sudo /usr/sbin/service $1 restart
494 else
495 sudo /sbin/service $1 restart
496 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500497}
498
499
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500500# pip install the dependencies of the package before we do the setup.py
501# develop, so that pip and not distutils process the dependency chain
502# setup_develop directory
503function setup_develop() {
504 (cd $1; \
505 python setup.py egg_info; \
506 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
507 depend_links=$(echo $raw_links | xargs); \
508 pip_install -r *-info/requires.txt $depend_links; \
509 sudo \
510 HTTP_PROXY=$http_proxy \
511 HTTPS_PROXY=$https_proxy \
512 python setup.py develop \
513 )
514}
515
516
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500517# Service wrapper to start services
518# start_service service-name
519function start_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600520 if [[ -z "$os_PACKAGE" ]]; then
521 GetOSVersion
522 fi
523 if [[ "$os_PACKAGE" = "deb" ]]; then
524 sudo /usr/sbin/service $1 start
525 else
526 sudo /sbin/service $1 start
527 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500528}
529
530
531# Service wrapper to stop services
532# stop_service service-name
533function stop_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600534 if [[ -z "$os_PACKAGE" ]]; then
535 GetOSVersion
536 fi
537 if [[ "$os_PACKAGE" = "deb" ]]; then
538 sudo /usr/sbin/service $1 stop
539 else
540 sudo /sbin/service $1 stop
541 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600542}
543
544
545# Normalize config values to True or False
546# VAR=`trueorfalse default-value test-value`
547function trueorfalse() {
548 local default=$1
549 local testval=$2
550
551 [[ -z "$testval" ]] && { echo "$default"; return; }
552 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
553 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
554 echo "$default"
555}
Dean Troyer27e32692012-03-16 16:16:56 -0500556
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500557
558# yum wrapper to set arguments correctly
559# yum_install package [package ...]
560function yum_install() {
561 [[ "$OFFLINE" = "True" ]] && return
562 local sudo="sudo"
563 [[ "$(id -u)" = "0" ]] && sudo="env"
564 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
565 yum install -y "$@"
566}
567
568
Dean Troyer27e32692012-03-16 16:16:56 -0500569# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000570$XTRACE