class Proxy::DHCP::DhcpKea::Provider
Attributes
db[R]
dbname[R]
free_ips[R]
host[R]
keapostgre_network[R]
password[R]
port[R]
subnet_service[R]
username[R]
Public Class Methods
new(host, port, dbname, username, password, keapostgre_network, subnet_service, free_ip_service, db)
click to toggle source
Calls superclass method
# File lib/smart_proxy_dhcp_kea/dhcp_kea_main.rb, line 9 def initialize(host, port, dbname, username, password, keapostgre_network, subnet_service, free_ip_service, db) @host = host @port = port @db = db @dbname = dbname @username = username @password = password @keapostgre_network = keapostgre_network @subnet_service = subnet_service @free_ips = free_ip_service super('kea', 'nil', subnet_service, free_ips) end
Public Instance Methods
add_record(options={})
click to toggle source
Calls superclass method
# File lib/smart_proxy_dhcp_kea/dhcp_kea_main.rb, line 87 def add_record(options={}) logger.debug "Hui1 '#{options}'" record = super(options) logger.debug "Hui '#{record}'" keapostgre_network.add_dhcp_record options record rescue Exception => e logger.error msg = "Error adding DHCP record: #{e}" raise Proxy::DHCP::Error, msg end
del_record(record)
click to toggle source
# File lib/smart_proxy_dhcp_kea/dhcp_kea_main.rb, line 98 def del_record(record) logger.debug "Hui '#{record}'" # libvirt only supports one subnet per network keapostgre_network.del_dhcp_record record rescue Exception => e logger.error msg = "Error removing DHCP record: #{e}" raise Proxy::DHCP::Error, msg end
del_record_by_mac(subnet_address, mac_address)
click to toggle source
# File lib/smart_proxy_dhcp_kea/dhcp_kea_main.rb, line 123 def del_record_by_mac(subnet_address, mac_address) keapostgre_network.del_record_by_mac mac_address end
find_record(subnet_address, ip_or_mac_address)
click to toggle source
# File lib/smart_proxy_dhcp_kea/dhcp_kea_main.rb, line 107 def find_record(subnet_address, ip_or_mac_address) if ip_or_mac_address =~ Resolv::IPv4::Regex keapostgre_network.find_records_by_ip ip_or_mac_address else keapostgre_network.find_records_by_mac ip_or_mac_address end end
find_records_by_ip(subnet_address, ip_address)
click to toggle source
# File lib/smart_proxy_dhcp_kea/dhcp_kea_main.rb, line 115 def find_records_by_ip(subnet_address, ip_address) keapostgre_network.find_records_by_ip ip_address end
find_records_by_mac(subnet_address, mac_address)
click to toggle source
# File lib/smart_proxy_dhcp_kea/dhcp_kea_main.rb, line 119 def find_records_by_mac(subnet_address, mac_address) keapostgre_network.find_records_by_mac mac_address end
unused_ip(subnet_address, mac_address, from_address, to_address)
click to toggle source
# File lib/smart_proxy_dhcp_kea/dhcp_kea_main.rb, line 22 def unused_ip(subnet_address, mac_address, from_address, to_address) require 'ipaddr' require 'resolv' require 'pg' require 'mysql2' if db == 'postgres' con = PG.connect :dbname => 'keasubnets', :user => username, :password => password, :host => host, :port => port subnet = con.exec_params("select encode(address, 'escape') from subnets where encode(address, 'escape') like $1;", ['%' + subnet_address + '%']) if subnet.num_tuples.zero? logger.error 'This subnet does not exist. Not found in databse' return nil end else con = Mysql2::Client.new(:dbname => dbname, :user => username, :password => password, :host => host, :port => port) st = con.prepare("select subnet_prefix from dhcp4_subnet where subnet_prefix like ?;") subnet = st.execute('%' + subnet_address + '%') if subnet.count.zero? logger.warning 'This subnet does not exist. Not found in databse' return nil end end mask = '/' + subnet.getvalue(0,0)[-2..-1] subnet_address = subnet_address + mask logger.debug 'Start searching ip address in subnet ' + subnet_address unless from_address addr = IPAddr.new(subnet_address.to_s) from_address = IPAddr.new((addr.to_range().first.to_i + 1), Socket::AF_INET).to_s end unless to_address addr = IPAddr.new(subnet_address.to_s) to_address = IPAddr.new((addr.to_range().last.to_i - 1), Socket::AF_INET).to_s end logger.debug 'Starting search for a free ip address form ' + from_address.to_s + ' to ' + to_address.to_s possible_ip = free_ips.find_free_ip(from_address, to_address, all_hosts(subnet_address) + all_leases(subnet_address)) logger.debug 'Possible ip found: ' + possible_ip while possible_ip != nil begin Resolv.getname possible_ip logger.warning 'Address ' + possible_ip + ' resolved. Cannot use it' possible_ip = free_ips.find_free_ip(from_address, to_address, all_hosts(subnet_address) + all_leases(subnet_address)) rescue Resolv::ResolvError => e logger.warning 'Address ' + possible_ip + ' not resolved. Can use it' return possible_ip end end end