made remote timeout configurable by env
[scpubgit/System-Introspector.git] / lib / System / Introspector / Gatherer.pm
1 package System::Introspector::Gatherer;
2 use Moo;
3 use Object::Remote;
4 use Object::Remote::Future;
5 use System::Introspector::Gatherer::Bridge;
6 use Module::Runtime qw( use_module );
7
8 has introspectors => (is => 'ro', required => 1);
9
10 sub gather_all {
11     my ($self) = @_;
12     my %report;
13     alarm($ENV{SI_GATHER_TIMEOUT} || 60*60);
14     for my $spec (@{ $self->introspectors }) {
15         my ($base, $args) = @$spec;
16         $report{$base} = use_module("System::Introspector::Probe::$base")
17             ->new($args)
18             ->gather;
19     }
20     return \%report;
21 }
22
23 sub _new_direct {
24     my ($class, $remote, $args) = @_;
25     return $class->new::on($remote, $args || {});
26 }
27
28 sub _new_bridged {
29     my ($class, $bridge, $remote, $args) = @_;
30     return System::Introspector::Gatherer::Bridge->new::on($bridge,
31         remote_spec  => $remote,
32         remote_class => $class,
33         remote_args  => $args || {},
34     );
35 }
36
37 sub new_from_spec {
38     my ($class, %arg) = @_;
39     my ($user, $host, $sudo_user) = @arg{qw( user host sudo_user )};
40     my $sudo = defined($sudo_user) ? sprintf('%s@', $sudo_user) : undef;
41     my $args = { introspectors => $arg{introspectors} };
42     if (defined $host) {
43         my $remote = join '@', grep defined, $user, $host;
44         if (defined $sudo_user) {
45             return $class->_new_bridged($remote, $sudo, $args);
46         }
47         else {
48             return $class->_new_direct($remote, $args);
49         }
50     }
51     else {
52         if (defined $sudo_user) {
53             return $class->_new_direct($sudo, $args);
54         }
55         else {
56             return $class->new($args);
57         }
58     }
59 }
60
61 1;