#!/usr/bin/env ruby require_relative 'utils.rb' class BgpRegistry < Registry def update_configs template_name = data["host"]["bird"]["template_name"] rescue nil template_name or die "bird.template_name not set for this host" peers = data["network"] v4_templ, v6_templ = generate_configs(template_name, peers) bird_path = Pathname.new(File.expand_path("../../bird/", __FILE__)) atomic_write(bird_path.join("peers.conf"), v4_templ) atomic_write(bird_path.join("peers6.conf"), v6_templ) end def generate_configs(template_name, peers) template_path = Pathname.new(File.expand_path("../../templates", __FILE__)) template = Template.new(template_path.join("bird.conf.erb")) v4_peers = collect_bgp_peers(peers, template_name, :v4) v6_peers = collect_bgp_peers(peers, template_name, :v6) return template.render(peers: v4_peers), template.render(peers: v6_peers) end private def collect_bgp_peers(peers, template_name, proto) peers.map do |name, peer| if peer["as"] && peer["#{proto}_tunnel"] context = { name: name, template: template_name, tunnel_ip: peer["#{proto}_tunnel"], as: peer["as"] } TemplateContext.new(context) end end.compact! end end