template_delete.in 5.55 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2006, 2007 University of Utah and the Flux Group.
5 6 7
# All rights reserved.
#
use English;
8
use strict;
9
use Getopt::Std;
10
use POSIX qw(setsid);
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
use POSIX qw(strftime);

#
# Delete an experiment template (and optionally its children).
#
# 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
28
	  "Usage: template_delete [-q] [-r] <guid/vers>\n".
29 30 31 32 33 34
	  "switches and arguments:\n".
	  "-q          - be less chatty\n".
	  "-r          - Delete recursively\n".
	  "<guid/vers> - GUID and version to swapin\n");
    exit(-1);
}
35
my $optlist	 = "qdr";
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
my $quiet        = 0;
my $debug        = 0;
my $recursive    = 0;

#
# Configure variables
#
my $TB		= "@prefix@";
my $EVENTSYS	= @EVENTSYS@;
my $TBOPS	= "@TBOPSEMAIL@";
my $TBLOGS	= "@TBLOGSEMAIL@";
my $TBDOCBASE	= "@TBDOCBASE@";
my $TBBASE	= "@TBBASE@";
my $CONTROL	= "@USERNODE@";

# Programs we need
my $endexp      = "$TB/bin/endexp";
my $makegraph   = "$TB/bin/template_graph";

# Locals
my $guid;
my $version;
my $dbuid;
my $logname;
my $user_name;
my $user_email;
62 63 64
my $template;
my @templates   = ();
my %instances   = ();	# Indexed by vers.
65 66 67 68 69 70 71 72 73 74 75 76 77

# Protos
sub ParseArgs();
sub fatal($$);
sub sighandler($);

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use libtblog;
78
use Template;
79
use libaudit;
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

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

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

#
# Set umask for start/swap. We want other members in the project to be
# able to swap/end experiments, so the log and intermediate files need
# to be 664 since some are opened for append.
#
umask(0002);

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

#
# Verify user and get his DB uid.
#
if (! UNIX2DBUID($UID, \$dbuid)) {
    tbdie("You do not exist in the Emulab Database!");
}

#
# Get email info for user.
#
if (! UserDBInfo($dbuid, \$user_name, \$user_email)) {
    tbdie("Cannot determine your name and email address.");
}


# Now parse arguments.
ParseArgs();

#
# Grab template info.
#
124 125 126 127 128 129 130 131 132 133
$template = Template->Lookup($guid, $version);

if (!defined($template)) {
    tbdie("Experiment template $guid/$version does not exist!");
}
if (! TBProjAccessCheck($dbuid,
			$template->pid(), $template->gid(),
			TB_PROJECT_CREATEEXPT)) {
    tberror("You do not have permission to delete template $guid/$version");
    exit(1);
134 135 136
}

#
137
# Ask for the children. If not in recursive mode, there better not be any!
138
#
139
my @children;
140

141 142
$template->Children(\@children) == 0
    or tbdie("Could not construct list of child templates for $guid/$version");
143

144
if ($recursive) {
145
    @templates = (@children, $template);
146 147
}
else {
148 149 150
    tbdie("Cannot delete template $template cause it has children @children")
	if (@children);

151
    # Just one.
152
    @templates = ($template);
153 154
}

155 156 157 158
if ($debug) {
    print STDERR "Looking at @templates\n"
	if ($debug);
}
159 160

#
161
# Now get the instance lists for each template and see if any are active.
162 163 164
#
my $active = 0;

165 166
foreach my $template (@templates) {
    my @instance_list;
167

168 169
    $template->InstanceList(1, \@instance_list) == 0
	or tbdie("Could not get instance list for $template!");
170

171 172 173 174 175 176 177 178 179 180 181 182 183
    print STDERR "Instances for $template: @instance_list\n"
	if ($debug);

    # Check each instance to make sure its not a current instance.
    foreach my $instance (@instance_list) {
	my $current = $instance->Instantiated();
	exit(-1)
	    if ($current < 0);
    
	if ($current) {
	    tberror("Instance $instance is still instantiated!");
	    $active++;
	}
184
    }
185
    $instances{$template->vers()} = [ @instance_list ];
186 187 188 189
}
exit(1)
    if ($active);

190 191 192
# Use the logonly option to audit so that we get a record mailed.
LogStart(0);

193
#
194
# Okay, do the deletions.
195
#
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
foreach my $template (@templates) {
    my @instance_list = @{ $instances{$template->vers()} };

    print STDERR "Deleting instances: @instance_list\n"
	if ($debug && @instance_list);

    foreach my $instance (@instance_list) {
	print "Deleting template instance $instance ... \n";

	$instance->Delete() == 0
	    or tbdie("Could not delete instance $instance");
    }
    my $pid = $template->pid();
    my $eid = $template->eid();

    # This is the hidden experiment under the template.
    if (ExpState($pid, $eid)) {
213
	system("$endexp -x -q -w $pid,$eid");
214 215
	exit(-1)
	    if ($?);
216 217
    }

218
    # And finally the template record.
219 220
    print "Deleting template $template ...\n";
    
221 222
    $template->Delete() == 0
	or tbdie("Could not delete template $template");
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
}

#
# Recreate the graph.
#
system("$makegraph $guid");

exit(0);

#
# Parse command arguments. Once we return from getopts, all that are
# left are the required arguments.
#
sub ParseArgs()
{
238 239
    my %options;
    
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
    if (! getopts($optlist, \%options)) {
	usage();
    }

    if (@ARGV != 1) {
	usage();
    }
    #
    # Pick up guid/version first and untaint.
    #
    my $tmp = shift(@ARGV);

    if ($tmp =~ /^([\w]*)\/([\d]*)$/) {
	$guid = $1;
	$version = $2;
    }
    else {
	tbdie("Bad data in argument: $tmp");
    }

    if (defined($options{"r"})) {
	$recursive = 1;
    }
    if (defined($options{"q"})) {
	$quiet = 1;
    }
    if (defined($options{"d"})) {
	$debug = 1;
    }
}