opsreboot.in 2.41 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2004, 2005 University of Utah and the Flux Group.
5 6 7 8 9 10 11
# All rights reserved.
#
use English;
use Getopt::Std;
use Fcntl ':flock';

#
12
# Bootup script for ops; invoked from stated via a trigger in the DB.
13 14 15 16 17 18
#
sub usage()
{
    print(STDERR "Usage: opsreboot [-d]\n");
    exit(1);
}
19
sub fatal($);
20 21 22 23 24 25 26 27
my $optlist = "d";
my $debug   = 0;

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
28
my $CVSSUPPORT  = @CVSSUPPORT@;
29 30 31 32 33 34 35 36 37 38 39 40 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

# Locals
my $lockfile    = "/var/tmp/testbed_opsreboot_lockfile";
my $logfile;

#
# Only root can run this script.
#
if ($UID || $EUID) {
    die("*** $0:\n".
	"    Only root can run this script\n");
}

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

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

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
if (@ARGV) {
    usage();
}
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}

#
# We only want to run this once; if another one is running, then let it
# do whatever needs to be done; just exit. 
#
if (!$TESTMODE) {
    open(LOCK, ">>$lockfile") || fatal("Couldn't open $lockfile\n");

    if (flock(LOCK, LOCK_EX|LOCK_NB) == 0) {
	fatal("Another opsreboot script is running!");
    }
}

if (!$debug) {
    $logfile = TBMakeLogname("opsreboot");

    if (my $childpid = TBBackGround($logfile)) {
	#
	# Parent exits normally
	#
	exit(0);
    }
}

SENDMAIL($TBOPS, "OPS has rebooted",
	 "OPS has rebooted.\n".
	 "Starting up services. Stay tuned for more info ...");

102 103 104 105
#
# restart the event servers on ops.
#
system("$TB/sbin/eventsys_start");
106

107 108 109 110
if ($CVSSUPPORT) {
    system("$TB/sbin/cvsrepo_ctrl -b");
}

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
SENDMAIL($TBOPS, "OPS has rebooted",
	 "OPS has rebooted. Services have been restarted.",
	 undef, undef, (defined($logfile) ? ($logfile) : ()));


if (!$TESTMODE) {
    #
    # Close the lock file. Exiting releases it, but might as well.
    #
    close(LOCK);
}

unlink($logfile);
exit(0);

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

    SENDMAIL($TBOPS, "OPS reboot script Failed", $msg, undef, undef
	     (defined($logfile) ? ($logfile) : ()));
    unlink($logfile)
	if (defined($logfile) && -e $logfile);
    die($msg);
}