update-install.in 7.67 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2010-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
#
use strict;
use English;
use Getopt::Std;
27
use Data::Dumper;
28 29 30 31 32 33

#
# Update DB.
#
sub usage()
{
34
    print STDERR "Usage: update-install [-v | -n | -l] [-s] [-f] [<version>]\n";
35 36
    exit(-1);
}
37
my $optlist = "dsfvp:qa:cinl";
38 39 40 41 42 43 44
my $debug   = 0;
my $force   = 0;
my $single  = 0;
my $verify  = 0;
my $quiet   = 0;
my $check   = 0;
my $impotent= 0;
45
my $logit   = 0;
46 47 48 49 50 51 52 53 54 55 56
my $path    = ".";
my $phase;
my $version;
my $verify_count = 0;
my $logfp;

#
# Configure variables
#
my $TB	      = "@prefix@";
my $TBOPS     = "@TBOPSEMAIL@";
57
my $logfile   = "/var/tmp/update-install.log";
58 59 60 61 62

# Protos
sub Fatal($);

# un-taint path
63
$ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/site/bin';
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

#
# 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 ($@) {
79 80 81 82 83 84 85 86 87 88
	if (-e "../Makeconf") {
	    # Prior to first install or running from object dir.
	    unshift(@INC, "../db");
	    unshift(@INC, "@top_srcdir@/install");
	    unshift(@INC, "@top_srcdir@/tbsetup");
	    unshift(@INC, "../tbsetup");
	}
	else {
	    unshift(@INC, "@prefix@/lib");
	}
89 90 91 92 93
    }
}
use emdb;
use libtestbed;
use libinstall;
94
use installvars;
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

#
# Parse command arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"a"})) {
    $phase = $options{"a"};
    Fatal("Phase (-a) must be either 'pre' or 'post'")
	if (! ($phase eq "pre" || $phase eq "post"));
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"c"})) {
    $check = 1;
}
114
if (defined($options{"i"}) || defined($options{"n"})) {
115 116 117 118 119
    $impotent = 1;
}
if (defined($options{"f"})) {
    $force = 1;
}
120 121 122
if (defined($options{"l"})) {
    $logit = 1;
}
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
if (defined($options{"v"})) {
    $verify = 1;
}
if (defined($options{"q"})) {
    $quiet = 1;
}
if (defined($options{"p"})) {
    $path = $options{"p"};
}
if (defined($options{"s"})) {
    $single = 1;
    if (@ARGV != 1) {
	Fatal("Must provide a version argument when using -s option");
    }
}
if (@ARGV > 1) {
    usage();
}
$version = shift()
    if (@ARGV);

#
# Must be root if actually doing this.
#
if ($UID && !($impotent || $check || $verify)) {
    Fatal("This script must be run as root! Maybe use sudo?")
}

151 152 153 154 155 156 157 158
#
# This is a utility routine that callee scripts can use to set a DB
# flag to indicate that a reboot is require.
#
sub MarkForReboot($)
{
    my ($version) = @_;

Leigh Stoller's avatar
Leigh Stoller committed
159 160
    print "*** $version says reboot is (will be) required!\n"
	if ($impotent);
161 162 163 164 165 166
    
    emdbi::DBQueryFatal("replace into version_info set ".
			"  value='1', name='needreboot'");
    return 0;
}

167 168 169 170 171 172
if ($impotent || $logit) {
    if (-e $logfile) {
	system("/bin/mv -f $logfile ${logfile}.$$") == 0
	    or Fatal("Could not back up $logfile");
    }
    
173 174 175 176
    open(LOGFP, "> $logfile")
	or Fatal("Could not open $logfile");
    $logfp = *LOGFP;

177 178 179 180 181 182 183 184
    if ($impotent) {
	SET_IMPOTENT_MODE($logfp);
    }
    else {
	SET_LOG_OUTPUT($logfp);
    }
    print "A more detailed log file is being written to $logfile\n";
    sleep(2);
185 186 187 188 189 190 191 192 193 194 195 196 197 198
}

#
# If no version number provided, then grab it from the DB.
#
if (!defined($version)) {
    my $query_result =
	DBQueryFatal("select value from version_info ".
		     "where name='install'");
    if (!$query_result || !$query_result->numrows) {
	$version = "5.0";

	DBQueryFatal("insert into version_info ".
		     "values ('install', '$version')");
199 200
	DBQueryFatal("insert into version_info ".
		     "values ('needreboot', '0')");
201 202 203 204 205 206 207 208
    }
    else {
	($version) = $query_result->fetchrow_array();
    }
}
if (! ($version =~ /^[\d\.]+$/)) {
    Fatal("'$version' does not look like a reasonable starting version.");
}
209 210 211
# Start off with reboots not needed. 
DBQueryFatal("replace into version_info ".
	     "values ('needreboot', '0')");
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

#
# 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. 
#
if (@dots) {
    my $dir = join("/", @dots);
    $path = "$path/$dir";
}
unshift(@INC, "$path");

my @files;

if ($single) {
    Fatal("Update file $start does not exist")
	if (! -e "$path/$start");
    # Just the one file.
    @files = ($start);
}
else {
    #
    # Open up the current directory. We want all numbered files.
    #
    opendir(DIR, $path) or
	Fatal("Could not opendir the current directory");
    @files  = grep { /^\d*$/ } readdir(DIR);
    closedir(DIR);
}

#
# 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.
#
sub RunUpdates($)
{
    my ($phase) = @_;
    
    foreach my $file (@files) {
	next
	    if (!$force && !$single && $file <= $start);
	next
	    if ($single && $file != $start);

	my $fullpath = join("/", @dots) . "/$file";
	my $revision = join(".", @dots);
	$revision .= (@dots ? "." : "") . $file;

	if ($verify) {
	    print "Need install update $fullpath\n";
	    $verify_count++;
	    next;
	}

	if ($check) {
	    print "Syntax checking update $fullpath\n";
	}
	elsif ($impotent) {
	    print "*** Processing (impotent mode) update $fullpath\n";
	}
	else {
	    print "*** Processing update $fullpath\n";
	}

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

	# This just loads the file.
	my $return = do $file;
	if (!defined($return)) {
	    Fatal("  could not parse $fullpath: $@") if $@;
	    Fatal("  could not do $fullpath: $!")    if $!;
	}
	next
	    if ($check);
	
	SET_TESTBED_VERSION($revision);
    
	# Then we run it.
	if (InstallUpdate($revision, $phase, $impotent) != 0) {
	    Fatal("  returned non-zero; aborting.\n");
	}
	# Mark that we have done it. 
	DBQueryFatal("update version_info set value='$revision' ".
		     "where name='install'")
	    if (!$single && !$force && !$impotent && $phase eq "post");
    }
}

if ($verify) {
    RunUpdates("pre");
    
    exit($verify_count)
	if (!$verify_count || $quiet);

    print "*** You have install 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);
}
elsif ($check) {
    print "Syntax checking updates ...\n";
    RunUpdates("pre");
    exit(0);
}
elsif ($impotent) {
    print "Running (impotent mode) pre-install updates  ...\n";
328
    print "More detailed info saved to $logfile\n";
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    RunUpdates("pre");
    print "Running (impotent mode) post-install updates  ...\n";
    RunUpdates("post");
    exit(0);
}

#
# When no phase is specifed, run both phases.
#
if (!defined($phase)) {
    print "Running pre-install updates ...\n";
    RunUpdates("pre");
    print "Running post-install updates ...\n";
    RunUpdates("post");
    exit(0);
}
RunUpdates($phase);
exit(0);

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

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