#!/bin/bash GEN_DATADIR=/var/lib/pgsql/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 info "result tarball $tarball" rm -rf "$tmpdir" debug "data removal" rm -rf "$GEN_DATADIR" run_if_defined "hook_end" ) } generate_tasks() { 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