blob: eb83dfb2d672b728bf42d44d1adbd83033481f5b [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 Troyerd4f69b22013-07-24 12:24:43 -050021# Convert CIDR notation to a IPv4 netmask
22# cidr2netmask cidr-bits
23function cidr2netmask() {
24 local maskpat="255 255 255 255"
25 local maskdgt="254 252 248 240 224 192 128"
26 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
27 echo ${1-0}.${2-0}.${3-0}.${4-0}
28}
29
30
31# Return the network portion of the given IP address using netmask
32# netmask is in the traditional dotted-quad format
33# maskip ip-address netmask
34function maskip() {
35 local ip=$1
36 local mask=$2
37 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
38 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
39 echo $subnet
40}
41
42
43# Exit 0 if address is in network or 1 if address is not in network
44# ip-range is in CIDR notation: 1.2.3.4/20
Dean Troyer4a43b7b2012-08-28 17:43:40 -050045# address_in_net ip-address ip-range
Vishvananda Ishayac9ad14b2012-07-03 20:29:01 +000046function address_in_net() {
Dean Troyerd4f69b22013-07-24 12:24:43 -050047 local ip=$1
48 local range=$2
49 local masklen=${range#*/}
50 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
51 local subnet=$(maskip $ip $(cidr2netmask $masklen))
52 [[ $network == $subnet ]]
Vishvananda Ishayac9ad14b2012-07-03 20:29:01 +000053}
54
55
Dean Troyer4a43b7b2012-08-28 17:43:40 -050056# Wrapper for ``apt-get`` to set cache and proxy environment variables
57# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -050058# apt_get operation package [package ...]
Dean Troyer7f9aa712012-01-31 12:11:56 -060059function apt_get() {
Dean Troyerd0b21e22012-03-07 14:52:25 -060060 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer7f9aa712012-01-31 12:11:56 -060061 local sudo="sudo"
62 [[ "$(id -u)" = "0" ]] && sudo="env"
63 $sudo DEBIAN_FRONTEND=noninteractive \
64 http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +090065 no_proxy=$no_proxy \
Dean Troyer7f9aa712012-01-31 12:11:56 -060066 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
67}
68
69
70# Gracefully cp only if source file/dir exists
71# cp_it source destination
72function cp_it {
73 if [ -e $1 ] || [ -d $1 ]; then
74 cp -pRL $1 $2
75 fi
76}
77
78
Dean Troyerac93efb2013-03-13 14:30:54 -050079# Prints line number and "message" then exits
80# die $LINENO "message"
Dean Troyer27e32692012-03-16 16:16:56 -050081function die() {
Dean Troyer489bd2a2012-03-02 10:44:29 -060082 local exitcode=$?
Dean Troyer896eb662013-04-05 15:02:01 -050083 set +o xtrace
84 local line=$1; shift
Nachi Ueno07115eb2013-02-26 12:38:18 -080085 if [ $exitcode == 0 ]; then
86 exitcode=1
87 fi
Dean Troyer896eb662013-04-05 15:02:01 -050088 err $line "$*"
Dean Troyer27e32692012-03-16 16:16:56 -050089 exit $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -060090}
91
92
93# Checks an environment variable is not set or has length 0 OR if the
94# exit code is non-zero and prints "message" and exits
95# NOTE: env-var is the variable name without a '$'
Dean Troyerac93efb2013-03-13 14:30:54 -050096# die_if_not_set $LINENO env-var "message"
Dean Troyer489bd2a2012-03-02 10:44:29 -060097function die_if_not_set() {
Dean Troyer896eb662013-04-05 15:02:01 -050098 local exitcode=$?
99 FXTRACE=$(set +o | grep xtrace)
100 set +o xtrace
101 local line=$1; shift
102 local evar=$1; shift
103 if ! is_set $evar || [ $exitcode != 0 ]; then
104 die $line "$*"
105 fi
106 $FXTRACE
107}
108
109
110# Prints line number and "message" in error format
111# err $LINENO "message"
112function err() {
113 local exitcode=$?
114 errXTRACE=$(set +o | grep xtrace)
115 set +o xtrace
116 local msg="[ERROR] $0:$1 $2"
117 echo $msg 1>&2;
118 if [[ -n ${SCREEN_LOGDIR} ]]; then
119 echo $msg >> "${SCREEN_LOGDIR}/error.log"
120 fi
121 $errXTRACE
122 return $exitcode
123}
124
125
126# Checks an environment variable is not set or has length 0 OR if the
127# exit code is non-zero and prints "message"
128# NOTE: env-var is the variable name without a '$'
129# err_if_not_set $LINENO env-var "message"
130function err_if_not_set() {
131 local exitcode=$?
132 errinsXTRACE=$(set +o | grep xtrace)
133 set +o xtrace
134 local line=$1; shift
135 local evar=$1; shift
136 if ! is_set $evar || [ $exitcode != 0 ]; then
137 err $line "$*"
138 fi
139 $errinsXTRACE
140 return $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -0600141}
142
143
Dean Troyer48352ee2012-12-12 12:50:38 -0600144# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
145# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
146# ``localrc`` or on the command line if necessary::
147#
148# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
149#
150# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
151
152function export_proxy_variables() {
153 if [[ -n "$http_proxy" ]]; then
154 export http_proxy=$http_proxy
155 fi
156 if [[ -n "$https_proxy" ]]; then
157 export https_proxy=$https_proxy
158 fi
159 if [[ -n "$no_proxy" ]]; then
160 export no_proxy=$no_proxy
161 fi
162}
163
164
Dean Troyer489bd2a2012-03-02 10:44:29 -0600165# Grab a numbered field from python prettytable output
166# Fields are numbered starting with 1
167# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
168# get_field field-number
169function get_field() {
170 while read data; do
171 if [ "$1" -lt 0 ]; then
172 field="(\$(NF$1))"
173 else
174 field="\$$(($1 + 1))"
175 fi
176 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
177 done
178}
179
180
Dean Troyerc892bde2013-03-13 14:06:13 -0500181# Get the default value for HOST_IP
182# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
183function get_default_host_ip() {
184 local fixed_range=$1
185 local floating_range=$2
186 local host_ip_iface=$3
187 local host_ip=$4
188
189 # Find the interface used for the default route
190 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
191 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
192 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
193 host_ip=""
194 host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}'`
195 for IP in $host_ips; do
196 # Attempt to filter out IP addresses that are part of the fixed and
197 # floating range. Note that this method only works if the ``netaddr``
198 # python library is installed. If it is not installed, an error
199 # will be printed and the first IP from the interface will be used.
200 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
201 # address.
202 if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then
203 host_ip=$IP
204 break;
205 fi
206 done
207 fi
208 echo $host_ip
209}
210
211
Isaku Yamahata8c438092013-02-12 22:30:56 +0900212function _get_package_dir() {
213 local pkg_dir
214 if is_ubuntu; then
215 pkg_dir=$FILES/apts
216 elif is_fedora; then
217 pkg_dir=$FILES/rpms
218 elif is_suse; then
219 pkg_dir=$FILES/rpms-suse
220 else
221 exit_distro_not_supported "list of packages"
222 fi
223 echo "$pkg_dir"
224}
225
Dean Troyer1a6d4492013-06-03 16:47:36 -0500226
Dean Troyer7e270512012-06-14 15:23:24 -0500227# get_packages() collects a list of package names of any type from the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500228# prerequisite files in ``files/{apts|rpms}``. The list is intended
229# to be passed to a package installer such as apt or yum.
Dean Troyer7e270512012-06-14 15:23:24 -0500230#
Isaku Yamahata8c438092013-02-12 22:30:56 +0900231# Only packages required for the services in 1st argument will be
Dean Troyer7e270512012-06-14 15:23:24 -0500232# included. Two bits of metadata are recognized in the prerequisite files:
233# - ``# NOPRIME`` defers installation to be performed later in stack.sh
234# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
235# of the package to the distros listed. The distro names are case insensitive.
Dean Troyer7e270512012-06-14 15:23:24 -0500236function get_packages() {
Isaku Yamahata8c438092013-02-12 22:30:56 +0900237 local services=$1
238 local package_dir=$(_get_package_dir)
Dean Troyer7e270512012-06-14 15:23:24 -0500239 local file_to_parse
240 local service
241
242 if [[ -z "$package_dir" ]]; then
243 echo "No package directory supplied"
244 return 1
245 fi
246 if [[ -z "$DISTRO" ]]; then
Vincent Untz855c5872012-10-04 13:36:46 +0200247 GetDistro
Dean Troyer7e270512012-06-14 15:23:24 -0500248 fi
Isaku Yamahata8c438092013-02-12 22:30:56 +0900249 for service in general ${services//,/ }; do
Dean Troyer7e270512012-06-14 15:23:24 -0500250 # Allow individual services to specify dependencies
251 if [[ -e ${package_dir}/${service} ]]; then
252 file_to_parse="${file_to_parse} $service"
253 fi
254 # NOTE(sdague) n-api needs glance for now because that's where
255 # glance client is
256 if [[ $service == n-api ]]; then
257 if [[ ! $file_to_parse =~ nova ]]; then
258 file_to_parse="${file_to_parse} nova"
259 fi
260 if [[ ! $file_to_parse =~ glance ]]; then
261 file_to_parse="${file_to_parse} glance"
262 fi
263 elif [[ $service == c-* ]]; then
264 if [[ ! $file_to_parse =~ cinder ]]; then
265 file_to_parse="${file_to_parse} cinder"
266 fi
John H. Tran93361642012-07-26 11:22:05 -0700267 elif [[ $service == ceilometer-* ]]; then
268 if [[ ! $file_to_parse =~ ceilometer ]]; then
269 file_to_parse="${file_to_parse} ceilometer"
270 fi
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100271 elif [[ $service == s-* ]]; then
272 if [[ ! $file_to_parse =~ swift ]]; then
273 file_to_parse="${file_to_parse} swift"
274 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500275 elif [[ $service == n-* ]]; then
276 if [[ ! $file_to_parse =~ nova ]]; then
277 file_to_parse="${file_to_parse} nova"
278 fi
279 elif [[ $service == g-* ]]; then
280 if [[ ! $file_to_parse =~ glance ]]; then
281 file_to_parse="${file_to_parse} glance"
282 fi
283 elif [[ $service == key* ]]; then
284 if [[ ! $file_to_parse =~ keystone ]]; then
285 file_to_parse="${file_to_parse} keystone"
286 fi
Robert Collins0a9954f2012-11-20 11:34:25 +1300287 elif [[ $service == q-* ]]; then
Mark McClainb05c8762013-07-06 23:29:39 -0400288 if [[ ! $file_to_parse =~ neutron ]]; then
289 file_to_parse="${file_to_parse} neutron"
Robert Collins0a9954f2012-11-20 11:34:25 +1300290 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500291 fi
292 done
293
294 for file in ${file_to_parse}; do
295 local fname=${package_dir}/${file}
296 local OIFS line package distros distro
297 [[ -e $fname ]] || continue
298
299 OIFS=$IFS
300 IFS=$'\n'
301 for line in $(<${fname}); do
302 if [[ $line =~ "NOPRIME" ]]; then
303 continue
304 fi
305
306 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
307 # We are using BASH regexp matching feature.
308 package=${BASH_REMATCH[1]}
309 distros=${BASH_REMATCH[2]}
310 # In bash ${VAR,,} will lowecase VAR
311 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package
312 continue
313 fi
314
315 echo ${line%#*}
316 done
317 IFS=$OIFS
318 done
319}
320
321
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500322# Determine OS Vendor, Release and Update
323# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
324# Returns results in global variables:
325# os_VENDOR - vendor name
326# os_RELEASE - release
327# os_UPDATE - update
328# os_PACKAGE - package type
329# os_CODENAME - vendor's codename for release
330# GetOSVersion
331GetOSVersion() {
332 # Figure out which vendor we are
333 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
334 # OS/X
335 os_VENDOR=`sw_vers -productName`
336 os_RELEASE=`sw_vers -productVersion`
337 os_UPDATE=${os_RELEASE##*.}
338 os_RELEASE=${os_RELEASE%.*}
339 os_PACKAGE=""
340 if [[ "$os_RELEASE" =~ "10.7" ]]; then
341 os_CODENAME="lion"
342 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
343 os_CODENAME="snow leopard"
344 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
345 os_CODENAME="leopard"
346 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
347 os_CODENAME="tiger"
348 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
349 os_CODENAME="panther"
350 else
351 os_CODENAME=""
352 fi
353 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
354 os_VENDOR=$(lsb_release -i -s)
355 os_RELEASE=$(lsb_release -r -s)
356 os_UPDATE=""
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100357 os_PACKAGE="rpm"
Derek Morton4a8496e2013-04-08 23:46:08 -0500358 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500359 os_PACKAGE="deb"
Vincent Untz856a11e2012-11-21 16:04:12 +0100360 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
361 lsb_release -d -s | grep -q openSUSE
362 if [[ $? -eq 0 ]]; then
363 os_VENDOR="openSUSE"
364 fi
Vincent Untzcd1fe982013-03-12 18:04:29 +0100365 elif [[ $os_VENDOR == "openSUSE project" ]]; then
366 os_VENDOR="openSUSE"
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100367 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
368 os_VENDOR="Red Hat"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500369 fi
370 os_CODENAME=$(lsb_release -c -s)
371 elif [[ -r /etc/redhat-release ]]; then
372 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
373 # CentOS release 5.5 (Final)
374 # CentOS Linux release 6.0 (Final)
375 # Fedora release 16 (Verne)
376 os_CODENAME=""
377 for r in "Red Hat" CentOS Fedora; do
378 os_VENDOR=$r
379 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
380 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
381 os_CODENAME=${ver#*|}
382 os_RELEASE=${ver%|*}
383 os_UPDATE=${os_RELEASE##*.}
384 os_RELEASE=${os_RELEASE%.*}
385 break
386 fi
387 os_VENDOR=""
388 done
389 os_PACKAGE="rpm"
Vincent Untz856a11e2012-11-21 16:04:12 +0100390 elif [[ -r /etc/SuSE-release ]]; then
391 for r in openSUSE "SUSE Linux"; do
392 if [[ "$r" = "SUSE Linux" ]]; then
393 os_VENDOR="SUSE LINUX"
394 else
395 os_VENDOR=$r
396 fi
397
398 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
399 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
400 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
401 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
402 break
403 fi
404 os_VENDOR=""
405 done
406 os_PACKAGE="rpm"
Émilien Macchib2ef8902013-05-04 00:48:20 +0200407 # If lsb_release is not installed, we should be able to detect Debian OS
408 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
409 os_VENDOR="Debian"
410 os_PACKAGE="deb"
411 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
412 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500413 fi
414 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
415}
416
Andrew Laskif900bd72012-09-05 17:23:14 -0400417
Dean Troyera9e0a482012-07-09 14:07:23 -0500418# Translate the OS version values into common nomenclature
419# Sets ``DISTRO`` from the ``os_*`` values
420function GetDistro() {
421 GetOSVersion
Émilien Macchib2ef8902013-05-04 00:48:20 +0200422 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
423 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
Dean Troyera9e0a482012-07-09 14:07:23 -0500424 DISTRO=$os_CODENAME
425 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
426 # For Fedora, just use 'f' and the release
427 DISTRO="f$os_RELEASE"
Vincent Untz856a11e2012-11-21 16:04:12 +0100428 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
429 DISTRO="opensuse-$os_RELEASE"
430 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
431 # For SLE, also use the service pack
432 if [[ -z "$os_UPDATE" ]]; then
433 DISTRO="sle${os_RELEASE}"
434 else
435 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
436 fi
Ian Wienandd857f4b2013-03-20 14:51:06 +1100437 elif [[ "$os_VENDOR" =~ (Red Hat) || "$os_VENDOR" =~ (CentOS) ]]; then
438 # Drop the . release as we assume it's compatible
439 DISTRO="rhel${os_RELEASE::1}"
Dean Troyera9e0a482012-07-09 14:07:23 -0500440 else
441 # Catch-all for now is Vendor + Release + Update
442 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
443 fi
444 export DISTRO
445}
446
447
Vincent Untz00011c02012-12-06 09:56:32 +0100448# Determine if current distribution is a Fedora-based distribution
Dean Troyer1a6d4492013-06-03 16:47:36 -0500449# (Fedora, RHEL, CentOS, etc).
Vincent Untz00011c02012-12-06 09:56:32 +0100450# is_fedora
451function is_fedora {
452 if [[ -z "$os_VENDOR" ]]; then
453 GetOSVersion
454 fi
455
456 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ]
457}
458
Dean Troyer1a6d4492013-06-03 16:47:36 -0500459
Vincent Untz856a11e2012-11-21 16:04:12 +0100460# Determine if current distribution is a SUSE-based distribution
461# (openSUSE, SLE).
462# is_suse
463function is_suse {
464 if [[ -z "$os_VENDOR" ]]; then
465 GetOSVersion
466 fi
467
Steve Baker1a7bbd22012-12-03 17:04:02 +1300468 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
Vincent Untz856a11e2012-11-21 16:04:12 +0100469}
470
471
Dean Troyer1a6d4492013-06-03 16:47:36 -0500472# Determine if current distribution is an Ubuntu-based distribution
473# It will also detect non-Ubuntu but Debian-based distros
474# is_ubuntu
475function is_ubuntu {
476 if [[ -z "$os_PACKAGE" ]]; then
477 GetOSVersion
478 fi
479 [ "$os_PACKAGE" = "deb" ]
480}
481
482
Vincent Untz00011c02012-12-06 09:56:32 +0100483# Exit after outputting a message about the distribution not being supported.
484# exit_distro_not_supported [optional-string-telling-what-is-missing]
485function exit_distro_not_supported {
486 if [[ -z "$DISTRO" ]]; then
487 GetDistro
488 fi
489
490 if [ $# -gt 0 ]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800491 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
Vincent Untz00011c02012-12-06 09:56:32 +0100492 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800493 die $LINENO "Support for $DISTRO is incomplete."
Vincent Untz00011c02012-12-06 09:56:32 +0100494 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100495}
496
Daniel Jonesfa868cb2013-06-18 15:28:01 -0500497# Utility function for checking machine architecture
498# is_arch arch-type
499function is_arch {
500 ARCH_TYPE=$1
501
502 [ "($uname -m)" = "$ARCH_TYPE" ]
503}
Vincent Untz00011c02012-12-06 09:56:32 +0100504
Dean Troyer7f9aa712012-01-31 12:11:56 -0600505# git clone only if directory doesn't exist already. Since ``DEST`` might not
506# be owned by the installation user, we create the directory and change the
507# ownership to the proper user.
508# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700509# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
510# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500511# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600512# git_clone remote dest-dir branch
513function git_clone {
514 [[ "$OFFLINE" = "True" ]] && return
515
516 GIT_REMOTE=$1
517 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300518 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600519
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300520 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600521 # If our branch name is a gerrit style refs/changes/...
522 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700523 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600524 git clone $GIT_REMOTE $GIT_DEST
525 fi
526 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300527 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600528 else
529 # do a full clone only if the directory doesn't exist
530 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700531 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600532 git clone $GIT_REMOTE $GIT_DEST
533 cd $GIT_DEST
534 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300535 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600536 elif [[ "$RECLONE" == "yes" ]]; then
537 # if it does exist then simulate what clone does if asked to RECLONE
538 cd $GIT_DEST
539 # set the url to pull from and fetch
540 git remote set-url origin $GIT_REMOTE
541 git fetch origin
542 # remove the existing ignored files (like pyc) as they cause breakage
543 # (due to the py files having older timestamps than our pyc, so python
544 # thinks the pyc files are correct using them)
545 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300546
547 # handle GIT_REF accordingly to type (tag, branch)
548 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
549 git_update_tag $GIT_REF
550 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
551 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400552 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
553 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300554 else
555 echo $GIT_REF is neither branch nor tag
556 exit 1
557 fi
558
Dean Troyer7f9aa712012-01-31 12:11:56 -0600559 fi
560 fi
561}
562
563
Dean Troyer1a6d4492013-06-03 16:47:36 -0500564# git update using reference as a branch.
565# git_update_branch ref
566function git_update_branch() {
567
568 GIT_BRANCH=$1
569
570 git checkout -f origin/$GIT_BRANCH
571 # a local branch might not exist
572 git branch -D $GIT_BRANCH || true
573 git checkout -b $GIT_BRANCH
574}
575
576
577# git update using reference as a branch.
578# git_update_remote_branch ref
579function git_update_remote_branch() {
580
581 GIT_BRANCH=$1
582
583 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
584}
585
586
587# git update using reference as a tag. Be careful editing source at that repo
588# as working copy will be in a detached mode
589# git_update_tag ref
590function git_update_tag() {
591
592 GIT_TAG=$1
593
594 git tag -d $GIT_TAG
595 # fetching given tag only
596 git fetch origin tag $GIT_TAG
597 git checkout -f $GIT_TAG
598}
599
600
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500601# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200602# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500603function inicomment() {
604 local file=$1
605 local section=$2
606 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100607 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500608}
609
Dean Troyer896eb662013-04-05 15:02:01 -0500610
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200611# Uncomment an option in an INI file
612# iniuncomment config-file section option
613function iniuncomment() {
614 local file=$1
615 local section=$2
616 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100617 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200618}
619
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500620
621# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500622# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500623function iniget() {
624 local file=$1
625 local section=$2
626 local option=$3
627 local line
Attila Fazekas588eb412012-12-20 10:57:16 +0100628 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500629 echo ${line#*=}
630}
631
Dean Troyer896eb662013-04-05 15:02:01 -0500632
Attila Fazekas588eb412012-12-20 10:57:16 +0100633# Determinate is the given option present in the INI file
634# ini_has_option config-file section option
635function ini_has_option() {
636 local file=$1
637 local section=$2
638 local option=$3
639 local line
640 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
641 [ -n "$line" ]
642}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500643
Dean Troyer896eb662013-04-05 15:02:01 -0500644
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500645# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500646# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500647function iniset() {
648 local file=$1
649 local section=$2
650 local option=$3
651 local value=$4
Attila Fazekas588eb412012-12-20 10:57:16 +0100652 if ! grep -q "^\[$section\]" "$file"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500653 # Add section at the end
Attila Fazekas588eb412012-12-20 10:57:16 +0100654 echo -e "\n[$section]" >>"$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500655 fi
Attila Fazekas588eb412012-12-20 10:57:16 +0100656 if ! ini_has_option "$file" "$section" "$option"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500657 # Add it
Attila Fazekas588eb412012-12-20 10:57:16 +0100658 sed -i -e "/^\[$section\]/ a\\
Dean Troyer09e636e2012-03-19 16:31:12 -0500659$option = $value
Attila Fazekas588eb412012-12-20 10:57:16 +0100660" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500661 else
662 # Replace it
Attila Fazekas588eb412012-12-20 10:57:16 +0100663 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500664 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500665}
666
Dean Troyer896eb662013-04-05 15:02:01 -0500667
Lianhao Lu239f3242013-03-01 15:54:02 +0800668# Get a multiple line option from an INI file
669# iniget_multiline config-file section option
670function iniget_multiline() {
671 local file=$1
672 local section=$2
673 local option=$3
674 local values
675 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
676 echo ${values}
677}
678
Dean Troyer896eb662013-04-05 15:02:01 -0500679
Lianhao Lu239f3242013-03-01 15:54:02 +0800680# Set a multiple line option in an INI file
681# iniset_multiline config-file section option value1 value2 valu3 ...
682function iniset_multiline() {
683 local file=$1
684 local section=$2
685 local option=$3
686 shift 3
687 local values
688 for v in $@; do
689 # The later sed command inserts each new value in the line next to
690 # the section identifier, which causes the values to be inserted in
691 # the reverse order. Do a reverse here to keep the original order.
692 values="$v ${values}"
693 done
694 if ! grep -q "^\[$section\]" "$file"; then
695 # Add section at the end
696 echo -e "\n[$section]" >>"$file"
697 else
698 # Remove old values
699 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
700 fi
701 # Add new ones
702 for v in $values; do
703 sed -i -e "/^\[$section\]/ a\\
704$option = $v
705" "$file"
706 done
707}
708
Dean Troyer896eb662013-04-05 15:02:01 -0500709
Lianhao Lu239f3242013-03-01 15:54:02 +0800710# Append a new option in an ini file without replacing the old value
711# iniadd config-file section option value1 value2 value3 ...
712function iniadd() {
713 local file=$1
714 local section=$2
715 local option=$3
716 shift 3
717 local values="$(iniget_multiline $file $section $option) $@"
718 iniset_multiline $file $section $option $values
719}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500720
Dean Troyer896eb662013-04-05 15:02:01 -0500721# Find out if a process exists by partial name.
722# is_running name
723function is_running() {
724 local name=$1
725 ps auxw | grep -v grep | grep ${name} > /dev/null
726 RC=$?
727 # some times I really hate bash reverse binary logic
728 return $RC
729}
730
731
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000732# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500733# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000734#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500735# Multiple services specified as arguments are ``OR``'ed together; the test
736# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000737#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500738# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000739# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500740# **cinder** returns true if any service enabled start with **c-**
741# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000742# **glance** returns true if any service enabled start with **g-**
Mark McClainb05c8762013-07-06 23:29:39 -0400743# **neutron** returns true if any service enabled start with **q-**
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100744# **swift** returns true if any service enabled start with **s-**
745# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
746# **s-** services will be enabled. This will be deprecated in the future.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500747#
748# Uses global ``ENABLED_SERVICES``
749# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000750function is_service_enabled() {
751 services=$@
752 for service in ${services}; do
753 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
754 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500755 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700756 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000757 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
Mark McClainb05c8762013-07-06 23:29:39 -0400758 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100759 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && return 0
760 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000761 done
762 return 1
763}
764
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500765
766# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
767# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400768function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500769 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400770 s/,,/,/g;
771 s/^,//;
772 s/,$//
773 '
774}
775
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500776
Doug Hellmannf04178f2012-07-05 17:10:03 -0400777# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500778# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400779#
780# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800781# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400782#
783# This function does not know about the special cases
Mark McClainb05c8762013-07-06 23:29:39 -0400784# for nova, glance, and neutron built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500785# Uses global ``ENABLED_SERVICES``
786# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400787function enable_service() {
788 local tmpsvcs="${ENABLED_SERVICES}"
789 for service in $@; do
790 if ! is_service_enabled $service; then
791 tmpsvcs+=",$service"
792 fi
793 done
794 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
795 disable_negated_services
796}
797
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500798
Doug Hellmannf04178f2012-07-05 17:10:03 -0400799# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500800# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400801#
802# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800803# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400804#
805# This function does not know about the special cases
Mark McClainb05c8762013-07-06 23:29:39 -0400806# for nova, glance, and neutron built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500807# Uses global ``ENABLED_SERVICES``
808# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400809function disable_service() {
810 local tmpsvcs=",${ENABLED_SERVICES},"
811 local service
812 for service in $@; do
813 if is_service_enabled $service; then
814 tmpsvcs=${tmpsvcs//,$service,/,}
815 fi
816 done
817 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
818}
819
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500820
Doug Hellmannf04178f2012-07-05 17:10:03 -0400821# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500822# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400823# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500824# Uses global ``ENABLED_SERVICES``
825# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400826function disable_all_services() {
827 ENABLED_SERVICES=""
828}
829
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500830
831# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800832# services except rabbit (rabbit) set in ``localrc``:
833# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500834# Uses global ``ENABLED_SERVICES``
835# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400836function disable_negated_services() {
837 local tmpsvcs="${ENABLED_SERVICES}"
838 local service
839 for service in ${tmpsvcs//,/ }; do
840 if [[ ${service} == -* ]]; then
841 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
842 fi
843 done
844 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
845}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600846
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500847
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500848# Distro-agnostic package installer
849# install_package package [package ...]
850function install_package() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100851 if is_ubuntu; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200852 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
853 NO_UPDATE_REPOS=True
854
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500855 apt_get install "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100856 elif is_fedora; then
857 yum_install "$@"
858 elif is_suse; then
859 zypper_install "$@"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500860 else
Vincent Untz00011c02012-12-06 09:56:32 +0100861 exit_distro_not_supported "installing packages"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500862 fi
863}
864
865
Dean Troyer995eb922013-03-07 16:11:40 -0600866# Distro-agnostic package uninstaller
867# uninstall_package package [package ...]
868function uninstall_package() {
869 if is_ubuntu; then
870 apt_get purge "$@"
871 elif is_fedora; then
Ian Wienand2c678cc2013-03-20 13:00:44 +1100872 sudo yum remove -y "$@"
Dean Troyer995eb922013-03-07 16:11:40 -0600873 elif is_suse; then
Ian Wienand2c678cc2013-03-20 13:00:44 +1100874 sudo rpm -e "$@"
Dean Troyer995eb922013-03-07 16:11:40 -0600875 else
876 exit_distro_not_supported "uninstalling packages"
877 fi
878}
879
880
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200881# Distro-agnostic function to tell if a package is installed
882# is_package_installed package [package ...]
883function is_package_installed() {
884 if [[ -z "$@" ]]; then
885 return 1
886 fi
887
888 if [[ -z "$os_PACKAGE" ]]; then
889 GetOSVersion
890 fi
Vincent Untzc18b9652012-12-04 12:36:34 +0100891
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200892 if [[ "$os_PACKAGE" = "deb" ]]; then
893 dpkg -l "$@" > /dev/null
Vincent Untz00011c02012-12-06 09:56:32 +0100894 elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200895 rpm --quiet -q "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100896 else
897 exit_distro_not_supported "finding if a package is installed"
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200898 fi
899}
900
901
Dean Troyer489bd2a2012-03-02 10:44:29 -0600902# Test if the named environment variable is set and not zero length
903# is_set env-var
904function is_set() {
905 local var=\$"$1"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100906 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 -0600907}
908
909
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500910# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +0000911# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
912# ``TRACK_DEPENDS``, ``*_proxy`
Dean Troyer7f9aa712012-01-31 12:11:56 -0600913# pip_install package [package ...]
914function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600915 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500916 if [[ -z "$os_PACKAGE" ]]; then
917 GetOSVersion
918 fi
Dean Troyercc6b4432013-04-08 15:38:03 -0500919 if [[ $TRACK_DEPENDS = True ]]; then
Monty Taylor47f02062012-07-26 11:09:24 -0500920 source $DEST/.venv/bin/activate
921 CMD_PIP=$DEST/.venv/bin/pip
922 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500923 else
Monty Taylor47f02062012-07-26 11:09:24 -0500924 SUDO_PIP="sudo"
Vincent Untz8ec27222012-11-29 09:25:31 +0100925 CMD_PIP=$(get_pip_command)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500926 fi
Ian Wienandd67dd872013-04-11 11:14:36 +1000927
928 if [[ is_fedora && $DISTRO =~ (rhel6) ]]; then
929 # RHEL6 pip by default doesn't have this (was introduced
930 # around 0.8.1 or so)
931 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
932 else
933 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-True}
934 fi
Maru Newby3a87edd2012-10-25 23:01:06 +0000935 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
936 PIP_MIRROR_OPT="--use-mirrors"
937 fi
Ian Wienandd67dd872013-04-11 11:14:36 +1000938
Ian Wienand31dcd3e2013-07-16 13:36:34 +1000939 # pip < 1.4 has a bug where it will use an already existing build
940 # directory unconditionally. Say an earlier component installs
941 # foo v1.1; pip will have built foo's source in
942 # /tmp/$USER-pip-build. Even if a later component specifies foo <
943 # 1.1, the existing extracted build will be used and cause
944 # confusing errors. By creating unique build directories we avoid
945 # this problem. See
946 # https://github.com/pypa/pip/issues/709
947 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
948
Monty Taylor47f02062012-07-26 11:09:24 -0500949 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600950 HTTP_PROXY=$http_proxy \
951 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900952 NO_PROXY=$no_proxy \
Ian Wienand31dcd3e2013-07-16 13:36:34 +1000953 $CMD_PIP install --build=${pip_build_tmp} \
954 $PIP_MIRROR_OPT $@ \
955 && $SUDO_PIP rm -rf ${pip_build_tmp}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500956}
957
958
Ian Wienand31dcd3e2013-07-16 13:36:34 +1000959# Cleanup anything from /tmp on unstack
960# clean_tmp
961function cleanup_tmp {
962 local tmp_dir=${TMPDIR:-/tmp}
963
964 # see comments in pip_install
965 sudo rm -rf ${tmp_dir}/pip-build.*
966}
967
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500968# Service wrapper to restart services
969# restart_service service-name
970function restart_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100971 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600972 sudo /usr/sbin/service $1 restart
973 else
974 sudo /sbin/service $1 restart
975 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500976}
977
978
Dean Troyer681f3fd2013-02-27 19:00:39 -0600979# _run_process() is designed to be backgrounded by run_process() to simulate a
980# fork. It includes the dirty work of closing extra filehandles and preparing log
981# files to produce the same logs as screen_it(). The log filename is derived
982# from the service name and global-and-now-misnamed SCREEN_LOGDIR
983# _run_process service "command-line"
984function _run_process() {
985 local service=$1
986 local command="$2"
987
988 # Undo logging redirections and close the extra descriptors
989 exec 1>&3
990 exec 2>&3
991 exec 3>&-
992 exec 6>&-
993
994 if [[ -n ${SCREEN_LOGDIR} ]]; then
995 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
996 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
997
998 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
999 export PYTHONUNBUFFERED=1
1000 fi
1001
1002 exec /bin/bash -c "$command"
1003 die "$service exec failure: $command"
1004}
1005
1006
1007# run_process() launches a child process that closes all file descriptors and
1008# then exec's the passed in command. This is meant to duplicate the semantics
1009# of screen_it() without screen. PIDs are written to
1010# $SERVICE_DIR/$SCREEN_NAME/$service.pid
1011# run_process service "command-line"
1012function run_process() {
1013 local service=$1
1014 local command="$2"
1015
1016 # Spawn the child process
1017 _run_process "$service" "$command" &
1018 echo $!
1019}
1020
1021
Dean Troyer15733352012-09-06 11:51:30 -05001022# Helper to launch a service in a named screen
1023# screen_it service "command-line"
1024function screen_it {
Dean Troyer15733352012-09-06 11:51:30 -05001025 SCREEN_NAME=${SCREEN_NAME:-stack}
jiajun xua9414242012-12-06 16:30:57 +08001026 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer681f3fd2013-02-27 19:00:39 -06001027 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
jiajun xua9414242012-12-06 16:30:57 +08001028
Dean Troyer15733352012-09-06 11:51:30 -05001029 if is_service_enabled $1; then
1030 # Append the service to the screen rc file
1031 screen_rc "$1" "$2"
1032
Dean Troyer681f3fd2013-02-27 19:00:39 -06001033 if [[ "$USE_SCREEN" = "True" ]]; then
1034 screen -S $SCREEN_NAME -X screen -t $1
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +00001035
Dean Troyer681f3fd2013-02-27 19:00:39 -06001036 if [[ -n ${SCREEN_LOGDIR} ]]; then
1037 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
1038 screen -S $SCREEN_NAME -p $1 -X log on
1039 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1040 fi
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +00001041
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001042 # sleep to allow bash to be ready to be send the command - we are
1043 # creating a new window in screen and then sends characters, so if
1044 # bash isn't running by the time we send the command, nothing happens
1045 sleep 1.5
Dean Troyer15733352012-09-06 11:51:30 -05001046
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001047 NL=`echo -ne '\015'`
1048 screen -S $SCREEN_NAME -p $1 -X stuff "$2 || touch \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
1049 else
Dean Troyer681f3fd2013-02-27 19:00:39 -06001050 # Spawn directly without screen
1051 run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyer15733352012-09-06 11:51:30 -05001052 fi
Dean Troyer15733352012-09-06 11:51:30 -05001053 fi
1054}
1055
1056
1057# Screen rc file builder
1058# screen_rc service "command-line"
1059function screen_rc {
1060 SCREEN_NAME=${SCREEN_NAME:-stack}
1061 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1062 if [[ ! -e $SCREENRC ]]; then
1063 # Name the screen session
1064 echo "sessionname $SCREEN_NAME" > $SCREENRC
1065 # Set a reasonable statusbar
1066 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
Steven Dake30396572013-06-30 16:11:54 -07001067 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1068 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
Dean Troyer15733352012-09-06 11:51:30 -05001069 echo "screen -t shell bash" >> $SCREENRC
1070 fi
1071 # If this service doesn't already exist in the screenrc file
1072 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1073 NL=`echo -ne '\015'`
1074 echo "screen -t $1 bash" >> $SCREENRC
1075 echo "stuff \"$2$NL\"" >> $SCREENRC
1076 fi
1077}
1078
Dean Troyer1a6d4492013-06-03 16:47:36 -05001079
jiajun xua9414242012-12-06 16:30:57 +08001080# Helper to remove the *.failure files under $SERVICE_DIR/$SCREEN_NAME
1081# This is used for service_check when all the screen_it are called finished
1082# init_service_check
1083function init_service_check() {
1084 SCREEN_NAME=${SCREEN_NAME:-stack}
1085 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1086
1087 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1088 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1089 fi
1090
1091 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1092}
1093
Dean Troyer1a6d4492013-06-03 16:47:36 -05001094
jiajun xua9414242012-12-06 16:30:57 +08001095# Helper to get the status of each running service
1096# service_check
1097function service_check() {
1098 local service
1099 local failures
1100 SCREEN_NAME=${SCREEN_NAME:-stack}
1101 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1102
1103
1104 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1105 echo "No service status directory found"
1106 return
1107 fi
1108
1109 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
1110 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null`
1111
1112 for service in $failures; do
1113 service=`basename $service`
1114 service=${service::-8}
1115 echo "Error: Service $service is not running"
1116 done
1117
1118 if [ -n "$failures" ]; then
1119 echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
1120 fi
1121}
Dean Troyer15733352012-09-06 11:51:30 -05001122
Dean Troyer1a6d4492013-06-03 16:47:36 -05001123
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001124# ``pip install`` the dependencies of the package before ``setup.py develop``
1125# so pip and not distutils processes the dependency chain
1126# Uses globals ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001127# setup_develop directory
1128function setup_develop() {
Dean Troyercc6b4432013-04-08 15:38:03 -05001129 if [[ $TRACK_DEPENDS = True ]]; then
Monty Taylor47f02062012-07-26 11:09:24 -05001130 SUDO_CMD="env"
1131 else
1132 SUDO_CMD="sudo"
1133 fi
Monty Taylore7e51ac2013-07-04 14:10:46 -04001134 for reqs_file in $1/requirements.txt $1/tools/pip-requires ; do
1135 if [ -f $reqs_file ] ; then
1136 pip_install -r $reqs_file
1137 fi
1138 done
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001139 (cd $1; \
1140 python setup.py egg_info; \
Monty Taylor47f02062012-07-26 11:09:24 -05001141 $SUDO_CMD \
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001142 HTTP_PROXY=$http_proxy \
1143 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001144 NO_PROXY=$no_proxy \
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001145 python setup.py develop \
1146 )
1147}
1148
1149
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001150# Service wrapper to start services
1151# start_service service-name
1152function start_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001153 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001154 sudo /usr/sbin/service $1 start
1155 else
1156 sudo /sbin/service $1 start
1157 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001158}
1159
1160
1161# Service wrapper to stop services
1162# stop_service service-name
1163function stop_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001164 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001165 sudo /usr/sbin/service $1 stop
1166 else
1167 sudo /sbin/service $1 stop
1168 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -06001169}
1170
1171
1172# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001173# Accepts as False: 0 no false False FALSE
1174# Accepts as True: 1 yes true True TRUE
1175# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -06001176function trueorfalse() {
1177 local default=$1
1178 local testval=$2
1179
1180 [[ -z "$testval" ]] && { echo "$default"; return; }
1181 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
1182 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
1183 echo "$default"
1184}
Dean Troyer27e32692012-03-16 16:16:56 -05001185
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001186
Dean Troyerca0e3d02012-04-13 15:58:37 -05001187# Retrieve an image from a URL and upload into Glance
1188# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001189# ``FILES`` must be set to the cache dir
1190# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -05001191# upload_image image-url glance-token
1192function upload_image() {
1193 local image_url=$1
1194 local token=$2
1195
1196 # Create a directory for the downloaded image tarballs.
1197 mkdir -p $FILES/images
1198
1199 # Downloads the image (uec ami+aki style), then extracts it.
1200 IMAGE_FNAME=`basename "$image_url"`
1201 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
1202 wget -c $image_url -O $FILES/$IMAGE_FNAME
1203 if [[ $? -ne 0 ]]; then
1204 echo "Not found: $image_url"
1205 return
1206 fi
1207 fi
1208
1209 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
1210 if [[ "$image_url" =~ 'openvz' ]]; then
1211 IMAGE="$FILES/${IMAGE_FNAME}"
1212 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
1213 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}"
1214 return
1215 fi
1216
Davanum Srinivas316ed6c2013-02-06 15:29:49 -05001217 # XenServer-ovf-format images are provided as .vhd.tgz as well
1218 # and should not be decompressed prior to loading
1219 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
1220 IMAGE="$FILES/${IMAGE_FNAME}"
1221 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
1222 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}"
1223 return
1224 fi
1225
Dean Troyerca0e3d02012-04-13 15:58:37 -05001226 KERNEL=""
1227 RAMDISK=""
1228 DISK_FORMAT=""
1229 CONTAINER_FORMAT=""
1230 UNPACK=""
1231 case "$IMAGE_FNAME" in
1232 *.tar.gz|*.tgz)
1233 # Extract ami and aki files
1234 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
1235 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
1236 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
1237 xdir="$FILES/images/$IMAGE_NAME"
1238 rm -Rf "$xdir";
1239 mkdir "$xdir"
1240 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
1241 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
1242 [ -f "$f" ] && echo "$f" && break; done; true)
1243 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
1244 [ -f "$f" ] && echo "$f" && break; done; true)
1245 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
1246 [ -f "$f" ] && echo "$f" && break; done; true)
1247 if [[ -z "$IMAGE_NAME" ]]; then
1248 IMAGE_NAME=$(basename "$IMAGE" ".img")
1249 fi
1250 ;;
1251 *.img)
1252 IMAGE="$FILES/$IMAGE_FNAME";
1253 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -05001254 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
1255 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
1256 DISK_FORMAT=$format
1257 else
1258 DISK_FORMAT=raw
1259 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -05001260 CONTAINER_FORMAT=bare
1261 ;;
1262 *.img.gz)
1263 IMAGE="$FILES/${IMAGE_FNAME}"
1264 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
1265 DISK_FORMAT=raw
1266 CONTAINER_FORMAT=bare
1267 UNPACK=zcat
1268 ;;
1269 *.qcow2)
1270 IMAGE="$FILES/${IMAGE_FNAME}"
1271 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
1272 DISK_FORMAT=qcow2
1273 CONTAINER_FORMAT=bare
1274 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +01001275 *.iso)
1276 IMAGE="$FILES/${IMAGE_FNAME}"
1277 IMAGE_NAME=$(basename "$IMAGE" ".iso")
1278 DISK_FORMAT=iso
1279 CONTAINER_FORMAT=bare
1280 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -05001281 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
1282 esac
1283
1284 if [ "$CONTAINER_FORMAT" = "bare" ]; then
1285 if [ "$UNPACK" = "zcat" ]; then
1286 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}")
1287 else
1288 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}"
1289 fi
1290 else
1291 # Use glance client to add the kernel the root filesystem.
1292 # We parse the results of the first upload to get the glance ID of the
1293 # kernel for use when uploading the root filesystem.
1294 KERNEL_ID=""; RAMDISK_ID="";
1295 if [ -n "$KERNEL" ]; then
1296 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)
1297 fi
1298 if [ -n "$RAMDISK" ]; then
1299 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)
1300 fi
1301 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}"
1302 fi
1303}
1304
Dean Troyer1a6d4492013-06-03 16:47:36 -05001305
Dean Troyerc1b486a2012-11-05 14:26:09 -06001306# Set the database backend to use
1307# When called from stackrc/localrc DATABASE_BACKENDS has not been
1308# initialized yet, just save the configuration selection and call back later
1309# to validate it.
1310# $1 The name of the database backend to use (mysql, postgresql, ...)
1311function use_database {
1312 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -06001313 # No backends registered means this is likely called from ``localrc``
1314 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -06001315 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +00001316 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 +01001317 else
Dean Troyerafc29fe2013-02-07 15:56:24 -06001318 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +01001319 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -06001320 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -06001321}
1322
Dean Troyer1a6d4492013-06-03 16:47:36 -05001323
Terry Wilson428af5a2012-11-01 16:12:39 -04001324# Toggle enable/disable_service for services that must run exclusive of each other
1325# $1 The name of a variable containing a space-separated list of services
1326# $2 The name of a variable in which to store the enabled service's name
1327# $3 The name of the service to enable
1328function use_exclusive_service {
1329 local options=${!1}
1330 local selection=$3
1331 out=$2
1332 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1333 for opt in $options;do
1334 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1335 done
1336 eval "$out=$selection"
1337 return 0
1338}
Dean Troyerca0e3d02012-04-13 15:58:37 -05001339
Dean Troyer1a6d4492013-06-03 16:47:36 -05001340
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001341# Wait for an HTTP server to start answering requests
1342# wait_for_service timeout url
1343function wait_for_service() {
1344 local timeout=$1
1345 local url=$2
1346 timeout $timeout sh -c "while ! http_proxy= https_proxy= curl -s $url >/dev/null; do sleep 1; done"
1347}
1348
Dean Troyer1a6d4492013-06-03 16:47:36 -05001349
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001350# Wrapper for ``yum`` to set proxy environment variables
1351# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001352# yum_install package [package ...]
1353function yum_install() {
1354 [[ "$OFFLINE" = "True" ]] && return
1355 local sudo="sudo"
1356 [[ "$(id -u)" = "0" ]] && sudo="env"
1357 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001358 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001359 yum install -y "$@"
1360}
1361
Dean Troyer1a6d4492013-06-03 16:47:36 -05001362
1363# zypper wrapper to set arguments correctly
1364# zypper_install package [package ...]
1365function zypper_install() {
1366 [[ "$OFFLINE" = "True" ]] && return
1367 local sudo="sudo"
1368 [[ "$(id -u)" = "0" ]] && sudo="env"
1369 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1370 zypper --non-interactive install --auto-agree-with-licenses "$@"
1371}
1372
1373
Nachi Uenofda946e2012-10-24 17:26:02 -07001374# ping check
1375# Uses globals ``ENABLED_SERVICES``
Dean Troyer1a6d4492013-06-03 16:47:36 -05001376# ping_check from-net ip boot-timeout expected
Nachi Uenofda946e2012-10-24 17:26:02 -07001377function ping_check() {
Mark McClainb05c8762013-07-06 23:29:39 -04001378 if is_service_enabled neutron; then
1379 _ping_check_neutron "$1" $2 $3 $4
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001380 return
1381 fi
1382 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -07001383}
1384
1385# ping check for nova
1386# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
1387function _ping_check_novanet() {
1388 local from_net=$1
1389 local ip=$2
1390 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001391 local expected=${4:-"True"}
1392 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001393 MULTI_HOST=`trueorfalse False $MULTI_HOST`
1394 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
1395 sleep $boot_timeout
1396 return
1397 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001398 if [[ "$expected" = "True" ]]; then
1399 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
1400 else
1401 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
1402 fi
1403 if ! timeout $boot_timeout sh -c "$check_command"; then
1404 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -08001405 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001406 else
Nachi Ueno07115eb2013-02-26 12:38:18 -08001407 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001408 fi
Nachi Uenofda946e2012-10-24 17:26:02 -07001409 exit 1
1410 fi
1411}
1412
Dean Troyer1a6d4492013-06-03 16:47:36 -05001413
Nachi Uenofda946e2012-10-24 17:26:02 -07001414# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001415
Dean Troyer1a6d4492013-06-03 16:47:36 -05001416# ssh_check net-name key-file floating-ip default-user active-timeout
Nachi Uenofda946e2012-10-24 17:26:02 -07001417function ssh_check() {
Mark McClainb05c8762013-07-06 23:29:39 -04001418 if is_service_enabled neutron; then
1419 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001420 return
1421 fi
1422 _ssh_check_novanet "$1" $2 $3 $4 $5
1423}
1424
1425function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -07001426 local NET_NAME=$1
1427 local KEY_FILE=$2
1428 local FLOATING_IP=$3
1429 local DEFAULT_INSTANCE_USER=$4
1430 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -06001431 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -05001432 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 -08001433 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -07001434 fi
1435}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001436
Vincent Untz856a11e2012-11-21 16:04:12 +01001437
Vincent Untz856a11e2012-11-21 16:04:12 +01001438# Add a user to a group.
1439# add_user_to_group user group
1440function add_user_to_group() {
1441 local user=$1
1442 local group=$2
1443
1444 if [[ -z "$os_VENDOR" ]]; then
1445 GetOSVersion
1446 fi
1447
1448 # SLE11 and openSUSE 12.2 don't have the usual usermod
1449 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1450 sudo usermod -a -G "$group" "$user"
1451 else
1452 sudo usermod -A "$group" "$user"
1453 fi
1454}
1455
1456
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001457# Get the path to the direcotry where python executables are installed.
1458# get_python_exec_prefix
1459function get_python_exec_prefix() {
Martin Vidner4f9b33d2013-06-27 13:11:22 +00001460 if is_fedora || is_suse; then
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001461 echo "/usr/bin"
1462 else
1463 echo "/usr/local/bin"
1464 fi
1465}
1466
Dean Troyer1a6d4492013-06-03 16:47:36 -05001467
Vincent Untz856a11e2012-11-21 16:04:12 +01001468# Get the location of the $module-rootwrap executables, where module is cinder
1469# or nova.
1470# get_rootwrap_location module
1471function get_rootwrap_location() {
1472 local module=$1
1473
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001474 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +01001475}
1476
Dean Troyer1a6d4492013-06-03 16:47:36 -05001477
Vincent Untz8ec27222012-11-29 09:25:31 +01001478# Get the path to the pip command.
1479# get_pip_command
1480function get_pip_command() {
Vincent Untz00011c02012-12-06 09:56:32 +01001481 if is_fedora; then
Nikhil Manchanda35138ed2013-01-03 17:49:58 -08001482 which pip-python
Vincent Untz00011c02012-12-06 09:56:32 +01001483 else
Nikhil Manchanda35138ed2013-01-03 17:49:58 -08001484 which pip
Vincent Untz8ec27222012-11-29 09:25:31 +01001485 fi
Ian Wienand535a8142013-05-15 09:25:27 +10001486
1487 if [ $? -ne 0 ]; then
1488 die $LINENO "Unable to find pip; cannot continue"
1489 fi
Vincent Untz8ec27222012-11-29 09:25:31 +01001490}
Vincent Untz856a11e2012-11-21 16:04:12 +01001491
Dean Troyer1a6d4492013-06-03 16:47:36 -05001492
Ian Wienand0488edd2013-04-11 12:04:36 +10001493# Path permissions sanity check
1494# check_path_perm_sanity path
1495function check_path_perm_sanity() {
1496 # Ensure no element of the path has 0700 permissions, which is very
1497 # likely to cause issues for daemons. Inspired by default 0700
1498 # homedir permissions on RHEL and common practice of making DEST in
1499 # the stack user's homedir.
1500
1501 local real_path=$(readlink -f $1)
1502 local rebuilt_path=""
1503 for i in $(echo ${real_path} | tr "/" " "); do
1504 rebuilt_path=$rebuilt_path"/"$i
1505
1506 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
1507 echo "*** DEST path element"
1508 echo "*** ${rebuilt_path}"
1509 echo "*** appears to have 0700 permissions."
1510 echo "*** This is very likely to cause fatal issues for devstack daemons."
1511
1512 if [[ -n "$SKIP_PATH_SANITY" ]]; then
1513 return
1514 else
1515 echo "*** Set SKIP_PATH_SANITY to skip this check"
1516 die $LINENO "Invalid path permissions"
1517 fi
1518 fi
1519 done
1520}
1521
Dean Troyer1a6d4492013-06-03 16:47:36 -05001522
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001523# This function recursively compares versions, and is not meant to be
1524# called by anything other than vercmp_numbers below. This function does
1525# not work with alphabetic versions.
1526#
1527# _vercmp_r sep ver1 ver2
1528function _vercmp_r {
1529 typeset sep
1530 typeset -a ver1=() ver2=()
1531 sep=$1; shift
1532 ver1=("${@:1:sep}")
1533 ver2=("${@:sep+1}")
1534
1535 if ((ver1 > ver2)); then
1536 echo 1; return 0
1537 elif ((ver2 > ver1)); then
1538 echo -1; return 0
1539 fi
1540
1541 if ((sep <= 1)); then
1542 echo 0; return 0
1543 fi
1544
1545 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
1546}
1547
1548
1549# This function compares two versions and is meant to be called by
1550# external callers. Please note the function assumes non-alphabetic
1551# versions. For example, this will work:
1552#
1553# vercmp_numbers 1.10 1.4
1554#
1555# The above will return "1", as 1.10 is greater than 1.4.
1556#
1557# vercmp_numbers 5.2 6.4
1558#
1559# The above will return "-1", as 5.2 is less than 6.4.
1560#
1561# vercmp_numbers 4.0 4.0
1562#
1563# The above will return "0", as the versions are equal.
1564#
1565# vercmp_numbers ver1 ver2
1566vercmp_numbers() {
1567 typeset v1=$1 v2=$2 sep
1568 typeset -a ver1 ver2
1569
1570 IFS=. read -ra ver1 <<< "$v1"
1571 IFS=. read -ra ver2 <<< "$v2"
1572
1573 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
1574}
1575
1576
Dean Troyer27e32692012-03-16 16:16:56 -05001577# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +00001578$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001579
1580
1581# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -04001582# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -04001583# End: