kanrisuru 0.7.1 → 0.7.2

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: 93755393c0e2f4b2353bdd4bda27df54e4aceccf3ae7cd6d77f0e4cbc8c908c7
4
- data.tar.gz: f28c287f77b9edcb505e319030d67fc51287b99dc23cc0903fcfcb7dcec8c52c
3
+ metadata.gz: 4ffb2183c8f1a5610dd416259a663787076f79cb8502fe8656a52d2c2cd1a7e0
4
+ data.tar.gz: 43b63c2e14d7f29356cd110261ef1ba55aabd50e97830e4aea3770429625caf8
5
5
  SHA512:
6
- metadata.gz: 493e8a9019139f5326e50b2ade2ef1f5c631bec796fc08655455e356b0718ce41f5e34d1b348cc88f8f6c1f6857a56ed36dff30c56f285866c525ed07500b342
7
- data.tar.gz: 4b4a6141a7bbc4731c6693639071470291881876589ee8033195d3f9e32a13fd6bad444ccfd2cf8bf1f5d302c93b3a09d6cf2e32ecda42fef954c2d2a93595d6
6
+ metadata.gz: c0b333f36fb00bd16177fe434522c2bd1717db0019d39dba681ab7810b786aca050e6ea79c2647a1bedf4f0bdb4d7e6ffb26079b2ad10d92d3b6da6e4372ee56
7
+ data.tar.gz: 17377294cc356a44b1a76c6d32bae36940d8ec43d31cdbf0716c4d347735a8d30591e94a8babc8f7cd70d68127f7177d6a2dd70015c4eb8132148868e47a227d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## Kanrisuru 0.7.2 (August 9, 2021) ##
2
+ * Fixed bug with the `os_method_cache` instance variable set in the namespaced instance of a host. This was causing collision issues inbetween host instances, where, hosts with the same aliased method name was getting overwritten (with a different OS), since the namespace instance variable existing on the host class definition wasn't getting reset inbetween host instantiations. Given that the `os_method_cache` is normally re-instantiated, this bug fix addresses this so that the `os_method_cache` is always defined on the host instance, ie:
3
+
4
+ ```ruby
5
+ host.instance_variable_get(:@os_method_cache)
6
+ host.instance_variable_set(:@os_method_cache, os_method_cache)
7
+ ```
8
+ This is done instead of being saved on the namespace module. With the previous bug fix of using namespaced keys, there's no way for a method to be overwritten otherwise with a global `os_method_cache`.
9
+
1
10
  ## Kanrisuru 0.7.1 (August 8, 2021) ##
2
11
  * Fix bug with `os_include` when caching namespace unbound methods, use the namespace in the
3
12
  cache key to avoid any namespace collisions with the same method name, namely:
@@ -8,7 +8,6 @@ module Kanrisuru
8
8
  def self.extended(base)
9
9
  base.instance_variable_set(:@os_method_properties, {})
10
10
  base.instance_variable_set(:@os_methods, Set.new)
11
- base.instance_variable_set(:@os_method_cache, {})
12
11
  end
13
12
 
14
13
  def os_define(os_name, method_name, options = {})
@@ -102,12 +101,6 @@ module Kanrisuru
102
101
  include_methods = (public_methods + protected_methods + private_methods).flatten
103
102
 
104
103
  include_method_bindings = proc do
105
- define_method 'os_method_cache' do
106
- @os_method_cache ||= {}
107
- end
108
-
109
- private :os_method_cache
110
-
111
104
  include_methods.each do |method_name|
112
105
  define_method method_name do |*args, &block|
113
106
  unbound_method = mod.instance_method(method_name)
@@ -144,10 +137,11 @@ module Kanrisuru
144
137
  ## defined with the methods added.
145
138
  if Kanrisuru::Remote::Host.instance_variable_defined?("@#{namespace}")
146
139
  namespace_class = Kanrisuru::Remote::Host.const_get(Kanrisuru::Util.camelize(namespace))
147
- namespace_instance = instance_variable_get("@#{namespace}")
140
+ namespace_instance = Kanrisuru::Remote::Host.instance_variable_get("@#{namespace}")
148
141
  else
149
142
  namespace_class = Kanrisuru::Remote::Host.const_set(Kanrisuru::Util.camelize(namespace), Class.new)
150
143
  namespace_instance = Kanrisuru::Remote::Host.instance_variable_set("@#{namespace}", namespace_class.new)
144
+
151
145
  class_eval do
152
146
  define_method namespace do
153
147
  namespace_instance.instance_variable_set(:@host, self)
@@ -165,14 +159,16 @@ module Kanrisuru
165
159
  os_method_names.each do |method_name|
166
160
  if namespace
167
161
  namespace_class.class_eval do
162
+
168
163
  define_method method_name do |*args, &block|
169
164
  unbound_method = nil
170
165
 
166
+ host = namespace_instance.instance_variable_get(:@host)
167
+ os_method_cache = host.instance_variable_get(:@os_method_cache) || {}
168
+
171
169
  if os_method_cache.key?("#{namespace}.#{method_name}")
172
170
  unbound_method = os_method_cache["#{namespace}.#{method_name}"]
173
171
  else
174
- host = namespace_instance.instance_variable_get(:@host)
175
-
176
172
  ## Find the correct method to resolve based on the OS for the remote host.
177
173
  defined_method_name = host.resolve_os_method_name(os_method_properties, method_name)
178
174
  unless defined_method_name
@@ -186,6 +182,7 @@ module Kanrisuru
186
182
  ## Cache the unbound method on this host instance for faster resolution on
187
183
  ## the next invocation of this method
188
184
  os_method_cache["#{namespace}.#{method_name}"] = unbound_method
185
+ host.instance_variable_set(:@os_method_cache, os_method_cache)
189
186
  end
190
187
 
191
188
  ## Bind the method to host instance and
@@ -196,12 +193,13 @@ module Kanrisuru
196
193
  else
197
194
  define_method method_name do |*args, &block|
198
195
  unbound_method = nil
196
+
197
+ host = self
198
+ os_method_cache = host.instance_variable_get(:@os_method_cache) || {}
199
199
 
200
200
  if os_method_cache.key?(method_name)
201
201
  unbound_method = os_method_cache[method_name]
202
202
  else
203
- host = self
204
-
205
203
  ## Find the correct method to resolve based on the OS for the remote host.
206
204
  defined_method_name = host.resolve_os_method_name(os_method_properties, method_name)
207
205
  raise NoMethodError, "undefined method `#{method_name}' for #{self.class}" unless defined_method_name
@@ -213,6 +211,7 @@ module Kanrisuru
213
211
  ## Cache the unbound method on this host instance for faster resolution on
214
212
  ## the next invocation of this method
215
213
  os_method_cache[method_name] = unbound_method
214
+ host.instance_variable_set(:@os_method_cache, os_method_cache)
216
215
  end
217
216
 
218
217
  ## Bind the method to host instance and
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kanrisuru
4
- VERSION = '0.7.1'
4
+ VERSION = '0.7.2'
5
5
  end
@@ -40,18 +40,66 @@ module Kanrisuru
40
40
  a - b
41
41
  end
42
42
  end
43
+
44
+ module TestAliasNames
45
+ extend Kanrisuru::OsPackage::Define
46
+
47
+ os_define :fedora, :output_fedora, alias: :output
48
+ os_define :debian, :output_debian, alias: :output
49
+ os_define :sles, :output_sles, alias: :output
50
+
51
+ def output_debian
52
+ 'debian'
53
+ end
54
+
55
+ def output_fedora
56
+ 'fedora'
57
+ end
58
+
59
+ def output_sles
60
+ 'sles'
61
+ end
62
+ end
63
+
64
+ module TestAliasNamesNamespace
65
+ extend Kanrisuru::OsPackage::Define
66
+
67
+ os_define :fedora, :output_fedora, alias: :output
68
+ os_define :debian, :output_debian, alias: :output
69
+ os_define :sles, :output_sles, alias: :output
70
+
71
+ def output_debian
72
+ 'debian'
73
+ end
74
+
75
+ def output_fedora
76
+ 'fedora'
77
+ end
78
+
79
+ def output_sles
80
+ 'sles'
81
+ end
82
+ end
43
83
  end
44
84
 
45
85
  module Kanrisuru
46
86
  module Remote
47
87
  class Host
48
88
  os_include Kanrisuru::TestInclude
89
+
90
+ os_include Kanrisuru::TestAliasNames
91
+ os_include Kanrisuru::TestAliasNamesNamespace, namespace: :alias
92
+
49
93
  os_include Kanrisuru::TestNamespaceAdditions, namespace: :asdf
50
94
  os_include Kanrisuru::TestNamespace, namespace: :asdf
51
95
  end
52
96
 
53
97
  class Cluster
54
98
  os_collection Kanrisuru::TestInclude
99
+
100
+ os_collection Kanrisuru::TestAliasNames
101
+ os_collection Kanrisuru::TestAliasNamesNamespace, namespace: :alias
102
+
55
103
  os_collection Kanrisuru::TestNamespaceAdditions, namespace: :asdf
56
104
  os_collection Kanrisuru::TestNamespace, namespace: :asdf
57
105
  end
@@ -94,6 +142,52 @@ RSpec.describe Kanrisuru::OsPackage do
94
142
  expect(cluster.asdf.tester).to be_instance_of(Array)
95
143
 
96
144
  host.disconnect
145
+ host2.disconnect
146
+ cluster.disconnect
147
+ end
148
+
149
+ it 'includes the correct alias named method' do
150
+ host1 = Kanrisuru::Remote::Host.new(host: 'ubuntu-host', username: 'ubuntu', keys: ['~/.ssh/id_rsa'])
151
+ host2 = Kanrisuru::Remote::Host.new(host: 'centos-host', username: 'centos', keys: ['~/.ssh/id_rsa'])
152
+ host3 = Kanrisuru::Remote::Host.new(host: 'opensuse-host', username: 'opensuse', keys: ['~/.ssh/id_rsa'])
153
+
154
+ cluster = Kanrisuru::Remote::Cluster.new([host1, host2, host3])
155
+
156
+ expect(host1).to respond_to(:output)
157
+ expect(host2).to respond_to(:output)
158
+ expect(host3).to respond_to(:output)
159
+ expect(host1.output).to eq('debian')
160
+ expect(host2.output).to eq('fedora')
161
+ expect(host3.output).to eq('sles')
162
+
163
+ expect(cluster).to respond_to(:output)
164
+ expect(cluster.output).to eq([
165
+ {:host=>"ubuntu-host", :result=>"debian"},
166
+ {:host=>"centos-host", :result=>"fedora"},
167
+ {:host=>"opensuse-host", :result=>"sles"}
168
+ ])
169
+
170
+ expect(host1).to respond_to(:alias)
171
+ expect(host2).to respond_to(:alias)
172
+ expect(host3).to respond_to(:alias)
173
+ expect(host1.alias).to respond_to(:output)
174
+ expect(host2.alias).to respond_to(:output)
175
+ expect(host3.alias).to respond_to(:output)
176
+ expect(host1.alias.output).to eq('debian')
177
+ expect(host2.alias.output).to eq('fedora')
178
+ expect(host3.alias.output).to eq('sles')
179
+
180
+ expect(cluster).to respond_to(:alias)
181
+ expect(cluster.alias).to respond_to(:output)
182
+ expect(cluster.alias.output).to eq([
183
+ {:host=>"ubuntu-host", :result=>"debian"},
184
+ {:host=>"centos-host", :result=>"fedora"},
185
+ {:host=>"opensuse-host", :result=>"sles"}
186
+ ])
187
+
188
+ host1.disconnect
189
+ host2.disconnect
190
+ host3.disconnect
97
191
  cluster.disconnect
98
192
  end
99
193
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kanrisuru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Mammina