X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/3660782f64b0e3dfc2db4b1bfce4c59b4b46e3fc..9c443d159056e112ed23af07afa38022882583b3:/pymin/services/util.py?ds=inline diff --git a/pymin/services/util.py b/pymin/services/util.py index b665c00..b8a2ace 100644 --- a/pymin/services/util.py +++ b/pymin/services/util.py @@ -11,14 +11,18 @@ except ImportError: from pymin.dispatcher import Handler, handler, HandlerError, \ CommandNotFoundError +from pymin.seqtools import Sequence #DEBUG = False DEBUG = True -__ALL__ = ('ServiceHandler', 'RestartHandler', 'ReloadHandler', 'InitdHandler', - 'SubHandler', 'DictSubHandler', 'ListSubHandler', 'Persistent', - 'ConfigWriter', 'Error', 'ReturnNot0Error', 'ExecutionError', - 'ItemError', 'ItemAlreadyExistsError', 'ItemNotFoundError', 'call') +__ALL__ = ('Error', 'ReturnNot0Error', 'ExecutionError', 'ItemError', + 'ItemAlreadyExistsError', 'ItemNotFoundError', 'ContainerError', + 'ContainerNotFoundError', 'call', 'get_network_devices', + 'Persistent', 'Restorable', 'ConfigWriter', 'ServiceHandler', + 'RestartHandler', 'ReloadHandler', 'InitdHandler', 'SubHandler', + 'DictSubHandler', 'ListSubHandler', 'ComposedSubHandler', + 'ListComposedSubHandler', 'DictComposedSubHandler', 'Device','Address') class Error(HandlerError): r""" @@ -126,6 +130,97 @@ class ItemNotFoundError(ItemError): r"Initialize the object. See class documentation for more info." self.message = u'Item not found: "%s"' % key +class ContainerError(Error, KeyError): + r""" + ContainerError(key) -> ContainerError instance. + + This is the base exception for all container related errors. + """ + + def __init__(self, key): + r"Initialize the object. See class documentation for more info." + self.message = u'Container error: "%s"' % key + +class ContainerNotFoundError(ContainerError): + r""" + ContainerNotFoundError(key) -> ContainerNotFoundError instance. + + This exception is raised when trying to operate on an container that + doesn't exists. + """ + + def __init__(self, key): + r"Initialize the object. See class documentation for more info." + self.message = u'Container not found: "%s"' % key + +class Address(Sequence): + def __init__(self, ip, netmask, broadcast=None, peer=None): + self.ip = ip + self.netmask = netmask + self.broadcast = broadcast + self.peer = peer + def update(self, netmask=None, broadcast=None): + if netmask is not None: self.netmask = netmask + if broadcast is not None: self.broadcast = broadcast + def as_tuple(self): + return (self.ip, self.netmask, self.broadcast, self.peer) + + +class Device(Sequence): + def __init__(self, name, mac, ppp): + self.name = name + self.mac = mac + self.ppp = ppp + self.addrs = dict() + self.routes = list() + def as_tuple(self): + return (self.name, self.mac, self.addrs) + + + +def get_network_devices(): + p = subprocess.Popen(('ip', '-o', 'link'), stdout=subprocess.PIPE, + close_fds=True) + string = p.stdout.read() + p.wait() + d = dict() + devices = string.splitlines() + for dev in devices: + mac = '' + if dev.find('link/ether') != -1: + i = dev.find('link/ether') + mac = dev[i+11 : i+11+17] + i = dev.find(':') + j = dev.find(':', i+1) + name = dev[i+2: j] + d[name] = Device(name,mac,False) + elif dev.find('link/ppp') != -1: + i = dev.find('link/ppp') + mac = '00:00:00:00:00:00' + i = dev.find(':') + j = dev.find(':', i+1) + name = dev[i+2 : j] + d[name] = Device(name,mac,True) + #since the device is ppp, get the address and peer + try: + p = subprocess.Popen(('ip', '-o', 'addr', 'show', name), stdout=subprocess.PIPE, + close_fds=True, stderr=subprocess.PIPE) + string = p.stdout.read() + p.wait() + addrs = string.splitlines() + inet = addrs[1].find('inet') + peer = addrs[1].find('peer') + bar = addrs[1].find('/') + from_addr = addrs[1][inet+5 : peer-1] + to_addr = addrs[1][peer+5 : bar] + d[name].addrs[from_addr] = Address(from_addr,24, peer=to_addr) + except IndexError: + pass + return d + +def get_peers(): + p = subprocess.Popen(('ip', '-o', 'addr'), stdout=subprocess.PIPE, + close_fds=True) def call(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, universal_newlines=True, @@ -136,6 +231,7 @@ def call(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, print 'Executing command:', command return try: + print 'Executing command:', command r = subprocess.call(command, stdin=stdin, stdout=stdout, stderr=stderr, universal_newlines=universal_newlines, close_fds=close_fds, **kw) @@ -252,9 +348,6 @@ class Restorable(Persistent): r"_restore() -> bool :: Restore persistent data or create a default." try: self._load() - # TODO tener en cuenta servicios que hay que levantar y los que no - if hasattr(self, 'commit'): # TODO deberia ser reload y/o algo para comandos - self.commit() return True except IOError: for (k, v) in self._restorable_defaults.items(): @@ -266,8 +359,6 @@ class Restorable(Persistent): self._dump() if hasattr(self, '_write_config'): self._write_config() - if hasattr(self, 'reload'): - self.reload() return False class ConfigWriter: @@ -395,7 +486,7 @@ class ConfigWriter: self._write_single_config(t) -class ServiceHandler(Handler): +class ServiceHandler(Handler, Restorable): r"""ServiceHandler([start[, stop[, restart[, reload]]]]) -> ServiceHandler. This is a helper class to inherit from to automatically handle services @@ -426,26 +517,63 @@ class ServiceHandler(Handler): reload=reload).items(): if action is not None: setattr(self, '_service_%s' % name, action) + self._persistent_attrs = list(self._persistent_attrs) + self._persistent_attrs.append('_service_running') + if '_service_running' not in self._restorable_defaults: + self._restorable_defaults['_service_running'] = False + self._restore() + if self._service_running: + self._service_running = False + self.start() @handler(u'Start the service.') def start(self): r"start() -> None :: Start the service." - call(self._service_start) + if not self._service_running: + if callable(self._service_start): + self._service_start() + else: + call(self._service_start) + self._service_running = True + self._dump_attr('_service_running') @handler(u'Stop the service.') def stop(self): r"stop() -> None :: Stop the service." - call(self._service_stop) + if self._service_running: + if callable(self._service_stop): + self._service_stop() + else: + call(self._service_stop) + self._service_running = False + self._dump_attr('_service_running') @handler(u'Restart the service.') def restart(self): r"restart() -> None :: Restart the service." - call(self._service_restart) + if callable(self._service_restart): + self._service_restart() + else: + call(self._service_restart) + self._service_running = True + self._dump_attr('_service_running') @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) + if self._service_running: + if callable(self._service_reload): + self._service_reload() + else: + call(self._service_reload) + + @handler(u'Tell if the service is running.') + def running(self): + r"reload() -> None :: Reload the configuration of the service." + if self._service_running: + return 1 + else: + return 0 class RestartHandler(Handler): r"""RestartHandler() -> RestartHandler :: Provides generic restart command. @@ -471,9 +599,11 @@ class ReloadHandler(Handler): @handler(u'Reload the service config (alias to restart).') def reload(self): r"reload() -> None :: Reload the configuration of the service." - self.restart() + if hasattr(self, '_service_running') and self._service_running: + self.restart() -class InitdHandler(Handler): +class InitdHandler(ServiceHandler): + # TODO update docs, declarative style is depracated r"""InitdHandler([initd_name[, initd_dir]]) -> InitdHandler. This is a helper class to inherit from to automatically handle services @@ -502,26 +632,20 @@ class InitdHandler(Handler): 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')) + actions = dict() + for action in ('start', 'stop', 'restart', 'reload'): + actions[action] = (path.join(self._initd_dir, self._initd_name), + action) + ServiceHandler.__init__(self, **actions) + + def handle_timer(self): + p = subprocess.Popen(('pgrep', '-f', self._initd_name), + stdout=subprocess.PIPE) + pid = p.communicate()[0] + if p.returncode == 0 and len(pid) > 0: + self._service_running = True + else: + self._service_running = False class TransactionalHandler(Handler): r"""Handle command transactions providing a commit and rollback commands. @@ -544,9 +668,10 @@ class TransactionalHandler(Handler): r"commit() -> None :: Commit the changes and reload the service." if hasattr(self, '_dump'): self._dump() + unchanged = False if hasattr(self, '_write_config'): - self._write_config() - if hasattr(self, 'reload'): + unchanged = self._write_config() + if not unchanged and hasattr(self, 'reload'): self.reload() @handler(u'Discard all the uncommited changes.') @@ -585,6 +710,8 @@ class ParametersHandler(Handler): if not param in self.params: raise ParameterNotFoundError(param) self.params[param] = value + if hasattr(self, '_update'): + self._update = True @handler(u'Get a service parameter.') def get(self, param): @@ -703,7 +830,7 @@ class ContainerSubHandler(SubHandler): item.update(*args, **kwargs) if hasattr(item, '_update'): item._update = True - except IndexError: + except LookupError: raise ItemNotFoundError(index) @handler(u'Delete an item') @@ -718,9 +845,17 @@ class ContainerSubHandler(SubHandler): else: del self._attr()[index] return item - except IndexError: + except LookupError: raise ItemNotFoundError(index) + @handler(u'Remove all items (use with care).') + def clear(self): + r"clear() -> None :: Delete all items of the container." + if isinstance(self._attr(), dict): + self._attr.clear() + else: + self._attr(list()) + @handler(u'Get information about an item') def get(self, index): r"get(index) -> item :: List all the information of an item." @@ -728,7 +863,7 @@ class ContainerSubHandler(SubHandler): index = int(index) # TODO validation try: return self._vattr()[index] - except IndexError: + except LookupError: raise ItemNotFoundError(index) @handler(u'Get information about all items') @@ -741,25 +876,8 @@ class ContainerSubHandler(SubHandler): class ListSubHandler(ContainerSubHandler): r"""ListSubHandler(parent) -> ListSubHandler instance. - This is a helper class to inherit from to automatically handle subcommands - that operates over a list parent attribute. - - The list attribute to handle and the class of objects that it contains can - be defined by calling the constructor or in a more declarative way as - class attributes, like: - - class TestHandler(ListSubHandler): - _cont_subhandler_attr = 'some_list' - _cont_subhandler_class = SomeClass - - This way, the parent's some_list attribute (self.parent.some_list) will be - managed automatically, providing the commands: add, update, delete, get, - list and show. New items will be instances of SomeClass, which should - provide a cmp operator to see if the item is on the list and an update() - method, if it should be possible to modify it. If SomeClass has an _add, - _update or _delete attribute, it set them to true when the item is added, - updated or deleted respectively (in case that it's deleted, it's not - removed from the list, but it's not listed either). + ContainerSubHandler holding lists. See ComposedSubHandler documentation + for details. """ @handler(u'Get how many items are in the list') @@ -770,36 +888,215 @@ class ListSubHandler(ContainerSubHandler): class DictSubHandler(ContainerSubHandler): r"""DictSubHandler(parent) -> DictSubHandler instance. - This is a helper class to inherit from to automatically handle subcommands - that operates over a dict parent attribute. + ContainerSubHandler holding dicts. See ComposedSubHandler documentation + for details. + """ - The dict attribute to handle and the class of objects that it contains can - be defined by calling the constructor or in a more declarative way as - class attributes, like: + @handler(u'List all the items by key') + def list(self): + r"list() -> tuple :: List all the item keys." + return self._attr().keys() - class TestHandler(DictSubHandler): - _cont_subhandler_attr = 'some_dict' - _cont_subhandler_class = SomeClass +class ComposedSubHandler(SubHandler): + r"""ComposedSubHandler(parent) -> ComposedSubHandler instance. + + This is a helper class to implement ListComposedSubHandler and + DictComposedSubHandler. You should not use it directly. + + This class is usefull when you have a parent that has a dict (cont) + that stores some object that has an attribute (attr) with a list or + a dict of objects of some class. In that case, this class provides + automated commands to add, update, delete, get and show that objects. + This commands takes the cont (key of the dict for the object holding + the attr), and an index for access the object itself (in the attr + list/dict). - This way, the parent's some_dict attribute (self.parent.some_dict) will be - managed automatically, providing the commands: add, update, delete, get, - list and show. New items will be instances of SomeClass, which should - provide a constructor with at least the key value, an as_tuple() method - and an update() method, if it should be possible to modify + The container object (cont) that holds a containers, the attribute of + that object that is the container itself, and the class of the objects + that it contains can be defined by calling the constructor or in a + more declarative way as class attributes, like: + + class TestHandler(ComposedSubHandler): + _comp_subhandler_cont = 'some_cont' + _comp_subhandler_attr = 'some_attr' + _comp_subhandler_class = SomeClass + + This way, the parent's some_cont attribute (self.parent.some_cont) + will be managed automatically, providing the commands: add, update, + delete, get and show for manipulating a particular instance that holds + of SomeClass. For example, updating an item at the index 5 is the same + (simplified) as doing parent.some_cont[cont][5].update(). + SomeClass should provide a cmp operator to see if the item is on the + container and an update() method, if it should be possible to modify it. If SomeClass has an _add, _update or _delete attribute, it set them to true when the item is added, updated or deleted respectively - (in case that it's deleted, it's not removed from the dict, but it's - not listed either). + (in case that it's deleted, it's not removed from the container, + but it's not listed either). If the container objects + (parent.some_cont[cont]) has an _update attribute, it's set to True + when any add, update or delete command is executed. + """ + + def __init__(self, parent, cont=None, attr=None, cls=None): + r"Initialize the object, see the class documentation for details." + self.parent = parent + if cont is not None: + self._comp_subhandler_cont = cont + if attr is not None: + self._comp_subhandler_attr = attr + if cls is not None: + self._comp_subhandler_class = cls + + def _cont(self): + return getattr(self.parent, self._comp_subhandler_cont) + + def _attr(self, cont, attr=None): + if attr is None: + return getattr(self._cont()[cont], self._comp_subhandler_attr) + setattr(self._cont()[cont], self._comp_subhandler_attr, attr) + + def _vattr(self, cont): + if isinstance(self._attr(cont), dict): + return dict([(k, i) for (k, i) in self._attr(cont).items() + if not hasattr(i, '_delete') or not i._delete]) + return [i for i in self._attr(cont) + if not hasattr(i, '_delete') or not i._delete] + + @handler(u'Add a new item') + def add(self, cont, *args, **kwargs): + r"add(cont, ...) -> None :: Add an item to the list." + if not cont in self._cont(): + raise ContainerNotFoundError(cont) + item = self._comp_subhandler_class(*args, **kwargs) + if hasattr(item, '_add'): + item._add = True + key = item + if isinstance(self._attr(cont), dict): + key = item.as_tuple()[0] + # do we have the same item? then raise an error + if key in self._vattr(cont): + raise ItemAlreadyExistsError(item) + # do we have the same item, but logically deleted? then update flags + if key in self._attr(cont): + index = key + if not isinstance(self._attr(cont), dict): + index = self._attr(cont).index(item) + if hasattr(item, '_add'): + self._attr(cont)[index]._add = False + if hasattr(item, '_delete'): + self._attr(cont)[index]._delete = False + else: # it's *really* new + if isinstance(self._attr(cont), dict): + self._attr(cont)[key] = item + else: + self._attr(cont).append(item) + if hasattr(self._cont()[cont], '_update'): + self._cont()[cont]._update = True + + @handler(u'Update an item') + def update(self, cont, index, *args, **kwargs): + r"update(cont, index, ...) -> None :: Update an item of the container." + # TODO make it right with metaclasses, so the method is not created + # unless the update() method really exists. + # TODO check if the modified item is the same of an existing one + if not cont in self._cont(): + raise ContainerNotFoundError(cont) + if not isinstance(self._attr(cont), dict): + index = int(index) # TODO validation + if not hasattr(self._comp_subhandler_class, 'update'): + raise CommandNotFoundError(('update',)) + try: + item = self._vattr(cont)[index] + item.update(*args, **kwargs) + if hasattr(item, '_update'): + item._update = True + if hasattr(self._cont()[cont], '_update'): + self._cont()[cont]._update = True + except LookupError: + raise ItemNotFoundError(index) + + @handler(u'Delete an item') + def delete(self, cont, index): + r"delete(cont, index) -> None :: Delete an item of the container." + if not cont in self._cont(): + raise ContainerNotFoundError(cont) + if not isinstance(self._attr(cont), dict): + index = int(index) # TODO validation + try: + item = self._vattr(cont)[index] + if hasattr(item, '_delete'): + item._delete = True + else: + del self._attr(cont)[index] + if hasattr(self._cont()[cont], '_update'): + self._cont()[cont]._update = True + return item + except LookupError: + raise ItemNotFoundError(index) + + @handler(u'Remove all items (use with care).') + def clear(self, cont): + r"clear(cont) -> None :: Delete all items of the container." + if not cont in self._cont(): + raise ContainerNotFoundError(cont) + if isinstance(self._attr(cont), dict): + self._attr(cont).clear() + else: + self._attr(cont, list()) + + @handler(u'Get information about an item') + def get(self, cont, index): + r"get(cont, index) -> item :: List all the information of an item." + if not cont in self._cont(): + raise ContainerNotFoundError(cont) + if not isinstance(self._attr(cont), dict): + index = int(index) # TODO validation + try: + return self._vattr(cont)[index] + except LookupError: + raise ItemNotFoundError(index) + + @handler(u'Get information about all items') + def show(self, cont): + r"show(cont) -> list of items :: List all the complete items information." + if not cont in self._cont(): + raise ContainerNotFoundError(cont) + if isinstance(self._attr(cont), dict): + return self._attr(cont).values() + return self._vattr(cont) + +class ListComposedSubHandler(ComposedSubHandler): + r"""ListComposedSubHandler(parent) -> ListComposedSubHandler instance. + + ComposedSubHandler holding lists. See ComposedSubHandler documentation + for details. + """ + + @handler(u'Get how many items are in the list') + def len(self, cont): + r"len(cont) -> int :: Get how many items are in the list." + if not cont in self._cont(): + raise ContainerNotFoundError(cont) + return len(self._vattr(cont)) + +class DictComposedSubHandler(ComposedSubHandler): + r"""DictComposedSubHandler(parent) -> DictComposedSubHandler instance. + + ComposedSubHandler holding dicts. See ComposedSubHandler documentation + for details. """ @handler(u'List all the items by key') - def list(self): - r"list() -> tuple :: List all the item keys." - return self._attr().keys() + def list(self, cont): + r"list(cont) -> tuple :: List all the item keys." + if not cont in self._cont(): + raise ContainerNotFoundError(cont) + return self._attr(cont).keys() if __name__ == '__main__': + import sys + # Execution tests class STestHandler1(ServiceHandler): _service_start = ('service', 'start') @@ -902,3 +1199,5 @@ if __name__ == '__main__': os.rmdir('templates') print + print get_network_devices() +