File Coverage

blib/lib/Rex/Apache/Deploy/Tomcat.pm
Criterion Covered Total %
statement 42 118 35.5
branch 0 20 0.0
condition 0 8 0.0
subroutine 14 22 63.6
pod 2 3 66.6
total 58 171 33.9


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             =head1 NAME
8              
9             Rex::Apache::Deploy::Tomcat - Deploy application to tomcat 6.
10              
11             =head1 DESCRIPTION
12              
13             With this module you can deploy WAR archives to Tomcat. Currently it works with Tomcat 6.
14              
15             =head1 SYNOPSIS
16              
17             use Rex::Apache::Deploy qw/Tomcat/;
18              
19             context_path "/myapp";
20              
21             task "dodeploy", "tc01", "tc02", sub {
22             deploy "myapp.war",
23             username => "manager",
24             password => "manager",
25             port => 8080;
26             };
27              
28             =head1 FUNCTIONS
29              
30             =over 4
31              
32             =cut
33              
34             package Rex::Apache::Deploy::Tomcat;
35              
36 1     1   1549 use strict;
  1         3  
  1         27  
37 1     1   5 use warnings;
  1         1  
  1         25  
38              
39 1     1   5 use Rex::Commands::Run;
  1         2  
  1         7  
40 1     1   82 use Rex::Commands::Fs;
  1         2  
  1         6  
41 1     1   433 use Rex::Commands::Upload;
  1         1  
  1         7  
42 1     1   54 use Rex::Commands;
  1         1  
  1         6  
43 1     1   756 use File::Basename qw(dirname basename);
  1         2  
  1         49  
44 1     1   6 use LWP::UserAgent;
  1         1  
  1         26  
45              
46             #require Exporter;
47             #use base qw(Exporter);
48              
49 1     1   31 use vars qw(@EXPORT $context_path);
  1         1  
  1         127  
50             @EXPORT = qw(deploy context_path jk);
51              
52             ############ deploy functions ################
53              
54             =item deploy($file, %option)
55              
56             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 option.
57              
58             task "dodeploy", "tc01", "tc02", sub {
59             deploy "myapp.war",
60             username => "manager",
61             password => "manager",
62             manager_url => "_manager",
63             port => 8080,
64             context_path => "/foo";
65             };
66              
67              
68             =cut
69              
70             sub deploy {
71 0     0 1   my ( $file, %option ) = @_;
72              
73 0 0         if ( !%option ) {
74 0 0         if ( Rex::Config->get("package_option") ) {
75 0           %option = %{ Rex::Config->get("package_option") };
  0            
76             }
77             }
78              
79 0           my $options = \%option;
80              
81 1     1   4 no strict;
  1         2  
  1         26  
82 1     1   5 no warnings;
  1         2  
  1         75  
83 0           my $rnd_file = get_random( 8, a .. z, 0 .. 9 );
84 1     1   5 use strict;
  1         2  
  1         26  
85 1     1   5 use warnings;
  1         2  
  1         952  
86              
87 0 0         if ( !exists $options->{"context_path"} ) {
88 0           $options->{"context_path"} = $context_path;
89             }
90              
91 0 0         if ( exists $options->{"manager_url"} ) {
92 0           my $mgr_url = $options->{"manager_url"};
93 0           $mgr_url =~ s{^/}{};
94 0           $options->{"manager_url"} = $mgr_url;
95             }
96             else {
97 0           $options->{"manager_url"} = "manager";
98             }
99              
100 0           upload( $file, "/tmp/$rnd_file.war" );
101 0           chmod 644, "/tmp/$rnd_file.war";
102              
103 0           $options->{"file"} = "/tmp/$rnd_file.war";
104              
105             # zuerst muss undeployed werden
106 0           _undeploy($options);
107              
108             # und dann wieder deployen
109 0           _deploy($options);
110              
111 0           unlink "/tmp/$rnd_file.war";
112             }
113              
114             sub jk {
115 0     0 0   my ( $action, $iname, @opts ) = @_;
116 0           my $option = {@opts};
117 0   0       my $path = $option->{"path"} || "/jkmanager";
118 0   0       my $worker = $option->{"worker"} || "";
119              
120 0           my $url = "http://%s%s/?cmd=update&w=$worker&att=vwa&sw=%s&vwa=%i";
121 0           my $server = Rex->get_current_connection()->{"server"};
122              
123 0 0         if ( $action eq "disable" ) {
124 0           $url = sprintf( $url, $server, $path, $iname, 1 );
125             }
126             else {
127 0           $url = sprintf( $url, $server, $path, $iname, 0 );
128             }
129              
130 0           my $ua = LWP::UserAgent->new;
131 0           my $response = $ua->get($url);
132              
133 0 0         if ( !$response->is_success ) {
134 0           die("Failed $action instance");
135             }
136             }
137              
138             ############ helper function ##############
139              
140             sub _deploy {
141              
142 0     0     my $p = shift;
143              
144 0           my $server = connection->server;
145 0 0         if ( $server eq "" ) {
146 0           $server = "localhost";
147             }
148              
149 0           my $ua = LWP::UserAgent->new();
150             my $url =
151             _get_url( "$server:$p->{port}",
152             "deploy?path=" . $p->{"context_path"} . "&war=file:" . $p->{"file"},
153 0           $p->{"username"}, $p->{"password"}, $p->{"manager_url"} );
154              
155 0           Rex::Logger::debug("Connection to: $url");
156 0           my $resp = $ua->get($url);
157 0 0         if ( $resp->is_success ) {
158 0           Rex::Logger::info( $resp->decoded_content );
159             }
160             else {
161 0           Rex::Logger::info( "FAILURE: $url: " . $resp->status_line );
162             }
163              
164             }
165              
166             sub _undeploy {
167              
168 0     0     my $p = shift;
169              
170             _do_action(
171             "undeploy", $p->{"context_path"}, $p->{"port"},
172 0           $p->{"username"}, $p->{"password"}, $p->{"manager_url"}
173             );
174              
175             }
176              
177             sub _get_url {
178 0     0     my $server = shift;
179 0           my $command = shift;
180 0           my $user = shift;
181 0           my $pw = shift;
182 0           my $mgr_path = shift;
183              
184 0   0       $mgr_path ||= "manager";
185              
186 0           return "http://$user:$pw\@" . "$server/$mgr_path/$command";
187             }
188              
189             sub _do_action {
190              
191 0     0     my $action = shift;
192 0           my $path = shift;
193 0           my $port = shift;
194 0           my $user = shift;
195 0           my $pw = shift;
196 0           my $mgr_path = shift;
197              
198 0   0       $mgr_path ||= "manager";
199              
200 0           my $ua = LWP::UserAgent->new();
201 0           my $current_connection = Rex::get_current_connection();
202              
203 0           my $server = connection->server;
204 0 0         if ( $server eq "" ) {
205 0           $server = "localhost";
206             }
207              
208 0           my $_url =
209             _get_url( "$server:$port", "$action?path=$path", $user, $pw, $mgr_path );
210 0           Rex::Logger::debug("Connecting to: $_url");
211              
212 0           my $resp = $ua->get($_url);
213 0 0         if ( $resp->is_success ) {
214 0           Rex::Logger::info( $resp->decoded_content );
215             }
216             else {
217 0           Rex::Logger::info( "FAILURE: $_url: " . $resp->status_line );
218             }
219              
220             }
221              
222             ############ configuration functions #############
223              
224             =item context_path($path)
225              
226             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 function.
227              
228             context_path "/myapp";
229              
230             =cut
231              
232             sub context_path {
233 0     0 1   $context_path = shift;
234             }
235              
236             ####### import function #######
237              
238             sub import {
239              
240 1     1   5 no strict 'refs';
  1         3  
  1         114  
241 0     0     for my $func (@EXPORT) {
242 0           Rex::Logger::debug("Registering main::$func");
243 0           *{"$_[1]::$func"} = \&$func;
  0            
244             }
245              
246             }
247              
248             =back
249              
250             =cut
251              
252             1;