File Coverage

blib/lib/Rex/Apache/Inject/YAML.pm
Criterion Covered Total %
statement 36 128 28.1
branch 0 44 0.0
condition n/a
subroutine 12 21 57.1
pod 0 3 0.0
total 48 196 24.4


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::YAML;
8              
9             =begin
10              
11             =head2 SYNOPSIS
12              
13             This is a (R)?ex module to ease the deployments of PHP, Perl or other languages.
14              
15             =cut
16              
17 1     1   1491 use strict;
  1         2  
  1         25  
18 1     1   5 use warnings;
  1         2  
  1         26  
19              
20 1     1   7 use Rex::Commands::Run;
  1         1  
  1         8  
21 1     1   83 use Rex::Commands::Fs;
  1         3  
  1         6  
22 1     1   412 use Rex::Commands::Upload;
  1         2  
  1         6  
23 1     1   51 use Rex::Commands;
  1         3  
  1         5  
24 1     1   788 use Rex::Config;
  1         3  
  1         6  
25 1     1   68 use File::Basename qw(dirname basename);
  1         2  
  1         56  
26              
27 1     1   9 use YAML;
  1         2  
  1         74  
28 1     1   5 use Data::Dumper;
  1         3  
  1         45  
29              
30             #require Exporter;
31             #use base qw(Exporter);
32              
33 1     1   5 use vars qw(@EXPORT $template_file $template_pattern);
  1         3  
  1         1208  
34             @EXPORT = qw(inject
35             template_file template_search_for);
36              
37             ############ deploy functions ################
38              
39             sub inject {
40 0     0 0   my ( $to, @options ) = @_;
41              
42 0           my $option = {@options};
43              
44 0           my $cmd1 = sprintf( _get_extract_command($to), "../$to" );
45 0           my $cmd2 = sprintf( _get_pack_command($to), "../$to", "." );
46              
47 0           my $template_params = _get_template_params($template_file);
48              
49 0           mkdir("tmp");
50 0           chdir("tmp");
51 0           run $cmd1;
52              
53 0           my $is_w = $^W;
54              
55 0           my $find = "find . -name '$template_pattern'";
56 0 0         if ( $^O =~ m/^MSWin/i ) {
57 0           $find = "find2 . -name \"$template_pattern\"";
58             }
59              
60 0           for my $file (`$find`) {
61 0           chomp $file;
62 0           Rex::Logger::debug("Found file: $file");
63              
64 0           my $content;
65             {
66 0           local $/ = undef;
  0            
67 0           local *FILE;
68 0           open FILE, "<$file";
69 0           $content = ;
70 0           close FILE
71             }
72              
73 0           my $data;
74 0           eval {
75 0 0         $^W = 0 if $is_w;
76 0           Rex::Logger::debug("Loading content from $file");
77 0           $data = Load($content);
78 0 0         $^W = 1 if $is_w;
79             };
80              
81 0 0         if ($@) {
82 0           Rex::Logger::info("Syntax-Error in $file -> skipping");
83 0           next;
84             }
85              
86 0           Rex::Logger::debug("Updating \$data - hash");
87 0           my $new = _update_hash( $data, $template_params );
88              
89 0           Rex::Logger::debug("Writing new content to $file");
90 0 0         open( my $fh, ">", $file ) or die($!);
91 0           print $fh Dump($new);
92 0           close($fh);
93             }
94              
95 0 0         if ( exists $option->{"pre_pack_hook"} ) {
96 0           &{ $option->{"pre_pack_hook"} };
  0            
97             }
98              
99 0           run $cmd2;
100 0 0         if ( $? != 0 ) {
101 0           chdir("..");
102 0           system("rm -rf tmp");
103 0           die("Can't re-pack archive. Please check permissions. Command was: $cmd2");
104             }
105              
106 0 0         if ( exists $option->{"post_pack_hook"} ) {
107 0           &{ $option->{"post_pack_hook"} };
  0            
108             }
109              
110 0           chdir("..");
111 0           system("rm -rf tmp");
112             }
113              
114             ############ configuration functions #############
115              
116             sub template_file {
117 0     0 0   $template_file = shift;
118             }
119              
120             sub template_search_for {
121 0     0 0   $template_pattern = shift;
122             }
123              
124             ############ helper functions #############
125              
126             sub _get_extract_command {
127 0     0     my ($file) = @_;
128              
129 0 0         if ( $file =~ m/\.tar\.gz$/ ) {
    0          
    0          
130 0           return "tar xzf %s";
131             }
132             elsif ( $file =~ m/\.zip$/ ) {
133 0           return "unzip %s";
134             }
135             elsif ( $file =~ m/\.tar\.bz2$/ ) {
136 0           return "tar xjf %s";
137             }
138              
139 0           die("Unknown Archive Format.");
140             }
141              
142             sub _get_pack_command {
143 0     0     my ($file) = @_;
144              
145 0 0         if ( $file =~ m/\.tar\.gz$/ ) {
    0          
    0          
146 0           return "tar czf %s %s";
147             }
148             elsif ( $file =~ m/\.zip$/ ) {
149 0           return "zip -r %s %s";
150             }
151             elsif ( $file =~ m/\.tar\.bz2$/ ) {
152 0           return "tar cjf %s %s";
153             }
154              
155 0           die("Unknown Archive Format.");
156             }
157              
158             # read the template file and return a hashref.
159             sub _get_template_params {
160 0     0     my ($template_file) = @_;
161              
162 0 0         if ( -f "$template_file." . Rex::Config->get_environment ) {
163 0           $template_file = "$template_file." . Rex::Config->get_environment;
164             }
165              
166 0           my $content;
167             {
168 0           local $/ = undef;
  0            
169 0           local *FILE;
170 0           open FILE, "<$template_file";
171 0           $content = ;
172 0           close FILE
173             }
174              
175 0           return Load($content);
176             }
177              
178             sub _get_ext {
179 0     0     my ($file) = @_;
180              
181 0 0         if ( $file =~ m/\.tar\.gz$/ ) {
    0          
    0          
182 0           return ".tar.gz";
183             }
184             elsif ( $file =~ m/\.zip$/ ) {
185 0           return ".zip";
186             }
187             elsif ( $file =~ m/\.tar\.bz2$/ ) {
188 0           return ".tar.bz2";
189             }
190              
191 0           die("Unknown Archive Format.");
192              
193             }
194              
195             sub _update_hash {
196 0 0   0     return $_[1] unless ( ref( $_[0] ) );
197             map {
198             $_[0]->{$_} = _update_hash( $_[0]->{$_}, $_[1]->{$_} )
199 0 0         if ( defined $_[1]->{$_} )
200 0 0         } keys %{ $_[0] } if ( ref( $_[0] ) eq "HASH" );
  0            
201 0 0         if ( ref( $_[0] ) eq "ARRAY" ) {
202 0           $_[0] = $_[1];
203             }
204              
205 0           $_[0];
206             }
207              
208             ####### import function #######
209              
210             sub import {
211              
212 1     1   6 no strict 'refs';
  1         2  
  1         98  
213 0     0     for my $func (@EXPORT) {
214 0           Rex::Logger::debug("Registering main::$func");
215 0           *{"$_[1]::$func"} = \&$func;
  0            
216             }
217              
218             }
219              
220             1;