addwire.in 8.14 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2003-2018 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 23 24 25 26 27 28 29 30 31 32 33 34
# 
# {{{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/>.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use Data::Dumper;

#
# Add a wire to the DB.
#
sub usage()
{
    print STDERR "Usage: addwire [-t type] [-c id [-l len]] ".
35
      "node1:iface1 portinfo1 node2:iface2 portinfo2\n";
36
    print STDERR "Usage: addwire -r node1:iface1 node2:iface2\n";
37
    print STDERR "Usage: addwire -i node1:iface1 node2:iface2\n";
38 39 40 41
    print STDERR "Options:\n";
    print STDERR " -v        - Turn on debugging\n";
    print STDERR " -n        - Impotent mode\n";
    print STDERR " -r        - Delete wire\n";
42
    print STDERR " -i        - Show info about wire from DB\n";
43
    print STDERR " -f        - (with -r) force deletion, even if wire is in use\n";
44 45 46
    print STDERR " -c cable  - Cable ID\n";
    print STDERR " -l len    - Cable length\n";
    print STDERR " -t type   - Wire type (defaults to Unused)\n";
47
    print STDERR " portinfo  - card,port (for now, might change later)\n";
48 49 50
	
    exit(-1);
}
51
my $optlist   = "t:nvfrc:l:i";
52 53 54
my $verbose   = 0;
my $impotent  = 0;
my $remove    = 0;
55
my $info      = 0;
56
my $force     = 0;
57
my $type      = "Unused";
58 59
my $cable;
my $cablelen;
60
my ($node_id1,$iface_id1,$card1,$port1,$node_id2,$iface_id2,$card2,$port2);
61 62 63

# Protos
sub fatal($);
64
sub RemoveWire($$);
65
sub ShowWire($$);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 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

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

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use emdb;
use EmulabConstants;
use emutil;
use User;
use Node;
use NodeType;
use Interface;
use Lan;

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

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:";

#
# Parse command arguments. 
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'h'})) {
    usage();
}
if (defined($options{'v'})) {
    $verbose = 1;
}
if (defined($options{'n'})) {
    $impotent = 1;
}
if (defined($options{'r'})) {
    $remove = 1;
    usage()
	if (@ARGV != 2);
}
116 117 118 119 120
if (defined($options{'i'})) {
    $info = 1;
    usage()
	if (@ARGV != 2);
}
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
if (defined($options{'f'})) {
    $force = 1;
}
if (defined($options{'t'})) {
    $type = $options{'t'};
    if ($type !~ /^(Trunk|Node|Control|Power|Serial|Unused|Management)$/) {
	fatal("Type must be one of: ".
	      "Trunk|Node|Control|Power|Serial|Unused|Management");
    }
}
if (defined($options{'c'})) {
    $cable = $options{'c'};
}
if (defined($options{'l'})) {
    $cablelen = $options{'l'};
}


#
# Verify user, must be admin.
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}
if (!$this_user->IsAdmin()) {
    fatal("You are not a testbed administrator!");
}
149 150 151
if ($remove) {
    exit(RemoveWire($ARGV[0],$ARGV[1]));
}
152 153 154
elsif ($info) {
    exit(ShowWire($ARGV[0],$ARGV[1]));
}
155
usage()
156
    if (@ARGV != 4);
157 158 159 160 161 162 163 164

if ($ARGV[0] =~ /^([-\w]+):(.+)$/) {
    $node_id1  = $1;
    $iface_id1 = $2;
}
else {
    fatal("Invalid argument for interface: " . $ARGV[0]);
}
165 166 167 168 169 170 171 172
if ($ARGV[1] =~ /^(\d+),(\d+)$/) {
    $card1 = $1;
    $port1 = $2;
}
else {
    fatal("Invalid argument for portinfo: " . $ARGV[1]);
}
if ($ARGV[2] =~ /^([-\w]+):(.+)$/) {
173 174 175 176
    $node_id2  = $1;
    $iface_id2 = $2;
}
else {
177
    fatal("Invalid argument for interface: " . $ARGV[2]);
178
}
179 180 181 182 183 184
if ($ARGV[3] =~ /^(\d+),(\d+)$/) {
    $card2 = $1;
    $port2 = $2;
}
else {
    fatal("Invalid argument for portinfo: " . $ARGV[3]);
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
}

#
# Cannot add wires unless interfaces exist for both side.
#
my $interface1 = Interface->LookupByIface($node_id1, $iface_id1);
if (!defined($interface1)) {
    fatal("No interface for $node_id1:$iface_id1");
}
my $interface2 = Interface->LookupByIface($node_id2, $iface_id2);
if (!defined($interface2)) {
    fatal("No interface for $node_id2:$iface_id2");
}
#
# Not allowed to have any existing wires using either interface.
#
my $wire = Interface::Wire->LookupAnyByIface($node_id1, $iface_id1);
if (defined($wire)) {
    fatal("Wire using interface one already exists: $wire\n");
}
$wire = Interface::Wire->LookupAnyByIface($node_id2, $iface_id2);
if (defined($wire)) {
    fatal("Wire using interface two already exists: $wire\n");
}
# Another check using card/port info, just to be sure.
210
$wire = Interface::Wire->LookupAny($node_id1, $card1, $port1);
211 212 213
if (defined($wire)) {
    fatal("Wire using interface two already exists: $wire\n");
}
Leigh Stoller's avatar
Leigh Stoller committed
214
$wire = Interface::Wire->LookupAny($node_id2, $card2, $port2);
215 216 217 218 219 220 221 222
if (defined($wire)) {
    fatal("Wire using interface two already exists: $wire\n");
}

#
# Create the wire.
#
my $wireargs = {
223 224 225 226
    "card1"  => $card1,
    "port1"  => $port1,
    "card2"  => $card2,
    "port2"  => $port2,
227 228
};
$wireargs->{"cable"}  = $cable if (defined($cable));
229
$wireargs->{"cablelen"} = $cablelen if (defined($cablelen));
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

if ($impotent || $verbose) {
    if ($verbose) {
	print "Creating interface with arguments:\n";
    }
    else {
	print "Would create interface with arguments:\n";
    }
    print Dumper($wireargs);
}
if (!$impotent) {
    Interface::Wire->Create($interface1, $interface2, $type, $wireargs)
	or fatal("Could not create wire entry");
}
exit(0);

sub fatal($)
{
    my ($mesg) = $_[0];

    die("*** $0:\n".
	"    $mesg\n");
}

#
# Remove a wire.
#
257
sub RemoveWire($$)
258
{
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    my ($arg1, $arg2) = @_;

    if ($arg1 =~ /^([-\w]+):(.+)$/) {
	$node_id1  = $1;
	$iface_id1 = $2;
    }
    else {
	fatal("Invalid argument for interface: " . $arg1);
    }
    if ($arg2 =~ /^([-\w]+):(.+)$/) {
	$node_id2  = $1;
	$iface_id2 = $2;
    }
    else {
	fatal("Invalid argument for interface: " . $arg2);
    }
    my $wire = Interface::Wire->LookupAnyByIfaces($node_id1, $iface_id1,
						  $node_id2, $iface_id2);
    if (!defined($wire)) {
	fatal("No such wire");
    }
280

281 282
    my $interface = Interface->LookupByIface($wire->node_id1(),
					     $wire->iface1());
283 284 285 286 287 288 289 290 291 292 293 294
    if (defined($interface)) {
	if (Lan->FindLansByMember($interface->node_id(), $interface->iface())) {
	    if ($force) {
		print "$interface appears to be in use; continuing anyway...\n";
	    }
	    else {
		print "warning: $interface appears to be in use; " .
		    "entering impotent mode (use -f to force removal)\n";
		$impotent = 1;
	    }
	}
    }
295 296
    $interface = Interface->LookupByIface($wire->node_id2(),
					  $wire->iface2());
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    if (defined($interface)) {
	if (Lan->FindLansByMember($interface->node_id(), $interface->iface())) {
	    if ($force) {
		print "$interface appears to be in use; continuing anyway...\n";
	    }
	    else {
		print "warning: $interface appears to be in use; " .
		    "entering impotent mode (use -f to force removal)\n";
		$impotent = 1;
	    }
	}
    }
    if ($impotent) {
	print "Would delete $wire\n";
    }
    else {
	$wire->Delete(1) == 0
	    or fatal("Could not delete $wire");
    }
    return 0;
}

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 350 351 352 353 354 355 356 357 358
#
# Display a wire.
#
sub ShowWire($$)
{
    my ($arg1, $arg2) = @_;

    if ($arg1 =~ /^([-\w]+):(.+)$/) {
	$node_id1  = $1;
	$iface_id1 = $2;
    }
    else {
	fatal("Invalid argument for interface: " . $arg1);
    }
    if ($arg2 =~ /^([-\w]+):(.+)$/) {
	$node_id2  = $1;
	$iface_id2 = $2;
    }
    else {
	fatal("Invalid argument for interface: " . $arg2);
    }
    my $wire = Interface::Wire->LookupAnyByIfaces($node_id1, $iface_id1,
						  $node_id2, $iface_id2);
    if (!defined($wire)) {
	fatal("No such wire");
    }
    $wire->Dump();
    my ($interface1,$interface2) = $wire->Interfaces();
    if (defined($interface1)) {
	print "\n";
	print "Interface1:\n";
	$interface1->Dump();
    }
    if (defined($interface2)) {
	print "\n";
	print "Interface2:\n";
	$interface2->Dump();
    }
    return 0;
}
359