Capistrano 3改变ssh_options里面的任务 [英] Capistrano 3 change ssh_options inside task

查看:153
本文介绍了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连接用户部署对于其余的taks来说是正确的。但是我需要通过在服务器中配置良好的 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 production namespace:my_task_with_an_other_user

cap production namespace:my_task_with_an_other_user

capistrano make ssh conexion with original:userdeploy(用户声明在生产.rb)。

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插入框中。在你的任务中,使用关键字上的

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 extension需要覆盖默认的 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问题

    • Read more about no_release flag: related Github issue.
    • 我曾经有一个自定义函数来关闭和重新连接ssh会话。

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

      • Reference Paul Gross's Blog

      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天全站免登陆