| 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::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 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 18 | 1 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | 1 | ▶ | use Rex::Commands::Run; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 21 | 1 | ▶ | use Rex::Commands::Fs; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 22 | 1 | ▶ | use Rex::Commands::Upload; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 23 | 1 | ▶ | use Rex::Commands; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 24 | 1 | ▶ | use Rex::Config; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 25 | 1 | ▶ | use File::Basename qw(dirname basename); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | 1 | ▶ | use YAML; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 28 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | #require Exporter; | ||||||||||||||||||
| 31 | #use base qw(Exporter); | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | 1 | ▶ | use vars qw(@EXPORT $template_file $template_pattern); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 34 | 1 | @EXPORT = qw(inject | |||||||||||||||||
| 35 | template_file template_search_for); | ||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | ############ deploy functions ################ | ||||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | 0 | ▶ | sub inject { | ||||||||||||||||
|
Subroutine
inject: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||
| 40 | 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 | ▶ | if ( $^O =~ m/^MSWin/i ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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; | |||||||||||||||||
| 67 | 0 | local *FILE; | |||||||||||||||||
| 68 | 0 | open FILE, "<$file"; | |||||||||||||||||
| 69 | 0 | $content = <FILE>; | |||||||||||||||||
| 70 | 0 | close FILE | |||||||||||||||||
| 71 | } | ||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | 0 | my $data; | |||||||||||||||||
| 74 | 0 | eval { | |||||||||||||||||
| 75 | 0 | ▶ | $^W = 0 if $is_w; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 76 | 0 | Rex::Logger::debug("Loading content from $file"); | |||||||||||||||||
| 77 | 0 | $data = Load($content); | |||||||||||||||||
| 78 | 0 | ▶ | $^W = 1 if $is_w; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 79 | }; | ||||||||||||||||||
| 80 | |||||||||||||||||||
| 81 | 0 | ▶ | if ($@) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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 | ▶ | open( my $fh, ">", $file ) or die($!); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 91 | 0 | print $fh Dump($new); | |||||||||||||||||
| 92 | 0 | close($fh); | |||||||||||||||||
| 93 | } | ||||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | 0 | ▶ | if ( exists $option->{"pre_pack_hook"} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 96 | 0 | &{ $option->{"pre_pack_hook"} }; | |||||||||||||||||
| 97 | } | ||||||||||||||||||
| 98 | |||||||||||||||||||
| 99 | 0 | run $cmd2; | |||||||||||||||||
| 100 | 0 | ▶ | if ( $? != 0 ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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 | ▶ | if ( exists $option->{"post_pack_hook"} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 107 | 0 | &{ $option->{"post_pack_hook"} }; | |||||||||||||||||
| 108 | } | ||||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | 0 | chdir(".."); | |||||||||||||||||
| 111 | 0 | system("rm -rf tmp"); | |||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | ############ configuration functions ############# | ||||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | 0 | ▶ | sub template_file { | ||||||||||||||||
|
Subroutine
template_file: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 117 | 0 | $template_file = shift; | |||||||||||||||||
| 118 | } | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | 0 | ▶ | sub template_search_for { | ||||||||||||||||
|
Subroutine
template_search_for: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 121 | 0 | $template_pattern = shift; | |||||||||||||||||
| 122 | } | ||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | ############ helper functions ############# | ||||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | 0 | ▶ | sub _get_extract_command { | ||||||||||||||||
|
Subroutine
_get_extract_command: not called CC=4 SCAR=30.0
| |||||||||||||||||||
| 127 | 0 | my ($file) = @_; | |||||||||||||||||
| 128 | |||||||||||||||||||
| 129 | 0 | ▶ | if ( $file =~ m/\.tar\.gz$/ ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 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 | 0 | ▶ | sub _get_pack_command { | ||||||||||||||||
|
Subroutine
_get_pack_command: not called CC=4 SCAR=30.0
| |||||||||||||||||||
| 143 | 0 | my ($file) = @_; | |||||||||||||||||
| 144 | |||||||||||||||||||
| 145 | 0 | ▶ | if ( $file =~ m/\.tar\.gz$/ ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 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 | 0 | ▶ | sub _get_template_params { | ||||||||||||||||
|
Subroutine
_get_template_params: not called CC=2 SCAR=17.9
| |||||||||||||||||||
| 160 | 0 | my ($template_file) = @_; | |||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | 0 | ▶ | if ( -f "$template_file." . Rex::Config->get_environment ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 163 | 0 | $template_file = "$template_file." . Rex::Config->get_environment; | |||||||||||||||||
| 164 | } | ||||||||||||||||||
| 165 | |||||||||||||||||||
| 166 | 0 | my $content; | |||||||||||||||||
| 167 | { | ||||||||||||||||||
| 168 | 0 | local $/ = undef; | |||||||||||||||||
| 169 | 0 | local *FILE; | |||||||||||||||||
| 170 | 0 | open FILE, "<$template_file"; | |||||||||||||||||
| 171 | 0 | $content = <FILE>; | |||||||||||||||||
| 172 | 0 | close FILE | |||||||||||||||||
| 173 | } | ||||||||||||||||||
| 174 | |||||||||||||||||||
| 175 | 0 | return Load($content); | |||||||||||||||||
| 176 | } | ||||||||||||||||||
| 177 | |||||||||||||||||||
| 178 | 0 | ▶ | sub _get_ext { | ||||||||||||||||
|
Subroutine
_get_ext: not called CC=4 SCAR=30.0
| |||||||||||||||||||
| 179 | 0 | my ($file) = @_; | |||||||||||||||||
| 180 | |||||||||||||||||||
| 181 | 0 | ▶ | if ( $file =~ m/\.tar\.gz$/ ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 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 | 0 | ▶ | sub _update_hash { | ||||||||||||||||
|
Subroutine
_update_hash: not called CC=5 SCAR=34.0
| |||||||||||||||||||
| 196 | 0 | ▶ | return $_[1] unless ( ref( $_[0] ) ); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 197 | map { | ||||||||||||||||||
| 198 | $_[0]->{$_} = _update_hash( $_[0]->{$_}, $_[1]->{$_} ) | ||||||||||||||||||
| 199 | 0 | ▶ | if ( defined $_[1]->{$_} ) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 200 | 0 | ▶ | } keys %{ $_[0] } if ( ref( $_[0] ) eq "HASH" ); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 201 | 0 | ▶ | if ( ref( $_[0] ) eq "ARRAY" ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 202 | 0 | $_[0] = $_[1]; | |||||||||||||||||
| 203 | } | ||||||||||||||||||
| 204 | |||||||||||||||||||
| 205 | 0 | $_[0]; | |||||||||||||||||
| 206 | } | ||||||||||||||||||
| 207 | |||||||||||||||||||
| 208 | ####### import function ####### | ||||||||||||||||||
| 209 | |||||||||||||||||||
| 210 | 0 | ▶ | sub import { | ||||||||||||||||
|
Subroutine
import: not called CC=2 SCAR=10.1
| |||||||||||||||||||
| 211 | |||||||||||||||||||
| 212 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 213 | 0 | for my $func (@EXPORT) { | |||||||||||||||||
| 214 | 0 | Rex::Logger::debug("Registering main::$func"); | |||||||||||||||||
| 215 | 0 | *{"$_[1]::$func"} = \&$func; | |||||||||||||||||
| 216 | } | ||||||||||||||||||
| 217 | |||||||||||||||||||
| 218 | } | ||||||||||||||||||
| 219 | |||||||||||||||||||
| 220 | 1 | 1; | |||||||||||||||||