Further work on getting cluster module working

This commit is contained in:
Duncan Ferguson 2011-11-21 18:02:26 +00:00
parent 26e3ab7ef7
commit cd4c59b12e
6 changed files with 125 additions and 3 deletions

View file

@ -10,6 +10,7 @@ use Exception::Class (
'App::ClusterSSH::Exception::Config' => {
fields => 'unknown_config',
},
'App::ClusterSSH::Exception::Cluster',
);
# Dont use SVN revision as it can cause problems

View file

@ -23,6 +23,81 @@ sub new {
return $master_object_ref;
}
sub get_clusters {
my ($self) = @_;
$self->read_cluster_file('/etc/clusters');
return $self;
}
sub read_cluster_file {
my ( $self, $filename ) = @_;
if ( -f $filename ) {
$self->debug( 2, 'Reading clusters from file ', $filename );
open( my $fh, '<', $filename )
|| croak(
App::ClusterSSH::Exception::Cluster->throw(
error => $self->loc(
'Unable to read file [_1]: [_2]',
$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);
}
close($fh);
}
return $self;
}
sub register_tag {
my ( $self, $tag, @nodes ) = @_;
$self->debug( 2, "Registering tag $tag: ", join( ' ', @nodes ) );
$self->{$tag} = \@nodes;
return $self;
}
sub get_tag {
my ( $self, $tag ) = @_;
if ( $self->{$tag} ) {
$self->debug( 2, "Retrieving tag $tag: ",
join( ' ', $self->{$tag} ) );
return $self->{$tag};
}
$self->debug( 2, "Tag $tag is not registered" );
return;
}
sub resolve_all_tags {
my ($self) = @_;
return $self;
}
#use overload (
# q{""} => sub {
# my ($self) = @_;
@ -51,7 +126,7 @@ Object representing application configuration
=item $host=ClusterSSH::Cluster->new();
Create a new object.
Create a new object. Object should be common across all invocations.
=back

1
t/30cluster.cannot_read Normal file
View file

@ -0,0 +1 @@
#cannot read this file

1
t/30cluster.file1 Normal file
View file

@ -0,0 +1 @@
tag1 host1

8
t/30cluster.file2 Normal file
View file

@ -0,0 +1,8 @@
# a comment
tag1 host1
tag2 host2
#line wrapped
tag3 host3 \
host4

View file

@ -21,8 +21,44 @@ isa_ok( $cluster1, 'App::ClusterSSH::Cluster' );
my $cluster2 = App::ClusterSSH::Cluster->new();
isa_ok( $cluster2, 'App::ClusterSSH::Cluster' );
$cluster1->{cluster} = 'fred';
my @expected = ( 'pete', 'jo', 'fred' );
ok( defined( $cluster2->{cluster} ), 'Shared cluster object' );
$cluster1->register_tag( 'people', @expected );
is_deeply( $cluster2->get_tag('people'), \@expected,
'Shared cluster object' );
# should pass without issue
trap {
$cluster1->read_cluster_file( $Bin . '/30cluster.doesnt exist' );
};
is( ! $trap, '', 'coped with missing file ok' );
isa_ok( $cluster1, 'App::ClusterSSH::Cluster' );
my $no_read=$Bin . '/30cluster.cannot_read';
chmod 0000, $no_read;
trap {
$cluster1->read_cluster_file( $no_read );
};
chmod 0644, $no_read;
isa_ok( $trap->die, 'App::ClusterSSH::Exception::Cluster' );
is( $trap->die, "Unable to read file $no_read: Permission denied", 'Error on reading an existing file ok');
@expected = ('host1');
$cluster1->read_cluster_file( $Bin . '/30cluster.file1' );
is_deeply( $cluster1->get_tag('tag1'), \@expected, 'read simple file OK' );
@expected = ('host1');
$cluster1->read_cluster_file( $Bin . '/30cluster.file2' );
is_deeply( $cluster1->get_tag('tag1'),
\@expected, 'read more complex file OK' );
@expected = ('host2');
is_deeply( $cluster1->get_tag('tag2'),
\@expected, 'read more complex file OK' );
@expected = ( 'host3', 'host4' );
is_deeply( $cluster1->get_tag('tag3'),
\@expected, 'read more complex file OK' );
done_testing();