blob: f4a3da113f013f24f65523b47d2181933f69d87f [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#
Dean Troyer4a43b7b2012-08-28 17:43:40 -05003# The following variables are assumed to be defined by certain functions:
Dean Troyer4a43b7b2012-08-28 17:43:40 -05004# ``ENABLED_SERVICES``
5# ``EROR_ON_CLONE``
6# ``FILES``
7# ``GLANCE_HOSTPORT``
8# ``OFFLINE``
9# ``PIP_DOWNLOAD_CACHE``
Maru Newby3a87edd2012-10-25 23:01:06 +000010# ``PIP_USE_MIRRORS``
Dean Troyer4a43b7b2012-08-28 17:43:40 -050011# ``RECLONE``
12# ``TRACK_DEPENDS``
13# ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer13dc5cc2012-03-27 14:50:45 -050014
Dean Troyer7f9aa712012-01-31 12:11:56 -060015
Dean Troyer27e32692012-03-16 16:16:56 -050016# Save trace setting
17XTRACE=$(set +o | grep xtrace)
18set +o xtrace
19
Dean Troyer7f9aa712012-01-31 12:11:56 -060020
Dean Troyer4a43b7b2012-08-28 17:43:40 -050021# Exit 0 if address is in network or 1 if address is not in
22# network or netaddr library is not installed.
23# address_in_net ip-address ip-range
Vishvananda Ishayac9ad14b2012-07-03 20:29:01 +000024function address_in_net() {
25 python -c "
26import netaddr
27import sys
28sys.exit(netaddr.IPAddress('$1') not in netaddr.IPNetwork('$2'))
29"
30}
31
32
Dean Troyer4a43b7b2012-08-28 17:43:40 -050033# Wrapper for ``apt-get`` to set cache and proxy environment variables
34# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -050035# apt_get operation package [package ...]
Dean Troyer7f9aa712012-01-31 12:11:56 -060036function apt_get() {
Dean Troyerd0b21e22012-03-07 14:52:25 -060037 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer7f9aa712012-01-31 12:11:56 -060038 local sudo="sudo"
39 [[ "$(id -u)" = "0" ]] && sudo="env"
40 $sudo DEBIAN_FRONTEND=noninteractive \
41 http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +090042 no_proxy=$no_proxy \
Dean Troyer7f9aa712012-01-31 12:11:56 -060043 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
44}
45
46
47# Gracefully cp only if source file/dir exists
48# cp_it source destination
49function cp_it {
50 if [ -e $1 ] || [ -d $1 ]; then
51 cp -pRL $1 $2
52 fi
53}
54
55
Dean Troyerac93efb2013-03-13 14:30:54 -050056# Prints line number and "message" then exits
57# die $LINENO "message"
Dean Troyer27e32692012-03-16 16:16:56 -050058function die() {
Dean Troyer489bd2a2012-03-02 10:44:29 -060059 local exitcode=$?
Dean Troyer896eb662013-04-05 15:02:01 -050060 set +o xtrace
61 local line=$1; shift
Nachi Ueno07115eb2013-02-26 12:38:18 -080062 if [ $exitcode == 0 ]; then
63 exitcode=1
64 fi
Dean Troyer896eb662013-04-05 15:02:01 -050065 err $line "$*"
Dean Troyer27e32692012-03-16 16:16:56 -050066 exit $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -060067}
68
69
70# Checks an environment variable is not set or has length 0 OR if the
71# exit code is non-zero and prints "message" and exits
72# NOTE: env-var is the variable name without a '$'
Dean Troyerac93efb2013-03-13 14:30:54 -050073# die_if_not_set $LINENO env-var "message"
Dean Troyer489bd2a2012-03-02 10:44:29 -060074function die_if_not_set() {
Dean Troyer896eb662013-04-05 15:02:01 -050075 local exitcode=$?
76 FXTRACE=$(set +o | grep xtrace)
77 set +o xtrace
78 local line=$1; shift
79 local evar=$1; shift
80 if ! is_set $evar || [ $exitcode != 0 ]; then
81 die $line "$*"
82 fi
83 $FXTRACE
84}
85
86
87# Prints line number and "message" in error format
88# err $LINENO "message"
89function err() {
90 local exitcode=$?
91 errXTRACE=$(set +o | grep xtrace)
92 set +o xtrace
93 local msg="[ERROR] $0:$1 $2"
94 echo $msg 1>&2;
95 if [[ -n ${SCREEN_LOGDIR} ]]; then
96 echo $msg >> "${SCREEN_LOGDIR}/error.log"
97 fi
98 $errXTRACE
99 return $exitcode
100}
101
102
103# Checks an environment variable is not set or has length 0 OR if the
104# exit code is non-zero and prints "message"
105# NOTE: env-var is the variable name without a '$'
106# err_if_not_set $LINENO env-var "message"
107function err_if_not_set() {
108 local exitcode=$?
109 errinsXTRACE=$(set +o | grep xtrace)
110 set +o xtrace
111 local line=$1; shift
112 local evar=$1; shift
113 if ! is_set $evar || [ $exitcode != 0 ]; then
114 err $line "$*"
115 fi
116 $errinsXTRACE
117 return $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -0600118}
119
120
Dean Troyer48352ee2012-12-12 12:50:38 -0600121# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
122# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
123# ``localrc`` or on the command line if necessary::
124#
125# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
126#
127# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
128
129function export_proxy_variables() {
130 if [[ -n "$http_proxy" ]]; then
131 export http_proxy=$http_proxy
132 fi
133 if [[ -n "$https_proxy" ]]; then
134 export https_proxy=$https_proxy
135 fi
136 if [[ -n "$no_proxy" ]]; then
137 export no_proxy=$no_proxy
138 fi
139}
140
141
Dean Troyer489bd2a2012-03-02 10:44:29 -0600142# Grab a numbered field from python prettytable output
143# Fields are numbered starting with 1
144# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
145# get_field field-number
146function get_field() {
147 while read data; do
148 if [ "$1" -lt 0 ]; then
149 field="(\$(NF$1))"
150 else
151 field="\$$(($1 + 1))"
152 fi
153 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
154 done
155}
156
157
Dean Troyerc892bde2013-03-13 14:06:13 -0500158# Get the default value for HOST_IP
159# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
160function get_default_host_ip() {
161 local fixed_range=$1
162 local floating_range=$2
163 local host_ip_iface=$3
164 local host_ip=$4
165
166 # Find the interface used for the default route
167 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
168 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
169 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
170 host_ip=""
171 host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}'`
172 for IP in $host_ips; do
173 # Attempt to filter out IP addresses that are part of the fixed and
174 # floating range. Note that this method only works if the ``netaddr``
175 # python library is installed. If it is not installed, an error
176 # will be printed and the first IP from the interface will be used.
177 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
178 # address.
179 if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then
180 host_ip=$IP
181 break;
182 fi
183 done
184 fi
185 echo $host_ip
186}
187
188
Isaku Yamahata8c438092013-02-12 22:30:56 +0900189function _get_package_dir() {
190 local pkg_dir
191 if is_ubuntu; then
192 pkg_dir=$FILES/apts
193 elif is_fedora; then
194 pkg_dir=$FILES/rpms
195 elif is_suse; then
196 pkg_dir=$FILES/rpms-suse
197 else
198 exit_distro_not_supported "list of packages"
199 fi
200 echo "$pkg_dir"
201}
202
Dean Troyer1a6d4492013-06-03 16:47:36 -0500203
Dean Troyer7e270512012-06-14 15:23:24 -0500204# get_packages() collects a list of package names of any type from the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500205# prerequisite files in ``files/{apts|rpms}``. The list is intended
206# to be passed to a package installer such as apt or yum.
Dean Troyer7e270512012-06-14 15:23:24 -0500207#
Isaku Yamahata8c438092013-02-12 22:30:56 +0900208# Only packages required for the services in 1st argument will be
Dean Troyer7e270512012-06-14 15:23:24 -0500209# included. Two bits of metadata are recognized in the prerequisite files:
210# - ``# NOPRIME`` defers installation to be performed later in stack.sh
211# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
212# of the package to the distros listed. The distro names are case insensitive.
Dean Troyer7e270512012-06-14 15:23:24 -0500213function get_packages() {
Isaku Yamahata8c438092013-02-12 22:30:56 +0900214 local services=$1
215 local package_dir=$(_get_package_dir)
Dean Troyer7e270512012-06-14 15:23:24 -0500216 local file_to_parse
217 local service
218
219 if [[ -z "$package_dir" ]]; then
220 echo "No package directory supplied"
221 return 1
222 fi
223 if [[ -z "$DISTRO" ]]; then
Vincent Untz855c5872012-10-04 13:36:46 +0200224 GetDistro
Dean Troyer7e270512012-06-14 15:23:24 -0500225 fi
Isaku Yamahata8c438092013-02-12 22:30:56 +0900226 for service in general ${services//,/ }; do
Dean Troyer7e270512012-06-14 15:23:24 -0500227 # Allow individual services to specify dependencies
228 if [[ -e ${package_dir}/${service} ]]; then
229 file_to_parse="${file_to_parse} $service"
230 fi
231 # NOTE(sdague) n-api needs glance for now because that's where
232 # glance client is
233 if [[ $service == n-api ]]; then
234 if [[ ! $file_to_parse =~ nova ]]; then
235 file_to_parse="${file_to_parse} nova"
236 fi
237 if [[ ! $file_to_parse =~ glance ]]; then
238 file_to_parse="${file_to_parse} glance"
239 fi
240 elif [[ $service == c-* ]]; then
241 if [[ ! $file_to_parse =~ cinder ]]; then
242 file_to_parse="${file_to_parse} cinder"
243 fi
John H. Tran93361642012-07-26 11:22:05 -0700244 elif [[ $service == ceilometer-* ]]; then
245 if [[ ! $file_to_parse =~ ceilometer ]]; then
246 file_to_parse="${file_to_parse} ceilometer"
247 fi
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100248 elif [[ $service == s-* ]]; then
249 if [[ ! $file_to_parse =~ swift ]]; then
250 file_to_parse="${file_to_parse} swift"
251 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500252 elif [[ $service == n-* ]]; then
253 if [[ ! $file_to_parse =~ nova ]]; then
254 file_to_parse="${file_to_parse} nova"
255 fi
256 elif [[ $service == g-* ]]; then
257 if [[ ! $file_to_parse =~ glance ]]; then
258 file_to_parse="${file_to_parse} glance"
259 fi
260 elif [[ $service == key* ]]; then
261 if [[ ! $file_to_parse =~ keystone ]]; then
262 file_to_parse="${file_to_parse} keystone"
263 fi
Robert Collins0a9954f2012-11-20 11:34:25 +1300264 elif [[ $service == q-* ]]; then
Mark McClainb05c8762013-07-06 23:29:39 -0400265 if [[ ! $file_to_parse =~ neutron ]]; then
266 file_to_parse="${file_to_parse} neutron"
Robert Collins0a9954f2012-11-20 11:34:25 +1300267 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500268 fi
269 done
270
271 for file in ${file_to_parse}; do
272 local fname=${package_dir}/${file}
273 local OIFS line package distros distro
274 [[ -e $fname ]] || continue
275
276 OIFS=$IFS
277 IFS=$'\n'
278 for line in $(<${fname}); do
279 if [[ $line =~ "NOPRIME" ]]; then
280 continue
281 fi
282
283 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
284 # We are using BASH regexp matching feature.
285 package=${BASH_REMATCH[1]}
286 distros=${BASH_REMATCH[2]}
287 # In bash ${VAR,,} will lowecase VAR
288 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
289 continue
290 fi
291
292 echo ${line%#*}
293 done
294 IFS=$OIFS
295 done
296}
297
298
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500299# Determine OS Vendor, Release and Update
300# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
301# Returns results in global variables:
302# os_VENDOR - vendor name
303# os_RELEASE - release
304# os_UPDATE - update
305# os_PACKAGE - package type
306# os_CODENAME - vendor's codename for release
307# GetOSVersion
308GetOSVersion() {
309 # Figure out which vendor we are
310 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
311 # OS/X
312 os_VENDOR=`sw_vers -productName`
313 os_RELEASE=`sw_vers -productVersion`
314 os_UPDATE=${os_RELEASE##*.}
315 os_RELEASE=${os_RELEASE%.*}
316 os_PACKAGE=""
317 if [[ "$os_RELEASE" =~ "10.7" ]]; then
318 os_CODENAME="lion"
319 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
320 os_CODENAME="snow leopard"
321 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
322 os_CODENAME="leopard"
323 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
324 os_CODENAME="tiger"
325 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
326 os_CODENAME="panther"
327 else
328 os_CODENAME=""
329 fi
330 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
331 os_VENDOR=$(lsb_release -i -s)
332 os_RELEASE=$(lsb_release -r -s)
333 os_UPDATE=""
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100334 os_PACKAGE="rpm"
Derek Morton4a8496e2013-04-08 23:46:08 -0500335 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500336 os_PACKAGE="deb"
Vincent Untz856a11e2012-11-21 16:04:12 +0100337 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
338 lsb_release -d -s | grep -q openSUSE
339 if [[ $? -eq 0 ]]; then
340 os_VENDOR="openSUSE"
341 fi
Vincent Untzcd1fe982013-03-12 18:04:29 +0100342 elif [[ $os_VENDOR == "openSUSE project" ]]; then
343 os_VENDOR="openSUSE"
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100344 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
345 os_VENDOR="Red Hat"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500346 fi
347 os_CODENAME=$(lsb_release -c -s)
348 elif [[ -r /etc/redhat-release ]]; then
349 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
350 # CentOS release 5.5 (Final)
351 # CentOS Linux release 6.0 (Final)
352 # Fedora release 16 (Verne)
353 os_CODENAME=""
354 for r in "Red Hat" CentOS Fedora; do
355 os_VENDOR=$r
356 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
357 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
358 os_CODENAME=${ver#*|}
359 os_RELEASE=${ver%|*}
360 os_UPDATE=${os_RELEASE##*.}
361 os_RELEASE=${os_RELEASE%.*}
362 break
363 fi
364 os_VENDOR=""
365 done
366 os_PACKAGE="rpm"
Vincent Untz856a11e2012-11-21 16:04:12 +0100367 elif [[ -r /etc/SuSE-release ]]; then
368 for r in openSUSE "SUSE Linux"; do
369 if [[ "$r" = "SUSE Linux" ]]; then
370 os_VENDOR="SUSE LINUX"
371 else
372 os_VENDOR=$r
373 fi
374
375 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
376 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
377 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
378 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
379 break
380 fi
381 os_VENDOR=""
382 done
383 os_PACKAGE="rpm"
Émilien Macchib2ef8902013-05-04 00:48:20 +0200384 # If lsb_release is not installed, we should be able to detect Debian OS
385 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
386 os_VENDOR="Debian"
387 os_PACKAGE="deb"
388 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
389 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500390 fi
391 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
392}
393
Andrew Laskif900bd72012-09-05 17:23:14 -0400394
Dean Troyera9e0a482012-07-09 14:07:23 -0500395# Translate the OS version values into common nomenclature
396# Sets ``DISTRO`` from the ``os_*`` values
397function GetDistro() {
398 GetOSVersion
Émilien Macchib2ef8902013-05-04 00:48:20 +0200399 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
400 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
Dean Troyera9e0a482012-07-09 14:07:23 -0500401 DISTRO=$os_CODENAME
402 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
403 # For Fedora, just use 'f' and the release
404 DISTRO="f$os_RELEASE"
Vincent Untz856a11e2012-11-21 16:04:12 +0100405 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
406 DISTRO="opensuse-$os_RELEASE"
407 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
408 # For SLE, also use the service pack
409 if [[ -z "$os_UPDATE" ]]; then
410 DISTRO="sle${os_RELEASE}"
411 else
412 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
413 fi
Ian Wienandd857f4b2013-03-20 14:51:06 +1100414 elif [[ "$os_VENDOR" =~ (Red Hat) || "$os_VENDOR" =~ (CentOS) ]]; then
415 # Drop the . release as we assume it's compatible
416 DISTRO="rhel${os_RELEASE::1}"
Dean Troyera9e0a482012-07-09 14:07:23 -0500417 else
418 # Catch-all for now is Vendor + Release + Update
419 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
420 fi
421 export DISTRO
422}
423
424
Vincent Untz00011c02012-12-06 09:56:32 +0100425# Determine if current distribution is a Fedora-based distribution
Dean Troyer1a6d4492013-06-03 16:47:36 -0500426# (Fedora, RHEL, CentOS, etc).
Vincent Untz00011c02012-12-06 09:56:32 +0100427# is_fedora
428function is_fedora {
429 if [[ -z "$os_VENDOR" ]]; then
430 GetOSVersion
431 fi
432
433 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ]
434}
435
Dean Troyer1a6d4492013-06-03 16:47:36 -0500436
Vincent Untz856a11e2012-11-21 16:04:12 +0100437# Determine if current distribution is a SUSE-based distribution
438# (openSUSE, SLE).
439# is_suse
440function is_suse {
441 if [[ -z "$os_VENDOR" ]]; then
442 GetOSVersion
443 fi
444
Steve Baker1a7bbd22012-12-03 17:04:02 +1300445 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
Vincent Untz856a11e2012-11-21 16:04:12 +0100446}
447
448
Dean Troyer1a6d4492013-06-03 16:47:36 -0500449# Determine if current distribution is an Ubuntu-based distribution
450# It will also detect non-Ubuntu but Debian-based distros
451# is_ubuntu
452function is_ubuntu {
453 if [[ -z "$os_PACKAGE" ]]; then
454 GetOSVersion
455 fi
456 [ "$os_PACKAGE" = "deb" ]
457}
458
459
Vincent Untz00011c02012-12-06 09:56:32 +0100460# Exit after outputting a message about the distribution not being supported.
461# exit_distro_not_supported [optional-string-telling-what-is-missing]
462function exit_distro_not_supported {
463 if [[ -z "$DISTRO" ]]; then
464 GetDistro
465 fi
466
467 if [ $# -gt 0 ]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800468 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
Vincent Untz00011c02012-12-06 09:56:32 +0100469 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800470 die $LINENO "Support for $DISTRO is incomplete."
Vincent Untz00011c02012-12-06 09:56:32 +0100471 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100472}
473
Daniel Jonesfa868cb2013-06-18 15:28:01 -0500474# Utility function for checking machine architecture
475# is_arch arch-type
476function is_arch {
477 ARCH_TYPE=$1
478
479 [ "($uname -m)" = "$ARCH_TYPE" ]
480}
Vincent Untz00011c02012-12-06 09:56:32 +0100481
Dean Troyer7f9aa712012-01-31 12:11:56 -0600482# git clone only if directory doesn't exist already. Since ``DEST`` might not
483# be owned by the installation user, we create the directory and change the
484# ownership to the proper user.
485# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700486# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
487# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500488# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600489# git_clone remote dest-dir branch
490function git_clone {
491 [[ "$OFFLINE" = "True" ]] && return
492
493 GIT_REMOTE=$1
494 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300495 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600496
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300497 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600498 # If our branch name is a gerrit style refs/changes/...
499 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700500 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600501 git clone $GIT_REMOTE $GIT_DEST
502 fi
503 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300504 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600505 else
506 # do a full clone only if the directory doesn't exist
507 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700508 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600509 git clone $GIT_REMOTE $GIT_DEST
510 cd $GIT_DEST
511 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300512 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600513 elif [[ "$RECLONE" == "yes" ]]; then
514 # if it does exist then simulate what clone does if asked to RECLONE
515 cd $GIT_DEST
516 # set the url to pull from and fetch
517 git remote set-url origin $GIT_REMOTE
518 git fetch origin
519 # remove the existing ignored files (like pyc) as they cause breakage
520 # (due to the py files having older timestamps than our pyc, so python
521 # thinks the pyc files are correct using them)
522 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300523
524 # handle GIT_REF accordingly to type (tag, branch)
525 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
526 git_update_tag $GIT_REF
527 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
528 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400529 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
530 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300531 else
532 echo $GIT_REF is neither branch nor tag
533 exit 1
534 fi
535
Dean Troyer7f9aa712012-01-31 12:11:56 -0600536 fi
537 fi
538}
539
540
Dean Troyer1a6d4492013-06-03 16:47:36 -0500541# git update using reference as a branch.
542# git_update_branch ref
543function git_update_branch() {
544
545 GIT_BRANCH=$1
546
547 git checkout -f origin/$GIT_BRANCH
548 # a local branch might not exist
549 git branch -D $GIT_BRANCH || true
550 git checkout -b $GIT_BRANCH
551}
552
553
554# git update using reference as a branch.
555# git_update_remote_branch ref
556function git_update_remote_branch() {
557
558 GIT_BRANCH=$1
559
560 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
561}
562
563
564# git update using reference as a tag. Be careful editing source at that repo
565# as working copy will be in a detached mode
566# git_update_tag ref
567function git_update_tag() {
568
569 GIT_TAG=$1
570
571 git tag -d $GIT_TAG
572 # fetching given tag only
573 git fetch origin tag $GIT_TAG
574 git checkout -f $GIT_TAG
575}
576
577
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500578# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200579# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500580function inicomment() {
581 local file=$1
582 local section=$2
583 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100584 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500585}
586
Dean Troyer896eb662013-04-05 15:02:01 -0500587
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200588# Uncomment an option in an INI file
589# iniuncomment config-file section option
590function iniuncomment() {
591 local file=$1
592 local section=$2
593 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100594 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200595}
596
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500597
598# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500599# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500600function iniget() {
601 local file=$1
602 local section=$2
603 local option=$3
604 local line
Attila Fazekas588eb412012-12-20 10:57:16 +0100605 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500606 echo ${line#*=}
607}
608
Dean Troyer896eb662013-04-05 15:02:01 -0500609
Attila Fazekas588eb412012-12-20 10:57:16 +0100610# Determinate is the given option present in the INI file
611# ini_has_option config-file section option
612function ini_has_option() {
613 local file=$1
614 local section=$2
615 local option=$3
616 local line
617 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
618 [ -n "$line" ]
619}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500620
Dean Troyer896eb662013-04-05 15:02:01 -0500621
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500622# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500623# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500624function iniset() {
625 local file=$1
626 local section=$2
627 local option=$3
628 local value=$4
Attila Fazekas588eb412012-12-20 10:57:16 +0100629 if ! grep -q "^\[$section\]" "$file"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500630 # Add section at the end
Attila Fazekas588eb412012-12-20 10:57:16 +0100631 echo -e "\n[$section]" >>"$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500632 fi
Attila Fazekas588eb412012-12-20 10:57:16 +0100633 if ! ini_has_option "$file" "$section" "$option"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500634 # Add it
Attila Fazekas588eb412012-12-20 10:57:16 +0100635 sed -i -e "/^\[$section\]/ a\\
Dean Troyer09e636e2012-03-19 16:31:12 -0500636$option = $value
Attila Fazekas588eb412012-12-20 10:57:16 +0100637" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500638 else
639 # Replace it
Attila Fazekas588eb412012-12-20 10:57:16 +0100640 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500641 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500642}
643
Dean Troyer896eb662013-04-05 15:02:01 -0500644
Lianhao Lu239f3242013-03-01 15:54:02 +0800645# Get a multiple line option from an INI file
646# iniget_multiline config-file section option
647function iniget_multiline() {
648 local file=$1
649 local section=$2
650 local option=$3
651 local values
652 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
653 echo ${values}
654}
655
Dean Troyer896eb662013-04-05 15:02:01 -0500656
Lianhao Lu239f3242013-03-01 15:54:02 +0800657# Set a multiple line option in an INI file
658# iniset_multiline config-file section option value1 value2 valu3 ...
659function iniset_multiline() {
660 local file=$1
661 local section=$2
662 local option=$3
663 shift 3
664 local values
665 for v in $@; do
666 # The later sed command inserts each new value in the line next to
667 # the section identifier, which causes the values to be inserted in
668 # the reverse order. Do a reverse here to keep the original order.
669 values="$v ${values}"
670 done
671 if ! grep -q "^\[$section\]" "$file"; then
672 # Add section at the end
673 echo -e "\n[$section]" >>"$file"
674 else
675 # Remove old values
676 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
677 fi
678 # Add new ones
679 for v in $values; do
680 sed -i -e "/^\[$section\]/ a\\
681$option = $v
682" "$file"
683 done
684}
685
Dean Troyer896eb662013-04-05 15:02:01 -0500686
Lianhao Lu239f3242013-03-01 15:54:02 +0800687# Append a new option in an ini file without replacing the old value
688# iniadd config-file section option value1 value2 value3 ...
689function iniadd() {
690 local file=$1
691 local section=$2
692 local option=$3
693 shift 3
694 local values="$(iniget_multiline $file $section $option) $@"
695 iniset_multiline $file $section $option $values
696}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500697
Dean Troyer896eb662013-04-05 15:02:01 -0500698# Find out if a process exists by partial name.
699# is_running name
700function is_running() {
701 local name=$1
702 ps auxw | grep -v grep | grep ${name} > /dev/null
703 RC=$?
704 # some times I really hate bash reverse binary logic
705 return $RC
706}
707
708
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000709# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500710# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000711#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500712# Multiple services specified as arguments are ``OR``'ed together; the test
713# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000714#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500715# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000716# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500717# **cinder** returns true if any service enabled start with **c-**
718# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000719# **glance** returns true if any service enabled start with **g-**
Mark McClainb05c8762013-07-06 23:29:39 -0400720# **neutron** returns true if any service enabled start with **q-**
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100721# **swift** returns true if any service enabled start with **s-**
722# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
723# **s-** services will be enabled. This will be deprecated in the future.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500724#
725# Uses global ``ENABLED_SERVICES``
726# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000727function is_service_enabled() {
728 services=$@
729 for service in ${services}; do
730 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
731 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500732 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700733 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000734 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
Mark McClainb05c8762013-07-06 23:29:39 -0400735 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100736 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && return 0
737 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000738 done
739 return 1
740}
741
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500742
743# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
744# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400745function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500746 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400747 s/,,/,/g;
748 s/^,//;
749 s/,$//
750 '
751}
752
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500753
Doug Hellmannf04178f2012-07-05 17:10:03 -0400754# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500755# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400756#
757# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800758# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400759#
760# This function does not know about the special cases
Mark McClainb05c8762013-07-06 23:29:39 -0400761# for nova, glance, and neutron built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500762# Uses global ``ENABLED_SERVICES``
763# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400764function enable_service() {
765 local tmpsvcs="${ENABLED_SERVICES}"
766 for service in $@; do
767 if ! is_service_enabled $service; then
768 tmpsvcs+=",$service"
769 fi
770 done
771 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
772 disable_negated_services
773}
774
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500775
Doug Hellmannf04178f2012-07-05 17:10:03 -0400776# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500777# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400778#
779# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800780# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400781#
782# This function does not know about the special cases
Mark McClainb05c8762013-07-06 23:29:39 -0400783# for nova, glance, and neutron built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500784# Uses global ``ENABLED_SERVICES``
785# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400786function disable_service() {
787 local tmpsvcs=",${ENABLED_SERVICES},"
788 local service
789 for service in $@; do
790 if is_service_enabled $service; then
791 tmpsvcs=${tmpsvcs//,$service,/,}
792 fi
793 done
794 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
795}
796
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500797
Doug Hellmannf04178f2012-07-05 17:10:03 -0400798# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500799# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400800# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500801# Uses global ``ENABLED_SERVICES``
802# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400803function disable_all_services() {
804 ENABLED_SERVICES=""
805}
806
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500807
808# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800809# services except rabbit (rabbit) set in ``localrc``:
810# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500811# Uses global ``ENABLED_SERVICES``
812# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400813function disable_negated_services() {
814 local tmpsvcs="${ENABLED_SERVICES}"
815 local service
816 for service in ${tmpsvcs//,/ }; do
817 if [[ ${service} == -* ]]; then
818 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
819 fi
820 done
821 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
822}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600823
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500824
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500825# Distro-agnostic package installer
826# install_package package [package ...]
827function install_package() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100828 if is_ubuntu; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200829 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
830 NO_UPDATE_REPOS=True
831
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500832 apt_get install "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100833 elif is_fedora; then
834 yum_install "$@"
835 elif is_suse; then
836 zypper_install "$@"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500837 else
Vincent Untz00011c02012-12-06 09:56:32 +0100838 exit_distro_not_supported "installing packages"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500839 fi
840}
841
842
Dean Troyer995eb922013-03-07 16:11:40 -0600843# Distro-agnostic package uninstaller
844# uninstall_package package [package ...]
845function uninstall_package() {
846 if is_ubuntu; then
847 apt_get purge "$@"
848 elif is_fedora; then
Ian Wienand2c678cc2013-03-20 13:00:44 +1100849 sudo yum remove -y "$@"
Dean Troyer995eb922013-03-07 16:11:40 -0600850 elif is_suse; then
Ian Wienand2c678cc2013-03-20 13:00:44 +1100851 sudo rpm -e "$@"
Dean Troyer995eb922013-03-07 16:11:40 -0600852 else
853 exit_distro_not_supported "uninstalling packages"
854 fi
855}
856
857
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200858# Distro-agnostic function to tell if a package is installed
859# is_package_installed package [package ...]
860function is_package_installed() {
861 if [[ -z "$@" ]]; then
862 return 1
863 fi
864
865 if [[ -z "$os_PACKAGE" ]]; then
866 GetOSVersion
867 fi
Vincent Untzc18b9652012-12-04 12:36:34 +0100868
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200869 if [[ "$os_PACKAGE" = "deb" ]]; then
870 dpkg -l "$@" > /dev/null
Vincent Untz00011c02012-12-06 09:56:32 +0100871 elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200872 rpm --quiet -q "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100873 else
874 exit_distro_not_supported "finding if a package is installed"
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200875 fi
876}
877
878
Dean Troyer489bd2a2012-03-02 10:44:29 -0600879# Test if the named environment variable is set and not zero length
880# is_set env-var
881function is_set() {
882 local var=\$"$1"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100883 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
Dean Troyer489bd2a2012-03-02 10:44:29 -0600884}
885
886
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500887# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +0000888# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
889# ``TRACK_DEPENDS``, ``*_proxy`
Dean Troyer7f9aa712012-01-31 12:11:56 -0600890# pip_install package [package ...]
891function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600892 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500893 if [[ -z "$os_PACKAGE" ]]; then
894 GetOSVersion
895 fi
Dean Troyercc6b4432013-04-08 15:38:03 -0500896 if [[ $TRACK_DEPENDS = True ]]; then
Monty Taylor47f02062012-07-26 11:09:24 -0500897 source $DEST/.venv/bin/activate
898 CMD_PIP=$DEST/.venv/bin/pip
899 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500900 else
Monty Taylor47f02062012-07-26 11:09:24 -0500901 SUDO_PIP="sudo"
Vincent Untz8ec27222012-11-29 09:25:31 +0100902 CMD_PIP=$(get_pip_command)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500903 fi
Ian Wienandd67dd872013-04-11 11:14:36 +1000904
905 if [[ is_fedora && $DISTRO =~ (rhel6) ]]; then
906 # RHEL6 pip by default doesn't have this (was introduced
907 # around 0.8.1 or so)
908 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
909 else
910 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-True}
911 fi
Maru Newby3a87edd2012-10-25 23:01:06 +0000912 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
913 PIP_MIRROR_OPT="--use-mirrors"
914 fi
Ian Wienandd67dd872013-04-11 11:14:36 +1000915
Ian Wienand31dcd3e2013-07-16 13:36:34 +1000916 # pip < 1.4 has a bug where it will use an already existing build
917 # directory unconditionally. Say an earlier component installs
918 # foo v1.1; pip will have built foo's source in
919 # /tmp/$USER-pip-build. Even if a later component specifies foo <
920 # 1.1, the existing extracted build will be used and cause
921 # confusing errors. By creating unique build directories we avoid
922 # this problem. See
923 # https://github.com/pypa/pip/issues/709
924 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
925
Monty Taylor47f02062012-07-26 11:09:24 -0500926 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600927 HTTP_PROXY=$http_proxy \
928 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900929 NO_PROXY=$no_proxy \
Ian Wienand31dcd3e2013-07-16 13:36:34 +1000930 $CMD_PIP install --build=${pip_build_tmp} \
931 $PIP_MIRROR_OPT $@ \
932 && $SUDO_PIP rm -rf ${pip_build_tmp}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500933}
934
935
Ian Wienand31dcd3e2013-07-16 13:36:34 +1000936# Cleanup anything from /tmp on unstack
937# clean_tmp
938function cleanup_tmp {
939 local tmp_dir=${TMPDIR:-/tmp}
940
941 # see comments in pip_install
942 sudo rm -rf ${tmp_dir}/pip-build.*
943}
944
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500945# Service wrapper to restart services
946# restart_service service-name
947function restart_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100948 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600949 sudo /usr/sbin/service $1 restart
950 else
951 sudo /sbin/service $1 restart
952 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500953}
954
955
Dean Troyer681f3fd2013-02-27 19:00:39 -0600956# _run_process() is designed to be backgrounded by run_process() to simulate a
957# fork. It includes the dirty work of closing extra filehandles and preparing log
958# files to produce the same logs as screen_it(). The log filename is derived
959# from the service name and global-and-now-misnamed SCREEN_LOGDIR
960# _run_process service "command-line"
961function _run_process() {
962 local service=$1
963 local command="$2"
964
965 # Undo logging redirections and close the extra descriptors
966 exec 1>&3
967 exec 2>&3
968 exec 3>&-
969 exec 6>&-
970
971 if [[ -n ${SCREEN_LOGDIR} ]]; then
972 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
973 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
974
975 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
976 export PYTHONUNBUFFERED=1
977 fi
978
979 exec /bin/bash -c "$command"
980 die "$service exec failure: $command"
981}
982
983
984# run_process() launches a child process that closes all file descriptors and
985# then exec's the passed in command. This is meant to duplicate the semantics
986# of screen_it() without screen. PIDs are written to
987# $SERVICE_DIR/$SCREEN_NAME/$service.pid
988# run_process service "command-line"
989function run_process() {
990 local service=$1
991 local command="$2"
992
993 # Spawn the child process
994 _run_process "$service" "$command" &
995 echo $!
996}
997
998
Dean Troyer15733352012-09-06 11:51:30 -0500999# Helper to launch a service in a named screen
1000# screen_it service "command-line"
1001function screen_it {
Dean Troyer15733352012-09-06 11:51:30 -05001002 SCREEN_NAME=${SCREEN_NAME:-stack}
jiajun xua9414242012-12-06 16:30:57 +08001003 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer681f3fd2013-02-27 19:00:39 -06001004 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
jiajun xua9414242012-12-06 16:30:57 +08001005
Dean Troyer15733352012-09-06 11:51:30 -05001006 if is_service_enabled $1; then
1007 # Append the service to the screen rc file
1008 screen_rc "$1" "$2"
1009
Dean Troyer681f3fd2013-02-27 19:00:39 -06001010 if [[ "$USE_SCREEN" = "True" ]]; then
1011 screen -S $SCREEN_NAME -X screen -t $1
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +00001012
Dean Troyer681f3fd2013-02-27 19:00:39 -06001013 if [[ -n ${SCREEN_LOGDIR} ]]; then
1014 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
1015 screen -S $SCREEN_NAME -p $1 -X log on
1016 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1017 fi
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +00001018
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001019 # sleep to allow bash to be ready to be send the command - we are
1020 # creating a new window in screen and then sends characters, so if
1021 # bash isn't running by the time we send the command, nothing happens
1022 sleep 1.5
Dean Troyer15733352012-09-06 11:51:30 -05001023
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001024 NL=`echo -ne '\015'`
1025 screen -S $SCREEN_NAME -p $1 -X stuff "$2 || touch \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
1026 else
Dean Troyer681f3fd2013-02-27 19:00:39 -06001027 # Spawn directly without screen
1028 run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyer15733352012-09-06 11:51:30 -05001029 fi
Dean Troyer15733352012-09-06 11:51:30 -05001030 fi
1031}
1032
1033
1034# Screen rc file builder
1035# screen_rc service "command-line"
1036function screen_rc {
1037 SCREEN_NAME=${SCREEN_NAME:-stack}
1038 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1039 if [[ ! -e $SCREENRC ]]; then
1040 # Name the screen session
1041 echo "sessionname $SCREEN_NAME" > $SCREENRC
1042 # Set a reasonable statusbar
1043 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
Steven Dake30396572013-06-30 16:11:54 -07001044 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1045 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
Dean Troyer15733352012-09-06 11:51:30 -05001046 echo "screen -t shell bash" >> $SCREENRC
1047 fi
1048 # If this service doesn't already exist in the screenrc file
1049 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1050 NL=`echo -ne '\015'`
1051 echo "screen -t $1 bash" >> $SCREENRC
1052 echo "stuff \"$2$NL\"" >> $SCREENRC
1053 fi
1054}
1055
Dean Troyer1a6d4492013-06-03 16:47:36 -05001056
jiajun xua9414242012-12-06 16:30:57 +08001057# Helper to remove the *.failure files under $SERVICE_DIR/$SCREEN_NAME
1058# This is used for service_check when all the screen_it are called finished
1059# init_service_check
1060function init_service_check() {
1061 SCREEN_NAME=${SCREEN_NAME:-stack}
1062 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1063
1064 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1065 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1066 fi
1067
1068 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1069}
1070
Dean Troyer1a6d4492013-06-03 16:47:36 -05001071
jiajun xua9414242012-12-06 16:30:57 +08001072# Helper to get the status of each running service
1073# service_check
1074function service_check() {
1075 local service
1076 local failures
1077 SCREEN_NAME=${SCREEN_NAME:-stack}
1078 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1079
1080
1081 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1082 echo "No service status directory found"
1083 return
1084 fi
1085
1086 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
1087 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null`
1088
1089 for service in $failures; do
1090 service=`basename $service`
1091 service=${service::-8}
1092 echo "Error: Service $service is not running"
1093 done
1094
1095 if [ -n "$failures" ]; then
1096 echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
1097 fi
1098}
Dean Troyer15733352012-09-06 11:51:30 -05001099
Dean Troyer1a6d4492013-06-03 16:47:36 -05001100
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001101# ``pip install`` the dependencies of the package before ``setup.py develop``
1102# so pip and not distutils processes the dependency chain
1103# Uses globals ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001104# setup_develop directory
1105function setup_develop() {
Dean Troyercc6b4432013-04-08 15:38:03 -05001106 if [[ $TRACK_DEPENDS = True ]]; then
Monty Taylor47f02062012-07-26 11:09:24 -05001107 SUDO_CMD="env"
1108 else
1109 SUDO_CMD="sudo"
1110 fi
Monty Taylore7e51ac2013-07-04 14:10:46 -04001111 for reqs_file in $1/requirements.txt $1/tools/pip-requires ; do
1112 if [ -f $reqs_file ] ; then
1113 pip_install -r $reqs_file
1114 fi
1115 done
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001116 (cd $1; \
1117 python setup.py egg_info; \
Monty Taylor47f02062012-07-26 11:09:24 -05001118 $SUDO_CMD \
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001119 HTTP_PROXY=$http_proxy \
1120 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001121 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001122 python setup.py develop \
1123 )
1124}
1125
1126
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001127# Service wrapper to start services
1128# start_service service-name
1129function start_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001130 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001131 sudo /usr/sbin/service $1 start
1132 else
1133 sudo /sbin/service $1 start
1134 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001135}
1136
1137
1138# Service wrapper to stop services
1139# stop_service service-name
1140function stop_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001141 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001142 sudo /usr/sbin/service $1 stop
1143 else
1144 sudo /sbin/service $1 stop
1145 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -06001146}
1147
1148
1149# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001150# Accepts as False: 0 no false False FALSE
1151# Accepts as True: 1 yes true True TRUE
1152# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -06001153function trueorfalse() {
1154 local default=$1
1155 local testval=$2
1156
1157 [[ -z "$testval" ]] && { echo "$default"; return; }
1158 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
1159 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
1160 echo "$default"
1161}
Dean Troyer27e32692012-03-16 16:16:56 -05001162
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001163
Dean Troyerca0e3d02012-04-13 15:58:37 -05001164# Retrieve an image from a URL and upload into Glance
1165# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001166# ``FILES`` must be set to the cache dir
1167# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -05001168# upload_image image-url glance-token
1169function upload_image() {
1170 local image_url=$1
1171 local token=$2
1172
1173 # Create a directory for the downloaded image tarballs.
1174 mkdir -p $FILES/images
1175
1176 # Downloads the image (uec ami+aki style), then extracts it.
1177 IMAGE_FNAME=`basename "$image_url"`
1178 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
1179 wget -c $image_url -O $FILES/$IMAGE_FNAME
1180 if [[ $? -ne 0 ]]; then
1181 echo "Not found: $image_url"
1182 return
1183 fi
1184 fi
1185
1186 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
1187 if [[ "$image_url" =~ 'openvz' ]]; then
1188 IMAGE="$FILES/${IMAGE_FNAME}"
1189 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
1190 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format ami --disk-format ami < "${IMAGE}"
1191 return
1192 fi
1193
Davanum Srinivas316ed6c2013-02-06 15:29:49 -05001194 # XenServer-ovf-format images are provided as .vhd.tgz as well
1195 # and should not be decompressed prior to loading
1196 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
1197 IMAGE="$FILES/${IMAGE_FNAME}"
1198 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
1199 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format=ovf --disk-format=vhd < "${IMAGE}"
1200 return
1201 fi
1202
Dean Troyerca0e3d02012-04-13 15:58:37 -05001203 KERNEL=""
1204 RAMDISK=""
1205 DISK_FORMAT=""
1206 CONTAINER_FORMAT=""
1207 UNPACK=""
1208 case "$IMAGE_FNAME" in
1209 *.tar.gz|*.tgz)
1210 # Extract ami and aki files
1211 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
1212 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
1213 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
1214 xdir="$FILES/images/$IMAGE_NAME"
1215 rm -Rf "$xdir";
1216 mkdir "$xdir"
1217 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
1218 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
1219 [ -f "$f" ] && echo "$f" && break; done; true)
1220 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
1221 [ -f "$f" ] && echo "$f" && break; done; true)
1222 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
1223 [ -f "$f" ] && echo "$f" && break; done; true)
1224 if [[ -z "$IMAGE_NAME" ]]; then
1225 IMAGE_NAME=$(basename "$IMAGE" ".img")
1226 fi
1227 ;;
1228 *.img)
1229 IMAGE="$FILES/$IMAGE_FNAME";
1230 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -05001231 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
1232 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
1233 DISK_FORMAT=$format
1234 else
1235 DISK_FORMAT=raw
1236 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -05001237 CONTAINER_FORMAT=bare
1238 ;;
1239 *.img.gz)
1240 IMAGE="$FILES/${IMAGE_FNAME}"
1241 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
1242 DISK_FORMAT=raw
1243 CONTAINER_FORMAT=bare
1244 UNPACK=zcat
1245 ;;
1246 *.qcow2)
1247 IMAGE="$FILES/${IMAGE_FNAME}"
1248 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
1249 DISK_FORMAT=qcow2
1250 CONTAINER_FORMAT=bare
1251 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +01001252 *.iso)
1253 IMAGE="$FILES/${IMAGE_FNAME}"
1254 IMAGE_NAME=$(basename "$IMAGE" ".iso")
1255 DISK_FORMAT=iso
1256 CONTAINER_FORMAT=bare
1257 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -05001258 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
1259 esac
1260
1261 if [ "$CONTAINER_FORMAT" = "bare" ]; then
1262 if [ "$UNPACK" = "zcat" ]; then
1263 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --public --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < <(zcat --force "${IMAGE}")
1264 else
1265 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --public --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < "${IMAGE}"
1266 fi
1267 else
1268 # Use glance client to add the kernel the root filesystem.
1269 # We parse the results of the first upload to get the glance ID of the
1270 # kernel for use when uploading the root filesystem.
1271 KERNEL_ID=""; RAMDISK_ID="";
1272 if [ -n "$KERNEL" ]; then
1273 KERNEL_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-kernel" --public --container-format aki --disk-format aki < "$KERNEL" | grep ' id ' | get_field 2)
1274 fi
1275 if [ -n "$RAMDISK" ]; then
1276 RAMDISK_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-ramdisk" --public --container-format ari --disk-format ari < "$RAMDISK" | grep ' id ' | get_field 2)
1277 fi
1278 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "${IMAGE_NAME%.img}" --public --container-format ami --disk-format ami ${KERNEL_ID:+--property kernel_id=$KERNEL_ID} ${RAMDISK_ID:+--property ramdisk_id=$RAMDISK_ID} < "${IMAGE}"
1279 fi
1280}
1281
Dean Troyer1a6d4492013-06-03 16:47:36 -05001282
Dean Troyerc1b486a2012-11-05 14:26:09 -06001283# Set the database backend to use
1284# When called from stackrc/localrc DATABASE_BACKENDS has not been
1285# initialized yet, just save the configuration selection and call back later
1286# to validate it.
1287# $1 The name of the database backend to use (mysql, postgresql, ...)
1288function use_database {
1289 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -06001290 # No backends registered means this is likely called from ``localrc``
1291 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -06001292 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +00001293 DEPRECATED_TEXT="$DEPRECATED_TEXT\nThe database backend needs to be properly set in ENABLED_SERVICES; use_database is deprecated localrc\n"
Attila Fazekas251d3b52012-12-16 15:05:44 +01001294 else
Dean Troyerafc29fe2013-02-07 15:56:24 -06001295 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +01001296 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -06001297 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -06001298}
1299
Dean Troyer1a6d4492013-06-03 16:47:36 -05001300
Terry Wilson428af5a2012-11-01 16:12:39 -04001301# Toggle enable/disable_service for services that must run exclusive of each other
1302# $1 The name of a variable containing a space-separated list of services
1303# $2 The name of a variable in which to store the enabled service's name
1304# $3 The name of the service to enable
1305function use_exclusive_service {
1306 local options=${!1}
1307 local selection=$3
1308 out=$2
1309 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1310 for opt in $options;do
1311 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1312 done
1313 eval "$out=$selection"
1314 return 0
1315}
Dean Troyerca0e3d02012-04-13 15:58:37 -05001316
Dean Troyer1a6d4492013-06-03 16:47:36 -05001317
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001318# Wait for an HTTP server to start answering requests
1319# wait_for_service timeout url
1320function wait_for_service() {
1321 local timeout=$1
1322 local url=$2
1323 timeout $timeout sh -c "while ! http_proxy= https_proxy= curl -s $url >/dev/null; do sleep 1; done"
1324}
1325
Dean Troyer1a6d4492013-06-03 16:47:36 -05001326
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001327# Wrapper for ``yum`` to set proxy environment variables
1328# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001329# yum_install package [package ...]
1330function yum_install() {
1331 [[ "$OFFLINE" = "True" ]] && return
1332 local sudo="sudo"
1333 [[ "$(id -u)" = "0" ]] && sudo="env"
1334 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001335 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001336 yum install -y "$@"
1337}
1338
Dean Troyer1a6d4492013-06-03 16:47:36 -05001339
1340# zypper wrapper to set arguments correctly
1341# zypper_install package [package ...]
1342function zypper_install() {
1343 [[ "$OFFLINE" = "True" ]] && return
1344 local sudo="sudo"
1345 [[ "$(id -u)" = "0" ]] && sudo="env"
1346 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1347 zypper --non-interactive install --auto-agree-with-licenses "$@"
1348}
1349
1350
Nachi Uenofda946e2012-10-24 17:26:02 -07001351# ping check
1352# Uses globals ``ENABLED_SERVICES``
Dean Troyer1a6d4492013-06-03 16:47:36 -05001353# ping_check from-net ip boot-timeout expected
Nachi Uenofda946e2012-10-24 17:26:02 -07001354function ping_check() {
Mark McClainb05c8762013-07-06 23:29:39 -04001355 if is_service_enabled neutron; then
1356 _ping_check_neutron "$1" $2 $3 $4
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001357 return
1358 fi
1359 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -07001360}
1361
1362# ping check for nova
1363# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
1364function _ping_check_novanet() {
1365 local from_net=$1
1366 local ip=$2
1367 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001368 local expected=${4:-"True"}
1369 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001370 MULTI_HOST=`trueorfalse False $MULTI_HOST`
1371 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
1372 sleep $boot_timeout
1373 return
1374 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001375 if [[ "$expected" = "True" ]]; then
1376 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
1377 else
1378 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
1379 fi
1380 if ! timeout $boot_timeout sh -c "$check_command"; then
1381 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -08001382 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001383 else
Nachi Ueno07115eb2013-02-26 12:38:18 -08001384 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001385 fi
Nachi Uenofda946e2012-10-24 17:26:02 -07001386 exit 1
1387 fi
1388}
1389
Dean Troyer1a6d4492013-06-03 16:47:36 -05001390
Nachi Uenofda946e2012-10-24 17:26:02 -07001391# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001392
Dean Troyer1a6d4492013-06-03 16:47:36 -05001393# ssh_check net-name key-file floating-ip default-user active-timeout
Nachi Uenofda946e2012-10-24 17:26:02 -07001394function ssh_check() {
Mark McClainb05c8762013-07-06 23:29:39 -04001395 if is_service_enabled neutron; then
1396 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001397 return
1398 fi
1399 _ssh_check_novanet "$1" $2 $3 $4 $5
1400}
1401
1402function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -07001403 local NET_NAME=$1
1404 local KEY_FILE=$2
1405 local FLOATING_IP=$3
1406 local DEFAULT_INSTANCE_USER=$4
1407 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -06001408 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -05001409 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP echo success; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -08001410 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -07001411 fi
1412}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001413
Vincent Untz856a11e2012-11-21 16:04:12 +01001414
Vincent Untz856a11e2012-11-21 16:04:12 +01001415# Add a user to a group.
1416# add_user_to_group user group
1417function add_user_to_group() {
1418 local user=$1
1419 local group=$2
1420
1421 if [[ -z "$os_VENDOR" ]]; then
1422 GetOSVersion
1423 fi
1424
1425 # SLE11 and openSUSE 12.2 don't have the usual usermod
1426 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1427 sudo usermod -a -G "$group" "$user"
1428 else
1429 sudo usermod -A "$group" "$user"
1430 fi
1431}
1432
1433
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001434# Get the path to the direcotry where python executables are installed.
1435# get_python_exec_prefix
1436function get_python_exec_prefix() {
Martin Vidner4f9b33d2013-06-27 13:11:22 +00001437 if is_fedora || is_suse; then
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001438 echo "/usr/bin"
1439 else
1440 echo "/usr/local/bin"
1441 fi
1442}
1443
Dean Troyer1a6d4492013-06-03 16:47:36 -05001444
Vincent Untz856a11e2012-11-21 16:04:12 +01001445# Get the location of the $module-rootwrap executables, where module is cinder
1446# or nova.
1447# get_rootwrap_location module
1448function get_rootwrap_location() {
1449 local module=$1
1450
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001451 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +01001452}
1453
Dean Troyer1a6d4492013-06-03 16:47:36 -05001454
Vincent Untz8ec27222012-11-29 09:25:31 +01001455# Get the path to the pip command.
1456# get_pip_command
1457function get_pip_command() {
Vincent Untz00011c02012-12-06 09:56:32 +01001458 if is_fedora; then
Nikhil Manchanda35138ed2013-01-03 17:49:58 -08001459 which pip-python
Vincent Untz00011c02012-12-06 09:56:32 +01001460 else
Nikhil Manchanda35138ed2013-01-03 17:49:58 -08001461 which pip
Vincent Untz8ec27222012-11-29 09:25:31 +01001462 fi
Ian Wienand535a8142013-05-15 09:25:27 +10001463
1464 if [ $? -ne 0 ]; then
1465 die $LINENO "Unable to find pip; cannot continue"
1466 fi
Vincent Untz8ec27222012-11-29 09:25:31 +01001467}
Vincent Untz856a11e2012-11-21 16:04:12 +01001468
Dean Troyer1a6d4492013-06-03 16:47:36 -05001469
Ian Wienand0488edd2013-04-11 12:04:36 +10001470# Path permissions sanity check
1471# check_path_perm_sanity path
1472function check_path_perm_sanity() {
1473 # Ensure no element of the path has 0700 permissions, which is very
1474 # likely to cause issues for daemons. Inspired by default 0700
1475 # homedir permissions on RHEL and common practice of making DEST in
1476 # the stack user's homedir.
1477
1478 local real_path=$(readlink -f $1)
1479 local rebuilt_path=""
1480 for i in $(echo ${real_path} | tr "/" " "); do
1481 rebuilt_path=$rebuilt_path"/"$i
1482
1483 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
1484 echo "*** DEST path element"
1485 echo "*** ${rebuilt_path}"
1486 echo "*** appears to have 0700 permissions."
1487 echo "*** This is very likely to cause fatal issues for devstack daemons."
1488
1489 if [[ -n "$SKIP_PATH_SANITY" ]]; then
1490 return
1491 else
1492 echo "*** Set SKIP_PATH_SANITY to skip this check"
1493 die $LINENO "Invalid path permissions"
1494 fi
1495 fi
1496 done
1497}
1498
Dean Troyer1a6d4492013-06-03 16:47:36 -05001499
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001500# This function recursively compares versions, and is not meant to be
1501# called by anything other than vercmp_numbers below. This function does
1502# not work with alphabetic versions.
1503#
1504# _vercmp_r sep ver1 ver2
1505function _vercmp_r {
1506 typeset sep
1507 typeset -a ver1=() ver2=()
1508 sep=$1; shift
1509 ver1=("${@:1:sep}")
1510 ver2=("${@:sep+1}")
1511
1512 if ((ver1 > ver2)); then
1513 echo 1; return 0
1514 elif ((ver2 > ver1)); then
1515 echo -1; return 0
1516 fi
1517
1518 if ((sep <= 1)); then
1519 echo 0; return 0
1520 fi
1521
1522 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
1523}
1524
1525
1526# This function compares two versions and is meant to be called by
1527# external callers. Please note the function assumes non-alphabetic
1528# versions. For example, this will work:
1529#
1530# vercmp_numbers 1.10 1.4
1531#
1532# The above will return "1", as 1.10 is greater than 1.4.
1533#
1534# vercmp_numbers 5.2 6.4
1535#
1536# The above will return "-1", as 5.2 is less than 6.4.
1537#
1538# vercmp_numbers 4.0 4.0
1539#
1540# The above will return "0", as the versions are equal.
1541#
1542# vercmp_numbers ver1 ver2
1543vercmp_numbers() {
1544 typeset v1=$1 v2=$2 sep
1545 typeset -a ver1 ver2
1546
1547 IFS=. read -ra ver1 <<< "$v1"
1548 IFS=. read -ra ver2 <<< "$v2"
1549
1550 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
1551}
1552
1553
Dean Troyer27e32692012-03-16 16:16:56 -05001554# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +00001555$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001556
1557
1558# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -04001559# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -04001560# End: