Enable H301, H302, H304, H306, H231, H233

Fix the following issues:
* Python 3.x compatibility
* Module imports

Change-Id: Ie6bfa1c3eb1494da3b9222b162a40a1181e41188
This commit is contained in:
Ekaterina Fedorova 2014-08-25 17:20:03 +04:00 committed by Ekaterina Chernova
parent dbe4eb5fa8
commit 218a51ece1
13 changed files with 82 additions and 67 deletions

View File

@ -13,29 +13,32 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import win32
import sys
import os import os
from execution_plan_runner import ExecutionPlanRunner import sys
from execution_plan_queue import ExecutionPlanQueue import time
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 types 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__) LOG = logging.getLogger(__name__)
format_version = '2.0.0' format_version = '2.0.0'
class MuranoAgent(service.Service): class MuranoAgent(service.Service):
def __init__(self): def __init__(self):
self._queue = ExecutionPlanQueue() self._queue = execution_plan_queue.ExecutionPlanQueue()
super(MuranoAgent, self).__init__() super(MuranoAgent, self).__init__()
@staticmethod @staticmethod
@ -63,7 +66,7 @@ class MuranoAgent(service.Service):
self._loop_func(msg_iterator) self._loop_func(msg_iterator)
except Exception as ex: except Exception as ex:
LOG.exception(ex) LOG.exception(ex)
sleep(5) time.sleep(5)
def _loop_func(self, msg_iterator): def _loop_func(self, msg_iterator):
result, timestamp = self._queue.get_execution_plan_result() result, timestamp = self._queue.get_execution_plan_result()
@ -81,18 +84,20 @@ class MuranoAgent(service.Service):
msg_iterator.next() msg_iterator.next()
def _run(self, plan): def _run(self, plan):
with ExecutionPlanRunner(plan) as runner: with execution_plan_runner.ExecutionPlanRunner(plan) as runner:
try: try:
result = runner.run() 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) self._queue.put_execution_result(execution_result, plan)
except Exception, ex: except Exception as ex:
execution_result = ExecutionResult.from_error(ex, plan) execution_result = ex_result.ExecutionResult.from_error(ex,
plan)
self._queue.put_execution_result(execution_result, plan) self._queue.put_execution_result(execution_result, plan)
def _send_result(self, result): def _send_result(self, result):
with self._create_rmq_client() as mq: with self._create_rmq_client() as mq:
msg = Message() msg = messaging.Message()
msg.body = result msg.body = result
msg.id = result.get('SourceID') msg.id = result.get('SourceID')
mq.send(message=msg, mq.send(message=msg,
@ -111,7 +116,7 @@ class MuranoAgent(service.Service):
'ssl': rabbitmq.ssl, 'ssl': rabbitmq.ssl,
'ca_certs': rabbitmq.ca_certs.strip() or None 'ca_certs': rabbitmq.ca_certs.strip() or None
} }
return MqClient(**connection_params) return messaging.MqClient(**connection_params)
def _wait_plan(self): def _wait_plan(self):
delay = 5 delay = 5
@ -133,11 +138,11 @@ class MuranoAgent(service.Service):
break break
except Exception: except Exception:
LOG.warn('Communication error', exc_info=True) LOG.warn('Communication error', exc_info=True)
sleep(delay) time.sleep(delay)
delay = min(delay * 1.2, 60) delay = min(delay * 1.2, 60)
def _handle_message(self, msg): def _handle_message(self, msg):
print msg.body print(msg.body)
if 'ID' not in msg.body and msg.id: if 'ID' not in msg.body and msg.id:
msg.body['ID'] = msg.id msg.body['ID'] = msg.id
err = self._verify_plan(msg.body) err = self._verify_plan(msg.body)
@ -145,8 +150,8 @@ class MuranoAgent(service.Service):
self._queue.put_execution_plan(msg.body) self._queue.put_execution_plan(msg.body)
else: else:
try: try:
execution_result = ExecutionResult.from_error( execution_result = ex_result.ExecutionResult.from_error(
err, Bunch(msg.body)) err, bunch.Bunch(msg.body))
self._send_result(execution_result) self._send_result(execution_result)
except ValueError: except ValueError:
@ -159,7 +164,7 @@ class MuranoAgent(service.Service):
range_str = 'in range 2.0.0-{0}'.format(plan_format_version) \ range_str = 'in range 2.0.0-{0}'.format(plan_format_version) \
if format_version != '2.0.0' \ if format_version != '2.0.0' \
else 'equal to {0}'.format(format_version) else 'equal to {0}'.format(format_version)
return AgentException( return exc.AgentException(
3, 3,
'Unsupported format version {0} (must be {1})'.format( 'Unsupported format version {0} (must be {1})'.format(
plan_format_version, range_str)) plan_format_version, range_str))
@ -167,40 +172,40 @@ class MuranoAgent(service.Service):
for attr in ('Scripts', 'Files', 'Options'): for attr in ('Scripts', 'Files', 'Options'):
if attr is plan and not isinstance( if attr is plan and not isinstance(
plan[attr], types.DictionaryType): plan[attr], types.DictionaryType):
return AgentException( return exc.AgentException(
2, '{0} is not a dictionary'.format(attr)) 2, '{0} is not a dictionary'.format(attr))
for name, script in plan.get('Scripts', {}).items(): for name, script in plan.get('Scripts', {}).items():
for attr in ('Type', 'EntryPoint'): for attr in ('Type', 'EntryPoint'):
if attr not in script or not isinstance( if attr not in script or not isinstance(
script[attr], types.StringTypes): script[attr], types.StringTypes):
return AgentException( return exc.AgentException(
2, 'Incorrect {0} entry in script {1}'.format( 2, 'Incorrect {0} entry in script {1}'.format(
attr, name)) attr, name))
if not isinstance(script.get('Options', {}), types.DictionaryType): if not isinstance(script.get('Options', {}), types.DictionaryType):
return AgentException( return exc.AgentException(
2, 'Incorrect Options entry in script {0}'.format(name)) 2, 'Incorrect Options entry in script {0}'.format(name))
if script['EntryPoint'] not in plan.get('Files', {}): if script['EntryPoint'] not in plan.get('Files', {}):
return AgentException( return exc.AgentException(
2, 'Script {0} misses entry point {1}'.format( 2, 'Script {0} misses entry point {1}'.format(
name, script['EntryPoint'])) name, script['EntryPoint']))
for additional_file in script.get('Files', []): for additional_file in script.get('Files', []):
if additional_file not in plan.get('Files', {}): if additional_file not in plan.get('Files', {}):
return AgentException( return exc.AgentException(
2, 'Script {0} misses file {1}'.format( 2, 'Script {0} misses file {1}'.format(
name, additional_file)) name, additional_file))
for key, plan_file in plan.get('Files', {}).items(): for key, plan_file in plan.get('Files', {}).items():
for attr in ('BodyType', 'Body', 'Name'): for attr in ('BodyType', 'Body', 'Name'):
if attr not in plan_file: if attr not in plan_file:
return AgentException( return exc.AgentException(
2, 'Incorrect {0} entry in file {1}'.format( 2, 'Incorrect {0} entry in file {1}'.format(
attr, key)) attr, key))
if plan_file['BodyType'] not in ('Text', 'Base64'): if plan_file['BodyType'] not in ('Text', 'Base64'):
return AgentException( return exc.AgentException(
2, 'Incorrect BodyType in file {1}'.format(key)) 2, 'Incorrect BodyType in file {1}'.format(key))
return None return None

View File

@ -13,8 +13,9 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import sys
import os import os
import sys
# If ../muranoagent/__init__.py exists, add ../ to Python search path, so # If ../muranoagent/__init__.py exists, add ../ to Python search path, so
# it will override what happens to be installed in /usr/(local/)lib/python... # 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')): '__init__.py')):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)
from muranoagent import app
from muranoagent.common import config from muranoagent.common import config
from muranoagent.openstack.common import log from muranoagent.openstack.common import log
from muranoagent.openstack.common import service from muranoagent.openstack.common import service
from muranoagent.app import MuranoAgent
def main(): def main():
@ -38,9 +39,9 @@ def main():
config.parse_args() config.parse_args()
log.setup('muranoagent') log.setup('muranoagent')
launcher = service.ServiceLauncher() launcher = service.ServiceLauncher()
launcher.launch_service(MuranoAgent()) launcher.launch_service(app.MuranoAgent())
launcher.wait() launcher.wait()
except RuntimeError, e: except RuntimeError as e:
sys.stderr.write("ERROR: %s\n" % e) sys.stderr.write("ERROR: %s\n" % e)
sys.exit(1) sys.exit(1)

View File

@ -13,8 +13,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from message import Message from muranoagent.common.messaging.message import Message # noqa
from subscription import Subscription from muranoagent.common.messaging.mqclient import MqClient # noqa
from mqclient import MqClient from muranoagent.common.messaging.subscription import Subscription # noqa
__all__ = ['Message', 'Subscription', 'MqClient'] __all__ = ['Message', 'Subscription', 'MqClient']

View File

@ -19,7 +19,7 @@ import time
from eventlet import patcher from eventlet import patcher
kombu = patcher.import_patched('kombu') kombu = patcher.import_patched('kombu')
from . import message from muranoagent.common.messaging import message
class Subscription(object): class Subscription(object):

View File

@ -17,8 +17,12 @@ import json
import os import os
import shutil import shutil
import time 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): class ExecutionPlanQueue(object):
@ -65,7 +69,7 @@ class ExecutionPlanQueue(object):
if ep is None: if ep is None:
return None return None
ep['_timestamp'] = timestamp ep['_timestamp'] = timestamp
return Bunch(ep) return bunch.Bunch(ep)
def put_execution_result(self, result, execution_plan): def put_execution_result(self, result, execution_plan):
timestamp = execution_plan['_timestamp'] timestamp = execution_plan['_timestamp']

View File

@ -14,9 +14,11 @@
# limitations under the License. # limitations under the License.
import sys import sys
from bunch import Bunch
from files_manager import FilesManager import bunch
from script_runner import ScriptRunner
from muranoagent import files_manager as fm
from muranoagent import script_runner
class ExecutionPlanRunner(object): class ExecutionPlanRunner(object):
@ -24,12 +26,12 @@ class ExecutionPlanRunner(object):
self._execution_plan = execution_plan self._execution_plan = execution_plan
self._main_script = self._prepare_script(execution_plan.Body) self._main_script = self._prepare_script(execution_plan.Body)
self._script_funcs = {} self._script_funcs = {}
self._files_manager = FilesManager(execution_plan) self._files_manager = fm.FilesManager(execution_plan)
self._prepare_executors(execution_plan) self._prepare_executors(execution_plan)
def run(self): def run(self):
script_globals = { 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) script_globals.update(self._script_funcs)
exec self._main_script in script_globals exec self._main_script in script_globals
@ -54,8 +56,8 @@ class ExecutionPlanRunner(object):
def _prepare_executors(self, execution_plan): def _prepare_executors(self, execution_plan):
for key, value in execution_plan.Scripts.items(): for key, value in execution_plan.Scripts.items():
self._script_funcs[key] = ScriptRunner( self._script_funcs[key] = script_runner.ScriptRunner(
key, Bunch(value), self._files_manager) key, bunch.Bunch(value), self._files_manager)
@staticmethod @staticmethod
def _prepare_script(body): def _prepare_script(body):

View File

@ -13,9 +13,10 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from exceptions import AgentException
import uuid import uuid
from openstack.common import timeutils
from muranoagent import exceptions as exc
from muranoagent.openstack.common import timeutils
class ExecutionResult(object): class ExecutionResult(object):
@ -46,7 +47,7 @@ class ExecutionResult(object):
error_code = error error_code = error
elif isinstance(error, Exception): elif isinstance(error, Exception):
message = error.message message = error.message
if isinstance(error, AgentException): if isinstance(error, exc.AgentException):
error_code = error.error_code error_code = error.error_code
additional_info = error.additional_data additional_info = error.additional_data

View File

@ -13,8 +13,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from functools import wraps
class ExecutorsRepo(object): class ExecutorsRepo(object):
def __init__(self): def __init__(self):

View File

@ -18,15 +18,16 @@ import stat
import subprocess import subprocess
import sys import sys
from muranoagent.openstack.common import log as logging import bunch
from muranoagent.executors import executor
import muranoagent.exceptions import muranoagent.exceptions
from bunch import Bunch from muranoagent import executors
from muranoagent.openstack.common import log as logging
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@executor('Application') @executors.executor('Application')
class ApplicationExecutor(object): class ApplicationExecutor(object):
def __init__(self, name): def __init__(self, name):
self._name = name self._name = name
@ -79,4 +80,4 @@ class ApplicationExecutor(object):
message='Script {0} returned error code'.format(self._name), message='Script {0} returned error code'.format(self._name),
additional_data=result) additional_data=result)
return Bunch(result) return bunch.Bunch(result)

View File

@ -13,10 +13,13 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import os
import base64 import base64
import os
import shutil import shutil
from muranoagent.common.config import CONF
from muranoagent.common import config
CONF = config.CONF
class FilesManager(object): class FilesManager(object):

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from muranoagent.executors import Executors from muranoagent import executors as exe
class FunctionRunner(object): class FunctionRunner(object):
@ -29,7 +29,7 @@ class FunctionRunner(object):
class ScriptRunner(object): class ScriptRunner(object):
def __init__(self, name, script_info, files_manager): def __init__(self, name, script_info, files_manager):
self._name = name 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_info = script_info
self._script_loaded = False self._script_loaded = False
self._files_manager = files_manager self._files_manager = files_manager

View File

@ -14,8 +14,8 @@
# limitations under the License. # limitations under the License.
try: try:
import win32file
import os import os
import win32file
def symlink(source, link_name): def symlink(source, link_name):
src = os.path.abspath(source) src = os.path.abspath(source)

View File

@ -35,7 +35,7 @@ commands = flake8
[flake8] [flake8]
# H301 one import per line # H301 one import per line
# H302 import only modules # H302 import only modules
ignore = H231,H233,H301,H302,H306,H404,F401 ignore = H404,F401
show-source = true show-source = true
builtins = _ builtins = _
exclude=.venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,tools exclude=.venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,tools