Module: ProxmoxVmHelper

Included in:
ForemanFogProxmox::Proxmox, ForemanFogProxmox::ProxmoxVmNew, ForemanFogProxmox::ProxmoxVolumes
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



40
41
42
43
44
45
46
# File 'app/helpers/proxmox_vm_helper.rb', line 40

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



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

def convert_memory_size(config_hash, key)
  # default unit memory size is Mb
  memory = (config_hash[key].to_i / MEGA).to_s == '0' ? config_hash[key] : (config_hash[key].to_i / MEGA).to_s
  config_hash.store(key, memory)
end

#convert_memory_sizes(args) ⇒ Object



60
61
62
63
64
65
# File 'app/helpers/proxmox_vm_helper.rb', line 60

def convert_memory_sizes(args)
  convert_memory_size(args['config_attributes'], 'memory')
  convert_memory_size(args['config_attributes'], 'balloon')
  convert_memory_size(args['config_attributes'], 'shares')
  convert_memory_size(args['config_attributes'], 'swap')
end

#convert_sizes(args) ⇒ Object



71
72
73
74
75
76
77
# File 'app/helpers/proxmox_vm_helper.rb', line 71

def convert_sizes(args)
  convert_memory_size(args['config_attributes'], 'memory')
  convert_memory_size(args['config_attributes'], 'balloon')
  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

#convert_volumes_size(args) ⇒ Object



67
68
69
# File 'app/helpers/proxmox_vm_helper.rb', line 67

def convert_volumes_size(args)
  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



48
49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/proxmox_vm_helper.rb', line 48

def disk_to_cdrom(disk, cdrom)
  volid = disk.volid
  cdrom_a = ['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

#object_to_config_hash(vm) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'app/helpers/proxmox_vm_helper.rb', line 29

def object_to_config_hash(vm)
  vm_h = ActiveSupport::HashWithIndifferentAccess.new
  main_a = ['hostname', 'name', 'vmid']
  main = vm.attributes.select { |key, _value| main_a.include? key }
  main_a += ['templated']
  config = vm.config.attributes.reject { |key, _value| main_a.include?(key) || Fog::Proxmox::DiskHelper.disk?(key) || Fog::Proxmox::NicHelper.nic?(key) }
  vm_h = vm_h.merge(main)
  vm_h = vm_h.merge('config_attributes': config)
  vm_h
end

#remove_deletes(args) ⇒ Object



79
80
81
# File 'app/helpers/proxmox_vm_helper.rb', line 79

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