Add DHCP server component based on python_dhcp_server
This commit is contained in:
@ -27,6 +27,8 @@ import daemon_lib.ansiiprint as ansiiprint
|
||||
import daemon_lib.zkhandler as zkhandler
|
||||
import daemon_lib.common as common
|
||||
|
||||
import pvcrd.DHCPServer as DHCPServer
|
||||
|
||||
class VXNetworkInstance():
|
||||
# Initialization function
|
||||
def __init__ (self, vni, zk_conn, config, this_router):
|
||||
@ -37,6 +39,7 @@ class VXNetworkInstance():
|
||||
|
||||
self.old_description = zkhandler.readdata(self.zk_conn, '/networks/{}'.format(self.vni))
|
||||
self.description = zkhandler.readdata(self.zk_conn, '/networks/{}'.format(self.vni))
|
||||
self.domain = zkhandler.readdata(self.zk_conn, '/networks/{}/domain'.format(self.vni))
|
||||
self.ip_gateway = zkhandler.readdata(self.zk_conn, '/networks/{}/ip_gateway'.format(self.vni))
|
||||
self.ip_network = zkhandler.readdata(self.zk_conn, '/networks/{}/ip_network'.format(self.vni))
|
||||
self.ip_cidrnetmask = self.ip_network.split('/')[-1]
|
||||
@ -45,6 +48,9 @@ class VXNetworkInstance():
|
||||
self.vxlan_nic = 'vxlan{}'.format(self.vni)
|
||||
self.bridge_nic = 'br{}'.format(self.vni)
|
||||
|
||||
self.dhcp_reservations = {}
|
||||
self.firewall_rules = {}
|
||||
|
||||
self.createNetwork()
|
||||
|
||||
# Zookeper handlers for changed states
|
||||
@ -72,32 +78,159 @@ class VXNetworkInstance():
|
||||
def watch_network_dhcp_status(data, stat, event=''):
|
||||
if data != None and self.dhcp_flag != data.decode('ascii'):
|
||||
self.dhcp_flag = ( data.decode('ascii') == 'True' )
|
||||
if self.dhcp_flag:
|
||||
self.dhcp_instance = DHCPInstance.DHCPInstance(self.vni, self.config)
|
||||
else:
|
||||
self.dhcp_instance.remove()
|
||||
self.dhcp_instance = None
|
||||
|
||||
|
||||
@zk_conn.ChildrenWatch('/networks/{}/dhcp_reservations'.format(self.vni))
|
||||
def watch_network_dhcp_reservations(data, event=''):
|
||||
print(data)
|
||||
|
||||
def getvni(self):
|
||||
return self.vni
|
||||
|
||||
def createNetwork(self):
|
||||
ansiiprint.echo('Creating VNI {} device on interface {}'.format(self.vni, self.vni_dev), '', 'o')
|
||||
common.run_os_command('ip link add {} type vxlan id {} dstport 4789 dev {}'.format(self.vxlan_nic, self.vni, self.vni_dev))
|
||||
common.run_os_command('brctl addbr {}'.format(self.bridge_nic))
|
||||
common.run_os_command('brctl addif {} {}'.format(self.bridge_nic, self.vxlan_nic))
|
||||
common.run_os_command('ip link set {} up'.format(self.vxlan_nic))
|
||||
common.run_os_command('ip link set {} up'.format(self.bridge_nic))
|
||||
ansiiprint.echo(
|
||||
'Creating VNI {} device on interface {}'.format(
|
||||
self.vni,
|
||||
self.vni_dev
|
||||
),
|
||||
'',
|
||||
'o'
|
||||
)
|
||||
common.run_os_command(
|
||||
'ip link add {} type vxlan id {} dstport 4789 dev {}'.format(
|
||||
self.vxlan_nic,
|
||||
self.vni,
|
||||
self.vni_dev
|
||||
)
|
||||
)
|
||||
common.run_os_command(
|
||||
'brctl addbr {}'.format(
|
||||
self.bridge_nic
|
||||
)
|
||||
)
|
||||
common.run_os_command(
|
||||
'brctl addif {} {}'.format(
|
||||
self.bridge_nic,
|
||||
self.vxlan_nic
|
||||
)
|
||||
)
|
||||
common.run_os_command(
|
||||
'ip link set {} up'.format(
|
||||
self.vxlan_nic
|
||||
)
|
||||
)
|
||||
common.run_os_command(
|
||||
'ip link set {} up'.format(
|
||||
self.bridge_nic
|
||||
)
|
||||
)
|
||||
|
||||
def createAddress(self):
|
||||
def createGatewayAddress(self):
|
||||
if self.this_router.getnetworkstate() == 'primary':
|
||||
ansiiprint.echo('Creating gateway {} on interface {} (VNI {})'.format(self.ip_gateway, self.bridge_nic, self.vni), '', 'o')
|
||||
common.run_os_command('ip address add {}/{} dev {}'.format(self.ip_gateway, self.ip_cidrnetmask, self.bridge_nic))
|
||||
common.run_os_command('arping -A -c2 -I {} {}'.format(self.bridge_nic, self.ip_gateway), background=True)
|
||||
ansiiprint.echo(
|
||||
'Creating gateway {} on interface {} (VNI {})'.format(
|
||||
self.ip_gateway,
|
||||
self.bridge_nic,
|
||||
self.vni
|
||||
),
|
||||
'',
|
||||
'o'
|
||||
)
|
||||
common.run_os_command(
|
||||
'ip address add {}/{} dev {}'.format(
|
||||
self.ip_gateway,
|
||||
self.ip_cidrnetmask,
|
||||
self.bridge_nic
|
||||
)
|
||||
)
|
||||
common.run_os_command(
|
||||
'arping -A -c2 -I {} {}'.format(
|
||||
self.bridge_nic,
|
||||
self.ip_gateway
|
||||
),
|
||||
background=True
|
||||
)
|
||||
|
||||
def startDHCPServer(self):
|
||||
if self.this_router.getnetworkstate() == 'primary':
|
||||
ansiiprint.echo(
|
||||
'Starting DHCP server on interface {} (VNI {})'.format(
|
||||
self.bridge_nic,
|
||||
self.vni
|
||||
),
|
||||
'',
|
||||
'o'
|
||||
)
|
||||
config = {
|
||||
'ipaddr': self.ip_gateway,
|
||||
'iface': self.bridge_nic,
|
||||
'network': self.ip_network,
|
||||
'router': [self.ip_gateway],
|
||||
'dns_servers': [self.ip_gateway]
|
||||
}
|
||||
dhcp_configuration = DHCPServerConfiguration(config)
|
||||
dhcp_configuration.debug = print
|
||||
self.dhcp_server = DHCPServer.DHCPServer(dhcp_configuration)
|
||||
self.dhcp_server.start()
|
||||
|
||||
def removeNetwork(self):
|
||||
ansiiprint.echo('Removing VNI {} device on interface {}'.format(self.vni, self.vni_dev), '', 'o')
|
||||
common.run_os_command('ip link set {} down'.format(self.bridge_nic))
|
||||
common.run_os_command('ip link set {} down'.format(self.vxlan_nic))
|
||||
common.run_os_command('brctl delif {} {}'.format(self.bridge_nic, self.vxlan_nic))
|
||||
common.run_os_command('brctl delbr {}'.format(self.bridge_nic))
|
||||
common.run_os_command('ip link delete {}'.format(self.vxlan_nic))
|
||||
ansiiprint.echo(
|
||||
'Removing VNI {} device on interface {}'.format(
|
||||
self.vni,
|
||||
self.vni_dev
|
||||
),
|
||||
'',
|
||||
'o'
|
||||
)
|
||||
common.run_os_command(
|
||||
'ip link set {} down'.format(
|
||||
self.bridge_nic
|
||||
)
|
||||
)
|
||||
common.run_os_command(
|
||||
'ip link set {} down'.format(
|
||||
self.vxlan_nic
|
||||
)
|
||||
)
|
||||
common.run_os_command(
|
||||
'brctl delif {} {}'.format(
|
||||
self.bridge_nic,
|
||||
self.vxlan_nic
|
||||
)
|
||||
)
|
||||
common.run_os_command(
|
||||
'brctl delbr {}'.format(
|
||||
self.bridge_nic
|
||||
)
|
||||
)
|
||||
common.run_os_command(
|
||||
'ip link delete {}'.format(
|
||||
self.vxlan_nic
|
||||
)
|
||||
)
|
||||
|
||||
def removeAddress(self):
|
||||
ansiiprint.echo('Removing gateway {} from interface {} (VNI {})'.format(self.ip_gateway, self.bridge_nic, self.vni), '', 'o')
|
||||
common.run_os_command('ip address delete {}/{} dev {}'.format(self.ip_gateway, self.ip_cidrnetmask, self.bridge_nic))
|
||||
def removeGatewayAddress(self):
|
||||
ansiiprint.echo(
|
||||
'Removing gateway {} from interface {} (VNI {})'.format(
|
||||
self.ip_gateway,
|
||||
self.bridge_nic,
|
||||
self.vni
|
||||
),
|
||||
'',
|
||||
'o'
|
||||
)
|
||||
common.run_os_command(
|
||||
'ip address delete {}/{} dev {}'.format(
|
||||
self.ip_gateway,
|
||||
self.ip_cidrnetmask,
|
||||
self.bridge_nic
|
||||
)
|
||||
)
|
||||
|
||||
def stopDHCPServer(self):
|
||||
self.dhcp_server.close()
|
||||
|
Reference in New Issue
Block a user