opsreboot.in 3.1 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2004, 2005 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
#
use English;
use Getopt::Std;
use Fcntl ':flock';

#
29
# Bootup script for ops; invoked from stated via a trigger in the DB.
30 31 32 33 34 35
#
sub usage()
{
    print(STDERR "Usage: opsreboot [-d]\n");
    exit(1);
}
36
sub fatal($);
37 38 39 40 41 42 43 44
my $optlist = "d";
my $debug   = 0;

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
45
my $CVSSUPPORT  = @CVSSUPPORT@;
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

# 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 ...");

119 120 121 122
#
# restart the event servers on ops.
#
system("$TB/sbin/eventsys_start");
123

124 125 126 127
if ($CVSSUPPORT) {
    system("$TB/sbin/cvsrepo_ctrl -b");
}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
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);
}