Guest User

Untitled

a guest
Jul 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use File::Monitor;
  7. use File::Path::Expand;
  8. use File::Copy;
  9. use File::Remove;
  10. use App::Rad;
  11.  
  12. App::Rad->run();
  13.  
  14. sub setup {
  15. my $c = shift;
  16. $c->register_commands( {
  17. watch => 'Watch a WAR file. arguments: --war=WAR --tomcat=TOMCAT'
  18. });
  19. }
  20.  
  21. sub redeploy {
  22. my ($war, $tomcat) = @_;
  23. my $webapps_dir = "$tomcat/webapps";
  24. warn "War file $war has changed\n\n";
  25. warn "-- STOPPING TOMCAT --\n";
  26. system("$tomcat/bin/catalina.sh stop");
  27. sleep 5;
  28. warn "-- CLEARING WEBAPPS DIRECTORY --\n";
  29. File::Remove::remove(\1, "$webapps_dir/*");
  30. File::Copy::copy($war, "$webapps_dir/ROOT.war");
  31. warn "-- STARTING TOMCAT --\n";
  32. system("$tomcat/bin/catalina.sh start");
  33. print "Restarted tomcat. Redeployment will likely take ~30 seconds...\n"
  34. }
  35.  
  36. sub watch {
  37. my $c = shift;
  38. if (!( $c->options->{'war'} && $c->options->{'tomcat'} )) {
  39. return "you didn't give me the correct arguments";
  40. }
  41.  
  42. my $war = File::Path::Expand::expand_filename($c->options->{'war'});
  43. my $tomcat = File::Path::Expand::expand_filename($c->options->{'tomcat'});
  44. my $webapps_dir = "$tomcat/webapps";
  45.  
  46. # test war exists
  47. if (not -f $war) {
  48. return "$war file (war) does not exist.";
  49. }
  50.  
  51. if (not -d $webapps_dir) {
  52. return "$tomcat/webapps directory does not exist.";
  53. }
  54.  
  55. my $monitor = File::Monitor->new();
  56. my $hasChanged = 0;
  57. my $lastHasChanged = 0;
  58.  
  59. $monitor->watch( $war, sub {
  60. $hasChanged = 1;
  61. } );
  62.  
  63. # wait for changes, poll every 3 seconds
  64. $monitor->scan;
  65. print "Waiting for changes...\n";
  66.  
  67. while(1) {
  68. $lastHasChanged = $hasChanged;
  69. $hasChanged = 0;
  70. $monitor->scan;
  71. if ($hasChanged == 0 && $lastHasChanged == 1) {
  72. &redeploy($war, $tomcat);
  73. }
  74. sleep 1;
  75. }
  76. }
Add Comment
Please, Sign In to add comment