Commit 36e4084a authored by Leigh Stoller's avatar Leigh Stoller

Another new script for setting up a new emulab; for creating stacks

parent a21dbef6
......@@ -59,7 +59,7 @@ SBIN_SCRIPTS = vlandiff vlansync withadminprivs export_tables cvsupd.pl \
addrfdevice addrfpath reserve announce createimagealias \
predict test-reserve notify-reservations \
deprecate_image pushrootkey addinterface addwire cnetwatch \
addswitch addtypetoimages resutil
addswitch addtypetoimages resutil addstack
WEB_SBIN_SCRIPTS= webnewnode webdeletenode webspewconlog webarchive_list \
webspewimage webdumpdescriptor webemulabfeature \
......
#!/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 switch stack to the DB.
#
sub usage()
{
print STDERR "Usage: addstack [options] <stack_id> <leader_id>\n";
print STDERR "Usage: addstack -r [-f] <stack_id>\n";
print STDERR "Options:\n";
print STDERR " -r - Remove stack instead.\n";
print STDERR " -n - Dry run mode\n";
print STDERR " -p passwd - SNMP community string or password\n";
print STDERR " -t type - Stack type (defaults to generic)\n";
print STDERR " -m min/max - Min/Max vlan numbers. Defaults to 10/750\n";
print STDERR " stack_id - Typically, Experiment or Control\n";
print STDERR " leader_id - Name of leader switch for the stack.\n";
print STDERR " Remember for later when adding switches,\n";
print STDERR " you will need a switch with this name.\n";
exit(-1);
}
my $optlist = "rnt:m:fp:";
my $debug = 0;
my $impotent = 0;
my $remove = 0;
my $force = 0;
my $minvlan = 10;
my $maxvlan = 750;
my $stack_type = "generic";
my $stack_id;
my $stack_leader;
my $password;
# Protos
sub fatal($);
#
# 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{'f'})) {
$force = 1;
}
if (defined($options{'n'})) {
$impotent = 1;
}
if (defined($options{'t'})) {
$stack_type = $options{'t'};
}
if (defined($options{'p'})) {
$password = $options{'p'};
}
if (defined($options{'m'})) {
if ($options{'m'} =~ /^(\d+)\/(\d+)$/) {
$minvlan = $1;
$maxvlan = $2;
if ($minvlan < 10) {
fatal("Min vlan must be >= 10");
}
if ($maxvlan > 4096) {
fatal("Max vlan must be < 4096");
}
}
else {
fatal("Could not parse -m option");
}
}
if (defined($options{'r'})) {
$remove = 1;
}
if ($remove) {
usage()
if (@ARGV != 1);
$stack_id = $ARGV[0];
}
else {
usage()
if (@ARGV != 2);
$stack_id = $ARGV[0];
$stack_leader = $ARGV[1];
}
#
# 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) {
if (!$force) {
print "Are you sure? This will delete existing stack entries also!\n".
"Use the -f (force) option\n";
exit(0);
}
DBQueryFatal("delete from switch_stacks where stack_id='$stack_id'");
DBQueryFatal("delete from switch_stack_types where stack_id='$stack_id'");
exit(0);
}
# Stack must not already exist.
my $query_result =
DBQueryFatal("select stack_type from switch_stack_types ".
"where stack_id='$stack_id'");
if ($query_result->numrows) {
fatal("Stack ($stack_id) aleady exists.");
}
if ($impotent) {
print "Would add stack $stack_id: leader:$stack_leader type:$stack_type ".
"minvlan:$minvlan maxvlan:$maxvlan" .
(defined($password) ? " passwd:$password" : "") . "\n";
}
else {
DBQueryFatal("insert into switch_stack_types set ".
" stack_id='$stack_id',leader='$stack_leader', ".
" stack_type='$stack_type',single_domain='0', ".
(defined($password) ? "snmp_community='$password'," : "").
" min_vlan='$minvlan', max_vlan='$maxvlan'");
}
exit(0);
sub fatal($)
{
my ($mesg) = $_[0];
die("*** $0:\n".
" $mesg\n");
}
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