sliveraction.in 4.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#!/usr/bin/perl -w
#
# Copyright (c) 2008-2014 University of Utah and the Flux Group.
# 
# {{{GENIPUBLIC-LICENSE
# 
# GENI Public License
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
# 
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;

#
# Cleanup a dead slice on the machine running a CM.
# 
sub usage()
{
39
    print "Usage: sliveraction [-f] start|stop|restart|reload <urn>\n";
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    exit(1);
}
sub fatal($);
my $optlist   = "f";
my $force     = 0;
my $locked    = 0;
my $childpid  = 0;

#
# Configure variables
#
my $TB		  = "@prefix@";
my $TBOPS         = "@TBOPSEMAIL@";
my $TBLOGS        = "@TBLOGSEMAIL@";

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

# Load the Testbed support stuff.
use lib "@prefix@/lib";
use GeniDB;
use GeniSlice;
use GeniUtil;
use GeniCM;
use GeniCertificate;
use GeniCredential;
use GeniAggregate;

#
# We don't want to run this script unless its the real version.
#
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be root! Maybe its a development version?\n");
}

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"f"})) {
    $force = 1;
}
usage()
    if (@ARGV != 2);
my $action = $ARGV[0];
my $token =  $ARGV[1];

# Connect to the proper DB.
DBConnect(GENICM_DBNAME());

#
# Need an RPC context for this to work.
#
my $certificate = GeniCertificate->LoadFromFile("$TB/etc/genicm.pem");
fatal("Could not load CM certificate")
    if (!defined($certificate));
Genixmlrpc->SetContext(Genixmlrpc->Context($certificate));
$ENV{'MYUUID'} = $certificate->uuid();
$ENV{'MYURN'} = "urn:publicid:IDN+@OURDOMAIN@+authority+cm";

my $slice = GeniSlice->Lookup($token);
if (!defined($token)) {
    fatal("No such slice for $token");
}
if ($slice->Lock()) {
    fatal("Could not lock slice $slice")
	if (!$force);
}
else {
    $locked = 1;
}
my $aggregate = GeniAggregate->SliceAggregate($slice);
if (!defined($aggregate)) {
    fatal("No local aggregate for $slice");
}
GeniUtil::FlipToGeniUser();
if ($action eq "start") {
128
    $aggregate->Start(2) == 0
129 130 131 132 133 134 135
	or fatal("Could not start $aggregate");
}
elsif ($action eq "stop") {
    $aggregate->Stop(2) == 0
	or fatal("Could not stop $aggregate");
}
elsif ($action eq "restart") {
136 137 138 139 140
    $aggregate->Restart(2) == 0
	or fatal("Could not restart $aggregate");
}
elsif ($action eq "reload") {
    $aggregate->Reload(2) == 0
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	or fatal("Could not restart $aggregate");
}
#
# See if we forked, If so, we wait.
#
if ($childpid) {
    waitpid($childpid, 0);
    if ($?) {
	fatal("Failed to $action $slice");
    }
}
$slice->UnLock()
    if ($locked);
exit(0);

sub WrapperFork()
{
    # Need this in the child.
    my $parent_lockname = "lock_$PID";

    # Before the fork, lock the lock in the parent. It will be
    # unlocked just before the parent exits, this informing the
    # child.
    emdb::DBQueryWarn("select GET_LOCK('$parent_lockname', 5)");
    
    $childpid = fork();
    if ($childpid) {
	# Tell caller we are the parent,
	return $childpid;
    }
    # XXX Need to let the parent finish up first, but the parent is
    # is in the "defunct" state, so cannot use kill(0) to figure out
    # it is gone.
    emdb::DBQueryWarn("select GET_LOCK('$parent_lockname', 15)");
    return 0;
}

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

    print STDERR "$msg\n";
    $slice->UnLock()
	if ($locked);
    exit(1);
}