lib/ztk/ssh.rb in ztk-0.0.4 vs lib/ztk/ssh.rb in ztk-0.0.5

- old
+ new

@@ -15,34 +15,34 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ################################################################################ + require "ostruct" +require "net/ssh" +require "net/ssh/proxy/command" +require "net/sftp" +################################################################################ + module ZTK + +################################################################################ + class SSHError < Error; end - class SSH ################################################################################ - attr_accessor :stdout, :stderr, :stdin, :config + class SSH < ::ZTK::Base ################################################################################ def initialize(config={}) - @config = OpenStruct.new({ - :stdout => $stdout, - :stderr => $stderr, - :stdin => $stdin, - :logger => $logger, - :ssh => Hash.new(nil) + super({ + :ssh => ::OpenStruct.new }.merge(config)) - @config.stdout.sync = true if @config.stdout.respond_to?(:sync=) - @config.stderr.sync = true if @config.stderr.respond_to?(:sync=) - @config.stdin.sync = true if @config.stdin.respond_to?(:sync=) - @config.logger.sync = true if @config.logger.respond_to?(:sync=) end ################################################################################ def console @@ -52,22 +52,22 @@ command << [ "-q" ] command << [ "-o", "UserKnownHostsFile=/dev/null" ] command << [ "-o", "StrictHostKeyChecking=no" ] command << [ "-o", "KeepAlive=yes" ] command << [ "-o", "ServerAliveInterval=60" ] - command << [ "-i", @config.ssh[:identity_file] ] if @config.ssh[:identity_file] - command << [ "-o", "ProxyCommand=\"#{proxy_command}\"" ] if @config.ssh[:proxy] - command << "#{@config.ssh[:ssh_user]}@#{@config.ssh[:host]}" + command << [ "-i", @config.ssh.identity_file ] if @config.ssh.identity_file + command << [ "-o", "ProxyCommand=\"#{proxy_command}\"" ] if @config.ssh.proxy + command << "#{@config.ssh.user}@#{@config.ssh.host}" command = command.flatten.compact.join(" ") @config.logger and @config.logger.info { "command(#{command})" } - Kernel.exec(command) + ::Kernel.exec(command) end ################################################################################ def exec(command, options={}) - @ssh ||= Net::SSH.start(@config.ssh[:host], @config.ssh[:ssh_user], ssh_options) + @ssh ||= ::Net::SSH.start(@config.ssh.host, @config.ssh.user, ssh_options) options = { :silence => false }.merge(options) silence = options[:silence] output = "" @@ -100,11 +100,11 @@ end ################################################################################ def upload(local, remote) - @sftp ||= Net::SFTP.start(@config.ssh[:host], @config.ssh[:ssh_user], ssh_options) + @sftp ||= ::Net::SFTP.start(@config.ssh.host, @config.ssh.user, ssh_options) @config.logger and @config.logger.debug { "config(#{@config.ssh.inspect})" } @config.logger and @config.logger.info { "parameters(#{local},#{remote})" } @sftp.upload!(local.to_s, remote.to_s) do |event, uploader, *args| case event @@ -118,16 +118,18 @@ @config.logger and @config.logger.debug { "put(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" } when :finish @config.logger and @config.logger.info { "finish" } end end + + true end ################################################################################ def download(remote, local) - @sftp ||= Net::SFTP.start(@config.ssh[:host], @config.ssh[:ssh_user], ssh_options) + @sftp ||= ::Net::SFTP.start(@config.ssh.host, @config.ssh.user, ssh_options) @config.logger and @config.logger.debug { "config(#{@config.ssh.inspect})" } @config.logger and @config.logger.info { "parameters(#{remote},#{local})" } @sftp.download!(remote.to_s, local.to_s) do |event, downloader, *args| case event @@ -141,21 +143,23 @@ @config.logger and @config.logger.debug { "get(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" } when :finish @config.logger and @config.logger.info { "finish" } end end + + true end ################################################################################ private ################################################################################ def proxy_command @config.logger and @config.logger.debug { "config(#{@config.ssh.inspect})" } - if !@config.ssh[:identity_file] + if !@config.ssh.identity_file message = "You must specify an identity file in order to SSH proxy." @config.logger and @config.logger.fatal { message } raise SSHError, message end @@ -163,12 +167,12 @@ command << [ "-q" ] command << [ "-o", "UserKnownHostsFile=/dev/null" ] command << [ "-o", "StrictHostKeyChecking=no" ] command << [ "-o", "KeepAlive=yes" ] command << [ "-o", "ServerAliveInterval=60" ] - command << [ "-i", @config.ssh[:proxy_identity_file] ] if @config.ssh[:proxy_identity_file] - command << "#{@config.ssh[:proxy_ssh_user]}@#{@config.ssh[:proxy_host]}" + command << [ "-i", @config.ssh.proxy_identity_file ] if @config.ssh.proxy_identity_file + command << "#{@config.ssh.proxy_user}@#{@config.ssh.proxy_host}" command << "nc %h %p" command = command.flatten.compact.join(" ") @config.logger and @config.logger.debug { "command(#{command})" } command end @@ -176,20 +180,23 @@ ################################################################################ def ssh_options @config.logger and @config.logger.debug { "config(#{@config.ssh.inspect})" } options = {} - options.merge!(:password => @config.ssh[:ssh_password]) if @config.ssh[:ssh_password] - options.merge!(:keys => @config.ssh[:identity_file]) if @config.ssh[:identity_file] - options.merge!(:timeout => @config.ssh[:timeout]) if @config.ssh[:timeout] - options.merge!(:user_known_hosts_file => '/dev/null') if !@config.ssh[:host_key_verify] - options.merge!(:proxy => Net::SSH::Proxy::Command.new(proxy_command)) if @config.ssh[:proxy] + options.merge!(:password => @config.ssh.password) if @config.ssh.password + options.merge!(:keys => @config.ssh.identity_file) if @config.ssh.identity_file + options.merge!(:timeout => @config.ssh.timeout) if @config.ssh.timeout + options.merge!(:user_known_hosts_file => '/dev/null') if !@config.ssh.host_key_verify + options.merge!(:proxy => ::Net::SSH::Proxy::Command.new(proxy_command)) if @config.ssh.proxy @config.logger and @config.logger.debug { "options(#{options.inspect})" } options end ################################################################################ end + +################################################################################ + end ################################################################################