From f0ddac8c9a2a6c89b44fbc8cdd931a5fad3ac89c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 8 Oct 2015 12:33:55 -0400 Subject: [PATCH] providers/virtualbox: clone --- lib/vagrant/environment.rb | 22 +++++++ lib/vagrant/errors.rb | 12 +++- plugins/providers/virtualbox/action.rb | 3 + .../virtualbox/action/create_clone.rb | 12 +--- .../virtualbox/action/import_master.rb | 26 ++------ .../virtualbox/action/prepare_clone.rb | 11 ++++ .../action/prepare_clone_snapshot.rb | 65 +++++++++++++++++++ templates/locales/en.yml | 11 ++++ 8 files changed, 130 insertions(+), 32 deletions(-) create mode 100644 plugins/providers/virtualbox/action/prepare_clone_snapshot.rb diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index c3dabd676..0981d9df6 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -9,6 +9,7 @@ require 'log4r' require 'vagrant/util/file_mode' require 'vagrant/util/platform' +require "vagrant/util/silence_warnings" require "vagrant/vagrantfile" require "vagrant/version" @@ -413,6 +414,27 @@ module Vagrant @config_loader end + # Loads another environment for the given Vagrantfile, sharing as much + # useful state from this Environment as possible (such as UI and paths). + # Any initialization options can be overidden using the opts hash. + # + # @param [String] vagrantfile Path to a Vagrantfile + # @return [Environment] + def environment(vagrantfile, **opts) + path = File.expand_path(vagrantfile, root_path) + file = File.basename(path) + path = File.dirname(path) + + Util::SilenceWarnings.silence! do + Environment.new({ + cwd: path, + home_path: home_path, + ui_class: ui_class, + vagrantfile_name: file, + }.merge(opts)) + end + end + # This defines a hook point where plugin action hooks that are registered # against the given name will be run in the context of this environment. # diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 2ce133618..906b8d9bc 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -288,6 +288,14 @@ module Vagrant error_key(:cli_invalid_options) end + class CloneNotFound < VagrantError + error_key(:clone_not_found) + end + + class CloneMachineNotFound < VagrantError + error_key(:clone_machine_not_found) + end + class CommandUnavailable < VagrantError error_key(:command_unavailable) end @@ -787,11 +795,11 @@ module Vagrant class VMCloneFailure < VagrantError error_key(:failure, "vagrant.actions.vm.clone") end - + class VMCreateMasterFailure < VagrantError error_key(:failure, "vagrant.actions.vm.clone.create_master") end - + class VMCustomizationFailed < VagrantError error_key(:failure, "vagrant.actions.vm.customize") end diff --git a/plugins/providers/virtualbox/action.rb b/plugins/providers/virtualbox/action.rb index 186f8debd..9a67f0b37 100644 --- a/plugins/providers/virtualbox/action.rb +++ b/plugins/providers/virtualbox/action.rb @@ -36,6 +36,7 @@ module VagrantPlugins autoload :Package, File.expand_path("../action/package", __FILE__) autoload :PackageVagrantfile, File.expand_path("../action/package_vagrantfile", __FILE__) autoload :PrepareClone, File.expand_path("../action/prepare_clone", __FILE__) + autoload :PrepareCloneSnapshot, File.expand_path("../action/prepare_clone_snapshot", __FILE__) autoload :PrepareNFSSettings, File.expand_path("../action/prepare_nfs_settings", __FILE__) autoload :PrepareNFSValidIds, File.expand_path("../action/prepare_nfs_valid_ids", __FILE__) autoload :PrepareForwardedPortCollisionParams, File.expand_path("../action/prepare_forwarded_port_collision_params", __FILE__) @@ -388,10 +389,12 @@ module VagrantPlugins if env[:machine].config.vm.clone # We are cloning from another Vagrant environment b2.use PrepareClone + b2.use PrepareCloneSnapshot b2.use CreateClone elsif env[:machine].provider_config.linked_clone # We are cloning from the box b2.use ImportMaster + b2.use PrepareCloneSnapshot b2.use CreateClone else # We are just doing a normal import from a box diff --git a/plugins/providers/virtualbox/action/create_clone.rb b/plugins/providers/virtualbox/action/create_clone.rb index ac3ca519e..526b5a5c1 100644 --- a/plugins/providers/virtualbox/action/create_clone.rb +++ b/plugins/providers/virtualbox/action/create_clone.rb @@ -10,20 +10,10 @@ module VagrantPlugins end def call(env) - # Get the snapshot to base the linked clone on. This defaults - # to "base" which is automatically setup with linked clones. - snapshot = nil - if env[:machine].provider_config.linked_clone - snapshot = "base" - if env[:machine].provider_config.linked_clone_snapshot - snapshot = env[:machine].provider_config.linked_clone_snapshot - end - end - # Do the actual clone env[:ui].info I18n.t("vagrant.actions.vm.clone.creating") env[:machine].id = env[:machine].provider.driver.clonevm( - env[:master_id], snapshot) do |progress| + env[:clone_id], env[:clone_snapshot]) do |progress| env[:ui].clear_line env[:ui].report_progress(progress, 100, false) end diff --git a/plugins/providers/virtualbox/action/import_master.rb b/plugins/providers/virtualbox/action/import_master.rb index 985a346ba..17296bbcc 100644 --- a/plugins/providers/virtualbox/action/import_master.rb +++ b/plugins/providers/virtualbox/action/import_master.rb @@ -37,14 +37,14 @@ module VagrantPlugins master_id_file = env[:machine].box.directory.join("master_id") # Read the master ID if we have it in the file. - env[:master_id] = master_id_file.read.chomp if master_id_file.file? + env[:clone_id] = master_id_file.read.chomp if master_id_file.file? # If we have the ID and the VM exists already, then we # have nothing to do. Success! - if env[:master_id] && env[:machine].provider.driver.vm_exists?(env[:master_id]) + if env[:clone_id] && env[:machine].provider.driver.vm_exists?(env[:clone_id]) @logger.info( "Master VM for '#{env[:machine].box.name}' already exists " + - " (id=#{env[:master_id]}) - skipping import step.") + " (id=#{env[:clone_id]}) - skipping import step.") return end @@ -53,27 +53,15 @@ module VagrantPlugins # Import the virtual machine import_env = env[:action_runner].run(Import, env.dup.merge(skip_machine: true)) - env[:master_id] = import_env[:machine_id] + env[:clone_id] = import_env[:machine_id] @logger.info( "Imported box #{env[:machine].box.name} as master vm " + - "with id #{env[:master_id]}") + "with id #{env[:clone_id]}") - if !env[:machine].provider_config.linked_clone_snapshot - snapshots = env[:machine].provider.driver.list_snapshots(env[:master_id]) - if !snapshots.include?("base") - @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 - end - end - - @logger.debug("Writing id of master VM '#{env[:master_id]}' to #{master_id_file}") + @logger.debug("Writing id of master VM '#{env[:clone_id]}' to #{master_id_file}") master_id_file.open("w+") do |f| - f.write(env[:master_id]) + f.write(env[:clone_id]) end end end diff --git a/plugins/providers/virtualbox/action/prepare_clone.rb b/plugins/providers/virtualbox/action/prepare_clone.rb index e3451cfef..6b3bf34dd 100644 --- a/plugins/providers/virtualbox/action/prepare_clone.rb +++ b/plugins/providers/virtualbox/action/prepare_clone.rb @@ -11,6 +11,17 @@ module VagrantPlugins def call(env) # We need to get the machine ID from this Vagrant environment + clone_env = env[:machine].env.environment( + env[:machine].config.vm.clone) + raise Vagrant::Errors::CloneNotFound if !clone_env.root_path + + # Get the machine itself + clone_machine = clone_env.machine( + clone_env.primary_machine_name, env[:machine].provider_name) + raise Vagrant::Errors::CloneMachineNotFound if !clone_machine.id + + # Set the ID of the master so we know what to clone from + env[:clone_id] = clone_machine.id # Continue @app.call(env) diff --git a/plugins/providers/virtualbox/action/prepare_clone_snapshot.rb b/plugins/providers/virtualbox/action/prepare_clone_snapshot.rb new file mode 100644 index 000000000..08ce16a6d --- /dev/null +++ b/plugins/providers/virtualbox/action/prepare_clone_snapshot.rb @@ -0,0 +1,65 @@ +require "log4r" + +require "digest/md5" + +module VagrantPlugins + module ProviderVirtualBox + module Action + class PrepareCloneSnapshot + def initialize(app, env) + @app = app + @logger = Log4r::Logger.new("vagrant::action::vm::prepare_clone") + end + + def call(env) + if !env[:clone_id] + @logger.info("no clone master, not preparing clone snapshot") + return @app.call(env) + end + + # If we're not doing a linked clone, snapshots don't matter + if !env[:machine].provider_config.linked_clone + return @app.call(env) + end + + # We lock so that we don't snapshot in parallel + lock_key = Digest::MD5.hexdigest("#{env[:clone_id]}-snapshot") + env[:machine].env.lock(lock_key, retry: true) do + prepare_snapshot(env) + end + + # Continue + @app.call(env) + end + + protected + + def prepare_snapshot(env) + name = env[:machine].provider_config.linked_clone_snapshot + name_set = !!name + name = "base" if !name + env[:clone_snapshot] = name + + # Get the snapshots. We're done if it already exists + snapshots = env[:machine].provider.driver.list_snapshots(env[:clone_id]) + if snapshots.include?(name) + @logger.info("clone snapshot already exists, doing nothing") + return + end + + # If they asked for a specific snapshot, it is an error + if name_set + # TODO: Error + end + + @logger.info("Creating base snapshot for master VM.") + env[:machine].provider.driver.create_snapshot( + env[:clone_id], name) do |progress| + env[:ui].clear_line + env[:ui].report_progress(progress, 100, false) + end + end + end + end + end +end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index d5fa23d2c..9e8c680aa 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -650,6 +650,17 @@ en: available below. %{help} + clone_not_found: |- + The specified Vagrantfile to clone from was not found. Please verify + the `config.vm.clone` setting points to a valid Vagrantfile. + clone_machine_not_found: |- + The clone environment hasn't been created yet. To clone from + another Vagrantfile, it must already be created with `vagrant up`. + It doesn't need to be running. + + Additionally, the created environment must be started with a provider + matching this provider. For example, if you're using VirtualBox, + the clone environment must also be using VirtualBox. command_unavailable: |- The executable '%{file}' Vagrant is trying to run was not found in the PATH variable. This is an error. Please verify