# vim: set encoding=utf-8 et sw=4 sts=4 :
from os import path
+import logging ; log = logging.getLogger('pymin.services.dhcp')
from pymin.seqtools import Sequence
from pymin.dispatcher import Handler, handler, HandlerError
from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
- TransactionalHandler, ParametersHandler
-
-__ALL__ = ('DhcpHandler', 'Error', 'HostError', 'HostAlreadyExistsError',
- 'HostNotFoundError')
-
-class Error(HandlerError):
- r"""
- Error(message) -> Error instance :: Base DhcpHandler exception class.
-
- All exceptions raised by the DhcpHandler inherits from this one, so you can
- easily catch any DhcpHandler exception.
-
- message - A descriptive error message.
- """
- pass
-
-class HostError(Error, KeyError):
- r"""
- HostError(hostname) -> HostError instance
-
- This is the base exception for all host related errors.
- """
-
- def __init__(self, hostname):
- r"Initialize the object. See class documentation for more info."
- self.message = u'Host error: "%s"' % hostname
-
-class HostAlreadyExistsError(HostError):
- r"""
- HostAlreadyExistsError(hostname) -> HostAlreadyExistsError instance
-
- This exception is raised when trying to add a hostname that already exists.
- """
-
- def __init__(self, hostname):
- r"Initialize the object. See class documentation for more info."
- self.message = u'Host already exists: "%s"' % hostname
-
-class HostNotFoundError(HostError):
- r"""
- HostNotFoundError(hostname) -> HostNotFoundError instance
-
- This exception is raised when trying to operate on a hostname that doesn't
- exists.
- """
-
- def __init__(self, hostname):
- r"Initialize the object. See class documentation for more info."
- self.message = u'Host not found: "%s"' % hostname
+ TransactionalHandler, ParametersHandler, \
+ DictSubHandler, ReloadHandler
+__all__ = ('DhcpHandler',)
class Host(Sequence):
r"""Host(name, ip, mac) -> Host instance :: Class representing a host.
r"Return a tuple representing the host."
return (self.name, self.ip, self.mac)
-class HostHandler(Handler):
- r"""HostHandler(hosts) -> HostHandler instance :: Handle a list of hosts.
+ def update(self, ip=None, mac=None):
+ if ip is not None:
+ self.ip = ip
+ if mac is not None:
+ self.mac = mac
+
+class HostHandler(DictSubHandler):
+ r"""HostHandler(parent) -> HostHandler instance :: Handle a list of hosts.
This class is a helper for DhcpHandler to do all the work related to hosts
administration.
-
- hosts - A dictionary with string keys (hostnames) and Host instances values.
"""
handler_help = u"Manage DHCP hosts"
- def __init__(self, hosts):
- r"Initialize HostHandler object, see class documentation for details."
- self.hosts = hosts
-
- @handler(u'Add a new host')
- def add(self, name, ip, mac):
- r"add(name, ip, mac) -> None :: Add a host to the hosts list."
- if name in self.hosts:
- raise HostAlreadyExistsError(name)
- self.hosts[name] = Host(name, ip, mac)
-
- @handler(u'Update a host')
- def update(self, name, ip=None, mac=None):
- r"update(name[, ip[, mac]]) -> None :: Update a host of the hosts list."
- if not name in self.hosts:
- raise HostNotFoundError(name)
- if ip is not None:
- self.hosts[name].ip = ip
- if mac is not None:
- self.hosts[name].mac = mac
-
- @handler(u'Delete a host')
- def delete(self, name):
- r"delete(name) -> None :: Delete a host of the hosts list."
- if not name in self.hosts:
- raise HostNotFoundError(name)
- del self.hosts[name]
-
- @handler(u'Get information about a host')
- def get(self, name):
- r"get(name) -> Host :: List all the information of a host."
- if not name in self.hosts:
- raise HostNotFoundError(name)
- return self.hosts[name]
-
- @handler(u'List hosts')
- def list(self):
- r"list() -> tuple :: List all the hostnames."
- return self.hosts.keys()
-
- @handler(u'Get information about all hosts')
- def show(self):
- r"show() -> list of Hosts :: List all the complete hosts information."
- return self.hosts.values()
-
-
-class DhcpHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler,
- ParametersHandler):
+ _cont_subhandler_attr = 'hosts'
+ _cont_subhandler_class = Host
+
+class DhcpHandler(Restorable, ConfigWriter, ReloadHandler, TransactionalHandler,
+ ParametersHandler, InitdHandler):
r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance.
Handles DHCP service commands for the dhcpd program.
def __init__(self, pickle_dir='.', config_dir='.'):
r"Initialize DhcpHandler object, see class documentation for details."
+ log.debug(u'DhcpHandler(%r, %r)', pickle_dir, config_dir)
self._persistent_dir = pickle_dir
self._config_writer_cfg_dir = config_dir
self._config_build_templates()
- self._restore()
- self.host = HostHandler(self.hosts)
+ InitdHandler.__init__(self)
+ self.host = HostHandler(self)
def _get_config_vars(self, config_file):
return dict(hosts=self.hosts.values(), **self.params)
if __name__ == '__main__':
+ logging.basicConfig(
+ level = logging.DEBUG,
+ format = '%(asctime)s %(levelname)-8s %(message)s',
+ datefmt = '%H:%M:%S',
+ )
+
import os
h = DhcpHandler()