Add entrypoint of Kuryr

This patch adds the entrypoint of Kuryr libnetwork remote driver, which
works as a JSON-oriented Flask app.

Change-Id: Idd6a8d5e72fd4c619bfa88c01edf1f0e19391f91
Signed-off-by: Taku Fukushima <f.tac.mac@gmail.com>
This commit is contained in:
Taku Fukushima 2015-07-22 12:54:44 +09:00
parent 415e663145
commit a8dd46e290
3 changed files with 54 additions and 7 deletions

View File

@ -15,5 +15,4 @@
import pbr.version
__version__ = pbr.version.VersionInfo(
'kuryr').version_string()
__version__ = pbr.version.VersionInfo('kuryr').version_string()

View File

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
# 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
@ -12,8 +10,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import pbr.version
import logging
import sys
from kuryr.utils import make_json_app
__version__ = pbr.version.VersionInfo(
'kuryr').version_string()
app = make_json_app(__name__)
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.INFO)

47
kuryr/utils.py Normal file
View File

@ -0,0 +1,47 @@
# 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 flask import Flask, jsonify
from werkzeug.exceptions import default_exceptions
from werkzeug.exceptions import HTTPException
# Return all errors as JSON. From http://flask.pocoo.org/snippets/83/
def make_json_app(import_name, **kwargs):
"""Creates a JSON-oriented Flask app.
All error responses that you don't specifically manage yourself will have
application/json content type, and will contain JSON that follows the
libnetwork remote driver protocol.
{ "Err": "405: Method Not Allowed" }
See:
- https://github.com/docker/libnetwork/blob/3c8e06bc0580a2a1b2440fe0792fbfcd43a9feca/docs/remote.md#errors # noqa
"""
def make_json_error(ex):
response = jsonify({"Err": str(ex)})
response.status_code = (ex.code
if isinstance(ex, HTTPException)
else 500)
content_type = 'application/vnd.docker.plugins.v1+json; charset=utf-8'
response.headers['Content-Type'] = content_type
return response
app = Flask(import_name, **kwargs)
for code in default_exceptions.iterkeys():
app.error_handler_spec[None][code] = make_json_error
return app