blob: 5114de1060c3430a75b9752ef4dafb3e136ef26e [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
145# git_clone remote dest-dir branch
146function git_clone {
147 [[ "$OFFLINE" = "True" ]] && return
148
149 GIT_REMOTE=$1
150 GIT_DEST=$2
151 GIT_BRANCH=$3
152
153 if echo $GIT_BRANCH | egrep -q "^refs"; then
154 # If our branch name is a gerrit style refs/changes/...
155 if [[ ! -d $GIT_DEST ]]; then
156 git clone $GIT_REMOTE $GIT_DEST
157 fi
158 cd $GIT_DEST
159 git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD
160 else
161 # do a full clone only if the directory doesn't exist
162 if [[ ! -d $GIT_DEST ]]; then
163 git clone $GIT_REMOTE $GIT_DEST
164 cd $GIT_DEST
165 # This checkout syntax works for both branches and tags
166 git checkout $GIT_BRANCH
167 elif [[ "$RECLONE" == "yes" ]]; then
168 # if it does exist then simulate what clone does if asked to RECLONE
169 cd $GIT_DEST
170 # set the url to pull from and fetch
171 git remote set-url origin $GIT_REMOTE
172 git fetch origin
173 # remove the existing ignored files (like pyc) as they cause breakage
174 # (due to the py files having older timestamps than our pyc, so python
175 # thinks the pyc files are correct using them)
176 find $GIT_DEST -name '*.pyc' -delete
177 git checkout -f origin/$GIT_BRANCH
178 # a local branch might not exist
179 git branch -D $GIT_BRANCH || true
180 git checkout -b $GIT_BRANCH
181 fi
182 fi
183}
184
185
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500186# Comment an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500187# iniset config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500188function inicomment() {
189 local file=$1
190 local section=$2
191 local option=$3
192 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
193}
194
195
196# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500197# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500198function iniget() {
199 local file=$1
200 local section=$2
201 local option=$3
202 local line
203 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
204 echo ${line#*=}
205}
206
207
208# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500209# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500210function iniset() {
211 local file=$1
212 local section=$2
213 local option=$3
214 local value=$4
Dean Troyer09e636e2012-03-19 16:31:12 -0500215 if ! grep -q "^\[$section\]" $file; then
216 # Add section at the end
217 echo -e "\n[$section]" >>$file
218 fi
219 if [[ -z "$(iniget $file $section $option)" ]]; then
220 # Add it
221 sed -i -e "/^\[$section\]/ a\\
222$option = $value
223" $file
224 else
225 # Replace it
226 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
227 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500228}
229
230
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000231# is_service_enabled() checks if the service(s) specified as arguments are
232# enabled by the user in **ENABLED_SERVICES**.
233#
234# If there are multiple services specified as arguments the test performs a
235# boolean OR or if any of the services specified on the command line
236# return true.
237#
238# There is a special cases for some 'catch-all' services::
239# **nova** returns true if any service enabled start with **n-**
240# **glance** returns true if any service enabled start with **g-**
241# **quantum** returns true if any service enabled start with **q-**
242function is_service_enabled() {
243 services=$@
244 for service in ${services}; do
245 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
246 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
247 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
248 [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
249 done
250 return 1
251}
252
Dean Troyer489bd2a2012-03-02 10:44:29 -0600253
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500254# Distro-agnostic package installer
255# install_package package [package ...]
256function install_package() {
257 if [[ -z "$os_PACKAGE" ]]; then
258 GetOSVersion
259 fi
260 if [[ "$os_PACKAGE" = "deb" ]]; then
261 apt_get install "$@"
262 else
263 yum_install "$@"
264 fi
265}
266
267
Dean Troyer489bd2a2012-03-02 10:44:29 -0600268# Test if the named environment variable is set and not zero length
269# is_set env-var
270function is_set() {
271 local var=\$"$1"
272 if eval "[ -z $var ]"; then
273 return 1
274 fi
275 return 0
276}
277
278
Dean Troyer7f9aa712012-01-31 12:11:56 -0600279# pip install wrapper to set cache and proxy environment variables
280# pip_install package [package ...]
281function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600282 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500283 if [[ -z "$os_PACKAGE" ]]; then
284 GetOSVersion
285 fi
286 if [[ "$os_PACKAGE" = "deb" ]]; then
287 CMD_PIP=/usr/bin/pip
288 else
289 CMD_PIP=/usr/bin/pip-python
290 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -0600291 sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \
292 HTTP_PROXY=$http_proxy \
293 HTTPS_PROXY=$https_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500294 $CMD_PIP install --use-mirrors $@
295}
296
297
298# Service wrapper to restart services
299# restart_service service-name
300function restart_service() {
301 sudo /usr/sbin/service $1 restart
302}
303
304
305# Service wrapper to start services
306# start_service service-name
307function start_service() {
308 sudo /usr/sbin/service $1 start
309}
310
311
312# Service wrapper to stop services
313# stop_service service-name
314function stop_service() {
315 sudo /usr/sbin/service $1 stop
Dean Troyer7f9aa712012-01-31 12:11:56 -0600316}
317
318
319# Normalize config values to True or False
320# VAR=`trueorfalse default-value test-value`
321function trueorfalse() {
322 local default=$1
323 local testval=$2
324
325 [[ -z "$testval" ]] && { echo "$default"; return; }
326 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
327 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
328 echo "$default"
329}
Dean Troyer27e32692012-03-16 16:16:56 -0500330
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500331
332# yum wrapper to set arguments correctly
333# yum_install package [package ...]
334function yum_install() {
335 [[ "$OFFLINE" = "True" ]] && return
336 local sudo="sudo"
337 [[ "$(id -u)" = "0" ]] && sudo="env"
338 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
339 yum install -y "$@"
340}
341
342
Dean Troyer27e32692012-03-16 16:16:56 -0500343# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000344$XTRACE