Module: ForemanSnapshotManagement::ProxmoxExtensions

Defined in:
app/models/foreman_snapshot_management/proxmox_extensions.rb

Instance Method Summary collapse

Instance Method Details

#capabilitiesObject

Extend Proxmox’s capabilities with snapshots.



6
7
8
# File 'app/models/foreman_snapshot_management/proxmox_extensions.rb', line 6

def capabilities
  super + [:snapshots, :limit_snapshot_name_format, :snapshot_include_ram]
end

#create_snapshot(host, name, description, include_ram = false, _quiesce = false) ⇒ Object

Create a Snapshot.

This method creates a Snapshot with a given name and optional description.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/foreman_snapshot_management/proxmox_extensions.rb', line 13

def create_snapshot(host, name, description, include_ram = false, _quiesce = false)
  server = find_vm_by_uuid host.uuid
  raise _('Name must contain at least 2 characters starting with alphabet. Valid characters are A-Z a-z 0-9 _') unless /^[A-Za-z]\w{1,}$/.match?(name)

  include_ram = include_ram ? 1 : 0
  snapshot = server.snapshots.create(name: name, vmstate: include_ram)
  snapshot.description = description
  snapshot.update
rescue StandardError => e
  Foreman::Logging.exception('Error creating Proxmox Snapshot', e)
  raise ::Foreman::WrappedException.new(e, N_('Unable to create Proxmox Snapshot'))
end

#get_snapshot(host, snapshot_id) ⇒ Object

Get Snapshot

This methods returns a specific Snapshot for a given host.



62
63
64
65
66
# File 'app/models/foreman_snapshot_management/proxmox_extensions.rb', line 62

def get_snapshot(host, snapshot_id)
  server = find_vm_by_uuid host.uuid
  snapshot = server.snapshots.get(snapshot_id)
  raw_to_snapshot(host, snapshot)
end

#get_snapshot_by_name(host, name) ⇒ Object

Get Snapshot by name

This method returns a specific Snapshot for a given host.



71
72
73
74
75
# File 'app/models/foreman_snapshot_management/proxmox_extensions.rb', line 71

def get_snapshot_by_name(host, name)
  server = find_vm_by_uuid host.uuid
  snapshot = server.snapshots.get(name)
  raw_to_snapshot(host, snapshot) if snapshot
end

#get_snapshots(host) ⇒ Object

Get Snapshots

This methods returns Snapshots for a given host.



80
81
82
83
84
85
86
# File 'app/models/foreman_snapshot_management/proxmox_extensions.rb', line 80

def get_snapshots(host)
  server = find_vm_by_uuid host.uuid
  server.snapshots.delete(server.snapshots.get('current'))
  server.snapshots.map do |snapshot|
    raw_to_snapshot(host, snapshot)
  end
end

#remove_snapshot(snapshot) ⇒ Object

Remove Snapshot

This method removes a Snapshot from a given host.



29
30
31
32
33
34
# File 'app/models/foreman_snapshot_management/proxmox_extensions.rb', line 29

def remove_snapshot(snapshot)
  snapshot.destroy
rescue StandardError => e
  Foreman::Logging.exception('Error removing Proxmox Snapshot', e)
  raise ::Foreman::WrappedException.new(e, N_('Unable to remove Proxmox Snapshot'))
end

#revert_snapshot(snapshot) ⇒ Object

Revert Snapshot

This method revert a host to a given Snapshot.



39
40
41
42
43
44
# File 'app/models/foreman_snapshot_management/proxmox_extensions.rb', line 39

def revert_snapshot(snapshot)
  snapshot.rollback
rescue StandardError => e
  Foreman::Logging.exception('Error reverting Proxmox Snapshot', e)
  raise ::Foreman::WrappedException.new(e, N_('Unable to revert Proxmox Snapshot'))
end

#update_snapshot(snapshot, name, description) ⇒ Object

Update Snapshot

This method renames a Snapshot from a given host.



49
50
51
52
53
54
55
56
57
# File 'app/models/foreman_snapshot_management/proxmox_extensions.rb', line 49

def update_snapshot(snapshot, name, description)
  raise _('Snapshot name cannot be changed') if snapshot.name != name

  snapshot.description = description
  snapshot.update
rescue StandardError => e
  Foreman::Logging.exception('Error updating Proxmox Snapshot', e)
  raise ::Foreman::WrappedException.new(e, N_('Unable to update Proxmox Snapshot'))
end