Class: Construqt::Flavour::Ubuntu::Bgp

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/construqt/flavour/ubuntu/flavour_ubuntu_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/ubuntu/flavour_ubuntu_bgp.rb', line 6

def initialize(cfg)
  super(cfg)
end

Class Method Details

.header(host) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/construqt/flavour/ubuntu/flavour_ubuntu_bgp.rb', line 10

def self.header(host)
  return if host.bgps.empty?
  # binding.pry
  bird_v4 = self.header_bird(host, OpenStruct.new(:net_clazz => IPAddress::IPv4, :filter => lambda {|ip| ip.ipv4? }))
  host.result.add(self, bird_v4, Construqt::Resources::Rights::ROOT_0644, "etc", "bird", "bird.conf")
  bird_v6 = self.header_bird(host, OpenStruct.new(:net_clazz => IPAddress::IPv6, :filter => lambda {|ip| ip.ipv6? }))
  host.result.add(self, bird_v6, Construqt::Resources::Rights::ROOT_0644, "etc", "bird", "bird6.conf")
end

.header_bird(host, mode) ⇒ Object



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
# File 'lib/construqt/flavour/ubuntu/flavour_ubuntu_bgp.rb', line 19

def self.header_bird(host, mode)
  #binding.pry
  ret = <<BGP
log syslog { debug, trace, info, remote, warning, error, auth, fatal, bug };
router id #{host.id.first_ipv4.first_ipv4.to_s};
protocol device {
}
protocol direct {
}
protocol kernel {
learn;
persist;                # Don't remove routes on bird shutdown
scan time 20;           # Scan kernel routing table every 20 seconds
export all;             # Default is export none
}
protocol static {
}

BGP
  Bgps.filters.each do |filter|
    ret = ret + "filter filter_#{filter.name} {\n"
    filter.list.each do |rule|
      nets = rule['network']
      if nets.kind_of?(String)
        nets = Construqt::Tags.find(nets, mode.net_clazz)
        #            puts ">>>>>>>>>> #{nets.map{|i| i.class.name}}"
        nets = IPAddress::summarize(nets)
      else
        nets = nets.ips
      end

      nets.each do |ip|
        next unless mode.filter.call(ip)
        ip_str = ip.to_string
        if rule['prefix_length']
          ip_str = "#{ip.to_string}{#{rule['prefix_length'].first},#{rule['prefix_length'].last}}"
        end

        ret = ret + "  if net ~ [ #{ip_str} ] then { print \"#{rule['rule']}:\",net; #{rule['rule']}; }\n"
      end
    end

    ret = ret + "}\n\n"
  end

  ret
end

Instance Method Details

#build_bird6_confObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/construqt/flavour/ubuntu/flavour_ubuntu_bgp.rb', line 85

def build_bird6_conf
  #      binding.pry
  if self.my.address.first_ipv6 && self.other.my.address.first_ipv6
    self.my.host.result.add(self, <<BGP, Construqt::Resources::Rights::ROOT_0644, "etc", "bird", "bird6.conf")
protocol bgp #{Util.clean_bgp(self.my.host.name)}_#{Util.clean_bgp(self.other.host.name)} {
description "#{self.my.host.name} <=> #{self.other.host.name}";
direct;
next hop self;
    #{self.as == self.other.as ? '' : '#'}rr client;
local as #{self.as.num};
neighbor #{self.other.my.address.first_ipv6}  as #{self.other.as.num};
password "#{Util.password(self.cfg.password)}";
import #{self.filter['in'] ? "filter filter_"+self.filter['in'].name : "all"};
export #{self.filter['out'] ? "filter filter_"+self.filter['out'].name : "all"};
}
BGP
  end
end

#build_bird_confObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/construqt/flavour/ubuntu/flavour_ubuntu_bgp.rb', line 67

def build_bird_conf
  if self.my.address.first_ipv4 && self.other.my.address.first_ipv4
    self.my.host.result.add(self, <<BGP, Construqt::Resources::Rights::ROOT_0644, "etc", "bird", "bird.conf")
protocol bgp #{Util.clean_bgp(self.my.host.name)}_#{Util.clean_bgp(self.other.host.name)} {
description "#{self.my.host.name} <=> #{self.other.host.name}";
direct;
next hop self;
    #{self.as == self.other.as ? '' : '#'}rr client;
local #{self.my.address.first_ipv4} as #{self.as.num};
neighbor #{self.other.my.address.first_ipv4}  as #{self.other.as.num};
password "#{Util.password(self.cfg.password)}";
import #{self.filter['in'] ? "filter filter_"+self.filter['in'].name : "all"};
export #{self.filter['out'] ? "filter filter_"+self.filter['out'].name : "all"};
}
BGP
  end
end

#build_config(unused, unused1) ⇒ Object



104
105
106
107
108
# File 'lib/construqt/flavour/ubuntu/flavour_ubuntu_bgp.rb', line 104

def build_config(unused, unused1)
  # binding.pry
  build_bird_conf
  build_bird6_conf
end