vtopgen.in 3.85 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2009-2013 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;

sub usage ()
{
29
    print STDERR "Usage: $0 [-v] [-u [-f]] [-x] [-o filename] pid eid\n";
30
    print STDERR " -p   - pre-assign mode, as from tbprerun\n";
31 32
    print STDERR " -v   - Enables verbose output\n";
    print STDERR " -u   - Enables update mode\n";
33
    print STDERR " -o   - Send output to file instead of stdout\n";
34 35 36
    print STDERR " -f   - Fix current resources during update mode\n";
    exit(1);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
37
my $optlist  = "vufxo:pr";
38
my $verbose  = 0;
39 40 41
my $fixmode  = 0;
my $updating = 0;
my $warnings = 0;
42
my $preassign= 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
43
my $rspec    = 0;
44 45 46
my $output   = *STDOUT;
my $retval   = 0;
my $filename;
47 48 49 50 51 52 53 54 55 56 57 58 59

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

#
# Load the Testbed support stuff. 
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
60
use User;
61 62
use libtblog;
use libvtop;
63
use Experiment;
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

# Protos
sub fatal($);

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

#
# 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 (@ARGV < 2) {
    usage();
}
if (defined($options{"v"})) {
    $verbose++;
}
if (defined($options{"u"})) {
    $updating = 1;
}
if (defined($options{"f"})) {
    $fixmode = 1;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
95 96 97
if (defined($options{"r"})) {
    $rspec = 1;
}
98 99 100
if (defined($options{"p"})) {
    $preassign = 1;
}
101 102 103
if (defined($options{"o"})) {
    $filename = $options{"o"};
}
104 105 106 107 108 109
my $pid = $ARGV[0];
my $eid = $ARGV[1];
my $experiment = Experiment->Lookup($pid, $eid);
if (!defined($experiment)) {
    fatal("Could not lookup experiment object $pid,$eid!")
}
110

111 112 113 114 115
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    tbdie("You ($UID) do not exist!");
}

Mike Hibler's avatar
Mike Hibler committed
116
libvtop::Init($this_user, $experiment->GetGroup(), $experiment);
117

118 119 120 121 122 123 124
my $flags = 0;
$flags |= $libvtop::VTOP_FLAGS_VERBOSE
    if ($verbose);
$flags |= $libvtop::VTOP_FLAGS_UPDATE
    if ($updating);
$flags |= $libvtop::VTOP_FLAGS_FIXNODES
    if ($fixmode);
125 126
$flags |= $libvtop::VTOP_FLAGS_PREASSIGN
    if ($preassign);
127 128 129 130 131 132 133 134

# Open up output file now.
if (defined($filename)) {
    open(OUT, ">$filename")
	or fatal("Could not open output file: $!");
    $output = *OUT;
}

135
my $vtop = libvtop->Create($experiment, $this_user, $flags);
136
if (!defined($vtop)) {
137
    fatal("Could not create vtop structure for $experiment");
138
}
139 140
$vtop->CreateVtop() == 0
    or fatal("Could not create vtop for $experiment");
141

142 143
if ($preassign) {
    if ($experiment->Update({"maximum_nodes" => $vtop->maximum_nodes(),
144 145
			     "minimum_nodes" => $vtop->minimum_nodes(),
			     "virtnode_count"=> $vtop->virtnodecount() })) {
146 147 148 149 150 151
	fatal("Could not update min/max nodes for $experiment");
	return -1;
    }
    exit(0);
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
152 153 154
if ($rspec && defined($vtop->rspec())) {
    $retval = $vtop->PrintRspec($output);
}
155 156 157 158 159 160
else {
    $retval = $vtop->PrintTop($output);
}
close($output)
    if (defined($filename));
exit($retval);
161 162 163 164 165 166 167 168

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

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