lib/Rex/Apache/Deploy/Tomcat7.pm

filter: Statementstmt 33.0% 44 / 133 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 63.6% 14 / 22 Podpod 66.6% 2 / 3 totaltotal 30.6% 60 / 196 CC 19 SCAR 24.1
CC
19
Cov
26%
jk
30.0
_deploy
20.0
_do_action
20.0
CRAP 11.1
« lib/Rex/Apache/Build/tgz.pm ↑ Summary lib/Rex/Apache/Deploy/Tomcat.pm »
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 =head1 NAME
8  
9 Rex::Apache::Deploy::Tomcat7 - Deploy application to Tomcat 7.
10  
11 =head1 DESCRIPTION
12  
13 With this module you can deploy WAR archives to Tomcat7.
14 This module needs the manager application and I<manager-script> permissions.
15  
16 =head1 SYNOPSIS
17  
18 use Rex::Apache::Deploy qw/Tomcat7/;
19  
20 context_path "/myapp";
21  
22 task "dodeploy", "tc01", "tc02", sub {
23 deploy "myapp.war",
24 username => "manager",
25 password => "manager",
26 port => 8080;
27 };
28  
29 =head1 FUNCTIONS
30  
31 =over 4
32  
33 =cut
34  
35 package Rex::Apache::Deploy::Tomcat7;
36  
37 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 1 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use Rex::Commands::Upload;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use Rex::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use File::Basename qw(dirname basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 #require Exporter;
48 #use base qw(Exporter);
49  
50 1 ▶use vars qw(@EXPORT $context_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 @EXPORT = qw(deploy context_path jk);
52  
53 ############ deploy functions ################
54  
55 =item deploy($file, %option)
56  
57 This function deploys the given WAR archive. For that it will connect to the Tomcat manager. You have to define username and password for the Tomcat manager in the %option hash. If the Tomcat manager isn't available under its default location /manager you can also define the location with the I<manager_url> option.
58  
59 task "dodeploy", "tc01", "tc02", sub {
60 deploy "myapp.war",
61 username => "manager",
62 password => "manager",
63 manager_url => "_manager",
64 port => 8080,
65 context_path => "/foo";
66 };
67  
68  
69 =cut
70  
71 0 ▶sub deploy {
Subroutine deploy: not called CC=5 SCAR=25.9
Pod documented
72 0 my ( $file, %option ) = @_;
73  
74 0 ▶ if ( !%option ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%option) 0 0 0
75 0 ▶ if ( Rex::Config->get("package_option") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Rex::Config"->get("package_option")) 0 0 0
76 0 %option = %{ Rex::Config->get("package_option") };
77 }
78 }
79  
80 0 my $options = \%option;
81  
82 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 0 my $rnd_file = get_random( 8, a .. z, 0 .. 9 );
85 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 0 ▶ if ( !exists $options->{"context_path"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options->{"context_path"}) 0 0 0
89 0 $options->{"context_path"} = $context_path;
90 }
91  
92 0 ▶ if ( exists $options->{"manager_url"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{'manager_url'}) { } 0 0 0
93 0 my $mgr_url = $options->{"manager_url"};
94 0 $mgr_url =~ s{^/}{};
95 0 $options->{"manager_url"} = $mgr_url;
96 }
97 else {
98 0 $options->{"manager_url"} = "manager";
99 }
100  
101 0 upload( $file, "/tmp/$rnd_file.war" );
102 0 chmod 644, "/tmp/$rnd_file.war";
103  
104 0 $options->{"file"} = "/tmp/$rnd_file.war";
105  
106 0 _deploy($options);
107  
108 0 unlink "/tmp/$rnd_file.war";
109 }
110  
111 0 ▶sub jk {
Subroutine jk: not called CC=5 SCAR=34.0
Pod undocumented
112 0 my ( $action, $iname, @opts ) = @_;
113 0 my $option = {@opts};
114 0 ▶ my $path = $option->{"path"} || "/jkmanager";
Condition0%0/2
$option->{'path'} || '/jkmanager'
l!l
00
MC/DC0%0/1
$option->{'path'} || '/jkmanager'
$option->{'path'}
Truth table0%0/2
A
A= $option->{'path'}
Aresult
11
00
115 0 ▶ my $worker = $option->{"worker"} || "";
Condition0%0/2
$option->{'worker'} || ''
l!l
00
MC/DC0%0/1
$option->{'worker'} || ''
$option->{'worker'}
Truth table0%0/2
A
A= $option->{'worker'}
Aresult
11
00
116  
117 0 my $url = "http://%s%s/?cmd=update&w=$worker&att=vwa&sw=%s&vwa=%i";
118 0 my $server = Rex->get_current_connection()->{"server"};
119  
120 0 ▶ if ( $action eq "disable" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq 'disable') { } 0 0 0
121 0 $url = sprintf( $url, $server, $path, $iname, 1 );
122 }
123 else {
124 0 $url = sprintf( $url, $server, $path, $iname, 0 );
125 }
126  
127 0 my $ua = LWP::UserAgent->new;
128 0 my $response = $ua->get($url);
129  
130 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response->is_success) 0 0 0
131 0 die("Failed $action instance");
132 }
133 }
134  
135 ############ helper function ##############
136  
137 0 ▶sub _deploy {
Subroutine _deploy: not called CC=4 SCAR=30.0
138  
139 0 my $p = shift;
140  
141 0 my $server = connection->server;
142 0 ▶ if ( $server eq "<local>" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server eq "<local>") 0 0 0
143 0 $server = "localhost";
144 }
145  
146 0 my $ua = LWP::UserAgent->new();
147 my $url =
148 _get_url( "$server:$p->{port}",
149 "deploy?path=$p->{context_path}&war=file:$p->{file}&update=true",
150 0 $p->{"username"}, $p->{"password"}, $p->{"manager_url"} );
151  
152 0 Rex::Logger::debug("Connection to: $url");
153 0 my $resp = $ua->get($url);
154 0 ▶ if ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 0 0 0
155 0 my $c = $resp->decoded_content;
156 0 ▶ if ( $c =~ m/FAIL/gmi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /FAIL/mig) { } 0 0 0
157 0 Rex::Logger::info( $resp->decoded_content );
158 0 die( $resp->decoded_content );
159 }
160 else {
161 0 Rex::Logger::info( $resp->decoded_content );
162 }
163 }
164 else {
165 0 Rex::Logger::info( "FAILURE: $url: " . $resp->status_line );
166 0 die( "FAILURE: $url: " . $resp->status_line );
167 }
168  
169 }
170  
171 0 ▶sub _undeploy {
Subroutine _undeploy: not called CC=3 SCAR=24.8
172  
173 0 my $p = shift;
174  
175 0 my $server = connection->server;
176 0 ▶ if ( $server eq "<local>" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server eq "<local>") 0 0 0
177 0 $server = "localhost";
178 }
179  
180 0 my $ua = LWP::UserAgent->new();
181 my $url = _get_url( "$server:$p->{port}", "undeploy?path=$p->{context_path}",
182 0 $p->{username}, $p->{password}, $p->{manager_url} );
183  
184 0 Rex::Logger::debug("Connection to: $url");
185 0 my $resp = $ua->get($url);
186 0 ▶ if ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 0 0 0
187 0 Rex::Logger::info( $resp->decoded_content );
188 }
189 else {
190 0 Rex::Logger::info( "FAILURE: $url: " . $resp->status_line );
191 }
192  
193 }
194  
195 0 ▶sub _get_url {
Subroutine _get_url: not called CC=2 SCAR=17.9
196 0 my $server = shift;
197 0 my $command = shift;
198 0 my $user = shift;
199 0 my $pw = shift;
200 0 my $mgr_path = shift;
201  
202 0 ▶ $mgr_path ||= "manager";
Condition0%0/2
$mgr_path ||= "manager"
l!l
00
MC/DC0%0/1
$mgr_path ||= "manager"
$mgr_path
Truth table0%0/2
A
A= $mgr_path
Aresult
11
00
203  
204 0 return "http://$user:$pw\@" . "$server/$mgr_path/text/$command";
205 }
206  
207 0 ▶sub _do_action {
Subroutine _do_action: not called CC=4 SCAR=30.0
208  
209 0 my $action = shift;
210 0 my $path = shift;
211 0 my $port = shift;
212 0 my $user = shift;
213 0 my $pw = shift;
214 0 my $mgr_path = shift;
215  
216 0 ▶ $mgr_path ||= "manager";
Condition0%0/2
$mgr_path ||= "manager"
l!l
00
MC/DC0%0/1
$mgr_path ||= "manager"
$mgr_path
Truth table0%0/2
A
A= $mgr_path
Aresult
11
00
217  
218 0 my $ua = LWP::UserAgent->new();
219 0 my $current_connection = Rex::get_current_connection();
220  
221 0 my $server = connection->server;
222 0 ▶ if ( $server eq "<local>" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server eq "<local>") 0 0 0
223 0 $server = "localhost";
224 }
225  
226 0 my $_url =
227 _get_url( "$server:$port", "$action?path=$path", $user, $pw, $mgr_path );
228 0 Rex::Logger::debug("Connecting to: $_url");
229  
230 0 my $resp = $ua->get($_url);
231 0 ▶ if ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 0 0 0
232 0 Rex::Logger::info( $resp->decoded_content );
233 }
234 else {
235 0 Rex::Logger::info( "FAILURE: $_url: " . $resp->status_line );
236 }
237  
238 }
239  
240 ############ configuration functions #############
241  
242 =item context_path($path)
243  
244 This function sets the context path for the application that gets deployed. This is a global setting. If you want to specify a custom context path for your application you can also do this as an option for the I<deploy> function.
245  
246 context_path "/myapp";
247  
248 =cut
249  
250 0 ▶sub context_path {
Subroutine context_path: not called CC=1 SCAR=6.9
Pod documented
251 0 $context_path = shift;
252 }
253  
254 ####### import function #######
255  
256 0 ▶sub import {
Subroutine import: not called CC=2 SCAR=10.1
257  
258 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
259 0 for my $func (@EXPORT) {
260 0 Rex::Logger::debug("Registering main::$func");
261 0 *{"$_[1]::$func"} = \&$func;
262 }
263  
264 }
265  
266 =back
267  
268 =cut
269  
270 1 1;
« lib/Rex/Apache/Build/tgz.pm ↑ Summary lib/Rex/Apache/Deploy/Tomcat.pm »