blob: 87586eb17c7c81740c4a686b1c2ba27557ba0eb4 [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
Kui Shi5e28a3e2013-08-02 17:26:28 +080079# Prints backtrace info
80# filename:lineno:function
81function backtrace {
82 local level=$1
83 local deep=$((${#BASH_SOURCE[@]} - 1))
84 echo "[Call Trace]"
85 while [ $level -le $deep ]; do
86 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
87 deep=$((deep - 1))
88 done
89}
90
91
Dean Troyerac93efb2013-03-13 14:30:54 -050092# Prints line number and "message" then exits
93# die $LINENO "message"
Dean Troyer27e32692012-03-16 16:16:56 -050094function die() {
Dean Troyer489bd2a2012-03-02 10:44:29 -060095 local exitcode=$?
Dean Troyer896eb662013-04-05 15:02:01 -050096 set +o xtrace
97 local line=$1; shift
Nachi Ueno07115eb2013-02-26 12:38:18 -080098 if [ $exitcode == 0 ]; then
99 exitcode=1
100 fi
Kui Shi5e28a3e2013-08-02 17:26:28 +0800101 backtrace 2
Dean Troyer896eb662013-04-05 15:02:01 -0500102 err $line "$*"
Dean Troyer27e32692012-03-16 16:16:56 -0500103 exit $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -0600104}
105
106
107# Checks an environment variable is not set or has length 0 OR if the
108# exit code is non-zero and prints "message" and exits
109# NOTE: env-var is the variable name without a '$'
Dean Troyerac93efb2013-03-13 14:30:54 -0500110# die_if_not_set $LINENO env-var "message"
Dean Troyer489bd2a2012-03-02 10:44:29 -0600111function die_if_not_set() {
Dean Troyer896eb662013-04-05 15:02:01 -0500112 local exitcode=$?
113 FXTRACE=$(set +o | grep xtrace)
114 set +o xtrace
115 local line=$1; shift
116 local evar=$1; shift
117 if ! is_set $evar || [ $exitcode != 0 ]; then
118 die $line "$*"
119 fi
120 $FXTRACE
121}
122
123
124# Prints line number and "message" in error format
125# err $LINENO "message"
126function err() {
127 local exitcode=$?
128 errXTRACE=$(set +o | grep xtrace)
129 set +o xtrace
Kui Shi17df0772013-08-02 17:55:41 +0800130 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
Dean Troyer896eb662013-04-05 15:02:01 -0500131 echo $msg 1>&2;
132 if [[ -n ${SCREEN_LOGDIR} ]]; then
133 echo $msg >> "${SCREEN_LOGDIR}/error.log"
134 fi
135 $errXTRACE
136 return $exitcode
137}
138
139
140# Checks an environment variable is not set or has length 0 OR if the
141# exit code is non-zero and prints "message"
142# NOTE: env-var is the variable name without a '$'
143# err_if_not_set $LINENO env-var "message"
144function err_if_not_set() {
145 local exitcode=$?
146 errinsXTRACE=$(set +o | grep xtrace)
147 set +o xtrace
148 local line=$1; shift
149 local evar=$1; shift
150 if ! is_set $evar || [ $exitcode != 0 ]; then
151 err $line "$*"
152 fi
153 $errinsXTRACE
154 return $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -0600155}
156
157
Dean Troyer893e6632013-09-13 15:05:51 -0500158# Prints line number and "message" in warning format
159# warn $LINENO "message"
160function warn() {
161 local exitcode=$?
162 errXTRACE=$(set +o | grep xtrace)
163 set +o xtrace
164 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
165 echo $msg 1>&2;
166 if [[ -n ${SCREEN_LOGDIR} ]]; then
167 echo $msg >> "${SCREEN_LOGDIR}/error.log"
168 fi
169 $errXTRACE
170 return $exitcode
171}
172
173
Dean Troyer48352ee2012-12-12 12:50:38 -0600174# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
175# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
176# ``localrc`` or on the command line if necessary::
177#
178# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
179#
180# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
181
182function export_proxy_variables() {
183 if [[ -n "$http_proxy" ]]; then
184 export http_proxy=$http_proxy
185 fi
186 if [[ -n "$https_proxy" ]]; then
187 export https_proxy=$https_proxy
188 fi
189 if [[ -n "$no_proxy" ]]; then
190 export no_proxy=$no_proxy
191 fi
192}
193
194
Dean Troyer489bd2a2012-03-02 10:44:29 -0600195# Grab a numbered field from python prettytable output
196# Fields are numbered starting with 1
197# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
198# get_field field-number
199function get_field() {
200 while read data; do
201 if [ "$1" -lt 0 ]; then
202 field="(\$(NF$1))"
203 else
204 field="\$$(($1 + 1))"
205 fi
206 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
207 done
208}
209
210
Dean Troyerc892bde2013-03-13 14:06:13 -0500211# Get the default value for HOST_IP
212# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
213function get_default_host_ip() {
214 local fixed_range=$1
215 local floating_range=$2
216 local host_ip_iface=$3
217 local host_ip=$4
218
219 # Find the interface used for the default route
220 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
221 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
222 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
223 host_ip=""
224 host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}'`
225 for IP in $host_ips; do
226 # Attempt to filter out IP addresses that are part of the fixed and
227 # floating range. Note that this method only works if the ``netaddr``
228 # python library is installed. If it is not installed, an error
229 # will be printed and the first IP from the interface will be used.
230 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
231 # address.
232 if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then
233 host_ip=$IP
234 break;
235 fi
236 done
237 fi
238 echo $host_ip
239}
240
241
Isaku Yamahata8c438092013-02-12 22:30:56 +0900242function _get_package_dir() {
243 local pkg_dir
244 if is_ubuntu; then
245 pkg_dir=$FILES/apts
246 elif is_fedora; then
247 pkg_dir=$FILES/rpms
248 elif is_suse; then
249 pkg_dir=$FILES/rpms-suse
250 else
251 exit_distro_not_supported "list of packages"
252 fi
253 echo "$pkg_dir"
254}
255
Dean Troyer1a6d4492013-06-03 16:47:36 -0500256
Dean Troyer7e270512012-06-14 15:23:24 -0500257# get_packages() collects a list of package names of any type from the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500258# prerequisite files in ``files/{apts|rpms}``. The list is intended
259# to be passed to a package installer such as apt or yum.
Dean Troyer7e270512012-06-14 15:23:24 -0500260#
Isaku Yamahata8c438092013-02-12 22:30:56 +0900261# Only packages required for the services in 1st argument will be
Dean Troyer7e270512012-06-14 15:23:24 -0500262# included. Two bits of metadata are recognized in the prerequisite files:
263# - ``# NOPRIME`` defers installation to be performed later in stack.sh
264# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
265# of the package to the distros listed. The distro names are case insensitive.
Dean Troyer7e270512012-06-14 15:23:24 -0500266function get_packages() {
Isaku Yamahata8c438092013-02-12 22:30:56 +0900267 local services=$1
268 local package_dir=$(_get_package_dir)
Dean Troyer7e270512012-06-14 15:23:24 -0500269 local file_to_parse
270 local service
271
272 if [[ -z "$package_dir" ]]; then
273 echo "No package directory supplied"
274 return 1
275 fi
276 if [[ -z "$DISTRO" ]]; then
Vincent Untz855c5872012-10-04 13:36:46 +0200277 GetDistro
Dean Troyer7e270512012-06-14 15:23:24 -0500278 fi
Isaku Yamahata8c438092013-02-12 22:30:56 +0900279 for service in general ${services//,/ }; do
Dean Troyer7e270512012-06-14 15:23:24 -0500280 # Allow individual services to specify dependencies
281 if [[ -e ${package_dir}/${service} ]]; then
282 file_to_parse="${file_to_parse} $service"
283 fi
284 # NOTE(sdague) n-api needs glance for now because that's where
285 # glance client is
286 if [[ $service == n-api ]]; then
287 if [[ ! $file_to_parse =~ nova ]]; then
288 file_to_parse="${file_to_parse} nova"
289 fi
290 if [[ ! $file_to_parse =~ glance ]]; then
291 file_to_parse="${file_to_parse} glance"
292 fi
293 elif [[ $service == c-* ]]; then
294 if [[ ! $file_to_parse =~ cinder ]]; then
295 file_to_parse="${file_to_parse} cinder"
296 fi
John H. Tran93361642012-07-26 11:22:05 -0700297 elif [[ $service == ceilometer-* ]]; then
298 if [[ ! $file_to_parse =~ ceilometer ]]; then
299 file_to_parse="${file_to_parse} ceilometer"
300 fi
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100301 elif [[ $service == s-* ]]; then
302 if [[ ! $file_to_parse =~ swift ]]; then
303 file_to_parse="${file_to_parse} swift"
304 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500305 elif [[ $service == n-* ]]; then
306 if [[ ! $file_to_parse =~ nova ]]; then
307 file_to_parse="${file_to_parse} nova"
308 fi
309 elif [[ $service == g-* ]]; then
310 if [[ ! $file_to_parse =~ glance ]]; then
311 file_to_parse="${file_to_parse} glance"
312 fi
313 elif [[ $service == key* ]]; then
314 if [[ ! $file_to_parse =~ keystone ]]; then
315 file_to_parse="${file_to_parse} keystone"
316 fi
Robert Collins0a9954f2012-11-20 11:34:25 +1300317 elif [[ $service == q-* ]]; then
Mark McClainb05c8762013-07-06 23:29:39 -0400318 if [[ ! $file_to_parse =~ neutron ]]; then
319 file_to_parse="${file_to_parse} neutron"
Robert Collins0a9954f2012-11-20 11:34:25 +1300320 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500321 fi
322 done
323
324 for file in ${file_to_parse}; do
325 local fname=${package_dir}/${file}
326 local OIFS line package distros distro
327 [[ -e $fname ]] || continue
328
329 OIFS=$IFS
330 IFS=$'\n'
331 for line in $(<${fname}); do
332 if [[ $line =~ "NOPRIME" ]]; then
333 continue
334 fi
335
Christian Berendt71d56302013-07-22 11:37:42 +0200336 # Assume we want this package
337 package=${line%#*}
338 inst_pkg=1
339
340 # Look for # dist:xxx in comment
Dean Troyer7e270512012-06-14 15:23:24 -0500341 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
342 # We are using BASH regexp matching feature.
343 package=${BASH_REMATCH[1]}
344 distros=${BASH_REMATCH[2]}
345 # In bash ${VAR,,} will lowecase VAR
Christian Berendt71d56302013-07-22 11:37:42 +0200346 # Look for a match in the distro list
347 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
348 # If no match then skip this package
349 inst_pkg=0
350 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500351 fi
352
Christian Berendt71d56302013-07-22 11:37:42 +0200353 # Look for # testonly in comment
354 if [[ $line =~ (.*)#.*testonly.* ]]; then
355 package=${BASH_REMATCH[1]}
356 # Are we installing test packages? (test for the default value)
357 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
358 # If not installing test packages the skip this package
359 inst_pkg=0
360 fi
361 fi
362
363 if [[ $inst_pkg = 1 ]]; then
364 echo $package
365 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500366 done
367 IFS=$OIFS
368 done
369}
370
371
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500372# Determine OS Vendor, Release and Update
373# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
374# Returns results in global variables:
375# os_VENDOR - vendor name
376# os_RELEASE - release
377# os_UPDATE - update
378# os_PACKAGE - package type
379# os_CODENAME - vendor's codename for release
380# GetOSVersion
381GetOSVersion() {
382 # Figure out which vendor we are
383 if [[ -n "`which sw_vers 2>/dev/null`" ]]; then
384 # OS/X
385 os_VENDOR=`sw_vers -productName`
386 os_RELEASE=`sw_vers -productVersion`
387 os_UPDATE=${os_RELEASE##*.}
388 os_RELEASE=${os_RELEASE%.*}
389 os_PACKAGE=""
390 if [[ "$os_RELEASE" =~ "10.7" ]]; then
391 os_CODENAME="lion"
392 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
393 os_CODENAME="snow leopard"
394 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
395 os_CODENAME="leopard"
396 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
397 os_CODENAME="tiger"
398 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
399 os_CODENAME="panther"
400 else
401 os_CODENAME=""
402 fi
403 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
404 os_VENDOR=$(lsb_release -i -s)
405 os_RELEASE=$(lsb_release -r -s)
406 os_UPDATE=""
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100407 os_PACKAGE="rpm"
Derek Morton4a8496e2013-04-08 23:46:08 -0500408 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500409 os_PACKAGE="deb"
Vincent Untz856a11e2012-11-21 16:04:12 +0100410 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
411 lsb_release -d -s | grep -q openSUSE
412 if [[ $? -eq 0 ]]; then
413 os_VENDOR="openSUSE"
414 fi
Vincent Untzcd1fe982013-03-12 18:04:29 +0100415 elif [[ $os_VENDOR == "openSUSE project" ]]; then
416 os_VENDOR="openSUSE"
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100417 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
418 os_VENDOR="Red Hat"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500419 fi
420 os_CODENAME=$(lsb_release -c -s)
421 elif [[ -r /etc/redhat-release ]]; then
422 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
423 # CentOS release 5.5 (Final)
424 # CentOS Linux release 6.0 (Final)
425 # Fedora release 16 (Verne)
Bob Ball46691222013-08-12 17:28:50 +0100426 # XenServer release 6.2.0-70446c (xenenterprise)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500427 os_CODENAME=""
Bob Ball46691222013-08-12 17:28:50 +0100428 for r in "Red Hat" CentOS Fedora XenServer; do
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500429 os_VENDOR=$r
430 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
431 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
432 os_CODENAME=${ver#*|}
433 os_RELEASE=${ver%|*}
434 os_UPDATE=${os_RELEASE##*.}
435 os_RELEASE=${os_RELEASE%.*}
436 break
437 fi
438 os_VENDOR=""
439 done
440 os_PACKAGE="rpm"
Vincent Untz856a11e2012-11-21 16:04:12 +0100441 elif [[ -r /etc/SuSE-release ]]; then
442 for r in openSUSE "SUSE Linux"; do
443 if [[ "$r" = "SUSE Linux" ]]; then
444 os_VENDOR="SUSE LINUX"
445 else
446 os_VENDOR=$r
447 fi
448
449 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
450 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
451 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
452 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
453 break
454 fi
455 os_VENDOR=""
456 done
457 os_PACKAGE="rpm"
Émilien Macchib2ef8902013-05-04 00:48:20 +0200458 # If lsb_release is not installed, we should be able to detect Debian OS
459 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
460 os_VENDOR="Debian"
461 os_PACKAGE="deb"
462 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
463 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500464 fi
465 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
466}
467
Andrew Laskif900bd72012-09-05 17:23:14 -0400468
Dean Troyera9e0a482012-07-09 14:07:23 -0500469# Translate the OS version values into common nomenclature
470# Sets ``DISTRO`` from the ``os_*`` values
471function GetDistro() {
472 GetOSVersion
Émilien Macchib2ef8902013-05-04 00:48:20 +0200473 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
474 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
Dean Troyera9e0a482012-07-09 14:07:23 -0500475 DISTRO=$os_CODENAME
476 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
477 # For Fedora, just use 'f' and the release
478 DISTRO="f$os_RELEASE"
Vincent Untz856a11e2012-11-21 16:04:12 +0100479 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
480 DISTRO="opensuse-$os_RELEASE"
481 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
482 # For SLE, also use the service pack
483 if [[ -z "$os_UPDATE" ]]; then
484 DISTRO="sle${os_RELEASE}"
485 else
486 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
487 fi
Ian Wienandd857f4b2013-03-20 14:51:06 +1100488 elif [[ "$os_VENDOR" =~ (Red Hat) || "$os_VENDOR" =~ (CentOS) ]]; then
489 # Drop the . release as we assume it's compatible
490 DISTRO="rhel${os_RELEASE::1}"
Bob Ball46691222013-08-12 17:28:50 +0100491 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
492 DISTRO="xs$os_RELEASE"
Dean Troyera9e0a482012-07-09 14:07:23 -0500493 else
494 # Catch-all for now is Vendor + Release + Update
495 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
496 fi
497 export DISTRO
498}
499
500
Vincent Untz00011c02012-12-06 09:56:32 +0100501# Determine if current distribution is a Fedora-based distribution
Dean Troyer1a6d4492013-06-03 16:47:36 -0500502# (Fedora, RHEL, CentOS, etc).
Vincent Untz00011c02012-12-06 09:56:32 +0100503# is_fedora
504function is_fedora {
505 if [[ -z "$os_VENDOR" ]]; then
506 GetOSVersion
507 fi
508
509 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ]
510}
511
Dean Troyer1a6d4492013-06-03 16:47:36 -0500512
Vincent Untz856a11e2012-11-21 16:04:12 +0100513# Determine if current distribution is a SUSE-based distribution
514# (openSUSE, SLE).
515# is_suse
516function is_suse {
517 if [[ -z "$os_VENDOR" ]]; then
518 GetOSVersion
519 fi
520
Steve Baker1a7bbd22012-12-03 17:04:02 +1300521 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
Vincent Untz856a11e2012-11-21 16:04:12 +0100522}
523
524
Dean Troyer1a6d4492013-06-03 16:47:36 -0500525# Determine if current distribution is an Ubuntu-based distribution
526# It will also detect non-Ubuntu but Debian-based distros
527# is_ubuntu
528function is_ubuntu {
529 if [[ -z "$os_PACKAGE" ]]; then
530 GetOSVersion
531 fi
532 [ "$os_PACKAGE" = "deb" ]
533}
534
535
Vincent Untz00011c02012-12-06 09:56:32 +0100536# Exit after outputting a message about the distribution not being supported.
537# exit_distro_not_supported [optional-string-telling-what-is-missing]
538function exit_distro_not_supported {
539 if [[ -z "$DISTRO" ]]; then
540 GetDistro
541 fi
542
543 if [ $# -gt 0 ]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800544 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
Vincent Untz00011c02012-12-06 09:56:32 +0100545 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800546 die $LINENO "Support for $DISTRO is incomplete."
Vincent Untz00011c02012-12-06 09:56:32 +0100547 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100548}
549
Daniel Jonesfa868cb2013-06-18 15:28:01 -0500550# Utility function for checking machine architecture
551# is_arch arch-type
552function is_arch {
553 ARCH_TYPE=$1
554
555 [ "($uname -m)" = "$ARCH_TYPE" ]
556}
Vincent Untz00011c02012-12-06 09:56:32 +0100557
Dean Troyer7f9aa712012-01-31 12:11:56 -0600558# git clone only if directory doesn't exist already. Since ``DEST`` might not
559# be owned by the installation user, we create the directory and change the
560# ownership to the proper user.
561# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700562# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
563# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500564# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600565# git_clone remote dest-dir branch
566function git_clone {
567 [[ "$OFFLINE" = "True" ]] && return
568
569 GIT_REMOTE=$1
570 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300571 GIT_REF=$3
Dean Troyer7f9aa712012-01-31 12:11:56 -0600572
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300573 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600574 # If our branch name is a gerrit style refs/changes/...
575 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700576 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600577 git clone $GIT_REMOTE $GIT_DEST
578 fi
579 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300580 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600581 else
582 # do a full clone only if the directory doesn't exist
583 if [[ ! -d $GIT_DEST ]]; then
James E. Blair94cb9602012-06-22 15:28:29 -0700584 [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600585 git clone $GIT_REMOTE $GIT_DEST
586 cd $GIT_DEST
587 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300588 git checkout $GIT_REF
Dean Troyer7f9aa712012-01-31 12:11:56 -0600589 elif [[ "$RECLONE" == "yes" ]]; then
590 # if it does exist then simulate what clone does if asked to RECLONE
591 cd $GIT_DEST
592 # set the url to pull from and fetch
593 git remote set-url origin $GIT_REMOTE
594 git fetch origin
595 # remove the existing ignored files (like pyc) as they cause breakage
596 # (due to the py files having older timestamps than our pyc, so python
597 # thinks the pyc files are correct using them)
598 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300599
600 # handle GIT_REF accordingly to type (tag, branch)
601 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
602 git_update_tag $GIT_REF
603 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
604 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400605 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
606 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300607 else
608 echo $GIT_REF is neither branch nor tag
609 exit 1
610 fi
611
Dean Troyer7f9aa712012-01-31 12:11:56 -0600612 fi
613 fi
614}
615
616
Dean Troyer1a6d4492013-06-03 16:47:36 -0500617# git update using reference as a branch.
618# git_update_branch ref
619function git_update_branch() {
620
621 GIT_BRANCH=$1
622
623 git checkout -f origin/$GIT_BRANCH
624 # a local branch might not exist
625 git branch -D $GIT_BRANCH || true
626 git checkout -b $GIT_BRANCH
627}
628
629
630# git update using reference as a branch.
631# git_update_remote_branch ref
632function git_update_remote_branch() {
633
634 GIT_BRANCH=$1
635
636 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
637}
638
639
640# git update using reference as a tag. Be careful editing source at that repo
641# as working copy will be in a detached mode
642# git_update_tag ref
643function git_update_tag() {
644
645 GIT_TAG=$1
646
647 git tag -d $GIT_TAG
648 # fetching given tag only
649 git fetch origin tag $GIT_TAG
650 git checkout -f $GIT_TAG
651}
652
653
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500654# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200655# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500656function inicomment() {
657 local file=$1
658 local section=$2
659 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100660 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500661}
662
Dean Troyer896eb662013-04-05 15:02:01 -0500663
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200664# Uncomment an option in an INI file
665# iniuncomment config-file section option
666function iniuncomment() {
667 local file=$1
668 local section=$2
669 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100670 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200671}
672
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500673
674# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500675# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500676function iniget() {
677 local file=$1
678 local section=$2
679 local option=$3
680 local line
Attila Fazekas588eb412012-12-20 10:57:16 +0100681 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500682 echo ${line#*=}
683}
684
Dean Troyer896eb662013-04-05 15:02:01 -0500685
Attila Fazekas588eb412012-12-20 10:57:16 +0100686# Determinate is the given option present in the INI file
687# ini_has_option config-file section option
688function ini_has_option() {
689 local file=$1
690 local section=$2
691 local option=$3
692 local line
693 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
694 [ -n "$line" ]
695}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500696
Dean Troyer896eb662013-04-05 15:02:01 -0500697
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500698# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500699# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500700function iniset() {
701 local file=$1
702 local section=$2
703 local option=$3
704 local value=$4
Attila Fazekas588eb412012-12-20 10:57:16 +0100705 if ! grep -q "^\[$section\]" "$file"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500706 # Add section at the end
Attila Fazekas588eb412012-12-20 10:57:16 +0100707 echo -e "\n[$section]" >>"$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500708 fi
Attila Fazekas588eb412012-12-20 10:57:16 +0100709 if ! ini_has_option "$file" "$section" "$option"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500710 # Add it
Attila Fazekas588eb412012-12-20 10:57:16 +0100711 sed -i -e "/^\[$section\]/ a\\
Dean Troyer09e636e2012-03-19 16:31:12 -0500712$option = $value
Attila Fazekas588eb412012-12-20 10:57:16 +0100713" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500714 else
715 # Replace it
Attila Fazekas588eb412012-12-20 10:57:16 +0100716 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500717 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500718}
719
Dean Troyer896eb662013-04-05 15:02:01 -0500720
Lianhao Lu239f3242013-03-01 15:54:02 +0800721# Get a multiple line option from an INI file
722# iniget_multiline config-file section option
723function iniget_multiline() {
724 local file=$1
725 local section=$2
726 local option=$3
727 local values
728 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
729 echo ${values}
730}
731
Dean Troyer896eb662013-04-05 15:02:01 -0500732
Lianhao Lu239f3242013-03-01 15:54:02 +0800733# Set a multiple line option in an INI file
734# iniset_multiline config-file section option value1 value2 valu3 ...
735function iniset_multiline() {
736 local file=$1
737 local section=$2
738 local option=$3
739 shift 3
740 local values
741 for v in $@; do
742 # The later sed command inserts each new value in the line next to
743 # the section identifier, which causes the values to be inserted in
744 # the reverse order. Do a reverse here to keep the original order.
745 values="$v ${values}"
746 done
747 if ! grep -q "^\[$section\]" "$file"; then
748 # Add section at the end
749 echo -e "\n[$section]" >>"$file"
750 else
751 # Remove old values
752 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
753 fi
754 # Add new ones
755 for v in $values; do
756 sed -i -e "/^\[$section\]/ a\\
757$option = $v
758" "$file"
759 done
760}
761
Dean Troyer896eb662013-04-05 15:02:01 -0500762
Lianhao Lu239f3242013-03-01 15:54:02 +0800763# Append a new option in an ini file without replacing the old value
764# iniadd config-file section option value1 value2 value3 ...
765function iniadd() {
766 local file=$1
767 local section=$2
768 local option=$3
769 shift 3
770 local values="$(iniget_multiline $file $section $option) $@"
771 iniset_multiline $file $section $option $values
772}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500773
Dean Troyer896eb662013-04-05 15:02:01 -0500774# Find out if a process exists by partial name.
775# is_running name
776function is_running() {
777 local name=$1
778 ps auxw | grep -v grep | grep ${name} > /dev/null
779 RC=$?
780 # some times I really hate bash reverse binary logic
781 return $RC
782}
783
784
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000785# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500786# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000787#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500788# Multiple services specified as arguments are ``OR``'ed together; the test
789# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000790#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500791# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000792# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500793# **cinder** returns true if any service enabled start with **c-**
794# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000795# **glance** returns true if any service enabled start with **g-**
Mark McClainb05c8762013-07-06 23:29:39 -0400796# **neutron** returns true if any service enabled start with **q-**
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100797# **swift** returns true if any service enabled start with **s-**
Nikhil Manchanda0cccad42012-12-03 18:15:09 -0700798# **trove** returns true if any service enabled start with **tr-**
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100799# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
800# **s-** services will be enabled. This will be deprecated in the future.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500801#
Chris Behrensc62c2b92013-07-24 03:56:13 -0700802# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
803# We also need to make sure to treat **n-cell-region** and **n-cell-child**
804# as enabled in this case.
805#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500806# Uses global ``ENABLED_SERVICES``
807# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000808function is_service_enabled() {
809 services=$@
810 for service in ${services}; do
811 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
Chris Behrensc62c2b92013-07-24 03:56:13 -0700812 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000813 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500814 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700815 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000816 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
Mark McClainb05c8762013-07-06 23:29:39 -0400817 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
Nikhil Manchanda0cccad42012-12-03 18:15:09 -0700818 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && return 0
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100819 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && return 0
820 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000821 done
822 return 1
823}
824
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500825
826# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
827# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400828function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500829 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400830 s/,,/,/g;
831 s/^,//;
832 s/,$//
833 '
834}
835
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500836
Doug Hellmannf04178f2012-07-05 17:10:03 -0400837# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500838# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400839#
840# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800841# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400842#
843# This function does not know about the special cases
Mark McClainb05c8762013-07-06 23:29:39 -0400844# for nova, glance, and neutron built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500845# Uses global ``ENABLED_SERVICES``
846# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400847function enable_service() {
848 local tmpsvcs="${ENABLED_SERVICES}"
849 for service in $@; do
850 if ! is_service_enabled $service; then
851 tmpsvcs+=",$service"
852 fi
853 done
854 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
855 disable_negated_services
856}
857
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500858
Doug Hellmannf04178f2012-07-05 17:10:03 -0400859# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500860# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400861#
862# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800863# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400864#
865# This function does not know about the special cases
Mark McClainb05c8762013-07-06 23:29:39 -0400866# for nova, glance, and neutron built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500867# Uses global ``ENABLED_SERVICES``
868# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400869function disable_service() {
870 local tmpsvcs=",${ENABLED_SERVICES},"
871 local service
872 for service in $@; do
873 if is_service_enabled $service; then
874 tmpsvcs=${tmpsvcs//,$service,/,}
875 fi
876 done
877 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
878}
879
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500880
Doug Hellmannf04178f2012-07-05 17:10:03 -0400881# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500882# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400883# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500884# Uses global ``ENABLED_SERVICES``
885# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400886function disable_all_services() {
887 ENABLED_SERVICES=""
888}
889
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500890
891# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800892# services except rabbit (rabbit) set in ``localrc``:
893# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500894# Uses global ``ENABLED_SERVICES``
895# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400896function disable_negated_services() {
897 local tmpsvcs="${ENABLED_SERVICES}"
898 local service
899 for service in ${tmpsvcs//,/ }; do
900 if [[ ${service} == -* ]]; then
901 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
902 fi
903 done
904 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
905}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600906
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500907
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500908# Distro-agnostic package installer
909# install_package package [package ...]
910function install_package() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100911 if is_ubuntu; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200912 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
913 NO_UPDATE_REPOS=True
914
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500915 apt_get install "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100916 elif is_fedora; then
917 yum_install "$@"
918 elif is_suse; then
919 zypper_install "$@"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500920 else
Vincent Untz00011c02012-12-06 09:56:32 +0100921 exit_distro_not_supported "installing packages"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500922 fi
923}
924
925
Dean Troyer995eb922013-03-07 16:11:40 -0600926# Distro-agnostic package uninstaller
927# uninstall_package package [package ...]
928function uninstall_package() {
929 if is_ubuntu; then
930 apt_get purge "$@"
931 elif is_fedora; then
Ian Wienand2c678cc2013-03-20 13:00:44 +1100932 sudo yum remove -y "$@"
Dean Troyer995eb922013-03-07 16:11:40 -0600933 elif is_suse; then
Ian Wienand2c678cc2013-03-20 13:00:44 +1100934 sudo rpm -e "$@"
Dean Troyer995eb922013-03-07 16:11:40 -0600935 else
936 exit_distro_not_supported "uninstalling packages"
937 fi
938}
939
940
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200941# Distro-agnostic function to tell if a package is installed
942# is_package_installed package [package ...]
943function is_package_installed() {
944 if [[ -z "$@" ]]; then
945 return 1
946 fi
947
948 if [[ -z "$os_PACKAGE" ]]; then
949 GetOSVersion
950 fi
Vincent Untzc18b9652012-12-04 12:36:34 +0100951
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200952 if [[ "$os_PACKAGE" = "deb" ]]; then
Dean Troyer04762cd2013-08-27 17:06:14 -0500953 dpkg -s "$@" > /dev/null 2> /dev/null
Vincent Untz00011c02012-12-06 09:56:32 +0100954 elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200955 rpm --quiet -q "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100956 else
957 exit_distro_not_supported "finding if a package is installed"
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200958 fi
959}
960
961
Dean Troyer489bd2a2012-03-02 10:44:29 -0600962# Test if the named environment variable is set and not zero length
963# is_set env-var
964function is_set() {
965 local var=\$"$1"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100966 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 -0600967}
968
969
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500970# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +0000971# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
972# ``TRACK_DEPENDS``, ``*_proxy`
Dean Troyer7f9aa712012-01-31 12:11:56 -0600973# pip_install package [package ...]
974function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -0600975 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500976 if [[ -z "$os_PACKAGE" ]]; then
977 GetOSVersion
978 fi
Dean Troyercc6b4432013-04-08 15:38:03 -0500979 if [[ $TRACK_DEPENDS = True ]]; then
Monty Taylor47f02062012-07-26 11:09:24 -0500980 source $DEST/.venv/bin/activate
981 CMD_PIP=$DEST/.venv/bin/pip
982 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500983 else
Monty Taylor47f02062012-07-26 11:09:24 -0500984 SUDO_PIP="sudo"
Vincent Untz8ec27222012-11-29 09:25:31 +0100985 CMD_PIP=$(get_pip_command)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500986 fi
Ian Wienandd67dd872013-04-11 11:14:36 +1000987
Roman Gorodeckij99405a42013-08-07 09:20:36 -0400988 # Mirror option not needed anymore because pypi has CDN available,
989 # but it's useful in certain circumstances
990 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
Maru Newby3a87edd2012-10-25 23:01:06 +0000991 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
992 PIP_MIRROR_OPT="--use-mirrors"
993 fi
Ian Wienandd67dd872013-04-11 11:14:36 +1000994
Ian Wienand31dcd3e2013-07-16 13:36:34 +1000995 # pip < 1.4 has a bug where it will use an already existing build
996 # directory unconditionally. Say an earlier component installs
997 # foo v1.1; pip will have built foo's source in
998 # /tmp/$USER-pip-build. Even if a later component specifies foo <
999 # 1.1, the existing extracted build will be used and cause
1000 # confusing errors. By creating unique build directories we avoid
1001 # this problem. See
1002 # https://github.com/pypa/pip/issues/709
1003 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1004
Monty Taylor47f02062012-07-26 11:09:24 -05001005 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -06001006 HTTP_PROXY=$http_proxy \
1007 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001008 NO_PROXY=$no_proxy \
Ian Wienand31dcd3e2013-07-16 13:36:34 +10001009 $CMD_PIP install --build=${pip_build_tmp} \
1010 $PIP_MIRROR_OPT $@ \
1011 && $SUDO_PIP rm -rf ${pip_build_tmp}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001012}
1013
1014
Ian Wienand31dcd3e2013-07-16 13:36:34 +10001015# Cleanup anything from /tmp on unstack
1016# clean_tmp
1017function cleanup_tmp {
1018 local tmp_dir=${TMPDIR:-/tmp}
1019
1020 # see comments in pip_install
1021 sudo rm -rf ${tmp_dir}/pip-build.*
1022}
1023
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001024# Service wrapper to restart services
1025# restart_service service-name
1026function restart_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001027 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001028 sudo /usr/sbin/service $1 restart
1029 else
1030 sudo /sbin/service $1 restart
1031 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001032}
1033
1034
Dean Troyer681f3fd2013-02-27 19:00:39 -06001035# _run_process() is designed to be backgrounded by run_process() to simulate a
1036# fork. It includes the dirty work of closing extra filehandles and preparing log
1037# files to produce the same logs as screen_it(). The log filename is derived
1038# from the service name and global-and-now-misnamed SCREEN_LOGDIR
1039# _run_process service "command-line"
1040function _run_process() {
1041 local service=$1
1042 local command="$2"
1043
1044 # Undo logging redirections and close the extra descriptors
1045 exec 1>&3
1046 exec 2>&3
1047 exec 3>&-
1048 exec 6>&-
1049
1050 if [[ -n ${SCREEN_LOGDIR} ]]; then
1051 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1052 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1053
1054 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1055 export PYTHONUNBUFFERED=1
1056 fi
1057
1058 exec /bin/bash -c "$command"
1059 die "$service exec failure: $command"
1060}
1061
1062
1063# run_process() launches a child process that closes all file descriptors and
1064# then exec's the passed in command. This is meant to duplicate the semantics
1065# of screen_it() without screen. PIDs are written to
1066# $SERVICE_DIR/$SCREEN_NAME/$service.pid
1067# run_process service "command-line"
1068function run_process() {
1069 local service=$1
1070 local command="$2"
1071
1072 # Spawn the child process
1073 _run_process "$service" "$command" &
1074 echo $!
1075}
1076
1077
Dean Troyer15733352012-09-06 11:51:30 -05001078# Helper to launch a service in a named screen
1079# screen_it service "command-line"
1080function screen_it {
Dean Troyer15733352012-09-06 11:51:30 -05001081 SCREEN_NAME=${SCREEN_NAME:-stack}
jiajun xua9414242012-12-06 16:30:57 +08001082 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer681f3fd2013-02-27 19:00:39 -06001083 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
jiajun xua9414242012-12-06 16:30:57 +08001084
Dean Troyer15733352012-09-06 11:51:30 -05001085 if is_service_enabled $1; then
1086 # Append the service to the screen rc file
1087 screen_rc "$1" "$2"
1088
Dean Troyer681f3fd2013-02-27 19:00:39 -06001089 if [[ "$USE_SCREEN" = "True" ]]; then
1090 screen -S $SCREEN_NAME -X screen -t $1
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +00001091
Dean Troyer681f3fd2013-02-27 19:00:39 -06001092 if [[ -n ${SCREEN_LOGDIR} ]]; then
1093 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
1094 screen -S $SCREEN_NAME -p $1 -X log on
1095 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1096 fi
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +00001097
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001098 # sleep to allow bash to be ready to be send the command - we are
1099 # creating a new window in screen and then sends characters, so if
1100 # bash isn't running by the time we send the command, nothing happens
1101 sleep 1.5
Dean Troyer15733352012-09-06 11:51:30 -05001102
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001103 NL=`echo -ne '\015'`
Clark Boylan41815cd2013-08-16 14:57:38 -07001104 screen -S $SCREEN_NAME -p $1 -X stuff "$2 || echo \"$1 failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001105 else
Dean Troyer681f3fd2013-02-27 19:00:39 -06001106 # Spawn directly without screen
1107 run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyer15733352012-09-06 11:51:30 -05001108 fi
Dean Troyer15733352012-09-06 11:51:30 -05001109 fi
1110}
1111
1112
1113# Screen rc file builder
1114# screen_rc service "command-line"
1115function screen_rc {
1116 SCREEN_NAME=${SCREEN_NAME:-stack}
1117 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1118 if [[ ! -e $SCREENRC ]]; then
1119 # Name the screen session
1120 echo "sessionname $SCREEN_NAME" > $SCREENRC
1121 # Set a reasonable statusbar
1122 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
Steven Dake30396572013-06-30 16:11:54 -07001123 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1124 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
Dean Troyer15733352012-09-06 11:51:30 -05001125 echo "screen -t shell bash" >> $SCREENRC
1126 fi
1127 # If this service doesn't already exist in the screenrc file
1128 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1129 NL=`echo -ne '\015'`
1130 echo "screen -t $1 bash" >> $SCREENRC
1131 echo "stuff \"$2$NL\"" >> $SCREENRC
1132 fi
1133}
1134
Dean Troyer1a6d4492013-06-03 16:47:36 -05001135
jiajun xua9414242012-12-06 16:30:57 +08001136# Helper to remove the *.failure files under $SERVICE_DIR/$SCREEN_NAME
1137# This is used for service_check when all the screen_it are called finished
1138# init_service_check
1139function init_service_check() {
1140 SCREEN_NAME=${SCREEN_NAME:-stack}
1141 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1142
1143 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1144 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1145 fi
1146
1147 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1148}
1149
Dean Troyer1a6d4492013-06-03 16:47:36 -05001150
jiajun xua9414242012-12-06 16:30:57 +08001151# Helper to get the status of each running service
1152# service_check
1153function service_check() {
1154 local service
1155 local failures
1156 SCREEN_NAME=${SCREEN_NAME:-stack}
1157 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1158
1159
1160 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1161 echo "No service status directory found"
1162 return
1163 fi
1164
1165 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
1166 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null`
1167
1168 for service in $failures; do
1169 service=`basename $service`
Bob Ball46287d82013-07-30 09:43:17 +01001170 service=${service%.failure}
jiajun xua9414242012-12-06 16:30:57 +08001171 echo "Error: Service $service is not running"
1172 done
1173
1174 if [ -n "$failures" ]; then
1175 echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
1176 fi
1177}
Dean Troyer15733352012-09-06 11:51:30 -05001178
Dean Troyer1a6d4492013-06-03 16:47:36 -05001179
Monty Taylor408a4a72013-08-02 15:43:47 -04001180# ``pip install -e`` the package, which processes the dependencies
1181# using pip before running `setup.py develop`
Monty Taylorb5bbaac2013-08-06 10:35:02 -03001182# Uses globals ``STACK_USER``, ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001183# setup_develop directory
1184function setup_develop() {
Sean Dague6c844632013-07-31 06:50:14 -04001185 local project_dir=$1
Dean Troyercc6b4432013-04-08 15:38:03 -05001186 if [[ $TRACK_DEPENDS = True ]]; then
Monty Taylor47f02062012-07-26 11:09:24 -05001187 SUDO_CMD="env"
1188 else
1189 SUDO_CMD="sudo"
1190 fi
Sean Dague6c844632013-07-31 06:50:14 -04001191
1192 echo "cd $REQUIREMENTS_DIR; $SUDO_CMD python update.py $project_dir"
1193
Dean Troyer62d1d692013-08-01 17:40:40 -05001194 # Don't update repo if local changes exist
1195 if (cd $project_dir && git diff --quiet); then
1196 (cd $REQUIREMENTS_DIR; \
1197 $SUDO_CMD python update.py $project_dir)
1198 fi
Sean Dague6c844632013-07-31 06:50:14 -04001199
Monty Taylorb5bbaac2013-08-06 10:35:02 -03001200 pip_install -e $project_dir
1201 # ensure that further actions can do things like setup.py sdist
1202 $SUDO_CMD chown -R $STACK_USER $1/*.egg-info
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001203}
1204
1205
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001206# Service wrapper to start services
1207# start_service service-name
1208function start_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001209 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001210 sudo /usr/sbin/service $1 start
1211 else
1212 sudo /sbin/service $1 start
1213 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001214}
1215
1216
1217# Service wrapper to stop services
1218# stop_service service-name
1219function stop_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001220 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001221 sudo /usr/sbin/service $1 stop
1222 else
1223 sudo /sbin/service $1 stop
1224 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -06001225}
1226
1227
1228# Normalize config values to True or False
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001229# Accepts as False: 0 no false False FALSE
1230# Accepts as True: 1 yes true True TRUE
1231# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -06001232function trueorfalse() {
1233 local default=$1
1234 local testval=$2
1235
1236 [[ -z "$testval" ]] && { echo "$default"; return; }
1237 [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
1238 [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
1239 echo "$default"
1240}
Dean Troyer27e32692012-03-16 16:16:56 -05001241
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001242
Dean Troyerca0e3d02012-04-13 15:58:37 -05001243# Retrieve an image from a URL and upload into Glance
1244# Uses the following variables:
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001245# ``FILES`` must be set to the cache dir
1246# ``GLANCE_HOSTPORT``
Dean Troyerca0e3d02012-04-13 15:58:37 -05001247# upload_image image-url glance-token
1248function upload_image() {
1249 local image_url=$1
1250 local token=$2
1251
1252 # Create a directory for the downloaded image tarballs.
1253 mkdir -p $FILES/images
1254
1255 # Downloads the image (uec ami+aki style), then extracts it.
1256 IMAGE_FNAME=`basename "$image_url"`
1257 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
1258 wget -c $image_url -O $FILES/$IMAGE_FNAME
1259 if [[ $? -ne 0 ]]; then
1260 echo "Not found: $image_url"
1261 return
1262 fi
1263 fi
1264
1265 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
1266 if [[ "$image_url" =~ 'openvz' ]]; then
1267 IMAGE="$FILES/${IMAGE_FNAME}"
1268 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
1269 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}"
1270 return
1271 fi
1272
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -07001273 # vmdk format images
1274 if [[ "$image_url" =~ '.vmdk' ]]; then
1275 IMAGE="$FILES/${IMAGE_FNAME}"
1276 IMAGE_NAME="${IMAGE_FNAME%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -07001277
1278 # Before we can upload vmdk type images to glance, we need to know it's
1279 # disk type, storage adapter, and networking adapter. These values are
1280 # passed to glance as custom properties. We take these values from the
1281 # vmdk filename, which is expected in the following format:
1282 #
1283 # <name>-<disk type>:<storage adapter>:<network adapter>
1284 #
1285 # If the filename does not follow the above format then the vsphere
1286 # driver will supply default values.
1287 property_string=`echo "$IMAGE_NAME" | grep -oP '(?<=-)(?!.*-).+:.+:.+$'`
1288 if [[ ! -z "$property_string" ]]; then
1289 IFS=':' read -a props <<< "$property_string"
1290 vmdk_disktype="${props[0]}"
1291 vmdk_adapter_type="${props[1]}"
1292 vmdk_net_adapter="${props[2]}"
1293 fi
1294
1295 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="$vmdk_disktype" --property vmware_adaptertype="$vmdk_adapter_type" --property hw_vif_model="$vmdk_net_adapter" < "${IMAGE}"
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -07001296 return
1297 fi
1298
Mate Lakatbc2ef922013-08-15 18:06:59 +01001299 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -05001300 # and should not be decompressed prior to loading
1301 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
1302 IMAGE="$FILES/${IMAGE_FNAME}"
1303 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
1304 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}"
1305 return
1306 fi
1307
Mate Lakatbc2ef922013-08-15 18:06:59 +01001308 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
1309 # and should not be decompressed prior to loading.
1310 # Setting metadata, so PV mode is used.
1311 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
1312 IMAGE="$FILES/${IMAGE_FNAME}"
1313 IMAGE_NAME="${IMAGE_FNAME%.xen-raw.tgz}"
1314 glance \
1315 --os-auth-token $token \
1316 --os-image-url http://$GLANCE_HOSTPORT \
1317 image-create \
1318 --name "$IMAGE_NAME" --is-public=True \
1319 --container-format=tgz --disk-format=raw \
1320 --property vm_mode=xen < "${IMAGE}"
1321 return
1322 fi
1323
Dean Troyerca0e3d02012-04-13 15:58:37 -05001324 KERNEL=""
1325 RAMDISK=""
1326 DISK_FORMAT=""
1327 CONTAINER_FORMAT=""
1328 UNPACK=""
1329 case "$IMAGE_FNAME" in
1330 *.tar.gz|*.tgz)
1331 # Extract ami and aki files
1332 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
1333 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
1334 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
1335 xdir="$FILES/images/$IMAGE_NAME"
1336 rm -Rf "$xdir";
1337 mkdir "$xdir"
1338 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
1339 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
1340 [ -f "$f" ] && echo "$f" && break; done; true)
1341 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
1342 [ -f "$f" ] && echo "$f" && break; done; true)
1343 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
1344 [ -f "$f" ] && echo "$f" && break; done; true)
1345 if [[ -z "$IMAGE_NAME" ]]; then
1346 IMAGE_NAME=$(basename "$IMAGE" ".img")
1347 fi
1348 ;;
1349 *.img)
1350 IMAGE="$FILES/$IMAGE_FNAME";
1351 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -05001352 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
1353 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
1354 DISK_FORMAT=$format
1355 else
1356 DISK_FORMAT=raw
1357 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -05001358 CONTAINER_FORMAT=bare
1359 ;;
1360 *.img.gz)
1361 IMAGE="$FILES/${IMAGE_FNAME}"
1362 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
1363 DISK_FORMAT=raw
1364 CONTAINER_FORMAT=bare
1365 UNPACK=zcat
1366 ;;
1367 *.qcow2)
1368 IMAGE="$FILES/${IMAGE_FNAME}"
1369 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
1370 DISK_FORMAT=qcow2
1371 CONTAINER_FORMAT=bare
1372 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +01001373 *.iso)
1374 IMAGE="$FILES/${IMAGE_FNAME}"
1375 IMAGE_NAME=$(basename "$IMAGE" ".iso")
1376 DISK_FORMAT=iso
1377 CONTAINER_FORMAT=bare
1378 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -05001379 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
1380 esac
1381
1382 if [ "$CONTAINER_FORMAT" = "bare" ]; then
1383 if [ "$UNPACK" = "zcat" ]; then
Christian Berendta7a219a2013-07-30 18:22:32 +02001384 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 -05001385 else
Christian Berendta7a219a2013-07-30 18:22:32 +02001386 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 -05001387 fi
1388 else
1389 # Use glance client to add the kernel the root filesystem.
1390 # We parse the results of the first upload to get the glance ID of the
1391 # kernel for use when uploading the root filesystem.
1392 KERNEL_ID=""; RAMDISK_ID="";
1393 if [ -n "$KERNEL" ]; then
Christian Berendta7a219a2013-07-30 18:22:32 +02001394 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 -05001395 fi
1396 if [ -n "$RAMDISK" ]; then
Christian Berendta7a219a2013-07-30 18:22:32 +02001397 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 -05001398 fi
Christian Berendta7a219a2013-07-30 18:22:32 +02001399 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 -05001400 fi
1401}
1402
Dean Troyer1a6d4492013-06-03 16:47:36 -05001403
Dean Troyerc1b486a2012-11-05 14:26:09 -06001404# Set the database backend to use
1405# When called from stackrc/localrc DATABASE_BACKENDS has not been
1406# initialized yet, just save the configuration selection and call back later
1407# to validate it.
1408# $1 The name of the database backend to use (mysql, postgresql, ...)
1409function use_database {
1410 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -06001411 # No backends registered means this is likely called from ``localrc``
1412 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -06001413 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +00001414 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 +01001415 else
Dean Troyerafc29fe2013-02-07 15:56:24 -06001416 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +01001417 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -06001418 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -06001419}
1420
Dean Troyer1a6d4492013-06-03 16:47:36 -05001421
Terry Wilson428af5a2012-11-01 16:12:39 -04001422# Toggle enable/disable_service for services that must run exclusive of each other
1423# $1 The name of a variable containing a space-separated list of services
1424# $2 The name of a variable in which to store the enabled service's name
1425# $3 The name of the service to enable
1426function use_exclusive_service {
1427 local options=${!1}
1428 local selection=$3
1429 out=$2
1430 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1431 for opt in $options;do
1432 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1433 done
1434 eval "$out=$selection"
1435 return 0
1436}
Dean Troyerca0e3d02012-04-13 15:58:37 -05001437
Dean Troyer1a6d4492013-06-03 16:47:36 -05001438
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001439# Wait for an HTTP server to start answering requests
1440# wait_for_service timeout url
1441function wait_for_service() {
1442 local timeout=$1
1443 local url=$2
1444 timeout $timeout sh -c "while ! http_proxy= https_proxy= curl -s $url >/dev/null; do sleep 1; done"
1445}
1446
Dean Troyer1a6d4492013-06-03 16:47:36 -05001447
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001448# Wrapper for ``yum`` to set proxy environment variables
1449# Uses globals ``OFFLINE``, ``*_proxy`
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001450# yum_install package [package ...]
1451function yum_install() {
1452 [[ "$OFFLINE" = "True" ]] && return
1453 local sudo="sudo"
1454 [[ "$(id -u)" = "0" ]] && sudo="env"
1455 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001456 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001457 yum install -y "$@"
1458}
1459
Dean Troyer1a6d4492013-06-03 16:47:36 -05001460
1461# zypper wrapper to set arguments correctly
1462# zypper_install package [package ...]
1463function zypper_install() {
1464 [[ "$OFFLINE" = "True" ]] && return
1465 local sudo="sudo"
1466 [[ "$(id -u)" = "0" ]] && sudo="env"
1467 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1468 zypper --non-interactive install --auto-agree-with-licenses "$@"
1469}
1470
1471
Nachi Uenofda946e2012-10-24 17:26:02 -07001472# ping check
1473# Uses globals ``ENABLED_SERVICES``
Dean Troyer1a6d4492013-06-03 16:47:36 -05001474# ping_check from-net ip boot-timeout expected
Nachi Uenofda946e2012-10-24 17:26:02 -07001475function ping_check() {
Mark McClainb05c8762013-07-06 23:29:39 -04001476 if is_service_enabled neutron; then
1477 _ping_check_neutron "$1" $2 $3 $4
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001478 return
1479 fi
1480 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -07001481}
1482
1483# ping check for nova
1484# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
1485function _ping_check_novanet() {
1486 local from_net=$1
1487 local ip=$2
1488 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001489 local expected=${4:-"True"}
1490 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001491 MULTI_HOST=`trueorfalse False $MULTI_HOST`
1492 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
Nachi Uenofda946e2012-10-24 17:26:02 -07001493 return
1494 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001495 if [[ "$expected" = "True" ]]; then
1496 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
1497 else
1498 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
1499 fi
1500 if ! timeout $boot_timeout sh -c "$check_command"; then
1501 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -08001502 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001503 else
Nachi Ueno07115eb2013-02-26 12:38:18 -08001504 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001505 fi
Nachi Uenofda946e2012-10-24 17:26:02 -07001506 exit 1
1507 fi
1508}
1509
Nachi Ueno6769b162013-08-12 18:18:56 -07001510# Get ip of instance
1511function get_instance_ip(){
1512 local vm_id=$1
1513 local network_name=$2
1514 local nova_result="$(nova show $vm_id)"
1515 local ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
1516 if [[ $ip = "" ]];then
1517 echo "$nova_result"
1518 die $LINENO "[Fail] Coudn't get ipaddress of VM"
1519 exit 1
1520 fi
1521 echo $ip
1522}
Dean Troyer1a6d4492013-06-03 16:47:36 -05001523
Nachi Uenofda946e2012-10-24 17:26:02 -07001524# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001525
Dean Troyer1a6d4492013-06-03 16:47:36 -05001526# ssh_check net-name key-file floating-ip default-user active-timeout
Nachi Uenofda946e2012-10-24 17:26:02 -07001527function ssh_check() {
Mark McClainb05c8762013-07-06 23:29:39 -04001528 if is_service_enabled neutron; then
1529 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001530 return
1531 fi
1532 _ssh_check_novanet "$1" $2 $3 $4 $5
1533}
1534
1535function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -07001536 local NET_NAME=$1
1537 local KEY_FILE=$2
1538 local FLOATING_IP=$3
1539 local DEFAULT_INSTANCE_USER=$4
1540 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -06001541 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -05001542 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 -08001543 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -07001544 fi
1545}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001546
Vincent Untz856a11e2012-11-21 16:04:12 +01001547
Vincent Untz856a11e2012-11-21 16:04:12 +01001548# Add a user to a group.
1549# add_user_to_group user group
1550function add_user_to_group() {
1551 local user=$1
1552 local group=$2
1553
1554 if [[ -z "$os_VENDOR" ]]; then
1555 GetOSVersion
1556 fi
1557
1558 # SLE11 and openSUSE 12.2 don't have the usual usermod
1559 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1560 sudo usermod -a -G "$group" "$user"
1561 else
1562 sudo usermod -A "$group" "$user"
1563 fi
1564}
1565
1566
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001567# Get the path to the direcotry where python executables are installed.
1568# get_python_exec_prefix
1569function get_python_exec_prefix() {
Martin Vidner4f9b33d2013-06-27 13:11:22 +00001570 if is_fedora || is_suse; then
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001571 echo "/usr/bin"
1572 else
1573 echo "/usr/local/bin"
1574 fi
1575}
1576
Dean Troyer1a6d4492013-06-03 16:47:36 -05001577
Vincent Untz856a11e2012-11-21 16:04:12 +01001578# Get the location of the $module-rootwrap executables, where module is cinder
1579# or nova.
1580# get_rootwrap_location module
1581function get_rootwrap_location() {
1582 local module=$1
1583
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001584 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +01001585}
1586
Dean Troyer1a6d4492013-06-03 16:47:36 -05001587
Vincent Untz8ec27222012-11-29 09:25:31 +01001588# Get the path to the pip command.
1589# get_pip_command
1590function get_pip_command() {
Dean Troyerd2cfcaa2013-08-01 14:17:27 -05001591 which pip || which pip-python
Ian Wienand535a8142013-05-15 09:25:27 +10001592
1593 if [ $? -ne 0 ]; then
1594 die $LINENO "Unable to find pip; cannot continue"
1595 fi
Vincent Untz8ec27222012-11-29 09:25:31 +01001596}
Vincent Untz856a11e2012-11-21 16:04:12 +01001597
Dean Troyer1a6d4492013-06-03 16:47:36 -05001598
Ian Wienand0488edd2013-04-11 12:04:36 +10001599# Path permissions sanity check
1600# check_path_perm_sanity path
1601function check_path_perm_sanity() {
1602 # Ensure no element of the path has 0700 permissions, which is very
1603 # likely to cause issues for daemons. Inspired by default 0700
1604 # homedir permissions on RHEL and common practice of making DEST in
1605 # the stack user's homedir.
1606
1607 local real_path=$(readlink -f $1)
1608 local rebuilt_path=""
1609 for i in $(echo ${real_path} | tr "/" " "); do
1610 rebuilt_path=$rebuilt_path"/"$i
1611
1612 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
1613 echo "*** DEST path element"
1614 echo "*** ${rebuilt_path}"
1615 echo "*** appears to have 0700 permissions."
1616 echo "*** This is very likely to cause fatal issues for devstack daemons."
1617
1618 if [[ -n "$SKIP_PATH_SANITY" ]]; then
1619 return
1620 else
1621 echo "*** Set SKIP_PATH_SANITY to skip this check"
1622 die $LINENO "Invalid path permissions"
1623 fi
1624 fi
1625 done
1626}
1627
Dean Troyer1a6d4492013-06-03 16:47:36 -05001628
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001629# This function recursively compares versions, and is not meant to be
1630# called by anything other than vercmp_numbers below. This function does
1631# not work with alphabetic versions.
1632#
1633# _vercmp_r sep ver1 ver2
1634function _vercmp_r {
1635 typeset sep
1636 typeset -a ver1=() ver2=()
1637 sep=$1; shift
1638 ver1=("${@:1:sep}")
1639 ver2=("${@:sep+1}")
1640
1641 if ((ver1 > ver2)); then
1642 echo 1; return 0
1643 elif ((ver2 > ver1)); then
1644 echo -1; return 0
1645 fi
1646
1647 if ((sep <= 1)); then
1648 echo 0; return 0
1649 fi
1650
1651 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
1652}
1653
1654
1655# This function compares two versions and is meant to be called by
1656# external callers. Please note the function assumes non-alphabetic
1657# versions. For example, this will work:
1658#
1659# vercmp_numbers 1.10 1.4
1660#
1661# The above will return "1", as 1.10 is greater than 1.4.
1662#
1663# vercmp_numbers 5.2 6.4
1664#
1665# The above will return "-1", as 5.2 is less than 6.4.
1666#
1667# vercmp_numbers 4.0 4.0
1668#
1669# The above will return "0", as the versions are equal.
1670#
1671# vercmp_numbers ver1 ver2
1672vercmp_numbers() {
1673 typeset v1=$1 v2=$2 sep
1674 typeset -a ver1 ver2
1675
1676 IFS=. read -ra ver1 <<< "$v1"
1677 IFS=. read -ra ver2 <<< "$v2"
1678
1679 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
1680}
1681
1682
Dean Troyer533e14d2013-08-30 15:11:22 -05001683# ``policy_add policy_file policy_name policy_permissions``
1684#
1685# Add a policy to a policy.json file
1686# Do nothing if the policy already exists
1687
1688function policy_add() {
1689 local policy_file=$1
1690 local policy_name=$2
1691 local policy_perm=$3
1692
1693 if grep -q ${policy_name} ${policy_file}; then
1694 echo "Policy ${policy_name} already exists in ${policy_file}"
1695 return
1696 fi
1697
1698 # Add a terminating comma to policy lines without one
1699 # Remove the closing '}' and all lines following to the end-of-file
1700 local tmpfile=$(mktemp)
1701 uniq ${policy_file} | sed -e '
1702 s/]$/],/
1703 /^[}]/,$d
1704 ' > ${tmpfile}
1705
1706 # Append policy and closing brace
1707 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
1708 echo "}" >>${tmpfile}
1709
1710 mv ${tmpfile} ${policy_file}
1711}
1712
1713
Dean Troyer27e32692012-03-16 16:16:56 -05001714# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +00001715$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001716
1717
1718# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -04001719# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -04001720# End: