summaryrefslogtreecommitdiffstats
path: root/postgresql-tests/gen-data/generate
blob: 996f9d3d02174396dcd8e1b100da9e6a22e0b7b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash

GEN_DATADIR=/var/lib/pgsql/data/data
CACHEDIR=/var/cache/dbt
OUTPUTDIR="$(pwd)/results"
SRCDIR="$(pwd)"
DEBUG=1
CHECK_LOCALE=1

printline() { echo "$INDENT$@" ; }

die() { printline " ERROR $@" ; exit 1 ; }

info() { printline " * $@"; }

debug() { test $DEBUG -eq 1 && printline " ~ $@"; }

admin_cmd()
{
    su - postgres -c "$@" || die "can not execute '$@'"
}

cached_download()
{
    local link=$1
    local bn="$(basename "$1")"
    local file="$CACHEDIR/$bn"

    if test ! -d "$CACHEDIR"; then
        mkdir -p "$CACHEDIR" || die "can't create cachedir"
    fi

    test -e "$file" && cp "$file" ./ && return

    { wget "$link" && cp "$bn" "$CACHEDIR" ; } \
        || die "can't download"
}

is_defined_f()
{
    declare -f "$1" >/dev/null
}

run_if_defined()
{
    is_defined_f "$1" || return 0
    "$1"
}

locale_prereq()
{
    local default_locale="LANG=en_US.UTF-8"
    local current_locale="$(cat /etc/locale.conf)"
    test "$current_locale" = "$default_locale" \
        || die "bad locale '$current_locale', should be '$default_locale'"
}

single_task()
{
    local task="$1"

    info "running task $task"

    (
        INDENT="  "
        . "$SRCDIR/databases/pagila.sh" || die "pagila incl fail"
        . "$SRCDIR/$task" || die "include fail"

        run_if_defined "hook_start"

        test "$CHECK_LOCALE" -eq 1 && locale_prereq

        debug "initializing database"
        postgresql-setup initdb &>/dev/null || die "can not initdb"

        debug "starting server"
        service postgresql start &>/dev/null || die "can't start postgresql"

        tmpdir=$(mktemp -d /tmp/dbt-XXXXXX) || die "can not create temp directory"

        debug "working in $tmpdir"
        pushd "$tmpdir" >/dev/null || die "can switch to $tmpdir"

        run || die "fail"

        popd >/dev/null || die "can not switch back"

        debug "stopping server"
        service postgresql stop &>/dev/null || die "can't stop postgresql"

        tarball="$OUTPUTDIR/$task_name.tar.gz"
        tar -czf "$tarball" -C /var/lib/pgsql/data data
        info "result tarball $tarball"
        rm -rf "$tmpdir"


        debug "data removal"
        rm -rf "$GEN_DATADIR"

        run_if_defined "hook_end"
    )
}

generate_tasks()
{
    rpm -qi postgresql > "$OUTPUTDIR"/postgresql-info
    find ./tasks -name run.sh | while read line; do
        single_task "$line"
    done
}

main()
{
    test "$UID" -ne 0 && die "run under 'root' user"

    test -d "$OUTPUTDIR" || die "$OUTPUTDIR does not seem to be directory"

    service postgresql status &>/dev/null
    test $? -ne 3 && die "stop the postgresql server"

    local dbdir="$GEN_DATADIR"
    test -e "$dbdir" && die "the '$dbdir' should not exist"

    { rpm -qa postgresql && rpm -qa postgresql-server ; } &>/dev/null \
        || die "PostgreSQL not installed"

    generate_tasks
}

main