-class ServiceHandler(Handler):
- r"""ServiceHandler([start[, stop[, restart[, reload]]]]) -> ServiceHandler.
-
- This is a helper class to inherit from to automatically handle services
- with start, stop, restart, reload actions.
-
- The actions can be defined by calling the constructor with all the
- parameters or in a more declarative way as class attributes, like:
-
- class TestHandler(ServiceHandler):
- _service_start = ('command', 'start')
- _service_stop = ('command', 'stop')
- _service_restart = ('command', 'restart')
- _service_reload = 'reload-command'
-
- Commands are executed without using the shell, that's why they are specified
- as tuples (where the first element is the command and the others are the
- command arguments). If only a command is needed (without arguments) a single
- string can be specified.
-
- All commands must be specified.
- """
- # TODO implement it using metaclasses to add the handlers method by demand
- # (only for specifieds commands).
-
- def __init__(self, start=None, stop=None, restart=None, reload=None):
- r"Initialize the object, see the class documentation for details."
- for (name, action) in dict(start=start, stop=stop, restart=restart,
- reload=reload).items():
- if action is not None:
- setattr(self, '_service_%s' % name, action)
-
- @handler(u'Start the service.')
- def start(self):
- r"start() -> None :: Start the service."
- call(self._service_start)
-
- @handler(u'Stop the service.')
- def stop(self):
- r"stop() -> None :: Stop the service."
- call(self._service_stop)
-
- @handler(u'Restart the service.')
- def restart(self):
- r"restart() -> None :: Restart the service."
- call(self._service_restart)
-
- @handler(u'Reload the service config (without restarting, if possible).')
- def reload(self):
- r"reload() -> None :: Reload the configuration of the service."
- call(self._service_reload)
-
-class InitdHandler(Handler):
- r"""InitdHandler([initd_name[, initd_dir]]) -> InitdHandler.
-
- This is a helper class to inherit from to automatically handle services
- with start, stop, restart, reload actions using a /etc/init.d like script.
-
- The name and directory of the script can be defined by calling the
- constructor or in a more declarative way as class attributes, like:
-
- class TestHandler(ServiceHandler):
- _initd_name = 'some-service'
- _initd_dir = '/usr/local/etc/init.d'
-
- The default _initd_dir is '/etc/init.d', _initd_name has no default and
- must be specified in either way.
-
- Commands are executed without using the shell.
- """
- # TODO implement it using metaclasses to add the handlers method by demand
- # (only for specifieds commands).
-
- _initd_dir = '/etc/init.d'
-
- def __init__(self, initd_name=None, initd_dir=None):
- r"Initialize the object, see the class documentation for details."
- if initd_name is not None:
- self._initd_name = initd_name
- if initd_dir is not None:
- self._initd_dir = initd_dir
-
- @handler(u'Start the service.')
- def start(self):
- r"start() -> None :: Start the service."
- call((path.join(self._initd_dir, self._initd_name), 'start'))
-
- @handler(u'Stop the service.')
- def stop(self):
- r"stop() -> None :: Stop the service."
- call((path.join(self._initd_dir, self._initd_name), 'stop'))
-
- @handler(u'Restart the service.')
- def restart(self):
- r"restart() -> None :: Restart the service."
- call((path.join(self._initd_dir, self._initd_name), 'restart'))
-
- @handler(u'Reload the service config (without restarting, if possible).')
- def reload(self):
- r"reload() -> None :: Reload the configuration of the service."
- call((path.join(self._initd_dir, self._initd_name), 'reload'))
-