lib/ztk/ssh.rb in ztk-0.3.1 vs lib/ztk/ssh.rb in ztk-1.0.0.rc.0

- old
+ new

@@ -15,11 +15,10 @@ # 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" @@ -83,10 +82,11 @@ # end # # @author Zachary Patten <zachary@jovelabs.net> class SSH < ZTK::Base + # Exit Signal Mappings EXIT_SIGNALS = { 1 => "SIGHUP", 2 => "SIGINT", 3 => "SIGQUIT", 4 => "SIGILL", @@ -113,11 +113,11 @@ 25 => "SIGXFSZ", 26 => "SIGVTALRM", 27 => "SIGPROF" } - # @param [Hash] config Configuration options hash. + # @param [Hash] configuration Configuration options hash. # @option config [String] :host_name Server hostname to connect to. # @option config [String] :user Username to use for authentication. # @option config [String, Array<String>] :keys A single or series of # identity files to use for authentication. # @option config [String] :password Password to use for authentication. @@ -140,35 +140,18 @@ :ignore_exit_status => false }.merge(configuration)) config.logger.debug { "config=#{config.send(:table).inspect}" } end - def inspect - 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 - # 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::SSH object. + # 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 @@ -217,18 +200,10 @@ # config.user = ENV["USER"] # config.host_name = "127.0.0.1" # end # puts ssh.exec("hostname -f").inspect def exec(command, options={}) - - def log_header(tag) - count = 8 - sep = ("=" * count) - header = [sep, "[ #{tag} ]", sep, "[ #{self.inspect} ]", sep, "[ #{tag} ]", sep].join - "#{header}\n" - end - options = OpenStruct.new({ :exit_code => 0, :silence => false }.merge(options)) config.logger.debug { "config=#{config.send(:table).inspect}" } config.logger.debug { "options=#{options.send(:table).inspect}" } config.logger.info { "exec(#{command.inspect})" } @@ -466,9 +441,35 @@ # This is not plainly documented on the Net::SSH config class. options.merge!(:password => config.password) if config.password config.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 end end