Module: ForemanFogProxmox::ProxmoxVmCommands

Includes:
ProxmoxPools, 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 ProxmoxPools

#add_vm_to_pool, #pool_owner, #pools, #remove_vm_from_pool, #update_pool

Methods included from ProxmoxVolumes

#add_volume, #delete_volume, #extract_id, #save_volume, #update_volume, #volume_exists?, #volume_options, #volume_to_delete?

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

#create_vm(args = {}) ⇒ Object



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
56
57
58
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 31

def create_vm(args = {})
  vmid = args[:vmid].to_i
  type = args[:type]
  node = client.nodes.get(args[:node_id])
  vmid = node.servers.next_id.to_i if vmid < 1
  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



60
61
62
63
64
65
66
67
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 60

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 91

def save_vm(uuid, new_attributes)
  vm = find_vm_by_uuid(uuid)
  templated = new_attributes['templated']
  node_id = new_attributes['node_id']
  if templated == '1' && !vm.templated?
    vm.create_template
  elsif vm.node_id != node_id
    vm.migrate(node_id)
  else
    convert_memory_sizes(new_attributes)
    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, :pool].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))
    poolid = new_attributes['pool'] if new_attributes.key?('pool')
    update_pool(vm, poolid) if poolid
  end
  find_vm_by_uuid(uuid)
end

#start_on_boot(vm, args) ⇒ Object



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

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

#supports_update?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 69

def supports_update?
  true
end

#update_required?(old_attrs, new_attrs) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 73

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)


87
88
89
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 87

def user_data_supported?
  true
end