Newer
Older
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2004, 2005 University of Utah and the Flux Group.
# All rights reserved.
#
# deletenode - a script for deleting a node from the database
#
# Configure variables
my $TB = "@prefix@";
my $ELABINELAB = @ELABINELAB@;
my $KILL = "/bin/kill";
use lib '@prefix@/lib';
use libdb;
use Getopt::Std;
# Turn off line buffering on output
$| = 1;
#
# A list of all the tables we have to delete this node from, and the name of
# the column(s) in that table that contains a node name
#
my %clean_tables = TBDB_PHYSICAL_NODE_TABLES;
if (!TBAdmin()) {
die "Sorry, only testbed administrators can run this script!\n";
}
#
# Handle command-line arguments
#
sub usage {
die "usage: deletenode [-b] [-f] [-q] <node>\n";
}
my %options = ();
if (!getopts("bfq",\%options)) {
usage();
}
my $node = shift @ARGV;
if (!$node || @ARGV) {
usage();
}
#
# Start off with some serious sanity checks
#
#
# First, make sure the node exists, and only let them delete experimental nodes
#
my $result = DBQueryFatal("select role from nodes where node_id='$node'");
if ($result->num_rows() != 1) {
die "Node $node does not exist!\n";
}
my ($role) = $result->fetch_row();
if ($role ne "testnode") {
die "Node $node is not a testnode!\n";
}
#
# Don't let 'em delete a node that is allocated, except to hwdown. Override
# with force option though (for ElabInElab).
#
my ($pid, $eid);
my $allocated = NodeidToExp($node,\$pid,\$eid);
if ($allocated && (($pid ne NODEDEAD_PID) || ($eid ne NODEDEAD_EID))) {
print "Node is not free or in the " .
NODEDEAD_PID . "/" . NODEDEAD_EID . "experiment!\n";
if (defined($options{"f"}) && $ELABINELAB) {
print "WARNING: Continuing anyway!\n";
}
else {
exit(-1);
}
}
#
# Make sure they know what they are getting themselves into
#
# Note: the -b (batch) option is intentionally undocumented, it should only be
# used from other scripts that have already asked for confirmation
#
unless ($options{b}) {
print "*** WARNING:\n";
print "This will erase all evidence that $node ever existed in the testbed!\n";
print "Are you SURE you want to continue? ";
my $answer = <>;
if ($answer !~ /^y/i) {
print "Okay, aborting...\n";
exit(1);
}
}
#
# Okay, let's clean out them tables
#
while (my ($table, $clist) = each %clean_tables) {
foreach my $column (@$clist) {
print "Removing from table $table, column $column\n"
if (! defined($options{"q"}));
DBQueryFatal("DELETE FROM $table WHERE $column='$node';");
}
}
#
# Need to run a bunch of stuff to really kill off the node.
#
print "Regenerating exports file and restarting daemon.\n"
if (! defined($options{"q"}));
if (system("$TB/sbin/exports_setup")) {
print STDERR "*** Failed to reset mountpoints.\n";
}
print "Regenerating named maps and restarting daemon.\n"
if (! defined($options{"q"}));
if (system("$TB/sbin/named_setup")) {
print STDERR "*** Failed to reset named maps.\n";
}
print "Regenerating DHCPD config file and restarting daemon.\n"
if (! defined($options{"q"}));
if (system("$TB/sbin/dhcpd_makeconf -i -r")) {
print STDERR "*** Failed to reset DHCPD config file.\n";
}
print "Hupping stated so that it will reload its tables\n"
if (! defined($options{"q"}));
if (system("sudo $KILL -HUP `cat /var/run/stated.pid`")) {
print STDERR "*** Failed to HUP stated.\n";
}