Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to work with Instances over SSH tunnel. #1091

Merged
merged 4 commits into from Aug 9, 2016

Conversation

EYurchenko
Copy link
Contributor

@EYurchenko EYurchenko commented Jul 28, 2016

Inspired by:
#294
#933

In your .kitchen.yml file you use:

transport:
  name: ssh
  ssh_gateway: <gateway>
  ssh_gateway_username: <username at the gateway>

and all ssh operations will be tunnelled through this .
Basically we replace Net::SSH session in transport/ssh.rb with Net::SSH::Gateway, since this moment all ssh operations go through the tunnel. Remote end 'not being ready' also reported correctly:

local@ey-test-kitchen:~/test-kitchen-ey$ bundle exec kitchen converge -l debug
-----> Starting Kitchen (v1.10.2)
D      Berksfile found at /home/local/test-kitchen-ey/Berksfile, loading Berkshelf
D      Berkshelf 4.3.5 library loaded
-----> Creating <default-centos-7>...
D      [SSH] opening connection to local@10.236.62.20<{:user_known_hosts_file=>"/dev/null", :paranoid=>false, :port=>22, :compression=>false, :compression_level=>0, :keepalive=>true, :keepalive_interval=>60, :timeout=>15}> via eyurchenko@10.236.11.190
D      [SSH] connection failed (#<Net::SSH::ConnectionTimeout: timeout during server version negotiating>)
       Waiting for SSH service on 10.236.62.20:22, retrying in 3 seconds
D      [SSH] opening connection to local@10.236.62.20<{:user_known_hosts_file=>"/dev/null", :paranoid=>false, :port=>22, :compression=>false, :compression_level=>0, :keepalive=>true, :keepalive_interval=>60, :timeout=>15}> via eyurchenko@10.236.11.190
D      [SSH] local@10.236.62.20<{:user_known_hosts_file=>"/dev/null", :paranoid=>false, :port=>22, :compression=>false, :compression_level=>0, :keepalive=>true, :keepalive_interval=>60, :timeout=>15}> (echo '[SSH] Established')
       [SSH] Established
D      SSH ready on <default-centos-7>
...

"#{ssh_gateway_username}@#{ssh_gateway}")
Net::SSH::Gateway.new(ssh_gateway,
ssh_gateway_username, options).ssh(hostname, username, options)
rescue *RESCUE_EXCEPTIONS_ON_ESTABLISH => e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great if we could reuse the retry logic accross the two connection strategies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried. Look at this commit d171410. But rubocop was very unhappy -(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you hat the gateway and non-gateway connection with the retry all in one method there. I think you did the right thing be making separate methods but I'd suggest extracting the retry logic which appears to be identical in both to be something like:

def retry_connection(opts)
  yield
# RESCUE CODE HERE
end

Then

def establish_connection_via_gateway(opts)
  retry_connection(opts) do
    Net::SSH::Gateway.new(ssh_gateway,
        ssh_gateway_username, options).ssh(hostname, username, options)
  end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint! Moved the code. Although now we have comments describing parameters identical for three methods -(

@mwrock
Copy link
Member

mwrock commented Aug 1, 2016

👍

1 similar comment
@cheeseplus
Copy link

+1

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants