foreman_fog_proxmox 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -0
  3. data/app/controllers/foreman_fog_proxmox/compute_resources_controller.rb +55 -14
  4. data/app/helpers/proxmox_vm_attrs_helper.rb +20 -15
  5. data/app/helpers/proxmox_vm_config_attrs_helper.rb +80 -0
  6. data/app/helpers/proxmox_vm_config_helper.rb +18 -5
  7. data/app/helpers/proxmox_vm_helper.rb +8 -0
  8. data/app/helpers/proxmox_vm_image_template_helper.rb +16 -0
  9. data/app/helpers/proxmox_vm_options_helper.rb +60 -0
  10. data/app/models/concerns/fog_extensions/proxmox/server.rb +1 -1
  11. data/app/models/concerns/orchestration/proxmox/compute.rb +6 -0
  12. data/app/models/foreman_fog_proxmox/proxmox_compute_attributes.rb +2 -0
  13. data/app/models/foreman_fog_proxmox/proxmox_images.rb +10 -28
  14. data/app/models/foreman_fog_proxmox/proxmox_rngs.rb +36 -0
  15. data/app/models/foreman_fog_proxmox/proxmox_vm_commands.rb +8 -9
  16. data/app/models/foreman_fog_proxmox/proxmox_vm_new.rb +7 -1
  17. data/app/models/foreman_fog_proxmox/proxmox_vm_queries.rb +31 -3
  18. data/app/services/foreman_fog_proxmox/node_dashboard/data.rb +4 -2
  19. data/app/views/compute_resources_vms/form/proxmox/_base.html.erb +1 -1
  20. data/lib/foreman_fog_proxmox/engine.rb +1 -1
  21. data/lib/foreman_fog_proxmox/version.rb +1 -1
  22. data/package.json +4 -18
  23. data/test/factories/foreman_fog_proxmox/proxmox_container_mock_factory.rb +10 -5
  24. data/test/factories/foreman_fog_proxmox/proxmox_node_mock_factory.rb +5 -0
  25. data/test/factories/foreman_fog_proxmox/proxmox_server_mock_factory.rb +1 -0
  26. data/test/functional/compute_resources_controller_test.rb +216 -10
  27. data/test/{unit/foreman_fog_proxmox → functional}/hosts_controller_test.rb +2 -2
  28. data/test/unit/foreman_fog_proxmox/helpers/proxmox_vm_attrs_helper_test.rb +104 -0
  29. data/test/unit/foreman_fog_proxmox/helpers/proxmox_vm_config_helper_test.rb +48 -0
  30. data/test/unit/foreman_fog_proxmox/helpers/proxmox_vm_helper_test.rb +24 -0
  31. data/test/unit/foreman_fog_proxmox/helpers/proxmox_vm_options_helper_test.rb +135 -0
  32. data/test/unit/foreman_fog_proxmox/proxmox_compute_attributes_test.rb +15 -0
  33. data/test/unit/foreman_fog_proxmox/proxmox_images_test.rb +130 -9
  34. data/test/unit/foreman_fog_proxmox/proxmox_orchestration_compute_test.rb +2 -0
  35. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_container_test.rb +2 -1
  36. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_create_test.rb +24 -3
  37. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_update_cdrom_test.rb +4 -0
  38. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_update_cloudinit_test.rb +2 -0
  39. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_update_hard_disk_test.rb +5 -0
  40. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_update_test.rb +47 -1
  41. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_test.rb +23 -0
  42. data/test/unit/foreman_fog_proxmox/proxmox_vm_new_test.rb +20 -0
  43. data/test/unit/foreman_fog_proxmox/proxmox_vm_queries_test.rb +114 -2
  44. data/webpack/components/GeneralTabContent.js +57 -2
  45. data/webpack/components/ProxmoxComputeSelectors.js +13 -0
  46. data/webpack/components/ProxmoxContainer/ProxmoxContainerStorage.js +1 -1
  47. data/webpack/components/ProxmoxServer/ProxmoxServerHardware.js +5 -1
  48. data/webpack/components/ProxmoxServer/ProxmoxServerOptions.js +91 -2
  49. data/webpack/components/ProxmoxServer/ProxmoxServerStorage.js +12 -17
  50. data/webpack/components/ProxmoxServer/components/EFIDisk.js +1 -1
  51. data/webpack/components/ProxmoxServer/components/HardDisk.js +1 -1
  52. data/webpack/components/ProxmoxServer/components/Rng.js +124 -0
  53. data/webpack/components/ProxmoxVmType.js +65 -3
  54. data/webpack/components/__tests__/GeneralTabContent.test.js +195 -14
  55. data/webpack/components/__tests__/StorageContentFiltering.test.js +156 -0
  56. data/webpack/components/common/FormInputs.js +10 -1
  57. data/webpack/components/common/WarningMessage.js +32 -0
  58. data/webpack/index.js +1 -0
  59. data/webpack/stylesheets/proxmox_server_options.css +51 -0
  60. data/webpack/test_setup.js +2 -13
  61. metadata +16 -4
  62. data/webpack/global_test_setup.js +0 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e72cd6c74e6250ab29a3e7fcd79f77251e2997e2b309c477e16718ddb6599ef5
4
- data.tar.gz: f8dd7ad601d77d25a4530e8cd29dc940f2c285ece0c45f58ff0398c5bfead2bf
3
+ metadata.gz: b2c892cc8490efafa67f87ecc9925430c572b522655ae899875d8dad29a5472b
4
+ data.tar.gz: 3dc96843a9cf02060990e72b8faf56166bb343604d760dbbdd496d5fd5ed82e7
5
5
  SHA512:
6
- metadata.gz: 2ef10ad730bde03e902a9e7780c4637d857678e083f7c35cda505d1dac4d5812b55a10a4925ad823c0fd0218f3678e477bacf7bd85b845993ca05f6fcb3bef26
7
- data.tar.gz: 4ea8aebed65ffb32547a020ee981ab8b1ce4bfae235a5d88751b2ef2e7bf37672b94f443ae8f9415662ade4179fb80cd31cc1476864c36784673619cfd315182
6
+ metadata.gz: 686f5a2012b3920f324cca3f7b56c2d228786ac7a924c65610bab3baa778ef5a8604142e8d3ff20cab5f5f59d65ac4735fa65b34680482de5834a2d653632f6d
7
+ data.tar.gz: 180236f2bc664083cd6ea20c3f877682260525f8a14991dd51d53ecc971469fa695e2105a6801620b36ade54bf7d360c742620218634cac8e71aa71994eb0540
data/README.md CHANGED
@@ -320,6 +320,20 @@ bundle exec foreman start
320
320
 
321
321
  See details in [foreman plugin development](https://projects.theforeman.org/projects/foreman/wiki/How_to_Create_a_Plugin)
322
322
 
323
+ ### JavaScript lint and tests
324
+
325
+ Plugin JavaScript is linted and tested through [Foreman](https://github.com/theforeman/foreman) core (not `tfm-test` / `tfm-lint` in this repository).
326
+
327
+ From the Foreman application directory (with this plugin linked, e.g. via `bundler.d/foreman_fog_proxmox.local.rb`):
328
+
329
+ ```bash
330
+ cd /path/to/foreman-core
331
+ npm run lint:plugins foreman_fog_proxmox
332
+ npm run test:plugins foreman_fog_proxmox
333
+ npm run test:plugins foreman_fog_proxmox -- --watchAll
334
+ npm run test:plugins foreman_fog_proxmox -- path/to/test.js
335
+ ```
336
+
323
337
  ## Contributing
324
338
 
325
339
  If you like it and still want to improve it, [sponsor me](https://github.com/sponsors/tristanrobert)!
@@ -84,7 +84,7 @@ module ForemanFogProxmox
84
84
  node_id = params[:node_id]
85
85
  storage = params[:storage]
86
86
 
87
- vols = cr.storages(node_id).find { |s| s.storage == storage }&.volumes || []
87
+ vols = cr.storages(node_id, nil).find { |s| s.storage == storage }&.volumes || []
88
88
 
89
89
  render json: Array(vols).map { |v|
90
90
  h = v.respond_to?(:as_json) ? v.as_json : v
@@ -95,34 +95,71 @@ module ForemanFogProxmox
95
95
  # GET foreman_fog_proxmox/metadata/:compute_resource_id
96
96
  def metadata
97
97
  cr = ComputeResource.find(params[:compute_resource_id])
98
- nodes = Array(cr.nodes)
99
-
100
- render json: {
101
- nodes: extract_nodes(nodes),
102
- pools: extract_pools(cr),
103
- storages: extract_storages(cr, nodes),
104
- bridges: extract_bridges(cr, nodes),
105
- images: extract_images(cr),
106
- }
98
+ node_metadata = extract_node_metadata(cr)
99
+ failed_metadata_sections = node_metadata[:failed_metadata_sections]
100
+
101
+ render json: node_metadata.merge(
102
+ pools: extract_pools(cr, failed_metadata_sections),
103
+ images: extract_images(cr, failed_metadata_sections),
104
+ failed_metadata_sections: failed_metadata_sections
105
+ )
107
106
  end
108
107
 
109
108
  private
110
109
 
110
+ def extract_node_metadata(compute_resource)
111
+ metadata = { nodes: [], storages: [], bridges: [], skipped_nodes: [], failed_metadata_sections: [] }
112
+
113
+ Array(compute_resource.nodes).each do |node|
114
+ node_id = node.node
115
+ storages = begin
116
+ extract_storages(compute_resource, [node])
117
+ rescue StandardError => e
118
+ Foreman::Logging.exception("Failed to load Proxmox storages for node #{node_id}", e)
119
+ metadata[:failed_metadata_sections] << "storages:#{node_id}"
120
+ nil
121
+ end
122
+
123
+ bridges = begin
124
+ extract_bridges(compute_resource, [node])
125
+ rescue StandardError => e
126
+ Foreman::Logging.exception("Failed to load Proxmox bridges for node #{node_id}", e)
127
+ metadata[:failed_metadata_sections] << "bridges:#{node_id}"
128
+ nil
129
+ end
130
+
131
+ if storages.nil? && bridges.nil?
132
+ metadata[:skipped_nodes] << node_id
133
+ next
134
+ end
135
+
136
+ metadata[:nodes].concat(extract_nodes([node]))
137
+ metadata[:storages].concat(storages || [])
138
+ metadata[:bridges].concat(bridges || [])
139
+ end
140
+
141
+ metadata
142
+ end
143
+
111
144
  def extract_nodes(nodes)
112
- nodes.map { |n| { node: n.node } }
145
+ nodes.map { |n| { node: n.node, status: n.status } }
113
146
  end
114
147
 
115
- def extract_pools(compute_resource)
148
+ def extract_pools(compute_resource, failed_metadata_sections)
116
149
  Array(compute_resource.pools).map do |p|
117
150
  poolid = p.respond_to?(:poolid) ? p.poolid : (p[:poolid] || p['poolid'])
118
151
  { poolid: poolid }
119
152
  end
153
+ rescue StandardError => e
154
+ Foreman::Logging.exception("Failed to load Proxmox pools for compute resource #{compute_resource.id}", e)
155
+ failed_metadata_sections << 'pools'
156
+ []
120
157
  end
121
158
 
122
159
  def extract_storages(compute_resource, nodes)
123
160
  nodes.flat_map do |node|
124
161
  node_id = node.node
125
- Array(compute_resource.storages(node_id)).map do |storage|
162
+ Array(compute_resource.storages(node_id, nil)).map do |storage|
126
163
  h = storage.respond_to?(:as_json) ? storage.as_json : storage
127
164
  {
128
165
  storage: (h[:storage] || h['storage']),
@@ -150,7 +187,7 @@ module ForemanFogProxmox
150
187
  end
151
188
  end
152
189
 
153
- def extract_images(compute_resource)
190
+ def extract_images(compute_resource, failed_metadata_sections)
154
191
  images = compute_resource.images
155
192
  images = images.order(:name) if images.respond_to?(:order)
156
193
 
@@ -164,6 +201,10 @@ module ForemanFogProxmox
164
201
  disks: image_disks(compute_resource, uuid),
165
202
  }
166
203
  end
204
+ rescue StandardError => e
205
+ Foreman::Logging.exception("Failed to load Proxmox images for compute resource #{compute_resource.id}", e)
206
+ failed_metadata_sections << 'images'
207
+ []
167
208
  end
168
209
 
169
210
  def image_disks(compute_resource, uuid)
@@ -25,10 +25,28 @@ require 'foreman_fog_proxmox/hash_collection'
25
25
 
26
26
  # Convert a foreman form server hash into a fog-proxmox server attributes hash
27
27
  module ProxmoxVMAttrsHelper
28
+ include ProxmoxVMConfigAttrsHelper
29
+
28
30
  def object_to_attributes_hash(vms, from_profile, start_checked)
29
31
  param_scope = from_profile ? "compute_attribute[vm_attrs]" : "host[compute_attributes]"
32
+ vm_h = base_vm_attrs(param_scope, vms)
33
+
34
+ vm_h.merge!(additional_attrs(vms, param_scope, start_checked))
35
+ vm_h[:interfaces] = network_attrs(param_scope, vms.interfaces)
36
+ vm_h[:disks] = volumes_attrs(param_scope, vms.volumes)
37
+ vm_h[:efidisk] = efidisk_attrs(param_scope, vms.efidisk) if vms.efidisk.present?
38
+ vm_h[:rng] = rng_attrs(param_scope, vms.config.rng) if vms.config.rng.present?
39
+ vm_h.merge!(cpu_flags_attrs(param_scope, vms.config))
40
+ if vms.type == 'qemu'
41
+ vm_h.merge!(hotplug_attrs(param_scope, vms.config))
42
+ vm_h.merge!(agent_attrs(param_scope, vms.config))
43
+ end
44
+ vm_h
45
+ end
46
+
47
+ def base_vm_attrs(param_scope, vms)
30
48
  vm_h = ActiveSupport::HashWithIndifferentAccess.new
31
- keys = [:vmid, :node_id, :type, :pool, :efidisk]
49
+ keys = [:vmid, :node_id, :type, :pool, :efidisk, :rng]
32
50
  main = vms.attributes.select { |key, _value| keys.include? key }
33
51
  vms.config.all_attributes.each do |key, value|
34
52
  camel_key = key.to_s.include?('_') ? snake_to_camel(key.to_s).to_sym : key
@@ -38,23 +56,9 @@ module ProxmoxVMAttrsHelper
38
56
  camel_key = key.to_s.include?('_') ? snake_to_camel(key.to_s).to_sym : key
39
57
  vm_h[camel_key] = { :name => "#{param_scope}[#{key}]", :value => value } if keys.include? key
40
58
  end
41
-
42
- vm_h.merge!(additional_attrs(vms, param_scope, start_checked))
43
- vm_h[:interfaces] = network_attrs(param_scope, vms.interfaces)
44
- vm_h[:disks] = volumes_attrs(param_scope, vms.volumes)
45
- vm_h[:efidisk] = efidisk_attrs(param_scope, vms.efidisk) if vms.efidisk.present?
46
- vm_h.merge!(cpu_flags_attrs(param_scope, vms.config))
47
59
  vm_h
48
60
  end
49
61
 
50
- def cpu_flags_attrs(param_scope, config)
51
- flag_attrs = ActiveSupport::HashWithIndifferentAccess.new
52
- Fog::Proxmox::CpuHelper.flags.each_key do |key|
53
- flag_attrs.merge!({ key => { :name => "#{param_scope}[config_attributes][#{key}]", :value => config.public_send(key) } })
54
- end
55
- flag_attrs
56
- end
57
-
58
62
  def efidisk_attrs(param_scope, efidisk)
59
63
  attrs = ActiveSupport::HashWithIndifferentAccess.new
60
64
  keys = ['id', 'volid', 'size', 'storage', 'format', 'efitype', 'pre_enrolled_keys']
@@ -132,6 +136,7 @@ module ProxmoxVMAttrsHelper
132
136
  ostemplate_file: vms.ostemplate_file,
133
137
  iso_upload_storage: vms.iso_upload_storage,
134
138
  start: start_checked,
139
+ full_clone: vms.full_clone,
135
140
  templated: vms.templated,
136
141
  is_secure_boot: vms.config.secure_boot?,
137
142
  }
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is part of ForemanFogProxmox.
4
+
5
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+
10
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ module ProxmoxVMConfigAttrsHelper
19
+ include ProxmoxVMConfigHelper
20
+
21
+ def rng_attrs(param_scope, rng)
22
+ attrs = ActiveSupport::HashWithIndifferentAccess.new
23
+ keys = ['max_bytes', 'period', 'source']
24
+ keys.each do |key|
25
+ attrs[key] = config_field_attrs(param_scope, key, rng.public_send(key), 'rng_attributes').values.first
26
+ end
27
+ attrs
28
+ end
29
+
30
+ def agent_attrs(param_scope, config)
31
+ settings = Fog::Proxmox::AgentHelper.extract(config.agent)
32
+ freeze_fs = settings['freeze-fs-on-backup']
33
+ values = {
34
+ 'agent' => bool_to_proxmox_flag(settings['enabled']),
35
+ 'agent_freeze_fs' => bool_to_proxmox_flag(freeze_fs),
36
+ 'agent_fstrim_cloned_disks' => bool_to_proxmox_flag(settings['fstrim_cloned_disks']),
37
+ 'agent_type' => settings['type'],
38
+ }
39
+ values.each_with_object({}) do |(key, value), attrs|
40
+ attrs.merge!(config_field_attrs(param_scope, key, value))
41
+ end
42
+ end
43
+
44
+ def hotplug_attrs(param_scope, config)
45
+ settings = Fog::Proxmox::HotplugHelper.extract(config.hotplug)
46
+ hotplug_enabled = settings[:enabled]
47
+ enabled_devices = settings[:devices]
48
+
49
+ attrs = {
50
+ 'hotplug' => {
51
+ name: "#{param_scope}[config_attributes][hotplug]",
52
+ value: hotplug_enabled ? '1' : '0',
53
+ },
54
+ }
55
+
56
+ HOTPLUG_DEVICES.each do |device|
57
+ key = "hotplug_#{device}"
58
+ value = enabled_devices.include?(device) ? '1' : '0'
59
+ attrs.merge!(config_field_attrs(param_scope, key, value))
60
+ end
61
+
62
+ attrs
63
+ end
64
+
65
+ def cpu_flags_attrs(param_scope, config)
66
+ flag_attrs = ActiveSupport::HashWithIndifferentAccess.new
67
+ Fog::Proxmox::CpuHelper.flags.each_key do |key|
68
+ flag_attrs[key] = config_field_attrs(param_scope, key.to_s, config.public_send(key)).values.first
69
+ end
70
+ flag_attrs
71
+ end
72
+
73
+ def config_field_attrs(param_scope, key, value, attributes_key = 'config_attributes')
74
+ {
75
+ snake_to_camel(key) => {
76
+ name: "#{param_scope}[#{attributes_key}][#{key}]", value: value
77
+ },
78
+ }
79
+ end
80
+ end
@@ -20,19 +20,20 @@
20
20
  require 'fog/proxmox/helpers/disk_helper'
21
21
  require 'fog/proxmox/helpers/nic_helper'
22
22
  require 'fog/proxmox/helpers/cpu_helper'
23
+ require 'fog/proxmox/helpers/rng_helper'
23
24
  require 'foreman_fog_proxmox/value'
24
25
  require 'foreman_fog_proxmox/hash_collection'
25
26
 
26
27
  # Convert a foreman form server hash into a fog-proxmox server attributes hash
27
28
  module ProxmoxVMConfigHelper
29
+ include ProxmoxVMOptionsHelper
30
+
28
31
  def object_to_config_hash(vm, type)
29
32
  vm_h = ActiveSupport::HashWithIndifferentAccess.new
30
33
  main_a = ['vmid', 'type']
31
34
  main = vm.attributes.select { |key, _value| main_a.include? key.to_s }
32
35
  main_a += ['templated']
33
- config = vm.config.attributes.reject do |key, _value|
34
- main_a.include?(key.to_s) || Fog::Proxmox::DiskHelper.disk?(key) || Fog::Proxmox::NicHelper.nic?(key)
35
- end
36
+ config = vm.config.attributes.reject { |key, _value| main_a.include?(key.to_s) || Fog::Proxmox::DiskHelper.disk?(key) || Fog::Proxmox::NicHelper.nic?(key) }
36
37
  vm_h = vm_h.merge(main)
37
38
  vm_h = vm_h.merge('config_attributes': config)
38
39
  logger.debug("object_to_config_hash(#{type}): vm_h=#{vm_h}")
@@ -40,8 +41,8 @@ module ProxmoxVMConfigHelper
40
41
  end
41
42
 
42
43
  def general_a(type)
43
- general_a = ['node_id', 'type', 'config_attributes', 'efidisk_attributes', 'volumes_attributes', 'interfaces_attributes', 'image_id']
44
- general_a += ['firmware_type', 'provision_method', 'container_volumes', 'server_volumes', 'start', 'iso_upload_storage']
44
+ general_a = ['node_id', 'type', 'config_attributes', 'efidisk_attributes', 'rng_attributes', 'volumes_attributes', 'interfaces_attributes', 'image_id']
45
+ general_a += ['firmware_type', 'provision_method', 'container_volumes', 'server_volumes', 'start', 'start_after_create', 'iso_upload_storage', 'full_clone']
45
46
  general_a += ['name'] if type == 'lxc'
46
47
  general_a
47
48
  end
@@ -121,10 +122,22 @@ module ProxmoxVMConfigHelper
121
122
  parsed_vm = args.reject { |key, value| args_a(type).include?(key) || ForemanFogProxmox::Value.empty?(value) }
122
123
  parsed_vm = parsed_vm.merge(config_options(config, args, type))
123
124
  parsed_vm = parsed_vm.merge(parsed_config).merge(cpu).merge(memory)
125
+ if type == 'qemu'
126
+ parsed_vm = parse_hotplug(parsed_vm)
127
+ parsed_vm = parse_agent(parsed_vm)
128
+ parsed_vm = parsed_typed_rng(args, parsed_vm)
129
+ end
124
130
  logger.debug("parsed_typed_config(#{type}): parsed_vm=#{parsed_vm}")
125
131
  parsed_vm
126
132
  end
127
133
 
134
+ def parsed_typed_rng(args, parsed_vm)
135
+ rng_attributes = args['rng_attributes'].to_h.symbolize_keys.reject { |_key, value| value.blank? }
136
+ return parsed_vm if rng_attributes.empty?
137
+
138
+ parsed_vm.merge(Fog::Proxmox::RngHelper.flatten(rng_attributes))
139
+ end
140
+
128
141
  def parse_typed_memory(args, type)
129
142
  ForemanFogProxmox::HashCollection.remove_empty_values(args)
130
143
  logger.debug("parse_typed_memory(#{type}): args=#{args}")
@@ -30,6 +30,14 @@ module ProxmoxVMHelper
30
30
  include ProxmoxVMOsTemplateHelper
31
31
  include ProxmoxVMEfidiskHelper
32
32
 
33
+ def normalize_vm_start_attributes(attributes)
34
+ attributes = attributes.with_indifferent_access
35
+ if attributes.key?(:start_after_create) && !attributes.key?(:start)
36
+ attributes[:start] = Foreman::Cast.to_bool(attributes[:start_after_create]) ? '1' : '0'
37
+ end
38
+ attributes
39
+ end
40
+
33
41
  def vm_collection(type)
34
42
  collection = :servers
35
43
  collection = :containers if type == 'lxc'
@@ -18,6 +18,22 @@
18
18
  require 'fog/proxmox/helpers/disk_helper'
19
19
 
20
20
  module ProxmoxVMImageTemplateHelper
21
+ def create_vm_from_image(image_id, vmid, args, type)
22
+ image = find_vm_by_uuid(image_id)
23
+ validate_image_template_disk_slots!(image, args) if type == 'qemu'
24
+ is_full_clone = full_clone?(args)
25
+ vm = clone_from_image(image, vmid, full_clone: is_full_clone)
26
+ vm.full_clone = is_full_clone ? '1' : '0'
27
+ cloudinit_args = cloudinit_clone_args(args, vm)
28
+ vm.update(compute_clone_attributes(cloudinit_args, vm.container?, type, image: image))
29
+ update_pool(vm, args[:pool]) if args[:pool]
30
+ vm
31
+ end
32
+
33
+ def full_clone?(args)
34
+ Foreman::Cast.to_bool(args[:full_clone])
35
+ end
36
+
21
37
  def validate_image_template_disk_slots!(image, args)
22
38
  reserved_slots = image.config.disks.select(&:hard_disk?).map(&:id)
23
39
  limits = ProxmoxComputeControllersHelper::CONTROLLER_LIMITS
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is part of ForemanFogProxmox.
4
+
5
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+
10
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require 'fog/proxmox/helpers/agent_helper'
19
+ require 'fog/proxmox/helpers/hotplug_helper'
20
+
21
+ module ProxmoxVMOptionsHelper
22
+ HOTPLUG_DEVICES = Fog::Proxmox::HotplugHelper::DEVICES
23
+ AGENT_FIELDS = %w[agent_freeze_fs agent_fstrim_cloned_disks agent_type].freeze
24
+
25
+ def parse_agent(config)
26
+ config = config.with_indifferent_access
27
+ return config unless AGENT_FIELDS.any? { |key| config.key?(key) }
28
+
29
+ agent_enabled = bool_to_proxmox_flag(config['agent'])
30
+ freeze_fs = bool_to_proxmox_flag(config.delete('agent_freeze_fs'))
31
+ fstrim = bool_to_proxmox_flag(config.delete('agent_fstrim_cloned_disks'))
32
+ agent_type = config.delete('agent_type').presence || 'virtio'
33
+ config['agent'] = Fog::Proxmox::AgentHelper.flatten(
34
+ 'enabled' => agent_enabled, 'freeze-fs-on-backup' => freeze_fs,
35
+ 'fstrim_cloned_disks' => fstrim, 'type' => agent_type
36
+ )
37
+ config
38
+ end
39
+
40
+ # We can move this later to a general helper file so it can be used by other methods
41
+ def bool_to_proxmox_flag(value)
42
+ Foreman::Cast.to_bool(value) ? '1' : '0'
43
+ end
44
+
45
+ def parse_hotplug(config)
46
+ config = config.with_indifferent_access
47
+ return config unless HOTPLUG_DEVICES.any? { |device| config.key?("hotplug_#{device}") }
48
+
49
+ hotplug_enabled = Foreman::Cast.to_bool(config['hotplug'])
50
+
51
+ hotplug_devices_enabled = HOTPLUG_DEVICES.select do |device|
52
+ Foreman::Cast.to_bool(config.delete("hotplug_#{device}"))
53
+ end
54
+
55
+ config['hotplug'] = Fog::Proxmox::HotplugHelper.flatten(
56
+ enabled: hotplug_enabled, devices: hotplug_enabled ? hotplug_devices_enabled : []
57
+ )
58
+ config
59
+ end
60
+ end
@@ -21,7 +21,7 @@ module FogExtensions
21
21
  module Proxmox
22
22
  module Server
23
23
  extend ActiveSupport::Concern
24
- attr_accessor :image_id, :templated, :ostemplate_storage, :ostemplate_file, :iso_upload_storage, :password, :compute_resource_id
24
+ attr_accessor :image_id, :templated, :ostemplate_storage, :ostemplate_file, :iso_upload_storage, :password, :full_clone, :compute_resource_id
25
25
 
26
26
  def unique_cluster_identity(compute_resource)
27
27
  compute_resource.id.to_s + '_' + identity.to_s
@@ -22,6 +22,12 @@ module Orchestration
22
22
  module Compute
23
23
  extend ActiveSupport::Concern
24
24
 
25
+ included do
26
+ before_validation do
27
+ self.compute_attributes = compute_resource.normalize_vm_start_attributes(compute_attributes) if compute_resource.is_a?(ForemanFogProxmox::Proxmox) && compute_attributes
28
+ end
29
+ end
30
+
25
31
  def setComputeUpdate
26
32
  if compute_resource.class != ForemanFogProxmox::Proxmox
27
33
  super
@@ -86,6 +86,8 @@ module ForemanFogProxmox
86
86
  def vm_compute_attributes(vm)
87
87
  vm_attrs = {}
88
88
  vm_attrs = vm_attrs.merge(vmid: vm.identity, node_id: vm.node_id, type: vm.type)
89
+ full_clone = vm.try(:full_clone)
90
+ vm_attrs[:full_clone] = full_clone unless full_clone.nil?
89
91
  if vm.respond_to?(:config)
90
92
  if vm.config.respond_to?(:disks)
91
93
  vm_attrs[:volumes_attributes] = Hash[vm.config.disks.each_with_index.map do |disk, idx|
@@ -26,53 +26,35 @@ module ForemanFogProxmox
26
26
  def images_by_storage(node_id, storage_id, type = 'iso')
27
27
  node = client.nodes.get node_id
28
28
  node ||= default_node
29
+ return [] unless online_node?(node)
30
+
29
31
  storage = node.storages.get storage_id if storage_id
30
32
  logger.debug("images_by_storage(): node_id #{node_id} storage_id #{storage_id} type #{type}")
31
33
  storage.volumes.list_by_content_type(type).sort_by(&:volid) if storage
32
34
  end
33
35
 
34
- def template_name(template)
35
- image = find_vm_by_uuid(template_uuid(template))
36
- image&.name
37
- end
38
-
39
36
  def template_uuid(template)
40
37
  id.to_s + '_' + template.vmid.to_s
41
38
  end
42
39
 
43
40
  def available_images
44
- templates.collect { |template| OpenStruct.new(id: template_uuid(template), name: template_name(template)) }.uniq
41
+ templates.collect { |template| OpenStruct.new(id: template_uuid(template), name: template.name) }.uniq
45
42
  end
46
43
 
47
44
  def templates
48
- volumes = []
49
- nodes.each do |node|
50
- storages(node.node).each do |storage|
51
- # Skip disabled storages (enabled == 0 or nil)
52
- unless storage.enabled.to_i == 1
53
- logger.warn("Skipping disabled storage #{storage.identity} on #{node.node}")
54
- next
55
- end
56
-
57
- # Fetch QEMU and LXC template images
58
- volumes += storage.volumes.list_by_content_type('images')
59
- volumes += storage.volumes.list_by_content_type('rootdir')
60
- rescue StandardError => e
61
- logger.error("Failed to fetch volumes for storage #{storage.identity} on #{node.node}: #{e.message}")
62
- next
63
- end
64
- end
65
- # for creating image, only list volumes which are templated
66
- volumes.select(&:template?)
45
+ client.list_cluster_resources(type: 'vm')
46
+ .select { |resource| resource['template'].to_i == 1 }
47
+ .map { |resource| OpenStruct.new(resource) }
67
48
  end
68
49
 
69
50
  def template(uuid)
70
51
  find_vm_by_uuid(uuid)
71
52
  end
72
53
 
73
- def clone_from_image(image, vmid)
74
- logger.debug("create_vm(): clone #{image.identity} in #{vmid}")
75
- image.clone(vmid)
54
+ def clone_from_image(image, vmid, full_clone: false)
55
+ logger.debug("create_vm(): clone #{image.identity} in #{vmid} full_clone=#{full_clone}")
56
+ clone_options = full_clone ? { full: 1 } : {}
57
+ image.clone(vmid, clone_options)
76
58
  find_vm_by_uuid(id.to_s + '_' + vmid.to_s)
77
59
  end
78
60
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is part of ForemanFogProxmox.
4
+
5
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+
10
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ module ForemanFogProxmox
19
+ module ProxmoxRngs
20
+ include ProxmoxVMHelper
21
+
22
+ def delete_rng(vm_obj)
23
+ logger.info("vm #{vm_obj.identity} delete rng0")
24
+ vm_obj.update(delete: 'rng0')
25
+ end
26
+
27
+ def process_rng_removal(vm_obj, attributes)
28
+ return unless vm_obj.type == 'qemu'
29
+ return if vm_obj.config.rng.blank?
30
+ return if attributes['rng_attributes'].present?
31
+
32
+ logger.debug("Removing rng from VM #{vm_obj}")
33
+ delete_rng(vm_obj)
34
+ end
35
+ end
36
+ end
@@ -23,22 +23,18 @@ module ForemanFogProxmox
23
23
  module ProxmoxVMCommands
24
24
  include ProxmoxVolumes
25
25
  include ProxmoxEfidisks
26
+ include ProxmoxRngs
26
27
  include ProxmoxPools
27
28
  include ProxmoxVMHelper
28
29
 
29
30
  def create_vm(args = {})
30
31
  type = args[:type]
31
- node = client.nodes.get(args[:node_id])
32
+ node = find_online_node!(args[:node_id])
32
33
  vmid = args[:vmid] = assign_vmid(args[:vmid].to_i, node)
33
34
  image_id = args[:image_id]
34
35
  remove_volume_keys(args)
35
36
  if image_id
36
- image = find_vm_by_uuid(image_id)
37
- validate_image_template_disk_slots!(image, args) if type == 'qemu'
38
- vm = clone_from_image(image, vmid)
39
- cloudinit_args = cloudinit_clone_args(args, vm)
40
- vm.update(compute_clone_attributes(cloudinit_args, vm.container?, type, image: image))
41
- update_pool(vm, args[:pool]) if args[:pool]
37
+ vm = create_vm_from_image(image_id, vmid, args, type)
42
38
  else
43
39
  logger.warn("create vm: args=#{args}")
44
40
  vm = node.send(vm_collection(type)).create(parse_typed_vm(args, type))
@@ -72,7 +68,8 @@ module ForemanFogProxmox
72
68
  options = { :hostname => args[:name] }
73
69
  parsed_args.merge(options)
74
70
  end
75
- parsed_args.reject { |k| k == 'pool' }
71
+ excluded_keys = ['pool', 'full_clone']
72
+ parsed_args.reject { |k| excluded_keys.include?(k) }
76
73
  end
77
74
 
78
75
  def destroy_vm(uuid)
@@ -95,7 +92,7 @@ module ForemanFogProxmox
95
92
 
96
93
  def compute_config_attributes(parsed_attr)
97
94
  excluded_keys = [:vmid, :templated, :ostemplate, :ostemplate_file, :ostemplate_storage, :volumes_attributes,
98
- :pool]
95
+ :pool, :full_clone]
99
96
  config_attributes = parsed_attr.reject { |key, _value| excluded_keys.include? key.to_sym }
100
97
  ForemanFogProxmox::HashCollection.remove_empty_values(config_attributes)
101
98
  config_attributes = config_attributes.reject { |key, _value| Fog::Proxmox::DiskHelper.disk?(key) }
@@ -109,6 +106,7 @@ module ForemanFogProxmox
109
106
  if templated == '1' && !vm.templated?
110
107
  vm.create_template
111
108
  elsif vm.node_id != node_id
109
+ find_online_node!(node_id)
112
110
  vm.migrate(node_id)
113
111
  else
114
112
  processed_attributes = process_firmware_attributes(new_attributes, vm.type)
@@ -124,6 +122,7 @@ module ForemanFogProxmox
124
122
  end
125
123
 
126
124
  process_efidisk_removal(vm, processed_attributes)
125
+ process_rng_removal(vm, processed_attributes)
127
126
 
128
127
  vm.update(config_attributes[:config_attributes])
129
128
  poolid = new_attributes['pool'] if new_attributes.key?('pool')