Class: Construqt::Interfaces
- Inherits:
-
Object
- Object
- Construqt::Interfaces
- Defined in:
- lib/construqt/interfaces.rb
Instance Method Summary collapse
- #_find(host_or_name, iface_name) ⇒ Object
- #add_bond(host, name, cfg) ⇒ Object
- #add_bridge(host, name, cfg) ⇒ Object
- #add_device(host, dev_name, cfg) ⇒ Object
- #add_gre(host, name, cfg) ⇒ Object
- #add_openvpn(host, name, cfg) ⇒ Object
- #add_vlan(host, name, cfg) ⇒ Object
- #add_vrrp(name, cfg) ⇒ Object
- #build_config(hosts = nil) ⇒ Object
- #find(host_or_name, iface_name) ⇒ Object
- #find!(host_or_name, iface_name) ⇒ Object
-
#initialize(region) ⇒ Interfaces
constructor
A new instance of Interfaces.
- #setup_template(iface) ⇒ Object
Constructor Details
#initialize(region) ⇒ Interfaces
Returns a new instance of Interfaces.
3 4 5 |
# File 'lib/construqt/interfaces.rb', line 3 def initialize(region) @region = region end |
Instance Method Details
#_find(host_or_name, iface_name) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/construqt/interfaces.rb', line 138 def _find(host_or_name, iface_name) if host_or_name.kind_of?(String) host = @region.hosts.find(host_or_name) return [nil, nil] unless host else host = host_or_name end iface = host.interfaces[iface_name] return [host, nil] unless iface [host, iface] end |
#add_bond(host, name, cfg) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/construqt/interfaces.rb', line 88 def add_bond(host, name, cfg) cfg['interfaces'].each do |interface| throw "interface not one same host:#{interface.host.name}:#{host.name}" unless host.name == interface.host.name end cfg['clazz'] = "bond" dev = add_device(host, name, cfg) dev.address.interface = host.interfaces[name] if dev.address dev end |
#add_bridge(host, name, cfg) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/construqt/interfaces.rb', line 126 def add_bridge(host, name, cfg) #cfg['interfaces'] = [] cfg['interfaces'].each do |interface| throw "interface not one same host:#{interface.host.name}:#{host.name}" unless host.name == interface.host.name end cfg['clazz'] = "bridge" dev = add_device(host, name, cfg) dev.address.interface = host.interfaces[name] if dev.address dev end |
#add_device(host, dev_name, cfg) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/construqt/interfaces.rb', line 21 def add_device(host, dev_name, cfg) throw "Host not found:#{dev_name}" unless host throw "Interface is duplicated:#{host.name}:#{dev_name}" if host.interfaces[dev_name] throw "invalid name #{dev_name}" unless dev_name.match(/^[A-Za-z0-9\-\.]+$/) if match=/^.*[^\d](\d+)$/.match(dev_name) cfg['number'] ||= match[1].to_i end cfg['host'] = host cfg['mtu'] ||= 1500 #binding.pry if host && host.name == "ct-iar1-ham" # binding.pry cfg['clazz'] ||= "device" cfg['address'] ||= nil cfg['firewalls'] ||= [] cfg['firewalls'] = cfg['firewalls'].map{|i| i.kind_of?(String) ? Construqt::Firewalls.find(i) : i } (dev_name, iface) = Construqt::Tags.add(dev_name) { |name| host.flavour.create_interface(name, cfg) } # iface.clazz.attach = iface host.interfaces[dev_name] = iface host.interfaces[dev_name].address.interface = host.interfaces[dev_name] if host.interfaces[dev_name].address setup_template(iface) if iface.template host.interfaces[dev_name] end |
#add_gre(host, name, cfg) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/construqt/interfaces.rb', line 62 def add_gre(host, name, cfg) throw "we need an address on this cfg #{cfg.inspect}" unless cfg['address'] cfg['clazz'] = "gre" cfg['local'] ||= nil cfg['remote'] ||= nil dev = add_device(host, name, cfg) dev.address.interface = host.interfaces[name] if dev.address dev end |
#add_openvpn(host, name, cfg) ⇒ Object
def add_template(host, name, cfg)
cfg['clazz'] = "template"
cfg['host'] = host
cfg['name'] = name
self.add_device(host,name, cfg)
end
52 53 54 55 56 57 58 59 60 |
# File 'lib/construqt/interfaces.rb', line 52 def add_openvpn(host, name, cfg) cfg['clazz'] = "opvn" cfg['ipv6'] ||= nil cfg['ipv4'] ||= nil dev = add_device(host, name, cfg) dev.address.interface = host.interfaces[name] if dev.address dev.network.name = "#{name}-#{host.name}" dev end |
#add_vlan(host, name, cfg) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/construqt/interfaces.rb', line 72 def add_vlan(host, name, cfg) unless cfg["vlan_id"].to_s.match(/^[0-9]+$/) && 1 <= cfg["vlan_id"].to_i && cfg["vlan_id"].to_i < 4096 throw "vlan_id must be set on vlan with name #{name}" end cfg = cfg.clone interfaces = cfg['interfaces'] || [] interfaces << cfg['interface'] if cfg['interface'] cfg.delete('interface') cfg['interfaces'] = interfaces # throw "we need an interface #{cfg['interfaces']}" if cfg['interfaces'].empty? cfg['clazz'] = "vlan" dev = add_device(host, name, cfg) dev.address.interface = host.interfaces[name] if dev.address dev end |
#add_vrrp(name, cfg) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/construqt/interfaces.rb', line 99 def add_vrrp(name, cfg) nets = {} cfg['address'].ips.each do |adr| if adr.ipv4? && adr.prefix != 32 unless cfg['address'].routes.find{ |rt| adr.include?(rt.via) } throw "only host ip's are allowed #{adr.to_s} with prefix != 32 or route" end end throw "only host ip's are allowed #{adr.to_s}" if adr.ipv6? && adr.prefix != 128 nets[adr.network.to_s] = true end cfg['interfaces'].each do |interface| throw "interface need priority #{interface.name}" unless interface.priority throw "interface not found:#{name}" unless interface cfg['clazz'] = "vrrp" cfg['interface'] = interface throw "vrrp interface does not have within the same network" if nets.length == interface.address.ips.select { |adr| nets[adr.network.to_s] }.length dev = add_device(interface.host, name, cfg) # interface.firewalls.push(*(dev.firewalls || [])) interface.vrrp = dev dev.address.interface = nil dev.address.host = nil dev.address.name = name end end |
#build_config(hosts = nil) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/construqt/interfaces.rb', line 163 def build_config(hosts = nil) (hosts||Hosts.get_hosts).each do |host| by_clazz = {} host.interfaces.values.each do |interface| #throw "class less interface #{interface.inspect}" unless interface.clazz #throw "no clazz defined in interface #{interface.clazz}" unless interface.clazz.name name = interface.clazz # .name[interface.clazz.name.rindex(':')+1..-1].downcase #puts "<<<<<<< #{name}" by_clazz[name] ||= [] by_clazz[name] << interface end #binding.pry ["host", "device", "vlan", "bond", "bridge", "vrrp", "gre", "bgp", "opvn", "ipsec"].each do |key| next unless by_clazz[key] by_clazz[key].each do |interface| #Construqt.logger.debug "Interface:build_config:#{interface.name}:#{interface.class.name}:#{interface.ident}" interface.build_config(host, interface) end end end end |
#find(host_or_name, iface_name) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/construqt/interfaces.rb', line 156 def find(host_or_name, iface_name) (host, iface) = _find(host_or_name, iface_name) throw "host not found #{host_or_name}" if host.nil? throw "interface not found for #{iface_name}:#{host.name}" if iface.nil? iface end |
#find!(host_or_name, iface_name) ⇒ Object
150 151 152 153 154 |
# File 'lib/construqt/interfaces.rb', line 150 def find!(host_or_name, iface_name) (host, iface) = _find(host_or_name, iface_name) return nil if host.nil? || iface.nil? iface end |
#setup_template(iface) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/construqt/interfaces.rb', line 7 def setup_template(iface) iface.template.vlans.each do |vlan| vname = vlan.description to_add_iface = iface.host.interfaces[vname] unless to_add_iface to_add_iface = add_vlan(iface.host, vname, vlan.to_h.inject({}){|r,(k,v)| r[k.to_s]=v; r }) end #puts ">>>>>#{iface.name}" to_add_iface.interfaces << iface end end |