summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Component.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/Bcfg2/Component.py')
-rw-r--r--src/lib/Bcfg2/Component.py52
1 files changed, 18 insertions, 34 deletions
diff --git a/src/lib/Bcfg2/Component.py b/src/lib/Bcfg2/Component.py
index eb9ea166a..bb0e64102 100644
--- a/src/lib/Bcfg2/Component.py
+++ b/src/lib/Bcfg2/Component.py
@@ -6,6 +6,7 @@ import inspect
import logging
import os
import pydoc
+import socket
import sys
import time
import threading
@@ -59,12 +60,14 @@ def run_component(component_cls, listen_all, location, daemon, pidfile_name,
pidfile.close()
component = component_cls(cfile=cfile, **cls_kwargs)
- up = urlparse(location)
- port = tuple(up[1].split(':'))
- port = (port[0], int(port[1]))
+ hostname, port = urlparse(location)[1].split(':')
+ server_address = socket.getaddrinfo(hostname,
+ port,
+ socket.AF_UNSPEC,
+ socket.SOCK_STREAM)[0][4]
try:
server = XMLRPCServer(listen_all,
- port,
+ server_address,
keyfile=keyfile,
certfile=certfile,
register=register,
@@ -82,23 +85,6 @@ def run_component(component_cls, listen_all, location, daemon, pidfile_name,
server.server_close()
component.shutdown()
-def exposed(func):
- """Mark a method to be exposed publically.
-
- Examples:
- class MyComponent (Component):
- @expose
- def my_method (self, param1, param2):
- do_stuff()
-
- class MyComponent (Component):
- def my_method (self, param1, param2):
- do_stuff()
- my_method = expose(my_method)
-
- """
- func.exposed = True
- return func
def automatic(func, period=10):
"""Mark a method to be run periodically."""
@@ -150,6 +136,11 @@ class Component (object):
self.lock = threading.Lock()
self.instance_statistics = Statistics()
+ def critical_error(self, operation):
+ """Log and err, traceback and return an xmlrpc fault to client."""
+ logger.error(operation, exc_info=1)
+ raise xmlrpclib.Fault(xmlrpclib.APPLICATION_ERROR, "Critical unexpected failure: %s" % (operation))
+
def do_tasks(self):
"""Perform automatic tasks for the component.
@@ -213,7 +204,8 @@ class Component (object):
method_func = self._resolve_exposed_method(method)
except NoExposedMethod:
self.logger.error("Unknown method %s" % (method))
- raise xmlrpclib.Fault(7, "Unknown method %s" % method)
+ raise xmlrpclib.Fault(xmlrpclib.METHOD_NOT_FOUND,
+ "Unknown method %s" % method)
except Exception:
e = sys.exc_info()[1]
if getattr(e, "log", True):
@@ -246,14 +238,7 @@ class Component (object):
raise xmlrpclib.Fault(getattr(e, "fault_code", 1), str(e))
return result
- def listMethods(self):
- """Custom XML-RPC introspective method list."""
- return [
- name for name, func in inspect.getmembers(self, callable)
- if getattr(func, "exposed", False)
- ]
- listMethods = exposed(listMethods)
-
+ @exposed
def methodHelp(self, method_name):
"""Custom XML-RPC introspective method help.
@@ -266,19 +251,18 @@ class Component (object):
except NoExposedMethod:
return ""
return pydoc.getdoc(func)
- methodHelp = exposed(methodHelp)
+ @exposed
def get_name(self):
"""The name of the component."""
return self.name
- get_name = exposed(get_name)
+ @exposed
def get_implementation(self):
"""The implementation of the component."""
return self.implementation
- get_implementation = exposed(get_implementation)
+ @exposed
def get_statistics(self, _):
"""Get current statistics about component execution"""
return self.instance_statistics.display()
- get_statistics = exposed(get_statistics)