blob: a3e95370fbccc6e34fd30e05faf39aa95bedfc5f [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 Troyer13dc5cc2012-03-27 14:50:45 -050076# Determine OS Vendor, Release and Update
77# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
78# Returns results in global variables:
79# os_VENDOR - vendor name
80# os_RELEASE - release
81# os_UPDATE - update
82# os_PACKAGE - package type
83# os_CODENAME - vendor's codename for release
84# GetOSVersion
85GetOSVersion() {
86 # Figure out which vendor we are
87 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
88 # OS/X
89 os_VENDOR=`sw_vers -productName`
90 os_RELEASE=`sw_vers -productVersion`
91 os_UPDATE=${os_RELEASE##*.}
92 os_RELEASE=${os_RELEASE%.*}
93 os_PACKAGE=""
94 if [[ "$os_RELEASE" =~ "10.7" ]]; then
95 os_CODENAME="lion"
96 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
97 os_CODENAME="snow leopard"
98 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
99 os_CODENAME="leopard"
100 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
101 os_CODENAME="tiger"
102 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
103 os_CODENAME="panther"
104 else
105 os_CODENAME=""
106 fi
107 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
108 os_VENDOR=$(lsb_release -i -s)
109 os_RELEASE=$(lsb_release -r -s)
110 os_UPDATE=""
111 if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then
112 os_PACKAGE="deb"
113 else
114 os_PACKAGE="rpm"
115 fi
116 os_CODENAME=$(lsb_release -c -s)
117 elif [[ -r /etc/redhat-release ]]; then
118 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
119 # CentOS release 5.5 (Final)
120 # CentOS Linux release 6.0 (Final)
121 # Fedora release 16 (Verne)
122 os_CODENAME=""
123 for r in "Red Hat" CentOS Fedora; do
124 os_VENDOR=$r
125 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
126 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
127 os_CODENAME=${ver#*|}
128 os_RELEASE=${ver%|*}
129 os_UPDATE=${os_RELEASE##*.}
130 os_RELEASE=${os_RELEASE%.*}
131 break
132 fi
133 os_VENDOR=""
134 done
135 os_PACKAGE="rpm"
136 fi
137 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
138}
139
140
Dean Troyer7f9aa712012-01-31 12:11:56 -0600141# git clone only if directory doesn't exist already. Since ``DEST`` might not
142# be owned by the installation user, we create the directory and change the
143# ownership to the proper user.
144# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700145# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
146# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer7f9aa712012-01-31 12:11:56 -0600147# git_clone remote dest-dir branch
148function git_clone {
149 [[ "$OFFLINE" = "True" ]] && return
150
151 GIT_REMOTE=$1
152 GIT_DEST=$2
153 GIT_BRANCH=$3
154
155 if echo $GIT_BRANCH | egrep -q "^refs"; then
156 # If our branch name is a gerrit style refs/changes/...
157 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700158 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600159 git clone $GIT_REMOTE $GIT_DEST
160 fi
161 cd $GIT_DEST
162 git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD
163 else
164 # do a full clone only if the directory doesn't exist
165 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700166 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600167 git clone $GIT_REMOTE $GIT_DEST
168 cd $GIT_DEST
169 # This checkout syntax works for both branches and tags
170 git checkout $GIT_BRANCH
171 elif [[ "$RECLONE" == "yes" ]]; then
172 # if it does exist then simulate what clone does if asked to RECLONE
173 cd $GIT_DEST
174 # set the url to pull from and fetch
175 git remote set-url origin $GIT_REMOTE
176 git fetch origin
177 # remove the existing ignored files (like pyc) as they cause breakage
178 # (due to the py files having older timestamps than our pyc, so python
179 # thinks the pyc files are correct using them)
180 find $GIT_DEST -name '*.pyc' -delete
181 git checkout -f origin/$GIT_BRANCH
182 # a local branch might not exist
183 git branch -D $GIT_BRANCH || true
184 git checkout -b $GIT_BRANCH
185 fi
186 fi
187}
188
189
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500190# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200191# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500192function inicomment() {
193 local file=$1
194 local section=$2
195 local option=$3
196 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
197}
198
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200199# Uncomment an option in an INI file
200# iniuncomment config-file section option
201function iniuncomment() {
202 local file=$1
203 local section=$2
204 local option=$3
205 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
206}
207
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500208
209# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500210# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500211function iniget() {
212 local file=$1
213 local section=$2
214 local option=$3
215 local line
216 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
217 echo ${line#*=}
218}
219
220
221# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500222# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500223function iniset() {
224 local file=$1
225 local section=$2
226 local option=$3
227 local value=$4
Dean Troyer09e636e2012-03-19 16:31:12 -0500228 if ! grep -q "^\[$section\]" $file; then
229 # Add section at the end
230 echo -e "\n[$section]" >>$file
231 fi
232 if [[ -z "$(iniget $file $section $option)" ]]; then
233 # Add it
234 sed -i -e "/^\[$section\]/ a\\
235$option = $value
236" $file
237 else
238 # Replace it
239 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
240 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500241}
242
243
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000244# is_service_enabled() checks if the service(s) specified as arguments are
245# enabled by the user in **ENABLED_SERVICES**.
246#
247# If there are multiple services specified as arguments the test performs a
248# boolean OR or if any of the services specified on the command line
249# return true.
250#
251# There is a special cases for some 'catch-all' services::
252# **nova** returns true if any service enabled start with **n-**
253# **glance** returns true if any service enabled start with **g-**
254# **quantum** returns true if any service enabled start with **q-**
255function is_service_enabled() {
256 services=$@
257 for service in ${services}; do
258 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
259 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500260 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000261 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
262 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
263 done
264 return 1
265}
266
Dean Troyer489bd2a2012-03-02 10:44:29 -0600267
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500268# Distro-agnostic package installer
269# install_package package [package ...]
270function install_package() {
271 if [[ -z "$os_PACKAGE" ]]; then
272 GetOSVersion
273 fi
274 if [[ "$os_PACKAGE" = "deb" ]]; then
275 apt_get install "$@"
276 else
277 yum_install "$@"
278 fi
279}
280
281
Dean Troyer489bd2a2012-03-02 10:44:29 -0600282# Test if the named environment variable is set and not zero length
283# is_set env-var
284function is_set() {
285 local var=\$"$1"
286 if eval "[ -z $var ]"; then
287 return 1
288 fi
289 return 0
290}
291
292
Dean Troyer7f9aa712012-01-31 12:11:56 -0600293# pip install wrapper to set cache and proxy environment variables
294# pip_install package [package ...]
295function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600296 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500297 if [[ -z "$os_PACKAGE" ]]; then
298 GetOSVersion
299 fi
300 if [[ "$os_PACKAGE" = "deb" ]]; then
301 CMD_PIP=/usr/bin/pip
302 else
303 CMD_PIP=/usr/bin/pip-python
304 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600305 sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \
306 HTTP_PROXY=$http_proxy \
307 HTTPS_PROXY=$https_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500308 $CMD_PIP install --use-mirrors $@
309}
310
311
312# Service wrapper to restart services
313# restart_service service-name
314function restart_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600315 if [[ -z "$os_PACKAGE" ]]; then
316 GetOSVersion
317 fi
318 if [[ "$os_PACKAGE" = "deb" ]]; then
319 sudo /usr/sbin/service $1 restart
320 else
321 sudo /sbin/service $1 restart
322 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500323}
324
325
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500326# pip install the dependencies of the package before we do the setup.py
327# develop, so that pip and not distutils process the dependency chain
328# setup_develop directory
329function setup_develop() {
330 (cd $1; \
331 python setup.py egg_info; \
332 raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \
333 depend_links=$(echo $raw_links | xargs); \
334 pip_install -r *-info/requires.txt $depend_links; \
335 sudo \
336 HTTP_PROXY=$http_proxy \
337 HTTPS_PROXY=$https_proxy \
338 python setup.py develop \
339 )
340}
341
342
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500343# Service wrapper to start services
344# start_service service-name
345function start_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600346 if [[ -z "$os_PACKAGE" ]]; then
347 GetOSVersion
348 fi
349 if [[ "$os_PACKAGE" = "deb" ]]; then
350 sudo /usr/sbin/service $1 start
351 else
352 sudo /sbin/service $1 start
353 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500354}
355
356
357# Service wrapper to stop services
358# stop_service service-name
359function stop_service() {
Dean Troyer5218d452012-02-04 02:13:23 -0600360 if [[ -z "$os_PACKAGE" ]]; then
361 GetOSVersion
362 fi
363 if [[ "$os_PACKAGE" = "deb" ]]; then
364 sudo /usr/sbin/service $1 stop
365 else
366 sudo /sbin/service $1 stop
367 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600368}
369
370
371# Normalize config values to True or False
372# VAR=`trueorfalse default-value test-value`
373function trueorfalse() {
374 local default=$1
375 local testval=$2
376
377 [[ -z "$testval" ]] && { echo "$default"; return; }
378 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
379 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
380 echo "$default"
381}
Dean Troyer27e32692012-03-16 16:16:56 -0500382
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500383
384# yum wrapper to set arguments correctly
385# yum_install package [package ...]
386function yum_install() {
387 [[ "$OFFLINE" = "True" ]] && return
388 local sudo="sudo"
389 [[ "$(id -u)" = "0" ]] && sudo="env"
390 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
391 yum install -y "$@"
392}
393
394
Dean Troyer27e32692012-03-16 16:16:56 -0500395# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000396$XTRACE