1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Django command: Configure master DHCP.
6
The master DHCP settings apply to the DHCP server running on the MAAS server
7
itself. They can be either disabled (if you don't want MAAS to manage DHCP)
8
or fully configured using this command.
11
from __future__ import (
22
from optparse import (
27
from django.core.management.base import BaseCommand
28
from maasserver.models import NodeGroup
32
'subnet_mask': "Subnet mask, e.g. 255.0.0.0",
33
'broadcast_ip': "Broadcast address for this subnet, e.g. 10.255.255.255",
34
'router_ip': "Address of default gateway.",
35
'ip_range_low': "Lowest IP address to assign to clients.",
36
'ip_range_high': "Highest IP address to assign to clients.",
40
# DHCP settings when disabled.
41
clear_settings = {item: None for item in dhcp_items}
44
def get_settings(options):
45
"""Get the DHCP settings from `options`, as a dict.
47
Checks validity of the settings.
50
item: options.get(item)
51
for item in dhcp_items}
52
# All of the DHCP settings must have (non-empty) values.
53
if not all(settings.values()):
54
raise OptionValueError(
55
"Specify all DHCP settings: %s" % ', '.join(dhcp_items))
59
def name_option(dhcp_setting):
60
"""Formulate the option name corresponding to a DHCP setting name."""
61
return '--%s' % dhcp_setting.replace('_', '-')
64
class Command(BaseCommand):
66
option_list = BaseCommand.option_list + (
68
'--clear', dest='clear', action='store_true', default=False,
70
"Clear settings. Do only when MAAS DHCP is disabled. "
71
"If given, any DHCP parameters are ignored.")),
73
'--ensure', dest='ensure', action='store_true', default=False,
75
"Ensure that the master node group is configured, "
76
"but if it was already set up, don't change its settings. "
77
"If given, any DHCP parameters are ignored.")),
80
name_option(item), dest=item, default=None,
81
help="DHCP parameter: %s" % help)
82
for item, help in dhcp_items.items())
83
help = "Initialize master DHCP settings."
85
def handle(self, *args, **options):
86
if options.get('ensure') and options.get('clear'):
87
raise OptionValueError(
88
"The --ensure option conflicts with --clear.")
89
master_nodegroup = NodeGroup.objects.ensure_master()
90
if not options.get('ensure'):
91
if options.get('clear'):
92
settings = clear_settings
94
settings = get_settings(options)
95
for item, value in settings.items():
96
setattr(master_nodegroup, item, value)
97
master_nodegroup.save()