Start of tags file handling

Refactor file loading code into one place since it should all work in a similar way.

Added in tag functionality.
- Cluster files are: tag host host host host
- Tag files are: host tag tag tag

Yes to add in remainder of code to load in the files automatically
This commit is contained in:
Duncan Ferguson 2013-03-16 21:56:56 +00:00
parent 2ab7527633
commit b2ba4daa46
8 changed files with 255 additions and 92 deletions

View file

@ -34,40 +34,32 @@ sub get_clusters {
return $self;
}
sub read_tag_file {
my ( $self, $filename ) = @_;
$self->debug( 2, 'Reading tags from file ', $filename );
if ( -f $filename ) {
my %hosts = $self->load_file( type => 'cluster', filename => $filename);
foreach my $host (keys %hosts) {
$self->debug(4, "Got entry for $host on tags $hosts{$host}");
$self->register_host($host, split(/\s+/, $hosts{$host}));
}
}
else {
$self->debug( 2, 'No file found to read');
}
return $self;
}
sub read_cluster_file {
my ( $self, $filename ) = @_;
$self->debug( 2, 'Reading clusters from file ', $filename );
if ( -f $filename ) {
open( my $fh, '<', $filename )
|| croak(
App::ClusterSSH::Exception::Cluster->throw(
error => $self->loc(
'Unable to read file [_1]: [_2]',
$filename, $!
)
)
);
my %tags = $self->load_file( type => 'cluster', filename => $filename);
my $line;
while ( defined( $line = <$fh> ) ) {
next
if ( $line =~ /^\s*$/ || $line =~ /^#/ )
; # ignore blank lines & commented lines
chomp $line;
if ( $line =~ s/\\\s*$// ) {
$line .= <$fh>;
redo unless eof($fh);
}
my @line = split( /\s+/, $line );
#s/^([\w-]+)\s*//; # remote first word and stick into $1
$self->debug( 3, "read line: $line" );
$self->register_tag(@line);
foreach my $tag (keys %tags) {
$self->register_tag($tag, split(/\s+/, $tags{$tag}));
}
close($fh);
}
else {
$self->debug( 2, 'No file found to read');
@ -75,12 +67,27 @@ sub read_cluster_file {
return $self;
}
sub register_host {
my ( $self, $node, @tags ) = @_;
$self->debug( 2, "Registering node $node on tags:", join( ' ', @tags ) );
foreach my $tag (@tags) {
if( $self->{tags}->{$tag} ) {
$self->{tags}->{$tag} = [ sort @{ $self->{tags}->{$tag} }, $node ] ;
} else {
$self->{tags}->{$tag} = [ $node ];
}
#push(@{ $self->{tags}->{$tag} }, $node);
}
return $self;
}
sub register_tag {
my ( $self, $tag, @nodes ) = @_;
$self->debug( 2, "Registering tag $tag: ", join( ' ', @nodes ) );
$self->{$tag} = \@nodes;
$self->{tags}->{$tag} = \@nodes;
return $self;
}
@ -88,11 +95,11 @@ sub register_tag {
sub get_tag {
my ( $self, $tag ) = @_;
if ( $self->{$tag} ) {
if ( $self->{tags}->{$tag} ) {
$self->debug( 2, "Retrieving tag $tag: ",
join( ' ', $self->{$tag} ) );
join( ' ', $self->{tags}->{$tag} ) );
return @{ $self->{$tag} };
return sort @{ $self->{tags}->{$tag} };
}
$self->debug( 2, "Tag $tag is not registered" );
@ -101,7 +108,12 @@ sub get_tag {
sub list_tags {
my ($self) = @_;
return keys(%$self);
return sort keys(%{ $self->{tags} });
}
sub dump_tags {
my ($self) = @_;
return %{ $self->{tags} };
}
#use overload (
@ -142,10 +154,18 @@ Read in /etc/clusters and any other given file name and register the tags found.
Read in the given cluster file and register the tags found
=item $cluster->read_tag_file($filename);
Read in the given tag file and register the tags found
=item $cluster->register_tag($tag,@hosts);
Register the given tag name with the given host names.
=item $cluster->register_host($host,@tags);
Register the given host on the provided tags.
=item @entries = $cluster->get_tag('tag');
Retrieve all entries for the given tag
@ -154,6 +174,10 @@ Retrieve all entries for the given tag
Return an array of all available tag names
=item %tags = $cluster->dump_tags();
Returns a hash of all tag data.
=back
=head1 AUTHOR