Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6547: allow_multiple_ssh_ports_and_usernames_in_capistrano.diff

File allow_multiple_ssh_ports_and_usernames_in_capistrano.diff, 4.7 kB (added by ezmobius, 2 years ago)

allow multiple ssh ports and usernames in capistyrano deploy.rb files

  • test/ssh_test.rb

    old new  
    4242      MockSSH.invocations.first[1][:auth_methods] 
    4343    assert_nil MockSSH.invocations.first[2] 
    4444  end 
     45   
     46  def test_explicit_ssh_ports_in_server_string_no_block 
     47    Net.const_during(:SSH, MockSSH) do 
     48      Capistrano::SSH.connect('demo.server.i:8088', @config) 
     49    end 
    4550 
     51    assert_equal 1, MockSSH.invocations.length 
     52    assert_equal 'demo.server.i', MockSSH.invocations.first[0] 
     53    assert_equal '8088', MockSSH.invocations.first[1][:port] 
     54    assert_equal 'demo', MockSSH.invocations.first[1][:username] 
     55  end 
     56   
     57  def test_explicit_ssh_username_in_server_string_no_block 
     58    Net.const_during(:SSH, MockSSH) do 
     59      Capistrano::SSH.connect('bob@demo.server.i', @config) 
     60    end 
     61 
     62    assert_equal 1, MockSSH.invocations.length 
     63    assert_equal 'demo.server.i', MockSSH.invocations.first[0] 
     64    assert_equal 22, MockSSH.invocations.first[1][:port] 
     65    assert_equal 'bob', MockSSH.invocations.first[1][:username] 
     66  end 
     67   
     68  def test_explicit_ssh_username_and_port_in_server_string_no_block 
     69    Net.const_during(:SSH, MockSSH) do 
     70      Capistrano::SSH.connect('bob@demo.server.i:8088', @config) 
     71    end 
     72 
     73    assert_equal 1, MockSSH.invocations.length 
     74    assert_equal 'demo.server.i', MockSSH.invocations.first[0] 
     75    assert_equal '8088', MockSSH.invocations.first[1][:port] 
     76    assert_equal 'bob', MockSSH.invocations.first[1][:username] 
     77  end 
     78 
    4679  def test_publickey_auth_succeeds_explicit_port_no_block 
    4780    Net.const_during(:SSH, MockSSH) do 
    4881      Capistrano::SSH.connect('demo.server.i', @config, 23) 
     
    5386    assert_nil MockSSH.invocations.first[2] 
    5487  end 
    5588 
     89 
     90  def test_explicit_ssh_ports_in_server_string_with_block 
     91    Net.const_during(:SSH, MockSSH) do 
     92      Capistrano::SSH.connect('demo.server.i:8088', @config) do |session| 
     93      end 
     94    end 
     95    assert_equal 'demo.server.i', MockSSH.invocations.first[0] 
     96    assert_equal '8088', MockSSH.invocations.first[1][:port] 
     97    assert_equal 1, MockSSH.invocations.length 
     98    assert_instance_of Proc, MockSSH.invocations.first[2] 
     99  end 
     100   
     101  def test_explicit_ssh_username_in_server_string_with_block 
     102    Net.const_during(:SSH, MockSSH) do 
     103      Capistrano::SSH.connect('bob@demo.server.i', @config) do |session| 
     104      end 
     105    end 
     106    assert_equal 'demo.server.i', MockSSH.invocations.first[0] 
     107    assert_equal 22, MockSSH.invocations.first[1][:port] 
     108    assert_equal 1, MockSSH.invocations.length 
     109    assert_equal 'bob', MockSSH.invocations.first[1][:username] 
     110    assert_instance_of Proc, MockSSH.invocations.first[2] 
     111  end 
     112   
     113  def test_explicit_ssh_username_and_port_in_server_string_with_block 
     114    Net.const_during(:SSH, MockSSH) do 
     115      Capistrano::SSH.connect('bob@demo.server.i:8088', @config) do |session| 
     116      end 
     117    end 
     118    assert_equal 'demo.server.i', MockSSH.invocations.first[0] 
     119    assert_equal '8088', MockSSH.invocations.first[1][:port] 
     120    assert_equal 1, MockSSH.invocations.length 
     121    assert_equal 'bob', MockSSH.invocations.first[1][:username] 
     122    assert_instance_of Proc, MockSSH.invocations.first[2] 
     123  end 
     124 
    56125  def test_publickey_auth_succeeds_with_block 
    57126    Net.const_during(:SSH, MockSSH) do 
    58127      Capistrano::SSH.connect('demo.server.i', @config) do |session| 
  • lib/capistrano/ssh.rb

    old new  
    2727                        :password => password_value, 
    2828                        :port => port, 
    2929                        :auth_methods => methods.shift }.merge(config.ssh_options) 
    30         Net::SSH.start(server,ssh_options,&block) 
     30                         
     31        # This regex is used for its byproducts, the $1-9 match vars. 
     32        # This regex will always match the ssh hostname and if there  
     33        # is a username or port they will be matched as well. This  
     34        # allows us to set the username and ssh port right in the  
     35        # server string:  "username@123.12.123.12:8088" 
     36        # This remains fully backwards compatible and can still be 
     37        # intermixed with the old way of doing things. usernames 
     38        # and ports will be used from the server string if present 
     39        # but they will fall back to the regular defaults when not 
     40        # present. 
     41        server =~ /^(?:([^;,:=]+)@|)(.*?)(?::(\d+)|)$/                 
     42        ssh_options[:username] = $1 if $1     
     43        ssh_options[:port] = $3 if $3 
     44         
     45        Net::SSH.start($2,ssh_options,&block) 
    3146      rescue Net::SSH::AuthenticationFailed 
    3247        raise if methods.empty? 
    3348        password_value = config.password