Module: ForemanFogProxmox::ProxmoxVMCommands

Includes:
ProxmoxEfidisks, ProxmoxPools, ProxmoxVolumes, ProxmoxVMHelper
Included in:
Proxmox
Defined in:
app/models/foreman_fog_proxmox/proxmox_vm_commands.rb

Instance Method Summary collapse

Methods included from ProxmoxVMHelper

#parse_typed_vm, #parse_vm_update_attributes, #update_boot_order, #vm_collection

Methods included from ProxmoxVMEfidiskHelper

#efidisk_defaults, #normalize_pre_enrolled_keys, #parse_typed_efidisk, #parsed_typed_efidisk, #process_firmware_attributes, #process_secure_boot_efidisk, #secure_boot_efidisk_attributes

Methods included from ProxmoxVMOsTemplateHelper

#ostemplate_keys, #parse_container_ostemplate, #parse_ostemplate, #parse_ostemplate_without_keys

Methods included from ProxmoxVMConfigHelper

#args_a, #config_a, #config_general_or_ostemplate_key?, #config_options, #config_typed_keys, #general_a, #object_to_config_hash, #parse_typed_cpu, #parse_typed_memory, #parsed_typed_config

Methods included from ProxmoxVMImageTemplateHelper

#hard_disk_slot, #validate_image_template_disk_slots!

Methods included from ProxmoxVMVolumesHelper

#add_disk_options, #add_typed_volume, #parse_hard_disk_volume, #parse_typed_volume, #parse_typed_volumes, #parsed_typed_volumes, #remove_volume_keys, #rootfs_volume?, #set_disk_attributes, #validate_cdrom_image_permission!, #volume_type?

Methods included from ProxmoxVMCloudinitHelper

#attach_cloudinit_iso, #check_template_format, #create_cloudinit_iso, #create_temp_directory, #default_iso_path, #delete_temp_dir, #generate_iso_command, #parse_cloudinit_config, #parse_server_cloudinit, #vm_ssh

Methods included from ProxmoxVMCdromHelper

#parse_server_cdrom

Methods included from ProxmoxVMInterfacesHelper

#add_or_delete_typed_interface, #compute_dhcps, #interface_common_typed_keys, #interface_compute_attributes_required_typed_keys, #interface_compute_attributes_typed_keys, #parse_typed_interfaces, #parsed_typed_interfaces, #sync_dhcp_for_ip

Methods included from ProxmoxPools

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

Methods included from ProxmoxEfidisks

#delete_efidisk, #process_efidisk_removal

Methods included from ProxmoxVolumes

#add_volume, #delete_volume, #extend_volume, #extract_id, #move_volume, #normalize_existing_volume_attributes, #save_volume, #update_cdrom, #update_options, #update_volume, #update_volume_required?, #volume_exists?, #volume_options, #volume_to_delete?

Instance Method Details

#assign_vmid(vmid, node, log: true) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 62

def assign_vmid(vmid, node, log: true)
  vmid = node.servers.next_id if vmid < 1

  unless node.servers.id_valid?(vmid)
    logger.warn("Requested VMID #{vmid} is occupied or out of range, requesting a new VMID") if log
    vmid = node.servers.next_id
  end

  vmid
end

#compute_clone_attributes(args, container, type, image: nil) ⇒ Object



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

def compute_clone_attributes(args, container, type, image: nil)
  args = parse_cloudinit_config(args) if args[:user_data]
  args[:config_attributes].merge!(update_boot_order(image)) if image && args[:config_attributes]
  parsed_args = parse_typed_vm(args, type)
  if container
    options = { :hostname => args[:name] }
    parsed_args.merge(options)
  end
  parsed_args.reject { |k| k == 'pool' }
end

#compute_config_attributes(parsed_attr) ⇒ Object



102
103
104
105
106
107
108
109
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 102

def compute_config_attributes(parsed_attr)
  excluded_keys = [:vmid, :templated, :ostemplate, :ostemplate_file, :ostemplate_storage, :volumes_attributes,
                   :pool]
  config_attributes = parsed_attr.reject { |key, _value| excluded_keys.include? key.to_sym }
  ForemanFogProxmox::HashCollection.remove_empty_values(config_attributes)
  config_attributes = config_attributes.reject { |key, _value| Fog::Proxmox::DiskHelper.disk?(key) }
  { config_attributes: config_attributes }
end

#create_vm(args = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 35

def create_vm(args = {})
  type = args[:type]
  node = client.nodes.get(args[:node_id])
  vmid = args[:vmid] = assign_vmid(args[:vmid].to_i, node)
  image_id = args[:image_id]
  remove_volume_keys(args)
  if image_id
    image = find_vm_by_uuid(image_id)
    validate_image_template_disk_slots!(image, args) if type == 'qemu'
    vm = clone_from_image(image, vmid)
    vm.update(compute_clone_attributes(args, vm.container?, type, image: image))
    update_pool(vm, args[:pool]) if args[:pool]
  else
    logger.warn("create vm: args=#{args}")
    vm = node.send(vm_collection(type)).create(parse_typed_vm(args, type))
    if type == 'qemu' && vm
      boot_order = update_boot_order(vm, exclude_cdrom: true, include_network: true)
      vm.update(boot_order) if boot_order
    end
  end
  start_on_boot(vm, args)
rescue StandardError => e
  logger.warn("failed to create vm: #{e}")
  destroy_vm id.to_s + '_' + vm.vmid.to_s if vm
  raise e
end

#destroy_vm(uuid) ⇒ Object



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

def destroy_vm(uuid)
  vm = find_vm_by_uuid(uuid)
  return true if vm.nil?
  vm.stop if vm.ready?
  vm.ha&.dig("managed").to_i == 1 ? vm.destroy(query: { purge: 1 }) : vm.destroy
rescue ActiveRecord::RecordNotFound
  # if the VM does not exists, we don't really care.
  true
end

#save_vm(uuid, new_attributes) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 111

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
    processed_attributes = process_firmware_attributes(new_attributes, vm.type)
    update_attributes = parse_vm_update_attributes(processed_attributes, vm.type)
    parsed_attr = parse_typed_vm(update_attributes, vm.type)
    config_attributes = compute_config_attributes(parsed_attr)

    volumes_attributes = new_attributes['volumes_attributes']
    logger.debug("save_vm(#{uuid}) volumes_attributes=#{volumes_attributes}")
    volumes_attributes&.each_value do |volume_attributes|
      validate_cdrom_image_permission!(volume_attributes)
      save_volume(vm, volume_attributes)
    end

    process_efidisk_removal(vm, processed_attributes)

    vm.update(config_attributes[:config_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



29
30
31
32
33
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 29

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)


94
95
96
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 94

def supports_update?
  true
end

#user_data_supported?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 98

def user_data_supported?
  true
end