| 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::Command; | ||||||||||||||||||||||||||
| 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 File::Basename qw(dirname basename); | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 25 | 1 | ▶ | use Cwd qw(getcwd); | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||
| 27 | #require Exporter; | ||||||||||||||||||||||||||
| 28 | #use base qw(Exporter); | ||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||
| 30 | 1 | ▶ | use vars qw(@EXPORT $inject_command); | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 31 | 1 | @EXPORT = qw(inject | |||||||||||||||||||||||||
| 32 | inject_command); | ||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||
| 34 | 1 | my $work_dir = getcwd; | |||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||
| 36 | ############ deploy functions ################ | ||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub inject { | ||||||||||||||||||||||||
|
Subroutine
inject: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||
| 39 | 0 | my ( $to, @options ) = @_; | |||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||
| 41 | 0 | my $option = {@options}; | |||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||
| 43 | 0 | my $cmd1 = sprintf( _get_extract_command($to), "../$to" ); | |||||||||||||||||||||||||
| 44 | 0 | my $cmd2 = sprintf( _get_pack_command($to), "../$to", "." ); | |||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||
| 46 | 0 | mkdir("tmp"); | |||||||||||||||||||||||||
| 47 | 0 | chdir("tmp"); | |||||||||||||||||||||||||
| 48 | 0 | run $cmd1; | |||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||
| 50 | 0 | for my $opt ( $option->{"inject"} ) { | |||||||||||||||||||||||||
| 51 | 0 | run sprintf( $inject_command, $opt->{"key"}, $opt->{"value"} ); | |||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ( exists $option->{"pre_pack_hook"} ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 55 | 0 | &{ $option->{"pre_pack_hook"} }; | |||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||
| 58 | 0 | run $cmd2; | |||||||||||||||||||||||||
| 59 | 0 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 60 | 0 | chdir(".."); | |||||||||||||||||||||||||
| 61 | 0 | system("rm -rf tmp"); | |||||||||||||||||||||||||
| 62 | 0 | die("Can't re-pack archive. Please check permissions. Command was: $cmd2"); | |||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ( exists $option->{"post_pack_hook"} ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 66 | 0 | &{ $option->{"post_pack_hook"} }; | |||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||
| 69 | 0 | chdir(".."); | |||||||||||||||||||||||||
| 70 | 0 | system("rm -rf tmp"); | |||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||
| 73 | ############ configuration functions ############# | ||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub inject_command { | ||||||||||||||||||||||||
|
Subroutine
inject_command: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||
| 76 | 0 | $inject_command = shift; | |||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||
| 79 | ############ helper functions ############# | ||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub _get_extract_command { | ||||||||||||||||||||||||
|
Subroutine
_get_extract_command: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||
| 82 | 0 | my ($file) = @_; | |||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ( $file =~ m/\.tar\.gz$/ ) { | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 85 | 0 | return "tar xzf %s"; | |||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||
| 87 | elsif ( $file =~ m/\.zip$/ ) { | ||||||||||||||||||||||||||
| 88 | 0 | return "unzip %s"; | |||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||
| 90 | elsif ( $file =~ m/\.tar\.bz2$/ ) { | ||||||||||||||||||||||||||
| 91 | 0 | return "tar xjf %s"; | |||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||
| 93 | elsif ( $file =~ m/\.war$/ ) { | ||||||||||||||||||||||||||
| 94 | 0 | return "unzip %s"; | |||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||
| 96 | elsif ( $file =~ m/\.jar$/ ) { | ||||||||||||||||||||||||||
| 97 | 0 | return "unzip %s"; | |||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||
| 100 | 0 | die("Unknown Archive Format."); | |||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub _get_pack_command { | ||||||||||||||||||||||||
|
Subroutine
_get_pack_command: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||
| 104 | 0 | my ($file) = @_; | |||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ( $file =~ m/\.tar\.gz$/ ) { | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 107 | 0 | return "tar czf %s %s"; | |||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||
| 109 | elsif ( $file =~ m/\.zip$/ ) { | ||||||||||||||||||||||||||
| 110 | 0 | return "zip -r %s %s"; | |||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||
| 112 | elsif ( $file =~ m/\.tar\.bz2$/ ) { | ||||||||||||||||||||||||||
| 113 | 0 | return "tar cjf %s %s"; | |||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||
| 115 | elsif ( $file =~ m/\.war$/ ) { | ||||||||||||||||||||||||||
| 116 | 0 | return "zip -r %s %s"; | |||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||
| 118 | elsif ( $file =~ m/\.jar$/ ) { | ||||||||||||||||||||||||||
| 119 | 0 | return "zip -r %s %s"; | |||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||
| 122 | 0 | die("Unknown Archive Format."); | |||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub _get_ext { | ||||||||||||||||||||||||
|
Subroutine
_get_ext: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||
| 126 | 0 | my ($file) = @_; | |||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | if ( $file =~ m/\.tar\.gz$/ ) { | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 129 | 0 | return ".tar.gz"; | |||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||
| 131 | elsif ( $file =~ m/\.zip$/ ) { | ||||||||||||||||||||||||||
| 132 | 0 | return ".zip"; | |||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||
| 134 | elsif ( $file =~ m/\.tar\.bz2$/ ) { | ||||||||||||||||||||||||||
| 135 | 0 | return ".tar.bz2"; | |||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||
| 137 | elsif ( $file =~ m/\.war$/ ) { | ||||||||||||||||||||||||||
| 138 | 0 | return ".war"; | |||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||
| 140 | elsif ( $file =~ m/\.jar$/ ) { | ||||||||||||||||||||||||||
| 141 | 0 | return ".jar"; | |||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||
| 144 | 0 | die("Unknown Archive Format."); | |||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||
| 148 | ####### import function ####### | ||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | sub import { | ||||||||||||||||||||||||
|
Subroutine
import: not called CC=2 SCAR=10.1
| |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||
| 152 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 153 | 0 | for my $func (@EXPORT) { | |||||||||||||||||||||||||
| 154 | 0 | Rex::Logger::debug("Registering main::$func"); | |||||||||||||||||||||||||
| 155 | 0 | *{"$_[1]::$func"} = \&$func; | |||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||
| 160 | 1 | 1; | |||||||||||||||||||||||||