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 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

Instance Method Details

#add_default_typed_interface(type, new_attr) ⇒ Object



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

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



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

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

#cdrom_defaultsObject



29
30
31
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 29

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

#cloudinit_defaultsObject



33
34
35
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 33

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

#config_attributes(type = 'qemu') ⇒ Object



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

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



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

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



99
100
101
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 99

def default_node
  nodes.first
end

#default_node_idObject



103
104
105
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 103

def default_node_id
  default_node.node
end

#hard_disk_typed_defaults(vm_type) ⇒ Object



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_new.rb', line 37

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' }
    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



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

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



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

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



93
94
95
96
97
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 93

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

#new_typed_interface(attr, type) ⇒ Object



88
89
90
91
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 88

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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 185

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
  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



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

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



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

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



65
66
67
68
69
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 65

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

#next_vmidObject



107
108
109
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 107

def next_vmid
  default_node.servers.next_id
end

#vm_instance_defaultsObject



128
129
130
# File 'app/models/foreman_fog_proxmox/proxmox_vm_new.rb', line 128

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

#vm_typed_instance_defaults(type) ⇒ Object



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

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