Module: ProxmoxVmHelper

Included in:
ForemanFogProxmox::Proxmox
Defined in:
app/helpers/proxmox_vm_helper.rb

Constant Summary collapse

KILO =
1024
MEGA =
KILO * KILO
GIGA =
KILO * MEGA

Instance Method Summary collapse

Instance Method Details

#add_cdrom_to_config_server(vm, config) ⇒ Object



45
46
47
48
49
50
51
# File 'app/helpers/proxmox_vm_helper.rb', line 45

def add_cdrom_to_config_server(vm,config)
  cd_disks = vm.config.disks.select { |disk| disk.id == 'ide2' }
  cdrom = {}
  disk_to_cdrom(cd_disks.first,cdrom)
  config = config.merge(cdrom)
  config
end

#convert_memory_size(config_hash, key) ⇒ Object



77
78
79
# File 'app/helpers/proxmox_vm_helper.rb', line 77

def convert_memory_size(config_hash, key)
  config_hash.store(key, (config_hash[key].to_i / MEGA).to_s) unless ForemanFogProxmox::Value.empty?(config_hash[key])
end

#convert_sizes(args) ⇒ Object



65
66
67
68
69
70
71
# File 'app/helpers/proxmox_vm_helper.rb', line 65

def convert_sizes(args)
  convert_memory_size(args['config_attributes'],'memory')
  convert_memory_size(args['config_attributes'],'min_memory')
  convert_memory_size(args['config_attributes'],'shares')
  convert_memory_size(args['config_attributes'],'swap')
  args['volumes_attributes'].each_value { |value| value['size'] = (value['size'].to_i / GIGA).to_s unless ForemanFogProxmox::Value.empty?(value['size']) }
end

#disk_to_cdrom(disk, cdrom) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/proxmox_vm_helper.rb', line 53

def disk_to_cdrom(disk,cdrom)
  volid = disk.volid  
  cdrom_a = %w[none cdrom]  
  if cdrom_a.include? volid
    cdrom.store('cdrom',volid)
  else
    cdrom.store('cdrom','image')
    cdrom.store('cdrom_iso',volid)
    cdrom.store('cdrom_storage',disk.storage)
  end
end

#mount_point_disk?(id) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'app/helpers/proxmox_vm_helper.rb', line 90

def mount_point_disk?(id)
  /^(mp)(\d+)$/.match?(id)
end

#object_to_config_hash(vm, type) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/helpers/proxmox_vm_helper.rb', line 30

def object_to_config_hash(vm,type)
  vm_h = ActiveSupport::HashWithIndifferentAccess.new
  main_a = %w[hostname name type node vmid]
  type = vm.config.attributes['type']
  type = vm.type unless type
  main = vm.config.attributes.select { |key,_value| main_a.include? key }
  disks_regexp = /^(scsi|sata|mp|rootfs|virtio|ide)(\d+){0,1}$/
  nics_regexp = /^(net)(\d+)/
  main_a += %w[templated]
  config = vm.config.attributes.reject { |key,_value| main_a.include?(key) || disks_regexp.match(key) || nics_regexp.match(key)  }
  vm_h = vm_h.merge(main)
  vm_h = vm_h.merge({'config_attributes': config})
  vm_h
end

#parse_type_and_vmid(uuid) ⇒ Object



81
82
83
84
85
86
87
88
# File 'app/helpers/proxmox_vm_helper.rb', line 81

def parse_type_and_vmid(uuid)
  uuid_regexp = /^(lxc|qemu)\_(\d+)$/
  raise ::Foreman::Exception.new _("Invalid uuid=[%{uuid}]." % { uuid: uuid }) unless uuid.match(uuid_regexp)
  id_a = uuid.scan(uuid_regexp).first
  type = id_a[0]
  vmid = id_a[1]
  return type, vmid
end

#remove_deletes(args) ⇒ Object



73
74
75
# File 'app/helpers/proxmox_vm_helper.rb', line 73

def remove_deletes(args)
  args['volumes_attributes'].delete_if { |_key,value| value.has_key? '_delete' }
end

#server_disk?(id) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'app/helpers/proxmox_vm_helper.rb', line 94

def server_disk?(id)
  /^(scsi|sata|virtio|ide)(\d+)$/.match?(id)
end