fragrant 0.0.3 → 0.0.4

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.
data/README.md CHANGED
@@ -4,3 +4,5 @@
4
4
  - Quick
5
5
  * Base notes
6
6
  - Dirty
7
+ * Documentation
8
+ - Turn this file into something delightful
@@ -34,27 +34,49 @@ module Fragrant
34
34
  end
35
35
 
36
36
  def self.create_worker_thread
37
+ $stderr.puts "Launching background worker"
37
38
  thread = Thread.new do
38
39
  Thread.current.abort_on_exception = true
39
- until Thread.current[:shutdown] do
40
- unless Fragrant.tasks.empty?
41
- task = Fragrant.tasks.pop
42
- env = Vagrant::Environment.new({ :cwd => File.join(env_dir, task[:id]) })
43
- env.cli(task[:args])
44
- end
45
- end
40
+ loop do
41
+ break if Thread.current[:shutdown]
42
+ break if Fragrant.tasks.empty?
43
+ task = Fragrant.tasks.pop
44
+ start_time = Time.now
45
+ $stderr.puts "Running background task #{task.inspect}"
46
+ env = Vagrant::Environment.new({ :cwd => File.join(env_dir, task[:id]) })
47
+ env.cli(task[:args])
48
+ elapsed = Time.now - start_time
49
+ $stderr.puts "Completed background task #{task.inspect} in #{'%.2f' % elapsed} seconds"
50
+ end
51
+ # Stop this thread when there is no work left to do.
52
+ $stderr.puts "Terminating background worker"
53
+ $stderr.flush
54
+ Fragrant.background_worker = nil
55
+ Thread.current.terminate
46
56
  end
57
+ thread
58
+ end
47
59
 
48
- at_exit do
60
+ # Registers an at_exit hook the first time it is called in a process.
61
+ def self.install_exit_hook
62
+ @exit_hook ||= at_exit do
49
63
  $stderr.puts "Waiting for any running Vagrant tasks to complete."
50
- thread[:shutdown] = true
51
- thread.join
64
+ if @background_worker
65
+ @background_worker[:shutdown] = true
66
+ @background_worker.join
67
+ end
52
68
  end
53
- thread
54
69
  end
55
70
 
56
71
  def self.background_worker
57
- @background_worker ||= create_worker_thread
72
+ @background_worker ||= begin
73
+ install_exit_hook
74
+ create_worker_thread
75
+ end
76
+ end
77
+
78
+ def self.background_worker=(val)
79
+ @background_worker = val
58
80
  end
59
81
 
60
82
  class Frontend < Grape::API
@@ -69,6 +91,7 @@ module Fragrant
69
91
 
70
92
  helpers do
71
93
  def add_task(task)
94
+ $stderr.puts "Adding background task: #{task.inspect}"
72
95
  Fragrant.add_task(task)
73
96
  end
74
97
 
@@ -111,6 +134,12 @@ module Fragrant
111
134
  Fragrant.address_manager.claim_address(env_id)
112
135
  end
113
136
 
137
+ def deallocate_address(env_id)
138
+ if Fragrant.address_manager.address_map.key?(env_id)
139
+ Fragrant.address_manager.release_addresses(env_id)
140
+ end
141
+ end
142
+
114
143
  def v_file(env_id, directory, contents = nil)
115
144
  addresses = [allocate_address(env_id)] unless contents
116
145
  VagrantfileGenerator.new(directory, :box_name => box_name,
@@ -146,6 +175,7 @@ module Fragrant
146
175
  delete '/destroy/:id' do
147
176
  args = [v_action, params[:vm_name], '--force']
148
177
  v_env.cli(args.compact)
178
+ deallocate_address(params[:id])
149
179
  {:id => params[:id]}
150
180
  end
151
181
 
@@ -239,6 +269,7 @@ module Fragrant
239
269
  else
240
270
  error!({ "error" => "Environment contains undestroyed machines!" }, 409)
241
271
  end
272
+ deallocate_address(params[:id])
242
273
  {:id => params[:id]}
243
274
  end
244
275
 
@@ -62,18 +62,14 @@ module Fragrant
62
62
  address
63
63
  end
64
64
 
65
- def release_address(environment_id, ip)
65
+ def release_addresses(environment_id)
66
66
  unless address_map.key?(environment_id)
67
67
  raise "No addresses registered to environment #{environment_id}"
68
68
  end
69
69
 
70
70
  address = address_map[environment_id]
71
- unless address == ip
72
- raise "IP #{ip} is not currently assigned to environment #{environment_id} (#{address} is)"
73
- end
74
-
71
+ allocated_addresses.delete address
75
72
  address_map.delete environment_id
76
- allocated_addresses.delete ip
77
73
  persist
78
74
  end
79
75
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fragrant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-18 00:00:00.000000000 Z
12
+ date: 2012-10-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: vagrant