Class: Construqt::Flavour::Mikrotik::Bgp

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/construqt/flavour/mikrotik/flavour_mikrotik_bgp.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfg) ⇒ Bgp

Returns a new instance of Bgp.



6
7
8
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_bgp.rb', line 6

def initialize(cfg)
  super(cfg)
end

Class Method Details

.header(host) ⇒ Object



77
78
79
80
81
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_bgp.rb', line 77

def self.header(host)
  #binding.pry if host.name == "s2b-l3-r01"
  self.write_peer(host)
  self.write_filter(host)
end

.set_routing_bgp_instance(host, cfg) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_bgp.rb', line 31

def self.set_routing_bgp_instance(host, cfg)
  default = {
    "name" => Schema.string.required,
    "as" => Schema.int.required.key,
    "router-id"=> Schema.address.required,
    "redistribute-connected" => Schema.boolean.default(true),
    "redistribute-static" => Schema.boolean.default(true),
    "redistribute-rip" => Schema.boolean.default(false),
    "redistribute-ospf" => Schema.boolean.default(false),
    "redistribute-other-bgp" => Schema.boolean.default(false),
    "out-filter"=>Schema.identifier.default(nil),
    "client-to-client-reflection"=>Schema.boolean.default(true),
    "ignore-as-path-len"=>Schema.boolean.default(false),
    "routing-table"=>Schema.identifier.default(nil),
    "comment"=>Schema.string.default(nil)
  }
  host.result.render_mikrotik(default, cfg, "routing", "bgp", "instance")
end

.write_filter(host) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_bgp.rb', line 10

def self.write_filter(host)
  Bgps.filters.each do |filter|
    v4_name="v4-#{filter.name}"
    v6_name="v6-#{filter.name}"
    host.result.add("set [ find chain=#{v4_name.inspect} ] comment=to_remove", nil, "routing", "filter")
    host.result.add("set [ find chain=#{v6_name.inspect} ] comment=to_remove", nil, "routing", "filter")
    filter.list.each do |rule|
      rule['network'].ips.each do |ip|
        prefix_len = ""
        if rule['prefix_length']
          prefix_len = "prefix-length=#{rule['prefix_length'].first}-#{rule['prefix_length'].last}"
        end

        host.result.add("add action=#{rule['rule']} chain=v#{ip.ipv4? ? '4':'6'}-#{filter.name} prefix=#{ip.to_string} #{prefix_len}", nil, "routing", "filter")
      end
    end

    host.result.add("remove [ find comment=to_remove && (chain=#{v4_name.inspect} || chain=#{v6_name.inspect}) ]", nil, "routing", "filter")
  end
end

.write_peer(host) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_bgp.rb', line 50

def self.write_peer(host)
  as_s = {}
  Bgps.connections.each do |bgp|
    as_s[bgp.left.as] ||= OpenStruct.new(:host => host) if bgp.left.my.host == host
    as_s[bgp.right.as] ||= OpenStruct.new(:host => host) if bgp.right.my.host == host
  end

  as_s.each do |as, val|
    host = val.host
    #puts "****** #{host.name}"
    digest=Digest::SHA256.hexdigest("#{host.name} #{host.id.first_ipv4.first_ipv4.to_s} #{as}")
    net = host.id.first_ipv4.first_ipv4.to_s.split('.')[0..1]
    net.push(digest[0..1].to_i(16).to_s)
    net.push(digest[-2..-1].to_i(16).to_s)
    router_id = IPAddress.parse(net.join('.')) # hack ..... achtung
    cfg = as.to_h.inject({}){|r,(k,v)| r[k.to_s]=v; r }.merge({
      "comment" => as.description,
      "name"=>"#{as.name}",
      "as" => as.num,
      "router-id" => router_id}).inject({}) {|r,p| r[p.first.to_s] = p.last; r}
    #puts ">>>#{cfg.inspect}"
    set_routing_bgp_instance(host, cfg)
  end

  #puts ">>>>>> #{as_s.keys}"
end

Instance Method Details

#build_config(unused, unused1) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_bgp.rb', line 108

def build_config(unused, unused1)
  #binding.pry
  #puts "as=>#{self.as} #{self.other.my.host.name}"
  self.other.my.address.first_ipv4 && set_routing_bgp_peer("name"=> "v4-#{self.other.my.host.name}-#{self.as.name}",
                                                           "comment" => "v4-#{self.other.my.host.name}-#{self.as.name}",
                                                           "instance" => "#{self.as.name}",
                                                           "remote-as" => self.other.as.num,
                                                           "address-families" => "ip",
                                                           "default-originate" => self.default_originate,
                                                           "remote-address" => self.other.my.address.first_ipv4,
                                                           "tcp-md5-key" => self.cfg.password,
                                                           "in-filter" => "v4-"+self.filter['in'].name,
                                                           "out-filter" => "v4-"+self.filter['out'].name)
  self.other.my.address.first_ipv6 && set_routing_bgp_peer("name"=> "v6-#{self.other.my.host.name}-#{self.as.name}",
                                                           "comment" => "v6-#{self.other.my.host.name}-#{self.as.name}",
                                                           "instance" => "#{self.as.name}",
                                                           "remote-as" => self.other.as.num,
                                                           "address-families" => "ipv6",
                                                           "remote-address" => self.other.my.address.first_ipv6,
                                                           "tcp-md5-key" => self.cfg.password,
                                                           "in-filter" => "v6-"+self.filter['in'].name,
                                                           "out-filter" => "v6-"+self.filter['out'].name)
end

#set_routing_bgp_peer(cfg) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_bgp.rb', line 83

def set_routing_bgp_peer(cfg)
  default = {
    "name" => Schema.identifier.required.key,
    "instance" => Schema.identifier.required,
    "remote-address" => Schema.address.required,
    "remote-as" => Schema.int.required,
    "in-filter" => Schema.identifier.required,
    "out-filter" => Schema.identifier.required,
    "tcp-md5-key" => Schema.string.default(""),
    "nexthop-choice" => Schema.identifier.default("force-self"),
    "multihop" => Schema.boolean.default(false),
    "route-reflect" => Schema.boolean.default(false),
    "hold-time" => Schema.identifier.default("3m"),
    "ttl" => Schema.identifier.default("default"),
    "address-families" => Schema.identifier.required,
    "default-originate" => Schema.identifier.default("never"),
    "remove-private-as" => Schema.boolean.default(false),
    "as-override" => Schema.boolean.default(false),
    "passive" => Schema.boolean.default(false),
    "use-bfd" => Schema.boolean.default(true),
    "comment" => Schema.string.null
  }
  self.host.result.render_mikrotik(default, cfg, "routing", "bgp", "peer")
end