Reputation: 1813
I am preparing capistrano to deploy ruby on rails application to AWS. The application servers will be behind bastian host.
I have two servers server1 and server2. I want to deploy and run puma, nginx on server1, and run resque workers and resque schedulers on server2. I know about roles and here is my configuration so far:
# deploy/production.rb
web_instances = [web-instance-ip]
worker_instances = [worker-instance-ip]
role :app, web_instances
role :web, web_instances
role :worker, worker_instances
set :deploy_user, ENV['DEPLOY_USER'] || 'ubuntu'
set :branch, 'master'
set :ssh_options, {
forward_agent: true,
keys: ENV['SSH_KEY_PATH'],
proxy: Net::SSH::Proxy::Command.new("ssh -i '#{ENV['SSH_KEY_PATH']}' #{fetch(:deploy_user)}@#{ENV['BASTIAN_PUBLIC_IP']} -W %h:%p"),
}
set :puma_role, :app
I am not sure what should I do or how to write tasks by making sure that puma start, restart is done on only server1 and resque, resque scheduler start restart etc is handled on only server2. While common tasks such as pulling latest code, bundle install etc is done on each instance?
Upvotes: 2
Views: 764
Reputation: 675
This can achieve by using role
to limit the tasks to be run for each servers and some hooks to trigger your custom tasks. Your deploy/production.rb
file will look something similar to this.
web_instances = [web-instance-ip]
worker_instances = [worker-instance-ip]
role :app, web_instances
role :web, web_instances
role :worker, worker_instances
set :deploy_user, ENV['DEPLOY_USER'] || 'ubuntu'
set :branch, 'master'
set :ssh_options, {
forward_agent: true,
keys: ENV['SSH_KEY_PATH'],
proxy: Net::SSH::Proxy::Command.new("ssh -i '#{ENV['SSH_KEY_PATH']}' #{fetch(:deploy_user)}@#{ENV['BASTIAN_PUBLIC_IP']} -W %h:%p"),
}
# This will run on server with web role only
namespace :puma do
task :restart do
on roles(:web) do |host|
with rails_env: fetch(:rails_env) do
** Your code to restart puma server **
end
end
end
end
# This will run on server with worker role only
namespace :resque do
task :restart do
on roles(:worker) do |host|
with rails_env: fetch(:rails_env) do
** Your code to restart resque server **
end
end
end
end
after :deploy, 'puma:restart'
after :deploy, 'resque:restart'
Check out the docs for more information about commands and hooks to setup your deployment.
Upvotes: 1
Reputation: 2367
Let's assume, you have defined the roles in the following manner
role :puma_nginx_role, 'server1.com'
role :resque_role, 'server2.com'
Now define a rake task in your config/deploy.rb file, ex:
namespace :git do
desc 'To push the code'
task :push do
execute "git push"
end
end
Now assuming the above example should be run on server1, all you have do is
namespace :git do
desc 'To push the code'
task :push, :roles => [:puma_nginx_role] do
execute "git push"
end
end
Thereby, your telling capistrano configuration, that the git:push should be executed on role :puma_nginx_role, which in-turn would run it on server1.com. You'll have to modify the tasks to run puma/nginx/resque and make changes based on roles.
Upvotes: 1