Module: Fragrant
- Defined in:
- lib/fragrant.rb,
lib/fragrant/address_manager.rb,
lib/fragrant/vagrantfile_generator.rb
Defined Under Namespace
Classes: AddressManager, Frontend, VagrantfileGenerator
Constant Summary
collapse
- AddressRangeExhausted =
Class.new(StandardError)
Class Method Summary
collapse
Class Method Details
.add_task(task) ⇒ Object
26
27
28
29
|
# File 'lib/fragrant.rb', line 26
def self.add_task(task)
background_worker
tasks.push(task)
end
|
.address_manager ⇒ Object
20
21
22
23
24
|
# File 'lib/fragrant.rb', line 20
def self.address_manager
data_location = File.join(Fragrant.env_dir, "addresses.json")
range = ENV["FRAGRANT_IP_RANGE"] || "172.24.24.128/25"
@address_manager ||= AddressManager.new(data_location, range)
end
|
.background_worker ⇒ Object
71
72
73
74
75
76
|
# File 'lib/fragrant.rb', line 71
def self.background_worker
@background_worker ||= begin
install_exit_hook
create_worker_thread
end
end
|
.background_worker=(val) ⇒ Object
78
79
80
|
# File 'lib/fragrant.rb', line 78
def self.background_worker=(val)
@background_worker = val
end
|
.create_worker_thread ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/fragrant.rb', line 36
def self.create_worker_thread
$stderr.puts "Launching background worker"
thread = Thread.new do
Thread.current.abort_on_exception = true
loop do
break if Thread.current[:shutdown]
break if Fragrant.tasks.empty?
task = Fragrant.tasks.pop
start_time = Time.now
$stderr.puts "Running background task #{task.inspect}"
env = Vagrant::Environment.new({ :cwd => File.join(env_dir, task[:id]) })
env.cli(task[:args])
elapsed = Time.now - start_time
$stderr.puts "Completed background task #{task.inspect} in #{'%.2f' % elapsed} seconds"
end
$stderr.puts "Terminating background worker"
$stderr.flush
Fragrant.background_worker = nil
Thread.current.terminate
end
thread
end
|
.env_dir ⇒ Object
12
13
14
15
16
17
18
|
# File 'lib/fragrant.rb', line 12
def self.env_dir
@env_dir ||= begin
dir = ENV["FRAGRANT_ENV_DIR"] || File.expand_path("~/.fragrant")
FileUtils.mkdir_p(dir)
dir
end
end
|
.install_exit_hook ⇒ Object
Registers an at_exit hook the first time it is called in a process.
61
62
63
64
65
66
67
68
69
|
# File 'lib/fragrant.rb', line 61
def self.install_exit_hook
@exit_hook ||= at_exit do
$stderr.puts "Waiting for any running Vagrant tasks to complete."
if @background_worker
@background_worker[:shutdown] = true
@background_worker.join
end
end
end
|
.tasks ⇒ Object
Tasks are two-element Arrays of a machine id and a set of vagrant args
32
33
34
|
# File 'lib/fragrant.rb', line 32
def self.tasks
@tasks ||= Queue.new
end
|