Support anding start and end addresses to DHCP config

This commit is contained in:
Joshua Boniface 2018-09-30 15:53:54 -04:00
parent 120b770a02
commit cb3fb84b8d
2 changed files with 32 additions and 19 deletions

View File

@ -504,7 +504,7 @@ class Transaction(object):
class DHCPServerConfiguration(object): class DHCPServerConfiguration(object):
def __init__(self, zk_conn, ipaddr, iface, vni, network, router, dns_servers): def __init__(self, zk_conn, ipaddr, iface, vni, network, router, dns_servers, start_addr, end_addr):
self.dhcp_offer_after_seconds = 1 self.dhcp_offer_after_seconds = 1
self.dhcp_acknowledge_after_seconds = 1 self.dhcp_acknowledge_after_seconds = 1
self.length_of_transaction = 60 self.length_of_transaction = 60
@ -515,36 +515,35 @@ class DHCPServerConfiguration(object):
self.iface = iface self.iface = iface
self.vni = vni self.vni = vni
network_cidr = ipaddress.IPv4Network(network, False) self.network_cidr = ipaddress.IPv4Network(network, False)
self.network = str(network_cidr.network_address) self.network = str(self.network_cidr.network_address)
self.broadcast_address = str(network_cidr.broadcast_address) self.broadcast_address = str(self.network_cidr.broadcast_address)
self.subnet_mask = str(network_cidr.netmask) self.subnet_mask = str(self.network_cidr.netmask)
self.router = router self.router = router
self.domain_name_server = dns_servers self.domain_name_server = dns_servers
self.start_addr = start_addr
self.end_addr = end_addr
# 1 day is 86400 # 1 day is 86400
self.ip_address_lease_time = 300 # seconds self.ip_address_lease_time = 300 # seconds
self.host_file = 'hosts.csv'
self.debug = lambda *args, **kw: None self.debug = lambda *args, **kw: None
def all_ip_addresses(self): def all_ip_addresses(self):
ips = ip_addresses(self.network, self.subnet_mask) ips = ip_addresses(self.network, self.subnet_mask, self.start_addr, self.end_addr)
for i in range(5):
next(ips)
return ips return ips
def network_filter(self): def network_filter(self):
return NETWORK(self.network, self.subnet_mask) return NETWORK(self.network, self.subnet_mask)
def ip_addresses(network, subnet_mask): def ip_addresses(network, subnet_mask, start_addr, end_addr):
subnet_mask = struct.unpack('>I', inet_aton(subnet_mask))[0] subnet_mask = struct.unpack('>I', inet_aton(subnet_mask))[0]
network = struct.unpack('>I', inet_aton(network))[0] network = struct.unpack('>I', inet_aton(network))[0]
network = network & subnet_mask network = network & subnet_mask
start = network + 1 start = struct.unpack('>I', inet_aton(start_addr))[0]
end = (network | (~subnet_mask & 0xffffffff)) end = struct.unpack('>I', inet_aton(end_addr))[0]
return (inet_ntoa(struct.pack('>I', i)) for i in range(start, end)) return (inet_ntoa(struct.pack('>I', i)) for i in range(start, end))
class ALL(object): class ALL(object):

View File

@ -44,6 +44,8 @@ class VXNetworkInstance():
self.ip_network = None self.ip_network = None
self.ip_cidrnetmask = None self.ip_cidrnetmask = None
self.dhcp_flag = None self.dhcp_flag = None
self.dhcp_start = None
self.dhcp_end = None
self.vxlan_nic = 'vxlan{}'.format(self.vni) self.vxlan_nic = 'vxlan{}'.format(self.vni)
self.bridge_nic = 'br{}'.format(self.vni) self.bridge_nic = 'br{}'.format(self.vni)
@ -57,26 +59,26 @@ class VXNetworkInstance():
# Zookeper handlers for changed states # Zookeper handlers for changed states
@zk_conn.DataWatch('/networks/{}'.format(self.vni)) @zk_conn.DataWatch('/networks/{}'.format(self.vni))
def watch_network_description(data, stat, event=''): def watch_network_description(data, stat, event=''):
if data != None and self.description != data.decode('ascii'): if data and self.description != data.decode('ascii'):
self.old_description = self.description self.old_description = self.description
self.description = data.decode('ascii') self.description = data.decode('ascii')
@zk_conn.DataWatch('/networks/{}/domain'.format(self.vni)) @zk_conn.DataWatch('/networks/{}/domain'.format(self.vni))
def watch_network_domain(data, stat, event=''): def watch_network_domain(data, stat, event=''):
if data != None and self.domain != data.decode('ascii'): if data and self.domain != data.decode('ascii'):
domain = data.decode('ascii') domain = data.decode('ascii')
self.domain = domain self.domain = domain
@zk_conn.DataWatch('/networks/{}/ip_network'.format(self.vni)) @zk_conn.DataWatch('/networks/{}/ip_network'.format(self.vni))
def watch_network_ip_network(data, stat, event=''): def watch_network_ip_network(data, stat, event=''):
if data != None and self.ip_network != data.decode('ascii'): if data and self.ip_network != data.decode('ascii'):
ip_network = data.decode('ascii') ip_network = data.decode('ascii')
self.ip_network = ip_network self.ip_network = ip_network
self.ip_cidrnetmask = ip_network.split('/')[-1] self.ip_cidrnetmask = ip_network.split('/')[-1]
@zk_conn.DataWatch('/networks/{}/ip_gateway'.format(self.vni)) @zk_conn.DataWatch('/networks/{}/ip_gateway'.format(self.vni))
def watch_network_gateway(data, stat, event=''): def watch_network_gateway(data, stat, event=''):
if data != None and self.ip_gateway != data.decode('ascii'): if data and self.ip_gateway != data.decode('ascii'):
if self.this_router.network_state == 'primary': if self.this_router.network_state == 'primary':
if self.ip_gateway: if self.ip_gateway:
self.removeGatewayAddress() self.removeGatewayAddress()
@ -85,13 +87,23 @@ class VXNetworkInstance():
@zk_conn.DataWatch('/networks/{}/dhcp_flag'.format(self.vni)) @zk_conn.DataWatch('/networks/{}/dhcp_flag'.format(self.vni))
def watch_network_dhcp_status(data, stat, event=''): def watch_network_dhcp_status(data, stat, event=''):
if data != None and self.dhcp_flag != data.decode('ascii'): if data and self.dhcp_flag != data.decode('ascii'):
self.dhcp_flag = ( data.decode('ascii') == 'True' ) self.dhcp_flag = ( data.decode('ascii') == 'True' )
if self.dhcp_flag and self.this_router.network_state == 'primary': if self.dhcp_flag and self.this_router.network_state == 'primary':
self.startDHCPServer() self.startDHCPServer()
elif self.this_router.network_state == 'primary': elif self.this_router.network_state == 'primary':
self.stopDHCPServer() self.stopDHCPServer()
@zk_conn.DataWatch('/networks/{}/dhcp_start'.format(self.vni))
def watch_network_dhcp_start(data, stat, event=''):
if data and self.dhcp_start != data.decode('ascii'):
self.dhcp_start = data.decode('ascii')
@zk_conn.DataWatch('/networks/{}/dhcp_end'.format(self.vni))
def watch_network_dhcp_end(data, stat, event=''):
if data and self.dhcp_end != data.decode('ascii'):
self.dhcp_end = data.decode('ascii')
def getvni(self): def getvni(self):
return self.vni return self.vni
@ -176,7 +188,9 @@ class VXNetworkInstance():
vni=self.vni, vni=self.vni,
network=self.ip_network, network=self.ip_network,
router=[self.ip_gateway], router=[self.ip_gateway],
dns_servers=[self.ip_gateway] dns_servers=[self.ip_gateway],
start_addr=self.dhcp_start,
end_addr=self.dhcp_end
) )
dhcp_configuration.debug = print dhcp_configuration.debug = print
self.dhcp_server = DHCPServer.DHCPServer(dhcp_configuration) self.dhcp_server = DHCPServer.DHCPServer(dhcp_configuration)