chef-provisioning-ssh 0.0.7 → 0.0.8.rc.1

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
  SHA1:
3
- metadata.gz: f8be4eea284f7d7404c6fb4465f3b0abf8906b4f
4
- data.tar.gz: 64b5600e5aeb58326cdfc8622d00f5c696f12863
3
+ metadata.gz: f3271039d59883f27dc58d29d172f54d37aa72a2
4
+ data.tar.gz: eac579801f2bb8492c0bb74407a0f2baf082d4b7
5
5
  SHA512:
6
- metadata.gz: 2604aabe0c9063ae30f99a712913bbe99415baa73d71f5f3f005506bb71e8c89176f6e9a4f55ae5b059e09402fab0089834229cd918fef5e7fb05abdea68eff5
7
- data.tar.gz: cebce0acee1c06106a10fe291345d48766d86533e1e33ee8ff75ac118fae2071971784cbc8b81e73ac206006527ba878dddeb7cd0df61895f5757be7ea0c851a
6
+ metadata.gz: 47f9c741cf883fde0972e57c4e7083e8f95f3a822e2b860ae9b807004778cfa71b7e70643d32de443d02bab2762492ab472bfd096e25361e65eda3069ae46c2c
7
+ data.tar.gz: 76485ca7698c2e22c864801b6769a4f68a631ff8f7a334ff440711164565550e509cc38d692db52451ddc2d9852595cbf8b180e15d8d635bc7d91a777b8231dc
@@ -41,23 +41,31 @@ class Chef
41
41
  end
42
42
 
43
43
  def allocate_machine(action_handler, machine_spec, machine_options)
44
- existing_machine = existing_ssh_machine(machine_spec)
45
- if !existing_machine
46
- validate_machine_options(action_handler, machine_spec, machine_options)
47
- ensure_ssh_cluster(action_handler)
48
- ssh_machine = create_ssh_machine(action_handler, machine_spec, machine_options)
44
+ existing_machine = ssh_machine_exists?(machine_spec)
45
+ ssh_machine_file_updated = create_machine(action_handler, machine_spec, machine_options)
46
+
47
+ if !existing_machine || !machine_spec.location
49
48
  machine_spec.location = {
50
49
  'driver_url' => driver_url,
51
50
  'driver_version' => Chef::Provisioning::SshDriver::VERSION,
52
51
  'target_name' => machine_spec.name,
53
- 'ssh_machine_file' => ssh_machine,
54
- 'allocated_at' => Time.now.utc.to_s
52
+ 'ssh_machine_file' => ssh_machine_file_updated,
53
+ 'allocated_at' => Time.now.utc.to_s,
54
+ 'updated_at' => Time.now.utc.to_s,
55
+ 'host' => action_handler.host_node
55
56
  }
57
+ elsif ssh_machine_file_updated
58
+ machine_spec.location['updated_at'] = Time.now.utc.to_s
59
+ end
60
+
61
+ if machine_spec.location && (machine_spec.location['driver_version'] != Chef::Provisioning::SshDriver::VERSION)
62
+ machine_spec.location['driver_version'] = Chef::Provisioning::SshDriver::VERSION
56
63
  end
64
+
57
65
  end
58
66
 
59
67
  def ready_machine(action_handler, machine_spec, machine_options)
60
- ssh_machine = existing_ssh_machine(machine_spec)
68
+ ssh_machine = existing_ssh_machine_to_sym(machine_spec)
61
69
 
62
70
  if !ssh_machine
63
71
  raise "SSH Machine #{machine_spec.name} does not have a machine file associated with it!"
@@ -68,21 +76,29 @@ class Chef
68
76
  end
69
77
 
70
78
  def connect_to_machine(machine_spec, machine_options)
71
- machine_for(machine_spec, machine_options)
79
+ ssh_machine = existing_ssh_machine_to_sym(machine_spec)
80
+ machine_for(machine_spec, ssh_machine)
72
81
  end
73
82
 
74
83
  def destroy_machine(action_handler, machine_spec, machine_options)
75
- ssh_machine = existing_ssh_machine(machine_spec)
84
+ ssh_machine = ssh_machine_exists?(machine_spec)
76
85
 
77
- if !ssh_machine
86
+ if !ssh_machine || !::File.exists?(machine_spec.location['ssh_machine_file'])
78
87
  raise "SSH Machine #{machine_spec.name} does not have a machine file associated with it!"
79
- end
88
+ else
89
+ Chef::Provisioning.inline_resource(action_handler) do
90
+ file machine_spec.location['ssh_machine_file'] do
91
+ action :delete
92
+ backup false
93
+ end
94
+ end
95
+ end
80
96
 
81
97
 
82
98
  end
83
99
 
84
100
  def stop_machine(action_handler, machine_spec, machine_options)
85
- ssh_machine = existing_ssh_machine(machine_spec)
101
+ ssh_machine = existing_ssh_machine_to_sym(machine_spec)
86
102
 
87
103
  if !ssh_machine
88
104
  raise "SSH Machine #{machine_spec.name} does not have a machine file associated with it!"
@@ -92,13 +108,13 @@ class Chef
92
108
  end
93
109
 
94
110
  def machine_for(machine_spec, machine_options)
95
- ssh_machine = existing_ssh_machine(machine_spec)
111
+ ssh_machine = existing_ssh_machine_to_sym(machine_spec)
96
112
 
97
113
  if !ssh_machine
98
114
  raise "SSH Machine #{machine_spec.name} does not have a machine file associated with it!"
99
115
  end
100
116
 
101
- if ssh_machine[:is_windows]
117
+ if ssh_machine[:transport_options][:is_windows]
102
118
  Chef::Provisioning::Machine::WindowsMachine.new(machine_spec,
103
119
  transport_for(ssh_machine),
104
120
  convergence_strategy_for(ssh_machine))
@@ -110,7 +126,7 @@ class Chef
110
126
  end
111
127
 
112
128
  def transport_for(ssh_machine)
113
- if ssh_machine[:is_windows]
129
+ if ssh_machine[:transport_options][:is_windows]
114
130
  create_winrm_transport(ssh_machine)
115
131
  else
116
132
  create_ssh_transport(ssh_machine)
@@ -118,31 +134,28 @@ class Chef
118
134
  end
119
135
 
120
136
  def convergence_strategy_for(ssh_machine)
121
- if ssh_machine[:is_windows]
137
+ if ssh_machine[:transport_options][:is_windows]
122
138
  Chef::Provisioning::ConvergenceStrategy::InstallMsi.
123
- new(ssh_machine[:convergence_options], config)
139
+ new(ssh_machine[:convergence_options], config)
124
140
  else
125
141
  Chef::Provisioning::ConvergenceStrategy::InstallSh.
126
- new(ssh_machine[:convergence_options], config)
142
+ new(ssh_machine[:convergence_options], config)
127
143
  end
128
144
  end
129
145
 
130
146
  def create_ssh_transport(ssh_machine)
131
- hostname = ssh_machine[:transport_options][:host] ||
132
- ssh_machine[:transport_options][:ip_address]
133
- username = ssh_machine[:transport_options][:username]
134
- ssh_options = ssh_machine[:transport_options][:ssh_options] ?
135
- ssh_machine[:transport_options][:ssh_options] : {}
136
- options = ssh_machine[:transport_options][:options] ?
137
- ssh_machine[:transport_options][:options] : {}
147
+ hostname = ssh_machine[:transport_options][:host]
148
+ username = ssh_machine[:transport_options][:username]
149
+ ssh_options = ssh_machine[:transport_options][:ssh_options]
150
+ options = ssh_machine[:transport_options][:options]
138
151
  Chef::Provisioning::Transport::SSH.new(hostname, username,
139
- ssh_options, {:prefix => 'sudo '}, config)
152
+ ssh_options, options, config)
140
153
  end
141
154
 
142
155
  def create_winrm_transport(ssh_machine)
143
- # TODO IPv6 loopback? What do we do for that?
156
+ # # TODO IPv6 loopback? What do we do for that?
144
157
  hostname = ssh_machine[:transport_options][:host] ||
145
- ssh_machine[:transport_options][:ip_address]
158
+ ssh_machine[:transport_options][:ip_address]
146
159
  port = ssh_machine[:transport_options][:port] || 5985
147
160
  # port = forwarded_ports[port] if forwarded_ports[port]
148
161
  endpoint = "http://#{hostname}:#{port}/wsman"
@@ -152,7 +165,6 @@ class Chef
152
165
  :pass => ssh_machine[:transport_options][:password],
153
166
  :disable_sspi => true
154
167
  }
155
-
156
168
  Chef::Provisioning::Transport::WinRM.new(endpoint, type, options, config)
157
169
  end
158
170
 
@@ -192,48 +204,48 @@ class Chef
192
204
 
193
205
  missing = req_and_valid_fields.flatten - machine_options[:transport_options].keys
194
206
 
195
- for oof in one_of_fields
207
+ one_of_fields.each do |oof|
196
208
  if oof == oof & missing
197
209
  error_msgs << ":transport_options => :#{oof.join(" or :")} required."
198
210
  end
199
211
  missing -= oof
200
212
  end
201
213
 
202
- for missed in missing do
214
+ missing.each do |missed|
203
215
  error_msgs << ":transport_options => :#{missed} required."
204
216
  valid = false
205
217
  end
206
218
 
207
219
  extras = machine_options[:transport_options].keys - req_and_valid_fields.flatten
208
220
 
209
- for extra in extras do
210
- error_msgs << ":transport_options => :#{extra} not allowed."
221
+ extras.each do |extra|
222
+ error_msgs << ":transport_options => :#{extra} not allowed." unless extra == :port
211
223
  valid = false
212
224
  end
213
225
  else
214
226
  # Validate Unix Options
215
- req_fields = [[:host, :ip_address], :username]
227
+ req_fields = [[:host, :hostname, :ip_address], :username]
216
228
  one_of_fields = req_fields.select{ |i| i.kind_of?(Array)}
217
229
 
218
230
  missing = req_fields.flatten - machine_options[:transport_options].keys
219
231
 
220
- for oof in one_of_fields
232
+ one_of_fields.each do |oof|
221
233
  if oof == oof & missing
222
234
  error_msgs << ":transport_options => :#{oof.join(" or :")} required."
223
235
  end
224
236
  missing -= oof
225
237
  end
226
238
 
227
- for missed in missing do
239
+ missing.each do |missed|
228
240
  error_msgs << ":transport_options => :#{missed} required."
229
241
  valid = false
230
242
  end
231
243
 
232
- valid_fields = [:is_windows, :host, :ip_address, :username, :ssh_options, :options]
244
+ valid_fields = [:is_windows, :host, :hostname, :ip_address, :username, :ssh_options, :options]
233
245
 
234
246
  extras = machine_options[:transport_options].keys - valid_fields
235
247
 
236
- for extra in extras do
248
+ extras.each do |extra|
237
249
  error_msgs << ":transport_options => :#{extra} not allowed."
238
250
  valid = false
239
251
  end
@@ -243,7 +255,7 @@ class Chef
243
255
 
244
256
  extras = machine_options[:transport_options][:ssh_options].keys - valid_fields
245
257
 
246
- for extra in extras do
258
+ extras.each do |extra|
247
259
  error_msgs << ":transport_options => ssh_options => :#{extra} not allowed."
248
260
  valid = false
249
261
  end
@@ -254,7 +266,7 @@ class Chef
254
266
 
255
267
  extras = machine_options[:transport_options][:options].keys - valid_fields
256
268
 
257
- for extra in extras do
269
+ extras.each do |extra|
258
270
  error_msgs << ":transport_options => :options => :#{extra} not allowed."
259
271
  valid = false
260
272
  end
@@ -264,7 +276,7 @@ class Chef
264
276
 
265
277
  if !valid
266
278
  exception_string = "Machine Options for #{machine_spec.name} are invalid cannot create machine."
267
- for string in error_msgs do
279
+ error_msgs.each do |string|
268
280
  exception_string = "#{exception_string}\n #{string}"
269
281
  end
270
282
  raise exception_string
@@ -273,17 +285,47 @@ class Chef
273
285
 
274
286
  def ensure_ssh_cluster(action_handler)
275
287
  _cluster_path = cluster_path
276
- Chef::Provisioning.inline_resource(action_handler) do
277
- ssh_cluster _cluster_path
288
+ unless ::File.exists?(_cluster_path)
289
+ Chef::Provisioning.inline_resource(action_handler) do
290
+ ssh_cluster _cluster_path
291
+ end
278
292
  end
279
293
  end
280
294
 
281
- def create_ssh_machine(action_handler, machine_spec, machine_options)
295
+ def create_machine(action_handler, machine_spec, machine_options)
296
+ ensure_ssh_cluster(action_handler)
297
+
298
+ machine_options_hash_for_sym = deep_hashify(machine_options)
299
+ symbolized_machine_options = symbolize_keys(machine_options_hash_for_sym)
300
+ validate_machine_options(action_handler, machine_spec, symbolized_machine_options)
301
+ # end
302
+
303
+
304
+ # def create_ssh_machine(action_handler, machine_spec, machine_options)
282
305
  log_info("File is = #{ssh_machine_file(machine_spec)}")
283
306
  log_info("current_machine_options = #{machine_options.to_s}")
307
+
308
+ machine_options_hash_for_s = deep_hashify(machine_options)
309
+ stringy_machine_options = stringify_keys(machine_options_hash_for_s)
310
+ given_machine_options = create_machine_hash(stringy_machine_options)
311
+
312
+ if ssh_machine_exists?(machine_spec)
313
+ existing_machine_hash = existing_ssh_machine(machine_spec)
314
+ if !existing_machine_hash.eql?(given_machine_options)
315
+ create_machine_file(action_handler, machine_spec, given_machine_options)
316
+ else
317
+ return false
318
+ end
319
+ else
320
+ file_updated = create_machine_file(action_handler, machine_spec, given_machine_options)
321
+ file_updated
322
+ end
323
+ end
324
+
325
+ def create_machine_file(action_handler, machine_spec, machine_options)
326
+ file_path = ssh_machine_file(machine_spec)
284
327
  machine_options_hash = deep_hashify(machine_options)
285
328
  stringy_machine_options = stringify_keys(machine_options_hash)
286
- file_path = ssh_machine_file(machine_spec)
287
329
  options_parsed = ::JSON.parse(stringy_machine_options.to_json)
288
330
  json_machine_options = ::JSON.pretty_generate(options_parsed)
289
331
  Chef::Provisioning.inline_resource(action_handler) do
@@ -307,7 +349,16 @@ class Chef
307
349
  def existing_ssh_machine(machine_spec)
308
350
  if ssh_machine_exists?(machine_spec)
309
351
  existing_machine_hash = JSON.parse(File.read(ssh_machine_file(machine_spec)))
310
- symbolize_keys(existing_machine_hash.to_hash)
352
+ existing_machine_hash.to_hash
353
+ else
354
+ return {}
355
+ end
356
+ end
357
+
358
+ def existing_ssh_machine_to_sym(machine_spec)
359
+ if ssh_machine_exists?(machine_spec)
360
+ existing_machine_hash = existing_ssh_machine(machine_spec)
361
+ symbolize_keys(existing_machine_hash)
311
362
  else
312
363
  return false
313
364
  end
@@ -329,6 +380,67 @@ class Chef
329
380
  ssh_machine_file
330
381
  end
331
382
  end
383
+
384
+ def create_machine_hash(machine_options)
385
+ if !machine_options['transport_options']['host']
386
+ machine_options['transport_options']['host'] = machine_options['transport_options']['ip_address'] ||
387
+ machine_options['transport_options']['hostname']
388
+ end
389
+ validate_transport_options_host(machine_options['transport_options']['host'])
390
+ unless machine_options['transport_options']['is_windows']
391
+ machine_options['transport_options']['options'] ||= {}
392
+ unless machine_options['transport_options']['username'] == 'root'
393
+ machine_options['transport_options']['options']['prefix'] = 'sudo '
394
+ end
395
+ end
396
+ ensure_has_keys_or_password(machine_options['transport_options'])
397
+ machine_options.to_hash
398
+ end
399
+
400
+ def ensure_has_keys_or_password(transport_hash)
401
+ if transport_hash['is_windows']
402
+ password = transport_hash['password'] || false
403
+ has_either = (password && password.kind_of?(String))
404
+ else
405
+ if transport_hash['ssh_options']
406
+ ssh_hash = transport_hash['ssh_options']
407
+ keys = ssh_hash['keys'] || false
408
+ password = ssh_hash['password'] || false
409
+ has_either = ((password && password.kind_of?(String)) ||
410
+ (keys && !keys.empty? && keys.kind_of?(Array)))
411
+ else
412
+ has_either = false
413
+ end
414
+ end
415
+ raise 'No Keys OR Password, No Can Do Compadre' unless has_either
416
+ has_either
417
+ end
418
+
419
+ def validate_transport_options_host(target_host)
420
+ rh = Resolv::Hosts.new
421
+ rd = Resolv.new
422
+
423
+ begin
424
+ rh.getaddress(target_host)
425
+ in_hosts_file = true
426
+ rescue
427
+ in_hosts_file = false
428
+ end
429
+
430
+ begin
431
+ rd.getaddress(target_host)
432
+ in_dns = true
433
+ rescue
434
+ in_dns = false
435
+ end
436
+
437
+ valid_ip = ( target_host =~ Resolv::IPv4::Regex ||
438
+ target_host =~ Resolv::IPv6::Regex )
439
+
440
+ raise 'Host is not a Valid IP or Resolvable Hostname' unless ( valid_ip || in_hosts_file || in_dns )
441
+ end
442
+
443
+
332
444
  end
333
445
  end
334
446
  end
@@ -1,7 +1,7 @@
1
1
  class Chef
2
2
  module Provisioning
3
3
  module SshDriver
4
- VERSION = '0.0.7'
4
+ VERSION = '0.0.8.rc.1'
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-provisioning-ssh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8.rc.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zack Zondlo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-07 00:00:00.000000000 Z
11
+ date: 2015-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chef-provisioning
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 1.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 1.1.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -98,9 +98,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
98
  version: '0'
99
99
  required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ">="
101
+ - - ">"
102
102
  - !ruby/object:Gem::Version
103
- version: '0'
103
+ version: 1.3.1
104
104
  requirements: []
105
105
  rubyforge_project:
106
106
  rubygems_version: 2.4.4