File Coverage

blib/lib/Rex/Apache/Deploy/JBoss.pm
Criterion Covered Total %
statement 30 80 37.5
branch 0 18 0.0
condition 0 6 0.0
subroutine 10 14 71.4
pod 2 2 100.0
total 42 120 35.0


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::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     1   1838 use strict;
  1         3  
  1         31  
35 1     1   6 use warnings;
  1         2  
  1         33  
36 1     1   6 use File::Basename qw'basename';
  1         2  
  1         55  
37 1     1   6 use File::Spec;
  1         2  
  1         65  
38 1     1   26 use Rex::Commands::Upload;
  1         2  
  1         8  
39 1     1   81 use Rex::Commands::Fs;
  1         2  
  1         8  
40 1     1   533 use Rex::Commands::Run;
  1         2  
  1         8  
41 1     1   102 use Rex::Commands;
  1         2  
  1         6  
42              
43 1     1   922 use vars qw(@EXPORT $context_path);
  1         2  
  1         809  
44             @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 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             sub deploy {
62 0     0 1   my ( $file, %option ) = @_;
63              
64 0           my $abs_file = File::Spec->rel2abs($file);
65              
66 0 0         if ( !%option ) {
67 0 0         if ( Rex::Config->get("package_option") ) {
68 0           %option = %{ Rex::Config->get("package_option") };
  0            
69             }
70             }
71              
72 0 0 0       if ( exists $option{context_path} || $context_path ) {
73             LOCAL {
74 0     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 0         if ( $? != 0 ) {
80 0           rmdir "tmp/$$";
81 0           Rex::Logger::info( "Error extracting $file.", "error" );
82 0           die("Error extracting $file.");
83             }
84              
85 0 0         if ( $file =~ m/\.ear$/ ) {
    0          
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 0         if ( !-f "tmp/$$/$inf_file" ) {
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   0       my $context = $option{context_path} || $context_path;
110 0           while ( my $line = <$file_in> ) {
111 0           chomp $line;
112 0 0         if ( $line =~ m// ) {
113 0           $line =~
114             s/([^>]+)<\/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 0         if ( $? != 0 ) {
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 function.
142              
143             context_path "/myapp";
144              
145             =cut
146              
147             sub context_path {
148 0     0 1   $context_path = shift;
149             }
150              
151             =back
152              
153             =cut
154              
155             ####### import function #######
156              
157             sub import {
158              
159 1     1   8 no strict 'refs';
  1         2  
  1         121  
160 0     0     for my $func (@EXPORT) {
161 0           Rex::Logger::debug("Registering main::$func");
162 0           *{"$_[1]::$func"} = \&$func;
  0            
163             }
164              
165             }
166              
167             1;