Module: Vagrant::Lube::Helpers

Defined in:
lib/vagrant/lube/helpers.rb

Instance Method Summary collapse

Instance Method Details

#local_ipObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/vagrant/lube/helpers.rb', line 8

def local_ip
  @local_ip ||= begin
    # turn off reverse DNS resolution temporarily
    orig, Socket.do_not_reverse_lookup =
      Socket.do_not_reverse_lookup, true

    UDPSocket.open do |s|
      s.connect '64.233.187.99', 1
      s.addr.last
    end
  ensure
    Socket.do_not_reverse_lookup = orig
  end
end

#next_free_chef_ip(start) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vagrant/lube/helpers.rb', line 57

def next_free_chef_ip start
  all, network, start_net = /(.*\.)([0-9]+)/.match(start).to_a
  (start_net.to_i..255).each do |subnet|
    host = "#{network}#{subnet}.2"
    if port_open?(host, 22)
      next
    else
      return host
    end
  end
end

#next_free_ip(start) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vagrant/lube/helpers.rb', line 33

def next_free_ip start
  all, network, start_ip = /(.*\.)([0-9]+)/.match(start).to_a
  (start_ip.to_i..255).each do |ip|
    host = "#{network}#{ip}"
    if port_open?(host, 22)
      next
    else
      return host
    end
  end
end

#next_random_free_ip(start) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vagrant/lube/helpers.rb', line 45

def next_random_free_ip start
  all, network, start_ip = /(.*\.)([0-9]+)/.match(start).to_a
  ((rand(start_ip.to_i..255))..255).each do |ip|
    host = "#{network}#{ip}"
    if port_open?(host, 22)
      next
    else
      return host
    end
  end
end

#port_open?(host, port) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
# File 'lib/vagrant/lube/helpers.rb', line 23

def port_open? host, port
  Timeout.timeout(0.25) do
    s = TCPSocket.new(host, port)
    s.close rescue nil
    return true
  end
rescue Timeout::Error, Errno::ECONNREFUSED, Errno::EHOSTUNREACH
  return false
end