Class: Train::Plugins::Transport::BaseConnection

Inherits:
Object
  • Object
show all
Includes:
Extras
Defined in:
lib/train/plugins/base_connection.rb

Overview

A Connection instance can be generated and re-generated, given new connection details such as connection port, hostname, credentials, etc. This object is responsible for carrying out the actions on the remote host such as executing commands, transferring files, etc.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) {|self| ... } ⇒ BaseConnection

Create a new Connection instance.

Parameters:

  • options (Hash) (defaults to: nil)

    connection options

Yields:

  • (self)

    yields itself for block-style invocation



28
29
30
31
32
# File 'lib/train/plugins/base_connection.rb', line 28

def initialize(options = nil)
  @options = options || {}
  @logger = @options.delete(:logger) || Logger.new(STDOUT)
  @files = {}
end

Instance Attribute Details

#filesObject (readonly)

Provide access to the files cache.



22
23
24
# File 'lib/train/plugins/base_connection.rb', line 22

def files
  @files
end

Instance Method Details

#closeObject

Closes the session connection, if it is still active.



35
36
37
# File 'lib/train/plugins/base_connection.rb', line 35

def close
  # this method may be left unimplemented if that is applicable
end

#file(_path, *_args) ⇒ FileCommon

Interact with files on the target. Read, write, and get metadata from files via the transport.

Parameters:

  • path (String)

    which is being inspected

Returns:

  • (FileCommon)

    file object that allows for interaction



72
73
74
# File 'lib/train/plugins/base_connection.rb', line 72

def file(_path, *_args)
  fail Train::ClientError, "#{self.class} does not implement #file(...)"
end

#load_json(j) ⇒ Object



45
46
47
48
49
50
# File 'lib/train/plugins/base_connection.rb', line 45

def load_json(j)
  require 'train/transports/mock'
  j['files'].each do |path, jf|
    @files[path] = Train::Transports::Mock::Connection::File.from_json(jf)
  end
end

#login_commandLoginCommand

Builds a LoginCommand which can be used to open an interactive session on the remote host.

Returns:



80
81
82
# File 'lib/train/plugins/base_connection.rb', line 80

def 
  fail Train::ClientError, "#{self.class} does not implement #run_command()"
end

#osOSCommon

Get information on the operating system which this transport connects to.

Returns:

  • (OSCommon)

    operating system information



63
64
65
# File 'lib/train/plugins/base_connection.rb', line 63

def os
  fail Train::ClientError, "#{self.class} does not implement #os()"
end

#run_command(_command) ⇒ CommandResult

Execute a command using this connection.

Parameters:

  • command (String)

    command string to execute

Returns:



56
57
58
# File 'lib/train/plugins/base_connection.rb', line 56

def run_command(_command)
  fail Train::ClientError, "#{self.class} does not implement #run_command()"
end

#to_jsonObject



39
40
41
42
43
# File 'lib/train/plugins/base_connection.rb', line 39

def to_json
  {
    'files' => Hash[@files.map { |x, y| [x, y.to_json] }],
  }
end

#wait_until_readyObject

Block and return only when the remote host is prepared and ready to execute command and upload files. The semantics and details will vary by implementation, but a round trip through the hosted service is preferred to simply waiting on a socket to become available.



89
90
91
# File 'lib/train/plugins/base_connection.rb', line 89

def wait_until_ready
  # this method may be left unimplemented if that is applicablelog
end