commands/snapshot

This commit is contained in:
Mitchell Hashimoto 2015-10-07 15:52:37 -04:00
parent 169cacd710
commit a99ebcb3ce
6 changed files with 234 additions and 0 deletions

View File

@ -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] <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

View File

@ -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

View File

@ -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] <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

View File

@ -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 <subcommand> [<args>]"
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 <subcommand> -h`"
end
@env.ui.info(opts.help, prefix: false)
end
end
end
end
end

View File

@ -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] <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

View File

@ -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