Skip to content

Commit

Permalink
Replace databasemanager by sqlalchemy
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd-ntrf committed May 24, 2022
1 parent 638128e commit 3f6cfae
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 277 deletions.
24 changes: 14 additions & 10 deletions mchub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
from flask import Flask, send_file, send_from_directory
from flask_cors import CORS


def create_app():
from . configuration import config
from . configuration.env import DIST_PATH
from . resources.magic_castle_api import MagicCastleAPI
from . resources.progress_api import ProgressAPI
from . resources.available_resources_api import AvailableResourcesApi
from . resources.user_api import UserAPI
from .configuration import config, DATABASE_FILENAME
from .configuration.env import DIST_PATH, DATABASE_PATH
from .database import db
from .resources.magic_castle_api import MagicCastleAPI
from .resources.progress_api import ProgressAPI
from .resources.available_resources_api import AvailableResourcesApi
from .resources.user_api import UserAPI

app = Flask(__name__)
app.config[
"SQLALCHEMY_DATABASE_URI"
] = f"sqlite:///{DATABASE_PATH}/{DATABASE_FILENAME}"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)

# Allows origins set in config file on all routes
CORS(
Expand Down Expand Up @@ -62,17 +69,14 @@ def create_app():
user_view = UserAPI.as_view("user")
app.add_url_rule("/api/users/me", view_func=user_view, methods=["GET"])


@app.route("/css/<path:path>")
def send_css_file(path):
return send_from_directory(os_path.join(DIST_PATH, "css"), path)


@app.route("/js/<path:path>")
def send_js_file(path):
return send_from_directory(os_path.join(DIST_PATH, "js"), path)


@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def catch_all(path):
Expand All @@ -85,4 +89,4 @@ def catch_all(path):
response.headers["Expires"] = "0"
return response

return app
return app
3 changes: 3 additions & 0 deletions mchub/database/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
17 changes: 10 additions & 7 deletions mchub/database/cleanup_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .. models.magic_castle.magic_castle import MagicCastle
from .. models.magic_castle.cluster_status_code import ClusterStatusCode
from . database_manager import DatabaseManager
from re import M
from ..models.magic_castle.magic_castle import MagicCastle, MagicCastleORM
from ..models.magic_castle.cluster_status_code import ClusterStatusCode
from . import db


class CleanupManager:
@classmethod
Expand All @@ -9,13 +11,14 @@ def clean_status(self):
back to a stable state. Applicable when booting the app
when and there is definetely no state running.
"""
with DatabaseManager.connect() as db_connection:
results = db_connection.execute("SELECT hostname FROM magic_castles").fetchall()
for result in results:
mc = MagicCastle(result[0])
results = MagicCastleORM.query.all()

for orm in results:
mc = MagicCastle(orm)
if mc.status == ClusterStatusCode.BUILD_RUNNING:
mc.status = ClusterStatusCode.BUILD_ERROR
elif mc.status == ClusterStatusCode.PLAN_RUNNING:
mc.status = ClusterStatusCode.CREATED
elif mc.status == ClusterStatusCode.DESTROY_RUNNING:
mc.status = ClusterStatusCode.DESTROY_ERROR
db.session.commit()
33 changes: 0 additions & 33 deletions mchub/database/database_manager.py

This file was deleted.

Loading

0 comments on commit 3f6cfae

Please sign in to comment.