diff --git a/muranoagent/app.py b/muranoagent/app.py index 21507813..adea26e3 100644 --- a/muranoagent/app.py +++ b/muranoagent/app.py @@ -13,29 +13,32 @@ # See the License for the specific language governing permissions and # limitations under the License. -import win32 -import sys import os -from execution_plan_runner import ExecutionPlanRunner -from execution_plan_queue import ExecutionPlanQueue -from execution_result import ExecutionResult -from openstack.common import log as logging -from openstack.common import service -from muranoagent.common.config import CONF -from muranoagent.common.messaging import MqClient, Message -from exceptions import AgentException -from time import sleep -from bunch import Bunch -import semver +import sys +import time import types +import bunch +import semver + +from muranoagent.common import config +from muranoagent.common import messaging +from muranoagent import exceptions as exc +from muranoagent import execution_plan_queue +from muranoagent import execution_plan_runner +from muranoagent import execution_result as ex_result +from muranoagent.openstack.common import log as logging +from muranoagent.openstack.common import service + +CONF = config.CONF + LOG = logging.getLogger(__name__) format_version = '2.0.0' class MuranoAgent(service.Service): def __init__(self): - self._queue = ExecutionPlanQueue() + self._queue = execution_plan_queue.ExecutionPlanQueue() super(MuranoAgent, self).__init__() @staticmethod @@ -63,7 +66,7 @@ class MuranoAgent(service.Service): self._loop_func(msg_iterator) except Exception as ex: LOG.exception(ex) - sleep(5) + time.sleep(5) def _loop_func(self, msg_iterator): result, timestamp = self._queue.get_execution_plan_result() @@ -81,18 +84,20 @@ class MuranoAgent(service.Service): msg_iterator.next() def _run(self, plan): - with ExecutionPlanRunner(plan) as runner: + with execution_plan_runner.ExecutionPlanRunner(plan) as runner: try: result = runner.run() - execution_result = ExecutionResult.from_result(result, plan) + execution_result = ex_result.ExecutionResult.from_result( + result, plan) self._queue.put_execution_result(execution_result, plan) - except Exception, ex: - execution_result = ExecutionResult.from_error(ex, plan) + except Exception as ex: + execution_result = ex_result.ExecutionResult.from_error(ex, + plan) self._queue.put_execution_result(execution_result, plan) def _send_result(self, result): with self._create_rmq_client() as mq: - msg = Message() + msg = messaging.Message() msg.body = result msg.id = result.get('SourceID') mq.send(message=msg, @@ -111,7 +116,7 @@ class MuranoAgent(service.Service): 'ssl': rabbitmq.ssl, 'ca_certs': rabbitmq.ca_certs.strip() or None } - return MqClient(**connection_params) + return messaging.MqClient(**connection_params) def _wait_plan(self): delay = 5 @@ -133,11 +138,11 @@ class MuranoAgent(service.Service): break except Exception: LOG.warn('Communication error', exc_info=True) - sleep(delay) + time.sleep(delay) delay = min(delay * 1.2, 60) def _handle_message(self, msg): - print msg.body + print(msg.body) if 'ID' not in msg.body and msg.id: msg.body['ID'] = msg.id err = self._verify_plan(msg.body) @@ -145,8 +150,8 @@ class MuranoAgent(service.Service): self._queue.put_execution_plan(msg.body) else: try: - execution_result = ExecutionResult.from_error( - err, Bunch(msg.body)) + execution_result = ex_result.ExecutionResult.from_error( + err, bunch.Bunch(msg.body)) self._send_result(execution_result) except ValueError: @@ -159,7 +164,7 @@ class MuranoAgent(service.Service): range_str = 'in range 2.0.0-{0}'.format(plan_format_version) \ if format_version != '2.0.0' \ else 'equal to {0}'.format(format_version) - return AgentException( + return exc.AgentException( 3, 'Unsupported format version {0} (must be {1})'.format( plan_format_version, range_str)) @@ -167,40 +172,40 @@ class MuranoAgent(service.Service): for attr in ('Scripts', 'Files', 'Options'): if attr is plan and not isinstance( plan[attr], types.DictionaryType): - return AgentException( + return exc.AgentException( 2, '{0} is not a dictionary'.format(attr)) for name, script in plan.get('Scripts', {}).items(): for attr in ('Type', 'EntryPoint'): if attr not in script or not isinstance( script[attr], types.StringTypes): - return AgentException( + return exc.AgentException( 2, 'Incorrect {0} entry in script {1}'.format( attr, name)) if not isinstance(script.get('Options', {}), types.DictionaryType): - return AgentException( + return exc.AgentException( 2, 'Incorrect Options entry in script {0}'.format(name)) if script['EntryPoint'] not in plan.get('Files', {}): - return AgentException( + return exc.AgentException( 2, 'Script {0} misses entry point {1}'.format( name, script['EntryPoint'])) for additional_file in script.get('Files', []): if additional_file not in plan.get('Files', {}): - return AgentException( + return exc.AgentException( 2, 'Script {0} misses file {1}'.format( name, additional_file)) for key, plan_file in plan.get('Files', {}).items(): for attr in ('BodyType', 'Body', 'Name'): if attr not in plan_file: - return AgentException( + return exc.AgentException( 2, 'Incorrect {0} entry in file {1}'.format( attr, key)) if plan_file['BodyType'] not in ('Text', 'Base64'): - return AgentException( + return exc.AgentException( 2, 'Incorrect BodyType in file {1}'.format(key)) return None diff --git a/muranoagent/cmd/run.py b/muranoagent/cmd/run.py index db5a42cb..8a304045 100644 --- a/muranoagent/cmd/run.py +++ b/muranoagent/cmd/run.py @@ -13,8 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys import os +import sys + # If ../muranoagent/__init__.py exists, add ../ to Python search path, so # it will override what happens to be installed in /usr/(local/)lib/python... @@ -27,10 +28,10 @@ if os.path.exists(os.path.join(possible_topdir, '__init__.py')): sys.path.insert(0, possible_topdir) +from muranoagent import app from muranoagent.common import config from muranoagent.openstack.common import log from muranoagent.openstack.common import service -from muranoagent.app import MuranoAgent def main(): @@ -38,9 +39,9 @@ def main(): config.parse_args() log.setup('muranoagent') launcher = service.ServiceLauncher() - launcher.launch_service(MuranoAgent()) + launcher.launch_service(app.MuranoAgent()) launcher.wait() - except RuntimeError, e: + except RuntimeError as e: sys.stderr.write("ERROR: %s\n" % e) sys.exit(1) diff --git a/muranoagent/common/messaging/__init__.py b/muranoagent/common/messaging/__init__.py index 2f85c16c..d0056b82 100644 --- a/muranoagent/common/messaging/__init__.py +++ b/muranoagent/common/messaging/__init__.py @@ -13,8 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from message import Message -from subscription import Subscription -from mqclient import MqClient +from muranoagent.common.messaging.message import Message # noqa +from muranoagent.common.messaging.mqclient import MqClient # noqa +from muranoagent.common.messaging.subscription import Subscription # noqa __all__ = ['Message', 'Subscription', 'MqClient'] diff --git a/muranoagent/common/messaging/subscription.py b/muranoagent/common/messaging/subscription.py index 340cc699..c9e4d0bc 100644 --- a/muranoagent/common/messaging/subscription.py +++ b/muranoagent/common/messaging/subscription.py @@ -19,7 +19,7 @@ import time from eventlet import patcher kombu = patcher.import_patched('kombu') -from . import message +from muranoagent.common.messaging import message class Subscription(object): diff --git a/muranoagent/execution_plan_queue.py b/muranoagent/execution_plan_queue.py index dba9d683..52767631 100644 --- a/muranoagent/execution_plan_queue.py +++ b/muranoagent/execution_plan_queue.py @@ -17,8 +17,12 @@ import json import os import shutil import time -from bunch import Bunch -from muranoagent.common.config import CONF + +import bunch + +from muranoagent.common import config + +CONF = config.CONF class ExecutionPlanQueue(object): @@ -65,7 +69,7 @@ class ExecutionPlanQueue(object): if ep is None: return None ep['_timestamp'] = timestamp - return Bunch(ep) + return bunch.Bunch(ep) def put_execution_result(self, result, execution_plan): timestamp = execution_plan['_timestamp'] diff --git a/muranoagent/execution_plan_runner.py b/muranoagent/execution_plan_runner.py index a2a56167..54d87c77 100644 --- a/muranoagent/execution_plan_runner.py +++ b/muranoagent/execution_plan_runner.py @@ -14,9 +14,11 @@ # limitations under the License. import sys -from bunch import Bunch -from files_manager import FilesManager -from script_runner import ScriptRunner + +import bunch + +from muranoagent import files_manager as fm +from muranoagent import script_runner class ExecutionPlanRunner(object): @@ -24,12 +26,12 @@ class ExecutionPlanRunner(object): self._execution_plan = execution_plan self._main_script = self._prepare_script(execution_plan.Body) self._script_funcs = {} - self._files_manager = FilesManager(execution_plan) + self._files_manager = fm.FilesManager(execution_plan) self._prepare_executors(execution_plan) def run(self): script_globals = { - "args": Bunch(self._execution_plan.get('Parameters') or {}) + "args": bunch.Bunch(self._execution_plan.get('Parameters') or {}) } script_globals.update(self._script_funcs) exec self._main_script in script_globals @@ -54,8 +56,8 @@ class ExecutionPlanRunner(object): def _prepare_executors(self, execution_plan): for key, value in execution_plan.Scripts.items(): - self._script_funcs[key] = ScriptRunner( - key, Bunch(value), self._files_manager) + self._script_funcs[key] = script_runner.ScriptRunner( + key, bunch.Bunch(value), self._files_manager) @staticmethod def _prepare_script(body): diff --git a/muranoagent/execution_result.py b/muranoagent/execution_result.py index fe7454c8..97ee8f42 100644 --- a/muranoagent/execution_result.py +++ b/muranoagent/execution_result.py @@ -13,9 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -from exceptions import AgentException import uuid -from openstack.common import timeutils + +from muranoagent import exceptions as exc +from muranoagent.openstack.common import timeutils class ExecutionResult(object): @@ -46,7 +47,7 @@ class ExecutionResult(object): error_code = error elif isinstance(error, Exception): message = error.message - if isinstance(error, AgentException): + if isinstance(error, exc.AgentException): error_code = error.error_code additional_info = error.additional_data diff --git a/muranoagent/executors/__init__.py b/muranoagent/executors/__init__.py index 662400ec..f26edf38 100644 --- a/muranoagent/executors/__init__.py +++ b/muranoagent/executors/__init__.py @@ -13,8 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from functools import wraps - class ExecutorsRepo(object): def __init__(self): diff --git a/muranoagent/executors/application/__init__.py b/muranoagent/executors/application/__init__.py index ae92ee6b..3d2874a1 100644 --- a/muranoagent/executors/application/__init__.py +++ b/muranoagent/executors/application/__init__.py @@ -18,15 +18,16 @@ import stat import subprocess import sys -from muranoagent.openstack.common import log as logging -from muranoagent.executors import executor +import bunch + import muranoagent.exceptions -from bunch import Bunch +from muranoagent import executors +from muranoagent.openstack.common import log as logging LOG = logging.getLogger(__name__) -@executor('Application') +@executors.executor('Application') class ApplicationExecutor(object): def __init__(self, name): self._name = name @@ -79,4 +80,4 @@ class ApplicationExecutor(object): message='Script {0} returned error code'.format(self._name), additional_data=result) - return Bunch(result) + return bunch.Bunch(result) diff --git a/muranoagent/files_manager.py b/muranoagent/files_manager.py index 3683f727..cc55fa08 100644 --- a/muranoagent/files_manager.py +++ b/muranoagent/files_manager.py @@ -13,10 +13,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import base64 +import os import shutil -from muranoagent.common.config import CONF + +from muranoagent.common import config + +CONF = config.CONF class FilesManager(object): diff --git a/muranoagent/script_runner.py b/muranoagent/script_runner.py index f6236030..0bd54d79 100644 --- a/muranoagent/script_runner.py +++ b/muranoagent/script_runner.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from muranoagent.executors import Executors +from muranoagent import executors as exe class FunctionRunner(object): @@ -29,7 +29,7 @@ class FunctionRunner(object): class ScriptRunner(object): def __init__(self, name, script_info, files_manager): self._name = name - self._executor = Executors.create_executor(script_info.Type, name) + self._executor = exe.Executors.create_executor(script_info.Type, name) self._script_info = script_info self._script_loaded = False self._files_manager = files_manager diff --git a/muranoagent/win32.py b/muranoagent/win32.py index 54ed3101..1c6626e2 100644 --- a/muranoagent/win32.py +++ b/muranoagent/win32.py @@ -14,8 +14,8 @@ # limitations under the License. try: - import win32file import os + import win32file def symlink(source, link_name): src = os.path.abspath(source) diff --git a/tox.ini b/tox.ini index 7a3f1160..7c7fc569 100644 --- a/tox.ini +++ b/tox.ini @@ -35,7 +35,7 @@ commands = flake8 [flake8] # H301 one import per line # H302 import only modules -ignore = H231,H233,H301,H302,H306,H404,F401 +ignore = H404,F401 show-source = true builtins = _ exclude=.venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,tools