Class: Pvectl::Commands::CreateSnapshot

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

Overview

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

Creates snapshots for VMs/containers. Snapshot name is the first positional argument, VMIDs are specified via –vmid flag. Without –vmid, operates on ALL VMs/CTs in the cluster.

Examples:

Create single snapshot

pvectl create snapshot before-upgrade --vmid 100

Create for multiple VMs

pvectl create snapshot before-upgrade --vmid 100 --vmid 101

Create cluster-wide

pvectl create 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) ⇒ CreateSnapshot

Initializes a create snapshot command.

Parameters:

  • args (Array<String>)

    positional args (snapshot name)

  • options (Hash)

    command options

  • global_options (Hash)

    global CLI options



86
87
88
89
90
91
92
93
# File 'lib/pvectl/commands/create_snapshot.rb', line 86

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

Class Method Details

.execute(args, options, global_options) ⇒ Integer

Executes the create snapshot command.

Parameters:

  • args (Array<String>)

    positional args (snapshot name)

  • options (Hash)

    command options

  • global_options (Hash)

    global CLI options

Returns:

  • (Integer)

    exit code



77
78
79
# File 'lib/pvectl/commands/create_snapshot.rb', line 77

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 create command.

Parameters:

  • parent (GLI::Command)

    the parent create 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
66
67
68
69
# File 'lib/pvectl/commands/create_snapshot.rb', line 27

def self.register_subcommand(parent)
  parent.desc "Create a snapshot of VMs or containers"
  parent.long_desc <<~HELP
    Create a snapshot of one or more VMs or containers. Snapshots capture
    the current state of disks (and optionally RAM) for later rollback.

    EXAMPLES
      Snapshot a single VM:
        $ pvectl create snapshot before-upgrade --vmid 100

      Snapshot multiple VMs:
        $ pvectl create snapshot before-upgrade --vmid 100 --vmid 101

      Snapshot with description and VM memory state:
        $ pvectl create snapshot before-upgrade --vmid 100 --description "Pre-upgrade" --vmstate

      Snapshot all VMs/containers cluster-wide:
        $ pvectl create snapshot before-upgrade --yes

    NOTES
      --vmstate saves the VM's RAM state (QEMU only, not for containers).
      This increases snapshot size but allows resuming from exact state.

      Without --vmid, operates cluster-wide (requires --yes confirmation).

    SEE ALSO
      pvectl help rollback        Rollback to a snapshot
      pvectl help delete snapshot Delete snapshots
      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.desc "Save VM memory state (QEMU only)"
    s.switch [:vmstate], negatable: false

    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 create snapshot command.

Returns:

  • (Integer)

    exit code



98
99
100
101
102
103
# File 'lib/pvectl/commands/create_snapshot.rb', line 98

def execute
  return usage_error("Snapshot name required") unless @snapshot_name
  return usage_error("Invalid VMID: #{invalid_vmid}") if invalid_vmid

  perform_operation
end