-
-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #393 from mterzo/clean_error
Handle server exception when unable to connect early.
- Loading branch information
Showing
8 changed files
with
157 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from __future__ import unicode_literals | ||
from __future__ import absolute_import | ||
|
||
import logging | ||
|
||
from flask import Flask | ||
|
||
from pypuppetdb import connect | ||
from puppetboard.utils import (jsonprint, prettyprint, url_for_field, | ||
get_or_abort) | ||
|
||
from . import __version__ | ||
|
||
APP = None | ||
PUPPETDB = None | ||
|
||
|
||
def get_app(): | ||
global APP | ||
|
||
if APP is None: | ||
app = Flask(__name__) | ||
app.config.from_object('puppetboard.default_settings') | ||
app.config.from_envvar('PUPPETBOARD_SETTINGS', silent=True) | ||
app.secret_key = app.config['SECRET_KEY'] | ||
|
||
numeric_level = getattr(logging, app.config['LOGLEVEL'].upper(), None) | ||
if not isinstance(numeric_level, int): | ||
raise ValueError('Invalid log level: %s' % app.config['LOGLEVEL']) | ||
|
||
app.jinja_env.filters['jsonprint'] = jsonprint | ||
app.jinja_env.filters['prettyprint'] = prettyprint | ||
app.jinja_env.globals['url_for_field'] = url_for_field | ||
APP = app | ||
|
||
return APP | ||
|
||
|
||
def get_puppetdb(): | ||
global PUPPETDB | ||
|
||
if PUPPETDB is None: | ||
app = get_app() | ||
puppetdb = connect(host=app.config['PUPPETDB_HOST'], | ||
port=app.config['PUPPETDB_PORT'], | ||
ssl_verify=app.config['PUPPETDB_SSL_VERIFY'], | ||
ssl_key=app.config['PUPPETDB_KEY'], | ||
ssl_cert=app.config['PUPPETDB_CERT'], | ||
timeout=app.config['PUPPETDB_TIMEOUT'],) | ||
PUPPETDB = puppetdb | ||
|
||
return PUPPETDB | ||
|
||
|
||
def environments(): | ||
puppetdb = get_puppetdb() | ||
envs = get_or_abort(puppetdb.environments) | ||
x = [] | ||
|
||
for env in envs: | ||
x.append(env['name']) | ||
|
||
return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from __future__ import unicode_literals | ||
from __future__ import absolute_import | ||
|
||
from puppetboard.core import get_app, environments | ||
from werkzeug.exceptions import InternalServerError | ||
from flask import render_template | ||
from . import __version__ | ||
|
||
app = get_app() | ||
|
||
|
||
@app.errorhandler(400) | ||
def bad_request(e): | ||
envs = environments() | ||
return render_template('400.html', envs=envs), 400 | ||
|
||
|
||
@app.errorhandler(403) | ||
def forbidden(e): | ||
envs = environments() | ||
return render_template('403.html', envs=envs), 403 | ||
|
||
|
||
@app.errorhandler(404) | ||
def not_found(e): | ||
envs = environments() | ||
return render_template('404.html', envs=envs), 404 | ||
|
||
|
||
@app.errorhandler(412) | ||
def precond_failed(e): | ||
"""We're slightly abusing 412 to handle missing features | ||
depending on the API version.""" | ||
envs = environments() | ||
return render_template('412.html', envs=envs), 412 | ||
|
||
|
||
@app.errorhandler(500) | ||
def server_error(e): | ||
envs = [] | ||
try: | ||
envs = environments() | ||
except InternalServerError as e: | ||
pass | ||
return render_template('500.html', envs=envs), 500 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters