File Coverage

blib/lib/Rex/JobControl/Helper/Project.pm
Criterion Covered Total %
statement 30 144 20.8
branch 0 20 0.0
condition 0 3 0.0
subroutine 10 36 27.7
pod 0 25 0.0
total 40 228 17.5


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4             # vim: set ts=2 sw=2 tw=0:
5             # vim: set expandtab:
6              
7             package Rex::JobControl::Helper::Project;
8             $Rex::JobControl::Helper::Project::VERSION = '0.18.0';
9 1     1   4 use strict;
  1         2  
  1         28  
10 1     1   3 use warnings;
  1         2  
  1         19  
11 1     1   3 use Data::Dumper;
  1         1  
  1         47  
12 1     1   5 use File::Spec;
  1         1  
  1         17  
13 1     1   4 use File::Path;
  1         0  
  1         45  
14 1     1   399 use YAML;
  1         4959  
  1         48  
15 1     1   5 use Digest::MD5 'md5_hex';
  1         1  
  1         33  
16              
17 1     1   382 use Rex::JobControl::Helper::Project::Job;
  1         1  
  1         29  
18 1     1   398 use Rex::JobControl::Helper::Project::Rexfile;
  1         2  
  1         35  
19 1     1   376 use Rex::JobControl::Helper::Project::Formular;
  1         2  
  1         1244  
20              
21             sub new {
22 0     0 0   my $that = shift;
23 0   0       my $proto = ref($that) || $that;
24 0           my $self = {@_};
25              
26 0           bless( $self, $proto );
27              
28 0           $self->load;
29              
30 0           return $self;
31             }
32              
33 0     0 0   sub app { (shift)->{app}; }
34 0     0 0   sub name { (shift)->{project_configuration}->{name}; }
35 0     0 0   sub directory { (shift)->{directory}; }
36              
37             sub dump {
38 0     0 0   my ($self) = @_;
39              
40 0           $self->app->log->debug( Dumper($self) );
41             }
42              
43             sub load {
44 0     0 0   my ($self) = @_;
45              
46 0 0         if ( -f $self->_config_file() ) {
47 0           $self->{project_configuration} = YAML::LoadFile( $self->_config_file );
48 0           $self->{name} = $self->{project_configuration}->{name};
49             }
50              
51             #$self->{directory} = $self->{name};
52             }
53              
54             sub _config_file {
55 0     0     my ($self) = @_;
56 0           return $self->project_path() . "/project.conf.yml";
57             }
58              
59             sub project_path {
60 0     0 0   my ($self) = @_;
61              
62 0           my $path = File::Spec->rel2abs( $self->app->config->{project_path} );
63 0           my $project_path = File::Spec->catdir( $path, $self->{directory} );
64              
65 0           return $project_path;
66             }
67              
68             sub get_last_job_execution {
69 0     0 0   my ($self) = @_;
70              
71 0           my $last_run_status_file = File::Spec->catfile($self->project_path, "last.run.status.yml");
72 0 0         if(-f $last_run_status_file) {
73 0           return YAML::LoadFile($last_run_status_file);
74             }
75              
76 0           return;
77             }
78              
79             sub create {
80 0     0 0   my ($self) = @_;
81              
82 0           my $path = File::Spec->rel2abs( $self->app->config->{project_path} );
83 0           my $project_path = File::Spec->catdir( $path, md5_hex( $self->{name} ) );
84              
85 0           $self->app->log->debug(
86             "Creating new project $self->{name} in $project_path.");
87              
88 0           File::Path::make_path($project_path);
89 0           File::Path::make_path( File::Spec->catdir( $project_path, "jobs" ) );
90 0           File::Path::make_path( File::Spec->catdir( $project_path, "rex" ) );
91 0           File::Path::make_path( File::Spec->catdir( $project_path, "formulars" ) );
92              
93 0           my $project_configuration = { name => $self->{name}, };
94              
95 0           YAML::DumpFile( "$project_path/project.conf.yml", $project_configuration );
96             }
97              
98             sub update {
99 0     0 0   my ($self) = @_;
100              
101 0           my $project_path = $self->project_path;
102 0           YAML::DumpFile( "$project_path/project.conf.yml",
103             $self->{project_configuration} );
104             }
105              
106             sub add_node {
107 0     0 0   my ( $self, $host ) = @_;
108              
109 0 0         if ( !exists $self->{project_configuration}->{nodes} ) {
110 0           $self->{project_configuration}->{nodes} = [];
111             }
112              
113 0           push @{ $self->{project_configuration}->{nodes} }, $host;
  0            
114 0           $self->update;
115             }
116              
117             sub job_count {
118 0     0 0   my ($self) = @_;
119 0           my $jobs = $self->jobs;
120 0           return scalar( @{$jobs} );
  0            
121             }
122              
123             sub jobs {
124 0     0 0   my ($self) = @_;
125              
126 0           my @jobs;
127              
128 0 0         opendir( my $dh, $self->project_path() . "/jobs" )
129             or die( "Error: $! (" . $self->project_path() . ")" );
130 0           while ( my $entry = readdir($dh) ) {
131 0 0         next if ( !-f $self->project_path() . "/jobs/$entry/job.conf.yml" );
132 0           push @jobs,
133             Rex::JobControl::Helper::Project::Job->new(
134             directory => $entry,
135             project => $self
136             );
137             }
138 0           closedir($dh);
139              
140 0           return \@jobs;
141             }
142              
143             sub get_job {
144 0     0 0   my ( $self, $dir ) = @_;
145 0           return Rex::JobControl::Helper::Project::Job->new(
146             directory => $dir,
147             project => $self
148             );
149             }
150              
151             sub create_job {
152 0     0 0   my ( $self, %data ) = @_;
153              
154 0           $data{directory} = md5_hex( $data{directory} );
155              
156 0           my $job =
157             Rex::JobControl::Helper::Project::Job->new( project => $self, %data );
158 0           $job->create(%data);
159             }
160              
161             sub rexfile_count {
162 0     0 0   my ($self) = @_;
163 0           my $rexfiles = $self->rexfiles;
164 0           return scalar( @{$rexfiles} );
  0            
165             }
166              
167             sub rexfiles {
168 0     0 0   my ($self) = @_;
169              
170 0           my @rexfiles;
171              
172 0 0         opendir( my $dh, $self->project_path() . "/rex" ) or die($!);
173 0           while ( my $entry = readdir($dh) ) {
174 0 0         next if ( !-f $self->project_path() . "/rex/$entry/rex.conf.yml" );
175 0           push @rexfiles,
176             Rex::JobControl::Helper::Project::Rexfile->new(
177             directory => $entry,
178             project => $self
179             );
180             }
181 0           closedir($dh);
182              
183 0           return \@rexfiles;
184             }
185              
186             sub create_rexfile {
187 0     0 0   my ( $self, %data ) = @_;
188              
189 0           $data{directory} = md5_hex( $data{directory} );
190              
191 0           my $rexfile =
192             Rex::JobControl::Helper::Project::Rexfile->new( project => $self, %data );
193 0           $rexfile->create(%data);
194             }
195              
196             sub get_rexfile {
197 0     0 0   my ( $self, $dir ) = @_;
198 0           return Rex::JobControl::Helper::Project::Rexfile->new(
199             directory => $dir,
200             project => $self
201             );
202             }
203              
204             sub all_server {
205 0     0 0   my ($self) = @_;
206              
207 0           my @all_server;
208              
209 0           for my $rex ( @{ $self->rexfiles } ) {
  0            
210 0           push @all_server, @{ $rex->all_server };
  0            
211             }
212              
213 0           $self->load;
214 0           for my $srv ( @{ $self->{project_configuration}->{nodes} } ) {
  0            
215 0           push @all_server, $srv;
216             }
217              
218 0           return \@all_server;
219             }
220              
221             sub remove {
222 0     0 0   my ($self) = @_;
223 0           File::Path::remove_tree( $self->project_path() );
224             }
225              
226             sub formular_count {
227 0     0 0   my ($self) = @_;
228 0           my $forms = $self->formulars;
229 0           return scalar( @{$forms} );
  0            
230             }
231              
232             sub formulars {
233 0     0 0   my ($self) = @_;
234              
235 0           my @formulars;
236              
237 0 0         if ( !-d $self->project_path() . "/formulars" ) {
238 0           return [];
239             }
240              
241 0 0         opendir( my $dh, $self->project_path() . "/formulars" )
242             or die( "Error: $! (" . $self->project_path() . ")" );
243 0           while ( my $entry = readdir($dh) ) {
244             next
245 0 0         if ( !-f $self->project_path() . "/formulars/$entry/formular.conf.yml" );
246 0           push @formulars,
247             Rex::JobControl::Helper::Project::Formular->new(
248             directory => $entry,
249             project => $self
250             );
251             }
252 0           closedir($dh);
253              
254 0           return \@formulars;
255             }
256              
257             sub get_formular {
258 0     0 0   my ( $self, $dir ) = @_;
259 0           return Rex::JobControl::Helper::Project::Formular->new(
260             directory => $dir,
261             project => $self
262             );
263             }
264              
265             sub create_formular {
266 0     0 0   my ( $self, %data ) = @_;
267              
268 0           $data{directory} = md5_hex( $data{directory} );
269              
270 0           my $form =
271             Rex::JobControl::Helper::Project::Formular->new( project => $self, %data );
272              
273 0           $form->create(%data);
274             }
275              
276             1;