File Coverage

blib/lib/App/Unix/RPasswd.pm
Criterion Covered Total %
statement 18 73 24.6
branch 0 22 0.0
condition 0 6 0.0
subroutine 6 10 60.0
pod 0 2 0.0
total 24 113 21.2


line stmt bran cond sub pod time code
1             package App::Unix::RPasswd;
2              
3 2     2   47140 use 5.010000;
  2         7  
  2         77  
4 2     2   11 use feature ':5.10';
  2         4  
  2         205  
5 2     2   1859 use Moo;
  2         75896  
  2         15  
6 2     2   13923 use Parallel::ForkManager;
  2         94673  
  2         71  
7 2     2   1385 use App::Unix::RPasswd::Connection;
  2         8  
  2         75  
8 2     2   1379 use App::Unix::RPasswd::SaltedPasswd;
  2         5  
  2         1980  
9              
10             our $VERSION = '0.53';
11             our $AUTHOR = 'Claudio Ramirez ';
12              
13             has 'args' => (
14             is => 'ro',
15             #isa => 'HashRef',
16             required => 1,
17             );
18              
19             has '_connection_obj' => (
20             is => 'ro',
21             does => 'App::Unix::RPasswd::Connection',
22             default => sub {
23             my $self = shift;
24             App::Unix::RPasswd::Connection->new(
25             user => $self->args->{user},
26             ssh_args => $self->args->{ssh},
27             );
28             },
29             lazy => 1,
30             init_arg => undef,
31             );
32              
33             has '_saltedpasswd_obj' => (
34             is => 'ro',
35             does => 'App::Unix::RPasswd::SaltedPasswd',
36             default => sub {
37             my $self = shift;
38             App::Unix::RPasswd::SaltedPasswd->new( salt => $self->args->{base} );
39             },
40             lazy => 1,
41             init_arg => undef,
42             );
43              
44             sub ask_key {
45 0     0 0   my ( $self, $key ) = @_;
46 0           my $ckeys = $key;
47 0           $ckeys =~ s/(\w)(.+)/\U$1\E$2s/; # key -> Keys
48 0           my @msg =
49             ( "Please introduce the $key: ", "\nPlease re-introduce the $key: " );
50 0           my $counter = 0;
51 0           my $first_time = 1;
52 0           print $msg[$counter];
53 0           my @input;
54 0           system( '/bin/stty', '-echo' );
55              
56 0           while () {
57 0           chomp;
58 0           $input[$counter] = $_;
59 0 0         if ( $counter == 1 ) {
60 0 0         if ( $input[0] eq $input[1] ) { last }
  0            
61             else {
62 0           say "\n$ckeys are not the same...";
63 0           $counter = 0;
64             }
65             }
66 0           else { $counter++; $first_time = 0; }
  0            
67              
68 0 0         print $msg[$counter] unless $first_time;
69             }
70 0           system '/bin/stty echo';
71 0           say '';
72 0           return $input[0];
73             }
74              
75             sub pexec {
76 0     0 0   my $self = shift;
77 0           my @servers = @_;
78 0           state $run++;
79 0           my @errors;
80              
81             # Setup forks
82 0           my $pfm = Parallel::ForkManager->new( $self->args->{sessions} );
83              
84             # Retrieve status
85             $pfm->run_on_finish(
86             sub {
87 0     0     my ( undef, undef, undef, undef, undef, $dataref ) = @_;
88 0 0 0       if ( defined $dataref and defined $$dataref ) {
89 0           push @errors, $$dataref;
90             }
91             }
92 0           );
93              
94 0           for my $server (@servers) {
95 0 0         if (@servers) {
96 0 0         $pfm->start() and next;
97 0           my $error;
98 0 0         if ( defined $self->args->{base} ) {
99 0           $self->args->{password} =
100             $self->_saltedpasswd_obj->generate(
101             $server . $self->args->{date} . $server );
102             }
103              
104 0 0         if ( $self->args->{generate_only} ) {
105 0           say $self->args->{password} . " ($server)";
106             }
107             else {
108 0 0         my $msg =
109             ( $self->args->{debug} )
110             ? 'Running on '
111             . $server
112             . ' with password \''
113             . $self->args->{password} . '\'...'
114             : "Running on $server...";
115 0           say $msg;
116 0           my $status = eval {
117             local $SIG{ALRM} = sub {
118 0     0     say "Timeout exceeded for $server.";
119 0           die "Timeout exceeded\n"; # NB: \n required
120 0           };
121 0           alarm $self->args->{timeout};
122 0           return $self->_connection_obj->run(
123             $server,
124             $self->args->{password},
125             $self->args->{debug}
126             );
127 0           alarm 0;
128             };
129 0 0         if ( !$status ) { $error = $server; }
  0            
130             }
131 0           $pfm->finish( 0, \$error );
132             }
133             }
134 0           $pfm->wait_all_children;
135 0           say "\nRun $run done.\n";
136 0 0 0       return ( scalar @errors and $run <= $self->args->{reruns} )
137             ? $self->pexec(@errors)
138             : @errors; # recursive
139             }
140              
141             1;
142             __END__