Compare commits

31 Commits
Author SHA1 Message Date
Nikolai R Kristiansen 9dc195fd46 python2 cleanups 2018-08-12 23:30:40 +02:00
Nikolai R Kristiansen d5d63b4dca Try bumping to django 2.0 2018-08-12 23:26:12 +02:00
Toralf Neby berge 47b69a9ea7 Remove unnecessary if statement 2018-03-10 15:52:40 +01:00
Toralf Neby berge 7de3e4008c Add check if macaddr is unique in the selected subnet 2018-03-10 15:37:27 +01:00
nikolark 54180d6bb5 refactor sync cmds 2017-01-05 00:29:16 +01:00
nikolark 49183fb646 Add sentry 2017-01-04 21:36:54 +01:00
nikolark cf6b1ad13d bump deps 2017-01-04 21:24:58 +01:00
nikolark 51b595a11d feat: better pxe error message 2016-12-07 22:26:54 +01:00
nikolark 093fae0fea admin: minor 2016-08-06 17:17:24 +02:00
nikolark b79fdafaa1 bind: fix reload of dynamic zones 2016-08-06 17:17:17 +02:00
nikolark 8fe8a1b311 Fix dhcpd restart 2016-07-26 13:12:59 +02:00
nikolark 935e38d224 Host: Make ipv6 enabled sortable++ 2016-07-25 18:39:10 +02:00
nikolark 2fd8001ba5 fix context_diff in python3 2016-07-25 18:06:44 +02:00
nikolark ca76aaa80b add gunicorn and bump deps 2016-07-25 17:47:55 +02:00
nikolark 456bcb7c50 More cleanups 2016-06-19 18:31:33 +02:00
nikolark 277fde9809 Misc pep8 cleanups 2016-06-19 18:29:07 +02:00
nikolark 257b00be1b feat(pxe): Add support for PXE config per host/os 2016-03-25 15:00:25 +01:00
nikolark 3659215a2f refactor(pxe): Move pxe functions to own file 2016-03-25 13:52:34 +01:00
nikolark 7b47e1d6d9 refactor(admin): Move admin specific code out of models 2016-03-25 13:41:58 +01:00
nikolark e17bf339e2 chore(app): Bump deps 2016-03-25 13:29:38 +01:00
nikolark 9d737dcd45 Merge branch 'hostadmin-perf' into 'master'
Hostadmin perf

very fast

See merge request !2
2016-01-07 23:40:40 +01:00
nikolark 898578852d select_related to the rescue, such performance! 2016-01-07 23:39:11 +01:00
nikolark 2a0744f602 Override form in InterfaceInline to get to expensive lookup 2016-01-06 22:39:32 +01:00
nikolark 15cde2cce4 Fixes broken zone-file when Domain.domain_ip6addr is empty 2015-11-03 11:02:19 +01:00
nikolark d523b4ae99 Merge branch 'ipv6-domain-addr' 2015-10-15 21:18:13 +02:00
nikolark fdb826d5ac Clear out pxe_key on validation 2015-10-15 21:17:28 +02:00
nikolark bcb4b2e713 Try fixing pxelinux.cfg 2015-10-14 21:33:35 +02:00
nikolark 4daf775adb Set less permissive filemode on pxe file, fix force param in zone-sync 2015-10-14 20:44:43 +02:00
nikolark 2df1f6ab36 PXE: Use one less dash for our slightly older kernel 2015-10-14 19:53:34 +02:00
Stian Wiken 5ece0c21f9 Better pxeconfig template 2015-10-07 20:00:09 +02:00
nikolark 8946234b10 Merge branch 'bump-bonanza-and-python3' 2015-10-07 10:42:55 +02:00
25 changed files with 654 additions and 335 deletions

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
django = "==2.0.8"
django-auth-ldap = "==1.7.0"
django-extensions = "==2.1.0"
djangorestframework = "==3.8.2"
gunicorn = "==19.9.0"
ipaddress = "==1.0.22"
mysqlclient = "==1.3.13"
raven = "==6.9.0"
django-suit = "*"
[dev-packages]
[requires]
python_version = "3.6"
Generated
+147
View File
@@ -0,0 +1,147 @@
{
"_meta": {
"hash": {
"sha256": "2280ffc42c9d702c0cfeba59fdb86215b1a8cad743ae3dc49fd6790aa00cf885"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.6"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"django": {
"hashes": [
"sha256:0c5b65847d00845ee404bbc0b4a85686f15eb3001ffddda3db4e9baa265bf136",
"sha256:68aeea369a8130259354b6ba1fa9babe0c5ee6bced505dea4afcd00f765ae38b"
],
"index": "pypi",
"version": "==2.0.8"
},
"django-auth-ldap": {
"hashes": [
"sha256:72848b3b036d299114be3c6ef38b12f83f6cf1cf1696c5f92e06fe45a1b8e27b",
"sha256:f96398ce957bb8aaaee2e9d93e3eaf9955ab95a6d342303b5519350eb834f5d1"
],
"index": "pypi",
"version": "==1.7.0"
},
"django-extensions": {
"hashes": [
"sha256:c3c8524056ab1174c6fc769b0599e60f7e7797ce34f38db2effc48885a0a982c",
"sha256:e279344c6d825adb27694755aa924702d3cc401f8fad5ad9ef2d577948778a5f"
],
"index": "pypi",
"version": "==2.1.0"
},
"django-suit": {
"hashes": [
"sha256:19ed865a478dfca81cb5f50a70317700dd70da92c465093251d0e14330a2b92b"
],
"index": "pypi",
"version": "==0.2.26"
},
"djangorestframework": {
"hashes": [
"sha256:b6714c3e4b0f8d524f193c91ecf5f5450092c2145439ac2769711f7eba89a9d9",
"sha256:c375e4f95a3a64fccac412e36fb42ba36881e52313ec021ef410b40f67cddca4"
],
"index": "pypi",
"version": "==3.8.2"
},
"gunicorn": {
"hashes": [
"sha256:aa8e0b40b4157b36a5df5e599f45c9c76d6af43845ba3b3b0efe2c70473c2471",
"sha256:fa2662097c66f920f53f70621c6c58ca4a3c4d3434205e608e121b5b3b71f4f3"
],
"index": "pypi",
"version": "==19.9.0"
},
"ipaddress": {
"hashes": [
"sha256:64b28eec5e78e7510698f6d4da08800a5c575caa4a286c93d651c5d3ff7b6794",
"sha256:b146c751ea45cad6188dd6cf2d9b757f6f4f8d6ffb96a023e6f2e26eea02a72c"
],
"index": "pypi",
"version": "==1.0.22"
},
"mysqlclient": {
"hashes": [
"sha256:ff8ee1be84215e6c30a746b728c41eb0701a46ca76e343af445b35ce6250644f"
],
"index": "pypi",
"version": "==1.3.13"
},
"pyasn1": {
"hashes": [
"sha256:0ad0fe0593dde1e599cac0bf65bb1a4ec663032f0bc68ee44850db4251e8c501",
"sha256:13794d835643ee970b2c059dbfe4eb5d751e16c693c8baee61c526abd209e5c7",
"sha256:49a8ed515f26913049113820b462f698e6ed26df62c389dafb6fa3685ddca8de",
"sha256:74ac8521a0480f228549be20bea555ae35678f0e754c2fbc6f1576b0959bec43",
"sha256:89399ca8ecd4524f974e926d4ef9e7a787903e01f0a9cdff3131ad1361792fe5",
"sha256:8f291e0338d519a1a0d07f0b9d03c9265f6be26eb32fdd21af6d3259d14ea49c",
"sha256:b9d3abc5031e61927c82d4d96c1cec1e55676c1a991623cfed28faea73cdd7ca",
"sha256:d3bbd726c1a760d4ca596a4d450c380b81737612fe0182f5bb3caebc17461fd9",
"sha256:dea873d6c907c1cf1341fd88742a61efce33227d7743cb37564ab7d7e77dd9fd",
"sha256:ded5eea5cb88bc1ce9aa074b5a3092f95ce4741887e317e9b49c7ece75d7ea0e",
"sha256:e8b69ea2200d42201cbedd486eedb8980f320d4534f83ce2fb468e96aa5545d0",
"sha256:edad117649643230493aeb4955456ce19ab4b12e94489dde6f7094cdb5a3c87e",
"sha256:f58f2a3d12fd754aa123e9fa74fb7345333000a035f3921dbdaa08597aa53137"
],
"version": "==0.4.4"
},
"pyasn1-modules": {
"hashes": [
"sha256:077250b34432520430bc1c80dcbda4e354090785567c33ded35faa6df8d24753",
"sha256:0da2f947e8ad2697e86fe5fd0e55a4093a2fd79d839c9e19c34e28097db7002c",
"sha256:35ff894a0b5df8e28b700126b2869c7dcfb2b2db5bc82e5d5e82547069241553",
"sha256:44688b94841349648b1e1a5a7a3d96e6596d5d4f21d0b59a82307e153c4dc74b",
"sha256:833716dde880a7f2f2ccdeea9a096842626981ff2a477d8b318c0906367ac11b",
"sha256:a0cf3e1842e7c60fde97cb22d275eb6f9524f5c5250489e292529de841417547",
"sha256:a38a8811ea784c0136abfdba73963876328f66172db21a05a82f9515909bfb4e",
"sha256:a728bb9502d1fdc104c66f24a176b6a70a32e89d1d8a5b55c959233ed51c67be",
"sha256:c30a098435ea0989c37005a971843e9d3966c7f6d056ddbf052e5061c06e3291",
"sha256:c355a45b32c5bc1d9893eceb704b0cfcd1126f91b5a7b9ee64c1c05383283381",
"sha256:e64679de1940f41ead5170fce364d54e7b9e2e862f064727b6bcb5cee753b7a2",
"sha256:ed71d20225c356881c29f0b1d7a0d6521563a389d9478e8f95d798cc5ba07b88",
"sha256:f183f0940b9f5ed2ad9d04c80cab2451440fa9af4fc959d85113fadd2e777962"
],
"version": "==0.2.2"
},
"python-ldap": {
"hashes": [
"sha256:41975e79406502c092732c57ef0c2c2eb318d91e8e765f81f5d4ab6c1db727c5"
],
"version": "==3.1.0"
},
"pytz": {
"hashes": [
"sha256:a061aa0a9e06881eb8b3b2b43f05b9439d6583c206d0a6c340ff72a7b6669053",
"sha256:ffb9ef1de172603304d9d2819af6f5ece76f2e85ec10692a524dd876e72bf277"
],
"version": "==2018.5"
},
"raven": {
"hashes": [
"sha256:3fd787d19ebb49919268f06f19310e8112d619ef364f7989246fc8753d469888",
"sha256:95f44f3ea2c1b176d5450df4becdb96c15bf2632888f9ab193e9dd22300ce46a"
],
"index": "pypi",
"version": "==6.9.0"
},
"six": {
"hashes": [
"sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9",
"sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb"
],
"version": "==1.11.0"
}
},
"develop": {}
}
+77 -14
View File
@@ -1,8 +1,12 @@
from django.contrib import admin, messages
import ipaddress
from mdb.models import Ip6Address, Interface, Ip4Address, DhcpOption, DhcpCustomField, DomainSrvRecord, DomainTxtRecord, \
DomainCnameRecord, DomainARecord, Domain, Host, Ip4Subnet, Ip6Subnet, Nameserver, MailExchange, OperatingSystem, \
HostType, DhcpConfig, DomainAAAARecord
from django.contrib import admin, messages
from django.db.models import Count
from mdb.forms import InterfaceForm
from mdb.models import (
Ip6Address, Interface, Ip4Address, DhcpOption, DhcpCustomField, DomainSrvRecord, DomainTxtRecord,
DomainCnameRecord, DomainARecord, Domain, Host, Ip4Subnet, Ip6Subnet, Nameserver, MailExchange, OperatingSystem,
HostType, DhcpConfig, DomainAAAARecord)
class Ip6AddressInline(admin.TabularInline):
@@ -14,6 +18,7 @@ class InterfaceInline(admin.TabularInline):
inlines = [Ip6AddressInline]
model = Interface
extra = 0
form = InterfaceForm
def get_queryset(self, request):
return super(InterfaceInline, self).get_queryset(request).select_related()
@@ -33,19 +38,39 @@ class HostAdmin(admin.ModelAdmin):
'fields': ('owner', 'location', 'description')
}),
('Hardware and Software Information', {
'fields': (('hostname', 'host_type'), ('pxe_key', 'pxe_installable'), ('brand', 'model'), 'serial_number', ('operating_system', 'virtual'))
'fields': (
('hostname', 'host_type'),
('pxe_key', 'pxe_installable'),
('brand', 'model'),
'serial_number',
('operating_system', 'virtual'))
}),
# FIXME: Not in use
# ('Domain and Kerberos Information', {
# 'description': 'If this host is a member of the LDAP domain, you need to tick the request kerberos principal checkbox. A principal will then be created for the host.',
# 'classes': ['collapse'],
# 'fields': ('request_kerberos_principal', 'kerberos_principal_created',
# ('kerberos_principal_name', 'kerberos_principal_created_date'))
# }),
)
actions = ['set_installable']
def _get_host_warning_message(self, host, ifs):
def mac_addresses(self, host):
addresses = host.interface_set.filter(macaddr__isnull=False).values_list('macaddr', flat=True)
return ", ".join(addresses)
def ip_addresses(self, host):
addresses = host.interface_set.filter(ip4address__isnull=False).values_list('ip4address__address', flat=True)
return ", ".join(addresses)
def in_domain(self, host):
domains = host.interface_set.values_list('domain__domain_name', flat=True)
return ",".join(domains)
in_domain.short_description = "in domains"
def ipv6_enabled(self, host):
ipv6_ifs = host.interface_set.annotate(num_ipv6=Count('ip6address')).filter(num_ipv6__gt=0)
return ipv6_ifs.exists()
ipv6_enabled.boolean = True
# FIXME: custom filter to sort custom field
@staticmethod
def _get_host_warning_message(host, ifs):
ifs_str = ', '.join([_if.name for _if in ifs])
return 'Host {} might not be installable since interface(s) {} has no IP set.'.format(host, ifs_str)
@@ -73,6 +98,11 @@ class Ip4AddressInline(admin.TabularInline):
extra = 0
readonly_fields = ['address', 'assigned_to_host']
def assigned_to_host(self, addr):
return addr.interface.host
assigned_to_host.short_description = "Assigned to Host"
class DhcpOptionInline(admin.TabularInline):
model = DhcpOption
@@ -88,6 +118,27 @@ class SubnetAdmin(admin.ModelAdmin):
list_display = ['name', 'network', 'netmask', 'num_addresses', 'broadcast_address', 'first_address', 'last_address']
inlines = [DhcpOptionInline, DhcpCustomFieldInline, Ip4AddressInline]
def num_addresses(self, ip4subnet):
subnet = ipaddress.IPv4Network(ip4subnet.network + "/" + ip4subnet.netmask)
return subnet.num_addresses
def broadcast_address(self, ip4subnet):
subnet = ipaddress.IPv4Network(ip4subnet.network + "/" + ip4subnet.netmask)
return subnet.broadcast_address
def first_address(self, ip4subnet):
subnet = ipaddress.IPv4Network(ip4subnet.network + "/" + ip4subnet.netmask)
return next(subnet.hosts())
def last_address(self, ip4subnet):
subnet = ipaddress.IPv4Network(ip4subnet.network + "/" + ip4subnet.netmask)
return list(subnet.hosts())[-1]
broadcast_address.short_description = 'broadcast'
num_addresses.short_description = '#addresses'
first_address.short_description = 'first address'
last_address.short_description = 'last address'
def get_queryset(self, request):
# TODO: optimize
return super(SubnetAdmin, self).get_queryset(request)
@@ -124,13 +175,25 @@ class DomainAdmin(admin.ModelAdmin):
list_display = ['domain_name', 'domain_soa', 'domain_admin', 'num_records', 'domain_ipaddr', 'domain_ip6addr']
search_fields = ['domain_name']
def num_records(self, domain):
host_a_records = Host.objects.filter(interface__domain=domain).count()
size = {
"CNAME": domain.domaincnamerecord_set.count(),
"SRV": domain.domainsrvrecord_set.count(),
"TXT": domain.domaintxtrecord_set.count(),
"A": domain.domainarecord_set.count() + host_a_records
}
return ', '.join(['{}: {}'.format(k, v) for k, v in size.items()])
num_records.short_description = "Num Records"
class HostTypeAdmin(admin.ModelAdmin):
list_display = ['host_type', 'description', 'num_members']
class OperatingSystemAdmin(admin.ModelAdmin):
list_display = ['name', 'version', 'arch']
list_display = ['name', 'version', 'arch', 'pxe_kernel', 'pxe_initrd', 'pxe_preseed_config_url']
admin.site.register(Domain, DomainAdmin)
+6 -2
View File
@@ -1,7 +1,8 @@
from mdb.models import Host
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from mdb.models import Host
class HostPXEValidateSerializer(serializers.Serializer):
certname = serializers.CharField(required=True)
@@ -32,7 +33,9 @@ class HostPXEValidateSerializer(serializers.Serializer):
raise ValidationError('Host \'{}\' has no pxe_key.'.format(attrs['certname']))
if host.pxe_key != attrs['pxe_key']:
raise ValidationError('Supplied pxe_key does not match host \'{}\'.'.format(attrs['certname']))
raise ValidationError('Supplied pxe_key \'{}\' does not match host \'{}\'.'.format(
attrs['pxe_key'],
attrs['certname']))
self.host = host
@@ -41,6 +44,7 @@ class HostPXEValidateSerializer(serializers.Serializer):
def create(self, validated_data):
# Make requests kind of idempotent by only allowing 1 request / installation
self.host.pxe_installable = False
self.host.pxe_key = ''
self.host.save()
return self.host
+1 -1
View File
@@ -15,6 +15,6 @@ class HostPXEValidate(APIView):
serializer.is_valid(raise_exception=True)
host = serializer.save() # sets Host.pxe_installable=False
serializer.save() # sets Host.pxe_installable=False
return Response({'valid_pxe_key': True})
+1 -1
View File
@@ -7,4 +7,4 @@ class MdbAppConfig(AppConfig):
def ready(self):
# all models are loaded, now attach signals
import mdb.signals
import mdb.signals # noqa
+12
View File
@@ -2738,6 +2738,18 @@
"model": "mdb.operatingsystem",
"pk": 1
},
{
"fields": {
"pxe_initrd": "path-to-initrd-for-os",
"pxe_kernel": "path-to-kernel-for-os",
"pxe_preseed_config_url": "http://url-to-preseed.config",
"arch": "x86_64",
"name": "Ubuntu",
"version": "16.04-beta-final"
},
"model": "mdb.operatingsystem",
"pk": 2
},
{
"fields": {
"kerberos_principal_name": "",
+34
View File
@@ -0,0 +1,34 @@
from django import forms
from django.core.exceptions import ValidationError
from django.forms import ModelChoiceField
from mdb.models import Interface, Ip4Address
class Ip4ModelChoiceField(ModelChoiceField):
def label_from_instance(self, obj):
if not hasattr(obj, 'interface'):
return obj.address
return "{} ({})".format(obj.address, obj.interface.host.hostname)
class InterfaceForm(forms.ModelForm):
model = Interface
def clean(self):
macaddr = self.cleaned_data['macaddr']
ip4address = self.cleaned_data['ip4address']
for interface in Interface.objects.filter(macaddr=macaddr):
subnet = interface.ip4address.subnet
if subnet == ip4address.subnet and interface != self.instance:
raise ValidationError('Macaddr already in this subnet')
def __init__(self, *args, **kwargs):
super(InterfaceForm, self).__init__(*args, **kwargs)
# Join Ip4Address with tables interface and host,
# before printing the select labels that walk those two relations.
queryset = Ip4Address.objects.select_related('interface', 'interface__host')
self.fields['ip4address'] = Ip4ModelChoiceField(queryset=queryset)
+27 -32
View File
@@ -1,15 +1,9 @@
from __future__ import with_statement
import os
import sys
from difflib import context_diff
from django.core.management.base import BaseCommand
from django.core.mail import mail_admins
from optparse import make_option
try:
from subprocess import getstatusoutput
except ImportError:
from commands import getstatusoutput
from subprocess import getstatusoutput
from mdb.models import DhcpConfig
@@ -18,29 +12,28 @@ class Command(BaseCommand):
args = '--output <path> [--force] [--restart]'
help = 'Generates the DHCP configuration'
option_list = BaseCommand.option_list + (
make_option('--output',
action='store',
dest='output',
default=None,
help='Path to dhcpd.conf, defaults to stdout'),
make_option('--force',
action='store_true',
dest='force',
default=False,
help='Write configuration even if serial is unchanged.'),
make_option('--restart',
action='store_true',
dest='restart',
default=False,
help='Restart dhcp3-server if serial is changed'),
)
def add_arguments(self, parser):
parser.add_argument('--output',
action='store',
dest='output',
default=None,
help='Path to dhcpd.conf, defaults to stdout'),
parser.add_argument('--force',
action='store_true',
dest='force',
default=False,
help='Write configuration even if serial is unchanged.'),
parser.add_argument('--restart',
action='store_true',
dest='restart',
default=False,
help='Restart dhcp3-server if serial is changed'),
def handle(self, *args, **options):
output = options['output'] if options['output'] else sys.stdout
force = options['force']
restart = options['restart']
restart_command = "service isc-dhcp-server restart"
restart_command = "/usr/sbin/service isc-dhcp-server restart"
config = DhcpConfig.objects.filter(name="default").get()
@@ -67,13 +60,7 @@ class Command(BaseCommand):
with open(output, 'w') as f:
f.write(new)
# calculate diff
diff = "\n".join(context_diff(
a=old.splitlines(),
b=new.splitlines(),
fromfile=config.active_serial,
tofile=config.serial,
lineterm=''))
diff = self.calculate_diff(config, new, old)
print(diff)
@@ -104,3 +91,11 @@ class Command(BaseCommand):
message=message,
html_message='<pre>%s</pre>' % message)
sys.exit(0)
def calculate_diff(self, config, new, old):
return "\n".join(context_diff(
a=old.splitlines(),
b=new.splitlines(),
fromfile=str(config.active_serial),
tofile=str(config.serial),
lineterm=''))
+79 -74
View File
@@ -1,16 +1,10 @@
from __future__ import with_statement
import os
import sys
from itertools import chain
from difflib import context_diff
from django.core.management.base import BaseCommand
from django.core.mail import mail_admins
from optparse import make_option
try:
from subprocess import getstatusoutput
except ImportError:
from commands import getstatusoutput
from subprocess import getstatusoutput
from mdb.models import Domain, Ip4Subnet, Ip6Subnet
@@ -19,50 +13,43 @@ class Command(BaseCommand):
args = '[--force] [--debug]'
help = 'Generates the bind configuration'
option_list = BaseCommand.option_list + (
make_option('--debug',
action='store_true',
dest='debug',
default=False,
help='Debug mode. Writes to /tmp and does not reload.'),
make_option('--force',
action='store_true',
dest='force',
default=False,
help='Write zone files even if serial is unchanged.'),
)
bind_bin = "/etc/init.d/bind9"
reload_command = "%s reload" % bind_bin
rndc_bin = "/usr/sbin/rndc"
reload_command = "%s reload" % rndc_bin
freeze_command = "%s freeze" % rndc_bin
thaw_command = "%s thaw" % rndc_bin
commands_in_order = [freeze_command, reload_command, thaw_command]
checkzone_bin = "/usr/sbin/named-checkzone"
checkzone_dir = "/tmp/mdb-checkzone"
debug = False
force = False
debug_dir = '/tmp/mdb-debug'
changes = {}
def add_arguments(self, parser):
parser.add_argument('--debug',
action='store_true',
dest='debug',
default=False,
help='Debug mode. Writes to /tmp and does not reload.')
parser.add_argument('--force',
action='store_true',
dest='force',
default=False,
help='Write zone files even if serial is unchanged.')
def handle(self, *args, **options):
self.debug = debug = options['debug']
force = options['force']
self.debug = options['debug']
self.force = options['force']
# do the binaries exist?
for f in (self.checkzone_bin, self.bind_bin):
if not os.path.isfile(f):
print("ERROR: no such file %s, exiting..." % f)
if not debug:
sys.exit(1)
self.validate_binaries_exist()
# create directories if needed
os.path.isdir(self.checkzone_dir) or os.mkdir(self.checkzone_dir)
debug and (os.path.isdir(self.debug_dir) or os.mkdir(self.debug_dir))
self.debug and (os.path.isdir(self.debug_dir) or os.mkdir(self.debug_dir))
# update zones with new serials
for zone in chain(Domain.objects.all(),
Ip4Subnet.objects.all(),
Ip6Subnet.objects.all()):
if zone.domain_serial == zone.domain_active_serial or force:
continue
if self.update_zone(zone):
self.changes[zone.domain_name]['success'] = True
self.update_zone_serials()
# join errors, diffs and zone names
errors = "\n\n\n".join(
@@ -72,35 +59,52 @@ class Command(BaseCommand):
zones = ", ".join(self.changes.keys())
# reload and send email with changes
if self.changes and not debug:
sys.stdout.write("reloading bind... ")
status, output = getstatusoutput(self.reload_command)
if status != 0:
sys.stdout.write("fail\n")
message = (
"Failed to reload bind, please inspect...\n\n" +
"%s@%s# %s" % (
os.getlogin(), os.uname()[1], self.reload_command) +
"\n%s\n\n\n%s\n\n\n%s" % (output, errors, diffs))
mail_admins(
subject="FAIL: bind (%s changed: %s)" % (len(self.changes), zones),
message=message,
html_message='<pre>%s</pre>' % message)
sys.exit(1)
else:
sys.stdout.write("ok\n")
message = (
"Successfully updated bind zone files\n\n" +
"%s\n\n%s" % (errors, diffs))
mail_admins(
subject="Success: bind (%s changed: %s)" % (len(self.changes), zones),
message=message,
html_message='<pre>%s</pre>' % message)
sys.exit(0)
if self.changes and not self.debug:
sys.stdout.write("reloading zones... ")
for command in self.commands_in_order:
status, output = getstatusoutput(command)
if status != 0:
sys.stdout.write("fail\n")
message = (
"Failed to reload zones, please inspect...\n\n" +
"%s@%s$ %s" % (
os.getlogin(), os.uname()[1], command) +
"\n%s\n\n\n%s\n\n\n%s" % (output, errors, diffs))
mail_admins(
subject="FAIL: bind (%s changed: %s)" % (len(self.changes), zones),
message=message,
html_message='<pre>%s</pre>' % message)
sys.exit(1)
sys.stdout.write("ok\n")
message = (
"Successfully updated bind zone files\n\n" +
"%s\n\n%s" % (errors, diffs))
mail_admins(
subject="Success: bind (%s changed: %s)" % (len(self.changes), zones),
message=message,
html_message='<pre>%s</pre>' % message)
sys.exit(0)
def update_zone_serials(self):
for zone in chain(Domain.objects.all(),
Ip4Subnet.objects.all(),
Ip6Subnet.objects.all()):
if zone.domain_serial == zone.domain_active_serial and not self.force:
continue
if self.update_zone(zone):
self.changes[zone.domain_name]['success'] = True
def validate_binaries_exist(self):
for f in (self.checkzone_bin, self.rndc_bin):
if not os.path.isfile(f) and not self.debug:
print("ERROR: no such file %s, exiting..." % f)
if not self.debug:
sys.exit(1)
def update_zone(self, zone):
zonetype = getattr(zone._meta, 'verbose_name', None) \
or zone._meta.model_name
zonetype = getattr(zone._meta, 'verbose_name', None) or zone._meta.model_name
self.changes[zone.domain_name] = {
'old_serial': zone.domain_active_serial,
@@ -126,14 +130,7 @@ class Command(BaseCommand):
except IOError:
old = ''
# calculate diff
diff = "\n".join(context_diff(
a=old.splitlines(),
b=new.splitlines(),
fromfile=str(zone.domain_active_serial) + '-' + zone.domain_name,
tofile=str(zone.domain_serial) + '-' + zone.domain_name,
lineterm=''))
self.changes[zone.domain_name]['diff'] = diff
self.changes[zone.domain_name]['diff'] = self.calculate_diff(new, old, zone)
# validate with zonecheck
sys.stdout.write("\t- validating... ")
@@ -142,6 +139,7 @@ class Command(BaseCommand):
sys.stdout.write("fail\n")
self.changes[zone.domain_name]['errors'] = output
return False
sys.stdout.write("ok\n")
# write to zone file
@@ -166,10 +164,17 @@ class Command(BaseCommand):
self.changes[zone.domain_name]['success'] = True
return True
def calculate_diff(self, new, old, zone):
return "\n".join(context_diff(
a=old.splitlines(),
b=new.splitlines(),
fromfile=str(zone.domain_active_serial) + '-' + zone.domain_name,
tofile=str(zone.domain_serial) + '-' + zone.domain_name,
lineterm=''))
def check_zone(self, zone, contents):
zonefile = self.checkzone_dir + '/' + zone.domain_name
with open(zonefile, 'w') as f:
f.write(contents)
return getstatusoutput("%s %s %s" % (
self.checkzone_bin, zone.domain_name, zonefile))
return getstatusoutput("%s %s %s" % (self.checkzone_bin, zone.domain_name, zonefile))
+16 -16
View File
@@ -81,7 +81,7 @@ class Migration(migrations.Migration):
('name', models.CharField(max_length=256)),
('target', models.CharField(max_length=256)),
('created_date', models.DateTimeField(auto_now_add=True)),
('domain', models.ForeignKey(to='mdb.Domain')),
('domain', models.ForeignKey(to='mdb.Domain', on_delete=models.CASCADE)),
],
options={
},
@@ -94,7 +94,7 @@ class Migration(migrations.Migration):
('name', models.CharField(max_length=256)),
('target', models.CharField(max_length=256)),
('created_date', models.DateTimeField(auto_now_add=True)),
('domain', models.ForeignKey(to='mdb.Domain')),
('domain', models.ForeignKey(to='mdb.Domain', on_delete=models.CASCADE)),
],
options={
},
@@ -112,7 +112,7 @@ class Migration(migrations.Migration):
('port', models.IntegerField()),
('target', models.CharField(max_length=256)),
('created_date', models.DateTimeField(auto_now_add=True)),
('domain', models.ForeignKey(to='mdb.Domain')),
('domain', models.ForeignKey(to='mdb.Domain', on_delete=models.CASCADE)),
],
options={
},
@@ -125,7 +125,7 @@ class Migration(migrations.Migration):
('name', models.CharField(max_length=256)),
('target', models.CharField(max_length=256)),
('created_date', models.DateTimeField(auto_now_add=True)),
('domain', models.ForeignKey(to='mdb.Domain')),
('domain', models.ForeignKey(to='mdb.Domain', on_delete=models.CASCADE)),
],
options={
},
@@ -174,8 +174,8 @@ class Migration(migrations.Migration):
('pxe_filename', models.CharField(max_length=64, blank=True)),
('dhcp_client', models.BooleanField(default=False)),
('created_date', models.DateTimeField(auto_now_add=True)),
('domain', models.ForeignKey(to='mdb.Domain')),
('host', models.ForeignKey(to='mdb.Host')),
('domain', models.ForeignKey(to='mdb.Domain', on_delete=models.CASCADE)),
('host', models.ForeignKey(to='mdb.Host', on_delete=models.CASCADE)),
],
options={
},
@@ -217,7 +217,7 @@ class Migration(migrations.Migration):
('dhcp_dynamic', models.BooleanField(default=False)),
('dhcp_dynamic_start', models.IPAddressField(null=True, blank=True)),
('dhcp_dynamic_end', models.IPAddressField(null=True, blank=True)),
('dhcp_config', models.ForeignKey(to='mdb.DhcpConfig')),
('dhcp_config', models.ForeignKey(to='mdb.DhcpConfig', on_delete=models.CASCADE)),
],
options={
'verbose_name': 'IPv4 subnet',
@@ -229,7 +229,7 @@ class Migration(migrations.Migration):
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('address', models.CharField(max_length=64)),
('interface', models.ForeignKey(to='mdb.Interface')),
('interface', models.ForeignKey(to='mdb.Interface', on_delete=models.CASCADE)),
],
options={
'verbose_name': 'IPv6 address',
@@ -309,7 +309,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='operatingsystem',
name='architecture',
field=models.ForeignKey(to='mdb.OsArchitecture'),
field=models.ForeignKey(to='mdb.OsArchitecture', on_delete=models.CASCADE),
preserve_default=True,
),
migrations.AddField(
@@ -321,7 +321,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='ip6address',
name='subnet',
field=models.ForeignKey(to='mdb.Ip6Subnet'),
field=models.ForeignKey(to='mdb.Ip6Subnet', on_delete=models.CASCADE),
preserve_default=True,
),
migrations.AddField(
@@ -333,25 +333,25 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='ip4address',
name='subnet',
field=models.ForeignKey(to='mdb.Ip4Subnet'),
field=models.ForeignKey(to='mdb.Ip4Subnet', on_delete=models.CASCADE),
preserve_default=True,
),
migrations.AddField(
model_name='interface',
name='ip4address',
field=models.ForeignKey(null=True, blank=True, to='mdb.Ip4Address', unique=True),
field=models.ForeignKey(null=True, blank=True, to='mdb.Ip4Address', unique=True, on_delete=models.SET_NULL),
preserve_default=True,
),
migrations.AddField(
model_name='host',
name='host_type',
field=models.ForeignKey(to='mdb.HostType'),
field=models.ForeignKey(to='mdb.HostType', on_delete=models.CASCADE),
preserve_default=True,
),
migrations.AddField(
model_name='host',
name='operating_system',
field=models.ForeignKey(to='mdb.OperatingSystem'),
field=models.ForeignKey(to='mdb.OperatingSystem', on_delete=models.CASCADE),
preserve_default=True,
),
migrations.AddField(
@@ -369,13 +369,13 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='dhcpoption',
name='ip4subnet',
field=models.ForeignKey(to='mdb.Ip4Subnet'),
field=models.ForeignKey(to='mdb.Ip4Subnet', on_delete=models.CASCADE),
preserve_default=True,
),
migrations.AddField(
model_name='dhcpcustomfield',
name='ip4subnet',
field=models.ForeignKey(to='mdb.Ip4Subnet'),
field=models.ForeignKey(to='mdb.Ip4Subnet', on_delete=models.CASCADE),
preserve_default=True,
),
]
+1 -1
View File
@@ -35,7 +35,7 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='interface',
name='ip4address',
field=models.OneToOneField(to='mdb.Ip4Address', null=True, blank=True),
field=models.OneToOneField(to='mdb.Ip4Address', null=True, blank=True, on_delete=models.SET_NULL),
),
migrations.AlterField(
model_name='interface',
+1 -1
View File
@@ -18,7 +18,7 @@ class Migration(migrations.Migration):
('name', models.CharField(max_length=256)),
('target', models.GenericIPAddressField(protocol='IPv6')),
('created_date', models.DateTimeField(auto_now_add=True)),
('domain', models.ForeignKey(to='mdb.Domain')),
('domain', models.ForeignKey(to='mdb.Domain', on_delete=models.CASCADE)),
],
),
migrations.AlterField(
+29
View File
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('mdb', '0010_auto_20151004_1709'),
]
operations = [
migrations.AddField(
model_name='operatingsystem',
name='pxe_initrd',
field=models.CharField(null=True, blank=True, max_length=255),
),
migrations.AddField(
model_name='operatingsystem',
name='pxe_kernel',
field=models.CharField(null=True, blank=True, max_length=255),
),
migrations.AddField(
model_name='operatingsystem',
name='pxe_preseed_config_url',
field=models.URLField(null=True, blank=True),
),
]
+24 -102
View File
@@ -1,8 +1,6 @@
from django.db import models
from django.db.models import Count
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from mdb.utils import host_as_pxe_files
from mdb.pxe import host_as_pxe_files
from mdb.validators import validate_hostname, validate_macaddr
@@ -10,7 +8,6 @@ import ipaddress
import datetime
@python_2_unicode_compatible
class Nameserver(models.Model):
hostname = models.CharField(max_length=256)
@@ -18,7 +15,6 @@ class Nameserver(models.Model):
return self.hostname
@python_2_unicode_compatible
class MailExchange(models.Model):
priority = models.IntegerField()
hostname = models.CharField(max_length=256)
@@ -27,7 +23,6 @@ class MailExchange(models.Model):
return "(" + str(self.priority) + ") " + self.hostname
@python_2_unicode_compatible
class Domain(models.Model):
domain_name = models.CharField(max_length=256)
domain_soa = models.CharField(max_length=256)
@@ -50,18 +45,6 @@ class Domain(models.Model):
def __str__(self):
return self.domain_name
def num_records(self):
host_a_records = Host.objects.filter(interface__domain=self).count()
size = {
"CNAME": self.domaincnamerecord_set.count(),
"SRV": self.domainsrvrecord_set.count(),
"TXT": self.domaintxtrecord_set.count(),
"A": self.domainarecord_set.count() + host_a_records
}
return ', '.join(['{}: {}'.format(k, v) for k, v in size.items()])
num_records.short_description = "Num Records"
def __eq__(self, other):
if not other or not hasattr(other, 'domain_name'):
return False
@@ -93,7 +76,7 @@ class Domain(models.Model):
if self.domain_ipaddr is not None:
content += "@\tIN\tA\t%s\n" % self.domain_ipaddr
if self.domain_ip6addr is not None:
if self.domain_ip6addr:
content += "@\tIN\tAAAA\t%s\n" % self.domain_ip6addr
content += "; SRV records\n"
@@ -137,7 +120,6 @@ class Domain(models.Model):
return content
@python_2_unicode_compatible
class DomainSrvRecord(models.Model):
srvce = models.CharField(max_length=64)
prot = models.CharField(max_length=64)
@@ -146,7 +128,7 @@ class DomainSrvRecord(models.Model):
weight = models.IntegerField()
port = models.IntegerField()
target = models.CharField(max_length=256)
domain = models.ForeignKey(Domain)
domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
created_date = models.DateTimeField(auto_now_add=True)
def as_record(self):
@@ -165,11 +147,10 @@ class DomainSrvRecord(models.Model):
return self.as_record()
@python_2_unicode_compatible
class DomainTxtRecord(models.Model):
name = models.CharField(max_length=256)
target = models.CharField(max_length=256)
domain = models.ForeignKey(Domain)
domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
created_date = models.DateTimeField(auto_now_add=True)
def as_record(self):
@@ -179,11 +160,10 @@ class DomainTxtRecord(models.Model):
return self.as_record()
@python_2_unicode_compatible
class DomainCnameRecord(models.Model):
name = models.CharField(max_length=256)
target = models.CharField(max_length=256)
domain = models.ForeignKey(Domain)
domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
created_date = models.DateTimeField(auto_now_add=True)
def as_record(self):
@@ -193,11 +173,10 @@ class DomainCnameRecord(models.Model):
return self.as_record()
@python_2_unicode_compatible
class DomainARecord(models.Model):
name = models.CharField(max_length=256)
target = models.GenericIPAddressField(protocol='IPv4')
domain = models.ForeignKey(Domain)
domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
created_date = models.DateTimeField(auto_now_add=True)
def as_record(self):
@@ -207,11 +186,10 @@ class DomainARecord(models.Model):
return self.as_record()
@python_2_unicode_compatible
class DomainAAAARecord(models.Model):
name = models.CharField(max_length=256)
target = models.GenericIPAddressField(protocol='IPv6')
domain = models.ForeignKey(Domain)
domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
created_date = models.DateTimeField(auto_now_add=True)
def as_record(self):
@@ -221,7 +199,6 @@ class DomainAAAARecord(models.Model):
return self.as_record()
@python_2_unicode_compatible
class DhcpConfig(models.Model):
serial = models.IntegerField()
active_serial = models.IntegerField()
@@ -283,7 +260,6 @@ class DhcpConfig(models.Model):
verbose_name = 'DHCP config'
@python_2_unicode_compatible
class Ip6Subnet(models.Model):
name = models.CharField(max_length=255)
network = models.CharField(max_length=255)
@@ -332,7 +308,7 @@ class Ip6Subnet(models.Model):
# content += "$ORIGIN " + ".".join(network.exploded.replace(":","")[:16])[::-1] + ".ip6.arpa.\n"
for addr in self.ip6address_set.all():
#if addr.interface_set.count() == 0: continue
# if addr.interface_set.count() == 0: continue
if addr.interface.domain is None:
continue
@@ -353,7 +329,6 @@ class Ip6Subnet(models.Model):
verbose_name = 'IPv6 subnet'
@python_2_unicode_compatible
class Ip4Subnet(models.Model):
name = models.CharField(max_length=256)
netmask = models.GenericIPAddressField(protocol='IPv4')
@@ -375,32 +350,11 @@ class Ip4Subnet(models.Model):
dhcp_dynamic = models.BooleanField(default=False)
dhcp_dynamic_start = models.GenericIPAddressField(protocol='IPv4', null=True, blank=True)
dhcp_dynamic_end = models.GenericIPAddressField(protocol='IPv4', null=True, blank=True)
dhcp_config = models.ForeignKey(DhcpConfig)
dhcp_config = models.ForeignKey(DhcpConfig, on_delete=models.CASCADE)
def __str__(self):
return self.network + " (" + self.name + ")"
def num_addresses(self):
subnet = ipaddress.IPv4Network(self.network + "/" + self.netmask)
return subnet.num_addresses
def broadcast_address(self):
subnet = ipaddress.IPv4Network(self.network + "/" + self.netmask)
return subnet.broadcast_address
def first_address(self):
subnet = ipaddress.IPv4Network(self.network + "/" + self.netmask)
return next(subnet.hosts())
def last_address(self):
subnet = ipaddress.IPv4Network(self.network + "/" + self.netmask)
return list(subnet.hosts())[-1]
broadcast_address.short_description = 'broadcast'
num_addresses.short_description = '#addresses'
first_address.short_description = 'first address'
last_address.short_description = 'last address'
def zone_file_contents(self, generate_unassigned=False):
content = ""
content += "; zone file for %s\n" % self.domain_name
@@ -443,11 +397,10 @@ class Ip4Subnet(models.Model):
verbose_name = 'IPv4 subnet'
@python_2_unicode_compatible
class DhcpOption(models.Model):
key = models.CharField(max_length=255)
value = models.CharField(max_length=255)
ip4subnet = models.ForeignKey(Ip4Subnet)
ip4subnet = models.ForeignKey(Ip4Subnet, on_delete=models.CASCADE)
def __str__(self):
return self.key + " " + self.value
@@ -458,37 +411,26 @@ class DhcpOption(models.Model):
class DhcpCustomField(models.Model):
value = models.CharField(max_length=255)
ip4subnet = models.ForeignKey(Ip4Subnet)
ip4subnet = models.ForeignKey(Ip4Subnet, on_delete=models.CASCADE)
class Meta:
verbose_name = 'DHCP custom field'
@python_2_unicode_compatible
class Ip4Address(models.Model):
subnet = models.ForeignKey(Ip4Subnet)
subnet = models.ForeignKey(Ip4Subnet, on_delete=models.CASCADE)
address = models.GenericIPAddressField(protocol='IPv4')
last_contact = models.DateTimeField(null=True, blank=True)
ping_avg_rtt = models.FloatField(null=True, blank=True)
def __str__(self):
# FIXME: generates 2 SQL queries?
if not hasattr(self, 'interface'):
return self.address
return "{} ({})".format(self.address, self.interface.host.hostname)
def assigned_to_host(self):
return self.interface.host
assigned_to_host.short_description = "Assigned to Host"
return self.address
class Meta:
verbose_name = 'IPv4 address'
verbose_name_plural = 'IPv4 addresses'
@python_2_unicode_compatible
class HostType(models.Model):
host_type = models.CharField(max_length=64)
description = models.CharField(max_length=1024)
@@ -503,7 +445,6 @@ class HostType(models.Model):
ordering = ("host_type",)
@python_2_unicode_compatible
class OperatingSystem(models.Model):
OS_ARCHITECTURES = (
('mips', _('MIPS')),
@@ -520,6 +461,10 @@ class OperatingSystem(models.Model):
version = models.CharField(max_length=64)
arch = models.CharField(max_length=255, choices=OS_ARCHITECTURES, blank=True, null=True)
pxe_kernel = models.CharField(max_length=255, blank=True, null=True)
pxe_initrd = models.CharField(max_length=255, blank=True, null=True)
pxe_preseed_config_url = models.URLField(blank=True, null=True)
def __str__(self):
return self.name + " " + self.version + " (" + self.arch + ")"
@@ -527,7 +472,6 @@ class OperatingSystem(models.Model):
ordering = ("name", "version")
@python_2_unicode_compatible
class Host(models.Model):
location = models.CharField(max_length=1024)
brand = models.CharField(max_length=1024)
@@ -537,9 +481,9 @@ class Host(models.Model):
serial_number = models.CharField(max_length=256)
description = models.CharField(max_length=1024)
created_date = models.DateTimeField(auto_now_add=True)
host_type = models.ForeignKey(HostType)
host_type = models.ForeignKey(HostType, on_delete=models.CASCADE)
virtual = models.BooleanField(default=False)
operating_system = models.ForeignKey(OperatingSystem)
operating_system = models.ForeignKey(OperatingSystem, on_delete=models.CASCADE)
request_kerberos_principal = models.BooleanField(default=False)
kerberos_principal_created = models.BooleanField(default=False, editable=False)
@@ -552,40 +496,19 @@ class Host(models.Model):
def __str__(self):
return self.hostname
def in_domain(self):
domains = self.interface_set.values_list('domain__domain_name', flat=True)
return ",".join(domains)
in_domain.short_description = "in domains"
def ipv6_enabled(self):
ipv6_ifs = self.interface_set.annotate(num_ipv6=Count('ip6address')).filter(num_ipv6__gt=0)
return ipv6_ifs.exists()
ipv6_enabled.boolean = True
def mac_addresses(self):
addresses = self.interface_set.filter(macaddr__isnull=False).values_list('macaddr', flat=True)
return ", ".join(addresses)
def ip_addresses(self):
addresses = self.interface_set.filter(ip4address__isnull=False).values_list('ip4address__address', flat=True)
return ", ".join(addresses)
def as_pxe_files(self):
return host_as_pxe_files(self)
@python_2_unicode_compatible
class Interface(models.Model):
name = models.CharField(max_length=128)
macaddr = models.CharField(max_length=17, validators=[validate_macaddr])
pxe_filename = models.CharField(max_length=64, blank=True)
dhcp_client = models.BooleanField(default=False)
host = models.ForeignKey(Host)
ip4address = models.OneToOneField(Ip4Address, blank=True, null=True)
host = models.ForeignKey(Host, on_delete=models.CASCADE)
ip4address = models.OneToOneField(Ip4Address, blank=True, null=True, on_delete=models.SET_NULL)
created_date = models.DateTimeField(auto_now_add=True)
domain = models.ForeignKey(Domain)
domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
def __str__(self):
return "%s (%s on %s)" % (self.macaddr, self.name, self.host.hostname)
@@ -594,11 +517,10 @@ class Interface(models.Model):
return self.ip6address_set.exists()
@python_2_unicode_compatible
class Ip6Address(models.Model):
subnet = models.ForeignKey(Ip6Subnet)
subnet = models.ForeignKey(Ip6Subnet, on_delete=models.CASCADE)
address = models.GenericIPAddressField(protocol='IPv6')
interface = models.ForeignKey(Interface)
interface = models.ForeignKey(Interface, on_delete=models.CASCADE)
def full_address(self):
return self.subnet.network + self.address
+58
View File
@@ -0,0 +1,58 @@
from django.conf import settings
from django.template.loader import render_to_string
import ipaddress
def render_pxelinux_cfg(_if, host):
"""Renders pxelinux.cfg based on a host and the set of host interfaces.
If the host is tied to an OS with PXE fields set, it will use these instead of defaults."""
# Default config
kernel = settings.MDB_PXE_KERNEL
initrd = settings.MDB_PXE_INITRD
preseed_config_url = settings.MDB_PXE_PRESEED_URL
# Per host config (via OS model)
_os = host.operating_system
pxe_attrs = [_os.pxe_initrd, _os.pxe_kernel, _os.pxe_preseed_config_url]
if None not in pxe_attrs and '' not in pxe_attrs:
kernel = _os.pxe_kernel
initrd = _os.pxe_initrd
preseed_config_url = _os.pxe_preseed_config_url
context = {
'kernel': kernel,
'initrd': initrd,
'preseed_config_url': preseed_config_url,
'host': host,
'interface': _if
}
return render_to_string('pxelinux.cfg', context=context)
def get_pxe_filename(interface, filename_format='mac_addr'):
"""
Mac or IP address based PXE filename
Ref: http://www.syslinux.org/wiki/index.php/PXELINUX#Examples
"""
ip_addr_in_hex = '{:02X}'.format(int(ipaddress.IPv4Address(interface.ip4address.address)))
formats = {
'ip_addr': ip_addr_in_hex,
'mac_addr': '01-{}'.format(interface.macaddr.lower().replace(':', '-'))
}
return formats.get(filename_format, formats['mac_addr'])
def host_as_pxe_files(host):
"""Returns a list of filename,content for each host interface with an IPv4 address."""
from mdb.models import Host
assert isinstance(host, Host)
pxe_files = []
for _if in host.interface_set.exclude(ip4address__isnull=True):
filename = get_pxe_filename(_if)
content = render_pxelinux_cfg(_if, host)
pxe_files.append((filename, content))
return pxe_files
+3
View File
@@ -98,9 +98,11 @@ def create_pxe_key_and_write_pxe_files_when_host_changes(sender, instance, creat
for pxe_file_name, pxe_file in host.as_pxe_files():
path = os.path.join(settings.MDB_PXE_TFTP_ROOT, pxe_file_name)
if host.pxe_installable:
# Write pxeconfig file to disk
with open(path, 'w+') as f:
f.write(pxe_file)
logger.info("Created or updated {}".format(path))
os.chmod(path, 0o644) # Not writeable for all
# Also set necessary fields for pxe installation
interfaces = host.interface_set.exclude(ip4address__isnull=True)
@@ -119,6 +121,7 @@ def create_pxe_key_and_write_pxe_files_when_host_changes(sender, instance, creat
else:
if os.path.exists(path):
# Delete pxeconfig file from disk
os.unlink(path)
logger.info("deleted {}".format(path))
+6 -3
View File
@@ -1,4 +1,7 @@
kernel {{kernel}}
append ramdisk_size=14984 locale=en_US console-setup/ask_detect=false keyboard-configuration/layoutcode=no netcfg/wireless_wep= netcfg_choose_interface=eth0 netcfg/get_hostname= preseed/url={{preseed_config_url}} vga=normal initrd={{initrd}} -- snop={{host.pxe_key}}
default ninja
prompt 0
timeout 0
timeout 0
label ninja
kernel {{kernel}}
append ramdisk_size=14984 locale=en_US console-setup/ask_detect=false keyboard-configuration/layoutcode=no netcfg/wireless_wep= netcfg_choose_interface=eth0 netcfg/get_hostname= preseed/url={{preseed_config_url}} vga=normal snop={{host.pxe_key}} initrd={{initrd}} -- quiet
+27 -3
View File
@@ -1,12 +1,15 @@
import io
from django.conf import settings
from django.contrib.auth.models import User
from django.core.management import call_command
from django.core.urlresolvers import reverse
from django.test import TestCase
from mdb.pxe import render_pxelinux_cfg
from rest_framework.authtoken.models import Token
from rest_framework.test import APITestCase
from mdb.models import Host
from mdb.models import Host, OperatingSystem
class MyAPITestCases(APITestCase):
@@ -35,6 +38,27 @@ class MyAPITestCases(APITestCase):
class UnitTests(TestCase):
fixtures = ['test_data']
def test_render_pxeconfig(self):
host = Host.objects.first()
interface = host.interface_set.exclude(ip4address__isnull=True).first()
config = render_pxelinux_cfg(interface, host)
self.assertIn(settings.MDB_PXE_INITRD, config)
self.assertIn(settings.MDB_PXE_KERNEL, config)
self.assertIn(settings.MDB_PXE_PRESEED_URL, config)
def test_render_pxeconfig_with_os_pxe_config(self):
_os = OperatingSystem.objects.get(pk=2) # OS with all pxe vars set
Host.objects.filter(pk=Host.objects.first().pk).update(operating_system=_os)
host = Host.objects.first()
interface = host.interface_set.exclude(ip4address__isnull=True).first()
config = render_pxelinux_cfg(interface, host)
self.assertIn(_os.pxe_initrd, config)
self.assertIn(_os.pxe_kernel, config)
self.assertIn(_os.pxe_preseed_config_url, config)
def test_host_as_pxe_files(self):
host = Host.objects.first()
self.assertEqual(len(host.as_pxe_files()), 1)
@@ -44,13 +68,13 @@ class RunManagementCommands(TestCase):
fixtures = ['test_data']
def test_zone_sync(self):
expected_zone_file = ''
# expected_zone_file = ''
with io.BytesIO() as f:
call_command('zone-sync', debug=True, stdout=f)
# TODO check expected_zone_file
def test_dhcp_sync(self):
expected_dhcp_file = ''
# expected_dhcp_file = ''
with io.BytesIO() as f:
call_command('dhcp-sync', stdout=f)
# TODO check expected_dhcp_file
+1 -48
View File
@@ -1,7 +1,4 @@
import datetime
from django.conf import settings
from django.template.loader import render_to_string
import ipaddress
import re
@@ -20,7 +17,7 @@ def format_domain_serial_and_add_one(serial):
""" The serial contains todays date, just update it. """
try:
number = int(res[0])
except:
except ValueError:
number = 1
if number >= 99:
""" This is bad... Just keep the number on 99.
@@ -35,47 +32,3 @@ def format_domain_serial_and_add_one(serial):
""" Just return the first serial for today. """
return "%4d%02d%02d%02d" % \
(today.year, today.month, today.day, 1)
def render_pxelinux_cfg(_if, host):
# TODO add fields to os model and make dynamic per host
distro = 'ubuntu/14_04/amd64/alternate/'
kernel = distro + 'linux'
initrd = distro + 'initrd.gz'
preseed_config_url = settings.MDB_PXE_PRESEED_URL
context = {
'kernel': kernel,
'initrd': initrd,
'preseed_config_url': preseed_config_url,
'host': host,
'interface': _if
}
return render_to_string('pxelinux.cfg', context=context)
def get_pxe_filename(interface, filename_format='mac_addr'):
"""
Mac or IP address based PXE filename
Ref: http://www.syslinux.org/wiki/index.php/PXELINUX#Examples
"""
ip_addr_in_hex = '{:02X}'.format(int(ipaddress.IPv4Address(interface.ip4address.address)))
formats = {
'ip_addr': ip_addr_in_hex,
'mac_addr': '01-{}'.format(interface.macaddr.lower().replace(':', '-'))
}
return formats.get(filename_format, formats['mac_addr'])
def host_as_pxe_files(host):
""" Returns a list of filename,content for each host interface with an IPv4 address """
from mdb.models import Host
assert isinstance(host, Host)
pxe_files = []
for _if in host.interface_set.exclude(ip4address__isnull=True):
filename = get_pxe_filename(_if)
content = render_pxelinux_cfg(_if, host)
pxe_files.append((filename, content))
return pxe_files
-1
View File
@@ -1,4 +1,3 @@
from __future__ import unicode_literals
from django.core.validators import RegexValidator
import re
+76 -22
View File
@@ -1,5 +1,5 @@
import os
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP
import raven
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
@@ -32,38 +32,40 @@ ADMIN_MEDIA_PREFIX = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# Make this unique, and don't share it with anybody.
SECRET_KEY = '@4ps=ebofcpv8sb#y7gm896=l&l*u0u-pgo7bcdj%+(k+2lqnl'
MIDDLEWARE_CLASSES = (
MIDDLEWARE = [
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
]
ROOT_URLCONF = 'mdbsite.urls'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
INSTALLED_APPS = (
'django.contrib.auth',
@@ -76,15 +78,56 @@ INSTALLED_APPS = (
'django.contrib.admin',
'rest_framework',
'rest_framework.authtoken',
'django_extensions',
'raven.contrib.django.raven_compat',
'mdb',
)
EMAIL_SUBJECT_PREFIX = '[MDB] '
TEMPLATE_CONTEXT_PROCESSORS = TCP + (
'django.core.context_processors.request', # for django-suit
)
# Logging
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s '
'%(process)d %(thread)d %(message)s'
},
},
'handlers': {
'sentry': {
'level': 'WARNING', # To capture more than ERROR, change to WARNING, INFO, etc.
'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
}
},
'loggers': {
'root': {
'level': 'WARNING',
'handlers': ['sentry'],
},
'django.db.backends': {
'level': 'ERROR',
'handlers': ['console'],
'propagate': False,
},
'raven': {
'level': 'WARNING',
'handlers': ['console'],
'propagate': False,
},
'sentry.errors': {
'level': 'DEBUG',
'handlers': ['console'],
'propagate': False,
},
},
}
# django-suit configuration
SUIT_CONFIG = {
@@ -115,16 +158,27 @@ SUIT_CONFIG = {
),
# misc
'LIST_PER_PAGE': 30
'LIST_PER_PAGE': 35
}
# REST Framework
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
'PAGINATE_BY': 10
}
# PXE
MDB_PXE_TFTP_ROOT = '/var/lib/tftpboot/pxelinux/pxelinux.cfg/'
MDB_PXE_KERNEL = 'ubuntu/14_04/amd64/alternate/linux'
MDB_PXE_INITRD = 'ubuntu/14_04/amd64/alternate/initrd.gz'
MDB_PXE_PRESEED_URL = 'http://158.36.190.194/ubuntu/preseed_1404.cfg'
# Sentry
RAVEN_CONFIG = {
'dsn': os.getenv('RAVEN_DSN'),
'release': raven.fetch_git_sha(BASE_DIR)
}
try:
from .local_settings import *
except ImportError:
+4 -4
View File
@@ -1,15 +1,15 @@
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import re_path, path
from django.views.generic.base import RedirectView
from mdb.api.views import HostPXEValidate
admin.autodiscover()
urlpatterns = [
url(r'^$', RedirectView.as_view(url='admin/', permanent=True)),
url(r'^admin/', include(admin.site.urls)),
url(r'^api/validate-host-secret/', HostPXEValidate.as_view(), name='api-validate-host-secret')
re_path(r'^$', RedirectView.as_view(url='admin/', permanent=True)),
path('admin/', admin.site.urls),
path('api/validate-host-secret/', HostPXEValidate.as_view(), name='api-validate-host-secret')
]
urlpatterns += staticfiles_urlpatterns()
+4 -3
View File
@@ -1,5 +1,6 @@
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mdb.settings")
from django.core.wsgi import get_wsgi_application
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mdbsite.settings")
application = get_wsgi_application()
-7
View File
@@ -1,7 +0,0 @@
Django>=1.8,<1.9
django-auth-ldap
django-extensions==1.5.7
djangorestframework==3.2.4
django-suit==0.2.15
ipaddress==1.0.14
mysqlclient==1.3.6