Sha256: c74ea573d6bcbee673a07af81d1822b244e7dbe500afa62ea1df8d554a1b2821

Contents?: true

Size: 990 Bytes

Versions: 5

Compression:

Stored size: 990 Bytes

Contents

module WatchmonkeyCli
  class SshConnection
    def initialize(id, opts = {}, &initializer)
      @id = id

      if opts.is_a?(String)
        u, h = opts.split("@", 2)
        opts = { user: u, host_name: h }
      elsif opts[:host].is_a?(String)
        u, h = opts[:host].split("@", 2)
        opts = opts.merge(user: u, host_name: h)
        opts.delete(:host)
      end

      # net/ssh options
      @opts = {
        config: false,
      }.merge(opts)
      @mutex = Monitor.new
      initializer.try(:call, @opts)
    end

    def to_s
      "#<WatchmonkeyCli::SshConnection:#{@id}>"
    end

    def name
      "ssh:#{@id}"
    end

    def sync &block
      @mutex.synchronize(&block)
    end

    def exec cmd, chomp = true
      sync do
        res = connection.exec!(cmd)
        chomp ? res.chomp : res
      end
    end

    def connection
      sync { @ssh ||= Net::SSH.start(nil, nil, @opts) }
    end

    def close!
      @ssh.try(:close) rescue false
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
watchmonkey_cli-1.7.1 lib/watchmonkey_cli/ssh_connection.rb
watchmonkey_cli-1.7 lib/watchmonkey_cli/ssh_connection.rb
watchmonkey_cli-1.6 lib/watchmonkey_cli/ssh_connection.rb
watchmonkey_cli-1.5 lib/watchmonkey_cli/ssh_connection.rb
watchmonkey_cli-1.4 lib/watchmonkey_cli/ssh_connection.rb