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.4"
- Vagrantfile =
<<-EOF require 'vagrant/prison' 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.
-
#configure_environment(env_opts = {}) ⇒ Object
Configures the environment.
-
#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.
31 32 33 34 35 36 |
# File 'lib/vagrant/prison.rb', line 31 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.
18 19 20 |
# File 'lib/vagrant/prison.rb', line 18 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.
46 47 48 49 |
# File 'lib/vagrant/prison.rb', line 46 def cleanup destroy FileUtils.rm_r(dir) end |
#config ⇒ Object
Returns the configuration associated with this prison.
54 55 56 |
# File 'lib/vagrant/prison.rb', line 54 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
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/vagrant/prison.rb', line 93 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 |
#configure_environment(env_opts = {}) ⇒ Object
Configures the environment. Useful if you wish to open a prison without re-creating it.
108 109 110 111 |
# File 'lib/vagrant/prison.rb', line 108 def configure_environment(env_opts={}) @env_opts ||= env_opts.merge(:cwd => dir) @env ||= Vagrant::Environment.new(@env_opts) 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.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/vagrant/prison.rb', line 123 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 configure_environment(env_opts) File.binwrite(File.join(dir, "Vagrantfile"), to_write) 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.
157 158 159 160 |
# File 'lib/vagrant/prison.rb', line 157 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.
62 63 64 |
# File 'lib/vagrant/prison.rb', line 62 def env_opts @env_opts end |