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.



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

def capabilities
  super + [:snapshots, :snapshot_include_ram, :snapshot_include_quiesce, :editable_snapshot_name]
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
25
26
27
28
29
# File 'app/models/foreman_snapshot_management/vmware_extensions.rb', line 13

def create_snapshot(host, name, description, include_ram = false, quiesce = false)
  server = find_vm_by_uuid(host.uuid)
  raise N_('Unable to create VMWare Snapshot with Quiesce. Check Power and VMWare Tools status.') if quiesce && (server.power_state != 'poweredOn' || server.tools_state != 'toolsOk')
  raise N_('Unable to create VMWare Snapshot. Cannot set both Memory and Quiesce options.') if quiesce && include_ram

  task = client.vm_take_snapshot(
    'instance_uuid' => host.uuid,
    'name' => name,
    'description' => description,
    'quiesce' => quiesce,
    '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(host, snapshot_id) ⇒ Object

Get Snapshot

This methods returns a specific Snapshot for a given host.



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

def get_snapshot(host, snapshot_id)
  raw_snapshot = client.snapshots(server_id: host.uuid).get(snapshot_id)
  raw_to_snapshot(host, raw_snapshot)
end

#get_snapshot_by_name(host, name) ⇒ Object

Get Snapshot by name

This method returns a specific Snapshot for a given host.



75
76
77
78
79
80
81
82
83
84
# File 'app/models/foreman_snapshot_management/vmware_extensions.rb', line 75

def get_snapshot_by_name(host, name)
  raw_snapshot = nil
  client.snapshots(server_id: host.uuid).all(recursive: true).each do |snapshot|
    if name == snapshot.name
      raw_snapshot = snapshot
      break
    end
  end
  raw_to_snapshot(host, raw_snapshot)
end

#get_snapshots(host) ⇒ Object

Get Snapshots

This methods returns Snapshots for a given host.



89
90
91
92
93
# File 'app/models/foreman_snapshot_management/vmware_extensions.rb', line 89

def get_snapshots(host)
  client.snapshots(server_id: host.uuid).all(recursive: true).map do |raw_snapshot|
    raw_to_snapshot(host, raw_snapshot)
  end
end

#remove_snapshot(snapshot, remove_children = false) ⇒ Object

Remove Snapshot

This method removes a Snapshot from a given host.



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

def remove_snapshot(snapshot, remove_children = false)
  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.



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

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.



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

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