-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.cgi
986 lines (805 loc) · 29.7 KB
/
setup.cgi
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
#!/usr/bin/perl
require './tomcat-lib.pl';
require 'java-lib.pl';
require 'jru-lib.pl';
require '../webmin/webmin-lib.pl'; #for OS detection
foreign_require('software', 'software-lib.pl');
foreign_require('apache', 'apache-lib.pl');
$www_user = 'www-data';
sub sort_version {
my @A = split(/\./, $a);
my @B = split(/\./, $b);
# a sort subroutine, expect $a and $b
for(my $i=0; $i < 3; $i++){
if ($A[$i] < $B[$i]) { return -1 } elsif ($A[$i] > $B[$i]) { return 1 }
}
return 0;
}
sub sort_version_des {
my @A = split(/\./, $a);
my @B = split(/\./, $b);
# a sort subroutine, expect $a and $b
for(my $i=0; $i < 3; $i++){
if ($A[$i] < $B[$i]) { return 1 } elsif ($A[$i] > $B[$i]) { return -1 }
}
return 0;
}
sub add_tomcat_user{
#check if tomcat user exists
if(read_file_contents('/etc/passwd') !~ /\ntomcat:/){
#add tomcat user
local $out = &backquote_command('useradd -m tomcat', 0);
}elsif(! -d '/home/tomcat'){
&make_dir("/home/tomcat", 0755, 1);
&set_ownership_permissions('tomcat','tomcat', undef, '/home/tomcat');
}
}
sub get_tomcat_major_versions(){
my @majors = ('8', '7','6', '9');
return @majors;
}
sub major_tomcat_versions{
my $major = $_[0]; #Tomcat major version 6,7,8
my $tmpfile = download_file("http://archive.apache.org/dist/tomcat/tomcat-$major/", 1);
if(! -f $tmpfile){
error($error);
}
my @latest_versions;
open(my $fh, '<', $tmpfile) or die "open:$!";
while(my $line = <$fh>){
if($line =~ /<a\s+href="v($major\.[0-9\.]+)\/">v[0-9\.]+\/<\/a>/){
push(@latest_versions, $1);
}
}
close $fh;
return sort sort_version @latest_versions;
}
sub download_and_install{
my $tomcat_ver;
my $major;
#download tomcat archive
if($in{'source'} == 100){
$tomcat_ver = $in{'source_archive'};
$major = substr($tomcat_ver, 0,1);
$in{'url'} = "http://archive.apache.org/dist/tomcat/tomcat-$major/v$tomcat_ver/bin/apache-tomcat-$tomcat_ver.tar.gz";
$in{'source'} = 2;
}
my $tmpfile = process_file_source();
if($tmpfile =~ /.*apache-tomcat-([0-9\.]+).tar.gz$/i){
$tomcat_ver = $1;
}else{
&error("Failed to match Tomcat version from archive");
}
$major = substr($tomcat_ver, 0,1);
#extract tomcat archive
print "<hr>Extracting to /home/tomcat/apache-tomcat-$tomcat_ver/ ...<br>";
exec_cmd("tar -x --overwrite -f \"$tmpfile\" -C/home/tomcat/");
print "Done<br>";
#folder is created after tomcat is started, but we need it now
&make_dir("/home/tomcat/apache-tomcat-$tomcat_ver/conf/Catalina/localhost/", 0755, 1);
open(my $fh, '>', "/home/tomcat/apache-tomcat-$tomcat_ver/conf/Catalina/localhost/manager.xml") or die "open:$!";
print $fh <<EOF;
<Context privileged="true" antiResourceLocking="false" docBase="\${catalina.home}/webapps/manager">
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*\$" />
</Context>
EOF
close $fh;
#&set_ownership_permissions('tomcat','tomcat', undef, "/home/tomcat/apache-tomcat-$tomcat_ver/");
&execute_command("chown -R tomcat:tomcat /home/tomcat/apache-tomcat-$tomcat_ver");
return $tomcat_ver;
}
sub setup_catalina_env{
my $tomcat_ver = $_[0];
my %os_env;
print "<hr>Setting CATALINA environment...";
read_env_file('/etc/environment', \%os_env);
$os_env{'CATALINA_HOME'} = "/home/tomcat/apache-tomcat-$tomcat_ver/";
$os_env{'CATALINA_BASE'} = "/home/tomcat/apache-tomcat-$tomcat_ver/";
write_env_file('/etc/environment', \%os_env, 0);
open(my $fh, '>>', "/home/tomcat/apache-tomcat-$tomcat_ver/bin/setenv.sh") or die "open:$!";
print $fh "CATALINA_PID=\"/home/tomcat/apache-tomcat-$tomcat_ver/temp/tomcat.pid\"";
close $fh;
}
sub setup_tomcat_users{
my $tomcat_ver = $_[0];
my @pw_chars = ("A".."Z", "a".."z", "0".."9", "_", "-");
my $manager_pass;
my $admin_pass;
$manager_pass .= $pw_chars[rand @pw_chars] for 1..32;
$admin_pass .= $pw_chars[rand @pw_chars] for 1..32;
#Save tomcat-users.xml
open(my $fh, '>', "/home/tomcat/apache-tomcat-$tomcat_ver/conf/tomcat-users.xml") or die "open:$!";
print $fh <<EOF;
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager-gui" />
<user username="manager" password="$manager_pass" roles="manager-gui" />
<role rolename="admin-gui" />
<user username="admin" password="$admin_pass" roles="manager-gui,admin-gui" />
</tomcat-users>
EOF
close $fh;
print "<hr>Setting Tomcat users...";
}
sub setup_tomcat_service{
my $tomcat_ver = $_[0];
copy_source_dest("$module_root_directory/tomcat.service", '/etc/systemd/system/tomcat.service');
my $ln = 0;
$lref = read_file_lines('/etc/systemd/system/tomcat.service');
foreach $line (@$lref){
chomp $line;
if($line =~ /\$TOMCAT_VER/){ #if its a section start
$line =~ s/\$TOMCAT_VER/$tomcat_ver/;
@{$lref}[$ln] = $line;
}elsif($line =~ /\$CATALINA_HOME/){
$line =~ s/\$CATALINA_HOME/\/home\/tomcat\/apache-tomcat-$tomcat_ver/;
@{$lref}[$ln] = $line;
}
$ln=$ln+1;
}
&flush_file_lines('/etc/systemd/system/tomcat.service');
&set_ownership_permissions('root','root', 0555, "/etc/systemd/system/tomcat.service");
&backquote_command("systemctl daemon-reload", undef, \$cmd_out, \$cmd_err, 0, 0);
print "<hr>Setting Tomcat service ...";
}
sub install_tomcat_from_archive{
add_tomcat_user();
my $tomcat_ver = download_and_install();
setup_catalina_env($tomcat_ver);
setup_tomcat_users($tomcat_ver);
setup_tomcat_service($tomcat_ver);
}
sub get_apache_proxy_file(){
my $proxy_file;
if( ( $osinfo{'real_os_type'} =~ /centos/i) or #CentOS
($osinfo{'real_os_type'} =~ /fedora/i) ){ #Fedora
if( ! -d '/etc/httpd/'){
return 0;
}
$proxy_file = '/etc/httpd/conf.d/includes/tomcat.conf';
}elsif( ($osinfo{'real_os_type'} =~ /ubuntu/i) or
($osinfo{'real_os_type'} =~ /debian/i) ){ #ubuntu or debian
if( ! -d '/etc/apache2/'){
return 0;
}
$proxy_file = '/etc/apache2/conf-available/tomcat.conf';
}
return $proxy_file;
}
sub setup_default_apache_proxy(){
my $proxy_file = get_apache_proxy_file();
if(-f $proxy_file){
return 0;
}
open(my $fh, '>', $proxy_file) or die "open:$!";
if( ($osinfo{'real_os_type'} =~ /centos/i) or #CentOS
($osinfo{'real_os_type'} =~ /fedora/i) ){ #Fedora
&exec_cmd('setsebool httpd_can_network_connect 1');
print $fh "LoadModule proxy_module modules/mod_proxy.so\n";
print $fh "LoadModule proxy_http_module modules/mod_proxy_http.so\n";
print $fh "LoadModule rewrite_module modules/mod_rewrite.so\n";
}elsif( $osinfo{'os_type'} =~ /debian/i){ #ubuntu or debian
print $fh "LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so\n";
print $fh "LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so\n";
print $fh "LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so\n";
}
print $fh "ProxyRequests Off\n";
print $fh "ProxyPreserveHost On\n";
print $fh " <Proxy *>\n";
print $fh " Order allow,deny\n";
print $fh " Allow from all\n";
print $fh " </Proxy>\n";
print $fh "ProxyPass / http://localhost:8080/\n";
print $fh "ProxyPassReverse / http://localhost:8080/\n";
close $fh;
print "Added proxy configuration / -> 8080 in $proxy_file\n";
}
sub select_jasper_version{
print "$text{'jru_desc4'}<p>\n";
print <<EOF;
<script type="text/javascript">
function update_versions(){
var checkBox = document.getElementsByName("show_beta")[0];
if (checkBox.checked == true){
get_pjax_content('/jri_publisher/setup.cgi?mode=select_jasper_version&show_beta=1');
}else{
get_pjax_content('/jri_publisher/setup.cgi?mode=select_jasper_version');
}
}
</script>
EOF
print &ui_form_start("setup.cgi", "form-data");
print ui_hidden('mode', 'install_jasper_reports');
print &ui_table_start($text{'base_options'}, undef, 2);
my $beta_enabled = 0;
if($in{'show_beta'}){
$beta_enabled = 1;
}
my %jr_vers = &get_jasper_reports_versions($beta_enabled);
my @jr_opts = ( );
foreach my $v (sort sort_version_des keys %jr_vers) {
if($v =~ /([0-9\-\.a-z]+) BETA$/){
push(@jr_opts, [ $jr_vers{$v}, $1 ]); #drop BETA from version label
}else{
push(@jr_opts, [ $jr_vers{$v}, $v ]);
}
}
print &ui_table_row($text{'base_installsource'},
&ui_radio_table("source", 100,
[ [100, $text{'jru_release'}, &ui_select("jr_ver", undef, \@jr_opts,1, 0).'</br>'.
&ui_checkbox("show_beta", 1, $text{'jru_show_beta'}, $beta_enabled, 'onclick="update_versions()"')],
[ 0, $text{'source_local'}, &ui_textbox("file", undef, 40)." ". &file_chooser_button("file", 0) ],
[ 1, $text{'source_uploaded'}, &ui_upload("upload", 40) ],
[ 2, $text{'source_ftp'},&ui_textbox("url", undef, 40) ]
]));
print &ui_table_end();
print &ui_form_end([ [ "", $text{'base_installok'} ] ]);
}
sub select_tomcat_archive{
print "$text{'base_desc1'}<p>\n";
print &ui_form_start("setup.cgi", "form-data");
print ui_hidden('mode', 'tomcat_install');
print &ui_table_start($text{'base_options'}, undef, 2);
my @tmver = sort sort_version_des &get_tomcat_major_versions();
my $sel_tmver = $in{'tmver'} || $tmver[0];
my @tm_opts = ( );
foreach my $v (@tmver) {
push(@tm_opts, [ $v, $v ]);
}
print <<EOF;
<script type="text/javascript">
function update_select(){
var majorSel = document.getElementById('base_major');
var major = majorSel.options[majorSel.selectedIndex].value;
get_pjax_content('/jri_publisher/setup.cgi?mode=tomcat_install_form&tmver='+major);
}
</script>
EOF
print &ui_table_row($text{'base_major'},
&ui_select("base_major", $sel_tmver, \@tm_opts, 1, 0, undef, undef, 'id="base_major" onchange="update_select()"'));
my @tver = sort sort_version_des &major_tomcat_versions($sel_tmver);
my @tver_opts = ( );
foreach my $v (@tver) {
push(@tver_opts, [ $v, $v ]);
}
print &ui_table_row($text{'base_installsource'},
&ui_radio_table("source", 100,
[ [ 100, $text{'source_archive'}, &ui_select("source_archive", undef, \@tver_opts,1, 0)],
[ 0, $text{'source_local'}, &ui_textbox("file", undef, 40)." ". &file_chooser_button("file", 0) ],
[ 1, $text{'source_uploaded'}, &ui_upload("upload", 40) ],
[ 2, $text{'source_ftp'},&ui_textbox("url", undef, 40) ]
]));
print &ui_table_end();
print &ui_form_end([ [ "", $text{'base_installok'} ] ]);
}
sub parse_jr_versions{
my $base_url = $_[0];
my %latest_versions;
my $tmpfile = download_file($base_url, 1);
if(! $tmpfile){
return %latest_versions;
}
open(my $fh, '<', $tmpfile) or die "open:$!";
while(my $line = <$fh>){
if($line =~ /<a\s+href="([0-9\.]+(\-beta)?)\/">[0-9\.]+(\-beta)?\/<\/a>/){
$latest_versions{$1} = $1.'@download';
}
}
close $fh;
return %latest_versions;
}
sub parse_jr_gh_versions{
my $base_url = 'https://github.com/daust/JasperReportsIntegration';
my %latest_versions;
my @tags = ();
my $tmpfile = download_file($base_url.'/tags', 1);
if(! $tmpfile){
return %latest_versions;
}
open(my $fh, '<', $tmpfile) or die "open:$!";
while(my $line = <$fh>){
# https://github.com/daust/JasperReportsIntegration/releases/tag/v2.10.1
if($line =~ /\/daust\/JasperReportsIntegration\/releases\/tag\/(v[0-9\.]+)/){
push(@tags, $1);
}
}
close $fh;
foreach my $tag (@tags) {
my $tmpfile = download_file($base_url.'/releases/expanded_assets/'.$tag, 1);
if(! $tmpfile){
return %latest_versions;
}
open(my $fh, '<', $tmpfile) or die "open:$!";
while(my $line = <$fh>){
# https://github.com/daust/JasperReportsIntegration/releases/download/v2.10.1/jri-2.10.1-jasper-6.20.0.zip
if($line =~ /<a\s+href="(\/daust\/JasperReportsIntegration\/releases\/download\/v([0-9\.]+)\/jri\-[0-9\.\-]+\-jasper\-[0-9\.\-]+\.zip)/){
$latest_versions{$2} = $2.'@'.$1;
}
}
close $fh;
}
return %latest_versions;
}
sub get_jasper_reports_versions(){
my $beta_enabled = $_[0];
my %jr_versions = parse_jr_versions('http://www.opal-consulting.de/downloads/free_tools/JasperReportsIntegration/');
my %gh_versions = parse_jr_gh_versions();
foreach my $v (keys %gh_versions){
$jr_versions{$v} = $gh_versions{$v};
}
if($beta_enabled){
my %beta_versions = parse_jr_versions('http://www.opal-consulting.de/downloads/free_tools/JasperReportsIntegration/Beta-releases/');
foreach my $v (keys %beta_versions){
$jr_versions{$v." BETA"} = $beta_versions{$v};
}
}
return %jr_versions;
}
sub get_jasper_archive_url{
my $jr_ver = $_[0];
my $beta_release = $_[1];
my $jr_site = $_[2];
#if our version is from github
if($jr_site =~ /\/daust\/JasperReportsIntegration\//){
return 'https://github.com'.$jr_site;
}
my $zip_ver = "${jr_ver}.0";
my $base_url = 'http://www.opal-consulting.de/downloads/free_tools/JasperReportsIntegration';
if($beta_release){
$base_url .= '/Beta-releases';
}
my $tmpfile = download_file($base_url.'/'.${jr_ver}.'/', 1);
if(! $tmpfile){
return "${base_url}/${jr_ver}/JasperReportsIntegration-${zip_ver}.zip";
}
open(my $fh, '<', $tmpfile) or die "open:$!";
while(my $line = <$fh>){
if($line =~ /<a\s+href="JasperReportsIntegration-([0-9\.]+)\.zip/){
$zip_ver = $1;
last;
}
}
close $fh;
return "${base_url}/${jr_ver}/JasperReportsIntegration-${zip_ver}.zip";
}
#set the oc.jasper.config.home manually
sub update_oc_jasper_config_home(){
my $webxml = get_catalina_home().'/webapps/JasperReportsIntegration/WEB-INF/web.xml';
if(! -f $webxml){
print "Error: $webxml not found. Update oc.jasper.config.home manually\n";
return;
}
my $lref = &read_file_lines($webxml);
my $lnum = 0;
foreach my $line (@$lref) {
if($line =~ /^[ \t]+<param\-name>oc\.jasper\.config\.home</){
@{$lref}[$lnum+1] = '<param-value>'.get_catalina_home().'/jasper_reports</param-value>';
last;
}
$lnum++;
}
flush_file_lines($webxml);
}
sub install_jasper_reports(){
my $catalina_home = get_catalina_home();
my $jasper_home = $catalina_home.'/jasper_reports';
print "<p>Installing Jasper Reports $jr_ver</p>";
if($in{'source'} == 100){
#get Jasper version
my @jr_ver_site = split(/@/, $in{'jr_ver'});
my $jr_ver = $jr_ver_site[0];
my $jr_site = $jr_ver_site[1];
my $beta_release = 0;
if($jr_ver =~ /([0-9\-\.a-z]+) BETA$/){
$jr_ver = $1;
$beta_release = 1;
}
$in{'url'} = get_jasper_archive_url($jr_ver, $beta_release, $jr_site);
$in{'source'} = 2;
}
my $tmpfile = process_file_source();
my $unzip_dir = unzip_me($tmpfile);
#github releases are in a subfolder
my $subdir = substr(file_basename($tmpfile), 0, -4); #take filename, and drop .zip
if(-d $unzip_dir.'/'.$subdir){
$unzip_dir = $unzip_dir.'/'.$subdir;
}
my $war_name = 'JasperReportsIntegration.war';
if( -f $unzip_dir.'/webapp/JasperReportsIntegration.war'){ #before v.2.6.1
$war_name = 'JasperReportsIntegration.war';
}elsif(-f $unzip_dir.'/webapp/jri.war'){ # from v.2.6.1
$war_name = 'jri.war';
}else{
die("Error: No war file found");
}
print "Installing $war_name</br>";
&rename_file($unzip_dir.'/webapp/'.$war_name, $catalina_home.'/webapps/JasperReportsIntegration.war');
#make the jasper home
&make_dir($jasper_home, 0750, 0);
&rename_file($unzip_dir.'/conf', $jasper_home.'/conf');
&rename_file($unzip_dir.'/reports', $jasper_home.'/reports');
&rename_file($unzip_dir.'/logs', $jasper_home.'/logs');
&make_dir($jasper_home.'/schedules', 0750, 0);
#TODO: Configure your database access
print '<b>Warning</b>: Skipping configuration of conf/application.properties</br>';
#set configuration directory
print 'shell: setConfigDir.sh</br>';
$tmpfile = &transname('script.sh');
open(my $fh, '>', $tmpfile) or die "open:$!";
print $fh "cd $unzip_dir/bin\n";
print $fh "chmod +x encryptPasswords.sh\n";
#print $fh "sh ./encryptPasswords.sh ${$jasper_home}/conf/application.properties\n";
if(-f $unzip_dir.'/bin/setConfigDir.sh'){
print $fh "chmod +x setConfigDir.sh\n";
print $fh "sh ./setConfigDir.sh $catalina_home/webapps/JasperReportsIntegration.war $jasper_home\n";
}
print $fh "chown -R tomcat:tomcat ${jasper_home}\n";
close $fh;
exec_cmd('bash '.$tmpfile);
print "Adding OC_JASPER_CONFIG_HOME to Tomcat setenv.sh</br>";
open(my $fh, '>>', $catalina_home.'/bin/setenv.sh') or die "open:$!";
print $fh "\nOC_JASPER_CONFIG_HOME=\"${jasper_home}\"";
close $fh;
# update reportsPath
my $app_prop = $jasper_home.'/conf/application.properties';
my $lref = &read_file_lines($app_prop);
my $lnum = 0;
foreach my $line (@$lref) {
if($line =~ /^[# ]*reportsPath=/){
@{$lref}[$lnum] = "reportsPath=${jasper_home}/reports";
last;
}
$lnum++;
}
flush_file_lines($app_prop);
tomcat_service_ctl('restart');
print "Done</br>";
}
sub install_gen_jri_report(){
©_source_dest($module_root_directory.'/gen_jri_report.sh', '/usr/local/bin');
&set_ownership_permissions('root', 'root', 0755, '/usr/local/bin/gen_jri_report.sh');
print 'Installed in /usr/local/bin/gen_jri_report.sh';
}
sub check_jdbc_pg_exists(){
my $catalina_home = get_catalina_home();
opendir(DIR, $catalina_home.'/lib') or die $!;
my @jars
= grep { /^postgresql\-[0-9\.]+\.jar$/ # pg jar
&& -f "$catalina_home/lib/$_" # and is a file
} readdir(DIR);
closedir(DIR);
if(@jars){
return $catalina_home.'/lib/'.$jars[0];
}else{
return $catalina_home.'/lib/';
}
}
sub check_jdbc_mysql_exists(){
my $catalina_home = get_catalina_home();
opendir(DIR, $catalina_home.'/lib') or die $!;
my @jars
= grep { /^mysql-connector-j\-[0-9\.]+\.jar$/ # pg jar
&& -f "$catalina_home/lib/$_" # and is a file
} readdir(DIR);
closedir(DIR);
if(@jars){
return $catalina_home.'/lib/'.$jars[0];
}else{
return $catalina_home.'/lib/';
}
}
sub check_jdbc_mssql_exists(){
my $catalina_home = get_catalina_home();
opendir(DIR, $catalina_home.'/lib') or die $!;
my @jars
= grep { /^mssql-jdbc\-[0-9\.]+\.jre[0-9]+\.jar$/ # mssql jar
&& -f "$catalina_home/lib/$_" # and is a file
} readdir(DIR);
closedir(DIR);
if(@jars){
return $catalina_home.'/lib/'.$jars[0];
}else{
return $catalina_home.'/lib/';
}
}
sub jri_add_datasource{
my $ds = $_[0];
my $ds_name = $_[1];
open(my $fh, '>>', get_catalina_home().'/jasper_reports/conf/application.properties') or die "open:$!";
print $fh "[datasource:$ds]\n";
print $fh "type=jndi\n";
print $fh "name=$ds_name\n";
close $fh
}
sub install_jri_pg(){
#download JDBC versions page
my $tmpfile = download_file('https://jdbc.postgresql.org/download', 1);
if(!$tmpfile){
die('Error: Failed to get JDBC PG page');
}
#find latest
$jdbc_pg_ver = '';
open(my $fh, '<', $tmpfile) or die "open:$!";
while(my $line = <$fh>){
if($line =~ /<a\s+href="\/download\/postgresql\-([0-9\.]+)\.jar/){
$jdbc_pg_ver = $1;
last;
}
}
close $fh;
print "Downloading JDBC PG ver. ".$jdbc_pg_ver."</br>";
$tmpfile = download_file('https://jdbc.postgresql.org/download/postgresql-'.$jdbc_pg_ver.'.jar');
if(!$tmpfile){
die('Error: Failed to get JDBC PG jar');
}
my $jar_filepath = get_catalina_home().'/lib/'.file_basename($tmpfile);
&rename_file($tmpfile, $jar_filepath);
print "Moving jar to ".$jar_filepath."</br>";
jri_add_pg_resource('jdbc/postgres', 'jdbc:postgresql://localhost:5432/xxx', 'xxx', 'xxx');
jri_add_datasource('postgres', 'postgres');
print "Done</br>";
}
sub install_jri_mysql(){
#download JDBC versions page
my $tmpfile = &transname('mysql.html');
exec_cmd('wget -O'.$tmpfile.' "https://dev.mysql.com/downloads/connector/j/"');
if(!$tmpfile){
die('Error: Failed to get JDBC MySQL page');
}
#find latest
$jdbc_mysql_ver = '';
open(my $fh, '<', $tmpfile) or die "open:$!";
while(my $line = <$fh>){
if($line =~ /<h1>Connector\/J[ \t]+([0-9\.]+)[ \t]/){
$jdbc_mysql_ver = $1;
last;
}
}
close $fh;
if(!$jdbc_mysql_ver){
die('Error: Failed to parse JDBC MySQL version');
}
print "Downloading JDBC MySQL ver. ".$jdbc_mysql_ver."</br>";
$tmpfile = &transname('mysql-connector-java-'.$jdbc_mysql_ver.'.zip');
exec_cmd('wget -O'.$tmpfile.' https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-j-'.$jdbc_mysql_ver.'.zip');
if(!$tmpfile){
die('Error: Failed to get JDBC MySQL zip');
}
my $unzip_dir = unzip_me($tmpfile);
my $jar_filepath = get_catalina_home().'/lib/mysql-connector-j-'.$jdbc_mysql_ver.'.jar';
&rename_file($unzip_dir.'/mysql-connector-j-'.$jdbc_mysql_ver.'/mysql-connector-j-'.$jdbc_mysql_ver.'.jar', $jar_filepath);
print "Moving jar to ".$jar_filepath."</br>";
jri_add_mysql_resource('jdbc/MySQL', 'jdbc:mysql://localhost:3306/xxx', 'xxx', 'xxx');
jri_add_datasource('MySQL', 'MySQL');
print "Done</br>";
}
sub install_jri_mssql(){
#download JDBC versions page
my $tmpfile = download_file('https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15', 1);
if(!$tmpfile){
die('Error: Failed to get JDBC MySQL page');
}
#find latest
$jdbc_mssql_ver = '';
$jdbc_mssql_url = 'https://go.microsoft.com/fwlink/?linkid=2137600';
open(my $fh, '<', $tmpfile) or die "open:$!";
while(my $line = <$fh>){
if($line =~ /Download Microsoft JDBC Driver ([0-9\.]+)/){
$jdbc_mssql_ver = $1;
if($line =~ /"(https:\/\/go\.microsoft\.com\/fwlink\/\?linkid=[0-9]+)"/){
$jdbc_mssql_url = $1;
last;
}
}
}
close $fh;
if(!$jdbc_mssql_url){
die('Error: Failed to parse JDBC MySQL version');
}
print "Downloading JDBC MySQL ver. ".$jdbc_mssql_ver."</br>";
$tmpfile = download_file($jdbc_mssql_url);
if(!$tmpfile){
die('Error: Failed to get JDBC MySQL zip');
}
my $unzip_dir = unzip_me($tmpfile);
#find which java we have
my %jv = get_java_version();
my $jdk_major = $jv{'major'};
my $sqljdbc_dir = $unzip_dir.'/sqljdbc_'.$jdbc_mssql_ver.'/enu/';
opendir(DIR, $sqljdbc_dir) or die $!;
my @jars = grep { /^mssql\-jdbc\-[0-9\.]+\.jre$jdk_major\.jar/ && -f "$sqljdbc_dir/$_" } readdir(DIR);
closedir(DIR);
if(!@jars){
# take all jars
opendir(DIR, $sqljdbc_dir) or die $!;
@jars = grep { /^mssql\-jdbc\-[0-9\.]+\.jre[0-9]+\.jar/ && -f "$sqljdbc_dir/$_" } readdir(DIR);
closedir(DIR);
if(!@jars){
die('Error: Failed to get JDBC MySQL jar for JDK '.$jv{'major'});
}else{
# sort in reverse to get latest jar
@jars = sort @jars;
}
}
my $jar_filepath = get_catalina_home().'/lib/'.$jars[0];
&rename_file($sqljdbc_dir.'/'.$jars[0], $jar_filepath);
print "Moving jar to ".$jar_filepath."</br>";
jri_add_mssql_resource('jdbc/MSSQL', 'jdbc:sqlserver://localhost:1433;databaseName=xxx', 'xxx', 'xxx');
jri_add_datasource('MSSQL', 'MSSQL');
print "Done</br>";
}
sub install_email_template(){
my $tmp_dir = get_email_tmpl_dir();
if(! -d $tmp_dir){
&make_dir($tmp_dir, 0755, 1);
&set_ownership_permissions('tomcat','tomcat', undef, $tmp_dir);
}
&rename_file($module_root_directory.'/email_template.html', $tmp_dir.'/email_template.html');
print "Done</br>";
}
sub install_html_app(){
my $app_dir = $module_root_directory.'/app';
&unlink_file('/var/www/html');
&rename_file($app_dir, '/var/www/html');
&exec_cmd("chown -R $www_user:$www_user /var/www/html");
opendir(DIR, '/var/www/html/portal') or die $!;
my @portal_files = grep { -f '/var/www/html/portal/'.$_ } readdir(DIR);
closedir(DIR);
if (! -d '/etc/webmin/authentic-theme/'){
&make_dir('/etc/webmin/authentic-theme/', 0755, 1);
}
foreach my $f (@portal_files){
©_source_dest('/var/www/html/portal/'.$f, '/etc/webmin/authentic-theme/'.$f);
}
my $hname = get_system_hostname();
my $ln=0;
my $html_file = '/var/www/html/index.html';
$lref = &read_file_lines($html_file);
foreach my $line (@$lref){
chomp($line);
if($line =~ /xyzIP/){
$line =~ s/xyzIP/$hname/g;
@{$lref}[$ln] = $line;
}
$ln++;
}
flush_file_lines($html_file);
print "Done</br>";
}
sub setup_checks{
#Check for commands
if (!&has_command('java')) {
print '<p>Warning: Java is not found. Install it manually or from the '.
"<a href='./edit_java.cgi?return=%2E%2E%2Fjri_publisher%2Fsetup.cgi&returndesc=Setup&caller=jri_publisher'>Java tab</a></p>";
}
my @pinfo = software::package_info('haveged', undef, );
if(!@pinfo){
if( $osinfo{'real_os_type'} =~ /centos/i){ #CentOS
@pinfo = software::package_info('epel-release', undef, );
if(!@pinfo){
print "<p>Warning: haveged needs epel-release. Install it manually or ".
"<a href='../package-updates/update.cgi?mode=new&source=3&u=epel-release&redir=%2E%2E%2Fjri_publisher%2Fsetup.cgi&redirdesc=Setup'>click here</a> to have it downloaded and installed.</p>";
}
}
print "<p>Warning: haveged package is not installed. Install it manually or ".
"<a href='../package-updates/update.cgi?mode=new&source=3&u=haveged&redir=%2E%2E%2Fjri_publisher%2Fsetup.cgi&redirdesc=Setup'>click here</a> to have it downloaded and installed.</p>";
}
my $tomcat_ver = installed_tomcat_version();
if(!$tomcat_ver){
print "<p>Apache Tomcat is not found. <a href='setup.cgi?mode=tomcat_install_form&return=%2E%2E%2Fjri_publisher%2Fsetup.cgi&returndesc=Setup&caller=jri_publisher'>Click here</a> to install Tomcat from Apache site.</p>";
}
my @pkg_deps;
if( ( $osinfo{'real_os_type'} =~ /centos/i) or #CentOS
($osinfo{'real_os_type'} =~ /fedora/i) ){ #Fedora
@pkg_deps = ('httpd', 'unzip', 'wget', 'mutt', 'zip');
}elsif( ($osinfo{'real_os_type'} =~ /ubuntu/i) or
($osinfo{'real_os_type'} =~ /debian/i) ){ #ubuntu or debian
@pkg_deps = ('apache2', 'unzip', 'wget', 'mutt', 'zip');
}
my @pkg_missing;
foreach my $pkg (@pkg_deps){
my @pinfo = software::package_info($pkg);
if(!@pinfo){
push(@pkg_missing, $pkg);
}
}
if(@pkg_missing){
my $url_pkg_list = '';
foreach my $pkg (@pkg_missing){
$url_pkg_list .= '&u='.&urlize($pkg);
}
my $pkg_list = join(', ', @pkg_missing);
print "<p>Warning: Missing package dependencies - $pkg_list - are not installed. Install them manually or ".
"<a href='../package-updates/update.cgi?mode=new&source=3${url_pkg_list}&redir=%2E%2E%2Fjri_publisher%2Fsetup.cgi&redirdesc=Setup'>click here</a> to have them installed.</p>";
}
my $proxy_file = get_apache_proxy_file();
if(! -f $proxy_file){
print "<p>Apache default proxy is not configured. ".
"<a href='./setup.cgi?mode=setup_apache_proxy&return=%2E%2E%2Fjri_publisher%2Fsetup.cgi&returndesc=Setup&caller=jri_publisher'>click here</a></p>";
}
if($tomcat_ver){
my $catalina_home = get_catalina_home();
if(! -f $catalina_home.'/webapps/JasperReportsIntegration.war'){
print "<p>JasperReportsIntegration is not installed. To select version and install, ".
"<a href='./setup.cgi?mode=select_jasper_version&return=%2E%2E%2Fjri_publisher%2Fsetup.cgi&returndesc=Setup&caller=jri_publisher'>click here</a></p>";
}
if(! -f check_jdbc_pg_exists()){
print "<p>JRI PG support is not installed. To install it ".
"<a href='./setup.cgi?mode=install_jri_pg&return=%2E%2E%2Fjri_publisher%2Fsetup.cgi&returndesc=Setup&caller=jri_publisher'>click here</a></p>";
}
if(! -f check_jdbc_mysql_exists()){
print "<p>JRI MySQL support is not installed. To install it ".
"<a href='./setup.cgi?mode=install_jri_mysql&return=%2E%2E%2Fjri_publisher%2Fsetup.cgi&returndesc=Setup&caller=jri_publisher'>click here</a></p>";
}
if(! -f check_jdbc_mssql_exists()){
print "<p>JRI MSSQL support is not installed. To install it ".
"<a href='./setup.cgi?mode=install_jri_mssql&return=%2E%2E%2Fjri_publisher%2Fsetup.cgi&returndesc=Setup&caller=jri_publisher'>click here</a></p>";
}
}
if(! -f '/usr/local/bin/gen_jri_report.sh'){
print "<p>JRI report script is not installed. To install it ".
"<a href='./setup.cgi?mode=install_gen_jri_report&return=%2E%2E%2Fjri_publisher%2Fsetup.cgi&returndesc=Setup&caller=jri_publisher'>click here</a></p>";
}
if(! -d get_email_tmpl_dir()){
print "<p>JRI email template is not installed. To install it ".
"<a href='./setup.cgi?mode=install_email_template&return=%2E%2E%2Fjri_publisher%2Fsetup.cgi&returndesc=Setup&caller=jri_publisher'>click here</a></p>";
}
if( -d $module_root_directory.'/app'){
print "<p>HTML App is not installed. To install it ".
"<a href='./setup.cgi?mode=install_html_app&return=%2E%2E%2Fjri_publisher%2Fsetup.cgi&returndesc=Setup&caller=jri_publisher'>click here</a></p>";
}
print '<p>If you don\'t see any warning above, you can complete setup from '.
"<a href='setup.cgi?mode=cleanup&return=%2E%2E%2Fjri_publisher%2F&returndesc=Setup&caller=jri_publisher'>here</a></p>";
}
#Remove all setup files
sub setup_cleanup{
my $file = $module_root_directory.'/setup.cgi';
print "Completing Installation</br>";
&unlink_file($file);
my @mods = ('proxy', 'proxy_http');
foreach my $mod (@mods){
my $err = &apache::add_configured_apache_module($mod);
#if($err){
# print "Warning:Apache:$mod:$err</br>";
#}
}
&apache::restart_apache();
update_oc_jasper_config_home();
print &js_redirect("index.cgi");
}
&ui_print_header(undef, $text{'setup_title'}, "");
print "<img src='images/wizard.png'> <span style='font-size: 24px; color: cadetblue'> Set Up Wizard </span>";
print "<p> </p>";
if($ENV{'CONTENT_TYPE'} =~ /boundary=(.*)$/) {
&ReadParseMime();
}else {
&ReadParse(); $no_upload = 1;
}
%osinfo = &detect_operating_system();
if( ( $osinfo{'real_os_type'} =~ /centos/i) or #CentOS
($osinfo{'real_os_type'} =~ /fedora/i) or #Fedora
($osinfo{'real_os_type'} =~ /scientific/i) ){
$www_user = 'apache';
}
my $mode = $in{'mode'} || "checks";
if($mode eq "checks"){ setup_checks();
&ui_print_footer('', $text{'index_return'});
exit 0;
}elsif($mode eq "cleanup"){ setup_cleanup();
&ui_print_footer('', $text{'index_return'});
exit 0;
}elsif($mode eq "tomcat_install_form"){ select_tomcat_archive();
}elsif($mode eq "select_jasper_version"){ select_jasper_version();
}elsif($mode eq "tomcat_install"){ install_tomcat_from_archive();
}elsif($mode eq "setup_apache_proxy"){ setup_default_apache_proxy();
}elsif($mode eq "install_jasper_reports"){ install_jasper_reports();
}elsif($mode eq "install_gen_jri_report"){ install_gen_jri_report();
}elsif($mode eq "install_jri_pg"){ install_jri_pg();
}elsif($mode eq "install_jri_mysql"){ install_jri_mysql();
}elsif($mode eq "install_jri_mssql"){ install_jri_mssql();
}elsif($mode eq "install_email_template"){ install_email_template();
}elsif($mode eq "install_html_app"){ install_html_app();
}else{
print "Error: Invalid setup mode\n";
}
&ui_print_footer('setup.cgi', $text{'setup_title'});