JobControl/Helper/Project/Rexfile.pm

filter: Statementstmt 18.8% 29 / 154 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 28.1% 9 / 32 Podpod 0.0% 0 / 16 totaltotal 15.4% 38 / 246 CC 23 SCAR 40.4
CC
23
Cov
15%
execute
210.0
new
6.0
load
6.0
CRAP 57.1
« JobControl/Helper/Project/Job.pm ↑ Summary JobControl/Rexfile.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4 # vim: set ts=2 sw=2 tw=0:
5 # vim: set expandtab:
6  
7 package Rex::JobControl::Helper::Project::Rexfile;
8 1 $Rex::JobControl::Helper::Project::Rexfile::VERSION = '0.18.0';
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Capture::Tiny qw'capture';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use Rex::JobControl::Helper::Chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
21 0 my $that = shift;
22 0 ▶ my $proto = ref($that) || $that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
23 0 my $self = {@_};
24  
25 0 bless( $self, $proto );
26  
27 0 $self->load;
28  
29 0 return $self;
30 }
31  
32 0 ▶sub load {
Subroutine load: not called CC=2 SCAR=17.9
Pod undocumented
33 0 my ($self) = @_;
34  
35 0 ▶ if ( -f $self->_config_file() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->_config_file) 0 0 0
36 0 $self->{rex_configuration} = YAML::LoadFile( $self->_config_file );
37 }
38 }
39  
40 0 ▶sub project { (shift)->{project} }
Subroutine project: not called CC=1 SCAR=6.9
Pod undocumented
41 0 ▶sub name { (shift)->{rex_configuration}->{name} }
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
42 0 ▶sub url { (shift)->{rex_configuration}->{url} }
Subroutine url: not called CC=1 SCAR=6.9
Pod undocumented
43 0 ▶sub description { (shift)->{rex_configuration}->{description} }
Subroutine description: not called CC=1 SCAR=6.9
Pod undocumented
44 0 ▶sub groups { (shift)->{rex_configuration}->{rex}->{groups} }
Subroutine groups: not called CC=1 SCAR=6.9
Pod undocumented
45 0 ▶sub directory { (shift)->{directory} }
Subroutine directory: not called CC=1 SCAR=6.9
Pod undocumented
46 0 ▶sub rexfile { (shift)->{rex_configuration}->{rexfile} }
Subroutine rexfile: not called CC=1 SCAR=6.9
Pod undocumented
47  
48 0 ▶sub _config_file {
Subroutine _config_file: not called CC=1 SCAR=6.9
49 0 my ($self) = @_;
50 return File::Spec->catfile( $self->project->project_path(),
51 0 "rex", $self->{directory}, "rex.conf.yml" );
52 }
53  
54 0 ▶sub create {
Subroutine create: not called CC=1 SCAR=6.9
Pod undocumented
55 0 my ( $self, %data ) = @_;
56  
57 my $rex_path = File::Spec->catdir( $self->project->project_path,
58 0 "rex", $self->{directory} );
59  
60 0 $self->project->app->log->debug(
61 "Creating new Rexfile $self->{directory} in $rex_path.");
62  
63 0 File::Path::make_path($rex_path);
64  
65 0 my $rexfile = basename( $self->{url} );
66 0 $rexfile =~ s/(\.git|\.tar\.gz)$//;
67  
68 0 my $url = $self->{url};
69 0 ▶ chwd "$rex_path", sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
70 0 my $rexify_cmd = $self->project->app->config->{rexify};
71 0 my @out = `$rexify_cmd --init=$url 2>&1`;
72 0 chomp @out;
73  
74 0 $self->project->app->log->debug("Output of rexify --init=$url");
75 0 for my $l (@out) {
76 0 $self->project->app->log->debug("rexfile: $l");
77 }
78 0 };
79  
80 0 my @tasks;
81 my $rex_info;
82  
83 0 ▶ chwd "$rex_path/$rexfile", sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
84 0 my $rex_cmd = $self->project->app->config->{rex};
85 0 my $out = `$rex_cmd -Ty 2>&1`;
86 0 ▶ eval { $rex_info = YAML::Load($out); } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $rex_info = YAML::Load($out) } } or do { $self->project->app->log->error("Error reading Rexfile information."); $self->project->app->log->error("$out"); $self->project->app->log->error("Please try to run rex -Ty on the Rexfile to see the error.") } 0 0 0
87 0 $self->project->app->log->error("Error reading Rexfile information.");
88 0 $self->project->app->log->error("$out");
89 0 $self->project->app->log->error(
90 "Please try to run rex -Ty on the Rexfile to see the error.");
91 };
92 0 };
93  
94 0 delete $data{directory};
95  
96 0 my $rex_configuration = {
97 %data,
98 rexfile => $rexfile,
99 rex => $rex_info,
100 };
101  
102 0 YAML::DumpFile( "$rex_path/rex.conf.yml", $rex_configuration );
103 }
104  
105 0 ▶sub tasks {
Subroutine tasks: not called CC=2 SCAR=17.9
Pod undocumented
106 0 my ($self) = @_;
107 0 ▶ return ( $self->{rex_configuration}->{rex}->{tasks} || [] );
Condition0%0/2
$self->{'rex_configuration'}{'rex'}{'tasks'} || []
l!l
00
MC/DC0%0/1
$self->{'rex_configuration'}{'rex'}{'tasks'} || []
$self->{'rex_configuration'}{'rex'}{'tasks'}
Truth table0%0/2
A
A= $self->{'rex_configuration'}{'rex'}{'tasks'}
Aresult
11
00
108 }
109  
110 0 ▶sub environments {
Subroutine environments: not called CC=2 SCAR=17.9
Pod undocumented
111 0 my ($self) = @_;
112 0 ▶ return ( $self->{rex_configuration}->{rex}->{envs} || [] );
Condition0%0/2
$self->{'rex_configuration'}{'rex'}{'envs'} || []
l!l
00
MC/DC0%0/1
$self->{'rex_configuration'}{'rex'}{'envs'} || []
$self->{'rex_configuration'}{'rex'}{'envs'}
Truth table0%0/2
A
A= $self->{'rex_configuration'}{'rex'}{'envs'}
Aresult
11
00
113 }
114  
115 0 ▶sub all_server {
Subroutine all_server: not called CC=2 SCAR=17.9
Pod undocumented
116 0 my ($self) = @_;
117  
118 0 my @all_server;
119  
120 0 for my $group ( keys %{ $self->groups } ) {
121 push @all_server,
122 0 ( map { $_ = { name => $_->{name}, group => $group, %{$_} } }
123 0 @{ $self->groups->{$group} } );
124 }
125  
126 0 return \@all_server;
127 }
128  
129 0 ▶sub reload {
Subroutine reload: not called CC=1 SCAR=6.9
Pod undocumented
130 0 my ($self) = @_;
131  
132 my $rex_path = File::Spec->catdir( $self->project->project_path,
133 0 "rex", $self->{directory} );
134  
135 0 my $rexfile = $self->rexfile;
136 0 my $url = $self->url;
137  
138 0 ▶ chwd "$rex_path", sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
139 0 my $rexify_cmd = $self->project->app->config->{rexify};
140 0 my @out = `$rexify_cmd --init=$url 2>&1`;
141 0 chomp @out;
142  
143 0 $self->project->app->log->debug("Output of rexify --init=$url");
144 0 for my $l (@out) {
145 0 $self->project->app->log->debug("rexfile: $l");
146 }
147 0 };
148  
149 0 my @tasks;
150 my $rex_info;
151  
152 0 ▶ chwd "$rex_path/$rexfile", sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 0 my $rex_cmd = $self->project->app->config->{rex};
154 0 my $out = `$rex_cmd -Ty`;
155 0 $rex_info = YAML::Load($out);
156 0 };
157  
158 0 my $rex_configuration = {
159 name => $self->name,
160 url => $url,
161 rexfile => $rexfile,
162 rex => $rex_info,
163 };
164  
165 0 YAML::DumpFile( "$rex_path/rex.conf.yml", $rex_configuration );
166  
167 }
168  
169 0 ▶sub remove {
Subroutine remove: not called CC=1 SCAR=6.9
Pod undocumented
170 0 my ($self) = @_;
171 my $rexfile_path = File::Spec->catdir( $self->project->project_path,
172 0 "rex", $self->{directory} );
173  
174 0 File::Path::remove_tree($rexfile_path);
175 }
176  
177 0 ▶sub execute {
Subroutine execute: not called CC=14 SCAR=53.5
Pod undocumented
178 0 my ( $self, %option ) = @_;
179  
180 0 my $task = $option{task};
181 0 my $job = $option{job};
182 0 my @server = @{ $option{server} };
183 0 my $cmdb = $option{cmdb};
184  
185 0 ▶ if ( scalar @server == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @server == 0) 0 0 0
186 0 @server = ("<local>");
187 }
188  
189 my $rex_path = File::Spec->catdir( $self->project->project_path,
190 0 "rex", $self->{directory}, $self->rexfile );
191  
192 0 $self->project->app->log->debug("rex_path: $rex_path");
193  
194 0 my @ret;
195  
196 0 my $all_server = $self->project->all_server;
197  
198 0 for my $srv (@server) {
199  
200 0 my ($srv_object) = grep { $_->{name} eq $srv } @{$all_server};
201  
202 0 ▶ if ( exists $srv_object->{auth} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $srv_object->{"auth"}) 0 0 0
203 0 ▶ if ( exists $srv_object->{auth}->{auth_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $srv_object->{"auth"}{"auth_type"}) 0 0 0
204 0 $ENV{REX_AUTH_TYPE} = $srv_object->{auth}->{auth_type};
205 }
206  
207 0 ▶ if ( exists $srv_object->{auth}->{public_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $srv_object->{"auth"}{"public_key"}) 0 0 0
208 0 $ENV{REX_PUBLIC_KEY} = $srv_object->{auth}->{public_key};
209 }
210  
211 0 ▶ if ( exists $srv_object->{auth}->{private_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $srv_object->{"auth"}{"private_key"}) 0 0 0
212 0 $ENV{REX_PRIVATE_KEY} = $srv_object->{auth}->{private_key};
213 }
214  
215 0 ▶ if ( exists $srv_object->{auth}->{user} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $srv_object->{"auth"}{"user"}) 0 0 0
216 0 $ENV{REX_USER} = $srv_object->{auth}->{user};
217 }
218  
219 0 ▶ if ( exists $srv_object->{auth}->{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $srv_object->{"auth"}{"password"}) 0 0 0
220 0 $ENV{REX_PASSWORD} = $srv_object->{auth}->{password};
221 }
222  
223 0 ▶ if ( exists $srv_object->{auth}->{sudo_password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $srv_object->{"auth"}{"sudo_password"}) 0 0 0
224 0 $ENV{REX_SUDO_PASSWORD} = $srv_object->{auth}->{sudo_password};
225 }
226  
227 0 ▶ if ( exists $srv_object->{auth}->{sudo} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $srv_object->{"auth"}{"sudo"}) 0 0 0
228 0 $ENV{REX_SUDO} = $srv_object->{auth}->{sudo};
229 }
230  
231 }
232  
233 0 $ENV{JOBCONTROL_PROJECT_PATH} = $self->project->project_path;
234  
235 0 my $child_exit_status;
236 0 ▶ chwd $rex_path, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
237 0 my ( $chld_out, $chld_in, $pid );
238  
239 0 $self->project->app->log->debug(
240 "Writing output to: $ENV{JOBCONTROL_EXECUTION_PATH}/output.log");
241  
242 0 my $out_fh =
243 IO::File->new( "$ENV{JOBCONTROL_EXECUTION_PATH}/output.log", "a+" );
244 0 my $err_fh =
245 IO::File->new( "$ENV{JOBCONTROL_EXECUTION_PATH}/output.log", "a+" );
246 0 ▶ capture {
Subroutine __ANON__: not called CC=2 SCAR=17.9
247 system( $self->project->app->config->{rex},
248 0 ▶ '-H', $srv, '-t', 1, '-F', '-c', '-m',
Branch: 1 branch
BranchTrueFalseTotal
$cmdb ? : 0 0 0
249 ( $cmdb ? ( '-O', "cmdb_path=$cmdb/jobcontrol.yml" ) : () ), $task );
250  
251 0 $child_exit_status = $?;
252 }
253 0 stdout => $out_fh, stderr => $err_fh;
254  
255 0 };
256  
257 0 ▶ if ( $child_exit_status == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child_exit_status == 0) { } 0 0 0
258 0 push @ret,
259 {
260 server => $srv,
261 rexfile => $self->name,
262 task => $task,
263 status => "success",
264 };
265 }
266 else {
267 0 push @ret,
268 {
269 server => $srv,
270 rexfile => $self->name,
271 task => $task,
272 status => "failed",
273 };
274 }
275  
276 0 ▶ if ( $child_exit_status != 0 && $job->fail_strategy eq "terminate" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child_exit_status != 0 and $job->fail_strategy eq "terminate") 0 0 0
Condition0%0/3
$child_exit_status != 0 and $job->fail_strategy eq "terminate"
!ll&&!rl&&r
000
MC/DC0%0/2
$child_exit_status != 0 and $job->fail_strategy eq "terminate"
$child_exit_status != 0 $job->fail_strategy eq "terminate"
Truth table0%0/3
A and B
A= $child_exit_status != 0B= $job->fail_strategy eq "terminate"
ABresult
0X0
100
111
277 $ret[-1]->{terminate_message} =
278 0 "Terminating execution due to terminate fail strategy.";
279 }
280  
281 }
282  
283 0 return \@ret;
284 }
285  
286 1 1;
« JobControl/Helper/Project/Job.pm ↑ Summary JobControl/Rexfile.pm »