add_rack_nodes.in 2.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/perl -w

if (scalar(@ARGV) != 1) {
    print "Usage: add_rack_nodes <type>\n";
    exit(1);
}

my $type = $ARGV[0];

our $TB = "/usr/testbed";
11
our $IP_START = "155.98.34.11";
12

13 14
use lib "/usr/testbed/lib";
use libdb;
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
use IPC::Open2;

sub sequentialSystem($$)
{
  my $program = shift(@_);
  my $input = shift(@_);
  local(*CHILD_OUT, *CHILD_IN);
  my $childpid = open2(*CHILD_OUT, *CHILD_IN, $program)
    or die("Can't open program $program\n");
  print CHILD_IN $input;
  close(CHILD_IN);
  waitpid($childpid, 0);
  my @output = <CHILD_OUT>;
  return join("", @output);
}

sub newAction(@)
{
  my $blob = "";
  $blob .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  $blob .= "<newnode>\n";
  my %actions = @_;
  for my $key (keys(%actions)) {
    $blob .= "  <attribute name=\"" . $key . "\">\n";
    $blob .= "    <value>" . $actions{$key} . "</value>\n";
    $blob .= "  </attribute>\n";
  }
  $blob .= "</newnode>\n";
  return sequentialSystem("perl -wT ./newscript.in", $blob);
}

# Reboot Nodes
# Wait for Nodes to Boot

# Gather Node IDs
my $nodeText = newAction("table" => "node",
			 "command" => "list");
my @nodeListText = split("\n", $nodeText);
my @nodeList = ();
foreach my $node (@nodeListText) {
    my @fields = split(" ", $node);
    if (scalar(@fields) >= 1) {
	push(@nodeList, $fields[0]);
    }
}

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
my @octets = split(/\./, $IP_START);
if (scalar(@octets) != 4) {
    print STDERR "Start IP Address is not a well-formed IPv4 address.\n";
    print STDERR join(" -- ", @octets)."\n";
    exit(1);
}

my %used_ips = ();
my $dbresult = DBQueryFatal("select IP from interfaces where role='ctrl'");
while (my ($ip) = $dbresult->fetchrow_array) {
    $used_ips{$ip} = 1;
}

my %node2ip = ();
foreach my $node (@nodeList) {
    my $done = 0;
    while (! $done) {
	my $current = join(".", @octets);
	if (! exists($used_ips{$current})) {
	    $node2ip{$node} = $current;
	    $done = 1;
	}
	$octets[3] += 1;
	if ($octets[3] > 254) {
	    $octets[3] = 1;
	    $octets[2] += 1;
	}
	if ($octets[2] > 254) {
	    $octets[2] = 1;
	    $octets[1] += 1;
	}
	if ($octets[1] > 254) {
	    $octets[1] = 1;
	    $octets[0] += 1;
	}
    }
}

99 100 101 102 103
# Set Node Types
foreach my $node (@nodeList) {
    newAction("table" => "node",
	      "id" => $node,
	      "command" => "update",
104 105
	      "type" => $type,
	      "IP" => $node2ip{$node});
106 107 108 109 110 111 112
}

# Associate Interfaces with Switch Ports
system("perl newnode_research.in");

# Commit Changes
foreach my $node (@nodeList) {
113
    newAction("table" => "node", "id" => $node, "command" => "commit");
114
}