fog-proxmox-configlmm 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +8 -0
- data/LICENSE +674 -0
- data/README.md +110 -0
- data/docs/compute.md +874 -0
- data/docs/connection_parameters.md +45 -0
- data/docs/getting_started.md +53 -0
- data/docs/identity.md +382 -0
- data/examples/compute.rb +276 -0
- data/examples/identity.rb +203 -0
- data/lib/fog/proxmox/attributes.rb +34 -0
- data/lib/fog/proxmox/auth/token/access_ticket.rb +96 -0
- data/lib/fog/proxmox/auth/token/user_token.rb +107 -0
- data/lib/fog/proxmox/auth/token.rb +94 -0
- data/lib/fog/proxmox/compute/models/disk.rb +94 -0
- data/lib/fog/proxmox/compute/models/disks.rb +55 -0
- data/lib/fog/proxmox/compute/models/interface.rb +56 -0
- data/lib/fog/proxmox/compute/models/interfaces.rb +44 -0
- data/lib/fog/proxmox/compute/models/node.rb +93 -0
- data/lib/fog/proxmox/compute/models/nodes.rb +39 -0
- data/lib/fog/proxmox/compute/models/server.rb +271 -0
- data/lib/fog/proxmox/compute/models/server_config.rb +161 -0
- data/lib/fog/proxmox/compute/models/servers.rb +80 -0
- data/lib/fog/proxmox/compute/models/snapshot.rb +83 -0
- data/lib/fog/proxmox/compute/models/snapshots.rb +47 -0
- data/lib/fog/proxmox/compute/models/storage.rb +65 -0
- data/lib/fog/proxmox/compute/models/storages.rb +51 -0
- data/lib/fog/proxmox/compute/models/task.rb +79 -0
- data/lib/fog/proxmox/compute/models/tasks.rb +65 -0
- data/lib/fog/proxmox/compute/models/volume.rb +68 -0
- data/lib/fog/proxmox/compute/models/volumes.rb +58 -0
- data/lib/fog/proxmox/compute/requests/action_server.rb +45 -0
- data/lib/fog/proxmox/compute/requests/clone_server.rb +44 -0
- data/lib/fog/proxmox/compute/requests/create_backup.rb +42 -0
- data/lib/fog/proxmox/compute/requests/create_server.rb +43 -0
- data/lib/fog/proxmox/compute/requests/create_snapshot.rb +46 -0
- data/lib/fog/proxmox/compute/requests/create_spice.rb +44 -0
- data/lib/fog/proxmox/compute/requests/create_term.rb +45 -0
- data/lib/fog/proxmox/compute/requests/create_vnc.rb +44 -0
- data/lib/fog/proxmox/compute/requests/delete_server.rb +45 -0
- data/lib/fog/proxmox/compute/requests/delete_snapshot.rb +47 -0
- data/lib/fog/proxmox/compute/requests/delete_volume.rb +40 -0
- data/lib/fog/proxmox/compute/requests/get_node_statistics.rb +44 -0
- data/lib/fog/proxmox/compute/requests/get_server_config.rb +60 -0
- data/lib/fog/proxmox/compute/requests/get_server_status.rb +63 -0
- data/lib/fog/proxmox/compute/requests/get_snapshot_config.rb +44 -0
- data/lib/fog/proxmox/compute/requests/get_task.rb +41 -0
- data/lib/fog/proxmox/compute/requests/get_vnc.rb +45 -0
- data/lib/fog/proxmox/compute/requests/get_volume.rb +42 -0
- data/lib/fog/proxmox/compute/requests/list_nodes.rb +48 -0
- data/lib/fog/proxmox/compute/requests/list_servers.rb +113 -0
- data/lib/fog/proxmox/compute/requests/list_snapshots.rb +66 -0
- data/lib/fog/proxmox/compute/requests/list_storages.rb +46 -0
- data/lib/fog/proxmox/compute/requests/list_tasks.rb +44 -0
- data/lib/fog/proxmox/compute/requests/list_volumes.rb +44 -0
- data/lib/fog/proxmox/compute/requests/log_task.rb +44 -0
- data/lib/fog/proxmox/compute/requests/migrate_server.rb +44 -0
- data/lib/fog/proxmox/compute/requests/move_disk.rb +43 -0
- data/lib/fog/proxmox/compute/requests/move_volume.rb +43 -0
- data/lib/fog/proxmox/compute/requests/next_vmid.rb +42 -0
- data/lib/fog/proxmox/compute/requests/resize_container.rb +43 -0
- data/lib/fog/proxmox/compute/requests/resize_server.rb +43 -0
- data/lib/fog/proxmox/compute/requests/rollback_snapshot.rb +48 -0
- data/lib/fog/proxmox/compute/requests/status_task.rb +41 -0
- data/lib/fog/proxmox/compute/requests/stop_task.rb +41 -0
- data/lib/fog/proxmox/compute/requests/template_server.rb +44 -0
- data/lib/fog/proxmox/compute/requests/update_server.rb +44 -0
- data/lib/fog/proxmox/compute/requests/update_snapshot.rb +45 -0
- data/lib/fog/proxmox/compute.rb +141 -0
- data/lib/fog/proxmox/core.rb +147 -0
- data/lib/fog/proxmox/errors.rb +65 -0
- data/lib/fog/proxmox/hash.rb +37 -0
- data/lib/fog/proxmox/helpers/controller_helper.rb +63 -0
- data/lib/fog/proxmox/helpers/cpu_helper.rb +88 -0
- data/lib/fog/proxmox/helpers/disk_helper.rb +185 -0
- data/lib/fog/proxmox/helpers/ip_helper.rb +101 -0
- data/lib/fog/proxmox/helpers/nic_helper.rb +132 -0
- data/lib/fog/proxmox/identity/models/domain.rb +69 -0
- data/lib/fog/proxmox/identity/models/domain_type.rb +47 -0
- data/lib/fog/proxmox/identity/models/domains.rb +44 -0
- data/lib/fog/proxmox/identity/models/group.rb +51 -0
- data/lib/fog/proxmox/identity/models/groups.rb +44 -0
- data/lib/fog/proxmox/identity/models/permission.rb +66 -0
- data/lib/fog/proxmox/identity/models/permissions.rb +46 -0
- data/lib/fog/proxmox/identity/models/pool.rb +89 -0
- data/lib/fog/proxmox/identity/models/pools.rb +46 -0
- data/lib/fog/proxmox/identity/models/principal.rb +42 -0
- data/lib/fog/proxmox/identity/models/role.rb +48 -0
- data/lib/fog/proxmox/identity/models/roles.rb +44 -0
- data/lib/fog/proxmox/identity/models/token.rb +72 -0
- data/lib/fog/proxmox/identity/models/token_info.rb +41 -0
- data/lib/fog/proxmox/identity/models/tokens.rb +54 -0
- data/lib/fog/proxmox/identity/models/user.rb +82 -0
- data/lib/fog/proxmox/identity/models/users.rb +44 -0
- data/lib/fog/proxmox/identity/requests/change_password.rb +41 -0
- data/lib/fog/proxmox/identity/requests/check_permissions.rb +41 -0
- data/lib/fog/proxmox/identity/requests/create_domain.rb +40 -0
- data/lib/fog/proxmox/identity/requests/create_group.rb +40 -0
- data/lib/fog/proxmox/identity/requests/create_pool.rb +40 -0
- data/lib/fog/proxmox/identity/requests/create_role.rb +40 -0
- data/lib/fog/proxmox/identity/requests/create_token.rb +40 -0
- data/lib/fog/proxmox/identity/requests/create_user.rb +40 -0
- data/lib/fog/proxmox/identity/requests/delete_domain.rb +40 -0
- data/lib/fog/proxmox/identity/requests/delete_group.rb +40 -0
- data/lib/fog/proxmox/identity/requests/delete_pool.rb +39 -0
- data/lib/fog/proxmox/identity/requests/delete_role.rb +40 -0
- data/lib/fog/proxmox/identity/requests/delete_token.rb +40 -0
- data/lib/fog/proxmox/identity/requests/delete_user.rb +39 -0
- data/lib/fog/proxmox/identity/requests/get_domain.rb +41 -0
- data/lib/fog/proxmox/identity/requests/get_group.rb +41 -0
- data/lib/fog/proxmox/identity/requests/get_pool.rb +41 -0
- data/lib/fog/proxmox/identity/requests/get_role.rb +41 -0
- data/lib/fog/proxmox/identity/requests/get_token_info.rb +41 -0
- data/lib/fog/proxmox/identity/requests/get_user.rb +41 -0
- data/lib/fog/proxmox/identity/requests/list_domains.rb +41 -0
- data/lib/fog/proxmox/identity/requests/list_groups.rb +41 -0
- data/lib/fog/proxmox/identity/requests/list_permissions.rb +41 -0
- data/lib/fog/proxmox/identity/requests/list_pools.rb +41 -0
- data/lib/fog/proxmox/identity/requests/list_roles.rb +40 -0
- data/lib/fog/proxmox/identity/requests/list_tokens.rb +41 -0
- data/lib/fog/proxmox/identity/requests/list_user_permissions.rb +44 -0
- data/lib/fog/proxmox/identity/requests/list_users.rb +42 -0
- data/lib/fog/proxmox/identity/requests/read_version.rb +39 -0
- data/lib/fog/proxmox/identity/requests/update_domain.rb +41 -0
- data/lib/fog/proxmox/identity/requests/update_group.rb +40 -0
- data/lib/fog/proxmox/identity/requests/update_permissions.rb +41 -0
- data/lib/fog/proxmox/identity/requests/update_pool.rb +41 -0
- data/lib/fog/proxmox/identity/requests/update_role.rb +41 -0
- data/lib/fog/proxmox/identity/requests/update_token.rb +41 -0
- data/lib/fog/proxmox/identity/requests/update_user.rb +41 -0
- data/lib/fog/proxmox/identity.rb +144 -0
- data/lib/fog/proxmox/json.rb +32 -0
- data/lib/fog/proxmox/network/models/network.rb +76 -0
- data/lib/fog/proxmox/network/models/networks.rb +48 -0
- data/lib/fog/proxmox/network/models/node.rb +75 -0
- data/lib/fog/proxmox/network/models/nodes.rb +39 -0
- data/lib/fog/proxmox/network/requests/create_network.rb +41 -0
- data/lib/fog/proxmox/network/requests/delete_network.rb +42 -0
- data/lib/fog/proxmox/network/requests/get_network.rb +43 -0
- data/lib/fog/proxmox/network/requests/get_node.rb +41 -0
- data/lib/fog/proxmox/network/requests/list_networks.rb +43 -0
- data/lib/fog/proxmox/network/requests/list_nodes.rb +42 -0
- data/lib/fog/proxmox/network/requests/power_node.rb +43 -0
- data/lib/fog/proxmox/network/requests/update_network.rb +42 -0
- data/lib/fog/proxmox/network.rb +90 -0
- data/lib/fog/proxmox/storage/requests/create.rb +23 -0
- data/lib/fog/proxmox/storage/requests/download_appliance.rb +24 -0
- data/lib/fog/proxmox/storage/requests/list.rb +22 -0
- data/lib/fog/proxmox/storage/requests/list_appliances.rb +23 -0
- data/lib/fog/proxmox/storage/requests/upload.rb +44 -0
- data/lib/fog/proxmox/storage.rb +82 -0
- data/lib/fog/proxmox/string.rb +32 -0
- data/lib/fog/proxmox/variables.rb +40 -0
- data/lib/fog/proxmox/version.rb +24 -0
- data/lib/fog/proxmox.rb +53 -0
- data/spec/compute_spec.rb +447 -0
- data/spec/fixtures/proxmox/compute/common_auth.yml +40 -0
- data/spec/fixtures/proxmox/compute/containers.yml +6921 -0
- data/spec/fixtures/proxmox/compute/nodes.yml +115 -0
- data/spec/fixtures/proxmox/compute/servers.yml +19011 -0
- data/spec/fixtures/proxmox/compute/snapshots.yml +1376 -0
- data/spec/fixtures/proxmox/compute/storages.yml +151 -0
- data/spec/fixtures/proxmox/compute/tasks.yml +465 -0
- data/spec/fixtures/proxmox/identity/auth.yml +42 -0
- data/spec/fixtures/proxmox/identity/auth_access_ticket.yml +77 -0
- data/spec/fixtures/proxmox/identity/auth_user_token.yml +77 -0
- data/spec/fixtures/proxmox/identity/common_auth.yml +40 -0
- data/spec/fixtures/proxmox/identity/domains.yml +665 -0
- data/spec/fixtures/proxmox/identity/groups.yml +345 -0
- data/spec/fixtures/proxmox/identity/permissions.yml +911 -0
- data/spec/fixtures/proxmox/identity/pools.yml +946 -0
- data/spec/fixtures/proxmox/identity/read_version.yml +40 -0
- data/spec/fixtures/proxmox/identity/roles.yml +345 -0
- data/spec/fixtures/proxmox/identity/tokens.yml +494 -0
- data/spec/fixtures/proxmox/identity/users.yml +725 -0
- data/spec/fixtures/proxmox/network/common_auth.yml +40 -0
- data/spec/fixtures/proxmox/network/networks.yml +375 -0
- data/spec/fixtures/proxmox/pve.home +34 -0
- data/spec/hash_spec.rb +43 -0
- data/spec/helpers/controller_helper_spec.rb +157 -0
- data/spec/helpers/cpu_helper_spec.rb +82 -0
- data/spec/helpers/disk_helper_spec.rb +304 -0
- data/spec/helpers/ip_helper_spec.rb +177 -0
- data/spec/helpers/nic_helper_spec.rb +144 -0
- data/spec/identity_spec.rb +416 -0
- data/spec/network_spec.rb +67 -0
- data/spec/proxmox_vcr.rb +97 -0
- data/spec/spec_helper.rb +41 -0
- metadata +566 -0
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2018 Tristan Robert
|
4
|
+
|
5
|
+
# This file is part of Fog::Proxmox.
|
6
|
+
|
7
|
+
# Fog::Proxmox 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
|
+
# Fog::Proxmox 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 Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
|
19
|
+
|
20
|
+
module Fog
|
21
|
+
module Proxmox
|
22
|
+
# module Cpu mixins
|
23
|
+
module CpuHelper
|
24
|
+
CPU_REGEXP = /(\bcputype=)?(?<cputype>[\w-]+),?(\bflags=)?(?<flags>[[+-][\w-]+;?]*)/
|
25
|
+
FLAGS = { spectre: 'spec-ctrl', pcid: 'pcid', ssbd: 'ssbd', ibpb: 'ibpb', virt_ssbd: 'virt-ssbd',
|
26
|
+
amd_ssbd: 'amd-ssbd', amd_no_ssb: 'amd-no-ssb', md_clear: 'md-clear', pdpe1gb: 'pdpe1gb', hv_tlbflush: 'hv-tlbflush', aes: 'aes', hv_evmcs: 'hv-evmcs' }
|
27
|
+
def self.flags
|
28
|
+
FLAGS
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.extract(cpu, name)
|
32
|
+
captures_h = cpu ? CPU_REGEXP.match(cpu.to_s) : { cputype: '', flags: '' }
|
33
|
+
captures_h[name]
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.extract_cputype(cpu)
|
37
|
+
extract(cpu, :cputype)
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.extract_flags(cpu)
|
41
|
+
extract(cpu, :flags)
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.flag_value(cpu, flag_key)
|
45
|
+
flag_value = '0'
|
46
|
+
raw_values = extract_flags(cpu).split(';').select { |flag| ['+' + flag_key, '-' + flag_key].include?(flag) }
|
47
|
+
unless raw_values.empty?
|
48
|
+
flag_value = if raw_values[0].start_with?('+')
|
49
|
+
'+1'
|
50
|
+
else
|
51
|
+
raw_values[0].start_with?('-') ? '-1' : '0'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
flag_value
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.hash_has_no_default_flag?(cpu_h, flag_name)
|
58
|
+
cpu_h.key?(flag_name) && ['-1', '+1'].include?(cpu_h[flag_name])
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.hash_flag(cpu_h, flag_name)
|
62
|
+
flag = ''
|
63
|
+
if cpu_h.key?(flag_name)
|
64
|
+
flag = '+' if cpu_h[flag_name] == '+1'
|
65
|
+
flag = '-' if cpu_h[flag_name] == '-1'
|
66
|
+
end
|
67
|
+
flag
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.flatten(cpu_h)
|
71
|
+
return '' unless cpu_h['cpu_type']
|
72
|
+
|
73
|
+
cpu_type = "cputype=#{cpu_h['cpu_type']}"
|
74
|
+
num_flags = 0
|
75
|
+
FLAGS.each_key { |flag_key| num_flags += 1 if hash_has_no_default_flag?(cpu_h, flag_key.to_s) }
|
76
|
+
cpu_type += ',flags=' if num_flags > 0
|
77
|
+
flags_with_no_default_value = FLAGS.select do |flag_key, _flag_value|
|
78
|
+
hash_has_no_default_flag?(cpu_h, flag_key.to_s)
|
79
|
+
end
|
80
|
+
flags_with_no_default_value.each_with_index do |(flag_key, flag_value), index|
|
81
|
+
cpu_type += hash_flag(cpu_h, flag_key.to_s) + flag_value if hash_has_no_default_flag?(cpu_h, flag_key.to_s)
|
82
|
+
cpu_type += ';' if num_flags > index + 1
|
83
|
+
end
|
84
|
+
cpu_type
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,185 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2018 Tristan Robert
|
4
|
+
|
5
|
+
# This file is part of Fog::Proxmox.
|
6
|
+
|
7
|
+
# Fog::Proxmox 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
|
+
# Fog::Proxmox 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 Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
|
19
|
+
|
20
|
+
require 'fog/proxmox/hash'
|
21
|
+
|
22
|
+
module Fog
|
23
|
+
module Proxmox
|
24
|
+
# module Disk mixins
|
25
|
+
module DiskHelper
|
26
|
+
DISKS_REGEXP = /^(scsi|sata|mp|rootfs|virtio|ide)(\d+){0,1}$/
|
27
|
+
SERVER_DISK_REGEXP = /^(scsi|sata|virtio|ide)(\d+)$/
|
28
|
+
MOUNT_POINT_REGEXP = /^(mp)(\d+)$/
|
29
|
+
ROOTFS_REGEXP = /^(rootfs)$/
|
30
|
+
CDROM_REGEXP = /^(.*),{0,1}(media=cdrom),{0,1}(.*)$/
|
31
|
+
TEMPLATE_REGEXP = /^(.*)(base-)(.*)$/
|
32
|
+
CLOUD_INIT_REGEXP = /^(.*)(cloudinit)(.*)$/
|
33
|
+
|
34
|
+
# Convert disk attributes hash into API Proxmox parameters string
|
35
|
+
def self.flatten(disk)
|
36
|
+
id = disk[:id]
|
37
|
+
value = ''
|
38
|
+
if disk[:volid]
|
39
|
+
value += disk[:volid]
|
40
|
+
value += ',size=' + disk[:size].to_s if disk[:size]
|
41
|
+
elsif disk[:storage] && disk[:size]
|
42
|
+
value += disk[:storage] + ':' + disk[:size].to_s
|
43
|
+
elsif id == 'ide2'
|
44
|
+
value += 'none'
|
45
|
+
end
|
46
|
+
opts = disk[:options] if disk[:options]
|
47
|
+
main_a = %i[id volid storage size]
|
48
|
+
opts ||= disk.reject { |key, _value| main_a.include? key }
|
49
|
+
options = ''
|
50
|
+
options += Fog::Proxmox::Hash.stringify(opts) if opts
|
51
|
+
if id == 'ide2' && !cdrom?(options)
|
52
|
+
options += ',' unless options.empty?
|
53
|
+
options += 'media=cdrom'
|
54
|
+
end
|
55
|
+
value += ',' if !options.empty? && !value.empty?
|
56
|
+
value += options
|
57
|
+
{ "#{id}": value }
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.extract_controller(id)
|
61
|
+
extract(id)[0]
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.extract_device(id)
|
65
|
+
extract(id)[1].to_i
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.extract(id)
|
69
|
+
id.scan(/(\w+)(\d+)/).first
|
70
|
+
end
|
71
|
+
|
72
|
+
# Convert API Proxmox parameter string into attribute hash value
|
73
|
+
def self.extract_option(name, disk_value)
|
74
|
+
values = disk_value.scan(/#{name}=(\w+)/)
|
75
|
+
name_value = values.first if values
|
76
|
+
name_value&.first
|
77
|
+
end
|
78
|
+
|
79
|
+
# Convert API Proxmox volume/disk parameter string into volume/disk attributes hash value
|
80
|
+
def self.extract_storage_volid_size(disk_value)
|
81
|
+
# volid definition: <VOLUME_ID>:=<STORAGE_ID>:<storage type dependent volume name>
|
82
|
+
values_a = disk_value.scan(%r{^(([\w-]+):{0,1}([\w/.-]+))})
|
83
|
+
no_cdrom = !disk_value.match(CDROM_REGEXP)
|
84
|
+
creation = disk_value.split(',')[0].match(/^(([\w-]+):{1}(\d+))$/)
|
85
|
+
values = values_a.first if values_a
|
86
|
+
if no_cdrom
|
87
|
+
storage = values[1]
|
88
|
+
if creation
|
89
|
+
volid = nil
|
90
|
+
size = values[2].to_i
|
91
|
+
else
|
92
|
+
volid = values[0]
|
93
|
+
size = extract_size(disk_value)
|
94
|
+
end
|
95
|
+
else
|
96
|
+
volid = values[0]
|
97
|
+
storage = values[1] unless %w[none cdrom].include? volid
|
98
|
+
size = nil
|
99
|
+
end
|
100
|
+
[storage, volid, size]
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.to_bytes(size)
|
104
|
+
val = size.match(/\d+(\w?)/)
|
105
|
+
m = 0
|
106
|
+
case val[1]
|
107
|
+
when 'K' then m = 1
|
108
|
+
when 'M' then m = 2
|
109
|
+
when 'G' then m = 3
|
110
|
+
when 'T' then m = 4
|
111
|
+
when 'P' then m = 5
|
112
|
+
end
|
113
|
+
val[0].to_i * 1024**m
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.modulo_bytes(size)
|
117
|
+
size / 1024
|
118
|
+
end
|
119
|
+
|
120
|
+
def self.to_human_bytes(size)
|
121
|
+
units = %w[Kb Mb Gb]
|
122
|
+
i = 0
|
123
|
+
human_size = size.to_s + 'b'
|
124
|
+
while i < 3 && size >= 1024
|
125
|
+
size = modulo_bytes(size)
|
126
|
+
human_size = size.to_s + units[i]
|
127
|
+
i += 1
|
128
|
+
end
|
129
|
+
human_size
|
130
|
+
end
|
131
|
+
|
132
|
+
def self.to_int_gb(size_bytes)
|
133
|
+
val = to_human_bytes(size_bytes.to_i).match(/\d+(Gb)/)
|
134
|
+
val ? val[0].to_i : 0
|
135
|
+
end
|
136
|
+
|
137
|
+
def self.extract_size(disk_value)
|
138
|
+
size = extract_option('size', disk_value)
|
139
|
+
size = to_int_gb(to_bytes(size)).to_s if size.match?(/\d+(G)/)
|
140
|
+
size
|
141
|
+
end
|
142
|
+
|
143
|
+
def self.disk?(id)
|
144
|
+
DISKS_REGEXP.match(id) ? true : false
|
145
|
+
end
|
146
|
+
|
147
|
+
def self.cdrom?(value)
|
148
|
+
CDROM_REGEXP.match(value) ? true : false
|
149
|
+
end
|
150
|
+
|
151
|
+
def self.server_disk?(id)
|
152
|
+
SERVER_DISK_REGEXP.match(id) ? true : false
|
153
|
+
end
|
154
|
+
|
155
|
+
def self.rootfs?(id)
|
156
|
+
ROOTFS_REGEXP.match(id) ? true : false
|
157
|
+
end
|
158
|
+
|
159
|
+
def self.mount_point?(id)
|
160
|
+
MOUNT_POINT_REGEXP.match(id) ? true : false
|
161
|
+
end
|
162
|
+
|
163
|
+
def self.container_disk?(id)
|
164
|
+
rootfs?(id) || mount_point?(id)
|
165
|
+
end
|
166
|
+
|
167
|
+
def self.template?(volid)
|
168
|
+
TEMPLATE_REGEXP.match(volid) ? true : false
|
169
|
+
end
|
170
|
+
|
171
|
+
def self.cloud_init?(volid)
|
172
|
+
CLOUD_INIT_REGEXP.match(volid) ? true : false
|
173
|
+
end
|
174
|
+
|
175
|
+
def self.of_type?(disk_h, vm_type)
|
176
|
+
id = disk_h['id'] if disk_h.key?('id')
|
177
|
+
id = disk_h[:id] if disk_h.key?(:id)
|
178
|
+
result = false
|
179
|
+
result = server_disk?(id) if vm_type == 'qemu'
|
180
|
+
result = container_disk?(id) if vm_type == 'lxc'
|
181
|
+
result
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2018 Tristan Robert
|
4
|
+
|
5
|
+
# This file is part of Fog::Proxmox.
|
6
|
+
|
7
|
+
# Fog::Proxmox 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
|
+
# Fog::Proxmox 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 Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
|
19
|
+
|
20
|
+
require 'resolv'
|
21
|
+
require 'fog/proxmox/hash'
|
22
|
+
|
23
|
+
module Fog
|
24
|
+
module Proxmox
|
25
|
+
# module IpHelper mixins
|
26
|
+
module IpHelper
|
27
|
+
CIDRv4_PREFIX = '([0-9]|[1-2][0-9]|3[0-2])'
|
28
|
+
CIDRv4_PREFIX_REGEXP = /^#{CIDRv4_PREFIX}$/
|
29
|
+
IPv4_SRC = "#{Resolv::IPv4::Regex.source.delete_suffix('\z').delete_prefix('\A')}"
|
30
|
+
CIDRv4_REGEXP = Regexp.new("\\A(#{IPv4_SRC})(\/#{CIDRv4_PREFIX})?\\z")
|
31
|
+
CIDRv6_PREFIX = '(\d+)'
|
32
|
+
CIDRv6_PREFIX_REGEXP = /^#{CIDRv6_PREFIX}$/xi
|
33
|
+
IPv6_SRC = "#{Resolv::IPv6::Regex_8Hex.source.delete_suffix('\z').delete_prefix('\A')}"
|
34
|
+
CIDRv6_REGEXP = Regexp.new("\\A(#{IPv6_SRC})(\/#{CIDRv6_PREFIX})?\\z", Regexp::EXTENDED | Regexp::IGNORECASE)
|
35
|
+
|
36
|
+
def self.cidr?(ip)
|
37
|
+
CIDRv4_REGEXP.match?(ip)
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.cidr6?(ip)
|
41
|
+
CIDRv6_REGEXP.match?(ip)
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.prefix(ip)
|
45
|
+
return unless cidr = CIDRv4_REGEXP.match(ip)
|
46
|
+
|
47
|
+
cidr[7]
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.prefix6(ip)
|
51
|
+
return unless cidr = CIDRv6_REGEXP.match(ip)
|
52
|
+
|
53
|
+
cidr[3]
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.ip?(ip)
|
57
|
+
Resolv::IPv4::Regex.match?(ip)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.ip6?(ip)
|
61
|
+
Resolv::IPv6::Regex.match?(ip)
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.cidr_prefix?(prefix)
|
65
|
+
CIDRv4_PREFIX_REGEXP.match?(prefix)
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.cidr6_prefix?(prefix)
|
69
|
+
CIDRv6_PREFIX_REGEXP.match?(prefix) && prefix.to_i >= 0 && prefix.to_i <= 128
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.ip(ip)
|
73
|
+
return unless cidr = CIDRv4_REGEXP.match(ip)
|
74
|
+
|
75
|
+
cidr[1]
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.ip6(ip)
|
79
|
+
return unless cidr = CIDRv6_REGEXP.match(ip)
|
80
|
+
|
81
|
+
cidr[1]
|
82
|
+
end
|
83
|
+
|
84
|
+
def self.to_cidr(ip, prefix = nil)
|
85
|
+
return nil unless ip?(ip) && (!prefix || cidr_prefix?(prefix))
|
86
|
+
|
87
|
+
cidr = "#{ip}"
|
88
|
+
cidr += "/#{prefix}" if cidr_prefix?(prefix)
|
89
|
+
cidr
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.to_cidr6(ip, prefix = nil)
|
93
|
+
return nil unless ip6?(ip) && (!prefix || cidr6_prefix?(prefix))
|
94
|
+
|
95
|
+
cidr = "#{ip}"
|
96
|
+
cidr += "/#{prefix}" if cidr6_prefix?(prefix)
|
97
|
+
cidr
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2018 Tristan Robert
|
4
|
+
|
5
|
+
# This file is part of Fog::Proxmox.
|
6
|
+
|
7
|
+
# Fog::Proxmox 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
|
+
# Fog::Proxmox 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 Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
|
19
|
+
|
20
|
+
require 'fog/proxmox/hash'
|
21
|
+
|
22
|
+
module Fog
|
23
|
+
module Proxmox
|
24
|
+
# module NicHelper mixins
|
25
|
+
module NicHelper
|
26
|
+
NICS_REGEXP = /^(net)(\d+)/
|
27
|
+
|
28
|
+
def self.extract_mac_address(nic_value)
|
29
|
+
nic_value[/([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})/]
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.model_regexp
|
33
|
+
/^model=(\w+),.+/
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.name_regexp
|
37
|
+
/^name=(\w+),.+/
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.ip_regexp
|
41
|
+
%r{^(.+),{1}ip=([\d./]+),?(.+)?$}
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.nic_update_regexp
|
45
|
+
/^(\w+)={1}([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}).+/
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.has_model?(nic_value)
|
49
|
+
nic_value.match?(model_regexp)
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.has_name?(nic_value)
|
53
|
+
nic_value.match?(name_regexp)
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.has_ip?(nic_value)
|
57
|
+
nic_value.match?(ip_regexp)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.extract_nic_id(nic_value)
|
61
|
+
if has_model?(nic_value)
|
62
|
+
nic_value.scan(model_regexp).first.first
|
63
|
+
elsif has_name?(nic_value)
|
64
|
+
nic_value.scan(name_regexp).first.first
|
65
|
+
else
|
66
|
+
nic_value.scan(nic_update_regexp).first.first
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.to_mac_adresses_array(nics)
|
71
|
+
addresses = []
|
72
|
+
nics.each { |nic| addresses.push(nic.macaddr) }
|
73
|
+
addresses
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.nic_name(nic)
|
77
|
+
if nic.has_key?(:model)
|
78
|
+
'model'
|
79
|
+
elsif nic.has_key?(:name)
|
80
|
+
'name'
|
81
|
+
else
|
82
|
+
''
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.set_mac(nic_id, nic_hash)
|
87
|
+
mac_keys = %i[macaddr hwaddr]
|
88
|
+
nic_value = ''
|
89
|
+
if (nic_hash.keys & mac_keys).empty?
|
90
|
+
nic_value = nic_name(nic_hash) + '=' + nic_id
|
91
|
+
else
|
92
|
+
mac_value = nic_hash[mac_keys[0]] if nic_hash.key?(mac_keys[0])
|
93
|
+
mac_value ||= nic_hash[mac_keys[1]] if nic_hash.key?(mac_keys[1])
|
94
|
+
nic_value = nic_id + '=' + mac_value
|
95
|
+
end
|
96
|
+
nic_value
|
97
|
+
end
|
98
|
+
|
99
|
+
# Convert nic attributes hash into API Proxmox parameters string
|
100
|
+
def self.flatten(nic_hash)
|
101
|
+
if nic_hash[:hwaddr].nil?
|
102
|
+
nic_id = nic_hash[nic_name(nic_hash).to_sym]
|
103
|
+
nic_value = set_mac(nic_id, nic_hash)
|
104
|
+
options = nic_hash.reject do |key, _value|
|
105
|
+
[nic_name(nic_hash).to_sym, :id, :hwaddr, :macaddr].include? key.to_sym
|
106
|
+
end
|
107
|
+
nic_value += ',' + Fog::Proxmox::Hash.stringify(options) unless options.empty?
|
108
|
+
else
|
109
|
+
options = nic_hash.reject do |key, _value|
|
110
|
+
%i[id model macaddr].include? key.to_sym
|
111
|
+
end
|
112
|
+
nic_value = Fog::Proxmox::Hash.stringify(options) unless options.empty?
|
113
|
+
end
|
114
|
+
{ "#{nic_hash[:id]}": nic_value }
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.collect_nics(attributes)
|
118
|
+
attributes.select { |key| nic?(key.to_s) }
|
119
|
+
end
|
120
|
+
|
121
|
+
def self.nic?(id)
|
122
|
+
NICS_REGEXP.match(id) ? true : false
|
123
|
+
end
|
124
|
+
|
125
|
+
def self.extract_ip(nic_value)
|
126
|
+
ip_regexp.match(nic_value) do |ip|
|
127
|
+
ip[2]
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# Copyright 2018 Tristan Robert
|
2
|
+
|
3
|
+
# This file is part of Fog::Proxmox.
|
4
|
+
|
5
|
+
# Fog::Proxmox 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
|
+
# Fog::Proxmox 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 Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module Fog
|
19
|
+
module Proxmox
|
20
|
+
class Identity
|
21
|
+
# class Domain model authentication
|
22
|
+
class Domain < Fog::Model
|
23
|
+
identity :realm
|
24
|
+
attribute :comment
|
25
|
+
attribute :tfa
|
26
|
+
attribute :type
|
27
|
+
|
28
|
+
def initialize(new_attributes = {})
|
29
|
+
initialize_type(new_attributes)
|
30
|
+
super(new_attributes)
|
31
|
+
end
|
32
|
+
|
33
|
+
def save(new_attributes = {})
|
34
|
+
service.create_domain(type.attributes.merge(new_attributes).merge(attributes.reject do |attribute|
|
35
|
+
[:type].include? attribute
|
36
|
+
end))
|
37
|
+
reload
|
38
|
+
end
|
39
|
+
|
40
|
+
def destroy
|
41
|
+
requires :realm
|
42
|
+
service.delete_domain(realm)
|
43
|
+
true
|
44
|
+
end
|
45
|
+
|
46
|
+
def update
|
47
|
+
requires :realm
|
48
|
+
service.update_domain(realm, type.attributes.merge(attributes).reject do |attribute|
|
49
|
+
%i[type realm].include? attribute
|
50
|
+
end)
|
51
|
+
reload
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def initialize_type(new_attributes = {})
|
57
|
+
if new_attributes.has_key? :realm
|
58
|
+
realm = new_attributes.delete(:realm)
|
59
|
+
elsif new_attributes.has_key? 'realm'
|
60
|
+
realm = new_attributes.delete('realm')
|
61
|
+
end
|
62
|
+
attributes[:type] = Fog::Proxmox::Identity::DomainType.new(new_attributes)
|
63
|
+
new_attributes.delete_if { |new_attribute| attributes[:type].attributes.has_key? new_attribute.to_sym }
|
64
|
+
new_attributes.store(:realm, realm)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# Copyright 2018 Tristan Robert
|
2
|
+
|
3
|
+
# This file is part of Fog::Proxmox.
|
4
|
+
|
5
|
+
# Fog::Proxmox 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
|
+
# Fog::Proxmox 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 Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module Fog
|
19
|
+
module Proxmox
|
20
|
+
class Identity
|
21
|
+
# class Domain type model authentication
|
22
|
+
class DomainType < Fog::Model
|
23
|
+
identity :type
|
24
|
+
attribute :id
|
25
|
+
attribute :key
|
26
|
+
attribute :url
|
27
|
+
attribute :base_dn
|
28
|
+
attribute :bind_dn
|
29
|
+
attribute :capath
|
30
|
+
attribute :cert
|
31
|
+
attribute :certkey
|
32
|
+
attribute :comment
|
33
|
+
attribute :default
|
34
|
+
attribute :domain
|
35
|
+
attribute :port
|
36
|
+
attribute :secure
|
37
|
+
attribute :server1
|
38
|
+
attribute :server2
|
39
|
+
attribute :tfa
|
40
|
+
attribute :user_attr
|
41
|
+
attribute :verify
|
42
|
+
attribute :step
|
43
|
+
attribute :digits
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2018 Tristan Robert
|
4
|
+
|
5
|
+
# This file is part of Fog::Proxmox.
|
6
|
+
|
7
|
+
# Fog::Proxmox 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
|
+
# Fog::Proxmox 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 Fog::Proxmox. If not, see <http://www.gnu.org/licenses/>.
|
19
|
+
|
20
|
+
require 'fog/proxmox/identity/models/domain'
|
21
|
+
|
22
|
+
module Fog
|
23
|
+
module Proxmox
|
24
|
+
class Identity
|
25
|
+
# class Domains collection authentication
|
26
|
+
class Domains < Fog::Collection
|
27
|
+
model Fog::Proxmox::Identity::Domain
|
28
|
+
|
29
|
+
def all
|
30
|
+
load service.list_domains
|
31
|
+
end
|
32
|
+
|
33
|
+
def get(id)
|
34
|
+
all.find { |domain| domain.identity == id }
|
35
|
+
end
|
36
|
+
|
37
|
+
def destroy(id)
|
38
|
+
domain = get(id)
|
39
|
+
domain.destroy
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|