diff --git a/README.md b/README.md index d877fbd..122ac63 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ See [example](example) directory. See [example/sshkit.rb](example/sshkit.rb) . ### With Capistrano +sshkit must be v1.9.0 or later due to [SSHKit::Backend::ConnectionPool API change](https://github.com/capistrano/sshkit/releases/tag/v1.9.0.rc1). ```ruby require 'sshkit/backends/libssh' diff --git a/example/sshkit.rb b/example/sshkit.rb index 21a367e..27e1886 100755 --- a/example/sshkit.rb +++ b/example/sshkit.rb @@ -15,6 +15,8 @@ SSHKit.config.output = SSHKit::Formatter::Pretty.new($stdout) SSHKit.config.output_verbosity = :debug +include SSHKit::DSL + on %w[barkhorn rossmann], in: :parallel do |host| date = capture(:date) puts "#{host}: #{date}" diff --git a/lib/sshkit/backends/libssh.rb b/lib/sshkit/backends/libssh.rb index a3cce45..02468e1 100644 --- a/lib/sshkit/backends/libssh.rb +++ b/lib/sshkit/backends/libssh.rb @@ -175,30 +175,26 @@ def download_file(scp, local) end end - def with_session + def with_session(&block) host.ssh_options = Libssh.config.ssh_options.merge(host.ssh_options || {}) - entry = self.class.pool.checkout(String(host.hostname), host.username, host.netssh_options) do |hostname, username, ssh_options| - LibSSH::Session.new.tap do |session| - session.host = hostname - username = ssh_options.fetch(:user, username) - if username - session.user = username - end - configure_session(session, ssh_options) - session.connect - if session.server_known != LibSSH::SERVER_KNOWN_OK - raise 'unknown host' - end - if session.userauth_publickey_auto != LibSSH::AUTH_SUCCESS - raise 'authorization failed' - end - end - end + self.class.pool.with(method(:create_session), String(host.hostname), host.username, host.netssh_options, &block) + end - begin - yield entry.connection - ensure - self.class.pool.checkin(entry) + def create_session(hostname, username, ssh_options) + LibSSH::Session.new.tap do |session| + session.host = hostname + username = ssh_options.fetch(:user, username) + if username + session.user = username + end + configure_session(session, ssh_options) + session.connect + if session.server_known != LibSSH::SERVER_KNOWN_OK + raise 'unknown host' + end + if session.userauth_publickey_auto != LibSSH::AUTH_SUCCESS + raise 'authorization failed' + end end end diff --git a/libssh.gemspec b/libssh.gemspec index e02a00a..c0d970e 100644 --- a/libssh.gemspec +++ b/libssh.gemspec @@ -26,6 +26,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rake-compiler' spec.add_development_dependency 'rspec', '>= 3.0.0' spec.add_development_dependency 'rubocop', '>= 0.36.0' - spec.add_development_dependency 'sshkit' + spec.add_development_dependency 'sshkit', '>= 1.9.0' spec.add_development_dependency 'yard' end