root/library/perl/trunk/Archer/lib/Archer/Plugin/Shell.pm

Revision 369, 1.5 kB (checked in by miya, 3 years ago)
Line 
1 package Archer::Plugin::Shell;
2
3 use strict;
4 use warnings;
5 use base qw( Archer::Plugin );
6
7 sub run {
8     my ($self, $context, $args) = @_;
9
10     my $config  = $self->{config};
11     my $project = $context->{project};
12     my $role    = $config->{role};
13
14     my $shell = MyShell->new({
15         context => $context,
16         config  => $config,
17         servers => $context->{config}->{projects}->{$project}->{$role},
18     });
19
20     $shell->cmdloop;
21 };
22
23 package MyShell;
24
25 use base qw( Term::Shell );
26
27 sub new {
28     my ( $class, $args ) = @_;
29     my $self = $class->SUPER::new;
30
31     for ( keys %$args ) {
32         $self->{$_} = $args->{$_};
33     }
34
35     return $self;
36 }
37
38 sub catch_run {
39     my ($self, $cmd, @args) = @_;
40
41     my $parallel = $self->{context}->{config}->{global}->{parallel}
42         || 'Archer::Parallel::ForkManager';
43     $parallel->use or die $@;
44
45     my $manager = $parallel->new;
46     $manager->run({
47         elems => $self->{servers},
48         callback => sub {
49             my $server = shift;
50             $self->callback($server, $cmd, @args);
51         },
52         num => $self->{config}->{para},
53     });
54 }
55
56 sub callback {
57     my ( $self, $server, $cmd, @args ) = @_;
58
59     my $out = `ssh $server $cmd @args`;
60     chomp $out;
61
62     print "[$server] $out\n";
63
64 }
65
66 1;
67 __END__
68
69 =head1 NAME
70
71 Archer::Plugin::Shell - display shell prompt for remote servers.
72
73 =head1 SYNOPSIS
74
75   init:
76     - module: Shell
77       config:
78         role: app
79         para: 5
80
81 =head1 DESCRIPTION
82
83 Shell prompt for remote servers.
84
85 =head1 AUTHORS
86
87 Gosuke Miyashita
88
89 =head1 SEE ALSO
90
91 L<Term::Shell>
92
93 =cut
94
Note: See TracBrowser for help on using the browser.