예제 #1
0
파일: app.py 프로젝트: zhaw-ims/mesito
def produce(
        session_factory: sqlalchemy.orm.scoped_session,
        cors_allowed_all_origins: bool
) -> Tuple[flask.Flask, flask_socketio.SocketIO]:  # yapf: enable
    """
    Produce our flask application.

    :param session_factory: SQLAlchemy session factory
    :param cors_allowed_origins:
        if set, changes the CORS allowed origins of the app to everybody
    :return: flask application
    """
    app = flask.Flask(__name__)

    v1_api = _v1_api_blueprint(session_factory=session_factory)
    app.register_blueprint(v1_api, url_prefix='/api/v1')

    static = _static_blueprint()
    app.register_blueprint(static)

    if cors_allowed_all_origins:
        flask_cors.CORS(app)
        socketio = flask_socketio.SocketIO(app=app, cors_allowed_origins="*")
    else:
        socketio = flask_socketio.SocketIO(app=app)

    def cleanup(
            resp_or_exc: Any) -> Any:  # pylint: disable=unused-argument, unused-variable
        """Release resources acquired in an app context."""
        session_factory.remove()

    app.teardown_appcontext(cleanup)

    return app, socketio
예제 #2
0
def test_view_websocket():
    from gravitate.main import app

    mediator = UserLocationWsMediator(view_model_cls=UserLocationWebsocket,
                                      namespace="/sublocations")

    print(Location.get_schema_obj().fields)

    io = flask_socketio.SocketIO(app=app)
    io.on_namespace(mediator)

    client = io.test_client(app=app, namespace='/sublocations')

    assert client.is_connected(namespace='/sublocations')

    _ = client.emit('create_draft', {
        "user_id": "test_user_id1",
        "latitude": 32.879707,
        "longitude": -117.241254
    },
                    namespace='/sublocations')

    res = client.get_received(namespace="/sublocations")
    assert res[-1] == {
        'name':
        'draft_created',
        'args': [{
            'latitude': 32.879707,
            'longitude': -117.241254,
            'address': 'Muir Ln, San Diego, CA 92161, USA'
        }],
        'namespace':
        '/sublocations'
    }
예제 #3
0
    def test_disconnect(self):
        socketio = flask_socketio.SocketIO(app.app)

        client = app.socketio.test_client(app.app)
        socketio.emit('disconnect')
        recieved = client.get_received()
        print(recieved)
def init_app():

    global my_worker

    app = flask.Flask(__name__)

    app.config["SECRET_KEY"] = "tkmu7l90u578t3q2cbvsd78345i8gbmghjp70i8ltheuyt"

    socketio = flask_socketio.SocketIO(app,
                                       async_mode=None,
                                       cors_allowed_origins="*")

    my_worker = MyWorker(socketio=socketio)

    def run(socketio, app):

        log.info("my_worker starting")

        my_worker.start(socketio=socketio)

        log.info("socketio=%s, app=%s, my_worker=%s", socketio, app, my_worker)

        socketio.run(app, debug=False)

    run_server = functools.partial(run, socketio=socketio, app=app)

    return socketio, run_server
예제 #5
0
 def __init__(self, app: flask.Flask):
     self.app = app
     self.sio = flask_socketio.SocketIO(app)
     self.discord = DiscordOAuth2Session(app)
     self.metrics = PrometheusMetrics(app)
     self.fernet_encrypt = Fernet(app.config["FERNET_KEY"])
     if app.config["GUEST_KEY"] is not None:
         self.guest_encrypt = Fernet(app.config["GUEST_KEY"])
예제 #6
0
파일: core.py 프로젝트: pbehnke/flask_react
 def init_socket(self):
     """Initialize SocketIO extension."""
     if "socket.io" in self.extensions:
         if not SOCKET_IO:
             raise ValueError("Flask-SocketIO is not installed "
                              "for 'socket.io' extension.")
         if self.socket is None:
             self.socket = sio.SocketIO(self.app)
예제 #7
0
    def __init__(self, scheduler, capabilities=None, operation_timeout=1):
        self.scheduler = scheduler
        patch_scheduler(scheduler)
        self.capabilities = {
            'pause_job': False,
            'remove_job': False,
            'pause_scheduler': False,
            'stop_scheduler': False,
            'run_job': False,
        }

        if not (isinstance(operation_timeout, int)
                or isinstance(operation_timeout, float)):
            raise TypeError(
                'operation_timeout should be either an int or a float')

        if operation_timeout <= 0:
            raise ValueError('operation_timeout should be a positive number')

        self.operation_timeout = operation_timeout

        if capabilities is not None:
            if isinstance(capabilities, dict):
                self.capabilities.update(capabilities)
            else:
                raise TypeError(
                    'capabilities should be a dict of str -> bool pairs')

        self._scheduler_listener = SchedulerWatcher(scheduler)

        self._web_server = flask.Flask(__name__)
        self._socket_io = None

        try:
            # TODO: see if we can support eventlet in the future.
            self._socket_io = flask_socketio.SocketIO(self._web_server,
                                                      async_mode='gevent')
        except ValueError:
            self._socket_io = flask_socketio.SocketIO(self._web_server,
                                                      async_mode='threading')

        self._init_endpoints()

        self._web_server_thread = None
        self._scheduler_lock = threading.Lock()
예제 #8
0
def build_app(
    directory: str,
    watch_filesystem: bool,
) -> Tuple[flask.Flask, flask_socketio.SocketIO, runner.PyTestRunner]:
    """Build a Flask app to serve the API and static files."""
    build_dir = pkg_resources.resource_filename(__name__, "web_client/build")
    LOGGER.debug("build_dir: %s", build_dir)
    static_dir = os.path.join(build_dir, "static")
    index_file = os.path.join(build_dir, "index.html")

    app = flask.Flask(__name__, root_path=build_dir, static_folder=static_dir)
    branch_schema = result_tree.BranchNodeSchema()
    socketio = flask_socketio.SocketIO(app)
    test_runner = runner.PyTestRunner(directory, socketio, watch_filesystem)

    @app.route("/")
    def index():
        return flask.send_file(index_file)

    @app.route("/<path:path>")
    def send_build(path):
        LOGGER.debug("Sending file: %s", path)
        return flask.send_from_directory(build_dir, path)

    @app.route("/api/v1/result-tree")
    def tree() -> Dict[str, Any]:
        try:
            return branch_schema.dump(test_runner.result_tree)
        except Exception:
            traceback.print_exc()
            raise

    @socketio.on("run test")
    def run_test(nodeid):
        LOGGER.info("Running test: %s", nodeid)
        test_runner.run_tests(nodeid)

    @socketio.on("start env")
    def start_env(nodeid):
        LOGGER.info("starting env: %s", nodeid)
        test_runner.start_env(nodeid)

    @socketio.on("stop env")
    def stop_env(nodeid):
        LOGGER.info("stopping env: %s", nodeid)
        test_runner.stop_env(nodeid)

    @socketio.on("connect")
    def connect():
        LOGGER.debug("Client connected")

    @socketio.on("disconnect")
    def disconnect():
        LOGGER.debug("Client disconnected")

    return app, socketio, test_runner
예제 #9
0
 def __init__(self):
     """
     Construct the Flask instance and configure the REST API
     """
     self._app = flask.Flask(__name__)
     self._app.config['SECRET_KEY'] = str(uuid.uuid4())
     self._socketio = flask_socketio.SocketIO(self._app, async_mode='eventlet')
     self._angular = None
     self._setup_rest()
     self._setup_socketio()
     self._setup_angular()
예제 #10
0
def main():
    app = Flask(__name__)
    socketio = flask_socketio.SocketIO(app,
                                       async_mode=ASYNC_MODE,
                                       ping_interval=PING_INTERVAL,
                                       message_queue='redis://')

    views.configure_views(app=app, socketio=socketio)

    app.secret_key = 'ssssssssssh'
    app.config['FLASK_LOG_LEVEL'] = 'DEBUG'

    return app, socketio
예제 #11
0
    def __init__(self, app: flask.Flask):
        self.app = app
        self.sio = flask_socketio.SocketIO(app)
        self.discord = CustomDiscordOAuth2Session(app)
        self.metrics = PrometheusMetrics(app)
        self.fernet_encrypt = Fernet(app.config["FERNET_KEY"])
        if app.config["GUEST_KEY"] is not None:
            self.guest_encrypt = Fernet(app.config["GUEST_KEY"])
        if app.config["ENFORCE_ROLE"] is not None:
            self.enforce_role = EnforceDiscordRole(app.config["ENFORCE_ROLE"])

        self.expected_headers = connection_headers()
        self.expected_headers.pop("X-Randovania-Version")
예제 #12
0
    def _prepare_websocket(self):
        # handles all namespaces without an explicit error handler
        websocket_instance = flask_socketio.SocketIO(
            web_interface_root.server_instance,
            async_mode="gevent",
            cors_allowed_origins=self.cors_allowed_origins
        )

        @websocket_instance.on_error_default
        def default_error_handler(e):
            self.logger.exception(e, True, f"Error with websocket: {e}")
        for namespace in websockets.namespaces:
            websocket_instance.on_namespace(namespace)

        bot_logging.register_error_notifier(web_interface_root.send_general_notifications)
        return websocket_instance
예제 #13
0
    def _run_flask(self) -> None:
        from gevent import monkey

        monkey.patch_all()
        import flask
        import flask_socketio
        import markupsafe

        config: Dict[str, Any] = {}
        dir_path = os.path.dirname(os.path.realpath(__file__))
        templates_path = os.path.join(dir_path, "resources", "templates")
        static_path = os.path.join(dir_path, "resources", "static")
        app = flask.Flask("clubs",
                          template_folder=templates_path,
                          static_folder=static_path)
        socketio = flask_socketio.SocketIO(app)

        @socketio.on("connect")
        def connect():  # type: ignore
            socketio.emit("config", config)

        @app.route("/")
        def index():  # type: ignore
            svg = str(self.svg_poker.base_svg)
            return flask.render_template("index.html",
                                         svg=markupsafe.Markup(svg))

        def listener() -> None:
            nonlocal config
            socket = connection.Listener((self.host, self.port + 1))
            conn = socket.accept()
            while True:
                if conn.poll():
                    message: Dict[str, Any] = conn.recv()
                    if message["content"] == "close":
                        conn.close()
                        break
                    else:
                        config = message["content"]
                        socketio.emit("config", config, broadcast=True)
                socketio.sleep(0.0001)
            socket.close()

        socketio.start_background_task(listener)

        socketio.run(app, port=self.port)
예제 #14
0
파일: server.py 프로젝트: AlienCookie/InDev
def main():  # pragma: no cover
    get_env('GP_SERVICE')
    get_env('GP_VERSION')
    get_env('GP_DATABASE_HOST')
    get_env('GP_DATABASE_USER')
    get_env('GP_DATABASE_PASSWORD')
    get_env('GP_DATABASE_PORT')
    get_env('GP_DATABASE_DB')

    client_manager = ClientManager()
    sio = flask_socketio.SocketIO(app,
                                  path='/api/v1/socket.io',
                                  async_mode='eventlet',
                                  client_manager=client_manager)

    port = int(os.environ.get('INFRABOX_PORT', 8080))
    logger.info('Starting Server on port %s', port)
    sio.run(app, host='0.0.0.0', port=port)
예제 #15
0
파일: app.py 프로젝트: juangon/var-sandbox
def main():
    """Start the application and processes.

    The main function will pull together the environment variables,
    start the process for the prediction loop, start the thread for the
    socketio responder, and setup the Flask HTTP server.
    """
    spark_master = os.environ.get('SPARK_MASTER', 'local[*]')
    wikieod_file = os.environ.get('WIKIEOD_FILE', '/data/wikieod.parquet')
    debug_mode = os.environ.get('VAR_DEBUG', False)

    input_queue = mp.Queue()
    output_queue = mp.Queue()

    if debug_mode:
        process = mp.Process(target=debug_processing_loop,
                             args=(input_queue, output_queue))
    else:
        process = mp.Process(target=processing_loop,
                             args=(spark_master, input_queue, output_queue,
                                   wikieod_file))

    process.start()
    output_queue.get()

    app = flask.Flask(__name__)
    app.config['SECRET_KEY'] = 'secret!'
    app.add_url_rule('/', view_func=HTMLContent.as_view('html'))
    app.add_url_rule('/img/brand-var.svg', view_func=BrandLogo.as_view('logo'))
    app.add_url_rule('/predictions',
                     view_func=PredictionAPI.as_view('predictions',
                                                     input_queue))

    socketio = io.SocketIO(app)
    thread = threading.Thread(target=responder_loop,
                              args=(socketio, output_queue))
    thread.start()
    try:
        print('server running on 0.0.0.0:8080, press Ctrl-C to stop')
        print('spark master = {}'.format(spark_master))
        print('wikieod file = {}'.format(wikieod_file))
        socketio.run(app, host='0.0.0.0', port=8080)
    except KeyboardInterrupt:
        output_queue.put('STOP')
예제 #16
0
    def __init__(self, app, db):
        self.app = app
        self.app.add_url_rule("/", "index", self.index)
        self.app.add_url_rule("/create-join", "create-join", self.create_join)

        if DEBUG:
            self.app.add_url_rule("/debug", "debug", self.debug)
            self.app.add_url_rule("/debug.json", "debug.json", self.debug_json)

        # self.app.config['SESSION_TYPE'] = 'redis'
        # self.app.config['SECRET_KEY'] = os.environ['FLASK_SECRET_KEY']
        # self.session = flask_session.Session()
        # self.session.init_app(self.app)

        self.socketio = flask_socketio.SocketIO(self.app)
        self.socketio.init_app(self.app, cors_allowed_origins="*")

        self.db = db

        self.base_ns = socketns.base.BaseNamespace("/", self)
        self.chat_ns = socketns.chat.ChatNamespace("/", self)
        self.youtube_ns = socketns.youtube.YoutubeNamespace("/", self)
        self.login_ns = socketns.login.LoginNamespace("/", self)
        self.room_ns = socketns.room.RoomNamespace("/", self)

        self.socketio.on_namespace(
            socketns.CustomCombinedNamespace(
                "/",
                self,
                [
                    self.base_ns,
                    self.chat_ns,
                    self.youtube_ns,
                    self.room_ns,
                    self.login_ns,
                ],
            ))

        self.rooms = {}
        self.users = {}

        self.test_login_enabled = False
예제 #17
0
def build_app(directory: str) -> Tuple[flask.Flask, flask_socketio.SocketIO]:
    """Build a Flask app to serve the API and static files."""
    build_dir = pkg_resources.resource_filename(__name__, "web_client/build")
    LOGGER.debug("build_dir: %s", build_dir)
    static_dir = os.path.join(build_dir, "static")
    index_file = os.path.join(build_dir, "index.html")

    app = flask.Flask(__name__, root_path=build_dir, static_folder=static_dir)
    branch_schema = result_tree.BranchNodeSchema()
    shallow_branch_schema = result_tree.NodeSchema()
    leaf_schema = result_tree.LeafNodeSchema()
    socketio = flask_socketio.SocketIO(app)
    test_runner = runner.PyTestRunner(directory, socketio)

    @app.route("/")
    def index():
        return flask.send_file(index_file)

    @app.route("/<path:path>")
    def send_build(path):
        LOGGER.debug("Sending file: %s", path)
        return flask.send_from_directory(build_dir, path)

    @app.route("/api/v1/result-tree")
    def tree() -> Dict[str, Any]:
        return branch_schema.dump(test_runner.result_tree)

    @socketio.on("run test")
    def run_test(nodeid):
        LOGGER.info("Running test: %s", nodeid)
        test_runner.run_tests(nodeid)

    @socketio.on("connect")
    def connect():
        LOGGER.debug("Client connected")

    @socketio.on("disconnect")
    def disconnect():
        LOGGER.debug("Client disconnected")

    return app, socketio
예제 #18
0
파일: web_server.py 프로젝트: limgit/opdss
    def __init__(self, chn_mng: ChannelManager, obj_mng: ObjectManager,
                 tpl_mng: TemplateManager, sgn_mng: SignageManager,
                 mtm_mng: MultimediaManager):
        self._app = flask.Flask(__name__)
        self._app.static_folder = str(tpl_mng.root_dir.resolve())
        self._app.add_url_rule('/favicon.ico', 'favicon', lambda: '')
        self._app.add_url_rule('/', 'handle_channel_list',
                               self.handle_channel_list)
        self._app.add_url_rule('/<channel_id>', 'handle_channel',
                               self.handle_channel)
        self._app.add_url_rule('/_/image/<file_name>', 'handle_image_static',
                               self.handle_image_static)
        self._app.add_url_rule('/_/video/<file_name>', 'handle_video_static',
                               self.handle_video_static)
        self._app.add_url_rule('/_/<path:path>', 'handle_template_static',
                               self.handle_template_static)

        self._chn_mng = chn_mng
        self._obj_mng = obj_mng
        self._tpl_mng = tpl_mng
        self._sgn_mng = sgn_mng
        self._mtm_mng = mtm_mng

        self._socket_io = flask_socketio.SocketIO(self._app,
                                                  ping_interval=10,
                                                  ping_timeout=60)
        self._io_server = FlaskIOServer(self._socket_io)

        def redirect_event(channel: Channel, old_id: str):
            self._io_server.request_redirect(old_id, channel.id)

        def count_event(channel: Channel):
            return self._io_server.get_connections(channel.id)

        self._chn_mng.redirect_event_handler = redirect_event
        self._chn_mng.count_event_handler = count_event

        self._socket_io.on_namespace(self._io_server)

        super().__init__()
예제 #19
0
파일: app.py 프로젝트: gunpowder78/mahjong
import flask
import flask_socketio

from server import session_manager

app = flask.Flask(__name__)
socketio = flask_socketio.SocketIO(app)
game_sessions = session_manager.GameSessionManager()


@app.route("/")
def index():
    return flask.render_template('game.html')


@socketio.on('join')
def handle_join(data=None):
    app.logger.info('Received request to join: ' + str(data))

    if 'username' not in data:
        raise RuntimeError("Missing username")
    elif 'game_id' not in data:
        raise RuntimeError("Missing game_id")

    client_id = flask.request.sid
    username = data['username']
    game_id = data['game_id']

    if username == '':
        raise RuntimeError("username cannot be empty")
    elif game_id == '':
예제 #20
0
파일: main.py 프로젝트: thefantas/tinypilot
# Location of file path at which to write mouse HID input.
mouse_path = os.environ.get('MOUSE_PATH', '/dev/hidg1')

# Socket.io logs are too chatty at INFO level.
if not debug:
    logging.getLogger('socketio').setLevel(logging.ERROR)
    logging.getLogger('engineio').setLevel(logging.ERROR)

app = flask.Flask(__name__, static_url_path='')
# TODO(mtlynch): Ideally, we wouldn't accept requests from any origin, but the
# risk of a CSRF attack for this app is very low. Additionally, CORS doesn't
# protect us from the dangerous part of a CSRF attack. Even without same-origin
# enforcement, third-party pages can still *send* requests (i.e. inject
# keystrokes into the target machine) - it doesn't matter much if they can't
# read responses.
socketio = flask_socketio.SocketIO(app, cors_allowed_origins='*')

# Configure CSRF protection.
csrf = flask_wtf.csrf.CSRFProtect(app)
app.config['SECRET_KEY'] = os.urandom(32)


@socketio.on('keystroke')
def socket_keystroke(message):
    try:
        keystroke = keystroke_request.parse_keystroke(message)
    except keystroke_request.Error as e:
        logger.error('Failed to parse keystroke request: %s', e)
        return
    hid_keycode = None
    processing_result = {'keystrokeId': keystroke.id, 'success': False}
예제 #21
0
from datetime import datetime
import flask
import flask_socketio
import sqlalchemy
from dotenv import load_dotenv
from pytz import timezone
import tables
from tables import BASE
import hourly_weather
import tweets
import news

load_dotenv()

APP = flask.Flask(__name__)
SOCKETIO = flask_socketio.SocketIO(APP)
SOCKETIO.init_app(APP, cors_allowed_origins="*")

# ENGINE = sqlalchemy.create_engine(database_url)
ENGINE = sqlalchemy.create_engine(os.environ["DATABASE_URL"])
BASE.metadata.create_all(ENGINE, checkfirst=True)

SESSION_MAKER = sqlalchemy.orm.sessionmaker(bind=ENGINE)
SESSION = SESSION_MAKER()

LOGGEDIN_CLIENTS = []

EST = timezone("EST")


@APP.route("/")
예제 #22
0
import flask
import flask_login
import flask_socketio
import swbs

config = configparser.ConfigParser()
config.read("/etc/ARBITER/web.cfg")
arbiter_config = configparser.ConfigParser()
arbiter_config.read("/etc/ARBITER/init.cfg")
application = flask.Flask(__name__)
application.secret_key = urandom(4096)
login_manager = flask_login.LoginManager()
login_manager.init_app(application)
login_manager.login_view = "login"
users = {"username": "******"}
socket_io = flask_socketio.SocketIO(application)

# all error messages get appended to this list, new clients will receive all
# error messages in list, to "catch them up"
errors: list = []
# dictionary overwritten when the directives database dispatches an update,
# sent to new clients instead of fetching database contents again
directives_database_cache: dict = {}


@socket_io.on("logError")
def log_error_broadcaster(message: str):
    """Emit event logError to all clients as a broadcast."""
    error = {
        "timestamp":
        datetime.utcnow().replace(tzinfo=timezone.utc).isoformat(),
예제 #23
0
from flask import Flask, request, render_template
import flask_socketio as SocketIO
from pydantic import BaseModel
import uuid
import json

app = Flask(__name__)
app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
socketio = SocketIO.SocketIO(app)

clients = {}
rooms = []


class User(BaseModel):
    name: str
    room_uuid: str


def messageReceived():
    print('message was received!!!')


@socketio.on('connect')
def connected():
    print('Connected {}'.format(request.sid))


@socketio.on('disconnect')
def disconnected():
    print('Disconnected {}'.format(request.sid))
예제 #24
0
'''
flask_session.Session(app)
params = dict(
    # ping_timeout=1000,
    # ping_interval=5000,
    cors_allowed_origins='*',
    logger=False,
    engineio_logger=False,
    async_mode='eventlet',
    # See: 5:22 in https://www.youtube.com/watch?v=OZ1yQTbtf5E
    # See: https://blog.miguelgrinberg.com/post/flask-socketio-and-the-user-session
    manage_session=False
    # async_mode='threading'
)
# socketio = flask_socketio.SocketIO(app, ping_timeout=20, ping_interval=10)
socketio = flask_socketio.SocketIO(app, **params)


class Rooms:
    def __init__(self):
        self.d = {}

    def get(self, json: dict):
        room = json['room']
        game = self.d.get(room, None)
        if game is not None:
            return game
        players, group = room.split('-', 2)
        return self.initialize(players=int(players), group=group)

    def initialize(self, players: int, group: str):
예제 #25
0
###############################################################################
"""HTTP request handlers."""

import datetime
import httplib

import flask
import flask_socketio

from config import Config
from runtime_status import RuntimeStatus
from socketio_api import SocketIOApi

app = flask.Flask(__name__)
app.secret_key = str(datetime.datetime.now())
socketio = flask_socketio.SocketIO(app, async_mode='gevent')


# Web page handlers.
@app.route('/')
def index_page():
    """Handler of index page."""
    protocol = 'https' if Config.get_pb().server.https.enabled else 'http'
    return flask.render_template('index.tpl', conf=Config, protocol=protocol)


@app.route('/runtime_status', methods=['GET', 'POST'])
def runtime_status():
    """
    Handler of runtime_status.
    Return current runtime status on GET request.
예제 #26
0
import time
import threading
import flask
import flask_socketio as sio
# from flask_socketio import SocketIO

app = flask.Flask(__name__)
socketio = sio.SocketIO(app)

sessions = {}


class LogSender(threading.Thread):
    def __init__(self, sid, config):
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self.sid = sid
        self.config = config
        self.need_to_stop = False

    def stop(self):
        self.need_to_stop = True

    def run(self):
        print(self.config)
        counter = 0
        while not self.need_to_stop:
            socketio.send('logging to {}. {}. config: {}'.format(
                str(self.sid), counter, self.config),
                          room=self.sid)
            counter += 1
예제 #27
0
import flask
from flask import Flask, render_template
import flask_socketio
import flask_sqlalchemy
from dotenv import load_dotenv
import requests
from flask import request
from datetime import datetime

#MAYBE WE DON'T NEED THESE TWO LINES.
from engineio.payload import Payload
Payload.max_decode_packets = 50

app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
server_socket = flask_socketio.SocketIO(app)
server_socket.init_app(app, cors_allowed_origins="*")

dotenv_path = join(dirname(__file__), "sql.env")
load_dotenv(dotenv_path)
database_uri = os.getenv("DATABASE_URL")
app.config["SQLALCHEMY_DATABASE_URI"] = database_uri

db = flask_sqlalchemy.SQLAlchemy(app)
db.init_app(app)
db.app = app

import models

db.create_all()
db.session.commit()
예제 #28
0
    DATABASE_URI = os.environ["DATABASE_URL"]
    AWS_ACCESS_KEY = os.environ["AWS_ACCESS_KEY"]
    AWS_SECRET_KEY = os.environ["AWS_SECRET_KEY"]
    twilio_account_sid = os.environ["TWILIO_ACCOUNT_SID"]
    twilio_api_key_sid = os.environ["TWILIO_API_KEY_SID"]
    twilio_api_key_secret = os.environ["TWILIO_API_KEY_SECRET"]
except KeyError:
    DATABASE_URI = ""
    AWS_ACCESS_KEY = ""
    AWS_SECRET_KEY = ""
    twilio_account_sid = ""
    twilio_api_key_sid = ""
    twilio_api_key_secret = ""

APP = flask.Flask(__name__)
socketio = flask_socketio.SocketIO(APP)
socketio.init_app(APP, cors_allowed_origins="*")
APP.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URI
APP.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

models.DB.init_app(APP)
models.DB.app = APP


def database_init():
    models.DB.create_all()
    models.DB.session.commit()


def emit_joined_rooms(client_sid):
    """
예제 #29
0
def main():  # pragma: no cover
    get_env('INFRABOX_VERSION')
    get_env('INFRABOX_DATABASE_HOST')
    get_env('INFRABOX_DATABASE_USER')
    get_env('INFRABOX_DATABASE_PASSWORD')
    get_env('INFRABOX_DATABASE_PORT')
    get_env('INFRABOX_DATABASE_DB')

    get_env('INFRABOX_GENERAL_REPORT_ISSUE_URL')

    if get_env('INFRABOX_STORAGE_GCS_ENABLED') == 'true':
        get_env('GOOGLE_APPLICATION_CREDENTIALS')
        get_env('INFRABOX_STORAGE_GCS_BUCKET')

    if get_env('INFRABOX_STORAGE_S3_ENABLED') == 'true':
        get_env('INFRABOX_STORAGE_S3_BUCKET')
        get_env('INFRABOX_STORAGE_S3_REGION')

    app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 1024 * 4
    client_manager = ClientManager()
    sio = flask_socketio.SocketIO(app,
                                  path='/api/v1/socket.io',
                                  async_mode='eventlet',
                                  client_manager=client_manager)

    urllib3.disable_warnings()

    @sio.on('listen:jobs')
    def __listen_jobs(project_id):
        logger.debug('listen:jobs for %s', project_id)

        if not project_id:
            logger.debug('project_id not set')
            return flask_socketio.disconnect()

        if not sio_is_authorized(["listen:jobs", project_id]):
            return flask_socketio.disconnect()

        flask_socketio.join_room(project_id)

    @sio.on('listen:build')
    def __listen_build(build_id):
        logger.debug('listen:build for %s', build_id)

        if not build_id:
            logger.debug('build_id not set')
            return flask_socketio.disconnect()

        try:
            uuid.UUID(build_id)
        except:
            logger.debug('build_id not a uuid')
            return flask_socketio.disconnect()

        if not sio_is_authorized(['listen:build', build_id]):
            return flask_socketio.disconnect()

        conn = dbpool.get()
        try:
            token = normalize_token(get_token())

            project_id = token['project']['id']

            build = conn.execute_one(
                '''
                SELECT id
                FROM build
                WHERE project_id = %s AND id = %s
            ''', [project_id, build_id])

            if not build:
                logger.debug('build does not belong to project')
                return flask_socketio.disconnect()
        except:
            logger.exception("Exception occured")
            return flask_socketio.disconnect()
        finally:
            dbpool.put(conn)

        flask_socketio.join_room(build_id)

    @sio.on('listen:console')
    def __listen_console(job_id):
        logger.debug('listen:console for %s', job_id)

        if not job_id:
            logger.debug('job_id not set')
            return flask_socketio.disconnect()

        try:
            uuid.UUID(job_id)
        except:
            logger.debug('job_id not a uuid')
            return flask_socketio.disconnect()

        if not sio_is_authorized(['listen:console', job_id]):
            return flask_socketio.disconnect()

        token = normalize_token(get_token())
        conn = dbpool.get()
        try:
            project_id = token['project']['id']

            build = conn.execute_one(
                '''
                SELECT id
                FROM job
                WHERE project_id = %s AND id = %s
            ''', [project_id, job_id])

            if not build:
                logger.debug('job does not belong to project')
                return flask_socketio.disconnect()
        except:
            logger.exception("Exception occured")
            return flask_socketio.disconnect()
        finally:
            dbpool.put(conn)

        flask_socketio.join_room(job_id)

    @sio.on('listen:dashboard-console')
    def __listen_dashboard_console(job_id):
        logger.debug('listen:dashboard-console for %s', job_id)

        if not job_id:
            logger.debug('job_id not set')
            return flask_socketio.disconnect()

        try:
            uuid.UUID(job_id)
        except:
            logger.debug('job_id not a uuid')
            return flask_socketio.disconnect()

        conn = dbpool.get()
        try:
            u = conn.execute_one_dict(
                '''
                SELECT p.public, j.project_id
                FROM project p
                INNER JOIN job j
                    ON j.project_id = p.id
                    AND j.id = %s
            ''', [job_id])

            if not u:
                logger.warn('job not found')
                return flask_socketio.disconnect()

            if not sio_is_authorized(
                ['listen:dashboard-console', u['project_id'], job_id]):
                return flask_socketio.disconnect()

        except:
            logger.exception("Exception occured")
            return flask_socketio.disconnect()
        finally:
            dbpool.put(conn)

        flask_socketio.join_room(job_id)

    def sio_is_authorized(path):
        g.db = dbpool.get()
        try:
            # Assemble Input Data for Open Policy Agent
            opa_input = {
                "input": {
                    "method": "WS",
                    "path": path,
                    "token": normalize_token(get_token())
                }
            }

            authorized = opa_do_auth(opa_input)
            if not authorized:
                logger.warn("Unauthorized socket.io access attempt")
                return False
            return True
        except RequestException as e:
            logger.error(e)
            return False
        finally:
            dbpool.put(g.db)
            g.db = None

    logger.info('Starting DB listeners')
    sio.start_background_task(listeners.job.listen, sio)
    sio.start_background_task(listeners.console.listen, sio, client_manager)

    logger.info('Starting repeated push of data to Open Policy Agent')
    opa_start_push_loop()

    port = int(os.environ.get('INFRABOX_PORT', 8080))
    logger.info('Starting Server on port %s', port)
    sio.run(app, host='0.0.0.0', port=port)
예제 #30
0
def main():  # pragma: no cover
    get_env('INFRABOX_SERVICE')
    get_env('INFRABOX_VERSION')
    get_env('INFRABOX_DATABASE_HOST')
    get_env('INFRABOX_DATABASE_USER')
    get_env('INFRABOX_DATABASE_PASSWORD')
    get_env('INFRABOX_DATABASE_PORT')
    get_env('INFRABOX_DATABASE_DB')

    get_env('INFRABOX_GITHUB_ENABLED')
    get_env('INFRABOX_GERRIT_ENABLED')
    get_env('INFRABOX_ACCOUNT_SIGNUP_ENABLED')
    get_env('INFRABOX_ACCOUNT_LDAP_ENABLED')
    get_env('INFRABOX_ROOT_URL')

    app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 1024

    client_manager = ClientManager()
    sio = flask_socketio.SocketIO(app,
                                  path='/api/dashboard/socket.io',
                                  async_mode='eventlet',
                                  client_manager=client_manager)

    @sio.on('connect')
    def __connect():
        try:
            get_token()
        except:
            logger.debug('disconnecting connection')
            return False

        return True

    @sio.on('listen:jobs')
    def __listen_build(project_id):
        logger.debug('listen:jobs for %s', project_id)

        if not project_id:
            logger.debug('project_id not set')
            return flask_socketio.disconnect()

        try:
            uuid.UUID(project_id)
        except:
            logger.debug('project_id not a uuid')
            return flask_socketio.disconnect()

        conn = dbpool.get()
        try:
            p = conn.execute_one_dict(
                '''
                SELECT public
                FROM project
                WHERE id = %s
            ''', [project_id])

            if not p['public']:
                token = get_token()
                if token['type'] == 'user':
                    user_id = token['user']['id']
                    collaborator = is_collaborator(user_id, project_id)

                    if not collaborator:
                        logger.warn('not a collaborator')
                        return flask_socketio.disconnect()
                else:
                    logger.debug('only user token allowed')
                    return flask_socketio.disconnect()

        finally:
            dbpool.put(conn)

        flask_socketio.join_room(project_id)

    @sio.on('listen:console')
    def __listen_console(job_id):
        logger.debug('listen:console for %s', job_id)

        if not job_id:
            logger.debug('job_id not set')
            return flask_socketio.disconnect()

        try:
            uuid.UUID(job_id)
        except:
            logger.debug('job_id not a uuid')
            return flask_socketio.disconnect()

        conn = dbpool.get()
        try:
            u = conn.execute_one_dict(
                '''
                SELECT p.public, j.project_id
                FROM project p
                INNER JOIN job j
                    ON j.project_id = p.id
                    AND j.id = %s
            ''', [job_id])

            if not u:
                logger.warn('job not found')
                return flask_socketio.disconnect()

            if not u['public']:
                token = get_token()
                if token['type'] == 'user':
                    user_id = token['user']['id']
                    collaborator = is_collaborator(user_id, u['project_id'])

                    if not collaborator:
                        logger.warn('not a collaborator')
                        return flask_socketio.disconnect()
                else:
                    logger.debug('only user token allowed')
                    return flask_socketio.disconnect()
        finally:
            dbpool.put(conn)

        flask_socketio.join_room(job_id)

    logger.info('Starting DB listeners')
    sio.start_background_task(dashboard_api.listeners.job.listen, sio)
    sio.start_background_task(dashboard_api.listeners.console.listen, sio,
                              client_manager)

    logger.info('Starting Server')
    sio.run(app, host='0.0.0.0', port=8080)