blib/lib/Ereshkigal/App/Command.pm

filter: Statementstmt 89.3% 59 / 66 Branchbran 62.5% 15 / 24 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 75.6% 90 / 119 CC 20 SCAR 30.4
CC
20
Cov
76%
_result_has_failures
41.2
run_command
4.0
_help_text
2.0
CRAP 21.0
« blib/lib/Ereshkigal/LogDrek.pm ↑ Summary blib/lib/Ereshkigal/IP.pm »
1 package Ereshkigal::App::Command;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use parent 'App::Cmd::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Ereshkigal::Client ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Ereshkigal::App::Command - Base class for the ereshkigal subcommands.
13  
14 =head1 VERSION
15  
16 Version 0.0.1
17  
18 =cut
19  
20 2 our $VERSION = '0.0.1';
21  
22 =head1 DESCRIPTION
23  
24 Base class for the Ereshkigal::App::Command modules, providing the shared
25 talk-to-the-manager plumbing.
26  
27 Commands going through run_command exit with...
28  
29 0 - clean success
30 1 - transport or server error, printed to STDERR
31 2 - the command completed, but the result carries per-kur,
32 per-IP, or per-CIDR failures
33  
34 =head1 METHODS
35  
36 =head2 client
37  
38 Returns a new L<Ereshkigal::Client> for the socket named by the global
39 --socket option.
40  
41 my $client = $self->client;
42  
43 =cut
44  
45 34 ▶sub client {
Subroutine client: called CC=1 SCAR=0.0
Pod documented
46 34 my ($self) = @_;
47  
48 34 return Ereshkigal::Client->new( 'socket' => $self->app->global_options->{socket} );
49 }
50  
51 =head2 run_command
52  
53 Sends the command to the manager and prints the result to STDOUT as pretty
54 JSON, exiting per the codes above.
55  
56 $self->run_command( $command, $command_args );
57  
58 =cut
59  
60 34 ▶sub run_command {
Subroutine run_command: called CC=4 SCAR=13.9
Pod documented
61 34 my ( $self, $command, $command_args ) = @_;
62  
63 34 my $result;
64 34 ▶ eval { $result = $self->client->call_ok( $command, defined($command_args) ? $command_args : () ); };
Branch: 1 branch
BranchTrueFalseTotal
defined $command_args ? : 23 11 34
65 34 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 11 23 34
66 11 my $error = $@;
67 11 $error =~ s/\n*$//;
68 11 print STDERR $error . "\n";
69 11 exit 1;
70 }
71  
72 23 print JSON::MaybeXS->new( 'pretty' => 1, 'canonical' => 1 )->encode($result);
73  
74 23 ▶ if ( _result_has_failures($result) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_result_has_failures($result)) 0 23 23
75 0 exit 2;
76 }
77  
78 23 return;
79 } ## end sub run_command
80  
81 # Decides whether a result that came back ok at the protocol level is
82 # nonetheless carrying failures inside it, which is what run_command turns
83 # into the exit 2 documented above. A ban fanned out to five kurs can
84 # succeed as a request while failing on two of them, and a script driving
85 # the CLI wants to know that without parsing the JSON... this is what lets
86 # the exit code say so. Called as a plain function rather than a method, as
87 # it needs nothing from the invocant.
88 #
89 # Three kinds of failure marker are looked for, and any one of them is
90 # enough... a non empty rejected hash, a kur carrying an error, or a per
91 # entry status of error. Both result shapes are handled, the manager's
92 # fanned out one with everything under kurs and the single kur one with
93 # ips/cidrs at the top level, that second being what comes back when -s is
94 # pointed straight at a kur socket.
95 #
96 # Args, one, positional and required...
97 #
98 # $result :: Whatever call_ok returned, which is the result value of the
99 # server's response. Normally a hash ref, but anything else,
100 # undef and array refs included, is answered 0 rather than
101 # dying, as plenty of commands answer with a shape carrying
102 # no per item status at all. The keys looked at, all
103 # optional, are 'rejected', a hash ref keyed by the raw entry
104 # the manager refused to validate, which being non empty
105 # means something was bounced before it ever reached a kur;
106 # 'kurs', a hash ref of kur name to that kur's own result,
107 # where a value carrying a 'error' key is a kur that could
108 # not be reached or refused the command; and 'ips' and
109 # 'cidrs', hash refs of entry to a hash ref carrying a
110 # 'status' of 'ok' or 'error', looked for both at the top
111 # level and inside each value of kurs.
112 #
113 # Returns 1 if any failure marker was found and 0 otherwise, nothing more...
114 # it does not say which failed or how many, only whether the exit code
115 # should be 2. Never dies, whatever it is handed.
116 #
117 # exit 2 if _result_has_failures($result);
118 #
119 # # a fanned out ban where one kur is down
120 # _result_has_failures(
121 # {
122 # 'kurs' => {
123 # 'sshd' => { 'ips' => { '1.2.3.4' => { 'status' => 'ok' } } },
124 # 'smtp' => { 'error' => 'not running' },
125 # }
126 # }
127 # );
128 # # 1, for the smtp error
129 #
130 # # an IP the manager would not validate
131 # _result_has_failures(
132 # { 'rejected' => { 'nope' => { 'status' => 'error' } } }
133 # );
134 # # 1
135 23 ▶sub _result_has_failures {
Subroutine _result_has_failures: called CC=15 SCAR=37.2
136 23 my ($result) = @_;
137  
138 23 ▶ return 0 if ref($result) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $result ne "HASH" 0 23 23
139  
140 0 ▶ if ( ref( $result->{rejected} ) eq 'HASH' && %{ $result->{rejected} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result->{"rejected"} eq "HASH" and %{$result->{"rejected"};}) 0 23 23
Condition33%1/3
ref $result->{"rejected"} eq "HASH" and %{$result->{"rejected"};}
!ll&&!rl&&r
2300
MC/DC0%0/2
ref $result->{"rejected"} eq "HASH" and %{$result->{"rejected"};}
ref $result->{"rejected"} eq "HASH" %{$result->{"rejected"};}
Truth table33%1/3
A and B
A= ref $result->{"rejected"} eq "HASH"B= %{$result->{"rejected"};}
ABresult
0X0
100
111
141 0 return 1;
142 }
143  
144 23 my @to_scan = ($result);
145 23 ▶ if ( ref( $result->{kurs} ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result->{"kurs"} eq "HASH") 2 21 23
146 2 foreach my $kur_result ( values( %{ $result->{kurs} } ) ) {
147 1 ▶ next if ref($kur_result) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $kur_result ne "HASH" 0 1 1
148 1 ▶ return 1 if defined( $kur_result->{error} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $kur_result->{"error"} 0 1 1
149 1 push( @to_scan, $kur_result );
150 }
151 }
152  
153 23 foreach my $to_scan (@to_scan) {
154 24 foreach my $entries_key ( 'ips', 'cidrs' ) {
155 48 ▶ next if ref( $to_scan->{$entries_key} ) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $to_scan->{$entries_key} ne "HASH" 48 0 48
156 0 foreach my $entry ( values( %{ $to_scan->{$entries_key} } ) ) {
157 0 ▶ if ( ref($entry) eq 'HASH' && defined( $entry->{status} ) && $entry->{status} eq 'error' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entry eq "HASH" and defined $entry->{"status"} and $entry->{"status"} eq "error") 0 0 0
Condition0%0/6
ref $entry eq "HASH" and defined $entry->{"status"} and $entry->{"status"} eq "error"
!ll&&!rl&&r
000
ref $entry eq "HASH" and defined $entry->{"status"}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $entry eq "HASH" and defined $entry->{"status"} and $entry->{"status"} eq "error"
ref $entry eq "HASH" defined $entry->{"status"} $entry->{"status"} eq "error"
Truth table0%0/4
A and B and C
A= ref $entry eq "HASH"B= defined $entry->{"status"}C= $entry->{"status"} eq "error"
ABCresult
0XX0
10X0
1100
1111
158 0 return 1;
159 }
160 }
161 }
162 } ## end foreach my $to_scan (@to_scan)
163  
164 23 return 0;
165 } ## end sub _result_has_failures
166  
167 # Overrides the App::Cmd::Command hook of the same name so every subcommand
168 # gains a --help/-h flag. App::Cmd wires up a help command but no such flag,
169 # so without this each subcommand answers "Unknown option: help" to the
170 # first thing most people reach for. Doing it here means the thirteen
171 # subcommands do not each have to remember to put it in their own opt_spec,
172 # and cannot drift from one another over it.
173 #
174 # The parent returns the usage_desc string followed by the opt_spec entries,
175 # which App::Cmd hands to _process_args and on to
176 # Getopt::Long::Descriptive. This returns that same list with one more
177 # option spec appended.
178 #
179 # Args, one, a list, forwarded verbatim...
180 #
181 # @args :: Whatever App::Cmd is passing through for this subcommand, in
182 # practice the Ereshkigal::App instance and nothing else. It is
183 # handed on unexamined to usage_desc and opt_spec, both called
184 # as class methods, so anything those accept is acceptable
185 # here.
186 #
187 # Returns a list, not a ref... the usage_desc string first, then each
188 # opt_spec entry as its own array ref of [ spec, description ] or
189 # [ spec, description, \%opts ], then [ 'help|h', 'show this help and
190 # exit' ] last. Last so it renders at the bottom of the options listing,
191 # under the subcommand's own options, rather than in amongst them.
192 #
193 # my @params
194 # = Ereshkigal::App::Command::ban->_option_processing_params($app);
195 # # ( '%c ban %o <IP> [<IP> ...]',
196 # # [ 'kur=s', 'kur instance to send the ban to, ...' ],
197 # # [ 'ban-time=i', 'seconds the bans should last, ...' ],
198 # # [ 'help|h', 'show this help and exit' ] )
199 111 ▶sub _option_processing_params {
Subroutine _option_processing_params: called CC=1 SCAR=0.0
200 111 my ( $class, @args ) = @_;
201  
202 111 return ( $class->usage_desc(@args), $class->opt_spec(@args), [ 'help|h', 'show this help and exit' ] );
203 }
204  
205 # Overrides the App::Cmd::Command hook that turns a command line into a
206 # constructed command object, so the --help added by
207 # _option_processing_params actually does something. Handled here rather
208 # than in each subcommand's validate_args because prepare runs first...
209 # "ban --help" should explain ban, not refuse for want of an IP, and it only
210 # behaves that way because the help is answered before validate_args ever
211 # sees the empty arg list. It is public rather than underscored only
212 # because App::Cmd names the hook, and it carries no POD for the same
213 # reason its siblings do not... it is a framework callback, not an API
214 # anyone here calls.
215 #
216 # The parent is left to do all the real work, parsing the options and
217 # building the object. All this adds is the check afterwards.
218 #
219 # Args, both positional and required, both forwarded to the parent
220 # untouched...
221 #
222 # $app :: The Ereshkigal::App instance the subcommand is running
223 # under, which the parent stores on the constructed object so
224 # things like global_options can be reached from it.
225 # @args :: The rest of the command line for this subcommand, options
226 # and positionals both, still unparsed. The parent hands it to
227 # Getopt::Long::Descriptive, so an unknown option in here is
228 # what dies with "Unknown option".
229 #
230 # Returns the parent's three part list unchanged... the constructed
231 # subcommand object, the Getopt::Long::Descriptive opts object, and then the
232 # leftover positional args as a plain list. App::Cmd feeds those straight
233 # into validate_args and execute.
234 #
235 # Or does not return at all. If --help or -h was given it prints the help
236 # screen to STDOUT and exits 0, so nothing downstream runs... no
237 # validate_args, no execute, and no connection to the manager.
238 #
239 # my ( $cmd, $opt, @args )
240 # = $class->prepare( $app, '--kur', 'sshd', '1.2.3.4' );
241 # # $cmd is an Ereshkigal::App::Command::ban, $opt->kur is 'sshd',
242 # # and @args is ( '1.2.3.4' )
243 #
244 # $class->prepare( $app, '--help' );
245 # # prints the help screen and exits 0, never returning
246 111 ▶sub prepare {
Subroutine prepare: called CC=2 SCAR=6.9
Pod documented
247 111 my ( $class, $app, @args ) = @_;
248  
249 111 my ( $self, $opt, @rest ) = $class->SUPER::prepare( $app, @args );
250  
251 111 ▶ if ( $opt->{help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"help"}) 42 69 111
252 42 print $self->_help_text;
253 42 exit 0;
254 }
255  
256 69 return ( $self, $opt, @rest );
257 } ## end sub prepare
258  
259 # Renders the help screen prepare prints for --help, assembled the same way
260 # and out of the same three pieces App::Cmd's own help command assembles
261 # its own. That sameness is the whole point... "ereshkigal help ban" and
262 # "ereshkigal ban --help" have to agree, and they only reliably do so by
263 # being built identically rather than by two bits of code being kept in step
264 # by hand.
265 #
266 # The three pieces are the usage leader line, the subcommand's description,
267 # and the option listing, joined with newlines. The description gets a
268 # leading newline of its own so it sits in a paragraph of its own under
269 # the usage line, but only when there is one... a subcommand with an empty
270 # description gets no stray blank line for it.
271 #
272 # Takes no arguments beyond the invocant, which must be a constructed
273 # subcommand object rather than a class... it reads description off it and
274 # needs the usage object the parent's prepare stored on it, so calling this
275 # on a class name dies for want of that.
276 #
277 # Returns the whole screen as one string, newline terminated and ready to
278 # hand straight to print. Nothing is written anywhere from here... printing
279 # is prepare's business.
280 #
281 # print $self->_help_text;
282 # # ereshkigal ban [-h] [long options...] <IP> [<IP> ...]
283 # #
284 # # Banish one or more IPs to the underworld.
285 # # ...
286 # # --kur STR kur instance to send the ban to, ...
287 # # --help (or -h) show this help and exit
288 42 ▶sub _help_text {
Subroutine _help_text: called CC=2 SCAR=7.0
289 42 my ($self) = @_;
290  
291 42 my $description = $self->description;
292 42 ▶ $description = "\n" . $description if ( length($description) );
Branch: 1 branch
BranchTrueFalseTotal
if length $description 42 0 42
293  
294 42 return join( "\n", $self->usage->leader_text, $description, $self->usage->option_text ) . "\n";
295 }
296  
297 2 1;
« blib/lib/Ereshkigal/LogDrek.pm ↑ Summary blib/lib/Ereshkigal/IP.pm »