geni_control.in 7.49 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2009, 2010 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 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

#
# Exit status is important. Exit with -1 if an error, else the number
# of nodes that could not be allocated. Otherwise zero.
#
sub usage()
{
    print("Usage: geni_control [-d] [-p] <pid> <eid> <action> ...\n".
	  "		-p enables partial allocation mode\n".
	  "		-d enables debugging output\n");
    exit(-1);
}
my $optlist = "dp";
my $debug   = 0;
my $partial = 0;
my $exitval = 0;

#
# Configure variables
#
my $TB          = "@prefix@";

#
# Testbed Support libraries
#
use lib '@prefix@/lib';
use libdb;
use libtestbed;
use Experiment;
use Project;
use User;
use Node;
use libGeni;
use GeniResource;
use GeniHRN;
Leigh Stoller's avatar
Leigh Stoller committed
63
use GeniXML;
64 65 66 67 68 69 70 71

sub fatal($);
sub AllocNodes();
sub FreeNodes();
sub Register();
sub UnRegister();
sub ClearAll();
sub StartAll();
72
sub StopAll();
73 74
sub WaitAll();
sub PurgeAll();
75
sub RenewAll();
76 77 78
sub GetManifests();
sub GetStatus();
sub MapNodes();
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

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

# For perf analysis.
#TBDebugTimeStampsOn();

#
# 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{"d"})) {
    $debug = 1;
}
if (defined($options{"p"})) {
    $partial = 1;
}
if (@ARGV < 3) {
    usage();
}
my $pid    = shift;
my $eid	   = shift;
my $action = shift;

109
if ($action =~
Leigh Stoller's avatar
Leigh Stoller committed
110
    /^(alloc|free|clear|wait|purge|start|mapnodes|status|stop|register|unregister||manifests|renew)$/) {
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 140 141 142 143 144 145 146 147 148 149 150 151 152
    $action = $1;
}
else {
    usage();
}

#
# Locals
# 
my $error	  = 0;
my $noalloc	  = 0;
my @oldnodes	  = ();
my @nodes	  = ();

#
# Experiment must exist.
# 
my $experiment = Experiment->Lookup($pid, $eid);
if (! $experiment) {
    die("*** $0:\n".
	"    No such experiment $pid/$eid in the Emulab Database.\n");
}
my $exptidx = $experiment->idx();
my $project = $experiment->GetProject();
if (!defined($project)) {
    die("*** $0:\n".
	"    Could not get project for experiment $experiment!\n");
}
    
#
# User must have permission to modify the experiment.
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    die("*** $0:\n".
	"    You ($UID) do not exist in the Emulab DB!\n");
}
if (!$experiment->AccessCheck($this_user, TB_EXPT_MODIFY)) {
    die("*** $0:\n".
	"    You do not have permission to allocate nodes in $pid/$eid\n");
}

Leigh Stoller's avatar
Leigh Stoller committed
153 154 155 156 157 158 159 160 161 162
#
# Need an RPC context for this to work.
#
my $certificate = GeniCertificate->LoadFromFile("$TB/etc/genisa.pem");
fatal("Could not load SA certificate")
    if (!defined($certificate));
Genixmlrpc->SetContext(Genixmlrpc->Context($certificate));
$ENV{'MYUUID'} = $certificate->uuid();
$ENV{'MYURN'}  = $certificate->urn();
    
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
my $foo = "urn:publicid:IDN+emulab.net+authority+cm";
my $fee = "urn:publicid:IDN+emulab.net+node+pc172";

@ARGV = ($fee);

#
# Now dispatch operation.
#
SWITCH: for ($action) {
    /^register$/ && do {
	Register();
	last SWITCH;
    };
    /^unregister$/ && do {
	UnRegister();
	last SWITCH;
    };
    /^alloc$/ && do {
	AllocNodes();
	last SWITCH;
    };
    /^free$/ && do {
	FreeNodes();
	last SWITCH;
    };
    /^clear$/ && do {
	ClearAll();
	last SWITCH;
    };
    /^start$/ && do {
	StartAll();
	last SWITCH;
    };
196 197 198 199
    /^stop$/ && do {
	StopAll();
	last SWITCH;
    };
200 201 202 203 204 205 206 207
    /^wait$/ && do {
	WaitAll();
	last SWITCH;
    };
    /^purge$/ && do {
	PurgeAll();
	last SWITCH;
    };
208 209 210 211
    /^renew$/ && do {
	RenewAll();
	last SWITCH;
    };
212 213 214 215 216 217 218 219 220 221 222 223
    /^manifests$/ && do {
	GetManifests();
	last SWITCH;
    };
    /^status$/ && do {
	GetStatus();
	last SWITCH;
    };
    /^mapnodes$/ && do {
	MapNodes();
	last SWITCH;
    };
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
    fatal("Unknown action $action");
}
exit($exitval);

#
# Allocate
#
sub AllocNodes()
{
    my $node_urn = $ARGV[0];

    my ($auth,$type,$node_id) = GeniHRN::Parse($node_urn);
    my $cm = GeniHRN::Generate($auth, "authority", "cm");
    
    my $resource = GeniResource->Lookup($experiment->idx(), $cm);
    if (!defined($resource)) {
	$resource = GeniResource->Create($experiment, $cm);
	if (!defined($resource)) {
	    fatal("Cannot create new geni resource object for $cm");
	}
    }
    print "$resource\n";

    my $rspec =
	"<rspec xmlns=\"http://protogeni.net/resources/rspec/0.1\"> ".
	" <node virtual_id=\"geni1\" ".
	"       virtualization_type=\"emulab-vnode\" ".
	"       > ".
	" </node>" .
	"</rspec>";

    $resource->ModifyResources($this_user, $rspec) == 0
	or fatal("Could not add new resources to $resource");

    return 0;
}

#
# Dealloc
#
sub FreeNodes()
{
}

sub ClearAll()
{
    my @resources = GeniResource->LookupAll($experiment);

    foreach my $resource (@resources) {
	$resource->Clear($this_user) == 0
	    or fatal("Could not clear resources from $resource");
    }

    return 0;
}

sub PurgeAll()
{
    my @resources = GeniResource->LookupAll($experiment);

    foreach my $resource (@resources) {
	$resource->Purge($this_user) == 0
	    or fatal("Could not purge resources from $resource");
287 288
	$resource->Delete() == 0
	    or fatal("Could not delete $resource");
289
    }
290
    UnRegister();
291 292 293 294 295
    return 0;
}

sub StartAll()
{
Leigh Stoller's avatar
Leigh Stoller committed
296
    libGeni::StartSlivers($experiment, $this_user, $debug) == 0 or
297 298 299
	fatal("Cannot start slivers!\n");
}

300 301 302 303 304 305 306 307 308 309 310
sub StopAll()
{
    my @resources = GeniResource->LookupAll($experiment);

    foreach my $resource (@resources) {
	$resource->StopSliver($this_user) == 0
	    or fatal("Could not stop $resource");
    }
    return 0;
}

311 312
sub WaitAll()
{
313
    libGeni::WaitForSlivers($experiment, $this_user, $debug) == 0 or
314 315 316
	fatal("Cannot wait on slivers!\n");
}

317 318
sub RenewAll()
{
319
    libGeni::RenewSlivers($experiment, $debug) == 0 or
320 321 322
	fatal("Cannot renew resources");
}

323 324 325 326 327 328
sub MapNodes()
{
    libGeni::MapNodes($experiment, $debug) == 0 or
	fatal("Cannot Mapnodes");
}

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
sub Register()
{
    libGeni::Register($experiment, $this_user) == 0 or
	fatal("Cannot register with Protgeni!\n");

    return 0;
}

#
# Unregister.
#
sub UnRegister()
{
    libGeni::UnRegister($experiment) == 0
	or fatal("Cannot unregister with Protgeni!");
      
   return 0;
}

348 349 350 351 352 353
sub GetManifests()
{
    my @resources = GeniResource->LookupAll($experiment);

    foreach my $resource (@resources) {
	$resource->GetManifest($this_user) == 0
Leigh Stoller's avatar
Leigh Stoller committed
354
	    or fatal("Could not get manifest for $resource");
355 356

	my $manifest = $resource->Manifest();
Leigh Stoller's avatar
Leigh Stoller committed
357 358 359 360
	if (!defined($manifest)) {
	    print STDERR "No manifest for $resource\n";
	    next;
	}
Leigh Stoller's avatar
Leigh Stoller committed
361 362
	
	print STDERR Dumper(GeniXML::Serialize($manifest, 1));
363 364 365 366 367 368 369 370 371 372 373 374 375
    }

    return 0;
}

sub GetStatus()
{
    my @resources = GeniResource->LookupAll($experiment);

    foreach my $resource (@resources) {
	my $ref;
	
	$resource->SliverStatus($this_user, \$ref) == 0
Leigh Stoller's avatar
Leigh Stoller committed
376
	    or fatal("Could not get status for $resource");
377 378 379 380 381 382 383

	print STDERR Dumper($ref);
    }

    return 0;
}

384 385 386 387 388 389 390
sub fatal($)
{
    my ($msg) = @_;

    print STDERR "*** $0:\n$msg\n";
    exit(-1);
}