Skip to content

Instantly share code, notes, and snippets.

@kares
Created August 20, 2010 05:45
Show Gist options
  • Save kares/539690 to your computer and use it in GitHub Desktop.
Save kares/539690 to your computer and use it in GitHub Desktop.
capistrano snippet that enables mongrel/thin server switching
# capistrano deploy.rb snippet that enables
# mongrel/thin (cluster) server switching
# usual capistrano setup such as :
#set :application ... (skipped)
# won't need this anymore - might delete the local .yml config :
#set :mongrel_config, "#{deploy_to}/shared/config/mongrel_cluster.yml"
# special setup for our custom tasks :
set :shared_config_path, "#{shared_path}/config"
set :server_type, :thin # or :mongrel
set :deploy_port, 8000
set :cluster_instances, 5
# helper methods :
def public_configuration_location_for(server)
"#{current_path}/config/#{server}.yml"
end
def shared_configuration_location_for(server)
"#{shared_config_path}/#{server}.yml"
end
# Application Server Choices
namespace :mongrel do
desc "Generate a mongrel configuration file"
task :build_configuration, :roles => :app do
config_options = {
"log_file" => "log/mongrel.log",
"cwd" => current_path,
"port" => deploy_port,
"servers" => cluster_instances,
"environment" => "production",
"address" => "127.0.0.1",
"pid_file" => "tmp/pids/mongrel.pid"
}.to_yaml
put config_options, shared_configuration_location_for(:mongrel)
end
desc "Links the configuration file"
task :link_configuration_file, :roles => :app do
run "ln -nsf #{shared_configuration_location_for(:mongrel)} #{public_configuration_location_for(:mongrel)}"
end
desc "Setup Mongrel Cluster After Code Update"
task :link_global_configuration, :roles => :app do
run "ln -nsf #{shared_configuration_location_for(:mongrel)} /etc/mongrel_cluster/#{application}.yml"
end
%w(start stop restart).each do |action|
desc "#{action} this app's Mongrel Cluster"
task action.to_sym, :roles => :app do
run "mongrel_rails cluster::#{action} -C #{shared_configuration_location_for(:mongrel)}"
end
end
end
namespace :thin do
desc "Generate a thin configuration file"
task :build_configuration, :roles => :app do
config_options = {
"log" => "log/thin.log",
"chdir" => current_path,
"port" => deploy_port,
"servers" => cluster_instances.to_i,
"environment" => "production",
"address" => "127.0.0.1",
"pid" => "tmp/pids/log.pid"
}.to_yaml
put config_options, shared_configuration_location_for(:thin)
end
desc "Links the configuration file"
task :link_configuration_file, :roles => :app do
run "ln -nsf #{shared_configuration_location_for(:thin)} #{public_configuration_location_for(:thin)}"
end
desc "Setup Thin Cluster After Code Update"
task :link_global_configuration, :roles => :app do
run "ln -nsf #{shared_configuration_location_for(:thin)} /etc/thin/#{application}.yml"
end
%w(start stop restart).each do |action|
desc "#{action} this app's Thin Cluster"
task action.to_sym, :roles => :app do
run "thin #{action} -C #{shared_configuration_location_for(:thin)}"
end
end
end
namespace :deploy do
%w(start stop restart).each do |action|
desc "#{action} the server"
task action.to_sym do
find_and_execute_task("#{server_type}:#{action}")
end
end
end
# After Tasks
after "deploy:setup" do
run "mkdir -p #{shared_config_path}"
end
after "deploy:setup", "#{server_type}:build_configuration"
after "#{server_type}:build_configuration", "#{server_type}:link_global_configuration"
after "deploy:symlink", "#{server_type}:link_configuration_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment