class Droxy::Server

Attributes

tld[R]

Public Class Methods

new(tld: @tld = tld) click to toggle source
# File lib/droxy.rb, line 16
def initialize tld:
  @tld      = tld
  @ip_cache = MaxAgeCache.new duration: 24 * 60 * 60

  @dns = DNSServer.new tld: tld, port: dns_port
end

Public Instance Methods

dns_port() click to toggle source
# File lib/droxy.rb, line 33
def dns_port
  @_dns_port ||= ResolverFile.new(tld: tld).read_port
end
ip_for_name(name) click to toggle source
# File lib/droxy.rb, line 23
def ip_for_name name
  @ip_cache.fetch(name) { query_ip name }
end
prefetch_running_ips() click to toggle source
# File lib/droxy.rb, line 42
def prefetch_running_ips
  docker_machine "ls" do |output|
    header, *machines = output.lines
    col = header =~ /URL\s/
    raise "Could not find URL in docker-machine output: #{output}" unless col
    machines.each do |line|
      name = line.split(/\s+/).first
      ip   = line[col..-1].split(/\s+/).first

      @ip_cache.fetch(name) { URI.parse(ip).host }
    end
  end
end
query_ip(name) click to toggle source
# File lib/droxy.rb, line 27
def query_ip name
  docker_machine "ip", name do |ip|
    return ip.empty? ? nil : ip
  end
end
run!() click to toggle source
# File lib/droxy.rb, line 37
def run!
  prefetch_running_ips
  @dns.translate { |name| ip_for_name name }
end

Private Instance Methods

docker_machine(*args, &block) click to toggle source
# File lib/droxy.rb, line 58
def docker_machine *args, &block
  Open3.popen3 "docker-machine", *args do |i,o,_,t|
    return unless t.value.success?
    block.call o.read.strip
  end
end