Class: Messhy::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/messhy/configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_hash, environment = 'development') ⇒ Configuration

Returns a new instance of Configuration.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/messhy/configuration.rb', line 18

def initialize(config_hash, environment = 'development')
  @environment = environment
  env_config = config_hash[environment] || {}

  @network = env_config['network'] || '10.8.0.0/24'
  @nodes = env_config['nodes'] || {}
  @dns = env_config['dns'] || {}
  @user = env_config['user'] || 'ubuntu'
  @ssh_key = File.expand_path(env_config['ssh_key'] || '~/.ssh/id_rsa')
  @mtu = env_config['mtu'] || 1280
  @listen_port = env_config['listen_port'] || 51_820
  @keepalive = env_config['keepalive'] || 25
  @verify_host_key = env_config.key?('verify_host_key') ? env_config['verify_host_key'] : true
end

Instance Attribute Details

#dnsObject (readonly)

Returns the value of attribute dns.



7
8
9
# File 'lib/messhy/configuration.rb', line 7

def dns
  @dns
end

#environmentObject (readonly)

Returns the value of attribute environment.



7
8
9
# File 'lib/messhy/configuration.rb', line 7

def environment
  @environment
end

#keepaliveObject (readonly)

Returns the value of attribute keepalive.



7
8
9
# File 'lib/messhy/configuration.rb', line 7

def keepalive
  @keepalive
end

#listen_portObject (readonly)

Returns the value of attribute listen_port.



7
8
9
# File 'lib/messhy/configuration.rb', line 7

def listen_port
  @listen_port
end

#mtuObject (readonly)

Returns the value of attribute mtu.



7
8
9
# File 'lib/messhy/configuration.rb', line 7

def mtu
  @mtu
end

#networkObject (readonly)

Returns the value of attribute network.



7
8
9
# File 'lib/messhy/configuration.rb', line 7

def network
  @network
end

#nodesObject (readonly)

Returns the value of attribute nodes.



7
8
9
# File 'lib/messhy/configuration.rb', line 7

def nodes
  @nodes
end

#ssh_keyObject (readonly)

Returns the value of attribute ssh_key.



7
8
9
# File 'lib/messhy/configuration.rb', line 7

def ssh_key
  @ssh_key
end

#userObject (readonly)

Returns the value of attribute user.



7
8
9
# File 'lib/messhy/configuration.rb', line 7

def user
  @user
end

#verify_host_keyObject (readonly)

Returns the value of attribute verify_host_key.



7
8
9
# File 'lib/messhy/configuration.rb', line 7

def verify_host_key
  @verify_host_key
end

Class Method Details

.load(config_path = 'config/mesh.yml', environment = nil) ⇒ Object

Raises:



33
34
35
36
37
38
39
40
# File 'lib/messhy/configuration.rb', line 33

def self.load(config_path = 'config/mesh.yml', environment = nil)
  environment ||= ENV['MESSHY_ENVIRONMENT'] || ENV['RAILS_ENV'] || 'development'

  raise Error, "Config file not found: #{config_path}" unless File.exist?(config_path)

  config_hash = YAML.load_file(config_path, aliases: true)
  new(config_hash, environment)
end

Instance Method Details

#dns_auto_records?Boolean

Returns:

  • (Boolean)


147
148
149
150
151
# File 'lib/messhy/configuration.rb', line 147

def dns_auto_records?
  return true unless @dns.key?('auto_records')

  @dns['auto_records'] == true
end

#dns_domainObject



110
111
112
113
114
115
116
# File 'lib/messhy/configuration.rb', line 110

def dns_domain
  domains = dns_domains
  return domains.first unless domains.empty?

  value = @dns['domain'] || 'mesh'
  value.to_s.strip
end

#dns_domainsObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/messhy/configuration.rb', line 118

def dns_domains
  domains = Array(@dns['domains']).map(&:to_s).map(&:strip).reject(&:empty?)
  return domains unless domains.empty?

  value = @dns['domain']
  return [] if value.nil?

  value = value.to_s.strip
  value.empty? ? [] : [value]
end

#dns_enabled?Boolean

Returns:

  • (Boolean)


99
100
101
102
103
# File 'lib/messhy/configuration.rb', line 99

def dns_enabled?
  return false if @dns.nil? || @dns.empty?

  @dns.key?('enabled') ? @dns['enabled'] == true : true
end

#dns_interfaceObject



129
130
131
132
# File 'lib/messhy/configuration.rb', line 129

def dns_interface
  value = @dns['interface'] || 'wg0'
  value.to_s.strip
end

#dns_providerObject



105
106
107
108
# File 'lib/messhy/configuration.rb', line 105

def dns_provider
  value = @dns['provider'] || 'dnsmasq'
  value.to_s.strip
end

#dns_recordsObject



143
144
145
# File 'lib/messhy/configuration.rb', line 143

def dns_records
  @dns['records'] || {}
end

#dns_server_nodesObject



139
140
141
# File 'lib/messhy/configuration.rb', line 139

def dns_server_nodes
  Array(@dns['servers']).map(&:to_s).reject(&:empty?)
end

#dns_ttlObject



134
135
136
137
# File 'lib/messhy/configuration.rb', line 134

def dns_ttl
  value = @dns['ttl'] || 30
  value.to_i
end

#each_nodeObject



50
51
52
# File 'lib/messhy/configuration.rb', line 50

def each_node(&)
  @nodes.each(&)
end

#network_prefix_lengthObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/messhy/configuration.rb', line 54

def network_prefix_length
  return 24 unless @network

  parts = @network.split('/')
  return 24 if parts.length < 2

  Integer(parts.last)
rescue ArgumentError
  24
end

#node_config(name) ⇒ Object



46
47
48
# File 'lib/messhy/configuration.rb', line 46

def node_config(name)
  @nodes[name]
end

#node_namesObject



42
43
44
# File 'lib/messhy/configuration.rb', line 42

def node_names
  @nodes.keys
end

#validate!Object

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/messhy/configuration.rb', line 65

def validate!
  raise Error, 'No nodes defined' if @nodes.empty?

  @nodes.each do |name, config|
    raise Error, "Node #{name} missing 'host'" unless config['host']
    raise Error, "Node #{name} missing 'private_ip'" unless config['private_ip']
  end

  if dns_enabled?
    raise Error, 'DNS domain is required when dns is enabled' if dns_domains.empty?
    raise Error, 'DNS servers are required when dns is enabled' if dns_server_nodes.empty?
    raise Error, "Unsupported DNS provider: #{dns_provider}" unless %w[dnsmasq].include?(dns_provider)

    dns_server_nodes.each do |name|
      raise Error, "DNS server node not found: #{name}" unless node_config(name)
    end
  end

  true
end

#verify_host_key_modeObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/messhy/configuration.rb', line 86

def verify_host_key_mode
  case @verify_host_key
  when true, 'always', :always
    :always
  when 'accept_new', :accept_new
    :accept_new
  when 'never', :never, false
    :never
  else
    :always
  end
end