Class: Pvectl::Repositories::Snapshot

Inherits:
Base
  • Object
show all
Defined in:
lib/pvectl/repositories/snapshot.rb

Overview

Repository for VM/container snapshots.

Handles listing snapshots for both QEMU VMs and LXC containers. Filters out the “current” snapshot which represents the live state.

Examples:

Listing snapshots for a VM

repo = Snapshot.new(connection)
snapshots = repo.list(100, "pve1", :qemu)
snapshots.each { |s| puts "#{s.name}: #{s.description}" }

Listing snapshots for a container

snapshots = repo.list(101, "pve1", :lxc)

See Also:

Instance Method Summary collapse

Methods inherited from Base

#get, #initialize

Constructor Details

This class inherits a constructor from Pvectl::Repositories::Base

Instance Method Details

#create(vmid, node, resource_type, name:, description: nil, vmstate: false) ⇒ String

Creates a snapshot for a VM or container.

Uses ‘POST /nodes/node/qemu|lxc/vmid/snapshot` with parameters:

  • snapname: Name of the snapshot

  • description: Optional description

  • vmstate: Include VM RAM state (QEMU only, ignored for LXC)

Parameters:

  • vmid (Integer, String)

    VM or container identifier

  • node (String)

    node name where the resource resides

  • resource_type (Symbol)

    :qemu for VMs, :lxc for containers

  • name (String)

    name for the snapshot

  • description (String, nil) (defaults to: nil)

    optional description

  • vmstate (Boolean) (defaults to: false)

    include RAM state (QEMU only)

Returns:

  • (String)

    UPID of the snapshot creation task



57
58
59
60
61
62
63
64
# File 'lib/pvectl/repositories/snapshot.rb', line 57

def create(vmid, node, resource_type, name:, description: nil, vmstate: false)
  endpoint = resource_endpoint(resource_type)
  params = { snapname: name }
  params[:description] = description if description
  params[:vmstate] = vmstate if vmstate && resource_type == :qemu

  connection.client["nodes/#{node}/#{endpoint}/#{vmid}/snapshot"].post(params)
end

#delete(vmid, node, resource_type, snapname, force: false) ⇒ String

Deletes a snapshot.

Uses ‘DELETE /nodes/node/qemu|lxc/vmid/snapshot/snapname`.

Parameters:

  • vmid (Integer, String)

    VM or container identifier

  • node (String)

    node name where the resource resides

  • resource_type (Symbol)

    :qemu for VMs, :lxc for containers

  • snapname (String)

    name of the snapshot to delete

  • force (Boolean) (defaults to: false)

    force deletion even if snapshot is referenced

Returns:

  • (String)

    UPID of the delete task



76
77
78
79
80
81
82
# File 'lib/pvectl/repositories/snapshot.rb', line 76

def delete(vmid, node, resource_type, snapname, force: false)
  endpoint = resource_endpoint(resource_type)
  params = {}
  params[:force] = true if force

  connection.client["nodes/#{node}/#{endpoint}/#{vmid}/snapshot/#{snapname}"].delete(params)
end

#list(vmid, node, resource_type) ⇒ Array<Models::Snapshot>

Lists all snapshots for a VM or container.

Uses ‘/nodes/node/qemu/vmid/snapshot` for VMs or `/nodes/node/lxc/vmid/snapshot` for containers. Filters out the “current” snapshot which represents live state.

Parameters:

  • vmid (Integer, String)

    VM or container identifier

  • node (String)

    node name where the resource resides

  • resource_type (Symbol)

    :qemu for VMs, :lxc for containers

Returns:



32
33
34
35
36
37
38
39
40
41
# File 'lib/pvectl/repositories/snapshot.rb', line 32

def list(vmid, node, resource_type)
  endpoint = resource_endpoint(resource_type)
  response = connection.client["nodes/#{node}/#{endpoint}/#{vmid}/snapshot"].get

  response
    .reject { |s| s[:name] == "current" }
    .map { |data| build_model(data, vmid, node, resource_type) }
rescue StandardError
  []
end

#rollback(vmid, node, resource_type, snapname, start: false) ⇒ String

Rolls back to a snapshot.

Uses ‘POST /nodes/node/qemu|lxc/vmid/snapshot/snapname/rollback`.

Parameters:

  • vmid (Integer, String)

    VM or container identifier

  • node (String)

    node name where the resource resides

  • resource_type (Symbol)

    :qemu for VMs, :lxc for containers

  • snapname (String)

    name of the snapshot to rollback to

  • start (Boolean) (defaults to: false)

    start VM/container after rollback

Returns:

  • (String)

    UPID of the rollback task



94
95
96
97
98
99
100
# File 'lib/pvectl/repositories/snapshot.rb', line 94

def rollback(vmid, node, resource_type, snapname, start: false)
  endpoint = resource_endpoint(resource_type)
  params = {}
  params[:start] = true if start

  connection.client["nodes/#{node}/#{endpoint}/#{vmid}/snapshot/#{snapname}/rollback"].post(params)
end