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;
|
|
|
|
}
|
|
|
|
|
2013-03-19 18:07:39 +00:00
|
|
|
sub get_cluster_entries {
|
2011-11-21 22:03:54 +00:00
|
|
|
my ( $self, @files ) = @_;
|
2011-11-21 18:02:26 +00:00
|
|
|
|
2012-07-20 10:08:35 +01:00
|
|
|
for my $file ( '/etc/clusters', $ENV{HOME}.'/.clusterssh/clusters',@files ) {
|
2013-03-19 18:07:39 +00:00
|
|
|
$self->debug(3, 'Loading in clusters from: ', $file);
|
2011-11-21 22:03:54 +00:00
|
|
|
$self->read_cluster_file($file);
|
|
|
|
}
|
2011-11-21 18:02:26 +00:00
|
|
|
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2013-03-19 18:07:39 +00:00
|
|
|
sub get_tag_entries {
|
|
|
|
my ( $self, @files ) = @_;
|
|
|
|
|
|
|
|
for my $file ( '/etc/tags', $ENV{HOME}.'/.clusterssh/tags',@files ) {
|
|
|
|
$self->debug(3, 'Loading in tags from: ', $file);
|
|
|
|
$self->read_tag_file($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2013-03-16 21:56:56 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-21 18:02:26 +00:00
|
|
|
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 ) {
|
2013-03-16 21:56:56 +00:00
|
|
|
my %tags = $self->load_file( type => 'cluster', filename => $filename);
|
2011-11-21 18:02:26 +00:00
|
|
|
|
2013-03-16 21:56:56 +00:00
|
|
|
foreach my $tag (keys %tags) {
|
|
|
|
$self->register_tag($tag, split(/\s+/, $tags{$tag}));
|
|
|
|
}
|
2011-11-21 18:02:26 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-16 21:56:56 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-21 18:02:26 +00:00
|
|
|
sub register_tag {
|
|
|
|
my ( $self, $tag, @nodes ) = @_;
|
|
|
|
|
|
|
|
$self->debug( 2, "Registering tag $tag: ", join( ' ', @nodes ) );
|
|
|
|
|
2013-03-16 21:56:56 +00:00
|
|
|
$self->{tags}->{$tag} = \@nodes;
|
2011-11-21 18:02:26 +00:00
|
|
|
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_tag {
|
|
|
|
my ( $self, $tag ) = @_;
|
|
|
|
|
2013-03-16 21:56:56 +00:00
|
|
|
if ( $self->{tags}->{$tag} ) {
|
2011-11-21 18:02:26 +00:00
|
|
|
$self->debug( 2, "Retrieving tag $tag: ",
|
2013-03-19 18:07:39 +00:00
|
|
|
join( ' ', sort @{ $self->{tags}->{$tag} } ) );
|
2011-11-21 22:03:54 +00:00
|
|
|
|
2013-03-16 21:56:56 +00:00
|
|
|
return sort @{ $self->{tags}->{$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) = @_;
|
2013-03-16 21:56:56 +00:00
|
|
|
return sort keys(%{ $self->{tags} });
|
|
|
|
}
|
|
|
|
|
|
|
|
sub dump_tags {
|
|
|
|
my ($self) = @_;
|
|
|
|
return %{ $self->{tags} };
|
2011-11-21 22:03:54 +00:00
|
|
|
}
|
|
|
|
|
2011-11-18 22:44:08 +00:00
|
|
|
#use overload (
|
|
|
|
# q{""} => sub {
|
|
|
|
# my ($self) = @_;
|
|
|
|
# return $self->{hostname};
|
|
|
|
# },
|
|
|
|
# fallback => 1,
|
|
|
|
#);
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
=pod
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
2013-02-14 22:11:21 +00:00
|
|
|
App::ClusterSSH::Cluster - Object representing cluster configuration
|
2011-11-18 22:44:08 +00:00
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
Object representing application configuration
|
|
|
|
|
|
|
|
=head1 METHODS
|
|
|
|
|
|
|
|
=over 4
|
|
|
|
|
2011-11-24 21:47:37 +00:00
|
|
|
=item $cluster=ClusterSSH::Cluster->new();
|
2011-11-18 22:44:08 +00:00
|
|
|
|
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
|
|
|
|
2013-03-19 18:07:39 +00:00
|
|
|
=item $cluster->get_cluster_entries($filename);
|
|
|
|
|
|
|
|
Read in /etc/clusters, $HOME/.clusterssh/clusters and any other given
|
|
|
|
file name and register the tags found.
|
|
|
|
|
|
|
|
=item $cluster->get_tag_entries($filename);
|
2011-11-24 21:47:37 +00:00
|
|
|
|
2013-03-19 18:07:39 +00:00
|
|
|
Read in /etc/tags, $HOME/.clusterssh/tags and any other given
|
|
|
|
file name and register the tags found.
|
2011-11-24 21:47:37 +00:00
|
|
|
|
|
|
|
=item $cluster->read_cluster_file($filename);
|
|
|
|
|
|
|
|
Read in the given cluster file and register the tags found
|
|
|
|
|
2013-03-16 21:56:56 +00:00
|
|
|
=item $cluster->read_tag_file($filename);
|
|
|
|
|
|
|
|
Read in the given tag file and register the tags found
|
|
|
|
|
2011-11-24 21:47:37 +00:00
|
|
|
=item $cluster->register_tag($tag,@hosts);
|
|
|
|
|
|
|
|
Register the given tag name with the given host names.
|
|
|
|
|
2013-03-16 21:56:56 +00:00
|
|
|
=item $cluster->register_host($host,@tags);
|
|
|
|
|
|
|
|
Register the given host on the provided tags.
|
|
|
|
|
2011-11-24 21:47:37 +00:00
|
|
|
=item @entries = $cluster->get_tag('tag');
|
|
|
|
|
|
|
|
Retrieve all entries for the given tag
|
|
|
|
|
|
|
|
=item @tags = $cluster->list_tags();
|
|
|
|
|
|
|
|
Return an array of all available tag names
|
|
|
|
|
2013-03-16 21:56:56 +00:00
|
|
|
=item %tags = $cluster->dump_tags();
|
|
|
|
|
|
|
|
Returns a hash of all tag data.
|
|
|
|
|
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;
|