Class: Pvectl::Commands::RollbackSnapshot

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

Overview

Handler for the ‘pvectl rollback snapshot` command.

Rolls back a VM/container to a snapshot. Requires –yes flag for confirmation. Only supports single VMID.

Examples:

Rollback to snapshot

pvectl rollback snapshot 100 before-upgrade --yes

Start after rollback (LXC only)

pvectl rollback snapshot 100 before-upgrade --yes --start

Constant Summary collapse

SUPPORTED_RESOURCES =
%w[snapshot].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_type, args, options, global_options) ⇒ RollbackSnapshot

Initializes a rollback snapshot command.

Parameters:

  • resource_type (String, nil)

    resource type (snapshot)

  • args (Array<String>, String, nil)

    VMID and snapshot name

  • options (Hash)

    command options

  • global_options (Hash)

    global CLI options



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

def initialize(resource_type, args, options, global_options)
  @resource_type = resource_type
  @args = Array(args).compact
  @options = options
  @global_options = global_options
  @too_many_args = false
  parse_args!
end

Class Method Details

.execute(resource_type, args, options, global_options) ⇒ Integer

Executes the rollback snapshot command.

Parameters:

  • resource_type (String, nil)

    resource type (snapshot)

  • args (Array<String>, String, nil)

    VMID and snapshot name

  • options (Hash)

    command options

  • global_options (Hash)

    global CLI options

Returns:

  • (Integer)

    exit code



88
89
90
# File 'lib/pvectl/commands/rollback_snapshot.rb', line 88

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

.register(cli) ⇒ void

This method returns an undefined value.

Registers the rollback command with the CLI.

Parameters:

  • cli (GLI::App)

    the CLI application object



21
22
23
24
25
26
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
70
71
72
73
74
75
76
77
# File 'lib/pvectl/commands/rollback_snapshot.rb', line 21

def self.register(cli)
  cli.desc "Rollback to a snapshot"
  cli.long_desc <<~HELP
    Rollback a virtual machine or container to a previous snapshot,
    restoring its disk (and optionally RAM) state to the snapshot point.

    WARNING: All changes made after the snapshot will be lost.

    EXAMPLES
      Rollback a VM to a snapshot:
        $ pvectl rollback snapshot 100 before-upgrade --yes

      Rollback and start the VM after:
        $ pvectl rollback snapshot 100 before-upgrade --yes --start

      Async rollback:
        $ pvectl rollback snapshot 100 before-upgrade --yes --async

    NOTES
      Rollback always operates on a single VM/container (not batch).

      The VM/container will be stopped during rollback if running.

      --yes is required — rollback is a destructive operation that
      discards all changes since the snapshot.

    SEE ALSO
      pvectl help create snapshot Create a snapshot before changes
      pvectl help get snapshots   List available snapshots
      pvectl help describe snapshot  View snapshot details
  HELP
  cli.arg_name "RESOURCE_TYPE ID SNAPSHOT_NAME"
  cli.command :rollback do |c|
    c.desc "Skip confirmation prompt (REQUIRED for destructive operations)"
    c.switch [:yes, :y], negatable: false

    c.desc "Start VM/container after rollback (LXC only)"
    c.switch [:start], negatable: false

    c.desc "Timeout in seconds for sync operations"
    c.flag [:timeout], type: Integer, arg_name: "SECONDS"

    c.desc "Force async mode (return task ID immediately)"
    c.switch [:async], negatable: false

    c.action do |global_options, options, args|
      resource_type = args.shift
      exit_code = Commands::RollbackSnapshot.execute(
        resource_type,
        args,
        options,
        global_options
      )
      exit exit_code if exit_code != 0
    end
  end
end

Instance Method Details

#executeInteger

Executes the rollback snapshot command.

Returns:

  • (Integer)

    exit code



110
111
112
113
114
115
116
117
118
# File 'lib/pvectl/commands/rollback_snapshot.rb', line 110

def execute
  return usage_error("Resource type required (snapshot)") unless @resource_type
  return usage_error("Unsupported resource: #{@resource_type}") unless SUPPORTED_RESOURCES.include?(@resource_type)
  return usage_error("Rollback supports only single VMID") if @too_many_args
  return usage_error("VMID and snapshot name required") if @vmid.nil? || @snapshot_name.nil?
  return usage_error("Confirmation required: use --yes to confirm rollback") unless @options[:yes]

  perform_operation
end