Add --no-delete to 'snapshot pop' command

In our test environments, it's good to be able to roll back to the same,
anonymous, snapshot repeatedly.  This patch adds a `--no-delete` option
to the `snapshot pop` command allowing this.

This makes the new core snapshot behaviour more consistent with what we
were doing with vagrant-multiprovider-snap
(https://github.com/scalefactory/vagrant-multiprovider-snap)
This commit is contained in:
Jon Topper 2016-01-15 18:21:47 +00:00
parent af40b0e628
commit 7ba398ead8
2 changed files with 17 additions and 7 deletions

View File

@ -10,17 +10,22 @@ module VagrantPlugins
include PushShared include PushShared
def execute def execute
options = {}
opts = OptionParser.new do |o| opts = OptionParser.new do |o|
o.banner = "Usage: vagrant snapshot pop [options] [vm-name]" o.banner = "Usage: vagrant snapshot pop [options] [vm-name]"
o.separator "" o.separator ""
o.separator "Restore state that was pushed with `vagrant snapshot push`." 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[:no_delete] = true
end
end end
# Parse the options # Parse the options
argv = parse_options(opts) argv = parse_options(opts)
return if !argv return if !argv
return shared_exec(argv, method(:pop)) return shared_exec(argv, method(:pop), options)
end end
end end
end end

View File

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