Class: Weave::LazyConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/weave.rb

Overview

An SSH connection which isn't established until it's needed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host_string) ⇒ LazyConnection

Returns a new instance of LazyConnection.

Parameters:

  • host_string (String)

    the host of the form user@host



122
123
124
125
126
# File 'lib/weave.rb', line 122

def initialize(host_string)
  @user, @host = self.class.user_and_host(host_string)
  @connection = nil
  @mutex = NilMutex
end

Instance Attribute Details

#hostObject (readonly)

The hostname for the ssh connection



119
120
121
# File 'lib/weave.rb', line 119

def host
  @host
end

#userObject (readonly)

The username for the ssh connection



117
118
119
# File 'lib/weave.rb', line 117

def user
  @user
end

Instance Method Details

#disconnectObject

Disconnect, if connected.



225
226
227
228
# File 'lib/weave.rb', line 225

def disconnect()
  @connection.close if @connection
  @connection = nil
end

#puts(*args) ⇒ Object

A thread-safe wrapper around Kernel.puts.



129
# File 'lib/weave.rb', line 129

def puts(*args) @mutex.synchronize { Kernel.puts(*args) } end

#run(command, options = {}) ⇒ Object

Run a command on this connection. This will open a connection if it's not already connected. The way the output is presented is determined by the option :output. The default, :output => :pretty, prints each line of output with the name of the host and whether the output is stderr or stdout. If :output => :raw, then the output will be passed as is directly back to STDERR or STDOUT as appropriate. If :output => :capture, then this method puts the output into the result hash as { :stdout => "...", :stderr => "..." }.

The result of this method is a hash containing either :exit_code (if the command exited normally) or :exit_signal (if the command exited due to a signal). It also has :stdout and :stderr strings, if option[:output] was set to :capture.

If the option :continue_on_failure is set to true, then this method will continue as normal if the command terminated via a signal or with a non-zero exit status. Otherwise (the default), these will cause a Weave::Error to be raised.

Parameters:

  • options (Hash) (defaults to: {})

    the output options

Options Hash (options):

  • :output (Symbol)

    the output format



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/weave.rb', line 148

def run(command, options = {})
  options[:output] ||= :pretty
  @connection ||= Net::SSH.start(@host, @user)
  result = options[:output] == :capture ? { :stdout => "", :stderr => "" } : {}
  @connection.open_channel do |channel|
    channel.exec(command) do |_, success|
      unless success
        raise Error, "Could not run ssh command: #{command}"
      end

      channel.on_data do |_, data|
        append_or_print_output(result, data, :stdout, options)
      end

      channel.on_extended_data do |_, type, data|
        next unless type == 1
        append_or_print_output(result, data, :stderr, options)
      end

      channel.on_request("exit-status") do |_, data|
        code = data.read_long
        unless code.zero? || options[:continue_on_failure]
          raise Error, "[#{@host}] command finished with exit status #{code}: #{command}"
        end
        result[:exit_code] = code
      end

      channel.on_request("exit-signal") do |_, data|
        signal = data.read_long
        unless options[:continue_on_failure]
          signal_name = Signal.list.invert[signal]
          signal_message = signal_name ? "#{signal} (#{signal_name})" : "#{signal}"
          raise Error, "[#{@host}] command received signal #{signal_message}: #{command}"
        end
        result[:exit_signal] = signal
      end
    end
  end
  @connection.loop(0.05)
  result
end