knife-stackbuilder 0.5.8 → 0.5.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aedb4132378ee68d7c8e56a485173de58d61a428
4
- data.tar.gz: 9fa027941863f5f08a5bdc41944256fbf728a348
3
+ metadata.gz: 7ab807fc026606708f650e207c681ea3ffb82486
4
+ data.tar.gz: 71a6b26ab5974d2e1c117f04da246f150dcc4375
5
5
  SHA512:
6
- metadata.gz: db532af7b3a449f18f47724bc7c098440cfd0f0aeda34eb6434701db013603295e3445168f9124cea1b49fc6ec21ae821275275d62b19f40f366101368d5c879
7
- data.tar.gz: 72b4d0c1b30a929468d089d8530cd1063904eee27e4bb58b300a64d76fa1bbaa2787305229a79664a9bbfa624fb466e3643eacce75e5f6c403fabc0d7dd23f7c
6
+ metadata.gz: 53bac33ce0fe0a52524d8dbe36d18aadd34cdecd0a0bbe66cd2bf2ff393f2741cf08f745cde95c34c962e8a6db1e56ebae016a182ae199157bb94d2014985577
7
+ data.tar.gz: 52a0c878e71ee0f512fa3eb8b6213aa03a24a7772e9e2bc2c99dfc0324168998e2f96b6f6878de2c6040ae197ed22ce84bcc58414269d5a24c08c2b343e00507
@@ -200,30 +200,26 @@ module StackBuilder::Chef
200
200
  unless ENV['DOCKER_TLS_VERIFY']
201
201
  end
202
202
 
203
- echo_output = @logger.info? || @logger.debug?
204
203
  build_exists = @name==`docker images | awk '$1=="#{@name}" { print $1 }'`.strip
205
204
 
206
205
  knife_cmd = Chef::Knife::ContainerDockerInit.new
207
206
 
208
207
  # Run as a forked job (This captures all output and removes noise from output)
209
- run_jobs(knife_cmd, true, echo_output) do |k|
208
+ knife_cmd.name_args = [ @name ]
210
209
 
211
- k.name_args = [ @name ]
210
+ knife_cmd.config[:local_mode] = false
211
+ knife_cmd.config[:base_image] = build_exists ? @name : @knife_config['image']
212
+ knife_cmd.config[:force] = true
213
+ knife_cmd.config[:generate_berksfile] = false
214
+ knife_cmd.config[:include_credentials] = true
212
215
 
213
- k.config[:local_mode] = false
214
- k.config[:base_image] = build_exists ? @name : @knife_config['image']
215
- k.config[:force] = true
216
- k.config[:generate_berksfile] = false
217
- k.config[:include_credentials] = true
216
+ knife_cmd.config[:dockerfiles_path] = @dockerfiles_build_dir
217
+ knife_cmd.config[:run_list] = @knife_config['run_list']
218
218
 
219
- k.config[:dockerfiles_path] = @dockerfiles_build_dir
220
- k.config[:run_list] = @knife_config['run_list']
219
+ knife_cmd.config[:encrypted_data_bag_secret] = IO.read(@env_key_file) \
220
+ unless File.exist? (@env_key_file)
221
221
 
222
- k.config[:encrypted_data_bag_secret] = IO.read(@env_key_file) \
223
- unless File.exist? (@env_key_file)
224
-
225
- run_knife(k)
226
- end
222
+ run_knife_forked(knife_cmd)
227
223
 
228
224
  dockerfiles_named_path = @dockerfiles_build_dir + '/' + @name
229
225
 
@@ -282,21 +278,16 @@ module StackBuilder::Chef
282
278
  # Run the build as a forked job (This captures all output and removes noise from output)
283
279
  knife_cmd = Chef::Knife::ContainerDockerBuild.new
284
280
 
285
- job_results = run_jobs(knife_cmd, true, echo_output) do |k|
286
-
287
- k.name_args = [ @name ]
281
+ knife_cmd.name_args = [ @name ]
288
282
 
289
- k.config[:run_berks] = false
290
- k.config[:force_build] = true
291
- k.config[:dockerfiles_path] = @dockerfiles_build_dir
292
- k.config[:cleanup] = true
293
-
294
- run_knife(k)
295
- end
283
+ knife_cmd.config[:run_berks] = false
284
+ knife_cmd.config[:force_build] = true
285
+ knife_cmd.config[:dockerfiles_path] = @dockerfiles_build_dir
286
+ knife_cmd.config[:cleanup] = true
296
287
 
297
- result = job_results[knife_cmd.object_id][0]
298
- if result.rindex('Chef run process exited unsuccessfully (exit code 1)') ||
299
- result.rindex(/The command \[.*\] returned a non-zero code:/)
288
+ begin
289
+ results = run_knife_forked(knife_cmd)
290
+ rescue Exception => msg
300
291
 
301
292
  if @logger.level>=::Logger::WARN
302
293
 
@@ -306,10 +297,10 @@ module StackBuilder::Chef
306
297
  %x(docker rmi -f #{@name})
307
298
 
308
299
  puts "Knife container build Chef convergence failed with an error."
309
- puts "#{job_results.first[0]}"
300
+ puts "#{results[0]}"
310
301
  end
311
302
 
312
- raise StackBuilder::Common::StackBuilderError, "Docker build of container #{@name} has errors."
303
+ raise msg
313
304
  end
314
305
 
315
306
  puts 'Saving docker image for upload. This may take a few minutes.'
@@ -6,7 +6,7 @@ module StackBuilder::Chef
6
6
 
7
7
  class GenericNodeManager < StackBuilder::Chef::NodeManager
8
8
 
9
- def create_vm(name, knife_config)
9
+ def create_vm(index, name, knife_config)
10
10
 
11
11
  create_class_name = knife_config['create']['class']
12
12
  raise ArgumentError, "Knife plugin's server 'create' class name not provided." \
@@ -21,16 +21,27 @@ module StackBuilder::Chef
21
21
  knife_cmd.name_args = [ name ]
22
22
  end
23
23
 
24
+ if knife_config['create'].has_key?('pool_key')
25
+
26
+ pool_key = knife_config['create']['pool_key']
27
+
28
+ placement_pools = knife_config['placement_pools']
29
+ raise ArgumentError, "Knife plugin 'placement_pools' list was not provided." \
30
+ if placement_pools.nil?
31
+
32
+ knife_cmd.config[pool_key.to_sym] = placement_pools[index % placement_pools.size]
33
+ end
34
+
24
35
  config_knife(knife_cmd, knife_config['create']['options'] || { })
25
36
  config_knife(knife_cmd, knife_config['options'] || { })
26
37
 
27
38
  if knife_config['create']['synchronized']
28
39
  @@sync ||= Mutex.new
29
40
  @@sync.synchronize {
30
- run_knife(knife_cmd, knife_config['create']['retries'] || 0)
41
+ run_knife_forked(knife_cmd)
31
42
  }
32
43
  else
33
- run_knife(knife_cmd, knife_config['create']['retries'] || 0)
44
+ run_knife_forked(knife_cmd)
34
45
  end
35
46
  end
36
47
 
@@ -59,10 +70,10 @@ module StackBuilder::Chef
59
70
  if knife_config['delete']['synchronized']
60
71
  @@sync ||= Mutex.new
61
72
  @@sync.synchronize {
62
- run_knife(knife_cmd, knife_config['delete']['retries'] || 0)
73
+ run_knife_forked(knife_cmd)
63
74
  }
64
75
  else
65
- run_knife(knife_cmd, knife_config['delete']['retries'] || 0)
76
+ run_knife_forked(knife_cmd)
66
77
  end
67
78
  end
68
79
  end
@@ -67,13 +67,22 @@ module StackBuilder::Chef
67
67
  def create(index)
68
68
 
69
69
  name = "#{@node_id}-#{index}"
70
- self.create_vm(name, @knife_config)
70
+ self.create_vm(index, name, @knife_config)
71
71
 
72
72
  node = Chef::Node.load(name)
73
73
  node.normal['stack_id'] = @id
74
74
  node.normal['stack_node'] = @name
75
75
  node.save
76
76
 
77
+ begin
78
+ # Wait for node to become available
79
+ node_search("name:#{name}", StackBuilder::Common::Config.timeouts[:QUERY_TIMEOUT])
80
+
81
+ rescue Exception => msg
82
+ raise StackBuilder::Common::StackBuilderError, \
83
+ "Error waiting for node named '#{name} to be indexed in Chef Server: #{msg}"
84
+ end
85
+
77
86
  unless @env_key_file.nil?
78
87
  env_key = IO.read(@env_key_file)
79
88
  knife_ssh( name,
@@ -88,7 +97,7 @@ module StackBuilder::Chef
88
97
  raise msg
89
98
  end
90
99
 
91
- def create_vm(name, knife_config)
100
+ def create_vm(index, name, knife_config)
92
101
  raise StackBuilder::Common::NotImplemented, 'HostNodeManager.create_vm'
93
102
  end
94
103
 
@@ -135,7 +144,7 @@ module StackBuilder::Chef
135
144
  "exit $result" )
136
145
  else
137
146
  node = Chef::Node.load(name)
138
- attributes.each { |k,v| node.override[k] = v }
147
+ attributes.each { |k,v| node.normal[k] = v }
139
148
  node.save
140
149
  end
141
150
 
@@ -199,16 +208,9 @@ module StackBuilder::Chef
199
208
 
200
209
  def get_stack_node_resources
201
210
 
202
- query = Chef::Search::Query.new
203
-
204
- escaped_query = URI.escape(
205
- "stack_id:#{@id} AND stack_node:#{@name}",
206
- Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") )
207
-
208
- results = query.search('node', escaped_query, nil, 0, 999999)
211
+ results = node_search("stack_id:#{@id} AND stack_node:#{@name}")
209
212
  @nodes = results[0]
210
-
211
- results[2]
213
+ @nodes.size
212
214
  end
213
215
 
214
216
  def knife_ssh(name, cmd)
@@ -235,11 +237,32 @@ module StackBuilder::Chef
235
237
  error = StackBuilder::Common::TeeIO.new($stderr)
236
238
 
237
239
  @logger.info("Running '#{cmd}' on node 'name:#{name}'.")
238
- run_knife(knife_cmd, 0, output, error)
240
+ run_knife(knife_cmd, output, error)
239
241
  else
240
242
  run_knife(knife_cmd)
241
243
  end
242
244
  end
243
245
 
246
+ def node_search(search_query, timeout = 0)
247
+
248
+ query = Chef::Search::Query.new
249
+ escaped_query = URI.escape(search_query, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
250
+
251
+ if timeout>0
252
+ @logger.info("Waiting '#{search_query}' to return results.")
253
+ results = Timeout::timeout(timeout) {
254
+
255
+ while true do
256
+ results = query.search('node', escaped_query, nil, 0, 999999)
257
+ return results if results[0].size>0
258
+ end
259
+ }
260
+ else
261
+ results = query.search('node', escaped_query, nil, 0, 999999)
262
+ end
263
+
264
+ results
265
+ end
266
+
244
267
  end
245
268
  end
@@ -37,7 +37,7 @@ module StackBuilder::Chef
37
37
  @id = id
38
38
 
39
39
  stack_environment = stack['environment']
40
- raise ArgmentError, "Stack file is fixed to the environment '#{stack_environment}', " +
40
+ raise ArgumentError, "Stack file is fixed to the environment '#{stack_environment}', " +
41
41
  " which it does not match the environment '#{@environment}' provided." \
42
42
  unless stack_environment.nil? || stack_environment==@environment
43
43
 
@@ -6,7 +6,7 @@ module StackBuilder::Chef
6
6
 
7
7
  class VagrantNodeManager < StackBuilder::Chef::NodeManager
8
8
 
9
- def create_vm(name, knife_config)
9
+ def create_vm(index, name, knife_config)
10
10
 
11
11
  handle_vagrant_box_additions(name, knife_config)
12
12
 
@@ -39,12 +39,15 @@ module StackBuilder::Chef
39
39
 
40
40
  @@sync ||= Mutex.new
41
41
  @@sync.synchronize {
42
- run_knife(knife_cmd)
42
+ run_knife_forked(knife_cmd)
43
43
  }
44
44
  end
45
45
 
46
46
  def delete_vm(name, knife_config)
47
47
 
48
+ puts "deleting #{name}"
49
+ exit 1
50
+
48
51
  knife_cmd = Chef::Knife::VagrantServerDelete.new
49
52
  knife_cmd.name_args = [ name ]
50
53
  knife_cmd.config[:yes] = true
@@ -52,7 +55,7 @@ module StackBuilder::Chef
52
55
 
53
56
  @@sync ||= Mutex.new
54
57
  @@sync.synchronize {
55
- run_knife(knife_cmd, 3)
58
+ run_knife_forked(knife_cmd)
56
59
  }
57
60
 
58
61
  handle_vagrant_box_cleanup(knife_config)
@@ -4,6 +4,7 @@ module StackBuilder::Common
4
4
 
5
5
  class Config
6
6
 
7
+ QUERY_TIMEOUT = 60
7
8
  CACHE_TIMEOUT = 60
8
9
 
9
10
  class << self
@@ -18,6 +19,7 @@ module StackBuilder::Common
18
19
  # Determine timeouts
19
20
  config.timeouts = { } if config.timeouts.nil?
20
21
  config.timeouts[:CACHE_TIMEOUT] = CACHE_TIMEOUT unless config.timeouts.has_key?(:CACHE_TIMEOUT)
22
+ config.timeouts[:QUERY_TIMEOUT] = QUERY_TIMEOUT unless config.timeouts.has_key?(:QUERY_TIMEOUT)
21
23
 
22
24
  # Create cache folder
23
25
  config.enable_caching = false if config.enable_caching.nil?
@@ -47,6 +47,8 @@ module StackBuilder::Common
47
47
  previous_stderr, $stderr = $stderr, stderr
48
48
  yield(job)
49
49
  Marshal.dump([stdout.string, stderr.string], write)
50
+ rescue Exception => msg
51
+ Marshal.dump([stdout.string, stderr.string, msg], write)
50
52
  ensure
51
53
  $stdout = previous_stdout
52
54
  $stderr = previous_stderr
@@ -369,42 +371,37 @@ module StackBuilder::Common
369
371
  end
370
372
 
371
373
  #
372
- # Helper command to run Chef knife
374
+ # Helper commands to run Chef knife
373
375
  #
374
- def run_knife(knife_cmd, retries = 0, output = StringIO.new, error = StringIO.new)
376
+ def run_knife(knife_cmd, output = StringIO.new, error = StringIO.new)
375
377
 
376
378
  knife_cmd.ui = Chef::Knife::UI.new(output, error, STDIN, knife_cmd.config) \
377
379
  unless output.nil? && error.nil?
378
380
 
379
- run = true
380
- while run
381
-
382
- begin
383
- knife_cmd.run
384
- run = false
381
+ knife_cmd.run
382
+ output.string
383
+ end
385
384
 
386
- rescue Exception => msg
385
+ #
386
+ # Helper to run one or more Chef knife commands as a forked jobs
387
+ #
388
+ def run_knife_forked(*knife_cmd)
387
389
 
388
- if retries==0
390
+ logger = StackBuilder::Common::Config.logger
389
391
 
390
- if @logger.level>=::Logger::WARN
391
- puts "Knife execution failed with an error."
392
- puts "* StdOut from knife run: #{output.string}"
393
- puts "* StdErr from knife run: #{error.string}"
394
- end
392
+ job_results = run_jobs(knife_cmd, true, logger.info? || logger.debug?) do |k|
393
+ run_knife(k)
394
+ end
395
395
 
396
- @logger.debug(msg.backtrace.join("\n\t")) if Config.logger.debug?
397
- raise msg
398
- end
396
+ results = knife_cmd.collect do |k|
399
397
 
400
- @logger.debug("Knife command #{knife_cmd} failed. Retrying after 2s.")
398
+ result = job_results[k.object_id]
399
+ raise result[2] unless result[2].nil?
401
400
 
402
- sleep 2
403
- retries -= 1
404
- end
401
+ result[0]
405
402
  end
406
403
 
407
- output.string
404
+ (results.size==1 ? results[0] : results)
408
405
  end
409
406
 
410
407
  #
@@ -211,29 +211,17 @@ module StackBuilder::Stack
211
211
 
212
212
  threads = [ ]
213
213
 
214
- scale = (@deleted ? @manager.get_scale : @scale)
215
214
  if @targets.empty?
216
215
 
217
- if scale > 0
218
-
219
- if @sync == "first"
220
- @manager.process(scale, events, self.parse_attributes(@attributes, 0))
221
- scale -= 1
222
- end
223
-
224
- if @sync == "all"
225
- scale.times do |i|
226
- @manager.process(i, events, self.parse_attributes(@attributes, i))
227
- end
228
- else
229
- scale.times do |i|
230
- spawn_processing(i, events, threads)
231
- end
232
- end
216
+ scale = (@deleted ? @manager.get_scale : @scale)
217
+ scale.times do |i|
218
+ spawn_processing(i, events, threads)
233
219
  end
234
220
  else
235
221
  @targets.each do |t|
236
- t.scale.times do |i|
222
+
223
+ scale = (@deleted ? t.manager.get_scale : t.scale)
224
+ scale.times do |i|
237
225
  spawn_processing(i, events, threads, t)
238
226
  end
239
227
  end
@@ -284,12 +272,13 @@ module StackBuilder::Stack
284
272
  "#{orchestrate_events.collect { |e| e } .join(", ")}") if @logger.debug?
285
273
 
286
274
  if @sync==SYNC_ALL || (i==0 && @sync==SYNC_FIRST)
275
+ @resource_sync[i].wait if target.nil?
287
276
  @manager.process(i, orchestrate_events, parse_attributes(@attributes, i), target_manager)
288
277
  else
289
- @resource_sync[i].wait if target.nil?
290
278
  threads << Thread.new {
291
279
 
292
280
  begin
281
+ @resource_sync[i].wait if target.nil?
293
282
  @manager.process(i, orchestrate_events, parse_attributes(@attributes, i), target_manager)
294
283
 
295
284
  rescue Exception => msg
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Knife
4
4
  module StackBuilder
5
- VERSION = "0.5.8"
5
+ VERSION = "0.5.9"
6
6
  MAJOR, MINOR, TINY = VERSION.split('.')
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-stackbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.8
4
+ version: 0.5.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mevan Samaratunga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-06 00:00:00.000000000 Z
11
+ date: 2015-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chef