Class: VagrantDNS::Installers::Linux

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

Constant Summary collapse

EXEC_STYLES =
%i{sudo}
RESOLVED_CONFIG =
"vagrant-dns.conf"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tmp_path, options = {}) ⇒ Linux

Returns a new instance of Linux.



9
10
11
12
13
# File 'lib/vagrant-dns/installers/linux.rb', line 9

def initialize(tmp_path, options = {})
  self.tmp_path     = tmp_path
  self.install_path = options.fetch(:install_path, "/etc/systemd/resolved.conf.d")
  self.exec_style   = options.fetch(:exec_style, :sudo)
end

Instance Attribute Details

#exec_styleObject

Returns the value of attribute exec_style.



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

def exec_style
  @exec_style
end

#install_pathObject

Returns the value of attribute install_path.



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

def install_path
  @install_path
end

#tmp_pathObject

Returns the value of attribute tmp_path.



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

def tmp_path
  @tmp_path
end

Class Method Details

.resolver_files(ip, port, tlds) {|filename, content| ... } ⇒ Object

Generate the resolved config.

Yields:

  • (filename, content)

Yield Parameters:

  • filename (String)

    The filename to use for the resolved config file (relative to install_path)

  • content (String)

    The content for filename



20
21
22
23
24
25
26
27
28
# File 'lib/vagrant-dns/installers/linux.rb', line 20

def self.resolver_files(ip, port, tlds)
  contents =
    "# This file is generated by vagrant-dns\n" \
    "[Resolve]\n" \
    "DNS=#{ip}:#{port}\n" \
    "Domains=~#{tlds.join(' ~')}\n"

  yield "vagrant-dns.conf", contents
end

Instance Method Details

#exec(*commands) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vagrant-dns/installers/linux.rb', line 59

def exec(*commands)
  return if !commands || commands.empty?

  case exec_style
  when :sudo
    commands.each do |c|
      system 'sudo', *c
    end
  else
    raise ArgumentError, "Unsupported execution style: #{exec_style}. Use one of #{EXEC_STYLES.map(&:inspect).join(' ')}"
  end
end

#install!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vagrant-dns/installers/linux.rb', line 30

def install!
  require 'fileutils'

  src = File.join(tmp_path, "resolver", RESOLVED_CONFIG)
  dest = File.join(install_path, RESOLVED_CONFIG)

  commands = [
    ['install', '-D', '-m', '0644', '-T', src.shellescape, dest.shellescape],
    ['systemctl', 'reload-or-restart', 'systemd-resolved.service']
  ]

  exec(*commands)
end

#purge!Object



53
54
55
56
57
# File 'lib/vagrant-dns/installers/linux.rb', line 53

def purge!
  require 'fileutils'
  uninstall!
  FileUtils.rm_r(tmp_path)
end

#uninstall!Object



44
45
46
47
48
49
50
51
# File 'lib/vagrant-dns/installers/linux.rb', line 44

def uninstall!
  commands = [
    ['rm', File.join(install_path, RESOLVED_CONFIG)],
    ['systemctl', 'reload-or-restart', 'systemd-resolved.service']
  ]

  exec(*commands)
end