blob: 281b6767c51b845eb8859a35d219ddf6db2d73a2 [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:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01004#
5# - ``ENABLED_SERVICES``
6# - ``ERROR_ON_CLONE``
7# - ``FILES``
8# - ``GLANCE_HOSTPORT``
9# - ``OFFLINE``
10# - ``PIP_DOWNLOAD_CACHE``
11# - ``PIP_USE_MIRRORS``
12# - ``RECLONE``
13# - ``TRACK_DEPENDS``
14# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer13dc5cc2012-03-27 14:50:45 -050015
Dean Troyer7f9aa712012-01-31 12:11:56 -060016
Dean Troyer27e32692012-03-16 16:16:56 -050017# Save trace setting
18XTRACE=$(set +o | grep xtrace)
19set +o xtrace
20
Dean Troyer7f9aa712012-01-31 12:11:56 -060021
Dean Troyerd4f69b22013-07-24 12:24:43 -050022# Convert CIDR notation to a IPv4 netmask
23# cidr2netmask cidr-bits
24function cidr2netmask() {
25 local maskpat="255 255 255 255"
26 local maskdgt="254 252 248 240 224 192 128"
27 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
28 echo ${1-0}.${2-0}.${3-0}.${4-0}
29}
30
31
32# Return the network portion of the given IP address using netmask
33# netmask is in the traditional dotted-quad format
34# maskip ip-address netmask
35function maskip() {
36 local ip=$1
37 local mask=$2
38 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
39 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
40 echo $subnet
41}
42
43
44# Exit 0 if address is in network or 1 if address is not in network
45# ip-range is in CIDR notation: 1.2.3.4/20
Dean Troyer4a43b7b2012-08-28 17:43:40 -050046# address_in_net ip-address ip-range
Vishvananda Ishayac9ad14b2012-07-03 20:29:01 +000047function address_in_net() {
Dean Troyerd4f69b22013-07-24 12:24:43 -050048 local ip=$1
49 local range=$2
50 local masklen=${range#*/}
51 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
52 local subnet=$(maskip $ip $(cidr2netmask $masklen))
53 [[ $network == $subnet ]]
Vishvananda Ishayac9ad14b2012-07-03 20:29:01 +000054}
55
56
Dean Troyer4a43b7b2012-08-28 17:43:40 -050057# Wrapper for ``apt-get`` to set cache and proxy environment variables
Adam Spierscb961592013-10-05 12:11:07 +010058# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyer13dc5cc2012-03-27 14:50:45 -050059# apt_get operation package [package ...]
Dean Troyer7f9aa712012-01-31 12:11:56 -060060function apt_get() {
Dean Troyerd0b21e22012-03-07 14:52:25 -060061 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer7f9aa712012-01-31 12:11:56 -060062 local sudo="sudo"
63 [[ "$(id -u)" = "0" ]] && sudo="env"
64 $sudo DEBIAN_FRONTEND=noninteractive \
65 http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +090066 no_proxy=$no_proxy \
Dean Troyer7f9aa712012-01-31 12:11:56 -060067 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
68}
69
70
71# Gracefully cp only if source file/dir exists
72# cp_it source destination
73function cp_it {
74 if [ -e $1 ] || [ -d $1 ]; then
75 cp -pRL $1 $2
76 fi
77}
78
79
Kui Shi5e28a3e2013-08-02 17:26:28 +080080# Prints backtrace info
81# filename:lineno:function
82function backtrace {
83 local level=$1
84 local deep=$((${#BASH_SOURCE[@]} - 1))
85 echo "[Call Trace]"
86 while [ $level -le $deep ]; do
87 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
88 deep=$((deep - 1))
89 done
90}
91
92
Dean Troyerac93efb2013-03-13 14:30:54 -050093# Prints line number and "message" then exits
94# die $LINENO "message"
Dean Troyer27e32692012-03-16 16:16:56 -050095function die() {
Dean Troyer489bd2a2012-03-02 10:44:29 -060096 local exitcode=$?
Dean Troyer896eb662013-04-05 15:02:01 -050097 set +o xtrace
98 local line=$1; shift
Nachi Ueno07115eb2013-02-26 12:38:18 -080099 if [ $exitcode == 0 ]; then
100 exitcode=1
101 fi
Kui Shi5e28a3e2013-08-02 17:26:28 +0800102 backtrace 2
Dean Troyer896eb662013-04-05 15:02:01 -0500103 err $line "$*"
Dean Troyer27e32692012-03-16 16:16:56 -0500104 exit $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -0600105}
106
107
108# Checks an environment variable is not set or has length 0 OR if the
109# exit code is non-zero and prints "message" and exits
110# NOTE: env-var is the variable name without a '$'
Dean Troyerac93efb2013-03-13 14:30:54 -0500111# die_if_not_set $LINENO env-var "message"
Dean Troyer489bd2a2012-03-02 10:44:29 -0600112function die_if_not_set() {
Dean Troyer896eb662013-04-05 15:02:01 -0500113 local exitcode=$?
114 FXTRACE=$(set +o | grep xtrace)
115 set +o xtrace
116 local line=$1; shift
117 local evar=$1; shift
118 if ! is_set $evar || [ $exitcode != 0 ]; then
119 die $line "$*"
120 fi
121 $FXTRACE
122}
123
124
125# Prints line number and "message" in error format
126# err $LINENO "message"
127function err() {
128 local exitcode=$?
129 errXTRACE=$(set +o | grep xtrace)
130 set +o xtrace
Kui Shi17df0772013-08-02 17:55:41 +0800131 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
Dean Troyer896eb662013-04-05 15:02:01 -0500132 echo $msg 1>&2;
133 if [[ -n ${SCREEN_LOGDIR} ]]; then
134 echo $msg >> "${SCREEN_LOGDIR}/error.log"
135 fi
136 $errXTRACE
137 return $exitcode
138}
139
140
141# Checks an environment variable is not set or has length 0 OR if the
142# exit code is non-zero and prints "message"
143# NOTE: env-var is the variable name without a '$'
144# err_if_not_set $LINENO env-var "message"
145function err_if_not_set() {
146 local exitcode=$?
147 errinsXTRACE=$(set +o | grep xtrace)
148 set +o xtrace
149 local line=$1; shift
150 local evar=$1; shift
151 if ! is_set $evar || [ $exitcode != 0 ]; then
152 err $line "$*"
153 fi
154 $errinsXTRACE
155 return $exitcode
Dean Troyer489bd2a2012-03-02 10:44:29 -0600156}
157
158
Dean Troyer893e6632013-09-13 15:05:51 -0500159# Prints line number and "message" in warning format
160# warn $LINENO "message"
161function warn() {
162 local exitcode=$?
163 errXTRACE=$(set +o | grep xtrace)
164 set +o xtrace
165 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
166 echo $msg 1>&2;
167 if [[ -n ${SCREEN_LOGDIR} ]]; then
168 echo $msg >> "${SCREEN_LOGDIR}/error.log"
169 fi
170 $errXTRACE
171 return $exitcode
172}
173
174
Dean Troyer48352ee2012-12-12 12:50:38 -0600175# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
176# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
177# ``localrc`` or on the command line if necessary::
178#
179# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
180#
181# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
182
183function export_proxy_variables() {
184 if [[ -n "$http_proxy" ]]; then
185 export http_proxy=$http_proxy
186 fi
187 if [[ -n "$https_proxy" ]]; then
188 export https_proxy=$https_proxy
189 fi
190 if [[ -n "$no_proxy" ]]; then
191 export no_proxy=$no_proxy
192 fi
193}
194
195
Dean Troyer489bd2a2012-03-02 10:44:29 -0600196# Grab a numbered field from python prettytable output
197# Fields are numbered starting with 1
198# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
199# get_field field-number
200function get_field() {
201 while read data; do
202 if [ "$1" -lt 0 ]; then
203 field="(\$(NF$1))"
204 else
205 field="\$$(($1 + 1))"
206 fi
207 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
208 done
209}
210
211
Dean Troyerc892bde2013-03-13 14:06:13 -0500212# Get the default value for HOST_IP
213# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
214function get_default_host_ip() {
215 local fixed_range=$1
216 local floating_range=$2
217 local host_ip_iface=$3
218 local host_ip=$4
219
220 # Find the interface used for the default route
221 host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
222 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
223 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
224 host_ip=""
225 host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}'`
226 for IP in $host_ips; do
227 # Attempt to filter out IP addresses that are part of the fixed and
228 # floating range. Note that this method only works if the ``netaddr``
229 # python library is installed. If it is not installed, an error
230 # will be printed and the first IP from the interface will be used.
231 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
232 # address.
233 if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then
234 host_ip=$IP
235 break;
236 fi
237 done
238 fi
239 echo $host_ip
240}
241
242
Isaku Yamahata8c438092013-02-12 22:30:56 +0900243function _get_package_dir() {
244 local pkg_dir
245 if is_ubuntu; then
246 pkg_dir=$FILES/apts
247 elif is_fedora; then
248 pkg_dir=$FILES/rpms
249 elif is_suse; then
250 pkg_dir=$FILES/rpms-suse
251 else
252 exit_distro_not_supported "list of packages"
253 fi
254 echo "$pkg_dir"
255}
256
Dean Troyer1a6d4492013-06-03 16:47:36 -0500257
Dean Troyer7e270512012-06-14 15:23:24 -0500258# get_packages() collects a list of package names of any type from the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500259# prerequisite files in ``files/{apts|rpms}``. The list is intended
260# to be passed to a package installer such as apt or yum.
Dean Troyer7e270512012-06-14 15:23:24 -0500261#
Isaku Yamahata8c438092013-02-12 22:30:56 +0900262# Only packages required for the services in 1st argument will be
Dean Troyer7e270512012-06-14 15:23:24 -0500263# included. Two bits of metadata are recognized in the prerequisite files:
Adam Spierscb961592013-10-05 12:11:07 +0100264#
265# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
Dean Troyer7e270512012-06-14 15:23:24 -0500266# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
267# of the package to the distros listed. The distro names are case insensitive.
Dean Troyer7e270512012-06-14 15:23:24 -0500268function get_packages() {
Dean Troyerca5af862013-10-04 13:33:07 -0500269 local services=$@
Isaku Yamahata8c438092013-02-12 22:30:56 +0900270 local package_dir=$(_get_package_dir)
Dean Troyer7e270512012-06-14 15:23:24 -0500271 local file_to_parse
272 local service
273
274 if [[ -z "$package_dir" ]]; then
275 echo "No package directory supplied"
276 return 1
277 fi
278 if [[ -z "$DISTRO" ]]; then
Vincent Untz855c5872012-10-04 13:36:46 +0200279 GetDistro
Dean Troyer7e270512012-06-14 15:23:24 -0500280 fi
Dean Troyerca5af862013-10-04 13:33:07 -0500281 for service in ${services//,/ }; do
Dean Troyer7e270512012-06-14 15:23:24 -0500282 # Allow individual services to specify dependencies
283 if [[ -e ${package_dir}/${service} ]]; then
284 file_to_parse="${file_to_parse} $service"
285 fi
286 # NOTE(sdague) n-api needs glance for now because that's where
287 # glance client is
288 if [[ $service == n-api ]]; then
289 if [[ ! $file_to_parse =~ nova ]]; then
290 file_to_parse="${file_to_parse} nova"
291 fi
292 if [[ ! $file_to_parse =~ glance ]]; then
293 file_to_parse="${file_to_parse} glance"
294 fi
295 elif [[ $service == c-* ]]; then
296 if [[ ! $file_to_parse =~ cinder ]]; then
297 file_to_parse="${file_to_parse} cinder"
298 fi
John H. Tran93361642012-07-26 11:22:05 -0700299 elif [[ $service == ceilometer-* ]]; then
300 if [[ ! $file_to_parse =~ ceilometer ]]; then
301 file_to_parse="${file_to_parse} ceilometer"
302 fi
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100303 elif [[ $service == s-* ]]; then
304 if [[ ! $file_to_parse =~ swift ]]; then
305 file_to_parse="${file_to_parse} swift"
306 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500307 elif [[ $service == n-* ]]; then
308 if [[ ! $file_to_parse =~ nova ]]; then
309 file_to_parse="${file_to_parse} nova"
310 fi
311 elif [[ $service == g-* ]]; then
312 if [[ ! $file_to_parse =~ glance ]]; then
313 file_to_parse="${file_to_parse} glance"
314 fi
315 elif [[ $service == key* ]]; then
316 if [[ ! $file_to_parse =~ keystone ]]; then
317 file_to_parse="${file_to_parse} keystone"
318 fi
Robert Collins0a9954f2012-11-20 11:34:25 +1300319 elif [[ $service == q-* ]]; then
Mark McClainb05c8762013-07-06 23:29:39 -0400320 if [[ ! $file_to_parse =~ neutron ]]; then
321 file_to_parse="${file_to_parse} neutron"
Robert Collins0a9954f2012-11-20 11:34:25 +1300322 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500323 fi
324 done
325
326 for file in ${file_to_parse}; do
327 local fname=${package_dir}/${file}
328 local OIFS line package distros distro
329 [[ -e $fname ]] || continue
330
331 OIFS=$IFS
332 IFS=$'\n'
333 for line in $(<${fname}); do
334 if [[ $line =~ "NOPRIME" ]]; then
335 continue
336 fi
337
Christian Berendt71d56302013-07-22 11:37:42 +0200338 # Assume we want this package
339 package=${line%#*}
340 inst_pkg=1
341
342 # Look for # dist:xxx in comment
Dean Troyer7e270512012-06-14 15:23:24 -0500343 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
344 # We are using BASH regexp matching feature.
345 package=${BASH_REMATCH[1]}
346 distros=${BASH_REMATCH[2]}
347 # In bash ${VAR,,} will lowecase VAR
Christian Berendt71d56302013-07-22 11:37:42 +0200348 # Look for a match in the distro list
349 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
350 # If no match then skip this package
351 inst_pkg=0
352 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500353 fi
354
Christian Berendt71d56302013-07-22 11:37:42 +0200355 # Look for # testonly in comment
356 if [[ $line =~ (.*)#.*testonly.* ]]; then
357 package=${BASH_REMATCH[1]}
358 # Are we installing test packages? (test for the default value)
359 if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then
360 # If not installing test packages the skip this package
361 inst_pkg=0
362 fi
363 fi
364
365 if [[ $inst_pkg = 1 ]]; then
366 echo $package
367 fi
Dean Troyer7e270512012-06-14 15:23:24 -0500368 done
369 IFS=$OIFS
370 done
371}
372
373
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500374# Determine OS Vendor, Release and Update
375# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
376# Returns results in global variables:
377# os_VENDOR - vendor name
378# os_RELEASE - release
379# os_UPDATE - update
380# os_PACKAGE - package type
381# os_CODENAME - vendor's codename for release
382# GetOSVersion
383GetOSVersion() {
384 # Figure out which vendor we are
Mehdi Abaakoukaee94122013-09-30 11:48:00 +0000385 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500386 # OS/X
387 os_VENDOR=`sw_vers -productName`
388 os_RELEASE=`sw_vers -productVersion`
389 os_UPDATE=${os_RELEASE##*.}
390 os_RELEASE=${os_RELEASE%.*}
391 os_PACKAGE=""
392 if [[ "$os_RELEASE" =~ "10.7" ]]; then
393 os_CODENAME="lion"
394 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
395 os_CODENAME="snow leopard"
396 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
397 os_CODENAME="leopard"
398 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
399 os_CODENAME="tiger"
400 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
401 os_CODENAME="panther"
402 else
403 os_CODENAME=""
404 fi
405 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
406 os_VENDOR=$(lsb_release -i -s)
407 os_RELEASE=$(lsb_release -r -s)
408 os_UPDATE=""
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100409 os_PACKAGE="rpm"
Derek Morton4a8496e2013-04-08 23:46:08 -0500410 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500411 os_PACKAGE="deb"
Vincent Untz856a11e2012-11-21 16:04:12 +0100412 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
413 lsb_release -d -s | grep -q openSUSE
414 if [[ $? -eq 0 ]]; then
415 os_VENDOR="openSUSE"
416 fi
Vincent Untzcd1fe982013-03-12 18:04:29 +0100417 elif [[ $os_VENDOR == "openSUSE project" ]]; then
418 os_VENDOR="openSUSE"
Attila Fazekasaf988fd2013-01-13 14:20:47 +0100419 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
420 os_VENDOR="Red Hat"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500421 fi
422 os_CODENAME=$(lsb_release -c -s)
423 elif [[ -r /etc/redhat-release ]]; then
424 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
Ian Wienandbe2ff9a2013-12-17 16:26:21 +1100425 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500426 # CentOS release 5.5 (Final)
427 # CentOS Linux release 6.0 (Final)
428 # Fedora release 16 (Verne)
Bob Ball46691222013-08-12 17:28:50 +0100429 # XenServer release 6.2.0-70446c (xenenterprise)
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500430 os_CODENAME=""
Bob Ball46691222013-08-12 17:28:50 +0100431 for r in "Red Hat" CentOS Fedora XenServer; do
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500432 os_VENDOR=$r
433 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
Ian Wienandbe2ff9a2013-12-17 16:26:21 +1100434 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500435 os_CODENAME=${ver#*|}
436 os_RELEASE=${ver%|*}
437 os_UPDATE=${os_RELEASE##*.}
438 os_RELEASE=${os_RELEASE%.*}
439 break
440 fi
441 os_VENDOR=""
442 done
443 os_PACKAGE="rpm"
Vincent Untz856a11e2012-11-21 16:04:12 +0100444 elif [[ -r /etc/SuSE-release ]]; then
445 for r in openSUSE "SUSE Linux"; do
446 if [[ "$r" = "SUSE Linux" ]]; then
447 os_VENDOR="SUSE LINUX"
448 else
449 os_VENDOR=$r
450 fi
451
452 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
453 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
454 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
455 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
456 break
457 fi
458 os_VENDOR=""
459 done
460 os_PACKAGE="rpm"
Émilien Macchib2ef8902013-05-04 00:48:20 +0200461 # If lsb_release is not installed, we should be able to detect Debian OS
462 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
463 os_VENDOR="Debian"
464 os_PACKAGE="deb"
465 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
466 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500467 fi
468 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
469}
470
Andrew Laskif900bd72012-09-05 17:23:14 -0400471
Dean Troyera9e0a482012-07-09 14:07:23 -0500472# Translate the OS version values into common nomenclature
473# Sets ``DISTRO`` from the ``os_*`` values
474function GetDistro() {
475 GetOSVersion
Émilien Macchib2ef8902013-05-04 00:48:20 +0200476 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
477 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
Dean Troyera9e0a482012-07-09 14:07:23 -0500478 DISTRO=$os_CODENAME
479 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
480 # For Fedora, just use 'f' and the release
481 DISTRO="f$os_RELEASE"
Vincent Untz856a11e2012-11-21 16:04:12 +0100482 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
483 DISTRO="opensuse-$os_RELEASE"
484 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
485 # For SLE, also use the service pack
486 if [[ -z "$os_UPDATE" ]]; then
487 DISTRO="sle${os_RELEASE}"
488 else
489 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
490 fi
Ian Wienandd857f4b2013-03-20 14:51:06 +1100491 elif [[ "$os_VENDOR" =~ (Red Hat) || "$os_VENDOR" =~ (CentOS) ]]; then
492 # Drop the . release as we assume it's compatible
493 DISTRO="rhel${os_RELEASE::1}"
Bob Ball46691222013-08-12 17:28:50 +0100494 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
495 DISTRO="xs$os_RELEASE"
Dean Troyera9e0a482012-07-09 14:07:23 -0500496 else
497 # Catch-all for now is Vendor + Release + Update
498 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
499 fi
500 export DISTRO
501}
502
503
Vincent Untz00011c02012-12-06 09:56:32 +0100504# Determine if current distribution is a Fedora-based distribution
Dean Troyer1a6d4492013-06-03 16:47:36 -0500505# (Fedora, RHEL, CentOS, etc).
Vincent Untz00011c02012-12-06 09:56:32 +0100506# is_fedora
507function is_fedora {
508 if [[ -z "$os_VENDOR" ]]; then
509 GetOSVersion
510 fi
511
512 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ]
513}
514
Dean Troyer1a6d4492013-06-03 16:47:36 -0500515
Vincent Untz856a11e2012-11-21 16:04:12 +0100516# Determine if current distribution is a SUSE-based distribution
517# (openSUSE, SLE).
518# is_suse
519function is_suse {
520 if [[ -z "$os_VENDOR" ]]; then
521 GetOSVersion
522 fi
523
Steve Baker1a7bbd22012-12-03 17:04:02 +1300524 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
Vincent Untz856a11e2012-11-21 16:04:12 +0100525}
526
527
Dean Troyer1a6d4492013-06-03 16:47:36 -0500528# Determine if current distribution is an Ubuntu-based distribution
529# It will also detect non-Ubuntu but Debian-based distros
530# is_ubuntu
531function is_ubuntu {
532 if [[ -z "$os_PACKAGE" ]]; then
533 GetOSVersion
534 fi
535 [ "$os_PACKAGE" = "deb" ]
536}
537
538
Vincent Untz00011c02012-12-06 09:56:32 +0100539# Exit after outputting a message about the distribution not being supported.
540# exit_distro_not_supported [optional-string-telling-what-is-missing]
541function exit_distro_not_supported {
542 if [[ -z "$DISTRO" ]]; then
543 GetDistro
544 fi
545
546 if [ $# -gt 0 ]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800547 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
Vincent Untz00011c02012-12-06 09:56:32 +0100548 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800549 die $LINENO "Support for $DISTRO is incomplete."
Vincent Untz00011c02012-12-06 09:56:32 +0100550 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100551}
552
Daniel Jonesfa868cb2013-06-18 15:28:01 -0500553# Utility function for checking machine architecture
554# is_arch arch-type
555function is_arch {
556 ARCH_TYPE=$1
557
Rafael Folcoab775872013-12-02 14:04:32 -0200558 [[ "$(uname -m)" == "$ARCH_TYPE" ]]
Daniel Jonesfa868cb2013-06-18 15:28:01 -0500559}
Vincent Untz00011c02012-12-06 09:56:32 +0100560
Chris Buccella610af8c2013-11-05 12:56:34 +0000561# Checks if installed Apache is <= given version
562# $1 = x.y.z (version string of Apache)
563function check_apache_version {
564 local cmd="apachectl"
565 if ! [[ -x $(which apachectl 2>/dev/null) ]]; then
566 cmd="/usr/sbin/apachectl"
567 fi
568
569 local version=$($cmd -v | grep version | grep -Po 'Apache/\K[^ ]*')
570 expr "$version" '>=' $1 > /dev/null
571}
572
Dean Troyer7f9aa712012-01-31 12:11:56 -0600573# git clone only if directory doesn't exist already. Since ``DEST`` might not
574# be owned by the installation user, we create the directory and change the
575# ownership to the proper user.
576# Set global RECLONE=yes to simulate a clone when dest-dir exists
James E. Blair94cb9602012-06-22 15:28:29 -0700577# Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo
578# does not exist (default is False, meaning the repo will be cloned).
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500579# Uses global ``OFFLINE``
Dean Troyer7f9aa712012-01-31 12:11:56 -0600580# git_clone remote dest-dir branch
581function git_clone {
Dean Troyer7f9aa712012-01-31 12:11:56 -0600582 GIT_REMOTE=$1
583 GIT_DEST=$2
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300584 GIT_REF=$3
Sirushti Murugesana8d41e32013-09-25 11:30:31 +0530585 RECLONE=$(trueorfalse False $RECLONE)
Dean Troyer7f9aa712012-01-31 12:11:56 -0600586
Sean Dague835db2f2013-09-23 14:17:06 -0400587 if [[ "$OFFLINE" = "True" ]]; then
588 echo "Running in offline mode, clones already exist"
589 # print out the results so we know what change was used in the logs
590 cd $GIT_DEST
Sean Dague45a21f02013-09-25 10:27:27 -0400591 git show --oneline | head -1
Sean Dague835db2f2013-09-23 14:17:06 -0400592 return
593 fi
594
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300595 if echo $GIT_REF | egrep -q "^refs"; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600596 # If our branch name is a gerrit style refs/changes/...
597 if [[ ! -d $GIT_DEST ]]; then
Sean Daguedc30bd32013-10-22 07:30:47 -0400598 [[ "$ERROR_ON_CLONE" = "True" ]] && \
599 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer7f9aa712012-01-31 12:11:56 -0600600 git clone $GIT_REMOTE $GIT_DEST
601 fi
602 cd $GIT_DEST
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300603 git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
Dean Troyer7f9aa712012-01-31 12:11:56 -0600604 else
605 # do a full clone only if the directory doesn't exist
606 if [[ ! -d $GIT_DEST ]]; then
Sean Daguedc30bd32013-10-22 07:30:47 -0400607 [[ "$ERROR_ON_CLONE" = "True" ]] && \
608 die $LINENO "Cloning not allowed in this configuration"
Dean Troyer7f9aa712012-01-31 12:11:56 -0600609 git clone $GIT_REMOTE $GIT_DEST
610 cd $GIT_DEST
611 # This checkout syntax works for both branches and tags
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300612 git checkout $GIT_REF
Sirushti Murugesana8d41e32013-09-25 11:30:31 +0530613 elif [[ "$RECLONE" = "True" ]]; then
Dean Troyer7f9aa712012-01-31 12:11:56 -0600614 # if it does exist then simulate what clone does if asked to RECLONE
615 cd $GIT_DEST
616 # set the url to pull from and fetch
617 git remote set-url origin $GIT_REMOTE
618 git fetch origin
619 # remove the existing ignored files (like pyc) as they cause breakage
620 # (due to the py files having older timestamps than our pyc, so python
621 # thinks the pyc files are correct using them)
622 find $GIT_DEST -name '*.pyc' -delete
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300623
624 # handle GIT_REF accordingly to type (tag, branch)
625 if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
626 git_update_tag $GIT_REF
627 elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
628 git_update_branch $GIT_REF
Andrew Laskif900bd72012-09-05 17:23:14 -0400629 elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then
630 git_update_remote_branch $GIT_REF
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300631 else
Sean Daguedc30bd32013-10-22 07:30:47 -0400632 die $LINENO "$GIT_REF is neither branch nor tag"
Evgeniy Afonichev6a3912d2012-07-10 14:02:43 +0300633 fi
634
Dean Troyer7f9aa712012-01-31 12:11:56 -0600635 fi
636 fi
Sean Dague835db2f2013-09-23 14:17:06 -0400637
638 # print out the results so we know what change was used in the logs
639 cd $GIT_DEST
Sean Dague45a21f02013-09-25 10:27:27 -0400640 git show --oneline | head -1
Dean Troyer7f9aa712012-01-31 12:11:56 -0600641}
642
643
Dean Troyer1a6d4492013-06-03 16:47:36 -0500644# git update using reference as a branch.
645# git_update_branch ref
646function git_update_branch() {
647
648 GIT_BRANCH=$1
649
650 git checkout -f origin/$GIT_BRANCH
651 # a local branch might not exist
652 git branch -D $GIT_BRANCH || true
653 git checkout -b $GIT_BRANCH
654}
655
656
657# git update using reference as a branch.
658# git_update_remote_branch ref
659function git_update_remote_branch() {
660
661 GIT_BRANCH=$1
662
663 git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH
664}
665
666
667# git update using reference as a tag. Be careful editing source at that repo
668# as working copy will be in a detached mode
669# git_update_tag ref
670function git_update_tag() {
671
672 GIT_TAG=$1
673
674 git tag -d $GIT_TAG
675 # fetching given tag only
676 git fetch origin tag $GIT_TAG
677 git checkout -f $GIT_TAG
678}
679
680
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500681# Comment an option in an INI file
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200682# inicomment config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500683function inicomment() {
684 local file=$1
685 local section=$2
686 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100687 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500688}
689
Dean Troyer896eb662013-04-05 15:02:01 -0500690
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200691# Uncomment an option in an INI file
692# iniuncomment config-file section option
693function iniuncomment() {
694 local file=$1
695 local section=$2
696 local option=$3
Attila Fazekas588eb412012-12-20 10:57:16 +0100697 sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
Chmouel Boudjnahc7214e82012-06-06 13:56:39 +0200698}
699
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500700
701# Get an option from an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500702# iniget config-file section option
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500703function iniget() {
704 local file=$1
705 local section=$2
706 local option=$3
707 local line
Attila Fazekas588eb412012-12-20 10:57:16 +0100708 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500709 echo ${line#*=}
710}
711
Dean Troyer896eb662013-04-05 15:02:01 -0500712
Attila Fazekas588eb412012-12-20 10:57:16 +0100713# Determinate is the given option present in the INI file
714# ini_has_option config-file section option
715function ini_has_option() {
716 local file=$1
717 local section=$2
718 local option=$3
719 local line
720 line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
721 [ -n "$line" ]
722}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500723
Dean Troyer896eb662013-04-05 15:02:01 -0500724
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500725# Set an option in an INI file
Dean Troyer09e636e2012-03-19 16:31:12 -0500726# iniset config-file section option value
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500727function iniset() {
728 local file=$1
729 local section=$2
730 local option=$3
731 local value=$4
DennyZhangf43f3a52013-10-11 23:09:47 -0500732
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600733 [[ -z $section || -z $option ]] && return
734
DennyZhangf43f3a52013-10-11 23:09:47 -0500735 if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500736 # Add section at the end
Attila Fazekas588eb412012-12-20 10:57:16 +0100737 echo -e "\n[$section]" >>"$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500738 fi
Attila Fazekas588eb412012-12-20 10:57:16 +0100739 if ! ini_has_option "$file" "$section" "$option"; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500740 # Add it
Attila Fazekas588eb412012-12-20 10:57:16 +0100741 sed -i -e "/^\[$section\]/ a\\
Dean Troyer09e636e2012-03-19 16:31:12 -0500742$option = $value
Attila Fazekas588eb412012-12-20 10:57:16 +0100743" "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500744 else
Andrea Frittolicd7d9562013-12-05 08:09:12 +0000745 local sep=$(echo -ne "\x01")
Dean Troyer09e636e2012-03-19 16:31:12 -0500746 # Replace it
Andrea Frittolicd7d9562013-12-05 08:09:12 +0000747 sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
Dean Troyer09e636e2012-03-19 16:31:12 -0500748 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500749}
750
Dean Troyer896eb662013-04-05 15:02:01 -0500751
Lianhao Lu239f3242013-03-01 15:54:02 +0800752# Get a multiple line option from an INI file
753# iniget_multiline config-file section option
754function iniget_multiline() {
755 local file=$1
756 local section=$2
757 local option=$3
758 local values
759 values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
760 echo ${values}
761}
762
Dean Troyer896eb662013-04-05 15:02:01 -0500763
Lianhao Lu239f3242013-03-01 15:54:02 +0800764# Set a multiple line option in an INI file
765# iniset_multiline config-file section option value1 value2 valu3 ...
766function iniset_multiline() {
767 local file=$1
768 local section=$2
769 local option=$3
770 shift 3
771 local values
772 for v in $@; do
773 # The later sed command inserts each new value in the line next to
774 # the section identifier, which causes the values to be inserted in
775 # the reverse order. Do a reverse here to keep the original order.
776 values="$v ${values}"
777 done
778 if ! grep -q "^\[$section\]" "$file"; then
779 # Add section at the end
780 echo -e "\n[$section]" >>"$file"
781 else
782 # Remove old values
783 sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
784 fi
785 # Add new ones
786 for v in $values; do
787 sed -i -e "/^\[$section\]/ a\\
788$option = $v
789" "$file"
790 done
791}
792
Dean Troyer896eb662013-04-05 15:02:01 -0500793
Lianhao Lu239f3242013-03-01 15:54:02 +0800794# Append a new option in an ini file without replacing the old value
795# iniadd config-file section option value1 value2 value3 ...
796function iniadd() {
797 local file=$1
798 local section=$2
799 local option=$3
800 shift 3
801 local values="$(iniget_multiline $file $section $option) $@"
802 iniset_multiline $file $section $option $values
803}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500804
Dean Troyer896eb662013-04-05 15:02:01 -0500805# Find out if a process exists by partial name.
806# is_running name
807function is_running() {
808 local name=$1
809 ps auxw | grep -v grep | grep ${name} > /dev/null
810 RC=$?
811 # some times I really hate bash reverse binary logic
812 return $RC
813}
814
815
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000816# is_service_enabled() checks if the service(s) specified as arguments are
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500817# enabled by the user in ``ENABLED_SERVICES``.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000818#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500819# Multiple services specified as arguments are ``OR``'ed together; the test
820# is a short-circuit boolean, i.e it returns on the first match.
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000821#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500822# There are special cases for some 'catch-all' services::
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000823# **nova** returns true if any service enabled start with **n-**
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500824# **cinder** returns true if any service enabled start with **c-**
825# **ceilometer** returns true if any service enabled start with **ceilometer**
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000826# **glance** returns true if any service enabled start with **g-**
Mark McClainb05c8762013-07-06 23:29:39 -0400827# **neutron** returns true if any service enabled start with **q-**
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100828# **swift** returns true if any service enabled start with **s-**
Nikhil Manchanda0cccad42012-12-03 18:15:09 -0700829# **trove** returns true if any service enabled start with **tr-**
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100830# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
831# **s-** services will be enabled. This will be deprecated in the future.
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500832#
Chris Behrensc62c2b92013-07-24 03:56:13 -0700833# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
834# We also need to make sure to treat **n-cell-region** and **n-cell-child**
835# as enabled in this case.
836#
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500837# Uses global ``ENABLED_SERVICES``
838# is_service_enabled service [service ...]
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000839function is_service_enabled() {
840 services=$@
841 for service in ${services}; do
842 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
Chris Behrensc62c2b92013-07-24 03:56:13 -0700843 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000844 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0
Dean Troyer67787e62012-05-02 11:48:15 -0500845 [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0
John H. Tran93361642012-07-26 11:22:05 -0700846 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000847 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0
Roman Prykhodchenkod0059592013-11-14 09:58:53 +0200848 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && return 0
Mark McClainb05c8762013-07-06 23:29:39 -0400849 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0
Nikhil Manchanda0cccad42012-12-03 18:15:09 -0700850 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && return 0
Chmouel Boudjnah0c3a5582013-03-06 10:58:33 +0100851 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && return 0
852 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && return 0
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000853 done
854 return 1
855}
856
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500857
858# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
859# _cleanup_service_list service-list
Doug Hellmannf04178f2012-07-05 17:10:03 -0400860function _cleanup_service_list () {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500861 echo "$1" | sed -e '
Doug Hellmannf04178f2012-07-05 17:10:03 -0400862 s/,,/,/g;
863 s/^,//;
864 s/,$//
865 '
866}
867
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500868
Doug Hellmannf04178f2012-07-05 17:10:03 -0400869# enable_service() adds the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500870# ``ENABLED_SERVICES`` list, if they are not already present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400871#
872# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800873# enable_service qpid
Doug Hellmannf04178f2012-07-05 17:10:03 -0400874#
875# This function does not know about the special cases
Mark McClainb05c8762013-07-06 23:29:39 -0400876# for nova, glance, and neutron built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500877# Uses global ``ENABLED_SERVICES``
878# enable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400879function enable_service() {
880 local tmpsvcs="${ENABLED_SERVICES}"
881 for service in $@; do
882 if ! is_service_enabled $service; then
883 tmpsvcs+=",$service"
884 fi
885 done
886 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
887 disable_negated_services
888}
889
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500890
Doug Hellmannf04178f2012-07-05 17:10:03 -0400891# disable_service() removes the services passed as argument to the
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500892# ``ENABLED_SERVICES`` list, if they are present.
Doug Hellmannf04178f2012-07-05 17:10:03 -0400893#
894# For example:
Joe Gordon6fd28112012-11-13 16:55:41 -0800895# disable_service rabbit
Doug Hellmannf04178f2012-07-05 17:10:03 -0400896#
897# This function does not know about the special cases
Mark McClainb05c8762013-07-06 23:29:39 -0400898# for nova, glance, and neutron built into is_service_enabled().
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500899# Uses global ``ENABLED_SERVICES``
900# disable_service service [service ...]
Doug Hellmannf04178f2012-07-05 17:10:03 -0400901function disable_service() {
902 local tmpsvcs=",${ENABLED_SERVICES},"
903 local service
904 for service in $@; do
905 if is_service_enabled $service; then
906 tmpsvcs=${tmpsvcs//,$service,/,}
907 fi
908 done
909 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
910}
911
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500912
Doug Hellmannf04178f2012-07-05 17:10:03 -0400913# disable_all_services() removes all current services
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500914# from ``ENABLED_SERVICES`` to reset the configuration
Doug Hellmannf04178f2012-07-05 17:10:03 -0400915# before a minimal installation
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500916# Uses global ``ENABLED_SERVICES``
917# disable_all_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400918function disable_all_services() {
919 ENABLED_SERVICES=""
920}
921
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500922
923# Remove all services starting with '-'. For example, to install all default
Joe Gordon6fd28112012-11-13 16:55:41 -0800924# services except rabbit (rabbit) set in ``localrc``:
925# ENABLED_SERVICES+=",-rabbit"
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500926# Uses global ``ENABLED_SERVICES``
927# disable_negated_services
Doug Hellmannf04178f2012-07-05 17:10:03 -0400928function disable_negated_services() {
929 local tmpsvcs="${ENABLED_SERVICES}"
930 local service
931 for service in ${tmpsvcs//,/ }; do
932 if [[ ${service} == -* ]]; then
933 tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
934 fi
935 done
936 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
937}
Dean Troyer489bd2a2012-03-02 10:44:29 -0600938
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500939
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500940# Distro-agnostic package installer
941# install_package package [package ...]
942function install_package() {
Vincent Untzc18b9652012-12-04 12:36:34 +0100943 if is_ubuntu; then
Vincent Untzc0482e62012-06-12 11:30:43 +0200944 [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update
945 NO_UPDATE_REPOS=True
946
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500947 apt_get install "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100948 elif is_fedora; then
949 yum_install "$@"
950 elif is_suse; then
951 zypper_install "$@"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500952 else
Vincent Untz00011c02012-12-06 09:56:32 +0100953 exit_distro_not_supported "installing packages"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500954 fi
955}
956
957
Dean Troyer995eb922013-03-07 16:11:40 -0600958# Distro-agnostic package uninstaller
959# uninstall_package package [package ...]
960function uninstall_package() {
961 if is_ubuntu; then
962 apt_get purge "$@"
963 elif is_fedora; then
Ian Wienand2c678cc2013-03-20 13:00:44 +1100964 sudo yum remove -y "$@"
Dean Troyer995eb922013-03-07 16:11:40 -0600965 elif is_suse; then
Adam Spiers6d8fce72013-10-01 15:59:05 +0100966 sudo zypper rm "$@"
Dean Troyer995eb922013-03-07 16:11:40 -0600967 else
968 exit_distro_not_supported "uninstalling packages"
969 fi
970}
971
972
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200973# Distro-agnostic function to tell if a package is installed
974# is_package_installed package [package ...]
975function is_package_installed() {
976 if [[ -z "$@" ]]; then
977 return 1
978 fi
979
980 if [[ -z "$os_PACKAGE" ]]; then
981 GetOSVersion
982 fi
Vincent Untzc18b9652012-12-04 12:36:34 +0100983
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200984 if [[ "$os_PACKAGE" = "deb" ]]; then
Dean Troyer04762cd2013-08-27 17:06:14 -0500985 dpkg -s "$@" > /dev/null 2> /dev/null
Vincent Untz00011c02012-12-06 09:56:32 +0100986 elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200987 rpm --quiet -q "$@"
Vincent Untz00011c02012-12-06 09:56:32 +0100988 else
989 exit_distro_not_supported "finding if a package is installed"
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200990 fi
991}
992
993
Dean Troyer489bd2a2012-03-02 10:44:29 -0600994# Test if the named environment variable is set and not zero length
995# is_set env-var
996function is_set() {
997 local var=\$"$1"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100998 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 -0600999}
1000
1001
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001002# Wrapper for ``pip install`` to set cache and proxy environment variables
Maru Newby3a87edd2012-10-25 23:01:06 +00001003# Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``,
Adam Spierscb961592013-10-05 12:11:07 +01001004# ``TRACK_DEPENDS``, ``*_proxy``
Dean Troyer7f9aa712012-01-31 12:11:56 -06001005# pip_install package [package ...]
1006function pip_install {
Dean Troyerd0b21e22012-03-07 14:52:25 -06001007 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001008 if [[ -z "$os_PACKAGE" ]]; then
1009 GetOSVersion
1010 fi
Dean Troyercc6b4432013-04-08 15:38:03 -05001011 if [[ $TRACK_DEPENDS = True ]]; then
Monty Taylor47f02062012-07-26 11:09:24 -05001012 source $DEST/.venv/bin/activate
1013 CMD_PIP=$DEST/.venv/bin/pip
1014 SUDO_PIP="env"
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001015 else
Monty Taylor47f02062012-07-26 11:09:24 -05001016 SUDO_PIP="sudo"
Vincent Untz8ec27222012-11-29 09:25:31 +01001017 CMD_PIP=$(get_pip_command)
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001018 fi
Ian Wienandd67dd872013-04-11 11:14:36 +10001019
Roman Gorodeckij99405a42013-08-07 09:20:36 -04001020 # Mirror option not needed anymore because pypi has CDN available,
1021 # but it's useful in certain circumstances
1022 PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False}
Maru Newby3a87edd2012-10-25 23:01:06 +00001023 if [[ "$PIP_USE_MIRRORS" != "False" ]]; then
1024 PIP_MIRROR_OPT="--use-mirrors"
1025 fi
Ian Wienandd67dd872013-04-11 11:14:36 +10001026
Ian Wienand31dcd3e2013-07-16 13:36:34 +10001027 # pip < 1.4 has a bug where it will use an already existing build
1028 # directory unconditionally. Say an earlier component installs
1029 # foo v1.1; pip will have built foo's source in
1030 # /tmp/$USER-pip-build. Even if a later component specifies foo <
1031 # 1.1, the existing extracted build will be used and cause
1032 # confusing errors. By creating unique build directories we avoid
Adam Spierscb961592013-10-05 12:11:07 +01001033 # this problem. See https://github.com/pypa/pip/issues/709
Ian Wienand31dcd3e2013-07-16 13:36:34 +10001034 local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX)
1035
Monty Taylor47f02062012-07-26 11:09:24 -05001036 $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \
Dean Troyer7f9aa712012-01-31 12:11:56 -06001037 HTTP_PROXY=$http_proxy \
1038 HTTPS_PROXY=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001039 NO_PROXY=$no_proxy \
Ian Wienand31dcd3e2013-07-16 13:36:34 +10001040 $CMD_PIP install --build=${pip_build_tmp} \
1041 $PIP_MIRROR_OPT $@ \
1042 && $SUDO_PIP rm -rf ${pip_build_tmp}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001043}
1044
1045
Ian Wienand31dcd3e2013-07-16 13:36:34 +10001046# Cleanup anything from /tmp on unstack
1047# clean_tmp
1048function cleanup_tmp {
1049 local tmp_dir=${TMPDIR:-/tmp}
1050
1051 # see comments in pip_install
1052 sudo rm -rf ${tmp_dir}/pip-build.*
1053}
1054
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001055# Service wrapper to restart services
1056# restart_service service-name
1057function restart_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001058 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001059 sudo /usr/sbin/service $1 restart
1060 else
1061 sudo /sbin/service $1 restart
1062 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001063}
1064
1065
Dean Troyer681f3fd2013-02-27 19:00:39 -06001066# _run_process() is designed to be backgrounded by run_process() to simulate a
1067# fork. It includes the dirty work of closing extra filehandles and preparing log
1068# files to produce the same logs as screen_it(). The log filename is derived
1069# from the service name and global-and-now-misnamed SCREEN_LOGDIR
1070# _run_process service "command-line"
1071function _run_process() {
1072 local service=$1
1073 local command="$2"
1074
1075 # Undo logging redirections and close the extra descriptors
1076 exec 1>&3
1077 exec 2>&3
1078 exec 3>&-
1079 exec 6>&-
1080
1081 if [[ -n ${SCREEN_LOGDIR} ]]; then
1082 exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1
1083 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1084
1085 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1086 export PYTHONUNBUFFERED=1
1087 fi
1088
1089 exec /bin/bash -c "$command"
1090 die "$service exec failure: $command"
1091}
1092
1093
1094# run_process() launches a child process that closes all file descriptors and
1095# then exec's the passed in command. This is meant to duplicate the semantics
1096# of screen_it() without screen. PIDs are written to
1097# $SERVICE_DIR/$SCREEN_NAME/$service.pid
1098# run_process service "command-line"
1099function run_process() {
1100 local service=$1
1101 local command="$2"
1102
1103 # Spawn the child process
1104 _run_process "$service" "$command" &
1105 echo $!
1106}
1107
1108
Dean Troyer15733352012-09-06 11:51:30 -05001109# Helper to launch a service in a named screen
1110# screen_it service "command-line"
1111function screen_it {
Dean Troyer15733352012-09-06 11:51:30 -05001112 SCREEN_NAME=${SCREEN_NAME:-stack}
jiajun xua9414242012-12-06 16:30:57 +08001113 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Dean Troyer681f3fd2013-02-27 19:00:39 -06001114 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
jiajun xua9414242012-12-06 16:30:57 +08001115
Dean Troyer15733352012-09-06 11:51:30 -05001116 if is_service_enabled $1; then
1117 # Append the service to the screen rc file
1118 screen_rc "$1" "$2"
1119
Dean Troyer681f3fd2013-02-27 19:00:39 -06001120 if [[ "$USE_SCREEN" = "True" ]]; then
1121 screen -S $SCREEN_NAME -X screen -t $1
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +00001122
Dean Troyer681f3fd2013-02-27 19:00:39 -06001123 if [[ -n ${SCREEN_LOGDIR} ]]; then
1124 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
1125 screen -S $SCREEN_NAME -p $1 -X log on
1126 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1127 fi
Jeremy Stanley25ebbcd2013-02-17 15:45:55 +00001128
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001129 # sleep to allow bash to be ready to be send the command - we are
1130 # creating a new window in screen and then sends characters, so if
1131 # bash isn't running by the time we send the command, nothing happens
1132 sleep 1.5
Dean Troyer15733352012-09-06 11:51:30 -05001133
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001134 NL=`echo -ne '\015'`
Dean Troyer9fc87922013-05-22 17:19:06 -05001135 # This fun command does the following:
1136 # - the passed server command is backgrounded
1137 # - the pid of the background process is saved in the usual place
1138 # - the server process is brought back to the foreground
1139 # - if the server process exits prematurely the fg command errors
1140 # and a message is written to stdout and the service failure file
1141 # The pid saved can be used in screen_stop() as a process group
1142 # id to kill off all child processes
1143 screen -S $SCREEN_NAME -p $1 -X stuff "$2 & echo \$! >$SERVICE_DIR/$SCREEN_NAME/$1.pid; fg || echo \"$1 failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL"
Vishvananda Ishaya58e21342013-02-11 16:48:12 -08001144 else
Dean Troyer681f3fd2013-02-27 19:00:39 -06001145 # Spawn directly without screen
Dean Troyer9fc87922013-05-22 17:19:06 -05001146 run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1147 fi
1148 fi
1149}
1150
1151
1152# Stop a service in screen
Dean Troyer579af5d2014-01-23 11:32:22 -06001153# If a PID is available use it, kill the whole process group via TERM
1154# If screen is being used kill the screen window; this will catch processes
1155# that did not leave a PID behind
Dean Troyer9fc87922013-05-22 17:19:06 -05001156# screen_stop service
1157function screen_stop() {
1158 SCREEN_NAME=${SCREEN_NAME:-stack}
1159 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1160 USE_SCREEN=$(trueorfalse True $USE_SCREEN)
1161
1162 if is_service_enabled $1; then
1163 # Kill via pid if we have one available
1164 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$1.pid ]]; then
Dean Troyer579af5d2014-01-23 11:32:22 -06001165 pkill -TERM -P -$(cat $SERVICE_DIR/$SCREEN_NAME/$1.pid)
Dean Troyer9fc87922013-05-22 17:19:06 -05001166 rm $SERVICE_DIR/$SCREEN_NAME/$1.pid
1167 fi
1168 if [[ "$USE_SCREEN" = "True" ]]; then
1169 # Clean up the screen window
1170 screen -S $SCREEN_NAME -p $1 -X kill
Dean Troyer15733352012-09-06 11:51:30 -05001171 fi
Dean Troyer15733352012-09-06 11:51:30 -05001172 fi
1173}
1174
1175
1176# Screen rc file builder
1177# screen_rc service "command-line"
1178function screen_rc {
1179 SCREEN_NAME=${SCREEN_NAME:-stack}
1180 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1181 if [[ ! -e $SCREENRC ]]; then
1182 # Name the screen session
1183 echo "sessionname $SCREEN_NAME" > $SCREENRC
1184 # Set a reasonable statusbar
1185 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
Steven Dake30396572013-06-30 16:11:54 -07001186 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1187 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
Dean Troyer15733352012-09-06 11:51:30 -05001188 echo "screen -t shell bash" >> $SCREENRC
1189 fi
1190 # If this service doesn't already exist in the screenrc file
1191 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1192 NL=`echo -ne '\015'`
1193 echo "screen -t $1 bash" >> $SCREENRC
1194 echo "stuff \"$2$NL\"" >> $SCREENRC
Darragh O'Reillybf36e8e2013-12-09 13:16:16 +00001195
1196 if [[ -n ${SCREEN_LOGDIR} ]]; then
1197 echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC
1198 echo "log on" >>$SCREENRC
1199 fi
Dean Troyer15733352012-09-06 11:51:30 -05001200 fi
1201}
1202
Dean Troyer1a6d4492013-06-03 16:47:36 -05001203
Adam Spierscb961592013-10-05 12:11:07 +01001204# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1205# This is used for ``service_check`` when all the ``screen_it`` are called finished
jiajun xua9414242012-12-06 16:30:57 +08001206# init_service_check
1207function init_service_check() {
1208 SCREEN_NAME=${SCREEN_NAME:-stack}
1209 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1210
1211 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1212 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1213 fi
1214
1215 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1216}
1217
Dean Troyer1a6d4492013-06-03 16:47:36 -05001218
jiajun xua9414242012-12-06 16:30:57 +08001219# Helper to get the status of each running service
1220# service_check
1221function service_check() {
1222 local service
1223 local failures
1224 SCREEN_NAME=${SCREEN_NAME:-stack}
1225 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1226
1227
1228 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1229 echo "No service status directory found"
1230 return
1231 fi
1232
1233 # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
1234 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null`
1235
1236 for service in $failures; do
1237 service=`basename $service`
Bob Ball46287d82013-07-30 09:43:17 +01001238 service=${service%.failure}
jiajun xua9414242012-12-06 16:30:57 +08001239 echo "Error: Service $service is not running"
1240 done
1241
1242 if [ -n "$failures" ]; then
1243 echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
1244 fi
1245}
Dean Troyer15733352012-09-06 11:51:30 -05001246
Doug Hellmanne7002672013-09-05 08:10:07 -04001247# Returns true if the directory is on a filesystem mounted via NFS.
1248function is_nfs_directory() {
1249 local mount_type=`stat -f -L -c %T $1`
1250 test "$mount_type" == "nfs"
1251}
1252
1253# Only run the command if the target file (the last arg) is not on an
1254# NFS filesystem.
1255function _safe_permission_operation() {
1256 local args=( $@ )
1257 local last
1258 local sudo_cmd
1259 local dir_to_check
1260
1261 let last="${#args[*]} - 1"
1262
1263 dir_to_check=${args[$last]}
1264 if [ ! -d "$dir_to_check" ]; then
1265 dir_to_check=`dirname "$dir_to_check"`
1266 fi
1267
1268 if is_nfs_directory "$dir_to_check" ; then
1269 return 0
1270 fi
1271
1272 if [[ $TRACK_DEPENDS = True ]]; then
1273 sudo_cmd="env"
1274 else
1275 sudo_cmd="sudo"
1276 fi
1277
1278 $sudo_cmd $@
1279}
1280
1281# Only change ownership of a file or directory if it is not on an NFS
1282# filesystem.
1283function safe_chown() {
1284 _safe_permission_operation chown $@
1285}
1286
1287# Only change permissions of a file or directory if it is not on an
1288# NFS filesystem.
1289function safe_chmod() {
1290 _safe_permission_operation chmod $@
1291}
Dean Troyer1a6d4492013-06-03 16:47:36 -05001292
Monty Taylor408a4a72013-08-02 15:43:47 -04001293# ``pip install -e`` the package, which processes the dependencies
1294# using pip before running `setup.py develop`
Doug Hellmannaaac4ee2013-11-18 22:12:46 +00001295#
1296# Updates the dependencies in project_dir from the
1297# openstack/requirements global list before installing anything.
1298#
1299# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001300# setup_develop directory
1301function setup_develop() {
Sean Dague6c844632013-07-31 06:50:14 -04001302 local project_dir=$1
Sean Dague6c844632013-07-31 06:50:14 -04001303
1304 echo "cd $REQUIREMENTS_DIR; $SUDO_CMD python update.py $project_dir"
1305
Dean Troyer62d1d692013-08-01 17:40:40 -05001306 # Don't update repo if local changes exist
IWAMOTO Toshihiro0b8f6e02014-01-23 12:02:34 +09001307 # Don't use buggy "git diff --quiet"
1308 (cd $project_dir && git diff --exit-code >/dev/null)
Doug Hellmannc3431bf2013-09-06 15:30:22 -04001309 local update_requirements=$?
1310
1311 if [ $update_requirements -eq 0 ]; then
Dean Troyer62d1d692013-08-01 17:40:40 -05001312 (cd $REQUIREMENTS_DIR; \
1313 $SUDO_CMD python update.py $project_dir)
1314 fi
Sean Dague6c844632013-07-31 06:50:14 -04001315
Doug Hellmannaaac4ee2013-11-18 22:12:46 +00001316 setup_develop_no_requirements_update $project_dir
Doug Hellmannc3431bf2013-09-06 15:30:22 -04001317
Sean Daguefd98edb2013-10-24 14:57:59 -04001318 # We've just gone and possibly modified the user's source tree in an
1319 # automated way, which is considered bad form if it's a development
1320 # tree because we've screwed up their next git checkin. So undo it.
1321 #
1322 # However... there are some circumstances, like running in the gate
1323 # where we really really want the overridden version to stick. So provide
1324 # a variable that tells us whether or not we should UNDO the requirements
1325 # changes (this will be set to False in the OpenStack ci gate)
DennyZhang89d41ca2013-11-01 15:41:01 -05001326 if [ $UNDO_REQUIREMENTS = "True" ]; then
Sean Daguefd98edb2013-10-24 14:57:59 -04001327 if [ $update_requirements -eq 0 ]; then
1328 (cd $project_dir && git reset --hard)
1329 fi
Doug Hellmannc3431bf2013-09-06 15:30:22 -04001330 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001331}
1332
Doug Hellmannaaac4ee2013-11-18 22:12:46 +00001333# ``pip install -e`` the package, which processes the dependencies
1334# using pip before running `setup.py develop`
1335# Uses globals ``STACK_USER``
1336# setup_develop_no_requirements_update directory
1337function setup_develop_no_requirements_update() {
1338 local project_dir=$1
1339
1340 pip_install -e $project_dir
1341 # ensure that further actions can do things like setup.py sdist
1342 safe_chown -R $STACK_USER $1/*.egg-info
1343}
1344
Dean Troyerbbafb1b2012-06-11 16:51:39 -05001345
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001346# Service wrapper to start services
1347# start_service service-name
1348function start_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001349 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001350 sudo /usr/sbin/service $1 start
1351 else
1352 sudo /sbin/service $1 start
1353 fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001354}
1355
1356
1357# Service wrapper to stop services
1358# stop_service service-name
1359function stop_service() {
Vincent Untzc18b9652012-12-04 12:36:34 +01001360 if is_ubuntu; then
Dean Troyer5218d452012-02-04 02:13:23 -06001361 sudo /usr/sbin/service $1 stop
1362 else
1363 sudo /sbin/service $1 stop
1364 fi
Dean Troyer7f9aa712012-01-31 12:11:56 -06001365}
1366
1367
1368# Normalize config values to True or False
Sirushti Murugesana8d41e32013-09-25 11:30:31 +05301369# Accepts as False: 0 no No NO false False FALSE
1370# Accepts as True: 1 yes Yes YES true True TRUE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001371# VAR=$(trueorfalse default-value test-value)
Dean Troyer7f9aa712012-01-31 12:11:56 -06001372function trueorfalse() {
1373 local default=$1
1374 local testval=$2
1375
1376 [[ -z "$testval" ]] && { echo "$default"; return; }
Sirushti Murugesana8d41e32013-09-25 11:30:31 +05301377 [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
1378 [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
Dean Troyer7f9aa712012-01-31 12:11:56 -06001379 echo "$default"
1380}
Dean Troyer27e32692012-03-16 16:16:56 -05001381
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001382
Adam Spierscb961592013-10-05 12:11:07 +01001383# Retrieve an image from a URL and upload into Glance.
Dean Troyerca0e3d02012-04-13 15:58:37 -05001384# Uses the following variables:
Adam Spierscb961592013-10-05 12:11:07 +01001385#
1386# - ``FILES`` must be set to the cache dir
1387# - ``GLANCE_HOSTPORT``
1388#
Dean Troyerca0e3d02012-04-13 15:58:37 -05001389# upload_image image-url glance-token
1390function upload_image() {
1391 local image_url=$1
1392 local token=$2
1393
1394 # Create a directory for the downloaded image tarballs.
1395 mkdir -p $FILES/images
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -08001396 IMAGE_FNAME=`basename "$image_url"`
Arnaud Legendre3e439442013-11-15 16:06:03 -08001397 if [[ $image_url != file* ]]; then
1398 # Downloads the image (uec ami+aki style), then extracts it.
Arnaud Legendre3e439442013-11-15 16:06:03 -08001399 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
Isaku Yamahata6681a4f2014-01-10 15:28:29 +09001400 wget -c $image_url -O $FILES/$IMAGE_FNAME
1401 if [[ $? -ne 0 ]]; then
1402 echo "Not found: $image_url"
1403 return
1404 fi
Arnaud Legendre3e439442013-11-15 16:06:03 -08001405 fi
1406 IMAGE="$FILES/${IMAGE_FNAME}"
1407 else
1408 # File based URL (RFC 1738): file://host/path
1409 # Remote files are not considered here.
1410 # *nix: file:///home/user/path/file
1411 # windows: file:///C:/Documents%20and%20Settings/user/path/file
1412 IMAGE=$(echo $image_url | sed "s/^file:\/\///g")
1413 if [[ ! -f $IMAGE || "$(stat -c "%s" $IMAGE)" == "0" ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -05001414 echo "Not found: $image_url"
1415 return
1416 fi
1417 fi
1418
1419 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
1420 if [[ "$image_url" =~ 'openvz' ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -05001421 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
1422 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}"
1423 return
1424 fi
1425
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -07001426 # vmdk format images
1427 if [[ "$image_url" =~ '.vmdk' ]]; then
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -07001428 IMAGE_NAME="${IMAGE_FNAME%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -07001429
1430 # Before we can upload vmdk type images to glance, we need to know it's
1431 # disk type, storage adapter, and networking adapter. These values are
Ryan Hsubfb3e5e2013-11-11 21:20:14 -08001432 # passed to glance as custom properties.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -07001433 # We take these values from the vmdk file if populated. Otherwise, we use
Ryan Hsua6273b92013-09-04 23:51:29 -07001434 # vmdk filename, which is expected in the following format:
1435 #
Ryan Hsubfb3e5e2013-11-11 21:20:14 -08001436 # <name>-<disk type>;<storage adapter>;<network adapter>
Ryan Hsua6273b92013-09-04 23:51:29 -07001437 #
1438 # If the filename does not follow the above format then the vsphere
1439 # driver will supply default values.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -07001440
Ryan Hsubfb3e5e2013-11-11 21:20:14 -08001441 vmdk_adapter_type=""
1442 vmdk_disktype=""
1443 vmdk_net_adapter=""
1444
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -07001445 # vmdk adapter type
1446 vmdk_adapter_type="$(head -25 $IMAGE | grep -a -F -m 1 'ddb.adapterType =' $IMAGE)"
1447 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
1448 vmdk_adapter_type="${vmdk_adapter_type%?}"
1449
1450 # vmdk disk type
1451 vmdk_create_type="$(head -25 $IMAGE | grep -a -F -m 1 'createType=' $IMAGE)"
1452 vmdk_create_type="${vmdk_create_type#*\"}"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -08001453 vmdk_create_type="${vmdk_create_type%\"*}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -08001454
1455 descriptor_data_pair_msg="Monolithic flat and VMFS disks "`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +09001456 `"should use a descriptor-data pair."
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -07001457 if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then
1458 vmdk_disktype="sparse"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -08001459 elif [[ "$vmdk_create_type" = "monolithicFlat" || \
1460 "$vmdk_create_type" = "vmfs" ]]; then
1461 # Attempt to retrieve the *-flat.vmdk
1462 flat_fname="$(head -25 $IMAGE | grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $IMAGE)"
1463 flat_fname="${flat_fname#*\"}"
1464 flat_fname="${flat_fname%?}"
1465 if [[ -z "$flat_name" ]]; then
1466 flat_fname="$IMAGE_NAME-flat.vmdk"
1467 fi
1468 path_len=`expr ${#image_url} - ${#IMAGE_FNAME}`
1469 flat_url="${image_url:0:$path_len}$flat_fname"
1470 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +09001471 `" Attempt to retrieve the *-flat.vmdk: $flat_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -08001472 if [[ $flat_url != file* ]]; then
1473 if [[ ! -f $FILES/$flat_fname || \
1474 "$(stat -c "%s" $FILES/$flat_fname)" = "0" ]]; then
1475 wget -c $flat_url -O $FILES/$flat_fname
1476 if [[ $? -ne 0 ]]; then
1477 echo "Flat disk not found: $flat_url"
1478 flat_found=false
1479 fi
1480 fi
1481 if $flat_found; then
1482 IMAGE="$FILES/${flat_fname}"
1483 fi
1484 else
1485 IMAGE=$(echo $flat_url | sed "s/^file:\/\///g")
1486 if [[ ! -f $IMAGE || "$(stat -c "%s" $IMAGE)" == "0" ]]; then
1487 echo "Flat disk not found: $flat_url"
1488 flat_found=false
1489 fi
1490 if ! $flat_found; then
1491 IMAGE=$(echo $image_url | sed "s/^file:\/\///g")
1492 fi
1493 fi
1494 if $flat_found; then
1495 IMAGE_NAME="${flat_fname}"
1496 fi
1497 vmdk_disktype="preallocated"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -08001498 elif [[ "$vmdk_create_type" = "streamOptimized" ]]; then
1499 vmdk_disktype="streamOptimized"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -08001500 elif [[ -z "$vmdk_create_type" ]]; then
1501 # *-flat.vmdk provided: attempt to retrieve the descriptor (*.vmdk)
1502 # to retrieve appropriate metadata
1503 if [[ ${IMAGE_NAME: -5} != "-flat" ]]; then
1504 warn $LINENO "Expected filename suffix: '-flat'."`
1505 `" Filename provided: ${IMAGE_NAME}"
1506 else
1507 descriptor_fname="${IMAGE_NAME:0:${#IMAGE_NAME} - 5}.vmdk"
1508 path_len=`expr ${#image_url} - ${#IMAGE_FNAME}`
1509 flat_path="${image_url:0:$path_len}"
1510 descriptor_url=$flat_path$descriptor_fname
1511 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +09001512 `" Attempt to retrieve the descriptor *.vmdk: $descriptor_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -08001513 if [[ $flat_path != file* ]]; then
1514 if [[ ! -f $FILES/$descriptor_fname || \
1515 "$(stat -c "%s" $FILES/$descriptor_fname)" = "0" ]]; then
1516 wget -c $descriptor_url -O $FILES/$descriptor_fname
1517 if [[ $? -ne 0 ]]; then
1518 warn $LINENO "Descriptor not found $descriptor_url"
1519 descriptor_found=false
1520 fi
1521 fi
1522 descriptor_url="$FILES/$descriptor_fname"
1523 else
1524 descriptor_url=$(echo $descriptor_url | sed "s/^file:\/\///g")
1525 if [[ ! -f $descriptor_url || \
1526 "$(stat -c "%s" $descriptor_url)" == "0" ]]; then
Isaku Yamahata6681a4f2014-01-10 15:28:29 +09001527 warn $LINENO "Descriptor not found $descriptor_url"
1528 descriptor_found=false
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -08001529 fi
1530 fi
1531 if $descriptor_found; then
1532 vmdk_adapter_type="$(head -25 $descriptor_url |"`
1533 `"grep -a -F -m 1 'ddb.adapterType =' $descriptor_url)"
1534 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
1535 vmdk_adapter_type="${vmdk_adapter_type%?}"
Isaku Yamahata6681a4f2014-01-10 15:28:29 +09001536 fi
1537 fi
Isaku Yamahata6681a4f2014-01-10 15:28:29 +09001538 vmdk_disktype="preallocated"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -07001539 else
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -07001540 vmdk_disktype="preallocated"
1541 fi
Ryan Hsubfb3e5e2013-11-11 21:20:14 -08001542
1543 # NOTE: For backwards compatibility reasons, colons may be used in place
1544 # of semi-colons for property delimiters but they are not permitted
1545 # characters in NTFS filesystems.
Arnaud Legendreb93cd642014-01-23 17:12:21 -08001546 property_string=`echo "$IMAGE_NAME" | grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$'`
Ryan Hsubfb3e5e2013-11-11 21:20:14 -08001547 IFS=':;' read -a props <<< "$property_string"
1548 vmdk_disktype="${props[0]:-$vmdk_disktype}"
1549 vmdk_adapter_type="${props[1]:-$vmdk_adapter_type}"
1550 vmdk_net_adapter="${props[2]:-$vmdk_net_adapter}"
Ryan Hsua6273b92013-09-04 23:51:29 -07001551
Ryan Hsu49f44862013-10-03 22:27:03 -07001552 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 -07001553 return
1554 fi
1555
Mate Lakatbc2ef922013-08-15 18:06:59 +01001556 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -05001557 # and should not be decompressed prior to loading
1558 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
Davanum Srinivas316ed6c2013-02-06 15:29:49 -05001559 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
1560 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}"
1561 return
1562 fi
1563
Mate Lakatbc2ef922013-08-15 18:06:59 +01001564 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
1565 # and should not be decompressed prior to loading.
1566 # Setting metadata, so PV mode is used.
1567 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
Mate Lakatbc2ef922013-08-15 18:06:59 +01001568 IMAGE_NAME="${IMAGE_FNAME%.xen-raw.tgz}"
1569 glance \
Sean Dague537d4022013-10-22 07:43:22 -04001570 --os-auth-token $token \
1571 --os-image-url http://$GLANCE_HOSTPORT \
1572 image-create \
Mate Lakatbc2ef922013-08-15 18:06:59 +01001573 --name "$IMAGE_NAME" --is-public=True \
1574 --container-format=tgz --disk-format=raw \
1575 --property vm_mode=xen < "${IMAGE}"
1576 return
1577 fi
1578
Dean Troyerca0e3d02012-04-13 15:58:37 -05001579 KERNEL=""
1580 RAMDISK=""
1581 DISK_FORMAT=""
1582 CONTAINER_FORMAT=""
1583 UNPACK=""
1584 case "$IMAGE_FNAME" in
1585 *.tar.gz|*.tgz)
1586 # Extract ami and aki files
1587 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
1588 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
1589 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
1590 xdir="$FILES/images/$IMAGE_NAME"
1591 rm -Rf "$xdir";
1592 mkdir "$xdir"
1593 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
1594 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -04001595 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001596 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -04001597 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001598 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -04001599 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001600 if [[ -z "$IMAGE_NAME" ]]; then
1601 IMAGE_NAME=$(basename "$IMAGE" ".img")
1602 fi
1603 ;;
1604 *.img)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001605 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -05001606 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
1607 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
1608 DISK_FORMAT=$format
1609 else
1610 DISK_FORMAT=raw
1611 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -05001612 CONTAINER_FORMAT=bare
1613 ;;
1614 *.img.gz)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001615 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
1616 DISK_FORMAT=raw
1617 CONTAINER_FORMAT=bare
1618 UNPACK=zcat
1619 ;;
1620 *.qcow2)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001621 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
1622 DISK_FORMAT=qcow2
1623 CONTAINER_FORMAT=bare
1624 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +01001625 *.iso)
Jonathan Michalon06802042013-03-21 14:29:58 +01001626 IMAGE_NAME=$(basename "$IMAGE" ".iso")
1627 DISK_FORMAT=iso
1628 CONTAINER_FORMAT=bare
1629 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -05001630 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
1631 esac
1632
Rafael Folcoab775872013-12-02 14:04:32 -02001633 if is_arch "ppc64"; then
1634 IMG_PROPERTY="--property hw_disk_bus=scsi --property hw_cdrom_bus=scsi"
1635 fi
1636
Dean Troyerca0e3d02012-04-13 15:58:37 -05001637 if [ "$CONTAINER_FORMAT" = "bare" ]; then
1638 if [ "$UNPACK" = "zcat" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -02001639 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" $IMG_PROPERTY --is-public True --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < <(zcat --force "${IMAGE}")
Dean Troyerca0e3d02012-04-13 15:58:37 -05001640 else
Rafael Folcoab775872013-12-02 14:04:32 -02001641 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" $IMG_PROPERTY --is-public True --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < "${IMAGE}"
Dean Troyerca0e3d02012-04-13 15:58:37 -05001642 fi
1643 else
1644 # Use glance client to add the kernel the root filesystem.
1645 # We parse the results of the first upload to get the glance ID of the
1646 # kernel for use when uploading the root filesystem.
1647 KERNEL_ID=""; RAMDISK_ID="";
1648 if [ -n "$KERNEL" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -02001649 KERNEL_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-kernel" $IMG_PROPERTY --is-public True --container-format aki --disk-format aki < "$KERNEL" | grep ' id ' | get_field 2)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001650 fi
1651 if [ -n "$RAMDISK" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -02001652 RAMDISK_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-ramdisk" $IMG_PROPERTY --is-public True --container-format ari --disk-format ari < "$RAMDISK" | grep ' id ' | get_field 2)
Dean Troyerca0e3d02012-04-13 15:58:37 -05001653 fi
Rafael Folcoab775872013-12-02 14:04:32 -02001654 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "${IMAGE_NAME%.img}" $IMG_PROPERTY --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 -05001655 fi
1656}
1657
Dean Troyer1a6d4492013-06-03 16:47:36 -05001658
Dean Troyerc1b486a2012-11-05 14:26:09 -06001659# Set the database backend to use
1660# When called from stackrc/localrc DATABASE_BACKENDS has not been
1661# initialized yet, just save the configuration selection and call back later
1662# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +01001663#
1664# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -06001665function use_database {
1666 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -06001667 # No backends registered means this is likely called from ``localrc``
1668 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -06001669 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +00001670 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 +01001671 else
Dean Troyerafc29fe2013-02-07 15:56:24 -06001672 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +01001673 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -06001674 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -06001675}
1676
Dean Troyer1a6d4492013-06-03 16:47:36 -05001677
Terry Wilson428af5a2012-11-01 16:12:39 -04001678# Toggle enable/disable_service for services that must run exclusive of each other
1679# $1 The name of a variable containing a space-separated list of services
1680# $2 The name of a variable in which to store the enabled service's name
1681# $3 The name of the service to enable
1682function use_exclusive_service {
1683 local options=${!1}
1684 local selection=$3
1685 out=$2
1686 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
1687 for opt in $options;do
1688 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1689 done
1690 eval "$out=$selection"
1691 return 0
1692}
Dean Troyerca0e3d02012-04-13 15:58:37 -05001693
Dean Troyer1a6d4492013-06-03 16:47:36 -05001694
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001695# Wait for an HTTP server to start answering requests
1696# wait_for_service timeout url
1697function wait_for_service() {
1698 local timeout=$1
1699 local url=$2
JUN JIE NAN0aa85342013-09-13 15:47:09 +08001700 timeout $timeout sh -c "while ! curl --noproxy '*' -s $url >/dev/null; do sleep 1; done"
Dean Troyer3a3a2ba2012-12-11 15:26:24 -06001701}
1702
Dean Troyer1a6d4492013-06-03 16:47:36 -05001703
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001704# Wrapper for ``yum`` to set proxy environment variables
Adam Spierscb961592013-10-05 12:11:07 +01001705# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001706# yum_install package [package ...]
1707function yum_install() {
1708 [[ "$OFFLINE" = "True" ]] && return
1709 local sudo="sudo"
1710 [[ "$(id -u)" = "0" ]] && sudo="env"
1711 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
Osamu Habuka7abe4f22012-07-25 12:39:32 +09001712 no_proxy=$no_proxy \
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001713 yum install -y "$@"
1714}
1715
Dean Troyer1a6d4492013-06-03 16:47:36 -05001716
1717# zypper wrapper to set arguments correctly
1718# zypper_install package [package ...]
1719function zypper_install() {
1720 [[ "$OFFLINE" = "True" ]] && return
1721 local sudo="sudo"
1722 [[ "$(id -u)" = "0" ]] && sudo="env"
1723 $sudo http_proxy=$http_proxy https_proxy=$https_proxy \
1724 zypper --non-interactive install --auto-agree-with-licenses "$@"
1725}
1726
1727
Nachi Uenofda946e2012-10-24 17:26:02 -07001728# ping check
1729# Uses globals ``ENABLED_SERVICES``
Dean Troyer1a6d4492013-06-03 16:47:36 -05001730# ping_check from-net ip boot-timeout expected
Nachi Uenofda946e2012-10-24 17:26:02 -07001731function ping_check() {
Mark McClainb05c8762013-07-06 23:29:39 -04001732 if is_service_enabled neutron; then
1733 _ping_check_neutron "$1" $2 $3 $4
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001734 return
1735 fi
1736 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -07001737}
1738
1739# ping check for nova
1740# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
1741function _ping_check_novanet() {
1742 local from_net=$1
1743 local ip=$2
1744 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001745 local expected=${4:-"True"}
1746 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -07001747 MULTI_HOST=`trueorfalse False $MULTI_HOST`
1748 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
Nachi Uenofda946e2012-10-24 17:26:02 -07001749 return
1750 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001751 if [[ "$expected" = "True" ]]; then
1752 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
1753 else
1754 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
1755 fi
1756 if ! timeout $boot_timeout sh -c "$check_command"; then
1757 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -08001758 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001759 else
Nachi Ueno07115eb2013-02-26 12:38:18 -08001760 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001761 fi
Nachi Uenofda946e2012-10-24 17:26:02 -07001762 fi
1763}
1764
Nachi Ueno6769b162013-08-12 18:18:56 -07001765# Get ip of instance
1766function get_instance_ip(){
1767 local vm_id=$1
1768 local network_name=$2
1769 local nova_result="$(nova show $vm_id)"
1770 local ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
1771 if [[ $ip = "" ]];then
1772 echo "$nova_result"
1773 die $LINENO "[Fail] Coudn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -07001774 fi
1775 echo $ip
1776}
Dean Troyer1a6d4492013-06-03 16:47:36 -05001777
Nachi Uenofda946e2012-10-24 17:26:02 -07001778# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001779
Dean Troyer1a6d4492013-06-03 16:47:36 -05001780# ssh_check net-name key-file floating-ip default-user active-timeout
Nachi Uenofda946e2012-10-24 17:26:02 -07001781function ssh_check() {
Mark McClainb05c8762013-07-06 23:29:39 -04001782 if is_service_enabled neutron; then
1783 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07001784 return
1785 fi
1786 _ssh_check_novanet "$1" $2 $3 $4 $5
1787}
1788
1789function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -07001790 local NET_NAME=$1
1791 local KEY_FILE=$2
1792 local FLOATING_IP=$3
1793 local DEFAULT_INSTANCE_USER=$4
1794 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -06001795 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -05001796 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 -08001797 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -07001798 fi
1799}
Dean Troyer13dc5cc2012-03-27 14:50:45 -05001800
Vincent Untz856a11e2012-11-21 16:04:12 +01001801
Vincent Untz856a11e2012-11-21 16:04:12 +01001802# Add a user to a group.
1803# add_user_to_group user group
1804function add_user_to_group() {
1805 local user=$1
1806 local group=$2
1807
1808 if [[ -z "$os_VENDOR" ]]; then
1809 GetOSVersion
1810 fi
1811
1812 # SLE11 and openSUSE 12.2 don't have the usual usermod
1813 if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1814 sudo usermod -a -G "$group" "$user"
1815 else
1816 sudo usermod -A "$group" "$user"
1817 fi
1818}
1819
1820
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001821# Get the path to the direcotry where python executables are installed.
1822# get_python_exec_prefix
1823function get_python_exec_prefix() {
Martin Vidner4f9b33d2013-06-27 13:11:22 +00001824 if is_fedora || is_suse; then
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001825 echo "/usr/bin"
1826 else
1827 echo "/usr/local/bin"
1828 fi
1829}
1830
Dean Troyer1a6d4492013-06-03 16:47:36 -05001831
Vincent Untz856a11e2012-11-21 16:04:12 +01001832# Get the location of the $module-rootwrap executables, where module is cinder
1833# or nova.
1834# get_rootwrap_location module
1835function get_rootwrap_location() {
1836 local module=$1
1837
Jakub Ruzicka4196d552013-01-30 15:35:54 +01001838 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +01001839}
1840
Dean Troyer1a6d4492013-06-03 16:47:36 -05001841
Vincent Untz8ec27222012-11-29 09:25:31 +01001842# Get the path to the pip command.
1843# get_pip_command
1844function get_pip_command() {
Dean Troyerd2cfcaa2013-08-01 14:17:27 -05001845 which pip || which pip-python
Ian Wienand535a8142013-05-15 09:25:27 +10001846
1847 if [ $? -ne 0 ]; then
1848 die $LINENO "Unable to find pip; cannot continue"
1849 fi
Vincent Untz8ec27222012-11-29 09:25:31 +01001850}
Vincent Untz856a11e2012-11-21 16:04:12 +01001851
Dean Troyer1a6d4492013-06-03 16:47:36 -05001852
Ian Wienand0488edd2013-04-11 12:04:36 +10001853# Path permissions sanity check
1854# check_path_perm_sanity path
1855function check_path_perm_sanity() {
1856 # Ensure no element of the path has 0700 permissions, which is very
1857 # likely to cause issues for daemons. Inspired by default 0700
1858 # homedir permissions on RHEL and common practice of making DEST in
1859 # the stack user's homedir.
1860
1861 local real_path=$(readlink -f $1)
1862 local rebuilt_path=""
1863 for i in $(echo ${real_path} | tr "/" " "); do
1864 rebuilt_path=$rebuilt_path"/"$i
1865
1866 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
1867 echo "*** DEST path element"
1868 echo "*** ${rebuilt_path}"
1869 echo "*** appears to have 0700 permissions."
1870 echo "*** This is very likely to cause fatal issues for devstack daemons."
1871
1872 if [[ -n "$SKIP_PATH_SANITY" ]]; then
1873 return
1874 else
1875 echo "*** Set SKIP_PATH_SANITY to skip this check"
1876 die $LINENO "Invalid path permissions"
1877 fi
1878 fi
1879 done
1880}
1881
Dean Troyer1a6d4492013-06-03 16:47:36 -05001882
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001883# This function recursively compares versions, and is not meant to be
1884# called by anything other than vercmp_numbers below. This function does
1885# not work with alphabetic versions.
1886#
1887# _vercmp_r sep ver1 ver2
1888function _vercmp_r {
Sean Dague537d4022013-10-22 07:43:22 -04001889 typeset sep
1890 typeset -a ver1=() ver2=()
1891 sep=$1; shift
1892 ver1=("${@:1:sep}")
1893 ver2=("${@:sep+1}")
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001894
Sean Dague537d4022013-10-22 07:43:22 -04001895 if ((ver1 > ver2)); then
1896 echo 1; return 0
1897 elif ((ver2 > ver1)); then
1898 echo -1; return 0
1899 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001900
Sean Dague537d4022013-10-22 07:43:22 -04001901 if ((sep <= 1)); then
1902 echo 0; return 0
1903 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001904
Sean Dague537d4022013-10-22 07:43:22 -04001905 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001906}
1907
1908
1909# This function compares two versions and is meant to be called by
1910# external callers. Please note the function assumes non-alphabetic
1911# versions. For example, this will work:
1912#
1913# vercmp_numbers 1.10 1.4
1914#
1915# The above will return "1", as 1.10 is greater than 1.4.
1916#
1917# vercmp_numbers 5.2 6.4
1918#
1919# The above will return "-1", as 5.2 is less than 6.4.
1920#
1921# vercmp_numbers 4.0 4.0
1922#
1923# The above will return "0", as the versions are equal.
1924#
1925# vercmp_numbers ver1 ver2
1926vercmp_numbers() {
Sean Dague537d4022013-10-22 07:43:22 -04001927 typeset v1=$1 v2=$2 sep
1928 typeset -a ver1 ver2
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001929
Sean Dague537d4022013-10-22 07:43:22 -04001930 IFS=. read -ra ver1 <<< "$v1"
1931 IFS=. read -ra ver2 <<< "$v2"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001932
Sean Dague537d4022013-10-22 07:43:22 -04001933 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +00001934}
1935
1936
Dean Troyer533e14d2013-08-30 15:11:22 -05001937# ``policy_add policy_file policy_name policy_permissions``
1938#
1939# Add a policy to a policy.json file
1940# Do nothing if the policy already exists
1941
1942function policy_add() {
1943 local policy_file=$1
1944 local policy_name=$2
1945 local policy_perm=$3
1946
1947 if grep -q ${policy_name} ${policy_file}; then
1948 echo "Policy ${policy_name} already exists in ${policy_file}"
1949 return
1950 fi
1951
1952 # Add a terminating comma to policy lines without one
1953 # Remove the closing '}' and all lines following to the end-of-file
1954 local tmpfile=$(mktemp)
1955 uniq ${policy_file} | sed -e '
1956 s/]$/],/
1957 /^[}]/,$d
1958 ' > ${tmpfile}
1959
1960 # Append policy and closing brace
1961 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
1962 echo "}" >>${tmpfile}
1963
1964 mv ${tmpfile} ${policy_file}
1965}
1966
1967
Salvatore Orlando05ae8332013-08-20 14:51:08 -07001968# This function sets log formatting options for colorizing log
1969# output to stdout. It is meant to be called by lib modules.
1970# The last two parameters are optional and can be used to specify
1971# non-default value for project and user format variables.
1972# Defaults are respectively 'project_name' and 'user_name'
1973#
1974# setup_colorized_logging something.conf SOMESECTION
1975function setup_colorized_logging() {
1976 local conf_file=$1
1977 local conf_section=$2
1978 local project_var=${3:-"project_name"}
1979 local user_var=${4:-"user_name"}
1980 # Add color to logging output
1981 iniset $conf_file $conf_section logging_context_format_string "%(asctime)s.%(msecs)03d %(color)s%(levelname)s %(name)s [%(request_id)s %("$user_var")s %("$project_var")s%(color)s] %(instance)s%(color)s%(message)s"
1982 iniset $conf_file $conf_section logging_default_format_string "%(asctime)s.%(msecs)03d %(color)s%(levelname)s %(name)s [-%(color)s] %(instance)s%(color)s%(message)s"
1983 iniset $conf_file $conf_section logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
1984 iniset $conf_file $conf_section logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
1985}
1986
Dean Troyer27e32692012-03-16 16:16:56 -05001987# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +00001988$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -05001989
1990
1991# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -04001992# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -04001993# End: