Class: Landrush::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/landrush/config.rb

Constant Summary collapse

INTERFACE_CLASSES =
[:any, :ipv4, :ipv6].freeze
INTERFACE_CLASS_INVALID =
"Invalid interface class, should be one of: #{INTERFACE_CLASSES.join(', ')}".freeze
DEFAULTS =
{
  enabled:                 false,
  tld:                     'vagrant.test',
  upstream_servers:        [[:udp, '8.8.8.8', 53], [:tcp, '8.8.8.8', 53]],
  host_ip_address:         nil,
  guest_redirect_dns:      true,
  host_interface:          nil,
  host_interface_excludes: [/lo[0-9]*/, /docker[0-9]+/, /tun[0-9]+/],
  host_interface_class:    :ipv4,
  host_redirect_dns:       true
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/landrush/config.rb', line 29

def initialize
  @hosts                     = {}
  @enabled                   = UNSET_VALUE
  @tld                       = UNSET_VALUE
  @upstream_servers          = UNSET_VALUE
  @host_ip_address           = UNSET_VALUE
  @guest_redirect_dns        = UNSET_VALUE
  @host_interface            = UNSET_VALUE
  @host_interface_excludes   = UNSET_VALUE
  @host_interface_class      = UNSET_VALUE
  @host_redirect_dns         = UNSET_VALUE
end

Instance Attribute Details

#enabledObject

Returns the value of attribute enabled.



4
5
6
# File 'lib/landrush/config.rb', line 4

def enabled
  @enabled
end

#guest_redirect_dnsObject

Returns the value of attribute guest_redirect_dns.



8
9
10
# File 'lib/landrush/config.rb', line 8

def guest_redirect_dns
  @guest_redirect_dns
end

#host_interfaceObject

Returns the value of attribute host_interface.



9
10
11
# File 'lib/landrush/config.rb', line 9

def host_interface
  @host_interface
end

#host_interface_classObject

Returns the value of attribute host_interface_class.



10
11
12
# File 'lib/landrush/config.rb', line 10

def host_interface_class
  @host_interface_class
end

#host_interface_excludesObject

Returns the value of attribute host_interface_excludes.



11
12
13
# File 'lib/landrush/config.rb', line 11

def host_interface_excludes
  @host_interface_excludes
end

#host_ip_addressObject

Returns the value of attribute host_ip_address.



7
8
9
# File 'lib/landrush/config.rb', line 7

def host_ip_address
  @host_ip_address
end

#host_redirect_dnsObject

Returns the value of attribute host_redirect_dns.



12
13
14
# File 'lib/landrush/config.rb', line 12

def host_redirect_dns
  @host_redirect_dns
end

#hostsObject

Returns the value of attribute hosts.



3
4
5
# File 'lib/landrush/config.rb', line 3

def hosts
  @hosts
end

#tldObject

Returns the value of attribute tld.



5
6
7
# File 'lib/landrush/config.rb', line 5

def tld
  @tld
end

#upstream_serversObject

Returns the value of attribute upstream_servers.



6
7
8
# File 'lib/landrush/config.rb', line 6

def upstream_servers
  @upstream_servers
end

Instance Method Details

#disableObject



46
47
48
# File 'lib/landrush/config.rb', line 46

def disable
  @enabled = false
end

#enableObject



42
43
44
# File 'lib/landrush/config.rb', line 42

def enable
  @enabled = true
end

#enabled?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/landrush/config.rb', line 50

def enabled?
  !!@enabled
end

#finalize!Object



83
84
85
86
87
88
89
# File 'lib/landrush/config.rb', line 83

def finalize!
  DEFAULTS.each do |name, value|
    if instance_variable_get('@' + name.to_s) == UNSET_VALUE
      instance_variable_set '@' + name.to_s, value
    end
  end
end

#guest_redirect_dns?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/landrush/config.rb', line 54

def guest_redirect_dns?
  @guest_redirect_dns
end

#host(hostname, ip_address = nil) ⇒ Object



62
63
64
# File 'lib/landrush/config.rb', line 62

def host(hostname, ip_address = nil)
  @hosts[hostname] = ip_address
end

#host_redirect_dns?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/landrush/config.rb', line 58

def host_redirect_dns?
  @host_redirect_dns
end

#merge(other) ⇒ Object



77
78
79
80
81
# File 'lib/landrush/config.rb', line 77

def merge(other)
  super.tap do |result|
    result.hosts = @hosts.merge(other.hosts)
  end
end

#upstream(ip, port = 53, protocol = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/landrush/config.rb', line 66

def upstream(ip, port = 53, protocol = nil)
  @upstream_servers = [] if @upstream_servers == UNSET_VALUE

  if !protocol
    @upstream_servers.push [:udp, ip, port]
    @upstream_servers.push [:tcp, ip, port]
  else
    @upstream_servers.push [protocol, ip, port]
  end
end

#validate(_machine) ⇒ Object



91
92
93
94
95
96
# File 'lib/landrush/config.rb', line 91

def validate(_machine)
  errors = _detected_errors
  errors.concat validate_host_interface_class

  { 'landrush' => errors }
end

#validate_host_interface_classObject



98
99
100
101
102
103
104
105
# File 'lib/landrush/config.rb', line 98

def validate_host_interface_class
  @host_interface_class = @host_interface_class.intern if @host_interface_class.is_a? String

  return [] if INTERFACE_CLASSES.include? @host_interface_class

  # TODO: Should really be using I18n; makes testing a lot cleaner too.
  [INTERFACE_CLASS_INVALID, fields: 'host_interface_class']
end