blob: 5d3a481c8a0c79fc8d78d2c35cae373ecb32015b [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#
3# ENABLED_SERVICES is used by is_service_enabled()
4
Dean Troyer7f9aa712012-01-31 12:11:56 -06005
Dean Troyer27e32692012-03-16 16:16:56 -05006# Save trace setting
7XTRACE=$(set +o | grep xtrace)
8set +o xtrace
9
Dean Troyer7f9aa712012-01-31 12:11:56 -060010
11# apt-get wrapper to set arguments correctly
Dean Troyer13dc5cc2012-03-27 14:50:45 -050012# apt_get operation package [package ...]
Dean Troyer7f9aa712012-01-31 12:11:56 -060013function apt_get() {
Dean Troyerd0b21e22012-03-07 14:52:25 -060014 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer7f9aa712012-01-31 12:11:56 -060015 local sudo="sudo"
16 [[ "$(id -u)" = "0" ]] && sudo="env"
17 $sudo DEBIAN_FRONTEND=noninteractive \
18 http_proxy=$http_proxy https_proxy=$https_proxy \
19 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
20}
21
22
23# Gracefully cp only if source file/dir exists
24# cp_it source destination
25function cp_it {
26 if [ -e $1 ] || [ -d $1 ]; then
27 cp -pRL $1 $2
28 fi
29}
30
31
Dean Troyer27e32692012-03-16 16:16:56 -050032# Prints "message" and exits
33# die "message"
34function die() {
Dean Troyer489bd2a2012-03-02 10:44:29 -060035 local exitcode=$?
Dean Troyer27e32692012-03-16 16:16:56 -050036 set +o xtrace
37 echo $@
38 exit $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -060039}
40
41
42# Checks an environment variable is not set or has length 0 OR if the
43# exit code is non-zero and prints "message" and exits
44# NOTE: env-var is the variable name without a '$'
45# die_if_not_set env-var "message"
46function die_if_not_set() {
Dean Troyer27e32692012-03-16 16:16:56 -050047 (
48 local exitcode=$?
49 set +o xtrace
50 local evar=$1; shift
51 if ! is_set $evar || [ $exitcode != 0 ]; then
52 set +o xtrace
53 echo $@
54 exit -1
55 fi
56 )
Dean Troyer489bd2a2012-03-02 10:44:29 -060057}
58
59
60# Grab a numbered field from python prettytable output
61# Fields are numbered starting with 1
62# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
63# get_field field-number
64function get_field() {
65 while read data; do
66 if [ "$1" -lt 0 ]; then
67 field="(\$(NF$1))"
68 else
69 field="\$$(($1 + 1))"
70 fi
71 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
72 done
73}
74
75
Dean Troyer7e270512012-06-14 15:23:24 -050076# get_packages() collects a list of package names of any type from the
77# prerequisite files in ``files/{apts|pips}``. The list is intended
78# to be passed to a package installer such as apt or pip.
79#
80# Only packages required for the services in ENABLED_SERVICES will be
81# included. Two bits of metadata are recognized in the prerequisite files:
82# - ``# NOPRIME`` defers installation to be performed later in stack.sh
83# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
84# of the package to the distros listed. The distro names are case insensitive.
85#
86# get_packages dir
87function get_packages() {
88 local package_dir=$1
89 local file_to_parse
90 local service
91
92 if [[ -z "$package_dir" ]]; then
93 echo "No package directory supplied"
94 return 1
95 fi
96 if [[ -z "$DISTRO" ]]; then
97 echo "No distro set in DISTRO"
98 return 1
99 fi
100 for service in general ${ENABLED_SERVICES//,/ }; do
101 # Allow individual services to specify dependencies
102 if [[ -e ${package_dir}/${service} ]]; then
103 file_to_parse="${file_to_parse} $service"
104 fi
105 # NOTE(sdague) n-api needs glance for now because that's where
106 # glance client is
107 if [[ $service == n-api ]]; then
108 if [[ ! $file_to_parse =~ nova ]]; then
109 file_to_parse="${file_to_parse} nova"
110 fi
111 if [[ ! $file_to_parse =~ glance ]]; then
112 file_to_parse="${file_to_parse} glance"
113 fi
114 elif [[ $service == c-* ]]; then
115 if [[ ! $file_to_parse =~ cinder ]]; then
116 file_to_parse="${file_to_parse} cinder"
117 fi
118 elif [[ $service == n-* ]]; then
119 if [[ ! $file_to_parse =~ nova ]]; then
120 file_to_parse="${file_to_parse} nova"
121 fi
122 elif [[ $service == g-* ]]; then
123 if [[ ! $file_to_parse =~ glance ]]; then
124 file_to_parse="${file_to_parse} glance"
125 fi
126 elif [[ $service == key* ]]; then
127 if [[ ! $file_to_parse =~ keystone ]]; then
128 file_to_parse="${file_to_parse} keystone"
129 fi
130 fi
131 done
132
133 for file in ${file_to_parse}; do
134 local fname=${package_dir}/${file}
135 local OIFS line package distros distro
136 [[ -e $fname ]] || continue
137
138 OIFS=$IFS
139 IFS=$'\n'
140 for line in $(<${fname}); do
141 if [[ $line =~ "NOPRIME" ]]; then
142 continue
143 fi
144
145 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
146 # We are using BASH regexp matching feature.
147 package=${BASH_REMATCH[1]}
148 distros=${BASH_REMATCH[2]}
149 # In bash ${VAR,,} will lowecase VAR
150 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
151 continue
152 fi
153
154 echo ${line%#*}
155 done
156 IFS=$OIFS
157 done
158}
159
160
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500161# Determine OS Vendor, Release and Update
162# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
163# Returns results in global variables:
164# os_VENDOR - vendor name
165# os_RELEASE - release
166# os_UPDATE - update
167# os_PACKAGE - package type
168# os_CODENAME - vendor's codename for release
169# GetOSVersion
170GetOSVersion() {
171 # Figure out which vendor we are
172 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
173 # OS/X
174 os_VENDOR=`sw_vers -productName`
175 os_RELEASE=`sw_vers -productVersion`
176 os_UPDATE=${os_RELEASE##*.}
177 os_RELEASE=${os_RELEASE%.*}
178 os_PACKAGE=""
179 if [[ "$os_RELEASE" =~ "10.7" ]]; then
180 os_CODENAME="lion"
181 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
182 os_CODENAME="snow leopard"
183 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
184 os_CODENAME="leopard"
185 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
186 os_CODENAME="tiger"
187 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
188 os_CODENAME="panther"
189 else
190 os_CODENAME=""
191 fi
192 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
193 os_VENDOR=$(lsb_release -i -s)
194 os_RELEASE=$(lsb_release -r -s)
195 os_UPDATE=""
196 if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
197 os_PACKAGE="deb"
198 else
199 os_PACKAGE="rpm"
200 fi
201 os_CODENAME=$(lsb_release -c -s)
202 elif [[ -r /etc/redhat-release ]]; then
203 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
204 # CentOS release 5.5 (Final)
205 # CentOS Linux release 6.0 (Final)
206 # Fedora release 16 (Verne)
207 os_CODENAME=""
208 for r in "Red Hat" CentOS Fedora; do
209 os_VENDOR=$r
210 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
211 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
212 os_CODENAME=${ver#*|}
213 os_RELEASE=${ver%|*}
214 os_UPDATE=${os_RELEASE##*.}
215 os_RELEASE=${os_RELEASE%.*}
216 break
217 fi
218 os_VENDOR=""
219 done
220 os_PACKAGE="rpm"
221 fi
222 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
223}
224
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300225# git update using reference as a branch.
226function git_update_branch() {
227
228 GIT_BRANCH=$1
229
230 git checkout -f origin/$GIT_BRANCH
231 # a local branch might not exist
232 git branch -D $GIT_BRANCH || true
233 git checkout -b $GIT_BRANCH
234}
235
236
237# git update using reference as a tag. Be careful editing source at that repo
238# as working copy will be in a detached mode
239function git_update_tag() {
240
241 GIT_TAG=$1
242
243 git tag -d $GIT_TAG
244 # fetching given tag only
245 git fetch origin tag $GIT_TAG
246 git checkout -f $GIT_TAG
247}
248
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500249
Dean Troyer7f9aa712012-01-31 12:11:56 -0600250# git clone only if directory doesn't exist already. Since ``DEST`` might not
251# be owned by the installation user, we create the directory and change the
252# ownership to the proper user.
253# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700254# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
255# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer7f9aa712012-01-31 12:11:56 -0600256# git_clone remote dest-dir branch
257function git_clone {
258 [[ "$OFFLINE" = "True" ]] && return
259
260 GIT_REMOTE=$1
261 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300262 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600263
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300264 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600265 # If our branch name is a gerrit style refs/changes/...
266 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700267 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600268 git clone $GIT_REMOTE $GIT_DEST
269 fi
270 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300271 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600272 else
273 # do a full clone only if the directory doesn't exist
274 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700275 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600276 git clone $GIT_REMOTE $GIT_DEST
277 cd $GIT_DEST
278 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300279 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600280 elif [[ "$RECLONE" == "yes" ]]; then
281 # if it does exist then simulate what clone does if asked to RECLONE
282 cd $GIT_DEST
283 # set the url to pull from and fetch
284 git remote set-url origin $GIT_REMOTE
285 git fetch origin
286 # remove the existing ignored files (like pyc) as they cause breakage
287 # (due to the py files having older timestamps than our pyc, so python
288 # thinks the pyc files are correct using them)
289 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300290
291 # handle GIT_REF accordingly to type (tag, branch)
292 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
293 git_update_tag $GIT_REF
294 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
295 git_update_branch $GIT_REF
296 else
297 echo $GIT_REF is neither branch nor tag
298 exit 1
299 fi
300
Dean Troyer7f9aa712012-01-31 12:11:56 -0600301 fi
302 fi
303}
304
305
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500306# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200307# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500308function inicomment() {
309 local file=$1
310 local section=$2
311 local option=$3
312 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
313}
314
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200315# Uncomment an option in an INI file
316# iniuncomment config-file section option
317function iniuncomment() {
318 local file=$1
319 local section=$2
320 local option=$3
321 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
322}
323
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500324
325# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500326# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500327function iniget() {
328 local file=$1
329 local section=$2
330 local option=$3
331 local line
332 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
333 echo ${line#*=}
334}
335
336
337# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500338# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500339function iniset() {
340 local file=$1
341 local section=$2
342 local option=$3
343 local value=$4
Dean Troyer09e636e2012-03-19 16:31:12 -0500344 if ! grep -q "^\[$section\]" $file; then
345 # Add section at the end
346 echo -e "\n[$section]" >>$file
347 fi
348 if [[ -z "$(iniget $file $section $option)" ]]; then
349 # Add it
350 sed -i -e "/^\[$section\]/ a\\
351$option = $value
352" $file
353 else
354 # Replace it
355 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
356 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500357}
358
359
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000360# is_service_enabled() checks if the service(s) specified as arguments are
361# enabled by the user in **ENABLED_SERVICES**.
362#
363# If there are multiple services specified as arguments the test performs a
364# boolean OR or if any of the services specified on the command line
365# return true.
366#
367# There is a special cases for some 'catch-all' services::
368# **nova** returns true if any service enabled start with **n-**
369# **glance** returns true if any service enabled start with **g-**
370# **quantum** returns true if any service enabled start with **q-**
371function is_service_enabled() {
372 services=$@
373 for service in ${services}; do
374 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
375 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500376 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000377 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
378 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
379 done
380 return 1
381}
382
Dean Troyer489bd2a2012-03-02 10:44:29 -0600383
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500384# Distro-agnostic package installer
385# install_package package [package ...]
386function install_package() {
387 if [[ -z "$os_PACKAGE" ]]; then
388 GetOSVersion
389 fi
390 if [[ "$os_PACKAGE" = "deb" ]]; then
391 apt_get install "$@"
392 else
393 yum_install "$@"
394 fi
395}
396
397
Dean Troyer489bd2a2012-03-02 10:44:29 -0600398# Test if the named environment variable is set and not zero length
399# is_set env-var
400function is_set() {
401 local var=\$"$1"
402 if eval "[ -z $var ]"; then
403 return 1
404 fi
405 return 0
406}
407
408
Dean Troyer7f9aa712012-01-31 12:11:56 -0600409# pip install wrapper to set cache and proxy environment variables
410# pip_install package [package ...]
411function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600412 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500413 if [[ -z "$os_PACKAGE" ]]; then
414 GetOSVersion
415 fi
416 if [[ "$os_PACKAGE" = "deb" ]]; then
417 CMD_PIP=/usr/bin/pip
418 else
419 CMD_PIP=/usr/bin/pip-python
420 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600421 sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \
422 HTTP_PROXY=$http_proxy \
423 HTTPS_PROXY=$https_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500424 $CMD_PIP install --use-mirrors $@
425}
426
427
428# Service wrapper to restart services
429# restart_service service-name
430function restart_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600431 if [[ -z "$os_PACKAGE" ]]; then
432 GetOSVersion
433 fi
434 if [[ "$os_PACKAGE" = "deb" ]]; then
435 sudo /usr/sbin/service $1 restart
436 else
437 sudo /sbin/service $1 restart
438 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500439}
440
441
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500442# pip install the dependencies of the package before we do the setup.py
443# develop, so that pip and not distutils process the dependency chain
444# setup_develop directory
445function setup_develop() {
446 (cd $1; \
447 python setup.py egg_info; \
448 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
449 depend_links=$(echo $raw_links | xargs); \
450 pip_install -r *-info/requires.txt $depend_links; \
451 sudo \
452 HTTP_PROXY=$http_proxy \
453 HTTPS_PROXY=$https_proxy \
454 python setup.py develop \
455 )
456}
457
458
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500459# Service wrapper to start services
460# start_service service-name
461function start_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600462 if [[ -z "$os_PACKAGE" ]]; then
463 GetOSVersion
464 fi
465 if [[ "$os_PACKAGE" = "deb" ]]; then
466 sudo /usr/sbin/service $1 start
467 else
468 sudo /sbin/service $1 start
469 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500470}
471
472
473# Service wrapper to stop services
474# stop_service service-name
475function stop_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600476 if [[ -z "$os_PACKAGE" ]]; then
477 GetOSVersion
478 fi
479 if [[ "$os_PACKAGE" = "deb" ]]; then
480 sudo /usr/sbin/service $1 stop
481 else
482 sudo /sbin/service $1 stop
483 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600484}
485
486
487# Normalize config values to True or False
488# VAR=`trueorfalse default-value test-value`
489function trueorfalse() {
490 local default=$1
491 local testval=$2
492
493 [[ -z "$testval" ]] && { echo "$default"; return; }
494 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
495 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
496 echo "$default"
497}
Dean Troyer27e32692012-03-16 16:16:56 -0500498
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500499
500# yum wrapper to set arguments correctly
501# yum_install package [package ...]
502function yum_install() {
503 [[ "$OFFLINE" = "True" ]] && return
504 local sudo="sudo"
505 [[ "$(id -u)" = "0" ]] && sudo="env"
506 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
507 yum install -y "$@"
508}
509
510
Dean Troyer27e32692012-03-16 16:16:56 -0500511# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000512$XTRACE