From 5ea24e39d021875c2d6cc3830236364f42f78f9f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 8 Oct 2015 13:07:05 -0400 Subject: [PATCH] providers/virtualbox: unify import/clone --- plugins/providers/virtualbox/action.rb | 13 +--- plugins/providers/virtualbox/action/clone.rb | 63 ------------------- plugins/providers/virtualbox/action/import.rb | 38 +++++++++++ 3 files changed, 41 insertions(+), 73 deletions(-) delete mode 100644 plugins/providers/virtualbox/action/clone.rb diff --git a/plugins/providers/virtualbox/action.rb b/plugins/providers/virtualbox/action.rb index f7984dca4..d0ef01bff 100644 --- a/plugins/providers/virtualbox/action.rb +++ b/plugins/providers/virtualbox/action.rb @@ -12,7 +12,6 @@ module VagrantPlugins autoload :CleanMachineFolder, File.expand_path("../action/clean_machine_folder", __FILE__) autoload :ClearForwardedPorts, File.expand_path("../action/clear_forwarded_ports", __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 :Customize, File.expand_path("../action/customize", __FILE__) autoload :Destroy, File.expand_path("../action/destroy", __FILE__) @@ -389,20 +388,14 @@ module VagrantPlugins if env[:machine].config.vm.clone # We are cloning from another Vagrant environment b2.use PrepareClone - b2.use PrepareCloneSnapshot - b2.use Clone - b2.use DiscardState elsif env[:machine].provider_config.linked_clone # We are cloning from the box 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 + b2.use PrepareCloneSnapshot + b2.use Import + b2.use DiscardState b2.use MatchMACAddress end end diff --git a/plugins/providers/virtualbox/action/clone.rb b/plugins/providers/virtualbox/action/clone.rb deleted file mode 100644 index 6b389d4f1..000000000 --- a/plugins/providers/virtualbox/action/clone.rb +++ /dev/null @@ -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 diff --git a/plugins/providers/virtualbox/action/import.rb b/plugins/providers/virtualbox/action/import.rb index 92e004f01..fb540f50a 100644 --- a/plugins/providers/virtualbox/action/import.rb +++ b/plugins/providers/virtualbox/action/import.rb @@ -7,6 +7,44 @@ module VagrantPlugins end 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", name: env[:machine].box.name)