Module: ForemanFogProxmox::ProxmoxImages

Included in:
Proxmox
Defined in:
app/models/foreman_fog_proxmox/proxmox_images.rb

Instance Method Summary collapse

Instance Method Details

#available_imagesObject



43
44
45
# File 'app/models/foreman_fog_proxmox/proxmox_images.rb', line 43

def available_images
  templates.collect { |template| OpenStruct.new(id: template_uuid(template), name: template_name(template)) }
end

#clone_from_image(image_id, args, vmid) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'app/models/foreman_fog_proxmox/proxmox_images.rb', line 60

def clone_from_image(image_id, args, vmid)
  logger.debug("create_vm(): clone #{image_id} in #{vmid}")
  image = find_vm_by_uuid(image_id)
  image.clone(vmid)
  clone = find_vm_by_uuid(id.to_s + '_' + vmid.to_s)
  options = {}
  options.store(:name, args[:name]) unless clone.container?
  options.store(:hostname, args[:name]) if clone.container?
  clone.update(options)
end

#image_exists?(image) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'app/models/foreman_fog_proxmox/proxmox_images.rb', line 22

def image_exists?(image)
  !find_vm_by_uuid(image).nil?
end

#images_by_storage(node_id, storage_id, type = 'iso') ⇒ Object



26
27
28
29
30
31
32
# File 'app/models/foreman_fog_proxmox/proxmox_images.rb', line 26

def images_by_storage(node_id, storage_id, type = 'iso')
  node = client.nodes.get node_id
  node ||= default_node
  storage = node.storages.get storage_id if storage_id
  logger.debug("images_by_storage(): node_id #{node_id} storage_id #{storage_id} type #{type}")
  storage.volumes.list_by_content_type(type).sort_by(&:volid) if storage
end

#template(uuid) ⇒ Object



56
57
58
# File 'app/models/foreman_fog_proxmox/proxmox_images.rb', line 56

def template(uuid)
  find_vm_by_uuid(uuid)
end

#template_name(template) ⇒ Object



34
35
36
37
# File 'app/models/foreman_fog_proxmox/proxmox_images.rb', line 34

def template_name(template)
  image = find_vm_by_uuid(template_uuid(template))
  image&.name
end

#template_uuid(template) ⇒ Object



39
40
41
# File 'app/models/foreman_fog_proxmox/proxmox_images.rb', line 39

def template_uuid(template)
  id.to_s + '_' + template.vmid.to_s
end

#templatesObject



47
48
49
50
51
52
53
54
# File 'app/models/foreman_fog_proxmox/proxmox_images.rb', line 47

def templates
  volumes = []
  nodes.each do |node|
    storage = storages(node.node).first
    volumes += storage.volumes.list_by_content_type('images')
  end
  volumes.select(&:template?)
end