2011-11-18 22:44:08 +00:00
|
|
|
package App::ClusterSSH::Cluster;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use version;
|
|
|
|
our $VERSION = version->new('0.01');
|
|
|
|
|
|
|
|
use Carp;
|
|
|
|
use Try::Tiny;
|
|
|
|
|
|
|
|
use base qw/ App::ClusterSSH::Base /;
|
|
|
|
|
|
|
|
our $master_object_ref;
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
my ( $class, %args ) = @_;
|
|
|
|
|
|
|
|
if ( !$master_object_ref ) {
|
|
|
|
$master_object_ref = $class->SUPER::new(%args);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $master_object_ref;
|
|
|
|
}
|
|
|
|
|
2011-11-21 18:02:26 +00:00
|
|
|
sub get_clusters {
|
2011-11-21 22:03:54 +00:00
|
|
|
my ( $self, @files ) = @_;
|
2011-11-21 18:02:26 +00:00
|
|
|
|
2011-11-21 22:03:54 +00:00
|
|
|
for my $file ( '/etc/clusters', @files ) {
|
|
|
|
$self->debug(3, 'Loading in config from: ', $file);
|
|
|
|
$self->read_cluster_file($file);
|
|
|
|
}
|
2011-11-21 18:02:26 +00:00
|
|
|
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub read_cluster_file {
|
|
|
|
my ( $self, $filename ) = @_;
|
2011-11-21 22:03:54 +00:00
|
|
|
$self->debug( 2, 'Reading clusters from file ', $filename );
|
2011-11-21 18:02:26 +00:00
|
|
|
|
|
|
|
if ( -f $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);
|
|
|
|
}
|
2011-11-21 22:03:54 +00:00
|
|
|
my @line = split( /\s+/, $line );
|
2011-11-21 18:02:26 +00:00
|
|
|
|
|
|
|
#s/^([\w-]+)\s*//; # remote first word and stick into $1
|
|
|
|
|
|
|
|
$self->debug( 3, "read line: $line" );
|
|
|
|
$self->register_tag(@line);
|
|
|
|
}
|
|
|
|
|
|
|
|
close($fh);
|
|
|
|
}
|
2011-11-21 22:03:54 +00:00
|
|
|
else {
|
|
|
|
$self->debug( 2, 'No file found to read');
|
|
|
|
}
|
2011-11-21 18:02:26 +00:00
|
|
|
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} ) );
|
2011-11-21 22:03:54 +00:00
|
|
|
|
|
|
|
return @{ $self->{$tag} };
|
2011-11-21 18:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$self->debug( 2, "Tag $tag is not registered" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-21 22:03:54 +00:00
|
|
|
sub list_tags {
|
|
|
|
my ($self) = @_;
|
|
|
|
return keys(%$self);
|
|
|
|
}
|
|
|
|
|
2011-11-21 18:02:26 +00:00
|
|
|
sub resolve_all_tags {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2011-11-18 22:44:08 +00:00
|
|
|
#use overload (
|
|
|
|
# q{""} => sub {
|
|
|
|
# my ($self) = @_;
|
|
|
|
# return $self->{hostname};
|
|
|
|
# },
|
|
|
|
# fallback => 1,
|
|
|
|
#);
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
=pod
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
App::ClusterSSH::Cluster
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
Object representing application configuration
|
|
|
|
|
|
|
|
=head1 METHODS
|
|
|
|
|
|
|
|
=over 4
|
|
|
|
|
|
|
|
=item $host=ClusterSSH::Cluster->new();
|
|
|
|
|
2011-11-21 18:02:26 +00:00
|
|
|
Create a new object. Object should be common across all invocations.
|
2011-11-18 22:44:08 +00:00
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
|
|
=head1 AUTHOR
|
|
|
|
|
|
|
|
Duncan Ferguson, C<< <duncan_j_ferguson at yahoo.co.uk> >>
|
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT
|
|
|
|
|
|
|
|
Copyright 1999-2010 Duncan Ferguson.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of either: the GNU General Public License as published
|
|
|
|
by the Free Software Foundation; or the Artistic License.
|
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
1;
|