Class: VagrantDNS::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-dns/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tmp_path) ⇒ Service

Returns a new instance of Service.



7
8
9
# File 'lib/vagrant-dns/service.rb', line 7

def initialize(tmp_path)
  self.tmp_path = tmp_path
end

Instance Attribute Details

#tmp_pathObject

Returns the value of attribute tmp_path.



5
6
7
# File 'lib/vagrant-dns/service.rb', line 5

def tmp_path
  @tmp_path
end

Instance Method Details

#restart!(start_opts = {}) ⇒ Object



47
48
49
50
# File 'lib/vagrant-dns/service.rb', line 47

def restart!(start_opts = {})
  stop!
  start!(start_opts)
end

#run!(cmd, opts = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vagrant-dns/service.rb', line 23

def run!(cmd, opts = {})
  # On darwin, when the running Ruby is not compiled for the running OS
  # @see: https://github.com/BerlinVagrant/vagrant-dns/issues/72
  use_issue_72_workround = RUBY_PLATFORM.match?(/darwin/) && !RUBY_PLATFORM.end_with?(`uname -r`[0, 2])

  if cmd == "start" && use_issue_72_workround
    require_relative "./server"
  end

  Daemons.run_proc("vagrant-dns", run_options(cmd, opts)) do
    unless use_issue_72_workround
      require_relative "./server"
    end

    VagrantDNS::Server.new(
      Registry.new(tmp_path),
      listen: VagrantDNS::Config.listen,
      ttl: VagrantDNS::Config.ttl,
      passthrough: VagrantDNS::Config.passthrough,
      resolver: VagrantDNS::Config.passthrough_resolver
    ).run
  end
end

#show_configObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vagrant-dns/service.rb', line 52

def show_config
  registry = Registry.open(tmp_path)
  return unless registry

  config = registry.to_hash
  if config.any?
    config.each do |pattern, ip|
      puts format("%s => %s", pattern.inspect, ip)
    end
  else
    puts "Pattern configuration missing or empty."
  end
end

#show_tld_configObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vagrant-dns/service.rb', line 66

def show_tld_config
  tld_registry = VagrantDNS::TldRegistry.open(tmp_path)
  return unless tld_registry

  tlds = tld_registry.transaction { |store| store.fetch("tlds", []) }
  if !tlds || tlds.empty?
    puts "No TLDs configured."
  else
    puts tlds
  end
end

#start!(opts = {}) ⇒ Object



11
12
13
# File 'lib/vagrant-dns/service.rb', line 11

def start!(opts = {})
  run!("start", { ontop: opts[:ontop] })
end

#status!Object



19
20
21
# File 'lib/vagrant-dns/service.rb', line 19

def status!
  run!("status")
end

#stop!Object



15
16
17
# File 'lib/vagrant-dns/service.rb', line 15

def stop!
  run!("stop")
end