/[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

Contents of /lib/SysIface/Context.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (show annotations)
Wed Jun 17 11:28:12 2009 UTC (9 years, 9 months ago) by dpavlin
File size: 1166 byte(s)
- run commands with file cache in var/hostname/command
- run commands on all hosts in etc/hosts and display table out of it

1 package SysIface::Context;
2
3 use strict;
4 use warnings;
5
6 use base 'Mojolicious::Context';
7
8 sub param {
9 my $self = shift;
10 $self->req->params->param( shift );
11 }
12
13 sub title {
14 my $self = shift;
15 $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;

  ViewVC Help
Powered by ViewVC 1.1.26