| 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::JBoss - Deploy application to JBoss. | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | With this module you can deploy EAR/WAR archives to JBoss. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | use Rex::Apache::Deploy qw/JBoss/; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | context_path "/myapp"; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | task "dodeploy", "j01", "j02", sub { | ||||||||||||||||||||||||||||
| 22 | deploy "myapp.ear", | ||||||||||||||||||||||||||||
| 23 | deploy_path => "/opt/jboss/server/default/deploy"; | ||||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =over 4 | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | package Rex::Apache::Deploy::JBoss; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | use File::Basename qw'basename'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | use Rex::Commands::Upload; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | use Rex::Commands::Fs; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | use Rex::Commands::Run; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use Rex::Commands; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | use vars qw(@EXPORT $context_path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 1 | @EXPORT = qw(deploy context_path); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | ############ deploy functions ################ | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =item deploy($file, %option) | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 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. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | task "dodeploy", "j01", "j02", sub { | ||||||||||||||||||||||||||||
| 53 | deploy "myapp.war", | ||||||||||||||||||||||||||||
| 54 | context_path => "/myapp", | ||||||||||||||||||||||||||||
| 55 | deploy_path => "/opt/jboss/server/default/deploy"; | ||||||||||||||||||||||||||||
| 56 | }; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub deploy { | ||||||||||||||||||||||||||
|
Subroutine
deploy: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 0 | my ( $file, %option ) = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my $abs_file = File::Spec->rel2abs($file); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if ( !%option ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( Rex::Config->get("package_option") ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | %option = %{ Rex::Config->get("package_option") }; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ( exists $option{context_path} || $context_path ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $option{"context_path"} or $Rex::Apache::Deploy::JBoss::context_path
MC/DC0%0/2
exists $option{"context_path"} or $Rex::Apache::Deploy::JBoss::context_path
exists $option{"context_path"}
$Rex::Apache::Deploy::JBoss::context_path
Truth table0%0/3
A or B
A= exists $option{"context_path"}B= $Rex::Apache::Deploy::JBoss::context_path
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | LOCAL { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 74 | 0 | Rex::Logger::debug("Context-Path given, need to extract archive."); | |||||||||||||||||||||||||||
| 75 | 0 | my $inf_file; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | mkdir "tmp/$$"; | |||||||||||||||||||||||||||
| 78 | 0 | run "cd tmp/$$; unzip $abs_file"; | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | rmdir "tmp/$$"; | |||||||||||||||||||||||||||
| 81 | 0 | Rex::Logger::info( "Error extracting $file.", "error" ); | |||||||||||||||||||||||||||
| 82 | 0 | die("Error extracting $file."); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ( $file =~ m/\.ear$/ ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 86 | 0 | $inf_file = "META-INF/application.xml"; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | elsif ( $file =~ m/\.war$/ ) { | ||||||||||||||||||||||||||||
| 89 | 0 | $inf_file = "WEB-INF/jboss-web.xml"; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||
| 92 | 0 | Rex::Logger::info( | |||||||||||||||||||||||||||
| 93 | "Can't set context path for this file ($file). Only .ear and .war are supported.", | ||||||||||||||||||||||||||||
| 94 | "error" | ||||||||||||||||||||||||||||
| 95 | ); | ||||||||||||||||||||||||||||
| 96 | 0 | die( | |||||||||||||||||||||||||||
| 97 | "Can't set context path for this file ($file). Only .ear and .war are supported." | ||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ( !-f "tmp/$$/$inf_file" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | Rex::Logger::info( "Can't find file $inf_file.", "error" ); | |||||||||||||||||||||||||||
| 103 | 0 | rmdir "tmp/$$"; | |||||||||||||||||||||||||||
| 104 | 0 | die("Can't find file $inf_file."); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | open my $file_in, "<", "tmp/$$/$inf_file"; | |||||||||||||||||||||||||||
| 108 | 0 | my @new_content; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | my $context = $option{context_path} || $context_path; | ||||||||||||||||||||||||||
|
Condition0%0/3
$option{'context_path'} || $Rex::Apache::Deploy::JBoss::context_path
MC/DC0%0/2
$option{'context_path'} || $Rex::Apache::Deploy::JBoss::context_path
$option{'context_path'}
$Rex::Apache::Deploy::JBoss::context_path
Truth table0%0/3
A || B
A= $option{'context_path'}B= $Rex::Apache::Deploy::JBoss::context_path
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | while ( my $line = <$file_in> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | chomp $line; | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | if ( $line =~ m/<context\-root>/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | $line =~ | |||||||||||||||||||||||||||
| 114 | s/<context\-root>([^>]+)<\/context\-root>/<context\-root>$context<\/context\-root>/; | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | push @new_content, $line; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 0 | close $file_in; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | open my $file_out, ">", "tmp/$$/$inf_file"; | |||||||||||||||||||||||||||
| 122 | 0 | print $file_out join( "\n", @new_content ); | |||||||||||||||||||||||||||
| 123 | 0 | close $file_out; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | mv $abs_file, "tmp/" . basename($file) . ".old"; | |||||||||||||||||||||||||||
| 126 | 0 | run "cd tmp/$$; zip -r $abs_file *"; | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | rmdir "tmp/$$"; | |||||||||||||||||||||||||||
| 129 | 0 | Rex::Logger::info( "Error creating ear archive.", "error" ); | |||||||||||||||||||||||||||
| 130 | 0 | die("Error creating ear archive."); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 0 | rmdir "tmp/$$"; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 0 | } | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | upload $abs_file, "$option{deploy_path}/" . basename($abs_file); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =item context_path($path) | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 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. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | context_path "/myapp"; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | sub context_path { | ||||||||||||||||||||||||||
|
Subroutine
context_path: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 0 | $context_path = shift; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =back | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =cut | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | ####### import function ####### | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: not called CC=2 SCAR=10.1
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | 0 | for my $func (@EXPORT) { | |||||||||||||||||||||||||||
| 161 | 0 | Rex::Logger::debug("Registering main::$func"); | |||||||||||||||||||||||||||
| 162 | 0 | *{"$_[1]::$func"} = \&$func; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 1 | 1; | |||||||||||||||||||||||||||