从第3章初始化ssh会话?

时间:2015-03-31 19:56:30

标签: ssh capistrano capistrano3

我以前使用cap-ssh作为启动与服务器的ssh连接的快捷方式,但it doesn't look like it works in capistrano 3已经不再了。

有没有人有任何关于从cap 3中的capistrano开始ssh连接的建议?

1 个答案:

答案 0 :(得分:1)

您可以像这样定义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