Merge "Explicit WSGI application and fixed static in dev"
This commit is contained in:
commit
3d729dc257
@ -232,7 +232,7 @@ def action_run(params):
|
||||
settings.update({attr: param})
|
||||
if params.config_file:
|
||||
settings.update_from_file(params.config_file)
|
||||
from nailgun.wsgi import appstart
|
||||
from nailgun.app import appstart
|
||||
appstart()
|
||||
|
||||
|
||||
|
85
nailgun/nailgun/app.py
Normal file
85
nailgun/nailgun/app.py
Normal file
@ -0,0 +1,85 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2013 Mirantis, 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 sys
|
||||
import web
|
||||
from web.httpserver import WSGIServer
|
||||
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
|
||||
from nailgun.api.handlers import forbid_client_caching
|
||||
from nailgun.api.handlers import load_db_driver
|
||||
from nailgun.db import engine
|
||||
from nailgun.logger import HTTPLoggerMiddleware
|
||||
from nailgun.logger import logger
|
||||
from nailgun.settings import settings
|
||||
from nailgun.static import StaticMiddleware
|
||||
from nailgun.urls import urls
|
||||
|
||||
|
||||
def build_app(db_driver=None):
|
||||
"""Build app and disable debug mode in case of production
|
||||
"""
|
||||
web.config.debug = bool(int(settings.DEVELOPMENT))
|
||||
app = web.application(urls(), locals())
|
||||
app.add_processor(db_driver or load_db_driver)
|
||||
app.add_processor(forbid_client_caching)
|
||||
return app
|
||||
|
||||
|
||||
def build_middleware(app):
|
||||
|
||||
middleware_list = [
|
||||
HTTPLoggerMiddleware
|
||||
]
|
||||
|
||||
if settings.DEVELOPMENT:
|
||||
middleware_list.append(StaticMiddleware)
|
||||
|
||||
logger.debug('Initialize middleware: %s' %
|
||||
(map(lambda x: x.__name__, middleware_list)))
|
||||
|
||||
return app(*middleware_list)
|
||||
|
||||
|
||||
def run_server(func, server_address=('0.0.0.0', 8080)):
|
||||
"""This function same as runsimple from web/httpserver
|
||||
except removed LogMiddleware because we use
|
||||
HTTPLoggerMiddleware instead
|
||||
"""
|
||||
server = WSGIServer(server_address, func)
|
||||
print('http://%s:%d/' % server_address)
|
||||
|
||||
try:
|
||||
server.start()
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
server.stop()
|
||||
|
||||
|
||||
def appstart():
|
||||
logger.info("Fuel version: %s", str(settings.VERSION))
|
||||
if not engine.dialect.has_table(engine.connect(), "nodes"):
|
||||
logger.error(
|
||||
"Database tables not created. Try './manage.py syncdb' first"
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
run_server(build_middleware(build_app().wsgifunc),
|
||||
(settings.LISTEN_ADDRESS, int(settings.LISTEN_PORT)))
|
||||
|
||||
logger.info("Stopping WSGI app...")
|
||||
logger.info("Done")
|
66
nailgun/nailgun/static.py
Normal file
66
nailgun/nailgun/static.py
Normal file
@ -0,0 +1,66 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2014 Mirantis, 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.path
|
||||
import urllib
|
||||
|
||||
from web.httpserver import StaticApp
|
||||
|
||||
from nailgun.settings import settings
|
||||
|
||||
|
||||
class NailgunStaticApp(StaticApp):
|
||||
|
||||
def translate_path(self, path):
|
||||
root = settings.STATIC_DIR
|
||||
path = path.split('?', 1)[0].split('#', 1)[0]
|
||||
if path.startswith("/static/"):
|
||||
path = path[7:]
|
||||
path = os.path.normpath(urllib.unquote(path))
|
||||
words = path.split('/')
|
||||
words = filter(None, words)
|
||||
|
||||
path = root
|
||||
for word in words:
|
||||
drive, word = os.path.splitdrive(word)
|
||||
head, word = os.path.split(word)
|
||||
if word in (os.curdir, os.pardir):
|
||||
continue
|
||||
path = os.path.join(path, word)
|
||||
|
||||
return path
|
||||
|
||||
|
||||
class StaticMiddleware(object):
|
||||
"""WSGI middleware for serving static files."""
|
||||
def __init__(self, app, prefix='/static/'):
|
||||
self.app = app
|
||||
self.prefix = prefix
|
||||
|
||||
def __call__(self, environ, start_response):
|
||||
path = environ.get('PATH_INFO', '')
|
||||
path = self.normpath(path)
|
||||
|
||||
if path.startswith(self.prefix):
|
||||
return NailgunStaticApp(environ, start_response)
|
||||
else:
|
||||
return self.app(environ, start_response)
|
||||
|
||||
def normpath(self, path):
|
||||
path2 = os.path.normpath(urllib.unquote(path))
|
||||
if path.endswith("/"):
|
||||
path2 += "/"
|
||||
return path2
|
@ -57,9 +57,9 @@ from nailgun.db.sqlalchemy.models import Task
|
||||
from nailgun.objects import Cluster
|
||||
from nailgun.objects import Release
|
||||
|
||||
from nailgun.app import build_app
|
||||
from nailgun.consts import NETWORK_INTERFACE_TYPES
|
||||
from nailgun.network.manager import NetworkManager
|
||||
from nailgun.wsgi import build_app
|
||||
|
||||
|
||||
class TimeoutError(Exception):
|
||||
|
@ -15,9 +15,7 @@
|
||||
# under the License.
|
||||
|
||||
import jinja2
|
||||
import mimetypes
|
||||
import os.path
|
||||
import web
|
||||
|
||||
from nailgun.settings import settings
|
||||
|
||||
@ -27,19 +25,4 @@ class IndexHandler(object):
|
||||
tpl_path = os.path.join(settings.TEMPLATE_DIR, 'index.html')
|
||||
with open(tpl_path, 'r') as f:
|
||||
tpl = jinja2.Template(f.read())
|
||||
return tpl.render(**{
|
||||
'use_less': bool(settings.DEVELOPMENT)
|
||||
})
|
||||
|
||||
|
||||
class StaticHandler(object):
|
||||
def GET(self, fl):
|
||||
fl_path = os.path.join(settings.STATIC_DIR, fl)
|
||||
mimetype = mimetypes.guess_type(fl_path)[0]
|
||||
if mimetype:
|
||||
web.header("Content-Type", mimetype)
|
||||
if os.path.exists(fl_path):
|
||||
with open(fl_path, 'r') as f:
|
||||
return f.read()
|
||||
else:
|
||||
raise web.notfound()
|
||||
return tpl.render()
|
||||
|
@ -17,10 +17,8 @@
|
||||
import web
|
||||
|
||||
from nailgun.webui.handlers import IndexHandler
|
||||
from nailgun.webui.handlers import StaticHandler
|
||||
|
||||
urls = (
|
||||
r"/static/(.*)", StaticHandler.__name__,
|
||||
r"/", IndexHandler.__name__,
|
||||
)
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2013 Mirantis, Inc.
|
||||
# Copyright 2014 Mirantis, 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
|
||||
@ -16,71 +16,11 @@
|
||||
|
||||
import os
|
||||
import sys
|
||||
import web
|
||||
from web.httpserver import WSGIServer
|
||||
|
||||
curdir = os.path.dirname(__file__)
|
||||
sys.path.insert(0, curdir)
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
|
||||
from nailgun.api.handlers import forbid_client_caching
|
||||
from nailgun.api.handlers import load_db_driver
|
||||
from nailgun.db import engine
|
||||
from nailgun.logger import HTTPLoggerMiddleware
|
||||
from nailgun.logger import logger
|
||||
from nailgun.settings import settings
|
||||
from nailgun.urls import urls
|
||||
from nailgun.app import build_app
|
||||
from nailgun.app import build_middleware
|
||||
|
||||
|
||||
def build_app(db_driver=None):
|
||||
"""Build app and disable debug mode in case of production
|
||||
"""
|
||||
web.config.debug = bool(int(settings.DEVELOPMENT))
|
||||
app = web.application(urls(), locals())
|
||||
app.add_processor(db_driver or load_db_driver)
|
||||
app.add_processor(forbid_client_caching)
|
||||
return app
|
||||
|
||||
|
||||
def build_middleware(app):
|
||||
middleware_list = [
|
||||
HTTPLoggerMiddleware
|
||||
]
|
||||
|
||||
logger.debug('Initialize middleware: %s' %
|
||||
(map(lambda x: x.__name__, middleware_list)))
|
||||
|
||||
return app(*middleware_list)
|
||||
|
||||
|
||||
def run_server(func, server_address=('0.0.0.0', 8080)):
|
||||
"""This function same as runsimple from web/httpserver
|
||||
except removed LogMiddleware because we use
|
||||
HTTPLoggerMiddleware instead
|
||||
"""
|
||||
global server
|
||||
server = WSGIServer(server_address, func)
|
||||
print('http://%s:%d/' % server_address)
|
||||
|
||||
try:
|
||||
server.start()
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
server.stop()
|
||||
|
||||
|
||||
def appstart():
|
||||
logger.info("Fuel version: %s", str(settings.VERSION))
|
||||
if not engine.dialect.has_table(engine.connect(), "nodes"):
|
||||
logger.error(
|
||||
"Database tables not created. Try './manage.py syncdb' first"
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
app = build_app()
|
||||
|
||||
wsgifunc = build_middleware(app.wsgifunc)
|
||||
|
||||
run_server(wsgifunc,
|
||||
(settings.LISTEN_ADDRESS, int(settings.LISTEN_PORT)))
|
||||
|
||||
logger.info("Stopping WSGI app...")
|
||||
logger.info("Done")
|
||||
application = build_middleware(build_app().wsgifunc)
|
||||
|
@ -451,7 +451,7 @@ function run_server {
|
||||
# $1 -- path to settings to be saved
|
||||
# $2 -- path to compressed static dir
|
||||
function create_settings_yaml {
|
||||
echo -e "DEVELOPMENT: 0\nSTATIC_DIR: '$2'\nTEMPLATE_DIR: '$2'" > "$1"
|
||||
echo -e "DEVELOPMENT: 1\nSTATIC_DIR: '$2'\nTEMPLATE_DIR: '$2'" > "$1"
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user