我正在准备capistrano,以将ruby on rails应用程序部署到AWS。应用程序服务器将位于bastian主机之后。
我有两个服务器server1和server2。我想在server1上部署并运行puma,nginx,并在server2上运行resque worker和resque Scheduler。我知道角色,这是到目前为止的配置:
# 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
通过确定仅在server1上执行puma启动,重启并仅在server2上处理resque,resque调度程序启动重启等操作,我不确定应该怎么做或如何编写任务。虽然在每个实例上完成了诸如提取最新代码,捆绑安装等常见任务?
答案 0 :(得分:1)
假设您以以下方式定义了角色
role :puma_nginx_role, 'server1.com'
role :resque_role, 'server2.com'
现在在 config / deploy.rb 文件中定义一个瑞克任务,例如:
namespace :git do
desc 'To push the code'
task :push do
execute "git push"
end
end
现在假设以上示例应在server1上运行,您所要做的就是
namespace :git do
desc 'To push the code'
task :push, :roles => [:puma_nginx_role] do
execute "git push"
end
end
因此,您具有说服力的capistrano配置应该在角色<strong>:puma_nginx_role 上执行 git:push ,这又将在 server1上运行它。 com 。您必须修改任务以运行 puma / nginx / resque ,并根据角色进行更改。
答案 1 :(得分:1)
这可以通过使用role
来限制要在每个服务器上运行的任务以及一些挂钩来触发自定义任务来实现。您的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"),
}
# 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'
请查看docs,以获取有关用于设置部署的命令和挂钩的更多信息。