Steve Simms steve
Wed Jan 5 16:07:04 PST 2005
I've added the same --config and --help options that I added to
slon_start.pl last night, but otherwise, this is just a lot of code
reorganization and prettifying.

Could you double-check the report, to make sure that I'm capturing what was
intended?  I was confused by it at first, but I think I understand what it's
conveying now.  I've included a simple two-server setup below.

Thanks,
Steve Simms


Changes:

- Switched from "#!perl" to "#!/usr/bin/perl"

- Added a --config option to specify the slon.env file.

- Added a --help option which displays a usage statement.

- Grouped each set of slonik commands with a comment.

- Made indentation consistent for the slonik scripts.

- @COST and @PATH were defined in two different scopes, causing the
  report to be incorrect.  Removed the declarations from the inner
  scope, and moved the declaration from the outer scope to be in the
  same place as the @VIA declaration.

- Cleaned up the report's layout.


Old Report Format:
cost
       1|   2|
--------------------------------------------
   1|inf
   1
   2|   1
inf


VIA
       1|   2|
--------------------------------------------
   1   0    2
   2   1    0
PATHS
       1|   2|
--------------------------------------------
   1   0    1
   2   1    0


New Report Format:
COST
     |   1 |   2 |
-----+-----+-----+
   1 | inf |   1 |
   2 |   1 | inf |

VIA
     |   1 |   2 |
-----+-----+-----+
   1 |   0 |   2 |
   2 |   1 |   0 |

PATHS
     |   1 |   2 |
-----+-----+-----+
   1 |   0 |   1 |
   2 |   1 |   0 |
-------------- next part --------------
--- ../altperl.orig/init_cluster.pl	2004-10-01 16:43:25.000000000 -0400
+++ init_cluster.pl	2005-01-05 10:05:14.000000000 -0500
@@ -1,48 +1,71 @@
-#!perl # -*- perl -*-
+#!/usr/bin/perl
 # $Id: init_cluster.pl,v 1.4.2.2 2004/10/01 20:43:25 cbbrowne Exp $
 # Author: Christopher Browne
 # Copyright 2004 Afilias Canada
-my @COST;
-my @PATH;
+
+use Getopt::Long;
+
+my $SLON_ENV_FILE = 'slon.env'; # Where to find the slon.env file
+my $SHOW_USAGE    = 0;          # Show usage, then quit
+
+# Read command-line options
+GetOptions("config=s"  => \$SLON_ENV_FILE,
+	   "help"      => \$SHOW_USAGE);
 
 require 'slon-tools.pm';
-require 'slon.env';
-my $FILE="/tmp/init-cluster.$$";
-open(SLONIK, ">$FILE");
+require $SLON_ENV_FILE;
 
-print SLONIK genheader();
+my $USAGE =
+"Usage: init_cluster.pl [--config file]
+
+    Generates the slonik commands necessary to create a cluster and
+    prepare the nodes for use.  Also displays a report showing the
+    relationships between the various nodes.
 
-my ($dbname, $dbhost)=($DBNAME[1], $HOST[1]);
-print SLONIK "
-   init cluster (id = $MASTERNODE, comment = 'Node $node - $dbname\@$dbhost');
 ";
+
+if ($SHOW_USAGE) {
+  print $USAGE;
+  exit 0;
+}
+
+my $FILE="/tmp/init-cluster.$$";
+
+# INIT CLUSTER
+open(SLONIK, ">", $FILE);
+print SLONIK "\n# INIT CLUSTER\n";
+print SLONIK genheader();
+my ($dbname, $dbhost) = ($DBNAME[1], $HOST[1]);
+print SLONIK "  init cluster (id = $MASTERNODE, comment = 'Node $MASTERNODE - $dbname\@$dbhost');\n";
 close SLONIK;
 run_slonik_script($FILE);
 
-open(SLONIK, ">$FILE");
+# STORE NODE
+open(SLONIK, ">", $FILE);
+print SLONIK "\n# STORE NODE\n";
 print SLONIK genheader();
-
 foreach my $node (@NODES) {
   if ($node != $MASTERNODE) {		# skip the master node; it's already initialized!
     my ($dbname, $dbhost) = ($DBNAME[$node], $HOST[$node]);
-    print SLONIK "   store node (id = $node, event node = $MASTERNODE, comment = 'Node $node - $dbname\@$dbhost');\n";
+    print SLONIK "  store node (id = $node, event node = $MASTERNODE, comment = 'Node $node - $dbname\@$dbhost');\n";
   }
 }
-
-print SLONIK "echo 'Set up replication nodes';
-";
+print SLONIK "  echo 'Set up replication nodes';\n";
 close SLONIK;
 run_slonik_script($FILE);
 
-open(SLONIK, ">$FILE");
+# STORE PATH
+open(SLONIK, ">", $FILE);
+print SLONIK "\n# STORE PATH\n";
 print SLONIK genheader();
 
-my @VIA ;
+my @COST;
+my @VIA;
+my @PATH;
 generate_listen_paths();
 report_on_paths();
-print SLONIK qq[
-echo 'Next: configure paths for each node/origin';
-];
+
+print SLONIK "  echo 'Next: configure paths for each node/origin';\n";
 foreach my $nodea (@NODES) {
   my $dsna = $DSN[$nodea];
   foreach my $nodeb (@NODES) {
@@ -51,47 +74,39 @@
       my $providerba = $VIA[$nodea][$nodeb];
       my $providerab = $VIA[$nodeb][$nodea];
       if (!$printed[$nodea][$nodeb]) {
-	print SLONIK "      store path (server = $nodea, client = $nodeb, conninfo = '$dsna');\n";
+	print SLONIK "  store path (server = $nodea, client = $nodeb, conninfo = '$dsna');\n";
 	$printed[$nodea][$nodeb] = "done";
       }
       if (!$printed[$nodeb][$nodea]) {
-	print SLONIK "      store path (server = $nodeb, client = $nodea, conninfo = '$dsnb');\n";
+	print SLONIK "  store path (server = $nodeb, client = $nodea, conninfo = '$dsnb');\n";
 	$printed[$nodeb][$nodea] = "done";
       }
-      print SLONIK "echo 'configured path between $nodea and $nodeb';\n";
+      print SLONIK "  echo 'configured path between $nodea and $nodeb';\n";
     }
   }
 }
-
 close SLONIK;
-
 run_slonik_script($FILE);
 
-open(SLONIK, ">$FILE");
+# STORE LISTEN
+open(SLONIK, ">", $FILE);
+print SLONIK "\n# STORE LISTEN\n";
 print SLONIK genheader();
-
 foreach my $origin (@NODES) {
   my $dsna = $DSN[$origin];
   foreach my $receiver (@NODES) {
     if ($origin != $receiver) {
       my $provider = $VIA[$origin][$receiver];
-      print SLONIK "      store listen (origin = $origin, receiver = $receiver, provider = $provider);\n";
+      print SLONIK "  store listen (origin = $origin, receiver = $receiver, provider = $provider);\n";
     }
   }
 }
-
-print SLONIK qq[
-        echo 'Replication nodes prepared';
-        echo 'Please start a slon replication daemon for each node';
-];
-
+print SLONIK "  echo 'Replication nodes prepared';\n";
+print SLONIK "  echo 'Please start a slon replication daemon for each node';\n";
 close SLONIK;
 run_slonik_script($FILE);
 
 sub generate_listen_paths {
-  my @COST;
-  my @PATH;
-
   my $infinity = 10000000;	# Initial costs are all infinite
   foreach my $node1 (@NODES) {
     foreach my $node2 (@NODES) {
@@ -162,49 +177,57 @@
 }
 
 sub report_on_paths {
-  print "cost\n";
-  print "    ";
+  print "COST\n";
+  print "     ";
   foreach my $node2 (@NODES) {
-    printf "%4d|", $node2;
+    printf "| %3d ", $node2;
   }
-  print "\n--------------------------------------------\n";
+  print "|\n";
+  print ("-----+" x (scalar(@NODES) + 1));
+  print "\n";
   foreach my $node1 (@NODES) {
-    printf "%4d|", $node1;
+    printf " %3d ", $node1;
     foreach my $node2 (@NODES) {
       if ($COST[$node2][$node1] == $infinity) {
-	printf "inf  ";
+	printf "| inf ";
       } else {
-	printf "%4d ", $COST[$node2][$node1];
+	printf "|%4d ", $COST[$node2][$node1];
       }
-      print "\n";
     }
+    print "|\n";
   }
-  print "\n\n";
+  print "\n";
   print "VIA\n";
-  print "    ";
+  print "     ";
   foreach my $node2 (@NODES) {
-    printf "%4d|", $node2;
+    printf "| %3d ", $node2;
   }
-  print "\n--------------------------------------------\n";
+  print "|\n";
+  print ("-----+" x (scalar(@NODES) + 1));
+  print "\n";
   foreach my $node1 (@NODES) {
-    printf "%4d", $node1;
+    printf " %3d ", $node1;
     foreach my $node2 (@NODES) {
-      printf "%4d ", $VIA[$node2][$node1];
+      printf "|%4d ", $VIA[$node2][$node1];
     }
-    print "\n";
+    print "|\n";
   }
 
+  print "\n";
   print "PATHS\n";
-  print "    ";
+  print "     ";
   foreach my $node2 (@NODES) {
-    printf "%4d|", $node2;
+    printf "| %3d ", $node2;
   }
-  print "\n--------------------------------------------\n";
+  print "|\n";
+  print ("-----+" x (scalar(@NODES) + 1));
+  print "\n";
   foreach my $node1 (@NODES) {
-    printf "%4d", $node1;
+    printf " %3d ", $node1;
     foreach my $node2 (@NODES) {
-      printf "%4d ", $PATH[$node2][$node1];
+      printf "|%4d ", $PATH[$node2][$node1];
     }
-    print "\n";
+    print "|\n";
   }
+  print "\n";
 }


More information about the Slony1-general mailing list