Class: Vagrant::Prison

Inherits:
Object
  • Object
show all
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.2"
Vagrantfile =
<<-EOF
dumped_config = <%= @config.inspect %>

Vagrant::Config.run do |config|
  Marshal.load(dumped_config).eval(config)
end
EOF

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#dirObject (readonly)

Returns the value of attribute dir.



17
18
19
# File 'lib/vagrant/prison.rb', line 17

def dir
  @dir
end

Class Method Details

.cleanup(dir, env) ⇒ Object



37
38
39
# File 'lib/vagrant/prison.rb', line 37

def self.cleanup(dir, env)
  Vagrant::Prison.new(dir, false, env).cleanup
end

Instance Method Details

#cleanupObject

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

#configObject

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


85
86
87
88
89
90
91
92
93
94
# File 'lib/vagrant/prison.rb', line 85

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.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/vagrant/prison.rb', line 106

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 = Vagrant::Environment.new(env_opts.merge(:cwd => dir))

  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

#destroyObject

Destroy the environment. This does not delete the directory, please see ‘cleanup` for a one-shot way to orchestrate that.



140
141
142
143
# File 'lib/vagrant/prison.rb', line 140

def destroy
  Dir.chdir(dir)
  Vagrant::Command::Destroy.new(%w[-f], @env).execute
end