providers/virtualbox: clone

This commit is contained in:
Mitchell Hashimoto 2015-10-08 12:33:55 -04:00
parent c5c3ba616b
commit f0ddac8c9a
8 changed files with 130 additions and 32 deletions

View File

@ -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.
#

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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