App-SSH-Cluster

 view release on metacpan or  search on metacpan

lib/App/SSH/Cluster.pm  view on Meta::CPAN

has '_config' => (
   is       => 'ro',
   isa      => HashRef,
   builder  => '_build_config',
   init_arg => undef,
   lazy     => 1,
);

sub run {
   my ($self) = @_;
   $self->_validate_config;

   my $parallel_executor    = Net::OpenSSH::Parallel->new;
   my $global_identity_file = $self->_config->{identity_file};
   my $global_username      = $self->_config->{user};

   my @hosts = @{ $self->_config->{servers} };
   foreach my $host ( @hosts ) {
      my $hostname = $host->{hostname};
      my $username = $host->{user} // $global_username;
      my $identity_file 

lib/App/SSH/Cluster.pm  view on Meta::CPAN

      close $fh;
   }
}

sub _build_config {
   my ($self) = @_;

   return YAML::Tiny->read( $self->config_file )->[0];
}

sub _validate_config {
   my ($self) = @_;

   die "No 'servers' key found in " . $self->config_file
      unless exists $self->_config->{servers};
   die "Existing 'servers' key found in " . $self->config_file . ", but has no servers listed"
      if exists $self->_config->{servers} 
      && ref $self->_config->{servers} ne 'ARRAY'
      || ( 
         ref $self->_config->{servers} eq 'ARRAY'
         && @{$self->_config->{servers}} == 0

t/02-config.t  view on Meta::CPAN

use App::SSH::Cluster;
use Test::Exception tests => 6;
use YAML::Tiny;

my %TEST_FILES;
foreach my $filename ( glob("t/conf/*.yml") ) {
   $TEST_FILES{ $filename =~ s|^t/conf/config-(.*)\.yml|$1|gr } = $filename;
}

lives_ok {
   App::SSH::Cluster->new( command => 'ls', config_file => $TEST_FILES{valid} )->_validate_config;
} 'creating a new instance of App::SSH::Cluster with a valid config lives';


foreach my $filename ( grep { $_ =~ m/missing/ } keys %TEST_FILES ) {
   my ($key) = $filename =~ m/missing-(.*)/;
   throws_ok {
      App::SSH::Cluster->new( 
         command     => 'ls', 
         config_file => $TEST_FILES{$filename},
      )->_validate_config;
   } qr/No '$key' key found in $TEST_FILES{$filename}/,
   "_validate_config dies when $key is missing from the configuration file";
}

throws_ok {
   App::SSH::Cluster->new(
      command     => 'ls',
      config_file => $TEST_FILES{'no-servers-defined'},
   )->_validate_config; 
} qr/Existing 'servers' key found in $TEST_FILES{'no-servers-defined'}/,
'_validate_config dies when there is a servers key defined, but no servers are listed';



( run in 0.496 second using v1.01-cache-2.11-cpan-a5abf4f5562 )