dbupdate.in 5.98 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2009-2012 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{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/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32
#
use strict;
use English;
use Getopt::Std;

#
# Update DB.
#
sub usage()
{
33
    print STDERR "Usage: dbupdate [-v] [-c] [-s] [-f] <dbname> [<version>]\n";
34 35
    exit(-1);
}
36
my $optlist = "dsfvp:qc";
37
my $debug   = 0;
38 39
my $force   = 0;
my $single  = 0;
40
my $verify  = 0;
41
my $check   = 0;
42 43
my $quiet   = 0;
my $path;
44 45 46
my $dbname;
my $version;
my $dbnumber = 0;	# XXX Core emulab code is very rigid.
47
my $verify_count = 0;
48 49 50 51 52 53 54

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $DEFDBNAME	= "@TBDBNAME@";
55
my $testbed_srcdir = "@top_srcdir@";
56 57 58 59 60 61 62 63 64 65 66 67 68 69

# 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; 

70 71 72 73 74 75 76 77 78 79 80
#
# 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");
    }
}
81 82 83
use emdbi;
use libtestbed;

84 85 86 87
# Need this below.
my $objdir = `/bin/pwd`;
chomp($objdir);

88 89 90 91 92 93 94 95 96 97
#
# Parse command arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
98 99 100
if (defined($options{"f"})) {
    $force = 1;
}
101 102 103 104 105 106
if (defined($options{"q"})) {
    $quiet = 1;
}
if (defined($options{"v"})) {
    $verify = 1;
}
107 108 109
if (defined($options{"c"})) {
    $check = 1;
}
110 111 112 113 114
if (defined($options{"p"})) {
    $path = $options{"p"};
    chdir($path) or
	Fatal("$path does not exist");
}
115 116 117 118 119 120
if (defined($options{"s"})) {
    $single = 1;
    if (@ARGV != 2) {
	Fatal("Must provide a version argument when using -s option");
    }
}
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
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 =
152 153 154
	emdbi::DBQueryN($dbnumber,
			"select value from version_info ".
			"where name='dbrev'");
155
    if (!$query_result || !$query_result->numrows) {
156 157 158 159
	if ($dbname eq $DEFDBNAME) {
	    $version = "4.0";
	}
	else {
160
	    $version = "0";
161 162 163 164 165
	}
    }
    else {
	($version) = $query_result->fetchrow_array();
    }
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
}
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");
    }
}
188
my @files;
189

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
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);
}
205 206 207 208 209 210 211 212 213 214 215 216

#
# 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
217 218 219
	if (!$force && !$single && $file <= $start);
    next
	if ($single && $file != $start);
220 221

    my $fullpath = join("/", @dots) . "/$file";
Leigh Stoller's avatar
Leigh Stoller committed
222 223
    my $revision = join(".", @dots);
    $revision .= (@dots ? "." : "") . $file;
224

225 226 227 228 229 230
    if ($verify) {
	print "Need DB update $fullpath\n";
	$verify_count++;
	next;
    }

231 232
    print "" .
	($check ? "Syntax checking" : "Processing") . " update $fullpath\n";
233 234 235 236 237 238 239 240 241 242

    # 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 $!;
    }
243 244 245
    next
	if ($check);
    
246 247 248 249 250 251 252
    # 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' ".
253 254
			 "where name='dbrev'")
	if (!$single && !$force);
255
}
256 257 258
if ($verify) {
    exit($verify_count)
	if (!$verify_count || $quiet);
259

260 261 262 263 264 265 266 267
    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);
}
268 269 270 271 272 273 274 275 276
exit(0);

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

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