Module: ForemanFogProxmox::ProxmoxVmCommands

Includes:
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, #vm_collection

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 ProxmoxVmVolumesHelper

#add_disk_options, #add_typed_volume, #parse_hard_disk_volume, #parse_typed_volume, #parse_typed_volumes, #parsed_typed_volumes, #remove_volume_keys, #volume_type?

Methods included from ProxmoxVmCloudinitHelper

#parse_server_cloudinit

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_typed_keys, #parse_typed_interfaces, #parsed_typed_interfaces

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, #extend_volume, #extract_id, #move_volume, #save_volume, #update_cdrom, #update_options, #update_volume, #update_volume_required?, #volume_exists?, #volume_options, #volume_to_delete?

Instance Method Details

#compute_config_attributes(parsed_attr) ⇒ Object



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

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



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

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
    remove_volume_keys(args)
    logger.warn("create vm: args=#{args}")
    vm = node.send(vm_collection(type)).create(parse_typed_vm(args, type))
    start_on_boot(vm, args)
  end
rescue StandardError => e
  logger.warn("failed to create vm: #{e}")
  destroy_vm client.identity + '_' + vm.id if vm
  raise e
end

#destroy_vm(uuid) ⇒ Object



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

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

#save_vm(uuid, new_attributes) ⇒ Object



84
85
86
87
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 84

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
    parsed_attr = parse_typed_vm(
      ForemanFogProxmox::HashCollection.new_hash_reject_keys(new_attributes,
        ['volumes_attributes']).merge(type: vm.type), 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 { |volume_attributes| save_volume(vm, volume_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



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

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)


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

def supports_update?
  true
end

#user_data_supported?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/foreman_fog_proxmox/proxmox_vm_commands.rb', line 71

def user_data_supported?
  true
end