From a3abcbe28d64b1a946ae8828b77a6171dc4357a3 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Fri, 29 Jan 2021 21:32:28 +0100 Subject: [PATCH] Add scheduler and logging --- flaredestroyy.py | 18 ------------------ pynyaata/__init__.py | 29 +++++++++++++++++++++++++++-- pynyaata/config.py | 7 ++++++- pynyaata/connectors/core.py | 5 ++--- requirements.txt | 1 + 5 files changed, 36 insertions(+), 24 deletions(-) delete mode 100644 flaredestroyy.py diff --git a/flaredestroyy.py b/flaredestroyy.py deleted file mode 100644 index afedadf..0000000 --- a/flaredestroyy.py +++ /dev/null @@ -1,18 +0,0 @@ -import json - -import requests - -from pynyaata.config import CLOUDPROXY_ENDPOINT - -json_session = requests.post(CLOUDPROXY_ENDPOINT, data=json.dumps({ - 'cmd': 'sessions.list' -})) -response = json.loads(json_session.text) -sessions = response['sessions'] - -for session in sessions: - requests.post(CLOUDPROXY_ENDPOINT, data=json.dumps({ - 'cmd': 'sessions.destroy', - 'session': session - })) - print('Destroyed %s' % session) diff --git a/pynyaata/__init__.py b/pynyaata/__init__.py index 3dfbdd4..d5095b3 100644 --- a/pynyaata/__init__.py +++ b/pynyaata/__init__.py @@ -2,10 +2,13 @@ from asyncio import get_event_loop, set_event_loop, SelectorEventLoop from functools import wraps from operator import attrgetter, itemgetter -from flask import redirect, render_template, request, url_for, abort +import requests +from flask import redirect, render_template, request, url_for, abort, json +from requests import RequestException from . import utils -from .config import app, auth, ADMIN_USERNAME, ADMIN_PASSWORD, MYSQL_ENABLED, APP_PORT, IS_DEBUG +from .config import app, auth, logger, scheduler, ADMIN_USERNAME, ADMIN_PASSWORD, MYSQL_ENABLED, APP_PORT, IS_DEBUG, \ + CLOUDPROXY_ENDPOINT from .connectors import get_instance, run_all from .connectors.core import ConnectorLang, ConnectorReturn from .forms import SearchForm, DeleteForm, EditForm @@ -191,6 +194,28 @@ def admin_edit(link_id=None): return render_template('admin/edit.html', search_form=SearchForm(), link=link, folders=folders, action_form=form) +@scheduler.task('interval', id='flaredestroyy', days=1) +def flaredestroyy(): + if CLOUDPROXY_ENDPOINT: + try: + json_session = requests.post(CLOUDPROXY_ENDPOINT, data=json.dumps({ + 'cmd': 'sessions.list' + })) + response = json.loads(json_session.text) + sessions = response['sessions'] + + for session in sessions: + requests.post(CLOUDPROXY_ENDPOINT, data=json.dumps({ + 'cmd': 'sessions.destroy', + 'session': session + })) + logger.info('Destroyed %s' % session) + except RequestException as e: + logger.exception(e) + + def run(): app.config['SQLALCHEMY_ECHO'] = IS_DEBUG + scheduler.start() + flaredestroyy() app.run('0.0.0.0', APP_PORT, IS_DEBUG) diff --git a/pynyaata/config.py b/pynyaata/config.py index 34dfe62..0766514 100644 --- a/pynyaata/config.py +++ b/pynyaata/config.py @@ -1,7 +1,9 @@ +import logging from os import environ, urandom from flask import Flask from flask.cli import load_dotenv +from flask_apscheduler import APScheduler from flask_httpauth import HTTPBasicAuth from flask_sqlalchemy import SQLAlchemy @@ -23,6 +25,9 @@ app.debug = IS_DEBUG app.secret_key = urandom(24).hex() app.url_map.strict_slashes = False auth = HTTPBasicAuth() +scheduler = APScheduler(app=app) +logging.basicConfig(level=(logging.DEBUG if IS_DEBUG else logging.INFO)) +logger = logging.getLogger(app.name) db_host = environ.get('MYSQL_SERVER') if db_host: @@ -31,7 +36,7 @@ if db_host: db_password = environ.get('MYSQL_PASSWORD') db_name = environ.get('MYSQL_DATABASE') if not db_user or not db_password or not db_name: - print('Missing connection environment variables') + logger.error('Missing connection environment variables') exit() app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://%s:%s@%s/%s?charset=utf8mb4' % ( db_user, db_password, db_host, db_name diff --git a/pynyaata/connectors/core.py b/pynyaata/connectors/core.py index 0efab6f..8dce83b 100644 --- a/pynyaata/connectors/core.py +++ b/pynyaata/connectors/core.py @@ -4,13 +4,12 @@ from datetime import datetime from enum import Enum from functools import wraps from json import dumps, loads -from logging import getLogger from urllib.parse import urlencode import requests from requests import RequestException -from ..config import CACHE_TIMEOUT, REQUESTS_TIMEOUT, CLOUDPROXY_ENDPOINT +from ..config import CACHE_TIMEOUT, REQUESTS_TIMEOUT, CLOUDPROXY_ENDPOINT, logger cloudproxy_session = None @@ -128,7 +127,7 @@ def curl_content(url, params=None, ajax=False, debug=True): cloudproxy_session = None except RequestException as e: if debug: - getLogger().exception(e) + logger.exception(e) return {'http_code': http_code, 'output': output} diff --git a/requirements.txt b/requirements.txt index f989dff..da00029 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ Flask==1.1.2 +Flask-APScheduler==1.11.0 Flask-SQLAlchemy==2.4.4 Flask-HTTPAuth==4.2.0 Flask-WTF==0.14.3