Module: ForemanFogProxmox::ProxmoxVolumes

Includes:
ProxmoxVmHelper
Included in:
Proxmox, ProxmoxVmCommands
Defined in:
app/models/foreman_fog_proxmox/proxmox_volumes.rb

Constant Summary

Constants included from ProxmoxVmHelper

ProxmoxVmHelper::GIGA, ProxmoxVmHelper::KILO, ProxmoxVmHelper::MEGA

Instance Method Summary collapse

Methods included from ProxmoxVmHelper

#add_cdrom_to_config_server, #convert_memory_size, #convert_memory_sizes, #convert_sizes, #convert_volumes_size, #disk_to_cdrom, #object_to_config_hash, #remove_deletes

Instance Method Details

#add_volume(vm, id, volume_attributes) ⇒ Object



75
76
77
78
79
# File 'app/models/foreman_fog_proxmox/proxmox_volumes.rb', line 75

def add_volume(vm, id, volume_attributes)
  options = volume_options(vm, id, volume_attributes)
  disk_attributes = { id: id, storage: volume_attributes['storage'], size: (volume_attributes['size'].to_i / GIGA).to_s }
  vm.attach(disk_attributes, options)
end

#delete_volume(vm, id) ⇒ Object



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

def delete_volume(vm, id)
  vm.detach(id)
  device = Fog::Proxmox::DiskHelper.extract_device(id)
  vm.detach('unused' + device.to_s)
end

#extract_id(vm, volume_attributes) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'app/models/foreman_fog_proxmox/proxmox_volumes.rb', line 64

def extract_id(vm, volume_attributes)
  id = ''
  if volume_exists?(volume_attributes)
    id = volume_attributes['id']
  else
    device = vm.container? ? 'mp' : volume_attributes['controller']
    id = device + volume_attributes['device']
  end
  id
end

#save_volume(vm, volume_attributes) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/foreman_fog_proxmox/proxmox_volumes.rb', line 81

def save_volume(vm, volume_attributes)
  id = extract_id(vm, volume_attributes)
  if volume_exists?(volume_attributes)
    if volume_to_delete?(volume_attributes)
      delete_volume(vm, id)
    else
      update_volume(vm, id, volume_attributes)
    end
  else
    add_volume(vm, id, volume_attributes)
  end
end

#update_volume(vm, id, volume_attributes) ⇒ Object

Raises:

  • (::Foreman::Exception)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/foreman_fog_proxmox/proxmox_volumes.rb', line 39

def update_volume(vm, id, volume_attributes)
  disk = vm.config.disks.get(id)
  diff_size = volume_attributes['size'].to_i - disk.size
  raise ::Foreman::Exception, format(_('Unable to shrink %<id>s size. Proxmox allows only increasing size.'), id: id) unless diff_size >= 0

  if diff_size > 0
    extension = '+' + (diff_size / GIGA).to_s + 'G'
    vm.extend(id, extension)
  elsif disk.storage != volume_attributes['storage']
    vm.move(id, volume_attributes['storage'])
  else
    options = volume_options(vm, id, volume_attributes)
    vm.attach({ :id => disk.id, :volid => disk.volid, :size => disk.size }, options)
  end
end

#volume_exists?(volume_attributes) ⇒ Boolean

Returns:

  • (Boolean)


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

def volume_exists?(volume_attributes)
  volid = volume_attributes.key?('volid') ? volume_attributes['volid'] : ''
  volid.present?
end

#volume_options(vm, id, volume_attributes) ⇒ Object



32
33
34
35
36
37
# File 'app/models/foreman_fog_proxmox/proxmox_volumes.rb', line 32

def volume_options(vm, id, volume_attributes)
  options = {}
  options.store(:mp, volume_attributes['mp']) if vm.container? && id != 'rootfs'
  options.store(:cache, volume_attributes['cache']) unless vm.container?
  options
end

#volume_to_delete?(volume_attributes) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/models/foreman_fog_proxmox/proxmox_volumes.rb', line 60

def volume_to_delete?(volume_attributes)
  volume_attributes['_delete'].blank? ? false : Foreman::Cast.to_bool(volume_attributes['_delete'])
end