/[SysIface]/lib/SysIface/Context.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/SysIface/Context.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 6 by dpavlin, Tue Jun 16 22:15:34 2009 UTC revision 7 by dpavlin, Wed Jun 17 11:28:12 2009 UTC
# Line 15  sub title { Line 15  sub title {
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;

Legend:
Removed from v.6  
changed lines
  Added in v.7

  ViewVC Help
Powered by ViewVC 1.1.26