« blib/lib/TAP/Parser/SourceHandler/Worker/LSF.pm
↑ Summary
blib/lib/TAP/Parser/Iterator/Worker.pm »
| 1 | package TAP::Parser::SourceHandler::Worker::SSH; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Getopt::Long; | ||
| 5 | use Carp; | ||
| 6 | use vars (qw($VERSION @ISA)); | ||
| 7 | |||
| 8 | use TAP::Parser::IteratorFactory (); | ||
| 9 | use TAP::Parser::SourceHandler::Worker (); | ||
| 10 | @ISA = 'TAP::Parser::SourceHandler::Worker'; | ||
| 11 | |||
| 12 | TAP::Parser::IteratorFactory->register_handler(__PACKAGE__); | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | TAP::Parser::SourceHandler::Worker::SSH - Stream TAP from an L<IO::Handle> or a GLOB. | ||
| 17 | |||
| 18 | =head1 VERSION | ||
| 19 | |||
| 20 | Version 0.03 | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | $VERSION = '0.03'; | ||
| 25 | |||
| 26 | use constant iterator_class => 'TAP::Parser::Iterator::Worker::SSH'; | ||
| 27 | |||
| 28 | =head3 C<@hosts> | ||
| 29 | |||
| 30 | Class static variable to keep track of hosts. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | my @hosts; | ||
| 35 | |||
| 36 | =head3 C<get_active_workers> | ||
| 37 | |||
| 38 | my @active_workers = $class->get_active_workers; | ||
| 39 | |||
| 40 | Returns list of active workers. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub get_active_workers { | ||
| 45 | my $class = shift; | ||
| 46 | |||
| 47 | my @workers = $class->SUPER::get_active_workers; | ||
| 48 | |||
| 49 | return unless (@workers); | ||
| 50 | my @active; | ||
| 51 | require Net::Ping; | ||
| 52 | my $ping = Net::Ping->new(); | ||
| 53 | for my $worker (@workers) { | ||
| 54 | if ( $worker->{host} ) { | ||
| 55 | my $status = $ping->ping( $worker->{host} ); | ||
| 56 | unless ($status) { | ||
| 57 | $worker = undef; | ||
| 58 | next; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | push @active, $worker if ($worker); | ||
| 62 | } | ||
| 63 | $ping->close(); | ||
| 64 | return @active; | ||
| 65 | } | ||
| 66 | |||
| 67 | =head3 C<load_options> | ||
| 68 | |||
| 69 | Setup the worker specific options. | ||
| 70 | |||
| 71 | my @active_workers = $class->load_options($app_prove_object, \@ARGV); | ||
| 72 | |||
| 73 | Returns boolean. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | sub load_options { | ||
| 78 | my $class = shift; | ||
| 79 | my ( $app, $args ) = @_; | ||
| 80 | croak 'parent failed to load options.' unless($class->SUPER::load_options(@_)); | ||
| 81 | { | ||
| 82 | local @ARGV = @$args; | ||
| 83 | Getopt::Long::Configure(qw(no_ignore_case bundling pass_through)); | ||
| 84 | |||
| 85 | # Don't add coderefs to GetOptions | ||
| 86 | GetOptions( 'hosts=s' => \$app->{hosts}, ) | ||
| 87 | or croak('Unable to continue'); | ||
| 88 | @hosts = split /,/, $app->{hosts} if ( $app->{hosts} ); | ||
| 89 | unless (@hosts) { | ||
| 90 | croak( | ||
| 91 | 'No host found. At least one host need to be specified with --hosts option.' | ||
| 92 | ); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | return 1; | ||
| 96 | } | ||
| 97 | |||
| 98 | =head3 C<get_next_host> | ||
| 99 | |||
| 100 | Get next hosts. | ||
| 101 | |||
| 102 | my @hosts = $class->get_next_host(); | ||
| 103 | |||
| 104 | Returns hostname. | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | sub get_next_host { | ||
| 109 | return unless (@hosts); | ||
| 110 | my $host = shift @hosts; | ||
| 111 | push @hosts, $host; | ||
| 112 | return $host; | ||
| 113 | } | ||
| 114 | |||
| 115 | END { | ||
| 116 | for my $worker ( __PACKAGE__->workers ) { | ||
| 117 | next unless ( $worker && $worker->{host} ); | ||
| 118 | |||
| 119 | #LSF: How to kill the ssh process. | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; | ||
| 124 | |||
| 125 | __END__ |