lib/ztk/ssh.rb in ztk-1.6.2 vs lib/ztk/ssh.rb in ztk-1.6.3
- old
+ new
@@ -1,10 +1,5 @@
-require 'ostruct'
-require 'net/ssh'
-require 'net/ssh/proxy/command'
-require 'net/sftp'
-
module ZTK
# SSH Error Class
#
# @author Zachary Patten <zachary AT jovelabs DOT com>
@@ -22,11 +17,11 @@
# ui = ZTK::UI.new(:stdout => std_combo, :stderr => std_combo)
# ssh = ZTK::SSH.new(:ui => ui)
#
# If you want to specify SSH options you can:
#
- # keys = File.expand_path(File.join(ENV['HOME'], '.ssh', 'id_rsa'))
+ # keys = File.expand_path(File.join(Dir.home, '.ssh', 'id_rsa'))
# ssh = ZTK::SSH.new(:host_name => '127.0.0.1', :user => ENV['USER'], :keys => keys)
#
# = Configuration Examples:
#
# To proxy through another host, for example SSH to 192.168.1.1 through 192.168.0.1:
@@ -39,12 +34,12 @@
# end
#
# Specify an identity file:
#
# ssh.config do |config|
- # config.keys = File.expand_path(File.join(ENV['HOME'], '.ssh', 'id_rsa'))
- # config.proxy_keys = File.expand_path(File.join(ENV['HOME'], '.ssh', 'id_rsa'))
+ # config.keys = File.expand_path(File.join(Dir.home, '.ssh', 'id_rsa'))
+ # config.proxy_keys = File.expand_path(File.join(Dir.home, '.ssh', 'id_rsa'))
# end
#
# Specify a timeout:
#
# ssh.config do |config|
@@ -63,10 +58,15 @@
# config.host_key_verify = true
# end
#
# @author Zachary Patten <zachary AT jovelabs DOT com>
class SSH < ZTK::Base
+ require 'ostruct'
+ require 'net/ssh'
+ require 'net/ssh/proxy/command'
+ require 'net/sftp'
+
# Exit Signal Mappings
EXIT_SIGNALS = {
1 => "SIGHUP",
2 => "SIGINT",
3 => "SIGQUIT",
@@ -94,19 +94,24 @@
25 => "SIGXFSZ",
26 => "SIGVTALRM",
27 => "SIGPROF"
}
- autoload :Bootstrap, 'ztk/ssh/bootstrap'
- autoload :Command, 'ztk/ssh/command'
- autoload :Download, 'ztk/ssh/download'
- autoload :Exec, 'ztk/ssh/exec'
- autoload :File, 'ztk/ssh/file'
- autoload :Upload, 'ztk/ssh/upload'
+ require 'ztk/ssh/bootstrap'
+ require 'ztk/ssh/command'
+ require 'ztk/ssh/console'
+ require 'ztk/ssh/core'
+ require 'ztk/ssh/download'
+ require 'ztk/ssh/exec'
+ require 'ztk/ssh/file'
+ require 'ztk/ssh/private'
+ require 'ztk/ssh/upload'
include ZTK::SSH::Bootstrap
include ZTK::SSH::Command
+ include ZTK::SSH::Console
+ include ZTK::SSH::Core
include ZTK::SSH::Download
include ZTK::SSH::Exec
include ZTK::SSH::File
include ZTK::SSH::Upload
@@ -153,116 +158,17 @@
:ignore_exit_status => false,
:request_pty => true,
:exit_code => 0,
:silence => false
}.merge(configuration))
- config.ui.logger.debug { "config=#{config.send(:table).inspect}" }
- end
- # Starts an SSH session. Can also be used to get the Net::SSH object.
- #
- # Primarily used internally.
- def ssh
- @ssh ||= Net::SSH.start(config.host_name, config.user, ssh_options)
- end
-
- # Starts an SFTP session. Can also be used to get the Net::SFTP object.
- #
- # Primarily used internally.
- def sftp
- @sftp ||= Net::SFTP.start(config.host_name, config.user, ssh_options)
- end
-
- # Close our session gracefully.
- def close
- config.ui.logger.debug { "close" }
- ssh and !ssh.closed? and ssh.close
- end
-
- # The on_retry method we'll use with the RescueRetry class.
- def on_retry(exception)
- close
- @ssh = nil
- @sftp = nil
- end
-
- # Launches an SSH console, replacing the current process with the console
- # process.
- #
- # @example Launch a console:
- # ssh = ZTK::SSH.new
- # ssh.config do |config|
- # config.user = ENV["USER"]
- # config.host_name = "127.0.0.1"
- # end
- # ssh.console
- def console
config.ui.logger.debug { "config=#{config.send(:table).inspect}" }
- config.ui.logger.info { "console(#{console_command.inspect})" }
-
- config.ui.logger.fatal { "REPLACING CURRENT PROCESS - GOODBYE!" }
- Kernel.exec(console_command)
end
private
- # Builds our SSH options hash.
- def ssh_options
- options = {}
-
- # These are plainly documented on the Net::SSH config class.
- options.merge!(:encryption => config.encryption) if config.encryption
- options.merge!(:compression => config.compression) if config.compression
- options.merge!(:compression_level => config.compression_level) if config.compression_level
- options.merge!(:timeout => config.timeout) if config.timeout
- options.merge!(:forward_agent => config.forward_agent) if config.forward_agent
- options.merge!(:global_known_hosts_file => config.global_known_hosts_file) if config.global_known_hosts_file
- options.merge!(:auth_methods => config.auth_methods) if config.auth_methods
- options.merge!(:host_key => config.host_key) if config.host_key
- options.merge!(:host_key_alias => config.host_key_alias) if config.host_key_alias
- options.merge!(:host_name => config.host_name) if config.host_name
- options.merge!(:keys => config.keys) if config.keys
- options.merge!(:keys_only => config.keys_only) if config.keys_only
- options.merge!(:hmac => config.hmac) if config.hmac
- options.merge!(:port => config.port) if config.port
- options.merge!(:proxy => Net::SSH::Proxy::Command.new(proxy_command)) if config.proxy_host_name
- options.merge!(:rekey_limit => config.rekey_limit) if config.rekey_limit
- options.merge!(:user => config.user) if config.user
- options.merge!(:user_known_hosts_file => config.user_known_hosts_file) if config.user_known_hosts_file
-
- # This is not plainly documented on the Net::SSH config class.
- options.merge!(:password => config.password) if config.password
-
- config.ui.logger.debug { "ssh_options(#{options.inspect})" }
- options
- end
-
- # Builds a human readable tag about our connection. Used for internal
- # logging purposes.
- def tag
- tags = Array.new
-
- if config.proxy_host_name
- proxy_user_host = "#{config.proxy_user}@#{config.proxy_host_name}"
- proxy_port = (config.proxy_port ? ":#{config.proxy_port}" : nil)
- tags << [proxy_user_host, proxy_port].compact.join
- tags << " >>> "
- end
-
- user_host = "#{config.user}@#{config.host_name}"
- port = (config.port ? ":#{config.port}" : nil)
- tags << [user_host, port].compact.join
-
- tags.join.strip
- end
-
- def log_header(what)
- count = 8
- sep = ("=" * count)
- header = [sep, "[ #{what} ]", sep, "[ #{tag} ]", sep, "[ #{what} ]", sep].join
- "#{header}\n"
- end
+ include ZTK::SSH::Private
end
end