sitevarscheck.in 2.88 KB
Newer Older
1 2 3 4
#!/usr/bin/perl -w

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2006 University of Utah and the Flux Group.
6 7 8 9 10 11 12 13 14 15 16 17 18
# All rights reserved.
#
use strict;

#
# sitevarscheck - Make sure that the site variables in the currently-running
# 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
#

19 20 21 22 23
#
# Function prototypes
#
sub fatal(@);

24 25 26 27 28 29 30 31 32
#
# Configure variables
#
my $testbed_srcdir = "@top_srcdir@";
my $DBNAME         = "@TBDBNAME@";

my $master_sitevars = "$testbed_srcdir/sql/sitevars-create.sql";
my $sitevarsdiff = "$testbed_srcdir/utils/sitevarsdiff";

33
my $isvers5   = system("mysql -V | egrep -q -s 'Distrib 5.'") == 0;
34 35
my $extraopts = ($isvers5 ? "--skip-opt" : "");

36 37 38 39
#
# XXX: Requires that mysqldump be in caller's $PATH - probably an OK
# assumption, but maybe not always
#
40
my $mysqldump = "mysqldump $extraopts -t $DBNAME sitevariables";
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

#
# We use Mysql module instead of the testbed libraries, so that we can get
# a more precise feel for what the error is.
#
use Mysql;

#
# Check to see if the mysqld is running by trying to connect to it.
#
my $dbh = Mysql->connect(undef,undef,undef,undef);
if (!$dbh) {
    fatal("Unable to connect to mysqld - Either it's not running or \n" .
    	  "permission was denied.\n");
}

#
# Check to see if the database exists
#
my @dbs = $dbh->listdbs();
if (! grep (/^$DBNAME$/,@dbs)) {
    fatal("Database $DBNAME does not exist in the running database\n");
}
undef $dbh;

#
# Make sure we can write the temporary file
#
my $db_sitevars = "/tmp/sitevarscheck.$$";
open(SITEVARSFILE,">$db_sitevars") or
    fatal("Unable to write to temporary file $db_sitevars: $!\n");
close(SITEVARSFILE);

#
# Stick the output of mysqldump into a file
#
system "$mysqldump 2> /dev/null > $db_sitevars";

#
# ... and compare it to the sitevars from the source
#
my $diff = (system("$sitevarsdiff $db_sitevars $master_sitevars") >> 8);
unlink($db_sitevars);

if ($diff) {

    print << "EOSTR";

The sitevariables from $master_sitevars differs from
the running database. As a safety measure, you will not be able to install
until this is resolved.

If you are a user of the emulab software outside of Utah, this is probably
because your site variables have not been updated since the installation
of an older version of the emulab software.
Contact testbed-ops\@emulab.net for help in updating your database.

If you're a developer at Utah, this is probably because you (or someone else)
forgot to update sql/sitevars-create.sql .

EOSTR

    exit(1);
} else {
    exit(0);
}

sub fatal(@) {

    # Offset the text with a few blank lines, so it doesn't get lost in the
    # make messages
    warn "\n\n";
    warn "*** sitevarscheck - Unable to check database sitevars:\n";
    warn @_;
    warn "\n\n";

    exit(-1);

}