Module: ForemanFogProxmox::ProxmoxVMNew

Includes:
ProxmoxVMHelper
Included in:
HostsController::Overrides, Proxmox
Defined in:
app/models/foreman_fog_proxmox/proxmox_vm_new.rb

Instance Method Summary collapse

Methods included from ProxmoxVMHelper

#parse_typed_vm, #vm_collection

Methods included from ProxmoxVMEfidiskHelper

#parse_typed_efidisk, #parsed_typed_efidisk

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, #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, #update_boot_order, #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

Instance Method Details

#add_default_typed_interface(type, new_attr) ⇒ Object



113
114
115
116
117
118
119
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 113

def add_default_typed_interface(type, new_attr)
  interfaces_attributes = []
  interfaces_attributes.push(interface_typed_defaults(type))
  new_attr = new_attr.merge(interfaces_attributes: interfaces_attributes.map.with_index.to_h.invert)
  logger.debug("add_default_typed_interface(#{type}) to new_attr=#{new_attr}")
  new_attr
end

#add_default_typed_volume(new_attr) ⇒ Object



121
122
123
124
125
126
127
128
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 121

def add_default_typed_volume(new_attr)
  volumes_attributes = []
  volumes_attributes.push(hard_disk_typed_defaults('qemu'))
  volumes_attributes.push(hard_disk_typed_defaults('lxc'))
  new_attr = new_attr.merge(volumes_attributes: volumes_attributes.map.with_index.to_h.invert)
  logger.debug("add_default_typed_volume(#{type}) to new_attr=#{new_attr}")
  new_attr
end

#assign_available_vmid(new_attr, node) ⇒ Object



187
188
189
190
191
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 187

def assign_available_vmid(new_attr, node)
  return if new_attr['vmid'].blank? || new_attr['node_id'].blank?

  new_attr['vmid'] = assign_vmid(new_attr['vmid'].to_i, node, log: false)
end

#cdrom_defaultsObject



31
32
33
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 31

def cdrom_defaults
  { storage_type: 'cdrom', id: 'ide2', volid: 'none', media: 'cdrom' }
end

#cloudinit_defaultsObject



35
36
37
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 35

def cloudinit_defaults
  { storage_type: 'cloud_init', id: 'ide0', storage: storages.first&.identity.to_s, media: 'cdrom' }
end

#config_attributes(type = 'qemu') ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 141

def config_attributes(type = 'qemu')
  case type
  when 'qemu'
    config_attributes = {
      cores: '1',
      sockets: '1',
      kvm: '1',
      vga: 'std',
      memory: '1024',
      ostype: 'l26',
      cpu: 'cputype=kvm64',
      scsihw: 'virtio-scsi-pci',
      templated: '0',
    }
    config_attributes = config_attributes
  when 'lxc'
    config_attributes = {
      memory: '1024',
      templated: '0',
    }
  end
  config_attributes
end

#convert_config_attributes(new_attr) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 172

def convert_config_attributes(new_attr)
  config_attributes = new_attr[:config_attributes]
  if config_attributes.key?(:disks)
    config_attributes[:volumes_attributes] = Hash[config_attributes[:disks].each_with_index.map do |disk, idx|
                                                    [idx.to_s, disk.attributes]
                                                  end ]
  end
  if config_attributes.key?(:interfaces)
    config_attributes[:interfaces_attributes] = Hash[config_attributes[:interfaces].each_with_index.map do |interface, idx|
                                                       [idx.to_s, interface_compute_attributes(interface.attributes)]
                                                     end ]
  end
  config_attributes.delete_if { |key, _value| ['disks', 'interfaces'].include?(key) }
end

#default_nodeObject



101
102
103
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 101

def default_node
  nodes.first
end

#default_node_idObject



105
106
107
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 105

def default_node_id
  default_node.node
end

#hard_disk_typed_defaults(vm_type) ⇒ Object



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

def hard_disk_typed_defaults(vm_type)
  options = {}
  volume_attributes_h = { storage: storages.first&.identity.to_s, size: '8' }
  case vm_type
  when 'qemu'
    controller = 'virtio'
    device = 0
    id = "#{controller}#{device}"
    options = { cache: 'none', backup: '1' }
    volume_attributes_h = volume_attributes_h.merge(controller: controller, device: device)
  when 'lxc'
    id = 'rootfs'
    volume_attributes_h = volume_attributes_h.merge(storage_type: 'rootfs')
  end
  volume_attributes_h[:id] = id
  volume_attributes_h[:options] = options
  volume_attributes_h
end

#interface_defaults(id = 'net0') ⇒ Object



73
74
75
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 73

def interface_defaults(id = 'net0')
  { id: id, compute_attributes: { model: 'virtio', name: 'eth0', bridge: bridges.first&.identity.to_s } }
end

#interface_typed_defaults(type) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 77

def interface_typed_defaults(type)
  interface_attributes_h = { id: 'net0', compute_attributes: {} }
  if type == 'qemu'
    interface_attributes_h[:compute_attributes] =
      { model: 'virtio', bridge: bridges.first&.identity.to_s }
  end
  if type == 'lxc'
    interface_attributes_h[:compute_attributes] =
      { name: 'eth0', bridge: bridges.first&.identity.to_s, dhcp: 1, dhcp6: 1 }
  end
  interface_attributes_h
end

#new_interface(attr = {}) ⇒ Object



95
96
97
98
99
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 95

def new_interface(attr = {})
  type = attr['type']
  type ||= 'qemu'
  new_typed_interface(attr, type)
end

#new_typed_interface(attr, type) ⇒ Object



90
91
92
93
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 90

def new_typed_interface(attr, type)
  opts = interface_typed_defaults(type).merge(attr.to_h).deep_symbolize_keys
  Fog::Proxmox::Compute::Interface.new(opts)
end

#new_typed_vm(new_attr, type) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 193

def new_typed_vm(new_attr, type)
  convert_config_attributes(new_attr) if new_attr.key?(:config_attributes)
  node_id = new_attr['node_id']
  node = node_id ? client.nodes.get(node_id) : default_node
  assign_available_vmid(new_attr, node)
  new_attr_type = new_attr['type']
  new_attr_type ||= new_attr['config_attributes']['type'] if new_attr.key?('config_attributes')
  new_attr_type ||= type
  logger.debug("new_typed_vm(#{type}): new_attr_type=#{new_attr_type}")
  logger.debug("new_typed_vm(#{type}): new_attr=#{new_attr}'")
  options = (!new_attr.key?('vmid') || ForemanFogProxmox::Value.empty?(new_attr['vmid'])) ? vm_typed_instance_defaults(type).merge(new_attr).merge(type: type) : new_attr
  logger.debug("new_typed_vm(#{type}): options=#{options}")
  vm_h = parse_typed_vm(options, type).deep_symbolize_keys
  logger.debug("new_typed_vm(#{type}): vm_h=#{vm_h}")
  vm_h = vm_h.merge(vm_typed_instance_defaults(type)) if vm_h.empty?
  logger.debug(format(_('new_typed_vm(%<type>s) with vm_typed_instance_defaults: vm_h=%<vm_h>s'), type: type, vm_h: vm_h))
  node.send(vm_collection(type)).new(vm_h)
end

#new_typed_volume(attr, vm_type, volume_type) ⇒ Object



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

def new_typed_volume(attr, vm_type, volume_type)
  volume_defaults = hard_disk_typed_defaults(vm_type) if ['hard_disk', 'rootfs', 'mp'].include?(volume_type)
  volume_defaults = cdrom_defaults if volume_type == 'cdrom'
  volume_defaults = cloudinit_defaults if volume_type == 'cloud_init'
  opts = volume_defaults.merge(attr.to_h).deep_symbolize_keys
  opts = ForemanFogProxmox::HashCollection.new_hash_transform_values(opts, :to_s)
  Fog::Proxmox::Compute::Disk.new(opts)
end

#new_vm(new_attr = {}) ⇒ Object



165
166
167
168
169
170
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 165

def new_vm(new_attr = {})
  new_attr = ActiveSupport::HashWithIndifferentAccess.new(new_attr)
  type = new_attr['type']
  type ||= 'qemu'
  new_typed_vm(new_attr, type)
end

#new_volume(attr = {}) ⇒ Object



67
68
69
70
71
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 67

def new_volume(attr = {})
  type = attr['type']
  type ||= 'qemu'
  new_typed_volume(attr, type, 'hard_disk')
end

#next_vmidObject



109
110
111
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 109

def next_vmid
  default_node.servers.next_id
end

#vm_instance_defaultsObject



130
131
132
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 130

def vm_instance_defaults
  super.merge(vmid: next_vmid, node_id: default_node_id, type: 'qemu')
end

#vm_typed_instance_defaults(type) ⇒ Object



134
135
136
137
138
139
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 134

def vm_typed_instance_defaults(type)
  defaults = vm_instance_defaults
  defaults = defaults.merge(config_attributes: config_attributes(type))
  defaults = add_default_typed_volume(defaults)
  add_default_typed_interface(type, defaults)
end