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
18
19
20
21
22
23
# 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 => lambda {|o|
    (o.kind_of?(IPAddress::IPv4)||o.kind_of?(Construqt::Addresses::CqIpAddress)) && o.ipv4?
  },
                                                  :filter => lambda {|ip| ip.ipv4? }))
  host.result.add(self, bird_v4, Construqt::Resources::Rights.root_0644(Construqt::Resources::Component::BGP), "etc", "bird", "bird.conf")
  bird_v6 = self.header_bird(host, OpenStruct.new(:net_clazz => lambda {|o|
    (o.kind_of?(IPAddress::IPv6)||o.kind_of?(Construqt::Addresses::CqIpAddress)) && o.ipv6?
  },
                                                  :filter => lambda {|ip| ip.ipv6? }))
  host.result.add(self, bird_v6, Construqt::Resources::Rights.root_0644(Construqt::Resources::Component::BGP), "etc", "bird", "bird6.conf")
end

.header_bird(host, mode) ⇒ Object



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

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)
        #binding.pry
        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['addr_sub_prefix']
          ip_str = "#{ip.to_string}{#{ip.prefix},#{ip.ipv4? ? 32 : 128}}"
        elsif 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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/construqt/flavour/ubuntu/flavour_ubuntu_bgp.rb', line 94

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(Construqt::Resources::Component::BGP), "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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/construqt/flavour/ubuntu/flavour_ubuntu_bgp.rb', line 76

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(Construqt::Resources::Component::BGP), "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



113
114
115
116
117
# File 'lib/construqt/flavour/ubuntu/flavour_ubuntu_bgp.rb', line 113

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