dbupdate.in 5.29 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2009-2012 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15
# All rights reserved.
#
use strict;
use English;
use Getopt::Std;

#
# Update DB.
#
sub usage()
{
16
    print STDERR "Usage: dbupdate [-v] [-c] [-s] [-f] <dbname> [<version>]\n";
17 18
    exit(-1);
}
19
my $optlist = "dsfvp:qc";
20
my $debug   = 0;
21 22
my $force   = 0;
my $single  = 0;
23
my $verify  = 0;
24
my $check   = 0;
25 26
my $quiet   = 0;
my $path;
27 28 29
my $dbname;
my $version;
my $dbnumber = 0;	# XXX Core emulab code is very rigid.
30
my $verify_count = 0;
31 32 33 34 35 36 37

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $DEFDBNAME	= "@TBDBNAME@";
38
my $testbed_srcdir = "@top_srcdir@";
39 40 41 42 43 44 45 46 47 48 49 50 51 52

# Protos
sub Fatal($);


# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output
#
$| = 1; 

53 54 55 56 57 58 59 60 61 62 63
#
# Do not modify the include path if it was specified when invoked.
# This allows update to run from the build tree.
#
BEGIN
{
    eval "require emdbi";
    if ($@) {
	unshift(@INC, "@prefix@/lib");
    }
}
64 65 66
use emdbi;
use libtestbed;

67 68 69 70
# Need this below.
my $objdir = `/bin/pwd`;
chomp($objdir);

71 72 73 74 75 76 77 78 79 80
#
# Parse command arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
81 82 83
if (defined($options{"f"})) {
    $force = 1;
}
84 85 86 87 88 89
if (defined($options{"q"})) {
    $quiet = 1;
}
if (defined($options{"v"})) {
    $verify = 1;
}
90 91 92
if (defined($options{"c"})) {
    $check = 1;
}
93 94 95 96 97
if (defined($options{"p"})) {
    $path = $options{"p"};
    chdir($path) or
	Fatal("$path does not exist");
}
98 99 100 101 102 103
if (defined($options{"s"})) {
    $single = 1;
    if (@ARGV != 2) {
	Fatal("Must provide a version argument when using -s option");
    }
}
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 131 132 133 134
if (@ARGV < 1 || @ARGV > 2) {
    usage();
}
$dbname  = shift();
$version = shift()
    if (@ARGV);

# Sanity check the args.
if (! ($dbname =~ /^[-\w]+$/)) {
    Fatal("'$dbname' does not look like a reasonable database name.");
}

#
# Open up a connection to the DB. The core emulab code is all written
# assuming that the first DB connection is used, so go with that. Be
# nice to change someday. For the Protogeni code, I can be less rigid.
#
if ($dbname eq $DEFDBNAME) {
    # This will exit on error by default.
    emdbi::TBDBConnect(0, $dbname);
}
else {
    # This will exit on error by default.
    $dbnumber = emdbi::NewTBDBHandle($dbname);
}

#
# If no version number provided, then grab it from the DB.
#
if (!defined($version)) {
    my $query_result =
135 136 137
	emdbi::DBQueryN($dbnumber,
			"select value from version_info ".
			"where name='dbrev'");
138
    if (!$query_result || !$query_result->numrows) {
139 140 141 142
	if ($dbname eq $DEFDBNAME) {
	    $version = "4.0";
	}
	else {
143
	    $version = "0";
144 145 146 147 148
	}
    }
    else {
	($version) = $query_result->fetchrow_array();
    }
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
}
if (! ($version =~ /^[\d\.]+$/)) {
    Fatal("'$version' does not look like a reasonable starting version.");
}

#
# Split apart the version number. Assumed to be in dotted notation.
#
my @dots  = split(/\./, "$version");
my $start = pop(@dots);

#
# If no dots, then assume the current directory. Otherwise, cd down into
# lowest directory and start. This could get fancier, but I leave that to
# someone else.
#
if (@dots) {
    my $dir = join("/", @dots);
    if (!chdir($dir)) {
	Fatal("Cannot chdir to $dir");
    }
}
171
my @files;
172

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
if ($single) {
    Fatal("Update file $start does not exist")
	if (! -e $start);
    # Just the one file.
    @files = ($start);
}
else {
    #
    # Open up the current directory. We want all numbered files.
    #
    opendir(DIR, ".") or
	Fatal("Could not opendir the current directory");
    @files  = grep { /^\d*$/ } readdir(DIR);
    closedir(DIR);
}
188 189 190 191 192 193 194 195 196 197 198 199

#
# Sort them since we we want to start at the right file, and proceed
# in order.
#
@files = sort {$a <=> $b} @files;

#
# Now process each file starting at the start version.
#
foreach my $file (@files) {
    next
200 201 202
	if (!$force && !$single && $file <= $start);
    next
	if ($single && $file != $start);
203 204

    my $fullpath = join("/", @dots) . "/$file";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
205 206
    my $revision = join(".", @dots);
    $revision .= (@dots ? "." : "") . $file;
207

208 209 210 211 212 213
    if ($verify) {
	print "Need DB update $fullpath\n";
	$verify_count++;
	next;
    }

214 215
    print "" .
	($check ? "Syntax checking" : "Processing") . " update $fullpath\n";
216 217 218 219 220 221 222 223 224 225

    # Undefine this to make sure we get a new version each file.
    undef &DoUpdate;

    # This just loads the file.
    my $return = do $file;
    if (!defined($return)) {
	Fatal("  could not parse $file: $@") if $@;
	Fatal("  could not do $file: $!")    if $!;
    }
226 227 228
    next
	if ($check);
    
229 230 231 232 233 234 235
    # Then we run it.
    if (DoUpdate($dbnumber, $dbname, $revision) != 0) {
	Fatal("  returned non-zero; aborting.\n");
    }
    # Mark that we have done it. 
    emdbi::DBQueryFatalN($dbnumber,
			 "update version_info set value='$revision' ".
236 237
			 "where name='dbrev'")
	if (!$single && !$force);
238
}
239 240 241
if ($verify) {
    exit($verify_count)
	if (!$verify_count || $quiet);
242

243 244 245 246 247 248 249 250
    my $updatedir = "$testbed_srcdir/sql/updates";
    $updatedir =~ s/^\.\.\///;

    print "*** Your have DB update scripts that have not been run.\n";
    print "    As a safety measure, you will not be able to install until\n";
    print "    this is resolved.\n";
    exit($verify_count);
}
251 252 253 254 255 256 257 258 259
exit(0);

sub Fatal($)
{
    my ($msg) = @_;

    die("*** $0:\n".
	"    $msg\n");
}
260