2014-09-09 17:17:04 +00:00
|
|
|
require "log4r"
|
|
|
|
#require "lockfile"
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module ProviderVirtualBox
|
|
|
|
module Action
|
|
|
|
class ImportMaster
|
|
|
|
def initialize(app, env)
|
|
|
|
@app = app
|
|
|
|
@logger = Log4r::Logger.new("vagrant::action::vm::create_master")
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
master_id_file = env[:machine].box.directory.join("master_id")
|
|
|
|
|
2015-06-03 11:31:43 +00:00
|
|
|
env[:machine].env.lock(env[:machine].box.name, retry: true) do
|
|
|
|
env[:master_id] = master_id_file.read.chomp if master_id_file.file?
|
|
|
|
if env[:master_id] && env[:machine].provider.driver.vm_exists?(env[:master_id])
|
|
|
|
# Master VM already exists -> nothing to do - continue.
|
|
|
|
@logger.info("Master VM for '#{env[:machine].box.name}' already exists (id=#{env[:master_id]}) - skipping import step.")
|
|
|
|
return @app.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
env[:ui].info I18n.t("vagrant.actions.vm.clone.importing", name: env[:machine].box.name)
|
|
|
|
|
|
|
|
# Import the virtual machine
|
|
|
|
ovf_file = env[:machine].box.directory.join("box.ovf").to_s
|
|
|
|
env[:master_id] = env[:machine].provider.driver.import(ovf_file) do |progress|
|
|
|
|
env[:ui].clear_line
|
|
|
|
env[:ui].report_progress(progress, 100, false)
|
|
|
|
end
|
2014-09-09 17:17:04 +00:00
|
|
|
|
2015-06-03 11:31:43 +00:00
|
|
|
# Clear the line one last time since the progress meter doesn't disappear immediately.
|
2014-09-09 17:17:04 +00:00
|
|
|
env[:ui].clear_line
|
2015-06-03 11:31:43 +00:00
|
|
|
|
|
|
|
# Flag as erroneous and return if import failed
|
|
|
|
raise Vagrant::Errors::VMImportFailure if !env[:master_id]
|
2014-09-09 17:17:04 +00:00
|
|
|
|
2015-06-03 11:31:43 +00:00
|
|
|
@logger.info("Imported box #{env[:machine].box.name} as master vm with id #{env[:master_id]}")
|
2014-09-09 17:17:04 +00:00
|
|
|
|
2015-06-03 11:31:43 +00:00
|
|
|
@logger.info("Creating base snapshot for master VM.")
|
|
|
|
env[:machine].provider.driver.create_snapshot(env[:master_id], "base") do |progress|
|
|
|
|
env[:ui].clear_line
|
|
|
|
env[:ui].report_progress(progress, 100, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
@logger.debug("Writing id of master VM '#{env[:master_id]}' to #{master_id_file}")
|
|
|
|
master_id_file.open("w+") do |f|
|
|
|
|
f.write(env[:master_id])
|
|
|
|
end
|
|
|
|
end
|
2014-09-09 17:17:04 +00:00
|
|
|
|
|
|
|
# If we got interrupted, then the import could have been
|
|
|
|
# interrupted and its not a big deal. Just return out.
|
2015-06-03 11:31:43 +00:00
|
|
|
if env[:interrupted]
|
|
|
|
@logger.info("Import of master VM was interrupted -> exiting.")
|
|
|
|
return
|
|
|
|
end
|
2014-09-09 17:17:04 +00:00
|
|
|
|
|
|
|
# Import completed successfully. Continue the chain
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|