Class: Vagrant::Prison
- Inherits:
-
Object
- Object
- Vagrant::Prison
- Defined in:
- lib/vagrant/prison/version.rb,
lib/vagrant/prison/config_proxy.rb,
lib/vagrant/prison.rb
Defined Under Namespace
Classes: ConfigProxy
Constant Summary collapse
- VERSION =
"0.0.3"
- Vagrantfile =
<<-EOF dumped_config = <%= @config.inspect %> Vagrant::Config.run do |config| Marshal.load(dumped_config).eval(config) end EOF
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
Class Method Summary collapse
Instance Method Summary collapse
-
#cleanup ⇒ Object
Clean up the sandbox.
-
#config ⇒ Object
Returns the configuration associated with this prison.
-
#configure(arg = nil) ⇒ Object
Configures a Vagrantfile.
-
#construct(env_opts = {}) ⇒ Object
Construct the sandbox.
-
#destroy ⇒ Object
Destroy the environment.
-
#env_opts ⇒ Object
Returns the options that were used to create the Vagrant::Environment if it has already been created.
-
#initialize(dir = nil, cleanup_on_exit = true, env = nil) ⇒ Prison
constructor
Construct a new Vagrant sandbox.
Constructor Details
#initialize(dir = nil, cleanup_on_exit = true, env = nil) ⇒ Prison
Construct a new Vagrant sandbox. Takes two arguments: (the third should be avoided)
-
‘dir` is a directory name; it will be created for you if it does not already exist, and if left as nil it will be created using Dir.mktmpdir.
-
if ‘cleanup_on_exit` is set to true, vagrant will be told to destroy the environment and the sandbox directory will be deleted. This occurs when the program exits, or when this object is garbage collected, which ever comes first.
30 31 32 33 34 35 |
# File 'lib/vagrant/prison.rb', line 30 def initialize(dir=nil, cleanup_on_exit=true, env=nil) @dir = dir ||= Dir.mktmpdir @initial_config = nil @env = env @cleanup_on_exit = cleanup_on_exit end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
17 18 19 |
# File 'lib/vagrant/prison.rb', line 17 def dir @dir end |
Class Method Details
Instance Method Details
#cleanup ⇒ Object
Clean up the sandbox. Vagrant will be asked to destroy the environment and the directory will be deleted.
45 46 47 48 |
# File 'lib/vagrant/prison.rb', line 45 def cleanup destroy FileUtils.rm_r(dir) end |
#config ⇒ Object
Returns the configuration associated with this prison.
53 54 55 |
# File 'lib/vagrant/prison.rb', line 53 def config @initial_config end |
#configure(arg = nil) ⇒ Object
Configures a Vagrantfile.
You can do this two ways: either supply a string of Ruby to configure the object, or a block.
For example:
obj.configure <<-EOF
Vagrant::Config.run do |config|
config.vm.box = "ubuntu"
config.vm.define :test, :primary => true do |test_config|
test_config.vm.network :hostonly, "192.168.33.10"
end
EOF
OR
obj.configure do |config|
config.vm.box = "ubuntu"
config.vm.define :test, :primary => true do |test_config|
test_config.vm.network :hostonly, "192.168.33.10"
end
end
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/vagrant/prison.rb', line 92 def configure(arg=nil) if arg @initial_config = arg elsif block_given? @initial_config ||= ConfigProxy.new yield @initial_config else raise "You must supply a string of configuration or a block." end end |
#construct(env_opts = {}) ⇒ Object
Construct the sandbox. This:
-
creates your directory (if necessary)
-
supplies a pre-built Vagrantfile with your configuration supplied from the ‘configure` call
-
returns a Vagrant::Environment referencing these items
-
if you set ‘cleanup_on_exit` in the constructor, runs `cleanup` on garbage collection of this object or program exit, which ever comes first.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/vagrant/prison.rb', line 113 def construct(env_opts={}) FileUtils.mkdir_p(dir) to_write = if @initial_config.kind_of?(ConfigProxy) @config = Marshal.dump(@initial_config) ERB.new(Vagrant::Prison::Vagrantfile).result(binding) else @initial_config end File.binwrite(File.join(dir, "Vagrantfile"), to_write) @env_opts = env_opts.merge(:cwd => dir) @env = Vagrant::Environment.new(@env_opts) if @cleanup_on_exit # clean up after garbage collection or if the system exits ObjectSpace.define_finalizer(self) do Vagrant::Prison.cleanup(dir, env) end obj = self # look ma, closures at_exit do obj.cleanup end end return @env end |
#destroy ⇒ Object
Destroy the environment. This does not delete the directory, please see ‘cleanup` for a one-shot way to orchestrate that.
148 149 150 151 |
# File 'lib/vagrant/prison.rb', line 148 def destroy Dir.chdir(dir) Vagrant::Command::Destroy.new(%w[-f], @env).execute end |
#env_opts ⇒ Object
Returns the options that were used to create the Vagrant::Environment if it has already been created.
61 62 63 |
# File 'lib/vagrant/prison.rb', line 61 def env_opts @env_opts end |