providers/virtualbox: unify import/clone

This commit is contained in:
Mitchell Hashimoto 2015-10-08 13:07:05 -04:00
parent 4908cd9cd9
commit 5ea24e39d0
3 changed files with 41 additions and 73 deletions

View File

@ -12,7 +12,6 @@ module VagrantPlugins
autoload :CleanMachineFolder, File.expand_path("../action/clean_machine_folder", __FILE__) autoload :CleanMachineFolder, File.expand_path("../action/clean_machine_folder", __FILE__)
autoload :ClearForwardedPorts, File.expand_path("../action/clear_forwarded_ports", __FILE__) autoload :ClearForwardedPorts, File.expand_path("../action/clear_forwarded_ports", __FILE__)
autoload :ClearNetworkInterfaces, File.expand_path("../action/clear_network_interfaces", __FILE__) autoload :ClearNetworkInterfaces, File.expand_path("../action/clear_network_interfaces", __FILE__)
autoload :Clone, File.expand_path("../action/clone", __FILE__)
autoload :Created, File.expand_path("../action/created", __FILE__) autoload :Created, File.expand_path("../action/created", __FILE__)
autoload :Customize, File.expand_path("../action/customize", __FILE__) autoload :Customize, File.expand_path("../action/customize", __FILE__)
autoload :Destroy, File.expand_path("../action/destroy", __FILE__) autoload :Destroy, File.expand_path("../action/destroy", __FILE__)
@ -389,20 +388,14 @@ module VagrantPlugins
if env[:machine].config.vm.clone if env[:machine].config.vm.clone
# We are cloning from another Vagrant environment # We are cloning from another Vagrant environment
b2.use PrepareClone b2.use PrepareClone
b2.use PrepareCloneSnapshot
b2.use Clone
b2.use DiscardState
elsif env[:machine].provider_config.linked_clone elsif env[:machine].provider_config.linked_clone
# We are cloning from the box # We are cloning from the box
b2.use ImportMaster b2.use ImportMaster
b2.use PrepareCloneSnapshot
b2.use Clone
b2.use DiscardState
else
# We are just doing a normal import from a box
b2.use Import
end end
b2.use PrepareCloneSnapshot
b2.use Import
b2.use DiscardState
b2.use MatchMACAddress b2.use MatchMACAddress
end end
end end

View File

@ -1,63 +0,0 @@
require "log4r"
require "fileutils"
module VagrantPlugins
module ProviderVirtualBox
module Action
class Clone
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant::action::vm::clone")
end
def call(env)
# Do the actual clone
env[:ui].info I18n.t("vagrant.actions.vm.clone.creating")
env[:machine].id = env[:machine].provider.driver.clonevm(
env[:clone_id], env[:clone_snapshot]) 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
# Flag as erroneous and return if clone failed
raise Vagrant::Errors::VMCloneFailure if !env[:machine].id
# Copy the SSH key from the clone machine if we can
if env[:clone_machine]
key_path = env[:clone_machine].data_dir.join("private_key")
if key_path.file?
FileUtils.cp(
key_path,
env[:machine].data_dir.join("private_key"))
end
end
# Continue
@app.call(env)
end
def recover(env)
if env[:machine].state.id != :not_created
return if env["vagrant.error"].is_a?(Vagrant::Errors::VagrantError)
# If we're not supposed to destroy on error then just return
return if !env[:destroy_on_error]
# Interrupted, destroy the VM. We note that we don't want to
# validate the configuration here, and we don't want to confirm
# we want to destroy.
destroy_env = env.clone
destroy_env[:config_validate] = false
destroy_env[:force_confirm_destroy] = true
env[:action_runner].run(Action.action_destroy, destroy_env)
end
end
end
end
end
end

View File

@ -7,6 +7,44 @@ module VagrantPlugins
end end
def call(env) def call(env)
if env[:clone_id]
clone(env)
else
import(env)
end
end
def clone(env)
# Do the actual clone
env[:ui].info I18n.t("vagrant.actions.vm.clone.creating")
env[:machine].id = env[:machine].provider.driver.clonevm(
env[:clone_id], env[:clone_snapshot]) 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
# Flag as erroneous and return if clone failed
raise Vagrant::Errors::VMCloneFailure if !env[:machine].id
# Copy the SSH key from the clone machine if we can
if env[:clone_machine]
key_path = env[:clone_machine].data_dir.join("private_key")
if key_path.file?
FileUtils.cp(
key_path,
env[:machine].data_dir.join("private_key"))
end
end
# Continue
@app.call(env)
end
def import(env)
env[:ui].info I18n.t("vagrant.actions.vm.import.importing", env[:ui].info I18n.t("vagrant.actions.vm.import.importing",
name: env[:machine].box.name) name: env[:machine].box.name)