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 'json'
require 'optparse' require 'optparse'
require 'vagrant'
require Vagrant.source_root.join("plugins/commands/up/start_mixins")
require_relative "push_shared" require_relative "push_shared"
module VagrantPlugins module VagrantPlugins
@ -8,19 +12,30 @@ module VagrantPlugins
module Command module Command
class Pop < Vagrant.plugin("2", :command) class Pop < Vagrant.plugin("2", :command)
include PushShared include PushShared
include VagrantPlugins::CommandUp::StartMixins
def execute def execute
options = {}
options[:snapshot_delete] = true
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 ""
build_start_options(o, options)
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[:snapshot_delete] = false
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)) # Validate the provisioners
validate_provisioner_flags!(options, argv)
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,9 @@ module VagrantPlugins
return return
end end
# Restore the snapshot and tell the provider to delete it as well. # Restore the snapshot and tell the provider to delete it, if required
machine.action( opts[:snapshot_name] = name
:snapshot_restore, machine.action(:snapshot_restore, opts)
snapshot_name: name,
snapshot_delete: true)
end end
end end
end end

View File

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