Finished management command for dhcp sync
This commit is contained in:
1 parent
0773ae9263
commit
11f3b23c67
1 file changed
+34
-21
@@ -1,13 +1,15 @@
|
|||||||
|
from __future__ import with_statement
|
||||||
|
from contextlib import closing
|
||||||
|
|
||||||
|
import sys
|
||||||
from django.core.management.base import BaseCommand, CommandError
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
from django.core.mail import mail_admins
|
from django.core.mail import mail_admins
|
||||||
import sys
|
|
||||||
from optparse import make_option
|
|
||||||
from commands import getstatusoutput
|
from commands import getstatusoutput
|
||||||
from mdb.models import DhcpConfig
|
from mdb.models import DhcpConfig
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
args = '<poll_id poll_id ...>'
|
args = '--output <path> [--force] [--restart]'
|
||||||
help = 'Generates the DHCP configuration'
|
help = 'Generates the DHCP configuration'
|
||||||
|
|
||||||
option_list = BaseCommand.option_list + (
|
option_list = BaseCommand.option_list + (
|
||||||
@@ -21,37 +23,48 @@ class Command(BaseCommand):
|
|||||||
dest='force',
|
dest='force',
|
||||||
default=False,
|
default=False,
|
||||||
help='Write to output even if serial is unchanged.'),
|
help='Write to output even if serial is unchanged.'),
|
||||||
|
make_option('--restart',
|
||||||
|
action='store_true',
|
||||||
|
dest='restart',
|
||||||
|
default=False,
|
||||||
|
help='Restart dhcp3-server if serial is changed'),
|
||||||
)
|
)
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
output = options['output'] if options['output'] else sys.stdout
|
output = options['output'] if options['output'] else sys.stdout
|
||||||
force = options['force']
|
force = options['force']
|
||||||
|
restart = options['restart']
|
||||||
|
|
||||||
#dhcpd_init = "/etc/init.d/dhcp3-server %s"
|
dhcpd_init = "/etc/init.d/dhcp3-server"
|
||||||
dhcpd_init = "echo %s"
|
|
||||||
|
|
||||||
#dhcpd_config = "/etc/dhcp3/dhcpd.conf"
|
|
||||||
|
|
||||||
config = DhcpConfig.objects.filter(name="default").get()
|
config = DhcpConfig.objects.filter(name="default").get()
|
||||||
|
|
||||||
|
# do we need to do anything?
|
||||||
if config.serial == config.active_serial and not force:
|
if config.serial == config.active_serial and not force:
|
||||||
|
print "Nothing to do, serial is the same: %s" % config.serial
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
dhcpd_file = open(output, 'w')
|
# write config
|
||||||
dhcpd_file.write(config.dhcpd_configuration())
|
if output == sys.stdout:
|
||||||
dhcpd_file.close()
|
print config.dhcpd_configuration()
|
||||||
|
else:
|
||||||
|
with closing(open(output, 'w')) as f:
|
||||||
|
f.write(config.dhcpd_configuration())
|
||||||
|
|
||||||
|
# update serial
|
||||||
config.active_serial = config.serial
|
config.active_serial = config.serial
|
||||||
config.save()
|
config.save()
|
||||||
|
|
||||||
status, output = getstatusoutput(dhcpd_init % "restart")
|
# restart the dhcp server?
|
||||||
if status != 0:
|
if restart:
|
||||||
mail_admins(
|
status, output = getstatusoutput("%s restart" % dhcpd_init)
|
||||||
"Failed to restart dhcp3-server",
|
if status != 0:
|
||||||
"Failed to restart dhcp3-server, please inspect...\n\n" + output)
|
mail_admins(
|
||||||
sys.exit(1)
|
"Failed to restart dhcp3-server",
|
||||||
else:
|
"Failed to restart dhcp3-server, please inspect...\n\n" + output)
|
||||||
mail_admins(
|
sys.exit(1)
|
||||||
"Update dhcpd configuration success!",
|
else:
|
||||||
"Updated dhcpd configuration to serial %d." % config.active_serial)
|
mail_admins(
|
||||||
sys.exit(0)
|
"Update dhcpd configuration success!",
|
||||||
|
"Updated dhcpd configuration to serial %d." % config.active_serial)
|
||||||
|
sys.exit(0)
|
||||||
Reference in new issue
Block a user