lib/Sys/Bprsync/Cmd/Command/configcheck.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.0% 23 / 25 CC 1 SCAR 0.4
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Sys/Bprsync/Job.pm ↑ Summary lib/Sys/Bprsync/Cmd/Command/run.pm »
1 package Sys::Bprsync::Cmd::Command::configcheck;
2 {
3 $Sys::Bprsync::Cmd::Command::configcheck::VERSION = '0.25';
4 }
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $Sys::Bprsync::Cmd::Command::configcheck::AUTHORITY = 'cpan:TEX';
7 }
8 # ABSTRACT: verify the bprsync configuration
9  
10 1 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # use IO::Handle;
18 # use autodie;
19 # use MooseX::Params::Validate;
20 # use Carp;
21 # use English qw( -no_match_vars );
22 # use Try::Tiny;
23 0 ▶use Sys::Bprsync;
Subroutine BEGIN: called CC=1 SCAR=2.6
24  
25 # extends ...
26 extends 'Sys::Bprsync::Cmd::Command';
27 # has ...
28 has '_bp' => (
29 'is' => 'ro',
30 'isa' => 'Sys::Bprsync',
31 'lazy' => 1,
32 'builder' => '_init_bp',
33 'reader' => 'bp',
34 );
35  
36 has 'verbose' => (
37 'is' => 'ro',
38 'isa' => 'Bool',
39 'required' => 0,
40 'traits' => [qw(Getopt)],
41 'cmd_aliases' => 'v',
42 'documentation' => 'Be more verbose',
43 );
44 # with ...
45 # initializers ...
46 sub _init_bp {
47 my $self = shift;
48  
49 my $BP = Sys::Bprsync::->new({
50 'config' => $self->config(),
51 'logger' => $self->logger(),
52 'logfile' => $self->config()->get( 'Sys::Bprsync::Logfile', { Default => '/tmp/bprsync.log', } ),
53 'concurrency' => 1,
54 });
55  
56 return $BP;
57 }
58  
59 # your code here ...
60 sub execute {
61 my $self = shift;
62  
63 my $status = 1;
64 # do we have at least one job?
65 my $jobs = $self->bp()->vaults();
66 if($jobs && ref($jobs) eq 'ARRAY') {
67 # check each vault if it is accessible
68 foreach my $job_name (sort @{$jobs}) {
69 my $job = $self->bp()->config()->get($self->bp()->config_prefix().'::Jobs::'.$job_name);
70 say 'Job: '.$job_name;
71 if($self->verbose()) {
72 say "\t".$job->{'source'}.' => '.$job->{'destination'};
73 say "\tBwlimit: ".$job->{'bwlimit'} if $job->{'bwlimit'};
74 say "\tCompression: ".$job->{'compression'} if $job->{'compression'};
75 say "\tDelete: ".$job->{'delete'} if $job->{'delete'};
76 say "\tDescription: ".$job->{'description'} if $job->{'description'};
77 say "\tExclude: ".$job->{'exclude'} if $job->{'exclude'};
78 say "\tExcludefrom: ".$job->{'excludefrom'} if $job->{'excludefrom'};
79 say "\tHardlink: ".$job->{'hardlink'} if $job->{'hardlink'};
80 say "\tWill cross FS boundaries: ".($job->{'nocrossfs'} ? 'No' : 'Yes');
81 say "\tNumeric-IDs: ".$job->{'numericids'} if $job->{'numericids'};
82 say "\tRemote-Shell: ".$job->{'rsh'} if $job->{'rsh'};
83 say "\tRemote-Shell Options: ".$job->{'rshopts'} if $job->{'rshopts'};
84 say "\tTimeframe: ".$job->{'timeframe'} if $job->{'timeframe'};
85 }
86  
87 # check access ...
88 ## for local dirs: check existence and rw
89 ## for remotes: check ssh access
90 # source
91 if($job->{'source'} =~ m/:/) {
92 # remote
93 if(!$self->_check_remote($job->{'source'},$job->{'rshopts'})) {
94 $status = 0;
95 }
96 } else {
97 # local
98 if(!$self->_check_local($job->{'source'})) {
99 $status = 0;
100 }
101 }
102 # destination
103 if($job->{'destination'} =~ m/:/) {
104 # remote
105 if(!$self->_check_remote($job->{'destination'},$job->{'rshopts'})) {
106 $status = 0;
107 }
108 } else {
109 # local
110 if(!$self->_check_local($job->{'destination'})) {
111 $status = 0;
112 }
113 }
114 }
115 } else {
116 my @files_read = @{$self->bp()->config()->files_read()};
117 if(@files_read) {
118 say 'ERROR - No jobs defined in: '.join(q{:},@files_read);
119 } else {
120 say 'ERROR - No config files found in '.join(q{:},@{$self->bp()->config()->locations()});
121 }
122 $status = 0;
123 }
124  
125 return $status;
126 }
127  
128 sub _check_remote {
129 my $self = shift;
130 my $location = shift;
131 my $rshopts = shift || '';
132  
133 my ($host, $path) = split /:/, $location, 2;
134 if($self->bp()->sys()->check_ssh_login($host, { SSHOpts => $rshopts, })) {
135 say ' OK - SSH access to '.$host.' works';
136 if($self->bp()->sys()->run_remote_cmd($host,'ls '.$path,, { SSHOpts => $rshopts, })) {
137 say ' OK - Remote location '.$path.' is a directory';
138 return 1;
139 } else {
140 say ' ERROR - Remote location '.$path.' not found on host '.$host;
141 return;
142 }
143 } else {
144 say ' ERROR - SSH access to '.$host.' failed!';
145 return;
146 }
147 }
148  
149 sub _check_local {
150 my $self = shift;
151 my $location = shift;
152  
153 if(-d $location) {
154 say ' OK - Location '.$location.' is a directory';
155 if(-w $location) {
156 say ' OK - Location '.$location.' is writeable';
157 return 1;
158 }
159 } else {
160 say ' ERROR - Location '.$location.' is no directory';
161 return;
162 }
163 }
164  
165 sub abstract {
166 return 'Check integrity of the configuration';
167 }
168  
169 no Moose;
170 __PACKAGE__->meta->make_immutable;
171  
172 1;
173  
174 __END__
« lib/Sys/Bprsync/Job.pm ↑ Summary lib/Sys/Bprsync/Cmd/Command/run.pm »