Class: Legion::Settings::DnsBootstrap

Inherits:
Object
  • Object
show all
Includes:
Logging::Helper
Defined in:
lib/legion/settings/dns_bootstrap.rb

Constant Summary collapse

CACHE_FILENAME =
'_dns_bootstrap.json'
HOSTNAME_PREFIX =
'legion-bootstrap'
URL_PATH =
'/legion/bootstrap.json'
HTTP_TIMEOUT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_domain:, cache_dir: nil) ⇒ DnsBootstrap

Returns a new instance of DnsBootstrap.



22
23
24
25
26
27
28
# File 'lib/legion/settings/dns_bootstrap.rb', line 22

def initialize(default_domain:, cache_dir: nil)
  @default_domain = default_domain
  @hostname = "#{HOSTNAME_PREFIX}.#{default_domain}"
  @url = "https://#{@hostname}#{URL_PATH}"
  dir = cache_dir || File.expand_path('~/.legionio/settings')
  @cache_path = File.join(dir, CACHE_FILENAME)
end

Instance Attribute Details

#cache_pathObject (readonly)

Returns the value of attribute cache_path.



20
21
22
# File 'lib/legion/settings/dns_bootstrap.rb', line 20

def cache_path
  @cache_path
end

#default_domainObject (readonly)

Returns the value of attribute default_domain.



20
21
22
# File 'lib/legion/settings/dns_bootstrap.rb', line 20

def default_domain
  @default_domain
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



20
21
22
# File 'lib/legion/settings/dns_bootstrap.rb', line 20

def hostname
  @hostname
end

#urlObject (readonly)

Returns the value of attribute url.



20
21
22
# File 'lib/legion/settings/dns_bootstrap.rb', line 20

def url
  @url
end

Instance Method Details

#cache_exists?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/legion/settings/dns_bootstrap.rb', line 82

def cache_exists?
  File.exist?(@cache_path)
end

#fetchObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/legion/settings/dns_bootstrap.rb', line 38

def fetch
  return nil unless resolve?

  uri = URI.parse(@url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.open_timeout = HTTP_TIMEOUT
  http.read_timeout = HTTP_TIMEOUT
  response = http.request(Net::HTTP::Get.new(uri))
  return nil unless response.is_a?(Net::HTTPSuccess)

  ::JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log.warn("DNS bootstrap fetch failed for #{@url}: #{e.message}")
  nil
end

#read_cacheObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/legion/settings/dns_bootstrap.rb', line 69

def read_cache
  return nil unless File.exist?(@cache_path)

  raw = ::JSON.parse(File.read(@cache_path), symbolize_names: true)
  log.debug("DNS bootstrap cache hit: #{@cache_path}")
  raw.delete(:_dns_bootstrap_meta)
  raw
rescue ::JSON::ParserError
  log.warn("DNS bootstrap cache corrupt, deleting: #{@cache_path}")
  FileUtils.rm_f(@cache_path)
  nil
end

#resolve?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/legion/settings/dns_bootstrap.rb', line 30

def resolve?
  Resolv.getaddress(@hostname)
  true
rescue Resolv::ResolvError, Resolv::ResolvTimeout => e
  log.debug("Legion::Settings::DnsBootstrap#resolve? could not resolve #{@hostname}: #{e.message}")
  false
end

#write_cache(config) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/legion/settings/dns_bootstrap.rb', line 55

def write_cache(config)
  FileUtils.mkdir_p(File.dirname(@cache_path))
  payload = config.merge(
    _dns_bootstrap_meta: {
      fetched_at: Time.now.utc.iso8601,
      hostname:   @hostname,
      url:        @url
    }
  )
  tmp = "#{@cache_path}.tmp"
  File.write(tmp, ::JSON.pretty_generate(payload))
  File.rename(tmp, @cache_path)
end