commands/snapshot: push now uses caps to be more resilient

This commit is contained in:
Mitchell Hashimoto 2015-10-08 08:55:13 -04:00
parent 7480b65e9d
commit 0abc17eaed
2 changed files with 24 additions and 29 deletions

View File

@ -24,16 +24,25 @@ module VagrantPlugins
# Save the snapshot. This will raise an exception if it fails.
machine.action(:snapshot_save, snapshot_name: snapshot_name)
# Success! Write the resulting stack out
modify_snapshot_stack(machine) do |stack|
stack << snapshot_name
end
end
def pop(machine)
modify_snapshot_stack(machine) do |stack|
name = stack.pop
# By reverse sorting, we should be able to find the first
# pushed snapshot.
name = nil
snapshots = machine.provider.capability(:snapshot_list)
snapshots.sort.reverse.each do |snapshot|
if snapshot =~ /^push_\d+_\d+$/
name = snapshot
break
end
end
# If no snapshot was found, we never pushed
if !name
machine.ui.info(I18n.t("vagrant.commands.snapshot.no_push_snapshot"))
return
end
# Restore the snapshot and tell the provider to delete it as well.
machine.action(
@ -42,24 +51,6 @@ module VagrantPlugins
snapshot_delete: true)
end
end
protected
def modify_snapshot_stack(machine)
# Get the stack
snapshot_stack = []
snapshot_file = machine.data_dir.join("snapshot_stack")
snapshot_stack = JSON.parse(snapshot_file.read) if snapshot_file.file?
# Yield it so it can be modified
yield snapshot_stack
# Write it out
snapshot_file.open("w+") do |f|
f.write(JSON.dump(snapshot_stack))
end
end
end
end
end
end

View File

@ -1501,13 +1501,17 @@ en:
Post install message from the '%{name}' plugin:
%{message}
snapshot: |-
snapshot:
not_supported: |-
This provider doesn't support snapshots.
This may be intentional or this may be a bug. If this provider
should support snapshots, then please report this as a bug to the
maintainer of the provider.
no_push_snapshot: |-
No pushed snapshot found!
Use `vagrant snapshot push` to push a snapshot to restore to.
status:
aborted: |-
The VM is in an aborted state. This means that it was abruptly