Class: Vagrant::Hivemind::Util::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/hivemind/util.rb

Class Method Summary collapse

Class Method Details

.get_host_keys_using_host_port(host_port, hosts) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/vagrant/hivemind/util.rb', line 75

def self.get_host_keys_using_host_port(host_port, hosts)
  hosts.keys.select do |key|
    host = hosts[key]
    host.forwarded_ports and (
      host.forwarded_ports.select do |forwarded_port|
        forwarded_port["host_port"] == host_port
      end).size > 0
  end
end

.get_host_port_pair_with_guest_port(guest_port, host) ⇒ Object



71
72
73
# File 'lib/vagrant/hivemind/util.rb', line 71

def self.get_host_port_pair_with_guest_port(guest_port, host)
  (host.forwarded_ports.select do |forwarded_port| forwarded_port["guest_port"] == guest_port end).first
end

.get_network(ip_address) ⇒ Object



85
86
87
88
89
# File 'lib/vagrant/hivemind/util.rb', line 85

def self.get_network(ip_address)
  split_ip_address = ip_address.split(".")
  return nil unless split_ip_address.size == 4
  split_ip_address[0,3].join(".")
end

.highest_ip_address(hosts = {}) ⇒ Object



95
96
97
# File 'lib/vagrant/hivemind/util.rb', line 95

def self.highest_ip_address(hosts = {})
  hosts.values.map(&:ip_address).sort.last
end

.is_valid_hostname?(hostname) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/vagrant/hivemind/util.rb', line 48

def self.is_valid_hostname?(hostname)
  return false unless hostname and hostname.size > 0 and hostname.size <= Vagrant::Hivemind::Constants::SIMPLE_HOSTNAME_MAX_LENGTH
  return (Vagrant::Hivemind::Constants::SIMPLE_HOSTNAME_REGEX =~ hostname) != nil
end

.is_valid_ip_address?(ip_address) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/vagrant/hivemind/util.rb', line 53

def self.is_valid_ip_address?(ip_address)
  return false unless ip_address and ip_address.split(".").size == 4
  return (Vagrant::Hivemind::Constants::IP_ADDRESS_REGEX =~ ip_address) != nil
end

.is_valid_port_pair?(port_pair) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/vagrant/hivemind/util.rb', line 58

def self.is_valid_port_pair?(port_pair)
  return false unless port_pair and port_pair.split(":").size == 2
  return (Vagrant::Hivemind::Constants::PORT_PAIR_REGEX =~ port_pair) != nil
end

.is_valid_port_value?(port) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/vagrant/hivemind/util.rb', line 63

def self.is_valid_port_value?(port)
  port >= 0 and port <= 65535
end

.next_ip_address(hosts = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/vagrant/hivemind/util.rb', line 99

def self.next_ip_address(hosts = {})
  highest_ip_address_index = Vagrant::Hivemind::Constants::PRIVATE_NETWORK_IP_ADDRESS_POOL.find_index highest_ip_address(hosts)

  next_ip_address_index = highest_ip_address_index
  loop do
    next_ip_address_index = ((next_ip_address_index + 1) % Vagrant::Hivemind::Constants::PRIVATE_NETWORK_IP_ADDRESS_POOL.size)
    next_ip_address_candidate = Vagrant::Hivemind::Constants::PRIVATE_NETWORK_IP_ADDRESS_POOL[next_ip_address_index]

    return next_ip_address_candidate unless hosts.values.map(&:ip_address).include? next_ip_address_candidate

    if next_ip_address_index == highest_ip_address_index
      raise StandardError, "There are no more available ip addresses for the private network!"
    end
  end
end

.port_pair_to_i(port_pair) ⇒ Object



67
68
69
# File 'lib/vagrant/hivemind/util.rb', line 67

def self.port_pair_to_i(port_pair)
  port_pair.split(":").map.each { |n| n.to_i }
end

.starting_ip_addressObject



91
92
93
# File 'lib/vagrant/hivemind/util.rb', line 91

def self.starting_ip_address
  Vagrant::Hivemind::Constants::PRIVATE_NETWORK_IP_ADDRESS_POOL[0]
end