Commit fd87aaa1 authored by Leigh B Stoller's avatar Leigh B Stoller

Remove direct DB insertions, use the library. Add cable/length

arguments. Add -i option to dump out interface/wire info for existing
management interface.
parent beb947af
#!/usr/bin/perl -w
#
# Copyright (c) 2003-2016 University of Utah and the Flux Group.
# Copyright (c) 2003-2017 University of Utah and the Flux Group.
#
# {{{EMULAB-LICENSE
#
......@@ -37,29 +37,37 @@ use File::Temp qw(tempfile);
sub usage()
{
print STDERR "Usage: management_iface -t <type> -a [key|pswd] ";
print STDERR " [-s <switchinfo>] <node_id> mac IP arg1 arg2\n";
print STDERR "[-s <switchinfo>] <node_id> mac IP key_uid key\n";
print STDERR " management_iface -r <node_id>\n";
print STDERR " management_iface -i <node_id>\n";
print STDERR " -h This message\n";
print STDERR " -t type Management type; ilo, ilo2, ilo3, drac, ipmi15, ipmi20\n";
print STDERR " -s info Optional switch info; switch,card,port\n";
print STDERR " -s - Search output of switchmac to find switch info\n";
print STDERR " -a pswd Password auth; provide login and password.\n";
print STDERR " -a key SSH key auth; provide login and key path.\n";
print STDERR " -c cable Cable ID\n";
print STDERR " -l len Cable length\n";
print STDERR " -r Remove management interface from DB.\n";
print STDERR " -i Display management interface from DB.\n";
print STDERR " -n Do not restart daemons after changing DB.\n";
exit(-1);
}
my $optlist = "ht:a:rs:n";
my $optlist = "ht:a:rs:nic:l:";
my $debug = 0;
my $remove = 0;
my $info = 0;
my $norestart = 0;
my $authtype;
my $type;
my $cable;
my $cablelen;
my $switchinfo;
# Protos
sub fatal($);
sub RemoveManagementInterface($);
sub ShowManagementInterface($);
#
# Configure variables
......@@ -104,11 +112,22 @@ if (defined($options{'h'})) {
if (defined($options{'d'})) {
$debug = 1;
}
if (defined($options{'c'})) {
$cable = $options{'c'};
}
if (defined($options{'l'})) {
$cablelen = $options{'l'};
}
if (defined($options{'r'})) {
$remove = 1;
usage()
if (@ARGV != 1);
}
if (defined($options{'i'})) {
$info = 1;
usage()
if (@ARGV != 1);
}
if (defined($options{'n'})) {
$norestart = 1;
}
......@@ -152,6 +171,9 @@ if (!defined($node)) {
if ($remove) {
exit(RemoveManagementInterface($node));
}
elsif ($info) {
exit(ShowManagementInterface($node));
}
#
# Else adding new one.
......@@ -170,6 +192,7 @@ if ($mac =~ /(\w\w):(\w\w):(\w\w):(\w\w):(\w\w):(\w\w)/) {
my $switchid;
my $switchcard;
my $switchport;
my $switch;
if (defined($switchinfo)) {
if ($switchinfo eq "-") {
......@@ -210,6 +233,10 @@ if (defined($switchinfo)) {
else {
fatal("Invalid switch info");
}
$switch = Node->Lookup($switchid);
if (!defined($switch)) {
fatal("Could not lookup switch in the database");
}
}
my $isipmi = ($type =~ /^ipmi/ ? 1 : 0);
......@@ -252,19 +279,42 @@ if (defined($interface)) {
else {
my $ifaceargs = {
"card" => 99, # XXX Bogus.
"port" => 1,
"iface" => $type,
"role" => TBDB_IFACEROLE_MANAGEMENT(),
"MAC" => $mac,
"IP" => $IP,
"type" => $type,
};
$interface = Interface->Create($node, $ifaceargs);
if (!defined($interface)) {
fatal("Could not create interface entry");
}
if (defined($switchinfo)) {
$ifaceargs->{'switch_id'} = $switchid;
$ifaceargs->{'switch_port'} = $switchport;
$ifaceargs->{'switch_card'} = $switchcard;
my $switchiface = $switchcard . "/" . $switchport;
$ifaceargs = {
"card" => $switchcard,
"port" => $switchport,
"iface" => $switchiface,
"role" => TBDB_IFACEROLE_OTHER(),
"type" => "generic",
"trunk" => 0,
"mac" => "000000000000",
};
my $switch_interface = Interface->Create($switch, $ifaceargs);
if (!defined($interface)) {
fatal("Could not create switch interface entry");
}
my $wireargs = {
};
$wireargs->{"cable"} = $cable if (defined($cable));
$wireargs->{"length"} = $cablelen if (defined($cablelen));
Interface::Wire->Create($interface, $switch_interface,
'Management', $wireargs)
or fatal("Could not create wire!");
}
Interface->Create($node, $ifaceargs)
or fatal("Could not create interface entry");
}
#
......@@ -293,12 +343,24 @@ sub RemoveManagementInterface($)
fatal("No management interface")
if (!defined($interface));
#
# Delete wire and switch side interface.
#
my $wire = Interface::Wire->Lookup($interface);
if ($wire) {
my $switch_interface = Interface->LookupByIface($wire->node_id2(),
$wire->iface2());
if (defined($switch_interface)) {
$switch_interface->Delete(1) == 0
or fatal("Could not lookup switch side of wire in the database: ".
$wire->node_id2() . ":" . $wire->iface2());
}
$wire->Delete(1) == 0
or fatal("Could not delete wire");
}
$node->DeleteOutlet() == 0
or fatal("Could not delete outlets");
$interface->DeleteWire() == 0
or fatal("Could not delete wire for $interface");
# Flag indicates it is okay to delete real interface.
$interface->Delete(1) == 0
or fatal("Could not delete $interface");
......@@ -315,6 +377,63 @@ sub RemoveManagementInterface($)
}
}
#
# Show management interface.
#
sub ShowManagementInterface($)
{
my ($node) = @_;
my $node_id = $node->node_id();
my $interface = Interface->LookupManagement($node);
fatal("No management interface")
if (!defined($interface));
print "Iface1:\n";
$interface->Dump();
print "\n";
my $wire = Interface::Wire->Lookup($interface);
if ($wire) {
my $switch_interface = Interface->LookupByIface($wire->node_id2(),
$wire->iface2());
if (defined($switch_interface)) {
print "Iface2:\n";
$switch_interface->Dump();
print "\n";
}
print "Wire:\n";
$wire->Dump();
}
my $query_result =
DBQueryFatal("select * from outlets where node_id='$node_id'");
if ($query_result->numrows) {
my $row = $query_result->fetchrow_hashref();
print "\n";
print "Outlet:\n";
print "Power ID: " . $row->{'power_id'} . "\n";
print "Outlet: " . $row->{'outlet'} . "\n";
print "Last Power: " . $row->{'last_power'} . "\n";
$query_result =
DBQueryFatal("select * from outlets_remoteauth ".
"where node_id='$node_id'");
if ($query_result->numrows) {
$row = $query_result->fetchrow_hashref();
print "Key Type: " . $row->{'key_type'} . "\n";
print "Key Role: " . $row->{'key_role'} . "\n";
print "Key UID: " . $row->{'key_uid'} . "\n";
if ($row->{'key_privlvl'}) {
print "Key Level: " . $row->{'key_privlvl'} . "\n";
}
if ($row->{'mykey'} ne "") {
print "Key Key: " . $row->{'mykey'} . "\n";
}
}
}
return 0;
}
sub fatal($)
{
my ($mesg) = $_[0];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment