remotus 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a719c4faff9f018cf95614e68886b53475218bb27a73cde4579bbf7eb90d638b
4
- data.tar.gz: f66e7025b43d7d343f269c076824eda4061b7565b418ee3637a57e17c404d9d6
3
+ metadata.gz: 11e887a11735554ddeb7ebc280bb8c76881d3c00fa174964ecf2c3360c70028d
4
+ data.tar.gz: 92c02029466a391a243b78de329413bdbccf1f591a89bc0bcbb060a7ad51d5ca
5
5
  SHA512:
6
- metadata.gz: a078c9a5e63f20059d6148e48356efead6f2520f47d0225c94a33cf3721b5294f9f6e893e166e53fb4a4609f3e5c62dee9872ea023d460d0731581d812eeb9bf
7
- data.tar.gz: b7595d1ab8c6c7df4506e590a19a6ab0653d27edcfec4a1eef1764c21e3d9eb2d2edaffae6beff4bbf09465b2d84d029bf15c89a7a5cb944104e712543ea5347
6
+ metadata.gz: 0c1967843958647f1c782bbda27d5b03519febf34faa841a7fe92f190a0ed29d733b50d4fe0b304fea624c8a1de7f3140f34fd5bb9068cc8d9c2fa8401bda0b6
7
+ data.tar.gz: cfd15ef457c8aaa9188588fcbd53d55fd62378f4e224d33df57b4a818ad69b839789be32e3ee893588f278e2be6885d23572d32065df707b4f5a6735ad120c04
data/.rubocop.yml CHANGED
@@ -39,3 +39,6 @@ Metrics/CyclomaticComplexity:
39
39
  Metrics/PerceivedComplexity:
40
40
  Exclude:
41
41
  - lib/remotus/ssh_connection.rb
42
+
43
+ Metrics/ParameterLists:
44
+ Max: 6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- remotus (0.1.0)
4
+ remotus (0.2.0)
5
5
  connection_pool (~> 2.2)
6
6
  net-scp (~> 3.0)
7
7
  net-ssh (~> 6.1)
@@ -88,6 +88,7 @@ GEM
88
88
  yard (0.9.26)
89
89
 
90
90
  PLATFORMS
91
+ ruby
91
92
  x86_64-linux
92
93
 
93
94
  DEPENDENCIES
@@ -100,4 +101,4 @@ DEPENDENCIES
100
101
  yard (~> 0.9)
101
102
 
102
103
  BUNDLED WITH
103
- 2.2.9
104
+ 2.2.14
data/README.md CHANGED
@@ -27,6 +27,9 @@ connection = Remotus.connect("remotehost.local")
27
27
  # Initialize a new connection pool to remotehost.local with a defined protocol and port
28
28
  connection = Remotus.connect("remotehost.local", proto: :ssh, port: 2222)
29
29
 
30
+ # Initialize a new connection pool to remotehost.local with a defined protocol and port and arbitrary metadata
31
+ connection = Remotus.connect("remotehost.local", proto: :ssh, port: 2222, company: "Test Corp", location: "Oslo")
32
+
30
33
  # Create a credential for the new connection pool
31
34
  connection.credential = Remotus::Auth::Credential.new("username", "password")
32
35
 
data/lib/remotus.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "remotus/core_ext/string"
3
4
  require "remotus/version"
4
5
  require "remotus/logger"
5
6
  require "remotus/pool"
@@ -139,4 +140,7 @@ module Remotus
139
140
 
140
141
  # Failed to find credential password when executing sudo command
141
142
  class MissingSudoPassword < Error; end
143
+
144
+ # Raised when an invalid metadata key is provided to a Remotus HostPool
145
+ class InvalidMetadataKey < Error; end
142
146
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Remotus
4
+ # Core Ruby extensions
5
+ module CoreExt
6
+ # String extension module
7
+ module String
8
+ unless method_defined?(:to_method_name)
9
+ #
10
+ # Converts a string into a safe method name that can be used for instance variables
11
+ #
12
+ # @return [Symbol] Method name
13
+ #
14
+ def to_method_name
15
+ gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
16
+ .gsub(/([a-z])([A-Z])/, '\1_\2')
17
+ .tr(" ", "_")
18
+ .gsub(/(?:[^_a-zA-Z0-9]|^\d+)/, "")
19
+ .downcase
20
+ .to_sym
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ String.include(Remotus::CoreExt::String)
@@ -4,6 +4,7 @@ require "remotus"
4
4
  require "remotus/auth"
5
5
  require "remotus/ssh_connection"
6
6
  require "remotus/winrm_connection"
7
+ require "remotus/core_ext/string"
7
8
  require "connection_pool"
8
9
 
9
10
  module Remotus
@@ -38,10 +39,16 @@ module Remotus
38
39
  # @param [Integer] timeout amount of time to wait for a connection from the pool (optional)
39
40
  # @param [Integer] port port to use for the connection
40
41
  # @param [Symbol] proto protocol to use for the connection (:winrm, :ssh), must be specified if port is specified
42
+ # @param [Hash] metadata metadata for this connection. Useful for providing additional information to various authentication stores
43
+ # should be specified using snake_case symbol keys. If keys are not snake_case, they will be converted.
41
44
  #
42
- def initialize(host, size: DEFAULT_POOL_SIZE, timeout: DEFAULT_EXPIRATION_SECONDS, port: nil, proto: nil)
45
+ def initialize(host, size: DEFAULT_POOL_SIZE, timeout: DEFAULT_EXPIRATION_SECONDS, port: nil, proto: nil, **metadata)
43
46
  Remotus.logger.debug { "Creating host pool for #{host}" }
44
47
 
48
+ # Update metadata information and generate the necessary accessor methods
49
+ @metadata = metadata
50
+ update_metadata_methods
51
+
45
52
  @host = host
46
53
  @proto = proto || Remotus.host_type(host)
47
54
 
@@ -177,5 +184,49 @@ module Remotus
177
184
  credential = Remotus::Auth::Credential.from_hash(credential) unless credential.is_a?(Remotus::Auth::Credential)
178
185
  Remotus::Auth.cache[host] = credential
179
186
  end
187
+
188
+ #
189
+ # Gets HostPool metadata at key
190
+ #
191
+ # @param [Object] key metadata key
192
+ #
193
+ # @return [Object] metadata value
194
+ #
195
+ def [](key)
196
+ @metadata[key]
197
+ end
198
+
199
+ #
200
+ # Sets HostPool metadata value at key
201
+ #
202
+ # @param [Object] key metadata key
203
+ # @param [Object] value new metadata value
204
+ #
205
+ def []=(key, value)
206
+ @metadata[key] = value
207
+ update_metadata_methods
208
+ end
209
+
210
+ private
211
+
212
+ #
213
+ # Updates accessor methods for any defined metadata in @metadata
214
+ #
215
+ def update_metadata_methods
216
+ @metadata.each do |k, _v|
217
+ safe_key = k.to_s.to_method_name
218
+
219
+ # Do not allow metadata to be set that conflicts with base HostPool instance methods
220
+ if RESERVED_METHOD_NAMES.include?(safe_key)
221
+ raise Remotus::InvalidMetadataKey, "Cannot use reserved method name #{safe_key} for a metadata key"
222
+ end
223
+
224
+ define_singleton_method(safe_key) { @metadata[k] } unless respond_to?(safe_key)
225
+ define_singleton_method("#{safe_key}=".to_sym) { |new_value| @metadata[k] = new_value } unless respond_to?("#{safe_key}=".to_sym)
226
+ end
227
+ end
228
+
229
+ # Array of all reserved method names, must set after all methods are defined
230
+ RESERVED_METHOD_NAMES = Remotus::HostPool.instance_methods.freeze
180
231
  end
181
232
  end
data/lib/remotus/pool.rb CHANGED
@@ -145,6 +145,9 @@ module Remotus
145
145
 
146
146
  options.each do |k, v|
147
147
  Remotus.logger.debug { "Checking if option #{k} => #{v} has changed" }
148
+
149
+ next unless pool[host].respond_to?(k.to_sym)
150
+
148
151
  host_value = pool[host].send(k.to_sym)
149
152
 
150
153
  if v != host_value
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Remotus
4
4
  # Remotus gem version
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remotus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Newell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-10 00:00:00.000000000 Z
11
+ date: 2021-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: connection_pool
@@ -188,6 +188,7 @@ files:
188
188
  - lib/remotus/auth/credential.rb
189
189
  - lib/remotus/auth/hash_store.rb
190
190
  - lib/remotus/auth/store.rb
191
+ - lib/remotus/core_ext/string.rb
191
192
  - lib/remotus/host_pool.rb
192
193
  - lib/remotus/logger.rb
193
194
  - lib/remotus/pool.rb