template_delete.in 6.06 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
#
use English;
25
use strict;
26
use Getopt::Std;
27
use POSIX qw(setsid);
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
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
45
	  "Usage: template_delete [-q] [-r] <guid/vers>\n".
46 47 48 49 50 51
	  "switches and arguments:\n".
	  "-q          - be less chatty\n".
	  "-r          - Delete recursively\n".
	  "<guid/vers> - GUID and version to swapin\n");
    exit(-1);
}
52
my $optlist	 = "qdr";
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
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 $logname;
76 77 78
my $template;
my @templates   = ();
my %instances   = ();	# Indexed by vers.
79 80 81 82 83 84 85 86 87 88 89 90 91

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

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use libtblog;
92
use Template;
93
use libaudit;
94
use User;
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

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

#
116
# Verify user and get his DB uid and other info for later.
117
#
118 119 120
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    tbdie("You ($UID) do not exist!");
121
}
122 123
my $user_name  = $this_user->name();
my $user_email = $this_user->email();
124 125 126 127 128 129 130

# Now parse arguments.
ParseArgs();

#
# Grab template info.
#
131 132 133 134 135
$template = Template->Lookup($guid, $version);

if (!defined($template)) {
    tbdie("Experiment template $guid/$version does not exist!");
}
136
if (! $template->AccessCheck($this_user, TB_EXPT_DESTROY())) {
137 138
    tberror("You do not have permission to delete template $guid/$version");
    exit(1);
139 140 141
}

#
142
# Ask for the children. If not in recursive mode, there better not be any!
143
#
144
my @children;
145

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

149
if ($recursive) {
150
    @templates = (@children, $template);
151 152
}
else {
153 154 155
    tbdie("Cannot delete template $template cause it has children @children")
	if (@children);

156
    # Just one.
157
    @templates = ($template);
158 159
}

160 161 162 163
if ($debug) {
    print STDERR "Looking at @templates\n"
	if ($debug);
}
164 165

#
166
# Now get the instance lists for each template and see if any are active.
167 168 169
#
my $active = 0;

170 171
foreach my $template (@templates) {
    my @instance_list;
172

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

176 177 178 179 180 181 182 183 184 185 186 187 188
    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++;
	}
189
    }
190
    $instances{$template->vers()} = [ @instance_list ];
191 192 193 194
}
exit(1)
    if ($active);

195 196 197
# Use the logonly option to audit so that we get a record mailed.
LogStart(0);

198
#
199
# Okay, do the deletions.
200
#
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
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)) {
218
	system("$endexp -x -q -w $pid,$eid");
219 220
	exit(-1)
	    if ($?);
221 222
    }

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

#
# 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()
{
243 244
    my %options;
    
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
    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;
    }
}