2015-10-08 02:52:27 +00:00
|
|
|
require 'json'
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module CommandSnapshot
|
|
|
|
module Command
|
|
|
|
module PushShared
|
2016-01-15 18:21:47 +00:00
|
|
|
def shared_exec(argv, m, opts={})
|
2015-10-08 02:52:27 +00:00
|
|
|
with_target_vms(argv) do |vm|
|
|
|
|
if !vm.id
|
|
|
|
vm.ui.info("Not created. Cannot push snapshot state.")
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
vm.env.lock("machine-snapshot-stack") do
|
2016-01-15 18:21:47 +00:00
|
|
|
m.call(vm,opts)
|
2015-10-08 02:52:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-08 14:40:46 +00:00
|
|
|
# Success, exit with 0
|
2015-10-08 02:52:27 +00:00
|
|
|
0
|
|
|
|
end
|
|
|
|
|
2016-01-15 18:21:47 +00:00
|
|
|
def push(machine,opts={})
|
2015-10-08 02:52:27 +00:00
|
|
|
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
|
|
|
|
|
2016-01-15 18:21:47 +00:00
|
|
|
def pop(machine,opts={})
|
2015-10-08 12:55:13 +00:00
|
|
|
# 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
|
2015-10-08 02:52:27 +00:00
|
|
|
end
|
|
|
|
|
2015-10-08 12:55:13 +00:00
|
|
|
# If no snapshot was found, we never pushed
|
|
|
|
if !name
|
|
|
|
machine.ui.info(I18n.t("vagrant.commands.snapshot.no_push_snapshot"))
|
|
|
|
return
|
2015-10-08 02:52:27 +00:00
|
|
|
end
|
2015-10-08 12:55:13 +00:00
|
|
|
|
2016-01-15 18:21:47 +00:00
|
|
|
snapshot_delete = true
|
|
|
|
if opts.key?(:no_delete)
|
|
|
|
snapshot_delete = false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Restore the snapshot and tell the provider to delete it, if required
|
2015-10-08 12:55:13 +00:00
|
|
|
machine.action(
|
|
|
|
:snapshot_restore,
|
|
|
|
snapshot_name: name,
|
2016-01-15 18:21:47 +00:00
|
|
|
snapshot_delete: snapshot_delete)
|
2015-10-08 02:52:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|