Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
753 views
in Technique[技术] by (71.8m points)

ruby - Capistrano 3 change ssh_options inside task

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

my production.rb say:

set :stage, :production

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

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
...

When execute:

cap production namespace:my_task_with_an_other_user

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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{[email protected]}
role :web, %w{[email protected]}
role :db,  %w{[email protected]}

# 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{[email protected]}, no_release: true

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

Other gotchas:

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.

Capistrano 2

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

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

Usage:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...