Class: ForemanFogProxmox::Proxmox
Constant Summary
ProxmoxContainerHelper::GIGA, ProxmoxContainerHelper::KILO, ProxmoxContainerHelper::MEGA
ProxmoxServerHelper::GIGA, ProxmoxServerHelper::KILO, ProxmoxServerHelper::MEGA
ProxmoxVmHelper::GIGA, ProxmoxVmHelper::KILO, ProxmoxVmHelper::MEGA
Class Method Summary
collapse
Instance Method Summary
collapse
#add_container_interface, #parse_container_cpu, #parse_container_interfaces, #parse_container_memory, #parse_container_ostemplate, #parse_container_vm, #parse_container_volume, #parse_container_volumes
#add_server_interface, #parse_server_cdrom, #parse_server_cpu, #parse_server_interfaces, #parse_server_memory, #parse_server_vm, #parse_server_volume, #parse_server_volumes
#add_cdrom_to_config_server, #convert_memory_size, #convert_sizes, #disk_to_cdrom, #object_to_config_hash, #remove_deletes
Class Method Details
.model_name ⇒ Object
51
52
53
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 51
def self.model_name
ComputeResource.model_name
end
|
.provider_friendly_name ⇒ Object
43
44
45
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 43
def self.provider_friendly_name
"Proxmox"
end
|
Instance Method Details
#associated_host(vm) ⇒ Object
98
99
100
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 98
def associated_host(vm)
associate_by('mac', vm.mac)
end
|
#available_images ⇒ Object
108
109
110
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 108
def available_images
templates.collect { |template| OpenStruct.new(id: template.vmid) }
end
|
#bridges ⇒ Object
102
103
104
105
106
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 102
def bridges
node = network_client.nodes.get node_id
bridges = node.networks.all(type: 'any_bridge')
bridges.sort_by(&:iface)
end
|
#capabilities ⇒ Object
47
48
49
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 47
def capabilities
[:build, :new_volume, :new_interface, :image]
end
|
#certs_to_store ⇒ Object
405
406
407
408
409
410
411
412
413
414
415
416
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 405
def certs_to_store
return if ssl_certs.blank?
store = OpenSSL::X509::Store.new
ssl_certs.split(/(?=-----BEGIN)/).each do |cert|
x509_cert = OpenSSL::X509::Certificate.new cert
store.add_cert x509_cert
end
store
rescue => e
logger.error(e)
raise ::Foreman::Exception.new N_("Unable to store X509 certificates")
end
|
#connection_options ⇒ Object
426
427
428
429
430
431
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 426
def connection_options
opts = http_proxy ? {proxy: http_proxy.full_url} : {disable_proxy: 1}
opts.store(:ssl_verify_peer, ssl_verify_peer)
opts.store(:ssl_cert_store, certs_to_store) if Foreman::Cast.to_bool(ssl_verify_peer)
opts
end
|
#console(uuid) ⇒ Object
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 433
def console(uuid)
vm = find_vm_by_uuid(uuid)
options = {}
if vm.container?
type_console = 'vnc'
options.store(:console, type_console)
else
type_console = vm.config.type_console
end
options.store(:websocket, 1) if type_console == 'vnc'
begin
vnc_console = vm.start_console(options)
WsProxy.start(:host => host, :host_port => vnc_console['port'], :password => vnc_console['ticket']).merge(:name => vm.name, :type => type_console)
rescue => e
logger.error(e)
raise ::Foreman::Exception.new(_("%s console is not supported at this time") % type_console)
end
end
|
#create_vm(args = {}) ⇒ Object
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 253
def create_vm(args = {})
vmid = args[:vmid].to_i
type = args[:type]
raise ::Foreman::Exception.new N_("invalid vmid=%{vmid}") % { vmid: vmid } unless node.servers.id_valid?(vmid)
image_id = args[:image_id]
if image_id
logger.debug(_("create_vm(): clone %{image_id} in %{vmid}") % { image_id: image_id, vmid: vmid })
image = node.servers.get image_id
image.clone(vmid)
clone = node.servers.get vmid
clone.update(name: args[:name])
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| %w[ostemplate_storage ostemplate_file].include? key })
end
end
rescue => e
logger.warn(_("failed to create vm: %{e}") % { e: e })
destroy_vm vm.id if vm
raise e
end
|
#credentials_valid? ⇒ Boolean
55
56
57
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 55
def credentials_valid?
errors[:url].empty? && errors[:user].empty? && errors[:user].include?('@') && errors[:password].empty? && errors[:node_id].empty?
end
|
#editable_network_interfaces? ⇒ Boolean
320
321
322
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 320
def editable_network_interfaces?
true
end
|
#find_vm_by_uuid(uuid) ⇒ Object
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 282
def find_vm_by_uuid(uuid)
begin
vm = node.servers.get(uuid)
rescue Fog::Errors::NotFound
vm = nil
rescue Fog::Errors::Error => e
Foreman::Logging.exception(_("Failed retrieving proxmox server vm by vmid=%{uuid}") % { vmid: uuid }, e)
raise(ActiveRecord::RecordNotFound)
end
begin
vm = node.containers.get(uuid) unless vm
rescue Fog::Errors::NotFound
vm = nil
rescue Fog::Errors::Error => e
Foreman::Logging.exception(_("Failed retrieving proxmox container vm by vmid=%{uuid}") % { vmid: uuid }, e)
raise(ActiveRecord::RecordNotFound)
end
vm
end
|
#host_compute_attrs(host) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 122
def host_compute_attrs(host)
super.tap do |attrs|
ostype = host.compute_attributes['config_attributes']['ostype']
type = host.compute_attributes['type']
case type
when 'lxc'
host.compute_attributes['config_attributes'].store('hostname',host.name)
when 'qemu'
raise ::Foreman::Exception.new(_("Operating system family %{type} is not consistent with %{ostype}") % { type: host.operatingsystem.type, ostype: ostype }) unless compute_os_types(host).include?(ostype)
end
end
end
|
#host_interfaces_attrs(host) ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 135
def host_interfaces_attrs(host)
host.interfaces.select(&:physical?).each.with_index.reduce({}) do |hash, (nic, index)|
nic.identifier = "net%{index}" % {index: index} if nic.identifier.empty?
raise ::Foreman::Exception.new _("Invalid identifier interface[%{index}]. Must be net[n] with n integer >= 0" % { index: index }) unless Fog::Proxmox::NicHelper.nic?(nic.identifier)
container = host.compute_attributes['type'] == 'lxc'
nic.compute_attributes['name'] = "eth%{index}" % {index: index} if container && nic.compute_attributes['name'].empty?
raise ::Foreman::Exception.new _("Invalid name interface[%{index}]. Must be eth[n] with n integer >= 0" % { index: index }) if container && !/^(eth)(\d+)$/.match?(nic.compute_attributes['name'])
nic_compute_attributes = nic.compute_attributes.merge(id: nic.identifier)
mac = nic.mac
mac = nic.attributes['mac'] unless mac
nic_compute_attributes.store(:macaddr, mac) if (mac && !mac.empty?)
interface_compute_attributes = host.compute_attributes['interfaces_attributes'].select { |_k,v| v['id'] == nic.identifier }
nic_compute_attributes.store(:_delete, interface_compute_attributes[interface_compute_attributes.keys[0]]['_delete']) unless interface_compute_attributes.empty?
nic_compute_attributes.store(:ip, nic.ip) if (nic.ip && !nic.ip.empty?)
nic_compute_attributes.store(:ip6, nic.ip6) if (nic.ip6 && !nic.ip6.empty?)
hash.merge(index.to_s => nic_compute_attributes)
end
end
|
#image_exists?(image) ⇒ Boolean
328
329
330
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 328
def image_exists?(image)
!find_vm_by_uuid(image).nil?
end
|
#images_by_storage(type = 'iso', storage_id) ⇒ Object
93
94
95
96
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 93
def images_by_storage(type = 'iso', storage_id)
storage = node.storages.get storage_id if storage_id
storage.volumes.list_by_content_type(type).sort_by(&:volid) if storage
end
|
#new_container_interface(attr = {}) ⇒ Object
196
197
198
199
200
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 196
def new_container_interface(attr = {})
logger.debug("new_container_interface")
opts = interface_container_defaults.merge(attr.to_h).deep_symbolize_keys
Fog::Proxmox::Compute::Interface.new(opts)
end
|
#new_container_vm(new_attr = {}) ⇒ Object
235
236
237
238
239
240
241
242
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 235
def new_container_vm(new_attr = {})
options = new_attr
options = options.merge(node_id: node_id).merge(type: 'lxc').merge(vmid: next_vmid)
options= vm_container_instance_defaults.merge(options) if new_attr.empty?
vm = node.containers.new(parse_container_vm(options).deep_symbolize_keys)
logger.debug(_("new_container_vm() vm.config=%{config}") % { config: vm.config.inspect })
vm
end
|
#new_interface(attr = {}) ⇒ Object
179
180
181
182
183
184
185
186
187
188
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 179
def new_interface(attr = {})
type = attr['type']
type = 'qemu' unless type
case type
when 'lxc'
return new_container_interface(attr)
when 'qemu'
return new_server_interface(attr)
end
end
|
#new_server_interface(attr = {}) ⇒ Object
190
191
192
193
194
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 190
def new_server_interface(attr = {})
logger.debug("new_server_interface")
opts = interface_server_defaults.merge(attr.to_h).deep_symbolize_keys
Fog::Proxmox::Compute::Interface.new(opts)
end
|
#new_server_vm(new_attr = {}) ⇒ Object
244
245
246
247
248
249
250
251
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 244
def new_server_vm(new_attr = {})
options = new_attr
options = options.merge(node_id: node_id).merge(type: 'qemu').merge(vmid: next_vmid)
options = vm_server_instance_defaults.merge(options) if new_attr.empty?
vm = node.servers.new(parse_server_vm(options).deep_symbolize_keys)
logger.debug(_("new_server_vm() vm.config=%{config}") % { config: vm.config.inspect })
vm
end
|
#new_vm(new_attr = {}) ⇒ Object
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 221
def new_vm(new_attr = {})
new_attr = ActiveSupport::HashWithIndifferentAccess.new(new_attr)
type = new_attr['type']
type = 'qemu' unless type
case type
when 'lxc'
vm = new_container_vm(new_attr)
when 'qemu'
vm = new_server_vm(new_attr)
end
logger.debug(_("new_vm() vm.config=%{config}") % { config: vm.config.inspect })
vm
end
|
#new_volume(attr = {}) ⇒ Object
156
157
158
159
160
161
162
163
164
165
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 156
def new_volume(attr = {})
type = attr['type']
type = 'qemu' unless type
case type
when 'lxc'
return new_volume_server(attr)
when 'qemu'
return new_volume_container(attr)
end
end
|
#new_volume_container(attr = {}) ⇒ Object
173
174
175
176
177
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 173
def new_volume_container(attr = {})
opts = volume_container_defaults.merge(attr.to_h).deep_symbolize_keys
opts[:size] = opts[:size].to_s
Fog::Proxmox::Compute::Disk.new(opts)
end
|
#new_volume_server(attr = {}) ⇒ Object
167
168
169
170
171
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 167
def new_volume_server(attr = {})
opts = volume_server_defaults.merge(attr.to_h).deep_symbolize_keys
opts[:size] = opts[:size].to_s
Fog::Proxmox::Compute::Disk.new(opts)
end
|
#next_vmid ⇒ Object
381
382
383
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 381
def next_vmid
node.servers.next_id
end
|
#node ⇒ Object
393
394
395
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 393
def node
client.nodes.get node_id
end
|
#node_id ⇒ Object
385
386
387
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 385
def node_id
self.attrs[:node_id]
end
|
#node_id=(value) ⇒ Object
389
390
391
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 389
def node_id=(value)
self.attrs[:node_id] = value
end
|
#nodes ⇒ Object
78
79
80
81
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 78
def nodes
nodes = client.nodes.all if client
nodes.sort_by(&:node) if nodes
end
|
#pools ⇒ Object
83
84
85
86
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 83
def pools
pools = identity_client.pools.all
pools.sort_by(&:poolid)
end
|
#provided_attributes ⇒ Object
37
38
39
40
41
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 37
def provided_attributes
super.merge(
:mac => :mac
)
end
|
#save_vm(uuid, new_attributes) ⇒ Object
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 363
def save_vm(uuid, new_attributes)
vm = find_vm_by_uuid(uuid)
templated = new_attributes['templated']
if (templated == '1' && !vm.templated?)
vm.create_template
else
volumes_attributes = new_attributes['volumes_attributes']
save_volumes(vm, volumes_attributes)
parsed_attr = vm.container? ? parse_container_vm(new_attributes.merge(type: vm.type)) : parse_server_vm(new_attributes.merge(type: vm.type))
config_attributes = parsed_attr.reject { |key,_value| [:templated,:ostemplate,:ostemplate_file,:ostemplate_storage,:volumes_attributes].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))
end
vm = find_vm_by_uuid(uuid)
end
|
#save_volumes(vm, volumes_attributes) ⇒ Object
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 332
def save_volumes(vm, volumes_attributes)
if volumes_attributes
volumes_attributes.each_value do |volume_attributes|
id = volume_attributes['id']
disk = vm.config.disks.get(id)
delete = volume_attributes['_delete']
if disk
if delete == '1'
vm.detach(id)
device = Fog::Proxmox::DiskHelper.(id)
vm.detach('unused' + device.to_s)
else
diff_size = volume_attributes['size'].to_i - disk.size
raise ::Foreman::Exception.new(_("Unable to shrink %{id} size. Proxmox allows only increasing size.") % { id: id }) unless diff_size >= 0
if diff_size > 0
extension = '+' + (diff_size / GIGA).to_s + 'G'
vm.extend(id,extension)
elsif disk.storage != volume_attributes['storage']
vm.move(id,volume_attributes['storage'])
end
end
else
options = {}
options.store(:mp, volume_attributes['mp']) if vm.container?
disk_attributes = { id: id, storage: volume_attributes['storage'], size: (volume_attributes['size'].to_i / GIGA).to_s }
vm.attach(disk_attributes, options) unless delete == '1'
end
end
end
end
|
#ssl_certs ⇒ Object
397
398
399
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 397
def ssl_certs
self.attrs[:ssl_certs]
end
|
#ssl_certs=(value) ⇒ Object
401
402
403
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 401
def ssl_certs=(value)
self.attrs[:ssl_certs] = value
end
|
#ssl_verify_peer ⇒ Object
418
419
420
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 418
def ssl_verify_peer
self.attrs[:ssl_verify_peer].blank? ? false : Foreman::Cast.to_bool(self.attrs[:ssl_verify_peer])
end
|
#ssl_verify_peer=(value) ⇒ Object
422
423
424
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 422
def ssl_verify_peer=(value)
self.attrs[:ssl_verify_peer] = value
end
|
#storages(type = 'images') ⇒ Object
88
89
90
91
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 88
def storages(type = 'images')
storages = node.storages.list_by_content_type type
storages.sort_by(&:storage)
end
|
#supports_update? ⇒ Boolean
302
303
304
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 302
def supports_update?
true
end
|
#template(vmid) ⇒ Object
118
119
120
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 118
def template(vmid)
find_vm_by_uuid(vmid)
end
|
#templates ⇒ Object
112
113
114
115
116
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 112
def templates
storage = storages.first
images = storage.volumes.list_by_content_type('images')
images.select { |image| image.templated? }
end
|
#test_connection(options = {}) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 65
def test_connection(options = {})
super
credentials_valid?
version_suitable?
rescue => e
errors[:base] << e.message
if e.message.include?('SSL')
errors[:ssl_certs] << e.message
else
errors[:url] << e.message
end
end
|
#update_required?(old_attrs, new_attrs) ⇒ Boolean
306
307
308
309
310
311
312
313
314
315
316
317
318
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 306
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' end if new_attrs[:interfaces_attributes]
new_attrs[:volumes_attributes].each do |key, volume|
return true if (volume[:id].blank? || volume[:_delete] == '1') && key != 'new_volumes' end if new_attrs[:volumes_attributes]
false
end
|
#user_data_supported? ⇒ Boolean
324
325
326
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 324
def user_data_supported?
true
end
|
#version ⇒ Object
452
453
454
455
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 452
def version
v = identity_client.read_version
"#{v['version']}.#{v['release']}"
end
|
#version_suitable? ⇒ Boolean
#vm_compute_attributes(vm) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 202
def vm_compute_attributes(vm)
vm_attrs = {}
if vm.respond_to?(:config)
vm_attrs = vm_attrs.merge(vmid: vm.identity, node_id: vm.node_id, type: vm.type)
if vm.config.respond_to?(:disks)
vm_attrs[:volumes_attributes] = Hash[vm.config.disks.each_with_index.map { |disk, idx| [idx.to_s, disk.attributes] }]
end
if vm.config.respond_to?(:interfaces)
vm_attrs[:interfaces_attributes] = Hash[vm.config.interfaces.each_with_index.map { |interface, idx| [idx.to_s, interface.attributes] }]
end
vm_attrs[:config_attributes] = vm.config.attributes.reject { |key,value| [:disks, :interfaces, :vmid, :node_id, :node, :type].include?(key) || !vm.config.respond_to?(key) || ForemanFogProxmox::Value.empty?(value.to_s) || Fog::Proxmox::DiskHelper.disk?(key.to_s) || Fog::Proxmox::NicHelper.nic?(key.to_s) }
end
vm_attrs
end
|
#vms(opts = {}) ⇒ Object
217
218
219
|
# File 'app/models/foreman_fog_proxmox/proxmox.rb', line 217
def vms(opts = {})
node
end
|