Class: Construqt::Interfaces
- Inherits:
-
Object
- Object
- Construqt::Interfaces
- Defined in:
- lib/construqt/interfaces.rb
Instance Method Summary collapse
- #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
-
#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
#add_bond(host, name, cfg) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/construqt/interfaces.rb', line 86 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
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/construqt/interfaces.rb', line 119 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 |
# 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 (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
60 61 62 63 64 65 66 67 68 |
# File 'lib/construqt/interfaces.rb', line 60 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
50 51 52 53 54 55 56 57 58 |
# File 'lib/construqt/interfaces.rb', line 50 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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/construqt/interfaces.rb', line 70 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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/construqt/interfaces.rb', line 97 def add_vrrp(name, cfg) nets = {} cfg['address'].ips.each do |adr| throw "only host ip's are allowed #{adr.to_s}" if adr.ipv4? && adr.prefix != 32 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}" 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.vrrp = dev dev.address.interface = nil dev.address.host = nil dev.address.name = name end end |
#build_config(hosts = nil) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/construqt/interfaces.rb', line 144 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
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/construqt/interfaces.rb', line 131 def find(host_or_name, iface_name) if host_or_name.kind_of?(String) host = @region.hosts.find(host_or_name) throw "host not found #{host_or_name}" unless host else host = host_or_name end iface = host.interfaces[iface_name] throw "interface not found for #{iface_name}:#{host.name}" unless iface 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 |