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



50
51
52
# File 'app/models/foreman_fog_proxmox/proxmox_images.rb', line 50

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

#clone_from_image(image_id, vmid) ⇒ Object



86
87
88
89
90
91
# File 'app/models/foreman_fog_proxmox/proxmox_images.rb', line 86

def clone_from_image(image_id, vmid)
  logger.debug("create_vm(): clone #{image_id} in #{vmid}")
  image = find_vm_by_uuid(image_id)
  image.clone(vmid)
  find_vm_by_uuid(id.to_s + '_' + vmid.to_s)
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
33
34
35
36
37
38
39
# File 'app/models/foreman_fog_proxmox/proxmox_images.rb', line 26

def images_by_storage(node_id, storage_id, type = 'iso')
  cached_images = cache.cache(:"images_by_storage-#{node_id}-#{storage_id}-#{type}") do
    node = client.nodes.get(node_id) || 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}")
    next [] unless storage

    storage.volumes.list_by_content_type(type).sort_by(&:volid).map do |volume|
      extract_attributes(volume, [:volid, :content, :format, :size, :used])
    end
  end

  structs_from_cache(cached_images)
end

#template(uuid) ⇒ Object



82
83
84
# File 'app/models/foreman_fog_proxmox/proxmox_images.rb', line 82

def template(uuid)
  find_vm_by_uuid(uuid)
end

#template_name(template) ⇒ Object



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

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

#template_uuid(template) ⇒ Object



46
47
48
# File 'app/models/foreman_fog_proxmox/proxmox_images.rb', line 46

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

#templatesObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/foreman_fog_proxmox/proxmox_images.rb', line 54

def templates
  cached_templates = cache.cache(:templates) do
    volumes = fog_nodes.flat_map do |node|
      storages = node.storages.list_by_content_type 'images'
      logger.debug("storages(): node_id #{node.node} type images")
      storages.reject { |storage| storage.active.to_i.zero? }.sort_by(&:storage).flat_map do |storage|
        # Skip disabled storages (enabled == 0 or nil)
        unless storage.enabled.to_i == 1
          logger.warn("Skipping disabled storage #{storage.identity} on #{node.node}")
          next []
        end

        # Fetch QEMU and LXC template images
        storage.volumes.list_by_content_type('images') + storage.volumes.list_by_content_type('rootdir')
      rescue StandardError => e
        logger.error("Failed to fetch volumes for storage #{storage.identity} on #{node.node}: #{e.message}")
        []
      end
    end

    volumes.select(&:template?).map do |volume|
      extract_attributes(volume, [:vmid, :name, :volid, :node_id]).merge(template: true)
    end
  end

  structs_from_cache(cached_templates)
end