summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTodd Willey <todd@ansolabs.com>2011-01-04 18:16:16 -0500
committerTodd Willey <todd@ansolabs.com>2011-01-04 18:16:16 -0500
commit2491c2484f025cb3f061fcc6a5c6915006feb47b (patch)
tree571e45ef7388aae336cb9e227379f3fdc22ee415
parentf55dbc2f599ed56fb59c7f7a94cd81d3fd82c8dd (diff)
downloadnova-2491c2484f025cb3f061fcc6a5c6915006feb47b.tar.gz
nova-2491c2484f025cb3f061fcc6a5c6915006feb47b.tar.xz
nova-2491c2484f025cb3f061fcc6a5c6915006feb47b.zip
Make paste the default api pattern.
* get rid of the --use_lockout flag since it will be specified in paste config (Example line is commented out in etc/nova-api.conf, factory is in place) * remove old nova-api binary and promote nova-api-paste * change how we store ec2 parameters to bin the the ApiRequest * get rid of Router, since paste.urlmap is equally effective (Requestify now gets passed the name of the controller requests are to.)
-rwxr-xr-xbin/nova-api75
-rwxr-xr-xbin/nova-api-paste109
-rw-r--r--etc/nova-api.conf4
-rw-r--r--nova/api/ec2/__init__.py74
-rw-r--r--nova/api/ec2/apirequest.py7
5 files changed, 79 insertions, 190 deletions
diff --git a/bin/nova-api b/bin/nova-api
index 1c671201e..6ee833a18 100755
--- a/bin/nova-api
+++ b/bin/nova-api
@@ -21,9 +21,12 @@
"""Starter script for Nova API."""
import gettext
+import logging
import os
import sys
+from paste import deploy
+
# If ../nova/__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]),
@@ -34,23 +37,73 @@ if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
gettext.install('nova', unicode=1)
-from nova import api
from nova import flags
-from nova import utils
from nova import wsgi
+LOG = logging.getLogger('nova.api')
+LOG.setLevel(logging.DEBUG)
+LOG.addHandler(logging.StreamHandler())
FLAGS = flags.FLAGS
-flags.DEFINE_integer('osapi_port', 8774, 'OpenStack API port')
-flags.DEFINE_string('osapi_host', '0.0.0.0', 'OpenStack API host')
-flags.DEFINE_integer('ec2api_port', 8773, 'EC2 API port')
-flags.DEFINE_string('ec2api_host', '0.0.0.0', 'EC2 API host')
+API_ENDPOINTS = ['ec2', 'openstack']
-if __name__ == '__main__':
- utils.default_flagfile()
- FLAGS(sys.argv)
+
+def load_configuration(paste_config):
+ """Load the paste configuration from the config file and return it."""
+ config = None
+ # Try each known name to get the global DEFAULTS, which will give ports
+ for name in API_ENDPOINTS:
+ try:
+ config = deploy.appconfig("config:%s" % paste_config, name=name)
+ except LookupError:
+ pass
+ if config:
+ verbose = config.get('verbose', None)
+ if verbose:
+ FLAGS.verbose = int(verbose) == 1
+ if FLAGS.verbose:
+ logging.getLogger().setLevel(logging.DEBUG)
+ return config
+ LOG.debug(_("Paste config at %s has no secion for known apis"),
+ paste_config)
+ print _("Paste config at %s has no secion for any known apis") % \
+ paste_config
+ os.exit(1)
+
+
+def launch_api(paste_config_file, section, server, port, host):
+ """Launch an api server from the specified port and IP."""
+ LOG.debug(_("Launching %s api on %s:%s"), section, host, port)
+ app = deploy.loadapp('config:%s' % paste_config_file, name=section)
+ server.start(app, int(port), host)
+
+
+def run_app(paste_config_file):
+ LOG.debug(_("Using paste.deploy config at: %s"), configfile)
+ config = load_configuration(paste_config_file)
+ LOG.debug(_("Configuration: %r"), config)
server = wsgi.Server()
- server.start(api.API('os'), FLAGS.osapi_port, host=FLAGS.osapi_host)
- server.start(api.API('ec2'), FLAGS.ec2api_port, host=FLAGS.ec2api_host)
+ ip = config.get('host', '0.0.0.0')
+ for api in API_ENDPOINTS:
+ port = config.get("%s_port" % api, None)
+ if not port:
+ continue
+ host = config.get("%s_host" % api, ip)
+ launch_api(configfile, api, server, port, host)
+ LOG.debug(_("All api servers launched, now waiting"))
server.wait()
+
+
+if __name__ == '__main__':
+ FLAGS(sys.argv)
+ configfiles = ['/etc/nova/nova-api.conf']
+ if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
+ configfiles.insert(0,
+ os.path.join(possible_topdir, 'etc', 'nova-api.conf'))
+ for configfile in configfiles:
+ if os.path.exists(configfile):
+ run_app(configfile)
+ break
+ else:
+ LOG.debug(_("Skipping missing configuration: %s"), configfile)
diff --git a/bin/nova-api-paste b/bin/nova-api-paste
deleted file mode 100755
index 6ee833a18..000000000
--- a/bin/nova-api-paste
+++ /dev/null
@@ -1,109 +0,0 @@
-#!/usr/bin/env python
-# pylint: disable-msg=C0103
-# 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.
-
-"""Starter script for Nova API."""
-
-import gettext
-import logging
-import os
-import sys
-
-from paste import deploy
-
-# If ../nova/__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, 'nova', '__init__.py')):
- sys.path.insert(0, possible_topdir)
-
-gettext.install('nova', unicode=1)
-
-from nova import flags
-from nova import wsgi
-
-LOG = logging.getLogger('nova.api')
-LOG.setLevel(logging.DEBUG)
-LOG.addHandler(logging.StreamHandler())
-
-FLAGS = flags.FLAGS
-
-API_ENDPOINTS = ['ec2', 'openstack']
-
-
-def load_configuration(paste_config):
- """Load the paste configuration from the config file and return it."""
- config = None
- # Try each known name to get the global DEFAULTS, which will give ports
- for name in API_ENDPOINTS:
- try:
- config = deploy.appconfig("config:%s" % paste_config, name=name)
- except LookupError:
- pass
- if config:
- verbose = config.get('verbose', None)
- if verbose:
- FLAGS.verbose = int(verbose) == 1
- if FLAGS.verbose:
- logging.getLogger().setLevel(logging.DEBUG)
- return config
- LOG.debug(_("Paste config at %s has no secion for known apis"),
- paste_config)
- print _("Paste config at %s has no secion for any known apis") % \
- paste_config
- os.exit(1)
-
-
-def launch_api(paste_config_file, section, server, port, host):
- """Launch an api server from the specified port and IP."""
- LOG.debug(_("Launching %s api on %s:%s"), section, host, port)
- app = deploy.loadapp('config:%s' % paste_config_file, name=section)
- server.start(app, int(port), host)
-
-
-def run_app(paste_config_file):
- LOG.debug(_("Using paste.deploy config at: %s"), configfile)
- config = load_configuration(paste_config_file)
- LOG.debug(_("Configuration: %r"), config)
- server = wsgi.Server()
- ip = config.get('host', '0.0.0.0')
- for api in API_ENDPOINTS:
- port = config.get("%s_port" % api, None)
- if not port:
- continue
- host = config.get("%s_host" % api, ip)
- launch_api(configfile, api, server, port, host)
- LOG.debug(_("All api servers launched, now waiting"))
- server.wait()
-
-
-if __name__ == '__main__':
- FLAGS(sys.argv)
- configfiles = ['/etc/nova/nova-api.conf']
- if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
- configfiles.insert(0,
- os.path.join(possible_topdir, 'etc', 'nova-api.conf'))
- for configfile in configfiles:
- if os.path.exists(configfile):
- run_app(configfile)
- break
- else:
- LOG.debug(_("Skipping missing configuration: %s"), configfile)
diff --git a/etc/nova-api.conf b/etc/nova-api.conf
index cf49b7254..dcb4e7894 100644
--- a/etc/nova-api.conf
+++ b/etc/nova-api.conf
@@ -20,10 +20,14 @@ use = egg:Paste#urlmap
[pipeline:ec2cloud]
pipeline = authenticate cloudrequest authorizer ec2executor
+#pipeline = ec2lockout authenticate cloudrequest authorizer ec2executor
[pipeline:ec2admin]
pipeline = authenticate adminrequest authorizer ec2executor
+[filter:ec2lockout]
+paste.filter_factory = nova.api.ec2:lockout_factory
+
[filter:authenticate]
paste.filter_factory = nova.api.ec2:authenticate_factory
diff --git a/nova/api/ec2/__init__.py b/nova/api/ec2/__init__.py
index a5810479e..2f8f4f272 100644
--- a/nova/api/ec2/__init__.py
+++ b/nova/api/ec2/__init__.py
@@ -21,7 +21,6 @@ Starting point for routing EC2 requests.
"""
import logging
-import routes
import webob
import webob.dec
import webob.exc
@@ -32,8 +31,6 @@ from nova import flags
from nova import utils
from nova import wsgi
from nova.api.ec2 import apirequest
-from nova.api.ec2 import admin
-from nova.api.ec2 import cloud
from nova.auth import manager
@@ -41,8 +38,6 @@ FLAGS = flags.FLAGS
flags.DEFINE_boolean('use_forwarded_for', False,
'Treat X-Forwarded-For as the canonical remote address. '
'Only enable this if you have a sanitizing proxy.')
-flags.DEFINE_boolean('use_lockout', False,
- 'Whether or not to use lockout middleware.')
flags.DEFINE_integer('lockout_attempts', 5,
'Number of failed auths before lockout.')
flags.DEFINE_integer('lockout_minutes', 15,
@@ -57,15 +52,6 @@ _log = logging.getLogger("api")
_log.setLevel(logging.DEBUG)
-class API(wsgi.Middleware):
- """Routing for all EC2 API requests."""
-
- def __init__(self):
- self.application = Authenticate(Router(Authorizer(Executor())))
- if FLAGS.use_lockout:
- self.application = Lockout(self.application)
-
-
class Lockout(wsgi.Middleware):
"""Lockout for x minutes on y failed auths in a z minute period.
@@ -177,55 +163,12 @@ class Requestify(wsgi.Middleware):
args.pop(non_arg)
except:
raise webob.exc.HTTPBadRequest()
- api_request = apirequest.APIRequest(self.controller, action)
+ api_request = apirequest.APIRequest(self.controller, action, args)
req.environ['ec2.request'] = api_request
req.environ['ec2.action_args'] = args
return self.application
-class Router(wsgi.Middleware):
-
- """Add ec2.'controller', .'action', and .'action_args' to WSGI environ."""
-
- def __init__(self, application):
- super(Router, self).__init__(application)
- self.map = routes.Mapper()
- self.map.connect("/{controller_name}/")
- self.controllers = dict(Cloud=cloud.CloudController(),
- Admin=admin.AdminController())
-
- @webob.dec.wsgify
- def __call__(self, req):
- # Obtain the appropriate controller and action for this request.
- try:
- match = self.map.match(req.path_info)
- controller_name = match['controller_name']
- controller = self.controllers[controller_name]
- except:
- raise webob.exc.HTTPNotFound()
- non_args = ['Action', 'Signature', 'AWSAccessKeyId', 'SignatureMethod',
- 'SignatureVersion', 'Version', 'Timestamp']
- args = dict(req.params)
- try:
- # Raise KeyError if omitted
- action = req.params['Action']
- for non_arg in non_args:
- # Remove, but raise KeyError if omitted
- args.pop(non_arg)
- except:
- raise webob.exc.HTTPBadRequest()
-
- _log.debug(_('action: %s') % action)
- for key, value in args.items():
- _log.debug(_('arg: %s\t\tval: %s') % (key, value))
-
- # Success!
- req.environ['ec2.controller'] = controller
- req.environ['ec2.action'] = action
- req.environ['ec2.action_args'] = args
- return self.application
-
-
class Authorizer(wsgi.Middleware):
"""Authorize an EC2 API request.
@@ -314,13 +257,11 @@ class Executor(wsgi.Application):
@webob.dec.wsgify
def __call__(self, req):
context = req.environ['ec2.context']
- args = req.environ['ec2.action_args']
api_request = req.environ['ec2.request']
result = None
try:
- result = api_request.send(context, **args)
+ result = api_request.invoke(context)
except exception.ApiError as ex:
-
if ex.code:
return self._error(req, ex.code, ex.message)
else:
@@ -373,12 +314,6 @@ def authenticate_factory(global_args, **local_args):
return authenticator
-def router_factory(global_args, **local_args):
- def router(app):
- return Router(app)
- return router
-
-
def authorizer_factory(global_args, **local_args):
def authorizer(app):
return Authorizer(app)
@@ -396,3 +331,8 @@ def requestify_factory(global_args, **local_args):
def requestifier(app):
return Requestify(app, local_args['controller'])
return requestifier
+
+def lockout_factory(global_args, **local_args):
+ def locksmith(app):
+ return Lockout(app)
+ return locksmith
diff --git a/nova/api/ec2/apirequest.py b/nova/api/ec2/apirequest.py
index a90fbeb0c..8a1dd3978 100644
--- a/nova/api/ec2/apirequest.py
+++ b/nova/api/ec2/apirequest.py
@@ -83,11 +83,12 @@ def _try_convert(value):
class APIRequest(object):
- def __init__(self, controller, action):
+ def __init__(self, controller, action, args):
self.controller = controller
self.action = action
+ self.args = args
- def send(self, context, **kwargs):
+ def invoke(self, context):
try:
method = getattr(self.controller,
_camelcase_to_underscore(self.action))
@@ -100,7 +101,7 @@ class APIRequest(object):
raise Exception(_error)
args = {}
- for key, value in kwargs.items():
+ for key, value in self.args.items():
parts = key.split(".")
key = _camelcase_to_underscore(parts[0])
if isinstance(value, str) or isinstance(value, unicode):