-
Notifications
You must be signed in to change notification settings - Fork 3
/
tomcat-lib.pl
244 lines (208 loc) · 5.64 KB
/
tomcat-lib.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
=head1 tomcat-lib.pl
Functions for managing Tomcat server configuration files.
foreign_require("tomcat", "tomcat-lib.pl");
@sites = tomcat::list_tomcat_websites()
=cut
BEGIN { push(@INC, ".."); };
use WebminCore;
use File::Copy;
use File::Basename;
init_config();
sub get_jri_publisher_config
{
my $lref = &read_file_lines($config{'foobar_conf'});
my @rv;
my $lnum = 0;
foreach my $line (@$lref) {
my ($n, $v) = split(/\s+/, $line, 2);
if ($n) {
push(@rv, { 'name' => $n, 'value' => $v, 'line' => $lnum });
}
$lnum++;
}
return @rv;
}
# Returns a hash containing the version type, number and full version
sub get_catalina_version
{
local %version;
local $catalina_home = get_catalina_home();
local $out = &backquote_command($catalina_home."/bin/catalina.sh version 2>&1 </dev/null");
if ($out =~ /(Server\s+version:\s+([a-z\s]+)\/([0-9\.]+))/i) {
$version{'type'} = $2;
$version{'number'} = $3;
$version{'full'} = $1;
}else {
$version{'type'} = 'unknown';
$version{'number'} = 0.0;
$version{'full'} = 'unknown/0.0';
}
if ($out =~ /JVM Version:\s+(.*)/i ){
$version{'jvm'} = $1;
}else{
$version{'jvm'} = 'unknown';
}
return %version;
}
sub tomcat_service_ctl{
my $ctl = $_[0];
my $status = '';
local $out = &execute_command("systemctl $ctl tomcat", undef, \$cmd_out, \$cmd_err, 0, 0);
if($cmd_err ne ""){
$status = $cmd_err;
}else{
my @kw = ('Main PID', 'Active', 'Tasks','Memory');
foreach $k (@kw) {
if($cmd_out =~ /$k: (.*)/i){
$status .= '<b>'.$k.'</b>: '.$1."</br>";
}
}
}
$running = $out >> 8;
if($ctl eq 'status'){
# swap running values, to match init.d logic
if($running == 0){
$running = 1;
}elsif($running > 0){
$running = 0;
}
}
return ($running, $status);
}
sub get_all_war_infos(){
my $catalina_home = get_catalina_home();
opendir(DIR, $catalina_home.'/webapps') or die $!;
my @dirs
= grep {
! /^\./ # Doesn't begins with a period
&& -d "$catalina_home/webapps/$_" # and is a directory
} readdir(DIR);
closedir(DIR);
return sort @dirs;
}
sub file_basename
{
my $rv = $_[0];
$rv =~ s/^.*[\/\\]//;
return $rv;
}
sub installed_tomcat_version(){
my %os_env;
read_env_file('/etc/environment', \%os_env);
if($os_env{'CATALINA_HOME'}){
$os_env{'CATALINA_HOME'} =~ /\/home\/tomcat\/apache-tomcat-([0-9\.]+)/;
return $1;
}else{
return undef;
}
}
sub get_catalina_home(){
my $tomcat_ver = installed_tomcat_version();
return "/home/tomcat/apache-tomcat-$tomcat_ver";
}
sub get_installed_libs{
opendir(DIR, $module_config_directory) or die $!;
my @lib_lists
= grep {
/^lib_[a-z0-9\.\-_\s]+\.list$/i #
&& -f "$module_config_directory/$_" # and is a file
} readdir(DIR);
closedir(DIR);
return sort @lib_lists;
}
sub process_file_source{
my $file = '';
if (($in{'source'} == 0) && ($in{'file'} ne "")) { # from local file
&error_setup(&text('source_err0', $in{'file'}));
$file = $in{'file'};
if (!(-r $file)){
&inst_error($text{'source_err0'});
}
}elsif (($in{'source'} == 1) && ($in{'upload_filename'} ne "")) { # from uploaded file
&error_setup($text{'source_err1'});
$need_unlink = 1;
if ($no_upload) {
&inst_error($text{'source_err1.2'});
}
$file = transname(file_basename($in{'upload_filename'}));
open(MOD, ">$file");
binmode(MOD);
print MOD $in{'upload'};
close(MOD);
}elsif ($in{'source'} == 2 and $in{'url'} ne '') { # from ftp or http url (possible third-party)
$url = $in{'url'};
&error_setup(&text('source_err2', $url));
$file = &transname(file_basename($url));
$need_unlink = 1;
my $error;
$progress_callback_url = $url;
if ($url =~ /^(http|https):\/\/([^\/]+)(\/.*)$/) {
$ssl = $1 eq 'https';
$host = $2; $page = $3; $port = $ssl ? 443 : 80;
if ($host =~ /^(.*):(\d+)$/) { $host = $1; $port = $2; }
my %cookie_headers = ('Cookie'=>'oraclelicense=accept-securebackup-cookie');
&http_download($host, $port, $page, $file, \$error,
\&progress_callback, $ssl, undef, undef, 0, 0, 1, \%cookie_headers);
} elsif (
$url =~ /^ftp:\/\/([^\/]+)(:21)?\/(.*)$/) {
$host = $1; $ffile = $3;
&ftp_download($host, $ffile, $file, \$error, \&progress_callback);
}else {
&inst_error($text{'source_err3'});
}
&inst_error($error) if ($error);
}
return $file;
}
sub download_file{
my $url = $_[0];
my ($proto, $x, $host, $path) = split('/', $url, 4);
my @paths = split('/', $url);
my $filename = $paths[-1];
if($filename eq ''){
$filename = 'index.html';
}
my $sslmode = $proto eq 'https:';
my $port = 80;
if($sslmode){
$port = 443;
}
&error_setup(&text('install_err3', $url));
my $tmpfile = &transname($filename);
$progress_callback_url = $url;
&http_download($host, $port, '/'.$path, $tmpfile, \$error, \&progress_callback, $sslmode);
if($error){
print &html_escape($error);
return '';
}
return $tmpfile;
}
sub exec_cmd{
my $cmd = $_[0];
my $cmd_out='';
my $rv = &execute_command($cmd, undef, \$cmd_out, \$cmd_out, 0, 0);
if($cmd_out){
$cmd_out = &html_escape($cmd_out);
$cmd_out =~ s/[\r\n]/<\/br>/g;
print $cmd_out;
}
return $rv;
}
sub unzip_me{
my $file = $_[0];
my @suffixlist = ('\.zip');
($file_name,$path,$lib_suffix) = fileparse($file,@suffixlist);
my $unzip_dir = &tempname();
#if old temp extension dir exist, remove it
#if( -d $unzip_dir and rmtree($unzip_dir) == 0){
# &error("Failed to remove temp extension dir");
# &ui_print_footer("", $text{'index_return'});
# exit;
#}
&make_dir($unzip_dir, 0754, 1);
my $unzip_out;
my $unzip_err;
print "<hr>Unzipping to $unzip_dir ...<br>";
exec_cmd("unzip -u \"$file\" -d \"$unzip_dir\"");
return $unzip_dir;
}