diff --git a/plugins/providers/virtualbox/action.rb b/plugins/providers/virtualbox/action.rb index 088221ed1..96cd2373d 100644 --- a/plugins/providers/virtualbox/action.rb +++ b/plugins/providers/virtualbox/action.rb @@ -42,6 +42,8 @@ module VagrantPlugins autoload :SaneDefaults, File.expand_path("../action/sane_defaults", __FILE__) autoload :SetName, File.expand_path("../action/set_name", __FILE__) autoload :SetupPackageFiles, File.expand_path("../action/setup_package_files", __FILE__) + autoload :SnapshotDelete, File.expand_path("../action/snapshot_delete", __FILE__) + autoload :SnapshotSave, File.expand_path("../action/snapshot_save", __FILE__) autoload :Suspend, File.expand_path("../action/suspend", __FILE__) # Include the built-in modules so that we can use them as top-level @@ -222,6 +224,33 @@ module VagrantPlugins end end + def self.action_snapshot_delete + Vagrant::Action::Builder.new.tap do |b| + b.use CheckVirtualbox + b.use Call, Created do |env, b2| + if env[:result] + b2.use SnapshotDelete + else + b2.use MessageNotCreated + end + end + end + end + + # This is the action that is primarily responsible for saving a snapshot + def self.action_snapshot_save + Vagrant::Action::Builder.new.tap do |b| + b.use CheckVirtualbox + b.use Call, Created do |env, b2| + if env[:result] + b2.use SnapshotSave + else + b2.use MessageNotCreated + end + end + end + end + # This is the action that will exec into an SSH shell. def self.action_ssh Vagrant::Action::Builder.new.tap do |b| diff --git a/plugins/providers/virtualbox/action/snapshot_delete.rb b/plugins/providers/virtualbox/action/snapshot_delete.rb new file mode 100644 index 000000000..2efc8eae2 --- /dev/null +++ b/plugins/providers/virtualbox/action/snapshot_delete.rb @@ -0,0 +1,25 @@ +module VagrantPlugins + module ProviderVirtualBox + module Action + class SnapshotDelete + def initialize(app, env) + @app = app + end + + def call(env) + env[:ui].info(I18n.t( + "vagrant.actions.vm.snapshot.deleting", + name: env[:snapshot_name])) + env[:machine].provider.driver.delete_snapshot( + env[:machine].id, env[:snapshot_name]) + + env[:ui].success(I18n.t( + "vagrant.actions.vm.snapshot.deleted", + name: env[:snapshot_name])) + + @app.call(env) + end + end + end + end +end diff --git a/plugins/providers/virtualbox/action/snapshot_save.rb b/plugins/providers/virtualbox/action/snapshot_save.rb new file mode 100644 index 000000000..98b720763 --- /dev/null +++ b/plugins/providers/virtualbox/action/snapshot_save.rb @@ -0,0 +1,25 @@ +module VagrantPlugins + module ProviderVirtualBox + module Action + class SnapshotSave + def initialize(app, env) + @app = app + end + + def call(env) + env[:ui].info(I18n.t( + "vagrant.actions.vm.snapshot.saving", + name: env[:snapshot_name])) + env[:machine].provider.driver.create_snapshot( + env[:machine].id, env[:snapshot_name]) + + env[:ui].success(I18n.t( + "vagrant.actions.vm.snapshot.saved", + name: env[:snapshot_name])) + + @app.call(env) + end + end + end + end +end diff --git a/plugins/providers/virtualbox/driver/meta.rb b/plugins/providers/virtualbox/driver/meta.rb index df52f0849..a4246ba78 100644 --- a/plugins/providers/virtualbox/driver/meta.rb +++ b/plugins/providers/virtualbox/driver/meta.rb @@ -89,6 +89,7 @@ module VagrantPlugins :create_host_only_network, :create_snapshot, :delete, + :delete_snapshot, :delete_unused_host_only_networks, :discard_saved_state, :enable_adapters, @@ -113,6 +114,7 @@ module VagrantPlugins :read_vms, :reconfig_host_only, :remove_dhcp_server, + :restore_snapshot, :resume, :set_mac_address, :set_name, diff --git a/plugins/providers/virtualbox/driver/version_5_0.rb b/plugins/providers/virtualbox/driver/version_5_0.rb index df833bb3f..b7e186538 100644 --- a/plugins/providers/virtualbox/driver/version_5_0.rb +++ b/plugins/providers/virtualbox/driver/version_5_0.rb @@ -86,6 +86,14 @@ module VagrantPlugins execute("snapshot", machine_id, "take", snapshot_name) end + def delete_snapshot(machine_id, snapshot_name) + execute("snapshot", machine_id, "delete", snapshot_name) + end + + def restore_snapshot(machine_id, snapshot_name) + execute("snapshot", machine_id, "restore", snapshot_name) + end + def delete execute("unregistervm", @uuid, "--delete") end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 171cd60ba..7db1260ae 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1765,6 +1765,17 @@ en: set_name: setting_name: |- Setting the name of the VM: %{name} + snapshot: + deleting: |- + Deleting the snapshot '%{name}'... + deleted: |- + Snapshot deleted! + saving: |- + Snapshotting the machine as '%{name}'... + saved: |- + Snapshot saved! You can restore the snapshot at any time by + using `vagrant snapshot restore`. You can delete it using + `vagrant snapshot delete`. suspend: suspending: Saving VM state and suspending execution...