addwire.in 7.12 KB
Newer Older
1 2 3 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
#!/usr/bin/perl -w
#
# Copyright (c) 2003-2017 University of Utah and the Flux Group.
# 
# {{{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 37 38 39 40 41 42 43 44
    print STDERR "Usage: addwire -r node1:iface1 node2:iface2\n";
    print STDERR "Options:\n";
    print STDERR " -v        - Turn on debugging\n";
    print STDERR " -n        - Impotent mode\n";
    print STDERR " -r        - Delete wire\n";
    print STDERR " -f        - (with -r) force deletion, even if iface exists\n";
    print STDERR " -c cable  - Cable ID\n";
    print STDERR " -l len    - Cable length\n";
    print STDERR " -t type   - Wire type (defaults to Unused)\n";
45
    print STDERR " portinfo  - card,port (for now, might change later)\n";
46 47 48 49 50 51 52 53 54 55 56
	
    exit(-1);
}
my $optlist   = "t:nvfrc:l:";
my $verbose   = 0;
my $impotent  = 0;
my $remove    = 0;
my $force     = 0;
my $type      = "other";
my $cable;
my $cablelen;
57
my ($node_id1,$iface_id1,$card1,$port1,$node_id2,$iface_id2,$card2,$port2);
58 59 60

# Protos
sub fatal($);
61
sub RemoveWire($$);
62 63 64 65 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

#
# 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);
}
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!");
}
140 141 142
if ($remove) {
    exit(RemoveWire($ARGV[0],$ARGV[1]));
}
143
usage()
144
    if (@ARGV != 4);
145 146 147 148 149 150 151 152

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

#
# 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.
$wire = Interface::Wire->LookupAny($node_id1,
				   $interface1->card(),
				   $interface1->port());
if (defined($wire)) {
    fatal("Wire using interface two already exists: $wire\n");
}
$wire = Interface::Wire->LookupAny($node_id1,
				   $interface2->card(),
				   $interface2->port());
if (defined($wire)) {
    fatal("Wire using interface two already exists: $wire\n");
}

#
# Create the wire.
#
my $wireargs = {
215 216 217 218
    "card1"  => $card1,
    "port1"  => $port1,
    "card2"  => $card2,
    "port2"  => $port2,
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
};
$wireargs->{"cable"}  = $cable if (defined($cable));
$wireargs->{"length"} = $cablelen if (defined($cablelen));

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.
#
249
sub RemoveWire($$)
250
{
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    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");
    }
272

273 274
    my $interface = Interface->LookupByIface($wire->node_id1(),
					     $wire->iface1());
275 276 277 278 279 280 281 282 283 284 285 286
    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;
	    }
	}
    }
287 288
    $interface = Interface->LookupByIface($wire->node_id2(),
					  $wire->iface2());
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    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;
}