xhyve-ruby 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/xhyve/dhcp.rb +21 -9
- data/lib/xhyve/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e54769a85f5ba636185ecd3427169c3d178f1018
|
4
|
+
data.tar.gz: 47b1630f45eb6d9f22517ea0454f73955bfeb98f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 315700aa265cd579a93f6f5677500e42479fe35654706753905c607507f3111e531c110807bae5ad2c067959c0398e9b3a5a4b3eabb5df020f45c04764198792
|
7
|
+
data.tar.gz: 0365bc91ad1b18be2025fb620cfa7e9e6ef4233e27d5bf41e2abeb9462f7ce44c1049650468b17925d526fda068e988b370a116fb7fcf759384429ab1818f24f
|
data/lib/xhyve/dhcp.rb
CHANGED
@@ -1,29 +1,41 @@
|
|
1
1
|
module Xhyve
|
2
2
|
# Parse DHCP leases file for a MAC address, and get its ip.
|
3
3
|
module DHCP
|
4
|
+
extend self
|
4
5
|
LEASES_FILE = '/var/db/dhcpd_leases'
|
5
6
|
WAIT_TIME = 1
|
6
7
|
MAX_ATTEMPTS = 60
|
7
8
|
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
while attempts < max_attempts
|
12
|
-
attempts += 1
|
9
|
+
def get_ip_for_mac(mac)
|
10
|
+
max = ENV.key?('MAX_IP_WAIT') ? ENV['MAX_IP_WAIT'].to_i : nil
|
11
|
+
ip = wait_for(max: max) do
|
13
12
|
ip = parse_lease_file_for_mac(mac)
|
14
|
-
return ip if ip
|
15
|
-
sleep(WAIT_TIME)
|
16
13
|
end
|
17
14
|
end
|
18
15
|
|
19
|
-
def
|
16
|
+
def parse_lease_file_for_mac(mac)
|
20
17
|
lease_file = (ENV['LEASES_FILE'] || LEASES_FILE)
|
21
|
-
contents =
|
18
|
+
contents = wait_for do
|
19
|
+
File.read(lease_file) if File.exists?(lease_file)
|
20
|
+
end
|
22
21
|
pattern = contents.match(/ip_address=(\S+)\n\thw_address=\d+,#{mac}/)
|
23
22
|
if pattern
|
24
23
|
addrs = pattern.captures
|
25
24
|
addrs.first if addrs
|
26
25
|
end
|
27
26
|
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def wait_for(max: nil)
|
31
|
+
attempts = 0
|
32
|
+
max ||= MAX_ATTEMPTS
|
33
|
+
while attempts < max
|
34
|
+
attempts += 1
|
35
|
+
result = yield
|
36
|
+
return result if result
|
37
|
+
sleep(WAIT_TIME)
|
38
|
+
end
|
39
|
+
end
|
28
40
|
end
|
29
41
|
end
|
data/lib/xhyve/version.rb
CHANGED