Retire stackforge/fuel-provision
This commit is contained in:
15
.gitignore
vendored
15
.gitignore
vendored
@@ -1,15 +0,0 @@
|
|||||||
*.pyc
|
|
||||||
|
|
||||||
# vim swap files
|
|
||||||
.*.swp
|
|
||||||
|
|
||||||
# services' runtime files
|
|
||||||
*.log
|
|
||||||
*.pid
|
|
||||||
|
|
||||||
.idea/
|
|
||||||
.DS_Store
|
|
||||||
|
|
||||||
*.egg-info
|
|
||||||
|
|
||||||
draft/
|
|
||||||
14
LICENSE
14
LICENSE
@@ -1,14 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
This project is no longer maintained.
|
||||||
|
|
||||||
|
The contents of this repository are still available in the Git source code
|
||||||
|
management system. To see the contents of this repository before it reached
|
||||||
|
its end of life, please check out the previous commit with
|
||||||
|
"git checkout HEAD^1".
|
||||||
|
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
|
|
||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import os
|
|
||||||
|
|
||||||
here = os.path.abspath(os.path.dirname(__file__))
|
|
||||||
sys.path[:0] = [os.path.join(here, '..')]
|
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
from galloper.cmd.api import run
|
|
||||||
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF([], project='galloper',
|
|
||||||
default_config_files=[os.path.join(here, '../etc/galloper/galloper-api.conf.example')])
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
run()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
|
|
||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import os
|
|
||||||
|
|
||||||
here = os.path.abspath(os.path.dirname(__file__))
|
|
||||||
sys.path[:0] = [os.path.join(here, "..")]
|
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
from galloper.cmd.cli import run
|
|
||||||
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF([], project='galloper',
|
|
||||||
default_config_files=[os.path.join(here, '../etc/galloper/galloper-cli.conf.example')])
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
run()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2011 OpenStack Foundation
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
import eventlet
|
|
||||||
eventlet.monkey_patch()
|
|
||||||
|
|
||||||
import contextlib
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
|
|
||||||
# If ../galloper/__init__.py exists, add ../ to Python search path, so that
|
|
||||||
# it will override what happens to be installed in /usr/(local/)lib/python...
|
|
||||||
POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
|
|
||||||
os.pardir,
|
|
||||||
os.pardir))
|
|
||||||
if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'galloper', '__init__.py')):
|
|
||||||
sys.path.insert(0, POSSIBLE_TOPDIR)
|
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
from galloper.openstack.common import rpc
|
|
||||||
from galloper.openstack.common.rpc import impl_zmq
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF.register_opts(rpc.rpc_opts)
|
|
||||||
CONF.register_opts(impl_zmq.zmq_opts)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
CONF(sys.argv[1:], project='galloper')
|
|
||||||
logging.setup("galloper")
|
|
||||||
|
|
||||||
with contextlib.closing(impl_zmq.ZmqProxy(CONF)) as reactor:
|
|
||||||
reactor.consume_in_thread()
|
|
||||||
reactor.wait()
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
[DEFAULT]
|
|
||||||
galloper_api_host = 0.0.0.0
|
|
||||||
galloper_api_port = 9999
|
|
||||||
|
|
||||||
debug=true
|
|
||||||
verbose=true
|
|
||||||
log_file = /var/tmp/galloper-api.log
|
|
||||||
use_stderr=false
|
|
||||||
|
|
||||||
[database]
|
|
||||||
backend=sqlalchemy
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
[DEFAULT]
|
|
||||||
galloper_api_host = 0.0.0.0
|
|
||||||
galloper_api_port = 9999
|
|
||||||
galloper_cli_user_agent = galloper-cli
|
|
||||||
|
|
||||||
debug=true
|
|
||||||
verbose=true
|
|
||||||
log_file = /var/tmp/galloper-cli.log
|
|
||||||
use_stderr=false
|
|
||||||
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
import pecan
|
|
||||||
|
|
||||||
|
|
||||||
config = {
|
|
||||||
'root': 'galloper.api.root.RootController',
|
|
||||||
'modules': ['galloper.api'],
|
|
||||||
'debug': True,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def setup_app(pecan_config=None):
|
|
||||||
if not pecan_config:
|
|
||||||
pecan_config = pecan.configuration.conf_from_dict(config)
|
|
||||||
|
|
||||||
app = pecan.make_app(
|
|
||||||
pecan_config.get('root'),
|
|
||||||
debug=pecan_config.get('debug'),
|
|
||||||
)
|
|
||||||
return app
|
|
||||||
|
|
||||||
|
|
||||||
class WsgiApplication(object):
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.app = setup_app()
|
|
||||||
|
|
||||||
def __call__(self, environ, start_response):
|
|
||||||
return self.app(environ, start_response)
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
|
|
||||||
from pecan import rest
|
|
||||||
from wsme import types as wtypes
|
|
||||||
from wsmeext.pecan import wsexpose
|
|
||||||
|
|
||||||
|
|
||||||
class Root(wtypes.Base):
|
|
||||||
|
|
||||||
name = wtypes.text
|
|
||||||
description = wtypes.text
|
|
||||||
version = wtypes.text
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def convert(cls):
|
|
||||||
root = Root()
|
|
||||||
root.name = "Fuel provisioning system."
|
|
||||||
root.description = "Fuel provisioning system."
|
|
||||||
root.version = "v1"
|
|
||||||
return root
|
|
||||||
|
|
||||||
|
|
||||||
class RootController(rest.RestController):
|
|
||||||
@wsexpose(Root)
|
|
||||||
def get(self):
|
|
||||||
return Root.convert()
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
|
|
||||||
import json
|
|
||||||
import requests
|
|
||||||
import urlparse
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.common import exceptions
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
|
|
||||||
|
|
||||||
cli_client_opts = [
|
|
||||||
cfg.StrOpt('galloper_cli_user_agent',
|
|
||||||
default='galloper-cli',
|
|
||||||
help='Galloper CLI user agent.'),
|
|
||||||
cfg.StrOpt('galloper_api_host',
|
|
||||||
default='0.0.0.0',
|
|
||||||
help='Galloper API host.'),
|
|
||||||
cfg.IntOpt('galloper_api_port',
|
|
||||||
default=9999,
|
|
||||||
help='Galloper API port number.'),
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF.register_opts(cli_client_opts)
|
|
||||||
LOG = logging.getLogger("galloper-cli")
|
|
||||||
|
|
||||||
|
|
||||||
class HTTPClient(object):
|
|
||||||
USER_AGENT = CONF.galloper_cli_user_agent
|
|
||||||
BASE_URL = 'http://{0}:{1}'.format(CONF.galloper_api_host,
|
|
||||||
CONF.galloper_api_port)
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.http = requests.Session()
|
|
||||||
|
|
||||||
def get(self, url, **kwargs):
|
|
||||||
return self.request(url, 'GET', **kwargs)
|
|
||||||
|
|
||||||
def request(self, url, method, **kwargs):
|
|
||||||
if urlparse.urlparse(url).scheme != 'http':
|
|
||||||
actual_url = urlparse.urljoin(self.BASE_URL, url)
|
|
||||||
else:
|
|
||||||
actual_url = url
|
|
||||||
|
|
||||||
kwargs.setdefault('headers', kwargs.get('headers', {}))
|
|
||||||
kwargs['headers']['User-Agent'] = self.USER_AGENT
|
|
||||||
kwargs['headers']['Accept'] = 'application/json'
|
|
||||||
if 'body' in kwargs:
|
|
||||||
kwargs['headers']['Content-Type'] = 'application/json'
|
|
||||||
kwargs['data'] = json.dumps(kwargs['body'])
|
|
||||||
del kwargs['body']
|
|
||||||
|
|
||||||
LOG.debug("Sending request to API: method={0} url={1} "
|
|
||||||
"kwargs={2}".format(method, actual_url, kwargs))
|
|
||||||
resp = self.http.request(
|
|
||||||
method,
|
|
||||||
actual_url,
|
|
||||||
**kwargs)
|
|
||||||
|
|
||||||
"""TODO(kozhukalov): accurate dealing with http
|
|
||||||
"""
|
|
||||||
if resp.text:
|
|
||||||
if resp.status_code == 400:
|
|
||||||
if ('Connection refused' in resp.text or
|
|
||||||
'actively refused' in resp.text):
|
|
||||||
raise exceptions.HttpException(resp.text)
|
|
||||||
try:
|
|
||||||
body = json.loads(resp.text)
|
|
||||||
except ValueError:
|
|
||||||
body = None
|
|
||||||
else:
|
|
||||||
body = None
|
|
||||||
|
|
||||||
if resp.status_code >= 400:
|
|
||||||
raise exceptions.HttpException(resp.status_code)
|
|
||||||
|
|
||||||
LOG.debug("Response: code={0} body={1}".format(resp.status_code, body))
|
|
||||||
return resp, body
|
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import importlib
|
|
||||||
|
|
||||||
from galloper.cli.client import HTTPClient
|
|
||||||
|
|
||||||
|
|
||||||
class GalloperCli(object):
|
|
||||||
SHELL_ROOT = 'galloper.cli'
|
|
||||||
|
|
||||||
def shell(self, parser, version='v1', shell_root=None,
|
|
||||||
shell_module_path=None):
|
|
||||||
|
|
||||||
def traverse(module_path, par):
|
|
||||||
module = importlib.import_module(module_path)
|
|
||||||
sub_parsers = par.add_subparsers(dest='subcommand',
|
|
||||||
metavar='<subcommand>')
|
|
||||||
|
|
||||||
for attr in dir(module):
|
|
||||||
func = getattr(module, attr)
|
|
||||||
|
|
||||||
if (not hasattr(func, 'is_branch') and
|
|
||||||
not hasattr(func, 'is_subtree')):
|
|
||||||
continue
|
|
||||||
|
|
||||||
description = func.__doc__ or ''
|
|
||||||
sub_parser = sub_parsers.add_parser(
|
|
||||||
attr.replace('_', '-'),
|
|
||||||
help=description.strip(),
|
|
||||||
description=description,
|
|
||||||
add_help=True,
|
|
||||||
)
|
|
||||||
if hasattr(func, 'is_branch') and func.is_branch:
|
|
||||||
for (args, kwargs) in getattr(func, 'args', []):
|
|
||||||
sub_parser.add_argument(*args, **kwargs)
|
|
||||||
sub_parser.set_defaults(func=func)
|
|
||||||
elif hasattr(func, 'is_subtree') and func.is_subtree:
|
|
||||||
for branch in func.branches:
|
|
||||||
sub_parser = traverse(module_path + '.' + branch,
|
|
||||||
sub_parser)
|
|
||||||
return par
|
|
||||||
|
|
||||||
if not shell_root:
|
|
||||||
shell_root = self.SHELL_ROOT
|
|
||||||
if not shell_module_path:
|
|
||||||
shell_module_path = shell_root + '.' + version
|
|
||||||
return traverse(shell_module_path, parser)
|
|
||||||
|
|
||||||
def main(self, argv):
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
prog='galloper',
|
|
||||||
description='Command line interface to the '
|
|
||||||
'galloper provisioning system',
|
|
||||||
add_help=True,
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--cli-version',
|
|
||||||
action='store',
|
|
||||||
dest='cli_version',
|
|
||||||
default='v1',
|
|
||||||
help='Command line interface version.'
|
|
||||||
)
|
|
||||||
|
|
||||||
parser = self.shell(parser)
|
|
||||||
(options, args) = parser.parse_known_args(argv)
|
|
||||||
|
|
||||||
if options.func:
|
|
||||||
http_client = HTTPClient()
|
|
||||||
options.func(http_client, *args)
|
|
||||||
@@ -1,106 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
import functools
|
|
||||||
import json
|
|
||||||
import sys
|
|
||||||
import yaml
|
|
||||||
|
|
||||||
|
|
||||||
def branch(func):
|
|
||||||
if hasattr(func, 'is_subtree') and func.is_subtree:
|
|
||||||
raise Exception("Incompatible decorator: only one of "
|
|
||||||
"'branch' and 'subtree' should be used")
|
|
||||||
func.is_branch = True
|
|
||||||
return func
|
|
||||||
|
|
||||||
|
|
||||||
def subtree(branches):
|
|
||||||
if not isinstance(branches, (list, tuple)):
|
|
||||||
branches = [branches]
|
|
||||||
branches = list(branches)
|
|
||||||
|
|
||||||
def wrapper(func):
|
|
||||||
if hasattr(func, 'is_branch') and func.is_branch:
|
|
||||||
raise Exception("Incompatible decorator: only one of "
|
|
||||||
"'branch' and 'subtree' should be used")
|
|
||||||
func.is_subtree = True
|
|
||||||
func.branches = branches
|
|
||||||
return func
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
def arg(*args, **kwargs):
|
|
||||||
def wrapper(func):
|
|
||||||
if not hasattr(func, 'args'):
|
|
||||||
func.args = []
|
|
||||||
if (args, kwargs) not in func.args:
|
|
||||||
func.args.insert(0, (args, kwargs))
|
|
||||||
return func
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
"""TODO(kozhukalov): Create default arguments decorator
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def printer(data, formatted="", indent=0):
|
|
||||||
"""Custom print method. Almost the same as yaml.safe_dump()."""
|
|
||||||
|
|
||||||
"""TODO(kozhukalov): it is almost the same as yaml, so it might be better
|
|
||||||
to remove this print method
|
|
||||||
"""
|
|
||||||
newformatted = formatted
|
|
||||||
if isinstance(data, (int, float, str, unicode)):
|
|
||||||
newformatted += " " * indent + "- " + str(data) + "\n"
|
|
||||||
elif isinstance(data, (list,)):
|
|
||||||
for newdata in data:
|
|
||||||
newformatted += printer(newdata, formatted, indent + 4)
|
|
||||||
elif isinstance(data, (dict,)):
|
|
||||||
for key, value in data.iteritems():
|
|
||||||
newformatted += " " * indent + str(key) + ":\n"
|
|
||||||
newformatted += printer(value, formatted, indent + 4)
|
|
||||||
return newformatted
|
|
||||||
|
|
||||||
|
|
||||||
def print_formatted(data, method='yaml', custom=None):
|
|
||||||
try:
|
|
||||||
formatted = {
|
|
||||||
'custom': (custom if custom else printer),
|
|
||||||
'json': functools.partial(json.dumps, indent=4),
|
|
||||||
'yaml': functools.partial(yaml.safe_dump, default_flow_style=False)
|
|
||||||
}[method](data)
|
|
||||||
sys.stdout.write(formatted)
|
|
||||||
except:
|
|
||||||
sys.stdout.write("Error occured while trying to print the result")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
@branch
|
|
||||||
def info(http_client, *args, **kwargs):
|
|
||||||
"""Print information about galloper provisioning system"""
|
|
||||||
print_formatted(http_client.get('')[1])
|
|
||||||
|
|
||||||
|
|
||||||
@subtree('node')
|
|
||||||
def node(*args, **kwargs):
|
|
||||||
"""Node subcommand."""
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@subtree('image')
|
|
||||||
def image(*args, **kwargs):
|
|
||||||
"""Image subcommand."""
|
|
||||||
pass
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
|
|
||||||
from galloper.cli.v1 import branch
|
|
||||||
from galloper.cli.v1 import arg
|
|
||||||
from galloper.cli.v1 import print_formatted
|
|
||||||
|
|
||||||
|
|
||||||
@branch
|
|
||||||
@arg('-a', '--all',
|
|
||||||
action='store_true',
|
|
||||||
dest='node_list_all',
|
|
||||||
default=False,
|
|
||||||
help="List all nodes.")
|
|
||||||
def list(http_client, *args, **kwargs):
|
|
||||||
"""List available images."""
|
|
||||||
print_formatted({"list": []})
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
|
|
||||||
from galloper.cli.v1 import branch
|
|
||||||
from galloper.cli.v1 import arg
|
|
||||||
|
|
||||||
|
|
||||||
@branch
|
|
||||||
@arg('-a', '--all',
|
|
||||||
action='store_true',
|
|
||||||
dest='node_list_all',
|
|
||||||
default=False,
|
|
||||||
help="List all nodes.")
|
|
||||||
def list(http_client, *args, **kwargs):
|
|
||||||
"""List available nodes."""
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@branch
|
|
||||||
@arg('-n', '--name',
|
|
||||||
action='store',
|
|
||||||
dest='node_create_name',
|
|
||||||
help="Name of node to create.")
|
|
||||||
def create(http_client, *args, **kwargs):
|
|
||||||
"""Create new node."""
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@branch
|
|
||||||
@arg('-n', '--name',
|
|
||||||
action='store',
|
|
||||||
dest='node_edit_name',
|
|
||||||
help="Name of node to edit.")
|
|
||||||
def edit(http_client, *args, **kwargs):
|
|
||||||
"""Edit existing node."""
|
|
||||||
pass
|
|
||||||
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
from wsgiref import simple_server
|
|
||||||
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
from galloper.api.app import WsgiApplication
|
|
||||||
|
|
||||||
|
|
||||||
api_opts = [
|
|
||||||
cfg.StrOpt('galloper_api_host',
|
|
||||||
default='0.0.0.0',
|
|
||||||
help='Galloper API host.'),
|
|
||||||
cfg.IntOpt('galloper_api_port',
|
|
||||||
default=9999,
|
|
||||||
help='Galloper API port number.'),
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF.register_opts(api_opts)
|
|
||||||
|
|
||||||
|
|
||||||
def run():
|
|
||||||
logging.setup("galloper-api")
|
|
||||||
LOG = logging.getLogger("galloper-api")
|
|
||||||
|
|
||||||
LOG.info("Starting galloper API host={0} port={1}".format(
|
|
||||||
CONF.galloper_api_host, CONF.galloper_api_port
|
|
||||||
))
|
|
||||||
wsgi = simple_server.make_server(CONF.galloper_api_host,
|
|
||||||
CONF.galloper_api_port,
|
|
||||||
WsgiApplication())
|
|
||||||
|
|
||||||
try:
|
|
||||||
wsgi.serve_forever()
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
LOG.info("Keyboard interruption caught. Stopping API.")
|
|
||||||
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
import sys
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.cli.parser import GalloperCli
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
|
|
||||||
|
|
||||||
def run():
|
|
||||||
logging.setup("galloper-cli")
|
|
||||||
|
|
||||||
try:
|
|
||||||
GalloperCli().main(sys.argv[1:])
|
|
||||||
except Exception as e:
|
|
||||||
print("Error occured while running CLI.")
|
|
||||||
print(str(e.message))
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
|
|
||||||
class NodeNotFound(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class MultipleNodesFound(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class HttpException(Exception):
|
|
||||||
pass
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
from galloper.db.node import Node
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
import abc
|
|
||||||
|
|
||||||
from galloper.openstack.common.db import api as db_api
|
|
||||||
|
|
||||||
_BACKEND_MAPPING = {'sqlalchemy': 'galloper.db.sqlalchemy.api'}
|
|
||||||
IMPL = db_api.DBAPI(backend_mapping=_BACKEND_MAPPING)
|
|
||||||
|
|
||||||
|
|
||||||
def get_instance():
|
|
||||||
"""Returns openstack.common.db.api.DBAPI instance"""
|
|
||||||
return IMPL
|
|
||||||
|
|
||||||
|
|
||||||
class Connection(object):
|
|
||||||
"""Base class for storage system connections."""
|
|
||||||
|
|
||||||
__metaclass__ = abc.ABCMeta
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def __init__(self):
|
|
||||||
"""Constructor."""
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def get_nodes(self):
|
|
||||||
"""Return a list of dicts of all nodes.
|
|
||||||
"""
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def get_node(self, uuid):
|
|
||||||
"""Return a dict representing node.
|
|
||||||
|
|
||||||
:param uuid: Node uuid
|
|
||||||
"""
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def create_node(self, data):
|
|
||||||
"""Create a new node.
|
|
||||||
|
|
||||||
:param data: A dict containing node data.
|
|
||||||
:returns: A node.
|
|
||||||
"""
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def destroy_node(self, uuid):
|
|
||||||
"""Destroy a node.
|
|
||||||
|
|
||||||
:param uuid: The uuid of a node.
|
|
||||||
"""
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def update_node(self, uuid, data):
|
|
||||||
"""Update properties of a node.
|
|
||||||
|
|
||||||
:param node: The uuid of a node.
|
|
||||||
:param data: Dict of containing node data to update.
|
|
||||||
:returns: A node.
|
|
||||||
"""
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
from galloper.db import api as db_api
|
|
||||||
from galloper.openstack.common import uuidutils
|
|
||||||
|
|
||||||
|
|
||||||
class Node(object):
|
|
||||||
"""This method returns openstack.common.db.api.DBAPI instance.
|
|
||||||
When we call methods we actually call instance methods from
|
|
||||||
backend which is instance of AConnection subclass.
|
|
||||||
"""
|
|
||||||
dbapi = db_api.get_instance()
|
|
||||||
fields = ('id', 'uuid', 'meta')
|
|
||||||
default_generators = {
|
|
||||||
'uuid': uuidutils.generate_uuid,
|
|
||||||
'meta': lambda: {}
|
|
||||||
}
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_db(cls, db_node):
|
|
||||||
"""Converts a database entity to a formal object."""
|
|
||||||
node = cls()
|
|
||||||
for field in cls.fields:
|
|
||||||
node[field] = db_node[field]
|
|
||||||
return node
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def get_by_uuid(cls, uuid):
|
|
||||||
"""Find a node based on uuid and return a Node object.
|
|
||||||
|
|
||||||
:param uuid: the uuid of a node.
|
|
||||||
:returns: a :class:`Node` object.
|
|
||||||
"""
|
|
||||||
db_node = cls.dbapi.get_node(uuid)
|
|
||||||
return Node.from_db(db_node)
|
|
||||||
|
|
||||||
def to_data(self):
|
|
||||||
data = {}
|
|
||||||
for field in self.fields:
|
|
||||||
data[field] = getattr(self, field)
|
|
||||||
return data
|
|
||||||
|
|
||||||
def save(self):
|
|
||||||
self.dbapi.update_node(self.uuid, self.to_data())
|
|
||||||
|
|
||||||
def refresh(self):
|
|
||||||
actual = self.__class__.get_by_uuid(self.uuid)
|
|
||||||
for field in self.fields:
|
|
||||||
if (hasattr(self, field) and
|
|
||||||
getattr(self, field) != getattr(actual, field)):
|
|
||||||
setattr(self, field, getattr(actual, field))
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
from sqlalchemy.orm.exc import MultipleResultsFound
|
|
||||||
from sqlalchemy.orm.exc import NoResultFound
|
|
||||||
|
|
||||||
from galloper.openstack.common.db.sqlalchemy import session as db_session
|
|
||||||
from galloper.db.api import Connection as AConnection
|
|
||||||
from galloper.db import Node
|
|
||||||
from galloper.common.exceptions import NodeNotFound
|
|
||||||
from galloper.common.exceptions import MultipleNodesFound
|
|
||||||
|
|
||||||
|
|
||||||
def get_backend():
|
|
||||||
"""This method is called from openstack.common.db.api.DBAPI
|
|
||||||
to get backend. openstack.common.db.api.DBAPI.__getattr__ method
|
|
||||||
tries to get attribute from backend. So we can call Connection instance
|
|
||||||
method as if they were openstack.common.db.api.DBAPI instance methods."""
|
|
||||||
return Connection()
|
|
||||||
|
|
||||||
|
|
||||||
def model_query(model, session=None):
|
|
||||||
session = session or db_session.get_session()
|
|
||||||
query = session.query(model)
|
|
||||||
return query
|
|
||||||
|
|
||||||
|
|
||||||
class Connection(AConnection):
|
|
||||||
"""SqlAlchemy connection."""
|
|
||||||
|
|
||||||
def get_db_nodes(self):
|
|
||||||
query = model_query(Node)
|
|
||||||
return iter(query)
|
|
||||||
|
|
||||||
def get_nodes(self):
|
|
||||||
for db_node in self.get_db_nodes():
|
|
||||||
yield Node.from_db(db_node)
|
|
||||||
|
|
||||||
def get_db_node(self, uuid):
|
|
||||||
query = model_query(Node)
|
|
||||||
query.filter_by(uuid=uuid)
|
|
||||||
try:
|
|
||||||
obj = query.one()
|
|
||||||
except NoResultFound:
|
|
||||||
raise NodeNotFound(uuid=uuid)
|
|
||||||
except MultipleResultsFound:
|
|
||||||
raise MultipleNodesFound(uuid=uuid)
|
|
||||||
return obj
|
|
||||||
|
|
||||||
def get_node(self, uuid):
|
|
||||||
return Node.from_db(self.get_db_node(uuid))
|
|
||||||
|
|
||||||
def create_node(self, data):
|
|
||||||
for name, generator in Node.default_generators:
|
|
||||||
if not data.get(name):
|
|
||||||
data[name] = generator()
|
|
||||||
node = Node()
|
|
||||||
node.update(values)
|
|
||||||
node.save()
|
|
||||||
return node
|
|
||||||
|
|
||||||
|
|
||||||
def destroy_node(self, uuid):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def update_db_node(self, uuid, data):
|
|
||||||
session = db_session.get_session()
|
|
||||||
with session.begin():
|
|
||||||
query = model_query(Node, session=session)
|
|
||||||
query.filter_by(uuid=uuid)
|
|
||||||
|
|
||||||
count = query.update(data, synchronize_session='fetch')
|
|
||||||
if count < 1:
|
|
||||||
raise NodeNotFound(uuid=uuid)
|
|
||||||
elif count > 1:
|
|
||||||
raise MultipleNodesFound(uuid=uuid)
|
|
||||||
obj = query.one()
|
|
||||||
return obj
|
|
||||||
|
|
||||||
def update_node(self, uuid, data):
|
|
||||||
self.update_db_node(uuid, data)
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
import json
|
|
||||||
|
|
||||||
from sqlalchemy import Column
|
|
||||||
from sqlalchemy import Integer
|
|
||||||
from sqlalchemy import schema, String
|
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
|
||||||
from sqlalchemy.types import TypeDecorator, VARCHAR
|
|
||||||
|
|
||||||
from galloper.openstack.common.db.sqlalchemy import models
|
|
||||||
|
|
||||||
|
|
||||||
class JSON(TypeDecorator):
|
|
||||||
impl = VARCHAR
|
|
||||||
|
|
||||||
def process_bind_param(self, value, dialect):
|
|
||||||
if value is not None:
|
|
||||||
value = json.dumps(value)
|
|
||||||
return value
|
|
||||||
|
|
||||||
def process_result_value(self, value, dialect):
|
|
||||||
if value is not None:
|
|
||||||
value = json.loads(value)
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
class GalloperBase(models.ModelBase):
|
|
||||||
metadata = None
|
|
||||||
|
|
||||||
def as_dict(self):
|
|
||||||
d = {}
|
|
||||||
for c in self.__table__.columns:
|
|
||||||
d[c.name] = self[c.name]
|
|
||||||
return d
|
|
||||||
|
|
||||||
|
|
||||||
Base = declarative_base(cls=GalloperBase)
|
|
||||||
|
|
||||||
|
|
||||||
class Node(Base):
|
|
||||||
__tablename__ = 'nodes'
|
|
||||||
__table_args__ = (schema.UniqueConstraint('uuid', name='node_uuid_ux'))
|
|
||||||
id = Column(Integer, primary_key=True)
|
|
||||||
uuid = Column(String(36), index=True)
|
|
||||||
meta = Column(JSON)
|
|
||||||
|
|
||||||
|
|
||||||
class Image(Base):
|
|
||||||
__tablename__ = 'images'
|
|
||||||
__table_args__ = (schema.UniqueConstraint('uuid', name='node_uuid_ux'))
|
|
||||||
id = Column(Integer, primary_key=True)
|
|
||||||
uuid = Column(String(36), index=True)
|
|
||||||
meta = Column(JSON)
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
from galloper.driver.base import Driver
|
|
||||||
from galloper.driver.dnsmasq import Dnsmasq
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
class Driver(object):
|
|
||||||
pass
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import jinja2
|
|
||||||
|
|
||||||
from galloper.driver import Driver
|
|
||||||
|
|
||||||
|
|
||||||
class Dnsmasq(Driver):
|
|
||||||
""" Subprocess with arguments """
|
|
||||||
pass
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
# Copyright 2013: Mirantis Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
class Manager(object):
|
|
||||||
pass
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2012 Red Hat, Inc.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
import inspect
|
|
||||||
|
|
||||||
|
|
||||||
class MissingArgs(Exception):
|
|
||||||
|
|
||||||
def __init__(self, missing):
|
|
||||||
self.missing = missing
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
if len(self.missing) == 1:
|
|
||||||
return "An argument is missing"
|
|
||||||
else:
|
|
||||||
return ("%(num)d arguments are missing" %
|
|
||||||
dict(num=len(self.missing)))
|
|
||||||
|
|
||||||
|
|
||||||
def validate_args(fn, *args, **kwargs):
|
|
||||||
"""Check that the supplied args are sufficient for calling a function.
|
|
||||||
|
|
||||||
>>> validate_args(lambda a: None)
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
MissingArgs: An argument is missing
|
|
||||||
>>> validate_args(lambda a, b, c, d: None, 0, c=1)
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
MissingArgs: 2 arguments are missing
|
|
||||||
|
|
||||||
:param fn: the function to check
|
|
||||||
:param arg: the positional arguments supplied
|
|
||||||
:param kwargs: the keyword arguments supplied
|
|
||||||
"""
|
|
||||||
argspec = inspect.getargspec(fn)
|
|
||||||
|
|
||||||
num_defaults = len(argspec.defaults or [])
|
|
||||||
required_args = argspec.args[:len(argspec.args) - num_defaults]
|
|
||||||
|
|
||||||
def isbound(method):
|
|
||||||
return getattr(method, 'im_self', None) is not None
|
|
||||||
|
|
||||||
if isbound(fn):
|
|
||||||
required_args.pop(0)
|
|
||||||
|
|
||||||
missing = [arg for arg in required_args if arg not in kwargs]
|
|
||||||
missing = missing[len(args):]
|
|
||||||
if missing:
|
|
||||||
raise MissingArgs(missing)
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2012 Cloudscaling Group, Inc
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
@@ -1,101 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright (c) 2013 Rackspace Hosting
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""Multiple DB API backend support.
|
|
||||||
|
|
||||||
Supported configuration options:
|
|
||||||
|
|
||||||
`db_backend`: DB backend name or full module path to DB backend module.
|
|
||||||
`dbapi_use_tpool`: Enable thread pooling of DB API calls.
|
|
||||||
|
|
||||||
A DB backend module should implement a method named 'get_backend' which
|
|
||||||
takes no arguments. The method can return any object that implements DB
|
|
||||||
API methods.
|
|
||||||
|
|
||||||
*NOTE*: There are bugs in eventlet when using tpool combined with
|
|
||||||
threading locks. The python logging module happens to use such locks. To
|
|
||||||
work around this issue, be sure to specify thread=False with
|
|
||||||
eventlet.monkey_patch().
|
|
||||||
|
|
||||||
A bug for eventlet has been filed here:
|
|
||||||
|
|
||||||
https://bitbucket.org/eventlet/eventlet/issue/137/
|
|
||||||
"""
|
|
||||||
import functools
|
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common import importutils
|
|
||||||
from galloper.openstack.common import lockutils
|
|
||||||
|
|
||||||
|
|
||||||
db_opts = [
|
|
||||||
cfg.StrOpt('db_backend',
|
|
||||||
default='sqlalchemy',
|
|
||||||
help='The backend to use for db'),
|
|
||||||
cfg.BoolOpt('dbapi_use_tpool',
|
|
||||||
default=False,
|
|
||||||
help='Enable the experimental use of thread pooling for '
|
|
||||||
'all DB API calls')
|
|
||||||
]
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF.register_opts(db_opts)
|
|
||||||
|
|
||||||
|
|
||||||
class DBAPI(object):
|
|
||||||
def __init__(self, backend_mapping=None):
|
|
||||||
if backend_mapping is None:
|
|
||||||
backend_mapping = {}
|
|
||||||
self.__backend = None
|
|
||||||
self.__backend_mapping = backend_mapping
|
|
||||||
|
|
||||||
@lockutils.synchronized('dbapi_backend', 'galloper-')
|
|
||||||
def __get_backend(self):
|
|
||||||
"""Get the actual backend. May be a module or an instance of
|
|
||||||
a class. Doesn't matter to us. We do this synchronized as it's
|
|
||||||
possible multiple greenthreads started very quickly trying to do
|
|
||||||
DB calls and eventlet can switch threads before self.__backend gets
|
|
||||||
assigned.
|
|
||||||
"""
|
|
||||||
if self.__backend:
|
|
||||||
# Another thread assigned it
|
|
||||||
return self.__backend
|
|
||||||
backend_name = CONF.db_backend
|
|
||||||
self.__use_tpool = CONF.dbapi_use_tpool
|
|
||||||
if self.__use_tpool:
|
|
||||||
from eventlet import tpool
|
|
||||||
self.__tpool = tpool
|
|
||||||
# Import the untranslated name if we don't have a
|
|
||||||
# mapping.
|
|
||||||
backend_path = self.__backend_mapping.get(backend_name,
|
|
||||||
backend_name)
|
|
||||||
backend_mod = importutils.import_module(backend_path)
|
|
||||||
self.__backend = backend_mod.get_backend()
|
|
||||||
return self.__backend
|
|
||||||
|
|
||||||
def __getattr__(self, key):
|
|
||||||
backend = self.__backend or self.__get_backend()
|
|
||||||
attr = getattr(backend, key)
|
|
||||||
if not self.__use_tpool or not hasattr(attr, '__call__'):
|
|
||||||
return attr
|
|
||||||
|
|
||||||
def tpool_wrapper(*args, **kwargs):
|
|
||||||
return self.__tpool.execute(attr, *args, **kwargs)
|
|
||||||
|
|
||||||
functools.update_wrapper(tpool_wrapper, attr)
|
|
||||||
return tpool_wrapper
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2010 United States Government as represented by the
|
|
||||||
# Administrator of the National Aeronautics and Space Administration.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""DB related custom exceptions."""
|
|
||||||
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
|
|
||||||
|
|
||||||
class DBError(Exception):
|
|
||||||
"""Wraps an implementation specific exception."""
|
|
||||||
def __init__(self, inner_exception=None):
|
|
||||||
self.inner_exception = inner_exception
|
|
||||||
super(DBError, self).__init__(str(inner_exception))
|
|
||||||
|
|
||||||
|
|
||||||
class DBDuplicateEntry(DBError):
|
|
||||||
"""Wraps an implementation specific exception."""
|
|
||||||
def __init__(self, columns=[], inner_exception=None):
|
|
||||||
self.columns = columns
|
|
||||||
super(DBDuplicateEntry, self).__init__(inner_exception)
|
|
||||||
|
|
||||||
|
|
||||||
class DBDeadlock(DBError):
|
|
||||||
def __init__(self, inner_exception=None):
|
|
||||||
super(DBDeadlock, self).__init__(inner_exception)
|
|
||||||
|
|
||||||
|
|
||||||
class DBInvalidUnicodeParameter(Exception):
|
|
||||||
message = _("Invalid Parameter: "
|
|
||||||
"Unicode is not supported by the current database.")
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2012 Cloudscaling Group, Inc
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright (c) 2011 X.commerce, a business unit of eBay Inc.
|
|
||||||
# Copyright 2010 United States Government as represented by the
|
|
||||||
# Administrator of the National Aeronautics and Space Administration.
|
|
||||||
# Copyright 2011 Piston Cloud Computing, Inc.
|
|
||||||
# Copyright 2012 Cloudscaling Group, Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
"""
|
|
||||||
SQLAlchemy models.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from sqlalchemy import Column, Integer
|
|
||||||
from sqlalchemy import DateTime
|
|
||||||
from sqlalchemy.orm import object_mapper
|
|
||||||
|
|
||||||
from galloper.openstack.common.db.sqlalchemy.session import get_session
|
|
||||||
from galloper.openstack.common import timeutils
|
|
||||||
|
|
||||||
|
|
||||||
class ModelBase(object):
|
|
||||||
"""Base class for models."""
|
|
||||||
__table_initialized__ = False
|
|
||||||
created_at = Column(DateTime, default=timeutils.utcnow)
|
|
||||||
updated_at = Column(DateTime, onupdate=timeutils.utcnow)
|
|
||||||
metadata = None
|
|
||||||
|
|
||||||
def save(self, session=None):
|
|
||||||
"""Save this object."""
|
|
||||||
if not session:
|
|
||||||
session = get_session()
|
|
||||||
# NOTE(boris-42): This part of code should be look like:
|
|
||||||
# sesssion.add(self)
|
|
||||||
# session.flush()
|
|
||||||
# But there is a bug in sqlalchemy and eventlet that
|
|
||||||
# raises NoneType exception if there is no running
|
|
||||||
# transaction and rollback is called. As long as
|
|
||||||
# sqlalchemy has this bug we have to create transaction
|
|
||||||
# explicity.
|
|
||||||
with session.begin(subtransactions=True):
|
|
||||||
session.add(self)
|
|
||||||
session.flush()
|
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
|
||||||
setattr(self, key, value)
|
|
||||||
|
|
||||||
def __getitem__(self, key):
|
|
||||||
return getattr(self, key)
|
|
||||||
|
|
||||||
def get(self, key, default=None):
|
|
||||||
return getattr(self, key, default)
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
columns = dict(object_mapper(self).columns).keys()
|
|
||||||
# NOTE(russellb): Allow models to specify other keys that can be looked
|
|
||||||
# up, beyond the actual db columns. An example would be the 'name'
|
|
||||||
# property for an Instance.
|
|
||||||
if hasattr(self, '_extra_keys'):
|
|
||||||
columns.extend(self._extra_keys())
|
|
||||||
self._i = iter(columns)
|
|
||||||
return self
|
|
||||||
|
|
||||||
def next(self):
|
|
||||||
n = self._i.next()
|
|
||||||
return n, getattr(self, n)
|
|
||||||
|
|
||||||
def update(self, values):
|
|
||||||
"""Make the model object behave like a dict."""
|
|
||||||
for k, v in values.iteritems():
|
|
||||||
setattr(self, k, v)
|
|
||||||
|
|
||||||
def iteritems(self):
|
|
||||||
"""Make the model object behave like a dict.
|
|
||||||
|
|
||||||
Includes attributes from joins."""
|
|
||||||
local = dict(self)
|
|
||||||
joined = dict([(k, v) for k, v in self.__dict__.iteritems()
|
|
||||||
if not k[0] == '_'])
|
|
||||||
local.update(joined)
|
|
||||||
return local.iteritems()
|
|
||||||
|
|
||||||
|
|
||||||
class SoftDeleteMixin(object):
|
|
||||||
deleted_at = Column(DateTime)
|
|
||||||
deleted = Column(Integer, default=0)
|
|
||||||
|
|
||||||
def soft_delete(self, session=None):
|
|
||||||
"""Mark this object as deleted."""
|
|
||||||
self.deleted = self.id
|
|
||||||
self.deleted_at = timeutils.utcnow()
|
|
||||||
self.save(session=session)
|
|
||||||
@@ -1,643 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2010 United States Government as represented by the
|
|
||||||
# Administrator of the National Aeronautics and Space Administration.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""Session Handling for SQLAlchemy backend.
|
|
||||||
|
|
||||||
Initializing:
|
|
||||||
|
|
||||||
* Call set_defaults with the minimal of the following kwargs:
|
|
||||||
sql_connection, sqlite_db
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
session.set_defaults(sql_connection="sqlite:///var/lib/galloper/sqlite.db",
|
|
||||||
sqlite_db="/var/lib/galloper/sqlite.db")
|
|
||||||
|
|
||||||
Recommended ways to use sessions within this framework:
|
|
||||||
|
|
||||||
* Don't use them explicitly; this is like running with AUTOCOMMIT=1.
|
|
||||||
model_query() will implicitly use a session when called without one
|
|
||||||
supplied. This is the ideal situation because it will allow queries
|
|
||||||
to be automatically retried if the database connection is interrupted.
|
|
||||||
|
|
||||||
Note: Automatic retry will be enabled in a future patch.
|
|
||||||
|
|
||||||
It is generally fine to issue several queries in a row like this. Even though
|
|
||||||
they may be run in separate transactions and/or separate sessions, each one
|
|
||||||
will see the data from the prior calls. If needed, undo- or rollback-like
|
|
||||||
functionality should be handled at a logical level. For an example, look at
|
|
||||||
the code around quotas and reservation_rollback().
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
|
|
||||||
def get_foo(context, foo):
|
|
||||||
return model_query(context, models.Foo).\
|
|
||||||
filter_by(foo=foo).\
|
|
||||||
first()
|
|
||||||
|
|
||||||
def update_foo(context, id, newfoo):
|
|
||||||
model_query(context, models.Foo).\
|
|
||||||
filter_by(id=id).\
|
|
||||||
update({'foo': newfoo})
|
|
||||||
|
|
||||||
def create_foo(context, values):
|
|
||||||
foo_ref = models.Foo()
|
|
||||||
foo_ref.update(values)
|
|
||||||
foo_ref.save()
|
|
||||||
return foo_ref
|
|
||||||
|
|
||||||
|
|
||||||
* Within the scope of a single method, keeping all the reads and writes within
|
|
||||||
the context managed by a single session. In this way, the session's __exit__
|
|
||||||
handler will take care of calling flush() and commit() for you.
|
|
||||||
If using this approach, you should not explicitly call flush() or commit().
|
|
||||||
Any error within the context of the session will cause the session to emit
|
|
||||||
a ROLLBACK. If the connection is dropped before this is possible, the
|
|
||||||
database will implicitly rollback the transaction.
|
|
||||||
|
|
||||||
Note: statements in the session scope will not be automatically retried.
|
|
||||||
|
|
||||||
If you create models within the session, they need to be added, but you
|
|
||||||
do not need to call model.save()
|
|
||||||
|
|
||||||
def create_many_foo(context, foos):
|
|
||||||
session = get_session()
|
|
||||||
with session.begin():
|
|
||||||
for foo in foos:
|
|
||||||
foo_ref = models.Foo()
|
|
||||||
foo_ref.update(foo)
|
|
||||||
session.add(foo_ref)
|
|
||||||
|
|
||||||
def update_bar(context, foo_id, newbar):
|
|
||||||
session = get_session()
|
|
||||||
with session.begin():
|
|
||||||
foo_ref = model_query(context, models.Foo, session).\
|
|
||||||
filter_by(id=foo_id).\
|
|
||||||
first()
|
|
||||||
model_query(context, models.Bar, session).\
|
|
||||||
filter_by(id=foo_ref['bar_id']).\
|
|
||||||
update({'bar': newbar})
|
|
||||||
|
|
||||||
Note: update_bar is a trivially simple example of using "with session.begin".
|
|
||||||
Whereas create_many_foo is a good example of when a transaction is needed,
|
|
||||||
it is always best to use as few queries as possible. The two queries in
|
|
||||||
update_bar can be better expressed using a single query which avoids
|
|
||||||
the need for an explicit transaction. It can be expressed like so:
|
|
||||||
|
|
||||||
def update_bar(context, foo_id, newbar):
|
|
||||||
subq = model_query(context, models.Foo.id).\
|
|
||||||
filter_by(id=foo_id).\
|
|
||||||
limit(1).\
|
|
||||||
subquery()
|
|
||||||
model_query(context, models.Bar).\
|
|
||||||
filter_by(id=subq.as_scalar()).\
|
|
||||||
update({'bar': newbar})
|
|
||||||
|
|
||||||
For reference, this emits approximagely the following SQL statement:
|
|
||||||
|
|
||||||
UPDATE bar SET bar = ${newbar}
|
|
||||||
WHERE id=(SELECT bar_id FROM foo WHERE id = ${foo_id} LIMIT 1);
|
|
||||||
|
|
||||||
* Passing an active session between methods. Sessions should only be passed
|
|
||||||
to private methods. The private method must use a subtransaction; otherwise
|
|
||||||
SQLAlchemy will throw an error when you call session.begin() on an existing
|
|
||||||
transaction. Public methods should not accept a session parameter and should
|
|
||||||
not be involved in sessions within the caller's scope.
|
|
||||||
|
|
||||||
Note that this incurs more overhead in SQLAlchemy than the above means
|
|
||||||
due to nesting transactions, and it is not possible to implicitly retry
|
|
||||||
failed database operations when using this approach.
|
|
||||||
|
|
||||||
This also makes code somewhat more difficult to read and debug, because a
|
|
||||||
single database transaction spans more than one method. Error handling
|
|
||||||
becomes less clear in this situation. When this is needed for code clarity,
|
|
||||||
it should be clearly documented.
|
|
||||||
|
|
||||||
def myfunc(foo):
|
|
||||||
session = get_session()
|
|
||||||
with session.begin():
|
|
||||||
# do some database things
|
|
||||||
bar = _private_func(foo, session)
|
|
||||||
return bar
|
|
||||||
|
|
||||||
def _private_func(foo, session=None):
|
|
||||||
if not session:
|
|
||||||
session = get_session()
|
|
||||||
with session.begin(subtransaction=True):
|
|
||||||
# do some other database things
|
|
||||||
return bar
|
|
||||||
|
|
||||||
|
|
||||||
There are some things which it is best to avoid:
|
|
||||||
|
|
||||||
* Don't keep a transaction open any longer than necessary.
|
|
||||||
|
|
||||||
This means that your "with session.begin()" block should be as short
|
|
||||||
as possible, while still containing all the related calls for that
|
|
||||||
transaction.
|
|
||||||
|
|
||||||
* Avoid "with_lockmode('UPDATE')" when possible.
|
|
||||||
|
|
||||||
In MySQL/InnoDB, when a "SELECT ... FOR UPDATE" query does not match
|
|
||||||
any rows, it will take a gap-lock. This is a form of write-lock on the
|
|
||||||
"gap" where no rows exist, and prevents any other writes to that space.
|
|
||||||
This can effectively prevent any INSERT into a table by locking the gap
|
|
||||||
at the end of the index. Similar problems will occur if the SELECT FOR UPDATE
|
|
||||||
has an overly broad WHERE clause, or doesn't properly use an index.
|
|
||||||
|
|
||||||
One idea proposed at ODS Fall '12 was to use a normal SELECT to test the
|
|
||||||
number of rows matching a query, and if only one row is returned,
|
|
||||||
then issue the SELECT FOR UPDATE.
|
|
||||||
|
|
||||||
The better long-term solution is to use INSERT .. ON DUPLICATE KEY UPDATE.
|
|
||||||
However, this can not be done until the "deleted" columns are removed and
|
|
||||||
proper UNIQUE constraints are added to the tables.
|
|
||||||
|
|
||||||
|
|
||||||
Enabling soft deletes:
|
|
||||||
|
|
||||||
* To use/enable soft-deletes, the SoftDeleteMixin must be added
|
|
||||||
to your model class. For example:
|
|
||||||
|
|
||||||
class NovaBase(models.SoftDeleteMixin, models.ModelBase):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
Efficient use of soft deletes:
|
|
||||||
|
|
||||||
* There are two possible ways to mark a record as deleted:
|
|
||||||
model.soft_delete() and query.soft_delete().
|
|
||||||
|
|
||||||
model.soft_delete() method works with single already fetched entry.
|
|
||||||
query.soft_delete() makes only one db request for all entries that correspond
|
|
||||||
to query.
|
|
||||||
|
|
||||||
* In almost all cases you should use query.soft_delete(). Some examples:
|
|
||||||
|
|
||||||
def soft_delete_bar():
|
|
||||||
count = model_query(BarModel).find(some_condition).soft_delete()
|
|
||||||
if count == 0:
|
|
||||||
raise Exception("0 entries were soft deleted")
|
|
||||||
|
|
||||||
def complex_soft_delete_with_synchronization_bar(session=None):
|
|
||||||
if session is None:
|
|
||||||
session = get_session()
|
|
||||||
with session.begin(subtransactions=True):
|
|
||||||
count = model_query(BarModel).\
|
|
||||||
find(some_condition).\
|
|
||||||
soft_delete(synchronize_session=True)
|
|
||||||
# Here synchronize_session is required, because we
|
|
||||||
# don't know what is going on in outer session.
|
|
||||||
if count == 0:
|
|
||||||
raise Exception("0 entries were soft deleted")
|
|
||||||
|
|
||||||
* There is only one situation where model.soft_delete() is appropriate: when
|
|
||||||
you fetch a single record, work with it, and mark it as deleted in the same
|
|
||||||
transaction.
|
|
||||||
|
|
||||||
def soft_delete_bar_model():
|
|
||||||
session = get_session()
|
|
||||||
with session.begin():
|
|
||||||
bar_ref = model_query(BarModel).find(some_condition).first()
|
|
||||||
# Work with bar_ref
|
|
||||||
bar_ref.soft_delete(session=session)
|
|
||||||
|
|
||||||
However, if you need to work with all entries that correspond to query and
|
|
||||||
then soft delete them you should use query.soft_delete() method:
|
|
||||||
|
|
||||||
def soft_delete_multi_models():
|
|
||||||
session = get_session()
|
|
||||||
with session.begin():
|
|
||||||
query = model_query(BarModel, session=session).\
|
|
||||||
find(some_condition)
|
|
||||||
model_refs = query.all()
|
|
||||||
# Work with model_refs
|
|
||||||
query.soft_delete(synchronize_session=False)
|
|
||||||
# synchronize_session=False should be set if there is no outer
|
|
||||||
# session and these entries are not used after this.
|
|
||||||
|
|
||||||
When working with many rows, it is very important to use query.soft_delete,
|
|
||||||
which issues a single query. Using model.soft_delete(), as in the following
|
|
||||||
example, is very inefficient.
|
|
||||||
|
|
||||||
for bar_ref in bar_refs:
|
|
||||||
bar_ref.soft_delete(session=session)
|
|
||||||
# This will produce count(bar_refs) db requests.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import os.path
|
|
||||||
import re
|
|
||||||
import time
|
|
||||||
|
|
||||||
from eventlet import greenthread
|
|
||||||
from oslo.config import cfg
|
|
||||||
from sqlalchemy import exc as sqla_exc
|
|
||||||
import sqlalchemy.interfaces
|
|
||||||
import sqlalchemy.orm
|
|
||||||
from sqlalchemy.pool import NullPool, StaticPool
|
|
||||||
from sqlalchemy.sql.expression import literal_column
|
|
||||||
|
|
||||||
from galloper.openstack.common.db import exception
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import timeutils
|
|
||||||
|
|
||||||
|
|
||||||
sql_opts = [
|
|
||||||
cfg.StrOpt('sql_connection',
|
|
||||||
default='sqlite:///' +
|
|
||||||
os.path.abspath(os.path.join(os.path.dirname(__file__),
|
|
||||||
'../', '$sqlite_db')),
|
|
||||||
help='The SQLAlchemy connection string used to connect to the '
|
|
||||||
'database',
|
|
||||||
secret=True),
|
|
||||||
cfg.StrOpt('sqlite_db',
|
|
||||||
default='galloper.sqlite',
|
|
||||||
help='the filename to use with sqlite'),
|
|
||||||
cfg.IntOpt('sql_idle_timeout',
|
|
||||||
default=3600,
|
|
||||||
help='timeout before idle sql connections are reaped'),
|
|
||||||
cfg.BoolOpt('sqlite_synchronous',
|
|
||||||
default=True,
|
|
||||||
help='If passed, use synchronous mode for sqlite'),
|
|
||||||
cfg.IntOpt('sql_min_pool_size',
|
|
||||||
default=1,
|
|
||||||
help='Minimum number of SQL connections to keep open in a '
|
|
||||||
'pool'),
|
|
||||||
cfg.IntOpt('sql_max_pool_size',
|
|
||||||
default=5,
|
|
||||||
help='Maximum number of SQL connections to keep open in a '
|
|
||||||
'pool'),
|
|
||||||
cfg.IntOpt('sql_max_retries',
|
|
||||||
default=10,
|
|
||||||
help='maximum db connection retries during startup. '
|
|
||||||
'(setting -1 implies an infinite retry count)'),
|
|
||||||
cfg.IntOpt('sql_retry_interval',
|
|
||||||
default=10,
|
|
||||||
help='interval between retries of opening a sql connection'),
|
|
||||||
cfg.IntOpt('sql_max_overflow',
|
|
||||||
default=None,
|
|
||||||
help='If set, use this value for max_overflow with sqlalchemy'),
|
|
||||||
cfg.IntOpt('sql_connection_debug',
|
|
||||||
default=0,
|
|
||||||
help='Verbosity of SQL debugging information. 0=None, '
|
|
||||||
'100=Everything'),
|
|
||||||
cfg.BoolOpt('sql_connection_trace',
|
|
||||||
default=False,
|
|
||||||
help='Add python stack traces to SQL as comment strings'),
|
|
||||||
]
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF.register_opts(sql_opts)
|
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
_ENGINE = None
|
|
||||||
_MAKER = None
|
|
||||||
|
|
||||||
|
|
||||||
def set_defaults(sql_connection, sqlite_db):
|
|
||||||
"""Set defaults for configuration variables."""
|
|
||||||
cfg.set_defaults(sql_opts,
|
|
||||||
sql_connection=sql_connection,
|
|
||||||
sqlite_db=sqlite_db)
|
|
||||||
|
|
||||||
|
|
||||||
def get_session(autocommit=True, expire_on_commit=False):
|
|
||||||
"""Return a SQLAlchemy session."""
|
|
||||||
global _MAKER
|
|
||||||
|
|
||||||
if _MAKER is None:
|
|
||||||
engine = get_engine()
|
|
||||||
_MAKER = get_maker(engine, autocommit, expire_on_commit)
|
|
||||||
|
|
||||||
session = _MAKER()
|
|
||||||
return session
|
|
||||||
|
|
||||||
|
|
||||||
# note(boris-42): In current versions of DB backends unique constraint
|
|
||||||
# violation messages follow the structure:
|
|
||||||
#
|
|
||||||
# sqlite:
|
|
||||||
# 1 column - (IntegrityError) column c1 is not unique
|
|
||||||
# N columns - (IntegrityError) column c1, c2, ..., N are not unique
|
|
||||||
#
|
|
||||||
# postgres:
|
|
||||||
# 1 column - (IntegrityError) duplicate key value violates unique
|
|
||||||
# constraint "users_c1_key"
|
|
||||||
# N columns - (IntegrityError) duplicate key value violates unique
|
|
||||||
# constraint "name_of_our_constraint"
|
|
||||||
#
|
|
||||||
# mysql:
|
|
||||||
# 1 column - (IntegrityError) (1062, "Duplicate entry 'value_of_c1' for key
|
|
||||||
# 'c1'")
|
|
||||||
# N columns - (IntegrityError) (1062, "Duplicate entry 'values joined
|
|
||||||
# with -' for key 'name_of_our_constraint'")
|
|
||||||
_DUP_KEY_RE_DB = {
|
|
||||||
"sqlite": re.compile(r"^.*columns?([^)]+)(is|are)\s+not\s+unique$"),
|
|
||||||
"postgresql": re.compile(r"^.*duplicate\s+key.*\"([^\"]+)\"\s*\n.*$"),
|
|
||||||
"mysql": re.compile(r"^.*\(1062,.*'([^\']+)'\"\)$")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def raise_if_duplicate_entry_error(integrity_error, engine_name):
|
|
||||||
"""
|
|
||||||
In this function will be raised DBDuplicateEntry exception if integrity
|
|
||||||
error wrap unique constraint violation.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def get_columns_from_uniq_cons_or_name(columns):
|
|
||||||
# note(boris-42): UniqueConstraint name convention: "uniq_c1_x_c2_x_c3"
|
|
||||||
# means that columns c1, c2, c3 are in UniqueConstraint.
|
|
||||||
uniqbase = "uniq_"
|
|
||||||
if not columns.startswith(uniqbase):
|
|
||||||
if engine_name == "postgresql":
|
|
||||||
return [columns[columns.index("_") + 1:columns.rindex("_")]]
|
|
||||||
return [columns]
|
|
||||||
return columns[len(uniqbase):].split("_x_")
|
|
||||||
|
|
||||||
if engine_name not in ["mysql", "sqlite", "postgresql"]:
|
|
||||||
return
|
|
||||||
|
|
||||||
m = _DUP_KEY_RE_DB[engine_name].match(integrity_error.message)
|
|
||||||
if not m:
|
|
||||||
return
|
|
||||||
columns = m.group(1)
|
|
||||||
|
|
||||||
if engine_name == "sqlite":
|
|
||||||
columns = columns.strip().split(", ")
|
|
||||||
else:
|
|
||||||
columns = get_columns_from_uniq_cons_or_name(columns)
|
|
||||||
raise exception.DBDuplicateEntry(columns, integrity_error)
|
|
||||||
|
|
||||||
|
|
||||||
# NOTE(comstud): In current versions of DB backends, Deadlock violation
|
|
||||||
# messages follow the structure:
|
|
||||||
#
|
|
||||||
# mysql:
|
|
||||||
# (OperationalError) (1213, 'Deadlock found when trying to get lock; try '
|
|
||||||
# 'restarting transaction') <query_str> <query_args>
|
|
||||||
_DEADLOCK_RE_DB = {
|
|
||||||
"mysql": re.compile(r"^.*\(1213, 'Deadlock.*")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def raise_if_deadlock_error(operational_error, engine_name):
|
|
||||||
"""
|
|
||||||
Raise DBDeadlock exception if OperationalError contains a Deadlock
|
|
||||||
condition.
|
|
||||||
"""
|
|
||||||
re = _DEADLOCK_RE_DB.get(engine_name)
|
|
||||||
if re is None:
|
|
||||||
return
|
|
||||||
m = re.match(operational_error.message)
|
|
||||||
if not m:
|
|
||||||
return
|
|
||||||
raise exception.DBDeadlock(operational_error)
|
|
||||||
|
|
||||||
|
|
||||||
def wrap_db_error(f):
|
|
||||||
def _wrap(*args, **kwargs):
|
|
||||||
try:
|
|
||||||
return f(*args, **kwargs)
|
|
||||||
except UnicodeEncodeError:
|
|
||||||
raise exception.DBInvalidUnicodeParameter()
|
|
||||||
# note(boris-42): We should catch unique constraint violation and
|
|
||||||
# wrap it by our own DBDuplicateEntry exception. Unique constraint
|
|
||||||
# violation is wrapped by IntegrityError.
|
|
||||||
except sqla_exc.OperationalError, e:
|
|
||||||
raise_if_deadlock_error(e, get_engine().name)
|
|
||||||
# NOTE(comstud): A lot of code is checking for OperationalError
|
|
||||||
# so let's not wrap it for now.
|
|
||||||
raise
|
|
||||||
except sqla_exc.IntegrityError, e:
|
|
||||||
# note(boris-42): SqlAlchemy doesn't unify errors from different
|
|
||||||
# DBs so we must do this. Also in some tables (for example
|
|
||||||
# instance_types) there are more than one unique constraint. This
|
|
||||||
# means we should get names of columns, which values violate
|
|
||||||
# unique constraint, from error message.
|
|
||||||
raise_if_duplicate_entry_error(e, get_engine().name)
|
|
||||||
raise exception.DBError(e)
|
|
||||||
except Exception, e:
|
|
||||||
LOG.exception(_('DB exception wrapped.'))
|
|
||||||
raise exception.DBError(e)
|
|
||||||
_wrap.func_name = f.func_name
|
|
||||||
return _wrap
|
|
||||||
|
|
||||||
|
|
||||||
def get_engine():
|
|
||||||
"""Return a SQLAlchemy engine."""
|
|
||||||
global _ENGINE
|
|
||||||
if _ENGINE is None:
|
|
||||||
_ENGINE = create_engine(CONF.sql_connection)
|
|
||||||
return _ENGINE
|
|
||||||
|
|
||||||
|
|
||||||
def synchronous_switch_listener(dbapi_conn, connection_rec):
|
|
||||||
"""Switch sqlite connections to non-synchronous mode."""
|
|
||||||
dbapi_conn.execute("PRAGMA synchronous = OFF")
|
|
||||||
|
|
||||||
|
|
||||||
def add_regexp_listener(dbapi_con, con_record):
|
|
||||||
"""Add REGEXP function to sqlite connections."""
|
|
||||||
|
|
||||||
def regexp(expr, item):
|
|
||||||
reg = re.compile(expr)
|
|
||||||
return reg.search(unicode(item)) is not None
|
|
||||||
dbapi_con.create_function('regexp', 2, regexp)
|
|
||||||
|
|
||||||
|
|
||||||
def greenthread_yield(dbapi_con, con_record):
|
|
||||||
"""
|
|
||||||
Ensure other greenthreads get a chance to execute by forcing a context
|
|
||||||
switch. With common database backends (eg MySQLdb and sqlite), there is
|
|
||||||
no implicit yield caused by network I/O since they are implemented by
|
|
||||||
C libraries that eventlet cannot monkey patch.
|
|
||||||
"""
|
|
||||||
greenthread.sleep(0)
|
|
||||||
|
|
||||||
|
|
||||||
def ping_listener(dbapi_conn, connection_rec, connection_proxy):
|
|
||||||
"""
|
|
||||||
Ensures that MySQL connections checked out of the
|
|
||||||
pool are alive.
|
|
||||||
|
|
||||||
Borrowed from:
|
|
||||||
http://groups.google.com/group/sqlalchemy/msg/a4ce563d802c929f
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
dbapi_conn.cursor().execute('select 1')
|
|
||||||
except dbapi_conn.OperationalError, ex:
|
|
||||||
if ex.args[0] in (2006, 2013, 2014, 2045, 2055):
|
|
||||||
LOG.warn(_('Got mysql server has gone away: %s'), ex)
|
|
||||||
raise sqla_exc.DisconnectionError("Database server went away")
|
|
||||||
else:
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
|
||||||
def is_db_connection_error(args):
|
|
||||||
"""Return True if error in connecting to db."""
|
|
||||||
# NOTE(adam_g): This is currently MySQL specific and needs to be extended
|
|
||||||
# to support Postgres and others.
|
|
||||||
conn_err_codes = ('2002', '2003', '2006')
|
|
||||||
for err_code in conn_err_codes:
|
|
||||||
if args.find(err_code) != -1:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def create_engine(sql_connection):
|
|
||||||
"""Return a new SQLAlchemy engine."""
|
|
||||||
connection_dict = sqlalchemy.engine.url.make_url(sql_connection)
|
|
||||||
|
|
||||||
engine_args = {
|
|
||||||
"pool_recycle": CONF.sql_idle_timeout,
|
|
||||||
"echo": False,
|
|
||||||
'convert_unicode': True,
|
|
||||||
}
|
|
||||||
|
|
||||||
# Map our SQL debug level to SQLAlchemy's options
|
|
||||||
if CONF.sql_connection_debug >= 100:
|
|
||||||
engine_args['echo'] = 'debug'
|
|
||||||
elif CONF.sql_connection_debug >= 50:
|
|
||||||
engine_args['echo'] = True
|
|
||||||
|
|
||||||
if "sqlite" in connection_dict.drivername:
|
|
||||||
engine_args["poolclass"] = NullPool
|
|
||||||
|
|
||||||
if CONF.sql_connection == "sqlite://":
|
|
||||||
engine_args["poolclass"] = StaticPool
|
|
||||||
engine_args["connect_args"] = {'check_same_thread': False}
|
|
||||||
else:
|
|
||||||
engine_args['pool_size'] = CONF.sql_max_pool_size
|
|
||||||
if CONF.sql_max_overflow is not None:
|
|
||||||
engine_args['max_overflow'] = CONF.sql_max_overflow
|
|
||||||
|
|
||||||
engine = sqlalchemy.create_engine(sql_connection, **engine_args)
|
|
||||||
|
|
||||||
sqlalchemy.event.listen(engine, 'checkin', greenthread_yield)
|
|
||||||
|
|
||||||
if 'mysql' in connection_dict.drivername:
|
|
||||||
sqlalchemy.event.listen(engine, 'checkout', ping_listener)
|
|
||||||
elif 'sqlite' in connection_dict.drivername:
|
|
||||||
if not CONF.sqlite_synchronous:
|
|
||||||
sqlalchemy.event.listen(engine, 'connect',
|
|
||||||
synchronous_switch_listener)
|
|
||||||
sqlalchemy.event.listen(engine, 'connect', add_regexp_listener)
|
|
||||||
|
|
||||||
if (CONF.sql_connection_trace and
|
|
||||||
engine.dialect.dbapi.__name__ == 'MySQLdb'):
|
|
||||||
patch_mysqldb_with_stacktrace_comments()
|
|
||||||
|
|
||||||
try:
|
|
||||||
engine.connect()
|
|
||||||
except sqla_exc.OperationalError, e:
|
|
||||||
if not is_db_connection_error(e.args[0]):
|
|
||||||
raise
|
|
||||||
|
|
||||||
remaining = CONF.sql_max_retries
|
|
||||||
if remaining == -1:
|
|
||||||
remaining = 'infinite'
|
|
||||||
while True:
|
|
||||||
msg = _('SQL connection failed. %s attempts left.')
|
|
||||||
LOG.warn(msg % remaining)
|
|
||||||
if remaining != 'infinite':
|
|
||||||
remaining -= 1
|
|
||||||
time.sleep(CONF.sql_retry_interval)
|
|
||||||
try:
|
|
||||||
engine.connect()
|
|
||||||
break
|
|
||||||
except sqla_exc.OperationalError, e:
|
|
||||||
if (remaining != 'infinite' and remaining == 0) or \
|
|
||||||
not is_db_connection_error(e.args[0]):
|
|
||||||
raise
|
|
||||||
return engine
|
|
||||||
|
|
||||||
|
|
||||||
class Query(sqlalchemy.orm.query.Query):
|
|
||||||
"""Subclass of sqlalchemy.query with soft_delete() method."""
|
|
||||||
def soft_delete(self, synchronize_session='evaluate'):
|
|
||||||
return self.update({'deleted': literal_column('id'),
|
|
||||||
'updated_at': literal_column('updated_at'),
|
|
||||||
'deleted_at': timeutils.utcnow()},
|
|
||||||
synchronize_session=synchronize_session)
|
|
||||||
|
|
||||||
|
|
||||||
class Session(sqlalchemy.orm.session.Session):
|
|
||||||
"""Custom Session class to avoid SqlAlchemy Session monkey patching."""
|
|
||||||
@wrap_db_error
|
|
||||||
def query(self, *args, **kwargs):
|
|
||||||
return super(Session, self).query(*args, **kwargs)
|
|
||||||
|
|
||||||
@wrap_db_error
|
|
||||||
def flush(self, *args, **kwargs):
|
|
||||||
return super(Session, self).flush(*args, **kwargs)
|
|
||||||
|
|
||||||
@wrap_db_error
|
|
||||||
def execute(self, *args, **kwargs):
|
|
||||||
return super(Session, self).execute(*args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def get_maker(engine, autocommit=True, expire_on_commit=False):
|
|
||||||
"""Return a SQLAlchemy sessionmaker using the given engine."""
|
|
||||||
return sqlalchemy.orm.sessionmaker(bind=engine,
|
|
||||||
class_=Session,
|
|
||||||
autocommit=autocommit,
|
|
||||||
expire_on_commit=expire_on_commit,
|
|
||||||
query_cls=Query)
|
|
||||||
|
|
||||||
|
|
||||||
def patch_mysqldb_with_stacktrace_comments():
|
|
||||||
"""Adds current stack trace as a comment in queries by patching
|
|
||||||
MySQLdb.cursors.BaseCursor._do_query.
|
|
||||||
"""
|
|
||||||
import MySQLdb.cursors
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
old_mysql_do_query = MySQLdb.cursors.BaseCursor._do_query
|
|
||||||
|
|
||||||
def _do_query(self, q):
|
|
||||||
stack = ''
|
|
||||||
for file, line, method, function in traceback.extract_stack():
|
|
||||||
# exclude various common things from trace
|
|
||||||
if file.endswith('session.py') and method == '_do_query':
|
|
||||||
continue
|
|
||||||
if file.endswith('api.py') and method == 'wrapper':
|
|
||||||
continue
|
|
||||||
if file.endswith('utils.py') and method == '_inner':
|
|
||||||
continue
|
|
||||||
if file.endswith('exception.py') and method == '_wrap':
|
|
||||||
continue
|
|
||||||
# db/api is just a wrapper around db/sqlalchemy/api
|
|
||||||
if file.endswith('db/api.py'):
|
|
||||||
continue
|
|
||||||
# only trace inside galloper
|
|
||||||
index = file.rfind('galloper')
|
|
||||||
if index == -1:
|
|
||||||
continue
|
|
||||||
stack += "File:%s:%s Method:%s() Line:%s | " \
|
|
||||||
% (file[index:], line, method, function)
|
|
||||||
|
|
||||||
# strip trailing " | " from stack
|
|
||||||
if stack:
|
|
||||||
stack = stack[:-3]
|
|
||||||
qq = "%s /* %s */" % (q, stack)
|
|
||||||
else:
|
|
||||||
qq = q
|
|
||||||
old_mysql_do_query(self, qq)
|
|
||||||
|
|
||||||
setattr(MySQLdb.cursors.BaseCursor, '_do_query', _do_query)
|
|
||||||
@@ -1,132 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2010 United States Government as represented by the
|
|
||||||
# Administrator of the National Aeronautics and Space Administration.
|
|
||||||
# Copyright 2010-2011 OpenStack Foundation.
|
|
||||||
# Copyright 2012 Justin Santa Barbara
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""Implementation of paginate query."""
|
|
||||||
|
|
||||||
import sqlalchemy
|
|
||||||
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
|
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class InvalidSortKey(Exception):
|
|
||||||
message = _("Sort key supplied was not valid.")
|
|
||||||
|
|
||||||
|
|
||||||
# copy from glance/db/sqlalchemy/api.py
|
|
||||||
def paginate_query(query, model, limit, sort_keys, marker=None,
|
|
||||||
sort_dir=None, sort_dirs=None):
|
|
||||||
"""Returns a query with sorting / pagination criteria added.
|
|
||||||
|
|
||||||
Pagination works by requiring a unique sort_key, specified by sort_keys.
|
|
||||||
(If sort_keys is not unique, then we risk looping through values.)
|
|
||||||
We use the last row in the previous page as the 'marker' for pagination.
|
|
||||||
So we must return values that follow the passed marker in the order.
|
|
||||||
With a single-valued sort_key, this would be easy: sort_key > X.
|
|
||||||
With a compound-values sort_key, (k1, k2, k3) we must do this to repeat
|
|
||||||
the lexicographical ordering:
|
|
||||||
(k1 > X1) or (k1 == X1 && k2 > X2) or (k1 == X1 && k2 == X2 && k3 > X3)
|
|
||||||
|
|
||||||
We also have to cope with different sort_directions.
|
|
||||||
|
|
||||||
Typically, the id of the last row is used as the client-facing pagination
|
|
||||||
marker, then the actual marker object must be fetched from the db and
|
|
||||||
passed in to us as marker.
|
|
||||||
|
|
||||||
:param query: the query object to which we should add paging/sorting
|
|
||||||
:param model: the ORM model class
|
|
||||||
:param limit: maximum number of items to return
|
|
||||||
:param sort_keys: array of attributes by which results should be sorted
|
|
||||||
:param marker: the last item of the previous page; we returns the next
|
|
||||||
results after this value.
|
|
||||||
:param sort_dir: direction in which results should be sorted (asc, desc)
|
|
||||||
:param sort_dirs: per-column array of sort_dirs, corresponding to sort_keys
|
|
||||||
|
|
||||||
:rtype: sqlalchemy.orm.query.Query
|
|
||||||
:return: The query with sorting/pagination added.
|
|
||||||
"""
|
|
||||||
|
|
||||||
if 'id' not in sort_keys:
|
|
||||||
# TODO(justinsb): If this ever gives a false-positive, check
|
|
||||||
# the actual primary key, rather than assuming its id
|
|
||||||
LOG.warn(_('Id not in sort_keys; is sort_keys unique?'))
|
|
||||||
|
|
||||||
assert(not (sort_dir and sort_dirs))
|
|
||||||
|
|
||||||
# Default the sort direction to ascending
|
|
||||||
if sort_dirs is None and sort_dir is None:
|
|
||||||
sort_dir = 'asc'
|
|
||||||
|
|
||||||
# Ensure a per-column sort direction
|
|
||||||
if sort_dirs is None:
|
|
||||||
sort_dirs = [sort_dir for _sort_key in sort_keys]
|
|
||||||
|
|
||||||
assert(len(sort_dirs) == len(sort_keys))
|
|
||||||
|
|
||||||
# Add sorting
|
|
||||||
for current_sort_key, current_sort_dir in zip(sort_keys, sort_dirs):
|
|
||||||
sort_dir_func = {
|
|
||||||
'asc': sqlalchemy.asc,
|
|
||||||
'desc': sqlalchemy.desc,
|
|
||||||
}[current_sort_dir]
|
|
||||||
|
|
||||||
try:
|
|
||||||
sort_key_attr = getattr(model, current_sort_key)
|
|
||||||
except AttributeError:
|
|
||||||
raise InvalidSortKey()
|
|
||||||
query = query.order_by(sort_dir_func(sort_key_attr))
|
|
||||||
|
|
||||||
# Add pagination
|
|
||||||
if marker is not None:
|
|
||||||
marker_values = []
|
|
||||||
for sort_key in sort_keys:
|
|
||||||
v = getattr(marker, sort_key)
|
|
||||||
marker_values.append(v)
|
|
||||||
|
|
||||||
# Build up an array of sort criteria as in the docstring
|
|
||||||
criteria_list = []
|
|
||||||
for i in xrange(0, len(sort_keys)):
|
|
||||||
crit_attrs = []
|
|
||||||
for j in xrange(0, i):
|
|
||||||
model_attr = getattr(model, sort_keys[j])
|
|
||||||
crit_attrs.append((model_attr == marker_values[j]))
|
|
||||||
|
|
||||||
model_attr = getattr(model, sort_keys[i])
|
|
||||||
if sort_dirs[i] == 'desc':
|
|
||||||
crit_attrs.append((model_attr < marker_values[i]))
|
|
||||||
elif sort_dirs[i] == 'asc':
|
|
||||||
crit_attrs.append((model_attr > marker_values[i]))
|
|
||||||
else:
|
|
||||||
raise ValueError(_("Unknown sort direction, "
|
|
||||||
"must be 'desc' or 'asc'"))
|
|
||||||
|
|
||||||
criteria = sqlalchemy.sql.and_(*crit_attrs)
|
|
||||||
criteria_list.append(criteria)
|
|
||||||
|
|
||||||
f = sqlalchemy.sql.or_(*criteria_list)
|
|
||||||
query = query.filter(f)
|
|
||||||
|
|
||||||
if limit is not None:
|
|
||||||
query = query.limit(limit)
|
|
||||||
|
|
||||||
return query
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2011 OpenStack Foundation.
|
|
||||||
# Copyright 2012, Red Hat, Inc.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""
|
|
||||||
Exception related utilities.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import contextlib
|
|
||||||
import logging
|
|
||||||
import sys
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
|
||||||
def save_and_reraise_exception():
|
|
||||||
"""Save current exception, run some code and then re-raise.
|
|
||||||
|
|
||||||
In some cases the exception context can be cleared, resulting in None
|
|
||||||
being attempted to be re-raised after an exception handler is run. This
|
|
||||||
can happen when eventlet switches greenthreads or when running an
|
|
||||||
exception handler, code raises and catches an exception. In both
|
|
||||||
cases the exception context will be cleared.
|
|
||||||
|
|
||||||
To work around this, we save the exception state, run handler code, and
|
|
||||||
then re-raise the original exception. If another exception occurs, the
|
|
||||||
saved exception is logged and the new exception is re-raised.
|
|
||||||
"""
|
|
||||||
type_, value, tb = sys.exc_info()
|
|
||||||
try:
|
|
||||||
yield
|
|
||||||
except Exception:
|
|
||||||
logging.error(_('Original exception being dropped: %s'),
|
|
||||||
traceback.format_exception(type_, value, tb))
|
|
||||||
raise
|
|
||||||
raise type_, value, tb
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2012 Red Hat, Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""
|
|
||||||
gettext for openstack-common modules.
|
|
||||||
|
|
||||||
Usual usage in an openstack.common module:
|
|
||||||
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
"""
|
|
||||||
|
|
||||||
import gettext
|
|
||||||
|
|
||||||
|
|
||||||
t = gettext.translation('openstack-common', 'locale', fallback=True)
|
|
||||||
|
|
||||||
|
|
||||||
def _(msg):
|
|
||||||
return t.ugettext(msg)
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2011 OpenStack Foundation.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""
|
|
||||||
Import related utilities and helper functions.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
|
|
||||||
def import_class(import_str):
|
|
||||||
"""Returns a class from a string including module and class"""
|
|
||||||
mod_str, _sep, class_str = import_str.rpartition('.')
|
|
||||||
try:
|
|
||||||
__import__(mod_str)
|
|
||||||
return getattr(sys.modules[mod_str], class_str)
|
|
||||||
except (ValueError, AttributeError):
|
|
||||||
raise ImportError('Class %s cannot be found (%s)' %
|
|
||||||
(class_str,
|
|
||||||
traceback.format_exception(*sys.exc_info())))
|
|
||||||
|
|
||||||
|
|
||||||
def import_object(import_str, *args, **kwargs):
|
|
||||||
"""Import a class and return an instance of it."""
|
|
||||||
return import_class(import_str)(*args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def import_object_ns(name_space, import_str, *args, **kwargs):
|
|
||||||
"""
|
|
||||||
Import a class and return an instance of it, first by trying
|
|
||||||
to find the class in a default namespace, then failing back to
|
|
||||||
a full path if not found in the default namespace.
|
|
||||||
"""
|
|
||||||
import_value = "%s.%s" % (name_space, import_str)
|
|
||||||
try:
|
|
||||||
return import_class(import_value)(*args, **kwargs)
|
|
||||||
except ImportError:
|
|
||||||
return import_class(import_str)(*args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def import_module(import_str):
|
|
||||||
"""Import a module."""
|
|
||||||
__import__(import_str)
|
|
||||||
return sys.modules[import_str]
|
|
||||||
|
|
||||||
|
|
||||||
def try_import(import_str, default=None):
|
|
||||||
"""Try to import a module and if it fails return default."""
|
|
||||||
try:
|
|
||||||
return import_module(import_str)
|
|
||||||
except ImportError:
|
|
||||||
return default
|
|
||||||
@@ -1,141 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2010 United States Government as represented by the
|
|
||||||
# Administrator of the National Aeronautics and Space Administration.
|
|
||||||
# Copyright 2011 Justin Santa Barbara
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
'''
|
|
||||||
JSON related utilities.
|
|
||||||
|
|
||||||
This module provides a few things:
|
|
||||||
|
|
||||||
1) A handy function for getting an object down to something that can be
|
|
||||||
JSON serialized. See to_primitive().
|
|
||||||
|
|
||||||
2) Wrappers around loads() and dumps(). The dumps() wrapper will
|
|
||||||
automatically use to_primitive() for you if needed.
|
|
||||||
|
|
||||||
3) This sets up anyjson to use the loads() and dumps() wrappers if anyjson
|
|
||||||
is available.
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
import datetime
|
|
||||||
import functools
|
|
||||||
import inspect
|
|
||||||
import itertools
|
|
||||||
import json
|
|
||||||
import xmlrpclib
|
|
||||||
|
|
||||||
from galloper.openstack.common import timeutils
|
|
||||||
|
|
||||||
|
|
||||||
def to_primitive(value, convert_instances=False, convert_datetime=True,
|
|
||||||
level=0, max_depth=3):
|
|
||||||
"""Convert a complex object into primitives.
|
|
||||||
|
|
||||||
Handy for JSON serialization. We can optionally handle instances,
|
|
||||||
but since this is a recursive function, we could have cyclical
|
|
||||||
data structures.
|
|
||||||
|
|
||||||
To handle cyclical data structures we could track the actual objects
|
|
||||||
visited in a set, but not all objects are hashable. Instead we just
|
|
||||||
track the depth of the object inspections and don't go too deep.
|
|
||||||
|
|
||||||
Therefore, convert_instances=True is lossy ... be aware.
|
|
||||||
|
|
||||||
"""
|
|
||||||
nasty = [inspect.ismodule, inspect.isclass, inspect.ismethod,
|
|
||||||
inspect.isfunction, inspect.isgeneratorfunction,
|
|
||||||
inspect.isgenerator, inspect.istraceback, inspect.isframe,
|
|
||||||
inspect.iscode, inspect.isbuiltin, inspect.isroutine,
|
|
||||||
inspect.isabstract]
|
|
||||||
for test in nasty:
|
|
||||||
if test(value):
|
|
||||||
return unicode(value)
|
|
||||||
|
|
||||||
# value of itertools.count doesn't get caught by inspects
|
|
||||||
# above and results in infinite loop when list(value) is called.
|
|
||||||
if type(value) == itertools.count:
|
|
||||||
return unicode(value)
|
|
||||||
|
|
||||||
# FIXME(vish): Workaround for LP bug 852095. Without this workaround,
|
|
||||||
# tests that raise an exception in a mocked method that
|
|
||||||
# has a @wrap_exception with a notifier will fail. If
|
|
||||||
# we up the dependency to 0.5.4 (when it is released) we
|
|
||||||
# can remove this workaround.
|
|
||||||
if getattr(value, '__module__', None) == 'mox':
|
|
||||||
return 'mock'
|
|
||||||
|
|
||||||
if level > max_depth:
|
|
||||||
return '?'
|
|
||||||
|
|
||||||
# The try block may not be necessary after the class check above,
|
|
||||||
# but just in case ...
|
|
||||||
try:
|
|
||||||
recursive = functools.partial(to_primitive,
|
|
||||||
convert_instances=convert_instances,
|
|
||||||
convert_datetime=convert_datetime,
|
|
||||||
level=level,
|
|
||||||
max_depth=max_depth)
|
|
||||||
# It's not clear why xmlrpclib created their own DateTime type, but
|
|
||||||
# for our purposes, make it a datetime type which is explicitly
|
|
||||||
# handled
|
|
||||||
if isinstance(value, xmlrpclib.DateTime):
|
|
||||||
value = datetime.datetime(*tuple(value.timetuple())[:6])
|
|
||||||
|
|
||||||
if isinstance(value, (list, tuple)):
|
|
||||||
return [recursive(v) for v in value]
|
|
||||||
elif isinstance(value, dict):
|
|
||||||
return dict((k, recursive(v)) for k, v in value.iteritems())
|
|
||||||
elif convert_datetime and isinstance(value, datetime.datetime):
|
|
||||||
return timeutils.strtime(value)
|
|
||||||
elif hasattr(value, 'iteritems'):
|
|
||||||
return recursive(dict(value.iteritems()), level=level + 1)
|
|
||||||
elif hasattr(value, '__iter__'):
|
|
||||||
return recursive(list(value))
|
|
||||||
elif convert_instances and hasattr(value, '__dict__'):
|
|
||||||
# Likely an instance of something. Watch for cycles.
|
|
||||||
# Ignore class member vars.
|
|
||||||
return recursive(value.__dict__, level=level + 1)
|
|
||||||
else:
|
|
||||||
return value
|
|
||||||
except TypeError:
|
|
||||||
# Class objects are tricky since they may define something like
|
|
||||||
# __iter__ defined but it isn't callable as list().
|
|
||||||
return unicode(value)
|
|
||||||
|
|
||||||
|
|
||||||
def dumps(value, default=to_primitive, **kwargs):
|
|
||||||
return json.dumps(value, default=default, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def loads(s):
|
|
||||||
return json.loads(s)
|
|
||||||
|
|
||||||
|
|
||||||
def load(s):
|
|
||||||
return json.load(s)
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
|
||||||
import anyjson
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
anyjson._modules.append((__name__, 'dumps', TypeError,
|
|
||||||
'loads', ValueError, 'load'))
|
|
||||||
anyjson.force_implementation(__name__)
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2011 OpenStack Foundation.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""Greenthread local storage of variables using weak references"""
|
|
||||||
|
|
||||||
import weakref
|
|
||||||
|
|
||||||
from eventlet import corolocal
|
|
||||||
|
|
||||||
|
|
||||||
class WeakLocal(corolocal.local):
|
|
||||||
def __getattribute__(self, attr):
|
|
||||||
rval = corolocal.local.__getattribute__(self, attr)
|
|
||||||
if rval:
|
|
||||||
# NOTE(mikal): this bit is confusing. What is stored is a weak
|
|
||||||
# reference, not the value itself. We therefore need to lookup
|
|
||||||
# the weak reference and return the inner value here.
|
|
||||||
rval = rval()
|
|
||||||
return rval
|
|
||||||
|
|
||||||
def __setattr__(self, attr, value):
|
|
||||||
value = weakref.ref(value)
|
|
||||||
return corolocal.local.__setattr__(self, attr, value)
|
|
||||||
|
|
||||||
|
|
||||||
# NOTE(mikal): the name "store" should be deprecated in the future
|
|
||||||
store = WeakLocal()
|
|
||||||
|
|
||||||
# A "weak" store uses weak references and allows an object to fall out of scope
|
|
||||||
# when it falls out of scope in the code that uses the thread local storage. A
|
|
||||||
# "strong" store will hold a reference to the object so that it never falls out
|
|
||||||
# of scope.
|
|
||||||
weak_store = WeakLocal()
|
|
||||||
strong_store = corolocal.local
|
|
||||||
@@ -1,522 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2011 OpenStack Foundation.
|
|
||||||
# Copyright 2010 United States Government as represented by the
|
|
||||||
# Administrator of the National Aeronautics and Space Administration.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""Openstack logging handler.
|
|
||||||
|
|
||||||
This module adds to logging functionality by adding the option to specify
|
|
||||||
a context object when calling the various log methods. If the context object
|
|
||||||
is not specified, default formatting is used. Additionally, an instance uuid
|
|
||||||
may be passed as part of the log message, which is intended to make it easier
|
|
||||||
for admins to find messages related to a specific instance.
|
|
||||||
|
|
||||||
It also allows setting of formatting information through conf.
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
import cStringIO
|
|
||||||
import inspect
|
|
||||||
import itertools
|
|
||||||
import logging
|
|
||||||
import logging.config
|
|
||||||
import logging.handlers
|
|
||||||
import os
|
|
||||||
import stat
|
|
||||||
import sys
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import jsonutils
|
|
||||||
from galloper.openstack.common import local
|
|
||||||
from galloper.openstack.common import notifier
|
|
||||||
|
|
||||||
|
|
||||||
_DEFAULT_LOG_FORMAT = "%(asctime)s %(levelname)8s [%(name)s] %(message)s"
|
|
||||||
_DEFAULT_LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
|
|
||||||
|
|
||||||
common_cli_opts = [
|
|
||||||
cfg.BoolOpt('debug',
|
|
||||||
short='d',
|
|
||||||
default=False,
|
|
||||||
help='Print debugging output (set logging level to '
|
|
||||||
'DEBUG instead of default WARNING level).'),
|
|
||||||
cfg.BoolOpt('verbose',
|
|
||||||
short='v',
|
|
||||||
default=False,
|
|
||||||
help='Print more verbose output (set logging level to '
|
|
||||||
'INFO instead of default WARNING level).'),
|
|
||||||
]
|
|
||||||
|
|
||||||
logging_cli_opts = [
|
|
||||||
cfg.StrOpt('log-config',
|
|
||||||
metavar='PATH',
|
|
||||||
help='If this option is specified, the logging configuration '
|
|
||||||
'file specified is used and overrides any other logging '
|
|
||||||
'options specified. Please see the Python logging module '
|
|
||||||
'documentation for details on logging configuration '
|
|
||||||
'files.'),
|
|
||||||
cfg.StrOpt('log-format',
|
|
||||||
default=_DEFAULT_LOG_FORMAT,
|
|
||||||
metavar='FORMAT',
|
|
||||||
help='A logging.Formatter log message format string which may '
|
|
||||||
'use any of the available logging.LogRecord attributes. '
|
|
||||||
'Default: %(default)s'),
|
|
||||||
cfg.StrOpt('log-date-format',
|
|
||||||
default=_DEFAULT_LOG_DATE_FORMAT,
|
|
||||||
metavar='DATE_FORMAT',
|
|
||||||
help='Format string for %%(asctime)s in log records. '
|
|
||||||
'Default: %(default)s'),
|
|
||||||
cfg.StrOpt('log-file',
|
|
||||||
metavar='PATH',
|
|
||||||
deprecated_name='logfile',
|
|
||||||
help='(Optional) Name of log file to output to. '
|
|
||||||
'If not set, logging will go to stdout.'),
|
|
||||||
cfg.StrOpt('log-dir',
|
|
||||||
deprecated_name='logdir',
|
|
||||||
help='(Optional) The directory to keep log files in '
|
|
||||||
'(will be prepended to --log-file)'),
|
|
||||||
cfg.BoolOpt('use-syslog',
|
|
||||||
default=False,
|
|
||||||
help='Use syslog for logging.'),
|
|
||||||
cfg.StrOpt('syslog-log-facility',
|
|
||||||
default='LOG_USER',
|
|
||||||
help='syslog facility to receive log lines')
|
|
||||||
]
|
|
||||||
|
|
||||||
generic_log_opts = [
|
|
||||||
cfg.BoolOpt('use_stderr',
|
|
||||||
default=True,
|
|
||||||
help='Log output to standard error'),
|
|
||||||
cfg.StrOpt('logfile_mode',
|
|
||||||
default='0644',
|
|
||||||
help='Default file mode used when creating log files'),
|
|
||||||
]
|
|
||||||
|
|
||||||
log_opts = [
|
|
||||||
cfg.StrOpt('logging_context_format_string',
|
|
||||||
default='%(asctime)s.%(msecs)03d %(levelname)s %(name)s '
|
|
||||||
'[%(request_id)s %(user)s %(tenant)s] %(instance)s'
|
|
||||||
'%(message)s',
|
|
||||||
help='format string to use for log messages with context'),
|
|
||||||
cfg.StrOpt('logging_default_format_string',
|
|
||||||
default='%(asctime)s.%(msecs)03d %(process)d %(levelname)s '
|
|
||||||
'%(name)s [-] %(instance)s%(message)s',
|
|
||||||
help='format string to use for log messages without context'),
|
|
||||||
cfg.StrOpt('logging_debug_format_suffix',
|
|
||||||
default='%(funcName)s %(pathname)s:%(lineno)d',
|
|
||||||
help='data to append to log format when level is DEBUG'),
|
|
||||||
cfg.StrOpt('logging_exception_prefix',
|
|
||||||
default='%(asctime)s.%(msecs)03d %(process)d TRACE %(name)s '
|
|
||||||
'%(instance)s',
|
|
||||||
help='prefix each line of exception output with this format'),
|
|
||||||
cfg.ListOpt('default_log_levels',
|
|
||||||
default=[
|
|
||||||
'amqplib=WARN',
|
|
||||||
'sqlalchemy=WARN',
|
|
||||||
'boto=WARN',
|
|
||||||
'suds=INFO',
|
|
||||||
'keystone=INFO',
|
|
||||||
'eventlet.wsgi.server=WARN'
|
|
||||||
],
|
|
||||||
help='list of logger=LEVEL pairs'),
|
|
||||||
cfg.BoolOpt('publish_errors',
|
|
||||||
default=False,
|
|
||||||
help='publish error events'),
|
|
||||||
cfg.BoolOpt('fatal_deprecations',
|
|
||||||
default=False,
|
|
||||||
help='make deprecations fatal'),
|
|
||||||
|
|
||||||
# NOTE(mikal): there are two options here because sometimes we are handed
|
|
||||||
# a full instance (and could include more information), and other times we
|
|
||||||
# are just handed a UUID for the instance.
|
|
||||||
cfg.StrOpt('instance_format',
|
|
||||||
default='[instance: %(uuid)s] ',
|
|
||||||
help='If an instance is passed with the log message, format '
|
|
||||||
'it like this'),
|
|
||||||
cfg.StrOpt('instance_uuid_format',
|
|
||||||
default='[instance: %(uuid)s] ',
|
|
||||||
help='If an instance UUID is passed with the log message, '
|
|
||||||
'format it like this'),
|
|
||||||
]
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF.register_cli_opts(common_cli_opts)
|
|
||||||
CONF.register_cli_opts(logging_cli_opts)
|
|
||||||
CONF.register_opts(generic_log_opts)
|
|
||||||
CONF.register_opts(log_opts)
|
|
||||||
|
|
||||||
# our new audit level
|
|
||||||
# NOTE(jkoelker) Since we synthesized an audit level, make the logging
|
|
||||||
# module aware of it so it acts like other levels.
|
|
||||||
logging.AUDIT = logging.INFO + 1
|
|
||||||
logging.addLevelName(logging.AUDIT, 'AUDIT')
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
|
||||||
NullHandler = logging.NullHandler
|
|
||||||
except AttributeError: # NOTE(jkoelker) NullHandler added in Python 2.7
|
|
||||||
class NullHandler(logging.Handler):
|
|
||||||
def handle(self, record):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def emit(self, record):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def createLock(self):
|
|
||||||
self.lock = None
|
|
||||||
|
|
||||||
|
|
||||||
def _dictify_context(context):
|
|
||||||
if context is None:
|
|
||||||
return None
|
|
||||||
if not isinstance(context, dict) and getattr(context, 'to_dict', None):
|
|
||||||
context = context.to_dict()
|
|
||||||
return context
|
|
||||||
|
|
||||||
|
|
||||||
def _get_binary_name():
|
|
||||||
return os.path.basename(inspect.stack()[-1][1])
|
|
||||||
|
|
||||||
|
|
||||||
def _get_log_file_path(binary=None):
|
|
||||||
logfile = CONF.log_file
|
|
||||||
logdir = CONF.log_dir
|
|
||||||
|
|
||||||
if logfile and not logdir:
|
|
||||||
return logfile
|
|
||||||
|
|
||||||
if logfile and logdir:
|
|
||||||
return os.path.join(logdir, logfile)
|
|
||||||
|
|
||||||
if logdir:
|
|
||||||
binary = binary or _get_binary_name()
|
|
||||||
return '%s.log' % (os.path.join(logdir, binary),)
|
|
||||||
|
|
||||||
|
|
||||||
class ContextAdapter(logging.LoggerAdapter):
|
|
||||||
warn = logging.LoggerAdapter.warning
|
|
||||||
|
|
||||||
def __init__(self, logger, project_name, version_string):
|
|
||||||
self.logger = logger
|
|
||||||
self.project = project_name
|
|
||||||
self.version = version_string
|
|
||||||
|
|
||||||
def audit(self, msg, *args, **kwargs):
|
|
||||||
self.log(logging.AUDIT, msg, *args, **kwargs)
|
|
||||||
|
|
||||||
def deprecated(self, msg, *args, **kwargs):
|
|
||||||
stdmsg = _("Deprecated: %s") % msg
|
|
||||||
if CONF.fatal_deprecations:
|
|
||||||
self.critical(stdmsg, *args, **kwargs)
|
|
||||||
raise DeprecatedConfig(msg=stdmsg)
|
|
||||||
else:
|
|
||||||
self.warn(stdmsg, *args, **kwargs)
|
|
||||||
|
|
||||||
def process(self, msg, kwargs):
|
|
||||||
if 'extra' not in kwargs:
|
|
||||||
kwargs['extra'] = {}
|
|
||||||
extra = kwargs['extra']
|
|
||||||
|
|
||||||
context = kwargs.pop('context', None)
|
|
||||||
if not context:
|
|
||||||
context = getattr(local.store, 'context', None)
|
|
||||||
if context:
|
|
||||||
extra.update(_dictify_context(context))
|
|
||||||
|
|
||||||
instance = kwargs.pop('instance', None)
|
|
||||||
instance_extra = ''
|
|
||||||
if instance:
|
|
||||||
instance_extra = CONF.instance_format % instance
|
|
||||||
else:
|
|
||||||
instance_uuid = kwargs.pop('instance_uuid', None)
|
|
||||||
if instance_uuid:
|
|
||||||
instance_extra = (CONF.instance_uuid_format
|
|
||||||
% {'uuid': instance_uuid})
|
|
||||||
extra.update({'instance': instance_extra})
|
|
||||||
|
|
||||||
extra.update({"project": self.project})
|
|
||||||
extra.update({"version": self.version})
|
|
||||||
extra['extra'] = extra.copy()
|
|
||||||
return msg, kwargs
|
|
||||||
|
|
||||||
|
|
||||||
class JSONFormatter(logging.Formatter):
|
|
||||||
def __init__(self, fmt=None, datefmt=None):
|
|
||||||
# NOTE(jkoelker) we ignore the fmt argument, but its still there
|
|
||||||
# since logging.config.fileConfig passes it.
|
|
||||||
self.datefmt = datefmt
|
|
||||||
|
|
||||||
def formatException(self, ei, strip_newlines=True):
|
|
||||||
lines = traceback.format_exception(*ei)
|
|
||||||
if strip_newlines:
|
|
||||||
lines = [itertools.ifilter(
|
|
||||||
lambda x: x,
|
|
||||||
line.rstrip().splitlines()) for line in lines]
|
|
||||||
lines = list(itertools.chain(*lines))
|
|
||||||
return lines
|
|
||||||
|
|
||||||
def format(self, record):
|
|
||||||
message = {'message': record.getMessage(),
|
|
||||||
'asctime': self.formatTime(record, self.datefmt),
|
|
||||||
'name': record.name,
|
|
||||||
'msg': record.msg,
|
|
||||||
'args': record.args,
|
|
||||||
'levelname': record.levelname,
|
|
||||||
'levelno': record.levelno,
|
|
||||||
'pathname': record.pathname,
|
|
||||||
'filename': record.filename,
|
|
||||||
'module': record.module,
|
|
||||||
'lineno': record.lineno,
|
|
||||||
'funcname': record.funcName,
|
|
||||||
'created': record.created,
|
|
||||||
'msecs': record.msecs,
|
|
||||||
'relative_created': record.relativeCreated,
|
|
||||||
'thread': record.thread,
|
|
||||||
'thread_name': record.threadName,
|
|
||||||
'process_name': record.processName,
|
|
||||||
'process': record.process,
|
|
||||||
'traceback': None}
|
|
||||||
|
|
||||||
if hasattr(record, 'extra'):
|
|
||||||
message['extra'] = record.extra
|
|
||||||
|
|
||||||
if record.exc_info:
|
|
||||||
message['traceback'] = self.formatException(record.exc_info)
|
|
||||||
|
|
||||||
return jsonutils.dumps(message)
|
|
||||||
|
|
||||||
|
|
||||||
class PublishErrorsHandler(logging.Handler):
|
|
||||||
def emit(self, record):
|
|
||||||
if ('galloper.openstack.common.notifier.log_notifier' in
|
|
||||||
CONF.notification_driver):
|
|
||||||
return
|
|
||||||
notifier.api.notify(None, 'error.publisher',
|
|
||||||
'error_notification',
|
|
||||||
notifier.api.ERROR,
|
|
||||||
dict(error=record.msg))
|
|
||||||
|
|
||||||
|
|
||||||
def _create_logging_excepthook(product_name):
|
|
||||||
def logging_excepthook(type, value, tb):
|
|
||||||
extra = {}
|
|
||||||
if CONF.verbose:
|
|
||||||
extra['exc_info'] = (type, value, tb)
|
|
||||||
getLogger(product_name).critical(str(value), **extra)
|
|
||||||
return logging_excepthook
|
|
||||||
|
|
||||||
|
|
||||||
def setup(product_name):
|
|
||||||
"""Setup logging."""
|
|
||||||
if CONF.log_config:
|
|
||||||
logging.config.fileConfig(CONF.log_config)
|
|
||||||
else:
|
|
||||||
_setup_logging_from_conf()
|
|
||||||
sys.excepthook = _create_logging_excepthook(product_name)
|
|
||||||
|
|
||||||
|
|
||||||
def set_defaults(logging_context_format_string):
|
|
||||||
cfg.set_defaults(log_opts,
|
|
||||||
logging_context_format_string=
|
|
||||||
logging_context_format_string)
|
|
||||||
|
|
||||||
|
|
||||||
def _find_facility_from_conf():
|
|
||||||
facility_names = logging.handlers.SysLogHandler.facility_names
|
|
||||||
facility = getattr(logging.handlers.SysLogHandler,
|
|
||||||
CONF.syslog_log_facility,
|
|
||||||
None)
|
|
||||||
|
|
||||||
if facility is None and CONF.syslog_log_facility in facility_names:
|
|
||||||
facility = facility_names.get(CONF.syslog_log_facility)
|
|
||||||
|
|
||||||
if facility is None:
|
|
||||||
valid_facilities = facility_names.keys()
|
|
||||||
consts = ['LOG_AUTH', 'LOG_AUTHPRIV', 'LOG_CRON', 'LOG_DAEMON',
|
|
||||||
'LOG_FTP', 'LOG_KERN', 'LOG_LPR', 'LOG_MAIL', 'LOG_NEWS',
|
|
||||||
'LOG_AUTH', 'LOG_SYSLOG', 'LOG_USER', 'LOG_UUCP',
|
|
||||||
'LOG_LOCAL0', 'LOG_LOCAL1', 'LOG_LOCAL2', 'LOG_LOCAL3',
|
|
||||||
'LOG_LOCAL4', 'LOG_LOCAL5', 'LOG_LOCAL6', 'LOG_LOCAL7']
|
|
||||||
valid_facilities.extend(consts)
|
|
||||||
raise TypeError(_('syslog facility must be one of: %s') %
|
|
||||||
', '.join("'%s'" % fac
|
|
||||||
for fac in valid_facilities))
|
|
||||||
|
|
||||||
return facility
|
|
||||||
|
|
||||||
|
|
||||||
def _setup_logging_from_conf():
|
|
||||||
log_root = getLogger(None).logger
|
|
||||||
for handler in log_root.handlers:
|
|
||||||
log_root.removeHandler(handler)
|
|
||||||
|
|
||||||
if CONF.use_syslog:
|
|
||||||
facility = _find_facility_from_conf()
|
|
||||||
syslog = logging.handlers.SysLogHandler(address='/dev/log',
|
|
||||||
facility=facility)
|
|
||||||
log_root.addHandler(syslog)
|
|
||||||
|
|
||||||
logpath = _get_log_file_path()
|
|
||||||
if logpath:
|
|
||||||
filelog = logging.handlers.WatchedFileHandler(logpath)
|
|
||||||
log_root.addHandler(filelog)
|
|
||||||
|
|
||||||
mode = int(CONF.logfile_mode, 8)
|
|
||||||
st = os.stat(logpath)
|
|
||||||
if st.st_mode != (stat.S_IFREG | mode):
|
|
||||||
os.chmod(logpath, mode)
|
|
||||||
|
|
||||||
if CONF.use_stderr:
|
|
||||||
streamlog = ColorHandler()
|
|
||||||
log_root.addHandler(streamlog)
|
|
||||||
|
|
||||||
elif not CONF.log_file:
|
|
||||||
# pass sys.stdout as a positional argument
|
|
||||||
# python2.6 calls the argument strm, in 2.7 it's stream
|
|
||||||
streamlog = logging.StreamHandler(sys.stdout)
|
|
||||||
log_root.addHandler(streamlog)
|
|
||||||
|
|
||||||
if CONF.publish_errors:
|
|
||||||
log_root.addHandler(PublishErrorsHandler(logging.ERROR))
|
|
||||||
|
|
||||||
for handler in log_root.handlers:
|
|
||||||
datefmt = CONF.log_date_format
|
|
||||||
if CONF.log_format:
|
|
||||||
handler.setFormatter(logging.Formatter(fmt=CONF.log_format,
|
|
||||||
datefmt=datefmt))
|
|
||||||
else:
|
|
||||||
handler.setFormatter(LegacyFormatter(datefmt=datefmt))
|
|
||||||
|
|
||||||
if CONF.debug:
|
|
||||||
log_root.setLevel(logging.DEBUG)
|
|
||||||
elif CONF.verbose:
|
|
||||||
log_root.setLevel(logging.INFO)
|
|
||||||
else:
|
|
||||||
log_root.setLevel(logging.WARNING)
|
|
||||||
|
|
||||||
level = logging.NOTSET
|
|
||||||
for pair in CONF.default_log_levels:
|
|
||||||
mod, _sep, level_name = pair.partition('=')
|
|
||||||
level = logging.getLevelName(level_name)
|
|
||||||
logger = logging.getLogger(mod)
|
|
||||||
logger.setLevel(level)
|
|
||||||
for handler in log_root.handlers:
|
|
||||||
logger.addHandler(handler)
|
|
||||||
|
|
||||||
_loggers = {}
|
|
||||||
|
|
||||||
|
|
||||||
def getLogger(name='unknown', version='unknown'):
|
|
||||||
if name not in _loggers:
|
|
||||||
_loggers[name] = ContextAdapter(logging.getLogger(name),
|
|
||||||
name,
|
|
||||||
version)
|
|
||||||
return _loggers[name]
|
|
||||||
|
|
||||||
|
|
||||||
class WritableLogger(object):
|
|
||||||
"""A thin wrapper that responds to `write` and logs."""
|
|
||||||
|
|
||||||
def __init__(self, logger, level=logging.INFO):
|
|
||||||
self.logger = logger
|
|
||||||
self.level = level
|
|
||||||
|
|
||||||
def write(self, msg):
|
|
||||||
self.logger.log(self.level, msg)
|
|
||||||
|
|
||||||
|
|
||||||
class LegacyFormatter(logging.Formatter):
|
|
||||||
"""A context.RequestContext aware formatter configured through flags.
|
|
||||||
|
|
||||||
The flags used to set format strings are: logging_context_format_string
|
|
||||||
and logging_default_format_string. You can also specify
|
|
||||||
logging_debug_format_suffix to append extra formatting if the log level is
|
|
||||||
debug.
|
|
||||||
|
|
||||||
For information about what variables are available for the formatter see:
|
|
||||||
http://docs.python.org/library/logging.html#formatter
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
def format(self, record):
|
|
||||||
"""Uses contextstring if request_id is set, otherwise default."""
|
|
||||||
# NOTE(sdague): default the fancier formating params
|
|
||||||
# to an empty string so we don't throw an exception if
|
|
||||||
# they get used
|
|
||||||
for key in ('instance', 'color'):
|
|
||||||
if key not in record.__dict__:
|
|
||||||
record.__dict__[key] = ''
|
|
||||||
|
|
||||||
if record.__dict__.get('request_id', None):
|
|
||||||
self._fmt = CONF.logging_context_format_string
|
|
||||||
else:
|
|
||||||
self._fmt = CONF.logging_default_format_string
|
|
||||||
|
|
||||||
if (record.levelno == logging.DEBUG and
|
|
||||||
CONF.logging_debug_format_suffix):
|
|
||||||
self._fmt += " " + CONF.logging_debug_format_suffix
|
|
||||||
|
|
||||||
# Cache this on the record, Logger will respect our formated copy
|
|
||||||
if record.exc_info:
|
|
||||||
record.exc_text = self.formatException(record.exc_info, record)
|
|
||||||
return logging.Formatter.format(self, record)
|
|
||||||
|
|
||||||
def formatException(self, exc_info, record=None):
|
|
||||||
"""Format exception output with CONF.logging_exception_prefix."""
|
|
||||||
if not record:
|
|
||||||
return logging.Formatter.formatException(self, exc_info)
|
|
||||||
|
|
||||||
stringbuffer = cStringIO.StringIO()
|
|
||||||
traceback.print_exception(exc_info[0], exc_info[1], exc_info[2],
|
|
||||||
None, stringbuffer)
|
|
||||||
lines = stringbuffer.getvalue().split('\n')
|
|
||||||
stringbuffer.close()
|
|
||||||
|
|
||||||
if CONF.logging_exception_prefix.find('%(asctime)') != -1:
|
|
||||||
record.asctime = self.formatTime(record, self.datefmt)
|
|
||||||
|
|
||||||
formatted_lines = []
|
|
||||||
for line in lines:
|
|
||||||
pl = CONF.logging_exception_prefix % record.__dict__
|
|
||||||
fl = '%s%s' % (pl, line)
|
|
||||||
formatted_lines.append(fl)
|
|
||||||
return '\n'.join(formatted_lines)
|
|
||||||
|
|
||||||
|
|
||||||
class ColorHandler(logging.StreamHandler):
|
|
||||||
LEVEL_COLORS = {
|
|
||||||
logging.DEBUG: '\033[00;32m', # GREEN
|
|
||||||
logging.INFO: '\033[00;36m', # CYAN
|
|
||||||
logging.AUDIT: '\033[01;36m', # BOLD CYAN
|
|
||||||
logging.WARN: '\033[01;33m', # BOLD YELLOW
|
|
||||||
logging.ERROR: '\033[01;31m', # BOLD RED
|
|
||||||
logging.CRITICAL: '\033[01;31m', # BOLD RED
|
|
||||||
}
|
|
||||||
|
|
||||||
def format(self, record):
|
|
||||||
record.color = self.LEVEL_COLORS[record.levelno]
|
|
||||||
return logging.StreamHandler.format(self, record)
|
|
||||||
|
|
||||||
|
|
||||||
class DeprecatedConfig(Exception):
|
|
||||||
message = _("Fatal call to deprecated config: %(msg)s")
|
|
||||||
|
|
||||||
def __init__(self, msg):
|
|
||||||
super(Exception, self).__init__(self.message % dict(msg=msg))
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
# Copyright 2011 OpenStack Foundation.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
@@ -1,183 +0,0 @@
|
|||||||
# Copyright 2011 OpenStack Foundation.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
import uuid
|
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common import context
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import importutils
|
|
||||||
from galloper.openstack.common import jsonutils
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
from galloper.openstack.common import timeutils
|
|
||||||
|
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
notifier_opts = [
|
|
||||||
cfg.MultiStrOpt('notification_driver',
|
|
||||||
default=[],
|
|
||||||
deprecated_name='list_notifier_drivers',
|
|
||||||
help='Driver or drivers to handle sending notifications'),
|
|
||||||
cfg.StrOpt('default_notification_level',
|
|
||||||
default='INFO',
|
|
||||||
help='Default notification level for outgoing notifications'),
|
|
||||||
cfg.StrOpt('default_publisher_id',
|
|
||||||
default='$host',
|
|
||||||
help='Default publisher_id for outgoing notifications'),
|
|
||||||
]
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF.register_opts(notifier_opts)
|
|
||||||
|
|
||||||
WARN = 'WARN'
|
|
||||||
INFO = 'INFO'
|
|
||||||
ERROR = 'ERROR'
|
|
||||||
CRITICAL = 'CRITICAL'
|
|
||||||
DEBUG = 'DEBUG'
|
|
||||||
|
|
||||||
log_levels = (DEBUG, WARN, INFO, ERROR, CRITICAL)
|
|
||||||
|
|
||||||
|
|
||||||
class BadPriorityException(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def notify_decorator(name, fn):
|
|
||||||
""" decorator for notify which is used from utils.monkey_patch()
|
|
||||||
|
|
||||||
:param name: name of the function
|
|
||||||
:param function: - object of the function
|
|
||||||
:returns: function -- decorated function
|
|
||||||
|
|
||||||
"""
|
|
||||||
def wrapped_func(*args, **kwarg):
|
|
||||||
body = {}
|
|
||||||
body['args'] = []
|
|
||||||
body['kwarg'] = {}
|
|
||||||
for arg in args:
|
|
||||||
body['args'].append(arg)
|
|
||||||
for key in kwarg:
|
|
||||||
body['kwarg'][key] = kwarg[key]
|
|
||||||
|
|
||||||
ctxt = context.get_context_from_function_and_args(fn, args, kwarg)
|
|
||||||
notify(ctxt,
|
|
||||||
CONF.default_publisher_id,
|
|
||||||
name,
|
|
||||||
CONF.default_notification_level,
|
|
||||||
body)
|
|
||||||
return fn(*args, **kwarg)
|
|
||||||
return wrapped_func
|
|
||||||
|
|
||||||
|
|
||||||
def publisher_id(service, host=None):
|
|
||||||
if not host:
|
|
||||||
host = CONF.host
|
|
||||||
return "%s.%s" % (service, host)
|
|
||||||
|
|
||||||
|
|
||||||
def notify(context, publisher_id, event_type, priority, payload):
|
|
||||||
"""Sends a notification using the specified driver
|
|
||||||
|
|
||||||
:param publisher_id: the source worker_type.host of the message
|
|
||||||
:param event_type: the literal type of event (ex. Instance Creation)
|
|
||||||
:param priority: patterned after the enumeration of Python logging
|
|
||||||
levels in the set (DEBUG, WARN, INFO, ERROR, CRITICAL)
|
|
||||||
:param payload: A python dictionary of attributes
|
|
||||||
|
|
||||||
Outgoing message format includes the above parameters, and appends the
|
|
||||||
following:
|
|
||||||
|
|
||||||
message_id
|
|
||||||
a UUID representing the id for this notification
|
|
||||||
|
|
||||||
timestamp
|
|
||||||
the GMT timestamp the notification was sent at
|
|
||||||
|
|
||||||
The composite message will be constructed as a dictionary of the above
|
|
||||||
attributes, which will then be sent via the transport mechanism defined
|
|
||||||
by the driver.
|
|
||||||
|
|
||||||
Message example::
|
|
||||||
|
|
||||||
{'message_id': str(uuid.uuid4()),
|
|
||||||
'publisher_id': 'compute.host1',
|
|
||||||
'timestamp': timeutils.utcnow(),
|
|
||||||
'priority': 'WARN',
|
|
||||||
'event_type': 'compute.create_instance',
|
|
||||||
'payload': {'instance_id': 12, ... }}
|
|
||||||
|
|
||||||
"""
|
|
||||||
if priority not in log_levels:
|
|
||||||
raise BadPriorityException(
|
|
||||||
_('%s not in valid priorities') % priority)
|
|
||||||
|
|
||||||
# Ensure everything is JSON serializable.
|
|
||||||
payload = jsonutils.to_primitive(payload, convert_instances=True)
|
|
||||||
|
|
||||||
msg = dict(message_id=str(uuid.uuid4()),
|
|
||||||
publisher_id=publisher_id,
|
|
||||||
event_type=event_type,
|
|
||||||
priority=priority,
|
|
||||||
payload=payload,
|
|
||||||
timestamp=str(timeutils.utcnow()))
|
|
||||||
|
|
||||||
for driver in _get_drivers():
|
|
||||||
try:
|
|
||||||
driver.notify(context, msg)
|
|
||||||
except Exception as e:
|
|
||||||
LOG.exception(_("Problem '%(e)s' attempting to "
|
|
||||||
"send to notification system. "
|
|
||||||
"Payload=%(payload)s")
|
|
||||||
% dict(e=e, payload=payload))
|
|
||||||
|
|
||||||
|
|
||||||
_drivers = None
|
|
||||||
|
|
||||||
|
|
||||||
def _get_drivers():
|
|
||||||
"""Instantiate, cache, and return drivers based on the CONF."""
|
|
||||||
global _drivers
|
|
||||||
if _drivers is None:
|
|
||||||
_drivers = {}
|
|
||||||
for notification_driver in CONF.notification_driver:
|
|
||||||
add_driver(notification_driver)
|
|
||||||
|
|
||||||
return _drivers.values()
|
|
||||||
|
|
||||||
|
|
||||||
def add_driver(notification_driver):
|
|
||||||
"""Add a notification driver at runtime."""
|
|
||||||
# Make sure the driver list is initialized.
|
|
||||||
_get_drivers()
|
|
||||||
if isinstance(notification_driver, basestring):
|
|
||||||
# Load and add
|
|
||||||
try:
|
|
||||||
driver = importutils.import_module(notification_driver)
|
|
||||||
_drivers[notification_driver] = driver
|
|
||||||
except ImportError:
|
|
||||||
LOG.exception(_("Failed to load notifier %s. "
|
|
||||||
"These notifications will not be sent.") %
|
|
||||||
notification_driver)
|
|
||||||
else:
|
|
||||||
# Driver is already loaded; just add the object.
|
|
||||||
_drivers[notification_driver] = notification_driver
|
|
||||||
|
|
||||||
|
|
||||||
def _reset_drivers():
|
|
||||||
"""Used by unit tests to reset the drivers."""
|
|
||||||
global _drivers
|
|
||||||
_drivers = None
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
# Copyright 2011 OpenStack Foundation.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common import jsonutils
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
|
|
||||||
|
|
||||||
def notify(_context, message):
|
|
||||||
"""Notifies the recipient of the desired event given the model.
|
|
||||||
Log notifications using openstack's default logging system"""
|
|
||||||
|
|
||||||
priority = message.get('priority',
|
|
||||||
CONF.default_notification_level)
|
|
||||||
priority = priority.lower()
|
|
||||||
logger = logging.getLogger(
|
|
||||||
'galloper.openstack.common.notification.%s' %
|
|
||||||
message['event_type'])
|
|
||||||
getattr(logger, priority)(jsonutils.dumps(message))
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
# Copyright 2011 OpenStack Foundation.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
|
|
||||||
def notify(_context, message):
|
|
||||||
"""Notifies the recipient of the desired event given the model"""
|
|
||||||
pass
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
# Copyright 2012 Red Hat, Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
from galloper.openstack.common.notifier import rpc_notifier
|
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
def notify(context, message):
|
|
||||||
"""Deprecated in Grizzly. Please use rpc_notifier instead."""
|
|
||||||
|
|
||||||
LOG.deprecated(_("The rabbit_notifier is now deprecated."
|
|
||||||
" Please use rpc_notifier instead."))
|
|
||||||
rpc_notifier.notify(context, message)
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
# Copyright 2011 OpenStack Foundation.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common import context as req_context
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
from galloper.openstack.common import rpc
|
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
notification_topic_opt = cfg.ListOpt(
|
|
||||||
'notification_topics', default=['notifications', ],
|
|
||||||
help='AMQP topic used for openstack notifications')
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF.register_opt(notification_topic_opt)
|
|
||||||
|
|
||||||
|
|
||||||
def notify(context, message):
|
|
||||||
"""Sends a notification via RPC"""
|
|
||||||
if not context:
|
|
||||||
context = req_context.get_admin_context()
|
|
||||||
priority = message.get('priority',
|
|
||||||
CONF.default_notification_level)
|
|
||||||
priority = priority.lower()
|
|
||||||
for topic in CONF.notification_topics:
|
|
||||||
topic = '%s.%s' % (topic, priority)
|
|
||||||
try:
|
|
||||||
rpc.notify(context, topic, message)
|
|
||||||
except Exception:
|
|
||||||
LOG.exception(_("Could not send notification to %(topic)s. "
|
|
||||||
"Payload=%(message)s"), locals())
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
# Copyright 2011 OpenStack Foundation.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
'''messaging based notification driver, with message envelopes'''
|
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common import context as req_context
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
from galloper.openstack.common import rpc
|
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
notification_topic_opt = cfg.ListOpt(
|
|
||||||
'topics', default=['notifications', ],
|
|
||||||
help='AMQP topic(s) used for openstack notifications')
|
|
||||||
|
|
||||||
opt_group = cfg.OptGroup(name='rpc_notifier2',
|
|
||||||
title='Options for rpc_notifier2')
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF.register_group(opt_group)
|
|
||||||
CONF.register_opt(notification_topic_opt, opt_group)
|
|
||||||
|
|
||||||
|
|
||||||
def notify(context, message):
|
|
||||||
"""Sends a notification via RPC"""
|
|
||||||
if not context:
|
|
||||||
context = req_context.get_admin_context()
|
|
||||||
priority = message.get('priority',
|
|
||||||
CONF.default_notification_level)
|
|
||||||
priority = priority.lower()
|
|
||||||
for topic in CONF.rpc_notifier2.topics:
|
|
||||||
topic = '%s.%s' % (topic, priority)
|
|
||||||
try:
|
|
||||||
rpc.notify(context, topic, message, envelope=True)
|
|
||||||
except Exception:
|
|
||||||
LOG.exception(_("Could not send notification to %(topic)s. "
|
|
||||||
"Payload=%(message)s"), locals())
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
# Copyright 2011 OpenStack Foundation.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
|
|
||||||
NOTIFICATIONS = []
|
|
||||||
|
|
||||||
|
|
||||||
def notify(_context, message):
|
|
||||||
"""Test notifier, stores notifications in memory for unittests."""
|
|
||||||
NOTIFICATIONS.append(message)
|
|
||||||
@@ -1,307 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2010 United States Government as represented by the
|
|
||||||
# Administrator of the National Aeronautics and Space Administration.
|
|
||||||
# All Rights Reserved.
|
|
||||||
# Copyright 2011 Red Hat, Inc.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""
|
|
||||||
A remote procedure call (rpc) abstraction.
|
|
||||||
|
|
||||||
For some wrappers that add message versioning to rpc, see:
|
|
||||||
rpc.dispatcher
|
|
||||||
rpc.proxy
|
|
||||||
"""
|
|
||||||
|
|
||||||
import inspect
|
|
||||||
import logging
|
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import importutils
|
|
||||||
from galloper.openstack.common import local
|
|
||||||
|
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
rpc_opts = [
|
|
||||||
cfg.StrOpt('rpc_backend',
|
|
||||||
default='%s.impl_kombu' % __package__,
|
|
||||||
help="The messaging module to use, defaults to kombu."),
|
|
||||||
cfg.IntOpt('rpc_thread_pool_size',
|
|
||||||
default=64,
|
|
||||||
help='Size of RPC thread pool'),
|
|
||||||
cfg.IntOpt('rpc_conn_pool_size',
|
|
||||||
default=30,
|
|
||||||
help='Size of RPC connection pool'),
|
|
||||||
cfg.IntOpt('rpc_response_timeout',
|
|
||||||
default=60,
|
|
||||||
help='Seconds to wait for a response from call or multicall'),
|
|
||||||
cfg.IntOpt('rpc_cast_timeout',
|
|
||||||
default=30,
|
|
||||||
help='Seconds to wait before a cast expires (TTL). '
|
|
||||||
'Only supported by impl_zmq.'),
|
|
||||||
cfg.ListOpt('allowed_rpc_exception_modules',
|
|
||||||
default=['galloper.openstack.common.exception',
|
|
||||||
'nova.exception',
|
|
||||||
'cinder.exception',
|
|
||||||
'exceptions',
|
|
||||||
],
|
|
||||||
help='Modules of exceptions that are permitted to be recreated'
|
|
||||||
'upon receiving exception data from an rpc call.'),
|
|
||||||
cfg.BoolOpt('fake_rabbit',
|
|
||||||
default=False,
|
|
||||||
help='If passed, use a fake RabbitMQ provider'),
|
|
||||||
cfg.StrOpt('control_exchange',
|
|
||||||
default='openstack',
|
|
||||||
help='AMQP exchange to connect to if using RabbitMQ or Qpid'),
|
|
||||||
]
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF.register_opts(rpc_opts)
|
|
||||||
|
|
||||||
|
|
||||||
def set_defaults(control_exchange):
|
|
||||||
cfg.set_defaults(rpc_opts,
|
|
||||||
control_exchange=control_exchange)
|
|
||||||
|
|
||||||
|
|
||||||
def create_connection(new=True):
|
|
||||||
"""Create a connection to the message bus used for rpc.
|
|
||||||
|
|
||||||
For some example usage of creating a connection and some consumers on that
|
|
||||||
connection, see nova.service.
|
|
||||||
|
|
||||||
:param new: Whether or not to create a new connection. A new connection
|
|
||||||
will be created by default. If new is False, the
|
|
||||||
implementation is free to return an existing connection from a
|
|
||||||
pool.
|
|
||||||
|
|
||||||
:returns: An instance of openstack.common.rpc.common.Connection
|
|
||||||
"""
|
|
||||||
return _get_impl().create_connection(CONF, new=new)
|
|
||||||
|
|
||||||
|
|
||||||
def _check_for_lock():
|
|
||||||
if not CONF.debug:
|
|
||||||
return None
|
|
||||||
|
|
||||||
if ((hasattr(local.strong_store, 'locks_held')
|
|
||||||
and local.strong_store.locks_held)):
|
|
||||||
stack = ' :: '.join([frame[3] for frame in inspect.stack()])
|
|
||||||
LOG.warn(_('A RPC is being made while holding a lock. The locks '
|
|
||||||
'currently held are %(locks)s. This is probably a bug. '
|
|
||||||
'Please report it. Include the following: [%(stack)s].'),
|
|
||||||
{'locks': local.strong_store.locks_held,
|
|
||||||
'stack': stack})
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def call(context, topic, msg, timeout=None, check_for_lock=False):
|
|
||||||
"""Invoke a remote method that returns something.
|
|
||||||
|
|
||||||
:param context: Information that identifies the user that has made this
|
|
||||||
request.
|
|
||||||
:param topic: The topic to send the rpc message to. This correlates to the
|
|
||||||
topic argument of
|
|
||||||
openstack.common.rpc.common.Connection.create_consumer()
|
|
||||||
and only applies when the consumer was created with
|
|
||||||
fanout=False.
|
|
||||||
:param msg: This is a dict in the form { "method" : "method_to_invoke",
|
|
||||||
"args" : dict_of_kwargs }
|
|
||||||
:param timeout: int, number of seconds to use for a response timeout.
|
|
||||||
If set, this overrides the rpc_response_timeout option.
|
|
||||||
:param check_for_lock: if True, a warning is emitted if a RPC call is made
|
|
||||||
with a lock held.
|
|
||||||
|
|
||||||
:returns: A dict from the remote method.
|
|
||||||
|
|
||||||
:raises: openstack.common.rpc.common.Timeout if a complete response
|
|
||||||
is not received before the timeout is reached.
|
|
||||||
"""
|
|
||||||
if check_for_lock:
|
|
||||||
_check_for_lock()
|
|
||||||
return _get_impl().call(CONF, context, topic, msg, timeout)
|
|
||||||
|
|
||||||
|
|
||||||
def cast(context, topic, msg):
|
|
||||||
"""Invoke a remote method that does not return anything.
|
|
||||||
|
|
||||||
:param context: Information that identifies the user that has made this
|
|
||||||
request.
|
|
||||||
:param topic: The topic to send the rpc message to. This correlates to the
|
|
||||||
topic argument of
|
|
||||||
openstack.common.rpc.common.Connection.create_consumer()
|
|
||||||
and only applies when the consumer was created with
|
|
||||||
fanout=False.
|
|
||||||
:param msg: This is a dict in the form { "method" : "method_to_invoke",
|
|
||||||
"args" : dict_of_kwargs }
|
|
||||||
|
|
||||||
:returns: None
|
|
||||||
"""
|
|
||||||
return _get_impl().cast(CONF, context, topic, msg)
|
|
||||||
|
|
||||||
|
|
||||||
def fanout_cast(context, topic, msg):
|
|
||||||
"""Broadcast a remote method invocation with no return.
|
|
||||||
|
|
||||||
This method will get invoked on all consumers that were set up with this
|
|
||||||
topic name and fanout=True.
|
|
||||||
|
|
||||||
:param context: Information that identifies the user that has made this
|
|
||||||
request.
|
|
||||||
:param topic: The topic to send the rpc message to. This correlates to the
|
|
||||||
topic argument of
|
|
||||||
openstack.common.rpc.common.Connection.create_consumer()
|
|
||||||
and only applies when the consumer was created with
|
|
||||||
fanout=True.
|
|
||||||
:param msg: This is a dict in the form { "method" : "method_to_invoke",
|
|
||||||
"args" : dict_of_kwargs }
|
|
||||||
|
|
||||||
:returns: None
|
|
||||||
"""
|
|
||||||
return _get_impl().fanout_cast(CONF, context, topic, msg)
|
|
||||||
|
|
||||||
|
|
||||||
def multicall(context, topic, msg, timeout=None, check_for_lock=False):
|
|
||||||
"""Invoke a remote method and get back an iterator.
|
|
||||||
|
|
||||||
In this case, the remote method will be returning multiple values in
|
|
||||||
separate messages, so the return values can be processed as the come in via
|
|
||||||
an iterator.
|
|
||||||
|
|
||||||
:param context: Information that identifies the user that has made this
|
|
||||||
request.
|
|
||||||
:param topic: The topic to send the rpc message to. This correlates to the
|
|
||||||
topic argument of
|
|
||||||
openstack.common.rpc.common.Connection.create_consumer()
|
|
||||||
and only applies when the consumer was created with
|
|
||||||
fanout=False.
|
|
||||||
:param msg: This is a dict in the form { "method" : "method_to_invoke",
|
|
||||||
"args" : dict_of_kwargs }
|
|
||||||
:param timeout: int, number of seconds to use for a response timeout.
|
|
||||||
If set, this overrides the rpc_response_timeout option.
|
|
||||||
:param check_for_lock: if True, a warning is emitted if a RPC call is made
|
|
||||||
with a lock held.
|
|
||||||
|
|
||||||
:returns: An iterator. The iterator will yield a tuple (N, X) where N is
|
|
||||||
an index that starts at 0 and increases by one for each value
|
|
||||||
returned and X is the Nth value that was returned by the remote
|
|
||||||
method.
|
|
||||||
|
|
||||||
:raises: openstack.common.rpc.common.Timeout if a complete response
|
|
||||||
is not received before the timeout is reached.
|
|
||||||
"""
|
|
||||||
if check_for_lock:
|
|
||||||
_check_for_lock()
|
|
||||||
return _get_impl().multicall(CONF, context, topic, msg, timeout)
|
|
||||||
|
|
||||||
|
|
||||||
def notify(context, topic, msg, envelope=False):
|
|
||||||
"""Send notification event.
|
|
||||||
|
|
||||||
:param context: Information that identifies the user that has made this
|
|
||||||
request.
|
|
||||||
:param topic: The topic to send the notification to.
|
|
||||||
:param msg: This is a dict of content of event.
|
|
||||||
:param envelope: Set to True to enable message envelope for notifications.
|
|
||||||
|
|
||||||
:returns: None
|
|
||||||
"""
|
|
||||||
return _get_impl().notify(cfg.CONF, context, topic, msg, envelope)
|
|
||||||
|
|
||||||
|
|
||||||
def cleanup():
|
|
||||||
"""Clean up resoruces in use by implementation.
|
|
||||||
|
|
||||||
Clean up any resources that have been allocated by the RPC implementation.
|
|
||||||
This is typically open connections to a messaging service. This function
|
|
||||||
would get called before an application using this API exits to allow
|
|
||||||
connections to get torn down cleanly.
|
|
||||||
|
|
||||||
:returns: None
|
|
||||||
"""
|
|
||||||
return _get_impl().cleanup()
|
|
||||||
|
|
||||||
|
|
||||||
def cast_to_server(context, server_params, topic, msg):
|
|
||||||
"""Invoke a remote method that does not return anything.
|
|
||||||
|
|
||||||
:param context: Information that identifies the user that has made this
|
|
||||||
request.
|
|
||||||
:param server_params: Connection information
|
|
||||||
:param topic: The topic to send the notification to.
|
|
||||||
:param msg: This is a dict in the form { "method" : "method_to_invoke",
|
|
||||||
"args" : dict_of_kwargs }
|
|
||||||
|
|
||||||
:returns: None
|
|
||||||
"""
|
|
||||||
return _get_impl().cast_to_server(CONF, context, server_params, topic,
|
|
||||||
msg)
|
|
||||||
|
|
||||||
|
|
||||||
def fanout_cast_to_server(context, server_params, topic, msg):
|
|
||||||
"""Broadcast to a remote method invocation with no return.
|
|
||||||
|
|
||||||
:param context: Information that identifies the user that has made this
|
|
||||||
request.
|
|
||||||
:param server_params: Connection information
|
|
||||||
:param topic: The topic to send the notification to.
|
|
||||||
:param msg: This is a dict in the form { "method" : "method_to_invoke",
|
|
||||||
"args" : dict_of_kwargs }
|
|
||||||
|
|
||||||
:returns: None
|
|
||||||
"""
|
|
||||||
return _get_impl().fanout_cast_to_server(CONF, context, server_params,
|
|
||||||
topic, msg)
|
|
||||||
|
|
||||||
|
|
||||||
def queue_get_for(context, topic, host):
|
|
||||||
"""Get a queue name for a given topic + host.
|
|
||||||
|
|
||||||
This function only works if this naming convention is followed on the
|
|
||||||
consumer side, as well. For example, in nova, every instance of the
|
|
||||||
nova-foo service calls create_consumer() for two topics:
|
|
||||||
|
|
||||||
foo
|
|
||||||
foo.<host>
|
|
||||||
|
|
||||||
Messages sent to the 'foo' topic are distributed to exactly one instance of
|
|
||||||
the nova-foo service. The services are chosen in a round-robin fashion.
|
|
||||||
Messages sent to the 'foo.<host>' topic are sent to the nova-foo service on
|
|
||||||
<host>.
|
|
||||||
"""
|
|
||||||
return '%s.%s' % (topic, host) if host else topic
|
|
||||||
|
|
||||||
|
|
||||||
_RPCIMPL = None
|
|
||||||
|
|
||||||
|
|
||||||
def _get_impl():
|
|
||||||
"""Delay import of rpc_backend until configuration is loaded."""
|
|
||||||
global _RPCIMPL
|
|
||||||
if _RPCIMPL is None:
|
|
||||||
try:
|
|
||||||
_RPCIMPL = importutils.import_module(CONF.rpc_backend)
|
|
||||||
except ImportError:
|
|
||||||
# For backwards compatibility with older nova config.
|
|
||||||
impl = CONF.rpc_backend.replace('nova.rpc',
|
|
||||||
'nova.openstack.common.rpc')
|
|
||||||
_RPCIMPL = importutils.import_module(impl)
|
|
||||||
return _RPCIMPL
|
|
||||||
@@ -1,674 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2010 United States Government as represented by the
|
|
||||||
# Administrator of the National Aeronautics and Space Administration.
|
|
||||||
# All Rights Reserved.
|
|
||||||
# Copyright 2011 - 2012, Red Hat, Inc.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""
|
|
||||||
Shared code between AMQP based openstack.common.rpc implementations.
|
|
||||||
|
|
||||||
The code in this module is shared between the rpc implemenations based on AMQP.
|
|
||||||
Specifically, this includes impl_kombu and impl_qpid. impl_carrot also uses
|
|
||||||
AMQP, but is deprecated and predates this code.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import collections
|
|
||||||
import inspect
|
|
||||||
import sys
|
|
||||||
import uuid
|
|
||||||
|
|
||||||
from eventlet import greenpool
|
|
||||||
from eventlet import pools
|
|
||||||
from eventlet import queue
|
|
||||||
from eventlet import semaphore
|
|
||||||
# TODO(pekowsk): Remove import cfg and below comment in Havana.
|
|
||||||
# This import should no longer be needed when the amqp_rpc_single_reply_queue
|
|
||||||
# option is removed.
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common import excutils
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import local
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
from galloper.openstack.common.rpc import common as rpc_common
|
|
||||||
|
|
||||||
|
|
||||||
# TODO(pekowski): Remove this option in Havana.
|
|
||||||
amqp_opts = [
|
|
||||||
cfg.BoolOpt('amqp_rpc_single_reply_queue',
|
|
||||||
default=False,
|
|
||||||
help='Enable a fast single reply queue if using AMQP based '
|
|
||||||
'RPC like RabbitMQ or Qpid.'),
|
|
||||||
]
|
|
||||||
|
|
||||||
cfg.CONF.register_opts(amqp_opts)
|
|
||||||
|
|
||||||
UNIQUE_ID = '_unique_id'
|
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class Pool(pools.Pool):
|
|
||||||
"""Class that implements a Pool of Connections."""
|
|
||||||
def __init__(self, conf, connection_cls, *args, **kwargs):
|
|
||||||
self.connection_cls = connection_cls
|
|
||||||
self.conf = conf
|
|
||||||
kwargs.setdefault("max_size", self.conf.rpc_conn_pool_size)
|
|
||||||
kwargs.setdefault("order_as_stack", True)
|
|
||||||
super(Pool, self).__init__(*args, **kwargs)
|
|
||||||
self.reply_proxy = None
|
|
||||||
|
|
||||||
# TODO(comstud): Timeout connections not used in a while
|
|
||||||
def create(self):
|
|
||||||
LOG.debug(_('Pool creating new connection'))
|
|
||||||
return self.connection_cls(self.conf)
|
|
||||||
|
|
||||||
def empty(self):
|
|
||||||
while self.free_items:
|
|
||||||
self.get().close()
|
|
||||||
# Force a new connection pool to be created.
|
|
||||||
# Note that this was added due to failing unit test cases. The issue
|
|
||||||
# is the above "while loop" gets all the cached connections from the
|
|
||||||
# pool and closes them, but never returns them to the pool, a pool
|
|
||||||
# leak. The unit tests hang waiting for an item to be returned to the
|
|
||||||
# pool. The unit tests get here via the teatDown() method. In the run
|
|
||||||
# time code, it gets here via cleanup() and only appears in service.py
|
|
||||||
# just before doing a sys.exit(), so cleanup() only happens once and
|
|
||||||
# the leakage is not a problem.
|
|
||||||
self.connection_cls.pool = None
|
|
||||||
|
|
||||||
|
|
||||||
_pool_create_sem = semaphore.Semaphore()
|
|
||||||
|
|
||||||
|
|
||||||
def get_connection_pool(conf, connection_cls):
|
|
||||||
with _pool_create_sem:
|
|
||||||
# Make sure only one thread tries to create the connection pool.
|
|
||||||
if not connection_cls.pool:
|
|
||||||
connection_cls.pool = Pool(conf, connection_cls)
|
|
||||||
return connection_cls.pool
|
|
||||||
|
|
||||||
|
|
||||||
class ConnectionContext(rpc_common.Connection):
|
|
||||||
"""The class that is actually returned to the caller of
|
|
||||||
create_connection(). This is essentially a wrapper around
|
|
||||||
Connection that supports 'with'. It can also return a new
|
|
||||||
Connection, or one from a pool. The function will also catch
|
|
||||||
when an instance of this class is to be deleted. With that
|
|
||||||
we can return Connections to the pool on exceptions and so
|
|
||||||
forth without making the caller be responsible for catching
|
|
||||||
them. If possible the function makes sure to return a
|
|
||||||
connection to the pool.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, conf, connection_pool, pooled=True, server_params=None):
|
|
||||||
"""Create a new connection, or get one from the pool"""
|
|
||||||
self.connection = None
|
|
||||||
self.conf = conf
|
|
||||||
self.connection_pool = connection_pool
|
|
||||||
if pooled:
|
|
||||||
self.connection = connection_pool.get()
|
|
||||||
else:
|
|
||||||
self.connection = connection_pool.connection_cls(
|
|
||||||
conf,
|
|
||||||
server_params=server_params)
|
|
||||||
self.pooled = pooled
|
|
||||||
|
|
||||||
def __enter__(self):
|
|
||||||
"""When with ConnectionContext() is used, return self"""
|
|
||||||
return self
|
|
||||||
|
|
||||||
def _done(self):
|
|
||||||
"""If the connection came from a pool, clean it up and put it back.
|
|
||||||
If it did not come from a pool, close it.
|
|
||||||
"""
|
|
||||||
if self.connection:
|
|
||||||
if self.pooled:
|
|
||||||
# Reset the connection so it's ready for the next caller
|
|
||||||
# to grab from the pool
|
|
||||||
self.connection.reset()
|
|
||||||
self.connection_pool.put(self.connection)
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
self.connection.close()
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
self.connection = None
|
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_value, tb):
|
|
||||||
"""End of 'with' statement. We're done here."""
|
|
||||||
self._done()
|
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
"""Caller is done with this connection. Make sure we cleaned up."""
|
|
||||||
self._done()
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
"""Caller is done with this connection."""
|
|
||||||
self._done()
|
|
||||||
|
|
||||||
def create_consumer(self, topic, proxy, fanout=False):
|
|
||||||
self.connection.create_consumer(topic, proxy, fanout)
|
|
||||||
|
|
||||||
def create_worker(self, topic, proxy, pool_name):
|
|
||||||
self.connection.create_worker(topic, proxy, pool_name)
|
|
||||||
|
|
||||||
def join_consumer_pool(self, callback, pool_name, topic, exchange_name):
|
|
||||||
self.connection.join_consumer_pool(callback,
|
|
||||||
pool_name,
|
|
||||||
topic,
|
|
||||||
exchange_name)
|
|
||||||
|
|
||||||
def consume_in_thread(self):
|
|
||||||
self.connection.consume_in_thread()
|
|
||||||
|
|
||||||
def __getattr__(self, key):
|
|
||||||
"""Proxy all other calls to the Connection instance"""
|
|
||||||
if self.connection:
|
|
||||||
return getattr(self.connection, key)
|
|
||||||
else:
|
|
||||||
raise rpc_common.InvalidRPCConnectionReuse()
|
|
||||||
|
|
||||||
|
|
||||||
class ReplyProxy(ConnectionContext):
|
|
||||||
""" Connection class for RPC replies / callbacks """
|
|
||||||
def __init__(self, conf, connection_pool):
|
|
||||||
self._call_waiters = {}
|
|
||||||
self._num_call_waiters = 0
|
|
||||||
self._num_call_waiters_wrn_threshhold = 10
|
|
||||||
self._reply_q = 'reply_' + uuid.uuid4().hex
|
|
||||||
super(ReplyProxy, self).__init__(conf, connection_pool, pooled=False)
|
|
||||||
self.declare_direct_consumer(self._reply_q, self._process_data)
|
|
||||||
self.consume_in_thread()
|
|
||||||
|
|
||||||
def _process_data(self, message_data):
|
|
||||||
msg_id = message_data.pop('_msg_id', None)
|
|
||||||
waiter = self._call_waiters.get(msg_id)
|
|
||||||
if not waiter:
|
|
||||||
LOG.warn(_('no calling threads waiting for msg_id : %s'
|
|
||||||
', message : %s') % (msg_id, message_data))
|
|
||||||
else:
|
|
||||||
waiter.put(message_data)
|
|
||||||
|
|
||||||
def add_call_waiter(self, waiter, msg_id):
|
|
||||||
self._num_call_waiters += 1
|
|
||||||
if self._num_call_waiters > self._num_call_waiters_wrn_threshhold:
|
|
||||||
LOG.warn(_('Number of call waiters is greater than warning '
|
|
||||||
'threshhold: %d. There could be a MulticallProxyWaiter '
|
|
||||||
'leak.') % self._num_call_waiters_wrn_threshhold)
|
|
||||||
self._num_call_waiters_wrn_threshhold *= 2
|
|
||||||
self._call_waiters[msg_id] = waiter
|
|
||||||
|
|
||||||
def del_call_waiter(self, msg_id):
|
|
||||||
self._num_call_waiters -= 1
|
|
||||||
del self._call_waiters[msg_id]
|
|
||||||
|
|
||||||
def get_reply_q(self):
|
|
||||||
return self._reply_q
|
|
||||||
|
|
||||||
|
|
||||||
def msg_reply(conf, msg_id, reply_q, connection_pool, reply=None,
|
|
||||||
failure=None, ending=False, log_failure=True):
|
|
||||||
"""Sends a reply or an error on the channel signified by msg_id.
|
|
||||||
|
|
||||||
Failure should be a sys.exc_info() tuple.
|
|
||||||
|
|
||||||
"""
|
|
||||||
with ConnectionContext(conf, connection_pool) as conn:
|
|
||||||
if failure:
|
|
||||||
failure = rpc_common.serialize_remote_exception(failure,
|
|
||||||
log_failure)
|
|
||||||
|
|
||||||
try:
|
|
||||||
msg = {'result': reply, 'failure': failure}
|
|
||||||
except TypeError:
|
|
||||||
msg = {'result': dict((k, repr(v))
|
|
||||||
for k, v in reply.__dict__.iteritems()),
|
|
||||||
'failure': failure}
|
|
||||||
if ending:
|
|
||||||
msg['ending'] = True
|
|
||||||
_add_unique_id(msg)
|
|
||||||
# If a reply_q exists, add the msg_id to the reply and pass the
|
|
||||||
# reply_q to direct_send() to use it as the response queue.
|
|
||||||
# Otherwise use the msg_id for backward compatibilty.
|
|
||||||
if reply_q:
|
|
||||||
msg['_msg_id'] = msg_id
|
|
||||||
conn.direct_send(reply_q, rpc_common.serialize_msg(msg))
|
|
||||||
else:
|
|
||||||
conn.direct_send(msg_id, rpc_common.serialize_msg(msg))
|
|
||||||
|
|
||||||
|
|
||||||
class RpcContext(rpc_common.CommonRpcContext):
|
|
||||||
"""Context that supports replying to a rpc.call"""
|
|
||||||
def __init__(self, **kwargs):
|
|
||||||
self.msg_id = kwargs.pop('msg_id', None)
|
|
||||||
self.reply_q = kwargs.pop('reply_q', None)
|
|
||||||
self.conf = kwargs.pop('conf')
|
|
||||||
super(RpcContext, self).__init__(**kwargs)
|
|
||||||
|
|
||||||
def deepcopy(self):
|
|
||||||
values = self.to_dict()
|
|
||||||
values['conf'] = self.conf
|
|
||||||
values['msg_id'] = self.msg_id
|
|
||||||
values['reply_q'] = self.reply_q
|
|
||||||
return self.__class__(**values)
|
|
||||||
|
|
||||||
def reply(self, reply=None, failure=None, ending=False,
|
|
||||||
connection_pool=None, log_failure=True):
|
|
||||||
if self.msg_id:
|
|
||||||
msg_reply(self.conf, self.msg_id, self.reply_q, connection_pool,
|
|
||||||
reply, failure, ending, log_failure)
|
|
||||||
if ending:
|
|
||||||
self.msg_id = None
|
|
||||||
|
|
||||||
|
|
||||||
def unpack_context(conf, msg):
|
|
||||||
"""Unpack context from msg."""
|
|
||||||
context_dict = {}
|
|
||||||
for key in list(msg.keys()):
|
|
||||||
# NOTE(vish): Some versions of python don't like unicode keys
|
|
||||||
# in kwargs.
|
|
||||||
key = str(key)
|
|
||||||
if key.startswith('_context_'):
|
|
||||||
value = msg.pop(key)
|
|
||||||
context_dict[key[9:]] = value
|
|
||||||
context_dict['msg_id'] = msg.pop('_msg_id', None)
|
|
||||||
context_dict['reply_q'] = msg.pop('_reply_q', None)
|
|
||||||
context_dict['conf'] = conf
|
|
||||||
ctx = RpcContext.from_dict(context_dict)
|
|
||||||
rpc_common._safe_log(LOG.debug, _('unpacked context: %s'), ctx.to_dict())
|
|
||||||
return ctx
|
|
||||||
|
|
||||||
|
|
||||||
def pack_context(msg, context):
|
|
||||||
"""Pack context into msg.
|
|
||||||
|
|
||||||
Values for message keys need to be less than 255 chars, so we pull
|
|
||||||
context out into a bunch of separate keys. If we want to support
|
|
||||||
more arguments in rabbit messages, we may want to do the same
|
|
||||||
for args at some point.
|
|
||||||
|
|
||||||
"""
|
|
||||||
context_d = dict([('_context_%s' % key, value)
|
|
||||||
for (key, value) in context.to_dict().iteritems()])
|
|
||||||
msg.update(context_d)
|
|
||||||
|
|
||||||
|
|
||||||
class _MsgIdCache(object):
|
|
||||||
"""This class checks any duplicate messages."""
|
|
||||||
|
|
||||||
# NOTE: This value is considered can be a configuration item, but
|
|
||||||
# it is not necessary to change its value in most cases,
|
|
||||||
# so let this value as static for now.
|
|
||||||
DUP_MSG_CHECK_SIZE = 16
|
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
|
||||||
self.prev_msgids = collections.deque([],
|
|
||||||
maxlen=self.DUP_MSG_CHECK_SIZE)
|
|
||||||
|
|
||||||
def check_duplicate_message(self, message_data):
|
|
||||||
"""AMQP consumers may read same message twice when exceptions occur
|
|
||||||
before ack is returned. This method prevents doing it.
|
|
||||||
"""
|
|
||||||
if UNIQUE_ID in message_data:
|
|
||||||
msg_id = message_data[UNIQUE_ID]
|
|
||||||
if msg_id not in self.prev_msgids:
|
|
||||||
self.prev_msgids.append(msg_id)
|
|
||||||
else:
|
|
||||||
raise rpc_common.DuplicateMessageError(msg_id=msg_id)
|
|
||||||
|
|
||||||
|
|
||||||
def _add_unique_id(msg):
|
|
||||||
"""Add unique_id for checking duplicate messages."""
|
|
||||||
unique_id = uuid.uuid4().hex
|
|
||||||
msg.update({UNIQUE_ID: unique_id})
|
|
||||||
LOG.debug(_('UNIQUE_ID is %s.') % (unique_id))
|
|
||||||
|
|
||||||
|
|
||||||
class _ThreadPoolWithWait(object):
|
|
||||||
"""Base class for a delayed invocation manager used by
|
|
||||||
the Connection class to start up green threads
|
|
||||||
to handle incoming messages.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, conf, connection_pool):
|
|
||||||
self.pool = greenpool.GreenPool(conf.rpc_thread_pool_size)
|
|
||||||
self.connection_pool = connection_pool
|
|
||||||
self.conf = conf
|
|
||||||
|
|
||||||
def wait(self):
|
|
||||||
"""Wait for all callback threads to exit."""
|
|
||||||
self.pool.waitall()
|
|
||||||
|
|
||||||
|
|
||||||
class CallbackWrapper(_ThreadPoolWithWait):
|
|
||||||
"""Wraps a straight callback to allow it to be invoked in a green
|
|
||||||
thread.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, conf, callback, connection_pool):
|
|
||||||
"""
|
|
||||||
:param conf: cfg.CONF instance
|
|
||||||
:param callback: a callable (probably a function)
|
|
||||||
:param connection_pool: connection pool as returned by
|
|
||||||
get_connection_pool()
|
|
||||||
"""
|
|
||||||
super(CallbackWrapper, self).__init__(
|
|
||||||
conf=conf,
|
|
||||||
connection_pool=connection_pool,
|
|
||||||
)
|
|
||||||
self.callback = callback
|
|
||||||
|
|
||||||
def __call__(self, message_data):
|
|
||||||
self.pool.spawn_n(self.callback, message_data)
|
|
||||||
|
|
||||||
|
|
||||||
class ProxyCallback(_ThreadPoolWithWait):
|
|
||||||
"""Calls methods on a proxy object based on method and args."""
|
|
||||||
|
|
||||||
def __init__(self, conf, proxy, connection_pool):
|
|
||||||
super(ProxyCallback, self).__init__(
|
|
||||||
conf=conf,
|
|
||||||
connection_pool=connection_pool,
|
|
||||||
)
|
|
||||||
self.proxy = proxy
|
|
||||||
self.msg_id_cache = _MsgIdCache()
|
|
||||||
|
|
||||||
def __call__(self, message_data):
|
|
||||||
"""Consumer callback to call a method on a proxy object.
|
|
||||||
|
|
||||||
Parses the message for validity and fires off a thread to call the
|
|
||||||
proxy object method.
|
|
||||||
|
|
||||||
Message data should be a dictionary with two keys:
|
|
||||||
method: string representing the method to call
|
|
||||||
args: dictionary of arg: value
|
|
||||||
|
|
||||||
Example: {'method': 'echo', 'args': {'value': 42}}
|
|
||||||
|
|
||||||
"""
|
|
||||||
# It is important to clear the context here, because at this point
|
|
||||||
# the previous context is stored in local.store.context
|
|
||||||
if hasattr(local.store, 'context'):
|
|
||||||
del local.store.context
|
|
||||||
rpc_common._safe_log(LOG.debug, _('received %s'), message_data)
|
|
||||||
self.msg_id_cache.check_duplicate_message(message_data)
|
|
||||||
ctxt = unpack_context(self.conf, message_data)
|
|
||||||
method = message_data.get('method')
|
|
||||||
args = message_data.get('args', {})
|
|
||||||
version = message_data.get('version', None)
|
|
||||||
if not method:
|
|
||||||
LOG.warn(_('no method for message: %s') % message_data)
|
|
||||||
ctxt.reply(_('No method for message: %s') % message_data,
|
|
||||||
connection_pool=self.connection_pool)
|
|
||||||
return
|
|
||||||
self.pool.spawn_n(self._process_data, ctxt, version, method, args)
|
|
||||||
|
|
||||||
def _process_data(self, ctxt, version, method, args):
|
|
||||||
"""Process a message in a new thread.
|
|
||||||
|
|
||||||
If the proxy object we have has a dispatch method
|
|
||||||
(see rpc.dispatcher.RpcDispatcher), pass it the version,
|
|
||||||
method, and args and let it dispatch as appropriate. If not, use
|
|
||||||
the old behavior of magically calling the specified method on the
|
|
||||||
proxy we have here.
|
|
||||||
"""
|
|
||||||
ctxt.update_store()
|
|
||||||
try:
|
|
||||||
rval = self.proxy.dispatch(ctxt, version, method, **args)
|
|
||||||
# Check if the result was a generator
|
|
||||||
if inspect.isgenerator(rval):
|
|
||||||
for x in rval:
|
|
||||||
ctxt.reply(x, None, connection_pool=self.connection_pool)
|
|
||||||
else:
|
|
||||||
ctxt.reply(rval, None, connection_pool=self.connection_pool)
|
|
||||||
# This final None tells multicall that it is done.
|
|
||||||
ctxt.reply(ending=True, connection_pool=self.connection_pool)
|
|
||||||
except rpc_common.ClientException as e:
|
|
||||||
LOG.debug(_('Expected exception during message handling (%s)') %
|
|
||||||
e._exc_info[1])
|
|
||||||
ctxt.reply(None, e._exc_info,
|
|
||||||
connection_pool=self.connection_pool,
|
|
||||||
log_failure=False)
|
|
||||||
except Exception:
|
|
||||||
# sys.exc_info() is deleted by LOG.exception().
|
|
||||||
exc_info = sys.exc_info()
|
|
||||||
LOG.error(_('Exception during message handling'),
|
|
||||||
exc_info=exc_info)
|
|
||||||
ctxt.reply(None, exc_info, connection_pool=self.connection_pool)
|
|
||||||
|
|
||||||
|
|
||||||
class MulticallProxyWaiter(object):
|
|
||||||
def __init__(self, conf, msg_id, timeout, connection_pool):
|
|
||||||
self._msg_id = msg_id
|
|
||||||
self._timeout = timeout or conf.rpc_response_timeout
|
|
||||||
self._reply_proxy = connection_pool.reply_proxy
|
|
||||||
self._done = False
|
|
||||||
self._got_ending = False
|
|
||||||
self._conf = conf
|
|
||||||
self._dataqueue = queue.LightQueue()
|
|
||||||
# Add this caller to the reply proxy's call_waiters
|
|
||||||
self._reply_proxy.add_call_waiter(self, self._msg_id)
|
|
||||||
self.msg_id_cache = _MsgIdCache()
|
|
||||||
|
|
||||||
def put(self, data):
|
|
||||||
self._dataqueue.put(data)
|
|
||||||
|
|
||||||
def done(self):
|
|
||||||
if self._done:
|
|
||||||
return
|
|
||||||
self._done = True
|
|
||||||
# Remove this caller from reply proxy's call_waiters
|
|
||||||
self._reply_proxy.del_call_waiter(self._msg_id)
|
|
||||||
|
|
||||||
def _process_data(self, data):
|
|
||||||
result = None
|
|
||||||
self.msg_id_cache.check_duplicate_message(data)
|
|
||||||
if data['failure']:
|
|
||||||
failure = data['failure']
|
|
||||||
result = rpc_common.deserialize_remote_exception(self._conf,
|
|
||||||
failure)
|
|
||||||
elif data.get('ending', False):
|
|
||||||
self._got_ending = True
|
|
||||||
else:
|
|
||||||
result = data['result']
|
|
||||||
return result
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
"""Return a result until we get a reply with an 'ending" flag"""
|
|
||||||
if self._done:
|
|
||||||
raise StopIteration
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
data = self._dataqueue.get(timeout=self._timeout)
|
|
||||||
result = self._process_data(data)
|
|
||||||
except queue.Empty:
|
|
||||||
self.done()
|
|
||||||
raise rpc_common.Timeout()
|
|
||||||
except Exception:
|
|
||||||
with excutils.save_and_reraise_exception():
|
|
||||||
self.done()
|
|
||||||
if self._got_ending:
|
|
||||||
self.done()
|
|
||||||
raise StopIteration
|
|
||||||
if isinstance(result, Exception):
|
|
||||||
self.done()
|
|
||||||
raise result
|
|
||||||
yield result
|
|
||||||
|
|
||||||
|
|
||||||
#TODO(pekowski): Remove MulticallWaiter() in Havana.
|
|
||||||
class MulticallWaiter(object):
|
|
||||||
def __init__(self, conf, connection, timeout):
|
|
||||||
self._connection = connection
|
|
||||||
self._iterator = connection.iterconsume(timeout=timeout or
|
|
||||||
conf.rpc_response_timeout)
|
|
||||||
self._result = None
|
|
||||||
self._done = False
|
|
||||||
self._got_ending = False
|
|
||||||
self._conf = conf
|
|
||||||
self.msg_id_cache = _MsgIdCache()
|
|
||||||
|
|
||||||
def done(self):
|
|
||||||
if self._done:
|
|
||||||
return
|
|
||||||
self._done = True
|
|
||||||
self._iterator.close()
|
|
||||||
self._iterator = None
|
|
||||||
self._connection.close()
|
|
||||||
|
|
||||||
def __call__(self, data):
|
|
||||||
"""The consume() callback will call this. Store the result."""
|
|
||||||
self.msg_id_cache.check_duplicate_message(data)
|
|
||||||
if data['failure']:
|
|
||||||
failure = data['failure']
|
|
||||||
self._result = rpc_common.deserialize_remote_exception(self._conf,
|
|
||||||
failure)
|
|
||||||
|
|
||||||
elif data.get('ending', False):
|
|
||||||
self._got_ending = True
|
|
||||||
else:
|
|
||||||
self._result = data['result']
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
"""Return a result until we get a 'None' response from consumer"""
|
|
||||||
if self._done:
|
|
||||||
raise StopIteration
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
self._iterator.next()
|
|
||||||
except Exception:
|
|
||||||
with excutils.save_and_reraise_exception():
|
|
||||||
self.done()
|
|
||||||
if self._got_ending:
|
|
||||||
self.done()
|
|
||||||
raise StopIteration
|
|
||||||
result = self._result
|
|
||||||
if isinstance(result, Exception):
|
|
||||||
self.done()
|
|
||||||
raise result
|
|
||||||
yield result
|
|
||||||
|
|
||||||
|
|
||||||
def create_connection(conf, new, connection_pool):
|
|
||||||
"""Create a connection"""
|
|
||||||
return ConnectionContext(conf, connection_pool, pooled=not new)
|
|
||||||
|
|
||||||
|
|
||||||
_reply_proxy_create_sem = semaphore.Semaphore()
|
|
||||||
|
|
||||||
|
|
||||||
def multicall(conf, context, topic, msg, timeout, connection_pool):
|
|
||||||
"""Make a call that returns multiple times."""
|
|
||||||
# TODO(pekowski): Remove all these comments in Havana.
|
|
||||||
# For amqp_rpc_single_reply_queue = False,
|
|
||||||
# Can't use 'with' for multicall, as it returns an iterator
|
|
||||||
# that will continue to use the connection. When it's done,
|
|
||||||
# connection.close() will get called which will put it back into
|
|
||||||
# the pool
|
|
||||||
# For amqp_rpc_single_reply_queue = True,
|
|
||||||
# The 'with' statement is mandatory for closing the connection
|
|
||||||
LOG.debug(_('Making synchronous call on %s ...'), topic)
|
|
||||||
msg_id = uuid.uuid4().hex
|
|
||||||
msg.update({'_msg_id': msg_id})
|
|
||||||
LOG.debug(_('MSG_ID is %s') % (msg_id))
|
|
||||||
_add_unique_id(msg)
|
|
||||||
pack_context(msg, context)
|
|
||||||
|
|
||||||
# TODO(pekowski): Remove this flag and the code under the if clause
|
|
||||||
# in Havana.
|
|
||||||
if not conf.amqp_rpc_single_reply_queue:
|
|
||||||
conn = ConnectionContext(conf, connection_pool)
|
|
||||||
wait_msg = MulticallWaiter(conf, conn, timeout)
|
|
||||||
conn.declare_direct_consumer(msg_id, wait_msg)
|
|
||||||
conn.topic_send(topic, rpc_common.serialize_msg(msg), timeout)
|
|
||||||
else:
|
|
||||||
with _reply_proxy_create_sem:
|
|
||||||
if not connection_pool.reply_proxy:
|
|
||||||
connection_pool.reply_proxy = ReplyProxy(conf, connection_pool)
|
|
||||||
msg.update({'_reply_q': connection_pool.reply_proxy.get_reply_q()})
|
|
||||||
wait_msg = MulticallProxyWaiter(conf, msg_id, timeout, connection_pool)
|
|
||||||
with ConnectionContext(conf, connection_pool) as conn:
|
|
||||||
conn.topic_send(topic, rpc_common.serialize_msg(msg), timeout)
|
|
||||||
return wait_msg
|
|
||||||
|
|
||||||
|
|
||||||
def call(conf, context, topic, msg, timeout, connection_pool):
|
|
||||||
"""Sends a message on a topic and wait for a response."""
|
|
||||||
rv = multicall(conf, context, topic, msg, timeout, connection_pool)
|
|
||||||
# NOTE(vish): return the last result from the multicall
|
|
||||||
rv = list(rv)
|
|
||||||
if not rv:
|
|
||||||
return
|
|
||||||
return rv[-1]
|
|
||||||
|
|
||||||
|
|
||||||
def cast(conf, context, topic, msg, connection_pool):
|
|
||||||
"""Sends a message on a topic without waiting for a response."""
|
|
||||||
LOG.debug(_('Making asynchronous cast on %s...'), topic)
|
|
||||||
_add_unique_id(msg)
|
|
||||||
pack_context(msg, context)
|
|
||||||
with ConnectionContext(conf, connection_pool) as conn:
|
|
||||||
conn.topic_send(topic, rpc_common.serialize_msg(msg))
|
|
||||||
|
|
||||||
|
|
||||||
def fanout_cast(conf, context, topic, msg, connection_pool):
|
|
||||||
"""Sends a message on a fanout exchange without waiting for a response."""
|
|
||||||
LOG.debug(_('Making asynchronous fanout cast...'))
|
|
||||||
_add_unique_id(msg)
|
|
||||||
pack_context(msg, context)
|
|
||||||
with ConnectionContext(conf, connection_pool) as conn:
|
|
||||||
conn.fanout_send(topic, rpc_common.serialize_msg(msg))
|
|
||||||
|
|
||||||
|
|
||||||
def cast_to_server(conf, context, server_params, topic, msg, connection_pool):
|
|
||||||
"""Sends a message on a topic to a specific server."""
|
|
||||||
_add_unique_id(msg)
|
|
||||||
pack_context(msg, context)
|
|
||||||
with ConnectionContext(conf, connection_pool, pooled=False,
|
|
||||||
server_params=server_params) as conn:
|
|
||||||
conn.topic_send(topic, rpc_common.serialize_msg(msg))
|
|
||||||
|
|
||||||
|
|
||||||
def fanout_cast_to_server(conf, context, server_params, topic, msg,
|
|
||||||
connection_pool):
|
|
||||||
"""Sends a message on a fanout exchange to a specific server."""
|
|
||||||
_add_unique_id(msg)
|
|
||||||
pack_context(msg, context)
|
|
||||||
with ConnectionContext(conf, connection_pool, pooled=False,
|
|
||||||
server_params=server_params) as conn:
|
|
||||||
conn.fanout_send(topic, rpc_common.serialize_msg(msg))
|
|
||||||
|
|
||||||
|
|
||||||
def notify(conf, context, topic, msg, connection_pool, envelope):
|
|
||||||
"""Sends a notification event on a topic."""
|
|
||||||
LOG.debug(_('Sending %(event_type)s on %(topic)s'),
|
|
||||||
dict(event_type=msg.get('event_type'),
|
|
||||||
topic=topic))
|
|
||||||
_add_unique_id(msg)
|
|
||||||
pack_context(msg, context)
|
|
||||||
with ConnectionContext(conf, connection_pool) as conn:
|
|
||||||
if envelope:
|
|
||||||
msg = rpc_common.serialize_msg(msg, force_envelope=True)
|
|
||||||
conn.notify_send(topic, msg)
|
|
||||||
|
|
||||||
|
|
||||||
def cleanup(connection_pool):
|
|
||||||
if connection_pool:
|
|
||||||
connection_pool.empty()
|
|
||||||
|
|
||||||
|
|
||||||
def get_control_exchange(conf):
|
|
||||||
return conf.control_exchange
|
|
||||||
@@ -1,497 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2010 United States Government as represented by the
|
|
||||||
# Administrator of the National Aeronautics and Space Administration.
|
|
||||||
# All Rights Reserved.
|
|
||||||
# Copyright 2011 Red Hat, Inc.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
import copy
|
|
||||||
import sys
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import importutils
|
|
||||||
from galloper.openstack.common import jsonutils
|
|
||||||
from galloper.openstack.common import local
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
'''RPC Envelope Version.
|
|
||||||
|
|
||||||
This version number applies to the top level structure of messages sent out.
|
|
||||||
It does *not* apply to the message payload, which must be versioned
|
|
||||||
independently. For example, when using rpc APIs, a version number is applied
|
|
||||||
for changes to the API being exposed over rpc. This version number is handled
|
|
||||||
in the rpc proxy and dispatcher modules.
|
|
||||||
|
|
||||||
This version number applies to the message envelope that is used in the
|
|
||||||
serialization done inside the rpc layer. See serialize_msg() and
|
|
||||||
deserialize_msg().
|
|
||||||
|
|
||||||
The current message format (version 2.0) is very simple. It is:
|
|
||||||
|
|
||||||
{
|
|
||||||
'oslo.version': <RPC Envelope Version as a String>,
|
|
||||||
'oslo.message': <Application Message Payload, JSON encoded>
|
|
||||||
}
|
|
||||||
|
|
||||||
Message format version '1.0' is just considered to be the messages we sent
|
|
||||||
without a message envelope.
|
|
||||||
|
|
||||||
So, the current message envelope just includes the envelope version. It may
|
|
||||||
eventually contain additional information, such as a signature for the message
|
|
||||||
payload.
|
|
||||||
|
|
||||||
We will JSON encode the application message payload. The message envelope,
|
|
||||||
which includes the JSON encoded application message body, will be passed down
|
|
||||||
to the messaging libraries as a dict.
|
|
||||||
'''
|
|
||||||
_RPC_ENVELOPE_VERSION = '2.0'
|
|
||||||
|
|
||||||
_VERSION_KEY = 'oslo.version'
|
|
||||||
_MESSAGE_KEY = 'oslo.message'
|
|
||||||
|
|
||||||
|
|
||||||
# TODO(russellb) Turn this on after Grizzly.
|
|
||||||
_SEND_RPC_ENVELOPE = False
|
|
||||||
|
|
||||||
|
|
||||||
class RPCException(Exception):
|
|
||||||
message = _("An unknown RPC related exception occurred.")
|
|
||||||
|
|
||||||
def __init__(self, message=None, **kwargs):
|
|
||||||
self.kwargs = kwargs
|
|
||||||
|
|
||||||
if not message:
|
|
||||||
try:
|
|
||||||
message = self.message % kwargs
|
|
||||||
|
|
||||||
except Exception:
|
|
||||||
# kwargs doesn't match a variable in the message
|
|
||||||
# log the issue and the kwargs
|
|
||||||
LOG.exception(_('Exception in string format operation'))
|
|
||||||
for name, value in kwargs.iteritems():
|
|
||||||
LOG.error("%s: %s" % (name, value))
|
|
||||||
# at least get the core message out if something happened
|
|
||||||
message = self.message
|
|
||||||
|
|
||||||
super(RPCException, self).__init__(message)
|
|
||||||
|
|
||||||
|
|
||||||
class RemoteError(RPCException):
|
|
||||||
"""Signifies that a remote class has raised an exception.
|
|
||||||
|
|
||||||
Contains a string representation of the type of the original exception,
|
|
||||||
the value of the original exception, and the traceback. These are
|
|
||||||
sent to the parent as a joined string so printing the exception
|
|
||||||
contains all of the relevant info.
|
|
||||||
|
|
||||||
"""
|
|
||||||
message = _("Remote error: %(exc_type)s %(value)s\n%(traceback)s.")
|
|
||||||
|
|
||||||
def __init__(self, exc_type=None, value=None, traceback=None):
|
|
||||||
self.exc_type = exc_type
|
|
||||||
self.value = value
|
|
||||||
self.traceback = traceback
|
|
||||||
super(RemoteError, self).__init__(exc_type=exc_type,
|
|
||||||
value=value,
|
|
||||||
traceback=traceback)
|
|
||||||
|
|
||||||
|
|
||||||
class Timeout(RPCException):
|
|
||||||
"""Signifies that a timeout has occurred.
|
|
||||||
|
|
||||||
This exception is raised if the rpc_response_timeout is reached while
|
|
||||||
waiting for a response from the remote side.
|
|
||||||
"""
|
|
||||||
message = _("Timeout while waiting on RPC response.")
|
|
||||||
|
|
||||||
|
|
||||||
class DuplicateMessageError(RPCException):
|
|
||||||
message = _("Found duplicate message(%(msg_id)s). Skipping it.")
|
|
||||||
|
|
||||||
|
|
||||||
class InvalidRPCConnectionReuse(RPCException):
|
|
||||||
message = _("Invalid reuse of an RPC connection.")
|
|
||||||
|
|
||||||
|
|
||||||
class UnsupportedRpcVersion(RPCException):
|
|
||||||
message = _("Specified RPC version, %(version)s, not supported by "
|
|
||||||
"this endpoint.")
|
|
||||||
|
|
||||||
|
|
||||||
class UnsupportedRpcEnvelopeVersion(RPCException):
|
|
||||||
message = _("Specified RPC envelope version, %(version)s, "
|
|
||||||
"not supported by this endpoint.")
|
|
||||||
|
|
||||||
|
|
||||||
class Connection(object):
|
|
||||||
"""A connection, returned by rpc.create_connection().
|
|
||||||
|
|
||||||
This class represents a connection to the message bus used for rpc.
|
|
||||||
An instance of this class should never be created by users of the rpc API.
|
|
||||||
Use rpc.create_connection() instead.
|
|
||||||
"""
|
|
||||||
def close(self):
|
|
||||||
"""Close the connection.
|
|
||||||
|
|
||||||
This method must be called when the connection will no longer be used.
|
|
||||||
It will ensure that any resources associated with the connection, such
|
|
||||||
as a network connection, and cleaned up.
|
|
||||||
"""
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
def create_consumer(self, topic, proxy, fanout=False):
|
|
||||||
"""Create a consumer on this connection.
|
|
||||||
|
|
||||||
A consumer is associated with a message queue on the backend message
|
|
||||||
bus. The consumer will read messages from the queue, unpack them, and
|
|
||||||
dispatch them to the proxy object. The contents of the message pulled
|
|
||||||
off of the queue will determine which method gets called on the proxy
|
|
||||||
object.
|
|
||||||
|
|
||||||
:param topic: This is a name associated with what to consume from.
|
|
||||||
Multiple instances of a service may consume from the same
|
|
||||||
topic. For example, all instances of nova-compute consume
|
|
||||||
from a queue called "compute". In that case, the
|
|
||||||
messages will get distributed amongst the consumers in a
|
|
||||||
round-robin fashion if fanout=False. If fanout=True,
|
|
||||||
every consumer associated with this topic will get a
|
|
||||||
copy of every message.
|
|
||||||
:param proxy: The object that will handle all incoming messages.
|
|
||||||
:param fanout: Whether or not this is a fanout topic. See the
|
|
||||||
documentation for the topic parameter for some
|
|
||||||
additional comments on this.
|
|
||||||
"""
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
def create_worker(self, topic, proxy, pool_name):
|
|
||||||
"""Create a worker on this connection.
|
|
||||||
|
|
||||||
A worker is like a regular consumer of messages directed to a
|
|
||||||
topic, except that it is part of a set of such consumers (the
|
|
||||||
"pool") which may run in parallel. Every pool of workers will
|
|
||||||
receive a given message, but only one worker in the pool will
|
|
||||||
be asked to process it. Load is distributed across the members
|
|
||||||
of the pool in round-robin fashion.
|
|
||||||
|
|
||||||
:param topic: This is a name associated with what to consume from.
|
|
||||||
Multiple instances of a service may consume from the same
|
|
||||||
topic.
|
|
||||||
:param proxy: The object that will handle all incoming messages.
|
|
||||||
:param pool_name: String containing the name of the pool of workers
|
|
||||||
"""
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
def join_consumer_pool(self, callback, pool_name, topic, exchange_name):
|
|
||||||
"""Register as a member of a group of consumers for a given topic from
|
|
||||||
the specified exchange.
|
|
||||||
|
|
||||||
Exactly one member of a given pool will receive each message.
|
|
||||||
|
|
||||||
A message will be delivered to multiple pools, if more than
|
|
||||||
one is created.
|
|
||||||
|
|
||||||
:param callback: Callable to be invoked for each message.
|
|
||||||
:type callback: callable accepting one argument
|
|
||||||
:param pool_name: The name of the consumer pool.
|
|
||||||
:type pool_name: str
|
|
||||||
:param topic: The routing topic for desired messages.
|
|
||||||
:type topic: str
|
|
||||||
:param exchange_name: The name of the message exchange where
|
|
||||||
the client should attach. Defaults to
|
|
||||||
the configured exchange.
|
|
||||||
:type exchange_name: str
|
|
||||||
"""
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
def consume_in_thread(self):
|
|
||||||
"""Spawn a thread to handle incoming messages.
|
|
||||||
|
|
||||||
Spawn a thread that will be responsible for handling all incoming
|
|
||||||
messages for consumers that were set up on this connection.
|
|
||||||
|
|
||||||
Message dispatching inside of this is expected to be implemented in a
|
|
||||||
non-blocking manner. An example implementation would be having this
|
|
||||||
thread pull messages in for all of the consumers, but utilize a thread
|
|
||||||
pool for dispatching the messages to the proxy objects.
|
|
||||||
"""
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
|
|
||||||
def _safe_log(log_func, msg, msg_data):
|
|
||||||
"""Sanitizes the msg_data field before logging."""
|
|
||||||
SANITIZE = {'set_admin_password': [('args', 'new_pass')],
|
|
||||||
'run_instance': [('args', 'admin_password')],
|
|
||||||
'route_message': [('args', 'message', 'args', 'method_info',
|
|
||||||
'method_kwargs', 'password'),
|
|
||||||
('args', 'message', 'args', 'method_info',
|
|
||||||
'method_kwargs', 'admin_password')]}
|
|
||||||
|
|
||||||
has_method = 'method' in msg_data and msg_data['method'] in SANITIZE
|
|
||||||
has_context_token = '_context_auth_token' in msg_data
|
|
||||||
has_token = 'auth_token' in msg_data
|
|
||||||
|
|
||||||
if not any([has_method, has_context_token, has_token]):
|
|
||||||
return log_func(msg, msg_data)
|
|
||||||
|
|
||||||
msg_data = copy.deepcopy(msg_data)
|
|
||||||
|
|
||||||
if has_method:
|
|
||||||
for arg in SANITIZE.get(msg_data['method'], []):
|
|
||||||
try:
|
|
||||||
d = msg_data
|
|
||||||
for elem in arg[:-1]:
|
|
||||||
d = d[elem]
|
|
||||||
d[arg[-1]] = '<SANITIZED>'
|
|
||||||
except KeyError, e:
|
|
||||||
LOG.info(_('Failed to sanitize %(item)s. Key error %(err)s'),
|
|
||||||
{'item': arg,
|
|
||||||
'err': e})
|
|
||||||
|
|
||||||
if has_context_token:
|
|
||||||
msg_data['_context_auth_token'] = '<SANITIZED>'
|
|
||||||
|
|
||||||
if has_token:
|
|
||||||
msg_data['auth_token'] = '<SANITIZED>'
|
|
||||||
|
|
||||||
return log_func(msg, msg_data)
|
|
||||||
|
|
||||||
|
|
||||||
def serialize_remote_exception(failure_info, log_failure=True):
|
|
||||||
"""Prepares exception data to be sent over rpc.
|
|
||||||
|
|
||||||
Failure_info should be a sys.exc_info() tuple.
|
|
||||||
|
|
||||||
"""
|
|
||||||
tb = traceback.format_exception(*failure_info)
|
|
||||||
failure = failure_info[1]
|
|
||||||
if log_failure:
|
|
||||||
LOG.error(_("Returning exception %s to caller"), unicode(failure))
|
|
||||||
LOG.error(tb)
|
|
||||||
|
|
||||||
kwargs = {}
|
|
||||||
if hasattr(failure, 'kwargs'):
|
|
||||||
kwargs = failure.kwargs
|
|
||||||
|
|
||||||
data = {
|
|
||||||
'class': str(failure.__class__.__name__),
|
|
||||||
'module': str(failure.__class__.__module__),
|
|
||||||
'message': unicode(failure),
|
|
||||||
'tb': tb,
|
|
||||||
'args': failure.args,
|
|
||||||
'kwargs': kwargs
|
|
||||||
}
|
|
||||||
|
|
||||||
json_data = jsonutils.dumps(data)
|
|
||||||
|
|
||||||
return json_data
|
|
||||||
|
|
||||||
|
|
||||||
def deserialize_remote_exception(conf, data):
|
|
||||||
failure = jsonutils.loads(str(data))
|
|
||||||
|
|
||||||
trace = failure.get('tb', [])
|
|
||||||
message = failure.get('message', "") + "\n" + "\n".join(trace)
|
|
||||||
name = failure.get('class')
|
|
||||||
module = failure.get('module')
|
|
||||||
|
|
||||||
# NOTE(ameade): We DO NOT want to allow just any module to be imported, in
|
|
||||||
# order to prevent arbitrary code execution.
|
|
||||||
if module not in conf.allowed_rpc_exception_modules:
|
|
||||||
return RemoteError(name, failure.get('message'), trace)
|
|
||||||
|
|
||||||
try:
|
|
||||||
mod = importutils.import_module(module)
|
|
||||||
klass = getattr(mod, name)
|
|
||||||
if not issubclass(klass, Exception):
|
|
||||||
raise TypeError("Can only deserialize Exceptions")
|
|
||||||
|
|
||||||
failure = klass(**failure.get('kwargs', {}))
|
|
||||||
except (AttributeError, TypeError, ImportError):
|
|
||||||
return RemoteError(name, failure.get('message'), trace)
|
|
||||||
|
|
||||||
ex_type = type(failure)
|
|
||||||
str_override = lambda self: message
|
|
||||||
new_ex_type = type(ex_type.__name__ + "_Remote", (ex_type,),
|
|
||||||
{'__str__': str_override, '__unicode__': str_override})
|
|
||||||
try:
|
|
||||||
# NOTE(ameade): Dynamically create a new exception type and swap it in
|
|
||||||
# as the new type for the exception. This only works on user defined
|
|
||||||
# Exceptions and not core python exceptions. This is important because
|
|
||||||
# we cannot necessarily change an exception message so we must override
|
|
||||||
# the __str__ method.
|
|
||||||
failure.__class__ = new_ex_type
|
|
||||||
except TypeError:
|
|
||||||
# NOTE(ameade): If a core exception then just add the traceback to the
|
|
||||||
# first exception argument.
|
|
||||||
failure.args = (message,) + failure.args[1:]
|
|
||||||
return failure
|
|
||||||
|
|
||||||
|
|
||||||
class CommonRpcContext(object):
|
|
||||||
def __init__(self, **kwargs):
|
|
||||||
self.values = kwargs
|
|
||||||
|
|
||||||
def __getattr__(self, key):
|
|
||||||
try:
|
|
||||||
return self.values[key]
|
|
||||||
except KeyError:
|
|
||||||
raise AttributeError(key)
|
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return copy.deepcopy(self.values)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_dict(cls, values):
|
|
||||||
return cls(**values)
|
|
||||||
|
|
||||||
def deepcopy(self):
|
|
||||||
return self.from_dict(self.to_dict())
|
|
||||||
|
|
||||||
def update_store(self):
|
|
||||||
local.store.context = self
|
|
||||||
|
|
||||||
def elevated(self, read_deleted=None, overwrite=False):
|
|
||||||
"""Return a version of this context with admin flag set."""
|
|
||||||
# TODO(russellb) This method is a bit of a nova-ism. It makes
|
|
||||||
# some assumptions about the data in the request context sent
|
|
||||||
# across rpc, while the rest of this class does not. We could get
|
|
||||||
# rid of this if we changed the nova code that uses this to
|
|
||||||
# convert the RpcContext back to its native RequestContext doing
|
|
||||||
# something like nova.context.RequestContext.from_dict(ctxt.to_dict())
|
|
||||||
|
|
||||||
context = self.deepcopy()
|
|
||||||
context.values['is_admin'] = True
|
|
||||||
|
|
||||||
context.values.setdefault('roles', [])
|
|
||||||
|
|
||||||
if 'admin' not in context.values['roles']:
|
|
||||||
context.values['roles'].append('admin')
|
|
||||||
|
|
||||||
if read_deleted is not None:
|
|
||||||
context.values['read_deleted'] = read_deleted
|
|
||||||
|
|
||||||
return context
|
|
||||||
|
|
||||||
|
|
||||||
class ClientException(Exception):
|
|
||||||
"""This encapsulates some actual exception that is expected to be
|
|
||||||
hit by an RPC proxy object. Merely instantiating it records the
|
|
||||||
current exception information, which will be passed back to the
|
|
||||||
RPC client without exceptional logging."""
|
|
||||||
def __init__(self):
|
|
||||||
self._exc_info = sys.exc_info()
|
|
||||||
|
|
||||||
|
|
||||||
def catch_client_exception(exceptions, func, *args, **kwargs):
|
|
||||||
try:
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
except Exception, e:
|
|
||||||
if type(e) in exceptions:
|
|
||||||
raise ClientException()
|
|
||||||
else:
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
|
||||||
def client_exceptions(*exceptions):
|
|
||||||
"""Decorator for manager methods that raise expected exceptions.
|
|
||||||
Marking a Manager method with this decorator allows the declaration
|
|
||||||
of expected exceptions that the RPC layer should not consider fatal,
|
|
||||||
and not log as if they were generated in a real error scenario. Note
|
|
||||||
that this will cause listed exceptions to be wrapped in a
|
|
||||||
ClientException, which is used internally by the RPC layer."""
|
|
||||||
def outer(func):
|
|
||||||
def inner(*args, **kwargs):
|
|
||||||
return catch_client_exception(exceptions, func, *args, **kwargs)
|
|
||||||
return inner
|
|
||||||
return outer
|
|
||||||
|
|
||||||
|
|
||||||
def version_is_compatible(imp_version, version):
|
|
||||||
"""Determine whether versions are compatible.
|
|
||||||
|
|
||||||
:param imp_version: The version implemented
|
|
||||||
:param version: The version requested by an incoming message.
|
|
||||||
"""
|
|
||||||
version_parts = version.split('.')
|
|
||||||
imp_version_parts = imp_version.split('.')
|
|
||||||
if int(version_parts[0]) != int(imp_version_parts[0]): # Major
|
|
||||||
return False
|
|
||||||
if int(version_parts[1]) > int(imp_version_parts[1]): # Minor
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def serialize_msg(raw_msg, force_envelope=False):
|
|
||||||
if not _SEND_RPC_ENVELOPE and not force_envelope:
|
|
||||||
return raw_msg
|
|
||||||
|
|
||||||
# NOTE(russellb) See the docstring for _RPC_ENVELOPE_VERSION for more
|
|
||||||
# information about this format.
|
|
||||||
msg = {_VERSION_KEY: _RPC_ENVELOPE_VERSION,
|
|
||||||
_MESSAGE_KEY: jsonutils.dumps(raw_msg)}
|
|
||||||
|
|
||||||
return msg
|
|
||||||
|
|
||||||
|
|
||||||
def deserialize_msg(msg):
|
|
||||||
# NOTE(russellb): Hang on to your hats, this road is about to
|
|
||||||
# get a little bumpy.
|
|
||||||
#
|
|
||||||
# Robustness Principle:
|
|
||||||
# "Be strict in what you send, liberal in what you accept."
|
|
||||||
#
|
|
||||||
# At this point we have to do a bit of guessing about what it
|
|
||||||
# is we just received. Here is the set of possibilities:
|
|
||||||
#
|
|
||||||
# 1) We received a dict. This could be 2 things:
|
|
||||||
#
|
|
||||||
# a) Inspect it to see if it looks like a standard message envelope.
|
|
||||||
# If so, great!
|
|
||||||
#
|
|
||||||
# b) If it doesn't look like a standard message envelope, it could either
|
|
||||||
# be a notification, or a message from before we added a message
|
|
||||||
# envelope (referred to as version 1.0).
|
|
||||||
# Just return the message as-is.
|
|
||||||
#
|
|
||||||
# 2) It's any other non-dict type. Just return it and hope for the best.
|
|
||||||
# This case covers return values from rpc.call() from before message
|
|
||||||
# envelopes were used. (messages to call a method were always a dict)
|
|
||||||
|
|
||||||
if not isinstance(msg, dict):
|
|
||||||
# See #2 above.
|
|
||||||
return msg
|
|
||||||
|
|
||||||
base_envelope_keys = (_VERSION_KEY, _MESSAGE_KEY)
|
|
||||||
if not all(map(lambda key: key in msg, base_envelope_keys)):
|
|
||||||
# See #1.b above.
|
|
||||||
return msg
|
|
||||||
|
|
||||||
# At this point we think we have the message envelope
|
|
||||||
# format we were expecting. (#1.a above)
|
|
||||||
|
|
||||||
if not version_is_compatible(_RPC_ENVELOPE_VERSION, msg[_VERSION_KEY]):
|
|
||||||
raise UnsupportedRpcEnvelopeVersion(version=msg[_VERSION_KEY])
|
|
||||||
|
|
||||||
raw_msg = jsonutils.loads(msg[_MESSAGE_KEY])
|
|
||||||
|
|
||||||
return raw_msg
|
|
||||||
@@ -1,138 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2012 Red Hat, Inc.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""
|
|
||||||
Code for rpc message dispatching.
|
|
||||||
|
|
||||||
Messages that come in have a version number associated with them. RPC API
|
|
||||||
version numbers are in the form:
|
|
||||||
|
|
||||||
Major.Minor
|
|
||||||
|
|
||||||
For a given message with version X.Y, the receiver must be marked as able to
|
|
||||||
handle messages of version A.B, where:
|
|
||||||
|
|
||||||
A = X
|
|
||||||
|
|
||||||
B >= Y
|
|
||||||
|
|
||||||
The Major version number would be incremented for an almost completely new API.
|
|
||||||
The Minor version number would be incremented for backwards compatible changes
|
|
||||||
to an existing API. A backwards compatible change could be something like
|
|
||||||
adding a new method, adding an argument to an existing method (but not
|
|
||||||
requiring it), or changing the type for an existing argument (but still
|
|
||||||
handling the old type as well).
|
|
||||||
|
|
||||||
The conversion over to a versioned API must be done on both the client side and
|
|
||||||
server side of the API at the same time. However, as the code stands today,
|
|
||||||
there can be both versioned and unversioned APIs implemented in the same code
|
|
||||||
base.
|
|
||||||
|
|
||||||
EXAMPLES
|
|
||||||
========
|
|
||||||
|
|
||||||
Nova was the first project to use versioned rpc APIs. Consider the compute rpc
|
|
||||||
API as an example. The client side is in nova/compute/rpcapi.py and the server
|
|
||||||
side is in nova/compute/manager.py.
|
|
||||||
|
|
||||||
|
|
||||||
Example 1) Adding a new method.
|
|
||||||
-------------------------------
|
|
||||||
|
|
||||||
Adding a new method is a backwards compatible change. It should be added to
|
|
||||||
nova/compute/manager.py, and RPC_API_VERSION should be bumped from X.Y to
|
|
||||||
X.Y+1. On the client side, the new method in nova/compute/rpcapi.py should
|
|
||||||
have a specific version specified to indicate the minimum API version that must
|
|
||||||
be implemented for the method to be supported. For example::
|
|
||||||
|
|
||||||
def get_host_uptime(self, ctxt, host):
|
|
||||||
topic = _compute_topic(self.topic, ctxt, host, None)
|
|
||||||
return self.call(ctxt, self.make_msg('get_host_uptime'), topic,
|
|
||||||
version='1.1')
|
|
||||||
|
|
||||||
In this case, version '1.1' is the first version that supported the
|
|
||||||
get_host_uptime() method.
|
|
||||||
|
|
||||||
|
|
||||||
Example 2) Adding a new parameter.
|
|
||||||
----------------------------------
|
|
||||||
|
|
||||||
Adding a new parameter to an rpc method can be made backwards compatible. The
|
|
||||||
RPC_API_VERSION on the server side (nova/compute/manager.py) should be bumped.
|
|
||||||
The implementation of the method must not expect the parameter to be present.::
|
|
||||||
|
|
||||||
def some_remote_method(self, arg1, arg2, newarg=None):
|
|
||||||
# The code needs to deal with newarg=None for cases
|
|
||||||
# where an older client sends a message without it.
|
|
||||||
pass
|
|
||||||
|
|
||||||
On the client side, the same changes should be made as in example 1. The
|
|
||||||
minimum version that supports the new parameter should be specified.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from galloper.openstack.common.rpc import common as rpc_common
|
|
||||||
|
|
||||||
|
|
||||||
class RpcDispatcher(object):
|
|
||||||
"""Dispatch rpc messages according to the requested API version.
|
|
||||||
|
|
||||||
This class can be used as the top level 'manager' for a service. It
|
|
||||||
contains a list of underlying managers that have an API_VERSION attribute.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, callbacks):
|
|
||||||
"""Initialize the rpc dispatcher.
|
|
||||||
|
|
||||||
:param callbacks: List of proxy objects that are an instance
|
|
||||||
of a class with rpc methods exposed. Each proxy
|
|
||||||
object should have an RPC_API_VERSION attribute.
|
|
||||||
"""
|
|
||||||
self.callbacks = callbacks
|
|
||||||
super(RpcDispatcher, self).__init__()
|
|
||||||
|
|
||||||
def dispatch(self, ctxt, version, method, **kwargs):
|
|
||||||
"""Dispatch a message based on a requested version.
|
|
||||||
|
|
||||||
:param ctxt: The request context
|
|
||||||
:param version: The requested API version from the incoming message
|
|
||||||
:param method: The method requested to be called by the incoming
|
|
||||||
message.
|
|
||||||
:param kwargs: A dict of keyword arguments to be passed to the method.
|
|
||||||
|
|
||||||
:returns: Whatever is returned by the underlying method that gets
|
|
||||||
called.
|
|
||||||
"""
|
|
||||||
if not version:
|
|
||||||
version = '1.0'
|
|
||||||
|
|
||||||
had_compatible = False
|
|
||||||
for proxyobj in self.callbacks:
|
|
||||||
if hasattr(proxyobj, 'RPC_API_VERSION'):
|
|
||||||
rpc_api_version = proxyobj.RPC_API_VERSION
|
|
||||||
else:
|
|
||||||
rpc_api_version = '1.0'
|
|
||||||
is_compatible = rpc_common.version_is_compatible(rpc_api_version,
|
|
||||||
version)
|
|
||||||
had_compatible = had_compatible or is_compatible
|
|
||||||
if not hasattr(proxyobj, method):
|
|
||||||
continue
|
|
||||||
if is_compatible:
|
|
||||||
return getattr(proxyobj, method)(ctxt, **kwargs)
|
|
||||||
|
|
||||||
if had_compatible:
|
|
||||||
raise AttributeError("No such RPC function '%s'" % method)
|
|
||||||
else:
|
|
||||||
raise rpc_common.UnsupportedRpcVersion(version=version)
|
|
||||||
@@ -1,191 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2011 OpenStack Foundation
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
"""Fake RPC implementation which calls proxy methods directly with no
|
|
||||||
queues. Casts will block, but this is very useful for tests.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import inspect
|
|
||||||
# NOTE(russellb): We specifically want to use json, not our own jsonutils.
|
|
||||||
# jsonutils has some extra logic to automatically convert objects to primitive
|
|
||||||
# types so that they can be serialized. We want to catch all cases where
|
|
||||||
# non-primitive types make it into this code and treat it as an error.
|
|
||||||
import json
|
|
||||||
import time
|
|
||||||
|
|
||||||
import eventlet
|
|
||||||
|
|
||||||
from galloper.openstack.common.rpc import common as rpc_common
|
|
||||||
|
|
||||||
CONSUMERS = {}
|
|
||||||
|
|
||||||
|
|
||||||
class RpcContext(rpc_common.CommonRpcContext):
|
|
||||||
def __init__(self, **kwargs):
|
|
||||||
super(RpcContext, self).__init__(**kwargs)
|
|
||||||
self._response = []
|
|
||||||
self._done = False
|
|
||||||
|
|
||||||
def deepcopy(self):
|
|
||||||
values = self.to_dict()
|
|
||||||
new_inst = self.__class__(**values)
|
|
||||||
new_inst._response = self._response
|
|
||||||
new_inst._done = self._done
|
|
||||||
return new_inst
|
|
||||||
|
|
||||||
def reply(self, reply=None, failure=None, ending=False):
|
|
||||||
if ending:
|
|
||||||
self._done = True
|
|
||||||
if not self._done:
|
|
||||||
self._response.append((reply, failure))
|
|
||||||
|
|
||||||
|
|
||||||
class Consumer(object):
|
|
||||||
def __init__(self, topic, proxy):
|
|
||||||
self.topic = topic
|
|
||||||
self.proxy = proxy
|
|
||||||
|
|
||||||
def call(self, context, version, method, args, timeout):
|
|
||||||
done = eventlet.event.Event()
|
|
||||||
|
|
||||||
def _inner():
|
|
||||||
ctxt = RpcContext.from_dict(context.to_dict())
|
|
||||||
try:
|
|
||||||
rval = self.proxy.dispatch(context, version, method, **args)
|
|
||||||
res = []
|
|
||||||
# Caller might have called ctxt.reply() manually
|
|
||||||
for (reply, failure) in ctxt._response:
|
|
||||||
if failure:
|
|
||||||
raise failure[0], failure[1], failure[2]
|
|
||||||
res.append(reply)
|
|
||||||
# if ending not 'sent'...we might have more data to
|
|
||||||
# return from the function itself
|
|
||||||
if not ctxt._done:
|
|
||||||
if inspect.isgenerator(rval):
|
|
||||||
for val in rval:
|
|
||||||
res.append(val)
|
|
||||||
else:
|
|
||||||
res.append(rval)
|
|
||||||
done.send(res)
|
|
||||||
except rpc_common.ClientException as e:
|
|
||||||
done.send_exception(e._exc_info[1])
|
|
||||||
except Exception as e:
|
|
||||||
done.send_exception(e)
|
|
||||||
|
|
||||||
thread = eventlet.greenthread.spawn(_inner)
|
|
||||||
|
|
||||||
if timeout:
|
|
||||||
start_time = time.time()
|
|
||||||
while not done.ready():
|
|
||||||
eventlet.greenthread.sleep(1)
|
|
||||||
cur_time = time.time()
|
|
||||||
if (cur_time - start_time) > timeout:
|
|
||||||
thread.kill()
|
|
||||||
raise rpc_common.Timeout()
|
|
||||||
|
|
||||||
return done.wait()
|
|
||||||
|
|
||||||
|
|
||||||
class Connection(object):
|
|
||||||
"""Connection object."""
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.consumers = []
|
|
||||||
|
|
||||||
def create_consumer(self, topic, proxy, fanout=False):
|
|
||||||
consumer = Consumer(topic, proxy)
|
|
||||||
self.consumers.append(consumer)
|
|
||||||
if topic not in CONSUMERS:
|
|
||||||
CONSUMERS[topic] = []
|
|
||||||
CONSUMERS[topic].append(consumer)
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
for consumer in self.consumers:
|
|
||||||
CONSUMERS[consumer.topic].remove(consumer)
|
|
||||||
self.consumers = []
|
|
||||||
|
|
||||||
def consume_in_thread(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def create_connection(conf, new=True):
|
|
||||||
"""Create a connection"""
|
|
||||||
return Connection()
|
|
||||||
|
|
||||||
|
|
||||||
def check_serialize(msg):
|
|
||||||
"""Make sure a message intended for rpc can be serialized."""
|
|
||||||
json.dumps(msg)
|
|
||||||
|
|
||||||
|
|
||||||
def multicall(conf, context, topic, msg, timeout=None):
|
|
||||||
"""Make a call that returns multiple times."""
|
|
||||||
|
|
||||||
check_serialize(msg)
|
|
||||||
|
|
||||||
method = msg.get('method')
|
|
||||||
if not method:
|
|
||||||
return
|
|
||||||
args = msg.get('args', {})
|
|
||||||
version = msg.get('version', None)
|
|
||||||
|
|
||||||
try:
|
|
||||||
consumer = CONSUMERS[topic][0]
|
|
||||||
except (KeyError, IndexError):
|
|
||||||
return iter([None])
|
|
||||||
else:
|
|
||||||
return consumer.call(context, version, method, args, timeout)
|
|
||||||
|
|
||||||
|
|
||||||
def call(conf, context, topic, msg, timeout=None):
|
|
||||||
"""Sends a message on a topic and wait for a response."""
|
|
||||||
rv = multicall(conf, context, topic, msg, timeout)
|
|
||||||
# NOTE(vish): return the last result from the multicall
|
|
||||||
rv = list(rv)
|
|
||||||
if not rv:
|
|
||||||
return
|
|
||||||
return rv[-1]
|
|
||||||
|
|
||||||
|
|
||||||
def cast(conf, context, topic, msg):
|
|
||||||
check_serialize(msg)
|
|
||||||
try:
|
|
||||||
call(conf, context, topic, msg)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def notify(conf, context, topic, msg, envelope):
|
|
||||||
check_serialize(msg)
|
|
||||||
|
|
||||||
|
|
||||||
def cleanup():
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def fanout_cast(conf, context, topic, msg):
|
|
||||||
"""Cast to all consumers of a topic"""
|
|
||||||
check_serialize(msg)
|
|
||||||
method = msg.get('method')
|
|
||||||
if not method:
|
|
||||||
return
|
|
||||||
args = msg.get('args', {})
|
|
||||||
version = msg.get('version', None)
|
|
||||||
|
|
||||||
for consumer in CONSUMERS.get(topic, []):
|
|
||||||
try:
|
|
||||||
consumer.call(context, version, method, args, None)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
@@ -1,838 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2011 OpenStack Foundation
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
import functools
|
|
||||||
import itertools
|
|
||||||
import socket
|
|
||||||
import ssl
|
|
||||||
import sys
|
|
||||||
import time
|
|
||||||
import uuid
|
|
||||||
|
|
||||||
import eventlet
|
|
||||||
import greenlet
|
|
||||||
import kombu
|
|
||||||
import kombu.connection
|
|
||||||
import kombu.entity
|
|
||||||
import kombu.messaging
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import network_utils
|
|
||||||
from galloper.openstack.common.rpc import amqp as rpc_amqp
|
|
||||||
from galloper.openstack.common.rpc import common as rpc_common
|
|
||||||
|
|
||||||
kombu_opts = [
|
|
||||||
cfg.StrOpt('kombu_ssl_version',
|
|
||||||
default='',
|
|
||||||
help='SSL version to use (valid only if SSL enabled)'),
|
|
||||||
cfg.StrOpt('kombu_ssl_keyfile',
|
|
||||||
default='',
|
|
||||||
help='SSL key file (valid only if SSL enabled)'),
|
|
||||||
cfg.StrOpt('kombu_ssl_certfile',
|
|
||||||
default='',
|
|
||||||
help='SSL cert file (valid only if SSL enabled)'),
|
|
||||||
cfg.StrOpt('kombu_ssl_ca_certs',
|
|
||||||
default='',
|
|
||||||
help=('SSL certification authority file '
|
|
||||||
'(valid only if SSL enabled)')),
|
|
||||||
cfg.StrOpt('rabbit_host',
|
|
||||||
default='localhost',
|
|
||||||
help='The RabbitMQ broker address where a single node is used'),
|
|
||||||
cfg.IntOpt('rabbit_port',
|
|
||||||
default=5672,
|
|
||||||
help='The RabbitMQ broker port where a single node is used'),
|
|
||||||
cfg.ListOpt('rabbit_hosts',
|
|
||||||
default=['$rabbit_host:$rabbit_port'],
|
|
||||||
help='RabbitMQ HA cluster host:port pairs'),
|
|
||||||
cfg.BoolOpt('rabbit_use_ssl',
|
|
||||||
default=False,
|
|
||||||
help='connect over SSL for RabbitMQ'),
|
|
||||||
cfg.StrOpt('rabbit_userid',
|
|
||||||
default='guest',
|
|
||||||
help='the RabbitMQ userid'),
|
|
||||||
cfg.StrOpt('rabbit_password',
|
|
||||||
default='guest',
|
|
||||||
help='the RabbitMQ password',
|
|
||||||
secret=True),
|
|
||||||
cfg.StrOpt('rabbit_virtual_host',
|
|
||||||
default='/',
|
|
||||||
help='the RabbitMQ virtual host'),
|
|
||||||
cfg.IntOpt('rabbit_retry_interval',
|
|
||||||
default=1,
|
|
||||||
help='how frequently to retry connecting with RabbitMQ'),
|
|
||||||
cfg.IntOpt('rabbit_retry_backoff',
|
|
||||||
default=2,
|
|
||||||
help='how long to backoff for between retries when connecting '
|
|
||||||
'to RabbitMQ'),
|
|
||||||
cfg.IntOpt('rabbit_max_retries',
|
|
||||||
default=0,
|
|
||||||
help='maximum retries with trying to connect to RabbitMQ '
|
|
||||||
'(the default of 0 implies an infinite retry count)'),
|
|
||||||
cfg.BoolOpt('rabbit_durable_queues',
|
|
||||||
default=False,
|
|
||||||
help='use durable queues in RabbitMQ'),
|
|
||||||
cfg.BoolOpt('rabbit_ha_queues',
|
|
||||||
default=False,
|
|
||||||
help='use H/A queues in RabbitMQ (x-ha-policy: all).'
|
|
||||||
'You need to wipe RabbitMQ database when '
|
|
||||||
'changing this option.'),
|
|
||||||
|
|
||||||
]
|
|
||||||
|
|
||||||
cfg.CONF.register_opts(kombu_opts)
|
|
||||||
|
|
||||||
LOG = rpc_common.LOG
|
|
||||||
|
|
||||||
|
|
||||||
def _get_queue_arguments(conf):
|
|
||||||
"""Construct the arguments for declaring a queue.
|
|
||||||
|
|
||||||
If the rabbit_ha_queues option is set, we declare a mirrored queue
|
|
||||||
as described here:
|
|
||||||
|
|
||||||
http://www.rabbitmq.com/ha.html
|
|
||||||
|
|
||||||
Setting x-ha-policy to all means that the queue will be mirrored
|
|
||||||
to all nodes in the cluster.
|
|
||||||
"""
|
|
||||||
return {'x-ha-policy': 'all'} if conf.rabbit_ha_queues else {}
|
|
||||||
|
|
||||||
|
|
||||||
class ConsumerBase(object):
|
|
||||||
"""Consumer base class."""
|
|
||||||
|
|
||||||
def __init__(self, channel, callback, tag, **kwargs):
|
|
||||||
"""Declare a queue on an amqp channel.
|
|
||||||
|
|
||||||
'channel' is the amqp channel to use
|
|
||||||
'callback' is the callback to call when messages are received
|
|
||||||
'tag' is a unique ID for the consumer on the channel
|
|
||||||
|
|
||||||
queue name, exchange name, and other kombu options are
|
|
||||||
passed in here as a dictionary.
|
|
||||||
"""
|
|
||||||
self.callback = callback
|
|
||||||
self.tag = str(tag)
|
|
||||||
self.kwargs = kwargs
|
|
||||||
self.queue = None
|
|
||||||
self.reconnect(channel)
|
|
||||||
|
|
||||||
def reconnect(self, channel):
|
|
||||||
"""Re-declare the queue after a rabbit reconnect"""
|
|
||||||
self.channel = channel
|
|
||||||
self.kwargs['channel'] = channel
|
|
||||||
self.queue = kombu.entity.Queue(**self.kwargs)
|
|
||||||
self.queue.declare()
|
|
||||||
|
|
||||||
def consume(self, *args, **kwargs):
|
|
||||||
"""Actually declare the consumer on the amqp channel. This will
|
|
||||||
start the flow of messages from the queue. Using the
|
|
||||||
Connection.iterconsume() iterator will process the messages,
|
|
||||||
calling the appropriate callback.
|
|
||||||
|
|
||||||
If a callback is specified in kwargs, use that. Otherwise,
|
|
||||||
use the callback passed during __init__()
|
|
||||||
|
|
||||||
If kwargs['nowait'] is True, then this call will block until
|
|
||||||
a message is read.
|
|
||||||
|
|
||||||
Messages will automatically be acked if the callback doesn't
|
|
||||||
raise an exception
|
|
||||||
"""
|
|
||||||
|
|
||||||
options = {'consumer_tag': self.tag}
|
|
||||||
options['nowait'] = kwargs.get('nowait', False)
|
|
||||||
callback = kwargs.get('callback', self.callback)
|
|
||||||
if not callback:
|
|
||||||
raise ValueError("No callback defined")
|
|
||||||
|
|
||||||
def _callback(raw_message):
|
|
||||||
message = self.channel.message_to_python(raw_message)
|
|
||||||
try:
|
|
||||||
msg = rpc_common.deserialize_msg(message.payload)
|
|
||||||
callback(msg)
|
|
||||||
except Exception:
|
|
||||||
LOG.exception(_("Failed to process message... skipping it."))
|
|
||||||
finally:
|
|
||||||
message.ack()
|
|
||||||
|
|
||||||
self.queue.consume(*args, callback=_callback, **options)
|
|
||||||
|
|
||||||
def cancel(self):
|
|
||||||
"""Cancel the consuming from the queue, if it has started"""
|
|
||||||
try:
|
|
||||||
self.queue.cancel(self.tag)
|
|
||||||
except KeyError, e:
|
|
||||||
# NOTE(comstud): Kludge to get around a amqplib bug
|
|
||||||
if str(e) != "u'%s'" % self.tag:
|
|
||||||
raise
|
|
||||||
self.queue = None
|
|
||||||
|
|
||||||
|
|
||||||
class DirectConsumer(ConsumerBase):
|
|
||||||
"""Queue/consumer class for 'direct'"""
|
|
||||||
|
|
||||||
def __init__(self, conf, channel, msg_id, callback, tag, **kwargs):
|
|
||||||
"""Init a 'direct' queue.
|
|
||||||
|
|
||||||
'channel' is the amqp channel to use
|
|
||||||
'msg_id' is the msg_id to listen on
|
|
||||||
'callback' is the callback to call when messages are received
|
|
||||||
'tag' is a unique ID for the consumer on the channel
|
|
||||||
|
|
||||||
Other kombu options may be passed
|
|
||||||
"""
|
|
||||||
# Default options
|
|
||||||
options = {'durable': False,
|
|
||||||
'queue_arguments': _get_queue_arguments(conf),
|
|
||||||
'auto_delete': True,
|
|
||||||
'exclusive': False}
|
|
||||||
options.update(kwargs)
|
|
||||||
exchange = kombu.entity.Exchange(name=msg_id,
|
|
||||||
type='direct',
|
|
||||||
durable=options['durable'],
|
|
||||||
auto_delete=options['auto_delete'])
|
|
||||||
super(DirectConsumer, self).__init__(channel,
|
|
||||||
callback,
|
|
||||||
tag,
|
|
||||||
name=msg_id,
|
|
||||||
exchange=exchange,
|
|
||||||
routing_key=msg_id,
|
|
||||||
**options)
|
|
||||||
|
|
||||||
|
|
||||||
class TopicConsumer(ConsumerBase):
|
|
||||||
"""Consumer class for 'topic'"""
|
|
||||||
|
|
||||||
def __init__(self, conf, channel, topic, callback, tag, name=None,
|
|
||||||
exchange_name=None, **kwargs):
|
|
||||||
"""Init a 'topic' queue.
|
|
||||||
|
|
||||||
:param channel: the amqp channel to use
|
|
||||||
:param topic: the topic to listen on
|
|
||||||
:paramtype topic: str
|
|
||||||
:param callback: the callback to call when messages are received
|
|
||||||
:param tag: a unique ID for the consumer on the channel
|
|
||||||
:param name: optional queue name, defaults to topic
|
|
||||||
:paramtype name: str
|
|
||||||
|
|
||||||
Other kombu options may be passed as keyword arguments
|
|
||||||
"""
|
|
||||||
# Default options
|
|
||||||
options = {'durable': conf.rabbit_durable_queues,
|
|
||||||
'queue_arguments': _get_queue_arguments(conf),
|
|
||||||
'auto_delete': False,
|
|
||||||
'exclusive': False}
|
|
||||||
options.update(kwargs)
|
|
||||||
exchange_name = exchange_name or rpc_amqp.get_control_exchange(conf)
|
|
||||||
exchange = kombu.entity.Exchange(name=exchange_name,
|
|
||||||
type='topic',
|
|
||||||
durable=options['durable'],
|
|
||||||
auto_delete=options['auto_delete'])
|
|
||||||
super(TopicConsumer, self).__init__(channel,
|
|
||||||
callback,
|
|
||||||
tag,
|
|
||||||
name=name or topic,
|
|
||||||
exchange=exchange,
|
|
||||||
routing_key=topic,
|
|
||||||
**options)
|
|
||||||
|
|
||||||
|
|
||||||
class FanoutConsumer(ConsumerBase):
|
|
||||||
"""Consumer class for 'fanout'"""
|
|
||||||
|
|
||||||
def __init__(self, conf, channel, topic, callback, tag, **kwargs):
|
|
||||||
"""Init a 'fanout' queue.
|
|
||||||
|
|
||||||
'channel' is the amqp channel to use
|
|
||||||
'topic' is the topic to listen on
|
|
||||||
'callback' is the callback to call when messages are received
|
|
||||||
'tag' is a unique ID for the consumer on the channel
|
|
||||||
|
|
||||||
Other kombu options may be passed
|
|
||||||
"""
|
|
||||||
unique = uuid.uuid4().hex
|
|
||||||
exchange_name = '%s_fanout' % topic
|
|
||||||
queue_name = '%s_fanout_%s' % (topic, unique)
|
|
||||||
|
|
||||||
# Default options
|
|
||||||
options = {'durable': False,
|
|
||||||
'queue_arguments': _get_queue_arguments(conf),
|
|
||||||
'auto_delete': True,
|
|
||||||
'exclusive': False}
|
|
||||||
options.update(kwargs)
|
|
||||||
exchange = kombu.entity.Exchange(name=exchange_name, type='fanout',
|
|
||||||
durable=options['durable'],
|
|
||||||
auto_delete=options['auto_delete'])
|
|
||||||
super(FanoutConsumer, self).__init__(channel, callback, tag,
|
|
||||||
name=queue_name,
|
|
||||||
exchange=exchange,
|
|
||||||
routing_key=topic,
|
|
||||||
**options)
|
|
||||||
|
|
||||||
|
|
||||||
class Publisher(object):
|
|
||||||
"""Base Publisher class"""
|
|
||||||
|
|
||||||
def __init__(self, channel, exchange_name, routing_key, **kwargs):
|
|
||||||
"""Init the Publisher class with the exchange_name, routing_key,
|
|
||||||
and other options
|
|
||||||
"""
|
|
||||||
self.exchange_name = exchange_name
|
|
||||||
self.routing_key = routing_key
|
|
||||||
self.kwargs = kwargs
|
|
||||||
self.reconnect(channel)
|
|
||||||
|
|
||||||
def reconnect(self, channel):
|
|
||||||
"""Re-establish the Producer after a rabbit reconnection"""
|
|
||||||
self.exchange = kombu.entity.Exchange(name=self.exchange_name,
|
|
||||||
**self.kwargs)
|
|
||||||
self.producer = kombu.messaging.Producer(exchange=self.exchange,
|
|
||||||
channel=channel,
|
|
||||||
routing_key=self.routing_key)
|
|
||||||
|
|
||||||
def send(self, msg, timeout=None):
|
|
||||||
"""Send a message"""
|
|
||||||
if timeout:
|
|
||||||
#
|
|
||||||
# AMQP TTL is in milliseconds when set in the header.
|
|
||||||
#
|
|
||||||
self.producer.publish(msg, headers={'ttl': (timeout * 1000)})
|
|
||||||
else:
|
|
||||||
self.producer.publish(msg)
|
|
||||||
|
|
||||||
|
|
||||||
class DirectPublisher(Publisher):
|
|
||||||
"""Publisher class for 'direct'"""
|
|
||||||
def __init__(self, conf, channel, msg_id, **kwargs):
|
|
||||||
"""init a 'direct' publisher.
|
|
||||||
|
|
||||||
Kombu options may be passed as keyword args to override defaults
|
|
||||||
"""
|
|
||||||
|
|
||||||
options = {'durable': False,
|
|
||||||
'auto_delete': True,
|
|
||||||
'exclusive': False}
|
|
||||||
options.update(kwargs)
|
|
||||||
super(DirectPublisher, self).__init__(channel, msg_id, msg_id,
|
|
||||||
type='direct', **options)
|
|
||||||
|
|
||||||
|
|
||||||
class TopicPublisher(Publisher):
|
|
||||||
"""Publisher class for 'topic'"""
|
|
||||||
def __init__(self, conf, channel, topic, **kwargs):
|
|
||||||
"""init a 'topic' publisher.
|
|
||||||
|
|
||||||
Kombu options may be passed as keyword args to override defaults
|
|
||||||
"""
|
|
||||||
options = {'durable': conf.rabbit_durable_queues,
|
|
||||||
'auto_delete': False,
|
|
||||||
'exclusive': False}
|
|
||||||
options.update(kwargs)
|
|
||||||
exchange_name = rpc_amqp.get_control_exchange(conf)
|
|
||||||
super(TopicPublisher, self).__init__(channel,
|
|
||||||
exchange_name,
|
|
||||||
topic,
|
|
||||||
type='topic',
|
|
||||||
**options)
|
|
||||||
|
|
||||||
|
|
||||||
class FanoutPublisher(Publisher):
|
|
||||||
"""Publisher class for 'fanout'"""
|
|
||||||
def __init__(self, conf, channel, topic, **kwargs):
|
|
||||||
"""init a 'fanout' publisher.
|
|
||||||
|
|
||||||
Kombu options may be passed as keyword args to override defaults
|
|
||||||
"""
|
|
||||||
options = {'durable': False,
|
|
||||||
'auto_delete': True,
|
|
||||||
'exclusive': False}
|
|
||||||
options.update(kwargs)
|
|
||||||
super(FanoutPublisher, self).__init__(channel, '%s_fanout' % topic,
|
|
||||||
None, type='fanout', **options)
|
|
||||||
|
|
||||||
|
|
||||||
class NotifyPublisher(TopicPublisher):
|
|
||||||
"""Publisher class for 'notify'"""
|
|
||||||
|
|
||||||
def __init__(self, conf, channel, topic, **kwargs):
|
|
||||||
self.durable = kwargs.pop('durable', conf.rabbit_durable_queues)
|
|
||||||
self.queue_arguments = _get_queue_arguments(conf)
|
|
||||||
super(NotifyPublisher, self).__init__(conf, channel, topic, **kwargs)
|
|
||||||
|
|
||||||
def reconnect(self, channel):
|
|
||||||
super(NotifyPublisher, self).reconnect(channel)
|
|
||||||
|
|
||||||
# NOTE(jerdfelt): Normally the consumer would create the queue, but
|
|
||||||
# we do this to ensure that messages don't get dropped if the
|
|
||||||
# consumer is started after we do
|
|
||||||
queue = kombu.entity.Queue(channel=channel,
|
|
||||||
exchange=self.exchange,
|
|
||||||
durable=self.durable,
|
|
||||||
name=self.routing_key,
|
|
||||||
routing_key=self.routing_key,
|
|
||||||
queue_arguments=self.queue_arguments)
|
|
||||||
queue.declare()
|
|
||||||
|
|
||||||
|
|
||||||
class Connection(object):
|
|
||||||
"""Connection object."""
|
|
||||||
|
|
||||||
pool = None
|
|
||||||
|
|
||||||
def __init__(self, conf, server_params=None):
|
|
||||||
self.consumers = []
|
|
||||||
self.consumer_thread = None
|
|
||||||
self.proxy_callbacks = []
|
|
||||||
self.conf = conf
|
|
||||||
self.max_retries = self.conf.rabbit_max_retries
|
|
||||||
# Try forever?
|
|
||||||
if self.max_retries <= 0:
|
|
||||||
self.max_retries = None
|
|
||||||
self.interval_start = self.conf.rabbit_retry_interval
|
|
||||||
self.interval_stepping = self.conf.rabbit_retry_backoff
|
|
||||||
# max retry-interval = 30 seconds
|
|
||||||
self.interval_max = 30
|
|
||||||
self.memory_transport = False
|
|
||||||
|
|
||||||
if server_params is None:
|
|
||||||
server_params = {}
|
|
||||||
# Keys to translate from server_params to kombu params
|
|
||||||
server_params_to_kombu_params = {'username': 'userid'}
|
|
||||||
|
|
||||||
ssl_params = self._fetch_ssl_params()
|
|
||||||
params_list = []
|
|
||||||
for adr in self.conf.rabbit_hosts:
|
|
||||||
hostname, port = network_utils.parse_host_port(
|
|
||||||
adr, default_port=self.conf.rabbit_port)
|
|
||||||
|
|
||||||
params = {
|
|
||||||
'hostname': hostname,
|
|
||||||
'port': port,
|
|
||||||
'userid': self.conf.rabbit_userid,
|
|
||||||
'password': self.conf.rabbit_password,
|
|
||||||
'virtual_host': self.conf.rabbit_virtual_host,
|
|
||||||
}
|
|
||||||
|
|
||||||
for sp_key, value in server_params.iteritems():
|
|
||||||
p_key = server_params_to_kombu_params.get(sp_key, sp_key)
|
|
||||||
params[p_key] = value
|
|
||||||
|
|
||||||
if self.conf.fake_rabbit:
|
|
||||||
params['transport'] = 'memory'
|
|
||||||
if self.conf.rabbit_use_ssl:
|
|
||||||
params['ssl'] = ssl_params
|
|
||||||
|
|
||||||
params_list.append(params)
|
|
||||||
|
|
||||||
self.params_list = params_list
|
|
||||||
|
|
||||||
self.memory_transport = self.conf.fake_rabbit
|
|
||||||
|
|
||||||
self.connection = None
|
|
||||||
self.reconnect()
|
|
||||||
|
|
||||||
def _fetch_ssl_params(self):
|
|
||||||
"""Handles fetching what ssl params
|
|
||||||
should be used for the connection (if any)"""
|
|
||||||
ssl_params = dict()
|
|
||||||
|
|
||||||
# http://docs.python.org/library/ssl.html - ssl.wrap_socket
|
|
||||||
if self.conf.kombu_ssl_version:
|
|
||||||
ssl_params['ssl_version'] = self.conf.kombu_ssl_version
|
|
||||||
if self.conf.kombu_ssl_keyfile:
|
|
||||||
ssl_params['keyfile'] = self.conf.kombu_ssl_keyfile
|
|
||||||
if self.conf.kombu_ssl_certfile:
|
|
||||||
ssl_params['certfile'] = self.conf.kombu_ssl_certfile
|
|
||||||
if self.conf.kombu_ssl_ca_certs:
|
|
||||||
ssl_params['ca_certs'] = self.conf.kombu_ssl_ca_certs
|
|
||||||
# We might want to allow variations in the
|
|
||||||
# future with this?
|
|
||||||
ssl_params['cert_reqs'] = ssl.CERT_REQUIRED
|
|
||||||
|
|
||||||
if not ssl_params:
|
|
||||||
# Just have the default behavior
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
# Return the extended behavior
|
|
||||||
return ssl_params
|
|
||||||
|
|
||||||
def _connect(self, params):
|
|
||||||
"""Connect to rabbit. Re-establish any queues that may have
|
|
||||||
been declared before if we are reconnecting. Exceptions should
|
|
||||||
be handled by the caller.
|
|
||||||
"""
|
|
||||||
if self.connection:
|
|
||||||
LOG.info(_("Reconnecting to AMQP server on "
|
|
||||||
"%(hostname)s:%(port)d") % params)
|
|
||||||
try:
|
|
||||||
self.connection.release()
|
|
||||||
except self.connection_errors:
|
|
||||||
pass
|
|
||||||
# Setting this in case the next statement fails, though
|
|
||||||
# it shouldn't be doing any network operations, yet.
|
|
||||||
self.connection = None
|
|
||||||
self.connection = kombu.connection.BrokerConnection(**params)
|
|
||||||
self.connection_errors = self.connection.connection_errors
|
|
||||||
if self.memory_transport:
|
|
||||||
# Kludge to speed up tests.
|
|
||||||
self.connection.transport.polling_interval = 0.0
|
|
||||||
self.consumer_num = itertools.count(1)
|
|
||||||
self.connection.connect()
|
|
||||||
self.channel = self.connection.channel()
|
|
||||||
# work around 'memory' transport bug in 1.1.3
|
|
||||||
if self.memory_transport:
|
|
||||||
self.channel._new_queue('ae.undeliver')
|
|
||||||
for consumer in self.consumers:
|
|
||||||
consumer.reconnect(self.channel)
|
|
||||||
LOG.info(_('Connected to AMQP server on %(hostname)s:%(port)d') %
|
|
||||||
params)
|
|
||||||
|
|
||||||
def reconnect(self):
|
|
||||||
"""Handles reconnecting and re-establishing queues.
|
|
||||||
Will retry up to self.max_retries number of times.
|
|
||||||
self.max_retries = 0 means to retry forever.
|
|
||||||
Sleep between tries, starting at self.interval_start
|
|
||||||
seconds, backing off self.interval_stepping number of seconds
|
|
||||||
each attempt.
|
|
||||||
"""
|
|
||||||
|
|
||||||
attempt = 0
|
|
||||||
while True:
|
|
||||||
params = self.params_list[attempt % len(self.params_list)]
|
|
||||||
attempt += 1
|
|
||||||
try:
|
|
||||||
self._connect(params)
|
|
||||||
return
|
|
||||||
except (IOError, self.connection_errors) as e:
|
|
||||||
pass
|
|
||||||
except Exception, e:
|
|
||||||
# NOTE(comstud): Unfortunately it's possible for amqplib
|
|
||||||
# to return an error not covered by its transport
|
|
||||||
# connection_errors in the case of a timeout waiting for
|
|
||||||
# a protocol response. (See paste link in LP888621)
|
|
||||||
# So, we check all exceptions for 'timeout' in them
|
|
||||||
# and try to reconnect in this case.
|
|
||||||
if 'timeout' not in str(e):
|
|
||||||
raise
|
|
||||||
|
|
||||||
log_info = {}
|
|
||||||
log_info['err_str'] = str(e)
|
|
||||||
log_info['max_retries'] = self.max_retries
|
|
||||||
log_info.update(params)
|
|
||||||
|
|
||||||
if self.max_retries and attempt == self.max_retries:
|
|
||||||
LOG.error(_('Unable to connect to AMQP server on '
|
|
||||||
'%(hostname)s:%(port)d after %(max_retries)d '
|
|
||||||
'tries: %(err_str)s') % log_info)
|
|
||||||
# NOTE(comstud): Copied from original code. There's
|
|
||||||
# really no better recourse because if this was a queue we
|
|
||||||
# need to consume on, we have no way to consume anymore.
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
if attempt == 1:
|
|
||||||
sleep_time = self.interval_start or 1
|
|
||||||
elif attempt > 1:
|
|
||||||
sleep_time += self.interval_stepping
|
|
||||||
if self.interval_max:
|
|
||||||
sleep_time = min(sleep_time, self.interval_max)
|
|
||||||
|
|
||||||
log_info['sleep_time'] = sleep_time
|
|
||||||
LOG.error(_('AMQP server on %(hostname)s:%(port)d is '
|
|
||||||
'unreachable: %(err_str)s. Trying again in '
|
|
||||||
'%(sleep_time)d seconds.') % log_info)
|
|
||||||
time.sleep(sleep_time)
|
|
||||||
|
|
||||||
def ensure(self, error_callback, method, *args, **kwargs):
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
return method(*args, **kwargs)
|
|
||||||
except (self.connection_errors, socket.timeout, IOError), e:
|
|
||||||
if error_callback:
|
|
||||||
error_callback(e)
|
|
||||||
except Exception, e:
|
|
||||||
# NOTE(comstud): Unfortunately it's possible for amqplib
|
|
||||||
# to return an error not covered by its transport
|
|
||||||
# connection_errors in the case of a timeout waiting for
|
|
||||||
# a protocol response. (See paste link in LP888621)
|
|
||||||
# So, we check all exceptions for 'timeout' in them
|
|
||||||
# and try to reconnect in this case.
|
|
||||||
if 'timeout' not in str(e):
|
|
||||||
raise
|
|
||||||
if error_callback:
|
|
||||||
error_callback(e)
|
|
||||||
self.reconnect()
|
|
||||||
|
|
||||||
def get_channel(self):
|
|
||||||
"""Convenience call for bin/clear_rabbit_queues"""
|
|
||||||
return self.channel
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
"""Close/release this connection"""
|
|
||||||
self.cancel_consumer_thread()
|
|
||||||
self.wait_on_proxy_callbacks()
|
|
||||||
self.connection.release()
|
|
||||||
self.connection = None
|
|
||||||
|
|
||||||
def reset(self):
|
|
||||||
"""Reset a connection so it can be used again"""
|
|
||||||
self.cancel_consumer_thread()
|
|
||||||
self.wait_on_proxy_callbacks()
|
|
||||||
self.channel.close()
|
|
||||||
self.channel = self.connection.channel()
|
|
||||||
# work around 'memory' transport bug in 1.1.3
|
|
||||||
if self.memory_transport:
|
|
||||||
self.channel._new_queue('ae.undeliver')
|
|
||||||
self.consumers = []
|
|
||||||
|
|
||||||
def declare_consumer(self, consumer_cls, topic, callback):
|
|
||||||
"""Create a Consumer using the class that was passed in and
|
|
||||||
add it to our list of consumers
|
|
||||||
"""
|
|
||||||
|
|
||||||
def _connect_error(exc):
|
|
||||||
log_info = {'topic': topic, 'err_str': str(exc)}
|
|
||||||
LOG.error(_("Failed to declare consumer for topic '%(topic)s': "
|
|
||||||
"%(err_str)s") % log_info)
|
|
||||||
|
|
||||||
def _declare_consumer():
|
|
||||||
consumer = consumer_cls(self.conf, self.channel, topic, callback,
|
|
||||||
self.consumer_num.next())
|
|
||||||
self.consumers.append(consumer)
|
|
||||||
return consumer
|
|
||||||
|
|
||||||
return self.ensure(_connect_error, _declare_consumer)
|
|
||||||
|
|
||||||
def iterconsume(self, limit=None, timeout=None):
|
|
||||||
"""Return an iterator that will consume from all queues/consumers"""
|
|
||||||
|
|
||||||
info = {'do_consume': True}
|
|
||||||
|
|
||||||
def _error_callback(exc):
|
|
||||||
if isinstance(exc, socket.timeout):
|
|
||||||
LOG.debug(_('Timed out waiting for RPC response: %s') %
|
|
||||||
str(exc))
|
|
||||||
raise rpc_common.Timeout()
|
|
||||||
else:
|
|
||||||
LOG.exception(_('Failed to consume message from queue: %s') %
|
|
||||||
str(exc))
|
|
||||||
info['do_consume'] = True
|
|
||||||
|
|
||||||
def _consume():
|
|
||||||
if info['do_consume']:
|
|
||||||
queues_head = self.consumers[:-1]
|
|
||||||
queues_tail = self.consumers[-1]
|
|
||||||
for queue in queues_head:
|
|
||||||
queue.consume(nowait=True)
|
|
||||||
queues_tail.consume(nowait=False)
|
|
||||||
info['do_consume'] = False
|
|
||||||
return self.connection.drain_events(timeout=timeout)
|
|
||||||
|
|
||||||
for iteration in itertools.count(0):
|
|
||||||
if limit and iteration >= limit:
|
|
||||||
raise StopIteration
|
|
||||||
yield self.ensure(_error_callback, _consume)
|
|
||||||
|
|
||||||
def cancel_consumer_thread(self):
|
|
||||||
"""Cancel a consumer thread"""
|
|
||||||
if self.consumer_thread is not None:
|
|
||||||
self.consumer_thread.kill()
|
|
||||||
try:
|
|
||||||
self.consumer_thread.wait()
|
|
||||||
except greenlet.GreenletExit:
|
|
||||||
pass
|
|
||||||
self.consumer_thread = None
|
|
||||||
|
|
||||||
def wait_on_proxy_callbacks(self):
|
|
||||||
"""Wait for all proxy callback threads to exit."""
|
|
||||||
for proxy_cb in self.proxy_callbacks:
|
|
||||||
proxy_cb.wait()
|
|
||||||
|
|
||||||
def publisher_send(self, cls, topic, msg, timeout=None, **kwargs):
|
|
||||||
"""Send to a publisher based on the publisher class"""
|
|
||||||
|
|
||||||
def _error_callback(exc):
|
|
||||||
log_info = {'topic': topic, 'err_str': str(exc)}
|
|
||||||
LOG.exception(_("Failed to publish message to topic "
|
|
||||||
"'%(topic)s': %(err_str)s") % log_info)
|
|
||||||
|
|
||||||
def _publish():
|
|
||||||
publisher = cls(self.conf, self.channel, topic, **kwargs)
|
|
||||||
publisher.send(msg, timeout)
|
|
||||||
|
|
||||||
self.ensure(_error_callback, _publish)
|
|
||||||
|
|
||||||
def declare_direct_consumer(self, topic, callback):
|
|
||||||
"""Create a 'direct' queue.
|
|
||||||
In nova's use, this is generally a msg_id queue used for
|
|
||||||
responses for call/multicall
|
|
||||||
"""
|
|
||||||
self.declare_consumer(DirectConsumer, topic, callback)
|
|
||||||
|
|
||||||
def declare_topic_consumer(self, topic, callback=None, queue_name=None,
|
|
||||||
exchange_name=None):
|
|
||||||
"""Create a 'topic' consumer."""
|
|
||||||
self.declare_consumer(functools.partial(TopicConsumer,
|
|
||||||
name=queue_name,
|
|
||||||
exchange_name=exchange_name,
|
|
||||||
),
|
|
||||||
topic, callback)
|
|
||||||
|
|
||||||
def declare_fanout_consumer(self, topic, callback):
|
|
||||||
"""Create a 'fanout' consumer"""
|
|
||||||
self.declare_consumer(FanoutConsumer, topic, callback)
|
|
||||||
|
|
||||||
def direct_send(self, msg_id, msg):
|
|
||||||
"""Send a 'direct' message"""
|
|
||||||
self.publisher_send(DirectPublisher, msg_id, msg)
|
|
||||||
|
|
||||||
def topic_send(self, topic, msg, timeout=None):
|
|
||||||
"""Send a 'topic' message"""
|
|
||||||
self.publisher_send(TopicPublisher, topic, msg, timeout)
|
|
||||||
|
|
||||||
def fanout_send(self, topic, msg):
|
|
||||||
"""Send a 'fanout' message"""
|
|
||||||
self.publisher_send(FanoutPublisher, topic, msg)
|
|
||||||
|
|
||||||
def notify_send(self, topic, msg, **kwargs):
|
|
||||||
"""Send a notify message on a topic"""
|
|
||||||
self.publisher_send(NotifyPublisher, topic, msg, None, **kwargs)
|
|
||||||
|
|
||||||
def consume(self, limit=None):
|
|
||||||
"""Consume from all queues/consumers"""
|
|
||||||
it = self.iterconsume(limit=limit)
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
it.next()
|
|
||||||
except StopIteration:
|
|
||||||
return
|
|
||||||
|
|
||||||
def consume_in_thread(self):
|
|
||||||
"""Consumer from all queues/consumers in a greenthread"""
|
|
||||||
def _consumer_thread():
|
|
||||||
try:
|
|
||||||
self.consume()
|
|
||||||
except greenlet.GreenletExit:
|
|
||||||
return
|
|
||||||
if self.consumer_thread is None:
|
|
||||||
self.consumer_thread = eventlet.spawn(_consumer_thread)
|
|
||||||
return self.consumer_thread
|
|
||||||
|
|
||||||
def create_consumer(self, topic, proxy, fanout=False):
|
|
||||||
"""Create a consumer that calls a method in a proxy object"""
|
|
||||||
proxy_cb = rpc_amqp.ProxyCallback(
|
|
||||||
self.conf, proxy,
|
|
||||||
rpc_amqp.get_connection_pool(self.conf, Connection))
|
|
||||||
self.proxy_callbacks.append(proxy_cb)
|
|
||||||
|
|
||||||
if fanout:
|
|
||||||
self.declare_fanout_consumer(topic, proxy_cb)
|
|
||||||
else:
|
|
||||||
self.declare_topic_consumer(topic, proxy_cb)
|
|
||||||
|
|
||||||
def create_worker(self, topic, proxy, pool_name):
|
|
||||||
"""Create a worker that calls a method in a proxy object"""
|
|
||||||
proxy_cb = rpc_amqp.ProxyCallback(
|
|
||||||
self.conf, proxy,
|
|
||||||
rpc_amqp.get_connection_pool(self.conf, Connection))
|
|
||||||
self.proxy_callbacks.append(proxy_cb)
|
|
||||||
self.declare_topic_consumer(topic, proxy_cb, pool_name)
|
|
||||||
|
|
||||||
def join_consumer_pool(self, callback, pool_name, topic,
|
|
||||||
exchange_name=None):
|
|
||||||
"""Register as a member of a group of consumers for a given topic from
|
|
||||||
the specified exchange.
|
|
||||||
|
|
||||||
Exactly one member of a given pool will receive each message.
|
|
||||||
|
|
||||||
A message will be delivered to multiple pools, if more than
|
|
||||||
one is created.
|
|
||||||
"""
|
|
||||||
callback_wrapper = rpc_amqp.CallbackWrapper(
|
|
||||||
conf=self.conf,
|
|
||||||
callback=callback,
|
|
||||||
connection_pool=rpc_amqp.get_connection_pool(self.conf,
|
|
||||||
Connection),
|
|
||||||
)
|
|
||||||
self.proxy_callbacks.append(callback_wrapper)
|
|
||||||
self.declare_topic_consumer(
|
|
||||||
queue_name=pool_name,
|
|
||||||
topic=topic,
|
|
||||||
exchange_name=exchange_name,
|
|
||||||
callback=callback_wrapper,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def create_connection(conf, new=True):
|
|
||||||
"""Create a connection"""
|
|
||||||
return rpc_amqp.create_connection(
|
|
||||||
conf, new,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection))
|
|
||||||
|
|
||||||
|
|
||||||
def multicall(conf, context, topic, msg, timeout=None):
|
|
||||||
"""Make a call that returns multiple times."""
|
|
||||||
return rpc_amqp.multicall(
|
|
||||||
conf, context, topic, msg, timeout,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection))
|
|
||||||
|
|
||||||
|
|
||||||
def call(conf, context, topic, msg, timeout=None):
|
|
||||||
"""Sends a message on a topic and wait for a response."""
|
|
||||||
return rpc_amqp.call(
|
|
||||||
conf, context, topic, msg, timeout,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection))
|
|
||||||
|
|
||||||
|
|
||||||
def cast(conf, context, topic, msg):
|
|
||||||
"""Sends a message on a topic without waiting for a response."""
|
|
||||||
return rpc_amqp.cast(
|
|
||||||
conf, context, topic, msg,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection))
|
|
||||||
|
|
||||||
|
|
||||||
def fanout_cast(conf, context, topic, msg):
|
|
||||||
"""Sends a message on a fanout exchange without waiting for a response."""
|
|
||||||
return rpc_amqp.fanout_cast(
|
|
||||||
conf, context, topic, msg,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection))
|
|
||||||
|
|
||||||
|
|
||||||
def cast_to_server(conf, context, server_params, topic, msg):
|
|
||||||
"""Sends a message on a topic to a specific server."""
|
|
||||||
return rpc_amqp.cast_to_server(
|
|
||||||
conf, context, server_params, topic, msg,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection))
|
|
||||||
|
|
||||||
|
|
||||||
def fanout_cast_to_server(conf, context, server_params, topic, msg):
|
|
||||||
"""Sends a message on a fanout exchange to a specific server."""
|
|
||||||
return rpc_amqp.fanout_cast_to_server(
|
|
||||||
conf, context, server_params, topic, msg,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection))
|
|
||||||
|
|
||||||
|
|
||||||
def notify(conf, context, topic, msg, envelope):
|
|
||||||
"""Sends a notification event on a topic."""
|
|
||||||
return rpc_amqp.notify(
|
|
||||||
conf, context, topic, msg,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection),
|
|
||||||
envelope)
|
|
||||||
|
|
||||||
|
|
||||||
def cleanup():
|
|
||||||
return rpc_amqp.cleanup(Connection.pool)
|
|
||||||
@@ -1,697 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2011 OpenStack Foundation
|
|
||||||
# Copyright 2011 - 2012, Red Hat, Inc.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
import functools
|
|
||||||
import itertools
|
|
||||||
import time
|
|
||||||
import uuid
|
|
||||||
|
|
||||||
import eventlet
|
|
||||||
import greenlet
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import importutils
|
|
||||||
from galloper.openstack.common import jsonutils
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
from galloper.openstack.common.rpc import amqp as rpc_amqp
|
|
||||||
from galloper.openstack.common.rpc import common as rpc_common
|
|
||||||
|
|
||||||
qpid_codec = importutils.try_import("qpid.codec010")
|
|
||||||
qpid_messaging = importutils.try_import("qpid.messaging")
|
|
||||||
qpid_exceptions = importutils.try_import("qpid.messaging.exceptions")
|
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
qpid_opts = [
|
|
||||||
cfg.StrOpt('qpid_hostname',
|
|
||||||
default='localhost',
|
|
||||||
help='Qpid broker hostname'),
|
|
||||||
cfg.IntOpt('qpid_port',
|
|
||||||
default=5672,
|
|
||||||
help='Qpid broker port'),
|
|
||||||
cfg.ListOpt('qpid_hosts',
|
|
||||||
default=['$qpid_hostname:$qpid_port'],
|
|
||||||
help='Qpid HA cluster host:port pairs'),
|
|
||||||
cfg.StrOpt('qpid_username',
|
|
||||||
default='',
|
|
||||||
help='Username for qpid connection'),
|
|
||||||
cfg.StrOpt('qpid_password',
|
|
||||||
default='',
|
|
||||||
help='Password for qpid connection',
|
|
||||||
secret=True),
|
|
||||||
cfg.StrOpt('qpid_sasl_mechanisms',
|
|
||||||
default='',
|
|
||||||
help='Space separated list of SASL mechanisms to use for auth'),
|
|
||||||
cfg.IntOpt('qpid_heartbeat',
|
|
||||||
default=60,
|
|
||||||
help='Seconds between connection keepalive heartbeats'),
|
|
||||||
cfg.StrOpt('qpid_protocol',
|
|
||||||
default='tcp',
|
|
||||||
help="Transport to use, either 'tcp' or 'ssl'"),
|
|
||||||
cfg.BoolOpt('qpid_tcp_nodelay',
|
|
||||||
default=True,
|
|
||||||
help='Disable Nagle algorithm'),
|
|
||||||
]
|
|
||||||
|
|
||||||
cfg.CONF.register_opts(qpid_opts)
|
|
||||||
|
|
||||||
JSON_CONTENT_TYPE = 'application/json; charset=utf8'
|
|
||||||
|
|
||||||
|
|
||||||
class ConsumerBase(object):
|
|
||||||
"""Consumer base class."""
|
|
||||||
|
|
||||||
def __init__(self, session, callback, node_name, node_opts,
|
|
||||||
link_name, link_opts):
|
|
||||||
"""Declare a queue on an amqp session.
|
|
||||||
|
|
||||||
'session' is the amqp session to use
|
|
||||||
'callback' is the callback to call when messages are received
|
|
||||||
'node_name' is the first part of the Qpid address string, before ';'
|
|
||||||
'node_opts' will be applied to the "x-declare" section of "node"
|
|
||||||
in the address string.
|
|
||||||
'link_name' goes into the "name" field of the "link" in the address
|
|
||||||
string
|
|
||||||
'link_opts' will be applied to the "x-declare" section of "link"
|
|
||||||
in the address string.
|
|
||||||
"""
|
|
||||||
self.callback = callback
|
|
||||||
self.receiver = None
|
|
||||||
self.session = None
|
|
||||||
|
|
||||||
addr_opts = {
|
|
||||||
"create": "always",
|
|
||||||
"node": {
|
|
||||||
"type": "topic",
|
|
||||||
"x-declare": {
|
|
||||||
"durable": True,
|
|
||||||
"auto-delete": True,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"link": {
|
|
||||||
"name": link_name,
|
|
||||||
"durable": True,
|
|
||||||
"x-declare": {
|
|
||||||
"durable": False,
|
|
||||||
"auto-delete": True,
|
|
||||||
"exclusive": False,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
addr_opts["node"]["x-declare"].update(node_opts)
|
|
||||||
addr_opts["link"]["x-declare"].update(link_opts)
|
|
||||||
|
|
||||||
self.address = "%s ; %s" % (node_name, jsonutils.dumps(addr_opts))
|
|
||||||
|
|
||||||
self.reconnect(session)
|
|
||||||
|
|
||||||
def reconnect(self, session):
|
|
||||||
"""Re-declare the receiver after a qpid reconnect"""
|
|
||||||
self.session = session
|
|
||||||
self.receiver = session.receiver(self.address)
|
|
||||||
self.receiver.capacity = 1
|
|
||||||
|
|
||||||
def _unpack_json_msg(self, msg):
|
|
||||||
"""Load the JSON data in msg if msg.content_type indicates that it
|
|
||||||
is necessary. Put the loaded data back into msg.content and
|
|
||||||
update msg.content_type appropriately.
|
|
||||||
|
|
||||||
A Qpid Message containing a dict will have a content_type of
|
|
||||||
'amqp/map', whereas one containing a string that needs to be converted
|
|
||||||
back from JSON will have a content_type of JSON_CONTENT_TYPE.
|
|
||||||
|
|
||||||
:param msg: a Qpid Message object
|
|
||||||
:returns: None
|
|
||||||
"""
|
|
||||||
if msg.content_type == JSON_CONTENT_TYPE:
|
|
||||||
msg.content = jsonutils.loads(msg.content)
|
|
||||||
msg.content_type = 'amqp/map'
|
|
||||||
|
|
||||||
def consume(self):
|
|
||||||
"""Fetch the message and pass it to the callback object"""
|
|
||||||
message = self.receiver.fetch()
|
|
||||||
try:
|
|
||||||
self._unpack_json_msg(message)
|
|
||||||
msg = rpc_common.deserialize_msg(message.content)
|
|
||||||
self.callback(msg)
|
|
||||||
except Exception:
|
|
||||||
LOG.exception(_("Failed to process message... skipping it."))
|
|
||||||
finally:
|
|
||||||
self.session.acknowledge(message)
|
|
||||||
|
|
||||||
def get_receiver(self):
|
|
||||||
return self.receiver
|
|
||||||
|
|
||||||
|
|
||||||
class DirectConsumer(ConsumerBase):
|
|
||||||
"""Queue/consumer class for 'direct'"""
|
|
||||||
|
|
||||||
def __init__(self, conf, session, msg_id, callback):
|
|
||||||
"""Init a 'direct' queue.
|
|
||||||
|
|
||||||
'session' is the amqp session to use
|
|
||||||
'msg_id' is the msg_id to listen on
|
|
||||||
'callback' is the callback to call when messages are received
|
|
||||||
"""
|
|
||||||
|
|
||||||
super(DirectConsumer, self).__init__(session, callback,
|
|
||||||
"%s/%s" % (msg_id, msg_id),
|
|
||||||
{"type": "direct"},
|
|
||||||
msg_id,
|
|
||||||
{"exclusive": True})
|
|
||||||
|
|
||||||
|
|
||||||
class TopicConsumer(ConsumerBase):
|
|
||||||
"""Consumer class for 'topic'"""
|
|
||||||
|
|
||||||
def __init__(self, conf, session, topic, callback, name=None,
|
|
||||||
exchange_name=None):
|
|
||||||
"""Init a 'topic' queue.
|
|
||||||
|
|
||||||
:param session: the amqp session to use
|
|
||||||
:param topic: is the topic to listen on
|
|
||||||
:paramtype topic: str
|
|
||||||
:param callback: the callback to call when messages are received
|
|
||||||
:param name: optional queue name, defaults to topic
|
|
||||||
"""
|
|
||||||
|
|
||||||
exchange_name = exchange_name or rpc_amqp.get_control_exchange(conf)
|
|
||||||
super(TopicConsumer, self).__init__(session, callback,
|
|
||||||
"%s/%s" % (exchange_name, topic),
|
|
||||||
{}, name or topic, {})
|
|
||||||
|
|
||||||
|
|
||||||
class FanoutConsumer(ConsumerBase):
|
|
||||||
"""Consumer class for 'fanout'"""
|
|
||||||
|
|
||||||
def __init__(self, conf, session, topic, callback):
|
|
||||||
"""Init a 'fanout' queue.
|
|
||||||
|
|
||||||
'session' is the amqp session to use
|
|
||||||
'topic' is the topic to listen on
|
|
||||||
'callback' is the callback to call when messages are received
|
|
||||||
"""
|
|
||||||
|
|
||||||
super(FanoutConsumer, self).__init__(
|
|
||||||
session, callback,
|
|
||||||
"%s_fanout" % topic,
|
|
||||||
{"durable": False, "type": "fanout"},
|
|
||||||
"%s_fanout_%s" % (topic, uuid.uuid4().hex),
|
|
||||||
{"exclusive": True})
|
|
||||||
|
|
||||||
|
|
||||||
class Publisher(object):
|
|
||||||
"""Base Publisher class"""
|
|
||||||
|
|
||||||
def __init__(self, session, node_name, node_opts=None):
|
|
||||||
"""Init the Publisher class with the exchange_name, routing_key,
|
|
||||||
and other options
|
|
||||||
"""
|
|
||||||
self.sender = None
|
|
||||||
self.session = session
|
|
||||||
|
|
||||||
addr_opts = {
|
|
||||||
"create": "always",
|
|
||||||
"node": {
|
|
||||||
"type": "topic",
|
|
||||||
"x-declare": {
|
|
||||||
"durable": False,
|
|
||||||
# auto-delete isn't implemented for exchanges in qpid,
|
|
||||||
# but put in here anyway
|
|
||||||
"auto-delete": True,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if node_opts:
|
|
||||||
addr_opts["node"]["x-declare"].update(node_opts)
|
|
||||||
|
|
||||||
self.address = "%s ; %s" % (node_name, jsonutils.dumps(addr_opts))
|
|
||||||
|
|
||||||
self.reconnect(session)
|
|
||||||
|
|
||||||
def reconnect(self, session):
|
|
||||||
"""Re-establish the Sender after a reconnection"""
|
|
||||||
self.sender = session.sender(self.address)
|
|
||||||
|
|
||||||
def _pack_json_msg(self, msg):
|
|
||||||
"""Qpid cannot serialize dicts containing strings longer than 65535
|
|
||||||
characters. This function dumps the message content to a JSON
|
|
||||||
string, which Qpid is able to handle.
|
|
||||||
|
|
||||||
:param msg: May be either a Qpid Message object or a bare dict.
|
|
||||||
:returns: A Qpid Message with its content field JSON encoded.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
msg.content = jsonutils.dumps(msg.content)
|
|
||||||
except AttributeError:
|
|
||||||
# Need to have a Qpid message so we can set the content_type.
|
|
||||||
msg = qpid_messaging.Message(jsonutils.dumps(msg))
|
|
||||||
msg.content_type = JSON_CONTENT_TYPE
|
|
||||||
return msg
|
|
||||||
|
|
||||||
def send(self, msg):
|
|
||||||
"""Send a message"""
|
|
||||||
try:
|
|
||||||
# Check if Qpid can encode the message
|
|
||||||
check_msg = msg
|
|
||||||
if not hasattr(check_msg, 'content_type'):
|
|
||||||
check_msg = qpid_messaging.Message(msg)
|
|
||||||
content_type = check_msg.content_type
|
|
||||||
enc, dec = qpid_messaging.message.get_codec(content_type)
|
|
||||||
enc(check_msg.content)
|
|
||||||
except qpid_codec.CodecException:
|
|
||||||
# This means the message couldn't be serialized as a dict.
|
|
||||||
msg = self._pack_json_msg(msg)
|
|
||||||
self.sender.send(msg)
|
|
||||||
|
|
||||||
|
|
||||||
class DirectPublisher(Publisher):
|
|
||||||
"""Publisher class for 'direct'"""
|
|
||||||
def __init__(self, conf, session, msg_id):
|
|
||||||
"""Init a 'direct' publisher."""
|
|
||||||
super(DirectPublisher, self).__init__(session, msg_id,
|
|
||||||
{"type": "Direct"})
|
|
||||||
|
|
||||||
|
|
||||||
class TopicPublisher(Publisher):
|
|
||||||
"""Publisher class for 'topic'"""
|
|
||||||
def __init__(self, conf, session, topic):
|
|
||||||
"""init a 'topic' publisher.
|
|
||||||
"""
|
|
||||||
exchange_name = rpc_amqp.get_control_exchange(conf)
|
|
||||||
super(TopicPublisher, self).__init__(session,
|
|
||||||
"%s/%s" % (exchange_name, topic))
|
|
||||||
|
|
||||||
|
|
||||||
class FanoutPublisher(Publisher):
|
|
||||||
"""Publisher class for 'fanout'"""
|
|
||||||
def __init__(self, conf, session, topic):
|
|
||||||
"""init a 'fanout' publisher.
|
|
||||||
"""
|
|
||||||
super(FanoutPublisher, self).__init__(
|
|
||||||
session,
|
|
||||||
"%s_fanout" % topic, {"type": "fanout"})
|
|
||||||
|
|
||||||
|
|
||||||
class NotifyPublisher(Publisher):
|
|
||||||
"""Publisher class for notifications"""
|
|
||||||
def __init__(self, conf, session, topic):
|
|
||||||
"""init a 'topic' publisher.
|
|
||||||
"""
|
|
||||||
exchange_name = rpc_amqp.get_control_exchange(conf)
|
|
||||||
super(NotifyPublisher, self).__init__(session,
|
|
||||||
"%s/%s" % (exchange_name, topic),
|
|
||||||
{"durable": True})
|
|
||||||
|
|
||||||
|
|
||||||
class Connection(object):
|
|
||||||
"""Connection object."""
|
|
||||||
|
|
||||||
pool = None
|
|
||||||
|
|
||||||
def __init__(self, conf, server_params=None):
|
|
||||||
if not qpid_messaging:
|
|
||||||
raise ImportError("Failed to import qpid.messaging")
|
|
||||||
|
|
||||||
self.session = None
|
|
||||||
self.consumers = {}
|
|
||||||
self.consumer_thread = None
|
|
||||||
self.proxy_callbacks = []
|
|
||||||
self.conf = conf
|
|
||||||
|
|
||||||
if server_params and 'hostname' in server_params:
|
|
||||||
# NOTE(russellb) This enables support for cast_to_server.
|
|
||||||
server_params['qpid_hosts'] = [
|
|
||||||
'%s:%d' % (server_params['hostname'],
|
|
||||||
server_params.get('port', 5672))
|
|
||||||
]
|
|
||||||
|
|
||||||
params = {
|
|
||||||
'qpid_hosts': self.conf.qpid_hosts,
|
|
||||||
'username': self.conf.qpid_username,
|
|
||||||
'password': self.conf.qpid_password,
|
|
||||||
}
|
|
||||||
params.update(server_params or {})
|
|
||||||
|
|
||||||
self.brokers = params['qpid_hosts']
|
|
||||||
self.username = params['username']
|
|
||||||
self.password = params['password']
|
|
||||||
self.connection_create(self.brokers[0])
|
|
||||||
self.reconnect()
|
|
||||||
|
|
||||||
def connection_create(self, broker):
|
|
||||||
# Create the connection - this does not open the connection
|
|
||||||
self.connection = qpid_messaging.Connection(broker)
|
|
||||||
|
|
||||||
# Check if flags are set and if so set them for the connection
|
|
||||||
# before we call open
|
|
||||||
self.connection.username = self.username
|
|
||||||
self.connection.password = self.password
|
|
||||||
|
|
||||||
self.connection.sasl_mechanisms = self.conf.qpid_sasl_mechanisms
|
|
||||||
# Reconnection is done by self.reconnect()
|
|
||||||
self.connection.reconnect = False
|
|
||||||
self.connection.heartbeat = self.conf.qpid_heartbeat
|
|
||||||
self.connection.protocol = self.conf.qpid_protocol
|
|
||||||
self.connection.tcp_nodelay = self.conf.qpid_tcp_nodelay
|
|
||||||
|
|
||||||
def _register_consumer(self, consumer):
|
|
||||||
self.consumers[str(consumer.get_receiver())] = consumer
|
|
||||||
|
|
||||||
def _lookup_consumer(self, receiver):
|
|
||||||
return self.consumers[str(receiver)]
|
|
||||||
|
|
||||||
def reconnect(self):
|
|
||||||
"""Handles reconnecting and re-establishing sessions and queues"""
|
|
||||||
attempt = 0
|
|
||||||
delay = 1
|
|
||||||
while True:
|
|
||||||
# Close the session if necessary
|
|
||||||
if self.connection.opened():
|
|
||||||
try:
|
|
||||||
self.connection.close()
|
|
||||||
except qpid_exceptions.ConnectionError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
broker = self.brokers[attempt % len(self.brokers)]
|
|
||||||
attempt += 1
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.connection_create(broker)
|
|
||||||
self.connection.open()
|
|
||||||
except qpid_exceptions.ConnectionError, e:
|
|
||||||
msg_dict = dict(e=e, delay=delay)
|
|
||||||
msg = _("Unable to connect to AMQP server: %(e)s. "
|
|
||||||
"Sleeping %(delay)s seconds") % msg_dict
|
|
||||||
LOG.error(msg)
|
|
||||||
time.sleep(delay)
|
|
||||||
delay = min(2 * delay, 60)
|
|
||||||
else:
|
|
||||||
LOG.info(_('Connected to AMQP server on %s'), broker)
|
|
||||||
break
|
|
||||||
|
|
||||||
self.session = self.connection.session()
|
|
||||||
|
|
||||||
if self.consumers:
|
|
||||||
consumers = self.consumers
|
|
||||||
self.consumers = {}
|
|
||||||
|
|
||||||
for consumer in consumers.itervalues():
|
|
||||||
consumer.reconnect(self.session)
|
|
||||||
self._register_consumer(consumer)
|
|
||||||
|
|
||||||
LOG.debug(_("Re-established AMQP queues"))
|
|
||||||
|
|
||||||
def ensure(self, error_callback, method, *args, **kwargs):
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
return method(*args, **kwargs)
|
|
||||||
except (qpid_exceptions.Empty,
|
|
||||||
qpid_exceptions.ConnectionError), e:
|
|
||||||
if error_callback:
|
|
||||||
error_callback(e)
|
|
||||||
self.reconnect()
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
"""Close/release this connection"""
|
|
||||||
self.cancel_consumer_thread()
|
|
||||||
self.wait_on_proxy_callbacks()
|
|
||||||
self.connection.close()
|
|
||||||
self.connection = None
|
|
||||||
|
|
||||||
def reset(self):
|
|
||||||
"""Reset a connection so it can be used again"""
|
|
||||||
self.cancel_consumer_thread()
|
|
||||||
self.wait_on_proxy_callbacks()
|
|
||||||
self.session.close()
|
|
||||||
self.session = self.connection.session()
|
|
||||||
self.consumers = {}
|
|
||||||
|
|
||||||
def declare_consumer(self, consumer_cls, topic, callback):
|
|
||||||
"""Create a Consumer using the class that was passed in and
|
|
||||||
add it to our list of consumers
|
|
||||||
"""
|
|
||||||
def _connect_error(exc):
|
|
||||||
log_info = {'topic': topic, 'err_str': str(exc)}
|
|
||||||
LOG.error(_("Failed to declare consumer for topic '%(topic)s': "
|
|
||||||
"%(err_str)s") % log_info)
|
|
||||||
|
|
||||||
def _declare_consumer():
|
|
||||||
consumer = consumer_cls(self.conf, self.session, topic, callback)
|
|
||||||
self._register_consumer(consumer)
|
|
||||||
return consumer
|
|
||||||
|
|
||||||
return self.ensure(_connect_error, _declare_consumer)
|
|
||||||
|
|
||||||
def iterconsume(self, limit=None, timeout=None):
|
|
||||||
"""Return an iterator that will consume from all queues/consumers"""
|
|
||||||
|
|
||||||
def _error_callback(exc):
|
|
||||||
if isinstance(exc, qpid_exceptions.Empty):
|
|
||||||
LOG.debug(_('Timed out waiting for RPC response: %s') %
|
|
||||||
str(exc))
|
|
||||||
raise rpc_common.Timeout()
|
|
||||||
else:
|
|
||||||
LOG.exception(_('Failed to consume message from queue: %s') %
|
|
||||||
str(exc))
|
|
||||||
|
|
||||||
def _consume():
|
|
||||||
nxt_receiver = self.session.next_receiver(timeout=timeout)
|
|
||||||
try:
|
|
||||||
self._lookup_consumer(nxt_receiver).consume()
|
|
||||||
except Exception:
|
|
||||||
LOG.exception(_("Error processing message. Skipping it."))
|
|
||||||
|
|
||||||
for iteration in itertools.count(0):
|
|
||||||
if limit and iteration >= limit:
|
|
||||||
raise StopIteration
|
|
||||||
yield self.ensure(_error_callback, _consume)
|
|
||||||
|
|
||||||
def cancel_consumer_thread(self):
|
|
||||||
"""Cancel a consumer thread"""
|
|
||||||
if self.consumer_thread is not None:
|
|
||||||
self.consumer_thread.kill()
|
|
||||||
try:
|
|
||||||
self.consumer_thread.wait()
|
|
||||||
except greenlet.GreenletExit:
|
|
||||||
pass
|
|
||||||
self.consumer_thread = None
|
|
||||||
|
|
||||||
def wait_on_proxy_callbacks(self):
|
|
||||||
"""Wait for all proxy callback threads to exit."""
|
|
||||||
for proxy_cb in self.proxy_callbacks:
|
|
||||||
proxy_cb.wait()
|
|
||||||
|
|
||||||
def publisher_send(self, cls, topic, msg):
|
|
||||||
"""Send to a publisher based on the publisher class"""
|
|
||||||
|
|
||||||
def _connect_error(exc):
|
|
||||||
log_info = {'topic': topic, 'err_str': str(exc)}
|
|
||||||
LOG.exception(_("Failed to publish message to topic "
|
|
||||||
"'%(topic)s': %(err_str)s") % log_info)
|
|
||||||
|
|
||||||
def _publisher_send():
|
|
||||||
publisher = cls(self.conf, self.session, topic)
|
|
||||||
publisher.send(msg)
|
|
||||||
|
|
||||||
return self.ensure(_connect_error, _publisher_send)
|
|
||||||
|
|
||||||
def declare_direct_consumer(self, topic, callback):
|
|
||||||
"""Create a 'direct' queue.
|
|
||||||
In nova's use, this is generally a msg_id queue used for
|
|
||||||
responses for call/multicall
|
|
||||||
"""
|
|
||||||
self.declare_consumer(DirectConsumer, topic, callback)
|
|
||||||
|
|
||||||
def declare_topic_consumer(self, topic, callback=None, queue_name=None,
|
|
||||||
exchange_name=None):
|
|
||||||
"""Create a 'topic' consumer."""
|
|
||||||
self.declare_consumer(functools.partial(TopicConsumer,
|
|
||||||
name=queue_name,
|
|
||||||
exchange_name=exchange_name,
|
|
||||||
),
|
|
||||||
topic, callback)
|
|
||||||
|
|
||||||
def declare_fanout_consumer(self, topic, callback):
|
|
||||||
"""Create a 'fanout' consumer"""
|
|
||||||
self.declare_consumer(FanoutConsumer, topic, callback)
|
|
||||||
|
|
||||||
def direct_send(self, msg_id, msg):
|
|
||||||
"""Send a 'direct' message"""
|
|
||||||
self.publisher_send(DirectPublisher, msg_id, msg)
|
|
||||||
|
|
||||||
def topic_send(self, topic, msg, timeout=None):
|
|
||||||
"""Send a 'topic' message"""
|
|
||||||
#
|
|
||||||
# We want to create a message with attributes, e.g. a TTL. We
|
|
||||||
# don't really need to keep 'msg' in its JSON format any longer
|
|
||||||
# so let's create an actual qpid message here and get some
|
|
||||||
# value-add on the go.
|
|
||||||
#
|
|
||||||
# WARNING: Request timeout happens to be in the same units as
|
|
||||||
# qpid's TTL (seconds). If this changes in the future, then this
|
|
||||||
# will need to be altered accordingly.
|
|
||||||
#
|
|
||||||
qpid_message = qpid_messaging.Message(content=msg, ttl=timeout)
|
|
||||||
self.publisher_send(TopicPublisher, topic, qpid_message)
|
|
||||||
|
|
||||||
def fanout_send(self, topic, msg):
|
|
||||||
"""Send a 'fanout' message"""
|
|
||||||
self.publisher_send(FanoutPublisher, topic, msg)
|
|
||||||
|
|
||||||
def notify_send(self, topic, msg, **kwargs):
|
|
||||||
"""Send a notify message on a topic"""
|
|
||||||
self.publisher_send(NotifyPublisher, topic, msg)
|
|
||||||
|
|
||||||
def consume(self, limit=None):
|
|
||||||
"""Consume from all queues/consumers"""
|
|
||||||
it = self.iterconsume(limit=limit)
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
it.next()
|
|
||||||
except StopIteration:
|
|
||||||
return
|
|
||||||
|
|
||||||
def consume_in_thread(self):
|
|
||||||
"""Consumer from all queues/consumers in a greenthread"""
|
|
||||||
def _consumer_thread():
|
|
||||||
try:
|
|
||||||
self.consume()
|
|
||||||
except greenlet.GreenletExit:
|
|
||||||
return
|
|
||||||
if self.consumer_thread is None:
|
|
||||||
self.consumer_thread = eventlet.spawn(_consumer_thread)
|
|
||||||
return self.consumer_thread
|
|
||||||
|
|
||||||
def create_consumer(self, topic, proxy, fanout=False):
|
|
||||||
"""Create a consumer that calls a method in a proxy object"""
|
|
||||||
proxy_cb = rpc_amqp.ProxyCallback(
|
|
||||||
self.conf, proxy,
|
|
||||||
rpc_amqp.get_connection_pool(self.conf, Connection))
|
|
||||||
self.proxy_callbacks.append(proxy_cb)
|
|
||||||
|
|
||||||
if fanout:
|
|
||||||
consumer = FanoutConsumer(self.conf, self.session, topic, proxy_cb)
|
|
||||||
else:
|
|
||||||
consumer = TopicConsumer(self.conf, self.session, topic, proxy_cb)
|
|
||||||
|
|
||||||
self._register_consumer(consumer)
|
|
||||||
|
|
||||||
return consumer
|
|
||||||
|
|
||||||
def create_worker(self, topic, proxy, pool_name):
|
|
||||||
"""Create a worker that calls a method in a proxy object"""
|
|
||||||
proxy_cb = rpc_amqp.ProxyCallback(
|
|
||||||
self.conf, proxy,
|
|
||||||
rpc_amqp.get_connection_pool(self.conf, Connection))
|
|
||||||
self.proxy_callbacks.append(proxy_cb)
|
|
||||||
|
|
||||||
consumer = TopicConsumer(self.conf, self.session, topic, proxy_cb,
|
|
||||||
name=pool_name)
|
|
||||||
|
|
||||||
self._register_consumer(consumer)
|
|
||||||
|
|
||||||
return consumer
|
|
||||||
|
|
||||||
def join_consumer_pool(self, callback, pool_name, topic,
|
|
||||||
exchange_name=None):
|
|
||||||
"""Register as a member of a group of consumers for a given topic from
|
|
||||||
the specified exchange.
|
|
||||||
|
|
||||||
Exactly one member of a given pool will receive each message.
|
|
||||||
|
|
||||||
A message will be delivered to multiple pools, if more than
|
|
||||||
one is created.
|
|
||||||
"""
|
|
||||||
callback_wrapper = rpc_amqp.CallbackWrapper(
|
|
||||||
conf=self.conf,
|
|
||||||
callback=callback,
|
|
||||||
connection_pool=rpc_amqp.get_connection_pool(self.conf,
|
|
||||||
Connection),
|
|
||||||
)
|
|
||||||
self.proxy_callbacks.append(callback_wrapper)
|
|
||||||
|
|
||||||
consumer = TopicConsumer(conf=self.conf,
|
|
||||||
session=self.session,
|
|
||||||
topic=topic,
|
|
||||||
callback=callback_wrapper,
|
|
||||||
name=pool_name,
|
|
||||||
exchange_name=exchange_name)
|
|
||||||
|
|
||||||
self._register_consumer(consumer)
|
|
||||||
return consumer
|
|
||||||
|
|
||||||
|
|
||||||
def create_connection(conf, new=True):
|
|
||||||
"""Create a connection"""
|
|
||||||
return rpc_amqp.create_connection(
|
|
||||||
conf, new,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection))
|
|
||||||
|
|
||||||
|
|
||||||
def multicall(conf, context, topic, msg, timeout=None):
|
|
||||||
"""Make a call that returns multiple times."""
|
|
||||||
return rpc_amqp.multicall(
|
|
||||||
conf, context, topic, msg, timeout,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection))
|
|
||||||
|
|
||||||
|
|
||||||
def call(conf, context, topic, msg, timeout=None):
|
|
||||||
"""Sends a message on a topic and wait for a response."""
|
|
||||||
return rpc_amqp.call(
|
|
||||||
conf, context, topic, msg, timeout,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection))
|
|
||||||
|
|
||||||
|
|
||||||
def cast(conf, context, topic, msg):
|
|
||||||
"""Sends a message on a topic without waiting for a response."""
|
|
||||||
return rpc_amqp.cast(
|
|
||||||
conf, context, topic, msg,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection))
|
|
||||||
|
|
||||||
|
|
||||||
def fanout_cast(conf, context, topic, msg):
|
|
||||||
"""Sends a message on a fanout exchange without waiting for a response."""
|
|
||||||
return rpc_amqp.fanout_cast(
|
|
||||||
conf, context, topic, msg,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection))
|
|
||||||
|
|
||||||
|
|
||||||
def cast_to_server(conf, context, server_params, topic, msg):
|
|
||||||
"""Sends a message on a topic to a specific server."""
|
|
||||||
return rpc_amqp.cast_to_server(
|
|
||||||
conf, context, server_params, topic, msg,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection))
|
|
||||||
|
|
||||||
|
|
||||||
def fanout_cast_to_server(conf, context, server_params, topic, msg):
|
|
||||||
"""Sends a message on a fanout exchange to a specific server."""
|
|
||||||
return rpc_amqp.fanout_cast_to_server(
|
|
||||||
conf, context, server_params, topic, msg,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection))
|
|
||||||
|
|
||||||
|
|
||||||
def notify(conf, context, topic, msg, envelope):
|
|
||||||
"""Sends a notification event on a topic."""
|
|
||||||
return rpc_amqp.notify(conf, context, topic, msg,
|
|
||||||
rpc_amqp.get_connection_pool(conf, Connection),
|
|
||||||
envelope)
|
|
||||||
|
|
||||||
|
|
||||||
def cleanup():
|
|
||||||
return rpc_amqp.cleanup(Connection.pool)
|
|
||||||
@@ -1,845 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2011 Cloudscaling Group, Inc
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
import os
|
|
||||||
import pprint
|
|
||||||
import re
|
|
||||||
import socket
|
|
||||||
import sys
|
|
||||||
import types
|
|
||||||
import uuid
|
|
||||||
|
|
||||||
import eventlet
|
|
||||||
import greenlet
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common import excutils
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import importutils
|
|
||||||
from galloper.openstack.common import jsonutils
|
|
||||||
from galloper.openstack.common import processutils as utils
|
|
||||||
from galloper.openstack.common.rpc import common as rpc_common
|
|
||||||
|
|
||||||
zmq = importutils.try_import('eventlet.green.zmq')
|
|
||||||
|
|
||||||
# for convenience, are not modified.
|
|
||||||
pformat = pprint.pformat
|
|
||||||
Timeout = eventlet.timeout.Timeout
|
|
||||||
LOG = rpc_common.LOG
|
|
||||||
RemoteError = rpc_common.RemoteError
|
|
||||||
RPCException = rpc_common.RPCException
|
|
||||||
|
|
||||||
zmq_opts = [
|
|
||||||
cfg.StrOpt('rpc_zmq_bind_address', default='*',
|
|
||||||
help='ZeroMQ bind address. Should be a wildcard (*), '
|
|
||||||
'an ethernet interface, or IP. '
|
|
||||||
'The "host" option should point or resolve to this '
|
|
||||||
'address.'),
|
|
||||||
|
|
||||||
# The module.Class to use for matchmaking.
|
|
||||||
cfg.StrOpt(
|
|
||||||
'rpc_zmq_matchmaker',
|
|
||||||
default=('galloper.openstack.common.rpc.'
|
|
||||||
'matchmaker.MatchMakerLocalhost'),
|
|
||||||
help='MatchMaker driver',
|
|
||||||
),
|
|
||||||
|
|
||||||
# The following port is unassigned by IANA as of 2012-05-21
|
|
||||||
cfg.IntOpt('rpc_zmq_port', default=9501,
|
|
||||||
help='ZeroMQ receiver listening port'),
|
|
||||||
|
|
||||||
cfg.IntOpt('rpc_zmq_contexts', default=1,
|
|
||||||
help='Number of ZeroMQ contexts, defaults to 1'),
|
|
||||||
|
|
||||||
cfg.IntOpt('rpc_zmq_topic_backlog', default=None,
|
|
||||||
help='Maximum number of ingress messages to locally buffer '
|
|
||||||
'per topic. Default is unlimited.'),
|
|
||||||
|
|
||||||
cfg.StrOpt('rpc_zmq_ipc_dir', default='/var/run/openstack',
|
|
||||||
help='Directory for holding IPC sockets'),
|
|
||||||
|
|
||||||
cfg.StrOpt('rpc_zmq_host', default=socket.gethostname(),
|
|
||||||
help='Name of this node. Must be a valid hostname, FQDN, or '
|
|
||||||
'IP address. Must match "host" option, if running Nova.')
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF.register_opts(zmq_opts)
|
|
||||||
|
|
||||||
ZMQ_CTX = None # ZeroMQ Context, must be global.
|
|
||||||
matchmaker = None # memoized matchmaker object
|
|
||||||
|
|
||||||
|
|
||||||
def _serialize(data):
|
|
||||||
"""
|
|
||||||
Serialization wrapper
|
|
||||||
We prefer using JSON, but it cannot encode all types.
|
|
||||||
Error if a developer passes us bad data.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
return jsonutils.dumps(data, ensure_ascii=True)
|
|
||||||
except TypeError:
|
|
||||||
with excutils.save_and_reraise_exception():
|
|
||||||
LOG.error(_("JSON serialization failed."))
|
|
||||||
|
|
||||||
|
|
||||||
def _deserialize(data):
|
|
||||||
"""
|
|
||||||
Deserialization wrapper
|
|
||||||
"""
|
|
||||||
LOG.debug(_("Deserializing: %s"), data)
|
|
||||||
return jsonutils.loads(data)
|
|
||||||
|
|
||||||
|
|
||||||
class ZmqSocket(object):
|
|
||||||
"""
|
|
||||||
A tiny wrapper around ZeroMQ to simplify the send/recv protocol
|
|
||||||
and connection management.
|
|
||||||
|
|
||||||
Can be used as a Context (supports the 'with' statement).
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, addr, zmq_type, bind=True, subscribe=None):
|
|
||||||
self.sock = _get_ctxt().socket(zmq_type)
|
|
||||||
self.addr = addr
|
|
||||||
self.type = zmq_type
|
|
||||||
self.subscriptions = []
|
|
||||||
|
|
||||||
# Support failures on sending/receiving on wrong socket type.
|
|
||||||
self.can_recv = zmq_type in (zmq.PULL, zmq.SUB)
|
|
||||||
self.can_send = zmq_type in (zmq.PUSH, zmq.PUB)
|
|
||||||
self.can_sub = zmq_type in (zmq.SUB, )
|
|
||||||
|
|
||||||
# Support list, str, & None for subscribe arg (cast to list)
|
|
||||||
do_sub = {
|
|
||||||
list: subscribe,
|
|
||||||
str: [subscribe],
|
|
||||||
type(None): []
|
|
||||||
}[type(subscribe)]
|
|
||||||
|
|
||||||
for f in do_sub:
|
|
||||||
self.subscribe(f)
|
|
||||||
|
|
||||||
str_data = {'addr': addr, 'type': self.socket_s(),
|
|
||||||
'subscribe': subscribe, 'bind': bind}
|
|
||||||
|
|
||||||
LOG.debug(_("Connecting to %(addr)s with %(type)s"), str_data)
|
|
||||||
LOG.debug(_("-> Subscribed to %(subscribe)s"), str_data)
|
|
||||||
LOG.debug(_("-> bind: %(bind)s"), str_data)
|
|
||||||
|
|
||||||
try:
|
|
||||||
if bind:
|
|
||||||
self.sock.bind(addr)
|
|
||||||
else:
|
|
||||||
self.sock.connect(addr)
|
|
||||||
except Exception:
|
|
||||||
raise RPCException(_("Could not open socket."))
|
|
||||||
|
|
||||||
def socket_s(self):
|
|
||||||
"""Get socket type as string."""
|
|
||||||
t_enum = ('PUSH', 'PULL', 'PUB', 'SUB', 'REP', 'REQ', 'ROUTER',
|
|
||||||
'DEALER')
|
|
||||||
return dict(map(lambda t: (getattr(zmq, t), t), t_enum))[self.type]
|
|
||||||
|
|
||||||
def subscribe(self, msg_filter):
|
|
||||||
"""Subscribe."""
|
|
||||||
if not self.can_sub:
|
|
||||||
raise RPCException("Cannot subscribe on this socket.")
|
|
||||||
LOG.debug(_("Subscribing to %s"), msg_filter)
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.sock.setsockopt(zmq.SUBSCRIBE, msg_filter)
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
|
|
||||||
self.subscriptions.append(msg_filter)
|
|
||||||
|
|
||||||
def unsubscribe(self, msg_filter):
|
|
||||||
"""Unsubscribe."""
|
|
||||||
if msg_filter not in self.subscriptions:
|
|
||||||
return
|
|
||||||
self.sock.setsockopt(zmq.UNSUBSCRIBE, msg_filter)
|
|
||||||
self.subscriptions.remove(msg_filter)
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
if self.sock is None or self.sock.closed:
|
|
||||||
return
|
|
||||||
|
|
||||||
# We must unsubscribe, or we'll leak descriptors.
|
|
||||||
if len(self.subscriptions) > 0:
|
|
||||||
for f in self.subscriptions:
|
|
||||||
try:
|
|
||||||
self.sock.setsockopt(zmq.UNSUBSCRIBE, f)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
self.subscriptions = []
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Default is to linger
|
|
||||||
self.sock.close()
|
|
||||||
except Exception:
|
|
||||||
# While this is a bad thing to happen,
|
|
||||||
# it would be much worse if some of the code calling this
|
|
||||||
# were to fail. For now, lets log, and later evaluate
|
|
||||||
# if we can safely raise here.
|
|
||||||
LOG.error("ZeroMQ socket could not be closed.")
|
|
||||||
self.sock = None
|
|
||||||
|
|
||||||
def recv(self):
|
|
||||||
if not self.can_recv:
|
|
||||||
raise RPCException(_("You cannot recv on this socket."))
|
|
||||||
return self.sock.recv_multipart()
|
|
||||||
|
|
||||||
def send(self, data):
|
|
||||||
if not self.can_send:
|
|
||||||
raise RPCException(_("You cannot send on this socket."))
|
|
||||||
self.sock.send_multipart(data)
|
|
||||||
|
|
||||||
|
|
||||||
class ZmqClient(object):
|
|
||||||
"""Client for ZMQ sockets."""
|
|
||||||
|
|
||||||
def __init__(self, addr, socket_type=None, bind=False):
|
|
||||||
if socket_type is None:
|
|
||||||
socket_type = zmq.PUSH
|
|
||||||
self.outq = ZmqSocket(addr, socket_type, bind=bind)
|
|
||||||
|
|
||||||
def cast(self, msg_id, topic, data, envelope=False):
|
|
||||||
msg_id = msg_id or 0
|
|
||||||
|
|
||||||
if not (envelope or rpc_common._SEND_RPC_ENVELOPE):
|
|
||||||
self.outq.send(map(bytes,
|
|
||||||
(msg_id, topic, 'cast', _serialize(data))))
|
|
||||||
return
|
|
||||||
|
|
||||||
rpc_envelope = rpc_common.serialize_msg(data[1], envelope)
|
|
||||||
zmq_msg = reduce(lambda x, y: x + y, rpc_envelope.items())
|
|
||||||
self.outq.send(map(bytes,
|
|
||||||
(msg_id, topic, 'impl_zmq_v2', data[0]) + zmq_msg))
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
self.outq.close()
|
|
||||||
|
|
||||||
|
|
||||||
class RpcContext(rpc_common.CommonRpcContext):
|
|
||||||
"""Context that supports replying to a rpc.call."""
|
|
||||||
def __init__(self, **kwargs):
|
|
||||||
self.replies = []
|
|
||||||
super(RpcContext, self).__init__(**kwargs)
|
|
||||||
|
|
||||||
def deepcopy(self):
|
|
||||||
values = self.to_dict()
|
|
||||||
values['replies'] = self.replies
|
|
||||||
return self.__class__(**values)
|
|
||||||
|
|
||||||
def reply(self, reply=None, failure=None, ending=False):
|
|
||||||
if ending:
|
|
||||||
return
|
|
||||||
self.replies.append(reply)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def marshal(self, ctx):
|
|
||||||
ctx_data = ctx.to_dict()
|
|
||||||
return _serialize(ctx_data)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def unmarshal(self, data):
|
|
||||||
return RpcContext.from_dict(_deserialize(data))
|
|
||||||
|
|
||||||
|
|
||||||
class InternalContext(object):
|
|
||||||
"""Used by ConsumerBase as a private context for - methods."""
|
|
||||||
|
|
||||||
def __init__(self, proxy):
|
|
||||||
self.proxy = proxy
|
|
||||||
self.msg_waiter = None
|
|
||||||
|
|
||||||
def _get_response(self, ctx, proxy, topic, data):
|
|
||||||
"""Process a curried message and cast the result to topic."""
|
|
||||||
LOG.debug(_("Running func with context: %s"), ctx.to_dict())
|
|
||||||
data.setdefault('version', None)
|
|
||||||
data.setdefault('args', {})
|
|
||||||
|
|
||||||
try:
|
|
||||||
result = proxy.dispatch(
|
|
||||||
ctx, data['version'], data['method'], **data['args'])
|
|
||||||
return ConsumerBase.normalize_reply(result, ctx.replies)
|
|
||||||
except greenlet.GreenletExit:
|
|
||||||
# ignore these since they are just from shutdowns
|
|
||||||
pass
|
|
||||||
except rpc_common.ClientException, e:
|
|
||||||
LOG.debug(_("Expected exception during message handling (%s)") %
|
|
||||||
e._exc_info[1])
|
|
||||||
return {'exc':
|
|
||||||
rpc_common.serialize_remote_exception(e._exc_info,
|
|
||||||
log_failure=False)}
|
|
||||||
except Exception:
|
|
||||||
LOG.error(_("Exception during message handling"))
|
|
||||||
return {'exc':
|
|
||||||
rpc_common.serialize_remote_exception(sys.exc_info())}
|
|
||||||
|
|
||||||
def reply(self, ctx, proxy,
|
|
||||||
msg_id=None, context=None, topic=None, msg=None):
|
|
||||||
"""Reply to a casted call."""
|
|
||||||
# Our real method is curried into msg['args']
|
|
||||||
|
|
||||||
child_ctx = RpcContext.unmarshal(msg[0])
|
|
||||||
response = ConsumerBase.normalize_reply(
|
|
||||||
self._get_response(child_ctx, proxy, topic, msg[1]),
|
|
||||||
ctx.replies)
|
|
||||||
|
|
||||||
LOG.debug(_("Sending reply"))
|
|
||||||
_multi_send(_cast, ctx, topic, {
|
|
||||||
'method': '-process_reply',
|
|
||||||
'args': {
|
|
||||||
'msg_id': msg_id, # Include for Folsom compat.
|
|
||||||
'response': response
|
|
||||||
}
|
|
||||||
}, _msg_id=msg_id)
|
|
||||||
|
|
||||||
|
|
||||||
class ConsumerBase(object):
|
|
||||||
"""Base Consumer."""
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.private_ctx = InternalContext(None)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def normalize_reply(self, result, replies):
|
|
||||||
#TODO(ewindisch): re-evaluate and document this method.
|
|
||||||
if isinstance(result, types.GeneratorType):
|
|
||||||
return list(result)
|
|
||||||
elif replies:
|
|
||||||
return replies
|
|
||||||
else:
|
|
||||||
return [result]
|
|
||||||
|
|
||||||
def process(self, proxy, ctx, data):
|
|
||||||
data.setdefault('version', None)
|
|
||||||
data.setdefault('args', {})
|
|
||||||
|
|
||||||
# Method starting with - are
|
|
||||||
# processed internally. (non-valid method name)
|
|
||||||
method = data.get('method')
|
|
||||||
if not method:
|
|
||||||
LOG.error(_("RPC message did not include method."))
|
|
||||||
return
|
|
||||||
|
|
||||||
# Internal method
|
|
||||||
# uses internal context for safety.
|
|
||||||
if method == '-reply':
|
|
||||||
self.private_ctx.reply(ctx, proxy, **data['args'])
|
|
||||||
return
|
|
||||||
|
|
||||||
proxy.dispatch(ctx, data['version'],
|
|
||||||
data['method'], **data['args'])
|
|
||||||
|
|
||||||
|
|
||||||
class ZmqBaseReactor(ConsumerBase):
|
|
||||||
"""
|
|
||||||
A consumer class implementing a
|
|
||||||
centralized casting broker (PULL-PUSH)
|
|
||||||
for RoundRobin requests.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, conf):
|
|
||||||
super(ZmqBaseReactor, self).__init__()
|
|
||||||
|
|
||||||
self.mapping = {}
|
|
||||||
self.proxies = {}
|
|
||||||
self.threads = []
|
|
||||||
self.sockets = []
|
|
||||||
self.subscribe = {}
|
|
||||||
|
|
||||||
self.pool = eventlet.greenpool.GreenPool(conf.rpc_thread_pool_size)
|
|
||||||
|
|
||||||
def register(self, proxy, in_addr, zmq_type_in, out_addr=None,
|
|
||||||
zmq_type_out=None, in_bind=True, out_bind=True,
|
|
||||||
subscribe=None):
|
|
||||||
|
|
||||||
LOG.info(_("Registering reactor"))
|
|
||||||
|
|
||||||
if zmq_type_in not in (zmq.PULL, zmq.SUB):
|
|
||||||
raise RPCException("Bad input socktype")
|
|
||||||
|
|
||||||
# Items push in.
|
|
||||||
inq = ZmqSocket(in_addr, zmq_type_in, bind=in_bind,
|
|
||||||
subscribe=subscribe)
|
|
||||||
|
|
||||||
self.proxies[inq] = proxy
|
|
||||||
self.sockets.append(inq)
|
|
||||||
|
|
||||||
LOG.info(_("In reactor registered"))
|
|
||||||
|
|
||||||
if not out_addr:
|
|
||||||
return
|
|
||||||
|
|
||||||
if zmq_type_out not in (zmq.PUSH, zmq.PUB):
|
|
||||||
raise RPCException("Bad output socktype")
|
|
||||||
|
|
||||||
# Items push out.
|
|
||||||
outq = ZmqSocket(out_addr, zmq_type_out, bind=out_bind)
|
|
||||||
|
|
||||||
self.mapping[inq] = outq
|
|
||||||
self.mapping[outq] = inq
|
|
||||||
self.sockets.append(outq)
|
|
||||||
|
|
||||||
LOG.info(_("Out reactor registered"))
|
|
||||||
|
|
||||||
def consume_in_thread(self):
|
|
||||||
def _consume(sock):
|
|
||||||
LOG.info(_("Consuming socket"))
|
|
||||||
while True:
|
|
||||||
self.consume(sock)
|
|
||||||
|
|
||||||
for k in self.proxies.keys():
|
|
||||||
self.threads.append(
|
|
||||||
self.pool.spawn(_consume, k)
|
|
||||||
)
|
|
||||||
|
|
||||||
def wait(self):
|
|
||||||
for t in self.threads:
|
|
||||||
t.wait()
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
for s in self.sockets:
|
|
||||||
s.close()
|
|
||||||
|
|
||||||
for t in self.threads:
|
|
||||||
t.kill()
|
|
||||||
|
|
||||||
|
|
||||||
class ZmqProxy(ZmqBaseReactor):
|
|
||||||
"""
|
|
||||||
A consumer class implementing a
|
|
||||||
topic-based proxy, forwarding to
|
|
||||||
IPC sockets.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, conf):
|
|
||||||
super(ZmqProxy, self).__init__(conf)
|
|
||||||
pathsep = set((os.path.sep or '', os.path.altsep or '', '/', '\\'))
|
|
||||||
self.badchars = re.compile(r'[%s]' % re.escape(''.join(pathsep)))
|
|
||||||
|
|
||||||
self.topic_proxy = {}
|
|
||||||
|
|
||||||
def consume(self, sock):
|
|
||||||
ipc_dir = CONF.rpc_zmq_ipc_dir
|
|
||||||
|
|
||||||
#TODO(ewindisch): use zero-copy (i.e. references, not copying)
|
|
||||||
data = sock.recv()
|
|
||||||
topic = data[1]
|
|
||||||
|
|
||||||
LOG.debug(_("CONSUMER GOT %s"), ' '.join(map(pformat, data)))
|
|
||||||
|
|
||||||
if topic.startswith('fanout~'):
|
|
||||||
sock_type = zmq.PUB
|
|
||||||
topic = topic.split('.', 1)[0]
|
|
||||||
elif topic.startswith('zmq_replies'):
|
|
||||||
sock_type = zmq.PUB
|
|
||||||
else:
|
|
||||||
sock_type = zmq.PUSH
|
|
||||||
|
|
||||||
if topic not in self.topic_proxy:
|
|
||||||
def publisher(waiter):
|
|
||||||
LOG.info(_("Creating proxy for topic: %s"), topic)
|
|
||||||
|
|
||||||
try:
|
|
||||||
# The topic is received over the network,
|
|
||||||
# don't trust this input.
|
|
||||||
if self.badchars.search(topic) is not None:
|
|
||||||
emsg = _("Topic contained dangerous characters.")
|
|
||||||
LOG.warn(emsg)
|
|
||||||
raise RPCException(emsg)
|
|
||||||
|
|
||||||
out_sock = ZmqSocket("ipc://%s/zmq_topic_%s" %
|
|
||||||
(ipc_dir, topic),
|
|
||||||
sock_type, bind=True)
|
|
||||||
except RPCException:
|
|
||||||
waiter.send_exception(*sys.exc_info())
|
|
||||||
return
|
|
||||||
|
|
||||||
self.topic_proxy[topic] = eventlet.queue.LightQueue(
|
|
||||||
CONF.rpc_zmq_topic_backlog)
|
|
||||||
self.sockets.append(out_sock)
|
|
||||||
|
|
||||||
# It takes some time for a pub socket to open,
|
|
||||||
# before we can have any faith in doing a send() to it.
|
|
||||||
if sock_type == zmq.PUB:
|
|
||||||
eventlet.sleep(.5)
|
|
||||||
|
|
||||||
waiter.send(True)
|
|
||||||
|
|
||||||
while(True):
|
|
||||||
data = self.topic_proxy[topic].get()
|
|
||||||
out_sock.send(data)
|
|
||||||
LOG.debug(_("ROUTER RELAY-OUT SUCCEEDED %(data)s") %
|
|
||||||
{'data': data})
|
|
||||||
|
|
||||||
wait_sock_creation = eventlet.event.Event()
|
|
||||||
eventlet.spawn(publisher, wait_sock_creation)
|
|
||||||
|
|
||||||
try:
|
|
||||||
wait_sock_creation.wait()
|
|
||||||
except RPCException:
|
|
||||||
LOG.error(_("Topic socket file creation failed."))
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.topic_proxy[topic].put_nowait(data)
|
|
||||||
LOG.debug(_("ROUTER RELAY-OUT QUEUED %(data)s") %
|
|
||||||
{'data': data})
|
|
||||||
except eventlet.queue.Full:
|
|
||||||
LOG.error(_("Local per-topic backlog buffer full for topic "
|
|
||||||
"%(topic)s. Dropping message.") % {'topic': topic})
|
|
||||||
|
|
||||||
def consume_in_thread(self):
|
|
||||||
"""Runs the ZmqProxy service"""
|
|
||||||
ipc_dir = CONF.rpc_zmq_ipc_dir
|
|
||||||
consume_in = "tcp://%s:%s" % \
|
|
||||||
(CONF.rpc_zmq_bind_address,
|
|
||||||
CONF.rpc_zmq_port)
|
|
||||||
consumption_proxy = InternalContext(None)
|
|
||||||
|
|
||||||
if not os.path.isdir(ipc_dir):
|
|
||||||
try:
|
|
||||||
utils.execute('mkdir', '-p', ipc_dir, run_as_root=True)
|
|
||||||
utils.execute('chown', "%s:%s" % (os.getuid(), os.getgid()),
|
|
||||||
ipc_dir, run_as_root=True)
|
|
||||||
utils.execute('chmod', '750', ipc_dir, run_as_root=True)
|
|
||||||
except utils.ProcessExecutionError:
|
|
||||||
with excutils.save_and_reraise_exception():
|
|
||||||
LOG.error(_("Could not create IPC directory %s") %
|
|
||||||
(ipc_dir, ))
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.register(consumption_proxy,
|
|
||||||
consume_in,
|
|
||||||
zmq.PULL,
|
|
||||||
out_bind=True)
|
|
||||||
except zmq.ZMQError:
|
|
||||||
with excutils.save_and_reraise_exception():
|
|
||||||
LOG.error(_("Could not create ZeroMQ receiver daemon. "
|
|
||||||
"Socket may already be in use."))
|
|
||||||
|
|
||||||
super(ZmqProxy, self).consume_in_thread()
|
|
||||||
|
|
||||||
|
|
||||||
def unflatten_envelope(packenv):
|
|
||||||
"""Unflattens the RPC envelope.
|
|
||||||
Takes a list and returns a dictionary.
|
|
||||||
i.e. [1,2,3,4] => {1: 2, 3: 4}
|
|
||||||
"""
|
|
||||||
i = iter(packenv)
|
|
||||||
h = {}
|
|
||||||
try:
|
|
||||||
while True:
|
|
||||||
k = i.next()
|
|
||||||
h[k] = i.next()
|
|
||||||
except StopIteration:
|
|
||||||
return h
|
|
||||||
|
|
||||||
|
|
||||||
class ZmqReactor(ZmqBaseReactor):
|
|
||||||
"""
|
|
||||||
A consumer class implementing a
|
|
||||||
consumer for messages. Can also be
|
|
||||||
used as a 1:1 proxy
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, conf):
|
|
||||||
super(ZmqReactor, self).__init__(conf)
|
|
||||||
|
|
||||||
def consume(self, sock):
|
|
||||||
#TODO(ewindisch): use zero-copy (i.e. references, not copying)
|
|
||||||
data = sock.recv()
|
|
||||||
LOG.debug(_("CONSUMER RECEIVED DATA: %s"), data)
|
|
||||||
if sock in self.mapping:
|
|
||||||
LOG.debug(_("ROUTER RELAY-OUT %(data)s") % {
|
|
||||||
'data': data})
|
|
||||||
self.mapping[sock].send(data)
|
|
||||||
return
|
|
||||||
|
|
||||||
proxy = self.proxies[sock]
|
|
||||||
|
|
||||||
if data[2] == 'cast': # Legacy protocol
|
|
||||||
packenv = data[3]
|
|
||||||
|
|
||||||
ctx, msg = _deserialize(packenv)
|
|
||||||
request = rpc_common.deserialize_msg(msg)
|
|
||||||
ctx = RpcContext.unmarshal(ctx)
|
|
||||||
elif data[2] == 'impl_zmq_v2':
|
|
||||||
packenv = data[4:]
|
|
||||||
|
|
||||||
msg = unflatten_envelope(packenv)
|
|
||||||
request = rpc_common.deserialize_msg(msg)
|
|
||||||
|
|
||||||
# Unmarshal only after verifying the message.
|
|
||||||
ctx = RpcContext.unmarshal(data[3])
|
|
||||||
else:
|
|
||||||
LOG.error(_("ZMQ Envelope version unsupported or unknown."))
|
|
||||||
return
|
|
||||||
|
|
||||||
self.pool.spawn_n(self.process, proxy, ctx, request)
|
|
||||||
|
|
||||||
|
|
||||||
class Connection(rpc_common.Connection):
|
|
||||||
"""Manages connections and threads."""
|
|
||||||
|
|
||||||
def __init__(self, conf):
|
|
||||||
self.topics = []
|
|
||||||
self.reactor = ZmqReactor(conf)
|
|
||||||
|
|
||||||
def create_consumer(self, topic, proxy, fanout=False):
|
|
||||||
# Register with matchmaker.
|
|
||||||
_get_matchmaker().register(topic, CONF.rpc_zmq_host)
|
|
||||||
|
|
||||||
# Subscription scenarios
|
|
||||||
if fanout:
|
|
||||||
sock_type = zmq.SUB
|
|
||||||
subscribe = ('', fanout)[type(fanout) == str]
|
|
||||||
topic = 'fanout~' + topic.split('.', 1)[0]
|
|
||||||
else:
|
|
||||||
sock_type = zmq.PULL
|
|
||||||
subscribe = None
|
|
||||||
topic = '.'.join((topic.split('.', 1)[0], CONF.rpc_zmq_host))
|
|
||||||
|
|
||||||
if topic in self.topics:
|
|
||||||
LOG.info(_("Skipping topic registration. Already registered."))
|
|
||||||
return
|
|
||||||
|
|
||||||
# Receive messages from (local) proxy
|
|
||||||
inaddr = "ipc://%s/zmq_topic_%s" % \
|
|
||||||
(CONF.rpc_zmq_ipc_dir, topic)
|
|
||||||
|
|
||||||
LOG.debug(_("Consumer is a zmq.%s"),
|
|
||||||
['PULL', 'SUB'][sock_type == zmq.SUB])
|
|
||||||
|
|
||||||
self.reactor.register(proxy, inaddr, sock_type,
|
|
||||||
subscribe=subscribe, in_bind=False)
|
|
||||||
self.topics.append(topic)
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
_get_matchmaker().stop_heartbeat()
|
|
||||||
for topic in self.topics:
|
|
||||||
_get_matchmaker().unregister(topic, CONF.rpc_zmq_host)
|
|
||||||
|
|
||||||
self.reactor.close()
|
|
||||||
self.topics = []
|
|
||||||
|
|
||||||
def wait(self):
|
|
||||||
self.reactor.wait()
|
|
||||||
|
|
||||||
def consume_in_thread(self):
|
|
||||||
_get_matchmaker().start_heartbeat()
|
|
||||||
self.reactor.consume_in_thread()
|
|
||||||
|
|
||||||
|
|
||||||
def _cast(addr, context, topic, msg, timeout=None, envelope=False,
|
|
||||||
_msg_id=None):
|
|
||||||
timeout_cast = timeout or CONF.rpc_cast_timeout
|
|
||||||
payload = [RpcContext.marshal(context), msg]
|
|
||||||
|
|
||||||
with Timeout(timeout_cast, exception=rpc_common.Timeout):
|
|
||||||
try:
|
|
||||||
conn = ZmqClient(addr)
|
|
||||||
|
|
||||||
# assumes cast can't return an exception
|
|
||||||
conn.cast(_msg_id, topic, payload, envelope)
|
|
||||||
except zmq.ZMQError:
|
|
||||||
raise RPCException("Cast failed. ZMQ Socket Exception")
|
|
||||||
finally:
|
|
||||||
if 'conn' in vars():
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
|
|
||||||
def _call(addr, context, topic, msg, timeout=None,
|
|
||||||
envelope=False):
|
|
||||||
# timeout_response is how long we wait for a response
|
|
||||||
timeout = timeout or CONF.rpc_response_timeout
|
|
||||||
|
|
||||||
# The msg_id is used to track replies.
|
|
||||||
msg_id = uuid.uuid4().hex
|
|
||||||
|
|
||||||
# Replies always come into the reply service.
|
|
||||||
reply_topic = "zmq_replies.%s" % CONF.rpc_zmq_host
|
|
||||||
|
|
||||||
LOG.debug(_("Creating payload"))
|
|
||||||
# Curry the original request into a reply method.
|
|
||||||
mcontext = RpcContext.marshal(context)
|
|
||||||
payload = {
|
|
||||||
'method': '-reply',
|
|
||||||
'args': {
|
|
||||||
'msg_id': msg_id,
|
|
||||||
'context': mcontext,
|
|
||||||
'topic': reply_topic,
|
|
||||||
'msg': [mcontext, msg]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG.debug(_("Creating queue socket for reply waiter"))
|
|
||||||
|
|
||||||
# Messages arriving async.
|
|
||||||
# TODO(ewindisch): have reply consumer with dynamic subscription mgmt
|
|
||||||
with Timeout(timeout, exception=rpc_common.Timeout):
|
|
||||||
try:
|
|
||||||
msg_waiter = ZmqSocket(
|
|
||||||
"ipc://%s/zmq_topic_zmq_replies.%s" %
|
|
||||||
(CONF.rpc_zmq_ipc_dir,
|
|
||||||
CONF.rpc_zmq_host),
|
|
||||||
zmq.SUB, subscribe=msg_id, bind=False
|
|
||||||
)
|
|
||||||
|
|
||||||
LOG.debug(_("Sending cast"))
|
|
||||||
_cast(addr, context, topic, payload, envelope)
|
|
||||||
|
|
||||||
LOG.debug(_("Cast sent; Waiting reply"))
|
|
||||||
# Blocks until receives reply
|
|
||||||
msg = msg_waiter.recv()
|
|
||||||
LOG.debug(_("Received message: %s"), msg)
|
|
||||||
LOG.debug(_("Unpacking response"))
|
|
||||||
|
|
||||||
if msg[2] == 'cast': # Legacy version
|
|
||||||
raw_msg = _deserialize(msg[-1])[-1]
|
|
||||||
elif msg[2] == 'impl_zmq_v2':
|
|
||||||
rpc_envelope = unflatten_envelope(msg[4:])
|
|
||||||
raw_msg = rpc_common.deserialize_msg(rpc_envelope)
|
|
||||||
else:
|
|
||||||
raise rpc_common.UnsupportedRpcEnvelopeVersion(
|
|
||||||
_("Unsupported or unknown ZMQ envelope returned."))
|
|
||||||
|
|
||||||
responses = raw_msg['args']['response']
|
|
||||||
# ZMQError trumps the Timeout error.
|
|
||||||
except zmq.ZMQError:
|
|
||||||
raise RPCException("ZMQ Socket Error")
|
|
||||||
except (IndexError, KeyError):
|
|
||||||
raise RPCException(_("RPC Message Invalid."))
|
|
||||||
finally:
|
|
||||||
if 'msg_waiter' in vars():
|
|
||||||
msg_waiter.close()
|
|
||||||
|
|
||||||
# It seems we don't need to do all of the following,
|
|
||||||
# but perhaps it would be useful for multicall?
|
|
||||||
# One effect of this is that we're checking all
|
|
||||||
# responses for Exceptions.
|
|
||||||
for resp in responses:
|
|
||||||
if isinstance(resp, types.DictType) and 'exc' in resp:
|
|
||||||
raise rpc_common.deserialize_remote_exception(CONF, resp['exc'])
|
|
||||||
|
|
||||||
return responses[-1]
|
|
||||||
|
|
||||||
|
|
||||||
def _multi_send(method, context, topic, msg, timeout=None,
|
|
||||||
envelope=False, _msg_id=None):
|
|
||||||
"""
|
|
||||||
Wraps the sending of messages,
|
|
||||||
dispatches to the matchmaker and sends
|
|
||||||
message to all relevant hosts.
|
|
||||||
"""
|
|
||||||
conf = CONF
|
|
||||||
LOG.debug(_("%(msg)s") % {'msg': ' '.join(map(pformat, (topic, msg)))})
|
|
||||||
|
|
||||||
queues = _get_matchmaker().queues(topic)
|
|
||||||
LOG.debug(_("Sending message(s) to: %s"), queues)
|
|
||||||
|
|
||||||
# Don't stack if we have no matchmaker results
|
|
||||||
if len(queues) == 0:
|
|
||||||
LOG.warn(_("No matchmaker results. Not casting."))
|
|
||||||
# While not strictly a timeout, callers know how to handle
|
|
||||||
# this exception and a timeout isn't too big a lie.
|
|
||||||
raise rpc_common.Timeout(_("No match from matchmaker."))
|
|
||||||
|
|
||||||
# This supports brokerless fanout (addresses > 1)
|
|
||||||
for queue in queues:
|
|
||||||
(_topic, ip_addr) = queue
|
|
||||||
_addr = "tcp://%s:%s" % (ip_addr, conf.rpc_zmq_port)
|
|
||||||
|
|
||||||
if method.__name__ == '_cast':
|
|
||||||
eventlet.spawn_n(method, _addr, context,
|
|
||||||
_topic, msg, timeout, envelope,
|
|
||||||
_msg_id)
|
|
||||||
return
|
|
||||||
return method(_addr, context, _topic, msg, timeout,
|
|
||||||
envelope)
|
|
||||||
|
|
||||||
|
|
||||||
def create_connection(conf, new=True):
|
|
||||||
return Connection(conf)
|
|
||||||
|
|
||||||
|
|
||||||
def multicall(conf, *args, **kwargs):
|
|
||||||
"""Multiple calls."""
|
|
||||||
return _multi_send(_call, *args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def call(conf, *args, **kwargs):
|
|
||||||
"""Send a message, expect a response."""
|
|
||||||
data = _multi_send(_call, *args, **kwargs)
|
|
||||||
return data[-1]
|
|
||||||
|
|
||||||
|
|
||||||
def cast(conf, *args, **kwargs):
|
|
||||||
"""Send a message expecting no reply."""
|
|
||||||
_multi_send(_cast, *args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def fanout_cast(conf, context, topic, msg, **kwargs):
|
|
||||||
"""Send a message to all listening and expect no reply."""
|
|
||||||
# NOTE(ewindisch): fanout~ is used because it avoid splitting on .
|
|
||||||
# and acts as a non-subtle hint to the matchmaker and ZmqProxy.
|
|
||||||
_multi_send(_cast, context, 'fanout~' + str(topic), msg, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def notify(conf, context, topic, msg, envelope):
|
|
||||||
"""
|
|
||||||
Send notification event.
|
|
||||||
Notifications are sent to topic-priority.
|
|
||||||
This differs from the AMQP drivers which send to topic.priority.
|
|
||||||
"""
|
|
||||||
# NOTE(ewindisch): dot-priority in rpc notifier does not
|
|
||||||
# work with our assumptions.
|
|
||||||
topic = topic.replace('.', '-')
|
|
||||||
cast(conf, context, topic, msg, envelope=envelope)
|
|
||||||
|
|
||||||
|
|
||||||
def cleanup():
|
|
||||||
"""Clean up resources in use by implementation."""
|
|
||||||
global ZMQ_CTX
|
|
||||||
if ZMQ_CTX:
|
|
||||||
ZMQ_CTX.term()
|
|
||||||
ZMQ_CTX = None
|
|
||||||
|
|
||||||
global matchmaker
|
|
||||||
matchmaker = None
|
|
||||||
|
|
||||||
|
|
||||||
def _get_ctxt():
|
|
||||||
if not zmq:
|
|
||||||
raise ImportError("Failed to import eventlet.green.zmq")
|
|
||||||
|
|
||||||
global ZMQ_CTX
|
|
||||||
if not ZMQ_CTX:
|
|
||||||
ZMQ_CTX = zmq.Context(CONF.rpc_zmq_contexts)
|
|
||||||
return ZMQ_CTX
|
|
||||||
|
|
||||||
|
|
||||||
def _get_matchmaker(*args, **kwargs):
|
|
||||||
global matchmaker
|
|
||||||
if not matchmaker:
|
|
||||||
matchmaker = importutils.import_object(
|
|
||||||
CONF.rpc_zmq_matchmaker, *args, **kwargs)
|
|
||||||
return matchmaker
|
|
||||||
@@ -1,425 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2011 Cloudscaling Group, Inc
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
"""
|
|
||||||
The MatchMaker classes should except a Topic or Fanout exchange key and
|
|
||||||
return keys for direct exchanges, per (approximate) AMQP parlance.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import contextlib
|
|
||||||
import itertools
|
|
||||||
import json
|
|
||||||
|
|
||||||
import eventlet
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
|
|
||||||
|
|
||||||
matchmaker_opts = [
|
|
||||||
# Matchmaker ring file
|
|
||||||
cfg.StrOpt('matchmaker_ringfile',
|
|
||||||
default='/etc/nova/matchmaker_ring.json',
|
|
||||||
help='Matchmaker ring file (JSON)'),
|
|
||||||
cfg.IntOpt('matchmaker_heartbeat_freq',
|
|
||||||
default=300,
|
|
||||||
help='Heartbeat frequency'),
|
|
||||||
cfg.IntOpt('matchmaker_heartbeat_ttl',
|
|
||||||
default=600,
|
|
||||||
help='Heartbeat time-to-live.'),
|
|
||||||
]
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF.register_opts(matchmaker_opts)
|
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
contextmanager = contextlib.contextmanager
|
|
||||||
|
|
||||||
|
|
||||||
class MatchMakerException(Exception):
|
|
||||||
"""Signified a match could not be found."""
|
|
||||||
message = _("Match not found by MatchMaker.")
|
|
||||||
|
|
||||||
|
|
||||||
class Exchange(object):
|
|
||||||
"""
|
|
||||||
Implements lookups.
|
|
||||||
Subclass this to support hashtables, dns, etc.
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def run(self, key):
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
|
|
||||||
class Binding(object):
|
|
||||||
"""
|
|
||||||
A binding on which to perform a lookup.
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def test(self, key):
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
|
|
||||||
class MatchMakerBase(object):
|
|
||||||
"""
|
|
||||||
Match Maker Base Class.
|
|
||||||
Build off HeartbeatMatchMakerBase if building a
|
|
||||||
heartbeat-capable MatchMaker.
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
|
||||||
# Array of tuples. Index [2] toggles negation, [3] is last-if-true
|
|
||||||
self.bindings = []
|
|
||||||
|
|
||||||
self.no_heartbeat_msg = _('Matchmaker does not implement '
|
|
||||||
'registration or heartbeat.')
|
|
||||||
|
|
||||||
def register(self, key, host):
|
|
||||||
"""
|
|
||||||
Register a host on a backend.
|
|
||||||
Heartbeats, if applicable, may keepalive registration.
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
def ack_alive(self, key, host):
|
|
||||||
"""
|
|
||||||
Acknowledge that a key.host is alive.
|
|
||||||
Used internally for updating heartbeats,
|
|
||||||
but may also be used publically to acknowledge
|
|
||||||
a system is alive (i.e. rpc message successfully
|
|
||||||
sent to host)
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
def is_alive(self, topic, host):
|
|
||||||
"""
|
|
||||||
Checks if a host is alive.
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
def expire(self, topic, host):
|
|
||||||
"""
|
|
||||||
Explicitly expire a host's registration.
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
def send_heartbeats(self):
|
|
||||||
"""
|
|
||||||
Send all heartbeats.
|
|
||||||
Use start_heartbeat to spawn a heartbeat greenthread,
|
|
||||||
which loops this method.
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
def unregister(self, key, host):
|
|
||||||
"""
|
|
||||||
Unregister a topic.
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
def start_heartbeat(self):
|
|
||||||
"""
|
|
||||||
Spawn heartbeat greenthread.
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
def stop_heartbeat(self):
|
|
||||||
"""
|
|
||||||
Destroys the heartbeat greenthread.
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
def add_binding(self, binding, rule, last=True):
|
|
||||||
self.bindings.append((binding, rule, False, last))
|
|
||||||
|
|
||||||
#NOTE(ewindisch): kept the following method in case we implement the
|
|
||||||
# underlying support.
|
|
||||||
#def add_negate_binding(self, binding, rule, last=True):
|
|
||||||
# self.bindings.append((binding, rule, True, last))
|
|
||||||
|
|
||||||
def queues(self, key):
|
|
||||||
workers = []
|
|
||||||
|
|
||||||
# bit is for negate bindings - if we choose to implement it.
|
|
||||||
# last stops processing rules if this matches.
|
|
||||||
for (binding, exchange, bit, last) in self.bindings:
|
|
||||||
if binding.test(key):
|
|
||||||
workers.extend(exchange.run(key))
|
|
||||||
|
|
||||||
# Support last.
|
|
||||||
if last:
|
|
||||||
return workers
|
|
||||||
return workers
|
|
||||||
|
|
||||||
|
|
||||||
class HeartbeatMatchMakerBase(MatchMakerBase):
|
|
||||||
"""
|
|
||||||
Base for a heart-beat capable MatchMaker.
|
|
||||||
Provides common methods for registering,
|
|
||||||
unregistering, and maintaining heartbeats.
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
|
||||||
self.hosts = set()
|
|
||||||
self._heart = None
|
|
||||||
self.host_topic = {}
|
|
||||||
|
|
||||||
super(HeartbeatMatchMakerBase, self).__init__()
|
|
||||||
|
|
||||||
def send_heartbeats(self):
|
|
||||||
"""
|
|
||||||
Send all heartbeats.
|
|
||||||
Use start_heartbeat to spawn a heartbeat greenthread,
|
|
||||||
which loops this method.
|
|
||||||
"""
|
|
||||||
for key, host in self.host_topic:
|
|
||||||
self.ack_alive(key, host)
|
|
||||||
|
|
||||||
def ack_alive(self, key, host):
|
|
||||||
"""
|
|
||||||
Acknowledge that a host.topic is alive.
|
|
||||||
Used internally for updating heartbeats,
|
|
||||||
but may also be used publically to acknowledge
|
|
||||||
a system is alive (i.e. rpc message successfully
|
|
||||||
sent to host)
|
|
||||||
"""
|
|
||||||
raise NotImplementedError("Must implement ack_alive")
|
|
||||||
|
|
||||||
def backend_register(self, key, host):
|
|
||||||
"""
|
|
||||||
Implements registration logic.
|
|
||||||
Called by register(self,key,host)
|
|
||||||
"""
|
|
||||||
raise NotImplementedError("Must implement backend_register")
|
|
||||||
|
|
||||||
def backend_unregister(self, key, key_host):
|
|
||||||
"""
|
|
||||||
Implements de-registration logic.
|
|
||||||
Called by unregister(self,key,host)
|
|
||||||
"""
|
|
||||||
raise NotImplementedError("Must implement backend_unregister")
|
|
||||||
|
|
||||||
def register(self, key, host):
|
|
||||||
"""
|
|
||||||
Register a host on a backend.
|
|
||||||
Heartbeats, if applicable, may keepalive registration.
|
|
||||||
"""
|
|
||||||
self.hosts.add(host)
|
|
||||||
self.host_topic[(key, host)] = host
|
|
||||||
key_host = '.'.join((key, host))
|
|
||||||
|
|
||||||
self.backend_register(key, key_host)
|
|
||||||
|
|
||||||
self.ack_alive(key, host)
|
|
||||||
|
|
||||||
def unregister(self, key, host):
|
|
||||||
"""
|
|
||||||
Unregister a topic.
|
|
||||||
"""
|
|
||||||
if (key, host) in self.host_topic:
|
|
||||||
del self.host_topic[(key, host)]
|
|
||||||
|
|
||||||
self.hosts.discard(host)
|
|
||||||
self.backend_unregister(key, '.'.join((key, host)))
|
|
||||||
|
|
||||||
LOG.info(_("Matchmaker unregistered: %s, %s" % (key, host)))
|
|
||||||
|
|
||||||
def start_heartbeat(self):
|
|
||||||
"""
|
|
||||||
Implementation of MatchMakerBase.start_heartbeat
|
|
||||||
Launches greenthread looping send_heartbeats(),
|
|
||||||
yielding for CONF.matchmaker_heartbeat_freq seconds
|
|
||||||
between iterations.
|
|
||||||
"""
|
|
||||||
if len(self.hosts) == 0:
|
|
||||||
raise MatchMakerException(
|
|
||||||
_("Register before starting heartbeat."))
|
|
||||||
|
|
||||||
def do_heartbeat():
|
|
||||||
while True:
|
|
||||||
self.send_heartbeats()
|
|
||||||
eventlet.sleep(CONF.matchmaker_heartbeat_freq)
|
|
||||||
|
|
||||||
self._heart = eventlet.spawn(do_heartbeat)
|
|
||||||
|
|
||||||
def stop_heartbeat(self):
|
|
||||||
"""
|
|
||||||
Destroys the heartbeat greenthread.
|
|
||||||
"""
|
|
||||||
if self._heart:
|
|
||||||
self._heart.kill()
|
|
||||||
|
|
||||||
|
|
||||||
class DirectBinding(Binding):
|
|
||||||
"""
|
|
||||||
Specifies a host in the key via a '.' character
|
|
||||||
Although dots are used in the key, the behavior here is
|
|
||||||
that it maps directly to a host, thus direct.
|
|
||||||
"""
|
|
||||||
def test(self, key):
|
|
||||||
if '.' in key:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
class TopicBinding(Binding):
|
|
||||||
"""
|
|
||||||
Where a 'bare' key without dots.
|
|
||||||
AMQP generally considers topic exchanges to be those *with* dots,
|
|
||||||
but we deviate here in terminology as the behavior here matches
|
|
||||||
that of a topic exchange (whereas where there are dots, behavior
|
|
||||||
matches that of a direct exchange.
|
|
||||||
"""
|
|
||||||
def test(self, key):
|
|
||||||
if '.' not in key:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
class FanoutBinding(Binding):
|
|
||||||
"""Match on fanout keys, where key starts with 'fanout.' string."""
|
|
||||||
def test(self, key):
|
|
||||||
if key.startswith('fanout~'):
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
class StubExchange(Exchange):
|
|
||||||
"""Exchange that does nothing."""
|
|
||||||
def run(self, key):
|
|
||||||
return [(key, None)]
|
|
||||||
|
|
||||||
|
|
||||||
class RingExchange(Exchange):
|
|
||||||
"""
|
|
||||||
Match Maker where hosts are loaded from a static file containing
|
|
||||||
a hashmap (JSON formatted).
|
|
||||||
|
|
||||||
__init__ takes optional ring dictionary argument, otherwise
|
|
||||||
loads the ringfile from CONF.mathcmaker_ringfile.
|
|
||||||
"""
|
|
||||||
def __init__(self, ring=None):
|
|
||||||
super(RingExchange, self).__init__()
|
|
||||||
|
|
||||||
if ring:
|
|
||||||
self.ring = ring
|
|
||||||
else:
|
|
||||||
fh = open(CONF.matchmaker_ringfile, 'r')
|
|
||||||
self.ring = json.load(fh)
|
|
||||||
fh.close()
|
|
||||||
|
|
||||||
self.ring0 = {}
|
|
||||||
for k in self.ring.keys():
|
|
||||||
self.ring0[k] = itertools.cycle(self.ring[k])
|
|
||||||
|
|
||||||
def _ring_has(self, key):
|
|
||||||
if key in self.ring0:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
class RoundRobinRingExchange(RingExchange):
|
|
||||||
"""A Topic Exchange based on a hashmap."""
|
|
||||||
def __init__(self, ring=None):
|
|
||||||
super(RoundRobinRingExchange, self).__init__(ring)
|
|
||||||
|
|
||||||
def run(self, key):
|
|
||||||
if not self._ring_has(key):
|
|
||||||
LOG.warn(
|
|
||||||
_("No key defining hosts for topic '%s', "
|
|
||||||
"see ringfile") % (key, )
|
|
||||||
)
|
|
||||||
return []
|
|
||||||
host = next(self.ring0[key])
|
|
||||||
return [(key + '.' + host, host)]
|
|
||||||
|
|
||||||
|
|
||||||
class FanoutRingExchange(RingExchange):
|
|
||||||
"""Fanout Exchange based on a hashmap."""
|
|
||||||
def __init__(self, ring=None):
|
|
||||||
super(FanoutRingExchange, self).__init__(ring)
|
|
||||||
|
|
||||||
def run(self, key):
|
|
||||||
# Assume starts with "fanout~", strip it for lookup.
|
|
||||||
nkey = key.split('fanout~')[1:][0]
|
|
||||||
if not self._ring_has(nkey):
|
|
||||||
LOG.warn(
|
|
||||||
_("No key defining hosts for topic '%s', "
|
|
||||||
"see ringfile") % (nkey, )
|
|
||||||
)
|
|
||||||
return []
|
|
||||||
return map(lambda x: (key + '.' + x, x), self.ring[nkey])
|
|
||||||
|
|
||||||
|
|
||||||
class LocalhostExchange(Exchange):
|
|
||||||
"""Exchange where all direct topics are local."""
|
|
||||||
def __init__(self, host='localhost'):
|
|
||||||
self.host = host
|
|
||||||
super(Exchange, self).__init__()
|
|
||||||
|
|
||||||
def run(self, key):
|
|
||||||
return [('.'.join((key.split('.')[0], self.host)), self.host)]
|
|
||||||
|
|
||||||
|
|
||||||
class DirectExchange(Exchange):
|
|
||||||
"""
|
|
||||||
Exchange where all topic keys are split, sending to second half.
|
|
||||||
i.e. "compute.host" sends a message to "compute.host" running on "host"
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
|
||||||
super(Exchange, self).__init__()
|
|
||||||
|
|
||||||
def run(self, key):
|
|
||||||
e = key.split('.', 1)[1]
|
|
||||||
return [(key, e)]
|
|
||||||
|
|
||||||
|
|
||||||
class MatchMakerRing(MatchMakerBase):
|
|
||||||
"""
|
|
||||||
Match Maker where hosts are loaded from a static hashmap.
|
|
||||||
"""
|
|
||||||
def __init__(self, ring=None):
|
|
||||||
super(MatchMakerRing, self).__init__()
|
|
||||||
self.add_binding(FanoutBinding(), FanoutRingExchange(ring))
|
|
||||||
self.add_binding(DirectBinding(), DirectExchange())
|
|
||||||
self.add_binding(TopicBinding(), RoundRobinRingExchange(ring))
|
|
||||||
|
|
||||||
|
|
||||||
class MatchMakerLocalhost(MatchMakerBase):
|
|
||||||
"""
|
|
||||||
Match Maker where all bare topics resolve to localhost.
|
|
||||||
Useful for testing.
|
|
||||||
"""
|
|
||||||
def __init__(self, host='localhost'):
|
|
||||||
super(MatchMakerLocalhost, self).__init__()
|
|
||||||
self.add_binding(FanoutBinding(), LocalhostExchange(host))
|
|
||||||
self.add_binding(DirectBinding(), DirectExchange())
|
|
||||||
self.add_binding(TopicBinding(), LocalhostExchange(host))
|
|
||||||
|
|
||||||
|
|
||||||
class MatchMakerStub(MatchMakerBase):
|
|
||||||
"""
|
|
||||||
Match Maker where topics are untouched.
|
|
||||||
Useful for testing, or for AMQP/brokered queues.
|
|
||||||
Will not work where knowledge of hosts is known (i.e. zeromq)
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
|
||||||
super(MatchMakerLocalhost, self).__init__()
|
|
||||||
|
|
||||||
self.add_binding(FanoutBinding(), StubExchange())
|
|
||||||
self.add_binding(DirectBinding(), StubExchange())
|
|
||||||
self.add_binding(TopicBinding(), StubExchange())
|
|
||||||
@@ -1,149 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2013 Cloudscaling Group, Inc
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
"""
|
|
||||||
The MatchMaker classes should accept a Topic or Fanout exchange key and
|
|
||||||
return keys for direct exchanges, per (approximate) AMQP parlance.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from galloper.openstack.common import importutils
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
from galloper.openstack.common.rpc import matchmaker as mm_common
|
|
||||||
|
|
||||||
redis = importutils.try_import('redis')
|
|
||||||
|
|
||||||
|
|
||||||
matchmaker_redis_opts = [
|
|
||||||
cfg.StrOpt('host',
|
|
||||||
default='127.0.0.1',
|
|
||||||
help='Host to locate redis'),
|
|
||||||
cfg.IntOpt('port',
|
|
||||||
default=6379,
|
|
||||||
help='Use this port to connect to redis host.'),
|
|
||||||
cfg.StrOpt('password',
|
|
||||||
default=None,
|
|
||||||
help='Password for Redis server. (optional)'),
|
|
||||||
]
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
opt_group = cfg.OptGroup(name='matchmaker_redis',
|
|
||||||
title='Options for Redis-based MatchMaker')
|
|
||||||
CONF.register_group(opt_group)
|
|
||||||
CONF.register_opts(matchmaker_redis_opts, opt_group)
|
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class RedisExchange(mm_common.Exchange):
|
|
||||||
def __init__(self, matchmaker):
|
|
||||||
self.matchmaker = matchmaker
|
|
||||||
self.redis = matchmaker.redis
|
|
||||||
super(RedisExchange, self).__init__()
|
|
||||||
|
|
||||||
|
|
||||||
class RedisTopicExchange(RedisExchange):
|
|
||||||
"""
|
|
||||||
Exchange where all topic keys are split, sending to second half.
|
|
||||||
i.e. "compute.host" sends a message to "compute" running on "host"
|
|
||||||
"""
|
|
||||||
def run(self, topic):
|
|
||||||
while True:
|
|
||||||
member_name = self.redis.srandmember(topic)
|
|
||||||
|
|
||||||
if not member_name:
|
|
||||||
# If this happens, there are no
|
|
||||||
# longer any members.
|
|
||||||
break
|
|
||||||
|
|
||||||
if not self.matchmaker.is_alive(topic, member_name):
|
|
||||||
continue
|
|
||||||
|
|
||||||
host = member_name.split('.', 1)[1]
|
|
||||||
return [(member_name, host)]
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
class RedisFanoutExchange(RedisExchange):
|
|
||||||
"""
|
|
||||||
Return a list of all hosts.
|
|
||||||
"""
|
|
||||||
def run(self, topic):
|
|
||||||
topic = topic.split('~', 1)[1]
|
|
||||||
hosts = self.redis.smembers(topic)
|
|
||||||
good_hosts = filter(
|
|
||||||
lambda host: self.matchmaker.is_alive(topic, host), hosts)
|
|
||||||
|
|
||||||
return [(x, x.split('.', 1)[1]) for x in good_hosts]
|
|
||||||
|
|
||||||
|
|
||||||
class MatchMakerRedis(mm_common.HeartbeatMatchMakerBase):
|
|
||||||
"""
|
|
||||||
MatchMaker registering and looking-up hosts with a Redis server.
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
|
||||||
super(MatchMakerRedis, self).__init__()
|
|
||||||
|
|
||||||
if not redis:
|
|
||||||
raise ImportError("Failed to import module redis.")
|
|
||||||
|
|
||||||
self.redis = redis.StrictRedis(
|
|
||||||
host=CONF.matchmaker_redis.host,
|
|
||||||
port=CONF.matchmaker_redis.port,
|
|
||||||
password=CONF.matchmaker_redis.password)
|
|
||||||
|
|
||||||
self.add_binding(mm_common.FanoutBinding(), RedisFanoutExchange(self))
|
|
||||||
self.add_binding(mm_common.DirectBinding(), mm_common.DirectExchange())
|
|
||||||
self.add_binding(mm_common.TopicBinding(), RedisTopicExchange(self))
|
|
||||||
|
|
||||||
def ack_alive(self, key, host):
|
|
||||||
topic = "%s.%s" % (key, host)
|
|
||||||
if not self.redis.expire(topic, CONF.matchmaker_heartbeat_ttl):
|
|
||||||
# If we could not update the expiration, the key
|
|
||||||
# might have been pruned. Re-register, creating a new
|
|
||||||
# key in Redis.
|
|
||||||
self.register(self.topic_host[host], host)
|
|
||||||
|
|
||||||
def is_alive(self, topic, host):
|
|
||||||
if self.redis.ttl(host) == -1:
|
|
||||||
self.expire(topic, host)
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def expire(self, topic, host):
|
|
||||||
with self.redis.pipeline() as pipe:
|
|
||||||
pipe.multi()
|
|
||||||
pipe.delete(host)
|
|
||||||
pipe.srem(topic, host)
|
|
||||||
pipe.execute()
|
|
||||||
|
|
||||||
def backend_register(self, key, key_host):
|
|
||||||
with self.redis.pipeline() as pipe:
|
|
||||||
pipe.multi()
|
|
||||||
pipe.sadd(key, key_host)
|
|
||||||
|
|
||||||
# No value is needed, we just
|
|
||||||
# care if it exists. Sets aren't viable
|
|
||||||
# because only keys can expire.
|
|
||||||
pipe.set(key_host, '')
|
|
||||||
|
|
||||||
pipe.execute()
|
|
||||||
|
|
||||||
def backend_unregister(self, key, key_host):
|
|
||||||
with self.redis.pipeline() as pipe:
|
|
||||||
pipe.multi()
|
|
||||||
pipe.srem(key, key_host)
|
|
||||||
pipe.delete(key_host)
|
|
||||||
pipe.execute()
|
|
||||||
@@ -1,165 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2012 Red Hat, Inc.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""
|
|
||||||
A helper class for proxy objects to remote APIs.
|
|
||||||
|
|
||||||
For more information about rpc API version numbers, see:
|
|
||||||
rpc/dispatcher.py
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
from galloper.openstack.common import rpc
|
|
||||||
|
|
||||||
|
|
||||||
class RpcProxy(object):
|
|
||||||
"""A helper class for rpc clients.
|
|
||||||
|
|
||||||
This class is a wrapper around the RPC client API. It allows you to
|
|
||||||
specify the topic and API version in a single place. This is intended to
|
|
||||||
be used as a base class for a class that implements the client side of an
|
|
||||||
rpc API.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, topic, default_version):
|
|
||||||
"""Initialize an RpcProxy.
|
|
||||||
|
|
||||||
:param topic: The topic to use for all messages.
|
|
||||||
:param default_version: The default API version to request in all
|
|
||||||
outgoing messages. This can be overridden on a per-message
|
|
||||||
basis.
|
|
||||||
"""
|
|
||||||
self.topic = topic
|
|
||||||
self.default_version = default_version
|
|
||||||
super(RpcProxy, self).__init__()
|
|
||||||
|
|
||||||
def _set_version(self, msg, vers):
|
|
||||||
"""Helper method to set the version in a message.
|
|
||||||
|
|
||||||
:param msg: The message having a version added to it.
|
|
||||||
:param vers: The version number to add to the message.
|
|
||||||
"""
|
|
||||||
msg['version'] = vers if vers else self.default_version
|
|
||||||
|
|
||||||
def _get_topic(self, topic):
|
|
||||||
"""Return the topic to use for a message."""
|
|
||||||
return topic if topic else self.topic
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def make_msg(method, **kwargs):
|
|
||||||
return {'method': method, 'args': kwargs}
|
|
||||||
|
|
||||||
def call(self, context, msg, topic=None, version=None, timeout=None):
|
|
||||||
"""rpc.call() a remote method.
|
|
||||||
|
|
||||||
:param context: The request context
|
|
||||||
:param msg: The message to send, including the method and args.
|
|
||||||
:param topic: Override the topic for this message.
|
|
||||||
:param timeout: (Optional) A timeout to use when waiting for the
|
|
||||||
response. If no timeout is specified, a default timeout will be
|
|
||||||
used that is usually sufficient.
|
|
||||||
:param version: (Optional) Override the requested API version in this
|
|
||||||
message.
|
|
||||||
|
|
||||||
:returns: The return value from the remote method.
|
|
||||||
"""
|
|
||||||
self._set_version(msg, version)
|
|
||||||
return rpc.call(context, self._get_topic(topic), msg, timeout)
|
|
||||||
|
|
||||||
def multicall(self, context, msg, topic=None, version=None, timeout=None):
|
|
||||||
"""rpc.multicall() a remote method.
|
|
||||||
|
|
||||||
:param context: The request context
|
|
||||||
:param msg: The message to send, including the method and args.
|
|
||||||
:param topic: Override the topic for this message.
|
|
||||||
:param timeout: (Optional) A timeout to use when waiting for the
|
|
||||||
response. If no timeout is specified, a default timeout will be
|
|
||||||
used that is usually sufficient.
|
|
||||||
:param version: (Optional) Override the requested API version in this
|
|
||||||
message.
|
|
||||||
|
|
||||||
:returns: An iterator that lets you process each of the returned values
|
|
||||||
from the remote method as they arrive.
|
|
||||||
"""
|
|
||||||
self._set_version(msg, version)
|
|
||||||
return rpc.multicall(context, self._get_topic(topic), msg, timeout)
|
|
||||||
|
|
||||||
def cast(self, context, msg, topic=None, version=None):
|
|
||||||
"""rpc.cast() a remote method.
|
|
||||||
|
|
||||||
:param context: The request context
|
|
||||||
:param msg: The message to send, including the method and args.
|
|
||||||
:param topic: Override the topic for this message.
|
|
||||||
:param version: (Optional) Override the requested API version in this
|
|
||||||
message.
|
|
||||||
|
|
||||||
:returns: None. rpc.cast() does not wait on any return value from the
|
|
||||||
remote method.
|
|
||||||
"""
|
|
||||||
self._set_version(msg, version)
|
|
||||||
rpc.cast(context, self._get_topic(topic), msg)
|
|
||||||
|
|
||||||
def fanout_cast(self, context, msg, topic=None, version=None):
|
|
||||||
"""rpc.fanout_cast() a remote method.
|
|
||||||
|
|
||||||
:param context: The request context
|
|
||||||
:param msg: The message to send, including the method and args.
|
|
||||||
:param topic: Override the topic for this message.
|
|
||||||
:param version: (Optional) Override the requested API version in this
|
|
||||||
message.
|
|
||||||
|
|
||||||
:returns: None. rpc.fanout_cast() does not wait on any return value
|
|
||||||
from the remote method.
|
|
||||||
"""
|
|
||||||
self._set_version(msg, version)
|
|
||||||
rpc.fanout_cast(context, self._get_topic(topic), msg)
|
|
||||||
|
|
||||||
def cast_to_server(self, context, server_params, msg, topic=None,
|
|
||||||
version=None):
|
|
||||||
"""rpc.cast_to_server() a remote method.
|
|
||||||
|
|
||||||
:param context: The request context
|
|
||||||
:param server_params: Server parameters. See rpc.cast_to_server() for
|
|
||||||
details.
|
|
||||||
:param msg: The message to send, including the method and args.
|
|
||||||
:param topic: Override the topic for this message.
|
|
||||||
:param version: (Optional) Override the requested API version in this
|
|
||||||
message.
|
|
||||||
|
|
||||||
:returns: None. rpc.cast_to_server() does not wait on any
|
|
||||||
return values.
|
|
||||||
"""
|
|
||||||
self._set_version(msg, version)
|
|
||||||
rpc.cast_to_server(context, server_params, self._get_topic(topic), msg)
|
|
||||||
|
|
||||||
def fanout_cast_to_server(self, context, server_params, msg, topic=None,
|
|
||||||
version=None):
|
|
||||||
"""rpc.fanout_cast_to_server() a remote method.
|
|
||||||
|
|
||||||
:param context: The request context
|
|
||||||
:param server_params: Server parameters. See rpc.cast_to_server() for
|
|
||||||
details.
|
|
||||||
:param msg: The message to send, including the method and args.
|
|
||||||
:param topic: Override the topic for this message.
|
|
||||||
:param version: (Optional) Override the requested API version in this
|
|
||||||
message.
|
|
||||||
|
|
||||||
:returns: None. rpc.fanout_cast_to_server() does not wait on any
|
|
||||||
return values.
|
|
||||||
"""
|
|
||||||
self._set_version(msg, version)
|
|
||||||
rpc.fanout_cast_to_server(context, server_params,
|
|
||||||
self._get_topic(topic), msg)
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2010 United States Government as represented by the
|
|
||||||
# Administrator of the National Aeronautics and Space Administration.
|
|
||||||
# All Rights Reserved.
|
|
||||||
# Copyright 2011 Red Hat, Inc.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
from galloper.openstack.common.gettextutils import _
|
|
||||||
from galloper.openstack.common import log as logging
|
|
||||||
from galloper.openstack.common import rpc
|
|
||||||
from galloper.openstack.common.rpc import dispatcher as rpc_dispatcher
|
|
||||||
from galloper.openstack.common import service
|
|
||||||
|
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class Service(service.Service):
|
|
||||||
"""Service object for binaries running on hosts.
|
|
||||||
|
|
||||||
A service enables rpc by listening to queues based on topic and host."""
|
|
||||||
def __init__(self, host, topic, manager=None):
|
|
||||||
super(Service, self).__init__()
|
|
||||||
self.host = host
|
|
||||||
self.topic = topic
|
|
||||||
if manager is None:
|
|
||||||
self.manager = self
|
|
||||||
else:
|
|
||||||
self.manager = manager
|
|
||||||
|
|
||||||
def start(self):
|
|
||||||
super(Service, self).start()
|
|
||||||
|
|
||||||
self.conn = rpc.create_connection(new=True)
|
|
||||||
LOG.debug(_("Creating Consumer connection for Service %s") %
|
|
||||||
self.topic)
|
|
||||||
|
|
||||||
dispatcher = rpc_dispatcher.RpcDispatcher([self.manager])
|
|
||||||
|
|
||||||
# Share this same connection for these Consumers
|
|
||||||
self.conn.create_consumer(self.topic, dispatcher, fanout=False)
|
|
||||||
|
|
||||||
node_topic = '%s.%s' % (self.topic, self.host)
|
|
||||||
self.conn.create_consumer(node_topic, dispatcher, fanout=False)
|
|
||||||
|
|
||||||
self.conn.create_consumer(self.topic, dispatcher, fanout=True)
|
|
||||||
|
|
||||||
# Hook to allow the manager to do other initializations after
|
|
||||||
# the rpc connection is created.
|
|
||||||
if callable(getattr(self.manager, 'initialize_service_hook', None)):
|
|
||||||
self.manager.initialize_service_hook(self)
|
|
||||||
|
|
||||||
# Consume from all consumers in a thread
|
|
||||||
self.conn.consume_in_thread()
|
|
||||||
|
|
||||||
def stop(self):
|
|
||||||
# Try to shut the connection down, but if we get any sort of
|
|
||||||
# errors, go ahead and ignore them.. as we're shutting down anyway
|
|
||||||
try:
|
|
||||||
self.conn.close()
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
super(Service, self).stop()
|
|
||||||
@@ -1,186 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright 2011 OpenStack Foundation.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""
|
|
||||||
Time related utilities and helper functions.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import calendar
|
|
||||||
import datetime
|
|
||||||
|
|
||||||
import iso8601
|
|
||||||
|
|
||||||
|
|
||||||
# ISO 8601 extended time format with microseconds
|
|
||||||
_ISO8601_TIME_FORMAT_SUBSECOND = '%Y-%m-%dT%H:%M:%S.%f'
|
|
||||||
_ISO8601_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S'
|
|
||||||
PERFECT_TIME_FORMAT = _ISO8601_TIME_FORMAT_SUBSECOND
|
|
||||||
|
|
||||||
|
|
||||||
def isotime(at=None, subsecond=False):
|
|
||||||
"""Stringify time in ISO 8601 format"""
|
|
||||||
if not at:
|
|
||||||
at = utcnow()
|
|
||||||
st = at.strftime(_ISO8601_TIME_FORMAT
|
|
||||||
if not subsecond
|
|
||||||
else _ISO8601_TIME_FORMAT_SUBSECOND)
|
|
||||||
tz = at.tzinfo.tzname(None) if at.tzinfo else 'UTC'
|
|
||||||
st += ('Z' if tz == 'UTC' else tz)
|
|
||||||
return st
|
|
||||||
|
|
||||||
|
|
||||||
def parse_isotime(timestr):
|
|
||||||
"""Parse time from ISO 8601 format"""
|
|
||||||
try:
|
|
||||||
return iso8601.parse_date(timestr)
|
|
||||||
except iso8601.ParseError as e:
|
|
||||||
raise ValueError(e.message)
|
|
||||||
except TypeError as e:
|
|
||||||
raise ValueError(e.message)
|
|
||||||
|
|
||||||
|
|
||||||
def strtime(at=None, fmt=PERFECT_TIME_FORMAT):
|
|
||||||
"""Returns formatted utcnow."""
|
|
||||||
if not at:
|
|
||||||
at = utcnow()
|
|
||||||
return at.strftime(fmt)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_strtime(timestr, fmt=PERFECT_TIME_FORMAT):
|
|
||||||
"""Turn a formatted time back into a datetime."""
|
|
||||||
return datetime.datetime.strptime(timestr, fmt)
|
|
||||||
|
|
||||||
|
|
||||||
def normalize_time(timestamp):
|
|
||||||
"""Normalize time in arbitrary timezone to UTC naive object"""
|
|
||||||
offset = timestamp.utcoffset()
|
|
||||||
if offset is None:
|
|
||||||
return timestamp
|
|
||||||
return timestamp.replace(tzinfo=None) - offset
|
|
||||||
|
|
||||||
|
|
||||||
def is_older_than(before, seconds):
|
|
||||||
"""Return True if before is older than seconds."""
|
|
||||||
if isinstance(before, basestring):
|
|
||||||
before = parse_strtime(before).replace(tzinfo=None)
|
|
||||||
return utcnow() - before > datetime.timedelta(seconds=seconds)
|
|
||||||
|
|
||||||
|
|
||||||
def is_newer_than(after, seconds):
|
|
||||||
"""Return True if after is newer than seconds."""
|
|
||||||
if isinstance(after, basestring):
|
|
||||||
after = parse_strtime(after).replace(tzinfo=None)
|
|
||||||
return after - utcnow() > datetime.timedelta(seconds=seconds)
|
|
||||||
|
|
||||||
|
|
||||||
def utcnow_ts():
|
|
||||||
"""Timestamp version of our utcnow function."""
|
|
||||||
return calendar.timegm(utcnow().timetuple())
|
|
||||||
|
|
||||||
|
|
||||||
def utcnow():
|
|
||||||
"""Overridable version of utils.utcnow."""
|
|
||||||
if utcnow.override_time:
|
|
||||||
try:
|
|
||||||
return utcnow.override_time.pop(0)
|
|
||||||
except AttributeError:
|
|
||||||
return utcnow.override_time
|
|
||||||
return datetime.datetime.utcnow()
|
|
||||||
|
|
||||||
|
|
||||||
def iso8601_from_timestamp(timestamp):
|
|
||||||
"""Returns a iso8601 formated date from timestamp"""
|
|
||||||
return isotime(datetime.datetime.utcfromtimestamp(timestamp))
|
|
||||||
|
|
||||||
|
|
||||||
utcnow.override_time = None
|
|
||||||
|
|
||||||
|
|
||||||
def set_time_override(override_time=datetime.datetime.utcnow()):
|
|
||||||
"""
|
|
||||||
Override utils.utcnow to return a constant time or a list thereof,
|
|
||||||
one at a time.
|
|
||||||
"""
|
|
||||||
utcnow.override_time = override_time
|
|
||||||
|
|
||||||
|
|
||||||
def advance_time_delta(timedelta):
|
|
||||||
"""Advance overridden time using a datetime.timedelta."""
|
|
||||||
assert(not utcnow.override_time is None)
|
|
||||||
try:
|
|
||||||
for dt in utcnow.override_time:
|
|
||||||
dt += timedelta
|
|
||||||
except TypeError:
|
|
||||||
utcnow.override_time += timedelta
|
|
||||||
|
|
||||||
|
|
||||||
def advance_time_seconds(seconds):
|
|
||||||
"""Advance overridden time by seconds."""
|
|
||||||
advance_time_delta(datetime.timedelta(0, seconds))
|
|
||||||
|
|
||||||
|
|
||||||
def clear_time_override():
|
|
||||||
"""Remove the overridden time."""
|
|
||||||
utcnow.override_time = None
|
|
||||||
|
|
||||||
|
|
||||||
def marshall_now(now=None):
|
|
||||||
"""Make an rpc-safe datetime with microseconds.
|
|
||||||
|
|
||||||
Note: tzinfo is stripped, but not required for relative times."""
|
|
||||||
if not now:
|
|
||||||
now = utcnow()
|
|
||||||
return dict(day=now.day, month=now.month, year=now.year, hour=now.hour,
|
|
||||||
minute=now.minute, second=now.second,
|
|
||||||
microsecond=now.microsecond)
|
|
||||||
|
|
||||||
|
|
||||||
def unmarshall_time(tyme):
|
|
||||||
"""Unmarshall a datetime dict."""
|
|
||||||
return datetime.datetime(day=tyme['day'],
|
|
||||||
month=tyme['month'],
|
|
||||||
year=tyme['year'],
|
|
||||||
hour=tyme['hour'],
|
|
||||||
minute=tyme['minute'],
|
|
||||||
second=tyme['second'],
|
|
||||||
microsecond=tyme['microsecond'])
|
|
||||||
|
|
||||||
|
|
||||||
def delta_seconds(before, after):
|
|
||||||
"""
|
|
||||||
Compute the difference in seconds between two date, time, or
|
|
||||||
datetime objects (as a float, to microsecond resolution).
|
|
||||||
"""
|
|
||||||
delta = after - before
|
|
||||||
try:
|
|
||||||
return delta.total_seconds()
|
|
||||||
except AttributeError:
|
|
||||||
return ((delta.days * 24 * 3600) + delta.seconds +
|
|
||||||
float(delta.microseconds) / (10 ** 6))
|
|
||||||
|
|
||||||
|
|
||||||
def is_soon(dt, window):
|
|
||||||
"""
|
|
||||||
Determines if time is going to happen in the next window seconds.
|
|
||||||
|
|
||||||
:params dt: the time
|
|
||||||
:params window: minimum seconds to remain to consider the time not soon
|
|
||||||
|
|
||||||
:return: True if expiration is within the given duration
|
|
||||||
"""
|
|
||||||
soon = (utcnow() + datetime.timedelta(seconds=window))
|
|
||||||
return normalize_time(dt) <= soon
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
||||||
|
|
||||||
# Copyright (c) 2012 Intel Corporation.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""
|
|
||||||
UUID related utilities and helper functions.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import uuid
|
|
||||||
|
|
||||||
|
|
||||||
def generate_uuid():
|
|
||||||
return str(uuid.uuid4())
|
|
||||||
|
|
||||||
|
|
||||||
def is_uuid_like(val):
|
|
||||||
"""Returns validation of a value as a UUID.
|
|
||||||
|
|
||||||
For our purposes, a UUID is a canonical form string:
|
|
||||||
aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa
|
|
||||||
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
return str(uuid.UUID(val)) == val
|
|
||||||
except (TypeError, ValueError, AttributeError):
|
|
||||||
return False
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
[DEFAULT]
|
|
||||||
|
|
||||||
# The list of modules to copy from oslo-incubator.git
|
|
||||||
module=cliutils
|
|
||||||
module=config
|
|
||||||
module=db
|
|
||||||
module=db.sqlalchemy
|
|
||||||
module=importutils
|
|
||||||
module=rpc
|
|
||||||
module=excutils
|
|
||||||
module=uuidutils
|
|
||||||
module=log
|
|
||||||
module=gettextutils
|
|
||||||
module=jsonutils
|
|
||||||
module=timeutils
|
|
||||||
module=local
|
|
||||||
module=notifier
|
|
||||||
|
|
||||||
# The base module to hold the copy of openstack.common
|
|
||||||
base=galloper
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
pecan>=0.4.2
|
|
||||||
WSME==0.5b6.8.gf191f32
|
|
||||||
requests>=2.0.1
|
|
||||||
SQLAlchemy>=0.8.3
|
|
||||||
wsgiref>=0.1.2
|
|
||||||
pbr>=0.5.23
|
|
||||||
PyYAML>=3.10
|
|
||||||
oslo.config>=1.2.0
|
|
||||||
iso8601>=0.1.8
|
|
||||||
eventlet>=0.14.0
|
|
||||||
Reference in New Issue
Block a user