lib/update.utils
author Tero Marttila <terom@paivola.fi>
Tue, 17 Dec 2013 00:26:44 +0200
branchdns-new
changeset 83 5a83f2abc0dd
parent 82 26a307558602
child 85 4ad9c9b7cd0e
permissions -rw-r--r--
cmd: log failed command
60
b65ce9123039 lib/update: add shebangs to make sloccount work
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
     1
#!/bin/bash
52
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     2
#
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     3
# Utility functions
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     4
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     5
### Command execution
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     6
## Execute command, possibly logging its execution.
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     7
#
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     8
#   cmd     $cmd...
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     9
#
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    10
# Fails if the command returns an error exit code.
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    11
function cmd {
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    12
    log_cmd "$@"
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    13
83
5a83f2abc0dd cmd: log failed command
Tero Marttila <terom@paivola.fi>
parents: 82
diff changeset
    14
    "$@" || die "Failed: $@"
52
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    15
}
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    16
66
482d06935d96 update: cmd_test; check_dhcp_hosts after do_reload_zones; reload_dhcp
Tero Marttila <terom@paivola.fi>
parents: 60
diff changeset
    17
## Execute command as a test, logging its execution at log_cmd
482d06935d96 update: cmd_test; check_dhcp_hosts after do_reload_zones; reload_dhcp
Tero Marttila <terom@paivola.fi>
parents: 60
diff changeset
    18
#
482d06935d96 update: cmd_test; check_dhcp_hosts after do_reload_zones; reload_dhcp
Tero Marttila <terom@paivola.fi>
parents: 60
diff changeset
    19
#   cmd_test $cmd... && ... || ...
482d06935d96 update: cmd_test; check_dhcp_hosts after do_reload_zones; reload_dhcp
Tero Marttila <terom@paivola.fi>
parents: 60
diff changeset
    20
#
482d06935d96 update: cmd_test; check_dhcp_hosts after do_reload_zones; reload_dhcp
Tero Marttila <terom@paivola.fi>
parents: 60
diff changeset
    21
# Fails if the command returns an error exit code.
482d06935d96 update: cmd_test; check_dhcp_hosts after do_reload_zones; reload_dhcp
Tero Marttila <terom@paivola.fi>
parents: 60
diff changeset
    22
function cmd_test {
482d06935d96 update: cmd_test; check_dhcp_hosts after do_reload_zones; reload_dhcp
Tero Marttila <terom@paivola.fi>
parents: 60
diff changeset
    23
    log_cmd "$@"
482d06935d96 update: cmd_test; check_dhcp_hosts after do_reload_zones; reload_dhcp
Tero Marttila <terom@paivola.fi>
parents: 60
diff changeset
    24
482d06935d96 update: cmd_test; check_dhcp_hosts after do_reload_zones; reload_dhcp
Tero Marttila <terom@paivola.fi>
parents: 60
diff changeset
    25
    "$@"
482d06935d96 update: cmd_test; check_dhcp_hosts after do_reload_zones; reload_dhcp
Tero Marttila <terom@paivola.fi>
parents: 60
diff changeset
    26
}
55
a2d87cfd77e4 update: LOG_DIFF to fix -q
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
    27
## Execute command, prefixing its output on stdout with given indent prefix.
a2d87cfd77e4 update: LOG_DIFF to fix -q
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
    28
#
a2d87cfd77e4 update: LOG_DIFF to fix -q
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
    29
#   indent  "    " $cmd...
a2d87cfd77e4 update: LOG_DIFF to fix -q
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
    30
#
a2d87cfd77e4 update: LOG_DIFF to fix -q
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
    31
# Output is kept on stdout, exit status is that of the given command.
52
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    32
function indent () {
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    33
    local indent=$1; shift
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    34
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    35
    "$@" | sed "s/^/$indent/"
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    36
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    37
    return ${PIPESTATUS[0]}
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    38
}
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    39
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    40
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    41
### FS utils
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    42
# Create dir if not exists.
52
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    43
function ensure_dir {
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    44
    local dir=$1
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    45
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    46
    if [ ! -d $dir ]; then
52
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    47
        log_warn "Creating output dir: $dir"
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    48
        cmd mkdir $dir
52
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    49
    fi
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    50
}
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    51
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    52
## Output absolute path
52
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    53
#
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    54
#   abspath $path
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    55
#
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    56
# XXX: improve...?
52
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    57
function abspath () {
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    58
    local path=$1
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    59
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    60
    echo "$SRV/$path"
52
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    61
}
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    62
69
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    63
## List names of files in dir:
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    64
#
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    65
#   list_files $dir $glob
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    66
#
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    67
function list_files {
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    68
    local dir=$1
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    69
    local glob=${2:-*}
69
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    70
    local name=
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    71
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    72
    for file in $dir/$glob; do
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    73
        # only files
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    74
        [ -f $file ] || continue
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    75
69
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    76
        # strip prefix
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    77
        name=${file#$dir/}
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    78
        name=${name%$glob}
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    79
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    80
        echo -n "$name "
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    81
    done
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    82
}
88a7683efc54 update: list_files to copy all DHCP_CONFS..
Tero Marttila <terom@paivola.fi>
parents: 66
diff changeset
    83
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    84
## List names of files in dirs..
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    85
function list_dirs {
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    86
    for dir in "$@"; do
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    87
        for file in $dir/*; do
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    88
            echo -n "${file#$dir/}"
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    89
        done
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents: 69
diff changeset
    90
    done
52
b68b8615c512 update: split out code into lib/update.foo
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    91
}