libtbsetup.pm 1.41 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
require 'ctime.pl';
3 4 5 6 7 8 9 10 11 12 13

# This has the common functionality for tbprerun/tbrun/tbend.

# tbs_initdbi(dbname) - Initializes a DBI connection to the testbed database
#                       Returns the database handle.
# tbs_prefix(s) - Returns the file prefix.  I.e. strips off \..+$ from the end.
# tbs_out(s) - Spits s out to stdout and LOGFILE.
# tbs_exec(s) - Acts like system except STDOUT and STDERR are both spat out
#               through tbs_out.  Returns 1 on failure and 0 on success.

my $LOGFILE;
14
my $logging = 0;
15
my $dostamp = 0;
16 17 18 19 20 21 22 23 24 25

sub tbs_initdbi {
    my($dbname) = $_[0];
    return DBI->connect("DBI:mysql:database=$dbname;host=localhost") 
	|| die "Could not connect to DB.\n";
};

sub tbs_initlog {
    my($logfile) = $_[0];

26 27 28
    # Turn off line buffering.
    $| = 1; 

29
    open(LOGFILE,">>$logfile") || do {
30 31 32
	print STDERR "Could not open $logfile for writing.\n";
	exit(1);
    };
33
    $logging = 1;
34 35 36 37 38 39 40 41 42 43 44
}

sub tbs_prefix {
    my($s) = $_[0];
    my($prefix);
    ($prefix) = ($s =~ /^(.+)\.[^.]+$/);
    return $prefix;
};

sub tbs_out {
    my($s) = $_[0];
45 46 47 48 49 50 51
    if ($dostamp) {
	my $t = ctime(time);
	print $t;
	if ($logging) {
	    print LOGFILE $t;
	}
    }
52
    print $s;
53 54 55
    if ($logging) {
	print LOGFILE $s;
    }
56 57 58 59 60 61 62 63
};

sub tbs_exec {
    my($cmd) = $_[0];
    open(EXEC,"$cmd 2>&1|") || return 1;
    while (<EXEC>) {
	&tbs_out($_);
    }
64 65 66
    close(EXEC) or
	return $! ? $! : $?;

67 68 69 70
    return 0;
};

1;