Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions cyder/core/ctnr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,6 @@ def eg_metadata():
{'name': 'description', 'datatype': 'string', 'editable': True},
]}

def build_legacy_classes(self, ip_type):
if ip_type == '4':
ranges = self.ranges.filter(
Q(range_type=DYNAMIC, dhcp_enabled=True) |
Q(start_str='10.255.255.255'),
ip_type='4')
elif ip_type == '6':
ranges = self.ranges.filter(
ip_type='6', range_type=DYNAMIC, dhcp_enabled=True)

build_str = ""
for range_ in ranges:
classname = '{0}:{1}:{2}'.format(
self.name, range_.start_str, range_.end_str)
build_str += (
'class "{0}" {{\n'
'\tmatch hardware;\n'
'}}\n'.format(classname))
clients = range_.dynamicinterface_set.filter(
ctnr=self, dhcp_enabled=True).exclude(mac=None)
for client in clients:
build_str += client.build_subclass(classname)
return build_str


class CtnrUser(BaseModel, ObjectUrlMixin):
user = models.ForeignKey(User)
Expand Down
4 changes: 1 addition & 3 deletions cyder/cydhcp/build/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from cyder.base.vcs import GitRepo

from cyder.core.utils import fail_mail
from cyder.core.ctnr.models import Ctnr
from cyder.cydhcp.network.models import Network
from cyder.cydhcp.range.models import Range
from cyder.cydhcp.vrf.models import Vrf
from cyder.cydhcp.workgroup.models import Workgroup

Expand Down Expand Up @@ -100,8 +100,6 @@ def build(self):
self.log_info('Building v{}...'.format(ip_type))
with open(os.path.join(self.stage_dir, files['target_file']),
'w') as f:
for ctnr in Ctnr.objects.all():
f.write(ctnr.build_legacy_classes(ip_type))
for vrf in Vrf.objects.all():
f.write(vrf.build_vrf(ip_type))
for network in Network.objects.filter(
Expand Down
19 changes: 11 additions & 8 deletions cyder/cydhcp/constants.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
ALLOW_ANY = 'a'
ALLOW_ALL = 'a'
ALLOW_KNOWN = 'k'
ALLOW_LEGACY = 'l'
ALLOW_STANDARD = 's'
ALLOW_VRF = 'v'

ALLOW_OPTIONS = [
(ALLOW_ANY, 'ANY: Allow any client'),
(ALLOW_KNOWN, 'KNOWN: Allow known clients'),
(ALLOW_LEGACY, "LEGACY: Allow any client that shares at least one of this "
"range's containers"),
(ALLOW_VRF, "VRF: Allow any client that shares this range's VRF"),
(ALLOW_STANDARD, 'STANDARD: All clients in this range'),
(ALLOW_ALL, 'ALL: All clients, even those not registered in Cyder'),
(ALLOW_KNOWN, 'KNOWN: All clients registered in Cyder'),
(ALLOW_LEGACY,
'LEGACY: All clients that are in this range and in one of its '
'containers'),
(ALLOW_VRF, "VRF: All clients in this range's VRF")
]

STATIC = "st"
DYNAMIC = "dy"
STATIC = 'st'
DYNAMIC = 'dy'
RANGE_TYPE = (
(STATIC, 'Static'),
(DYNAMIC, 'Dynamic'),
Expand Down
28 changes: 17 additions & 11 deletions cyder/cydhcp/network/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from cyder.base.mixins import ObjectUrlMixin
from cyder.base.models import BaseModel
from cyder.base.utils import transaction_atomic
from cyder.cydhcp.constants import DYNAMIC
from cyder.cydhcp.constants import ALLOW_LEGACY, ALLOW_STANDARD, DYNAMIC
from cyder.cydhcp.utils import IPFilter, join_dhcp_args
from cyder.cydhcp.vlan.models import Vlan
from cyder.cydhcp.vrf.models import Vrf
Expand Down Expand Up @@ -220,29 +220,35 @@ def get_related_sites(self, networks=None):
return set([network.site for network in networks]).discard(None)

def build_subnet(self, raw=False):
build_str = ''
ranges = self.range_set.filter(
range_type=DYNAMIC, dhcp_enabled=True)

# Build classes.
for rng in ranges.filter(allow__in=(ALLOW_STANDARD, ALLOW_LEGACY)):
build_str += rng.build_classes()

# Build subnet declaration.
self.update_network()
statements = self.networkav_set.filter(
attribute__attribute_type=ATTRIBUTE_STATEMENT)
options = self.networkav_set.filter(
attribute__attribute_type=ATTRIBUTE_OPTION)
ranges = self.range_set.filter(range_type=DYNAMIC, dhcp_enabled=True)
if self.ip_type == IP_TYPE_4:
build_str = "\nsubnet {0} netmask {1} {{\n".format(
build_str += 'subnet {0} netmask {1} {{\n'.format(
self.network.network, self.network.netmask)
else:
build_str = "\nsubnet6 {0} netmask {1} {{\n".format(
build_str += 'subnet6 {0} netmask {1} {{\n'.format(
self.network.network, self.network.netmask)
if not raw:
build_str += "\t# Network statements\n"
build_str += join_dhcp_args(statements)
build_str += "\t# Network options\n"
build_str += join_dhcp_args(options)
if self.dhcpd_raw_include:
build_str += "\t# Raw network options\n"
build_str += join_dhcp_args(self.dhcpd_raw_include.split("\n"))
for range_ in ranges:
build_str += range_.build_range()
build_str += "}\n"
build_str += join_dhcp_args(self.dhcpd_raw_include.split('\n'))
for rng in ranges:
build_str += rng.build_pool()
build_str += '}\n\n'

return build_str

def get_related(self):
Expand Down
97 changes: 59 additions & 38 deletions cyder/cydhcp/range/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from cyder.base.models import BaseModel
from cyder.base.utils import simple_descriptor, transaction_atomic
from cyder.cydns.validation import validate_ip_type
from cyder.cydhcp.constants import (ALLOW_OPTIONS, ALLOW_ANY, ALLOW_KNOWN,
ALLOW_LEGACY, ALLOW_VRF, RANGE_TYPE,
STATIC, DYNAMIC)
from cyder.cydhcp.constants import (
ALLOW_OPTIONS, ALLOW_ALL, ALLOW_STANDARD, ALLOW_KNOWN, ALLOW_LEGACY,
ALLOW_VRF, RANGE_TYPE, STATIC, DYNAMIC)
from cyder.cydhcp.interface.static_intr.models import StaticInterface
from cyder.cydhcp.network.models import Network
from cyder.cydhcp.utils import (IPFilter, four_to_two, join_dhcp_args,
Expand Down Expand Up @@ -87,7 +87,7 @@ def pretty_type(self, obj, type):
is_reserved = models.BooleanField(default=False, blank=False)

allow = models.CharField(max_length=1, choices=ALLOW_OPTIONS,
default=ALLOW_LEGACY)
default=ALLOW_STANDARD)

dhcpd_raw_include = models.TextField(blank=True)
dhcp_enabled = models.BooleanField(default=True)
Expand Down Expand Up @@ -272,30 +272,27 @@ def clean(self):

self.check_for_overlaps()

def get_allow_deny_list(self):
if self.allow == ALLOW_ANY:
allow = []
def get_allow_deny_lines(self):
if self.allow == ALLOW_ALL:
lines = []
elif self.allow == ALLOW_KNOWN:
# FIXME: add hyphen once compatibility with Maintain is established
allow = ['allow known clients']
allow += [
'allow members of "{0}:{1}:{2}"'.format(
ctnr.name, self.start_str, self.end_str)
for ctnr in self.ctnr_set.all()]
lines = ['allow known-clients']
else:
allow = []
lines = []
if self.allow == ALLOW_VRF:
allow += ['allow members of "{0}"'.format(
self.network.vrf.name)]
if self.allow == ALLOW_LEGACY:
allow += [
'allow members of "{0}:{1}:{2}"'.format(
ctnr.name, self.start_str, self.end_str)
for ctnr in self.ctnr_set.all()]
if not allow:
allow += ['deny unknown-clients']

return allow
lines.append('allow members of "{0}"'.format(
self.network.vrf.name))
elif self.allow == ALLOW_LEGACY:
for ctnr in self.ctnr_set.all():
lines.append('allow members of "{}:{}:{}"'.format(
ctnr.name, self.start_str, self.end_str))
elif self.allow == ALLOW_STANDARD:
lines.append('allow members of "{}:{}"'.format(
self.start_str, self.end_str))
if not lines:
lines.append('deny unknown-clients')

return lines

def check_for_overlaps(self):
"""
Expand All @@ -319,31 +316,55 @@ def check_for_overlaps(self):
oldrange.get_ip_str(padded=False),
self.get_ip_str(padded=False)))

def build_range(self):
def build_classes(self):
if self.allow == ALLOW_STANDARD:
ifaces = self.dynamicinterface_set.filter(dhcp_enabled=True)
classname = self.start_str + ':' + self.end_str
build_str = (
'class "{}" {{\n'
'\tmatch hardware;\n'
'}}\n'.format(classname)
)
for i in ifaces:
build_str += i.build_subclass(classname)
return build_str
elif self.allow == ALLOW_LEGACY:
build_str = ''
for ctnr in self.ctnr_set.all():
ifaces = self.dynamicinterface_set.filter(
dhcp_enabled=True, ctnr=ctnr)
classname = (ctnr.name + ':' + self.start_str + ':' +
self.end_str)
build_str += (
'class "{}" {{\n'
'\tmatch hardware;\n'
'}}\n'.format(classname)
)
for i in ifaces:
build_str += i.build_subclass(classname)
return build_str

def build_pool(self):
range_options = self.rangeav_set.filter(
attribute__attribute_type=ATTRIBUTE_OPTION)
range_statements = self.rangeav_set.filter(
attribute__attribute_type=ATTRIBUTE_STATEMENT)
build_str = "\tpool {\n"
build_str += "\t\t# Pool Statements\n"
build_str += "\t\tfailover peer \"dhcp\";\n"
build_str += "\t\tdeny dynamic bootp clients;\n"
build_str = '\tpool {\n'
build_str += '\t\tfailover peer "dhcp";\n'
build_str += '\t\tdeny dynamic bootp clients;\n'
build_str += join_dhcp_args(range_statements, depth=2)
if range_options:
build_str += "\t\t# Pool Options\n"
build_str += join_dhcp_args(range_options, depth=2)
if self.dhcpd_raw_include:
build_str += "\t\t# Raw pool includes\n"
build_str += "\t\t{0};".format(self.dhcp_raw_include)
build_str += "\t\t# Allow statements\n"
build_str += join_dhcp_args(self.get_allow_deny_list(), depth=2)
build_str += '\t\t{0};'.format(self.dhcp_raw_include)
build_str += join_dhcp_args(self.get_allow_deny_lines(), depth=2)
if self.ip_type == IP_TYPE_4:
build_str += "\t\trange {0} {1};\n".format(self.start_str,
build_str += '\t\trange {0} {1};\n'.format(self.start_str,
self.end_str)
else:
build_str += "\t\trange6{0} {1};\n".format(self.start_str,
build_str += '\t\trange6 {0} {1};\n'.format(self.start_str,
self.end_str)
build_str += "\t}\n\n"
build_str += '\t}\n\n'
return build_str

def update_ipf(self):
Expand Down
18 changes: 10 additions & 8 deletions cyder/cydhcp/range/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from cyder.base.utils import make_paginator, tablefy, make_megafilter
from cyder.base.views import cy_render
from cyder.core.ctnr.models import Ctnr
from cyder.cydhcp.constants import (ALLOW_ANY, ALLOW_KNOWN, ALLOW_VRF,
ALLOW_LEGACY)
from cyder.cydhcp.constants import (
ALLOW_STANDARD, ALLOW_ALL, ALLOW_KNOWN, ALLOW_VRF, ALLOW_LEGACY)
from cyder.cydhcp.range.models import Range, RangeAV
from cyder.cydhcp.range.range_usage import range_usage
from cyder.cydhcp.utils import two_to_one
Expand All @@ -23,16 +23,18 @@
def range_detail(request, pk):
mrange = get_object_or_404(Range, pk=pk)

if mrange.allow == ALLOW_ANY:
allow = ['Any client']
if mrange.allow == ALLOW_ALL:
allow = ['All clients']
elif mrange.allow == ALLOW_KNOWN:
allow = ['Known clients']
elif mrange.allow == ALLOW_STANDARD:
allow = ['All clients in this range']
elif mrange.allow == ALLOW_VRF:
allow = map(str, Vrf.objects.filter(network=mrange.network))
elif mrange.allow == ALLOW_LEGACY:
allow = map(str, Ctnr.objects.filter(ranges=mrange))
else:
allow = []
if mrange.allow == ALLOW_VRF:
allow += map(str, Vrf.objects.filter(network=mrange.network))
if mrange.allow == ALLOW_LEGACY:
allow += map(str, Ctnr.objects.filter(ranges=mrange))

allow.sort(key=lambda x: x.lower())

Expand Down
4 changes: 1 addition & 3 deletions cyder/cydhcp/vrf/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def build_vrf(self, ip_type):
'\tmatch hardware;\n'
'}}\n'
.format(self.name))

for network_ in self.network_set.filter(ip_type=ip_type):
for range_ in network_.range_set.all():
clients = chain(
Expand All @@ -81,8 +80,7 @@ def build_vrf(self, ip_type):
)
for client in clients:
build_str += client.build_subclass(self.name)


build_str += '\n'
return build_str


Expand Down
3 changes: 0 additions & 3 deletions cyder/cydhcp/workgroup/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,10 @@ def is_host_option(option):
for x in host_options:
options.remove(x)

build_str += '\t# Workgroup Options\n'
if options:
build_str += join_dhcp_args(options)
build_str += '\t# Workgroup Statements\n'
if statements:
build_str += join_dhcp_args(statements)
build_str += '\t# Static Hosts in Workgroup\n'
for client in chain(dynamic_clients, static_clients):
build_str += client.build_host(host_options)
build_str += '}\n'
Expand Down