blib/lib/MYDan/Agent/Client.pm untested

filter: Statementstmt 0.0% 0 / 197 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 290 CC 30 SCAR 63.7
CC
30
Cov
0%
run
930.0
new
2.0
BEGIN
2.0
CRAP 582.0
1 package MYDan::Agent::Client;
2  
3 =head1 NAME
4  
5 MYDan::Agent::Client
6  
7 =head1 SYNOPSIS
8  
9 use MYDan::Agent::Client;
10 my $client = MYDan::Agent::Client->new( [ 'node1', 'node2' ] );
11 my %result = $client->run( timeout => 300, input => '' );
12  
13 =cut
14 use strict;
15 use warnings;
16  
17 use Carp;
18 use YAML::XS;
19 use File::Spec;
20 use File::Basename;
21 use FindBin qw( $RealBin );
22 use YAML::XS;
23  
24 use MYDan::Agent::Query;
25 use AnyEvent;
26 use AnyEvent::Handle;
27 use AnyEvent::Socket;
28 use Time::HiRes qw(time);
29  
30 use MYDan::API::Agent;
31 use MYDan::Util::Percent;
32 use MYDan::Agent::Proxy;
33 use MYDan::Util::Hosts;
34  
35 our %RUN = ( user => 'root', max => 128, timeout => 300 );
36  
37 sub new
38 {
39 my $class = shift;
40 bless +{ node => \@_ }, ref $class || $class;
41 }
42  
43 sub run
44 {
45 my ( $this, %run, %result ) = ( shift, %RUN, @_ );
46  
47 return unless my @node = @{$this->{node}};
48  
49 my $percent = MYDan::Util::Percent->new( scalar @node, 'run ..' );
50  
51 my %proxy;
52 if( $run{proxy} )
53 {
54 my $proxy = MYDan::Agent::Proxy->new( $run{proxy} );
55 %proxy = $proxy->search( @node );
56 }
57 else { %proxy = map{ $_ => undef }@node; }
58  
59 my $isc = $run{role} && $run{role} eq 'client' ? 1 : 0;
60 $run{query}{node} = \@node if $isc;
61  
62 my $query = MYDan::Agent::Query->dump($run{query});
63  
64 eval{ $query = MYDan::API::Agent->new()->encryption( $query ) if $isc };
65 if( $@ )
66 {
67 warn "ERROR:$@\n";
68 return map{ $_ => "norun --- 1\n" }@node;
69 }
70  
71 @node = (); my %node;
72  
73 while( my( $n, $p ) = each %proxy )
74 {
75 if( $p ) { push @{$node{$p}}, $n; }
76 else { push @node, $n; }
77 }
78  
79 my $cv = AE::cv;
80 my ( @work, $stop );
81  
82 $SIG{TERM} = $SIG{INT} = my $tocb = sub
83 {
84 $stop = 1;
85 for my $w ( @work )
86 {
87 next unless $$w && $$w->fh;
88 $$w->destroy;
89 $cv->end;
90 }
91  
92 map{ $cv->end; } 1 .. $cv->{_ae_counter}||0;
93 };
94  
95 my %hosts = MYDan::Util::Hosts->new()->match( @node );
96 my $work;$work = sub{
97 return unless my $node = shift @node;
98 $result{$node} = '';
99
100 $cv->begin;
101 tcp_connect $hosts{$node}, $run{port}, sub {
102 my ( $fh ) = @_;
103 unless( $fh ){
104 $percent->add()->print() if $run{verbose};
105 $result{$node} = "tcp_connect: $!";
106 $work->();
107 $cv->end;
108 return;
109 }
110 if( $stop )
111 {
112 close $fh;
113 return;
114 }
115  
116 my $hdl;
117 push @work, \$hdl;
118 $hdl = new AnyEvent::Handle(
119 fh => $fh,
120 on_read => sub {
121 my $self = shift;
122 $self->unshift_read (
123 chunk => length $self->{rbuf},
124 sub { $result{$node} .= $_[1];}
125 );
126 },
127 on_eof => sub{
128 undef $hdl;
129 $percent->add()->print() if $run{verbose};
130 $work->();
131 $cv->end;
132 }
133 );
134 $hdl->push_write($query);
135 $hdl->push_shutdown;
136 };
137 };
138  
139 my $max = scalar @node > $run{max} ? $run{max} : scalar @node;
140 $work->() for 1 .. $max ;
141  
142 my %rresult;
143 my $rwork = sub{
144 my $node = shift;
145 $cv->begin;
146  
147 my @node = @{$node{$node}};
148 map{ $result{$_} = '' }@node;
149  
150 my %rquery = (
151 code => 'proxy',
152 argv => [ \@node, +{ query => $query, map{ $_ => $run{$_} }grep{ $run{$_} }qw( timeout max port ) } ],
153 map{ $_ => $run{query}{$_} }qw( user sudo env )
154 );
155  
156 $rquery{node} = [ $node ] if $isc;
157  
158 my $rquery = MYDan::Agent::Query->dump(\%rquery);
159
160 eval{ $rquery = MYDan::API::Agent->new()->encryption( $rquery ) if $isc };
161 if( $@ )
162 {
163 warn "ERROR:$@\n";
164 map{ $result{$_} = "norun --- 1\n" }@node;
165 return;
166 }
167  
168 tcp_connect $node, $run{port}, sub {
169 my ( $fh ) = @_;
170 unless( $fh ){
171 $cv->end;
172 $percent->add( scalar @node )->print() if $run{verbose};
173 map{ $result{$_} = "proxy $node fail tcp_connect: $!" }@node;
174 return;
175 }
176 if( $stop )
177 {
178 close $fh;
179 return;
180 }
181  
182 my $hdl;
183 push @work, \$hdl;
184 $hdl = new AnyEvent::Handle(
185 fh => $fh,
186 on_read => sub {
187 my $self = shift;
188 $self->unshift_read (
189 chunk => length $self->{rbuf},
190 sub { $rresult{$node} .= $_[1];}
191 );
192 },
193 on_eof => sub{
194 undef $hdl;
195 $percent->add(scalar @node)->print() if $run{verbose};
196 $cv->end;
197  
198 unless( $rresult{$node} )
199 {
200 map{ $result{$_} = "proxy $node result null" }@node;
201 return;
202 }
203  
204 $rresult{$node} =~ s/^\**#\*MYDan_\d+\*#//;
205 my @c = eval{ YAML::XS::Load $rresult{$node} };
206  
207 my $error = $@ ? "\$@ = $@" :
208 @c != 2 ? "\@c count != 2":
209 $c[1] != 0 ? "exit != 0" :
210 ( $c[0] && ref $c[0] eq 'HASH' ) ? undef : "no hash";
211  
212 if( $error )
213 {
214 warn "call proxy result no good: $error\n";
215 map{ $result{$_} = "proxy $node result format error" }@node;
216 return;
217 }
218  
219 map
220 {
221 $result{$_} = exists $c[0]{$_} ? $c[0]{$_} : "no any result by proxy $node";
222 }
223 @node;
224
225 return;
226 },
227  
228 on_error => sub {
229 close $fh;
230 map { $result{$_} = "no_error by proxy $node"; } @node;
231 }
232 );
233 $hdl->push_write($rquery);
234 $hdl->push_shutdown;
235 };
236 };
237  
238 #Don't change it to map
239 foreach( keys %node ) { $rwork->( $_ ); }
240  
241 my $w = AnyEvent->timer ( after => $run{timeout}, cb => $tocb );
242  
243 $cv->recv;
244 undef $w;
245  
246 if( $run{version} )
247 {
248 map{ $_ =~ s/^\**#\*MYDan_(\d+)\*#/runtime version:$1\n/;}values %result;
249 }
250 else { map{ $_ =~ s/^\**#\*MYDan_\d+\*#//;}values %result; }
251  
252 return %result;
253 }
254  
255 1;