Class: Pvectl::Commands::DeleteSnapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/commands/delete_snapshot.rb

Overview

Handler for the ‘pvectl delete snapshot` sub-command.

Deletes snapshots from VMs/containers. Snapshot name is the first positional argument, or use –all to delete ALL snapshots. VMIDs are specified via –vmid flag. Without –vmid, operates cluster-wide.

Examples:

Delete named snapshot

pvectl delete snapshot before-upgrade --vmid 100 --yes

Delete all snapshots from a VM

pvectl delete snapshot --all --vmid 100 --yes

Delete named snapshot cluster-wide

pvectl delete snapshot before-upgrade --yes

Constant Summary collapse

VMID_PATTERN =
/\A[1-9]\d{0,8}\z/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, options, global_options) ⇒ DeleteSnapshot

Initializes a delete snapshot command.

Parameters:

  • args (Array<String>)

    positional args (snapshot name)

  • options (Hash)

    command options

  • global_options (Hash)

    global CLI options



82
83
84
85
86
87
88
89
90
# File 'lib/pvectl/commands/delete_snapshot.rb', line 82

def initialize(args, options, global_options)
  @args = Array(args)
  @options = options
  @global_options = global_options
  @snapshot_name = @args.first
  @vmids = parse_vmids(options[:vmid])
  @node = options[:node]
  @delete_all = options[:all] || false
end

Class Method Details

.execute(args, options, global_options) ⇒ Integer

Executes the delete snapshot command.

Parameters:

  • args (Array<String>)

    positional args (snapshot name)

  • options (Hash)

    command options

  • global_options (Hash)

    global CLI options

Returns:

  • (Integer)

    exit code



73
74
75
# File 'lib/pvectl/commands/delete_snapshot.rb', line 73

def self.execute(args, options, global_options)
  new(args, options, global_options).execute
end

.register_subcommand(parent) ⇒ void

This method returns an undefined value.

Registers as a sub-command under the parent delete command.

Parameters:

  • parent (GLI::Command)

    the parent delete command



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pvectl/commands/delete_snapshot.rb', line 27

def self.register_subcommand(parent)
  parent.desc "Delete snapshots from VMs or containers"
  parent.long_desc <<~HELP
    Delete snapshots from VMs and containers.

    EXAMPLES
      Delete a specific snapshot from a VM:
        $ pvectl delete snapshot before-upgrade --vmid 100 --yes

      Delete a snapshot from all VMs that have it:
        $ pvectl delete snapshot before-upgrade --yes

      Delete ALL snapshots from a specific VM:
        $ pvectl delete snapshot --all --vmid 100 --yes

      Delete ALL snapshots cluster-wide:
        $ pvectl delete snapshot --all --yes

    NOTES
      Snapshot deletion is irreversible.

      --all deletes every snapshot, not just the named one. Use with
      extreme caution, especially without --vmid.

    SEE ALSO
      pvectl help create snapshot Create snapshots
      pvectl help rollback        Rollback to a snapshot
      pvectl help get snapshots   List existing snapshots
  HELP
  parent.command :snapshot do |s|
    s.desc "VM/CT ID (repeatable)"
    s.flag [:vmid], arg_name: "VMID", multiple: true

    s.action do |global_options, options, args|
      exit_code = execute(args, options, global_options)
      exit exit_code if exit_code != 0
    end
  end
end

Instance Method Details

#executeInteger

Executes the delete snapshot command.

Returns:

  • (Integer)

    exit code



95
96
97
98
99
100
101
102
103
# File 'lib/pvectl/commands/delete_snapshot.rb', line 95

def execute
  return usage_error("Snapshot name or --all required") if @snapshot_name.nil? && !@delete_all
  return usage_error("Cannot use --all with snapshot name") if @snapshot_name && @delete_all
  return usage_error("Invalid VMID: #{invalid_vmid}") if invalid_vmid

  return ExitCodes::SUCCESS unless confirm_operation

  perform_operation
end