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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58005c60cc515f17ef119f259d4115b8b0638bd8
4
- data.tar.gz: 20672ceda7b0184432661e0d13f04fdfd84be7af
3
+ metadata.gz: e54769a85f5ba636185ecd3427169c3d178f1018
4
+ data.tar.gz: 47b1630f45eb6d9f22517ea0454f73955bfeb98f
5
5
  SHA512:
6
- metadata.gz: d4b28c161598fbc12b3a2b1f8b457fb5902c31f3a6bdf3b76e4244a82e8809ef234eef842ce5db99c910f3ec7c4b04b5fa75dff9f173d225db67867dd88300dc
7
- data.tar.gz: e22a918fede16e4b4a327f800bb6eb67ce2784b535bc3d877004281a7f39a39e81ad2432f45f840244131a102a649b6f36bc4fb31edd3f103de3f41e773a2cd5
6
+ metadata.gz: 315700aa265cd579a93f6f5677500e42479fe35654706753905c607507f3111e531c110807bae5ad2c067959c0398e9b3a5a4b3eabb5df020f45c04764198792
7
+ data.tar.gz: 0365bc91ad1b18be2025fb620cfa7e9e6ef4233e27d5bf41e2abeb9462f7ce44c1049650468b17925d526fda068e988b370a116fb7fcf759384429ab1818f24f
@@ -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 self.get_ip_for_mac(mac)
9
- attempts = 0
10
- max_attempts = ENV.key?('MAX_IP_WAIT') ? ENV['MAX_IP_WAIT'].to_i : MAX_ATTEMPTS
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 self.parse_lease_file_for_mac(mac)
16
+ def parse_lease_file_for_mac(mac)
20
17
  lease_file = (ENV['LEASES_FILE'] || LEASES_FILE)
21
- contents = File.read(lease_file)
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
@@ -1,4 +1,4 @@
1
1
  # Common Xhyve functionality nampesace
2
2
  module Xhyve
3
- VERSION = '0.0.5'
3
+ VERSION = '0.0.6'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xhyve-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Hamel