Module: ForemanSnapshotManagement::VmwareExtensions

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

Instance Method Summary collapse

Instance Method Details

#capabilitiesObject

Extend VMWare’s capabilities with snapshots.



4
5
6
# File 'app/models/foreman_snapshot_management/vmware_extensions.rb', line 4

def capabilities
  super + [:snapshots]
end

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

Create a Snapshot.

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



11
12
13
14
15
16
17
# File 'app/models/foreman_snapshot_management/vmware_extensions.rb', line 11

def create_snapshot(uuid, name, description, include_ram = false)
  task = client.vm_take_snapshot('instance_uuid' => uuid, 'name' => name, 'description' => description, 'memory' => include_ram)
  task_successful?(task)
rescue RbVmomi::Fault => e
  Foreman::Logging.exception('Error creating VMWare Snapshot', e)
  raise ::Foreman::WrappedException.new(e, N_('Unable to create VMWare Snapshot'))
end

#get_snapshot(server_id, snapshot_id) ⇒ Object

Get Snapshot

This methods returns a specific Snapshot for a given host.



55
56
57
58
59
60
61
# File 'app/models/foreman_snapshot_management/vmware_extensions.rb', line 55

def get_snapshot(server_id, snapshot_id)
  snapshot = client.snapshots(server_id: server_id).get(snapshot_id)
  # Workaround for https://github.com/fog/fog-vsphere/commit/d808255cd19c3d43d3227825f1e0d72d3f6ee6b9
  # Remove, when fog-vshpere 1.11 lands in foreman
  snapshot = snapshot.get_child(snapshot_id) while snapshot && snapshot.ref != snapshot_id
  snapshot
end

#get_snapshots(server_id) ⇒ Object

Get Snapshots

This methods returns Snapshots from a given host.



66
67
68
# File 'app/models/foreman_snapshot_management/vmware_extensions.rb', line 66

def get_snapshots(server_id)
  client.snapshots(server_id: server_id).all(recursive: true)
end

#remove_snapshot(snapshot, remove_children) ⇒ Object

Remove Snapshot

This method removes a Snapshot from a given host.



22
23
24
25
26
27
28
# File 'app/models/foreman_snapshot_management/vmware_extensions.rb', line 22

def remove_snapshot(snapshot, remove_children)
  task = client.remove_snapshot('snapshot' => snapshot, 'removeChildren' => remove_children)
  task_successful?(task)
rescue RbVmomi::Fault => e
  Foreman::Logging.exception('Error removing VMWare Snapshot', e)
  raise ::Foreman::WrappedException.new(e, N_('Unable to remove VMWare Snapshot'))
end

#revert_snapshot(snapshot) ⇒ Object

Revert Snapshot

This method revert a host to a given Snapshot.



33
34
35
36
37
38
39
# File 'app/models/foreman_snapshot_management/vmware_extensions.rb', line 33

def revert_snapshot(snapshot)
  task = client.revert_to_snapshot(snapshot)
  task_successful?(task)
rescue RbVmomi::Fault => e
  Foreman::Logging.exception('Error reverting VMWare Snapshot', e)
  raise ::Foreman::WrappedException.new(e, N_('Unable to revert VMWare Snapshot'))
end

#update_snapshot(snapshot, name, description) ⇒ Object

Update Snapshot

This method renames a Snapshot from a given host.



44
45
46
47
48
49
50
# File 'app/models/foreman_snapshot_management/vmware_extensions.rb', line 44

def update_snapshot(snapshot, name, description)
  client.rename_snapshot('snapshot' => snapshot, 'name' => name, 'description' => description)
  true
rescue RbVmomi::Fault => e
  Foreman::Logging.exception('Error updating VMWare Snapshot', e)
  raise ::Foreman::WrappedException.new(e, N_('Unable to update VMWare Snapshot'))
end