Monday, December 17, 2007

Rake 'ing in the Tomcats

So I needed to have a quick way to start/stop/restart/tail -f logs for two different Apache Tomcat instances, Rake is a great tool for this.

I wrote the following very quickly, sure there may be other "frameworks" out there to do this, but I didn't download anything, just wrote it and off I go. (no xml, sorry Ant)

[sourcecode language="ruby"]
# home directory for all this stuff
HOME = '/path/to/my/tomcats'
# defining our servers and their locations
SERVERS = {
:one => "#{HOME}/tomcat_one/",
:two => "#{HOME}/tomcat_two/"
}

# sure there is a better way than this.?.?.
task :default do
puts "\nThe following are the tasks you may choose from:\n"
app = Rake.application
app.do_option('--tasks', ARGV.shift)
app.display_tasks_and_comments()
end

SERVERS.each do |name, dir|
namespace "#{name}" do
desc "start the #{name} server instance"
task :start do
Dir.chdir(dir)
puts `./bin/startup.sh`
end

desc "stop the #{name} server instance"
task :stop do
Dir.chdir(dir)
puts `./bin/shutdown.sh`
end

desc "tail the #{name} server log"
task :tail do
system("tail -f #{dir}/logs/catalina.out")
end
end
end

def server_names
SERVERS.keys.join(', ')
end

[:start, :stop].each do |action|
desc "#{action} servers (#{server_names()})"
task "#{action}" do
SERVERS.keys.each do |key|
task("#{key}:#{action}").invoke
end
end
end

desc "restart servers (#{server_names()})"
task :restart do
task(:stop).invoke
task(:start).invoke
end
[/sourcecode]

No comments: