File Coverage

blib/lib/Rex/Apache/Inject/Template.pm
Criterion Covered Total %
statement 33 167 19.7
branch 0 74 0.0
condition n/a
subroutine 11 21 52.3
pod 0 4 0.0
total 44 266 16.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::Apache::Inject::Template;
8              
9 1     1   1579 use strict;
  1         2  
  1         27  
10 1     1   6 use warnings;
  1         2  
  1         26  
11              
12 1     1   5 use Rex::Commands::Run;
  1         4  
  1         7  
13 1     1   91 use Rex::Commands::Fs;
  1         2  
  1         15  
14 1     1   413 use Rex::Commands::Upload;
  1         2  
  1         6  
15 1     1   45 use Rex::Commands;
  1         2  
  1         5  
16 1     1   761 use Rex::Config;
  1         1  
  1         6  
17 1     1   66 use File::Basename qw(dirname basename);
  1         2  
  1         55  
18 1     1   5 use Cwd qw(getcwd);
  1         2  
  1         53  
19              
20             #require Exporter;
21             #use base qw(Exporter);
22              
23 1     1   8 use vars qw(@EXPORT $real_name_from_template $template_file $template_pattern);
  1         1  
  1         1857  
24             @EXPORT = qw(inject
25             generate_real_name
26             template_file template_search_for);
27              
28             my $work_dir = getcwd;
29              
30             ############ deploy functions ################
31              
32             sub inject {
33 0     0 0   my ( $to, @options ) = @_;
34              
35 0           my $start_dir = getcwd;
36              
37 0           my $option = {@options};
38              
39 0           my ( $cmd1, $cmd2 );
40              
41 0 0         if ( is_file($to) ) {
42 0           my $tmp_to = $to;
43 0 0         if ( $tmp_to !~ m/^\// ) { $tmp_to = "../$tmp_to"; }
  0            
44              
45 0           $cmd1 = sprintf( _get_extract_command($to), "$tmp_to" );
46 0           $cmd2 = sprintf( _get_pack_command($to), "$tmp_to", "." );
47              
48 0           mkdir("tmp");
49 0           chdir("tmp");
50 0           run $cmd1;
51             }
52             else {
53 0           chdir($to);
54             }
55              
56 0           my $template_params = _get_template_params($template_file);
57              
58 0 0         if ( exists $option->{"extract"} ) {
59 0           for my $file_pattern ( @{ $option->{"extract"} } ) {
  0            
60              
61 0           my $find = "find ../ -name '$file_pattern'";
62 0 0         if ( $^O =~ m/^MSWin/i ) {
63 0           $find = "find2 ../ -name \"$file_pattern\"";
64             }
65              
66 0           for my $found_file (`$find`) {
67 0           chomp $found_file;
68              
69 0           mkdir "tmp-b";
70 0           chdir "tmp-b";
71              
72 0           my $extract_cmd =
73             sprintf( _get_extract_command($found_file), "../$found_file" );
74 0           my $compress_cmd =
75             sprintf( _get_pack_command($found_file), "../$found_file", "." );
76              
77 0           run $extract_cmd;
78              
79 0           _find_and_parse_templates();
80              
81 0 0         if ( exists $option->{"pre_pack_hook"} ) {
82 0           &{ $option->{"pre_pack_hook"} }($found_file);
  0            
83             }
84              
85 0           run $compress_cmd;
86              
87 0 0         if ( exists $option->{"post_pack_hook"} ) {
88 0           &{ $option->{"post_pack_hook"} }($found_file);
  0            
89             }
90              
91 0           chdir "..";
92 0           rmdir "tmp-b";
93             }
94              
95             }
96             }
97              
98 0           _find_and_parse_templates();
99              
100 0           my $cur_dir = getcwd;
101 0           chdir $start_dir;
102 0 0         if ( is_file($to) ) {
103 0           chdir $cur_dir;
104              
105 0 0         if ( exists $option->{"pre_pack_hook"} ) {
106 0           &{ $option->{"pre_pack_hook"} };
  0            
107             }
108              
109 0           run $cmd2;
110 0 0         if ( $? != 0 ) {
111 0           chdir $start_dir;
112 0           system("rm -rf tmp");
113 0           die(
114             "Can't re-pack archive. Please check permissions. Command was: $cmd2");
115             }
116              
117 0 0         if ( exists $option->{"post_pack_hook"} ) {
118 0           &{ $option->{"post_pack_hook"} };
  0            
119             }
120             }
121              
122 0           chdir $start_dir;
123              
124 0 0         if ( is_file($to) ) {
125 0           system("rm -rf tmp");
126             }
127             }
128              
129             sub _find_and_parse_templates {
130              
131 0     0     my $template_params = _get_template_params($template_file);
132              
133 0           my $find = "find . -name '$template_pattern'";
134 0 0         if ( $^O =~ m/^MSWin/i ) {
135 0           $find = "find2 . -name \"$template_pattern\"";
136             }
137              
138 0           for my $file (`$find`) {
139 0 0         next if ( $file =~ m/\.svn\// );
140 0 0         next if ( $file =~ m/\.git\// );
141              
142 0           chomp $file;
143 0           my $content;
144             {
145 0           local $/ = undef;
  0            
146 0           local *FILE;
147 0           open FILE, "<$file";
148 0           $content = ;
149 0           close FILE
150             }
151 0           for my $key ( keys %$template_params ) {
152 0           my $val = $template_params->{$key};
153 0 0         if ( $content =~ m/\@$key\@/gm ) {
154 0           Rex::Logger::info("Replacing \@$key\@ with $val ($file)");
155 0           $content =~ s/\@$key\@/$val/g;
156             }
157             }
158              
159 0 0         my $new_file_name =
160             $real_name_from_template ? &$real_name_from_template($file) : $file;
161 0 0         open( my $fh, ">", $new_file_name ) or die($!);
162 0           print $fh $content;
163 0           close($fh);
164             }
165              
166             }
167              
168             ############ configuration functions #############
169              
170             sub generate_real_name(&) {
171 0     0 0   $real_name_from_template = shift;
172             }
173              
174             sub template_file {
175 0     0 0   $template_file = shift;
176             }
177              
178             sub template_search_for {
179 0     0 0   $template_pattern = shift;
180             }
181              
182             ############ helper functions #############
183              
184             sub _get_extract_command {
185 0     0     my ($file) = @_;
186              
187 0 0         if ( $file =~ m/\.tar\.gz$/ ) {
    0          
    0          
    0          
    0          
188 0           return "tar xzf %s";
189             }
190             elsif ( $file =~ m/\.zip$/ ) {
191 0           return "unzip %s";
192             }
193             elsif ( $file =~ m/\.tar\.bz2$/ ) {
194 0           return "tar xjf %s";
195             }
196             elsif ( $file =~ m/\.war$/ ) {
197 0           return "unzip %s";
198             }
199             elsif ( $file =~ m/\.jar$/ ) {
200 0           return "unzip %s";
201             }
202              
203 0           die("Unknown Archive Format.");
204             }
205              
206             sub _get_pack_command {
207 0     0     my ($file) = @_;
208              
209 0 0         if ( $file =~ m/\.tar\.gz$/ ) {
    0          
    0          
    0          
    0          
210 0           return "tar czf %s %s";
211             }
212             elsif ( $file =~ m/\.zip$/ ) {
213 0           return "zip -r %s %s";
214             }
215             elsif ( $file =~ m/\.tar\.bz2$/ ) {
216 0           return "tar cjf %s %s";
217             }
218             elsif ( $file =~ m/\.war$/ ) {
219 0           return "zip -r %s %s";
220             }
221             elsif ( $file =~ m/\.jar$/ ) {
222 0           return "zip -r %s %s";
223             }
224              
225 0           die("Unknown Archive Format.");
226             }
227              
228             # read the template file and return a hashref.
229             sub _get_template_params {
230 0     0     my ($template_file) = @_;
231 0           my @lines;
232              
233             my $t_file;
234 0 0         if ( $template_file =~ m/^\// ) {
235 0           $t_file = $template_file;
236             }
237             else {
238 0           $t_file = "$work_dir/$template_file";
239             }
240              
241 0 0         if ( -f "$t_file." . Rex::Config->get_environment ) {
242 0           $t_file = "$t_file." . Rex::Config->get_environment;
243             }
244              
245 0 0         open( my $fh, "<", $t_file ) or die($!);
246 0           @lines = <$fh>;
247 0           close($fh);
248 0           chomp @lines;
249              
250 0           my $r = {};
251 0           for my $line (@lines) {
252 0 0         next if ( $line =~ m/^#/ );
253 0 0         next if ( $line =~ m/^\s*?$/ );
254 0           $line =~ s/\r//gs;
255 0           $line =~ s/\n//gs;
256              
257 0           my ( $key, $val ) = ( $line =~ m/^(.*?) ?= ?(.*)$/ );
258 0           $val =~ s/^["']//;
259 0           $val =~ s/["']$//;
260              
261 0           $r->{$key} = $val;
262             }
263              
264 0           $r;
265             }
266              
267             sub _get_ext {
268 0     0     my ($file) = @_;
269              
270 0 0         if ( $file =~ m/\.tar\.gz$/ ) {
    0          
    0          
    0          
    0          
271 0           return ".tar.gz";
272             }
273             elsif ( $file =~ m/\.zip$/ ) {
274 0           return ".zip";
275             }
276             elsif ( $file =~ m/\.tar\.bz2$/ ) {
277 0           return ".tar.bz2";
278             }
279             elsif ( $file =~ m/\.war$/ ) {
280 0           return ".war";
281             }
282             elsif ( $file =~ m/\.jar$/ ) {
283 0           return ".jar";
284             }
285              
286 0           die("Unknown Archive Format.");
287              
288             }
289              
290             ####### import function #######
291              
292             sub import {
293              
294 1     1   6 no strict 'refs';
  1         2  
  1         106  
295 0     0     for my $func (@EXPORT) {
296 0           Rex::Logger::debug("Registering main::$func");
297 0           *{"$_[1]::$func"} = \&$func;
  0            
298             }
299              
300             }
301              
302             1;