changeiface.in 7.41 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 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 215 216 217 218 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 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 275 276 277 278 279 280 281
#!/usr/bin/perl -w

#
# Copyright (c) 2000-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 English;
use Getopt::Std;

sub usage()
{
    print("Usage: updateiface [-n] nodeid iface newiface\n");
    exit(-1);
}
my $optlist   = "n";
my $impotent  = 0;

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

# Load the Testbed support stuff.
use lib "@prefix@/lib";
use emdb;
use Interface;
use Node;
use Lan;
use EmulabConstants;

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

# Protos
sub fatal($);

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
$impotent = 1
    if (defined($options{"n"}));

usage()
    if (@ARGV != 3);

my $nodeid   = $ARGV[0];
my $iface    = $ARGV[1];
my $newiface = $ARGV[2];

#
# Grab interface and wire.
#
my $interface = Interface->LookupByIface($nodeid, $iface);
if (!defined($interface)) {
    fatal("No such iface: $nodeid:$iface");
}
# Find the wire, need to check both sides.
my $wire = Interface::Wire->LookupAnyByIface($nodeid, $iface);
if (!defined($interface)) {
    fatal("No wire for $nodeid:$iface, why bother?");
}

# Lan members (iface).

my @lans = Lan->FindLansByMember($nodeid, $iface);
foreach my $lan (@lans) {
    my $member = $lan->FindMember($nodeid, $iface);
    if (!defined($member)) {
	fatal("Could not lookup member for $lan:$nodeid:$iface");
    }
    if ($impotent) {
	print "Would change $member iface\n";
    }
    else {
	print "Changing $member iface\n";
	$member->SetAttribute("iface", $newiface) == 0
	    or fatal("Could not update $member");
    }
}

# vlans (member list).

my $query_result =
    DBQueryWarn("select id from vlans  ".
		"where members like '%${nodeid}:${iface}%'");
while (my ($lanid) = $query_result->fetchrow_array()) {
    my $vlan = VLan->Lookup($lanid);
    if (!defined($vlan)) {
	fatal("Could not lookup VLan $lanid");
    }
    if ($impotent) {
	print "Would change member list for $vlan\n";
    }
    else {
	my @added   = ("${nodeid}:${newiface}");
	my @removed = ("${nodeid}:${iface}");
	print "Changing member list for $vlan\n";
	VLan->RecordVLanModification($vlan, \@added, \@removed) == 0
	    or fatal("Could not update members for $vlan");
    }
}

# Delays table.

$query_result =
    DBQueryFatal("select distinct node_id,iface0,iface1 from delays ".
		 "where node_id='$nodeid' and ".
		 "      (iface0='$iface' or iface1='$iface')");
while (my (undef,$iface0,$iface1) = $query_result->fetchrow_array()) {
    # Watch for multiplexed delays.
    if ($iface0 eq $iface) {
	if ($impotent) {
	    print "Would change delays for $nodeid:iface0\n";
	}
	else {
	    print "Changing delay $nodeid:iface0\n";
	    DBQueryFatal("update delays set iface0='$newiface' ".
			 "where node_id='$nodeid' and iface0='$iface'");
	}
    }
    if ($iface1 eq $iface) {
	if ($impotent) {
	    print "Would change delays for $nodeid:iface1\n";
	}
	else {
	    print "Changing delay $nodeid:iface1\n";
	    DBQueryFatal("update delays set iface1='$newiface' ".
			 "where node_id='$nodeid' and iface1='$iface'");
	}
    }
}

# linkdelays
$query_result =
    DBQueryFatal("select distinct iface from linkdelays ".
		 "where node_id='$nodeid' and iface='$iface'");
while (my ($iface) = $query_result->fetchrow_array()) {
    if ($impotent) {
	print "Would change linkdelays for $nodeid:$iface\n";
    }
    else {
	print "Changing linkdelays for $nodeid:$iface\n";
	DBQueryFatal("update linkdelays set iface='$newiface' ".
		     "where node_id='$nodeid' and iface='$iface'");
    }
}

# interface_settings;

$query_result =
    DBQueryFatal("select distinct iface from interface_settings ".
		 "where node_id='$nodeid' and iface='$iface'");
while (my ($iface) = $query_result->fetchrow_array()) {
    if ($impotent) {
	print "Would change interface_settings for $nodeid:$iface\n";
    }
    else {
	print "Changing interface_settings for $nodeid:$iface\n";
	DBQueryFatal("update interface_settings set iface='$newiface' ".
		     "where node_id='$nodeid' and iface='$iface'");
    }
}

# port_counters.

$query_result =
    DBQueryFatal("select distinct iface from port_counters ".
		 "where node_id='$nodeid' and iface='$iface'");
while (my ($iface) = $query_result->fetchrow_array()) {
    if ($impotent) {
	print "Would change port_counters for $nodeid:$iface\n";
    }
    else {
	print "Changing port_counters for $nodeid:$iface\n";
	DBQueryFatal("update port_counters set iface='$newiface' ".
		     "where node_id='$nodeid' and iface='$iface'");
    }
}

# traces

$query_result =
    DBQueryFatal("select distinct node_id,iface0,iface1 from traces ".
		 "where node_id='$nodeid' and ".
		 "      (iface0='$iface' or iface1='$iface')");
while (my (undef,$iface0,$iface1) = $query_result->fetchrow_array()) {
    # Watch for multiplexed delays.
    if ($iface0 eq $iface) {
	if ($impotent) {
	    print "Would change traces for $nodeid:iface0\n";
	}
	else {
	    print "Changing trace for $nodeid:iface0\n";
	    DBQueryFatal("update traces set iface0='$newiface' ".
			 "where node_id='$nodeid' and iface0='$iface'");
	}
    }
    if ($iface1 eq $iface) {
	if ($impotent) {
	    print "Would change traces for $nodeid:iface1\n";
	}
	else {
	    print "Changing trace for $nodeid:iface1\n";
	    DBQueryFatal("update traces set iface1='$newiface' ".
			 "where node_id='$nodeid' and iface1='$iface'");
	}
    }
}

# vinterfaces

$query_result =
    DBQueryFatal("select distinct iface from vinterfaces ".
		 "where node_id='$nodeid' and iface='$iface'");
while (my ($iface) = $query_result->fetchrow_array()) {
    if ($impotent) {
	print "Would change vinterfaces for $nodeid:$iface\n";
    }
    else {
	print "Changing vinterfaces for $nodeid:$iface\n";
	DBQueryFatal("update vinterfaces set iface='$newiface' ".
		     "where node_id='$nodeid' and iface='$iface'");
    }
}

#
# And the main tables in a single update to avoid consistency
# problems on failure.
#
if ($impotent) {
    print "Would change interfaces, interface_state, and wires\n";
}
else {
    print "Changing interfaces, interface_state, and wires\n";
    
    DBQueryFatal("update interfaces i ".
		 "inner join wires w on ".
		 ($wire->node_id1() eq $nodeid && $wire->iface1() eq $iface
		  ? "(i.node_id=w.node_id1 and i.iface=w.iface1) "
		  : "(i.node_id=w.node_id2 and i.iface=w.iface2) ") .
		 "inner join interface_state s on ".
		 "   (i.node_id=s.node_id and i.iface=s.iface) ".
		 "set ".
		 "  i.iface='$newiface',s.iface='$newiface', ".
		 "  w.iface1='$newiface' ".
		 "where i.node_id='$nodeid' and i.iface='$iface'");
}

sub fatal($)
{
    my ($mesg) = @_;

    die("*** $0:\n".
	"    $mesg\n".
	"    Must fix this first!\n");
}