addswitch.in 6.31 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
#!/usr/bin/perl -w
#
# Copyright (c) 2003-2018 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 Date::Parse;
use Time::Local;
use Data::Dumper;
use File::Temp qw(tempfile);

#
# Add a generic device to the DB. 
#
sub usage()
{
37
    print STDERR "Usage: addswitch -t <type> [-u] [-S <stack>] <switch_id>\n";
38 39 40 41 42 43 44 45 46 47
    print STDERR "Usage: addswitch -r <switch_id>\n";
    print STDERR "Options:\n";
    print STDERR " -d       - Turn on debugging\n";
    print STDERR " -r       - Remove switch instead.\n";
    print STDERR " -n       - Dry run mode\n";
    print STDERR " -t type  - Type name (eg: cisco-ios, whatever)\n";
    print STDERR "            Type is created if it does not exist.\n";
    print STDERR " -1       - Indicate a layer one switch instead\n";
    print STDERR " -S stack - Add switch to switch stack.\n";
    print STDERR "            Stack (type) must already exist, use mysql.\n";
48
    print STDERR " -u       - A user allocatable switch.\n";
49 50
    exit(-1);
}
51
my $optlist   = "t:dnr1S:u";
52 53 54 55
my $debug     = 0;
my $impotent  = 0;
my $remove    = 0;
my $layer1    = 0;
56
my $usersw    = 0;
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
my $stack;
my $type;

# Protos
sub fatal($);
sub CreateType($);
sub CreateSwitch();

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

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

#
# 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{'d'})) {
    $debug = 1;
}
if (defined($options{'n'})) {
    $impotent = 1;
}
if (defined($options{'t'})) {
    $type = $options{'t'};
}
if (defined($options{'S'})) {
    $stack = $options{'S'};
}
if (defined($options{'1'})) {
    $layer1 = 1;
}
if (defined($options{'r'})) {
    $remove = 1;
}
119 120 121
if (defined($options{'u'})) {
    $usersw = 1;
}
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
usage()
    if (@ARGV != 1);
usage()
    if (!$remove && !defined($type));

my $node_id = $ARGV[0];

#
# 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!");
}

#
# Remove and done.
#
if ($remove) {
    my $switch = Node->Lookup($node_id);
    if (!defined($switch))  {
	print STDERR "Switch does not exist in the DB\n";
	exit(0);
    }
    $switch->Delete();
    # Must also delete from the switch_stacks table.
    DBQueryFatal("delete from switch_stacks where node_id='$node_id'");
    exit(0);
}

#
# The node may not already exist.
#
if (Node->Lookup($node_id)) {
    fatal("Switch already exists in the DB");
}

# Stack (type) must already exist, has to be created by hand with mysql.
if (defined($stack)) {
    my $query_result =
	DBQueryFatal("select stack_type from switch_stack_types ".
		     "where stack_id='$stack'");
    if (!$query_result->numrows) {
	fatal("Stack does not exist in the switch_stack_types table");
    }
}

#
# Create the type if it does not exist.
#
my $nodetype = NodeType->Lookup($type);
if (!defined($nodetype)) {
    CreateType($type);
    $nodetype = NodeType->Lookup($type);    
}
CreateSwitch();
exit(0);

#
# Add a new node type. 
#
sub CreateType($)
{
    my ($type) = @_;
    my $protocol = ($layer1 ? "wire" : "ethernet");

    #
    # Defaults work for almost everything. 
    #
    if ($debug || $impotent) {
	print "Creating switch type: $type\n";
    }

    if (!$impotent) {
	DBQueryFatal("replace into node_types set ".
		     "  class='switch', type='$type', isswitch='1'");
    }

    #
    # Device nodes are neither rebootable nor imageable.
    #
    if ($debug || $impotent) {
	print "Adding false attributes rebootable/imageable\n";
	print "Adding forwarding_protocols: $protocol\n";
    }
    if (!$impotent) {
	DBQueryFatal("replace into node_type_attributes values ".
		     " ('$type','rebootable','0','boolean')");
	DBQueryFatal("replace into node_type_attributes values ".
		     " ('$type','imageable','0','boolean')");
	DBQueryFatal("replace into node_type_attributes values ".
		     " ('$type','forwarding_protocols','$protocol','string')");
    }
    return 0;
}

#
# Create the switch
#
sub CreateSwitch()
{
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    my $role = "testswitch";

    if ($usersw) {
	$role = "testnode";
    }
    elsif ($layer1) {
	# XXX Huh?
	$role = "widearea_switch";
    }
    elsif (defined($stack)) {
	if ($stack eq "Control") {
	    $role = "ctrlswitch";
	}
	elsif ($stack eq "Experiment") {
	    $role = "testswitch";
	}
	else {
	    fatal("Do not know how to handle stack $stack wrt role");
	}
    }
    else {
	print STDERR "No -S argument, defaulting to role=$role\n";
    }
	
250 251
    my $blob = {
	"type"               => $type,
252
	"role"               => $role,
253 254
	"initial_eventstate" => TBDB_NODESTATE_ISUP(),
    };
255 256 257 258 259

    #
    # User switches needs some different stuff.
    #
    if ($usersw) {
260
	$blob->{"initial_eventstate"} = TBDB_NODESTATE_ALWAYSUP();
261
    }
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    my $node;
    
    print "Creating new node $node_id\n";
    if (!$impotent) {
	$node = Node->Create($node_id, undef, $blob);
	if (!defined($node)) {
	    fatal("Could not create new node");
	}
	if (defined($stack)) {
	    print STDERR "Adding to stack: $stack\n";
	    if (!DBQueryWarn("replace into switch_stacks set ".
			     "  node_id='$node_id', stack_id='$stack'")) {
		$node->Delete();
		fatal("Could not add switch to stack");
	    }
	}
	if ($debug) {
	    print STDERR "$node\n";
	}
    }
    return 0;
}

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

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