diff --git a/app/commands.py b/app/commands.py index 3134399786216dc0d83cb122e8068d1c1f8ea5dc..221ca712ca7cca04251425d97f2d8ee1d451d3c5 100644 --- a/app/commands.py +++ b/app/commands.py @@ -94,7 +94,7 @@ def register_cli(app): @app.cli.command() def runworker(): """Run RQ worker""" - redis_url = current_app.config['RQ_REDIS_URL'] + redis_url = current_app.config['REDIS_URL'] redis_connection = redis.from_url(redis_url) with rq.Connection(redis_connection): worker = rq.Worker(current_app.config['QUEUES']) diff --git a/app/factory.py b/app/factory.py index 94371ed700ada7e47b8062099483dff2ea3c0ac9..afea592076303f0bb5f1ab0fad5f0bb374f52cfb 100644 --- a/app/factory.py +++ b/app/factory.py @@ -10,6 +10,7 @@ Create the WSGI application. """ import sqlalchemy as sa +import rq_dashboard from flask import Flask from whitenoise import WhiteNoise from . import settings, models @@ -30,6 +31,7 @@ from .commands import register_cli def create_app(config=None): app = Flask(__name__) + app.config.from_object(rq_dashboard.default_settings) app.config.from_object(settings) app.config.from_envvar('LOCAL_SETTINGS', silent=True) app.config.update(config or {}) @@ -118,6 +120,7 @@ def create_app(config=None): app.register_blueprint(user_api, url_prefix='/api/v1/user') app.register_blueprint(inventory_api, url_prefix='/api/v1/inventory') app.register_blueprint(network_api, url_prefix='/api/v1/network') + app.register_blueprint(rq_dashboard.blueprint, url_prefix='/rq') app.wsgi_app = WhiteNoise(app.wsgi_app, root='static/') app.wsgi_app.add_files( diff --git a/app/main/views.py b/app/main/views.py index dcc9375d4a49341fd6ccada1ee941e394765e2d2..3fc9bcb26209a2e0093a8342300ff3f07d715920 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -11,14 +11,23 @@ This module implements the main blueprint. """ import os import redis -from flask import Blueprint, render_template, jsonify, g, current_app -from flask_login import login_required +import rq_dashboard +from flask import Blueprint, render_template, jsonify, g, current_app, abort +from flask_login import login_required, current_user from rq import push_connection, pop_connection from .. import utils bp = Blueprint('main', __name__) +# Allow only admin to access the RQ Dashboard +@rq_dashboard.blueprint.before_request +@login_required +def before_request(): + if not current_user.is_admin: + abort(403) + + # Declare custom error handlers for all views @bp.app_errorhandler(403) def forbidden_error(error): @@ -63,7 +72,7 @@ def modified_static_file(endpoint, values): def get_redis_connection(): redis_connection = getattr(g, '_redis_connection', None) if redis_connection is None: - redis_url = current_app.config['RQ_REDIS_URL'] + redis_url = current_app.config['REDIS_URL'] redis_connection = g._redis_connection = redis.from_url(redis_url) return redis_connection diff --git a/app/settings.py b/app/settings.py index 1694d16cd3110723d3aef7d68a5052c9c2a32600..e45241b748cd6077b64533ffa6790649f0342d80 100644 --- a/app/settings.py +++ b/app/settings.py @@ -30,7 +30,7 @@ SESSION_TYPE = 'redis' SESSION_REDIS_URL = 'redis://redis:6379/0' CACHE_TYPE = 'redis' CACHE_REDIS_URL = 'redis://redis:6379/1' -RQ_REDIS_URL = 'redis://redis:6379/2' +REDIS_URL = 'redis://redis:6379/2' QUEUES = ['default'] LDAP_HOST = 'esss.lu.se' diff --git a/app/templates/base.html b/app/templates/base.html index 0477e63ebbab6bb3a7cd9dddd202b3b7585549a7..e0c65746ead1292b039b1385f0d43033573039e7 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -28,6 +28,7 @@ <a class="nav-item nav-link {{ is_active(path.startswith("/network")) }}" href="{{ url_for('network.list_hosts') }}">Network</a> {% if current_user.is_authenticated and current_user.is_admin %} <a class="nav-item nav-link" href="{{ url_for('admin.index') }}">Admin</a> + <a class="nav-item nav-link" href="{{ url_for('rq_dashboard.overview') }}">RQ Dashboard</a> {% endif %} <a class="nav-item nav-link" href="{{ config['DOCUMENTATION_URL'] }}" target="_blank">Help</a> </div> diff --git a/environment.yml b/environment.yml index 5134bf90aef5fabacbe50a96950b4dc2d84eceef..5e8fd7eb458d1e55ae1322b24ec7650c2058be8a 100644 --- a/environment.yml +++ b/environment.yml @@ -96,6 +96,7 @@ dependencies: - ecdsa=0.13=py36_0 - pip: - ansible-tower-cli==3.2.1 + - arrow==0.12.1 - dominate==2.3.1 - email-validator==1.0.2 - git+https://github.com/beenje/flask-bootstrap@4.0.0.0.dev1 @@ -107,6 +108,7 @@ dependencies: - inflection==0.3.1 - pytest-factoryboy==2.0.1 - rq==0.10.0 + - rq-dashboard==0.3.10 - sphinxcontrib-httpdomain==1.6.0 - sqlalchemy-citext==1.3.post0 - sqlalchemy-continuum==1.3.3