2014-09-09 17:17:04 +00:00
|
|
|
require "log4r"
|
2015-10-06 18:11:41 +00:00
|
|
|
|
|
|
|
require "digest/md5"
|
2014-09-09 17:17:04 +00:00
|
|
|
|
|
|
|
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)
|
2015-10-16 17:30:04 +00:00
|
|
|
# If we don't have a box, nothing to do
|
|
|
|
if !env[:machine].box
|
|
|
|
return @app.call(env)
|
|
|
|
end
|
|
|
|
|
2015-10-06 18:11:41 +00:00
|
|
|
# Do the import while locked so that nobody else imports
|
|
|
|
# a master at the same time. This is a no-op if we already
|
|
|
|
# have a master that exists.
|
|
|
|
lock_key = Digest::MD5.hexdigest(env[:machine].box.name)
|
|
|
|
env[:machine].env.lock(lock_key, retry: true) do
|
|
|
|
import_master(env)
|
|
|
|
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
|
2015-10-06 18:11:41 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def import_master(env)
|
|
|
|
master_id_file = env[:machine].box.directory.join("master_id")
|
|
|
|
|
|
|
|
# Read the master ID if we have it in the file.
|
2015-10-08 16:33:55 +00:00
|
|
|
env[:clone_id] = master_id_file.read.chomp if master_id_file.file?
|
2015-10-06 18:11:41 +00:00
|
|
|
|
|
|
|
# If we have the ID and the VM exists already, then we
|
|
|
|
# have nothing to do. Success!
|
2015-10-08 16:33:55 +00:00
|
|
|
if env[:clone_id] && env[:machine].provider.driver.vm_exists?(env[:clone_id])
|
2015-10-06 18:11:41 +00:00
|
|
|
@logger.info(
|
|
|
|
"Master VM for '#{env[:machine].box.name}' already exists " +
|
2015-10-08 16:33:55 +00:00
|
|
|
" (id=#{env[:clone_id]}) - skipping import step.")
|
2015-10-06 18:11:41 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
env[:ui].info(I18n.t("vagrant.actions.vm.clone.setup_master"))
|
2015-10-06 20:22:48 +00:00
|
|
|
env[:ui].detail(I18n.t("vagrant.actions.vm.clone.setup_master_detail"))
|
2015-10-06 18:11:41 +00:00
|
|
|
|
|
|
|
# Import the virtual machine
|
2015-10-06 20:22:48 +00:00
|
|
|
import_env = env[:action_runner].run(Import, env.dup.merge(skip_machine: true))
|
2015-10-08 16:33:55 +00:00
|
|
|
env[:clone_id] = import_env[:machine_id]
|
2015-10-06 18:11:41 +00:00
|
|
|
|
|
|
|
@logger.info(
|
|
|
|
"Imported box #{env[:machine].box.name} as master vm " +
|
2015-10-08 16:33:55 +00:00
|
|
|
"with id #{env[:clone_id]}")
|
2015-10-06 18:11:41 +00:00
|
|
|
|
2015-10-08 16:33:55 +00:00
|
|
|
@logger.debug("Writing id of master VM '#{env[:clone_id]}' to #{master_id_file}")
|
2015-10-06 18:11:41 +00:00
|
|
|
master_id_file.open("w+") do |f|
|
2015-10-08 16:33:55 +00:00
|
|
|
f.write(env[:clone_id])
|
2015-10-06 18:11:41 +00:00
|
|
|
end
|
|
|
|
end
|
2014-09-09 17:17:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|