Skip to content

Instantly share code, notes, and snippets.

@jeroenr
Created May 1, 2012 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenr/2568420 to your computer and use it in GitHub Desktop.
Save jeroenr/2568420 to your computer and use it in GitHub Desktop.
Replace box name with uuid before passing ovf to VirtualBox to enable parallellization
require 'uuid'
module Vagrant
module Action
module VM
class Import
def initialize(app, env)
@app = app
end
def call(env)
env[:ui].info I18n.t("vagrant.actions.vm.import.importing", :name => env[:vm].box.name)
tmp_box_id = UUID.new.generate
# Import the virtual machine
ovf_file = env[:vm].box.directory.join("box.ovf").to_s
ovf_file_contents = File.read(ovf_file)
ovf_file_contents.gsub!(/vagrant-box-squeeze/, tmp_box_id)
ovf_file = ovf_file << "." << tmp_box_id << ".ovf"
new_ovf_file = File.new(ovf_file, "w")
new_ovf_file.write(ovf_file_contents)
new_ovf_file.close
env[:vm].uuid = env[:vm].driver.import(ovf_file) do |progress|
env[:ui].clear_line
env[:ui].report_progress(progress, 100, false)
end
# Clear the line one last time since the progress meter doesn't disappear
# immediately.
env[:ui].clear_line
# If we got interrupted, then the import could have been
# interrupted and its not a big deal. Just return out.
return if env[:interrupted]
# Flag as erroneous and return if import failed
raise Errors::VMImportFailure if !env[:vm].uuid
# Import completed successfully. Continue the chain
@app.call(env)
end
def recover(env)
if env[:vm].created?
return if env["vagrant.error"].is_a?(Errors::VagrantError)
# Interrupted, destroy the VM. We note that we don't want to
# validate the configuration here.
destroy_env = env.clone
destroy_env[:validate] = false
env[:action_runner].run(:destroy, destroy_env)
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment