| 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::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 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 37 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 38 | |||||||||||||
| 39 | 1 | ▶ | use Rex::Commands::Run; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 40 | 1 | ▶ | use Rex::Commands::Fs; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 41 | 1 | ▶ | use Rex::Commands::Upload; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 42 | 1 | ▶ | use Rex::Commands; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 43 | 1 | ▶ | use File::Basename qw(dirname basename); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 44 | 1 | ▶ | use LWP::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 45 | |||||||||||||
| 46 | #require Exporter; | ||||||||||||
| 47 | #use base qw(Exporter); | ||||||||||||
| 48 | |||||||||||||
| 49 | 1 | ▶ | use vars qw(@EXPORT $context_path); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 50 | 1 | @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<manager_url> 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 | 0 | ▶ | sub deploy { | ||||||||||
|
Subroutine
deploy: not called CC=5 SCAR=26.1
Pod
documented
| |||||||||||||
| 71 | 0 | my ( $file, %option ) = @_; | |||||||||||
| 72 | |||||||||||||
| 73 | 0 | ▶ | if ( !%option ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 0 | ▶ | if ( Rex::Config->get("package_option") ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 0 | %option = %{ Rex::Config->get("package_option") }; | |||||||||||
| 76 | } | ||||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | 0 | my $options = \%option; | |||||||||||
| 80 | |||||||||||||
| 81 | 1 | ▶ | no strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 82 | 1 | ▶ | no warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 83 | 0 | my $rnd_file = get_random( 8, a .. z, 0 .. 9 ); | |||||||||||
| 84 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 85 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 86 | |||||||||||||
| 87 | 0 | ▶ | if ( !exists $options->{"context_path"} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 0 | $options->{"context_path"} = $context_path; | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | 0 | ▶ | if ( exists $options->{"manager_url"} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | 0 | ▶ | sub jk { | ||||||||||
|
Subroutine
jk: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 115 | 0 | my ( $action, $iname, @opts ) = @_; | |||||||||||
| 116 | 0 | my $option = {@opts}; | |||||||||||
| 117 | 0 | ▶ | my $path = $option->{"path"} || "/jkmanager"; | ||||||||||
|
Condition0%0/2
$option->{'path'} || '/jkmanager'
MC/DC0%0/1
$option->{'path'} || '/jkmanager'
$option->{'path'}
Truth table0%0/2
A
A= $option->{'path'}
| |||||||||||||
| 118 | 0 | ▶ | my $worker = $option->{"worker"} || ""; | ||||||||||
|
Condition0%0/2
$option->{'worker'} || ''
MC/DC0%0/1
$option->{'worker'} || ''
$option->{'worker'}
Truth table0%0/2
A
A= $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 | ▶ | if ( $action eq "disable" ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | ▶ | if ( !$response->is_success ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 134 | 0 | die("Failed $action instance"); | |||||||||||
| 135 | } | ||||||||||||
| 136 | } | ||||||||||||
| 137 | |||||||||||||
| 138 | ############ helper function ############## | ||||||||||||
| 139 | |||||||||||||
| 140 | 0 | ▶ | sub _deploy { | ||||||||||
|
Subroutine
_deploy: not called CC=3 SCAR=24.8
| |||||||||||||
| 141 | |||||||||||||
| 142 | 0 | my $p = shift; | |||||||||||
| 143 | |||||||||||||
| 144 | 0 | my $server = connection->server; | |||||||||||
| 145 | 0 | ▶ | if ( $server eq "<local>" ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | ▶ | if ( $resp->is_success ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | 0 | ▶ | sub _undeploy { | ||||||||||
|
Subroutine
_undeploy: not called CC=1 SCAR=6.9
| |||||||||||||
| 167 | |||||||||||||
| 168 | 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 | 0 | ▶ | sub _get_url { | ||||||||||
|
Subroutine
_get_url: not called CC=2 SCAR=17.9
| |||||||||||||
| 178 | 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 | ▶ | $mgr_path ||= "manager"; | ||||||||||
|
Condition0%0/2
$mgr_path ||= "manager"
MC/DC0%0/1
$mgr_path ||= "manager"
$mgr_path
Truth table0%0/2
A
A= $mgr_path
| |||||||||||||
| 185 | |||||||||||||
| 186 | 0 | return "http://$user:$pw\@" . "$server/$mgr_path/$command"; | |||||||||||
| 187 | } | ||||||||||||
| 188 | |||||||||||||
| 189 | 0 | ▶ | sub _do_action { | ||||||||||
|
Subroutine
_do_action: not called CC=4 SCAR=30.0
| |||||||||||||
| 190 | |||||||||||||
| 191 | 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 | ▶ | $mgr_path ||= "manager"; | ||||||||||
|
Condition0%0/2
$mgr_path ||= "manager"
MC/DC0%0/1
$mgr_path ||= "manager"
$mgr_path
Truth table0%0/2
A
A= $mgr_path
| |||||||||||||
| 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 | ▶ | if ( $server eq "<local>" ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | ▶ | if ( $resp->is_success ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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<deploy> function. | ||||||||||||
| 227 | |||||||||||||
| 228 | context_path "/myapp"; | ||||||||||||
| 229 | |||||||||||||
| 230 | =cut | ||||||||||||
| 231 | |||||||||||||
| 232 | 0 | ▶ | sub context_path { | ||||||||||
|
Subroutine
context_path: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 233 | 0 | $context_path = shift; | |||||||||||
| 234 | } | ||||||||||||
| 235 | |||||||||||||
| 236 | ####### import function ####### | ||||||||||||
| 237 | |||||||||||||
| 238 | 0 | ▶ | sub import { | ||||||||||
|
Subroutine
import: not called CC=2 SCAR=10.1
| |||||||||||||
| 239 | |||||||||||||
| 240 | 1 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 241 | 0 | for my $func (@EXPORT) { | |||||||||||
| 242 | 0 | Rex::Logger::debug("Registering main::$func"); | |||||||||||
| 243 | 0 | *{"$_[1]::$func"} = \&$func; | |||||||||||
| 244 | } | ||||||||||||
| 245 | |||||||||||||
| 246 | } | ||||||||||||
| 247 | |||||||||||||
| 248 | =back | ||||||||||||
| 249 | |||||||||||||
| 250 | =cut | ||||||||||||
| 251 | |||||||||||||
| 252 | 1 | 1; | |||||||||||