Capistrano 3 在任务中更改 ssh_options [英] Capistrano 3 change ssh_options inside task

查看:20
本文介绍了Capistrano 3 在任务中更改 ssh_options的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用不同的 ssh_options 在同一阶段运行 capistrano v.3 任务.

I trying to run capistrano v.3 task in same stage with diferent ssh_options.

我的 production.rb 说:

my production.rb say:

set :stage, :production

set :user, 'deploy'
set :ssh_options, { user: 'deploy' }

使用此配置 capistrano 与用户 deploy 连接,这对于其余任务是正确的.但是我需要将它连接到在服务器中配置良好的 an_other_user 以完成一项特定任务.然后我的食谱说:

With this configuration capistrano connect with user deploy which is correct for the rest of taks. But I need connect it for one specific task with an_other_user wich is well configured in server. Then my recipe say:

...
tasks with original user
...

task :my_task_with_an_other_user do
  set :user, 'an_other_user'
  set :ssh_options, { user: 'an_other_user' }

  on roles(:all) do |host|
    execute :mkdir, '-p', 'mydir'
  end
end

...
other tasks with original user
...

执行时:

cap 生产命名空间:my_task_with_an_other_user

cap production namespace:my_task_with_an_other_user

capistrano 使用原始 :user "deploy"(在 production.rb 中声明的用户)进行 ssh conexion.

capistrano make ssh conexion with original :user "deploy" (the user declared in production.rb).

如何在任务中更改用户和/或 ssh_options?

How can I change the user and/or ssh_options it inside task?

推荐答案

Capistrano 3

我很难找到解决方案.但是解决方案比版本 2 好得多.Cap 团队做得很好.确保您已将 Capistrano 更新到 3.2.x+ 技巧如下:

Capistrano 3

I had hard time finding out a solution. But the solution is much nicer than version 2. Cap team has done a great job. Make sure you have updated Capistrano to 3.2.x+ Here's the trick:

# in config/deploy/production.rb, or config/deploy/staging.rb
# These roles are used for deployment that works with Cap hooks
role :app, %w{deploy@myserver.com}
role :web, %w{deploy@myserver.com}
role :db,  %w{deploy@myserver.com}

# Use additional roles to run side job out side Capistrano hooks
# 'foo' is another ssh user for none-release purpose tasks (mostly root tasks).
# e.g. user 'deploy' does not have root permission, but 'foo' has root permission.
# 'no_release' flag is important to flag this user will skip some standard hooks
# (e.g. scm/git/svn checkout )
role :foo_role, %w{foo@myserver.com}, no_release: true

确保部署"和'foo' 用户可以通过 ssh 进入该框.在您的任务中,使用 on 关键字:

Make sure both 'deploy' & 'foo' user can ssh into the box. Within your tasks, use the on keyword:

task :restart do
  on roles(:foo_role) do
    sudo "service nginx restart"
  end
end

task :other_standard_deployment_tasks do
  on release_roles(:all) do
     # ...
  end
end

其他问题:

确保某些 Capistrano 任务跳过您添加的额外无发布角色.否则,在部署过程中可能会导致文件权限问题.例如.capistrano/bundler 扩展需要覆盖默认的bundler_roles

Make sure some Capistrano tasks skips the additional no release role you added. Otherwise, it might cause file permission issues during deployment. E.g. capistrano/bundler extension need to override the default bundler_roles

set :bundler_roles, %w(web app db)  # excludes the no release role.

  • 阅读有关 no_release 标志的更多信息:相关的 Github 问题.
  • 我曾经有一个自定义函数来关闭和重新连接 ssh 会话.

    I used to have a custom functions to close and reconnect ssh sessions.

    将以下方法放在 deploy.rb 中.调用 with_user 切换 ssh 会话.稍微简化的版本:

    Place the following methods in deploy.rb. Call with_user to switch ssh session. Slightly simplified version:

    def with_user(new_user, &block)
      old_user = user
      set :user, new_user
      close_sessions
      yield
      set :user, old_user
      close_sessions
    end
    
    def close_sessions
      sessions.values.each { |session| session.close }
      sessions.clear
    end
    

    用法:

    task :update_nginx_config, :roles => :app do
      with_user "root" do
        sudo "nginx -s reload"
      end
    end
    

    这篇关于Capistrano 3 在任务中更改 ssh_options的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆