foreman_fog_proxmox 0.12.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of foreman_fog_proxmox might be problematic. Click here for more details.

Files changed (113) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +619 -0
  3. data/README.md +290 -0
  4. data/Rakefile +49 -0
  5. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_compute_resource.js +33 -0
  6. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_vm.js +187 -0
  7. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_vm_container.js +24 -0
  8. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_vm_server.js +125 -0
  9. data/app/controllers/concerns/foreman_fog_proxmox/controller/parameters/compute_resource.rb +41 -0
  10. data/app/controllers/foreman_fog_proxmox/compute_resources_controller.rb +86 -0
  11. data/app/helpers/node_dashboard_helper.rb +35 -0
  12. data/app/helpers/proxmox_compute_selectors_helper.rb +184 -0
  13. data/app/helpers/proxmox_container_helper.rb +163 -0
  14. data/app/helpers/proxmox_form_helper.rb +70 -0
  15. data/app/helpers/proxmox_server_helper.rb +155 -0
  16. data/app/helpers/proxmox_vm_helper.rb +88 -0
  17. data/app/models/concerns/fog_extensions/proxmox/disk.rb +29 -0
  18. data/app/models/concerns/fog_extensions/proxmox/interface.rb +40 -0
  19. data/app/models/concerns/fog_extensions/proxmox/node.rb +41 -0
  20. data/app/models/concerns/fog_extensions/proxmox/server.rb +93 -0
  21. data/app/models/concerns/fog_extensions/proxmox/server_config.rb +71 -0
  22. data/app/models/concerns/fog_extensions/proxmox/volume.rb +29 -0
  23. data/app/models/concerns/host_ext/proxmox/interfaces.rb +52 -0
  24. data/app/models/concerns/orchestration/proxmox/compute.rb +42 -0
  25. data/app/models/foreman_fog_proxmox/options_select.rb +36 -0
  26. data/app/models/foreman_fog_proxmox/proxmox.rb +133 -0
  27. data/app/models/foreman_fog_proxmox/proxmox_compute_attributes.rb +67 -0
  28. data/app/models/foreman_fog_proxmox/proxmox_connection.rb +67 -0
  29. data/app/models/foreman_fog_proxmox/proxmox_console.rb +41 -0
  30. data/app/models/foreman_fog_proxmox/proxmox_images.rb +61 -0
  31. data/app/models/foreman_fog_proxmox/proxmox_interfaces.rb +103 -0
  32. data/app/models/foreman_fog_proxmox/proxmox_operating_systems.rb +49 -0
  33. data/app/models/foreman_fog_proxmox/proxmox_pools.rb +56 -0
  34. data/app/models/foreman_fog_proxmox/proxmox_token_expiration.rb +30 -0
  35. data/app/models/foreman_fog_proxmox/proxmox_version.rb +36 -0
  36. data/app/models/foreman_fog_proxmox/proxmox_vm_commands.rb +116 -0
  37. data/app/models/foreman_fog_proxmox/proxmox_vm_new.rb +178 -0
  38. data/app/models/foreman_fog_proxmox/proxmox_vm_queries.rb +75 -0
  39. data/app/models/foreman_fog_proxmox/proxmox_volumes.rb +94 -0
  40. data/app/models/foreman_fog_proxmox/vms.rb +37 -0
  41. data/app/overrides/compute_resources_vms/form/add_clone_to_new_vm_compute_detail.rb +25 -0
  42. data/app/overrides/compute_resources_vms/form/add_from_profile_to_compute_attributes_form.rb +34 -0
  43. data/app/overrides/compute_resources_vms/form/add_vm_type_node_to_new_form.rb +25 -0
  44. data/app/overrides/compute_resources_vms/form/add_vm_type_to_networks_form.rb +34 -0
  45. data/app/overrides/compute_resources_vms/form/add_vm_type_to_nic_provider_specific_form.rb +26 -0
  46. data/app/overrides/compute_resources_vms/form/add_vm_type_to_volumes_edit.rb +25 -0
  47. data/app/overrides/compute_resources_vms/form/add_vm_type_to_volumes_new_volume.rb +25 -0
  48. data/app/overrides/compute_resources_vms/form/remove_new_vm_from_removable_layout.rb +25 -0
  49. data/app/services/foreman_fog_proxmox/node_dashboard/data.rb +37 -0
  50. data/app/views/api/v2/compute_resources/proxmox.json.rabl +3 -0
  51. data/app/views/compute_resources/form/_proxmox.html.erb +29 -0
  52. data/app/views/compute_resources/show/_proxmox.html.erb +37 -0
  53. data/app/views/compute_resources_vms/form/proxmox/_add_from_profile_to_compute_attributes_form.html.erb +23 -0
  54. data/app/views/compute_resources_vms/form/proxmox/_add_from_profile_to_compute_form.html.erb +23 -0
  55. data/app/views/compute_resources_vms/form/proxmox/_add_from_profile_to_hosts_compute_detail_form.html.erb +24 -0
  56. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_node_to_new_form.html.erb +24 -0
  57. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_networks_form.html.erb +28 -0
  58. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_networks_new_childs_form.html.erb +32 -0
  59. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_nic_provider_specific_form.html.erb +30 -0
  60. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_volumes_edit.html.erb +71 -0
  61. data/app/views/compute_resources_vms/form/proxmox/_base.html.erb +39 -0
  62. data/app/views/compute_resources_vms/form/proxmox/_general.html.erb +39 -0
  63. data/app/views/compute_resources_vms/form/proxmox/_network.html.erb +19 -0
  64. data/app/views/compute_resources_vms/form/proxmox/_removable_layout.html.erb +27 -0
  65. data/app/views/compute_resources_vms/form/proxmox/container/_advanced.html.erb +31 -0
  66. data/app/views/compute_resources_vms/form/proxmox/container/_config.html.erb +41 -0
  67. data/app/views/compute_resources_vms/form/proxmox/container/_extended.html.erb +27 -0
  68. data/app/views/compute_resources_vms/form/proxmox/container/_network.html.erb +33 -0
  69. data/app/views/compute_resources_vms/form/proxmox/container/_volume_mp.html.erb +27 -0
  70. data/app/views/compute_resources_vms/form/proxmox/container/_volume_rootfs.html.erb +24 -0
  71. data/app/views/compute_resources_vms/form/proxmox/server/_advanced.html.erb +31 -0
  72. data/app/views/compute_resources_vms/form/proxmox/server/_config.html.erb +60 -0
  73. data/app/views/compute_resources_vms/form/proxmox/server/_network.html.erb +30 -0
  74. data/app/views/compute_resources_vms/form/proxmox/server/_volume.html.erb +28 -0
  75. data/app/views/compute_resources_vms/index/_proxmox.html.erb +49 -0
  76. data/app/views/compute_resources_vms/show/_proxmox.html.erb +42 -0
  77. data/app/views/dashboard/_foreman_fog_proxmox_widget.erb +21 -0
  78. data/app/views/images/form/_proxmox.html.erb +21 -0
  79. data/config/routes.rb +30 -0
  80. data/lib/foreman_fog_proxmox.rb +23 -0
  81. data/lib/foreman_fog_proxmox/engine.rb +105 -0
  82. data/lib/foreman_fog_proxmox/semver.rb +102 -0
  83. data/lib/foreman_fog_proxmox/value.rb +26 -0
  84. data/lib/foreman_fog_proxmox/version.rb +22 -0
  85. data/lib/tasks/foreman_fog_proxmox_tasks.rake +37 -0
  86. data/locale/Makefile +60 -0
  87. data/locale/en/foreman_fog_proxmox.po +401 -0
  88. data/locale/foreman_fog_proxmox.pot +627 -0
  89. data/locale/fr/foreman_fog_proxmox.po +401 -0
  90. data/locale/gemspec.rb +4 -0
  91. data/test/factories/foreman_fog_proxmox/proxmox_container_mock_factory.rb +137 -0
  92. data/test/factories/foreman_fog_proxmox/proxmox_node_mock_factory.rb +87 -0
  93. data/test/factories/foreman_fog_proxmox/proxmox_server_mock_factory.rb +134 -0
  94. data/test/factories/proxmox_factory.rb +79 -0
  95. data/test/functional/compute_resources_controller_test.rb +48 -0
  96. data/test/test_plugin_helper.rb +19 -0
  97. data/test/unit/foreman_fog_proxmox/helpers/proxmox_container_helper_test.rb +204 -0
  98. data/test/unit/foreman_fog_proxmox/helpers/proxmox_server_helper_test.rb +149 -0
  99. data/test/unit/foreman_fog_proxmox/helpers/proxmox_vm_helper_test.rb +216 -0
  100. data/test/unit/foreman_fog_proxmox/proxmox_compute_attributes_test.rb +124 -0
  101. data/test/unit/foreman_fog_proxmox/proxmox_images_test.rb +52 -0
  102. data/test/unit/foreman_fog_proxmox/proxmox_interfaces_test.rb +117 -0
  103. data/test/unit/foreman_fog_proxmox/proxmox_test.rb +58 -0
  104. data/test/unit/foreman_fog_proxmox/proxmox_version_test.rb +94 -0
  105. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_container_test.rb +280 -0
  106. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_create_test.rb +110 -0
  107. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_update_test.rb +222 -0
  108. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_update_volumes_test.rb +308 -0
  109. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_test.rb +43 -0
  110. data/test/unit/foreman_fog_proxmox/proxmox_vm_new_test.rb +71 -0
  111. data/test/unit/foreman_fog_proxmox/proxmox_vm_queries_test.rb +68 -0
  112. data/test/unit/foreman_fog_proxmox/semver_test.rb +111 -0
  113. metadata +249 -0
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 Tristan Robert
4
+
5
+ # This file is part of ForemanFogProxmox.
6
+
7
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ module ForemanFogProxmox
21
+ module ProxmoxTokenExpiration
22
+ def token_expired?
23
+ Fog::Proxmox.credentials_has_expired?
24
+ end
25
+
26
+ def token_deadline
27
+ Fog::Proxmox.credentials[:deadline]
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 Tristan Robert
4
+
5
+ # This file is part of ForemanFogProxmox.
6
+
7
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'foreman_fog_proxmox/semver'
21
+
22
+ module ForemanFogProxmox
23
+ module ProxmoxVersion
24
+ def version_suitable?
25
+ logger.debug(format(_('Proxmox compute resource version is %<version>s'), version: version))
26
+ raise ::Foreman::Exception, format(_('Proxmox version %<version>s is not semver suitable'), version: version) unless ForemanFogProxmox::Semver.semver?(version)
27
+
28
+ ForemanFogProxmox::Semver.to_semver(version) >= ForemanFogProxmox::Semver.to_semver('5.3.0')
29
+ end
30
+
31
+ def version
32
+ v = identity_client.read_version
33
+ "#{v['version']}.#{v['release']}"
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 Tristan Robert
4
+
5
+ # This file is part of ForemanFogProxmox.
6
+
7
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ module ForemanFogProxmox
21
+ module ProxmoxVmCommands
22
+ include ProxmoxVolumes
23
+ include ProxmoxPools
24
+
25
+ def start_on_boot(vm, args)
26
+ startonboot = args[:start_after_create].blank? ? false : Foreman::Cast.to_bool(args[:start_after_create])
27
+ vm.start if startonboot
28
+ vm
29
+ end
30
+
31
+ def create_vm(args = {})
32
+ vmid = args[:vmid].to_i
33
+ type = args[:type]
34
+ node = client.nodes.get(args[:node_id])
35
+ vmid = node.servers.next_id.to_i if vmid < 1
36
+ raise ::Foreman::Exception, format(N_('invalid vmid=%<vmid>s'), vmid: vmid) unless node.servers.id_valid?(vmid)
37
+
38
+ image_id = args[:image_id]
39
+ if image_id
40
+ clone_from_image(image_id, args, vmid)
41
+ else
42
+ convert_sizes(args)
43
+ remove_deletes(args)
44
+ case type
45
+ when 'qemu'
46
+ vm = node.servers.create(parse_server_vm(args))
47
+ when 'lxc'
48
+ hash = parse_container_vm(args)
49
+ hash = hash.merge(vmid: vmid)
50
+ vm = node.containers.create(hash.reject { |key, _value| ['ostemplate_storage', 'ostemplate_file'].include? key })
51
+ end
52
+ start_on_boot(vm, args)
53
+ end
54
+ rescue StandardError => e
55
+ logger.warn(format(_('failed to create vm: %<e>s'), e: e))
56
+ destroy_vm vm.id if vm
57
+ raise e
58
+ end
59
+
60
+ def destroy_vm(uuid)
61
+ vm = find_vm_by_uuid(uuid)
62
+ vm.stop if vm.ready?
63
+ vm.destroy
64
+ rescue ActiveRecord::RecordNotFound
65
+ # if the VM does not exists, we don't really care.
66
+ true
67
+ end
68
+
69
+ def supports_update?
70
+ true
71
+ end
72
+
73
+ def update_required?(old_attrs, new_attrs)
74
+ return true if super(old_attrs, new_attrs)
75
+
76
+ new_attrs[:interfaces_attributes]&.each do |key, interface|
77
+ return true if (interface[:id].blank? || interface[:_delete] == '1') && key != 'new_interfaces' # ignore the template
78
+ end
79
+
80
+ new_attrs[:volumes_attributes]&.each do |key, volume|
81
+ return true if (volume[:id].blank? || volume[:_delete] == '1') && key != 'new_volumes' # ignore the template
82
+ end
83
+
84
+ false
85
+ end
86
+
87
+ def user_data_supported?
88
+ true
89
+ end
90
+
91
+ def save_vm(uuid, new_attributes)
92
+ vm = find_vm_by_uuid(uuid)
93
+ templated = new_attributes['templated']
94
+ node_id = new_attributes['node_id']
95
+ if templated == '1' && !vm.templated?
96
+ vm.create_template
97
+ elsif vm.node_id != node_id
98
+ vm.migrate(node_id)
99
+ else
100
+ convert_memory_sizes(new_attributes)
101
+ volumes_attributes = new_attributes['volumes_attributes']
102
+ volumes_attributes&.each_value { |volume_attributes| save_volume(vm, volume_attributes) }
103
+ parsed_attr = vm.container? ? parse_container_vm(new_attributes.merge(type: vm.type)) : parse_server_vm(new_attributes.merge(type: vm.type))
104
+ logger.debug("parsed_attr=#{parsed_attr}")
105
+ config_attributes = parsed_attr.reject { |key, _value| [:vmid, :templated, :ostemplate, :ostemplate_file, :ostemplate_storage, :volumes_attributes, :pool].include? key.to_sym }
106
+ config_attributes = config_attributes.reject { |_key, value| ForemanFogProxmox::Value.empty?(value) }
107
+ cdrom_attributes = parsed_attr.select { |_key, value| Fog::Proxmox::DiskHelper.cdrom?(value.to_s) }
108
+ config_attributes = config_attributes.reject { |key, _value| Fog::Proxmox::DiskHelper.disk?(key) }
109
+ vm.update(config_attributes.merge(cdrom_attributes))
110
+ poolid = new_attributes['pool'] if new_attributes.key?('pool')
111
+ update_pool(vm, poolid) if poolid
112
+ end
113
+ find_vm_by_uuid(uuid)
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,178 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 Tristan Robert
4
+
5
+ # This file is part of ForemanFogProxmox.
6
+
7
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'fog/proxmox'
21
+ require 'fog/proxmox/helpers/nic_helper'
22
+ require 'fog/proxmox/helpers/disk_helper'
23
+
24
+ module ForemanFogProxmox
25
+ module ProxmoxVmNew
26
+ include ProxmoxVmHelper
27
+
28
+ def volume_server_defaults(controller = 'scsi', device = 0)
29
+ id = "#{controller}#{device}"
30
+ { id: id, storage: storages.first.identity.to_s, size: (8 * GIGA), options: { cache: 'none' } }
31
+ end
32
+
33
+ def volume_container_defaults(id = 'rootfs')
34
+ { id: id, storage: storages.first.identity.to_s, size: (8 * GIGA), options: {} }
35
+ end
36
+
37
+ def new_volume(attr = {})
38
+ type = attr['type']
39
+ type ||= 'qemu'
40
+ case type
41
+ when 'lxc'
42
+ new_volume_server(attr)
43
+ when 'qemu'
44
+ new_volume_container(attr)
45
+ end
46
+ end
47
+
48
+ def new_volume_server(attr = {})
49
+ opts = volume_server_defaults.merge(attr.to_h).deep_symbolize_keys
50
+ opts[:size] = opts[:size].to_s
51
+ Fog::Proxmox::Compute::Disk.new(opts)
52
+ end
53
+
54
+ def new_volume_container(attr = {})
55
+ opts = volume_container_defaults.merge(attr.to_h).deep_symbolize_keys
56
+ opts[:size] = opts[:size].to_s
57
+ Fog::Proxmox::Compute::Disk.new(opts)
58
+ end
59
+
60
+ def interface_defaults(id = 'net0')
61
+ { id: id, model: 'virtio', name: 'eth0', bridge: bridges.first.identity.to_s }
62
+ end
63
+
64
+ def interface_server_defaults(id = 'net0')
65
+ { id: id, model: 'virtio', bridge: bridges.first.identity.to_s }
66
+ end
67
+
68
+ def interface_container_defaults(id = 'net0')
69
+ { id: id, name: 'eth0', bridge: bridges.first.identity.to_s, dhcpv4: 1, dhcpv6: 1 }
70
+ end
71
+
72
+ def new_interface(attr = {})
73
+ type = attr['type']
74
+ type ||= 'qemu'
75
+ case type
76
+ when 'lxc'
77
+ new_container_interface(attr)
78
+ when 'qemu'
79
+ new_server_interface(attr)
80
+ end
81
+ end
82
+
83
+ def new_server_interface(attr = {})
84
+ logger.debug('new_server_interface')
85
+ opts = interface_server_defaults.merge(attr.to_h).deep_symbolize_keys
86
+ Fog::Proxmox::Compute::Interface.new(opts)
87
+ end
88
+
89
+ def new_container_interface(attr = {})
90
+ logger.debug('new_container_interface')
91
+ opts = interface_container_defaults.merge(attr.to_h).deep_symbolize_keys
92
+ Fog::Proxmox::Compute::Interface.new(opts)
93
+ end
94
+
95
+ def default_node
96
+ nodes.first
97
+ end
98
+
99
+ def default_node_id
100
+ default_node.node
101
+ end
102
+
103
+ def next_vmid
104
+ default_node.servers.next_id
105
+ end
106
+
107
+ def vm_server_instance_defaults
108
+ ActiveSupport::HashWithIndifferentAccess.new(
109
+ name: "foreman_#{Time.now.to_i}",
110
+ vmid: next_vmid,
111
+ type: 'qemu',
112
+ node_id: default_node_id,
113
+ cores: 1,
114
+ sockets: 1,
115
+ kvm: 1,
116
+ vga: 'std',
117
+ memory: 512 * MEGA,
118
+ ostype: 'l26',
119
+ keyboard: 'en-us',
120
+ cpu: 'kvm64',
121
+ scsihw: 'virtio-scsi-pci',
122
+ ide2: 'none,media=cdrom',
123
+ templated: 0
124
+ ).merge(Fog::Proxmox::DiskHelper.flatten(volume_server_defaults)).merge(Fog::Proxmox::DiskHelper.flatten(volume_container_defaults)).merge(Fog::Proxmox::NicHelper.flatten(interface_defaults))
125
+ end
126
+
127
+ def vm_container_instance_defaults
128
+ ActiveSupport::HashWithIndifferentAccess.new(
129
+ name: "foreman_#{Time.now.to_i}",
130
+ vmid: next_vmid,
131
+ type: 'lxc',
132
+ node_id: default_node_id,
133
+ memory: 512 * MEGA,
134
+ templated: 0
135
+ ).merge(Fog::Proxmox::DiskHelper.flatten(volume_container_defaults)).merge(Fog::Proxmox::DiskHelper.flatten(volume_server_defaults)).merge(Fog::Proxmox::NicHelper.flatten(interface_defaults))
136
+ end
137
+
138
+ def vm_instance_defaults
139
+ super.merge(vmid: next_vmid, node_id: default_node_id)
140
+ end
141
+
142
+ def new_vm(new_attr = {})
143
+ new_attr = ActiveSupport::HashWithIndifferentAccess.new(new_attr)
144
+ type = new_attr['type']
145
+ type ||= 'qemu'
146
+ case type
147
+ when 'lxc'
148
+ vm = new_container_vm(new_attr)
149
+ when 'qemu'
150
+ vm = new_server_vm(new_attr)
151
+ end
152
+ logger.debug(format(_('new_vm() vm.config=%<config>s'), config: vm.config.inspect))
153
+ vm
154
+ end
155
+
156
+ def new_container_vm(new_attr = {})
157
+ options = new_attr
158
+ node_id = new_attr['node_id']
159
+ node = node_id ? client.nodes.get(node_id) : default_node
160
+ options = options.merge(type: 'lxc').merge(vmid: next_vmid)
161
+ options = vm_container_instance_defaults.merge(options) if new_attr.empty?
162
+ vm = node.containers.new(parse_container_vm(options).deep_symbolize_keys)
163
+ logger.debug(format(_('new_container_vm() vm.config=%<config>s'), config: vm.config.inspect))
164
+ vm
165
+ end
166
+
167
+ def new_server_vm(new_attr = {})
168
+ options = new_attr
169
+ node_id = new_attr['node_id']
170
+ node = node_id ? client.nodes.get(node_id) : default_node
171
+ options = options.merge(type: 'qemu').merge(vmid: next_vmid)
172
+ options = vm_server_instance_defaults.merge(options) if new_attr.empty?
173
+ vm = node.servers.new(parse_server_vm(options).deep_symbolize_keys)
174
+ logger.debug(format(_('new_server_vm() vm.config=%<config>s'), config: vm.config.inspect))
175
+ vm
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 Tristan Robert
4
+
5
+ # This file is part of ForemanFogProxmox.
6
+
7
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ module ForemanFogProxmox
21
+ module ProxmoxVmQueries
22
+ include ProxmoxPools
23
+
24
+ def nodes
25
+ nodes = client.nodes.all if client
26
+ nodes&.sort_by(&:node)
27
+ end
28
+
29
+ def storages(node_id = default_node_id, type = 'images')
30
+ node = client.nodes.get node_id
31
+ node ||= default_node
32
+ storages = node.storages.list_by_content_type type
33
+ storages.sort_by(&:storage)
34
+ end
35
+
36
+ def bridges(node_id = default_node_id)
37
+ node = network_client.nodes.get node_id
38
+ node ||= network_client.nodes.first
39
+ bridges = node.networks.all(type: 'any_bridge')
40
+ bridges.sort_by(&:iface)
41
+ end
42
+
43
+ # TODO: Pagination with filters
44
+ def vms(_opts = {})
45
+ vms = []
46
+ nodes.each { |node| vms += node.servers.all + node.containers.all }
47
+ ForemanFogProxmox::Vms.new(vms)
48
+ end
49
+
50
+ def find_vm_by_uuid(uuid)
51
+ # look for the uuid on all known nodes
52
+ vm = nil
53
+ nodes.each do |node|
54
+ vm = find_vm_in_servers_by_uuid(node.servers, uuid)
55
+ vm ||= find_vm_in_servers_by_uuid(node.containers, uuid)
56
+ unless vm.nil?
57
+ logger.debug("found vm #{uuid} on node #{node.node}")
58
+ break
59
+ end
60
+ end
61
+ vm
62
+ end
63
+
64
+ def find_vm_in_servers_by_uuid(servers, uuid)
65
+ vm = servers.get(uuid) if !uuid.nil? && !uuid.to_s.empty?
66
+ pool_owner(vm) if vm
67
+ vm
68
+ rescue Fog::Errors::NotFound
69
+ nil
70
+ rescue StandardError => e
71
+ Foreman::Logging.exception(format(_('Failed retrieving proxmox server vm by vmid=%<vmid>s'), vmid: uuid), e)
72
+ raise(ActiveRecord::RecordNotFound, e)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 Tristan Robert
4
+
5
+ # This file is part of ForemanFogProxmox.
6
+
7
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'fog/proxmox/helpers/disk_helper'
21
+
22
+ module ForemanFogProxmox
23
+ module ProxmoxVolumes
24
+ include ProxmoxVmHelper
25
+
26
+ def delete_volume(vm, id)
27
+ vm.detach(id)
28
+ device = Fog::Proxmox::DiskHelper.extract_device(id)
29
+ vm.detach('unused' + device.to_s)
30
+ end
31
+
32
+ def volume_options(vm, id, volume_attributes)
33
+ options = {}
34
+ options.store(:mp, volume_attributes['mp']) if vm.container? && id != 'rootfs'
35
+ options.store(:cache, volume_attributes['cache']) unless vm.container?
36
+ options
37
+ end
38
+
39
+ def update_volume(vm, id, volume_attributes)
40
+ disk = vm.config.disks.get(id)
41
+ diff_size = volume_attributes['size'].to_i - disk.size
42
+ raise ::Foreman::Exception, format(_('Unable to shrink %<id>s size. Proxmox allows only increasing size.'), id: id) unless diff_size >= 0
43
+
44
+ if diff_size > 0
45
+ extension = '+' + (diff_size / GIGA).to_s + 'G'
46
+ vm.extend(id, extension)
47
+ elsif disk.storage != volume_attributes['storage']
48
+ vm.move(id, volume_attributes['storage'])
49
+ else
50
+ options = volume_options(vm, id, volume_attributes)
51
+ vm.attach({ :id => disk.id, :volid => disk.volid, :size => disk.size }, options)
52
+ end
53
+ end
54
+
55
+ def volume_exists?(volume_attributes)
56
+ volid = volume_attributes.key?('volid') ? volume_attributes['volid'] : ''
57
+ volid.present?
58
+ end
59
+
60
+ def volume_to_delete?(volume_attributes)
61
+ volume_attributes['_delete'].blank? ? false : Foreman::Cast.to_bool(volume_attributes['_delete'])
62
+ end
63
+
64
+ def extract_id(vm, volume_attributes)
65
+ id = ''
66
+ if volume_exists?(volume_attributes)
67
+ id = volume_attributes['id']
68
+ else
69
+ device = vm.container? ? 'mp' : volume_attributes['controller']
70
+ id = device + volume_attributes['device']
71
+ end
72
+ id
73
+ end
74
+
75
+ def add_volume(vm, id, volume_attributes)
76
+ options = volume_options(vm, id, volume_attributes)
77
+ disk_attributes = { id: id, storage: volume_attributes['storage'], size: (volume_attributes['size'].to_i / GIGA).to_s }
78
+ vm.attach(disk_attributes, options)
79
+ end
80
+
81
+ def save_volume(vm, volume_attributes)
82
+ id = extract_id(vm, volume_attributes)
83
+ if volume_exists?(volume_attributes)
84
+ if volume_to_delete?(volume_attributes)
85
+ delete_volume(vm, id)
86
+ else
87
+ update_volume(vm, id, volume_attributes)
88
+ end
89
+ else
90
+ add_volume(vm, id, volume_attributes)
91
+ end
92
+ end
93
+ end
94
+ end