pingable_ipam_plugin 0.0.5 → 0.0.10

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a74278facc95a796eaa1a5cf9122ba1869dbc3300bd17a16e919deb53fbc6206
4
- data.tar.gz: cba5705fe14534f4c24734951a3fb8e809709f052b13c65ed8845b9b8d7c8c4a
3
+ metadata.gz: 32ff6f6ec47a972a551c4a45211160cf6a49321e97c440e9aa427fbd9e563407
4
+ data.tar.gz: 3b79031e72d5b4a81627e5ea0c041d88449fb4484443f39817ef940e4ad56e80
5
5
  SHA512:
6
- metadata.gz: 8d9edce903ef99a98ab52e32ce97a18e9eb948c84389a5057c8faf6cd97c366d2097f4df58a90149ab477b9a9b40b14d86ffe904f3a26be74eee0796b2fe89b1
7
- data.tar.gz: 8f23bf9ac404645cf6e25a16dc17c258f6e947dc2045a68cea6a61886845092c062175a4f933be1c18e69aeb57eea6ef503b24ff169d3c97a08a85a64d2dd39d
6
+ metadata.gz: c55c277edef4196f0585bbf84c7d00a40f97aca487fd8c026ca2f5354d7c1d7234ef11feb65fba973d0a4902cd9c6fe6bfaa798bbf068c5b0f1b50a0599df924
7
+ data.tar.gz: abd0bdc066e593d2051890cf0442048bfc75da6ed92397d0aebda50a1d7176e77a3f55a0032b56e9ee97540be26831e67cb1559d8d52969c04e3208a1fd46c7b
@@ -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(generator.rand(range.first.to_i..range.last.to_i), subnet.family)
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.debug "Unable to icmp ping #{ip} because #{err.inspect}."
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,30 +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
 
80
- if range.empty?
81
- errors.add(:subnet, _('no free IP could be found in our DB, enlarge subnet range'))
82
- return nil
91
+ unless range.empty?
92
+ logger.info "[IPAM] IP range from #{range.first.to_s} to #{range.last.to_s}, total: #{range.length}"
83
93
  end
84
94
 
85
95
  loop do
86
- # next random IP from the sequence generated by MAC seed
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
+
87
101
  candidate = random_ip(range)
88
102
  iterations += 1
89
103
  break if iterations >= MAX_ITERATIONS
90
104
  # try to match it
91
105
  ip = candidate.to_s
106
+ logger.info "[IPAM] Searching for free IP - resolving #{ip}"
107
+
92
108
  # Check again if something has been changed.
93
109
  if !excluded_ips.include?(ip) && !subnet.known_ips.include?(ip)
94
- logger.debug "Searching for free IP - pinging #{ip}."
95
110
  if tcp_pingable?(ip) || icmp_pingable?(ip)
96
- 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...")
97
112
  else
98
- logger.debug("Found #{ip} in #{iterations} iterations")
99
- return ip
113
+ unless ns_resolvable?(ip)
114
+ logger.info("[IPAM] Found #{ip} in #{iterations} iterations")
115
+ return ip
116
+ end
100
117
  end
101
118
  end
119
+
120
+ range -= [candidate]
102
121
  end
103
- logger.debug("Not suggesting IP Address for #{subnet} as no free IP found in reasonable time (#{iterations} iterations)")
122
+
123
+ logger.warn("[IPAM] Not suggesting IP Address for #{subnet} as no free IP found in reasonable time (#{iterations} iterations)")
104
124
  errors.add(:subnet, _('no random free IP could be found in our DB, enlarge subnet range'))
105
125
  nil
106
126
  end
@@ -1,3 +1,3 @@
1
1
  module PingableIpamPlugin
2
- VERSION = '0.0.5'.freeze
2
+ VERSION = '0.0.10'.freeze
3
3
  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.5
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Parshin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-11 00:00:00.000000000 Z
11
+ date: 2021-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop