从 cap 3 初始化 ssh 会话?

Initializing an ssh session from cap 3?

我以前用cap-ssh for a shortcut to initiate an ssh connection to the server, but it doesn't look like it works in capistrano 3了。

有人对从 cap 3 中的 capistrano 启动 ssh 连接有任何建议吗?

您可以这样定义 ssh 任务:

desc 'Start an ssh session to your servers.'
task :ssh do
  role = (ENV['ROLE'] || :app).to_sym
  on roles(role) do
    hosts = env.instance_variable_get(:@servers).instance_variable_get(:@servers)
    hosts = hosts.select { |h| h.roles.include? role } if role
    if hosts.size > 1
      $stdout.puts "Pick a server to connect to:"
      hosts.each.with_index do |host, i|
        $stdout.puts "\t#{i + 1}: #{host.user}@#{host.hostname} role: #{host.roles.to_a}"
      end
      selected = $stdin.gets
      selected = 1 if selected.empty?
      host = hosts[selected.to_i - 1]
    else
      host = hosts.first
    end
    fail "No server defined!" unless host

    port = host.netssh_options[:port] || fetch(:ssh_options) && fetch(:ssh_options)[:port] || 22
    system "ssh -t -p #{port} #{host.user}@#{host.hostname} #{host.netssh_options[:forward_agent] ? '-a' : ''} 'cd #{current_path}; bash --login'"
  end
end