]> xenbits.xensource.com Git - people/iwj/colo-for-testing.git/commitdiff
convert-cabinet-layout: WIP, generates "testhosts.m4"
authorIan Jackson <ian.jackson@eu.citrix.com>
Mon, 9 Mar 2015 17:31:22 +0000 (17:31 +0000)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Mon, 9 Mar 2015 17:31:22 +0000 (17:31 +0000)
convert-cabinet-layout [new file with mode: 0755]

diff --git a/convert-cabinet-layout b/convert-cabinet-layout
new file mode 100755 (executable)
index 0000000..4c61dbf
--- /dev/null
@@ -0,0 +1,90 @@
+#!/usr/bin/perl -w
+#
+# * "Save as" the file "COLO Cabinet Layout.*" as .csv with the default
+#   settings from LibreOffice Calc, generating "COLO Cabinet Layout.csv"
+#
+# * Run
+#      ./convert-cabinet-layout "COLO Cabinet Layout.csv";
+
+use strict;
+use Text::CSV;
+
+use Data::Dumper;
+
+open DEBUG, ">/dev/null";
+
+while (@ARGV && $ARGV[0] =~ m/^-/) {
+    $_ = shift @ARGV;
+    last if m/^\-\-?$/;
+    for (;;) {
+        last unless m/^-./;
+        if (s/^-D/-/) {
+            open DEBUG, ">&STDOUT" or die $!;
+            select(DEBUG);
+            $|=1;
+            select(STDOUT);
+            $|=1;
+        } else {
+            die "bad option $_ ?";
+        }
+    }
+}
+
+die unless @ARGV==1;
+
+our $inputfile = shift @ARGV;
+
+my $csv = Text::CSV->new or die $!;
+
+my $fh = new IO::File $inputfile, "<:encoding(utf8)" or die "$inputfile $!";
+
+my $headings = $csv->getline($fh);
+die $! unless $headings;
+$csv->column_names($headings);
+
+our $machine;
+
+sub reportno ($) {
+    my ($what) = @_;
+    return unless $@;
+    print "# $machine: no $what: $@\n";
+}
+
+our %dests;
+our %outfor;
+
+sub outfor ($$) {
+    my ($dest,$s) = @_;
+    $dests{$dest} //= %dests;
+    $outfor{$dest} .= $s;
+}
+
+while (my $row = $csv->getline_hr($fh)) {
+    print DEBUG Dumper($row);
+
+    $machine = $row->{'Server / Appliance'};
+    next unless $machine =~ m/^([a-z]+) ([0-1])$/;
+    $machine = $1.$2;
+
+    print "# $machine\n";
+
+    eval {
+        my $ip = $row->{'IP addr'} or die "no ip\n";
+        $ip =~ m/^172\.16\.(144|145|146|147)\.(\d+)$/ or die "bad ip\n";
+        my ($net, $host) = ($1,$2);
+        my $mac = $row->{'MAC addr'} or die "no mac addr\n";
+        $mac =~ m/^[0-9a-f]{2}(?:\:[0-9a-f]{2}){5}$/ or die "bad mac\n";
+        $mac = $&;
+        outfor 'testhosts.m4',
+        sprintf "T%d(\t%d,\t%s,%s\t%s)\n", $net, $host, $machine,
+               (length($machine)<=6 ? "\t" : ""), $mac;
+    };
+    reportno 'Ether';
+}
+
+close $fh or die $!;
+
+foreach my $dest (sort { $dests{$a} <=> $dests{$b} } keys %dests) {
+    print "\n#-------------------- $dest --------------------\n" or die $!;
+    print $outfor{$dest} or die $!;
+}