template_control.in 6.81 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2006, 2007 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 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
#
use English;
use strict;
use Getopt::Std;

#
# Do various things to templates.
#
# Exit codes are important; they tell the web page what has happened so
# it can say something useful to the user. Fatal errors are mostly done
# with die(), but expected errors use this routine. At some point we will
# use the DB to communicate the actual error.
#
# $status < 0 - Fatal error. Something went wrong we did not expect.
# $status = 0 - Everything okay.
# $status > 0 - Expected error. User not allowed for some reason. 
# 
sub usage()
{
    print(STDERR
	  "Usage: template_control -a <action> <guid/vers> [args ...]\n".
	  "switches and arguments:\n".
	  "-a <action>    - add, delete or modify\n".
	  "<guid/vers>    - GUID and version of template\n");
    exit(-1);
}
my $optlist	 = "a:";
my %options      = ();
my $action;
my $template_guid;
my $template_vers;

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

# Locals
my $template;
my $instance;
# For the END block below.
my $cleaning    = 0;
my $justexit    = 1;

# Protos
sub ParseArgs();
sub HideTemplate();
sub ShowTemplate();
sub ShowHidden();
sub HideHidden();
Leigh Stoller's avatar
Leigh Stoller committed
74 75
sub Activate();
sub InActivate();
76
sub AddEvent();
77 78 79 80 81 82 83 84 85

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use libtblog;
use Template;
86
use User;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

# Be careful not to exit on transient error
$libdb::DBQUERY_MAXTRIES = 0;

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

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

#
104
# Verify user and get his DB uid and other info for later.
105
#
106 107 108
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    tbdie("You ($UID) do not exist!");
109
}
110
my $user_uid   = $this_user->uid();
111 112 113 114 115 116 117 118 119 120 121 122

# Now parse arguments.
ParseArgs();

#
# Grab template and do access check.
#
$template = Template->Lookup($template_guid, $template_vers);

if (!defined($template)) {
    tbdie("Experiment template $template_guid/$template_vers does not exist!");
}
123 124
if (! $template->AccessCheck($this_user, TB_EXPT_MODIFY)) {
    tberror("You do not have permission to modify $template");
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    exit(1);
}

#
# Do the action.
#
if ($action eq "hide") {
    exit(HideTemplate());
}
elsif ($action eq "show") {
    exit(ShowTemplate());
}
elsif ($action eq "showhidden") {
    exit(ShowHidden());
}
elsif ($action eq "hidehidden") {
    exit(HideHidden());
}
Leigh Stoller's avatar
Leigh Stoller committed
143 144 145 146 147 148
elsif ($action eq "activate") {
    exit(Activate());
}
elsif ($action eq "inactivate") {
    exit(InActivate());
}
149 150 151
elsif ($action eq "addevent") {
    exit(AddEvent());
}
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
usage();

sub HideTemplate()
{
    my $optlist   = "r";
    my $recursive = 0;
    my %options;
    
    if (! getopts($optlist, \%options)) {
	usage();
    }
    if (defined($options{"r"})) {
	$recursive = 1;
    }
    $template->Hide($recursive) == 0
	or return -1;
    
    return 0;
}

sub ShowTemplate()
{
    my $optlist   = "r";
    my $recursive = 0;
    my %options;
    
    if (! getopts($optlist, \%options)) {
	usage();
    }
    if (defined($options{"r"})) {
	$recursive = 1;
    }
    $template->Show($recursive) == 0
	or return -1;
    
    return 0;
}

sub ShowHidden()
{
    if (! $template->IsRoot()) {
	print STDERR "Must apply to the root template!\n";
	return -1;
    }
    # We overload the hidden bit on the root template
    $template->Show(0) == 0
	or return -1;
    
    return 0;
}
sub HideHidden()
{
    if (! $template->IsRoot()) {
	print STDERR "Must apply to the root template!\n";
	return -1;
    }
    # We overload the hidden bit on the root template
    $template->Hide(0) == 0
	or return -1;
   
    return 0;
}

Leigh Stoller's avatar
Leigh Stoller committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
sub Activate()
{
    $template->Activate() == 0
	or return -1;
    
    return 0;
}
sub InActivate()
{
    $template->InActivate() == 0
	or return -1;
   
    return 0;
}

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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
#
# Add a dynamic event.
#
sub AddEvent()
{
    my $timestamp;
    my $commandline;
    my $vnode;
    my $exptidx;
    
    # Parse the rest of the options.
    while (@ARGV) {
	my $arg = shift(@ARGV);

	if ($arg eq "-t") {
	    usage()
		if (@ARGV == 0);
	    
	    $timestamp = shift(@ARGV);

	    if ("$timestamp" eq "now") {
		$timestamp = time();
	    }
	    elsif ($timestamp =~ /^([\d]*)$/) {
		$timestamp = $1;
	    }
	    else {
		tbdie("Illegal characters in timestamp!");
	    }
	}
	elsif ($arg eq "-c") {
	    usage()
		if (@ARGV == 0);

	    $arg = shift(@ARGV);
	    
	    if (! TBcheck_dbslot($arg, 'eventlist','arguments',
			TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
		tbdie("Illegal characters in commandline!");
	    }
	    $commandline = $arg;
	}
	elsif ($arg eq "-n") {
	    usage()
		if (@ARGV == 0);

	    $arg = shift(@ARGV);
	    
	    if (! TBcheck_dbslot($arg, 'virt_nodes','vname',
			TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
		tbdie("Illegal characters in node name!");
	    }
	    $vnode = $arg;
	}
	elsif ($arg eq "-i") {
	    usage()
		if (@ARGV == 0);

	    $arg = shift(@ARGV);
	    
	    if (! TBcheck_dbslot($arg, "default", "int",
			TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
		tbdie("Illegal characters in experiment index!");
	    }
	    $exptidx = $arg;
	}
	else {
	    usage();
	}
    }
    usage()
	if (! (defined($timestamp) && defined($commandline) &&
	       defined($vnode) && defined($exptidx)));

    my $instance = $template->LookupInstance($exptidx);
    if (!defined($instance)) {
	tbdie("Could not get instance record for experiment $exptidx!");
    }

    $instance->AddEvent($timestamp, $vnode, $commandline) == 0
	or tbdie("Could not add dynamic event to $instance!");
    
    return 0;
}

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
#
# Parse command arguments. Once we return from getopts, all that are
# left are the required arguments.
#
sub ParseArgs()
{
    my %options;
    
    if (! getopts($optlist, \%options)) {
	usage();
    }

    if (@ARGV < 1) {
	usage();
    }

    #
    # Pick up guid/version and untaint.
    #
    my $tmp = shift(@ARGV);

    if ($tmp =~ /^([\w]*)\/([\d]*)$/) {
	$template_guid = $1;
	$template_vers = $2;
    }
    else {
	usage();
    }

    if (!defined($options{"a"})) {
	tbdie("Must supply -a option");
    }
    $action = $options{"a"};
}