blob: 632cd46b2f3a863068e7097b4318edc8668c6cc2 [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#
Chris Behrensc62c2b92013-07-24 03:56:13 -0700748# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
749# We also need to make sure to treat **n-cell-region** and **n-cell-child**
750# as enabled in this case.
751#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500752# Uses global ``ENABLED_SERVICES``
753# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000754function is_service_enabled() {
755 services=$@
756 for service in ${services}; do
757 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
Chris Behrensc62c2b92013-07-24 03:56:13 -0700758 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000759 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500760 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700761 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000762 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
Mark McClainb05c8762013-07-06 23:29:39 -0400763 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100764 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && return 0
765 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000766 done
767 return 1
768}
769
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500770
771# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
772# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400773function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500774 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400775 s/,,/,/g;
776 s/^,//;
777 s/,$//
778 '
779}
780
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500781
Doug Hellmannf04178f2012-07-05 17:10:03 -0400782# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500783# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400784#
785# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800786# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400787#
788# This function does not know about the special cases
Mark McClainb05c8762013-07-06 23:29:39 -0400789# for nova, glance, and neutron built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500790# Uses global ``ENABLED_SERVICES``
791# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400792function enable_service() {
793 local tmpsvcs="${ENABLED_SERVICES}"
794 for service in $@; do
795 if ! is_service_enabled $service; then
796 tmpsvcs+=",$service"
797 fi
798 done
799 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
800 disable_negated_services
801}
802
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500803
Doug Hellmannf04178f2012-07-05 17:10:03 -0400804# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500805# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400806#
807# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800808# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400809#
810# This function does not know about the special cases
Mark McClainb05c8762013-07-06 23:29:39 -0400811# for nova, glance, and neutron built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500812# Uses global ``ENABLED_SERVICES``
813# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400814function disable_service() {
815 local tmpsvcs=",${ENABLED_SERVICES},"
816 local service
817 for service in $@; do
818 if is_service_enabled $service; then
819 tmpsvcs=${tmpsvcs//,$service,/,}
820 fi
821 done
822 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
823}
824
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500825
Doug Hellmannf04178f2012-07-05 17:10:03 -0400826# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500827# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400828# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500829# Uses global ``ENABLED_SERVICES``
830# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400831function disable_all_services() {
832 ENABLED_SERVICES=""
833}
834
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500835
836# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800837# services except rabbit (rabbit) set in ``localrc``:
838# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500839# Uses global ``ENABLED_SERVICES``
840# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400841function disable_negated_services() {
842 local tmpsvcs="${ENABLED_SERVICES}"
843 local service
844 for service in ${tmpsvcs//,/ }; do
845 if [[ ${service} == -* ]]; then
846 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
847 fi
848 done
849 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
850}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600851
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500852
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500853# Distro-agnostic package installer
854# install_package package [package ...]
855function install_package() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100856 if is_ubuntu; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200857 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
858 NO_UPDATE_REPOS=True
859
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500860 apt_get install "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100861 elif is_fedora; then
862 yum_install "$@"
863 elif is_suse; then
864 zypper_install "$@"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500865 else
Vincent Untz00011c02012-12-06 09:56:32 +0100866 exit_distro_not_supported "installing packages"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500867 fi
868}
869
870
Dean Troyer995eb922013-03-07 16:11:40 -0600871# Distro-agnostic package uninstaller
872# uninstall_package package [package ...]
873function uninstall_package() {
874 if is_ubuntu; then
875 apt_get purge "$@"
876 elif is_fedora; then
Ian Wienand2c678cc2013-03-20 13:00:44 +1100877 sudo yum remove -y "$@"
Dean Troyer995eb922013-03-07 16:11:40 -0600878 elif is_suse; then
Ian Wienand2c678cc2013-03-20 13:00:44 +1100879 sudo rpm -e "$@"
Dean Troyer995eb922013-03-07 16:11:40 -0600880 else
881 exit_distro_not_supported "uninstalling packages"
882 fi
883}
884
885
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200886# Distro-agnostic function to tell if a package is installed
887# is_package_installed package [package ...]
888function is_package_installed() {
889 if [[ -z "$@" ]]; then
890 return 1
891 fi
892
893 if [[ -z "$os_PACKAGE" ]]; then
894 GetOSVersion
895 fi
Vincent Untzc18b9652012-12-04 12:36:34 +0100896
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200897 if [[ "$os_PACKAGE" = "deb" ]]; then
898 dpkg -l "$@" > /dev/null
Vincent Untz00011c02012-12-06 09:56:32 +0100899 elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200900 rpm --quiet -q "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100901 else
902 exit_distro_not_supported "finding if a package is installed"
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200903 fi
904}
905
906
Dean Troyer489bd2a2012-03-02 10:44:29 -0600907# Test if the named environment variable is set and not zero length
908# is_set env-var
909function is_set() {
910 local var=\$"$1"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100911 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 -0600912}
913
914
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500915# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +0000916# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
917# ``TRACK_DEPENDS``, ``*_proxy`
Dean Troyer7f9aa712012-01-31 12:11:56 -0600918# pip_install package [package ...]
919function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600920 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500921 if [[ -z "$os_PACKAGE" ]]; then
922 GetOSVersion
923 fi
Dean Troyercc6b4432013-04-08 15:38:03 -0500924 if [[ $TRACK_DEPENDS = True ]]; then
Monty Taylor47f02062012-07-26 11:09:24 -0500925 source $DEST/.venv/bin/activate
926 CMD_PIP=$DEST/.venv/bin/pip
927 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500928 else
Monty Taylor47f02062012-07-26 11:09:24 -0500929 SUDO_PIP="sudo"
Vincent Untz8ec27222012-11-29 09:25:31 +0100930 CMD_PIP=$(get_pip_command)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500931 fi
Ian Wienandd67dd872013-04-11 11:14:36 +1000932
933 if [[ is_fedora && $DISTRO =~ (rhel6) ]]; then
934 # RHEL6 pip by default doesn't have this (was introduced
935 # around 0.8.1 or so)
936 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
937 else
938 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-True}
939 fi
Maru Newby3a87edd2012-10-25 23:01:06 +0000940 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
941 PIP_MIRROR_OPT="--use-mirrors"
942 fi
Ian Wienandd67dd872013-04-11 11:14:36 +1000943
Ian Wienand31dcd3e2013-07-16 13:36:34 +1000944 # pip < 1.4 has a bug where it will use an already existing build
945 # directory unconditionally. Say an earlier component installs
946 # foo v1.1; pip will have built foo's source in
947 # /tmp/$USER-pip-build. Even if a later component specifies foo <
948 # 1.1, the existing extracted build will be used and cause
949 # confusing errors. By creating unique build directories we avoid
950 # this problem. See
951 # https://github.com/pypa/pip/issues/709
952 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
953
Monty Taylor47f02062012-07-26 11:09:24 -0500954 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -0600955 HTTP_PROXY=$http_proxy \
956 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +0900957 NO_PROXY=$no_proxy \
Ian Wienand31dcd3e2013-07-16 13:36:34 +1000958 $CMD_PIP install --build=${pip_build_tmp} \
959 $PIP_MIRROR_OPT $@ \
960 && $SUDO_PIP rm -rf ${pip_build_tmp}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500961}
962
963
Ian Wienand31dcd3e2013-07-16 13:36:34 +1000964# Cleanup anything from /tmp on unstack
965# clean_tmp
966function cleanup_tmp {
967 local tmp_dir=${TMPDIR:-/tmp}
968
969 # see comments in pip_install
970 sudo rm -rf ${tmp_dir}/pip-build.*
971}
972
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500973# Service wrapper to restart services
974# restart_service service-name
975function restart_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100976 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -0600977 sudo /usr/sbin/service $1 restart
978 else
979 sudo /sbin/service $1 restart
980 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500981}
982
983
Dean Troyer681f3fd2013-02-27 19:00:39 -0600984# _run_process() is designed to be backgrounded by run_process() to simulate a
985# fork. It includes the dirty work of closing extra filehandles and preparing log
986# files to produce the same logs as screen_it(). The log filename is derived
987# from the service name and global-and-now-misnamed SCREEN_LOGDIR
988# _run_process service "command-line"
989function _run_process() {
990 local service=$1
991 local command="$2"
992
993 # Undo logging redirections and close the extra descriptors
994 exec 1>&3
995 exec 2>&3
996 exec 3>&-
997 exec 6>&-
998
999 if [[ -n ${SCREEN_LOGDIR} ]]; then
1000 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1001 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1002
1003 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1004 export PYTHONUNBUFFERED=1
1005 fi
1006
1007 exec /bin/bash -c "$command"
1008 die "$service exec failure: $command"
1009}
1010
1011
1012# run_process() launches a child process that closes all file descriptors and
1013# then exec's the passed in command. This is meant to duplicate the semantics
1014# of screen_it() without screen. PIDs are written to
1015# $SERVICE_DIR/$SCREEN_NAME/$service.pid
1016# run_process service "command-line"
1017function run_process() {
1018 local service=$1
1019 local command="$2"
1020
1021 # Spawn the child process
1022 _run_process "$service" "$command" &
1023 echo $!
1024}
1025
1026
Dean Troyer15733352012-09-06 11:51:30 -05001027# Helper to launch a service in a named screen
1028# screen_it service "command-line"
1029function screen_it {
Dean Troyer15733352012-09-06 11:51:30 -05001030 SCREEN_NAME=${SCREEN_NAME:-stack}
jiajun xua9414242012-12-06 16:30:57 +08001031 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer681f3fd2013-02-27 19:00:39 -06001032 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
jiajun xua9414242012-12-06 16:30:57 +08001033
Dean Troyer15733352012-09-06 11:51:30 -05001034 if is_service_enabled $1; then
1035 # Append the service to the screen rc file
1036 screen_rc "$1" "$2"
1037
Dean Troyer681f3fd2013-02-27 19:00:39 -06001038 if [[ "$USE_SCREEN" = "True" ]]; then
1039 screen -S $SCREEN_NAME -X screen -t $1
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +00001040
Dean Troyer681f3fd2013-02-27 19:00:39 -06001041 if [[ -n ${SCREEN_LOGDIR} ]]; then
1042 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
1043 screen -S $SCREEN_NAME -p $1 -X log on
1044 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1045 fi
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +00001046
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001047 # sleep to allow bash to be ready to be send the command - we are
1048 # creating a new window in screen and then sends characters, so if
1049 # bash isn't running by the time we send the command, nothing happens
1050 sleep 1.5
Dean Troyer15733352012-09-06 11:51:30 -05001051
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001052 NL=`echo -ne '\015'`
1053 screen -S $SCREEN_NAME -p $1 -X stuff "$2 || touch \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
1054 else
Dean Troyer681f3fd2013-02-27 19:00:39 -06001055 # Spawn directly without screen
1056 run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyer15733352012-09-06 11:51:30 -05001057 fi
Dean Troyer15733352012-09-06 11:51:30 -05001058 fi
1059}
1060
1061
1062# Screen rc file builder
1063# screen_rc service "command-line"
1064function screen_rc {
1065 SCREEN_NAME=${SCREEN_NAME:-stack}
1066 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1067 if [[ ! -e $SCREENRC ]]; then
1068 # Name the screen session
1069 echo "sessionname $SCREEN_NAME" > $SCREENRC
1070 # Set a reasonable statusbar
1071 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
Steven Dake30396572013-06-30 16:11:54 -07001072 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1073 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
Dean Troyer15733352012-09-06 11:51:30 -05001074 echo "screen -t shell bash" >> $SCREENRC
1075 fi
1076 # If this service doesn't already exist in the screenrc file
1077 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1078 NL=`echo -ne '\015'`
1079 echo "screen -t $1 bash" >> $SCREENRC
1080 echo "stuff \"$2$NL\"" >> $SCREENRC
1081 fi
1082}
1083
Dean Troyer1a6d4492013-06-03 16:47:36 -05001084
jiajun xua9414242012-12-06 16:30:57 +08001085# Helper to remove the *.failure files under $SERVICE_DIR/$SCREEN_NAME
1086# This is used for service_check when all the screen_it are called finished
1087# init_service_check
1088function init_service_check() {
1089 SCREEN_NAME=${SCREEN_NAME:-stack}
1090 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1091
1092 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1093 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1094 fi
1095
1096 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1097}
1098
Dean Troyer1a6d4492013-06-03 16:47:36 -05001099
jiajun xua9414242012-12-06 16:30:57 +08001100# Helper to get the status of each running service
1101# service_check
1102function service_check() {
1103 local service
1104 local failures
1105 SCREEN_NAME=${SCREEN_NAME:-stack}
1106 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1107
1108
1109 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1110 echo "No service status directory found"
1111 return
1112 fi
1113
1114 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
1115 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null`
1116
1117 for service in $failures; do
1118 service=`basename $service`
Bob Ball46287d82013-07-30 09:43:17 +01001119 service=${service%.failure}
jiajun xua9414242012-12-06 16:30:57 +08001120 echo "Error: Service $service is not running"
1121 done
1122
1123 if [ -n "$failures" ]; then
1124 echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
1125 fi
1126}
Dean Troyer15733352012-09-06 11:51:30 -05001127
Dean Troyer1a6d4492013-06-03 16:47:36 -05001128
Monty Taylor408a4a72013-08-02 15:43:47 -04001129# ``pip install -e`` the package, which processes the dependencies
1130# using pip before running `setup.py develop`
1131# Uses globals ``STACK_USER``, ``TRACK_DEPENDES``, ``*_proxy`
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001132# setup_develop directory
1133function setup_develop() {
Sean Dague6c844632013-07-31 06:50:14 -04001134 local project_dir=$1
Dean Troyercc6b4432013-04-08 15:38:03 -05001135 if [[ $TRACK_DEPENDS = True ]]; then
Monty Taylor47f02062012-07-26 11:09:24 -05001136 SUDO_CMD="env"
1137 else
1138 SUDO_CMD="sudo"
1139 fi
Sean Dague6c844632013-07-31 06:50:14 -04001140
1141 echo "cd $REQUIREMENTS_DIR; $SUDO_CMD python update.py $project_dir"
1142
1143 (cd $REQUIREMENTS_DIR; \
1144 $SUDO_CMD python update.py $project_dir)
1145
1146 for reqs_file in $project_dir/requirements.txt $project_dir/tools/pip-requires ; do
1147 if [ -f $reqs_file ] ; then
1148 pip_install -r $reqs_file
1149 fi
1150 done
1151
1152 (cd $project_dir; \
1153 python setup.py egg_info; \
1154 $SUDO_CMD \
1155 HTTP_PROXY=$http_proxy \
1156 HTTPS_PROXY=$https_proxy \
1157 NO_PROXY=$no_proxy \
1158 python setup.py develop \
1159 )
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001160}
1161
1162
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001163# Service wrapper to start services
1164# start_service service-name
1165function start_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001166 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001167 sudo /usr/sbin/service $1 start
1168 else
1169 sudo /sbin/service $1 start
1170 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001171}
1172
1173
1174# Service wrapper to stop services
1175# stop_service service-name
1176function stop_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001177 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001178 sudo /usr/sbin/service $1 stop
1179 else
1180 sudo /sbin/service $1 stop
1181 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -06001182}
1183
1184
1185# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001186# Accepts as False: 0 no false False FALSE
1187# Accepts as True: 1 yes true True TRUE
1188# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -06001189function trueorfalse() {
1190 local default=$1
1191 local testval=$2
1192
1193 [[ -z "$testval" ]] && { echo "$default"; return; }
1194 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
1195 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
1196 echo "$default"
1197}
Dean Troyer27e32692012-03-16 16:16:56 -05001198
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001199
Dean Troyerca0e3d02012-04-13 15:58:37 -05001200# Retrieve an image from a URL and upload into Glance
1201# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001202# ``FILES`` must be set to the cache dir
1203# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -05001204# upload_image image-url glance-token
1205function upload_image() {
1206 local image_url=$1
1207 local token=$2
1208
1209 # Create a directory for the downloaded image tarballs.
1210 mkdir -p $FILES/images
1211
1212 # Downloads the image (uec ami+aki style), then extracts it.
1213 IMAGE_FNAME=`basename "$image_url"`
1214 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
1215 wget -c $image_url -O $FILES/$IMAGE_FNAME
1216 if [[ $? -ne 0 ]]; then
1217 echo "Not found: $image_url"
1218 return
1219 fi
1220 fi
1221
1222 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
1223 if [[ "$image_url" =~ 'openvz' ]]; then
1224 IMAGE="$FILES/${IMAGE_FNAME}"
1225 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
1226 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}"
1227 return
1228 fi
1229
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -07001230 # vmdk format images
1231 if [[ "$image_url" =~ '.vmdk' ]]; then
1232 IMAGE="$FILES/${IMAGE_FNAME}"
1233 IMAGE_NAME="${IMAGE_FNAME%.vmdk}"
1234 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format bare --disk-format vmdk --property vmware-disktype="preallocated" < "${IMAGE}"
1235 return
1236 fi
1237
Davanum Srinivas316ed6c2013-02-06 15:29:49 -05001238 # XenServer-ovf-format images are provided as .vhd.tgz as well
1239 # and should not be decompressed prior to loading
1240 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
1241 IMAGE="$FILES/${IMAGE_FNAME}"
1242 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
1243 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}"
1244 return
1245 fi
1246
Dean Troyerca0e3d02012-04-13 15:58:37 -05001247 KERNEL=""
1248 RAMDISK=""
1249 DISK_FORMAT=""
1250 CONTAINER_FORMAT=""
1251 UNPACK=""
1252 case "$IMAGE_FNAME" in
1253 *.tar.gz|*.tgz)
1254 # Extract ami and aki files
1255 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
1256 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
1257 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
1258 xdir="$FILES/images/$IMAGE_NAME"
1259 rm -Rf "$xdir";
1260 mkdir "$xdir"
1261 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
1262 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
1263 [ -f "$f" ] && echo "$f" && break; done; true)
1264 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
1265 [ -f "$f" ] && echo "$f" && break; done; true)
1266 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
1267 [ -f "$f" ] && echo "$f" && break; done; true)
1268 if [[ -z "$IMAGE_NAME" ]]; then
1269 IMAGE_NAME=$(basename "$IMAGE" ".img")
1270 fi
1271 ;;
1272 *.img)
1273 IMAGE="$FILES/$IMAGE_FNAME";
1274 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -05001275 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
1276 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
1277 DISK_FORMAT=$format
1278 else
1279 DISK_FORMAT=raw
1280 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -05001281 CONTAINER_FORMAT=bare
1282 ;;
1283 *.img.gz)
1284 IMAGE="$FILES/${IMAGE_FNAME}"
1285 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
1286 DISK_FORMAT=raw
1287 CONTAINER_FORMAT=bare
1288 UNPACK=zcat
1289 ;;
1290 *.qcow2)
1291 IMAGE="$FILES/${IMAGE_FNAME}"
1292 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
1293 DISK_FORMAT=qcow2
1294 CONTAINER_FORMAT=bare
1295 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +01001296 *.iso)
1297 IMAGE="$FILES/${IMAGE_FNAME}"
1298 IMAGE_NAME=$(basename "$IMAGE" ".iso")
1299 DISK_FORMAT=iso
1300 CONTAINER_FORMAT=bare
1301 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -05001302 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
1303 esac
1304
1305 if [ "$CONTAINER_FORMAT" = "bare" ]; then
1306 if [ "$UNPACK" = "zcat" ]; then
Christian Berendta7a219a2013-07-30 18:22:32 +02001307 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public True --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < <(zcat --force "${IMAGE}")
Dean Troyerca0e3d02012-04-13 15:58:37 -05001308 else
Christian Berendta7a219a2013-07-30 18:22:32 +02001309 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public True --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < "${IMAGE}"
Dean Troyerca0e3d02012-04-13 15:58:37 -05001310 fi
1311 else
1312 # Use glance client to add the kernel the root filesystem.
1313 # We parse the results of the first upload to get the glance ID of the
1314 # kernel for use when uploading the root filesystem.
1315 KERNEL_ID=""; RAMDISK_ID="";
1316 if [ -n "$KERNEL" ]; then
Christian Berendta7a219a2013-07-30 18:22:32 +02001317 KERNEL_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-kernel" --is-public True --container-format aki --disk-format aki < "$KERNEL" | grep ' id ' | get_field 2)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001318 fi
1319 if [ -n "$RAMDISK" ]; then
Christian Berendta7a219a2013-07-30 18:22:32 +02001320 RAMDISK_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-ramdisk" --is-public True --container-format ari --disk-format ari < "$RAMDISK" | grep ' id ' | get_field 2)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001321 fi
Christian Berendta7a219a2013-07-30 18:22:32 +02001322 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "${IMAGE_NAME%.img}" --is-public True --container-format ami --disk-format ami ${KERNEL_ID:+--property kernel_id=$KERNEL_ID} ${RAMDISK_ID:+--property ramdisk_id=$RAMDISK_ID} < "${IMAGE}"
Dean Troyerca0e3d02012-04-13 15:58:37 -05001323 fi
1324}
1325
Dean Troyer1a6d4492013-06-03 16:47:36 -05001326
Dean Troyerc1b486a2012-11-05 14:26:09 -06001327# Set the database backend to use
1328# When called from stackrc/localrc DATABASE_BACKENDS has not been
1329# initialized yet, just save the configuration selection and call back later
1330# to validate it.
1331# $1 The name of the database backend to use (mysql, postgresql, ...)
1332function use_database {
1333 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -06001334 # No backends registered means this is likely called from ``localrc``
1335 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -06001336 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +00001337 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 +01001338 else
Dean Troyerafc29fe2013-02-07 15:56:24 -06001339 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +01001340 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -06001341 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -06001342}
1343
Dean Troyer1a6d4492013-06-03 16:47:36 -05001344
Terry Wilson428af5a2012-11-01 16:12:39 -04001345# Toggle enable/disable_service for services that must run exclusive of each other
1346# $1 The name of a variable containing a space-separated list of services
1347# $2 The name of a variable in which to store the enabled service's name
1348# $3 The name of the service to enable
1349function use_exclusive_service {
1350 local options=${!1}
1351 local selection=$3
1352 out=$2
1353 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1354 for opt in $options;do
1355 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1356 done
1357 eval "$out=$selection"
1358 return 0
1359}
Dean Troyerca0e3d02012-04-13 15:58:37 -05001360
Dean Troyer1a6d4492013-06-03 16:47:36 -05001361
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001362# Wait for an HTTP server to start answering requests
1363# wait_for_service timeout url
1364function wait_for_service() {
1365 local timeout=$1
1366 local url=$2
1367 timeout $timeout sh -c "while ! http_proxy= https_proxy= curl -s $url >/dev/null; do sleep 1; done"
1368}
1369
Dean Troyer1a6d4492013-06-03 16:47:36 -05001370
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001371# Wrapper for ``yum`` to set proxy environment variables
1372# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001373# yum_install package [package ...]
1374function yum_install() {
1375 [[ "$OFFLINE" = "True" ]] && return
1376 local sudo="sudo"
1377 [[ "$(id -u)" = "0" ]] && sudo="env"
1378 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001379 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001380 yum install -y "$@"
1381}
1382
Dean Troyer1a6d4492013-06-03 16:47:36 -05001383
1384# zypper wrapper to set arguments correctly
1385# zypper_install package [package ...]
1386function zypper_install() {
1387 [[ "$OFFLINE" = "True" ]] && return
1388 local sudo="sudo"
1389 [[ "$(id -u)" = "0" ]] && sudo="env"
1390 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1391 zypper --non-interactive install --auto-agree-with-licenses "$@"
1392}
1393
1394
Nachi Uenofda946e2012-10-24 17:26:02 -07001395# ping check
1396# Uses globals ``ENABLED_SERVICES``
Dean Troyer1a6d4492013-06-03 16:47:36 -05001397# ping_check from-net ip boot-timeout expected
Nachi Uenofda946e2012-10-24 17:26:02 -07001398function ping_check() {
Mark McClainb05c8762013-07-06 23:29:39 -04001399 if is_service_enabled neutron; then
1400 _ping_check_neutron "$1" $2 $3 $4
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001401 return
1402 fi
1403 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -07001404}
1405
1406# ping check for nova
1407# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
1408function _ping_check_novanet() {
1409 local from_net=$1
1410 local ip=$2
1411 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001412 local expected=${4:-"True"}
1413 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001414 MULTI_HOST=`trueorfalse False $MULTI_HOST`
1415 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
1416 sleep $boot_timeout
1417 return
1418 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001419 if [[ "$expected" = "True" ]]; then
1420 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
1421 else
1422 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
1423 fi
1424 if ! timeout $boot_timeout sh -c "$check_command"; then
1425 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -08001426 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001427 else
Nachi Ueno07115eb2013-02-26 12:38:18 -08001428 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001429 fi
Nachi Uenofda946e2012-10-24 17:26:02 -07001430 exit 1
1431 fi
1432}
1433
Dean Troyer1a6d4492013-06-03 16:47:36 -05001434
Nachi Uenofda946e2012-10-24 17:26:02 -07001435# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001436
Dean Troyer1a6d4492013-06-03 16:47:36 -05001437# ssh_check net-name key-file floating-ip default-user active-timeout
Nachi Uenofda946e2012-10-24 17:26:02 -07001438function ssh_check() {
Mark McClainb05c8762013-07-06 23:29:39 -04001439 if is_service_enabled neutron; then
1440 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001441 return
1442 fi
1443 _ssh_check_novanet "$1" $2 $3 $4 $5
1444}
1445
1446function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -07001447 local NET_NAME=$1
1448 local KEY_FILE=$2
1449 local FLOATING_IP=$3
1450 local DEFAULT_INSTANCE_USER=$4
1451 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -06001452 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -05001453 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 -08001454 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -07001455 fi
1456}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001457
Vincent Untz856a11e2012-11-21 16:04:12 +01001458
Vincent Untz856a11e2012-11-21 16:04:12 +01001459# Add a user to a group.
1460# add_user_to_group user group
1461function add_user_to_group() {
1462 local user=$1
1463 local group=$2
1464
1465 if [[ -z "$os_VENDOR" ]]; then
1466 GetOSVersion
1467 fi
1468
1469 # SLE11 and openSUSE 12.2 don't have the usual usermod
1470 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1471 sudo usermod -a -G "$group" "$user"
1472 else
1473 sudo usermod -A "$group" "$user"
1474 fi
1475}
1476
1477
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001478# Get the path to the direcotry where python executables are installed.
1479# get_python_exec_prefix
1480function get_python_exec_prefix() {
Martin Vidner4f9b33d2013-06-27 13:11:22 +00001481 if is_fedora || is_suse; then
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001482 echo "/usr/bin"
1483 else
1484 echo "/usr/local/bin"
1485 fi
1486}
1487
Dean Troyer1a6d4492013-06-03 16:47:36 -05001488
Vincent Untz856a11e2012-11-21 16:04:12 +01001489# Get the location of the $module-rootwrap executables, where module is cinder
1490# or nova.
1491# get_rootwrap_location module
1492function get_rootwrap_location() {
1493 local module=$1
1494
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001495 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +01001496}
1497
Dean Troyer1a6d4492013-06-03 16:47:36 -05001498
Vincent Untz8ec27222012-11-29 09:25:31 +01001499# Get the path to the pip command.
1500# get_pip_command
1501function get_pip_command() {
Dean Troyerd2cfcaa2013-08-01 14:17:27 -05001502 which pip || which pip-python
Ian Wienand535a8142013-05-15 09:25:27 +10001503
1504 if [ $? -ne 0 ]; then
1505 die $LINENO "Unable to find pip; cannot continue"
1506 fi
Vincent Untz8ec27222012-11-29 09:25:31 +01001507}
Vincent Untz856a11e2012-11-21 16:04:12 +01001508
Dean Troyer1a6d4492013-06-03 16:47:36 -05001509
Ian Wienand0488edd2013-04-11 12:04:36 +10001510# Path permissions sanity check
1511# check_path_perm_sanity path
1512function check_path_perm_sanity() {
1513 # Ensure no element of the path has 0700 permissions, which is very
1514 # likely to cause issues for daemons. Inspired by default 0700
1515 # homedir permissions on RHEL and common practice of making DEST in
1516 # the stack user's homedir.
1517
1518 local real_path=$(readlink -f $1)
1519 local rebuilt_path=""
1520 for i in $(echo ${real_path} | tr "/" " "); do
1521 rebuilt_path=$rebuilt_path"/"$i
1522
1523 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
1524 echo "*** DEST path element"
1525 echo "*** ${rebuilt_path}"
1526 echo "*** appears to have 0700 permissions."
1527 echo "*** This is very likely to cause fatal issues for devstack daemons."
1528
1529 if [[ -n "$SKIP_PATH_SANITY" ]]; then
1530 return
1531 else
1532 echo "*** Set SKIP_PATH_SANITY to skip this check"
1533 die $LINENO "Invalid path permissions"
1534 fi
1535 fi
1536 done
1537}
1538
Dean Troyer1a6d4492013-06-03 16:47:36 -05001539
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001540# This function recursively compares versions, and is not meant to be
1541# called by anything other than vercmp_numbers below. This function does
1542# not work with alphabetic versions.
1543#
1544# _vercmp_r sep ver1 ver2
1545function _vercmp_r {
1546 typeset sep
1547 typeset -a ver1=() ver2=()
1548 sep=$1; shift
1549 ver1=("${@:1:sep}")
1550 ver2=("${@:sep+1}")
1551
1552 if ((ver1 > ver2)); then
1553 echo 1; return 0
1554 elif ((ver2 > ver1)); then
1555 echo -1; return 0
1556 fi
1557
1558 if ((sep <= 1)); then
1559 echo 0; return 0
1560 fi
1561
1562 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
1563}
1564
1565
1566# This function compares two versions and is meant to be called by
1567# external callers. Please note the function assumes non-alphabetic
1568# versions. For example, this will work:
1569#
1570# vercmp_numbers 1.10 1.4
1571#
1572# The above will return "1", as 1.10 is greater than 1.4.
1573#
1574# vercmp_numbers 5.2 6.4
1575#
1576# The above will return "-1", as 5.2 is less than 6.4.
1577#
1578# vercmp_numbers 4.0 4.0
1579#
1580# The above will return "0", as the versions are equal.
1581#
1582# vercmp_numbers ver1 ver2
1583vercmp_numbers() {
1584 typeset v1=$1 v2=$2 sep
1585 typeset -a ver1 ver2
1586
1587 IFS=. read -ra ver1 <<< "$v1"
1588 IFS=. read -ra ver2 <<< "$v2"
1589
1590 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
1591}
1592
1593
Dean Troyer27e32692012-03-16 16:16:56 -05001594# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +00001595$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001596
1597
1598# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -04001599# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -04001600# End: