Loading ccnode/host/__init__.py +22 −1 Original line number Diff line number Diff line import logging import os.path from subprocess import Popen, PIPE, STDOUT from ccnode.node import DefaultHandler from ccnode.tags import tag_inspector from ccnode.tags import Tag, tag_inspector from ccnode.host import tags logger = logging.getLogger(__name__) def disk_tag_value(disk_name): def size(): s = open(os.path.join('/sys/block', disk_name, 'size')).read().strip() try: s = int(s) if s > 0: return s * 512 else: return None except ValueError: return None return size class Handler(DefaultHandler): """Handler for host role.""" def __init__(self, *args, **kwargs): Loading @@ -17,6 +33,11 @@ class Handler(DefaultHandler): for t in tag_inspector(tags): self.tags[t.name] = t # disk related tags for d in self.tags['disk'].value.split(): t = Tag('disk%s_size' % d, disk_tag_value(d), 3600) self.tags[t.name] = t def execute_command(self, command): """Execute an arbitrary shell command on the host. Loading ccnode/host/tags.py +0 −28 Original line number Diff line number Diff line Loading @@ -10,8 +10,6 @@ from socket import getfqdn import psutil from ccnode.tags import Tag, DynamicTags def h(): """Hostname tag.""" Loading Loading @@ -74,32 +72,6 @@ def disk(): '/sys/block/') if disk_pattern.match(d)) def _disk_tag_value(disk_name): def size(): s = open(os_.path.join('/sys/block', disk_name, 'size')).read().strip() try: s = int(s) if s > 0: return s * 512 else: return None except ValueError: return None return size class DynamicDisks(DynamicTags): def __init__(self): DynamicTags.__init__(self) for d in disk().split(): self.tags.append(Tag(u'disk%s_size' % d, _disk_tag_value(d), 3600)) disks_size = DynamicDisks() # other hardware related tags def chaserial(): """Blade chassis serial number.""" Loading ccnode/tags.py +0 −52 Original line number Diff line number Diff line Loading @@ -50,48 +50,6 @@ class Tag(object): return None class DynamicTags(object): """Set tags dynamicaly. Tags must be defined in the constructor. Example use: * first subclass :class:`DynamicTags`: .. code-block:: python class Disks(DynamicTags): def __init__(self): # find disk by calling an arbitrary function disks = find_disks() # for each disk associate a function that calculate # the size for d in disks: # arbitrary example: for each disk set a tag # with the disk name and the size as value with # a ttl of one hour self.tags.append( Tag(d, find_disk_size(d), 3600), ) # we're done :) * we now just need to instanciate our new ``class``: .. code-block:: python disks_size = Disks() * the tags are automaticaly found using :func:`tag_inspector` """ def __init__(self): #: iterable containing :class:`Tag` objects (``list`` by default) self.tags = [] def iter_tags(self): for t in self.tags: yield t def tag_inspector(mod, parent=None): """Inspect module to find tags. Loading @@ -104,10 +62,6 @@ def tag_inspector(mod, parent=None): * define a function that returns a value as a string or None (meaning the tag doesn't exist on the host), as you guessed the function name will define the tag name * subclass :class:`DynamicTags` and create an instance. The instance must have an attribute ``tags`` which must be an **iterable** of :class:`Tag` objects (by default it is a ``list``). The name of the instance isn't meaningfull. """ tags = [] for n, m in inspect.getmembers(mod): # (name, member) Loading @@ -121,12 +75,6 @@ def tag_inspector(mod, parent=None): elif inspect.isfunction(m): # if function take function ttl argument or set -1 by default ttl = getattr(m, 'ttl', -1) elif isinstance(m, DynamicTags): for t in m.iter_tags(): tags.append(t) logger.debug('Introspected %s with ttl %s.' % (t.name, t.ttl)) continue else: # whatever it is we don't care... continue Loading Loading
ccnode/host/__init__.py +22 −1 Original line number Diff line number Diff line import logging import os.path from subprocess import Popen, PIPE, STDOUT from ccnode.node import DefaultHandler from ccnode.tags import tag_inspector from ccnode.tags import Tag, tag_inspector from ccnode.host import tags logger = logging.getLogger(__name__) def disk_tag_value(disk_name): def size(): s = open(os.path.join('/sys/block', disk_name, 'size')).read().strip() try: s = int(s) if s > 0: return s * 512 else: return None except ValueError: return None return size class Handler(DefaultHandler): """Handler for host role.""" def __init__(self, *args, **kwargs): Loading @@ -17,6 +33,11 @@ class Handler(DefaultHandler): for t in tag_inspector(tags): self.tags[t.name] = t # disk related tags for d in self.tags['disk'].value.split(): t = Tag('disk%s_size' % d, disk_tag_value(d), 3600) self.tags[t.name] = t def execute_command(self, command): """Execute an arbitrary shell command on the host. Loading
ccnode/host/tags.py +0 −28 Original line number Diff line number Diff line Loading @@ -10,8 +10,6 @@ from socket import getfqdn import psutil from ccnode.tags import Tag, DynamicTags def h(): """Hostname tag.""" Loading Loading @@ -74,32 +72,6 @@ def disk(): '/sys/block/') if disk_pattern.match(d)) def _disk_tag_value(disk_name): def size(): s = open(os_.path.join('/sys/block', disk_name, 'size')).read().strip() try: s = int(s) if s > 0: return s * 512 else: return None except ValueError: return None return size class DynamicDisks(DynamicTags): def __init__(self): DynamicTags.__init__(self) for d in disk().split(): self.tags.append(Tag(u'disk%s_size' % d, _disk_tag_value(d), 3600)) disks_size = DynamicDisks() # other hardware related tags def chaserial(): """Blade chassis serial number.""" Loading
ccnode/tags.py +0 −52 Original line number Diff line number Diff line Loading @@ -50,48 +50,6 @@ class Tag(object): return None class DynamicTags(object): """Set tags dynamicaly. Tags must be defined in the constructor. Example use: * first subclass :class:`DynamicTags`: .. code-block:: python class Disks(DynamicTags): def __init__(self): # find disk by calling an arbitrary function disks = find_disks() # for each disk associate a function that calculate # the size for d in disks: # arbitrary example: for each disk set a tag # with the disk name and the size as value with # a ttl of one hour self.tags.append( Tag(d, find_disk_size(d), 3600), ) # we're done :) * we now just need to instanciate our new ``class``: .. code-block:: python disks_size = Disks() * the tags are automaticaly found using :func:`tag_inspector` """ def __init__(self): #: iterable containing :class:`Tag` objects (``list`` by default) self.tags = [] def iter_tags(self): for t in self.tags: yield t def tag_inspector(mod, parent=None): """Inspect module to find tags. Loading @@ -104,10 +62,6 @@ def tag_inspector(mod, parent=None): * define a function that returns a value as a string or None (meaning the tag doesn't exist on the host), as you guessed the function name will define the tag name * subclass :class:`DynamicTags` and create an instance. The instance must have an attribute ``tags`` which must be an **iterable** of :class:`Tag` objects (by default it is a ``list``). The name of the instance isn't meaningfull. """ tags = [] for n, m in inspect.getmembers(mod): # (name, member) Loading @@ -121,12 +75,6 @@ def tag_inspector(mod, parent=None): elif inspect.isfunction(m): # if function take function ttl argument or set -1 by default ttl = getattr(m, 'ttl', -1) elif isinstance(m, DynamicTags): for t in m.iter_tags(): tags.append(t) logger.debug('Introspected %s with ttl %s.' % (t.name, t.ttl)) continue else: # whatever it is we don't care... continue Loading