pingable_ipam_plugin 0.0.4 → 0.0.9
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.
- checksums.yaml +4 -4
- data/app/services/ipam/ping_random_db.rb +34 -9
- data/lib/pingable_ipam_plugin/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22f73c17ff2eb460277f3f6cc52b3f903a6fb7b23a2d8802d8e267e25bf06dca
|
4
|
+
data.tar.gz: beea45db1643011ed3fa73e20b402dc4220b39a02cac919a9d9189a95833dedb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 349bbd435aac85475b7b4cba9e5955b4a865d0707a3fa41af6a08dae2c2e7f31c698b581d3d5ecd657954f547a77f2c5350a59ba96be25a4cc6b8f5e5150dd78
|
7
|
+
data.tar.gz: b9e222bb00880333c6a8b77723ff529d15ecd54d9eb99d54b8f9ae47165da4104caefab17095831b0a4768b73f306a833e89506163a090537abe940bcfb009e8
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'timeout'
|
2
2
|
require 'socket'
|
3
|
+
require 'resolv'
|
3
4
|
|
4
5
|
module IPAM
|
5
6
|
class PingRandomDb < Base
|
@@ -8,7 +9,7 @@ module IPAM
|
|
8
9
|
end
|
9
10
|
|
10
11
|
def random_ip(range)
|
11
|
-
IPAddr.new(
|
12
|
+
IPAddr.new(range.sample, subnet.family)
|
12
13
|
end
|
13
14
|
|
14
15
|
def tcp_pingable?(ip)
|
@@ -45,7 +46,7 @@ module IPAM
|
|
45
46
|
end
|
46
47
|
|
47
48
|
if bool
|
48
|
-
logger.info "Succesful telnet ping #{ip}, port #{port}"
|
49
|
+
logger.info "[IPAM] Succesful telnet ping #{ip}, port #{port}"
|
49
50
|
end
|
50
51
|
|
51
52
|
bool
|
@@ -63,10 +64,20 @@ module IPAM
|
|
63
64
|
system("ping -c 2 -W 1 #{ip} > /dev/null")
|
64
65
|
end
|
65
66
|
rescue => err
|
66
|
-
logger.
|
67
|
+
logger.warn "[IPAM] Unable to icmp ping #{ip} because #{err.inspect}."
|
67
68
|
true
|
68
69
|
end
|
69
70
|
|
71
|
+
def ns_resolvable? ip
|
72
|
+
begin
|
73
|
+
name = Resolv.getname ip
|
74
|
+
logger.warn "[IPAM] Found a DNS resolvable IP #{ip}, resolved name: #{name}."
|
75
|
+
true
|
76
|
+
rescue StandardError
|
77
|
+
false
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
70
81
|
# Safety check not to spend much CPU time when there are no many free IPs left. This gives up
|
71
82
|
# in about a second on Ryzen 1700 running with Ruby 2.4.
|
72
83
|
MAX_ITERATIONS = 100_000
|
@@ -77,25 +88,39 @@ module IPAM
|
|
77
88
|
range = subnet_range.to_a - excluded_ips.to_a
|
78
89
|
range -= subnet.known_ips.to_a
|
79
90
|
|
91
|
+
unless range.empty?
|
92
|
+
logger.info "[IPAM] IP range from #{range.first.to_s} to #{range.last.to_s}, total: #{range.length}"
|
93
|
+
end
|
94
|
+
|
80
95
|
loop do
|
81
|
-
|
96
|
+
if range.empty?
|
97
|
+
errors.add(:subnet, _('no free IP could be found in our DB, enlarge subnet range'))
|
98
|
+
return nil
|
99
|
+
end
|
100
|
+
|
82
101
|
candidate = random_ip(range)
|
83
102
|
iterations += 1
|
84
103
|
break if iterations >= MAX_ITERATIONS
|
85
104
|
# try to match it
|
86
105
|
ip = candidate.to_s
|
106
|
+
logger.info "[IPAM] Searching for free IP - resolving #{ip}"
|
107
|
+
|
87
108
|
# Check again if something has been changed.
|
88
109
|
if !excluded_ips.include?(ip) && !subnet.known_ips.include?(ip)
|
89
|
-
logger.debug "Searching for free IP - pinging #{ip}."
|
90
110
|
if tcp_pingable?(ip) || icmp_pingable?(ip)
|
91
|
-
logger.warn("Found a pingable IP (#{ip}) address which not marked as known. Skipping it...")
|
111
|
+
logger.warn("[IPAM] Found a pingable IP (#{ip}) address which not marked as known. Skipping it...")
|
92
112
|
else
|
93
|
-
|
94
|
-
|
113
|
+
unless ns_resolvable?(ip)
|
114
|
+
logger.info("[IPAM] Found #{ip} in #{iterations} iterations")
|
115
|
+
return ip
|
116
|
+
end
|
95
117
|
end
|
96
118
|
end
|
119
|
+
|
120
|
+
range -= [ip]
|
97
121
|
end
|
98
|
-
|
122
|
+
|
123
|
+
logger.warn("[IPAM] Not suggesting IP Address for #{subnet} as no free IP found in reasonable time (#{iterations} iterations)")
|
99
124
|
errors.add(:subnet, _('no random free IP could be found in our DB, enlarge subnet range'))
|
100
125
|
nil
|
101
126
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pingable_ipam_plugin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Parshin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|