Merge pull request #6879 from jtopper/allow_no_delete_snapshot_on_pop

Add missing features to snapshot restore/pop
This commit is contained in:
Paul Hinze 2016-03-04 11:40:54 -06:00
commit 32519b226c
3 changed files with 37 additions and 11 deletions

View File

@ -1,6 +1,10 @@
require 'json'
require 'optparse'
require 'vagrant'
require Vagrant.source_root.join("plugins/commands/up/start_mixins")
require_relative "push_shared"
module VagrantPlugins
@ -8,19 +12,30 @@ module VagrantPlugins
module Command
class Pop < Vagrant.plugin("2", :command)
include PushShared
include VagrantPlugins::CommandUp::StartMixins
def execute
options = {}
options[:snapshot_delete] = true
opts = OptionParser.new do |o|
o.banner = "Usage: vagrant snapshot pop [options] [vm-name]"
o.separator ""
build_start_options(o, options)
o.separator "Restore state that was pushed with `vagrant snapshot push`."
o.on("--no-delete", "Don't delete the snapshot after the restore") do
options[:snapshot_delete] = false
end
end
# Parse the options
argv = parse_options(opts)
return if !argv
return shared_exec(argv, method(:pop))
# Validate the provisioners
validate_provisioner_flags!(options, argv)
return shared_exec(argv, method(:pop), options)
end
end
end

View File

@ -4,7 +4,7 @@ module VagrantPlugins
module CommandSnapshot
module Command
module PushShared
def shared_exec(argv, m)
def shared_exec(argv, m, opts={})
with_target_vms(argv) do |vm|
if !vm.id
vm.ui.info("Not created. Cannot push snapshot state.")
@ -12,7 +12,7 @@ module VagrantPlugins
end
vm.env.lock("machine-snapshot-stack") do
m.call(vm)
m.call(vm, opts)
end
end
@ -20,14 +20,14 @@ module VagrantPlugins
0
end
def push(machine)
def push(machine, opts={})
snapshot_name = "push_#{Time.now.to_i}_#{rand(10000)}"
# Save the snapshot. This will raise an exception if it fails.
machine.action(:snapshot_save, snapshot_name: snapshot_name)
end
def pop(machine)
def pop(machine, opts={})
# By reverse sorting, we should be able to find the first
# pushed snapshot.
name = nil
@ -45,11 +45,9 @@ module VagrantPlugins
return
end
# Restore the snapshot and tell the provider to delete it as well.
machine.action(
:snapshot_restore,
snapshot_name: name,
snapshot_delete: true)
# Restore the snapshot and tell the provider to delete it, if required
opts[:snapshot_name] = name
machine.action(:snapshot_restore, opts)
end
end
end

View File

@ -1,15 +1,23 @@
require 'optparse'
require 'vagrant'
require Vagrant.source_root.join("plugins/commands/up/start_mixins")
module VagrantPlugins
module CommandSnapshot
module Command
class Restore < Vagrant.plugin("2", :command)
include VagrantPlugins::CommandUp::StartMixins
def execute
options = {}
opts = OptionParser.new do |o|
o.banner = "Usage: vagrant snapshot restore [options] [vm-name] <name>"
o.separator ""
build_start_options(o, options)
o.separator "Restore a snapshot taken previously with snapshot save."
end
@ -21,9 +29,14 @@ module VagrantPlugins
help: opts.help.chomp
end
# Validate the provisioners
validate_provisioner_flags!(options, argv)
name = argv.pop
options[:snapshot_name] = name
with_target_vms(argv) do |vm|
vm.action(:snapshot_restore, snapshot_name: name)
vm.action(:snapshot_restore, options)
end
# Success, exit status 0