From b0579fe7c01628492c541553cd28f555f00c1b9c Mon Sep 17 00:00:00 2001 From: Jonas Braathen Date: Wed, 15 Oct 2014 19:40:38 +0200 Subject: [PATCH] Adds unfinished management command for dhcp sync --- mdb/management/__init__.py | 0 mdb/management/commands/__init__.py | 0 mdb/management/commands/dhcp-sync.py | 57 ++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 mdb/management/__init__.py create mode 100644 mdb/management/commands/__init__.py create mode 100644 mdb/management/commands/dhcp-sync.py diff --git a/mdb/management/__init__.py b/mdb/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mdb/management/commands/__init__.py b/mdb/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mdb/management/commands/dhcp-sync.py b/mdb/management/commands/dhcp-sync.py new file mode 100644 index 0000000..85f63cf --- /dev/null +++ b/mdb/management/commands/dhcp-sync.py @@ -0,0 +1,57 @@ +from django.core.management.base import BaseCommand, CommandError +from django.core.mail import mail_admins +import sys +from optparse import make_option +from commands import getstatusoutput +from mdb.models import DhcpConfig + + +class Command(BaseCommand): + args = '' + 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 to output even if serial is unchanged.'), + ) + + def handle(self, *args, **options): + output = options['output'] if options['output'] else sys.stdout + force = options['force'] + + #dhcpd_init = "/etc/init.d/dhcp3-server %s" + dhcpd_init = "echo %s" + + #dhcpd_config = "/etc/dhcp3/dhcpd.conf" + + config = DhcpConfig.objects.filter(name="default").get() + + if config.serial == config.active_serial and not force: + sys.exit(0) + + dhcpd_file = open(output, 'w') + dhcpd_file.write(config.dhcpd_configuration()) + dhcpd_file.close() + + config.active_serial = config.serial + config.save() + + status, output = getstatusoutput(dhcpd_init % "restart") + if status != 0: + mail_admins( + "Failed to restart dhcp3-server", + "Failed to restart dhcp3-server, please inspect...\n\n" + output) + sys.exit(1) + else: + mail_admins( + "Update dhcpd configuration success!", + "Updated dhcpd configuration to serial %d." % config.active_serial) + sys.exit(0)