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 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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/train/plugins/base_connection.rb', line 22

def initialize(options = nil)
  @options = options || {}
  @logger = @options.delete(:logger) || Logger.new(STDOUT)
  Train::Platforms::Detect::Specifications::OS.load

  # default caching options
  @cache_enabled = {
    file: true,
    command: false,
  }

  @cache = {}
  @cache_enabled.each_key do |type|
    clear_cache(type)
  end
end

Instance Method Details

#cache_enabled?(type) ⇒ Boolean

Returns:

  • (Boolean)


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

def cache_enabled?(type)
  @cache_enabled[type.to_sym]
end

#closeObject

Closes the session connection, if it is still active.



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

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

#disable_cache(type) ⇒ Object



50
51
52
53
54
# File 'lib/train/plugins/base_connection.rb', line 50

def disable_cache(type)
  fail Train::UnknownCacheType, "#{type} is not a valid cache type" unless @cache_enabled.keys.include?(type.to_sym)
  @cache_enabled[type.to_sym] = false
  clear_cache(type.to_sym)
end

#enable_cache(type) ⇒ Object

Enable caching types for Train. Currently we support :file and :command types



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

def enable_cache(type)
  fail Train::UnknownCacheType, "#{type} is not a valid cache type" unless @cache_enabled.keys.include?(type.to_sym)
  @cache_enabled[type.to_sym] = true
end

#file(path, *args) ⇒ Object

This is the main file call for all connections. This will call the private file_via_connection on the connection with optional caching



98
99
100
101
102
# File 'lib/train/plugins/base_connection.rb', line 98

def file(path, *args)
  return file_via_connection(path, *args) unless cache_enabled?(:file)

  @cache[:file][path] ||= file_via_connection(path, *args)
end

#load_json(j) ⇒ Object



67
68
69
70
71
72
# File 'lib/train/plugins/base_connection.rb', line 67

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

#local?Boolean

Is this a local transport?

Returns:

  • (Boolean)


75
76
77
# File 'lib/train/plugins/base_connection.rb', line 75

def local?
  false
end

#login_commandLoginCommand

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

Returns:



108
109
110
# File 'lib/train/plugins/base_connection.rb', line 108

def 
  fail NotImplementedError, "#{self.class} does not implement #login_command()"
end

#platformPlatform Also known as: os

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

Returns:

  • (Platform)

    system information



82
83
84
# File 'lib/train/plugins/base_connection.rb', line 82

def platform
  @platform ||= Train::Platforms::Detect.scan(self)
end

#run_command(cmd) ⇒ Object

This is the main command call for all connections. This will call the private run_command_via_connection on the connection with optional caching



90
91
92
93
94
# File 'lib/train/plugins/base_connection.rb', line 90

def run_command(cmd)
  return run_command_via_connection(cmd) unless cache_enabled?(:command)

  @cache[:command][cmd] ||= run_command_via_connection(cmd)
end

#to_jsonObject



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

def to_json
  {
    'files' => Hash[@cache[:file].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.



117
118
119
# File 'lib/train/plugins/base_connection.rb', line 117

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