Module: Train
- Defined in:
- lib/train/errors.rb,
lib/train.rb,
lib/train/options.rb,
lib/train/plugins.rb,
lib/train/version.rb
Overview
- Author
-
Dominik Richter (<[email protected]>)
Defined Under Namespace
Modules: Extras, Options, Transports Classes: ClientError, Plugins, TransportError, UserError
Constant Summary collapse
- VERSION =
'0.19.0'.freeze
Class Method Summary collapse
-
.create(name, *args) ⇒ Transport
Create a new transport instance, with the plugin indicated by the given name.
- .group_keys_and_keyfiles(conf) ⇒ Object
-
.load_transport(name) ⇒ Train::Transport
Load the transport plugin indicated by name.
-
.options(name) ⇒ Hash
Retrieve the configuration options of a transport plugin.
-
.plugin(version = 1) ⇒ Transport
Create a new plugin by inheriting from the class returned by this method.
-
.target_config(config = nil) ⇒ Object
Resolve target configuration in URI-scheme into all respective fields and merge with existing configuration.
- .validate_backend(conf, default = :local) ⇒ Object
Class Method Details
.create(name, *args) ⇒ Transport
Create a new transport instance, with the plugin indicated by the given name.
17 18 19 20 |
# File 'lib/train.rb', line 17 def self.create(name, *args) cls = load_transport(name) cls.new(*args) unless cls.nil? end |
.group_keys_and_keyfiles(conf) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/train.rb', line 101 def self.group_keys_and_keyfiles(conf) # in case the user specified a key-file, register it that way # we will clear the list of keys and put keys and key_files separately keys_mixed = conf[:keys] return if keys_mixed.nil? conf[:key_files] = [] conf[:keys] = [] keys_mixed.each do |key| if !key.nil? and File.file?(key) conf[:key_files].push(key) else conf[:keys].push(key) end end end |
.load_transport(name) ⇒ Train::Transport
Load the transport plugin indicated by name. If the plugin is not yet found in the plugin registry, it will be attempted to load from ‘train/transports/plugin_name`.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/train.rb', line 37 def self.load_transport(name) res = Train::Plugins.registry[name.to_s] return res unless res.nil? # if the plugin wasnt loaded yet: require 'train/transports/' + name.to_s Train::Plugins.registry[name.to_s] rescue LoadError => _ raise Train::UserError, "Can't find train plugin #{name.inspect}. Please install it first." end |
.options(name) ⇒ Hash
Retrieve the configuration options of a transport plugin.
26 27 28 29 |
# File 'lib/train.rb', line 26 def self.(name) cls = load_transport(name) cls. unless cls.nil? end |
.plugin(version = 1) ⇒ Transport
Create a new plugin by inheriting from the class returned by this method. Create a versioned plugin by providing the transport layer plugin version to this method. It will then select the correct class to inherit from.
The plugin version determins what methods will be available to your plugin.
31 32 33 34 35 36 37 38 39 |
# File 'lib/train/plugins.rb', line 31 def self.plugin(version = 1) if version != 1 fail ClientError, 'Only understand train plugin version 1. You are trying to '\ "initialize a train plugin #{version}, which is not supported "\ 'in the current release of train.' end ::Train::Plugins::Transport end |
.target_config(config = nil) ⇒ Object
Resolve target configuration in URI-scheme into all respective fields and merge with existing configuration. e.g. ssh://bob@remote => backend: ssh, user: bob, host: remote
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/train.rb', line 52 def self.target_config(config = nil) # rubocop:disable Metrics/AbcSize conf = config.nil? ? {} : config.dup # symbolize keys conf = conf.each_with_object({}) do |(k, v), acc| acc[k.to_sym] = v acc end group_keys_and_keyfiles(conf) return conf if conf[:target].to_s.empty? # split up the target's host/scheme configuration uri = URI.parse(conf[:target].to_s) unless uri.host.nil? and uri.scheme.nil? conf[:backend] ||= uri.scheme conf[:host] ||= uri.host conf[:port] ||= uri.port conf[:user] ||= uri.user conf[:password] ||= uri.password conf[:path] ||= uri.path end # ensure path is nil, if its empty; e.g. required to reset defaults for winrm conf[:path] = nil if !conf[:path].nil? && conf[:path].to_s.empty? # return the updated config conf end |
.validate_backend(conf, default = :local) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/train.rb', line 83 def self.validate_backend(conf, default = :local) return default if conf.nil? res = conf[:backend] return res if !res.nil? if !conf[:target].nil? fail Train::UserError, 'Cannot determine backend from target '\ "configuration #{conf[:target].inspect}. Valid example: ssh://192.168.0.1." end if !conf[:host].nil? fail Train::UserError, 'Host configured, but no backend was provided. Please '\ 'specify how you want to connect. Valid example: ssh://192.168.0.1.' end conf[:backend] = default end |