fog-proxmox 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.bundle/config +4 -0
- data/.codeclimate.yml +14 -0
- data/.github/CODE_OF_CONDUCT.md +74 -0
- data/.github/CONTRIBUTING.md +20 -0
- data/.github/CONTRIBUTORS.md +9 -0
- data/.github/FUNDING.yml +12 -0
- data/.github/ISSUE_TEMPLATE.md +43 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +35 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +17 -0
- data/.github/SUPPORT.md +9 -0
- data/.github/fogproxmox.png +0 -0
- data/.github/workflows/ci.yml +79 -0
- data/.gitignore +8 -0
- data/.rubocop.yml +13 -0
- data/.ruby-gemset +1 -0
- data/.solargraph.yml +10 -0
- data/.vscode/launch.json +96 -0
- data/.vscode/settings.json +45 -0
- data/.vscode/tasks.json +27 -0
- data/Gemfile +23 -0
- data/LICENSE +674 -0
- data/README.md +79 -0
- data/Rakefile +58 -0
- data/docs/compute.md +858 -0
- data/docs/connection_parameters.md +45 -0
- data/docs/getting_started.md +53 -0
- data/docs/identity.md +357 -0
- data/examples/compute.rb +276 -0
- data/examples/identity.rb +190 -0
- data/fog-proxmox.gemspec +63 -0
- data/lib/fog/compute/proxmox.rb +126 -0
- data/lib/fog/compute/proxmox/models/disk.rb +76 -0
- data/lib/fog/compute/proxmox/models/disks.rb +51 -0
- data/lib/fog/compute/proxmox/models/interface.rb +52 -0
- data/lib/fog/compute/proxmox/models/interfaces.rb +44 -0
- data/lib/fog/compute/proxmox/models/node.rb +95 -0
- data/lib/fog/compute/proxmox/models/nodes.rb +39 -0
- data/lib/fog/compute/proxmox/models/server.rb +254 -0
- data/lib/fog/compute/proxmox/models/server_config.rb +148 -0
- data/lib/fog/compute/proxmox/models/servers.rb +80 -0
- data/lib/fog/compute/proxmox/models/snapshot.rb +83 -0
- data/lib/fog/compute/proxmox/models/snapshots.rb +47 -0
- data/lib/fog/compute/proxmox/models/storage.rb +64 -0
- data/lib/fog/compute/proxmox/models/storages.rb +51 -0
- data/lib/fog/compute/proxmox/models/task.rb +79 -0
- data/lib/fog/compute/proxmox/models/tasks.rb +64 -0
- data/lib/fog/compute/proxmox/models/volume.rb +61 -0
- data/lib/fog/compute/proxmox/models/volumes.rb +58 -0
- data/lib/fog/compute/proxmox/requests/action_server.rb +45 -0
- data/lib/fog/compute/proxmox/requests/clone_server.rb +44 -0
- data/lib/fog/compute/proxmox/requests/create_backup.rb +42 -0
- data/lib/fog/compute/proxmox/requests/create_server.rb +43 -0
- data/lib/fog/compute/proxmox/requests/create_snapshot.rb +46 -0
- data/lib/fog/compute/proxmox/requests/create_spice.rb +44 -0
- data/lib/fog/compute/proxmox/requests/create_term.rb +44 -0
- data/lib/fog/compute/proxmox/requests/create_vnc.rb +44 -0
- data/lib/fog/compute/proxmox/requests/delete_server.rb +45 -0
- data/lib/fog/compute/proxmox/requests/delete_snapshot.rb +47 -0
- data/lib/fog/compute/proxmox/requests/delete_volume.rb +40 -0
- data/lib/fog/compute/proxmox/requests/get_node_statistics.rb +44 -0
- data/lib/fog/compute/proxmox/requests/get_server_config.rb +60 -0
- data/lib/fog/compute/proxmox/requests/get_server_status.rb +63 -0
- data/lib/fog/compute/proxmox/requests/get_snapshot_config.rb +44 -0
- data/lib/fog/compute/proxmox/requests/get_task.rb +41 -0
- data/lib/fog/compute/proxmox/requests/get_vnc.rb +44 -0
- data/lib/fog/compute/proxmox/requests/get_volume.rb +42 -0
- data/lib/fog/compute/proxmox/requests/list_nodes.rb +48 -0
- data/lib/fog/compute/proxmox/requests/list_servers.rb +113 -0
- data/lib/fog/compute/proxmox/requests/list_snapshots.rb +66 -0
- data/lib/fog/compute/proxmox/requests/list_storages.rb +46 -0
- data/lib/fog/compute/proxmox/requests/list_tasks.rb +44 -0
- data/lib/fog/compute/proxmox/requests/list_volumes.rb +44 -0
- data/lib/fog/compute/proxmox/requests/log_task.rb +44 -0
- data/lib/fog/compute/proxmox/requests/migrate_server.rb +44 -0
- data/lib/fog/compute/proxmox/requests/move_disk.rb +43 -0
- data/lib/fog/compute/proxmox/requests/move_volume.rb +43 -0
- data/lib/fog/compute/proxmox/requests/next_vmid.rb +42 -0
- data/lib/fog/compute/proxmox/requests/resize_container.rb +43 -0
- data/lib/fog/compute/proxmox/requests/resize_server.rb +43 -0
- data/lib/fog/compute/proxmox/requests/rollback_snapshot.rb +48 -0
- data/lib/fog/compute/proxmox/requests/status_task.rb +41 -0
- data/lib/fog/compute/proxmox/requests/stop_task.rb +41 -0
- data/lib/fog/compute/proxmox/requests/template_server.rb +44 -0
- data/lib/fog/compute/proxmox/requests/update_server.rb +44 -0
- data/lib/fog/compute/proxmox/requests/update_snapshot.rb +45 -0
- data/lib/fog/identity/proxmox.rb +130 -0
- data/lib/fog/identity/proxmox/models/domain.rb +65 -0
- data/lib/fog/identity/proxmox/models/domain_type.rb +48 -0
- data/lib/fog/identity/proxmox/models/domains.rb +45 -0
- data/lib/fog/identity/proxmox/models/group.rb +47 -0
- data/lib/fog/identity/proxmox/models/groups.rb +44 -0
- data/lib/fog/identity/proxmox/models/permission.rb +65 -0
- data/lib/fog/identity/proxmox/models/permissions.rb +44 -0
- data/lib/fog/identity/proxmox/models/pool.rb +89 -0
- data/lib/fog/identity/proxmox/models/pools.rb +46 -0
- data/lib/fog/identity/proxmox/models/principal.rb +42 -0
- data/lib/fog/identity/proxmox/models/role.rb +48 -0
- data/lib/fog/identity/proxmox/models/roles.rb +44 -0
- data/lib/fog/identity/proxmox/models/user.rb +60 -0
- data/lib/fog/identity/proxmox/models/users.rb +44 -0
- data/lib/fog/identity/proxmox/requests/change_password.rb +41 -0
- data/lib/fog/identity/proxmox/requests/check_permissions.rb +41 -0
- data/lib/fog/identity/proxmox/requests/create_domain.rb +40 -0
- data/lib/fog/identity/proxmox/requests/create_group.rb +40 -0
- data/lib/fog/identity/proxmox/requests/create_pool.rb +40 -0
- data/lib/fog/identity/proxmox/requests/create_role.rb +40 -0
- data/lib/fog/identity/proxmox/requests/create_user.rb +40 -0
- data/lib/fog/identity/proxmox/requests/delete_domain.rb +40 -0
- data/lib/fog/identity/proxmox/requests/delete_group.rb +40 -0
- data/lib/fog/identity/proxmox/requests/delete_pool.rb +39 -0
- data/lib/fog/identity/proxmox/requests/delete_role.rb +40 -0
- data/lib/fog/identity/proxmox/requests/delete_user.rb +39 -0
- data/lib/fog/identity/proxmox/requests/get_domain.rb +41 -0
- data/lib/fog/identity/proxmox/requests/get_group.rb +41 -0
- data/lib/fog/identity/proxmox/requests/get_pool.rb +41 -0
- data/lib/fog/identity/proxmox/requests/get_role.rb +41 -0
- data/lib/fog/identity/proxmox/requests/get_user.rb +40 -0
- data/lib/fog/identity/proxmox/requests/list_domains.rb +41 -0
- data/lib/fog/identity/proxmox/requests/list_groups.rb +41 -0
- data/lib/fog/identity/proxmox/requests/list_permissions.rb +41 -0
- data/lib/fog/identity/proxmox/requests/list_pools.rb +41 -0
- data/lib/fog/identity/proxmox/requests/list_roles.rb +40 -0
- data/lib/fog/identity/proxmox/requests/list_users.rb +42 -0
- data/lib/fog/identity/proxmox/requests/read_version.rb +39 -0
- data/lib/fog/identity/proxmox/requests/update_domain.rb +41 -0
- data/lib/fog/identity/proxmox/requests/update_group.rb +40 -0
- data/lib/fog/identity/proxmox/requests/update_permissions.rb +41 -0
- data/lib/fog/identity/proxmox/requests/update_pool.rb +41 -0
- data/lib/fog/identity/proxmox/requests/update_role.rb +41 -0
- data/lib/fog/identity/proxmox/requests/update_user.rb +41 -0
- data/lib/fog/network/proxmox.rb +83 -0
- data/lib/fog/network/proxmox/models/network.rb +76 -0
- data/lib/fog/network/proxmox/models/networks.rb +48 -0
- data/lib/fog/network/proxmox/models/node.rb +74 -0
- data/lib/fog/network/proxmox/models/nodes.rb +39 -0
- data/lib/fog/network/proxmox/requests/create_network.rb +41 -0
- data/lib/fog/network/proxmox/requests/delete_network.rb +42 -0
- data/lib/fog/network/proxmox/requests/get_network.rb +43 -0
- data/lib/fog/network/proxmox/requests/get_node.rb +41 -0
- data/lib/fog/network/proxmox/requests/list_networks.rb +43 -0
- data/lib/fog/network/proxmox/requests/list_nodes.rb +42 -0
- data/lib/fog/network/proxmox/requests/power_node.rb +43 -0
- data/lib/fog/network/proxmox/requests/update_network.rb +42 -0
- data/lib/fog/proxmox.rb +115 -0
- data/lib/fog/proxmox/attributes.rb +33 -0
- data/lib/fog/proxmox/core.rb +135 -0
- data/lib/fog/proxmox/errors.rb +64 -0
- data/lib/fog/proxmox/hash.rb +39 -0
- data/lib/fog/proxmox/helpers/controller_helper.rb +64 -0
- data/lib/fog/proxmox/helpers/cpu_helper.rb +42 -0
- data/lib/fog/proxmox/helpers/disk_helper.rb +159 -0
- data/lib/fog/proxmox/helpers/ip_helper.rb +100 -0
- data/lib/fog/proxmox/helpers/nic_helper.rb +115 -0
- data/lib/fog/proxmox/json.rb +32 -0
- data/lib/fog/proxmox/string.rb +31 -0
- data/lib/fog/proxmox/variables.rb +40 -0
- data/lib/fog/proxmox/version.rb +24 -0
- data/lib/fog/storage/proxmox.rb +29 -0
- data/spec/compute_spec.rb +446 -0
- data/spec/fixtures/proxmox/compute/containers.yml +12737 -0
- data/spec/fixtures/proxmox/compute/identity_ticket.yml +40 -0
- data/spec/fixtures/proxmox/compute/nodes.yml +121 -0
- data/spec/fixtures/proxmox/compute/servers.yml +24679 -0
- data/spec/fixtures/proxmox/compute/snapshots.yml +3321 -0
- data/spec/fixtures/proxmox/compute/storages.yml +159 -0
- data/spec/fixtures/proxmox/compute/tasks.yml +685 -0
- data/spec/fixtures/proxmox/identity/auth.yml +44 -0
- data/spec/fixtures/proxmox/identity/domains.yml +701 -0
- data/spec/fixtures/proxmox/identity/groups.yml +363 -0
- data/spec/fixtures/proxmox/identity/identity_ticket.yml +40 -0
- data/spec/fixtures/proxmox/identity/permissions.yml +803 -0
- data/spec/fixtures/proxmox/identity/pools.yml +1191 -0
- data/spec/fixtures/proxmox/identity/read_version.yml +42 -0
- data/spec/fixtures/proxmox/identity/renew.yml +40 -0
- data/spec/fixtures/proxmox/identity/roles.yml +363 -0
- data/spec/fixtures/proxmox/identity/users.yml +763 -0
- data/spec/fixtures/proxmox/network/identity_ticket.yml +40 -0
- data/spec/fixtures/proxmox/network/networks.yml +372 -0
- data/spec/fixtures/proxmox/pve.home +34 -0
- data/spec/hash_spec.rb +42 -0
- data/spec/helpers/controller_helper_spec.rb +145 -0
- data/spec/helpers/cpu_helper_spec.rb +70 -0
- data/spec/helpers/disk_helper_spec.rb +210 -0
- data/spec/helpers/ip_helper_spec.rb +160 -0
- data/spec/helpers/nic_helper_spec.rb +136 -0
- data/spec/identity_spec.rb +322 -0
- data/spec/network_spec.rb +67 -0
- data/spec/proxmox_vcr.rb +95 -0
- data/spec/spec_helper.rb +40 -0
- metadata +547 -0
@@ -0,0 +1,42 @@
|
|
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=)?([\w-]+)[,]?(\bflags=)?(\+[\w-]+)?[;]?(\+[\w-]+)?/
|
25
|
+
def self.extract(cpu,i)
|
26
|
+
cpu ? CPU_REGEXP.match(cpu.to_s)[i] : ''
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.extract_type(cpu)
|
30
|
+
extract(cpu,2)
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.has_pcid?(cpu)
|
34
|
+
extract(cpu,5) == '+pcid'
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.has_spectre?(cpu)
|
38
|
+
extract(cpu,4) == '+spec-ctrl'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,159 @@
|
|
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
|
+
|
27
|
+
DISKS_REGEXP = /^(scsi|sata|mp|rootfs|virtio|ide)(\d+){0,1}$/
|
28
|
+
SERVER_DISK_REGEXP = /^(scsi|sata|virtio|ide)(\d+)$/
|
29
|
+
MOUNT_POINT_REGEXP = /^(mp)(\d+)$/
|
30
|
+
ROOTFS_REGEXP = /^(rootfs)$/
|
31
|
+
CDROM_REGEXP = /^(.*)[,]{0,1}(media=cdrom)[,]{0,1}(.*)$/
|
32
|
+
|
33
|
+
def self.flatten(disk)
|
34
|
+
id = disk[:id]
|
35
|
+
value = ''
|
36
|
+
if disk[:volid]
|
37
|
+
value += disk[:volid]
|
38
|
+
value += ',size=' + disk[:size].to_s if disk[:size]
|
39
|
+
elsif disk[:storage] && disk[:size]
|
40
|
+
value += disk[:storage] + ':' + disk[:size].to_s
|
41
|
+
elsif id == 'ide2'
|
42
|
+
value += 'none'
|
43
|
+
end
|
44
|
+
opts = disk[:options] if disk[:options]
|
45
|
+
main_a = [:id,:volid,:storage,:size]
|
46
|
+
opts = disk.reject { |key,_value| main_a.include? key } unless opts
|
47
|
+
options = ''
|
48
|
+
options += Fog::Proxmox::Hash.stringify(opts) if opts
|
49
|
+
if id == 'ide2' && !self.cdrom?(options)
|
50
|
+
options += ',' if !options.empty?
|
51
|
+
options += 'media=cdrom'
|
52
|
+
end
|
53
|
+
value += ',' if !options.empty? && !value.empty?
|
54
|
+
value += options
|
55
|
+
{ "#{id}": value }
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.extract_controller(id)
|
59
|
+
extract(id)[0]
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.extract_device(id)
|
63
|
+
extract(id)[1].to_i
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.extract(id)
|
67
|
+
id.scan(/(\w+)(\d+)/).first
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.extract_option(name, disk_value)
|
71
|
+
values = disk_value.scan(/#{name}=(\w+)/)
|
72
|
+
name_value = values.first if values
|
73
|
+
name_value&.first
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.extract_storage_volid_size(disk_value)
|
77
|
+
#volid definition: <VOULME_ID>:=<STORAGE_ID>:<storage type dependent volume name>
|
78
|
+
values_a = disk_value.scan(/^(([\w-]+)[:]{0,1}([\w\/\.-]+))/)
|
79
|
+
no_cdrom = !disk_value.match(CDROM_REGEXP)
|
80
|
+
creation = disk_value.split(',')[0].match(/^(([\w-]+)[:]{1}([\d]+))$/)
|
81
|
+
values = values_a.first if values_a
|
82
|
+
if no_cdrom
|
83
|
+
if creation
|
84
|
+
storage = values[1]
|
85
|
+
volid = nil
|
86
|
+
size = values[2].to_i
|
87
|
+
else
|
88
|
+
storage = values[1]
|
89
|
+
volid = values[0]
|
90
|
+
size = extract_size(disk_value)
|
91
|
+
end
|
92
|
+
else
|
93
|
+
volid = values[0]
|
94
|
+
storage = values[1] unless %w[none cdrom].include? volid
|
95
|
+
size = nil
|
96
|
+
end
|
97
|
+
[storage, volid, size]
|
98
|
+
end
|
99
|
+
|
100
|
+
def self.to_bytes(size)
|
101
|
+
val=size.match(/\d+(\w?)/)
|
102
|
+
m=0
|
103
|
+
case val[1]
|
104
|
+
when "K" then m=1
|
105
|
+
when "M" then m=2
|
106
|
+
when "G" then m=3
|
107
|
+
when "T" then m=4
|
108
|
+
when "P" then m=5
|
109
|
+
end
|
110
|
+
val[0].to_i*1024**m
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.modulo_bytes(size)
|
114
|
+
size / 1024
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.to_human_bytes(size)
|
118
|
+
units = ['Kb','Mb','Gb','Tb','Pb']
|
119
|
+
i = 0
|
120
|
+
human_size = size.to_s + 'b'
|
121
|
+
while i < 5 && size >= 1024
|
122
|
+
size = self.modulo_bytes(size)
|
123
|
+
human_size = size.to_s + units[i]
|
124
|
+
i+=1
|
125
|
+
end
|
126
|
+
human_size
|
127
|
+
end
|
128
|
+
|
129
|
+
def self.extract_size(disk_value)
|
130
|
+
size=extract_option('size', disk_value)
|
131
|
+
size ? self.to_bytes(size) : "1G"
|
132
|
+
end
|
133
|
+
|
134
|
+
def self.disk?(id)
|
135
|
+
DISKS_REGEXP.match(id) ? true : false
|
136
|
+
end
|
137
|
+
|
138
|
+
def self.cdrom?(value)
|
139
|
+
CDROM_REGEXP.match(value) ? true : false
|
140
|
+
end
|
141
|
+
|
142
|
+
def self.server_disk?(id)
|
143
|
+
SERVER_DISK_REGEXP.match(id) ? true : false
|
144
|
+
end
|
145
|
+
|
146
|
+
def self.rootfs?(id)
|
147
|
+
ROOTFS_REGEXP.match(id) ? true : false
|
148
|
+
end
|
149
|
+
|
150
|
+
def self.mount_point?(id)
|
151
|
+
MOUNT_POINT_REGEXP.match(id) ? true : false
|
152
|
+
end
|
153
|
+
|
154
|
+
def self.container_disk?(id)
|
155
|
+
rootfs?(id) || mount_point?(id)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
@@ -0,0 +1,100 @@
|
|
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
|
+
|
28
|
+
CIDRv4_PREFIX = '([0-9]|[1-2][0-9]|3[0-2])'
|
29
|
+
CIDRv4_PREFIX_REGEXP = /^#{CIDRv4_PREFIX}$/
|
30
|
+
IPv4_SRC = "#{Resolv::IPv4::Regex.source.delete_suffix('\z').delete_prefix('\A')}"
|
31
|
+
CIDRv4_REGEXP = Regexp.new("\\A(#{IPv4_SRC})(\/#{CIDRv4_PREFIX})?\\z")
|
32
|
+
CIDRv6_PREFIX = '(\d+)'
|
33
|
+
CIDRv6_PREFIX_REGEXP = /^#{CIDRv6_PREFIX}$/xi
|
34
|
+
IPv6_SRC = "#{Resolv::IPv6::Regex_8Hex.source.delete_suffix('\z').delete_prefix('\A')}"
|
35
|
+
CIDRv6_REGEXP = Regexp.new("\\A(#{IPv6_SRC})(\/#{CIDRv6_PREFIX})?\\z", Regexp::EXTENDED | Regexp::IGNORECASE)
|
36
|
+
|
37
|
+
def self.cidr?(ip)
|
38
|
+
CIDRv4_REGEXP.match?(ip)
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.cidr6?(ip)
|
42
|
+
CIDRv6_REGEXP.match?(ip)
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.prefix(ip)
|
46
|
+
if cidr = CIDRv4_REGEXP.match(ip)
|
47
|
+
cidr[7]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.prefix6(ip)
|
52
|
+
if cidr = CIDRv6_REGEXP.match(ip)
|
53
|
+
cidr[3]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.ip?(ip)
|
58
|
+
Resolv::IPv4::Regex.match?(ip)
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.ip6?(ip)
|
62
|
+
Resolv::IPv6::Regex.match?(ip)
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.cidr_prefix?(prefix)
|
66
|
+
CIDRv4_PREFIX_REGEXP.match?(prefix)
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.cidr6_prefix?(prefix)
|
70
|
+
CIDRv6_PREFIX_REGEXP.match?(prefix) && prefix.to_i >= 0 && prefix.to_i <= 128
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.ip(ip)
|
74
|
+
if cidr = CIDRv4_REGEXP.match(ip)
|
75
|
+
cidr[1]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.ip6(ip)
|
80
|
+
if cidr = CIDRv6_REGEXP.match(ip)
|
81
|
+
cidr[1]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.to_cidr(ip,prefix = nil)
|
86
|
+
return nil unless self.ip?(ip) && (!prefix || self.cidr_prefix?(prefix))
|
87
|
+
cidr = "#{ip}"
|
88
|
+
cidr += "/#{prefix}" if self.cidr_prefix?(prefix)
|
89
|
+
cidr
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.to_cidr6(ip,prefix = nil)
|
93
|
+
return nil unless self.ip6?(ip) && (!prefix || self.cidr6_prefix?(prefix))
|
94
|
+
cidr = "#{ip}"
|
95
|
+
cidr += "/#{prefix}" if self.cidr6_prefix?(prefix)
|
96
|
+
cidr
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,115 @@
|
|
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
|
+
|
27
|
+
NICS_REGEXP = /^(net)(\d+)/
|
28
|
+
|
29
|
+
def self.extract_mac_address(nic_value)
|
30
|
+
nic_value[/([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})/]
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.model_regexp
|
34
|
+
/^model=(\w+)[,].+/
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.name_regexp
|
38
|
+
/^name=(\w+)[,].+/
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.ip_regexp
|
42
|
+
/^(.+)[,]{1}ip=([\d\.\/]+)[,]?(.+)?$/
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.nic_update_regexp
|
46
|
+
/^(\w+)[=]{1}([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}).+/
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.has_model?(nic_value)
|
50
|
+
nic_value.match?(self.model_regexp)
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.has_name?(nic_value)
|
54
|
+
nic_value.match?(self.name_regexp)
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.has_ip?(nic_value)
|
58
|
+
nic_value.match?(self.ip_regexp)
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.extract_nic_id(nic_value)
|
62
|
+
if self.has_model?(nic_value)
|
63
|
+
nic_value.scan(self.model_regexp).first.first
|
64
|
+
elsif self.has_name?(nic_value)
|
65
|
+
nic_value.scan(self.name_regexp).first.first
|
66
|
+
else
|
67
|
+
nic_value.scan(self.nic_update_regexp).first.first
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.to_mac_adresses_array(nics)
|
72
|
+
addresses = []
|
73
|
+
nics.each { |nic| addresses.push(nic.macaddr) }
|
74
|
+
addresses
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.nic_name(nic)
|
78
|
+
if nic.has_key?(:model)
|
79
|
+
"model"
|
80
|
+
elsif nic.has_key?(:name)
|
81
|
+
"name"
|
82
|
+
else
|
83
|
+
""
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.flatten(nic_hash)
|
88
|
+
nic_id = nic_hash[self.nic_name(nic_hash).to_sym]
|
89
|
+
if nic_hash.has_key?(:macaddr)
|
90
|
+
nic_value = nic_id + "=" + nic_hash[:macaddr]
|
91
|
+
else
|
92
|
+
nic_value = self.nic_name(nic_hash) + "=" + nic_id
|
93
|
+
end
|
94
|
+
options = nic_hash.reject { |key, _value| [self.nic_name(nic_hash).to_sym,:id,:macaddr].include? key.to_sym }
|
95
|
+
nic_value += ',' unless options.empty?
|
96
|
+
nic_value += Fog::Proxmox::Hash.stringify(options) unless options.empty?
|
97
|
+
{ "#{nic_hash[:id]}": nic_value }
|
98
|
+
end
|
99
|
+
|
100
|
+
def self.collect_nics(attributes)
|
101
|
+
attributes.select { |key| nic?(key.to_s) }
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.nic?(id)
|
105
|
+
NICS_REGEXP.match(id) ? true : false
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.extract_ip(nic_value)
|
109
|
+
if ip = self.ip_regexp.match(nic_value)
|
110
|
+
ip[2]
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,32 @@
|
|
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/json'
|
21
|
+
|
22
|
+
module Fog
|
23
|
+
module Proxmox
|
24
|
+
# module Json mixins
|
25
|
+
module Json
|
26
|
+
def self.get_data(response)
|
27
|
+
body = JSON.decode(response.body)
|
28
|
+
body['data']
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|