dbfillcheck.in 3.18 KB
Newer Older
1 2 3
#!/usr/bin/perl -w

#
4
# Copyright (c) 2000-2010 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
24 25
#
use strict;
26
use Getopt::Std;
27 28 29 30 31 32 33 34

#
# dbfillcheck - Make sure that the initial fill contents on the
# database match the ones in the checked-out source.  Exits with value
# 1 if the two differ, 0 if they do not, and -1 if some error (such as
# the mysql server is not running, or the database does not exist)
# prevents us from telling if they differ or not.
#
35
my $optlist = "d";
36 37 38 39 40

#
# Configure variables
#
my $testbed_srcdir = "@top_srcdir@";
41
my $DBNAME         = "@TBDBNAME@";
42 43

# Locals
44 45
my $dbfill        = "sql/database-fill.sql";
my $master_fill   = "$testbed_srcdir/$dbfill";
46
my $export_tables = "../utils/export_tables";
47 48 49
my $tempdifffile  = "/tmp/dbdiff.$$";
my $tempnewfile   = "/tmp/dbdiff.new.$$";
my $tempoldfile   = "/tmp/dbdiff.old.$$";
50
my $exitcode      = 0;
51
my $debug         = 1;
52 53 54 55 56 57 58 59 60

my %options = ();
if (! getopts($optlist, \%options)) {
    die("*** $0:\n".
	"    Usage: dbfillcheck [-d]\n");
}
if (defined($options{"d"})) {
    $debug = 1;
}
61 62 63

#
# Diff exits with 2 if a problem. We tell diff to ignore whitespace
64 65
# changes and comment lines (line starting with '--').
#
66 67 68
# We sort(1) the text files so that we don't pick up differences in ordering,
# which could be pretty random.
#
69 70 71 72
if (system("perl $export_tables | ".
	   "grep -E -v -e '^--.*\$' -e '^\\/\\*.*\\*\\/;\$' | ".
	   "sed -e 's/INTO `\\(.*\\)`/INTO \\1/' | ".
	   "sort > $tempoldfile")) {
73 74 75 76
    unlink($tempoldfile);
    die("*** $0:\n".
	"    $export_tables failed!\n");
}
77 78 79 80
if (system("grep -E -v '^(LOCK|UNLOCK|DELETE) ' $master_fill | ".
	   "grep -E -v -e '^--.*\$' -e '^\\/\\*.*\\*\\/;\$' | ".
	   "sed -e 's/INTO `\\(.*\\)`/INTO \\1/' | ".
	   "sort > $tempnewfile")) {
81
    unlink($tempoldfile);
82
    die("*** $0:\n".
83
	"    sort $master_fill failed!\n");
84
}
85
if (system("diff -b -B $tempoldfile $tempnewfile > $tempdifffile")
86 87 88 89 90 91 92 93
	== 2) {
    unlink($tempdifffile);
    die("*** $0:\n".
	"    diff $tempoldfile $tempnewfile failed!\n");
}

unlink($tempoldfile);
unlink($tempnewfile);
94 95 96

# No differences ...
if (!$?) {
97
    unlink($tempdifffile);
98 99 100 101
    exit(0);
}

#
102 103
# Oh well. Explain to the user what they need to do, and exit with non-zero
# status
104
#
105
print "*** Your installed database is out of date with $dbfill\n";
106 107
print "    Differences are listed in $tempdifffile\n"
    if ($debug);
108 109 110
print "    As a safety measure, you will not be able to install until this\n";
print "    is resolved.\n";

111 112
unlink($tempdifffile)
    if (!$debug);
113
exit(1);