| 1 | # | ||||||||||
| 2 | # This file is part of StorageDisplay | ||||||||||
| 3 | # | ||||||||||
| 4 | # This software is copyright (c) 2014-2023 by Vincent Danjean. | ||||||||||
| 5 | # | ||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||
| 8 | # | ||||||||||
| 9 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 7 | ▶ | use 5.14.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | # in order to load the StorageDisplay::Collect::CMD module | ||||||||||
| 14 | 7 | ▶ | use StorageDisplay::Collect; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | package StorageDisplay::Collect::CMD::Replay; | ||||||||||
| 17 | # ABSTRACT: Use cached command output to collect data | ||||||||||
| 18 | 7 | our $VERSION = '2.06'; # VERSION | |||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | 7 | ▶ | use parent -norequire => "StorageDisplay::Collect::CMD"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 7 | ▶ | use Scalar::Util 'blessed'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 7 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 7 | ▶ | use Data::Compare; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | 7 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 27 | 7 | my $class = shift; | |||||||||
| 28 | 7 | my %args = ( @_ ); | |||||||||
| 29 | 7 | ▶ | if (not exists($args{'replay-data'})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 0 | die 'replay-data argument required'; | |||||||||
| 31 | } | ||||||||||
| 32 | 7 | my $self = $class->SUPER::new(@_); | |||||||||
| 33 | 7 | $self->{'_attr_replay_data'} = $args{'replay-data'}; | |||||||||
| 34 | 7 | $self->{'_attr_replay_data_nextid'}=0; | |||||||||
| 35 | 7 | return $self; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 451 | ▶ | sub _replay { | ||||||||
|
Subroutine
_replay: called CC=6 SCAR=22.6
| |||||||||||
| 39 | 451 | my $self = shift; | |||||||||
| 40 | 451 | my $args = shift; | |||||||||
| 41 | 451 | my $ignore_keys = shift; | |||||||||
| 42 | 451 | my $msgerr = shift; | |||||||||
| 43 | |||||||||||
| 44 | 451 | my $entry = $self->{'_attr_replay_data'}->[$self->{'_attr_replay_data_nextid'}++]; | |||||||||
| 45 | 451 | ▶ | if (not defined($entry)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 0 | print STDERR "E: no record for $msgerr\n"; | |||||||||
| 47 | 0 | die "No records anymore\n"; | |||||||||
| 48 | } | ||||||||||
| 49 | 451 | foreach my $k (keys %{$args}) { | |||||||||
| 50 | 895 | ▶ | if (not exists($entry->{$k})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | print STDERR "E: no record for $msgerr\n"; | |||||||||
| 52 | 0 | die "Missing '$k' in record:\n".Data::Dumper->Dump([$entry], ['record'])."\n"; | |||||||||
| 53 | } | ||||||||||
| 54 | } | ||||||||||
| 55 | 451 | ▶ | if (! Compare($entry, $args, { ignore_hash_keys => $ignore_keys })) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | print STDERR "E: record for different arguments\n"; | |||||||||
| 57 | 0 | foreach my $k (@{$ignore_keys}) { | |||||||||
| 58 | 0 | delete($entry->{$k}); | |||||||||
| 59 | } | ||||||||||
| 60 | 0 | die "Bad record:\n". | |||||||||
| 61 | Data::Dumper->Dump([$args, $entry], ['requested', 'recorded'])."\n"; | ||||||||||
| 62 | } | ||||||||||
| 63 | 451 | return $entry; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 444 | ▶ | sub _replay_cmd { | ||||||||
|
Subroutine
_replay_cmd: called CC=4 SCAR=13.9
| |||||||||||
| 67 | 444 | my $self = shift; | |||||||||
| 68 | 444 | my $args = { @_ }; | |||||||||
| 69 | my $cmd = $self->_replay( | ||||||||||
| 70 | $args, | ||||||||||
| 71 | ['stdout', 'root'], | ||||||||||
| 72 | 444 | "command ".$self->cmd2str(@{$args->{'cmd'}}), | |||||||||
| 73 | ); | ||||||||||
| 74 | 444 | my $cmdrequested = $self->cmd2str(@{$args->{'cmd'}}); | |||||||||
| 75 | 444 | ▶ | if ($args->{'root'} != $cmd->{'root'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 0 | print STDERR "W: Root mode different for $cmdrequested\n"; | |||||||||
| 77 | } | ||||||||||
| 78 | 444 | ▶ | print STDERR "Replaying".($cmd->{'root'}?' (as root)':'') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | .": ", $cmdrequested, "\n"; | ||||||||||
| 80 | 444 | my @infos = @{$cmd->{'stdout'}}; | |||||||||
| 81 | 444 | my $infos = join("\n", @infos); | |||||||||
| 82 | 444 | ▶ | if (scalar(@infos)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | # will add final endline | ||||||||||
| 84 | 416 | $infos .= "\n"; | |||||||||
| 85 | } | ||||||||||
| 86 | 444 | open(my $fh, "<", \$infos); | |||||||||
| 87 | 444 | return $fh; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 112 | ▶ | sub open_cmd_pipe { | ||||||||
|
Subroutine
open_cmd_pipe: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 91 | 112 | my $self = shift; | |||||||||
| 92 | 112 | return $self->_replay_cmd( | |||||||||
| 93 | 'root' => 0, | ||||||||||
| 94 | 'cmd' => [ @_ ], | ||||||||||
| 95 | ); | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 332 | ▶ | sub open_cmd_pipe_root { | ||||||||
|
Subroutine
open_cmd_pipe_root: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 99 | 332 | my $self = shift; | |||||||||
| 100 | 332 | return $self->_replay_cmd( | |||||||||
| 101 | 'root' => 1, | ||||||||||
| 102 | 'cmd' => [ @_ ], | ||||||||||
| 103 | ); | ||||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 7 | ▶ | sub has_file { | ||||||||
|
Subroutine
has_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 107 | 7 | my $self = shift; | |||||||||
| 108 | 7 | my $filename = shift; | |||||||||
| 109 | 7 | my $fileaccess = $self->_replay( | |||||||||
| 110 | { | ||||||||||
| 111 | 'filename' => $filename, | ||||||||||
| 112 | }, | ||||||||||
| 113 | [ 'value' ], | ||||||||||
| 114 | "file access check to '$filename'"); | ||||||||||
| 115 | 7 | return $fileaccess->{'value'}; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 7 | 1; | |||||||||
| 119 | |||||||||||
| 120 | __END__ | ||||||||||