~ubuntu-branches/ubuntu/raring/maas/raring-proposed

« back to all changes in this revision

Viewing changes to src/maasserver/management/commands/config_master_dhcp.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-07-17 08:28:36 UTC
  • mfrom: (1.1.16)
  • Revision ID: package-import@ubuntu.com-20120717082836-ucb2vou8tqg353eq
Tags: 0.1+bzr777+dfsg-0ubuntu1
* New upstream release
* Only run 'maas' command as root. (LP: #974046)
  - debian/extras/maas: Check id.
  - debian/maas.install: Install in 'sbin'.
* debian/maas.postinst:
  - restart apache2 after everything gets processed.
  - Update version to handle upgrades.
* debian/extras/maas-provision: Add wrapper to access 'maasprovisiong'
  command line.
* debian/patches/99_temporary_fix_path.patch: Dropped. No longer needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Django command: Configure master DHCP.
 
5
 
 
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.
 
9
"""
 
10
 
 
11
from __future__ import (
 
12
    absolute_import,
 
13
    print_function,
 
14
    unicode_literals,
 
15
    )
 
16
 
 
17
__metaclass__ = type
 
18
__all__ = [
 
19
    'Command',
 
20
    ]
 
21
 
 
22
from optparse import (
 
23
    make_option,
 
24
    OptionValueError,
 
25
    )
 
26
 
 
27
from django.core.management.base import BaseCommand
 
28
from maasserver.models import NodeGroup
 
29
 
 
30
 
 
31
dhcp_items = {
 
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.",
 
37
    }
 
38
 
 
39
 
 
40
# DHCP settings when disabled.
 
41
clear_settings = {item: None for item in dhcp_items}
 
42
 
 
43
 
 
44
def get_settings(options):
 
45
    """Get the DHCP settings from `options`, as a dict.
 
46
 
 
47
    Checks validity of the settings.
 
48
    """
 
49
    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))
 
56
    return settings
 
57
 
 
58
 
 
59
def name_option(dhcp_setting):
 
60
    """Formulate the option name corresponding to a DHCP setting name."""
 
61
    return '--%s' % dhcp_setting.replace('_', '-')
 
62
 
 
63
 
 
64
class Command(BaseCommand):
 
65
 
 
66
    option_list = BaseCommand.option_list + (
 
67
        make_option(
 
68
            '--clear', dest='clear', action='store_true', default=False,
 
69
            help=(
 
70
                "Clear settings.  Do only when MAAS DHCP is disabled.  "
 
71
                "If given, any DHCP parameters are ignored.")),
 
72
        make_option(
 
73
            '--ensure', dest='ensure', action='store_true', default=False,
 
74
            help=(
 
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.")),
 
78
        ) + tuple(
 
79
            make_option(
 
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."
 
84
 
 
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
 
93
            else:
 
94
                settings = get_settings(options)
 
95
            for item, value in settings.items():
 
96
                setattr(master_nodegroup, item, value)
 
97
            master_nodegroup.save()