From a99ebcb3ceeead60397d5632b19b61917eb6cc22 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 7 Oct 2015 15:52:37 -0400 Subject: [PATCH] commands/snapshot --- plugins/commands/snapshot/command/delete.rb | 35 +++++++++ plugins/commands/snapshot/command/list.rb | 30 ++++++++ plugins/commands/snapshot/command/restore.rb | 35 +++++++++ plugins/commands/snapshot/command/root.rb | 79 ++++++++++++++++++++ plugins/commands/snapshot/command/save.rb | 40 ++++++++++ plugins/commands/snapshot/plugin.rb | 15 ++++ 6 files changed, 234 insertions(+) create mode 100644 plugins/commands/snapshot/command/delete.rb create mode 100644 plugins/commands/snapshot/command/list.rb create mode 100644 plugins/commands/snapshot/command/restore.rb create mode 100644 plugins/commands/snapshot/command/root.rb create mode 100644 plugins/commands/snapshot/command/save.rb create mode 100644 plugins/commands/snapshot/plugin.rb diff --git a/plugins/commands/snapshot/command/delete.rb b/plugins/commands/snapshot/command/delete.rb new file mode 100644 index 000000000..0d5118847 --- /dev/null +++ b/plugins/commands/snapshot/command/delete.rb @@ -0,0 +1,35 @@ +require 'optparse' + +module VagrantPlugins + module CommandSnapshot + module Command + class Delete < Vagrant.plugin("2", :command) + def execute + options = {} + + opts = OptionParser.new do |o| + o.banner = "Usage: vagrant snapshot delete [options] [vm-name] " + o.separator "" + o.separator "Delete a snapshot taken previously with snapshot save." + end + + # Parse the options + argv = parse_options(opts) + return if !argv + if argv.empty? || argv.length > 2 + raise Vagrant::Errors::CLIInvalidUsage, + help: opts.help.chomp + end + + name = argv.pop + with_target_vms(argv) do |vm| + vm.action(:snapshot_delete, snapshot_name: name) + end + + # Success, exit status 0 + 0 + end + end + end + end +end diff --git a/plugins/commands/snapshot/command/list.rb b/plugins/commands/snapshot/command/list.rb new file mode 100644 index 000000000..af91e76f5 --- /dev/null +++ b/plugins/commands/snapshot/command/list.rb @@ -0,0 +1,30 @@ +require 'optparse' + +module VagrantPlugins + module CommandSnapshot + module Command + class List < Vagrant.plugin("2", :command) + def execute + options = {} + + opts = OptionParser.new do |o| + o.banner = "Usage: vagrant snapshot list [options] [vm-name]" + o.separator "" + o.separator "List all snapshots taken for a machine." + end + + # Parse the options + argv = parse_options(opts) + return if !argv + + with_target_vms(argv) do |vm| + vm.action(:snapshot_list) + end + + # Success, exit status 0 + 0 + end + end + end + end +end diff --git a/plugins/commands/snapshot/command/restore.rb b/plugins/commands/snapshot/command/restore.rb new file mode 100644 index 000000000..930a811f4 --- /dev/null +++ b/plugins/commands/snapshot/command/restore.rb @@ -0,0 +1,35 @@ +require 'optparse' + +module VagrantPlugins + module CommandSnapshot + module Command + class Restore < Vagrant.plugin("2", :command) + def execute + options = {} + + opts = OptionParser.new do |o| + o.banner = "Usage: vagrant snapshot restore [options] [vm-name] " + o.separator "" + o.separator "Restore a snapshot taken previously with snapshot save." + end + + # Parse the options + argv = parse_options(opts) + return if !argv + if argv.empty? || argv.length > 2 + raise Vagrant::Errors::CLIInvalidUsage, + help: opts.help.chomp + end + + name = argv.pop + with_target_vms(argv) do |vm| + vm.action(:snapshot_restore, snapshot_name: name) + end + + # Success, exit status 0 + 0 + end + end + end + end +end diff --git a/plugins/commands/snapshot/command/root.rb b/plugins/commands/snapshot/command/root.rb new file mode 100644 index 000000000..03bcebb80 --- /dev/null +++ b/plugins/commands/snapshot/command/root.rb @@ -0,0 +1,79 @@ +require 'optparse' + +module VagrantPlugins + module CommandSnapshot + module Command + class Root < Vagrant.plugin("2", :command) + def self.synopsis + "manages snapshots: saving, restoring, etc." + end + + def initialize(argv, env) + super + + @main_args, @sub_command, @sub_args = split_main_and_subcommand(argv) + + @subcommands = Vagrant::Registry.new + @subcommands.register(:save) do + require File.expand_path("../save", __FILE__) + Save + end + + @subcommands.register(:restore) do + require File.expand_path("../restore", __FILE__) + Restore + end + + @subcommands.register(:delete) do + require File.expand_path("../delete", __FILE__) + Delete + end + + @subcommands.register(:list) do + require File.expand_path("../list", __FILE__) + List + end + end + + def execute + if @main_args.include?("-h") || @main_args.include?("--help") + # Print the help for all the commands. + return help + end + + # If we reached this far then we must have a subcommand. If not, + # then we also just print the help and exit. + command_class = @subcommands.get(@sub_command.to_sym) if @sub_command + return help if !command_class || !@sub_command + @logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}") + + # Initialize and execute the command class + command_class.new(@sub_args, @env).execute + end + + # Prints the help out for this command + def help + opts = OptionParser.new do |opts| + opts.banner = "Usage: vagrant snapshot []" + opts.separator "" + opts.separator "Available subcommands:" + + # Add the available subcommands as separators in order to print them + # out as well. + keys = [] + @subcommands.each { |key, value| keys << key.to_s } + + keys.sort.each do |key| + opts.separator " #{key}" + end + + opts.separator "" + opts.separator "For help on any individual subcommand run `vagrant snapshot -h`" + end + + @env.ui.info(opts.help, prefix: false) + end + end + end + end +end diff --git a/plugins/commands/snapshot/command/save.rb b/plugins/commands/snapshot/command/save.rb new file mode 100644 index 000000000..496891eac --- /dev/null +++ b/plugins/commands/snapshot/command/save.rb @@ -0,0 +1,40 @@ +require 'optparse' + +module VagrantPlugins + module CommandSnapshot + module Command + class Save < Vagrant.plugin("2", :command) + def execute + options = {} + + opts = OptionParser.new do |o| + o.banner = "Usage: vagrant snapshot save [options] [vm-name] " + o.separator "" + o.separator "Take a snapshot of the current state of the machine. The snapshot" + o.separator "can be restored via `vagrant snapshot restore` at any point in the" + o.separator "future to get back to this exact machine state." + o.separator "" + o.separator "Snapshots are useful for experimenting in a machine and being able" + o.separator "to rollback quickly." + end + + # Parse the options + argv = parse_options(opts) + return if !argv + if argv.empty? || argv.length > 2 + raise Vagrant::Errors::CLIInvalidUsage, + help: opts.help.chomp + end + + name = argv.pop + with_target_vms(argv) do |vm| + vm.action(:snapshot_save, snapshot_name: name) + end + + # Success, exit status 0 + 0 + end + end + end + end +end diff --git a/plugins/commands/snapshot/plugin.rb b/plugins/commands/snapshot/plugin.rb new file mode 100644 index 000000000..e83be5bca --- /dev/null +++ b/plugins/commands/snapshot/plugin.rb @@ -0,0 +1,15 @@ +require "vagrant" + +module VagrantPlugins + module CommandSnapshot + class Plugin < Vagrant.plugin("2") + name "snapshot command" + description "The `snapshot` command gives you a way to manage snapshots." + + command("snapshot") do + require File.expand_path("../command/root", __FILE__) + Command::Root + end + end + end +end