~devstack-core/devstack/github

2722.1.2 by Sean Dague
add shebang lines to all lib files
1
#!/bin/bash
2
#
1922.1.1 by Dean Troyer
Split functions
3
# functions-common - Common functions used by DevStack components
4
#
5
# The canonical copy of this file is maintained in the DevStack repo.
6
# All modifications should be made there and then sync'ed to other repos
7
# as required.
8
#
9
# This file is sorted alphabetically within the function groups.
10
#
11
# - Config Functions
12
# - Control Functions
13
# - Distro Functions
14
# - Git Functions
15
# - OpenStack Functions
16
# - Package Functions
17
# - Process Functions
18
# - Service Functions
1954.3.1 by Masayuki Igawa
Fix comments about System Functions
19
# - System Functions
1922.1.1 by Dean Troyer
Split functions
20
#
21
# The following variables are assumed to be defined by certain functions:
22
#
23
# - ``ENABLED_SERVICES``
24
# - ``ERROR_ON_CLONE``
25
# - ``FILES``
26
# - ``OFFLINE``
27
# - ``RECLONE``
1993.2.1 by Masayuki Igawa
Move some comments of variables to right place
28
# - ``REQUIREMENTS_DIR``
29
# - ``STACK_USER``
1922.1.1 by Dean Troyer
Split functions
30
# - ``TRACK_DEPENDS``
1993.2.1 by Masayuki Igawa
Move some comments of variables to right place
31
# - ``UNDO_REQUIREMENTS``
1922.1.1 by Dean Troyer
Split functions
32
# - ``http_proxy``, ``https_proxy``, ``no_proxy``
2518.6.1 by Dean Troyer
Fix docs build errors
33
#
1922.1.1 by Dean Troyer
Split functions
34
35
# Save trace setting
36
XTRACE=$(set +o | grep xtrace)
37
set +o xtrace
38
2553.4.1 by Sean Dague
use released library versions by default
39
# Global Config Variables
40
declare -A GITREPO
41
declare -A GITBRANCH
42
declare -A GITDIR
43
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
44
TRACK_DEPENDS=${TRACK_DEPENDS:-False}
45
1922.1.1 by Dean Troyer
Split functions
46
47
# Normalize config values to True or False
48
# Accepts as False: 0 no No NO false False FALSE
49
# Accepts as True: 1 yes Yes YES true True TRUE
50
# VAR=$(trueorfalse default-value test-value)
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
51
function trueorfalse {
1966.2.1 by Sean Dague
xtrace less
52
    local xtrace=$(set +o | grep xtrace)
53
    set +o xtrace
1922.1.1 by Dean Troyer
Split functions
54
    local default=$1
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
55
    local literal=$2
2862.3.1 by Dean Troyer
Fix unbound literal in trueorfalse()
56
    local testval=${!literal:-}
1922.1.1 by Dean Troyer
Split functions
57
58
    [[ -z "$testval" ]] && { echo "$default"; return; }
59
    [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; }
60
    [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; }
61
    echo "$default"
1966.2.1 by Sean Dague
xtrace less
62
    $xtrace
1922.1.1 by Dean Troyer
Split functions
63
}
64
3064.4.1 by Attila Fazekas
Move back isset to the functions-common
65
function isset {
66
    [[ -v "$1" ]]
67
}
1922.1.1 by Dean Troyer
Split functions
68
69
# Control Functions
70
# =================
71
72
# Prints backtrace info
73
# filename:lineno:function
74
# backtrace level
75
function backtrace {
76
    local level=$1
77
    local deep=$((${#BASH_SOURCE[@]} - 1))
78
    echo "[Call Trace]"
79
    while [ $level -le $deep ]; do
80
        echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
81
        deep=$((deep - 1))
82
    done
83
}
84
85
# Prints line number and "message" then exits
86
# die $LINENO "message"
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
87
function die {
1922.1.1 by Dean Troyer
Split functions
88
    local exitcode=$?
89
    set +o xtrace
90
    local line=$1; shift
91
    if [ $exitcode == 0 ]; then
92
        exitcode=1
93
    fi
94
    backtrace 2
95
    err $line "$*"
1977.2.1 by Dean Troyer
Unbuffer log output
96
    # Give buffers a second to flush
97
    sleep 1
1922.1.1 by Dean Troyer
Split functions
98
    exit $exitcode
99
}
100
101
# Checks an environment variable is not set or has length 0 OR if the
102
# exit code is non-zero and prints "message" and exits
103
# NOTE: env-var is the variable name without a '$'
104
# die_if_not_set $LINENO env-var "message"
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
105
function die_if_not_set {
1922.1.1 by Dean Troyer
Split functions
106
    local exitcode=$?
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
107
    local xtrace=$(set +o | grep xtrace)
1922.1.1 by Dean Troyer
Split functions
108
    set +o xtrace
109
    local line=$1; shift
110
    local evar=$1; shift
111
    if ! is_set $evar || [ $exitcode != 0 ]; then
112
        die $line "$*"
113
    fi
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
114
    $xtrace
1922.1.1 by Dean Troyer
Split functions
115
}
116
117
# Prints line number and "message" in error format
118
# err $LINENO "message"
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
119
function err {
1922.1.1 by Dean Troyer
Split functions
120
    local exitcode=$?
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
121
    local xtrace=$(set +o | grep xtrace)
1922.1.1 by Dean Troyer
Split functions
122
    set +o xtrace
123
    local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
124
    echo $msg 1>&2;
2835.1.1 by Dean Troyer
Deprecate SCREEN_LOGDIR in favor of LOGDIR
125
    if [[ -n ${LOGDIR} ]]; then
126
        echo $msg >> "${LOGDIR}/error.log"
1922.1.1 by Dean Troyer
Split functions
127
    fi
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
128
    $xtrace
1922.1.1 by Dean Troyer
Split functions
129
    return $exitcode
130
}
131
132
# Checks an environment variable is not set or has length 0 OR if the
133
# exit code is non-zero and prints "message"
134
# NOTE: env-var is the variable name without a '$'
135
# err_if_not_set $LINENO env-var "message"
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
136
function err_if_not_set {
1922.1.1 by Dean Troyer
Split functions
137
    local exitcode=$?
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
138
    local xtrace=$(set +o | grep xtrace)
1922.1.1 by Dean Troyer
Split functions
139
    set +o xtrace
140
    local line=$1; shift
141
    local evar=$1; shift
142
    if ! is_set $evar || [ $exitcode != 0 ]; then
143
        err $line "$*"
144
    fi
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
145
    $xtrace
1922.1.1 by Dean Troyer
Split functions
146
    return $exitcode
147
}
148
149
# Exit after outputting a message about the distribution not being supported.
150
# exit_distro_not_supported [optional-string-telling-what-is-missing]
151
function exit_distro_not_supported {
152
    if [[ -z "$DISTRO" ]]; then
153
        GetDistro
154
    fi
155
156
    if [ $# -gt 0 ]; then
157
        die $LINENO "Support for $DISTRO is incomplete: no support for $@"
158
    else
159
        die $LINENO "Support for $DISTRO is incomplete."
160
    fi
161
}
162
163
# Test if the named environment variable is set and not zero length
164
# is_set env-var
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
165
function is_set {
1922.1.1 by Dean Troyer
Split functions
166
    local var=\$"$1"
167
    eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
168
}
169
170
# Prints line number and "message" in warning format
171
# warn $LINENO "message"
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
172
function warn {
1922.1.1 by Dean Troyer
Split functions
173
    local exitcode=$?
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
174
    local xtrace=$(set +o | grep xtrace)
1922.1.1 by Dean Troyer
Split functions
175
    set +o xtrace
176
    local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
3159.1.1 by Sean Dague
fix warn function
177
    echo $msg
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
178
    $xtrace
1922.1.1 by Dean Troyer
Split functions
179
    return $exitcode
180
}
181
182
183
# Distro Functions
184
# ================
185
186
# Determine OS Vendor, Release and Update
187
# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
188
# Returns results in global variables:
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
189
# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
190
# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
191
# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
192
# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
193
# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
194
os_VENDOR=""
195
os_RELEASE=""
196
os_UPDATE=""
197
os_PACKAGE=""
198
os_CODENAME=""
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
199
1922.1.1 by Dean Troyer
Split functions
200
# GetOSVersion
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
201
function GetOSVersion {
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
202
1922.1.1 by Dean Troyer
Split functions
203
    # Figure out which vendor we are
204
    if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
205
        # OS/X
206
        os_VENDOR=`sw_vers -productName`
207
        os_RELEASE=`sw_vers -productVersion`
208
        os_UPDATE=${os_RELEASE##*.}
209
        os_RELEASE=${os_RELEASE%.*}
210
        os_PACKAGE=""
211
        if [[ "$os_RELEASE" =~ "10.7" ]]; then
212
            os_CODENAME="lion"
213
        elif [[ "$os_RELEASE" =~ "10.6" ]]; then
214
            os_CODENAME="snow leopard"
215
        elif [[ "$os_RELEASE" =~ "10.5" ]]; then
216
            os_CODENAME="leopard"
217
        elif [[ "$os_RELEASE" =~ "10.4" ]]; then
218
            os_CODENAME="tiger"
219
        elif [[ "$os_RELEASE" =~ "10.3" ]]; then
220
            os_CODENAME="panther"
221
        else
222
            os_CODENAME=""
223
        fi
224
    elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
225
        os_VENDOR=$(lsb_release -i -s)
226
        os_RELEASE=$(lsb_release -r -s)
227
        os_UPDATE=""
228
        os_PACKAGE="rpm"
229
        if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
230
            os_PACKAGE="deb"
231
        elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
232
            lsb_release -d -s | grep -q openSUSE
233
            if [[ $? -eq 0 ]]; then
234
                os_VENDOR="openSUSE"
235
            fi
236
        elif [[ $os_VENDOR == "openSUSE project" ]]; then
237
            os_VENDOR="openSUSE"
238
        elif [[ $os_VENDOR =~ Red.*Hat ]]; then
239
            os_VENDOR="Red Hat"
240
        fi
241
        os_CODENAME=$(lsb_release -c -s)
242
    elif [[ -r /etc/redhat-release ]]; then
243
        # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
244
        # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
245
        # CentOS release 5.5 (Final)
246
        # CentOS Linux release 6.0 (Final)
247
        # Fedora release 16 (Verne)
248
        # XenServer release 6.2.0-70446c (xenenterprise)
3033.1.1 by Wiekus Beukes
Add support for Oracle Linux 7 and later.
249
        # Oracle Linux release 7
1922.1.1 by Dean Troyer
Split functions
250
        os_CODENAME=""
251
        for r in "Red Hat" CentOS Fedora XenServer; do
252
            os_VENDOR=$r
253
            if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
254
                ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
255
                os_CODENAME=${ver#*|}
256
                os_RELEASE=${ver%|*}
257
                os_UPDATE=${os_RELEASE##*.}
258
                os_RELEASE=${os_RELEASE%.*}
259
                break
260
            fi
261
            os_VENDOR=""
262
        done
3033.1.1 by Wiekus Beukes
Add support for Oracle Linux 7 and later.
263
        if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
264
            os_VENDOR=OracleLinux
265
        fi
1922.1.1 by Dean Troyer
Split functions
266
        os_PACKAGE="rpm"
267
    elif [[ -r /etc/SuSE-release ]]; then
268
        for r in openSUSE "SUSE Linux"; do
269
            if [[ "$r" = "SUSE Linux" ]]; then
270
                os_VENDOR="SUSE LINUX"
271
            else
272
                os_VENDOR=$r
273
            fi
274
275
            if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
276
                os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
277
                os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
278
                os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
279
                break
280
            fi
281
            os_VENDOR=""
282
        done
283
        os_PACKAGE="rpm"
284
    # If lsb_release is not installed, we should be able to detect Debian OS
285
    elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
286
        os_VENDOR="Debian"
287
        os_PACKAGE="deb"
288
        os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
289
        os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
290
    fi
291
    export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
292
}
293
294
# Translate the OS version values into common nomenclature
295
# Sets global ``DISTRO`` from the ``os_*`` values
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
296
declare DISTRO
297
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
298
function GetDistro {
1922.1.1 by Dean Troyer
Split functions
299
    GetOSVersion
300
    if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
301
        # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
302
        DISTRO=$os_CODENAME
303
    elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
304
        # For Fedora, just use 'f' and the release
305
        DISTRO="f$os_RELEASE"
306
    elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
307
        DISTRO="opensuse-$os_RELEASE"
308
    elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
309
        # For SLE, also use the service pack
310
        if [[ -z "$os_UPDATE" ]]; then
311
            DISTRO="sle${os_RELEASE}"
312
        else
313
            DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
314
        fi
2333.1.1 by anju Tiwari
Added Oracle Linux distribution support
315
    elif [[ "$os_VENDOR" =~ (Red Hat) || \
316
        "$os_VENDOR" =~ (CentOS) || \
3033.1.1 by Wiekus Beukes
Add support for Oracle Linux 7 and later.
317
        "$os_VENDOR" =~ (OracleLinux) ]]; then
1922.1.1 by Dean Troyer
Split functions
318
        # Drop the . release as we assume it's compatible
319
        DISTRO="rhel${os_RELEASE::1}"
320
    elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
321
        DISTRO="xs$os_RELEASE"
322
    else
323
        # Catch-all for now is Vendor + Release + Update
324
        DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
325
    fi
326
    export DISTRO
327
}
328
329
# Utility function for checking machine architecture
330
# is_arch arch-type
331
function is_arch {
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
332
    [[ "$(uname -m)" == "$1" ]]
1922.1.1 by Dean Troyer
Split functions
333
}
334
3033.1.1 by Wiekus Beukes
Add support for Oracle Linux 7 and later.
335
# Determine if current distribution is an Oracle distribution
336
# is_oraclelinux
337
function is_oraclelinux {
338
    if [[ -z "$os_VENDOR" ]]; then
339
        GetOSVersion
340
    fi
341
342
    [ "$os_VENDOR" = "OracleLinux" ]
343
}
344
345
1922.1.1 by Dean Troyer
Split functions
346
# Determine if current distribution is a Fedora-based distribution
347
# (Fedora, RHEL, CentOS, etc).
348
# is_fedora
349
function is_fedora {
350
    if [[ -z "$os_VENDOR" ]]; then
351
        GetOSVersion
352
    fi
353
2333.1.1 by anju Tiwari
Added Oracle Linux distribution support
354
    [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
3033.1.1 by Wiekus Beukes
Add support for Oracle Linux 7 and later.
355
        [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ]
1922.1.1 by Dean Troyer
Split functions
356
}
357
358
359
# Determine if current distribution is a SUSE-based distribution
360
# (openSUSE, SLE).
361
# is_suse
362
function is_suse {
363
    if [[ -z "$os_VENDOR" ]]; then
364
        GetOSVersion
365
    fi
366
367
    [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
368
}
369
370
371
# Determine if current distribution is an Ubuntu-based distribution
372
# It will also detect non-Ubuntu but Debian-based distros
373
# is_ubuntu
374
function is_ubuntu {
375
    if [[ -z "$os_PACKAGE" ]]; then
376
        GetOSVersion
377
    fi
378
    [ "$os_PACKAGE" = "deb" ]
379
}
380
381
382
# Git Functions
383
# =============
384
1922.1.2 by Dean Troyer
Backport Grenade updates
385
# Returns openstack release name for a given branch name
386
# ``get_release_name_from_branch branch-name``
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
387
function get_release_name_from_branch {
1922.1.2 by Dean Troyer
Backport Grenade updates
388
    local branch=$1
2586.1.1 by Adam Gandelman
support proposed/* branches
389
    if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
1922.1.2 by Dean Troyer
Backport Grenade updates
390
        echo ${branch#*/}
391
    else
392
        echo "master"
393
    fi
394
}
395
1922.1.1 by Dean Troyer
Split functions
396
# git clone only if directory doesn't exist already.  Since ``DEST`` might not
397
# be owned by the installation user, we create the directory and change the
398
# ownership to the proper user.
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
399
# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
400
# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
1922.1.1 by Dean Troyer
Split functions
401
# does not exist (default is False, meaning the repo will be cloned).
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
402
# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
1922.1.1 by Dean Troyer
Split functions
403
# git_clone remote dest-dir branch
404
function git_clone {
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
405
    local git_remote=$1
406
    local git_dest=$2
407
    local git_ref=$3
408
    local orig_dir=$(pwd)
2608.2.1 by Jamie Lennox
Allow depth limiting git clones
409
    local git_clone_flags=""
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
410
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
411
    RECLONE=$(trueorfalse False RECLONE)
2823.5.1 by Kevin Benton
Disable shallow cloning with GIT_DEPTH=0
412
    if [[ "${GIT_DEPTH}" -gt 0 ]]; then
2608.2.1 by Jamie Lennox
Allow depth limiting git clones
413
        git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
414
    fi
415
1922.1.1 by Dean Troyer
Split functions
416
    if [[ "$OFFLINE" = "True" ]]; then
417
        echo "Running in offline mode, clones already exist"
418
        # print out the results so we know what change was used in the logs
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
419
        cd $git_dest
1922.1.1 by Dean Troyer
Split functions
420
        git show --oneline | head -1
2057.1.1 by Sean Dague
make git_clone safer
421
        cd $orig_dir
1922.1.1 by Dean Troyer
Split functions
422
        return
423
    fi
424
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
425
    if echo $git_ref | egrep -q "^refs"; then
1922.1.1 by Dean Troyer
Split functions
426
        # If our branch name is a gerrit style refs/changes/...
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
427
        if [[ ! -d $git_dest ]]; then
1922.1.1 by Dean Troyer
Split functions
428
            [[ "$ERROR_ON_CLONE" = "True" ]] && \
429
                die $LINENO "Cloning not allowed in this configuration"
2608.2.1 by Jamie Lennox
Allow depth limiting git clones
430
            git_timed clone $git_clone_flags $git_remote $git_dest
1922.1.1 by Dean Troyer
Split functions
431
        fi
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
432
        cd $git_dest
433
        git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
1922.1.1 by Dean Troyer
Split functions
434
    else
435
        # do a full clone only if the directory doesn't exist
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
436
        if [[ ! -d $git_dest ]]; then
1922.1.1 by Dean Troyer
Split functions
437
            [[ "$ERROR_ON_CLONE" = "True" ]] && \
438
                die $LINENO "Cloning not allowed in this configuration"
2608.2.1 by Jamie Lennox
Allow depth limiting git clones
439
            git_timed clone $git_clone_flags $git_remote $git_dest
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
440
            cd $git_dest
1922.1.1 by Dean Troyer
Split functions
441
            # This checkout syntax works for both branches and tags
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
442
            git checkout $git_ref
1922.1.1 by Dean Troyer
Split functions
443
        elif [[ "$RECLONE" = "True" ]]; then
444
            # if it does exist then simulate what clone does if asked to RECLONE
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
445
            cd $git_dest
1922.1.1 by Dean Troyer
Split functions
446
            # set the url to pull from and fetch
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
447
            git remote set-url origin $git_remote
1952.4.1 by Ian Wienand
Add GIT_TIMEOUT variable to watch git operations
448
            git_timed fetch origin
1922.1.1 by Dean Troyer
Split functions
449
            # remove the existing ignored files (like pyc) as they cause breakage
450
            # (due to the py files having older timestamps than our pyc, so python
451
            # thinks the pyc files are correct using them)
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
452
            find $git_dest -name '*.pyc' -delete
1922.1.1 by Dean Troyer
Split functions
453
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
454
            # handle git_ref accordingly to type (tag, branch)
455
            if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
456
                git_update_tag $git_ref
457
            elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
458
                git_update_branch $git_ref
459
            elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
460
                git_update_remote_branch $git_ref
1922.1.1 by Dean Troyer
Split functions
461
            else
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
462
                die $LINENO "$git_ref is neither branch nor tag"
1922.1.1 by Dean Troyer
Split functions
463
            fi
464
465
        fi
466
    fi
467
468
    # print out the results so we know what change was used in the logs
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
469
    cd $git_dest
1922.1.1 by Dean Troyer
Split functions
470
    git show --oneline | head -1
2057.1.1 by Sean Dague
make git_clone safer
471
    cd $orig_dir
1922.1.1 by Dean Troyer
Split functions
472
}
473
2553.4.1 by Sean Dague
use released library versions by default
474
# A variation on git clone that lets us specify a project by it's
475
# actual name, like oslo.config. This is exceptionally useful in the
476
# library installation case
477
function git_clone_by_name {
478
    local name=$1
479
    local repo=${GITREPO[$name]}
480
    local dir=${GITDIR[$name]}
481
    local branch=${GITBRANCH[$name]}
482
    git_clone $repo $dir $branch
483
}
484
485
1952.4.1 by Ian Wienand
Add GIT_TIMEOUT variable to watch git operations
486
# git can sometimes get itself infinitely stuck with transient network
487
# errors or other issues with the remote end.  This wraps git in a
488
# timeout/retry loop and is intended to watch over non-local git
489
# processes that might hang.  GIT_TIMEOUT, if set, is passed directly
490
# to timeout(1); otherwise the default value of 0 maintains the status
491
# quo of waiting forever.
492
# usage: git_timed <git-command>
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
493
function git_timed {
1952.4.1 by Ian Wienand
Add GIT_TIMEOUT variable to watch git operations
494
    local count=0
495
    local timeout=0
496
497
    if [[ -n "${GIT_TIMEOUT}" ]]; then
498
        timeout=${GIT_TIMEOUT}
499
    fi
500
501
    until timeout -s SIGINT ${timeout} git "$@"; do
502
        # 124 is timeout(1)'s special return code when it reached the
503
        # timeout; otherwise assume fatal failure
504
        if [[ $? -ne 124 ]]; then
505
            die $LINENO "git call failed: [git $@]"
506
        fi
507
508
        count=$(($count + 1))
3159.1.1 by Sean Dague
fix warn function
509
        warn $LINENO "timeout ${count} for git call: [git $@]"
1952.4.1 by Ian Wienand
Add GIT_TIMEOUT variable to watch git operations
510
        if [ $count -eq 3 ]; then
511
            die $LINENO "Maximum of 3 git retries reached"
512
        fi
513
        sleep 5
514
    done
515
}
516
1922.1.1 by Dean Troyer
Split functions
517
# git update using reference as a branch.
518
# git_update_branch ref
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
519
function git_update_branch {
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
520
    local git_branch=$1
521
522
    git checkout -f origin/$git_branch
1922.1.1 by Dean Troyer
Split functions
523
    # a local branch might not exist
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
524
    git branch -D $git_branch || true
525
    git checkout -b $git_branch
1922.1.1 by Dean Troyer
Split functions
526
}
527
528
# git update using reference as a branch.
529
# git_update_remote_branch ref
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
530
function git_update_remote_branch {
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
531
    local git_branch=$1
532
533
    git checkout -b $git_branch -t origin/$git_branch
1922.1.1 by Dean Troyer
Split functions
534
}
535
536
# git update using reference as a tag. Be careful editing source at that repo
537
# as working copy will be in a detached mode
538
# git_update_tag ref
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
539
function git_update_tag {
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
540
    local git_tag=$1
541
542
    git tag -d $git_tag
1922.1.1 by Dean Troyer
Split functions
543
    # fetching given tag only
2356.1.1 by Dean Troyer
Clean up local variable usage - git functions
544
    git_timed fetch origin tag $git_tag
545
    git checkout -f $git_tag
1922.1.1 by Dean Troyer
Split functions
546
}
547
548
549
# OpenStack Functions
550
# ===================
551
552
# Get the default value for HOST_IP
553
# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
554
function get_default_host_ip {
1922.1.1 by Dean Troyer
Split functions
555
    local fixed_range=$1
556
    local floating_range=$2
557
    local host_ip_iface=$3
558
    local host_ip=$4
559
560
    # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
561
    if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
562
        host_ip=""
3016.1.1 by Andreas Scheuring
Support detection of interfaces with non-word chars in the name
563
        # Find the interface used for the default route
564
        host_ip_iface=${host_ip_iface:-$(ip route | awk '/default/ {print $5}' | head -1)}
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
565
        local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/");  print parts[1]}')
566
        local ip
567
        for ip in $host_ips; do
1922.1.1 by Dean Troyer
Split functions
568
            # Attempt to filter out IP addresses that are part of the fixed and
569
            # floating range. Note that this method only works if the ``netaddr``
570
            # python library is installed. If it is not installed, an error
571
            # will be printed and the first IP from the interface will be used.
572
            # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
573
            # address.
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
574
            if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
575
                host_ip=$ip
1922.1.1 by Dean Troyer
Split functions
576
                break;
577
            fi
578
        done
579
    fi
580
    echo $host_ip
581
}
582
2462.1.1 by Attila Fazekas
Faster nova fixed key generation
583
# Generates hex string from ``size`` byte of pseudo random data
584
# generate_hex_string size
585
function generate_hex_string {
586
    local size=$1
587
    hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
588
}
589
1922.1.1 by Dean Troyer
Split functions
590
# Grab a numbered field from python prettytable output
591
# Fields are numbered starting with 1
592
# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
593
# get_field field-number
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
594
function get_field {
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
595
    local data field
1922.1.1 by Dean Troyer
Split functions
596
    while read data; do
597
        if [ "$1" -lt 0 ]; then
598
            field="(\$(NF$1))"
599
        else
600
            field="\$$(($1 + 1))"
601
        fi
602
        echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
603
    done
604
}
605
2969.4.1 by yuntongjin
create install_default_policy
606
# install default policy
607
# copy over a default policy.json and policy.d for projects
608
function install_default_policy {
609
    local project=$1
610
    local project_uc=$(echo $1|tr a-z A-Z)
611
    local conf_dir="${project_uc}_CONF_DIR"
612
    # eval conf dir to get the variable
613
    conf_dir="${!conf_dir}"
614
    local project_dir="${project_uc}_DIR"
615
    # eval project dir to get the variable
616
    project_dir="${!project_dir}"
617
    local sample_conf_dir="${project_dir}/etc/${project}"
618
    local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
619
620
    # first copy any policy.json
621
    cp -p $sample_conf_dir/policy.json $conf_dir
622
    # then optionally copy over policy.d
623
    if [[ -d $sample_policy_dir ]]; then
624
        cp -r $sample_policy_dir $conf_dir/policy.d
625
    fi
626
}
627
1922.1.1 by Dean Troyer
Split functions
628
# Add a policy to a policy.json file
629
# Do nothing if the policy already exists
630
# ``policy_add policy_file policy_name policy_permissions``
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
631
function policy_add {
1922.1.1 by Dean Troyer
Split functions
632
    local policy_file=$1
633
    local policy_name=$2
634
    local policy_perm=$3
635
636
    if grep -q ${policy_name} ${policy_file}; then
637
        echo "Policy ${policy_name} already exists in ${policy_file}"
638
        return
639
    fi
640
641
    # Add a terminating comma to policy lines without one
642
    # Remove the closing '}' and all lines following to the end-of-file
643
    local tmpfile=$(mktemp)
644
    uniq ${policy_file} | sed -e '
645
        s/]$/],/
646
        /^[}]/,$d
647
    ' > ${tmpfile}
648
649
    # Append policy and closing brace
650
    echo "    \"${policy_name}\": ${policy_perm}" >>${tmpfile}
651
    echo "}" >>${tmpfile}
652
653
    mv ${tmpfile} ${policy_file}
654
}
655
2599.3.1 by Alistair Coles
Add swift user and project in non-default domain
656
# Gets or creates a domain
657
# Usage: get_or_create_domain <name> <description>
658
function get_or_create_domain {
2809.2.1 by Steve Martinelli
Add a group create function, and a sample group
659
    local os_url="$KEYSTONE_SERVICE_URI_V3"
2599.3.1 by Alistair Coles
Add swift user and project in non-default domain
660
    # Gets domain id
661
    local domain_id=$(
662
        # Gets domain id
663
        openstack --os-token=$OS_TOKEN --os-url=$os_url \
664
            --os-identity-api-version=3 domain show $1 \
665
            -f value -c id 2>/dev/null ||
666
        # Creates new domain
667
        openstack --os-token=$OS_TOKEN --os-url=$os_url \
668
            --os-identity-api-version=3 domain create $1 \
669
            --description "$2" \
670
            -f value -c id
671
    )
672
    echo $domain_id
673
}
674
2809.2.1 by Steve Martinelli
Add a group create function, and a sample group
675
# Gets or creates group
676
# Usage: get_or_create_group <groupname> [<domain> <description>]
677
function get_or_create_group {
678
    local domain=${2:+--domain ${2}}
679
    local desc="${3:-}"
680
    local os_url="$KEYSTONE_SERVICE_URI_V3"
681
    # Gets group id
682
    local group_id=$(
683
        # Creates new group with --or-show
684
        openstack --os-token=$OS_TOKEN --os-url=$os_url \
685
            --os-identity-api-version=3 group create $1 \
686
            $domain --description "$desc" --or-show \
687
            -f value -c id
688
    )
689
    echo $group_id
690
}
691
2310.3.1 by Bartosz Górski
Adds support for multi-region
692
# Gets or creates user
2851.1.1 by Jamie Lennox
Remove the default project from all users
693
# Usage: get_or_create_user <username> <password> [<email> [<domain>]]
2310.3.1 by Bartosz Górski
Adds support for multi-region
694
function get_or_create_user {
2851.1.1 by Jamie Lennox
Remove the default project from all users
695
    if [[ ! -z "$3" ]]; then
696
        local email="--email=$3"
697
    else
698
        local email=""
699
    fi
700
    local os_cmd="openstack"
701
    local domain=""
2344.1.1 by Gael Chamoulaud
Users in service group should not have email addresses
702
    if [[ ! -z "$4" ]]; then
2851.1.1 by Jamie Lennox
Remove the default project from all users
703
        domain="--domain=$4"
2809.2.1 by Steve Martinelli
Add a group create function, and a sample group
704
        os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
2599.3.1 by Alistair Coles
Add swift user and project in non-default domain
705
    fi
2310.3.1 by Bartosz Górski
Adds support for multi-region
706
    # Gets user id
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
707
    local user_id=$(
2727.2.1 by Steve Martinelli
Use --or-show for get_or_create_user/project/role function
708
        # Creates new user with --or-show
2599.3.1 by Alistair Coles
Add swift user and project in non-default domain
709
        $os_cmd user create \
2310.3.1 by Bartosz Górski
Adds support for multi-region
710
            $1 \
711
            --password "$2" \
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
712
            $email \
2599.3.1 by Alistair Coles
Add swift user and project in non-default domain
713
            $domain \
2727.2.1 by Steve Martinelli
Use --or-show for get_or_create_user/project/role function
714
            --or-show \
2310.3.1 by Bartosz Górski
Adds support for multi-region
715
            -f value -c id
716
    )
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
717
    echo $user_id
2310.3.1 by Bartosz Górski
Adds support for multi-region
718
}
719
720
# Gets or creates project
2599.3.1 by Alistair Coles
Add swift user and project in non-default domain
721
# Usage: get_or_create_project <name> [<domain>]
2310.3.1 by Bartosz Górski
Adds support for multi-region
722
function get_or_create_project {
723
    # Gets project id
2599.3.1 by Alistair Coles
Add swift user and project in non-default domain
724
    local os_cmd="openstack"
725
    local domain=""
726
    if [[ ! -z "$2" ]]; then
727
        domain="--domain=$2"
2809.2.1 by Steve Martinelli
Add a group create function, and a sample group
728
        os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3"
2599.3.1 by Alistair Coles
Add swift user and project in non-default domain
729
    fi
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
730
    local project_id=$(
2727.2.1 by Steve Martinelli
Use --or-show for get_or_create_user/project/role function
731
        # Creates new project with --or-show
732
        $os_cmd project create $1 $domain --or-show -f value -c id
2310.3.1 by Bartosz Górski
Adds support for multi-region
733
    )
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
734
    echo $project_id
2310.3.1 by Bartosz Górski
Adds support for multi-region
735
}
736
737
# Gets or creates role
738
# Usage: get_or_create_role <name>
739
function get_or_create_role {
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
740
    local role_id=$(
2727.2.1 by Steve Martinelli
Use --or-show for get_or_create_user/project/role function
741
        # Creates role with --or-show
742
        openstack role create $1 --or-show -f value -c id
2310.3.1 by Bartosz Górski
Adds support for multi-region
743
    )
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
744
    echo $role_id
2310.3.1 by Bartosz Górski
Adds support for multi-region
745
}
746
2909 by Jamie Lennox
Rename get_or_add_user_role
747
# Gets or adds user role to project
748
# Usage: get_or_add_user_project_role <role> <user> <project>
749
function get_or_add_user_project_role {
2310.3.1 by Bartosz Górski
Adds support for multi-region
750
    # Gets user role id
2823.4.1 by Steve Martinelli
Use `os role list` instead of `os user role list`
751
    local user_role_id=$(openstack role list \
752
        --user $2 \
2310.3.1 by Bartosz Górski
Adds support for multi-region
753
        --project $3 \
754
        --column "ID" \
755
        --column "Name" \
756
        | grep " $1 " | get_field 1)
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
757
    if [[ -z "$user_role_id" ]]; then
2310.3.1 by Bartosz Górski
Adds support for multi-region
758
        # Adds role to user
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
759
        user_role_id=$(openstack role add \
2310.3.1 by Bartosz Górski
Adds support for multi-region
760
            $1 \
761
            --user $2 \
762
            --project $3 \
763
            | grep " id " | get_field 2)
764
    fi
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
765
    echo $user_role_id
2310.3.1 by Bartosz Górski
Adds support for multi-region
766
}
767
3023.2.1 by Steve Martinelli
Add roles when we create groups
768
# Gets or adds group role to project
769
# Usage: get_or_add_group_project_role <role> <group> <project>
770
function get_or_add_group_project_role {
771
    # Gets group role id
772
    local group_role_id=$(openstack role list \
773
        --group $2 \
774
        --project $3 \
775
        --column "ID" \
776
        --column "Name" \
777
        | grep " $1 " | get_field 1)
778
    if [[ -z "$group_role_id" ]]; then
779
        # Adds role to group
780
        group_role_id=$(openstack role add \
781
            $1 \
782
            --group $2 \
783
            --project $3 \
784
            | grep " id " | get_field 2)
785
    fi
786
    echo $group_role_id
787
}
788
2310.3.1 by Bartosz Górski
Adds support for multi-region
789
# Gets or creates service
790
# Usage: get_or_create_service <name> <type> <description>
791
function get_or_create_service {
792
    # Gets service id
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
793
    local service_id=$(
2310.3.1 by Bartosz Górski
Adds support for multi-region
794
        # Gets service id
795
        openstack service show $1 -f value -c id 2>/dev/null ||
796
        # Creates new service if not exists
797
        openstack service create \
2823.6.1 by Steve Martinelli
Update osc server create to have type as positional arg
798
            $2 \
799
            --name $1 \
2310.3.1 by Bartosz Górski
Adds support for multi-region
800
            --description="$3" \
801
            -f value -c id
802
    )
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
803
    echo $service_id
2310.3.1 by Bartosz Górski
Adds support for multi-region
804
}
805
806
# Gets or creates endpoint
807
# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
808
function get_or_create_endpoint {
809
    # Gets endpoint id
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
810
    local endpoint_id=$(openstack endpoint list \
2310.3.1 by Bartosz Górski
Adds support for multi-region
811
        --column "ID" \
812
        --column "Region" \
813
        --column "Service Name" \
814
        | grep " $2 " \
815
        | grep " $1 " | get_field 1)
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
816
    if [[ -z "$endpoint_id" ]]; then
2310.3.1 by Bartosz Górski
Adds support for multi-region
817
        # Creates new endpoint
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
818
        endpoint_id=$(openstack endpoint create \
2310.3.1 by Bartosz Górski
Adds support for multi-region
819
            $1 \
820
            --region $2 \
821
            --publicurl $3 \
822
            --adminurl $4 \
823
            --internalurl $5 \
824
            | grep " id " | get_field 2)
825
    fi
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
826
    echo $endpoint_id
2310.3.1 by Bartosz Górski
Adds support for multi-region
827
}
1922.1.1 by Dean Troyer
Split functions
828
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
829
1922.1.1 by Dean Troyer
Split functions
830
# Package Functions
831
# =================
832
833
# _get_package_dir
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
834
function _get_package_dir {
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
835
    local base_dir=$1
1922.1.1 by Dean Troyer
Split functions
836
    local pkg_dir
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
837
838
    if [[ -z "$base_dir" ]]; then
839
        base_dir=$FILES
840
    fi
1922.1.1 by Dean Troyer
Split functions
841
    if is_ubuntu; then
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
842
        pkg_dir=$base_dir/debs
1922.1.1 by Dean Troyer
Split functions
843
    elif is_fedora; then
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
844
        pkg_dir=$base_dir/rpms
1922.1.1 by Dean Troyer
Split functions
845
    elif is_suse; then
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
846
        pkg_dir=$base_dir/rpms-suse
1922.1.1 by Dean Troyer
Split functions
847
    else
848
        exit_distro_not_supported "list of packages"
849
    fi
850
    echo "$pkg_dir"
851
}
852
853
# Wrapper for ``apt-get`` to set cache and proxy environment variables
854
# Uses globals ``OFFLINE``, ``*_proxy``
855
# apt_get operation package [package ...]
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
856
function apt_get {
1966.2.1 by Sean Dague
xtrace less
857
    local xtrace=$(set +o | grep xtrace)
858
    set +o xtrace
859
1922.1.1 by Dean Troyer
Split functions
860
    [[ "$OFFLINE" = "True" || -z "$@" ]] && return
861
    local sudo="sudo"
862
    [[ "$(id -u)" = "0" ]] && sudo="env"
1966.2.1 by Sean Dague
xtrace less
863
864
    $xtrace
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
865
1922.1.1 by Dean Troyer
Split functions
866
    $sudo DEBIAN_FRONTEND=noninteractive \
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
867
        http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
868
        no_proxy=${no_proxy:-} \
1922.1.1 by Dean Troyer
Split functions
869
        apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
870
}
871
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
872
function _parse_package_files {
873
    local files_to_parse=$@
874
875
    if [[ -z "$DISTRO" ]]; then
876
        GetDistro
877
    fi
878
879
    for fname in ${files_to_parse}; do
880
        local OIFS line package distros distro
881
        [[ -e $fname ]] || continue
882
883
        OIFS=$IFS
884
        IFS=$'\n'
885
        for line in $(<${fname}); do
886
            if [[ $line =~ "NOPRIME" ]]; then
887
                continue
888
            fi
889
890
            # Assume we want this package
891
            package=${line%#*}
892
            inst_pkg=1
893
894
            # Look for # dist:xxx in comment
895
            if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
896
                # We are using BASH regexp matching feature.
897
                package=${BASH_REMATCH[1]}
898
                distros=${BASH_REMATCH[2]}
899
                # In bash ${VAR,,} will lowecase VAR
900
                # Look for a match in the distro list
901
                if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
902
                    # If no match then skip this package
903
                    inst_pkg=0
904
                fi
905
            fi
906
907
            if [[ $inst_pkg = 1 ]]; then
908
                echo $package
909
            fi
910
        done
911
        IFS=$OIFS
912
    done
913
}
914
1922.1.1 by Dean Troyer
Split functions
915
# get_packages() collects a list of package names of any type from the
2678.1.1 by Monty Taylor
Rename apts to debs
916
# prerequisite files in ``files/{debs|rpms}``.  The list is intended
1922.1.1 by Dean Troyer
Split functions
917
# to be passed to a package installer such as apt or yum.
918
#
919
# Only packages required for the services in 1st argument will be
920
# included.  Two bits of metadata are recognized in the prerequisite files:
921
#
922
# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
923
# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
924
#   of the package to the distros listed.  The distro names are case insensitive.
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
925
function get_packages {
1966.2.1 by Sean Dague
xtrace less
926
    local xtrace=$(set +o | grep xtrace)
927
    set +o xtrace
1922.1.1 by Dean Troyer
Split functions
928
    local services=$@
929
    local package_dir=$(_get_package_dir)
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
930
    local file_to_parse=""
931
    local service=""
1922.1.1 by Dean Troyer
Split functions
932
933
    if [[ -z "$package_dir" ]]; then
934
        echo "No package directory supplied"
935
        return 1
936
    fi
937
    for service in ${services//,/ }; do
938
        # Allow individual services to specify dependencies
939
        if [[ -e ${package_dir}/${service} ]]; then
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
940
            file_to_parse="${file_to_parse} ${package_dir}/${service}"
1922.1.1 by Dean Troyer
Split functions
941
        fi
942
        # NOTE(sdague) n-api needs glance for now because that's where
943
        # glance client is
944
        if [[ $service == n-api ]]; then
3036 by Ryan Hsu
Fix packages not getting installed if service name in base path
945
            if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
946
                file_to_parse="${file_to_parse} ${package_dir}/nova"
1922.1.1 by Dean Troyer
Split functions
947
            fi
3036 by Ryan Hsu
Fix packages not getting installed if service name in base path
948
            if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
949
                file_to_parse="${file_to_parse} ${package_dir}/glance"
1922.1.1 by Dean Troyer
Split functions
950
            fi
951
        elif [[ $service == c-* ]]; then
3036 by Ryan Hsu
Fix packages not getting installed if service name in base path
952
            if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
953
                file_to_parse="${file_to_parse} ${package_dir}/cinder"
1922.1.1 by Dean Troyer
Split functions
954
            fi
955
        elif [[ $service == ceilometer-* ]]; then
3036 by Ryan Hsu
Fix packages not getting installed if service name in base path
956
            if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
957
                file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
1922.1.1 by Dean Troyer
Split functions
958
            fi
959
        elif [[ $service == s-* ]]; then
3036 by Ryan Hsu
Fix packages not getting installed if service name in base path
960
            if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
961
                file_to_parse="${file_to_parse} ${package_dir}/swift"
1922.1.1 by Dean Troyer
Split functions
962
            fi
963
        elif [[ $service == n-* ]]; then
3036 by Ryan Hsu
Fix packages not getting installed if service name in base path
964
            if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
965
                file_to_parse="${file_to_parse} ${package_dir}/nova"
1922.1.1 by Dean Troyer
Split functions
966
            fi
967
        elif [[ $service == g-* ]]; then
3036 by Ryan Hsu
Fix packages not getting installed if service name in base path
968
            if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
969
                file_to_parse="${file_to_parse} ${package_dir}/glance"
1922.1.1 by Dean Troyer
Split functions
970
            fi
971
        elif [[ $service == key* ]]; then
3036 by Ryan Hsu
Fix packages not getting installed if service name in base path
972
            if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
973
                file_to_parse="${file_to_parse} ${package_dir}/keystone"
1922.1.1 by Dean Troyer
Split functions
974
            fi
975
        elif [[ $service == q-* ]]; then
3036 by Ryan Hsu
Fix packages not getting installed if service name in base path
976
            if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
977
                file_to_parse="${file_to_parse} ${package_dir}/neutron"
1922.1.1 by Dean Troyer
Split functions
978
            fi
2082.3.1 by Adam Gandelman
Parse Ironic packages files/{apts, rpms}/ironic
979
        elif [[ $service == ir-* ]]; then
3036 by Ryan Hsu
Fix packages not getting installed if service name in base path
980
            if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
981
                file_to_parse="${file_to_parse} ${package_dir}/ironic"
2082.3.1 by Adam Gandelman
Parse Ironic packages files/{apts, rpms}/ironic
982
            fi
1922.1.1 by Dean Troyer
Split functions
983
        fi
984
    done
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
985
    echo "$(_parse_package_files $file_to_parse)"
986
    $xtrace
987
}
988
989
# get_plugin_packages() collects a list of package names of any type from a
990
# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``.  The
991
# list is intended to be passed to a package installer such as apt or yum.
992
#
993
# Only packages required for enabled and collected plugins will included.
994
#
3081.1.1 by Dean Troyer
Mostly docs cleanups
995
# The same metadata used in the main DevStack prerequisite files may be used
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
996
# in these prerequisite files, see get_packages() for more info.
997
function get_plugin_packages {
998
    local xtrace=$(set +o | grep xtrace)
999
    set +o xtrace
1000
    local files_to_parse=""
1001
    local package_dir=""
1002
    for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1003
        local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1004
        files_to_parse+="$package_dir/$plugin"
1922.1.1 by Dean Troyer
Split functions
1005
    done
2985.4.1 by Adam Gandelman
Allow devstack plugins to specify prereq packages
1006
    echo "$(_parse_package_files $files_to_parse)"
1966.2.1 by Sean Dague
xtrace less
1007
    $xtrace
1922.1.1 by Dean Troyer
Split functions
1008
}
1009
1010
# Distro-agnostic package installer
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
1011
# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
1922.1.1 by Dean Troyer
Split functions
1012
# install_package package [package ...]
2235.2.1 by Monty Taylor
Update again and retry a failed package install
1013
function update_package_repo {
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
1014
    NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1015
    REPOS_UPDATED=${REPOS_UPDATED:-False}
1016
    RETRY_UPDATE=${RETRY_UPDATE:-False}
1017
2343.3.1 by Paul Linchpiner
Fixed NO_UPDATE_REPOS variable usage
1018
    if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
2235.2.1 by Monty Taylor
Update again and retry a failed package install
1019
        return 0
1020
    fi
1021
1922.1.1 by Dean Troyer
Split functions
1022
    if is_ubuntu; then
2235.2.1 by Monty Taylor
Update again and retry a failed package install
1023
        local xtrace=$(set +o | grep xtrace)
1024
        set +o xtrace
1025
        if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1026
            # if there are transient errors pulling the updates, that's fine.
1027
            # It may be secondary repositories that we don't really care about.
1028
            apt_get update  || /bin/true
1029
            REPOS_UPDATED=True
1030
        fi
1966.2.1 by Sean Dague
xtrace less
1031
        $xtrace
2235.2.1 by Monty Taylor
Update again and retry a failed package install
1032
    fi
1033
}
1034
1035
function real_install_package {
1036
    if is_ubuntu; then
1922.1.1 by Dean Troyer
Split functions
1037
        apt_get install "$@"
1038
    elif is_fedora; then
1039
        yum_install "$@"
1040
    elif is_suse; then
1041
        zypper_install "$@"
1042
    else
1043
        exit_distro_not_supported "installing packages"
1044
    fi
1045
}
1046
2235.2.1 by Monty Taylor
Update again and retry a failed package install
1047
# Distro-agnostic package installer
1048
# install_package package [package ...]
1049
function install_package {
1050
    update_package_repo
1051
    real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1052
}
1053
1922.1.1 by Dean Troyer
Split functions
1054
# Distro-agnostic function to tell if a package is installed
1055
# is_package_installed package [package ...]
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1056
function is_package_installed {
1922.1.1 by Dean Troyer
Split functions
1057
    if [[ -z "$@" ]]; then
1058
        return 1
1059
    fi
1060
1061
    if [[ -z "$os_PACKAGE" ]]; then
1062
        GetOSVersion
1063
    fi
1064
1065
    if [[ "$os_PACKAGE" = "deb" ]]; then
1066
        dpkg -s "$@" > /dev/null 2> /dev/null
1067
    elif [[ "$os_PACKAGE" = "rpm" ]]; then
1068
        rpm --quiet -q "$@"
1069
    else
1070
        exit_distro_not_supported "finding if a package is installed"
1071
    fi
1072
}
1073
1074
# Distro-agnostic package uninstaller
1075
# uninstall_package package [package ...]
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1076
function uninstall_package {
1922.1.1 by Dean Troyer
Split functions
1077
    if is_ubuntu; then
1078
        apt_get purge "$@"
1079
    elif is_fedora; then
2872.5.1 by Ian Wienand
Add default for YUM
1080
        sudo ${YUM:-yum} remove -y "$@" ||:
1922.1.1 by Dean Troyer
Split functions
1081
    elif is_suse; then
1082
        sudo zypper rm "$@"
1083
    else
1084
        exit_distro_not_supported "uninstalling packages"
1085
    fi
1086
}
1087
1088
# Wrapper for ``yum`` to set proxy environment variables
2728.1.1 by Daniel P. Berrange
Allow use of dnf instead of yum on Fedora
1089
# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
1922.1.1 by Dean Troyer
Split functions
1090
# yum_install package [package ...]
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1091
function yum_install {
1922.1.1 by Dean Troyer
Split functions
1092
    [[ "$OFFLINE" = "True" ]] && return
1093
    local sudo="sudo"
1094
    [[ "$(id -u)" = "0" ]] && sudo="env"
2042.2.1 by Ian Wienand
Detect missing packages with yum
1095
1096
    # The manual check for missing packages is because yum -y assumes
1097
    # missing packages are OK.  See
1098
    # https://bugzilla.redhat.com/show_bug.cgi?id=965567
3012.2.1 by Ian Wienand
Add defaults for yum proxy variables
1099
    $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1100
        no_proxy="${no_proxy:-}" \
2872.5.1 by Ian Wienand
Add default for YUM
1101
        ${YUM:-yum} install -y "$@" 2>&1 | \
2042.2.1 by Ian Wienand
Detect missing packages with yum
1102
        awk '
1103
            BEGIN { fail=0 }
1104
            /No package/ { fail=1 }
1105
            { print }
1106
            END { exit fail }' || \
1107
                die $LINENO "Missing packages detected"
1108
1109
    # also ensure we catch a yum failure
1110
    if [[ ${PIPESTATUS[0]} != 0 ]]; then
2872.5.1 by Ian Wienand
Add default for YUM
1111
        die $LINENO "${YUM:-yum} install failure"
2042.2.1 by Ian Wienand
Detect missing packages with yum
1112
    fi
1922.1.1 by Dean Troyer
Split functions
1113
}
1114
1115
# zypper wrapper to set arguments correctly
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
1116
# Uses globals ``OFFLINE``, ``*_proxy``
1922.1.1 by Dean Troyer
Split functions
1117
# zypper_install package [package ...]
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1118
function zypper_install {
1922.1.1 by Dean Troyer
Split functions
1119
    [[ "$OFFLINE" = "True" ]] && return
1120
    local sudo="sudo"
1121
    [[ "$(id -u)" = "0" ]] && sudo="env"
3012.2.1 by Ian Wienand
Add defaults for yum proxy variables
1122
    $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1123
        no_proxy="${no_proxy:-}" \
1922.1.1 by Dean Troyer
Split functions
1124
        zypper --non-interactive install --auto-agree-with-licenses "$@"
1125
}
1126
1127
1128
# Process Functions
1129
# =================
1130
1131
# _run_process() is designed to be backgrounded by run_process() to simulate a
1132
# fork.  It includes the dirty work of closing extra filehandles and preparing log
1133
# files to produce the same logs as screen_it().  The log filename is derived
2835.1.1 by Dean Troyer
Deprecate SCREEN_LOGDIR in favor of LOGDIR
1134
# from the service name.
1135
# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
2496.2.1 by Chris Dent
Replace screen_it() with run_process() throughout
1136
# If an optional group is provided sg will be used to set the group of
1137
# the command.
1138
# _run_process service "command-line" [group]
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1139
function _run_process {
3161.1.1 by Sean Dague
clean up logging around run_process
1140
    # disable tracing through the exec redirects, it's just confusing in the logs.
1141
    xtrace=$(set +o | grep xtrace)
1142
    set +o xtrace
1143
1922.1.1 by Dean Troyer
Split functions
1144
    local service=$1
1145
    local command="$2"
2496.2.1 by Chris Dent
Replace screen_it() with run_process() throughout
1146
    local group=$3
1922.1.1 by Dean Troyer
Split functions
1147
1148
    # Undo logging redirections and close the extra descriptors
1149
    exec 1>&3
1150
    exec 2>&3
1151
    exec 3>&-
1152
    exec 6>&-
1153
2835.1.1 by Dean Troyer
Deprecate SCREEN_LOGDIR in favor of LOGDIR
1154
    local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1155
    if [[ -n ${LOGDIR} ]]; then
1156
        exec 1>&"$real_logfile" 2>&1
1157
        ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1158
        if [[ -n ${SCREEN_LOGDIR} ]]; then
1159
            # Drop the backward-compat symlink
1160
            ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1161
        fi
1922.1.1 by Dean Troyer
Split functions
1162
1163
        # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1164
        export PYTHONUNBUFFERED=1
1165
    fi
1166
3161.1.1 by Sean Dague
clean up logging around run_process
1167
    # reenable xtrace before we do *real* work
1168
    $xtrace
1169
2491.1.1 by Dean Troyer
Run processes without screen
1170
    # Run under ``setsid`` to force the process to become a session and group leader.
1171
    # The pid saved can be used with pkill -g to get the entire process group.
2496.2.1 by Chris Dent
Replace screen_it() with run_process() throughout
1172
    if [[ -n "$group" ]]; then
1173
        setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1174
    else
1175
        setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1176
    fi
2491.1.1 by Dean Troyer
Run processes without screen
1177
1178
    # Just silently exit this process
1179
    exit 0
1922.1.1 by Dean Troyer
Split functions
1180
}
1181
1182
# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1183
# This is used for ``service_check`` when all the ``screen_it`` are called finished
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
1184
# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
1922.1.1 by Dean Troyer
Split functions
1185
# init_service_check
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1186
function init_service_check {
1922.1.1 by Dean Troyer
Split functions
1187
    SCREEN_NAME=${SCREEN_NAME:-stack}
1188
    SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1189
1190
    if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1191
        mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1192
    fi
1193
1194
    rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1195
}
1196
1197
# Find out if a process exists by partial name.
1198
# is_running name
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1199
function is_running {
1922.1.1 by Dean Troyer
Split functions
1200
    local name=$1
1201
    ps auxw | grep -v grep | grep ${name} > /dev/null
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
1202
    local exitcode=$?
1922.1.1 by Dean Troyer
Split functions
1203
    # some times I really hate bash reverse binary logic
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
1204
    return $exitcode
1922.1.1 by Dean Troyer
Split functions
1205
}
1206
2491.1.1 by Dean Troyer
Run processes without screen
1207
# Run a single service under screen or directly
1208
# If the command includes shell metachatacters (;<>*) it must be run using a shell
2496.2.1 by Chris Dent
Replace screen_it() with run_process() throughout
1209
# If an optional group is provided sg will be used to run the
1210
# command as that group.
1211
# run_process service "command-line" [group]
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1212
function run_process {
1922.1.1 by Dean Troyer
Split functions
1213
    local service=$1
1214
    local command="$2"
2496.2.1 by Chris Dent
Replace screen_it() with run_process() throughout
1215
    local group=$3
1922.1.1 by Dean Troyer
Split functions
1216
2491.1.1 by Dean Troyer
Run processes without screen
1217
    if is_service_enabled $service; then
1218
        if [[ "$USE_SCREEN" = "True" ]]; then
2592.2.2 by Adam Gandelman
Make screen_service() useful for more than services
1219
            screen_process "$service" "$command" "$group"
2491.1.1 by Dean Troyer
Run processes without screen
1220
        else
1221
            # Spawn directly without screen
2496.2.1 by Chris Dent
Replace screen_it() with run_process() throughout
1222
            _run_process "$service" "$command" "$group" &
2491.1.1 by Dean Troyer
Run processes without screen
1223
        fi
1224
    fi
1922.1.1 by Dean Troyer
Split functions
1225
}
1226
2592.2.2 by Adam Gandelman
Make screen_service() useful for more than services
1227
# Helper to launch a process in a named screen
2835.1.1 by Dean Troyer
Deprecate SCREEN_LOGDIR in favor of LOGDIR
1228
# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
1229
# ``SERVICE_DIR``, ``USE_SCREEN``
2592.2.2 by Adam Gandelman
Make screen_service() useful for more than services
1230
# screen_process name "command-line" [group]
2496.2.1 by Chris Dent
Replace screen_it() with run_process() throughout
1231
# Run a command in a shell in a screen window, if an optional group
1232
# is provided, use sg to set the group of the command.
2592.2.2 by Adam Gandelman
Make screen_service() useful for more than services
1233
function screen_process {
1234
    local name=$1
2491.1.1 by Dean Troyer
Run processes without screen
1235
    local command="$2"
2496.2.1 by Chris Dent
Replace screen_it() with run_process() throughout
1236
    local group=$3
2491.1.1 by Dean Troyer
Run processes without screen
1237
2301.1.1 by Sean Dague
Revert "Build retry loop for screen sessions"
1238
    SCREEN_NAME=${SCREEN_NAME:-stack}
1239
    SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
1240
    USE_SCREEN=$(trueorfalse True USE_SCREEN)
1922.1.1 by Dean Troyer
Split functions
1241
2592.2.2 by Adam Gandelman
Make screen_service() useful for more than services
1242
    screen -S $SCREEN_NAME -X screen -t $name
1243
2835.1.1 by Dean Troyer
Deprecate SCREEN_LOGDIR in favor of LOGDIR
1244
    local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1245
    echo "LOGDIR: $LOGDIR"
1246
    echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1247
    echo "log: $real_logfile"
1248
    if [[ -n ${LOGDIR} ]]; then
1249
        screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
2592.2.2 by Adam Gandelman
Make screen_service() useful for more than services
1250
        screen -S $SCREEN_NAME -p $name -X log on
2835.1.1 by Dean Troyer
Deprecate SCREEN_LOGDIR in favor of LOGDIR
1251
        ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1252
        if [[ -n ${SCREEN_LOGDIR} ]]; then
1253
            # Drop the backward-compat symlink
1254
            ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1255
        fi
2592.2.2 by Adam Gandelman
Make screen_service() useful for more than services
1256
    fi
1257
1258
    # sleep to allow bash to be ready to be send the command - we are
1259
    # creating a new window in screen and then sends characters, so if
3113.1.1 by Sean Dague
Make screen sleep time configurable
1260
    # bash isn't running by the time we send the command, nothing
1261
    # happens.  This sleep was added originally to handle gate runs
1262
    # where we needed this to be at least 3 seconds to pass
1263
    # consistently on slow clouds. Now this is configurable so that we
1264
    # can determine a reasonable value for the local case which should
1265
    # be much smaller.
1266
    sleep ${SCREEN_SLEEP:-3}
2592.2.2 by Adam Gandelman
Make screen_service() useful for more than services
1267
1268
    NL=`echo -ne '\015'`
1269
    # This fun command does the following:
1270
    # - the passed server command is backgrounded
1271
    # - the pid of the background process is saved in the usual place
1272
    # - the server process is brought back to the foreground
1273
    # - if the server process exits prematurely the fg command errors
1274
    # and a message is written to stdout and the process failure file
1275
    #
1276
    # The pid saved can be used in stop_process() as a process group
1277
    # id to kill off all child processes
1278
    if [[ -n "$group" ]]; then
1279
        command="sg $group '$command'"
1280
    fi
3122.3.1 by Ian Wienand
Append command to screenrc after we update it
1281
1282
    # Append the process to the screen rc file
1283
    screen_rc "$name" "$command"
1284
2592.2.2 by Adam Gandelman
Make screen_service() useful for more than services
1285
    screen -S $SCREEN_NAME -p $name -X stuff "$command & echo \$! >$SERVICE_DIR/$SCREEN_NAME/${name}.pid; fg || echo \"$name failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/${name}.failure\"$NL"
1922.1.1 by Dean Troyer
Split functions
1286
}
1287
1288
# Screen rc file builder
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
1289
# Uses globals ``SCREEN_NAME``, ``SCREENRC``
1922.1.1 by Dean Troyer
Split functions
1290
# screen_rc service "command-line"
1291
function screen_rc {
1292
    SCREEN_NAME=${SCREEN_NAME:-stack}
1293
    SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1294
    if [[ ! -e $SCREENRC ]]; then
1295
        # Name the screen session
1296
        echo "sessionname $SCREEN_NAME" > $SCREENRC
1297
        # Set a reasonable statusbar
1298
        echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1299
        # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1300
        echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1301
        echo "screen -t shell bash" >> $SCREENRC
1302
    fi
1303
    # If this service doesn't already exist in the screenrc file
1304
    if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1305
        NL=`echo -ne '\015'`
1306
        echo "screen -t $1 bash" >> $SCREENRC
1307
        echo "stuff \"$2$NL\"" >> $SCREENRC
1308
2835.1.1 by Dean Troyer
Deprecate SCREEN_LOGDIR in favor of LOGDIR
1309
        if [[ -n ${LOGDIR} ]]; then
1310
            echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
1922.1.1 by Dean Troyer
Split functions
1311
            echo "log on" >>$SCREENRC
1312
        fi
1313
    fi
1314
}
1315
1316
# Stop a service in screen
1317
# If a PID is available use it, kill the whole process group via TERM
1318
# If screen is being used kill the screen window; this will catch processes
1319
# that did not leave a PID behind
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
1320
# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
2496.2.1 by Chris Dent
Replace screen_it() with run_process() throughout
1321
# screen_stop_service service
2491.1.1 by Dean Troyer
Run processes without screen
1322
function screen_stop_service {
1323
    local service=$1
1324
1922.1.1 by Dean Troyer
Split functions
1325
    SCREEN_NAME=${SCREEN_NAME:-stack}
1326
    SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
1327
    USE_SCREEN=$(trueorfalse True USE_SCREEN)
1922.1.1 by Dean Troyer
Split functions
1328
2491.1.1 by Dean Troyer
Run processes without screen
1329
    if is_service_enabled $service; then
1330
        # Clean up the screen window
1331
        screen -S $SCREEN_NAME -p $service -X kill
1332
    fi
1333
}
1334
1335
# Stop a service process
1336
# If a PID is available use it, kill the whole process group via TERM
1337
# If screen is being used kill the screen window; this will catch processes
1338
# that did not leave a PID behind
1339
# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1340
# stop_process service
1341
function stop_process {
1342
    local service=$1
1343
1344
    SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
1345
    USE_SCREEN=$(trueorfalse True USE_SCREEN)
2491.1.1 by Dean Troyer
Run processes without screen
1346
1347
    if is_service_enabled $service; then
1922.1.1 by Dean Troyer
Split functions
1348
        # Kill via pid if we have one available
2491.1.1 by Dean Troyer
Run processes without screen
1349
        if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1350
            pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1351
            rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
1922.1.1 by Dean Troyer
Split functions
1352
        fi
1353
        if [[ "$USE_SCREEN" = "True" ]]; then
1354
            # Clean up the screen window
2491.1.1 by Dean Troyer
Run processes without screen
1355
            screen_stop_service $service
1922.1.1 by Dean Troyer
Split functions
1356
        fi
1357
    fi
1358
}
1359
1360
# Helper to get the status of each running service
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
1361
# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
1922.1.1 by Dean Troyer
Split functions
1362
# service_check
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1363
function service_check {
1922.1.1 by Dean Troyer
Split functions
1364
    local service
1365
    local failures
1366
    SCREEN_NAME=${SCREEN_NAME:-stack}
1367
    SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1368
1369
1370
    if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1371
        echo "No service status directory found"
1372
        return
1373
    fi
1374
1375
    # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME
1977.2.2 by Sean Dague
enable -o errexit
1376
    # make this -o errexit safe
1377
    failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
1922.1.1 by Dean Troyer
Split functions
1378
1379
    for service in $failures; do
1380
        service=`basename $service`
1381
        service=${service%.failure}
1382
        echo "Error: Service $service is not running"
1383
    done
1384
1385
    if [ -n "$failures" ]; then
1990.3.1 by Sean Dague
make service_check fatal
1386
        die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
1922.1.1 by Dean Troyer
Split functions
1387
    fi
1388
}
1389
2496.2.1 by Chris Dent
Replace screen_it() with run_process() throughout
1390
# Tail a log file in a screen if USE_SCREEN is true.
1391
function tail_log {
2592.2.2 by Adam Gandelman
Make screen_service() useful for more than services
1392
    local name=$1
2496.2.1 by Chris Dent
Replace screen_it() with run_process() throughout
1393
    local logfile=$2
1394
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
1395
    USE_SCREEN=$(trueorfalse True USE_SCREEN)
2496.2.1 by Chris Dent
Replace screen_it() with run_process() throughout
1396
    if [[ "$USE_SCREEN" = "True" ]]; then
2592.2.2 by Adam Gandelman
Make screen_service() useful for more than services
1397
        screen_process "$name" "sudo tail -f $logfile"
2496.2.1 by Chris Dent
Replace screen_it() with run_process() throughout
1398
    fi
1399
}
1400
1922.1.1 by Dean Troyer
Split functions
1401
2491.1.1 by Dean Troyer
Run processes without screen
1402
# Deprecated Functions
1403
# --------------------
1404
1405
# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1406
# fork.  It includes the dirty work of closing extra filehandles and preparing log
1407
# files to produce the same logs as screen_it().  The log filename is derived
1408
# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1409
# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1410
# _old_run_process service "command-line"
1411
function _old_run_process {
1412
    local service=$1
1413
    local command="$2"
1414
1415
    # Undo logging redirections and close the extra descriptors
1416
    exec 1>&3
1417
    exec 2>&3
1418
    exec 3>&-
1419
    exec 6>&-
1420
1421
    if [[ -n ${SCREEN_LOGDIR} ]]; then
2820.1.1 by Dean Troyer
Rename screen logfiles
1422
        exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1423
        ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
2491.1.1 by Dean Troyer
Run processes without screen
1424
1425
        # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1426
        export PYTHONUNBUFFERED=1
1427
    fi
1428
1429
    exec /bin/bash -c "$command"
1430
    die "$service exec failure: $command"
1431
}
1432
1433
# old_run_process() launches a child process that closes all file descriptors and
1434
# then exec's the passed in command.  This is meant to duplicate the semantics
1435
# of screen_it() without screen.  PIDs are written to
1436
# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1437
# old_run_process service "command-line"
1438
function old_run_process {
1439
    local service=$1
1440
    local command="$2"
1441
1442
    # Spawn the child process
1443
    _old_run_process "$service" "$command" &
1444
    echo $!
1445
}
1446
1447
# Compatibility for existing start_XXXX() functions
1448
# Uses global ``USE_SCREEN``
1449
# screen_it service "command-line"
1450
function screen_it {
1451
    if is_service_enabled $1; then
1452
        # Append the service to the screen rc file
1453
        screen_rc "$1" "$2"
1454
1455
        if [[ "$USE_SCREEN" = "True" ]]; then
2592.2.2 by Adam Gandelman
Make screen_service() useful for more than services
1456
            screen_process "$1" "$2"
2491.1.1 by Dean Troyer
Run processes without screen
1457
        else
1458
            # Spawn directly without screen
1459
            old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1460
        fi
1461
    fi
1462
}
1463
1464
# Compatibility for existing stop_XXXX() functions
1465
# Stop a service in screen
1466
# If a PID is available use it, kill the whole process group via TERM
1467
# If screen is being used kill the screen window; this will catch processes
1468
# that did not leave a PID behind
1469
# screen_stop service
1470
function screen_stop {
1471
    # Clean up the screen window
1472
    stop_process $1
1473
}
1474
1475
2773.1.1 by Sean Dague
Implement devstack external plugins
1476
# Plugin Functions
1477
# =================
1478
1479
DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1480
1481
# enable_plugin <name> <url> [branch]
1482
#
1483
# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1484
# ``url`` is a git url
1485
# ``branch`` is a gitref. If it's not set, defaults to master
1486
function enable_plugin {
1487
    local name=$1
1488
    local url=$2
1489
    local branch=${3:-master}
1490
    DEVSTACK_PLUGINS+=",$name"
1491
    GITREPO[$name]=$url
1492
    GITDIR[$name]=$DEST/$name
1493
    GITBRANCH[$name]=$branch
1494
}
1495
1496
# fetch_plugins
1497
#
1498
# clones all plugins
1499
function fetch_plugins {
1500
    local plugins="${DEVSTACK_PLUGINS}"
1501
    local plugin
1502
1503
    # short circuit if nothing to do
1504
    if [[ -z $plugins ]]; then
1505
        return
1506
    fi
1507
3081.1.1 by Dean Troyer
Mostly docs cleanups
1508
    echo "Fetching DevStack plugins"
2773.1.1 by Sean Dague
Implement devstack external plugins
1509
    for plugin in ${plugins//,/ }; do
1510
        git_clone_by_name $plugin
1511
    done
1512
}
1513
1514
# load_plugin_settings
1515
#
1516
# Load settings from plugins in the order that they were registered
1517
function load_plugin_settings {
1518
    local plugins="${DEVSTACK_PLUGINS}"
1519
    local plugin
1520
1521
    # short circuit if nothing to do
1522
    if [[ -z $plugins ]]; then
1523
        return
1524
    fi
1525
1526
    echo "Loading plugin settings"
1527
    for plugin in ${plugins//,/ }; do
1528
        local dir=${GITDIR[$plugin]}
1529
        # source any known settings
1530
        if [[ -f $dir/devstack/settings ]]; then
1531
            source $dir/devstack/settings
1532
        fi
1533
    done
1534
}
1535
3064.1.1 by Sean Dague
provide an override-defaults phase
1536
# plugin_override_defaults
1537
#
1538
# Run an extremely early setting phase for plugins that allows default
1539
# overriding of services.
1540
function plugin_override_defaults {
1541
    local plugins="${DEVSTACK_PLUGINS}"
1542
    local plugin
1543
1544
    # short circuit if nothing to do
1545
    if [[ -z $plugins ]]; then
1546
        return
1547
    fi
1548
1549
    echo "Overriding Configuration Defaults"
1550
    for plugin in ${plugins//,/ }; do
1551
        local dir=${GITDIR[$plugin]}
1552
        # source any overrides
1553
        if [[ -f $dir/devstack/override-defaults ]]; then
1554
            # be really verbose that an override is happening, as it
1555
            # may not be obvious if things fail later.
1556
            echo "$plugin has overriden the following defaults"
1557
            cat $dir/devstack/override-defaults
1558
            source $dir/devstack/override-defaults
1559
        fi
1560
    done
1561
}
1562
2773.1.1 by Sean Dague
Implement devstack external plugins
1563
# run_plugins
1564
#
1565
# Run the devstack/plugin.sh in all the plugin directories. These are
1566
# run in registration order.
1567
function run_plugins {
1568
    local mode=$1
1569
    local phase=$2
2801 by Bharat Kumar Kobagana
Fixing the problem in loading plugins
1570
1571
    local plugins="${DEVSTACK_PLUGINS}"
1572
    local plugin
2773.1.1 by Sean Dague
Implement devstack external plugins
1573
    for plugin in ${plugins//,/ }; do
1574
        local dir=${GITDIR[$plugin]}
1575
        if [[ -f $dir/devstack/plugin.sh ]]; then
1576
            source $dir/devstack/plugin.sh $mode $phase
1577
        fi
1578
    done
1579
}
1580
1581
function run_phase {
1582
    local mode=$1
1583
    local phase=$2
1584
    if [[ -d $TOP_DIR/extras.d ]]; then
1585
        for i in $TOP_DIR/extras.d/*.sh; do
1586
            [[ -r $i ]] && source $i $mode $phase
1587
        done
1588
    fi
1589
    # the source phase corresponds to settings loading in plugins
1590
    if [[ "$mode" == "source" ]]; then
1591
        load_plugin_settings
3064.1.1 by Sean Dague
provide an override-defaults phase
1592
    elif [[ "$mode" == "override_defaults" ]]; then
1593
        plugin_override_defaults
2773.1.1 by Sean Dague
Implement devstack external plugins
1594
    else
1595
        run_plugins $mode $phase
1596
    fi
1597
}
1598
1922.1.1 by Dean Troyer
Split functions
1599
1600
# Service Functions
1601
# =================
1602
1603
# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1604
# _cleanup_service_list service-list
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1605
function _cleanup_service_list {
1922.1.1 by Dean Troyer
Split functions
1606
    echo "$1" | sed -e '
1607
        s/,,/,/g;
1608
        s/^,//;
1609
        s/,$//
1610
    '
1611
}
1612
1613
# disable_all_services() removes all current services
1614
# from ``ENABLED_SERVICES`` to reset the configuration
1615
# before a minimal installation
1616
# Uses global ``ENABLED_SERVICES``
1617
# disable_all_services
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1618
function disable_all_services {
1922.1.1 by Dean Troyer
Split functions
1619
    ENABLED_SERVICES=""
1620
}
1621
1622
# Remove all services starting with '-'.  For example, to install all default
1623
# services except rabbit (rabbit) set in ``localrc``:
1624
# ENABLED_SERVICES+=",-rabbit"
1625
# Uses global ``ENABLED_SERVICES``
1626
# disable_negated_services
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1627
function disable_negated_services {
1922.1.1 by Dean Troyer
Split functions
1628
    local tmpsvcs="${ENABLED_SERVICES}"
1629
    local service
1630
    for service in ${tmpsvcs//,/ }; do
1631
        if [[ ${service} == -* ]]; then
1632
            tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g")
1633
        fi
1634
    done
1635
    ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1636
}
1637
1638
# disable_service() removes the services passed as argument to the
1639
# ``ENABLED_SERVICES`` list, if they are present.
1640
#
1641
# For example:
1642
#   disable_service rabbit
1643
#
1644
# This function does not know about the special cases
1645
# for nova, glance, and neutron built into is_service_enabled().
1646
# Uses global ``ENABLED_SERVICES``
1647
# disable_service service [service ...]
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1648
function disable_service {
1922.1.1 by Dean Troyer
Split functions
1649
    local tmpsvcs=",${ENABLED_SERVICES},"
1650
    local service
1651
    for service in $@; do
1652
        if is_service_enabled $service; then
1653
            tmpsvcs=${tmpsvcs//,$service,/,}
1654
        fi
1655
    done
1656
    ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1657
}
1658
1659
# enable_service() adds the services passed as argument to the
1660
# ``ENABLED_SERVICES`` list, if they are not already present.
1661
#
1662
# For example:
1663
#   enable_service qpid
1664
#
1665
# This function does not know about the special cases
1666
# for nova, glance, and neutron built into is_service_enabled().
1667
# Uses global ``ENABLED_SERVICES``
1668
# enable_service service [service ...]
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1669
function enable_service {
1922.1.1 by Dean Troyer
Split functions
1670
    local tmpsvcs="${ENABLED_SERVICES}"
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
1671
    local service
1922.1.1 by Dean Troyer
Split functions
1672
    for service in $@; do
1673
        if ! is_service_enabled $service; then
1674
            tmpsvcs+=",$service"
1675
        fi
1676
    done
1677
    ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1678
    disable_negated_services
1679
}
1680
1681
# is_service_enabled() checks if the service(s) specified as arguments are
1682
# enabled by the user in ``ENABLED_SERVICES``.
1683
#
1684
# Multiple services specified as arguments are ``OR``'ed together; the test
1685
# is a short-circuit boolean, i.e it returns on the first match.
1686
#
1687
# There are special cases for some 'catch-all' services::
1688
#   **nova** returns true if any service enabled start with **n-**
1689
#   **cinder** returns true if any service enabled start with **c-**
1690
#   **ceilometer** returns true if any service enabled start with **ceilometer**
1691
#   **glance** returns true if any service enabled start with **g-**
1692
#   **neutron** returns true if any service enabled start with **q-**
1693
#   **swift** returns true if any service enabled start with **s-**
1694
#   **trove** returns true if any service enabled start with **tr-**
1695
#   For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1696
#   **s-** services will be enabled. This will be deprecated in the future.
1697
#
1698
# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1699
# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1700
# as enabled in this case.
1701
#
1702
# Uses global ``ENABLED_SERVICES``
1703
# is_service_enabled service [service ...]
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1704
function is_service_enabled {
1966.2.1 by Sean Dague
xtrace less
1705
    local xtrace=$(set +o | grep xtrace)
1706
    set +o xtrace
1707
    local enabled=1
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
1708
    local services=$@
1709
    local service
1922.1.1 by Dean Troyer
Split functions
1710
    for service in ${services}; do
1966.2.1 by Sean Dague
xtrace less
1711
        [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
1922.1.1 by Dean Troyer
Split functions
1712
1713
        # Look for top-level 'enabled' function for this service
1714
        if type is_${service}_enabled >/dev/null 2>&1; then
1715
            # A function exists for this service, use it
1716
            is_${service}_enabled
1966.2.1 by Sean Dague
xtrace less
1717
            enabled=$?
1922.1.1 by Dean Troyer
Split functions
1718
        fi
1719
1720
        # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1721
        #                are implemented
1722
1966.2.1 by Sean Dague
xtrace less
1723
        [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
2496.2.1 by Chris Dent
Replace screen_it() with run_process() throughout
1724
        [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
1966.2.1 by Sean Dague
xtrace less
1725
        [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
1726
        [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0
1727
        [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1728
        [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1729
        [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1730
        [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1731
        [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1732
        [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1733
        [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
1922.1.1 by Dean Troyer
Split functions
1734
    done
1966.2.1 by Sean Dague
xtrace less
1735
    $xtrace
1736
    return $enabled
1922.1.1 by Dean Troyer
Split functions
1737
}
1738
1739
# Toggle enable/disable_service for services that must run exclusive of each other
1740
#  $1 The name of a variable containing a space-separated list of services
1741
#  $2 The name of a variable in which to store the enabled service's name
1742
#  $3 The name of the service to enable
1743
function use_exclusive_service {
1744
    local options=${!1}
1745
    local selection=$3
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
1746
    local out=$2
1922.1.1 by Dean Troyer
Split functions
1747
    [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
1748
    local opt
1922.1.1 by Dean Troyer
Split functions
1749
    for opt in $options;do
1750
        [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1751
    done
1752
    eval "$out=$selection"
1753
    return 0
1754
}
1755
1756
1954.3.1 by Masayuki Igawa
Fix comments about System Functions
1757
# System Functions
1758
# ================
1922.1.1 by Dean Troyer
Split functions
1759
1760
# Only run the command if the target file (the last arg) is not on an
1761
# NFS filesystem.
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1762
function _safe_permission_operation {
1966.2.1 by Sean Dague
xtrace less
1763
    local xtrace=$(set +o | grep xtrace)
1764
    set +o xtrace
1922.1.1 by Dean Troyer
Split functions
1765
    local args=( $@ )
1766
    local last
1767
    local sudo_cmd
1768
    local dir_to_check
1769
1770
    let last="${#args[*]} - 1"
1771
2356.2.1 by Dean Troyer
Clean up local variable usage - misc functions
1772
    local dir_to_check=${args[$last]}
1922.1.1 by Dean Troyer
Split functions
1773
    if [ ! -d "$dir_to_check" ]; then
1774
        dir_to_check=`dirname "$dir_to_check"`
1775
    fi
1776
1777
    if is_nfs_directory "$dir_to_check" ; then
1966.2.1 by Sean Dague
xtrace less
1778
        $xtrace
1922.1.1 by Dean Troyer
Split functions
1779
        return 0
1780
    fi
1781
1782
    if [[ $TRACK_DEPENDS = True ]]; then
1783
        sudo_cmd="env"
1784
    else
1785
        sudo_cmd="sudo"
1786
    fi
1787
1966.2.1 by Sean Dague
xtrace less
1788
    $xtrace
1922.1.1 by Dean Troyer
Split functions
1789
    $sudo_cmd $@
1790
}
1791
1792
# Exit 0 if address is in network or 1 if address is not in network
1793
# ip-range is in CIDR notation: 1.2.3.4/20
1794
# address_in_net ip-address ip-range
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1795
function address_in_net {
1922.1.1 by Dean Troyer
Split functions
1796
    local ip=$1
1797
    local range=$2
1798
    local masklen=${range#*/}
1799
    local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1800
    local subnet=$(maskip $ip $(cidr2netmask $masklen))
1801
    [[ $network == $subnet ]]
1802
}
1803
1804
# Add a user to a group.
1805
# add_user_to_group user group
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1806
function add_user_to_group {
1922.1.1 by Dean Troyer
Split functions
1807
    local user=$1
1808
    local group=$2
1809
1810
    if [[ -z "$os_VENDOR" ]]; then
1811
        GetOSVersion
1812
    fi
1813
1814
    # SLE11 and openSUSE 12.2 don't have the usual usermod
1815
    if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then
1816
        sudo usermod -a -G "$group" "$user"
1817
    else
1818
        sudo usermod -A "$group" "$user"
1819
    fi
1820
}
1821
1822
# Convert CIDR notation to a IPv4 netmask
1823
# cidr2netmask cidr-bits
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1824
function cidr2netmask {
1922.1.1 by Dean Troyer
Split functions
1825
    local maskpat="255 255 255 255"
1826
    local maskdgt="254 252 248 240 224 192 128"
1827
    set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1828
    echo ${1-0}.${2-0}.${3-0}.${4-0}
1829
}
1830
1831
# Gracefully cp only if source file/dir exists
1832
# cp_it source destination
1833
function cp_it {
1834
    if [ -e $1 ] || [ -d $1 ]; then
1835
        cp -pRL $1 $2
1836
    fi
1837
}
1838
1839
# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1840
# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1841
# ``localrc`` or on the command line if necessary::
1842
#
1843
# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1844
#
1845
#     http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1846
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1847
function export_proxy_variables {
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
1848
    if isset http_proxy ; then
1922.1.1 by Dean Troyer
Split functions
1849
        export http_proxy=$http_proxy
1850
    fi
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
1851
    if isset https_proxy ; then
1922.1.1 by Dean Troyer
Split functions
1852
        export https_proxy=$https_proxy
1853
    fi
2818.1.1 by Sean Dague
Make changes such that -o nounset runs
1854
    if isset no_proxy ; then
1922.1.1 by Dean Troyer
Split functions
1855
        export no_proxy=$no_proxy
1856
    fi
1857
}
1858
1859
# Returns true if the directory is on a filesystem mounted via NFS.
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1860
function is_nfs_directory {
1922.1.1 by Dean Troyer
Split functions
1861
    local mount_type=`stat -f -L -c %T $1`
1862
    test "$mount_type" == "nfs"
1863
}
1864
1865
# Return the network portion of the given IP address using netmask
1866
# netmask is in the traditional dotted-quad format
1867
# maskip ip-address netmask
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1868
function maskip {
1922.1.1 by Dean Troyer
Split functions
1869
    local ip=$1
1870
    local mask=$2
1871
    local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
1872
    local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
1873
    echo $subnet
1874
}
1875
1876
# Service wrapper to restart services
1877
# restart_service service-name
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1878
function restart_service {
1922.1.1 by Dean Troyer
Split functions
1879
    if is_ubuntu; then
1880
        sudo /usr/sbin/service $1 restart
1881
    else
1882
        sudo /sbin/service $1 restart
1883
    fi
1884
}
1885
1886
# Only change permissions of a file or directory if it is not on an
1887
# NFS filesystem.
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1888
function safe_chmod {
1922.1.1 by Dean Troyer
Split functions
1889
    _safe_permission_operation chmod $@
1890
}
1891
1892
# Only change ownership of a file or directory if it is not on an NFS
1893
# filesystem.
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1894
function safe_chown {
1922.1.1 by Dean Troyer
Split functions
1895
    _safe_permission_operation chown $@
1896
}
1897
1898
# Service wrapper to start services
1899
# start_service service-name
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1900
function start_service {
1922.1.1 by Dean Troyer
Split functions
1901
    if is_ubuntu; then
1902
        sudo /usr/sbin/service $1 start
1903
    else
1904
        sudo /sbin/service $1 start
1905
    fi
1906
}
1907
1908
# Service wrapper to stop services
1909
# stop_service service-name
1990.2.1 by Ian Wienand
Enforce function declaration format in bash8
1910
function stop_service {
1922.1.1 by Dean Troyer
Split functions
1911
    if is_ubuntu; then
1912
        sudo /usr/sbin/service $1 stop
1913
    else
1914
        sudo /sbin/service $1 stop
1915
    fi
1916
}
1917
1918
1919
# Restore xtrace
1920
$XTRACE
1921
1922
# Local variables:
1923
# mode: shell-script
1924
# End: