Class: Train::Plugins::Transport::BaseConnection
- Inherits:
-
Object
- Object
- Train::Plugins::Transport::BaseConnection
- 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.
Instance Method Summary collapse
- #cache_enabled?(type) ⇒ Boolean
-
#close ⇒ Object
Closes the session connection, if it is still active.
- #direct_platform(name, platform_details = nil) ⇒ Object
- #disable_cache(type) ⇒ Object
-
#enable_cache(type) ⇒ Object
Enable caching types for Train.
- #family_hierarchy(plat) ⇒ Object
-
#file(path, *args) ⇒ Object
This is the main file call for all connections.
-
#initialize(options = nil) {|self| ... } ⇒ BaseConnection
constructor
Create a new Connection instance.
- #load_json(j) ⇒ Object
-
#local? ⇒ Boolean
Is this a local transport?.
-
#login_command ⇒ LoginCommand
Builds a LoginCommand which can be used to open an interactive session on the remote host.
-
#platform ⇒ Platform
(also: #os)
Get information on the operating system which this transport connects to.
-
#run_command(cmd) ⇒ Object
This is the main command call for all connections.
- #to_json ⇒ Object
-
#wait_until_ready ⇒ Object
Block and return only when the remote host is prepared and ready to execute command and upload files.
Constructor Details
#initialize(options = nil) {|self| ... } ⇒ BaseConnection
Create a new Connection instance.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/train/plugins/base_connection.rb', line 22 def initialize( = nil) @options = || {} @logger = @options.delete(:logger) || Logger.new(STDOUT) Train::Platforms::Detect::Specifications::OS.load Train::Platforms::Detect::Specifications::Api.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
40 41 42 |
# File 'lib/train/plugins/base_connection.rb', line 40 def cache_enabled?(type) @cache_enabled[type.to_sym] end |
#close ⇒ Object
Closes the session connection, if it is still active.
58 59 60 |
# File 'lib/train/plugins/base_connection.rb', line 58 def close # this method may be left unimplemented if that is applicable end |
#direct_platform(name, platform_details = nil) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/train/plugins/base_connection.rb', line 80 def direct_platform(name, platform_details = nil) plat = Train::Platforms.name(name) plat.backend = self plat.platform = platform_details unless platform_details.nil? plat.family_hierarchy = family_hierarchy(plat).flatten plat.add_platform_methods plat end |
#disable_cache(type) ⇒ Object
51 52 53 54 55 |
# File 'lib/train/plugins/base_connection.rb', line 51 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
46 47 48 49 |
# File 'lib/train/plugins/base_connection.rb', line 46 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 |
#family_hierarchy(plat) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/train/plugins/base_connection.rb', line 89 def family_hierarchy(plat) plat.families.each_with_object([]) do |(k, _v), memo| memo << k.name memo << family_hierarchy(k) unless k.families.empty? end 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
115 116 117 118 119 |
# File 'lib/train/plugins/base_connection.rb', line 115 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
68 69 70 71 72 73 |
# File 'lib/train/plugins/base_connection.rb', line 68 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?
76 77 78 |
# File 'lib/train/plugins/base_connection.rb', line 76 def local? false end |
#login_command ⇒ LoginCommand
Builds a LoginCommand which can be used to open an interactive session on the remote host.
125 126 127 |
# File 'lib/train/plugins/base_connection.rb', line 125 def login_command fail NotImplementedError, "#{self.class} does not implement #login_command()" end |
#platform ⇒ Platform Also known as: os
Get information on the operating system which this transport connects to.
99 100 101 |
# File 'lib/train/plugins/base_connection.rb', line 99 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
107 108 109 110 111 |
# File 'lib/train/plugins/base_connection.rb', line 107 def run_command(cmd) return run_command_via_connection(cmd) unless cache_enabled?(:command) @cache[:command][cmd] ||= run_command_via_connection(cmd) end |
#to_json ⇒ Object
62 63 64 65 66 |
# File 'lib/train/plugins/base_connection.rb', line 62 def to_json { 'files' => Hash[@cache[:file].map { |x, y| [x, y.to_json] }], } end |
#wait_until_ready ⇒ Object
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.
134 135 136 |
# File 'lib/train/plugins/base_connection.rb', line 134 def wait_until_ready # this method may be left unimplemented if that is applicablelog end |