Module: ForemanFogProxmox::ProxmoxVmCommands

Includes:
ProxmoxVolumes
Included in:
Proxmox
Defined in:
app/models/foreman_fog_proxmox/proxmox_vm_commands.rb

Constant Summary

Constants included from ProxmoxVmHelper

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

Instance Method Summary collapse

Methods included from ProxmoxVolumes

#add_volume, #delete_volume, #extend_or_move_volume, #extract_id, #save_volume, #volume_exists?, #volume_to_delete?

Methods included from ProxmoxVmHelper

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

Instance Method Details

#create_vm(args = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 30

def create_vm(args = {})
  vmid = args[:vmid].to_i
  type = args[:type]
  raise ::Foreman::Exception, format(N_('invalid vmid=%<vmid>s'), vmid: vmid) unless node.servers.id_valid?(vmid)

  image_id = args[:image_id]
  if image_id
    clone_from_image(image_id, args, vmid)
  else
    convert_sizes(args)
    remove_deletes(args)
    case type
    when 'qemu'
      vm = node.servers.create(parse_server_vm(args))
    when 'lxc'
      hash = parse_container_vm(args)
      hash = hash.merge(vmid: vmid)
      vm = node.containers.create(hash.reject { |key, _value| ['ostemplate_storage', 'ostemplate_file'].include? key })
    end
    start_on_boot(vm, args)
  end
rescue StandardError => e
  logger.warn(format(_('failed to create vm: %<e>s'), e: e))
  destroy_vm vm.id if vm
  raise e
end

#destroy_vm(uuid) ⇒ Object



57
58
59
60
61
62
63
64
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 57

def destroy_vm(uuid)
  vm = find_vm_by_uuid(uuid)
  vm.stop if vm.ready?
  vm.destroy
rescue ActiveRecord::RecordNotFound
  # if the VM does not exists, we don't really care.
  true
end

#save_vm(uuid, new_attributes) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 88

def save_vm(uuid, new_attributes)
  vm = find_vm_by_uuid(uuid)
  templated = new_attributes['templated']
  if templated == '1' && !vm.templated?
    vm.create_template
  else
    volumes_attributes = new_attributes['volumes_attributes']
    volumes_attributes&.each_value { |volume_attributes| save_volume(vm, volume_attributes) }
    parsed_attr = vm.container? ? parse_container_vm(new_attributes.merge(type: vm.type)) : parse_server_vm(new_attributes.merge(type: vm.type))
    logger.debug("parsed_attr=#{parsed_attr}")
    config_attributes = parsed_attr.reject { |key, _value| [:vmid, :templated, :ostemplate, :ostemplate_file, :ostemplate_storage, :volumes_attributes].include? key.to_sym }
    config_attributes = config_attributes.reject { |_key, value| ForemanFogProxmox::Value.empty?(value) }
    cdrom_attributes = parsed_attr.select { |_key, value| Fog::Proxmox::DiskHelper.cdrom?(value.to_s) }
    config_attributes = config_attributes.reject { |key, _value| Fog::Proxmox::DiskHelper.disk?(key) }
    vm.update(config_attributes.merge(cdrom_attributes))
    start_on_boot(vm, new_attributes)
  end
  find_vm_by_uuid(uuid)
end

#start_on_boot(vm, args) ⇒ Object



24
25
26
27
28
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 24

def start_on_boot(vm, args)
  startonboot = args[:config_attributes][:onboot].blank? ? false : Foreman::Cast.to_bool(args[:config_attributes][:onboot])
  vm.start if startonboot
  vm
end

#supports_update?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 66

def supports_update?
  true
end

#update_required?(old_attrs, new_attrs) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 70

def update_required?(old_attrs, new_attrs)
  return true if super(old_attrs, new_attrs)

  new_attrs[:interfaces_attributes]&.each do |key, interface|
    return true if (interface[:id].blank? || interface[:_delete] == '1') && key != 'new_interfaces' # ignore the template
  end

  new_attrs[:volumes_attributes]&.each do |key, volume|
    return true if (volume[:id].blank? || volume[:_delete] == '1') && key != 'new_volumes' # ignore the template
  end

  false
end

#user_data_supported?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 84

def user_data_supported?
  true
end