Class: Vagrant::Action::VM::CleanMachineFolder

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/action/vm/clean_machine_folder.rb

Overview

Cleans up the VirtualBox machine folder for any ".xml-prev" files which VirtualBox may have left over. This is a bug in VirtualBox. As soon as this is fixed, this middleware can and will be removed.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ CleanMachineFolder

Returns a new instance of CleanMachineFolder.



11
12
13
# File 'lib/vagrant/action/vm/clean_machine_folder.rb', line 11

def initialize(app, env)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
# File 'lib/vagrant/action/vm/clean_machine_folder.rb', line 15

def call(env)
  clean_machine_folder(env[:vm].driver.read_machine_folder)
  @app.call(env)
end

#clean_machine_folder(machine_folder) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vagrant/action/vm/clean_machine_folder.rb', line 20

def clean_machine_folder(machine_folder)
  folder = File.join(machine_folder, "*")

  # Small safeguard against potentially unwanted rm-rf, since the default
  # machine folder will typically always be greater than 10 characters long.
  # For users with it < 10, out of luck?
  return if folder.length < 10

  Dir[folder].each do |f|
    next unless File.directory?(f)

    keep = Dir["#{f}/**/*"].find do |d|
      # Find a file that doesn't have ".xml-prev" as the suffix,
      # which signals that we want to keep this folder
      File.file?(d) && !(File.basename(d) =~ /\.vbox-prev$/)
    end

    FileUtils.rm_rf(f) if !keep
  end
end