Create Duplicate Disabled Pool

Problem this snippet solves:

Perl script to copy an arbitrary pool, disabling all nodes in the pool. This was intended for creating duplicate pools that start out with everything disabled, and then moving a node between pools would simply require disabling it in the pool it's in and enabling it in the one you want it in.

Code :

#!/usr/bin/perl -w

# Duplicates a pool, modifying it such that all members are disabled.

$bigpipe = "bigpipe";

if(@ARGV < 2) {
  die "Usage: perl copyPool.pl  \n" .
      "    toCopy - The name of the pool to copy\n" .
      "    name - The name to give the new pool\n";
}

$toCopy = shift;
$name = shift;

print "Loading pool info...\n";

if(!($poolInfo = `$bigpipe pool $toCopy list`)) {
  die "Error invoking $bigpipe\n";
}
# $poolInfo = `cat poolExample.txt`;
# $poolInfo = `cat badPoolExample.txt`;

if($poolInfo =~ m/^BIGpipe:/) {
  die "Error copying pool:\n$poolInfo\n";
}
if(!($poolInfo =~ m/^pool $toCopy/)) {
  die "Error copying pool: No pool info received\n$poolInfo\n";
}

# The pipe here is to redirect stderr to stdout.  "Successfull" checks will
# cause bigpipe to display an error in stderr and we want to surpress it.
# The pipe redirects stderr to stdout, which is already captured by perl.
if(($tmp = `$bigpipe pool $name list 2>&1`) && ($? == 0)) {
  die "Error: Pool $name already exists:\n$tmp\n";
}


#print "Original Pool:\n$poolInfo\n";

# Disable all the members of the pool.
# First, remove all 'session disable' declarations from the members in the pool,
# then add a 'session disable' declaration to every member.  This is simpler
# than trying to detect which members aren't already disabled and then adding
# it to just those members.
$newPoolInfo = $poolInfo;
$newPoolInfo =~ s/([ ]*member .*) session disable(.*)/$1 $2/g;
$newPoolInfo =~ s/([ ]*member [^\r\n]*)([\r\n]*)/$1 session disable$2/g;
$newPoolInfo =~ s/$toCopy/$name/g;

print "New Pool:\n$newPoolInfo";

$flatPoolInfo = $newPoolInfo;
$flatPoolInfo =~ s/(\r|\n)//g;
chomp $flatPoolInfo;

# print "Flat pool data:\n$flatPoolInfo\n";

$command = "$bigpipe $flatPoolInfo";
print "Duplicating pool...\n";
#print "Command: $command\n";
system($command);

print "Saving config...\n";
system("$bigpipe save");

print "Done.\n";
Published Mar 12, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment