Sha256: 3d45ec09d6002419b9b8f41eee67b595f2ffee428c6b2e7a4b57aebf5f3a4299

Contents?: true

Size: 1.9 KB

Versions: 30

Compression:

Stored size: 1.9 KB

Contents

require 'optparse'

module VagrantPlugins
  module CommandSnapshot
    module Command
      class Save < Vagrant.plugin("2", :command)
        def execute
          options = {}
          options[:force] = false

          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."

            o.on("-f", "--force", "Replace snapshot without confirmation") do |f|
              options[:force] = f
            end
          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|
            if !vm.provider.capability?(:snapshot_list)
              raise Vagrant::Errors::SnapshotNotSupported
            end

            snapshot_list = vm.provider.capability(:snapshot_list)

            if !snapshot_list.include? name
              vm.action(:snapshot_save, snapshot_name: name)
            elsif options[:force]
              # not a unique snapshot name
              vm.action(:snapshot_delete, snapshot_name: name)
              vm.action(:snapshot_save, snapshot_name: name)
            else
              raise Vagrant::Errors::SnapshotConflictFailed
            end
          end

          # Success, exit status 0
          0
        end
      end
    end
  end
end

Version data entries

30 entries across 26 versions & 4 rubygems

Version Path
vagrant-unbundled-2.2.5.0 plugins/commands/snapshot/command/save.rb
vagrant-unbundled-2.2.4.0 plugins/commands/snapshot/command/save.rb
vagrant-unbundled-2.2.3.0 plugins/commands/snapshot/command/save.rb
vagrant-unbundled-2.2.2.0 plugins/commands/snapshot/command/save.rb
vagrant-unbundled-2.2.0.0 plugins/commands/snapshot/command/save.rb
vagrant-unbundled-2.1.4.0 plugins/commands/snapshot/command/save.rb
vagrant-unbundled-2.1.2.0 plugins/commands/snapshot/command/save.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.5.0/bundler/gems/vagrant-04f7215b5e3f/plugins/commands/snapshot/command/save.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-04f7215b5e3f/plugins/commands/snapshot/command/save.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-04f7215b5e3f/plugins/commands/snapshot/command/save.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-04f7215b5e3f/plugins/commands/snapshot/command/save.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/bundler/gems/vagrant-04f7215b5e3f/plugins/commands/snapshot/command/save.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-04f7215b5e3f/plugins/commands/snapshot/command/save.rb
vagrant-unbundled-2.1.1.0 plugins/commands/snapshot/command/save.rb
vagrant-unbundled-2.0.4.0 plugins/commands/snapshot/command/save.rb
vagrant-unbundled-2.0.3.0 plugins/commands/snapshot/command/save.rb
vagrant-aws-detiber-0.7.2.pre.4 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/plugins/commands/snapshot/command/save.rb
vagrant-aws-detiber-0.7.2.pre.3 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/plugins/commands/snapshot/command/save.rb
vagrant-aws-detiber-0.7.2.pre.2 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/plugins/commands/snapshot/command/save.rb
vagrant-unbundled-2.0.2.0 plugins/commands/snapshot/command/save.rb