blob: 8cf7c74b6b3ca232ecd537481b8d64f96438767b [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 Troyer7f9aa712012-01-31 12:11:56 -0600226# git clone only if directory doesn't exist already. Since ``DEST`` might not
227# be owned by the installation user, we create the directory and change the
228# ownership to the proper user.
229# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700230# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
231# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer7f9aa712012-01-31 12:11:56 -0600232# git_clone remote dest-dir branch
233function git_clone {
234 [[ "$OFFLINE" = "True" ]] && return
235
236 GIT_REMOTE=$1
237 GIT_DEST=$2
238 GIT_BRANCH=$3
239
240 if echo $GIT_BRANCH | egrep -q "^refs"; then
241 # If our branch name is a gerrit style refs/changes/...
242 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700243 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600244 git clone $GIT_REMOTE $GIT_DEST
245 fi
246 cd $GIT_DEST
247 git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD
248 else
249 # do a full clone only if the directory doesn't exist
250 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700251 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600252 git clone $GIT_REMOTE $GIT_DEST
253 cd $GIT_DEST
254 # This checkout syntax works for both branches and tags
255 git checkout $GIT_BRANCH
256 elif [[ "$RECLONE" == "yes" ]]; then
257 # if it does exist then simulate what clone does if asked to RECLONE
258 cd $GIT_DEST
259 # set the url to pull from and fetch
260 git remote set-url origin $GIT_REMOTE
261 git fetch origin
262 # remove the existing ignored files (like pyc) as they cause breakage
263 # (due to the py files having older timestamps than our pyc, so python
264 # thinks the pyc files are correct using them)
265 find $GIT_DEST -name '*.pyc' -delete
266 git checkout -f origin/$GIT_BRANCH
267 # a local branch might not exist
268 git branch -D $GIT_BRANCH || true
269 git checkout -b $GIT_BRANCH
270 fi
271 fi
272}
273
274
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500275# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200276# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500277function inicomment() {
278 local file=$1
279 local section=$2
280 local option=$3
281 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
282}
283
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200284# Uncomment an option in an INI file
285# iniuncomment config-file section option
286function iniuncomment() {
287 local file=$1
288 local section=$2
289 local option=$3
290 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
291}
292
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500293
294# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500295# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500296function iniget() {
297 local file=$1
298 local section=$2
299 local option=$3
300 local line
301 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
302 echo ${line#*=}
303}
304
305
306# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500307# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500308function iniset() {
309 local file=$1
310 local section=$2
311 local option=$3
312 local value=$4
Dean Troyer09e636e2012-03-19 16:31:12 -0500313 if ! grep -q "^\[$section\]" $file; then
314 # Add section at the end
315 echo -e "\n[$section]" >>$file
316 fi
317 if [[ -z "$(iniget $file $section $option)" ]]; then
318 # Add it
319 sed -i -e "/^\[$section\]/ a\\
320$option = $value
321" $file
322 else
323 # Replace it
324 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
325 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500326}
327
328
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000329# is_service_enabled() checks if the service(s) specified as arguments are
330# enabled by the user in **ENABLED_SERVICES**.
331#
332# If there are multiple services specified as arguments the test performs a
333# boolean OR or if any of the services specified on the command line
334# return true.
335#
336# There is a special cases for some 'catch-all' services::
337# **nova** returns true if any service enabled start with **n-**
338# **glance** returns true if any service enabled start with **g-**
339# **quantum** returns true if any service enabled start with **q-**
340function is_service_enabled() {
341 services=$@
342 for service in ${services}; do
343 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
344 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500345 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000346 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
347 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
348 done
349 return 1
350}
351
Dean Troyer489bd2a2012-03-02 10:44:29 -0600352
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500353# Distro-agnostic package installer
354# install_package package [package ...]
355function install_package() {
356 if [[ -z "$os_PACKAGE" ]]; then
357 GetOSVersion
358 fi
359 if [[ "$os_PACKAGE" = "deb" ]]; then
360 apt_get install "$@"
361 else
362 yum_install "$@"
363 fi
364}
365
366
Dean Troyer489bd2a2012-03-02 10:44:29 -0600367# Test if the named environment variable is set and not zero length
368# is_set env-var
369function is_set() {
370 local var=\$"$1"
371 if eval "[ -z $var ]"; then
372 return 1
373 fi
374 return 0
375}
376
377
Dean Troyer7f9aa712012-01-31 12:11:56 -0600378# pip install wrapper to set cache and proxy environment variables
379# pip_install package [package ...]
380function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600381 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500382 if [[ -z "$os_PACKAGE" ]]; then
383 GetOSVersion
384 fi
385 if [[ "$os_PACKAGE" = "deb" ]]; then
386 CMD_PIP=/usr/bin/pip
387 else
388 CMD_PIP=/usr/bin/pip-python
389 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600390 sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \
391 HTTP_PROXY=$http_proxy \
392 HTTPS_PROXY=$https_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500393 $CMD_PIP install --use-mirrors $@
394}
395
396
397# Service wrapper to restart services
398# restart_service service-name
399function restart_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600400 if [[ -z "$os_PACKAGE" ]]; then
401 GetOSVersion
402 fi
403 if [[ "$os_PACKAGE" = "deb" ]]; then
404 sudo /usr/sbin/service $1 restart
405 else
406 sudo /sbin/service $1 restart
407 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500408}
409
410
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500411# pip install the dependencies of the package before we do the setup.py
412# develop, so that pip and not distutils process the dependency chain
413# setup_develop directory
414function setup_develop() {
415 (cd $1; \
416 python setup.py egg_info; \
417 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
418 depend_links=$(echo $raw_links | xargs); \
419 pip_install -r *-info/requires.txt $depend_links; \
420 sudo \
421 HTTP_PROXY=$http_proxy \
422 HTTPS_PROXY=$https_proxy \
423 python setup.py develop \
424 )
425}
426
427
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500428# Service wrapper to start services
429# start_service service-name
430function start_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 start
436 else
437 sudo /sbin/service $1 start
438 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500439}
440
441
442# Service wrapper to stop services
443# stop_service service-name
444function stop_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600445 if [[ -z "$os_PACKAGE" ]]; then
446 GetOSVersion
447 fi
448 if [[ "$os_PACKAGE" = "deb" ]]; then
449 sudo /usr/sbin/service $1 stop
450 else
451 sudo /sbin/service $1 stop
452 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600453}
454
455
456# Normalize config values to True or False
457# VAR=`trueorfalse default-value test-value`
458function trueorfalse() {
459 local default=$1
460 local testval=$2
461
462 [[ -z "$testval" ]] && { echo "$default"; return; }
463 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
464 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
465 echo "$default"
466}
Dean Troyer27e32692012-03-16 16:16:56 -0500467
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500468
469# yum wrapper to set arguments correctly
470# yum_install package [package ...]
471function yum_install() {
472 [[ "$OFFLINE" = "True" ]] && return
473 local sudo="sudo"
474 [[ "$(id -u)" = "0" ]] && sudo="env"
475 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
476 yum install -y "$@"
477}
478
479
Dean Troyer27e32692012-03-16 16:16:56 -0500480# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000481$XTRACE