blob: db104633ba718059b4ae67ef9cb0dea353e0f513 [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
225
Dean Troyera9e0a482012-07-09 14:07:23 -0500226# Translate the OS version values into common nomenclature
227# Sets ``DISTRO`` from the ``os_*`` values
228function GetDistro() {
229 GetOSVersion
230 if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then
231 # 'Everyone' refers to Ubuntu releases by the code name adjective
232 DISTRO=$os_CODENAME
233 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
234 # For Fedora, just use 'f' and the release
235 DISTRO="f$os_RELEASE"
236 else
237 # Catch-all for now is Vendor + Release + Update
238 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
239 fi
240 export DISTRO
241}
242
243
Dean Troyer7f9aa712012-01-31 12:11:56 -0600244# git clone only if directory doesn't exist already. Since ``DEST`` might not
245# be owned by the installation user, we create the directory and change the
246# ownership to the proper user.
247# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700248# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
249# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer7f9aa712012-01-31 12:11:56 -0600250# git_clone remote dest-dir branch
251function git_clone {
252 [[ "$OFFLINE" = "True" ]] && return
253
254 GIT_REMOTE=$1
255 GIT_DEST=$2
256 GIT_BRANCH=$3
257
258 if echo $GIT_BRANCH | egrep -q "^refs"; then
259 # If our branch name is a gerrit style refs/changes/...
260 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700261 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600262 git clone $GIT_REMOTE $GIT_DEST
263 fi
264 cd $GIT_DEST
265 git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD
266 else
267 # do a full clone only if the directory doesn't exist
268 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700269 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600270 git clone $GIT_REMOTE $GIT_DEST
271 cd $GIT_DEST
272 # This checkout syntax works for both branches and tags
273 git checkout $GIT_BRANCH
274 elif [[ "$RECLONE" == "yes" ]]; then
275 # if it does exist then simulate what clone does if asked to RECLONE
276 cd $GIT_DEST
277 # set the url to pull from and fetch
278 git remote set-url origin $GIT_REMOTE
279 git fetch origin
280 # remove the existing ignored files (like pyc) as they cause breakage
281 # (due to the py files having older timestamps than our pyc, so python
282 # thinks the pyc files are correct using them)
283 find $GIT_DEST -name '*.pyc' -delete
284 git checkout -f origin/$GIT_BRANCH
285 # a local branch might not exist
286 git branch -D $GIT_BRANCH || true
287 git checkout -b $GIT_BRANCH
288 fi
289 fi
290}
291
292
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500293# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200294# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500295function inicomment() {
296 local file=$1
297 local section=$2
298 local option=$3
299 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
300}
301
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200302# Uncomment an option in an INI file
303# iniuncomment config-file section option
304function iniuncomment() {
305 local file=$1
306 local section=$2
307 local option=$3
308 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
309}
310
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500311
312# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500313# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500314function iniget() {
315 local file=$1
316 local section=$2
317 local option=$3
318 local line
319 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
320 echo ${line#*=}
321}
322
323
324# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500325# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500326function iniset() {
327 local file=$1
328 local section=$2
329 local option=$3
330 local value=$4
Dean Troyer09e636e2012-03-19 16:31:12 -0500331 if ! grep -q "^\[$section\]" $file; then
332 # Add section at the end
333 echo -e "\n[$section]" >>$file
334 fi
335 if [[ -z "$(iniget $file $section $option)" ]]; then
336 # Add it
337 sed -i -e "/^\[$section\]/ a\\
338$option = $value
339" $file
340 else
341 # Replace it
342 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
343 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500344}
345
346
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000347# is_service_enabled() checks if the service(s) specified as arguments are
348# enabled by the user in **ENABLED_SERVICES**.
349#
350# If there are multiple services specified as arguments the test performs a
351# boolean OR or if any of the services specified on the command line
352# return true.
353#
354# There is a special cases for some 'catch-all' services::
355# **nova** returns true if any service enabled start with **n-**
356# **glance** returns true if any service enabled start with **g-**
357# **quantum** returns true if any service enabled start with **q-**
358function is_service_enabled() {
359 services=$@
360 for service in ${services}; do
361 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
362 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500363 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000364 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
365 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
366 done
367 return 1
368}
369
Dean Troyer489bd2a2012-03-02 10:44:29 -0600370
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500371# Distro-agnostic package installer
372# install_package package [package ...]
373function install_package() {
374 if [[ -z "$os_PACKAGE" ]]; then
375 GetOSVersion
376 fi
377 if [[ "$os_PACKAGE" = "deb" ]]; then
378 apt_get install "$@"
379 else
380 yum_install "$@"
381 fi
382}
383
384
Dean Troyer489bd2a2012-03-02 10:44:29 -0600385# Test if the named environment variable is set and not zero length
386# is_set env-var
387function is_set() {
388 local var=\$"$1"
389 if eval "[ -z $var ]"; then
390 return 1
391 fi
392 return 0
393}
394
395
Dean Troyer7f9aa712012-01-31 12:11:56 -0600396# pip install wrapper to set cache and proxy environment variables
397# pip_install package [package ...]
398function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600399 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500400 if [[ -z "$os_PACKAGE" ]]; then
401 GetOSVersion
402 fi
403 if [[ "$os_PACKAGE" = "deb" ]]; then
404 CMD_PIP=/usr/bin/pip
405 else
406 CMD_PIP=/usr/bin/pip-python
407 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600408 sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \
409 HTTP_PROXY=$http_proxy \
410 HTTPS_PROXY=$https_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500411 $CMD_PIP install --use-mirrors $@
412}
413
414
415# Service wrapper to restart services
416# restart_service service-name
417function restart_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600418 if [[ -z "$os_PACKAGE" ]]; then
419 GetOSVersion
420 fi
421 if [[ "$os_PACKAGE" = "deb" ]]; then
422 sudo /usr/sbin/service $1 restart
423 else
424 sudo /sbin/service $1 restart
425 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500426}
427
428
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500429# pip install the dependencies of the package before we do the setup.py
430# develop, so that pip and not distutils process the dependency chain
431# setup_develop directory
432function setup_develop() {
433 (cd $1; \
434 python setup.py egg_info; \
435 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
436 depend_links=$(echo $raw_links | xargs); \
437 pip_install -r *-info/requires.txt $depend_links; \
438 sudo \
439 HTTP_PROXY=$http_proxy \
440 HTTPS_PROXY=$https_proxy \
441 python setup.py develop \
442 )
443}
444
445
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500446# Service wrapper to start services
447# start_service service-name
448function start_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600449 if [[ -z "$os_PACKAGE" ]]; then
450 GetOSVersion
451 fi
452 if [[ "$os_PACKAGE" = "deb" ]]; then
453 sudo /usr/sbin/service $1 start
454 else
455 sudo /sbin/service $1 start
456 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500457}
458
459
460# Service wrapper to stop services
461# stop_service service-name
462function stop_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600463 if [[ -z "$os_PACKAGE" ]]; then
464 GetOSVersion
465 fi
466 if [[ "$os_PACKAGE" = "deb" ]]; then
467 sudo /usr/sbin/service $1 stop
468 else
469 sudo /sbin/service $1 stop
470 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600471}
472
473
474# Normalize config values to True or False
475# VAR=`trueorfalse default-value test-value`
476function trueorfalse() {
477 local default=$1
478 local testval=$2
479
480 [[ -z "$testval" ]] && { echo "$default"; return; }
481 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
482 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
483 echo "$default"
484}
Dean Troyer27e32692012-03-16 16:16:56 -0500485
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500486
487# yum wrapper to set arguments correctly
488# yum_install package [package ...]
489function yum_install() {
490 [[ "$OFFLINE" = "True" ]] && return
491 local sudo="sudo"
492 [[ "$(id -u)" = "0" ]] && sudo="env"
493 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
494 yum install -y "$@"
495}
496
497
Dean Troyer27e32692012-03-16 16:16:56 -0500498# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000499$XTRACE