Class: Messhy::MeshBuilder
- Inherits:
-
Object
- Object
- Messhy::MeshBuilder
- Defined in:
- lib/messhy/mesh_builder.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#node_keys ⇒ Object
readonly
Returns the value of attribute node_keys.
-
#psk_map ⇒ Object
readonly
Returns the value of attribute psk_map.
Instance Method Summary collapse
-
#build_all_configs ⇒ Object
rubocop:enable Metrics/AbcSize.
-
#build_config_for_node(node_name) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#initialize(config, node_keys = {}, psk_map = {}) ⇒ MeshBuilder
constructor
A new instance of MeshBuilder.
Constructor Details
#initialize(config, node_keys = {}, psk_map = {}) ⇒ MeshBuilder
Returns a new instance of MeshBuilder.
9 10 11 12 13 |
# File 'lib/messhy/mesh_builder.rb', line 9 def initialize(config, node_keys = {}, psk_map = {}) @config = config @node_keys = node_keys @psk_map = psk_map end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/messhy/mesh_builder.rb', line 7 def config @config end |
#node_keys ⇒ Object (readonly)
Returns the value of attribute node_keys.
7 8 9 |
# File 'lib/messhy/mesh_builder.rb', line 7 def node_keys @node_keys end |
#psk_map ⇒ Object (readonly)
Returns the value of attribute psk_map.
7 8 9 |
# File 'lib/messhy/mesh_builder.rb', line 7 def psk_map @psk_map end |
Instance Method Details
#build_all_configs ⇒ Object
rubocop:enable Metrics/AbcSize
76 77 78 79 80 81 82 |
# File 'lib/messhy/mesh_builder.rb', line 76 def build_all_configs configs = {} config.each_node do |node_name, _| configs[node_name] = build_config_for_node(node_name) end configs end |
#build_config_for_node(node_name) ⇒ Object
rubocop:disable Metrics/AbcSize
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/messhy/mesh_builder.rb', line 16 def build_config_for_node(node_name) node_config = config.node_config(node_name) raise Error, "Node not found: #{node_name}" unless node_config # Get keys for this node keys = node_keys[node_name] raise Error, "Keys not found for node: #{node_name}" unless keys template_path = File.join(Messhy.root, 'templates', 'wg0.conf.erb') template = ERB.new(File.read(template_path), trim_mode: '-') # Prepare data for template interface_ip = node_config['private_ip'] prefix_length = config.network_prefix_length private_key = keys[:private_key] listen_port = node_config['listen_port'] || config.listen_port mtu = config.mtu # DNS config for PostUp persistence dns_server_ips = [] dns_domain = nil dns_interface = nil if config.dns_enabled? dns_domain = config.dns_domain dns_interface = config.dns_interface dns_server_ips = config.dns_server_nodes.filter_map do |name| config.node_config(name)&.dig('private_ip') end end is_dns_server = config.dns_enabled? && config.dns_server_nodes.include?(node_name) # Build peers list (all other nodes) peers = [] config.each_node do |peer_name, peer_config| next if peer_name == node_name # Skip self peer_keys = node_keys[peer_name] next unless peer_keys # Skip if keys not available # Get symmetric PSK for this peer pair pair_key = [node_name, peer_name].sort.join('-') psk = psk_map[pair_key] peers << { name: peer_name, public_key: peer_keys[:public_key], preshared_key: psk, allowed_ips: "#{peer_config['private_ip']}/32", endpoint: "#{peer_config['host']}:#{peer_config['listen_port'] || config.listen_port}", keepalive: config.keepalive } end # Render template binding_context = binding template.result(binding_context) end |