15 |
$self->req->params->param( 'hostname' ); |
$self->req->params->param( 'hostname' ); |
16 |
} |
} |
17 |
|
|
18 |
|
use File::Slurp; |
19 |
|
use File::Path; |
20 |
|
|
21 |
|
our @hosts; |
22 |
|
if ( ! @hosts ) { |
23 |
|
@hosts = grep { ! /^\s*#/ } map { s/[\n\r]+$//; $_; } read_file( 'etc/hosts' ); |
24 |
|
warn "hosts: ",join(" ", @hosts),$/; |
25 |
|
} |
26 |
|
|
27 |
|
sub run { |
28 |
|
my ($self,$cmd) = @_; |
29 |
|
chomp( $cmd ); |
30 |
|
|
31 |
|
my @out; |
32 |
|
foreach my $hostname ( @hosts ) { |
33 |
|
warn "# hostname $hostname\n"; |
34 |
|
|
35 |
|
my $run = "ssh $hostname $cmd"; |
36 |
|
my $path = "var/$hostname"; |
37 |
|
mkpath $path unless -e $path; |
38 |
|
my $path_cmd = $cmd; |
39 |
|
$path_cmd =~ s{^.+/([^/]+)$}{$1}; |
40 |
|
$path .= "/$path_cmd"; |
41 |
|
my $out; |
42 |
|
|
43 |
|
if ( -e $path ) { |
44 |
|
my $mtime = (stat($path))[9]; |
45 |
|
if ( time - $mtime < 60 ) { |
46 |
|
$out = read_file $path; |
47 |
|
warn "# cache $path ", -s $path, "\n"; |
48 |
|
} else { |
49 |
|
undef $out; |
50 |
|
} |
51 |
|
} |
52 |
|
|
53 |
|
if ( ! $out ) { |
54 |
|
warn "run $run"; |
55 |
|
$out = `$run`; |
56 |
|
write_file $path, $out; |
57 |
|
warn "$path ", -s $path, "\n"; |
58 |
|
} |
59 |
|
|
60 |
|
push @out, { |
61 |
|
hostname => $hostname, |
62 |
|
command => $cmd, |
63 |
|
out => $out, |
64 |
|
}; |
65 |
|
} |
66 |
|
return @out; |
67 |
|
|
68 |
|
} |
69 |
|
|
70 |
1; |
1; |